testexecfw/useremul/src/ScanFolders.cpp
changeset 0 3e07fef1e154
equal deleted inserted replaced
-1:000000000000 0:3e07fef1e154
       
     1 /*------------------------------------------------------------------
       
     2  -
       
     3  * Software Name : UserEmulator
       
     4  * Version       : v4.2.1309
       
     5  * 
       
     6  * Copyright (c) 2009 France Telecom. All rights reserved.
       
     7  * This software is distributed under the License 
       
     8  * "Eclipse Public License - v 1.0" the text of which is available
       
     9  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
    10  *
       
    11  * Initial Contributors:
       
    12  * France Telecom 
       
    13  *
       
    14  * Contributors:
       
    15  *------------------------------------------------------------------
       
    16  -
       
    17  * File Name: ScanFolders.cpp
       
    18  * 
       
    19  * Created: 13/08/2009
       
    20  * Author(s): Marcell Kiss, Reshma Sandeep Das
       
    21  *   
       
    22  * Description:
       
    23  * Active object implementation for folder scanning operation
       
    24  *------------------------------------------------------------------
       
    25  -
       
    26  *
       
    27  */
       
    28 
       
    29 //System Includes
       
    30 #include <EikApp.h>
       
    31 #include <EikEnv.h>
       
    32 #include <F32File.h>
       
    33 
       
    34 //User Includes
       
    35 #include "ScanFolders.h"
       
    36 #include "FolderContentsListener.h"
       
    37 #include "UserEmulatorScriptsView.h"
       
    38 
       
    39 //Constants
       
    40 const TInt KDRIVESIZE      = 3;
       
    41 
       
    42 // -----------------------------------------------------------------------------
       
    43 // CScanFolders::NewL
       
    44 // -----------------------------------------------------------------------------
       
    45 //
       
    46 CScanFolders* CScanFolders::NewL()
       
    47 {
       
    48 	CScanFolders* self = CScanFolders::NewLC();
       
    49 	CleanupStack::Pop(self);
       
    50 	return self;
       
    51 }
       
    52 // -----------------------------------------------------------------------------
       
    53 // CScanFolders::NewLC
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 CScanFolders* CScanFolders::NewLC()
       
    57 {
       
    58 	CScanFolders* self = new ( ELeave ) CScanFolders;
       
    59 	CleanupStack::PushL(self);
       
    60 	self->ConstructL();
       
    61 	return self;	
       
    62 }
       
    63 // -----------------------------------------------------------------------------
       
    64 // CScanFolders::CScanFolders
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 CScanFolders::CScanFolders() : CActive( EPriorityStandard )
       
    68 {
       
    69 
       
    70 }
       
    71 // -----------------------------------------------------------------------------
       
    72 // CScanFolders::~CScanFolders
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 CScanFolders::~CScanFolders()
       
    76 {
       
    77 	Cancel();
       
    78 	iListeners.Close();
       
    79 	iCurrentDir.Close();
       
    80 	iFs.Close();
       
    81 
       
    82 	delete iDirectories; 
       
    83 	iDirectories = NULL;
       
    84 	delete iFileNames; 
       
    85 	iFileNames = NULL;
       
    86 }
       
    87 // -----------------------------------------------------------------------------
       
    88 // CScanFolders::ConstructL
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 void CScanFolders::ConstructL()
       
    92 {
       
    93 	// Create an array to hold the directories
       
    94 	iDirectories = new ( ELeave ) CDesCArrayFlat( 10 );
       
    95 
       
    96 	// Create an array to hold the full filenames
       
    97 	iFileNames = new ( ELeave ) CDesCArrayFlat( 10 );
       
    98 
       
    99 	// Create a file server session
       
   100 	User::LeaveIfError(iFs.Connect());
       
   101 
       
   102 	// Register this as an active object (for the async file scanning)
       
   103 	CActiveScheduler::Add( this );
       
   104 }
       
   105 // -----------------------------------------------------------------------------
       
   106 // CScanFolders::AddListenerL
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 void CScanFolders::AddListenerL( CUserEmulatorScriptsView* aListener )
       
   110 {
       
   111 	if ( iListeners.Find( aListener ) != KErrNotFound )
       
   112 	{
       
   113 		User::Leave(KErrAlreadyExists);
       
   114 	}
       
   115 	iListeners.Append( aListener );
       
   116 }
       
   117 // -----------------------------------------------------------------------------
       
   118 // CScanFolders::RemoveListener
       
   119 // -----------------------------------------------------------------------------
       
   120 //
       
   121 void CScanFolders::RemoveListener( CUserEmulatorScriptsView * aListener )
       
   122 {
       
   123 	TInt index = iListeners.Find( aListener );
       
   124 	iListeners.Remove( index );
       
   125 }
       
   126 // -----------------------------------------------------------------------------
       
   127 // CScanFolders::SetFilter
       
   128 // -----------------------------------------------------------------------------
       
   129 //
       
   130 void CScanFolders::SetFilter( const TDesC & aFilter )
       
   131 {
       
   132 	iFileFilter.Copy( aFilter );
       
   133 	if ( iFileFilter[ iFileFilter.Length() - 1 ] != ';' )
       
   134 	{
       
   135 		iFileFilter.Append( ';' );
       
   136 	}
       
   137 }
       
   138 // -----------------------------------------------------------------------------
       
   139 // CScanFolders::SetFilter
       
   140 // -----------------------------------------------------------------------------
       
   141 //
       
   142 void CScanFolders::SetFilter( const TDesC8 & aFilter )
       
   143 {
       
   144 	iFileFilter.Copy( aFilter );
       
   145 	if ( iFileFilter[ iFileFilter.Length() - 1 ] != ';' )
       
   146 	{
       
   147 		iFileFilter.Append( ';' );
       
   148 	}
       
   149 }
       
   150 // -----------------------------------------------------------------------------
       
   151 // CScanFolders::ScanFolderL
       
   152 // -----------------------------------------------------------------------------
       
   153 //
       
   154 void CScanFolders::ScanFolderL( const TDesC & aPath )
       
   155 {
       
   156 
       
   157 	iCurrentPath.Set( aPath, NULL, NULL );
       
   158 
       
   159 	if ( IsActive() )
       
   160 	{
       
   161 		iStartNewScan = ETrue;
       
   162 	}
       
   163 	else
       
   164 	{
       
   165 		StartScanningL();
       
   166 	}
       
   167 }
       
   168 // -----------------------------------------------------------------------------
       
   169 // CScanFolders::ScanFolderL
       
   170 // -----------------------------------------------------------------------------
       
   171 //
       
   172 void CScanFolders::ScanFolderL( const TDesC8 & aPath )
       
   173 {
       
   174 	TBuf< KBuffer256 > temp;
       
   175 	temp.Copy( aPath );
       
   176 	ScanFolderL( temp );
       
   177 }
       
   178 // -----------------------------------------------------------------------------
       
   179 // CScanFolders::DoCancel
       
   180 // -----------------------------------------------------------------------------
       
   181 //
       
   182 void CScanFolders::DoCancel()
       
   183 {
       
   184 	iAbortScan = ETrue;
       
   185 }
       
   186 // -----------------------------------------------------------------------------
       
   187 // CScanFolders::RunL
       
   188 // -----------------------------------------------------------------------------
       
   189 //
       
   190 void CScanFolders::RunL()
       
   191 {
       
   192 	switch ( iScanState )
       
   193 	{
       
   194 	case EIdle:
       
   195 		User::Panic(KIDLE, 0);
       
   196 		break;
       
   197 
       
   198 	case EScanning:
       
   199 		{
       
   200 			if ( iStatus == KErrNone )
       
   201 			{
       
   202 				FireEntriesL();
       
   203 				Reset();
       
   204 
       
   205 				// After the initial entries have been fired, set the
       
   206 				// #iContinued flag so that #FolderStarting is not called
       
   207 				// again for this reading session.
       
   208 				iContinued = ETrue;
       
   209 
       
   210 				ContinueScanning();
       
   211 			}
       
   212 			else if ( iStatus == KErrEof )
       
   213 			{
       
   214 				// Before delivering this last set of entries, set the
       
   215 				// #iComplete flag so that #FolderComplete is called in
       
   216 				// #FireEntries.
       
   217 				iComplete = ETrue;
       
   218 
       
   219 				FireEntriesL();
       
   220 				Reset();
       
   221 				StopScanning();
       
   222 			}
       
   223 			else
       
   224 			{
       
   225 				Reset();
       
   226 				FireError( iStatus.Int() );
       
   227 			}
       
   228 		}
       
   229 		break;
       
   230 
       
   231 	case EStartNewScan:
       
   232 		Reset();
       
   233 		StartScanningL();
       
   234 		break;
       
   235 
       
   236 	case EAbortScan:
       
   237 		Reset();
       
   238 		break;
       
   239 	}
       
   240 }
       
   241 // -----------------------------------------------------------------------------
       
   242 // CScanFolders::StartScanningL
       
   243 // -----------------------------------------------------------------------------
       
   244 //
       
   245 void CScanFolders::StartScanningL()
       
   246 {
       
   247 	if ( iCurrentPath.FullName().Compare( KPATHDELIM ) == 0 )
       
   248 	{
       
   249 		DeliverDrivesL();
       
   250 		return;
       
   251 	}
       
   252 
       
   253 	TInt error = iCurrentDir.Open( iFs, iCurrentPath.FullName(),
       
   254 		KEntryAttNormal|KEntryAttDir|KEntryAttReadOnly|KEntryAttSystem|KEntryAttHidden );
       
   255 
       
   256 	if ( error != KErrNone )
       
   257 	{
       
   258 		FireError( error );
       
   259 		return;
       
   260 	}
       
   261 
       
   262 	iContinued = EFalse;
       
   263 	iComplete = EFalse;
       
   264 
       
   265 	ContinueScanning();
       
   266 }
       
   267 
       
   268 // -----------------------------------------------------------------------------
       
   269 // CScanFolders::ContinueScanning
       
   270 // -----------------------------------------------------------------------------
       
   271 //
       
   272 void CScanFolders::ContinueScanning()
       
   273 {
       
   274 	iCurrentDir.Read( iEntries, iStatus );
       
   275 	if(!IsActive())
       
   276 		SetActive();
       
   277 
       
   278 	iScanState = EScanning;
       
   279 }
       
   280 // -----------------------------------------------------------------------------
       
   281 // CScanFolders::StopScanning
       
   282 // -----------------------------------------------------------------------------
       
   283 //
       
   284 void CScanFolders::StopScanning()
       
   285 {
       
   286 	iCurrentDir.Close();
       
   287 }
       
   288 // -----------------------------------------------------------------------------
       
   289 // CScanFolders::DeliverDrivesL
       
   290 // -----------------------------------------------------------------------------
       
   291 //
       
   292 void CScanFolders::DeliverDrivesL()
       
   293 {
       
   294 	FireFolderStarting( ETrue );
       
   295 
       
   296 	TDriveList drives;
       
   297 	User::LeaveIfError( iFs.DriveList( drives ) );
       
   298 
       
   299 	for ( TInt idx = 0; idx < KMaxDrives; idx++ )
       
   300 	{
       
   301 		if ( drives[ idx ] == 0 )
       
   302 		{
       
   303 			continue;
       
   304 		}
       
   305 		TChar driveLetter;
       
   306 		User::LeaveIfError( iFs.DriveToChar( idx, driveLetter ) );
       
   307 
       
   308 		TBuf< KDRIVESIZE > temp;
       
   309 		temp.Append( driveLetter );
       
   310 		temp.Append( KDRIVEDEL );
       
   311 
       
   312 		FireFolder( temp );
       
   313 	}
       
   314 
       
   315 	FireFolderCompleteL();
       
   316 	Reset();
       
   317 }
       
   318 // -----------------------------------------------------------------------------
       
   319 // CScanFolders::Reset
       
   320 // -----------------------------------------------------------------------------
       
   321 //
       
   322 void CScanFolders::Reset()
       
   323 {
       
   324 	iDirectories->Reset();
       
   325 	iFileNames->Reset();
       
   326 
       
   327 	iScanState = EIdle;
       
   328 }
       
   329 // -----------------------------------------------------------------------------
       
   330 // CScanFolders::FireError
       
   331 // -----------------------------------------------------------------------------
       
   332 //
       
   333 void CScanFolders::FireError( TInt aReasonCode )
       
   334 {
       
   335 	for ( TInt idx = 0; idx < iListeners.Count(); idx++ )
       
   336 	{
       
   337 		iListeners[ idx ]->ErrorOccured( aReasonCode );
       
   338 	}
       
   339 }
       
   340 // -----------------------------------------------------------------------------
       
   341 // CScanFolders::FireEntriesL
       
   342 // -----------------------------------------------------------------------------
       
   343 //
       
   344 void CScanFolders::FireEntriesL()
       
   345 {
       
   346 	if ( iContinued == EFalse )
       
   347 	{
       
   348 		FireFolderStarting( EFalse );
       
   349 	}
       
   350 
       
   351 	for ( TInt idx = 0; idx < iEntries.Count(); idx++ )
       
   352 	{
       
   353 		if ( iEntries[ idx ].IsDir() || IsMatch( iEntries[ idx ].iName ) )
       
   354 		{
       
   355 			FireEntry( iEntries[ idx ] );
       
   356 		}
       
   357 	}
       
   358 
       
   359 	if ( iComplete )
       
   360 	{
       
   361 		FireFolderCompleteL();
       
   362 	}
       
   363 }
       
   364 // -----------------------------------------------------------------------------
       
   365 // CScanFolders::FireEntry
       
   366 // -----------------------------------------------------------------------------
       
   367 //
       
   368 void CScanFolders::FireEntry( const TEntry & aEntry )
       
   369 {
       
   370 	if ( aEntry.IsDir() )
       
   371 	{
       
   372 		FireFolder( aEntry.iName );
       
   373 	}
       
   374 	else
       
   375 	{
       
   376 		FireFile( aEntry.iName );
       
   377 	}
       
   378 }
       
   379 // -----------------------------------------------------------------------------
       
   380 // CScanFolders::FireFolder
       
   381 // -----------------------------------------------------------------------------
       
   382 //
       
   383 void CScanFolders::FireFolder( const TDesC & aFolder )
       
   384 {
       
   385 	for ( TInt idx = 0; idx < iListeners.Count(); idx++ )
       
   386 	{
       
   387 		iListeners[ idx ]->NewFolder( aFolder );
       
   388 	}
       
   389 }
       
   390 // -----------------------------------------------------------------------------
       
   391 // CScanFolders::FireFile
       
   392 // -----------------------------------------------------------------------------
       
   393 //
       
   394 void CScanFolders::FireFile( const TDesC & aFile )
       
   395 {
       
   396 	for ( TInt idx = 0; idx < iListeners.Count(); idx++ )
       
   397 	{
       
   398 		iListeners[ idx ]->NewFile( aFile );
       
   399 	}
       
   400 }
       
   401 // -----------------------------------------------------------------------------
       
   402 // CScanFolders::FireFolderStarting
       
   403 // -----------------------------------------------------------------------------
       
   404 //
       
   405 void CScanFolders::FireFolderStarting( TBool aIsDriveListFlag )
       
   406 {
       
   407 	for ( TInt idx = 0; idx < iListeners.Count(); idx++ )
       
   408 	{		
       
   409 		iListeners[ idx ]->FolderStarting( iCurrentPath.FullName(), aIsDriveListFlag );
       
   410 	}
       
   411 }
       
   412 // -----------------------------------------------------------------------------
       
   413 // CScanFolders::FireFolderCompleteL
       
   414 // -----------------------------------------------------------------------------
       
   415 //
       
   416 void CScanFolders::FireFolderCompleteL()
       
   417 {
       
   418 	for ( TInt idx = 0; idx < iListeners.Count(); idx++ )
       
   419 	{
       
   420 		iListeners[ idx ]->FolderCompleteL();
       
   421 	}
       
   422 }
       
   423 // -----------------------------------------------------------------------------
       
   424 // CScanFolders::IsMatch
       
   425 // -----------------------------------------------------------------------------
       
   426 //
       
   427 TBool CScanFolders::IsMatch( const TDesC & aFilename )
       
   428 {
       
   429 	TPtrC remaining = iFileFilter.Mid( 0 );
       
   430 	while ( remaining.Length() > 0 )
       
   431 	{
       
   432 		TInt delimeterPos = remaining.Locate( ';' );
       
   433 		if ( delimeterPos == KErrNotFound )
       
   434 		{
       
   435 			delimeterPos = remaining.Length();
       
   436 		}
       
   437 		TPtrC pattern = remaining.Mid( 0, delimeterPos );
       
   438 		if ( aFilename.MatchF( pattern ) != KErrNotFound )
       
   439 		{
       
   440 			return ETrue;
       
   441 		}
       
   442 		if( delimeterPos + 1 <= remaining.Length() )
       
   443 			remaining.Set( remaining.Mid( delimeterPos + 1 ) );
       
   444 	}
       
   445 	return EFalse;
       
   446 }