coreapplicationuis/SysAp/Src/memorycard/sysapdrivelist.cpp
changeset 0 2e3d3ce01487
child 19 924385140d98
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 /*
       
     2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  SysAp drive list implementation
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDES
       
    21 #include <e32std.h>
       
    22 #include <f32file.h>
       
    23 #include <badesca.h>
       
    24 #include <StringLoader.h>
       
    25 #include <driveinfo.h>
       
    26 #include "sysapdrivelist.h"
       
    27 #include "SysAp.hrh"
       
    28 
       
    29 // CONSTANTS
       
    30 _LIT( KMemoryCardFSName, "Fat" );
       
    31 const TInt KNameFormatArrayLen = 2;
       
    32 #ifdef __WINS__
       
    33 const TInt KMountDelay = 1000000; // 1s
       
    34 #endif
       
    35 
       
    36 // ============================ MEMBER FUNCTIONS =============================
       
    37 // ---------------------------------------------------------------------------
       
    38 // CSysApDriveList::NewL
       
    39 // ---------------------------------------------------------------------------
       
    40 //
       
    41 CSysApDriveList* CSysApDriveList::NewL( RFs& aFs )
       
    42     {
       
    43     CSysApDriveList* self = new ( ELeave ) CSysApDriveList( aFs );
       
    44     CleanupStack::PushL( self );
       
    45     self->ConstructL();
       
    46     CleanupStack::Pop( self );
       
    47     return self;
       
    48     }
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // CSysApDriveList::ConstructL
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 void CSysApDriveList::ConstructL()
       
    55     {
       
    56     // Get the default memory card. If memory card is unavailable,
       
    57     // then get the next best suitable drive.
       
    58     TInt err( DriveInfo::GetDefaultDrive(
       
    59         DriveInfo::EDefaultRemovableMassStorage, iDefaultMemoryCard ) );
       
    60     if ( err != KErrNone )
       
    61         {
       
    62         err = DriveInfo::GetDefaultDrive(
       
    63             DriveInfo::EDefaultMassStorage, iDefaultMemoryCard );
       
    64         }
       
    65     if ( err != KErrNone )
       
    66         {
       
    67         err = DriveInfo::GetDefaultDrive(
       
    68             DriveInfo::EDefaultPhoneMemory, iDefaultMemoryCard );
       
    69         }
       
    70     if ( err != KErrNone )
       
    71         {
       
    72         err = DriveInfo::GetDefaultDrive(
       
    73             DriveInfo::EDefaultSystem, iDefaultMemoryCard );
       
    74         }
       
    75 
       
    76     TRACES( RDebug::Print(
       
    77         _L( "CSysApDriveList::ConstructL: iDefaultMemoryCard: %d, err: %d" ),
       
    78         iDefaultMemoryCard, err ) );
       
    79 
       
    80     User::LeaveIfError( err );
       
    81     }
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // CSysApDriveList::CSysApDriveList
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 CSysApDriveList::CSysApDriveList( RFs& aFs ) :
       
    88         iFs( aFs )
       
    89     {
       
    90     }
       
    91 
       
    92 // ---------------------------------------------------------------------------
       
    93 // CSysApDriveList::~CSysApDriveList
       
    94 // ---------------------------------------------------------------------------
       
    95 //
       
    96 CSysApDriveList::~CSysApDriveList()
       
    97     {
       
    98     TRACES( RDebug::Print( _L( "CSysApDriveList::~CSysApDriveList" ) ) );
       
    99     }
       
   100 
       
   101 // ---------------------------------------------------------------------------
       
   102 // CSysApDriveList::MountDrive
       
   103 // ---------------------------------------------------------------------------
       
   104 //
       
   105 TInt CSysApDriveList::MountDrive( TInt aDrive )
       
   106     {
       
   107     TRACES( RDebug::Print(
       
   108         _L( "CSysApDriveList::MountDrive: drive: %d" ), aDrive ) );
       
   109 
       
   110 #ifdef __WINS__ // Let's sleep a second in WINS
       
   111     User::After( KMountDelay );
       
   112 #endif
       
   113     TInt ret( iFs.MountFileSystem( KMemoryCardFSName, aDrive ) );
       
   114     if ( ret != KErrNone )
       
   115         {
       
   116         TRACES( RDebug::Print(
       
   117             _L( "CSysApDriveList::MountDrive: drive: %d, ret: %d" ),
       
   118             aDrive, ret ) );
       
   119         }
       
   120     return ret;
       
   121     }
       
   122 
       
   123 // ---------------------------------------------------------------------------
       
   124 // CSysApDriveList::DismountDrive
       
   125 // ---------------------------------------------------------------------------
       
   126 //
       
   127 TInt CSysApDriveList::DismountDrive( TInt aDrive )
       
   128     {
       
   129     TRACES( RDebug::Print(
       
   130         _L( "CSysApDriveList::DismountDrive: drive: %d" ), aDrive ) );
       
   131 
       
   132 #ifdef __WINS__ // Let's sleep a second in WINS
       
   133     User::After( KMountDelay );
       
   134 #endif
       
   135 	TRequestStatus status;
       
   136 
       
   137 	TRACES( RDebug::Print( _L("CSysApDriveList::Start DismountDrive: RFs::NotifyDismount()") ) );
       
   138 
       
   139 	iFs.NotifyDismount( aDrive, status, EFsDismountForceDismount );
       
   140 
       
   141 	User::WaitForRequest( status );
       
   142 
       
   143 	TRACES( RDebug::Print( _L("CSysApDriveList::DismountDrive: RFs::NotifyDismount() returned: %d"), status.Int() ) );
       
   144 
       
   145     return status.Int();
       
   146     }
       
   147 
       
   148 // ---------------------------------------------------------------------------
       
   149 // CSysApDriveList::MemoryCardStatus
       
   150 // ---------------------------------------------------------------------------
       
   151 //
       
   152 TSysApMemoryCardStatus CSysApDriveList::MemoryCardStatus(
       
   153         TInt aDrive )
       
   154     {
       
   155     UpdateDrive( aDrive, EFalse );
       
   156     return iStatusCache[ aDrive ].iStatus;
       
   157     }
       
   158 
       
   159 // ---------------------------------------------------------------------------
       
   160 // CSysApDriveList::DefaultMemoryCard
       
   161 // ---------------------------------------------------------------------------
       
   162 //
       
   163 TInt CSysApDriveList::DefaultMemoryCard() const
       
   164     {
       
   165     return iDefaultMemoryCard;
       
   166     }
       
   167 
       
   168 // ---------------------------------------------------------------------------
       
   169 // CSysApDriveList::GetMemoryCardsL
       
   170 // ---------------------------------------------------------------------------
       
   171 //
       
   172 void CSysApDriveList::GetMemoryCardsL(
       
   173         RArray< TMemoryCardEntry >& aMemoryCardArray,
       
   174         TUint aInclusionMask )
       
   175     {
       
   176     aMemoryCardArray.Reset();
       
   177     TMemoryCardEntry entry;
       
   178     for ( TInt i( 0 ); i < KMaxDrives; ++i )
       
   179         {
       
   180         UpdateDrive( i, EFalse );
       
   181         TSysApMemoryCardStatus memoryCardStatus( iStatusCache[ i ].iStatus );
       
   182         if ( memoryCardStatus != ESysApMemoryCardStatusNotKnown )
       
   183             {
       
   184             entry.iDrive = i;
       
   185             entry.iStatus = memoryCardStatus;
       
   186             entry.iDriveStatus = iStatusCache[ i ].iDriveStatus;
       
   187             TBool append( EFalse );
       
   188             if ( aInclusionMask == EIncludeAll )
       
   189                 {
       
   190                 append = ETrue;
       
   191                 }
       
   192             else if ( ( aInclusionMask & EIncludeInserted ) &&
       
   193                 entry.iStatus == ESysApMemoryCardInserted ||
       
   194                 entry.iStatus == ESysApMemoryCardLocked )
       
   195                 {
       
   196                 append = ETrue;
       
   197                 }
       
   198             else if ( ( aInclusionMask & EIncludeNonInserted ) &&
       
   199                 entry.iStatus == ESysApMemoryCardNotInserted )
       
   200                 {
       
   201                 append = ETrue;
       
   202                 }
       
   203             else if ( ( aInclusionMask & EIncludeLocked ) &&
       
   204                 entry.iStatus == ESysApMemoryCardLocked )
       
   205                 {
       
   206                 append = ETrue;
       
   207                 }
       
   208             if ( append )
       
   209                 {
       
   210                 aMemoryCardArray.AppendL( entry );
       
   211                 }
       
   212             }
       
   213         }
       
   214 
       
   215     TRACES( RDebug::Print(
       
   216         _L( "CSysApDriveList::GetMemoryCardsL: count: %d, inclusion: 0x%x" ),
       
   217         aMemoryCardArray.Count(), aInclusionMask ) );
       
   218     }
       
   219 
       
   220 // ---------------------------------------------------------------------------
       
   221 // CSysApDriveList::GetFormattedDriveNameLC
       
   222 // ---------------------------------------------------------------------------
       
   223 //
       
   224 HBufC* CSysApDriveList::GetFormattedDriveNameLC(
       
   225         const TInt aDrive,
       
   226         const TInt aTextIdForDefaultName,
       
   227         const TInt aTextIdForName ) const
       
   228     {
       
   229     TRACES( RDebug::Print(
       
   230         _L( "CSysApDriveList::GetFormattedDriveNameLC: drive: %d" ),
       
   231         aDrive ) );
       
   232 
       
   233     TDriveName drvLetter( TDriveUnit( aDrive ).Name() );
       
   234     TPtrC drvName;
       
   235     TVolumeInfo drvVolumeInfo;
       
   236     if ( iFs.Volume( drvVolumeInfo, aDrive ) == KErrNone )
       
   237         {
       
   238         drvName.Set( drvVolumeInfo.iName );
       
   239         }
       
   240     HBufC* ret = NULL;
       
   241     if ( aTextIdForName && ( drvName.Length() || !aTextIdForDefaultName ) )
       
   242         {
       
   243         CDesCArray* array = new( ELeave ) CDesCArrayFlat(
       
   244             KNameFormatArrayLen );
       
   245         CleanupStack::PushL( array );
       
   246         array->AppendL( drvLetter );
       
   247         array->AppendL( drvName );
       
   248         ret = StringLoader::LoadL( aTextIdForName, *array );
       
   249         CleanupStack::PopAndDestroy( array );
       
   250         }
       
   251     else if ( aTextIdForDefaultName )
       
   252         {
       
   253         ret = StringLoader::LoadL( aTextIdForDefaultName, drvLetter );
       
   254         }
       
   255     else
       
   256         {
       
   257         User::Leave( KErrNotFound );
       
   258         }
       
   259     CleanupStack::PushL( ret );
       
   260     return ret;
       
   261     }
       
   262 
       
   263 // ---------------------------------------------------------------------------
       
   264 // CSysApDriveList::UpdateDrive
       
   265 // ---------------------------------------------------------------------------
       
   266 //
       
   267 void CSysApDriveList::UpdateDrive( TInt aDrive, TBool aForceUpdate )
       
   268     {
       
   269     TDriveStateEntry& entry( iStatusCache[ aDrive ] );
       
   270     if ( !aForceUpdate && ( entry.iState & EStateStatusUpdated ) )
       
   271         {
       
   272         return;
       
   273         }
       
   274 
       
   275     TUint drvStatus( 0 );
       
   276     DriveInfo::GetDriveStatus( iFs, aDrive, drvStatus );
       
   277 
       
   278     TSysApMemoryCardStatus memoryCardStatus( ESysApMemoryCardStatusNotKnown );
       
   279     if ( drvStatus & DriveInfo::EDriveRemovable )
       
   280         {
       
   281         // Set memory card status for physically 
       
   282         // removable memory cards only
       
   283         if ( drvStatus & DriveInfo::EDriveLocked )
       
   284             {
       
   285             memoryCardStatus = ESysApMemoryCardLocked;
       
   286             }
       
   287         else if ( drvStatus & DriveInfo::EDrivePresent )
       
   288             {
       
   289             memoryCardStatus = ESysApMemoryCardInserted;
       
   290             }
       
   291         else
       
   292             {
       
   293             memoryCardStatus = ESysApMemoryCardNotInserted;
       
   294             }
       
   295         }
       
   296 
       
   297     TRACES( RDebug::Print(
       
   298         _L( "CSysApDriveList::UpdateDrive: drive: %d, drvStatus: 0x%x, memoryCardStatus: %d" ),
       
   299         aDrive, drvStatus, memoryCardStatus ) );
       
   300 
       
   301     entry.iStatus = memoryCardStatus;
       
   302     entry.iState |= EStateStatusUpdated;
       
   303     entry.iDriveStatus = drvStatus;
       
   304     }
       
   305 
       
   306 // ---------------------------------------------------------------------------
       
   307 // CSysApDriveList::Find
       
   308 // ---------------------------------------------------------------------------
       
   309 //
       
   310 TInt CSysApDriveList::Find(
       
   311         const RArray< TMemoryCardEntry >& aMemoryCardArray,
       
   312         TInt aDrive )
       
   313     {
       
   314     return aMemoryCardArray.Find( aDrive, TMemoryCardEntry::CompareFind );
       
   315     }
       
   316 
       
   317 // ---------------------------------------------------------------------------
       
   318 // CSysApDriveList::MarkDriveToEject
       
   319 // ---------------------------------------------------------------------------
       
   320 //
       
   321 void CSysApDriveList::MarkDriveToEject(
       
   322         TInt aDrive, TDriveEjectType aEjectUsed )
       
   323     {
       
   324     TRACES( RDebug::Print(
       
   325         _L( "CSysApDriveList::MarkDriveToEject: drive: %d, ejectType:%d" ),
       
   326         aDrive, aEjectUsed ) );
       
   327 
       
   328     ResetDriveToEject( aDrive );
       
   329     switch ( aEjectUsed )
       
   330         {
       
   331         case EEjectFromMenu:
       
   332             {
       
   333             iStatusCache[ aDrive ].iState |= EStateEjectedFromMenu;
       
   334             break;
       
   335             }
       
   336         case EEjectRemovedWithoutEject:
       
   337             {
       
   338             iStatusCache[ aDrive ].iState |= EStateRemovedWithoutEject;
       
   339             break;
       
   340             }
       
   341         default:
       
   342             {
       
   343             break;
       
   344             }
       
   345         }
       
   346     }
       
   347 
       
   348 // ---------------------------------------------------------------------------
       
   349 // CSysApDriveList::ResetDriveToEject
       
   350 // ---------------------------------------------------------------------------
       
   351 //
       
   352 void CSysApDriveList::ResetDriveToEject( TInt aDrive )
       
   353     {
       
   354     TRACES( RDebug::Print(
       
   355         _L( "CSysApDriveList::ResetDriveToEject: drive: %d" ), aDrive ) );
       
   356 
       
   357     iStatusCache[ aDrive ].iState &= ~EStateAllEjected;
       
   358     }
       
   359 
       
   360 // ---------------------------------------------------------------------------
       
   361 // CSysApDriveList::ResetDrivesToEject
       
   362 // ---------------------------------------------------------------------------
       
   363 //
       
   364 void CSysApDriveList::ResetDrivesToEject()
       
   365     {
       
   366     for ( TInt i( 0 ); i < KMaxDrives; ++i )
       
   367         {
       
   368         ResetDriveToEject( i );
       
   369         }
       
   370     }
       
   371 
       
   372 // ---------------------------------------------------------------------------
       
   373 // CSysApDriveList::DriveToEject
       
   374 // ---------------------------------------------------------------------------
       
   375 //
       
   376 TInt CSysApDriveList::DriveToEject( TDriveEjectType& aEjectUsed )
       
   377     {
       
   378     aEjectUsed = EEjectNone;
       
   379     TInt ret( KErrNotFound );
       
   380     for ( TInt i( 0 ); i < KMaxDrives; ++i )
       
   381         {
       
   382         TUint state( iStatusCache[ i ].iState );
       
   383         if ( state & EStateAllEjected )
       
   384             {
       
   385             // Store current drive to eject
       
   386             ret = i;
       
   387             if ( state & EStateEjectedFromMenu )
       
   388                 {
       
   389                 aEjectUsed = EEjectFromMenu;
       
   390                 }
       
   391             else if ( state & EStateRemovedWithoutEject )
       
   392                 {
       
   393                 aEjectUsed = EEjectRemovedWithoutEject;
       
   394                 }
       
   395             break;
       
   396             }
       
   397         }
       
   398     return ret;
       
   399     }
       
   400 
       
   401 // ---------------------------------------------------------------------------
       
   402 // CSysApDriveList::IsDriveToEject
       
   403 // ---------------------------------------------------------------------------
       
   404 //
       
   405 TBool CSysApDriveList::IsDriveToEject( TInt aDrive )
       
   406     {
       
   407     if ( iStatusCache[ aDrive ].iState & EStateAllEjected )
       
   408         {
       
   409         return ETrue;
       
   410         }
       
   411     return EFalse;
       
   412     }
       
   413 
       
   414 // ---------------------------------------------------------------------------
       
   415 // CSysApDriveList::MarkDrivesInsertBeepIgnored
       
   416 // ---------------------------------------------------------------------------
       
   417 //
       
   418 void CSysApDriveList::MarkDrivesInsertBeepIgnored(
       
   419       const RArray< TMemoryCardEntry >& aArray )
       
   420     {
       
   421     TInt count( aArray.Count() );
       
   422     for( TInt i( 0 ); i < count; ++i )
       
   423         {
       
   424         MarkDriveInsertBeepIgnored( aArray[ i ].iDrive );
       
   425         }
       
   426     }
       
   427 
       
   428 // ---------------------------------------------------------------------------
       
   429 // CSysApDriveList::MarkDriveInsertBeepIgnored
       
   430 // ---------------------------------------------------------------------------
       
   431 //
       
   432 void CSysApDriveList::MarkDriveInsertBeepIgnored( TInt aDrive )
       
   433     {
       
   434     TRACES( RDebug::Print(
       
   435         _L( "CSysApDriveList::MarkDriveInsertBeepIgnored: drive: %d" ),
       
   436         aDrive ) );
       
   437 
       
   438     iStatusCache[ aDrive ].iState |= EStateBeepIgnored;
       
   439     }
       
   440 
       
   441 // ---------------------------------------------------------------------------
       
   442 // CSysApDriveList::ResetDriveInsertBeepIgnored
       
   443 // ---------------------------------------------------------------------------
       
   444 //
       
   445 void CSysApDriveList::ResetDriveInsertBeepIgnored( TInt aDrive )
       
   446     {
       
   447     TRACES( RDebug::Print(
       
   448         _L( "CSysApDriveList::ResetDriveInsertBeepIgnored: drive: %d" ),
       
   449         aDrive ) );
       
   450 
       
   451     iStatusCache[ aDrive ].iState &= ~EStateBeepIgnored;
       
   452     }
       
   453 
       
   454 // ---------------------------------------------------------------------------
       
   455 // CSysApDriveList::ResetDrivesInsertBeepIgnored
       
   456 // ---------------------------------------------------------------------------
       
   457 //
       
   458 void CSysApDriveList::ResetDrivesInsertBeepIgnored()
       
   459     {
       
   460     for ( TInt i( 0 ); i < KMaxDrives; ++i )
       
   461         {
       
   462         ResetDriveInsertBeepIgnored( i );
       
   463         }
       
   464     }
       
   465 
       
   466 // ---------------------------------------------------------------------------
       
   467 // CSysApDriveList::IsDriveInsertBeepIgnored
       
   468 // ---------------------------------------------------------------------------
       
   469 //
       
   470 TBool CSysApDriveList::IsDriveInsertBeepIgnored( TInt aDrive )
       
   471     {
       
   472     if ( iStatusCache[ aDrive ].iState & EStateBeepIgnored )
       
   473         {
       
   474         return ETrue;
       
   475         }
       
   476     return EFalse;
       
   477     }
       
   478 
       
   479 // ---------------------------------------------------------------------------
       
   480 // CSysApDriveList::MarkDriveUnlockQueryShown
       
   481 // ---------------------------------------------------------------------------
       
   482 //
       
   483 void CSysApDriveList::MarkDriveUnlockQueryShown( TInt aDrive )
       
   484     {
       
   485     TRACES( RDebug::Print(
       
   486         _L( "CSysApDriveList::MarkDriveUnlockQueryShown: drive: %d" ),
       
   487         aDrive ) );
       
   488 
       
   489     iStatusCache[ aDrive ].iState |= EStateUnlockQueryShown;
       
   490     }
       
   491 
       
   492 // ---------------------------------------------------------------------------
       
   493 // CSysApDriveList::ResetDriveUnlockQueryShown
       
   494 // ---------------------------------------------------------------------------
       
   495 //
       
   496 void CSysApDriveList::ResetDriveUnlockQueryShown( TInt aDrive )
       
   497     {
       
   498     TRACES( RDebug::Print(
       
   499         _L( "CSysApDriveList::ResetDriveUnlockQueryShown: drive: %d" ),
       
   500         aDrive ) );
       
   501 
       
   502     iStatusCache[ aDrive ].iState &= ~EStateUnlockQueryShown;
       
   503     }
       
   504 
       
   505 // ---------------------------------------------------------------------------
       
   506 // CSysApDriveList::DriveToUnlock
       
   507 // ---------------------------------------------------------------------------
       
   508 //
       
   509 TInt CSysApDriveList::DriveToUnlock()
       
   510     {
       
   511     TInt ret( KErrNotFound );
       
   512     for ( TInt i( 0 ); i < KMaxDrives; ++i )
       
   513         {
       
   514         TDriveStateEntry& entry( iStatusCache[ i ] );
       
   515         if ( entry.iStatus == ESysApMemoryCardLocked &&
       
   516             !( entry.iState & EStateUnlockQueryShown ) )
       
   517             {
       
   518             ret = i;
       
   519             break;
       
   520             }
       
   521         }
       
   522     return ret;
       
   523     }
       
   524 
       
   525 // ---------------------------------------------------------------------------
       
   526 // CSysApDriveList::TMemoryCardEntry::CompareFind
       
   527 // ---------------------------------------------------------------------------
       
   528 //
       
   529 TInt CSysApDriveList::TMemoryCardEntry::CompareFind(
       
   530         const TInt* aDrive, const TMemoryCardEntry& aEntry )
       
   531     {
       
   532     return *aDrive == aEntry.iDrive;
       
   533     }
       
   534 
       
   535 // ---------------------------------------------------------------------------
       
   536 // CSysApDriveList::NonUsbDriveCount
       
   537 // ---------------------------------------------------------------------------
       
   538 //
       
   539 TInt CSysApDriveList::NonUsbDriveCount( const RArray< TMemoryCardEntry >& aDriveArray )
       
   540     {
       
   541     TInt ret( 0 );
       
   542     TInt count( aDriveArray.Count() );
       
   543     for( TInt i( 0 ); i < count; ++i )
       
   544         {
       
   545         if ( !( aDriveArray[ i ].iDriveStatus & DriveInfo::EDriveUsbMemory ) )
       
   546             {
       
   547             ++ret;
       
   548             }
       
   549         }
       
   550     TRACES( RDebug::Print( _L( "CSysApDriveList::NonUsbDriveCount: %d" ), ret ) );
       
   551     return ret;
       
   552     }
       
   553 
       
   554 // End of File