harvester/monitorplugins/mmcplugin/src/mmcscannerao.cpp
changeset 0 c53acadfccc6
child 1 acef663c1218
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Scans MMC after phone reboot for file changes*
       
    15 */
       
    16 
       
    17 #include "mmcscannerao.h"
       
    18 #include "harvesterlog.h"
       
    19 #include "fsutil.h"
       
    20 #include <harvesterdata.h>
       
    21 #include <placeholderdata.h>
       
    22 #include <centralrepository.h>
       
    23 
       
    24 _LIT( KColon, ":" );
       
    25 
       
    26 const TInt KEntryBufferSize = 100;
       
    27 const TInt KDefaultDelay = 4;
       
    28 const TInt KMillion = 1000000;
       
    29 
       
    30 const TUid KRepositoryUid = { 0x20007183 };
       
    31 const TUint32 KScanDelayKey = 0x00000001;
       
    32 
       
    33 CMmcScannerAO::CMmcScannerAO( TUint32 aMediaId, 
       
    34 		CMdEHarvesterSession* aMdEClient, MMonitorPluginObserver* aObserver, 
       
    35 		CHarvesterPluginFactory* aHarvesterPluginFactory, CActive::TPriority aPriority ) : 
       
    36 		CTimer( aPriority ), iState( EUninitialized ), iMmcFileList( NULL )   
       
    37 	{
       
    38 	iMediaId = aMediaId;
       
    39 	iMdEClient = aMdEClient;
       
    40 	iObserver = aObserver;
       
    41 	iHarvesterPluginFactory = aHarvesterPluginFactory;
       
    42 	}
       
    43 
       
    44 CMmcScannerAO* CMmcScannerAO::NewL( TUint32 aMediaId, CMdEHarvesterSession* aMdEClient,
       
    45 		MMonitorPluginObserver* aObserver, CHarvesterPluginFactory* aHarvesterPluginFactory, 
       
    46 		CActive::TPriority aPriority, TBool aAlreadyWaited )
       
    47 	{
       
    48 	CMmcScannerAO* self = new ( ELeave ) CMmcScannerAO( aMediaId, aMdEClient, aObserver, 
       
    49 			aHarvesterPluginFactory, aPriority );
       
    50 	
       
    51 	CleanupStack::PushL( self );
       
    52 	self->ConstructL( aAlreadyWaited );
       
    53 	CleanupStack::Pop( self );
       
    54 	return self;
       
    55 	}
       
    56 
       
    57 void CMmcScannerAO::ConstructL( TBool aAlreadyWaited )
       
    58 	{
       
    59 	CTimer::ConstructL();
       
    60 	CActiveScheduler::Add( this ); // Add to scheduler
       
    61 	iState = EUninitialized;
       
    62 	User::LeaveIfError( iFs.Connect() );
       
    63 	iMmcFileList = CMmcFileList::NewL();
       
    64 	
       
    65 	if( !aAlreadyWaited )
       
    66 	    {
       
    67         TInt tmpDelay( KDefaultDelay );
       
    68         TTimeIntervalMicroSeconds32 delay( tmpDelay * KMillion ); 
       
    69         CRepository* repo = CRepository::NewLC( KRepositoryUid );
       
    70         TInt err = repo->Get( KScanDelayKey, tmpDelay );
       
    71         if ( err == KErrNone )
       
    72             {
       
    73             delay = tmpDelay * KMillion;
       
    74             }
       
    75         CleanupStack::PopAndDestroy( repo );
       
    76         After( delay );
       
    77 	    }
       
    78 	else
       
    79 	    {
       
    80 	    TTimeIntervalMicroSeconds32 delay( 5 ); 
       
    81 	    After( delay );
       
    82 	    }
       
    83 	}
       
    84 
       
    85 CMmcScannerAO::~CMmcScannerAO()
       
    86 	{
       
    87 	Cancel(); // Cancel any request, if outstanding
       
    88 	// Delete instance variables if any
       
    89 	
       
    90 	delete iMmcFileList;
       
    91 	
       
    92 	iEntryArray.ResetAndDestroy();
       
    93 	iEntryArray.Close();
       
    94 
       
    95 	iHarvestEntryArray.ResetAndDestroy();
       
    96 	iHarvestEntryArray.Close();
       
    97 	
       
    98 	iFs.Close();
       
    99 	}
       
   100 
       
   101 
       
   102 void CMmcScannerAO::RunL()
       
   103 	{
       
   104 	switch( iState )
       
   105 		{
       
   106 		case( EUninitialized ):
       
   107 			{
       
   108 			WRITELOG("CMmcScannerAO::RunL - Setting files to not present");
       
   109 			iMdEClient->SetFilesToNotPresent( iMediaId, ETrue );
       
   110 			SetState( EReadFiles );
       
   111 			break;
       
   112 			}
       
   113 		
       
   114 		case( EReadFiles ):
       
   115 			{
       
   116 			for ( TInt i=0; i < KMaxDrives; i++ )
       
   117 				{
       
   118 				const TUint32 mediaId = FSUtil::MediaID(iFs, i);
       
   119 				if( mediaId == iMediaId )
       
   120 					{
       
   121 					TChar chr;
       
   122 					iFs.DriveToChar( i, chr );
       
   123 					i = KMaxDrives;
       
   124 					iDrive.Zero();
       
   125 					iDrive.Append( chr );
       
   126 					iDrive.Append( KColon );
       
   127 					}
       
   128 				}
       
   129 			// drive not found (unmount before scanning delay)
       
   130 			if ( iDrive.Length() == 0 )
       
   131 				{
       
   132 				SetState( EDone );
       
   133 				break;
       
   134 				}
       
   135 			
       
   136 			WRITELOG("CMmcScannerAO::RunL - build file list");
       
   137 			iMmcFileList->BuildFileListL( iFs, iDrive, iEntryArray );
       
   138 			SetState( EProcessFiles );
       
   139 			break;
       
   140 			}
       
   141 		
       
   142 		case( EProcessFiles ):
       
   143 			{
       
   144 			if( iEntryArray.Count() > 0 )
       
   145 				{
       
   146 				WRITELOG("CMmcScannerAO::RunL - handling file list");
       
   147 				iMmcFileList->HandleFileEntryL( *iMdEClient, iEntryArray, 
       
   148 						iHarvestEntryArray, iMediaId, iHarvesterPluginFactory );
       
   149 				SetState( EHarvestFiles );
       
   150 				}
       
   151 			else 
       
   152 				{
       
   153 				SetState( ERemoveNPFiles );
       
   154 				}
       
   155 			break;
       
   156 			}
       
   157 		
       
   158 		case( EHarvestFiles ):
       
   159 			{
       
   160 			if ( iHarvestEntryArray.Count() > 0 )
       
   161 				{
       
   162 				WRITELOG("CMmcScannerAO::RunL - adding new files to harvester queue");
       
   163 				HandleReharvestL();
       
   164 				SetState( EHarvestFiles );
       
   165 				}
       
   166 			else
       
   167 				{
       
   168 				SetState( EProcessFiles );
       
   169 				}
       
   170 			break;
       
   171 			}
       
   172 		
       
   173 		case( ERemoveNPFiles ):
       
   174 			{
       
   175 			WRITELOG("CMmcScannerAO::RunL - Removing not present files");
       
   176 			iMdEClient->RemoveFilesNotPresent( iMediaId, ETrue );
       
   177 			SetState( EDone );
       
   178 			break;
       
   179 			}
       
   180 		
       
   181 		case( EDone ):
       
   182 			{
       
   183 			iFs.Close();
       
   184 			break;
       
   185 			}
       
   186 		
       
   187 		default: 
       
   188 			break;
       
   189 		
       
   190 		}
       
   191 	}
       
   192 
       
   193 void CMmcScannerAO::HandleReharvestL()
       
   194 	{
       
   195 	WRITELOG("CMMCMountTaskAO::HandleReharvestL");
       
   196 		
       
   197 	TInt batchSize( 0 );
       
   198 	RPointerArray<CHarvesterData> hdArray;
       
   199 	CleanupClosePushL( hdArray );
       
   200 	
       
   201     if ( iHarvestEntryArray.Count() >= KEntryBufferSize )
       
   202         {
       
   203         batchSize = KEntryBufferSize;
       
   204         }
       
   205     else
       
   206         {
       
   207         batchSize = iHarvestEntryArray.Count();
       
   208         }
       
   209         
       
   210     for ( TInt i = 0; i < batchSize; i++ )
       
   211         {
       
   212         CPlaceholderData* ei = iHarvestEntryArray[0];
       
   213 
       
   214         HBufC* fileName = ei->Uri().AllocLC();
       
   215         CHarvesterData* hd = CHarvesterData::NewL( fileName );
       
   216 		hd->SetOrigin( MdeConstants::Object::EOther );
       
   217 		CleanupStack::Pop( fileName );
       
   218 
       
   219 		if ( ei->PresentState() == EMdsPlaceholder || 
       
   220 			 ei->PresentState() == EMdsModified )
       
   221 			{
       
   222 			hd->SetEventType( EHarvesterEdit );
       
   223 			hd->SetObjectType( ENormal );
       
   224 			delete ei;
       
   225 			}
       
   226 		else
       
   227 			{
       
   228 			hd->SetEventType( EHarvesterAdd );
       
   229 			hd->SetObjectType( EPlaceholder );
       
   230 			hd->SetClientData( ei );
       
   231 			}
       
   232 		hdArray.Append( hd );
       
   233         iHarvestEntryArray.Remove( 0 );
       
   234         }
       
   235 
       
   236     if( iHarvestEntryArray.Count() == 0 )
       
   237     	{
       
   238     	iHarvestEntryArray.Compress();
       
   239     	}
       
   240     
       
   241 	if ( iObserver )
       
   242 		{
       
   243 		if( hdArray.Count() > 0)
       
   244 			{
       
   245 			iObserver->MonitorEvent( hdArray );
       
   246 			}
       
   247 		else
       
   248 			{
       
   249 			iObserver->MonitorEvent( hdArray[0] );
       
   250 			}
       
   251 		}
       
   252 	
       
   253 	CleanupStack::PopAndDestroy( &hdArray ); 
       
   254 	}
       
   255 	
       
   256 
       
   257 TInt CMmcScannerAO::RunError( TInt /*aError*/ )
       
   258 	{
       
   259 	return KErrNone;
       
   260 	}
       
   261 
       
   262 void CMmcScannerAO::SetState( TCMmcScannerAOState aState )
       
   263 	{
       
   264 	WRITELOG("CMmcScannerAO::SetNextRequest" );
       
   265 	if ( !IsActive() )
       
   266 		{
       
   267 		iState = aState;
       
   268 		TRequestStatus* ptrStatus = &iStatus;
       
   269 		User::RequestComplete( ptrStatus, KErrNone );
       
   270 		SetActive();
       
   271 		}
       
   272 	}