harvesterplugins/file/src/cfileharvester.cpp
changeset 0 ccd0fd43f247
child 2 208a4ba3894c
equal deleted inserted replaced
-1:000000000000 0:ccd0fd43f247
       
     1 /*
       
     2 * Copyright (c) 2010 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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDES 
       
    20 #include <e32base.h>
       
    21 #include <pathinfo.h>
       
    22 #include <bautils.h>
       
    23 
       
    24 // For Logging
       
    25 #include "harvesterserverlogger.h"
       
    26 #include "cfileharvester.h"
       
    27 
       
    28 // CONSTANTS
       
    29 _LIT( KFileMask, "*.*" );
       
    30 const TInt KItemsPerRun = 1;
       
    31 
       
    32 // ============================ MEMBER FUNCTIONS ===============================
       
    33 
       
    34 // ---------------------------------------------------------------------------
       
    35 // CFileHarvester::NewL
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 CFileHarvester* CFileHarvester::NewL( CFilePlugin& aFilePlugin, RFs& aFs )
       
    39 	{
       
    40 	CFileHarvester* self = new (ELeave) CFileHarvester( aFilePlugin, aFs );
       
    41 	CleanupStack::PushL(self);
       
    42 	self->ConstructL();
       
    43 	CleanupStack::Pop(self);
       
    44 	return self;
       
    45 	}
       
    46 
       
    47 // ---------------------------------------------------------------------------
       
    48 // CFileHarvester::~CFileHarvester
       
    49 // ---------------------------------------------------------------------------
       
    50 //
       
    51 CFileHarvester::~CFileHarvester()
       
    52     {
       
    53     Cancel();
       
    54     delete iDir;
       
    55     delete iDirscan;
       
    56     iIgnorePaths.ResetAndDestroy();
       
    57     iIgnorePaths.Close();
       
    58     }
       
    59 
       
    60 
       
    61 // ---------------------------------------------------------------------------
       
    62 // CFileHarvester::CFileHarvester
       
    63 // ---------------------------------------------------------------------------
       
    64 //
       
    65 CFileHarvester::CFileHarvester( CFilePlugin& aFilePlugin, RFs& aFs )
       
    66   : CActive( CActive::EPriorityIdle ),
       
    67     iFilePlugin( aFilePlugin ),
       
    68     iFs( aFs )
       
    69     {
       
    70     CPIXLOGSTRING("ENTER CFileHarvester::CFileHarvester");
       
    71     CActiveScheduler::Add( this );
       
    72     CPIXLOGSTRING("END CFileHarvester::CFileHarvester");
       
    73     }
       
    74 
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // CFileHarvester::ConstructL
       
    78 // ---------------------------------------------------------------------------
       
    79 //
       
    80 void CFileHarvester::ConstructL()
       
    81     {
       
    82     iDirscan = CDirScan::NewL( iFs );
       
    83     }
       
    84 
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 // CFileHarvester::StartL
       
    88 // ---------------------------------------------------------------------------
       
    89 //
       
    90 void CFileHarvester::StartL( const TDriveNumber aDriveNumber )
       
    91     {
       
    92     CPIXLOGSTRING("ENTER CFileHarvester::Start");
       
    93 
       
    94     TFileName rootPath;
       
    95     User::LeaveIfError( PathInfo::GetRootPath( rootPath, aDriveNumber ) );
       
    96 
       
    97 	iDirscan->SetScanDataL( rootPath, KEntryAttDir|KEntryAttMatchExclusive,
       
    98 				ESortNone, // No need to sort data
       
    99 				CDirScan::EScanDownTree );
       
   100 	iCurrentHarvestDrive = aDriveNumber;
       
   101 	SetNextRequest( EHarvesterStartHarvest );
       
   102     CPIXLOGSTRING("END CFileHarvester::Start");
       
   103     }
       
   104 
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CFileHarvester::AddIgnorePathsL
       
   108 // -----------------------------------------------------------------------------
       
   109 //
       
   110 void CFileHarvester::AddIgnorePathsL( const TDriveNumber aDriveNumber )
       
   111     {
       
   112     TFileName ignorePath;
       
   113     TChar chr;
       
   114     User::LeaveIfError( RFs::DriveToChar( aDriveNumber, chr ) );
       
   115 
       
   116     ignorePath.Append( chr );
       
   117     ignorePath.Append( KExcludePathSystem );
       
   118     AddIgnorePathL( ignorePath );
       
   119     CPIXLOGSTRING2("CFileHarvester::AddIgnorePathsL - AddIgnorePath: %S", &ignorePath );
       
   120 
       
   121     ignorePath.Zero();
       
   122 
       
   123     // As index databases are located under \\Private\\ path,
       
   124     // this ignore path will mean index databases are also ignored.
       
   125     ignorePath.Append( chr );
       
   126     ignorePath.Append( KExcludePathPrivate );
       
   127     AddIgnorePathL( ignorePath );
       
   128     CPIXLOGSTRING2("CFileHarvester::AddIgnorePathsL - AddIgnorePath: %S", &ignorePath );
       
   129 
       
   130     ignorePath.Zero();
       
   131 
       
   132     // Maps data must not be indexed
       
   133     ignorePath.Append( chr );
       
   134     ignorePath.Append( KExcludePathMapsCities );
       
   135     AddIgnorePathL( ignorePath );
       
   136     CPIXLOGSTRING2("CFileHarvester::AddIgnorePathsL - AddIgnorePath: %S", &ignorePath );
       
   137     }
       
   138 
       
   139 
       
   140 // -----------------------------------------------------------------------------
       
   141 // CFileHarvester::RemoveIgnorePaths
       
   142 // -----------------------------------------------------------------------------
       
   143 //
       
   144 void CFileHarvester::RemoveIgnorePaths( const TDriveNumber aDriveNumber )
       
   145     {
       
   146     TFileName ignorePath;
       
   147     TChar chr;
       
   148     RFs::DriveToChar( aDriveNumber, chr );
       
   149 
       
   150     ignorePath.Append( chr );
       
   151     ignorePath.Append( KExcludePathSystem );
       
   152     RemoveIgnorePath( ignorePath );
       
   153     CPIXLOGSTRING2("CFileHarvester::RemoveIgnorePaths - RemoveIgnorePath: %S", &ignorePath );
       
   154 
       
   155     ignorePath.Zero();
       
   156 
       
   157     // As index databases are located under \\Private\\ path,
       
   158     // this ignore path will mean index databases are also ignored.
       
   159     ignorePath.Append( chr );
       
   160     ignorePath.Append( KExcludePathPrivate );
       
   161     RemoveIgnorePath( ignorePath );
       
   162     CPIXLOGSTRING2("CFileHarvester::RemoveIgnorePaths - RemoveIgnorePath: %S", &ignorePath );
       
   163 
       
   164     ignorePath.Zero();
       
   165 
       
   166     // Maps
       
   167     ignorePath.Append( chr );
       
   168     ignorePath.Append( KExcludePathMapsCities );
       
   169     RemoveIgnorePath( ignorePath );
       
   170     CPIXLOGSTRING2("CFileHarvester::RemoveIgnorePaths - RemoveIgnorePath: %S", &ignorePath );
       
   171     }
       
   172 
       
   173 
       
   174 // -----------------------------------------------------------------------------
       
   175 // CFileHarvester::AddIgnorePathL
       
   176 // -----------------------------------------------------------------------------
       
   177 //
       
   178 TInt CFileHarvester::AddIgnorePathL( const TFileName& aIgnorePath )
       
   179     {
       
   180     TInt err( KErrNotFound );
       
   181 
       
   182     if ( aIgnorePath.Length() > 0 )
       
   183         {
       
   184         // check if already exist
       
   185         if ( iIgnorePaths.Count() > 0 )
       
   186             {
       
   187             for ( TInt i = 0; i < iIgnorePaths.Count(); i++ )
       
   188                 {
       
   189                 TFileName* tf = iIgnorePaths[i];
       
   190                 if( tf->Compare(aIgnorePath) == 0 )
       
   191 					{
       
   192 					return KErrNone;
       
   193 					}
       
   194 				}
       
   195 			}
       
   196         TFileName* fn = new(ELeave) TFileName;
       
   197         
       
   198         if ( fn )
       
   199             {
       
   200             fn->Copy( aIgnorePath );
       
   201             iIgnorePaths.Append( fn ); // ownership is transferred
       
   202             }
       
   203         else
       
   204         	{
       
   205         	err = KErrNoMemory;
       
   206         	}
       
   207         }
       
   208     else
       
   209         {
       
   210         err = KErrNotFound;
       
   211         }
       
   212         
       
   213     return err;
       
   214 	}
       
   215 
       
   216 
       
   217 // -----------------------------------------------------------------------------
       
   218 // CFileHarvester::RemoveIgnorePath
       
   219 // -----------------------------------------------------------------------------
       
   220 //
       
   221 TInt CFileHarvester::RemoveIgnorePath( const TFileName& aIgnorePath )
       
   222     {
       
   223     TInt err( KErrNotFound );
       
   224 
       
   225     if ( aIgnorePath.Length() > 0 )
       
   226         {
       
   227         // check if already exist
       
   228         if ( iIgnorePaths.Count() > 0 )
       
   229             {
       
   230             for ( TInt i = 0; i < iIgnorePaths.Count(); i++ )
       
   231                 {
       
   232                 TFileName* tf = iIgnorePaths[i];
       
   233                 if ( tf->Compare(aIgnorePath) == 0 )
       
   234                     {
       
   235                     CPIXLOGSTRING2( "CFileHarvester::RemoveIgnorePath() - remove path: %S", &aIgnorePath );
       
   236                     delete tf;
       
   237                     tf = NULL;
       
   238                     iIgnorePaths.Remove( i );
       
   239                     }
       
   240                 }
       
   241             }
       
   242         }
       
   243     else
       
   244         {
       
   245         err = KErrNotFound;
       
   246         }
       
   247         
       
   248     return err;
       
   249     }
       
   250 
       
   251 
       
   252 // -----------------------------------------------------------------------------
       
   253 // CFileHarvester::CheckPath
       
   254 // -----------------------------------------------------------------------------
       
   255 //
       
   256 TBool CFileHarvester::CheckPath( const TFileName& aFileName )
       
   257 	{
       
   258 	// check if ignored pathlist
       
   259 	const TInt count( iIgnorePaths.Count() );
       
   260 
       
   261 	if ( count > 0 )
       
   262 		{
       
   263 		for ( TInt i = 0; i < count; i++ )
       
   264 			{    
       
   265 			TFileName* pathName = iIgnorePaths[i];
       
   266 			pathName->LowerCase();
       
   267 			TFileName tempFileName;
       
   268 			tempFileName.Copy( aFileName );
       
   269 			tempFileName.LowerCase();
       
   270 			if ( tempFileName.Find(*pathName) != KErrNotFound )
       
   271 				{
       
   272 				CPIXLOGSTRING( "CFileHarvester::CheckPath() - is ignore path" );
       
   273 				return EFalse;
       
   274 				}
       
   275 			}
       
   276 		}
       
   277 	return ETrue;
       
   278 	}
       
   279 
       
   280 
       
   281 // ---------------------------------------------------------------------------
       
   282 // CFileHarvester::GetNextFolderL
       
   283 // ---------------------------------------------------------------------------
       
   284 //		
       
   285 void CFileHarvester::GetNextFolderL()
       
   286     {
       
   287     CPIXLOGSTRING("ENTER CFileHarvester::GetNextFolderL");
       
   288 
       
   289     delete iDir;
       
   290     iDir = NULL;
       
   291     // Documentation: CDirScan::NextL() The caller of this function 
       
   292     // is responsible for deleting iDir after the function has returned.
       
   293     iDirscan->NextL(iDir);
       
   294 
       
   295     if ( iDir )
       
   296     	{
       
   297         // if folder is in ignore path then skip it
       
   298         if ( !CheckPath( iDirscan->FullPath() ) )
       
   299         	{
       
   300 			CPIXLOGSTRING("CFileHarvester::GetNextFolderL - IF EHarvesterStartHarvest");
       
   301         	SetNextRequest( EHarvesterStartHarvest );
       
   302         	}
       
   303         else
       
   304 			{
       
   305 			CPIXLOGSTRING("CFileHarvester::GetNextFolderL - IF EHarvesterGetFileId");
       
   306 			SetNextRequest( EHarvesterGetFileId );
       
   307 			}
       
   308     	}
       
   309     else
       
   310         {
       
   311         CPIXLOGSTRING("CFileHarvester::GetNextFolderL - IF EHarvesterIdle");
       
   312         SetNextRequest( EHarvesterIdleState );
       
   313         }
       
   314     CPIXLOGSTRING("END CFileHarvester::GetNextFolderL");
       
   315     }
       
   316 
       
   317 
       
   318 // ---------------------------------------------------------------------------
       
   319 // CFileHarvester::GetFileIdL
       
   320 // 
       
   321 // ---------------------------------------------------------------------------
       
   322 //
       
   323 void CFileHarvester::GetFileIdL()
       
   324     {
       
   325     CPIXLOGSTRING("ENTER CFileHarvester::GetFileId");
       
   326 
       
   327     if( iCurrentIndex == 0 )
       
   328         {
       
   329         TParse parse;
       
   330         parse.Set(KFileMask(), &( iDirscan->FullPath() ), NULL);
       
   331 
       
   332         // FindWildByPath assigns iDir = NULL, then allocates the memory for it.
       
   333         // Therefore must delete iDir first.
       
   334         delete iDir;
       
   335         iDir = NULL;
       
   336 
       
   337         TFindFile find( iFs );
       
   338         find.FindWildByPath(parse.FullName(), NULL, iDir);
       
   339         }
       
   340 
       
   341     if( iDir )
       
   342         {
       
   343         const TInt count(iDir->Count());
       
   344         while( ( iCurrentIndex < count ) && ( iStepNumber < KItemsPerRun ) )
       
   345             {
       
   346             TEntry aEntry = (*iDir)[iCurrentIndex];
       
   347             // Check if entry is a hidden or system file
       
   348             // if true -> continue until find something to index or have checked whole directory
       
   349             if( !aEntry.IsHidden() && !aEntry.IsSystem() && !aEntry.IsDir() )
       
   350                 {
       
   351                 TParse fileParser;
       
   352                 fileParser.Set( iDirscan->FullPath(), &(*iDir)[iCurrentIndex].iName, NULL );
       
   353                 iFilePlugin.CreateFileIndexItemL(fileParser.FullName(), ECPixAddAction);
       
   354                 // TODO: If this is not TRAPPED, state machine breaks 
       
   355                 iStepNumber++;
       
   356                 }
       
   357             iCurrentIndex++;
       
   358             }
       
   359 
       
   360         iStepNumber = 0;
       
   361 
       
   362         if( iCurrentIndex >= count )
       
   363             {
       
   364             iCurrentIndex = 0;
       
   365             SetNextRequest( EHarvesterStartHarvest );
       
   366             }
       
   367         else
       
   368             {
       
   369             SetNextRequest( EHarvesterGetFileId );
       
   370             }
       
   371         }
       
   372     else
       
   373         {
       
   374         SetNextRequest( EHarvesterStartHarvest );
       
   375         }
       
   376     CPIXLOGSTRING("END CFileHarvester::GetFileId");
       
   377     }
       
   378 
       
   379 
       
   380 // -----------------------------------------------------------------------------
       
   381 // CFileHarvester::DoCancel
       
   382 // -----------------------------------------------------------------------------
       
   383 //   
       
   384 void CFileHarvester::DoCancel()
       
   385 	{
       
   386     CPIXLOGSTRING("CFileHarvester::DoCancel");
       
   387 	}
       
   388 
       
   389 
       
   390 // -----------------------------------------------------------------------------
       
   391 // CFileHarvester::RunL
       
   392 // -----------------------------------------------------------------------------
       
   393 //   
       
   394 void CFileHarvester::RunL()
       
   395     {
       
   396     CPIXLOGSTRING("ENTER CFileHarvester::RunL");
       
   397     // Simple Round-Robin scheduling.
       
   398     Deque();
       
   399     CActiveScheduler::Add( this );
       
   400 
       
   401     switch ( iHarvestState )
       
   402         {
       
   403 		case EHarvesterIdleState:
       
   404 			{
       
   405 			iFilePlugin.HarvestingCompleted(iCurrentHarvestDrive, KErrNone);
       
   406 			break;
       
   407 			}
       
   408 
       
   409 		case EHarvesterGetFileId:
       
   410 			{
       
   411 			GetFileIdL();
       
   412 			break;
       
   413 			}
       
   414 		
       
   415 		case EHarvesterStartHarvest:
       
   416 			{
       
   417 			GetNextFolderL();
       
   418 			break;
       
   419 			}
       
   420 
       
   421 		default:
       
   422 			break;
       
   423 		}
       
   424 	CPIXLOGSTRING("END CFileHarvester::RunL");
       
   425 	}
       
   426 
       
   427 
       
   428 // -----------------------------------------------------------------------------
       
   429 // CFileHarvester::RunError
       
   430 // -----------------------------------------------------------------------------
       
   431 //   
       
   432 TInt CFileHarvester::RunError(TInt aError)
       
   433 	{
       
   434     CPIXLOGSTRING2("CFileHarvester::RunError - aError: %d", aError );
       
   435 	iHarvestState = EHarvesterIdleState;
       
   436     iFilePlugin.HarvestingCompleted(iCurrentHarvestDrive, aError);
       
   437 	return KErrNone;
       
   438 	}
       
   439 
       
   440 
       
   441 // ---------------------------------------------------------------------------
       
   442 // SetNextRequest
       
   443 // ---------------------------------------------------------------------------
       
   444 //
       
   445 void CFileHarvester::SetNextRequest( TFileHarvesterState aState )
       
   446     {
       
   447     CPIXLOGSTRING("CFileHarvester::SetNextRequest");
       
   448     if ( !IsActive() )
       
   449         {
       
   450         iHarvestState = aState;
       
   451         SetActive();
       
   452         TRequestStatus* status = &iStatus;
       
   453         User::RequestComplete( status, KErrNone );
       
   454         }
       
   455     }
       
   456 
       
   457 
       
   458 // End of File