filemanager/Engine/src/CfilemanageractiveDelete.cpp
branchRCL_3
changeset 20 491b3ed49290
parent 19 95243422089a
child 21 65326cf895ed
equal deleted inserted replaced
19:95243422089a 20:491b3ed49290
     1 /*
       
     2 * Copyright (c) 2002-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:  Deletes files
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "Cfilemanageractivedelete.h"
       
    21 #include "MFileManagerProcessObserver.h"
       
    22 #include "CFileManagerEngine.h"
       
    23 #include "CFileManagerCommonDefinitions.h"
       
    24 #include "CFileManagerUtils.h"
       
    25 #include "FileManagerDebug.h"
       
    26 //#include <cmgxfilemanager.h>
       
    27 
       
    28 // CONSTANTS
       
    29 const TInt KFileManagerDeletionPerStep = 20;
       
    30 const TInt64 KFileManagerMaxStepTime = 1000000; // 1s
       
    31 const TInt KFileManagerNotificationArrayGranularity = 64;
       
    32 
       
    33 
       
    34 // ============================ LOCAL FUNCTIONS ================================
       
    35 // -----------------------------------------------------------------------------
       
    36 // GetTimeStamp
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 static TInt64 GetTimeStamp()
       
    40     {
       
    41     TTime time;
       
    42     time.UniversalTime();
       
    43     return time.Int64();
       
    44     }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // IsTimedOut
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 static TBool IsTimedOut( const TInt64& aStartTime )
       
    51     {
       
    52     TInt64 time( GetTimeStamp() );
       
    53     TBool ret( time - aStartTime > KFileManagerMaxStepTime );
       
    54     if ( ret )
       
    55         {
       
    56         INFO_LOG("CFileManagerActiveDelete-TimedOut");
       
    57         }
       
    58     return ret;
       
    59     }
       
    60 
       
    61 
       
    62 // ============================ MEMBER FUNCTIONS ===============================
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // CFileManagerActiveDelete::CFileManagerActiveDelete
       
    66 // C++ default constructor can NOT contain any code, that
       
    67 // might leave.
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 CFileManagerActiveDelete::CFileManagerActiveDelete(
       
    71         RFs& aFs,
       
    72         CArrayFixFlat< TInt >& aIndexList,
       
    73         CFileManagerEngine& aEngine,
       
    74         CFileManagerUtils& aUtils ) :
       
    75     iFs( aFs ),
       
    76     iIndexList( aIndexList ),
       
    77     iError( KErrNone ),
       
    78     iEngine( aEngine ),
       
    79     iUtils( aUtils )
       
    80     {
       
    81     }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // CFileManagerActiveDelete::NewL
       
    85 // Two-phased constructor.
       
    86 // -----------------------------------------------------------------------------
       
    87 //
       
    88 CFileManagerActiveDelete* CFileManagerActiveDelete::NewL(
       
    89         CArrayFixFlat< TInt >& aIndexList,
       
    90         CFileManagerEngine& aEngine,
       
    91         CFileManagerUtils& aUtils )
       
    92     {
       
    93     CFileManagerActiveDelete* self = new( ELeave ) CFileManagerActiveDelete(
       
    94         aEngine.Fs(), aIndexList, aEngine, aUtils );
       
    95     CleanupStack::PushL( self );
       
    96     self->ConstructL();
       
    97     CleanupStack::Pop( self );
       
    98     return self;
       
    99     }
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // CFileManagerActiveDelete::ConstructL
       
   103 // 
       
   104 // -----------------------------------------------------------------------------
       
   105 // 
       
   106 void CFileManagerActiveDelete::ConstructL()
       
   107     {
       
   108     iStringBuffer = HBufC::NewL( KMaxFileName );
       
   109     //MG2 notification object
       
   110     //iMgxFileManager = &iEngine.MGXFileManagerL();
       
   111     iRemovedItems = new( ELeave ) CDesCArrayFlat(
       
   112         KFileManagerNotificationArrayGranularity );
       
   113     iIsRemoteDrive = iUtils.IsRemoteDrive( iEngine.CurrentDirectory() );
       
   114     }
       
   115 
       
   116 // -----------------------------------------------------------------------------
       
   117 // CFileManagerActiveDelete::~CFileManagerActiveDelete
       
   118 // Destructor
       
   119 // -----------------------------------------------------------------------------
       
   120 // 
       
   121 EXPORT_C CFileManagerActiveDelete::~CFileManagerActiveDelete()
       
   122     {
       
   123     delete iStringBuffer;
       
   124     delete iFullPath;
       
   125     delete iDirScan;
       
   126     delete iDir;
       
   127     delete iRemovedItems;
       
   128     }
       
   129 
       
   130 // -----------------------------------------------------------------------------
       
   131 // CFileManagerActiveDelete::IsProcessDone
       
   132 // 
       
   133 // -----------------------------------------------------------------------------
       
   134 // 
       
   135 TBool CFileManagerActiveDelete::IsProcessDone() const
       
   136     {
       
   137     return iProcessDone;
       
   138     }
       
   139 
       
   140 // -----------------------------------------------------------------------------
       
   141 // CFileManagerEngine::DeleteItemsInDirectoryL
       
   142 // -----------------------------------------------------------------------------
       
   143 //
       
   144 TBool CFileManagerActiveDelete::DeleteItemsInDirectoryL()
       
   145     {
       
   146     if ( !iDirScan )
       
   147         {
       
   148         iDirScan = CDirScan::NewL( iFs );
       
   149         // Set scanning from current directory, take all files
       
   150         // No sorting needed
       
   151         iDirScan->SetScanDataL( *iFullPath,
       
   152             KEntryAttNormal | KEntryAttHidden | KEntryAttSystem,
       
   153             ESortNone );
       
   154         }
       
   155 
       
   156     if ( iDir && iFileIndex < iDir->Count() )
       
   157         {
       
   158         // Delete file item
       
   159         const TEntry& item = ( *iDir )[ iFileIndex ];
       
   160         TPtr ptr( iStringBuffer->Des() );
       
   161         CFileManagerUtils::GetFullPath(
       
   162             iDirScan->FullPath(), item, ptr );
       
   163         CFileManagerUtils::RemoveReadOnlyAttribute( iFs, ptr, item );
       
   164         DeleteFileL( ptr, ETrue );
       
   165         ++iFileIndex;
       
   166         }
       
   167     else
       
   168         {
       
   169         // Fetch next directory
       
   170         delete iDir;
       
   171         iDir = NULL;
       
   172         iDirScan->NextL( iDir );
       
   173         iFileIndex = 0;
       
   174 
       
   175         if ( iDir )
       
   176             {
       
   177             CFileManagerUtils::RemoveReadOnlyAttribute(
       
   178                 iFs, iDirScan->FullPath() );
       
   179             }
       
   180         }
       
   181 
       
   182     if ( !iDir )
       
   183         {
       
   184         // Items are deleted now, report done
       
   185         if ( !iNotDeletedItems )
       
   186             {
       
   187             // Delete all directories
       
   188             CFileMan* fileMan = CFileMan::NewL( iFs );
       
   189             SetError( fileMan->RmDir( *iFullPath ), *iFullPath );
       
   190             delete fileMan;
       
   191             }
       
   192         return ETrue;
       
   193         }
       
   194     return EFalse; // Still items left
       
   195     }
       
   196 
       
   197 // -----------------------------------------------------------------------------
       
   198 // CFileManagerActiveDelete::StepL
       
   199 // 
       
   200 // -----------------------------------------------------------------------------
       
   201 // 
       
   202 void CFileManagerActiveDelete::StepL()
       
   203     {
       
   204     FUNC_LOG
       
   205 
       
   206     TInt64 startTime( GetTimeStamp() );
       
   207     TInt indexCount( iIndexList.Count() );
       
   208     TInt i( KFileManagerDeletionPerStep );
       
   209     TBool timedOut( EFalse );
       
   210 
       
   211     while ( iCurrentIndex < indexCount && i && !timedOut )
       
   212         {
       
   213         TBool isItemDone( EFalse );
       
   214 
       
   215         // Fetch item path if missing
       
   216         if ( !iFullPath )
       
   217             {
       
   218             iFullPath = iEngine.IndexToFullPathL(
       
   219                 iIndexList.At( iCurrentIndex ) );
       
   220             if ( IsDir( *iFullPath ) )
       
   221                 {
       
   222                 // Ignore default folders
       
   223                 if ( iUtils.DefaultFolder( *iFullPath ) )
       
   224                     {
       
   225                     SetError( KErrFmgrDefaultFolder, *iFullPath );
       
   226                     isItemDone = ETrue;
       
   227                     }
       
   228                 }
       
   229             }
       
   230         // Delete item
       
   231         if ( !isItemDone )
       
   232             {
       
   233             if ( IsDir( *iFullPath ) )
       
   234                 {
       
   235                 // Delete directory item
       
   236                 while ( iCurrentIndex < indexCount && i && !isItemDone && !timedOut )
       
   237                     {
       
   238                     isItemDone = DeleteItemsInDirectoryL();
       
   239                     --i;
       
   240 
       
   241                     // Adjust amount of deleted files per step by consumed time. 
       
   242                     // This is an attempt to avoid long periods of time, 
       
   243                     // where the UI does not respond to user activity.
       
   244                     timedOut = IsTimedOut( startTime );
       
   245                     }
       
   246                 }
       
   247             else
       
   248                 {
       
   249                 // Delete file item
       
   250                 DeleteFileL( *iFullPath );
       
   251                 isItemDone = ETrue;
       
   252                 --i;
       
   253 
       
   254                 // Adjust amount of deleted files per step by consumed time. 
       
   255                 // This is an attempt to avoid long periods of time, 
       
   256                 // where the UI does not respond to user activity.
       
   257                 timedOut = IsTimedOut( startTime );
       
   258                 }
       
   259             }
       
   260 
       
   261         // Move to next item if done
       
   262         if ( isItemDone )
       
   263             {
       
   264             delete iFullPath;
       
   265             iFullPath = NULL;
       
   266             ++iCurrentIndex;
       
   267             }
       
   268         }
       
   269 
       
   270     if ( iCurrentIndex >= indexCount )
       
   271         {
       
   272         iProcessDone = ETrue;
       
   273         FlushNotifications();
       
   274         }
       
   275     }
       
   276 
       
   277 // -----------------------------------------------------------------------------
       
   278 // CFileManagerActiveDelete::DialogDismissedL
       
   279 //
       
   280 // -----------------------------------------------------------------------------
       
   281 //
       
   282 void CFileManagerActiveDelete::DialogDismissedL( TInt aButtonId )
       
   283     {
       
   284     if ( aButtonId == EAknSoftkeyCancel )
       
   285         {
       
   286         FlushNotifications();
       
   287         }
       
   288     }
       
   289 
       
   290 // -----------------------------------------------------------------------------
       
   291 // CFileManagerActiveDelete::GetError
       
   292 // 
       
   293 // -----------------------------------------------------------------------------
       
   294 // 
       
   295 EXPORT_C TInt CFileManagerActiveDelete::GetError( TDes& aFileName )
       
   296     {
       
   297     if( iFileName.Length() > 0)
       
   298         {
       
   299         aFileName.Zero();
       
   300         aFileName.Append( iFileName );
       
   301         }
       
   302     if ( iOpenFiles > 1 )
       
   303         {
       
   304         iError = KErrFmgrSeveralFilesInUse;
       
   305         }
       
   306     return iError;
       
   307     }
       
   308 
       
   309 // -----------------------------------------------------------------------------
       
   310 // CFileManagerActiveDelete::DeletedDrmItems
       
   311 // 
       
   312 // -----------------------------------------------------------------------------
       
   313 // 
       
   314 EXPORT_C TInt CFileManagerActiveDelete::DeletedDrmItems(TInt& aTotalCount )
       
   315     {
       
   316     aTotalCount = iDeletedItems;
       
   317     return iDeletedDrmItems;
       
   318     }
       
   319 
       
   320 // -----------------------------------------------------------------------------
       
   321 // CFileManagerActiveDelete::DeleteFileL
       
   322 // 
       
   323 // -----------------------------------------------------------------------------
       
   324 // 
       
   325 void CFileManagerActiveDelete::DeleteFileL(
       
   326         const TDesC& aFullPath, const TBool aReadOnlyChecked )
       
   327     {
       
   328 #ifndef RD_DRM_RIGHTS_MANAGER_REMOVAL
       
   329     TBool isLocalDataFile( EFalse );
       
   330     TBool isDrmProtected( EFalse );
       
   331     if ( !iIsRemoteDrive )
       
   332         {
       
   333         // Check DRM protection
       
   334         isDrmProtected = iUtils.IsDrmProtectedFile( aFullPath );
       
   335         if ( isDrmProtected )
       
   336             {
       
   337             isLocalDataFile = iUtils.IsDrmLocalDataFile( aFullPath );
       
   338             }
       
   339         }
       
   340 #endif // RD_DRM_RIGHTS_MANAGER_REMOVAL
       
   341 
       
   342     TInt err( iFs.Delete( aFullPath ) );
       
   343     if ( err == KErrAccessDenied && !aReadOnlyChecked )
       
   344         {
       
   345         // Remove readonly and retry
       
   346         TEntry entry;
       
   347         if( iFs.Entry( aFullPath, entry ) == KErrNone )
       
   348             {
       
   349             CFileManagerUtils::RemoveReadOnlyAttribute(
       
   350                 iFs, aFullPath, entry );
       
   351             err = iFs.Delete( aFullPath );
       
   352             }
       
   353         }
       
   354     if ( !IsError( err ) )
       
   355         {
       
   356         ++iDeletedItems;
       
   357         // Notification is relevant only for local drives
       
   358         if ( !iIsRemoteDrive )
       
   359             {
       
   360             TRAPD( err2, iRemovedItems->AppendL( aFullPath ) );
       
   361             if ( err2 != KErrNone )
       
   362                 {
       
   363                 ERROR_LOG1(
       
   364                     "CFileManagerActiveExecute::DeleteFileL-NotificationAppend-err=%d",
       
   365                     err2 )
       
   366                 }
       
   367             }
       
   368 #ifndef RD_DRM_RIGHTS_MANAGER_REMOVAL
       
   369         // Inform deletion of DRM protected files except local data files
       
   370         if( isDrmProtected && !isLocalDataFile )
       
   371             {
       
   372             ++iDeletedDrmItems;
       
   373             SetName( aFullPath );
       
   374             }
       
   375 #endif // RD_DRM_RIGHTS_MANAGER_REMOVAL
       
   376         }
       
   377     else
       
   378         {
       
   379         // Delete failed, update error info
       
   380         SetError( err, aFullPath );
       
   381         if ( err == KErrInUse )
       
   382             {
       
   383             ++iOpenFiles;
       
   384             }
       
   385         ++iNotDeletedItems;
       
   386         }
       
   387     }
       
   388 
       
   389 // -----------------------------------------------------------------------------
       
   390 // CFileManagerActiveDelete::IsDir
       
   391 // 
       
   392 // -----------------------------------------------------------------------------
       
   393 // 
       
   394 TBool CFileManagerActiveDelete::IsDir( const TDesC& aFullPath )
       
   395     {
       
   396     return CFileManagerUtils::HasFinalBackslash( aFullPath );
       
   397     }
       
   398 
       
   399 // -----------------------------------------------------------------------------
       
   400 // CFileManagerActiveDelete::SetName
       
   401 // 
       
   402 // -----------------------------------------------------------------------------
       
   403 // 
       
   404 void CFileManagerActiveDelete::SetName( const TDesC& aFullPath,
       
   405         TBool aOverWrite )
       
   406     {
       
   407     if ( !aOverWrite && iFileName.Length() )
       
   408         {
       
   409         return;
       
   410         }
       
   411     iFileName.Zero();
       
   412     if ( aFullPath.Length() )
       
   413         {
       
   414         if ( IsDir( aFullPath ) )
       
   415             {
       
   416             TParsePtrC parse( aFullPath.Left( aFullPath.Length() - 1 ) );
       
   417             iFileName.Append( parse.Name() );
       
   418             }
       
   419         else
       
   420             {
       
   421             TParsePtrC parse( aFullPath );
       
   422             iFileName.Append( parse.NameAndExt() );
       
   423             }
       
   424         }
       
   425     }
       
   426 
       
   427 // -----------------------------------------------------------------------------
       
   428 // CFileManagerActiveDelete::IsError
       
   429 // 
       
   430 // -----------------------------------------------------------------------------
       
   431 // 
       
   432 TBool CFileManagerActiveDelete::IsError( TInt aErr )
       
   433     {
       
   434     return ( aErr != KErrNone &&
       
   435             aErr != KErrCorrupt &&
       
   436             aErr != KErrNotFound &&
       
   437             aErr != KErrPathNotFound );
       
   438     }
       
   439 
       
   440 // -----------------------------------------------------------------------------
       
   441 // CFileManagerActiveDelete::SetError
       
   442 // 
       
   443 // -----------------------------------------------------------------------------
       
   444 // 
       
   445 void CFileManagerActiveDelete::SetError( TInt aErr, const TDesC& aFullPath )
       
   446     {
       
   447     if ( iError == KErrNone && IsError( aErr ) )
       
   448         {
       
   449         iError = aErr;
       
   450         SetName( aFullPath, ETrue );
       
   451         }
       
   452     }
       
   453 
       
   454 // -----------------------------------------------------------------------------
       
   455 // CFileManagerActiveDelete::FlushNotifications
       
   456 // -----------------------------------------------------------------------------
       
   457 //
       
   458 void CFileManagerActiveDelete::FlushNotifications()
       
   459     {
       
   460     if ( iRemovedItems->MdcaCount() > 0 )
       
   461         {
       
   462         //TRAP_IGNORE( iMgxFileManager->UpdateL( *iRemovedItems ) );
       
   463         iRemovedItems->Reset();
       
   464         }
       
   465     }
       
   466 
       
   467 // End of file