filemanager/Engine/src/CFileManagerRemovableDriveHandler.cpp
changeset 14 1957042d8c7e
parent 1 d1daf54a55b5
child 16 ada7962b4308
equal deleted inserted replaced
1:d1daf54a55b5 14:1957042d8c7e
     1 /*
       
     2 * Copyright (c) 2006-2008 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:  Wraps removable drive functionality
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <FileManagerEngine.rsg>
       
    22 #include "CFileManagerRemovableDriveHandler.h"
       
    23 #include "MFileManagerProcessObserver.h"
       
    24 #include "CFileManagerUtils.h"
       
    25 #include "CFileManagerEngine.h"
       
    26 #include <coreapplicationuisdomainpskeys.h>
       
    27 #include <coemain.h>
       
    28 #include <apgwgnam.h>
       
    29 #include <apgtask.h>
       
    30 #include <bautils.h>
       
    31 #include <tz.h>
       
    32 #include <babackup.h>
       
    33 #include <pathinfo.h>
       
    34 #include <sysutil.h>
       
    35 #ifdef RD_MULTIPLE_DRIVE
       
    36 #include <driveinfo.h>
       
    37 #endif // RD_MULTIPLE_DRIVE
       
    38 #include <e32property.h>
       
    39 #include <centralrepository.h>
       
    40 #include "CMMCScBkupEngine.h"
       
    41 #include "MMCScBkupOperations.h"
       
    42 #include "CMMCScBkupOperationParameters.h"
       
    43 #include "CFileManagerItemProperties.h"
       
    44 #include "CFileManagerBackupSettings.h"
       
    45 #include "FileManagerPrivateCRKeys.h"
       
    46 #include "BkupEngine.hrh"
       
    47 #include "FileManagerDebug.h"
       
    48 #include "CFileManagerCommonDefinitions.h"
       
    49 #include "FileManagerUID.h"
       
    50 #include "filemanagerprivatepskeys.h"
       
    51 
       
    52 
       
    53 // CONSTANTS
       
    54 const TInt KEjectScanInterval = 1000000; // 1 second
       
    55 const TInt KEjectScanRoundsMax = 7;
       
    56 NONSHARABLE_CLASS(TMaskLookup)
       
    57     {
       
    58     public:
       
    59         TUint32 iBkupMask;
       
    60         TUint32 iFmgrMask;
       
    61     };
       
    62 
       
    63 const TMaskLookup KMaskLookup[] = {
       
    64     { EBUCatSettings,  EFileManagerBackupContentSettings  },
       
    65     { EBUCatMessages,  EFileManagerBackupContentMessages  },
       
    66     { EBUCatContacts,  EFileManagerBackupContentContacts  },
       
    67     { EBUCatCalendar,  EFileManagerBackupContentCalendar  },
       
    68     { EBUCatBookmarks, EFileManagerBackupContentBookmarks },
       
    69     { EBUCatUserFiles, EFileManagerBackupContentUserFiles }
       
    70 };
       
    71 const TUint32 KMaskLookupLen =
       
    72     sizeof( KMaskLookup ) / sizeof( KMaskLookup[ 0 ] );
       
    73 
       
    74 const TInt KForcedFormatTimeout = 1000000;
       
    75 const TInt KAppCloseTimeout = 1000000;
       
    76 const TInt KFileManagerAppUid = 0x101F84EB;
       
    77 
       
    78 NONSHARABLE_CLASS(TFileManagerVolumeNameStore)
       
    79     {
       
    80 public:
       
    81     inline TFileManagerVolumeNameStore() : iDrive( KErrNotFound ), iName( KNullDesC ) {};
       
    82 
       
    83     TInt iDrive; // The drive using the store
       
    84     TFileName iName; // Use the same length than TVolumeInfo
       
    85     };
       
    86 
       
    87 typedef TPckg< TFileManagerVolumeNameStore > TFileManagerVolumeNameStorePckg;
       
    88 
       
    89 
       
    90 // ======== MEMBER FUNCTIONS ========
       
    91 
       
    92 // ---------------------------------------------------------------------------
       
    93 // CFileManagerRemovableDriveHandler::CFileManagerRemovableDriveHandler
       
    94 // ---------------------------------------------------------------------------
       
    95 //
       
    96 CFileManagerRemovableDriveHandler::CFileManagerRemovableDriveHandler(
       
    97         RFs& aFs,
       
    98         CFileManagerUtils& aUtils,
       
    99         CFileManagerEngine& aEngine ) :
       
   100     CActive( CActive::EPriorityStandard ),
       
   101     iFs( aFs ),
       
   102     iUtils( aUtils ),
       
   103     iEngine( aEngine )
       
   104     {
       
   105     }
       
   106 
       
   107 // ---------------------------------------------------------------------------
       
   108 // CFileManagerRemovableDriveHandler::ConstructL
       
   109 // ---------------------------------------------------------------------------
       
   110 //
       
   111 void CFileManagerRemovableDriveHandler::ConstructL()
       
   112     {
       
   113     CActiveScheduler::Add( this );
       
   114     iBkupEngine = CMMCScBkupEngine::NewL( iFs );
       
   115     PublishBurStatus( EFileManagerBkupStatusUnset );
       
   116     }
       
   117 
       
   118 // ---------------------------------------------------------------------------
       
   119 // CFileManagerRemovableDriveHandler::NewL
       
   120 // ---------------------------------------------------------------------------
       
   121 //
       
   122 CFileManagerRemovableDriveHandler* CFileManagerRemovableDriveHandler::NewL(
       
   123         RFs& aFs,
       
   124         CFileManagerUtils& aUtils,
       
   125         CFileManagerEngine& aEngine )
       
   126     {
       
   127     CFileManagerRemovableDriveHandler* self =
       
   128         new( ELeave ) CFileManagerRemovableDriveHandler(
       
   129             aFs, aUtils, aEngine );
       
   130 
       
   131     CleanupStack::PushL( self );
       
   132     self->ConstructL();
       
   133     CleanupStack::Pop( self );
       
   134 
       
   135     return self;
       
   136     }
       
   137 
       
   138 // ---------------------------------------------------------------------------
       
   139 // CFileManagerRemovableDriveHandler::~CFileManagerRemovableDriveHandler
       
   140 // ---------------------------------------------------------------------------
       
   141 // 
       
   142 CFileManagerRemovableDriveHandler::~CFileManagerRemovableDriveHandler()
       
   143     {
       
   144     Cancel();
       
   145     delete iBSWrapper;
       
   146     delete iEjectScanPeriodic;
       
   147     delete iBkupEngine;
       
   148     iFormatter.Close();
       
   149     PublishBurStatus( EFileManagerBkupStatusUnset );
       
   150     }
       
   151 
       
   152 // ---------------------------------------------------------------------------
       
   153 // CFileManagerRemovableDriveHandler::EjectScanAndShutdownApps
       
   154 // ---------------------------------------------------------------------------
       
   155 //
       
   156 TInt CFileManagerRemovableDriveHandler::EjectScanAndShutdownApps( TAny* ptr )
       
   157     {
       
   158     CFileManagerRemovableDriveHandler* self = 
       
   159         static_cast< CFileManagerRemovableDriveHandler* >( ptr );
       
   160     TRAPD( err, self->DoEjectScanAndShutdownL() );
       
   161     if ( err != KErrNone )
       
   162         {
       
   163         self->EjectComplete( err );
       
   164         }
       
   165     return KErrNone;
       
   166     }
       
   167 
       
   168 // ---------------------------------------------------------------------------
       
   169 // CFileManagerRemovableDriveHandler::DoEjectScanAndShutdownL
       
   170 // ---------------------------------------------------------------------------
       
   171 //
       
   172 void CFileManagerRemovableDriveHandler::DoEjectScanAndShutdownL()
       
   173     {
       
   174     RWsSession wsSession;
       
   175     User::LeaveIfError( wsSession.Connect() );
       
   176     CleanupClosePushL( wsSession );
       
   177 
       
   178     const TInt KDefaultWgIdArraySize = 4;
       
   179     CArrayFixFlat<TInt>* wgIds = new( ELeave ) CArrayFixFlat< TInt >(
       
   180         KDefaultWgIdArraySize );
       
   181     CleanupStack::PushL( wgIds );
       
   182     User::LeaveIfError( wsSession.WindowGroupList( 0, wgIds ) );
       
   183     TInt last( wgIds->Count() - 1 );
       
   184     TInt appsToShutDown( 0 );
       
   185 
       
   186     for ( TInt i( last ); i >= 0; i-- )
       
   187         {
       
   188         CApaWindowGroupName* doomedApp =
       
   189             CApaWindowGroupName::NewLC( wsSession, wgIds->At( i ) );
       
   190         TBool systemApp( doomedApp->IsSystem() );
       
   191         TBool hiddenApp( doomedApp->Hidden() );
       
   192 
       
   193         if ( !systemApp && !hiddenApp && doomedApp->AppUid().iUid != KFileManagerAppUid  )
       
   194             {
       
   195             appsToShutDown++;
       
   196             TApaTask* task = new (ELeave) TApaTask( wsSession );
       
   197             CleanupDeletePushL( task );
       
   198             task->SetWgId( wgIds->At( i ) );
       
   199 
       
   200             if ( !iEjectScanRounds )
       
   201                 {
       
   202                  // applications are kindly requested to close themselves
       
   203                  // on the first round
       
   204                 task->EndTask();
       
   205                 }
       
   206             else if ( iEjectScanRounds >= KEjectScanRoundsMax )
       
   207                 {
       
   208                 task->KillTask();
       
   209                 }
       
   210             CleanupStack::PopAndDestroy( task );
       
   211             }
       
   212         CleanupStack::PopAndDestroy( doomedApp );
       
   213         }
       
   214     CleanupStack::PopAndDestroy( wgIds );
       
   215 
       
   216     if ( !appsToShutDown || iEjectScanRounds >= KEjectScanRoundsMax )
       
   217         {
       
   218         EjectComplete( KErrNone );
       
   219         }
       
   220     iEjectScanRounds++;
       
   221     
       
   222     CleanupStack::PopAndDestroy( &wsSession );
       
   223     }
       
   224 
       
   225 // ---------------------------------------------------------------------------
       
   226 // CFileManagerRemovableDriveHandler::EjectComplete
       
   227 // ---------------------------------------------------------------------------
       
   228 //
       
   229 void CFileManagerRemovableDriveHandler::EjectComplete( TInt aErr )
       
   230     {
       
   231     delete iEjectScanPeriodic;
       
   232     iEjectScanPeriodic = NULL;
       
   233     iLastError = aErr;
       
   234 
       
   235     TRAP_IGNORE( InformFinishL() );
       
   236 
       
   237     RProperty::Set(
       
   238         KPSUidCoreApplicationUIs,
       
   239         KCoreAppUIsMmcRemovedWithoutEject,
       
   240         ECoreAppUIsEjectCommandNotUsed );
       
   241     }
       
   242 
       
   243 // ---------------------------------------------------------------------------
       
   244 // CFileManagerRemovableDriveHandler::InformStartL
       
   245 // ---------------------------------------------------------------------------
       
   246 // 
       
   247 void CFileManagerRemovableDriveHandler::InformStartL( TInt aTotalCount )
       
   248     {
       
   249     iLastError = KErrNone;
       
   250     if ( iObserver )
       
   251         {
       
   252         iObserver->ProcessStartedL( iProcess,  aTotalCount );
       
   253         }
       
   254     }
       
   255 
       
   256 // ---------------------------------------------------------------------------
       
   257 // CFileManagerRemovableDriveHandler::InformUpdateL
       
   258 // ---------------------------------------------------------------------------
       
   259 // 
       
   260 void CFileManagerRemovableDriveHandler::InformUpdateL( TInt aCount )
       
   261     {
       
   262     if ( iObserver )
       
   263         {
       
   264         iObserver->ProcessAdvanceL( aCount );
       
   265         }
       
   266     }
       
   267 
       
   268 // ---------------------------------------------------------------------------
       
   269 // CFileManagerRemovableDriveHandler::InformFinishL
       
   270 // ---------------------------------------------------------------------------
       
   271 // 
       
   272 void CFileManagerRemovableDriveHandler::InformFinishL()
       
   273     {
       
   274     iEngine.ClearDriveInfo();
       
   275     PublishBurStatus( EFileManagerBkupStatusUnset );
       
   276     iProcess = MFileManagerProcessObserver::ENoProcess;
       
   277     if ( iObserver )
       
   278         {
       
   279         iObserver->ProcessFinishedL( iLastError, KNullDesC );
       
   280         }
       
   281     }
       
   282 
       
   283 // ---------------------------------------------------------------------------
       
   284 // CFileManagerRemovableDriveHandler::InformError
       
   285 // ---------------------------------------------------------------------------
       
   286 // 
       
   287 void CFileManagerRemovableDriveHandler::InformError( TInt aErr )
       
   288     {
       
   289     iLastError = aErr;
       
   290     if ( iObserver )
       
   291         {
       
   292         iObserver->Error( aErr );
       
   293         }
       
   294     }
       
   295 
       
   296 // ---------------------------------------------------------------------------
       
   297 // CFileManagerRemovableDriveHandler::HandleBkupEngineEventL
       
   298 // ---------------------------------------------------------------------------
       
   299 // 
       
   300 TInt CFileManagerRemovableDriveHandler::HandleBkupEngineEventL(
       
   301         MMMCScBkupEngineObserver::TEvent aEvent, TInt aAssociatedData )
       
   302     {
       
   303     TInt ret( KErrNone );
       
   304     switch( aEvent )
       
   305         {
       
   306         case MMMCScBkupEngineObserver::ECommonOperationStarting:
       
   307             {
       
   308             iFinalValue = KMaxTInt;
       
   309             InformStartL( KMaxTInt );
       
   310             break;
       
   311             }
       
   312         case MMMCScBkupEngineObserver::ECommonSizeOfTaskUnderstood:
       
   313             {
       
   314             iFinalValue = aAssociatedData;
       
   315             InformStartL( aAssociatedData );
       
   316             break;
       
   317             }
       
   318         case MMMCScBkupEngineObserver::ECommonOperationPrepareEnded:
       
   319             {
       
   320             PublishBurStatus( EFileManagerBkupStatusUnset );
       
   321             // In order to show finished dialog prior SysAp's note,
       
   322             // inform observer already at prepare-ended state.
       
   323             if( iProcess == MFileManagerProcessObserver::ERestoreProcess )
       
   324                 {
       
   325                 if ( iFinalValue )
       
   326                     {
       
   327                     InformUpdateL( iFinalValue );
       
   328                     }
       
   329                 InformFinishL();
       
   330                 }
       
   331             break;
       
   332             }
       
   333         case MMMCScBkupEngineObserver::ECommonOperationEnded:
       
   334             {
       
   335             PublishBurStatus( EFileManagerBkupStatusUnset );
       
   336             if( iProcess != MFileManagerProcessObserver::ERestoreProcess )
       
   337                 {
       
   338                 if ( iFinalValue )
       
   339                     {
       
   340                     InformUpdateL( iFinalValue );
       
   341                     }
       
   342                 InformFinishL();
       
   343                 }
       
   344             break;
       
   345             }
       
   346         case MMMCScBkupEngineObserver::ECommonOperationError:
       
   347             {
       
   348             iLastError = aAssociatedData;
       
   349             break;
       
   350             }
       
   351         case MMMCScBkupEngineObserver::ECommonProgress:
       
   352             {
       
   353             InformUpdateL( aAssociatedData );
       
   354             break;
       
   355             }
       
   356         case MMMCScBkupEngineObserver::EBackupAnalysingData:
       
   357             {
       
   358             if ( iObserver &&
       
   359                 iProcess == MFileManagerProcessObserver::EBackupProcess )
       
   360                 {
       
   361                 ret = iObserver->NotifyL(
       
   362                     MFileManagerProcessObserver::ENotifyBackupMemoryLow,
       
   363                     aAssociatedData );
       
   364                 }
       
   365             break;
       
   366             }
       
   367         default:
       
   368             {
       
   369             break;
       
   370             }
       
   371         }
       
   372     return ret;
       
   373     }
       
   374 
       
   375 // ---------------------------------------------------------------------------
       
   376 // CFileManagerRemovableDriveHandler::SetObserver
       
   377 // ---------------------------------------------------------------------------
       
   378 // 
       
   379 void CFileManagerRemovableDriveHandler::SetObserver(
       
   380         MFileManagerProcessObserver* aObserver )
       
   381     {
       
   382     iObserver = aObserver;
       
   383     }
       
   384 
       
   385 // ---------------------------------------------------------------------------
       
   386 // CFileManagerRemovableDriveHandler::BackupFileNameLC
       
   387 // ---------------------------------------------------------------------------
       
   388 // 
       
   389 HBufC* CFileManagerRemovableDriveHandler::BackupFileNameLC(
       
   390         TBool aFullPath ) const
       
   391     {
       
   392     HBufC* file = CCoeEnv::Static()->AllocReadResourceLC(
       
   393         R_TEXT_MMC_BACKUP_FILE );
       
   394     if ( !aFullPath )
       
   395         {
       
   396         return file; // Get just name
       
   397         }
       
   398     // Get full path
       
   399 #ifdef RD_MULTIPLE_DRIVE
       
   400     TInt drive( 0 );
       
   401     User::LeaveIfError( DriveInfo::GetDefaultDrive(
       
   402         DriveInfo::EDefaultRemovableMassStorage, drive ) );
       
   403     TDriveUnit driveUnit( drive );
       
   404 #else // RD_MULTIPLE_DRIVE
       
   405     TDriveUnit driveUnit( PathInfo::MemoryCardRootPath() );
       
   406 #endif // RD_MULTIPLE_DRIVE
       
   407     HBufC* fileAndPath = HBufC::NewLC( KMaxFileName );
       
   408     TPtr ptr( fileAndPath->Des() );
       
   409     TPtr filePtr( file->Des() );
       
   410     ptr.Copy( driveUnit.Name() );
       
   411     ptr.Append( filePtr );
       
   412     CleanupStack::Pop( fileAndPath );
       
   413     CleanupStack::PopAndDestroy( file );
       
   414     CleanupStack::PushL( fileAndPath );
       
   415     return fileAndPath;
       
   416     }
       
   417 
       
   418 // ---------------------------------------------------------------------------
       
   419 // CFileManagerRemovableDriveHandler::StartFormatL()
       
   420 // ---------------------------------------------------------------------------
       
   421 //  
       
   422 void CFileManagerRemovableDriveHandler::StartFormatL(
       
   423         const TInt aDrive )
       
   424     {
       
   425     TInt err( KErrNone );
       
   426 
       
   427     iDrive = aDrive;
       
   428     iProcess = MFileManagerProcessObserver::EFormatProcess;
       
   429     iFinalValue = 0;
       
   430 
       
   431     TRAP( err, InformStartL( 0 ) );
       
   432     if ( err == KErrNone )
       
   433         {
       
   434         // Close apps and then start format
       
   435         TRAP( err, CloseAppsL() );
       
   436         }
       
   437     if ( err != KErrNone )
       
   438         {
       
   439         EndFormatProcessL( err );
       
   440         }
       
   441     }
       
   442 
       
   443 // ---------------------------------------------------------------------------
       
   444 // CFileManagerRemovableDriveHandler::StartEjectL()
       
   445 // ---------------------------------------------------------------------------
       
   446 //  
       
   447 void CFileManagerRemovableDriveHandler::StartEjectL()
       
   448     {
       
   449     TRAPD( err, StartEjectScanL() );
       
   450     if ( err != KErrNone )
       
   451         {
       
   452         iProcess = MFileManagerProcessObserver::ENoProcess;
       
   453         User::Leave( err );
       
   454         }
       
   455     }
       
   456 
       
   457 // ---------------------------------------------------------------------------
       
   458 // CFileManagerRemovableDriveHandler::StartEjectScanL()
       
   459 // ---------------------------------------------------------------------------
       
   460 //  
       
   461 void CFileManagerRemovableDriveHandler::StartEjectScanL()
       
   462     {
       
   463     iProcess = MFileManagerProcessObserver::EEjectProcess;
       
   464     InformStartL( 0 );
       
   465 
       
   466     RProperty::Set(
       
   467         KPSUidCoreApplicationUIs,
       
   468         KCoreAppUIsMmcRemovedWithoutEject,
       
   469         ECoreAppUIsEjectCommandUsed );
       
   470 
       
   471     iEjectScanRounds = 0;
       
   472     iEjectScanPeriodic = CPeriodic::NewL( CActive::EPriorityLow );
       
   473     iEjectScanPeriodic->Start(
       
   474         KEjectScanInterval,
       
   475         KEjectScanInterval,
       
   476         TCallBack( EjectScanAndShutdownApps, this ) );
       
   477     }
       
   478 
       
   479 // ---------------------------------------------------------------------------
       
   480 // CFileManagerRemovableDriveHandler::CancelProcess()
       
   481 // ---------------------------------------------------------------------------
       
   482 //    
       
   483 void CFileManagerRemovableDriveHandler::CancelProcess()
       
   484     {
       
   485     switch( iProcess )
       
   486         {
       
   487         case MFileManagerProcessObserver::EFormatProcess:
       
   488             {
       
   489             Cancel();
       
   490             break;
       
   491             }
       
   492         case MFileManagerProcessObserver::EBackupProcess: // FALLTHROUGH
       
   493         case MFileManagerProcessObserver::ERestoreProcess:
       
   494         case MFileManagerProcessObserver::ESchBackupProcess:
       
   495             {
       
   496             PublishBurStatus( EFileManagerBkupStatusUnset );
       
   497             iBkupEngine->CancelOperation();
       
   498             break;
       
   499             }
       
   500         default:
       
   501             {
       
   502             break;
       
   503             }
       
   504         }
       
   505     }
       
   506 
       
   507 // ---------------------------------------------------------------------------
       
   508 // CFileManagerRemovableDriveHandler::BackupFileExistsL()
       
   509 // ---------------------------------------------------------------------------
       
   510 //   
       
   511 TBool CFileManagerRemovableDriveHandler::BackupFileExistsL(
       
   512         const TInt /*aDrive*/ )
       
   513     {
       
   514     HBufC* backupFile = BackupFileNameLC( ETrue );
       
   515     TBool ret( iBkupEngine->ValidArchiveForRestore( *backupFile ) );
       
   516     CleanupStack::PopAndDestroy( backupFile );
       
   517     return ret;
       
   518     }
       
   519 
       
   520 // ---------------------------------------------------------------------------
       
   521 // CFileManagerRemovableDriveHandler::StartBackupL()
       
   522 // ---------------------------------------------------------------------------
       
   523 //  
       
   524 void CFileManagerRemovableDriveHandler::StartBackupL(
       
   525         MFileManagerProcessObserver::TFileManagerProcess aProcess )
       
   526     {
       
   527     if ( aProcess != MFileManagerProcessObserver::EBackupProcess &&
       
   528         aProcess != MFileManagerProcessObserver::ESchBackupProcess )
       
   529         {
       
   530         User::Leave( KErrNotSupported );
       
   531         }
       
   532 
       
   533     CCoeEnv* coeEnv = CCoeEnv::Static();
       
   534 
       
   535     // Create backup params - ownership is transferred to
       
   536     // secure backup engine
       
   537     TResourceReader driveReader;
       
   538     coeEnv->CreateResourceReaderLC(
       
   539         driveReader,
       
   540         R_FILEMANAGER_BACKUP_RESTORE_DRIVES_AND_OPERATIONS );
       
   541     TResourceReader categoryReader;
       
   542     coeEnv->CreateResourceReaderLC(
       
   543         categoryReader,
       
   544         R_FILEMANAGER_BACKUP_CATEGORIES );
       
   545 
       
   546 #ifdef RD_FILE_MANAGER_BACKUP
       
   547 
       
   548     CFileManagerBackupSettings& settings( iEngine.BackupSettingsL() );
       
   549     TUint32 bkupContent( FmgrToBkupMask( settings.Content() ) );
       
   550     TInt drive( settings.TargetDrive() );
       
   551 
       
   552     CMMCScBkupOpParamsBackupFull* params =
       
   553         CMMCScBkupOpParamsBackupFull::NewL(
       
   554             driveReader,
       
   555             categoryReader,
       
   556             TDriveNumber( drive ),
       
   557             bkupContent );
       
   558 
       
   559 #else // RD_FILE_MANAGER_BACKUP
       
   560 
       
   561     HBufC* backupFile = BackupFileNameLC( ETrue );
       
   562     TInt drive = TDriveUnit( *backupFile );
       
   563     CleanupStack::PopAndDestroy( backupFile );
       
   564 
       
   565     CMMCScBkupOpParamsBackupFull* params =
       
   566         CMMCScBkupOpParamsBackupFull::NewL( driveReader,
       
   567             categoryReader,
       
   568             TDriveNumber( drive ),
       
   569             EBUCatAllInOne );
       
   570 
       
   571 #endif // RD_FILE_MANAGER_BACKUP
       
   572 
       
   573     CleanupStack::PopAndDestroy(); // categoryReader
       
   574     CleanupStack::PopAndDestroy(); // driveReader
       
   575 
       
   576     CleanupStack::PushL( params );
       
   577     TBool diskFull( SysUtil::DiskSpaceBelowCriticalLevelL( &iFs, 0, drive ) );
       
   578     CleanupStack::Pop( params );
       
   579 
       
   580     if ( diskFull )
       
   581         {
       
   582         iProcess = aProcess;
       
   583         TRAP_IGNORE( InformStartL( KMaxTInt ) );
       
   584         iLastError = KErrDiskFull;
       
   585         TRAP_IGNORE( InformFinishL() );
       
   586         iProcess = MFileManagerProcessObserver::ENoProcess;
       
   587         delete params;
       
   588         }
       
   589     else
       
   590         {
       
   591         // Start the process - engine owns the parameters immediately
       
   592         iProcess = aProcess;
       
   593         
       
   594         PublishBurStatus( EFileManagerBkupStatusBackup );
       
   595         TRAPD( err, iBkupEngine->StartOperationL(
       
   596             EMMCScBkupOperationTypeFullBackup, *this, params ) );
       
   597         if ( err != KErrNone )
       
   598             {
       
   599             PublishBurStatus( EFileManagerBkupStatusUnset );
       
   600             iProcess = MFileManagerProcessObserver::ENoProcess;
       
   601             User::Leave( err );
       
   602             }
       
   603         }
       
   604     }
       
   605 
       
   606 // ---------------------------------------------------------------------------
       
   607 // CFileManagerRemovableDriveHandler::StartRestoreL()
       
   608 // ---------------------------------------------------------------------------
       
   609 //  
       
   610 void CFileManagerRemovableDriveHandler::StartRestoreL()
       
   611     {
       
   612     TBool diskFull( SysUtil::DiskSpaceBelowCriticalLevelL(
       
   613         &iFs, 0, KFmgrSystemDrive ) );
       
   614     if ( diskFull )
       
   615         {
       
   616         iProcess = MFileManagerProcessObserver::ERestoreProcess;
       
   617         TRAP_IGNORE( InformStartL( KMaxTInt ) );
       
   618         iLastError = KErrDiskFull;
       
   619         TRAP_IGNORE( InformFinishL() );
       
   620         iProcess = MFileManagerProcessObserver::ENoProcess;
       
   621         return;
       
   622         }
       
   623 
       
   624     CFileManagerBackupSettings& bkupSettings( iEngine.BackupSettingsL() );
       
   625 
       
   626     // Create restore params - ownership is transferred to
       
   627     // secure backup engine
       
   628     TResourceReader driveReader;
       
   629     CCoeEnv::Static()->CreateResourceReaderLC(
       
   630         driveReader,
       
   631         R_FILEMANAGER_BACKUP_RESTORE_DRIVES_AND_OPERATIONS );
       
   632 
       
   633 #ifdef RD_FILE_MANAGER_BACKUP
       
   634 
       
   635     CMMCScBkupOpParamsRestoreFull* params =
       
   636         CMMCScBkupOpParamsRestoreFull::NewL(
       
   637             driveReader,
       
   638             EBUCatAllSeparately );
       
   639     CleanupStack::PopAndDestroy(); // driveReader
       
   640     CleanupStack::PushL( params );
       
   641 
       
   642     // Get list of all archives
       
   643     RPointerArray< CMMCScBkupArchiveInfo > archives;
       
   644     TCleanupItem cleanupItem( ResetAndDestroyArchives, &archives );
       
   645     CleanupStack::PushL( cleanupItem );
       
   646     iBkupEngine->ListArchivesL(
       
   647         archives,
       
   648         params,
       
   649         bkupSettings.AllowedDriveAttMatchMask() );
       
   650 
       
   651     // Get user set restore selection
       
   652     RArray< CFileManagerRestoreSettings::TInfo > selection;
       
   653     CleanupClosePushL( selection );
       
   654     CFileManagerRestoreSettings& rstSettings( iEngine.RestoreSettingsL() );
       
   655     rstSettings.GetSelectionL( selection );
       
   656 
       
   657     // Remove non user selected archives
       
   658     TInt i( 0 );
       
   659     while ( i < archives.Count() )
       
   660         {
       
   661         TBool remove( ETrue );
       
   662 
       
   663         // Compare archives category and drive
       
   664         CMMCScBkupArchiveInfo* archiveInfo = archives[ i ];
       
   665         TUint32 fmgrContent(
       
   666             BkupToFmgrMask( archiveInfo->Category().iFlags ) );
       
   667         TInt drive( archiveInfo->Drive() );
       
   668 
       
   669         TInt count( selection.Count() );
       
   670         for( TInt j( 0 ); j < count; ++j )
       
   671             {
       
   672             const CFileManagerRestoreSettings::TInfo& info( selection[ j ] );
       
   673             if ( ( drive == info.iDrive ) && ( fmgrContent & info.iContent ) )
       
   674                 {
       
   675                 // Found user selected archive
       
   676                 // Do not check this archive again
       
   677                 selection.Remove( j );
       
   678                 remove = EFalse;
       
   679                 break;
       
   680                 }
       
   681             }
       
   682         if ( remove )
       
   683             {
       
   684             // Remove non selected archive
       
   685             archives.Remove( i );
       
   686             delete archiveInfo;
       
   687             }
       
   688         else
       
   689             {
       
   690             // Move to next archive
       
   691             ++i;
       
   692             }
       
   693         }
       
   694 
       
   695     CleanupStack::PopAndDestroy( &selection );
       
   696     params->SetArchiveInfosL( archives );
       
   697     CleanupStack::Pop( &archives );
       
   698     archives.Close();
       
   699     CleanupStack::Pop( params );
       
   700 
       
   701 #else // RD_FILE_MANAGER_BACKUP
       
   702 
       
   703     CMMCScBkupOpParamsRestoreFull* params =
       
   704         CMMCScBkupOpParamsRestoreFull::NewL( driveReader, EBUCatAllInOne );
       
   705     CleanupStack::PopAndDestroy(); // driveReader
       
   706 
       
   707     // Get list of all archives
       
   708     RPointerArray< CMMCScBkupArchiveInfo > archives;
       
   709     TCleanupItem cleanupItem( ResetAndDestroyArchives, &archives );
       
   710     CleanupStack::PushL( cleanupItem );
       
   711     iBkupEngine->ListArchivesL(
       
   712         archives,
       
   713         params,
       
   714         bkupSettings.AllowedDriveAttMatchMask() );
       
   715     params->SetArchiveInfosL( archives );
       
   716     CleanupStack::Pop( &archives );
       
   717 
       
   718 #endif // RD_FILE_MANAGER_BACKUP
       
   719 
       
   720     // Start the process - engine owns the parameters immediately
       
   721     iProcess = MFileManagerProcessObserver::ERestoreProcess;
       
   722     PublishBurStatus( EFileManagerBkupStatusRestore );
       
   723     TRAPD( err, iBkupEngine->StartOperationL(
       
   724         EMMCScBkupOperationTypeFullRestore, *this, params ) );
       
   725     if ( err != KErrNone )
       
   726         {
       
   727         PublishBurStatus( EFileManagerBkupStatusUnset );
       
   728         iProcess = MFileManagerProcessObserver::ENoProcess;
       
   729         User::Leave( err );
       
   730         }
       
   731     }
       
   732 
       
   733 // ---------------------------------------------------------------------------
       
   734 // CFileManagerRemovableDriveHandler::GetRestoreInfoArrayL
       
   735 // ---------------------------------------------------------------------------
       
   736 // 
       
   737 void CFileManagerRemovableDriveHandler::GetRestoreInfoArrayL(
       
   738         RArray< CFileManagerRestoreSettings::TInfo >& aArray,
       
   739         const TInt aDrive )
       
   740     {
       
   741     CFileManagerBackupSettings& settings( iEngine.BackupSettingsL() );
       
   742 
       
   743     aArray.Reset();
       
   744 
       
   745     TResourceReader driveReader;
       
   746     CCoeEnv::Static()->CreateResourceReaderLC(
       
   747         driveReader,
       
   748         R_FILEMANAGER_BACKUP_RESTORE_DRIVES_AND_OPERATIONS );
       
   749 
       
   750     CMMCScBkupOpParamsRestoreFull* params =
       
   751         CMMCScBkupOpParamsRestoreFull::NewL(
       
   752             driveReader, EBUCatAllSeparately );
       
   753     CleanupStack::PopAndDestroy(); // driveReader
       
   754     CleanupStack::PushL( params );
       
   755 
       
   756     // Get list of all archives
       
   757     RPointerArray< CMMCScBkupArchiveInfo > archives;
       
   758     TCleanupItem cleanupItem( ResetAndDestroyArchives, &archives );
       
   759     CleanupStack::PushL( cleanupItem );
       
   760     iBkupEngine->ListArchivesL(
       
   761         archives,
       
   762         params,
       
   763         settings.AllowedDriveAttMatchMask(),
       
   764         aDrive );
       
   765 
       
   766     // Prepare time zone conversion
       
   767     RTz tz;
       
   768     User::LeaveIfError( tz.Connect() );
       
   769     CleanupClosePushL( tz );
       
   770 
       
   771     // Fill restore info
       
   772     CFileManagerRestoreSettings::TInfo info;
       
   773     TInt count( archives.Count() );
       
   774     aArray.ReserveL( count );
       
   775 
       
   776     for( TInt i( 0 ); i < count; ++i )
       
   777         {
       
   778         // Content
       
   779         CMMCScBkupArchiveInfo& archiveInfo( *archives[ i ] );
       
   780         info.iContent = BkupToFmgrMask( archiveInfo.Category().iFlags );
       
   781 
       
   782         // Local time
       
   783         info.iTime = archiveInfo.DateTime();
       
   784         User::LeaveIfError( tz.ConvertToLocalTime( info.iTime ) );
       
   785 
       
   786         // Drive
       
   787         info.iDrive = archiveInfo.Drive();
       
   788 
       
   789         aArray.AppendL( info );
       
   790         }
       
   791 
       
   792     CleanupStack::PopAndDestroy( &tz );
       
   793     CleanupStack::PopAndDestroy( &archives );
       
   794     CleanupStack::PopAndDestroy( params );
       
   795     }
       
   796 
       
   797 // ---------------------------------------------------------------------------
       
   798 // CFileManagerRemovableDriveHandler::DoCancel
       
   799 // ---------------------------------------------------------------------------
       
   800 // 
       
   801 void CFileManagerRemovableDriveHandler::DoCancel()
       
   802     {
       
   803     switch( iProcess )
       
   804         {
       
   805         case MFileManagerProcessObserver::EFormatProcess:
       
   806             {
       
   807             TRAP_IGNORE( EndFormatProcessL( KErrCancel ) );
       
   808             break;
       
   809             }
       
   810         default:
       
   811             {
       
   812             break;
       
   813             }
       
   814         }
       
   815     }
       
   816 
       
   817 // ---------------------------------------------------------------------------
       
   818 // CFileManagerRemovableDriveHandler::RunL
       
   819 // ---------------------------------------------------------------------------
       
   820 // 
       
   821 void CFileManagerRemovableDriveHandler::RunL()
       
   822     {
       
   823     TInt err( iStatus.Int() );
       
   824     switch( iProcess )
       
   825         {
       
   826         case MFileManagerProcessObserver::EFormatProcess:
       
   827             {
       
   828             if( err != KErrNone )
       
   829                 {
       
   830                 if ( !iFinalValue && err == KErrInUse )
       
   831                     {
       
   832                     // Some app remained open, try still to start format
       
   833                     StartFormatProcessL();
       
   834                     }
       
   835                 else
       
   836                     {
       
   837                     // Format failed
       
   838                     EndFormatProcessL( err );
       
   839                     }
       
   840                 }
       
   841             else if( !iFinalValue )
       
   842                 {
       
   843                 // Apps have been closed. Start format.
       
   844                 StartFormatProcessL();
       
   845                 }
       
   846             else if( iFormatCountBuf() > 0 )
       
   847                 {
       
   848                 // Update observer and format next track
       
   849                 InformUpdateL( iFinalValue - iFormatCountBuf() );
       
   850                 iFormatter.Next( iFormatCountBuf, iStatus );
       
   851                 SetActive( );
       
   852                 }
       
   853             else
       
   854                 {
       
   855                 // Format complete
       
   856                 EndFormatProcessL( KErrNone );
       
   857                 }
       
   858             break;
       
   859             }
       
   860         default:
       
   861             {
       
   862             break;
       
   863             }
       
   864         }
       
   865     }
       
   866 
       
   867 // ---------------------------------------------------------------------------
       
   868 // CFileManagerRemovableDriveHandler::RunError
       
   869 // ---------------------------------------------------------------------------
       
   870 // 
       
   871 TInt CFileManagerRemovableDriveHandler::RunError( TInt aError )
       
   872     {
       
   873     switch( iProcess )
       
   874         {
       
   875         case MFileManagerProcessObserver::EFormatProcess:
       
   876             {
       
   877             TRAP_IGNORE( EndFormatProcessL( aError ) );
       
   878             break;
       
   879             }
       
   880         default:
       
   881             {
       
   882             break;
       
   883             }
       
   884         }
       
   885     return KErrNone;
       
   886     }
       
   887 
       
   888 // ---------------------------------------------------------------------------
       
   889 // CFileManagerRemovableDriveHandler::EndFormatProcessL
       
   890 // ---------------------------------------------------------------------------
       
   891 // 
       
   892 void CFileManagerRemovableDriveHandler::EndFormatProcessL( TInt aErr )
       
   893     {
       
   894     iFormatter.Close();
       
   895     iLastError = aErr;
       
   896     if( aErr == KErrNone )
       
   897         {
       
   898         // Restore the volume name stored before format operation.
       
   899         TRAPD( err, RestoreVolumeNameL( iDrive ) );
       
   900         LOG_IF_ERROR1( err,
       
   901             "FileManagerRemovableDriveHandler::EndFormatProcessL-RestoreVolumeName %d",
       
   902             err );
       
   903         iUtils.CreateDefaultFolders( iDrive );
       
   904         }
       
   905     else if ( aErr != KErrCancel )
       
   906         {
       
   907         InformError( aErr );
       
   908         }
       
   909     TRAPD( err,  RestartAppsL() );
       
   910       LOG_IF_ERROR1( err,
       
   911          "FileManagerRemovableDriveHandler::EndFormatProcessL-Restart apps %d",
       
   912          err );
       
   913     InformFinishL();
       
   914 
       
   915     }
       
   916 
       
   917 // ---------------------------------------------------------------------------
       
   918 // CFileManagerRemovableDriveHandler::StartFormatProcessL
       
   919 // ---------------------------------------------------------------------------
       
   920 // 
       
   921 void CFileManagerRemovableDriveHandler::StartFormatProcessL()
       
   922     {
       
   923     RDebug::Printf(">> CFileManagerRemovableDriveHandler::StartFormatProcessL() 1 ");
       
   924     // Store the current volume name over format operation.
       
   925     TRAPD( err, StoreVolumeNameL( iDrive ) );
       
   926     LOG_IF_ERROR1(
       
   927         err,
       
   928         "FileManagerRemovableDriveHandler::StartFormatProcessL-StoreVolumeName %d",
       
   929         err );
       
   930 
       
   931     TDriveName driveName( TDriveUnit( iDrive ).Name() );
       
   932     RDebug::Printf(">> CFileManagerRemovableDriveHandler::StartFormatProcessL() 2");
       
   933     // Resolve drive character and open formatter
       
   934     iFormatter.Close();
       
   935     RDebug::Printf(">> CFileManagerRemovableDriveHandler::StartFormatProcessL() 2.1");
       
   936     err = iFormatter.Open(
       
   937         iFs, driveName, EFullFormat, iFinalValue );
       
   938     RDebug::Printf(">> CFileManagerRemovableDriveHandler::StartFormatProcessL() 2.2 RFormat::Open full err=%d", err);
       
   939 
       
   940     // Forced format for locked card
       
   941     if ( err == KErrLocked )
       
   942         {
       
   943         // Erase password and try again
       
   944         err = iFs.ErasePassword( iDrive );
       
   945         INFO_LOG1( "FileManagerRemovableDriveHandler::StartFormatProcessL-ErasePassword result=%d", err);
       
   946 
       
   947         if (err == KErrNone)
       
   948         	{
       
   949         	err = iFormatter.Open(iFs, driveName, EFullFormat , iFinalValue );
       
   950         	RDebug::Printf(">> CFileManagerRemovableDriveHandler::StartFormatProcessL() RFormat::Open again err=%d", err);
       
   951         	}
       
   952         }
       
   953 
       
   954     if (err == KErrInUse)
       
   955     	{
       
   956     	RDebug::Printf(">> CFileManagerRemovableDriveHandler::StartFormatProcessL KErrInUse try force format err=%d", err);
       
   957 
       
   958     	// There are still files open on the drive being formatted, prompt the user to
       
   959     	// ask if they want to format anyway.
       
   960     	//
       
   961     	// formatting.
       
   962     	//TBool reallyFormat = FileManagerDlgUtils::ShowConfirmQueryWithYesNoL(_L("There are files open on this drive, continue with format?"));
       
   963     	TBool reallyFormat = ETrue; // TEMP! - should ask the user to confirm Yes / No
       
   964 
       
   965     	if (reallyFormat)
       
   966     		{
       
   967     		// Open the RFormat sub-session with force format flags
       
   968     		RDebug::Printf( "FileManagerRemovableDriveHandler::StartFormatProcessL-******force format*****");
       
   969     		err = iFormatter.Open(
       
   970     				iFs, driveName, EFullFormat | EForceFormat, iFinalValue );
       
   971     		RDebug::Printf( "FileManagerRemovableDriveHandler::StartFormatProcessL-******force format***** err = %d", err);
       
   972     		}
       
   973     	}
       
   974 
       
   975     // By the time we get here, one of three things could have happened:
       
   976     // 1.  	RFormat has been opened with standard full format flag with no error
       
   977     // 2.  	Some other app still has files opened so full format cannot be used.
       
   978     //		A second attempt has been made to open the format sub-session with
       
   979     //		full format flags and this has succeeded.
       
   980     // 3.	As 2 but for some other reason (corrupt card perhaps?), force format
       
   981     // 		does not work in which case abort format.
       
   982 
       
   983 
       
   984    TFullName fsName;
       
   985    if (err == KErrNone)
       
   986 	   {
       
   987 	   err = iFs.FileSystemName( fsName, iDrive );
       
   988 
       
   989 	   RDebug::Printf( "FileManagerRemovableDriveHandler::StartFormatProcessL-fsName=%S, result=%d",
       
   990 			   &fsName, err );
       
   991 
       
   992 	   if ( err == KErrNone && fsName.Length() > 0 )
       
   993 		   {
       
   994 		   // Prevent SysAp shutting down applications
       
   995 		   RProperty::Set(
       
   996 				   KPSUidCoreApplicationUIs,
       
   997 				   KCoreAppUIsMmcRemovedWithoutEject,
       
   998 				   ECoreAppUIsEjectCommandUsed );
       
   999 		   }
       
  1000 	   else
       
  1001 		   {
       
  1002 		   // Don't continue with format if there is no file system name
       
  1003 		   // or file system name could not be obtained.
       
  1004 		   err = KErrCancel;
       
  1005 		   }
       
  1006 	   }
       
  1007 
       
  1008     // On successful open of RFormat::Open(), iFinalValue contains the number of
       
  1009     // tracks to be formatted
       
  1010 
       
  1011     if ( iFinalValue && err == KErrNone )
       
  1012         {
       
  1013         TRAP( err, InformStartL( iFinalValue ) );
       
  1014         if ( err == KErrNone )
       
  1015             {
       
  1016             RDebug::Printf(">> CFileManagerRemovableDriveHandler::StartFormatProcessL() RFormat::Next err = %d, tracks left=%d", err, iFinalValue);
       
  1017             iFormatCountBuf = iFinalValue;
       
  1018             iFormatter.Next( iFormatCountBuf, iStatus );
       
  1019             SetActive();
       
  1020             }
       
  1021         }
       
  1022     if ( !iFinalValue || err != KErrNone )
       
  1023         {
       
  1024         RDebug::Printf(">> CFileManagerRemovableDriveHandler::StartFormatProcessL() 6 err = %d", err);
       
  1025         EndFormatProcessL( err );
       
  1026         RDebug::Printf(">> CFileManagerRemovableDriveHandler::StartFormatProcessL() 7 err = %d", err);
       
  1027         }
       
  1028     RDebug::Printf("<< CFileManagerRemovableDriveHandler::StartFormatProcessL() 8 err=%d", err);
       
  1029     }
       
  1030 // ---------------------------------------------------------------------------
       
  1031 // CFileManagerRemovableDriveHandler::CloseAppsL
       
  1032 // ---------------------------------------------------------------------------
       
  1033 // 
       
  1034 void CFileManagerRemovableDriveHandler::CloseAppsL()
       
  1035     {
       
  1036     delete iBSWrapper;
       
  1037     iBSWrapper = NULL;
       
  1038 
       
  1039     iBSWrapper = CBaBackupSessionWrapper::NewL();
       
  1040 
       
  1041     TBackupOperationAttributes atts(
       
  1042         MBackupObserver::EReleaseLockNoAccess,
       
  1043         MBackupOperationObserver::EStart );
       
  1044     iBSWrapper->NotifyBackupOperationL( atts );
       
  1045     iBSWrapper->CloseAll( MBackupObserver::EReleaseLockNoAccess, iStatus );
       
  1046     SetActive();
       
  1047 
       
  1048     // Memory card formatting cannot be executed if there are open files on it.
       
  1049     // It has been detected, that in some cases memory card using applications 
       
  1050     // have no time to close file handles before formatting is tried to be executed. 
       
  1051     // To address this issue, we need to add a delay here after client-notification 
       
  1052     // about pending format and real formatting procedure.
       
  1053     User::After( KAppCloseTimeout );
       
  1054     }
       
  1055 
       
  1056 // ---------------------------------------------------------------------------
       
  1057 // CFileManagerRemovableDriveHandler::RestartAppsL
       
  1058 // ---------------------------------------------------------------------------
       
  1059 // 
       
  1060 void CFileManagerRemovableDriveHandler::RestartAppsL()
       
  1061     {
       
  1062     if ( !iBSWrapper )
       
  1063         {
       
  1064         return;
       
  1065         }
       
  1066 
       
  1067     TBackupOperationAttributes atts(
       
  1068         MBackupObserver::ETakeLock, MBackupOperationObserver::EEnd );
       
  1069     iBSWrapper->NotifyBackupOperationL( atts );
       
  1070     iBSWrapper->RestartAll();
       
  1071 
       
  1072     // Get rid of the wrapper instance
       
  1073     delete iBSWrapper;
       
  1074     iBSWrapper = NULL;
       
  1075     }
       
  1076 
       
  1077 // ---------------------------------------------------------------------------
       
  1078 // CFileManagerRemovableDriveHandler::ResetAndDestroyArchives
       
  1079 // ---------------------------------------------------------------------------
       
  1080 // 
       
  1081 void CFileManagerRemovableDriveHandler::ResetAndDestroyArchives( TAny* aPtr )
       
  1082     {
       
  1083     RPointerArray< CMMCScBkupArchiveInfo >* archive = 
       
  1084         static_cast< RPointerArray< CMMCScBkupArchiveInfo >* >( aPtr );
       
  1085     archive->ResetAndDestroy();
       
  1086     archive->Close();
       
  1087     }
       
  1088 
       
  1089 // ---------------------------------------------------------------------------
       
  1090 // CFileManagerRemovableDriveHandler::BkupToFmgrMask
       
  1091 // ---------------------------------------------------------------------------
       
  1092 // 
       
  1093 TUint32 CFileManagerRemovableDriveHandler::BkupToFmgrMask(
       
  1094         const TUint32 aBkupMask )
       
  1095     {
       
  1096     TUint32 ret( 0 );
       
  1097 
       
  1098     for( TInt i( 0 ); i < KMaskLookupLen; ++i )
       
  1099         {
       
  1100         if ( aBkupMask & KMaskLookup[ i ].iBkupMask )
       
  1101             {
       
  1102             ret |= KMaskLookup[ i ].iFmgrMask;
       
  1103             }
       
  1104         }
       
  1105     return ret;
       
  1106     }
       
  1107 
       
  1108 #ifdef RD_FILE_MANAGER_BACKUP
       
  1109 // ---------------------------------------------------------------------------
       
  1110 // CFileManagerRemovableDriveHandler::FmgrToBkupMask
       
  1111 // ---------------------------------------------------------------------------
       
  1112 // 
       
  1113 TUint32 CFileManagerRemovableDriveHandler::FmgrToBkupMask(
       
  1114         const TUint32 aFmrgMask )
       
  1115     {
       
  1116     TUint32 ret( 0 );
       
  1117 
       
  1118     for( TInt i( 0 ); i < KMaskLookupLen; ++i )
       
  1119         {
       
  1120         if ( ( aFmrgMask & EFileManagerBackupContentAll ) ||
       
  1121             ( aFmrgMask & KMaskLookup[ i ].iFmgrMask ) )
       
  1122             {
       
  1123             ret |= KMaskLookup[ i ].iBkupMask;
       
  1124             }
       
  1125         }
       
  1126     return ret;
       
  1127     }
       
  1128 #endif // RD_FILE_MANAGER_BACKUP
       
  1129 
       
  1130 // ---------------------------------------------------------------------------
       
  1131 // CFileManagerRemovableDriveHandler::IsProcessOngoing
       
  1132 // ---------------------------------------------------------------------------
       
  1133 // 
       
  1134 TBool CFileManagerRemovableDriveHandler::IsProcessOngoing() const
       
  1135     {
       
  1136     return iProcess != MFileManagerProcessObserver::ENoProcess;
       
  1137     }
       
  1138 
       
  1139 // -----------------------------------------------------------------------------
       
  1140 // CFileManagerEngine::PublishBurStatus()
       
  1141 //
       
  1142 // -----------------------------------------------------------------------------
       
  1143 //  
       
  1144 void CFileManagerRemovableDriveHandler::PublishBurStatus( TInt aType )
       
  1145 	{
       
  1146    _LIT_SECURITY_POLICY_S0( KFileManagerBkupWritePolicy, KFileManagerUID3 );
       
  1147    _LIT_SECURITY_POLICY_PASS( KFileManagerBkupReadPolicy );
       
  1148 
       
  1149 	TInt err( RProperty::Set(
       
  1150 	    KPSUidFileManagerStatus, KFileManagerBkupStatus, aType ) );
       
  1151 	if ( err != KErrNone )
       
  1152 		{
       
  1153 		err = RProperty::Define(
       
  1154 		    KPSUidFileManagerStatus, KFileManagerBkupStatus,
       
  1155 		    RProperty::EInt, KFileManagerBkupReadPolicy,
       
  1156 		    KFileManagerBkupWritePolicy );
       
  1157 		if ( err == KErrNone || err == KErrAlreadyExists )
       
  1158 		    {
       
  1159 		    err = RProperty::Set(
       
  1160 		        KPSUidFileManagerStatus, KFileManagerBkupStatus, aType );
       
  1161 		    }
       
  1162 		}
       
  1163     LOG_IF_ERROR1(
       
  1164         err, "FileManagerRemovableDriveHandler::PublishBurStatus-err=%d", err )
       
  1165 	}
       
  1166 
       
  1167 // -----------------------------------------------------------------------------
       
  1168 // CFileManagerEngine::IsMassStorageDrive()
       
  1169 //
       
  1170 // -----------------------------------------------------------------------------
       
  1171 //  
       
  1172 #ifdef RD_MULTIPLE_DRIVE
       
  1173 
       
  1174 TBool CFileManagerRemovableDriveHandler::IsInternalMassStorage( TInt aDrive )
       
  1175 	{
       
  1176     FUNC_LOG;
       
  1177 
       
  1178     TBool ret( EFalse );
       
  1179     TUint driveStatus( 0 );
       
  1180     DriveInfo::GetDriveStatus( iFs, aDrive, driveStatus );
       
  1181     if ( ( driveStatus & DriveInfo::EDriveInternal ) &&
       
  1182          ( driveStatus & DriveInfo::EDriveExternallyMountable ) )
       
  1183         {
       
  1184         ret = ETrue;
       
  1185         }
       
  1186     INFO_LOG2(
       
  1187         "FileManagerRemovableDriveHandler::IsInternalMassStorage-drive=%d,ret=%d",
       
  1188         aDrive, ret );
       
  1189     return ret;
       
  1190     }
       
  1191 
       
  1192 #else // RD_MULTIPLE_DRIVE
       
  1193 
       
  1194 TBool CFileManagerRemovableDriveHandler::IsInternalMassStorage( TInt /*aDrive*/ )
       
  1195 	{
       
  1196     FUNC_LOG;
       
  1197 
       
  1198     TInt ret( EFalse );
       
  1199     return ret;
       
  1200     }
       
  1201 
       
  1202 #endif // RD_MULTIPLE_DRIVE
       
  1203 
       
  1204 // -----------------------------------------------------------------------------
       
  1205 // CFileManagerEngine::StoreVolumeNameL()
       
  1206 //
       
  1207 // -----------------------------------------------------------------------------
       
  1208 //  
       
  1209 void CFileManagerRemovableDriveHandler::StoreVolumeNameL( TInt aDrive )
       
  1210 	{
       
  1211     FUNC_LOG;
       
  1212 
       
  1213     if ( IsInternalMassStorage( aDrive ) )
       
  1214         {
       
  1215         TVolumeInfo volumeInfo;
       
  1216         User::LeaveIfError( iFs.Volume( volumeInfo, aDrive ) );
       
  1217         CRepository* cenRep = CRepository::NewLC( KCRUidFileManagerSettings );
       
  1218         TFileManagerVolumeNameStore volumeStore;
       
  1219         TFileManagerVolumeNameStorePckg volumeStorePckg( volumeStore );
       
  1220         volumeStore.iDrive = aDrive;
       
  1221         volumeStore.iName.Copy( volumeInfo.iName );
       
  1222         User::LeaveIfError( cenRep->Set(
       
  1223             KFileManagerStoredInternalMassStorageVolumeName, volumeStorePckg ) );
       
  1224         CleanupStack::PopAndDestroy( cenRep );
       
  1225         INFO_LOG2(
       
  1226             "FileManagerRemovableDriveHandler::StoreVolumeNameL-drive=%d,name=%S",
       
  1227             aDrive, &volumeStore.iName );
       
  1228         }
       
  1229     }
       
  1230 
       
  1231 // -----------------------------------------------------------------------------
       
  1232 // CFileManagerEngine::RestoreVolumeNameL()
       
  1233 //
       
  1234 // -----------------------------------------------------------------------------
       
  1235 //  
       
  1236 void CFileManagerRemovableDriveHandler::RestoreVolumeNameL( TInt aDrive )
       
  1237 	{
       
  1238     FUNC_LOG;
       
  1239 
       
  1240     if ( IsInternalMassStorage( aDrive ) )
       
  1241         {
       
  1242         TFileName volumeName;
       
  1243         CRepository* cenRep = CRepository::NewLC( KCRUidFileManagerSettings );
       
  1244         TFileManagerVolumeNameStore volumeStore;
       
  1245         TFileManagerVolumeNameStorePckg volumeStorePckg( volumeStore );
       
  1246         TInt err( cenRep->Get(
       
  1247             KFileManagerStoredInternalMassStorageVolumeName, volumeStorePckg ) );
       
  1248         if ( err == KErrNone && volumeStore.iDrive == aDrive )
       
  1249             {
       
  1250             volumeName.Copy( volumeStore.iName );
       
  1251             }
       
  1252         if ( !volumeName.Length() )
       
  1253             {
       
  1254             User::LeaveIfError( cenRep->Get(
       
  1255                 KFileManagerDefaultInternalMassStorageVolumeName, volumeName ) );
       
  1256             }
       
  1257         if ( volumeName.Length() > 0 )
       
  1258             {
       
  1259             User::LeaveIfError( iFs.SetVolumeLabel( volumeName, aDrive ) );
       
  1260             }
       
  1261         CleanupStack::PopAndDestroy( cenRep );
       
  1262         INFO_LOG2(
       
  1263             "FileManagerRemovableDriveHandler::RestoreVolumeNameL-drive=%d,name=%S",
       
  1264             aDrive, &volumeName );
       
  1265         }
       
  1266     }
       
  1267 
       
  1268 // ---------------------------------------------------------------------------
       
  1269 // CFileManagerRemovableDriveHandler::ListArchivesL
       
  1270 // ---------------------------------------------------------------------------
       
  1271 //  
       
  1272 void CFileManagerRemovableDriveHandler::ListArchivesL(
       
  1273         RPointerArray< CMMCScBkupArchiveInfo >& aArchives,
       
  1274         const CFileManagerBackupSettings& aBackupSettings )
       
  1275     {
       
  1276     aArchives.ResetAndDestroy();
       
  1277     TResourceReader driveReader;
       
  1278     CCoeEnv::Static()->CreateResourceReaderLC(
       
  1279         driveReader,
       
  1280         R_FILEMANAGER_BACKUP_RESTORE_DRIVES_AND_OPERATIONS );
       
  1281     CMMCScBkupOpParamsRestoreFull* params =
       
  1282         CMMCScBkupOpParamsRestoreFull::NewL(
       
  1283             driveReader, EBUCatAllSeparately );
       
  1284     CleanupStack::PopAndDestroy(); // driveReader
       
  1285     CleanupStack::PushL( params );
       
  1286     iBkupEngine->ListArchivesL(
       
  1287         aArchives, params, aBackupSettings.AllowedDriveAttMatchMask() );
       
  1288     CleanupStack::PopAndDestroy( params );
       
  1289     }
       
  1290 
       
  1291 // ---------------------------------------------------------------------------
       
  1292 // CFileManagerRemovableDriveHandler::LatestBackupTimeL
       
  1293 // ---------------------------------------------------------------------------
       
  1294 //  
       
  1295 void CFileManagerRemovableDriveHandler::LatestBackupTimeL(
       
  1296         TTime& aBackupTime )
       
  1297     {
       
  1298     aBackupTime = 0;
       
  1299     CFileManagerBackupSettings& bkupSettings( iEngine.BackupSettingsL() );
       
  1300     RPointerArray< CMMCScBkupArchiveInfo > archives;
       
  1301     TCleanupItem cleanupItem( ResetAndDestroyArchives, &archives );
       
  1302     CleanupStack::PushL( cleanupItem );
       
  1303     ListArchivesL( archives, bkupSettings );
       
  1304 
       
  1305     // Find the latest archive
       
  1306     TBool found( EFalse );
       
  1307     TInt count( archives.Count() );
       
  1308     for( TInt i( 0 ); i < count; ++i )
       
  1309         {
       
  1310         TTime time( archives[ i ]->DateTime() );
       
  1311         if ( time > aBackupTime )
       
  1312             {
       
  1313             aBackupTime = time;
       
  1314             found = ETrue;
       
  1315             }
       
  1316         }
       
  1317     CleanupStack::PopAndDestroy( &archives );
       
  1318     if ( !found )
       
  1319         {
       
  1320         User::Leave( KErrNotFound );
       
  1321         }
       
  1322     }
       
  1323 
       
  1324 // ---------------------------------------------------------------------------
       
  1325 // CFileManagerRemovableDriveHandler::DeleteBackupsL
       
  1326 // ---------------------------------------------------------------------------
       
  1327 //  
       
  1328 void CFileManagerRemovableDriveHandler::DeleteBackupsL()
       
  1329     {
       
  1330     CFileManagerBackupSettings& bkupSettings( iEngine.BackupSettingsL() );
       
  1331     RPointerArray< CMMCScBkupArchiveInfo > archives;
       
  1332     TCleanupItem cleanupItem( ResetAndDestroyArchives, &archives );
       
  1333     CleanupStack::PushL( cleanupItem );
       
  1334     ListArchivesL( archives, bkupSettings );
       
  1335 
       
  1336     // Get the user set selection
       
  1337     RArray< CFileManagerRestoreSettings::TInfo > selection;
       
  1338     CleanupClosePushL( selection );
       
  1339     CFileManagerRestoreSettings& rstSettings( iEngine.RestoreSettingsL() );
       
  1340     rstSettings.GetSelectionL( selection );
       
  1341 
       
  1342     // Delete selected archives
       
  1343     TInt archivesCount( archives.Count() );
       
  1344     for ( TInt i( 0 ); i < archivesCount; ++i )
       
  1345         {
       
  1346         CMMCScBkupArchiveInfo* archiveInfo = archives[ i ];
       
  1347         TUint32 fmgrContent(
       
  1348             BkupToFmgrMask( archiveInfo->Category().iFlags ) );
       
  1349         TInt drive( archiveInfo->Drive() );
       
  1350         TInt selectionCount( selection.Count() );
       
  1351         for( TInt j( 0 ); j < selectionCount; ++j )
       
  1352             {
       
  1353             const CFileManagerRestoreSettings::TInfo& info( selection[ j ] );
       
  1354             if ( ( drive == info.iDrive ) && ( fmgrContent & info.iContent ) )
       
  1355                 {
       
  1356                 TPtrC fullPath( archiveInfo->FileName() );
       
  1357                 User::LeaveIfError(
       
  1358                     CFileManagerUtils::RemoveReadOnlyAttribute(
       
  1359                         iFs, fullPath ) );
       
  1360                 User::LeaveIfError( iFs.Delete( fullPath ) );
       
  1361                 selection.Remove( j ); // Do not check again
       
  1362                 break;
       
  1363                 }
       
  1364             }
       
  1365         }
       
  1366     CleanupStack::PopAndDestroy( &selection );
       
  1367     CleanupStack::PopAndDestroy( &archives );
       
  1368     }
       
  1369 
       
  1370 
       
  1371 //  End of File