mmappcomponents/harvester/filehandler/src/mpxfolderscanner.cpp
changeset 0 a2952bb97e68
child 23 4740b34b83ce
equal deleted inserted replaced
-1:000000000000 0:a2952bb97e68
       
     1 /*
       
     2 * Copyright (c) 2006 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:  Folder scanning class to scan files in the file system
       
    15 *
       
    16 */
       
    17 
       
    18 #include <e32base.h>
       
    19 #include <mpxlog.h>
       
    20 #include "mpxfolderscanner.h"
       
    21 #include "mpxfileadditionobserver.h"
       
    22 #include "mpxfilescanstateobserver.h"
       
    23 #include "mpxfhcommon.h"
       
    24 
       
    25 // CONSTANTS
       
    26 const TInt KFileNumBreakCount = 5;
       
    27 
       
    28 // ======== LOCAL FUNCTIONS ========
       
    29 
       
    30 // ---------------------------------------------------------------------------
       
    31 // RArray compare function to compare strings
       
    32 // ---------------------------------------------------------------------------
       
    33 //
       
    34 static TInt CompareString(const TPath& aFirst,
       
    35                           const TPath& aSecond)
       
    36     {
       
    37     return aFirst.Compare( aSecond );
       
    38     }
       
    39     
       
    40 // ======== MEMBER FUNCTIONS ========
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 // Constructor
       
    44 // ---------------------------------------------------------------------------
       
    45 //
       
    46 CMPXFolderScanner::CMPXFolderScanner( MMPXFileAdditionObserver& aObserver,
       
    47                                       MMPXFileScanStateObserver& aStateObs,
       
    48                                       RFs& aFs  ) : CActive(EPriorityNull),
       
    49                                                   iObserver( aObserver ),
       
    50                                                   iStateObserver( aStateObs ),
       
    51                                                   iFs( aFs )
       
    52                                                     
       
    53     {
       
    54     CActiveScheduler::Add(this);
       
    55     }
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // 2nd Phase Constructor
       
    59 // ---------------------------------------------------------------------------
       
    60 //
       
    61 void CMPXFolderScanner::ConstructL()
       
    62     {
       
    63     iDirScan = CDirScan::NewL(iFs);
       
    64     }
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // Two Phased Constructor
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 CMPXFolderScanner* CMPXFolderScanner::NewL( MMPXFileAdditionObserver& aObserver,
       
    71                                             MMPXFileScanStateObserver& aStateObs,
       
    72                                             RFs& aFs )
       
    73     {
       
    74     CMPXFolderScanner* self = new( ELeave ) CMPXFolderScanner( aObserver, 
       
    75                                                                aStateObs,
       
    76                                                                aFs );
       
    77     CleanupStack::PushL( self );
       
    78     self->ConstructL();
       
    79     CleanupStack::Pop( self );
       
    80     return self;
       
    81     }
       
    82 
       
    83 
       
    84 // ---------------------------------------------------------------------------
       
    85 // Destructor
       
    86 // ---------------------------------------------------------------------------
       
    87 //
       
    88 CMPXFolderScanner::~CMPXFolderScanner()
       
    89     {
       
    90     Cancel();
       
    91     
       
    92     delete iDirScan;
       
    93     delete iDir;
       
    94     
       
    95     iDrivesToScan.Close();
       
    96     }
       
    97     
       
    98 // ---------------------------------------------------------------------------
       
    99 // Scans a list of drives for files
       
   100 // ---------------------------------------------------------------------------
       
   101 //
       
   102 void CMPXFolderScanner::ScanL( RArray<TPath>& aDrives )
       
   103     {
       
   104     MPX_DEBUG1("CMPXFolderScanner::ScanL <---");
       
   105     
       
   106     // Copy all the other drives we want to scan
       
   107     //
       
   108     TInt count( aDrives.Count() );
       
   109     for( TInt i=0; i<count; ++i )
       
   110         {
       
   111         // Check if we are already scanning this drive
       
   112         TInt found( iDrivesToScan.FindInOrder( aDrives[i], CompareString ) ); 
       
   113         if( found == KErrNotFound )
       
   114             {
       
   115             iDrivesToScan.Append( aDrives[i] );
       
   116             }
       
   117         }
       
   118     
       
   119     // If we were already scanning, don't do it again
       
   120     //
       
   121     if( !iScanning )
       
   122         {
       
   123         // Setup the next drive to scan
       
   124         //
       
   125         if( !SetupNextDriveToScanL() )
       
   126             {
       
   127             // Kick off the scanning
       
   128             iStatus = KRequestPending;
       
   129             SetActive();
       
   130             TRequestStatus* status = &iStatus;
       
   131             User::RequestComplete( status, KErrNone );
       
   132             
       
   133             // We've started scanning
       
   134             iScanning = ETrue;
       
   135             }
       
   136         else
       
   137             {
       
   138             // Nothing to scan
       
   139             DoScanCompleteL(KErrNone);    
       
   140             }        
       
   141         }
       
   142     MPX_DEBUG1("CMPXFolderScanner::ScanL --->");
       
   143     }
       
   144 
       
   145 // ---------------------------------------------------------------------------
       
   146 // Continue Scanning for more files
       
   147 // ---------------------------------------------------------------------------
       
   148 //
       
   149 TBool CMPXFolderScanner::DoScanL()
       
   150     {
       
   151     MPX_DEBUG1("CMPXFolderScanner::DoScanL <---");
       
   152     TBool done (EFalse);
       
   153     
       
   154     // Check each file in each directory
       
   155     TInt numFiles( iDir->Count() );
       
   156     while( iCount < numFiles )
       
   157         {
       
   158         TEntry entry = (*iDir)[iCount];
       
   159 
       
   160         // Construct the full path and file name
       
   161         TParse fullEntry;
       
   162         TPtrC dirPath(iDirScan->FullPath());
       
   163         fullEntry.Set(entry.iName, &dirPath, NULL);
       
   164         
       
   165         TPtrC fullname = fullEntry.FullName();
       
   166         TInt index = iObserver.IsMediaFileL( fullname );
       
   167         if( KErrNotFound != index )
       
   168             {
       
   169             iObserver.HandleFileAdditionL( fullname, index );
       
   170             }
       
   171         
       
   172         // Break if we have scanned enough files
       
   173         //
       
   174         ++iCount;
       
   175         if( iCount%KFileNumBreakCount == 0 )
       
   176             {
       
   177             return EFalse;
       
   178             }
       
   179         }
       
   180     
       
   181     // All files from this directory scanned, so move onto next
       
   182     //
       
   183     TInt err( KErrNone );
       
   184     TBool blocked (EFalse);
       
   185     if( iCount == numFiles )
       
   186         {
       
   187         // Get next Folder
       
   188         //
       
   189         iCount = 0;          
       
   190         delete iDir;
       
   191         iDir = NULL;
       
   192         do
       
   193             {
       
   194             TRAP(err, iDirScan->NextL(iDir));
       
   195             blocked = iObserver.IsPathBlockedL( iDirScan->FullPath() );
       
   196             if( blocked )
       
   197                 {
       
   198                 delete iDir;
       
   199                 iDir = NULL;
       
   200                 }
       
   201             if( err == KErrNotReady )
       
   202                 {
       
   203                 delete iDir;
       
   204                 iDir = NULL;
       
   205                 break;
       
   206                 }
       
   207             }
       
   208         while ( err == KErrPathNotFound || blocked );  
       
   209         
       
   210         // No more directories to scan on this drive
       
   211         //
       
   212         if( !iDir )
       
   213             {
       
   214             done = SetupNextDriveToScanL();  
       
   215             }
       
   216         }
       
   217         
       
   218     MPX_DEBUG1("CMPXFolderScanner::DoScanL --->");
       
   219     return done;
       
   220     }
       
   221 
       
   222 // ---------------------------------------------------------------------------
       
   223 // Setup the object to scan the next directory
       
   224 // ---------------------------------------------------------------------------
       
   225 //
       
   226 TBool CMPXFolderScanner::SetupNextDriveToScanL()
       
   227     {
       
   228     MPX_DEBUG1("CMPXFolderScanner::SetupNextDriveToScanL <---");
       
   229     
       
   230     TBool done(EFalse);
       
   231     TBool blocked(EFalse);
       
   232     // Scan next drive
       
   233     while( iDir == NULL && !done )
       
   234         {
       
   235         if( !iDrivesToScan.Count() )
       
   236             {
       
   237             // No more drives or folders that we are interested in
       
   238             done = ETrue;
       
   239             }
       
   240         else
       
   241             {
       
   242             iDirScan->SetScanDataL(iDrivesToScan[0], KEntryAttNormal, ESortNone);
       
   243             iCount = 0;
       
   244             TInt err(KErrNone);
       
   245             do
       
   246                 {
       
   247                 MPX_DEBUG1("CMPXFolderScanner::SetupNextDriveToScanL iDirScan->NexL()");
       
   248                 TRAP(err, iDirScan->NextL(iDir));
       
   249                 MPX_DEBUG1("CMPXFolderScanner::SetupNextDriveToScanL path blocked?");
       
   250                 blocked = iObserver.IsPathBlockedL( iDirScan->FullPath() );
       
   251                 MPX_DEBUG2("CMPXFolderScanner::SetupNextDriveToScanL path blocked %i", blocked);
       
   252                 if( blocked )
       
   253                     {
       
   254                     delete iDir;
       
   255                     iDir = NULL;
       
   256                     }
       
   257                 }
       
   258             while (err == KErrPathNotFound || blocked );  
       
   259             
       
   260             // If there was something to scan
       
   261             //
       
   262             if( iDir != NULL )
       
   263                 {
       
   264                 // Inform Observer of the new drive that we are scanning
       
   265                 iObserver.HandleOpenDriveL( ::ExtractDrive(iDrivesToScan[0]), 
       
   266                                             iDrivesToScan[0] );
       
   267                 }
       
   268                 
       
   269             // Remove the 0th element
       
   270             iDrivesToScan.Remove(0); 
       
   271             iDrivesToScan.Compress();
       
   272             }
       
   273         }
       
   274     
       
   275     
       
   276     MPX_DEBUG1("CMPXFolderScanner::SetupNextDriveToScanL --->");
       
   277     return done;
       
   278     }
       
   279     
       
   280 // ---------------------------------------------------------------------------
       
   281 // Handle when scanning is complete
       
   282 // ---------------------------------------------------------------------------
       
   283 //
       
   284 void CMPXFolderScanner::DoScanCompleteL( TInt aErr )
       
   285     {
       
   286     MPX_DEBUG1("CMPXFolderScanner::DoScanCompleteL <---");
       
   287     
       
   288     // Reset all arrays and data
       
   289     iDrivesToScan.Reset();
       
   290     
       
   291     // All done!
       
   292     iScanning = EFalse;
       
   293     
       
   294     delete iDir;
       
   295     iDir = NULL;
       
   296     
       
   297     // Callback to observer
       
   298     iStateObserver.HandleScanStateCompleteL( MMPXFileScanStateObserver::EScanFiles, 
       
   299                                               aErr );
       
   300     
       
   301     MPX_DEBUG1("CMPXFolderScanner::DoScanCompleteL --->");
       
   302     }
       
   303      
       
   304 // ---------------------------------------------------------------------------
       
   305 // From CActive
       
   306 // ---------------------------------------------------------------------------
       
   307 //
       
   308 void CMPXFolderScanner::RunL()
       
   309     {
       
   310     MPX_DEBUG1("CMPXFolderScanner::RunL <---");
       
   311     
       
   312     // Do more scanning
       
   313     TBool done(EFalse);
       
   314     TRAPD( err, done = DoScanL() );   
       
   315      
       
   316     // We are all done
       
   317     //
       
   318     if( KErrNone != err || done )
       
   319         {
       
   320         DoScanCompleteL( err );
       
   321         }
       
   322     else // if( !done )
       
   323         {
       
   324         MPX_DEBUG1("CMPXFolderScanner::RunL -- Run again");
       
   325         iStatus = KRequestPending;
       
   326         SetActive();
       
   327         TRequestStatus* status = &iStatus;
       
   328         User::RequestComplete( status, KErrNone );
       
   329         }
       
   330     }
       
   331 
       
   332 // ---------------------------------------------------------------------------
       
   333 // From CActive
       
   334 // ---------------------------------------------------------------------------
       
   335 //
       
   336 void CMPXFolderScanner::DoCancel()
       
   337     {
       
   338     if( iScanning )
       
   339         {
       
   340         // Callback to observer with the partial list?
       
   341         TRAP_IGNORE( DoScanCompleteL( KErrCancel ) );
       
   342         }
       
   343     }
       
   344     
       
   345 // ----------------------------------------------------------------------------
       
   346 // Handles a leave occurring in the request completion event handler RunL()
       
   347 // ----------------------------------------------------------------------------
       
   348 //
       
   349 TInt CMPXFolderScanner::RunError(TInt aError)
       
   350     {
       
   351     MPX_DEBUG2("CMPXFolderScanner::RunError(%d)", aError );
       
   352     
       
   353     TRAP_IGNORE( DoScanCompleteL( aError ) );
       
   354     
       
   355     return KErrNone;
       
   356     }