profilesservices/FileList/Src/CFLDFileListModel.cpp
changeset 0 8c5d936e5675
child 2 051d34a3f367
equal deleted inserted replaced
-1:000000000000 0:8c5d936e5675
       
     1 /*
       
     2 * Copyright (c) 2002 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 *      This class implements a model of a file list. Features:
       
    16 *      - Contents of multiple folders in the same list
       
    17 *      - Filtering with wildcards (*?)
       
    18 *      - Filtering by maximum file size
       
    19 *      - Sorting (name / date, ascending / descending)
       
    20 *      - Entry presentation formatting using an abstract interface
       
    21 *     The class is inherited from MDesCArray, so it can be used directly
       
    22 *     as a model for a listbox.
       
    23 *
       
    24 *
       
    25 */
       
    26 
       
    27 
       
    28 // CLASS HEADER
       
    29 #include "CFLDFileListModel.h"
       
    30 
       
    31 // INTERNAL INCLUDES
       
    32 #include "CFLDOperationObserver.h"
       
    33 #include "CFLDChangedItemObserver.h"
       
    34 #include "CFLDEntryReference.h"
       
    35 #include "CFLDWaitNote.h"
       
    36 #include "CFLDPopupList.h"
       
    37 #include "MFLDEntryFormatter.h"
       
    38 
       
    39 // EXTERNAL INCLUDES
       
    40 #include <MCLFContentListingEngine.h>
       
    41 #include <MCLFItem.h>
       
    42 #include <MCLFItemListModel.h>
       
    43 #include <MCLFModifiableItem.h>
       
    44 #include <ContentListingFactory.h>
       
    45 #include <CLFContentListing.hrh>
       
    46 #include <barsread.h>	// For TResourceReader
       
    47 #include <ConeResLoader.h>	// For RConeResourceLoader
       
    48 #include <coemain.h>	// CCoeEnv
       
    49 #include <pathinfo.h>
       
    50 #include <data_caging_path_literals.hrh>	// For KDC_RESOURCE_FILES_DIR
       
    51 #include <FileList.rsg>	// For R_FLD_WAIT_NOTE
       
    52 #include <bautils.h>	// For BaflUtils
       
    53 #include <apgcli.h>	// For RApaLsSession
       
    54 #include <aknnotewrappers.h>
       
    55 #include <StringLoader.h>
       
    56 
       
    57 // CONSTANTS
       
    58 namespace
       
    59 	{
       
    60 	_LIT( KFLDROMDriveLetter, "Z:" );
       
    61 	const TInt KGranularityFilters( 2 );
       
    62 	}
       
    63 
       
    64 // ============================ MEMBER FUNCTIONS ===============================
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CFLDFileListModel::NewL
       
    68 // Two-phased constructor.
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71  CFLDFileListModel* CFLDFileListModel::NewL(
       
    72   const TInt aModelResourceId, const TInt aDirectoriesResourceId )
       
    73     {
       
    74     // The default stuff.
       
    75     CFLDFileListModel* self =
       
    76      CFLDFileListModel::NewLC( aModelResourceId, aDirectoriesResourceId );
       
    77     CleanupStack::Pop( self ); // self
       
    78     return self;
       
    79     }
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // CFLDFileListModel::NewLC
       
    83 // Two-phased constructor.
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 CFLDFileListModel* CFLDFileListModel::NewLC(
       
    87  const TInt aModelResourceId, const TInt aDirectoriesResourceId )
       
    88     {
       
    89     CFLDFileListModel* self =
       
    90         new( ELeave ) CFLDFileListModel();
       
    91     CleanupStack::PushL( self );
       
    92     self->ConstructL( aModelResourceId, aDirectoriesResourceId );
       
    93     return self;
       
    94     }
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // CFLDFileListModel::CFLDFileListModel
       
    98 // C++ constructor can NOT contain any code, that might leave.
       
    99 // -----------------------------------------------------------------------------
       
   100 //
       
   101 CFLDFileListModel::CFLDFileListModel()
       
   102 	: iExclusiveMimeTypes( KGranularityFilters ),
       
   103 	iExclusiveMediaTypes( KGranularityFilters ),
       
   104 	iDirectoryPaths( KGranularityFilters ),
       
   105 	iRomDirectoryPaths( KGranularityFilters ),
       
   106 	iMaxFileSize( 0 )
       
   107     {
       
   108     }
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // CFLDFileListModel::ConstructL
       
   112 // Symbian 2nd phase constructor can leave.
       
   113 // -----------------------------------------------------------------------------
       
   114 //
       
   115 void CFLDFileListModel::ConstructL(
       
   116  const TInt aModelResourceId, const TInt aDirectoriesResourceId )
       
   117     {
       
   118 	// Create CLF Operation Observer to monitor, when
       
   119     // refresh operation has completed
       
   120     iObserver = CFLDOperationObserver::NewL();
       
   121      
       
   122     // Create Content Listing Engine and a list model
       
   123     iEngine = ContentListingFactory::NewContentListingEngineLC();
       
   124     CleanupStack::Pop(); // iEngine
       
   125 
       
   126 	// Load CLF definitions from user given resource
       
   127     TResourceReader reader;
       
   128 	CCoeEnv::Static()->CreateResourceReaderLC( reader, aModelResourceId );
       
   129     iModel = iEngine->CreateListModelLC( *iObserver, reader );
       
   130     CleanupStack::Pop(); // iModel
       
   131    	CleanupStack::PopAndDestroy();  // reader
       
   132 
       
   133     // Load wait note from default-resource
       
   134     iWaitNote = CFLDWaitNote::NewL( R_FLD_WAIT_NOTE );
       
   135     
       
   136 	// Load directories
       
   137 	CFLDFileListModel::LoadDirectoriesL( aDirectoriesResourceId );
       
   138 	
       
   139 	// Create changed item observer to monitor, when
       
   140     // tone selection files are modified or deleted
       
   141     iChangedItemObserver = CFLDChangedItemObserver::NewL(iWaitNote);
       
   142 	iEngine->AddChangedItemObserverL( *iChangedItemObserver );
       
   143 
       
   144 	// Set post filter	
       
   145 	iModel->SetPostFilter( this );
       
   146     }
       
   147 
       
   148 // Destructor
       
   149  CFLDFileListModel::~CFLDFileListModel()
       
   150     {
       
   151      if( iModel )
       
   152         {
       
   153         iModel->CancelRefresh();
       
   154         delete iModel;
       
   155         }
       
   156 
       
   157     delete iEngine;
       
   158     delete iObserver;
       
   159     delete iChangedItemObserver;
       
   160     delete iEntryFormatter;
       
   161 	delete iWaitNote;
       
   162 
       
   163     iEndNullTextRingingToneArray.ResetAndDestroy();
       
   164     iEndNullTextArray.ResetAndDestroy();
       
   165     iNullTextRingingToneArray.ResetAndDestroy();
       
   166     iNullTextArray.ResetAndDestroy();
       
   167     iEntryReferences.ResetAndDestroy();
       
   168     iExclusiveMimeTypes.Reset();
       
   169     iExclusiveMediaTypes.Reset();
       
   170    	iDirectoryPaths.Reset();
       
   171    	iRomDirectoryPaths.Reset();
       
   172     iRomRingingToneArray.ResetAndDestroy();
       
   173     }
       
   174 
       
   175 
       
   176 // -----------------------------------------------------------------------------
       
   177 // CFLDFileListModel::LoadRomDirectoriesL
       
   178 // (other items were commented in a header).
       
   179 // -----------------------------------------------------------------------------
       
   180 //
       
   181 void CFLDFileListModel::LoadDirectoriesL( const TInt aDirectoriesResourceId )	
       
   182 	 {
       
   183 	 TResourceReader reader;
       
   184   	 CCoeEnv::Static()->CreateResourceReaderLC( reader, aDirectoriesResourceId );
       
   185 	 
       
   186 	 // Read ROM directories from resource
       
   187 	 TInt count( reader.ReadInt16() );
       
   188 	 while( --count >= 0 )
       
   189      	{
       
   190         TPtrC ptr( reader.ReadTPtrC() );
       
   191 		// Ignore return value
       
   192         AddRomDirectoryL( ptr );
       
   193         }
       
   194         
       
   195      // Read directories from resource
       
   196 	 count = reader.ReadInt16();
       
   197 	 while( --count >= 0 )
       
   198      	{
       
   199         TPtrC ptr( reader.ReadTPtrC() );
       
   200 		// Ignore return value
       
   201         AddDirectoryL( ptr );
       
   202         }
       
   203         
       
   204   	 CleanupStack::PopAndDestroy();  // reader
       
   205 	 }
       
   206 // -----------------------------------------------------------------------------
       
   207 // CFLDFileListModel::AddRomDirectoryL
       
   208 // (other items were commented in a header).
       
   209 // -----------------------------------------------------------------------------
       
   210 //
       
   211  TInt CFLDFileListModel::AddRomDirectoryL( const TDesC& aDirectory )
       
   212     {
       
   213     TInt index( 0 );
       
   214 	if( iRomDirectoryPaths.Find( aDirectory, index ) == 0 )
       
   215 		{
       
   216         // A duplicate was found, do not add to the array, return.
       
   217 		return KErrAlreadyExists;
       
   218 		}
       
   219 
       
   220 	RFs& fsSession( CCoeEnv::Static()->FsSession() );
       
   221     // Check that the directory really exists
       
   222     if ( !BaflUtils::FolderExists( fsSession, aDirectory ) )
       
   223         {
       
   224         return KErrPathNotFound;
       
   225         }
       
   226     iRomDirectoryPaths.AppendL( aDirectory );
       
   227 
       
   228     return KErrNone;
       
   229     }
       
   230 
       
   231 // -----------------------------------------------------------------------------
       
   232 // CFLDFileListModel::AddDirectoryL
       
   233 // (other items were commented in a header).
       
   234 // -----------------------------------------------------------------------------
       
   235 //
       
   236  TInt CFLDFileListModel::AddDirectoryL( const TDesC& aDirectory )
       
   237     {
       
   238     TInt index( 0 );
       
   239 	if( iDirectoryPaths.Find( aDirectory, index ) == 0 )
       
   240 		{
       
   241         // A duplicate was found, do not add to the array, return.
       
   242 		return KErrAlreadyExists;
       
   243 		}
       
   244 
       
   245     iDirectoryPaths.AppendL( aDirectory );
       
   246 
       
   247     return KErrNone;
       
   248     }
       
   249 
       
   250 // -----------------------------------------------------------------------------
       
   251 // CFLDFileListModel::SetWantedMimeTypesL()
       
   252 // (other items were commented in a header).
       
   253 // -----------------------------------------------------------------------------
       
   254 //
       
   255 void CFLDFileListModel::SetWantedMimeTypesL( const MDesCArray& aMimeTypes )
       
   256 	{
       
   257 	CFLDFileListModel::ResetExclusiveMimeTypes();
       
   258 	CFLDFileListModel::ResetExclusiveMediaTypes();
       
   259 
       
   260 	iModel->SetWantedMimeTypesL( aMimeTypes );
       
   261 	}
       
   262 
       
   263 // -----------------------------------------------------------------------------
       
   264 // CFLDFileListModel::SetWantedMediaTypesL()
       
   265 // (other items were commented in a header).
       
   266 // -----------------------------------------------------------------------------
       
   267 //
       
   268 void CFLDFileListModel::SetWantedMediaTypesL(
       
   269  const TArray<TCLFMediaType>& aMediaTypes )
       
   270 	{
       
   271 	CFLDFileListModel::ResetExclusiveMimeTypes();
       
   272 	CFLDFileListModel::ResetExclusiveMediaTypes();
       
   273 
       
   274 	// Temporary solution
       
   275 	RArray<TInt> array;
       
   276 	TInt count( aMediaTypes.Count() );
       
   277     CleanupClosePushL( array );
       
   278 	for( TInt i = 0; i < count; i++ )
       
   279 		{
       
   280 		array.AppendL( aMediaTypes[i]);
       
   281 		}
       
   282 
       
   283     iModel->SetWantedMediaTypesL( array.Array() );
       
   284     CleanupStack::PopAndDestroy( &array );
       
   285 	}
       
   286 
       
   287 // -----------------------------------------------------------------------------
       
   288 // CFLDFileListModel::PopulateReferenceListL
       
   289 // (other items were commented in a header).
       
   290 // -----------------------------------------------------------------------------
       
   291 //
       
   292  void CFLDFileListModel::PopulateReferenceListL()
       
   293     {
       
   294     // Do not allow to update model from changed item observer
       
   295     iChangedItemObserver->SetFileListModel( NULL );
       
   296     
       
   297     // Send reference of CActiveSchedulerWait to the CLF Operation Observer
       
   298     iObserver->PrepareForRefresh( iWait );
       
   299 
       
   300     // CLF hogs the processor when doing its refresh. Make sure we'll
       
   301     // have enough of a slice to be able to handle the End key presses.
       
   302     RWsSession& wsSession = CCoeEnv::Static()->WsSession();
       
   303     wsSession.ComputeMode( RWsSession::EPriorityControlDisabled );
       
   304 
       
   305     // Call refresh to get all music files from the file system to the model
       
   306     iModel->RefreshL();
       
   307 
       
   308 	// Display a wait note if user tries to do something while refresh
       
   309 	// operation of the list model has not finished yet.
       
   310     if( iObserver->IsRefreshOngoing() && !iWait.IsStarted() )
       
   311         {
       
   312         // View the wait note
       
   313         if( iWaitNote && !iWaitNote->IsRunning() )
       
   314         	{
       
   315         	 iWaitNote->OpenWaitNoteL();
       
   316         	}
       
   317        
       
   318    	    // Wait for the refresh operation to complete. Operation Observer will
       
   319        	// stop this wait when the refresh operation has compeleted.
       
   320        	iWait.Start(); // CSI: 10 # the state of iWait is checked above
       
   321 
       
   322         // Close the wait note
       
   323         iWaitNote->CloseWaitNoteL();
       
   324         }
       
   325 
       
   326     // Back to default behavior
       
   327     wsSession.ComputeMode( RWsSession::EPriorityControlComputeOff );
       
   328 
       
   329 	PopulateListBoxL();
       
   330 		
       
   331 	if( iPopupList )
       
   332 		{ // Model has updated; list should try to handle it
       
   333 		iPopupList->CancelPreview();
       
   334 		TRAP_IGNORE( iPopupList->ListBox()->HandleItemAdditionL() );
       
   335 		// List has updated; set focus to the first item
       
   336 		iPopupList->ListBox()->SetCurrentItemIndexAndDraw( 0 );
       
   337 		}
       
   338 
       
   339 	// Allow to update model from changed item observer
       
   340 	// (unless we're displaying ROM tones only)
       
   341 	if( !iRomTonesOnly )
       
   342 		{
       
   343 		iChangedItemObserver->SetFileListModel( this );
       
   344 		}
       
   345     }
       
   346 
       
   347 // -----------------------------------------------------------------------------
       
   348 // CFLDFileListModel::ScanRomDirectoriesL
       
   349 // (other items were commented in a header).
       
   350 // -----------------------------------------------------------------------------
       
   351 //
       
   352 void CFLDFileListModel::ScanRomDirectoriesL()
       
   353 	{
       
   354 	CDir* fileList( NULL );
       
   355 	RFs& fsSession( CCoeEnv::Static()->FsSession() );
       
   356 	
       
   357 	TInt directoryCount( iRomDirectoryPaths.Count() );
       
   358 	for( TInt i= 0; i<directoryCount; i++ )
       
   359 		{
       
   360 		User::LeaveIfError(fsSession.GetDir(
       
   361 			iRomDirectoryPaths.MdcaPoint(i),
       
   362 			 KEntryAttNormal,ESortByName, fileList));
       
   363 		CleanupStack::PushL( fileList );
       
   364 	
       
   365 		TInt fileCount( fileList->Count() );
       
   366 		for ( TInt j=0; j<fileCount; j++ )
       
   367 			{
       
   368 			const TEntry item = (*fileList)[j];
       
   369 			TBufC<KMaxFileName> tempBuf = item.iName;
       
   370 			HBufC* concateName = HBufC::NewLC(
       
   371 			 iRomDirectoryPaths.MdcaPoint(i).Length() + tempBuf.Length() );
       
   372 			TPtr des = concateName->Des();
       
   373 			des.Insert(0, iRomDirectoryPaths.MdcaPoint(i) );
       
   374 			des.Append( tempBuf );
       
   375 			
       
   376 			// Only files located on ROM must be scanned
       
   377 			// CLF has already scanned other ( supported ) locations
       
   378 			if( ( des.Left( KFLDROMDriveLetter().Length() ).CompareF(
       
   379           		KFLDROMDriveLetter ) == 0 ) )
       
   380 				{
       
   381 				TParsePtrC parsedName( des );
       
   382 				
       
   383 				MCLFModifiableItem* clfModifiableItem
       
   384 				 = ContentListingFactory::NewModifiableItemLC();
       
   385 				clfModifiableItem->AddFieldL( ECLFFieldIdFileName, parsedName.NameAndExt() );
       
   386 				clfModifiableItem->AddFieldL( ECLFFieldIdPath, parsedName.Path() );
       
   387 				clfModifiableItem->AddFieldL( ECLFFieldIdDrive, parsedName.Drive() );
       
   388 #ifdef RD_VIDEO_AS_RINGING_TONE
       
   389 				if( PathInfo::PathType( parsedName.DriveAndPath() ) == PathInfo::EVideosPath )
       
   390 					{
       
   391 					clfModifiableItem->AddFieldL( ECLFFieldIdMediaType, ECLFMediaTypeVideo);
       
   392 					}
       
   393 				else
       
   394 					{
       
   395 					clfModifiableItem->AddFieldL( ECLFFieldIdMediaType, ECLFMediaTypeSound);
       
   396 					}
       
   397 #else
       
   398 				clfModifiableItem->AddFieldL( ECLFFieldIdMediaType, ECLFMediaTypeSound);
       
   399 #endif
       
   400 				clfModifiableItem->AddFieldL( ECLFFieldIdFileNameAndPath, des );
       
   401 		
       
   402         		// Find out MIME type
       
   403         		RApaLsSession apaLsSession;
       
   404     			User::LeaveIfError( apaLsSession.Connect() );
       
   405     			CleanupClosePushL( apaLsSession );
       
   406     			TUid dummyUid = { 0 }; // instantiate as zero
       
   407     			TDataType dataType( dummyUid );
       
   408     			TInt err = apaLsSession.AppForDocument( parsedName.FullName(), dummyUid, dataType );
       
   409     			CleanupStack::PopAndDestroy(); // apaLsSession.Close()
       
   410     			if( err == KErrNone )
       
   411     				{
       
   412         			clfModifiableItem->AddFieldL( ECLFFieldIdMimeType, dataType.Des() );
       
   413     				User::LeaveIfError(	iRomRingingToneArray.Append( clfModifiableItem ) );
       
   414     				CleanupStack::Pop(); // clfModifiableItem
       
   415     				}
       
   416     			else
       
   417     				{
       
   418     				CleanupStack::PopAndDestroy(); // clfModifiableItem
       
   419     				}
       
   420 				}
       
   421 			CleanupStack::PopAndDestroy( concateName );
       
   422 			}
       
   423 		CleanupStack::PopAndDestroy( fileList);
       
   424 		}
       
   425 	}
       
   426 
       
   427 
       
   428 // -----------------------------------------------------------------------------
       
   429 // CompareEntries
       
   430 // For sorting entry references alphabetically by tone name.
       
   431 // -----------------------------------------------------------------------------
       
   432 //
       
   433 TInt CompareEntries(const CFLDEntryReference& entry1, const CFLDEntryReference& entry2 )
       
   434 	{
       
   435 	TParsePtrC parsed1( entry1.PathAndMediaFileName() );
       
   436 	TParsePtrC parsed2( entry2.PathAndMediaFileName() );
       
   437 
       
   438 	return parsed1.Name().CompareF( parsed2.Name() );
       
   439 	}
       
   440 
       
   441 // -----------------------------------------------------------------------------
       
   442 // CFLDFileListModel::ScanRomDirectoriesAndInsertL
       
   443 // (other items were commented in a header).
       
   444 // -----------------------------------------------------------------------------
       
   445 //
       
   446 void CFLDFileListModel::ScanRomDirectoriesAndInsertL()
       
   447 	{
       
   448 	CDir* fileList( NULL );
       
   449 	RFs fsSession;
       
   450 	User::LeaveIfError( fsSession.Connect() ); 
       
   451 	CleanupClosePushL( fsSession );
       
   452 	
       
   453 	TInt directoryCount( iRomDirectoryPaths.Count() );
       
   454 	for( TInt i= 0; i<directoryCount; i++ )
       
   455 		{
       
   456 		User::LeaveIfError(fsSession.GetDir(
       
   457 			iRomDirectoryPaths.MdcaPoint(i),
       
   458 			 KEntryAttNormal,ESortByName, fileList));
       
   459 		CleanupStack::PushL( fileList );
       
   460 	
       
   461 		TInt fileCount( fileList->Count() );
       
   462 		for ( TInt j=0; j<fileCount; j++ )
       
   463 			{
       
   464 			const TEntry item = (*fileList)[j];
       
   465 			TBufC<KMaxFileName> tempBuf = item.iName;
       
   466 			HBufC* concateName = HBufC::NewLC(
       
   467 			 iRomDirectoryPaths.MdcaPoint(i).Length() + tempBuf.Length() );
       
   468 			TPtr des = concateName->Des();
       
   469 			des.Insert(0, iRomDirectoryPaths.MdcaPoint(i) );
       
   470 			des.Append( tempBuf );
       
   471 			
       
   472 			// Only files located on ROM must be scanned
       
   473 			if( ( des.Left( KFLDROMDriveLetter().Length() ).CompareF(
       
   474           		KFLDROMDriveLetter ) == 0 ) )
       
   475 				{
       
   476 				// Add file to entry-list 							            	
       
   477 		   		CFLDEntryReference* entryRef = new( ELeave ) CFLDEntryReference();
       
   478 		   		CleanupStack::PushL( entryRef );
       
   479 	        	HBufC* pathAndMediaFileName	= des.AllocL();
       
   480 				entryRef->SetPathAndMediaFileName( pathAndMediaFileName );
       
   481 
       
   482 				TParsePtrC parsedName( des );
       
   483 				if( PathInfo::PathType( parsedName.DriveAndPath() ) == PathInfo::EVideosPath )
       
   484 					{
       
   485 					entryRef->SetMediaType( ECLFMediaTypeVideo);
       
   486 					}
       
   487 				else
       
   488 					{
       
   489 					entryRef->SetMediaType( ECLFMediaTypeSound);
       
   490 					}
       
   491 
       
   492     			iEntryFormatter->FormatL( *entryRef );
       
   493 				User::LeaveIfError(	iEntryReferences.Append( entryRef ) );
       
   494 				CleanupStack::Pop( entryRef );
       
   495 				}
       
   496 			CleanupStack::PopAndDestroy( concateName );
       
   497 			}
       
   498 		CleanupStack::PopAndDestroy( fileList);
       
   499 		}
       
   500 
       
   501 	TLinearOrder<CFLDEntryReference>* sorter = 
       
   502 		new( ELeave ) TLinearOrder<CFLDEntryReference> ( CompareEntries );
       
   503 	iEntryReferences.Sort( *sorter );
       
   504 	delete sorter;
       
   505 
       
   506 	CleanupStack::PopAndDestroy(); // fsSession
       
   507 	}
       
   508 
       
   509 
       
   510 // -----------------------------------------------------------------------------
       
   511 // CFLDFileListModel::FilterItemsL
       
   512 // (other items were commented in a header).
       
   513 // -----------------------------------------------------------------------------
       
   514 //
       
   515 void CFLDFileListModel::FilterItemsL( const TArray<MCLFItem*>& aItemList,
       
   516                                 RPointerArray<MCLFItem>& aFilteredItemList )
       
   517 	{
       
   518 	
       
   519 	// Add all found entries into model
       
   520     TInt count( aItemList.Count() );
       
   521 	for( TInt i = 0 ; i < count ; ++i )
       
   522     	{
       
   523 		MCLFItem* item = aItemList[i];
       
   524 		aFilteredItemList.AppendL( item );	    	
       
   525     	}
       
   526 	
       
   527 	// Fetch tones from ROM directories
       
   528 	CFLDFileListModel::ScanRomDirectoriesL();
       
   529 	
       
   530 	// Add all found entries into model
       
   531     count = iRomRingingToneArray.Count();
       
   532 	for( TInt i = 0 ; i < count ; ++i )
       
   533     	{
       
   534 		MCLFItem* item = iRomRingingToneArray[i];
       
   535 		aFilteredItemList.AppendL( item );
       
   536     	}
       
   537     
       
   538 	}	
       
   539 
       
   540 // -----------------------------------------------------------------------------
       
   541 // CFLDFileListModel::GetPopupList
       
   542 // (other items were commented in a header).
       
   543 // -----------------------------------------------------------------------------
       
   544 //
       
   545 CFLDPopupList* CFLDFileListModel::GetPopupList()
       
   546 	{
       
   547 	return iPopupList;
       
   548 	}
       
   549 
       
   550 // -----------------------------------------------------------------------------
       
   551 // CFLDFileListModel::AddExclusiveMimeTypeL
       
   552 // (other items were commented in a header).
       
   553 // -----------------------------------------------------------------------------
       
   554 //
       
   555 void CFLDFileListModel::AddExclusiveMimeTypeL( const TDesC& aMimeType )
       
   556 	{
       
   557 	TInt index( 0 );
       
   558 	if( iExclusiveMimeTypes.Find( aMimeType, index ) == 0 )
       
   559 
       
   560 		{
       
   561         // A duplicate was found, do not add to the array, just return.
       
   562 		return;
       
   563 		}
       
   564 	iExclusiveMimeTypes.AppendL( aMimeType );
       
   565 	}
       
   566 
       
   567 // -----------------------------------------------------------------------------
       
   568 // CFLDFileListModel::AddExclusiveMediaTypeL
       
   569 // (other items were commented in a header).
       
   570 // -----------------------------------------------------------------------------
       
   571 //
       
   572 void CFLDFileListModel::AddExclusiveMediaTypeL( const TInt32 aMediaType )
       
   573 	{
       
   574 	if( iExclusiveMediaTypes.Find( aMediaType ) != KErrNotFound )
       
   575 		{
       
   576 		// A duplicate was found, do not add to the array, just return.
       
   577 		return;
       
   578 		}
       
   579 	iExclusiveMediaTypes.AppendL( aMediaType );
       
   580 	}
       
   581 
       
   582 // -----------------------------------------------------------------------------
       
   583 // CFLDFileListModel::ResetExclusiveMimeTypes
       
   584 // (other items were commented in a header).
       
   585 // -----------------------------------------------------------------------------
       
   586 //
       
   587 void CFLDFileListModel::ResetExclusiveMimeTypes()
       
   588 	{
       
   589 	iExclusiveMimeTypes.Reset();
       
   590 	}
       
   591 
       
   592 // -----------------------------------------------------------------------------
       
   593 // CFLDFileListModel::ResetExclusiveMediaTypes
       
   594 // (other items were commented in a header).
       
   595 // -----------------------------------------------------------------------------
       
   596 //
       
   597 void CFLDFileListModel::ResetExclusiveMediaTypes()
       
   598 	{
       
   599 	iExclusiveMediaTypes.Reset();
       
   600 	}
       
   601 
       
   602 // -----------------------------------------------------------------------------
       
   603 // CFLDFileListModel::CheckRules
       
   604 // (other items were commented in a header).
       
   605 // -----------------------------------------------------------------------------
       
   606 //
       
   607 TBool CFLDFileListModel::CheckRules( 
       
   608 		 TPtrC& aMimeType, TInt32 aMediaType, TPtrC& aPathAndFileName )
       
   609 	{
       
   610 	
       
   611 	// Check exclusive MIME types
       
   612     if( iExclusiveMimeTypes.Count() > 0 )
       
   613     	{
       
   614        	TInt index( 0 );
       
   615        	if( iExclusiveMimeTypes.Find( aMimeType, index ) == 0 )
       
   616       		{
       
   617            	return EFalse;
       
   618            	}
       
   619        	}
       
   620 
       
   621 	// Check exclusive media types
       
   622 	if( iExclusiveMediaTypes.Count() > 0 )
       
   623 		{
       
   624        	if( iExclusiveMediaTypes.Find( aMediaType ) != KErrNotFound )
       
   625 	   		{
       
   626            	return EFalse;
       
   627            	}
       
   628     	}
       
   629 
       
   630    	if( ( aPathAndFileName.Left( KFLDROMDriveLetter().Length() ).CompareF(
       
   631           KFLDROMDriveLetter ) == 0 ) )
       
   632 		{
       
   633 		// ROM tones are accepted always
       
   634 		return ETrue;
       
   635 		}
       
   636 	
       
   637 	// Check defined pathes
       
   638 	if( iDirectoryPaths.Count() > 0 )
       
   639 		{
       
   640 		TParsePtrC parsedName( aPathAndFileName );
       
   641 		TInt index( 0 );
       
   642        	if( iDirectoryPaths.Find( parsedName.DriveAndPath(), index ) != 0 )
       
   643 	   		{
       
   644 	   		// File is not part of defined path set
       
   645            	return EFalse;
       
   646            	}
       
   647     	}
       
   648     	
       
   649 	// File is part of defined path set or then no directory paths are defined
       
   650     return ETrue;
       
   651 	}
       
   652 
       
   653 // -----------------------------------------------------------------------------
       
   654 // CFLDFileListModel::PopulateListBoxL
       
   655 // (other items were commented in a header).
       
   656 // -----------------------------------------------------------------------------
       
   657 //
       
   658 void CFLDFileListModel::PopulateListBoxL()
       
   659     {
       
   660     // Add all items to the list box
       
   661     TInt countItems( iModel->ItemCount() );
       
   662     TInt error;
       
   663        
       
   664     for( TInt i = 0; i < countItems; i++ )
       
   665         {
       
   666         // Check name and path
       
   667         TPtrC fieldValueNameAndPath;
       
   668         error = iModel->Item( i ).GetField(
       
   669          ECLFFieldIdFileNameAndPath, fieldValueNameAndPath );
       
   670 
       
   671         if( error == KErrNone )
       
   672             {
       
   673 	        // Check media type
       
   674 			TInt32 mediaTypeValue;
       
   675     		error = iModel->Item( i ).GetField(
       
   676      		ECLFFieldIdMediaType, mediaTypeValue );
       
   677 	        	
       
   678 	   		if( error == KErrNone )
       
   679 		   		{
       
   680 	       		// Check MIME type
       
   681         		TPtrC fieldValueMime;
       
   682         		error = iModel->Item( i ).GetField(
       
   683         			ECLFFieldIdMimeType, fieldValueMime );
       
   684 	
       
   685 	   		 	if( error == KErrNone )
       
   686 		       		{
       
   687             		// Check include/exclude rules
       
   688 		        	if( CFLDFileListModel::CheckRules(
       
   689 	       	     		fieldValueMime, mediaTypeValue,
       
   690                  		fieldValueNameAndPath ) )
       
   691  		       			{
       
   692 						// Everything seems to be ok,
       
   693 						// now check file-size limit
       
   694     					if ( iMaxFileSize )
       
   695         					{
       
   696    	            			if ( CheckToneFileSizeL(
       
   697    	            				fieldValueNameAndPath, iMaxFileSize)
       
   698    	            		 		!= KErrNone )
       
   699 	        	   				{
       
   700 	        	   				continue;
       
   701             	   				}
       
   702 							}
       
   703 						// Finally, add file to entry-list 							            	
       
   704 				   		CFLDEntryReference* entryRef
       
   705 				  			= new( ELeave ) CFLDEntryReference();
       
   706     			   		CleanupStack::PushL( entryRef );
       
   707 			        	HBufC* pathAndMediaFileName
       
   708 			      			= fieldValueNameAndPath.AllocL();
       
   709 						entryRef->SetPathAndMediaFileName(
       
   710 							pathAndMediaFileName );
       
   711 						entryRef->SetMediaType( mediaTypeValue );
       
   712 	        			iEntryFormatter->FormatL( *entryRef );
       
   713 						User::LeaveIfError(
       
   714 							iEntryReferences.Append( entryRef ) );
       
   715 						CleanupStack::Pop( entryRef );
       
   716  		       			}
       
   717 		       		}
       
   718 	            }
       
   719 	        }
       
   720         }
       
   721 
       
   722 	if( countItems == 0 )
       
   723 		{
       
   724 		// No tones from CLF -- likely out of memory. Scan for ROM tones only.
       
   725 		HBufC* errorText = StringLoader::LoadLC( 
       
   726 									R_FLD_QTN_PROFILES_ERROR_NOT_ENOUGH_MEMORY );
       
   727 		CAknErrorNote* note = new( ELeave ) CAknErrorNote( ETrue );
       
   728 		note->ExecuteLD( *errorText );
       
   729 		CleanupStack::PopAndDestroy( errorText );
       
   730 
       
   731 		iRomTonesOnly = ETrue;
       
   732 		ScanRomDirectoriesAndInsertL();
       
   733 		}
       
   734 
       
   735     }
       
   736 
       
   737 
       
   738 // -----------------------------------------------------------------------------
       
   739 // CFLDFileListModel::RefreshEntryListL
       
   740 // (other items were commented in a header).
       
   741 // -----------------------------------------------------------------------------
       
   742 //
       
   743  void CFLDFileListModel::RefreshEntryListL()
       
   744     {
       
   745     iRomTonesOnly = EFalse;
       
   746 
       
   747     iRomRingingToneArray.ResetAndDestroy();
       
   748     iEntryReferences.ResetAndDestroy();
       
   749     PopulateReferenceListL();
       
   750     }
       
   751 
       
   752 // -----------------------------------------------------------------------------
       
   753 // CFLDFileListModel::SetPopupList
       
   754 // (other items were commented in a header).
       
   755 // -----------------------------------------------------------------------------
       
   756 //
       
   757 void CFLDFileListModel::SetPopupList( CFLDPopupList* aPopupList )
       
   758 	{
       
   759 	iPopupList = aPopupList;
       
   760 	}
       
   761 
       
   762 // -----------------------------------------------------------------------------
       
   763 // CFLDFileListModel::MdcaPoint
       
   764 // (other items were commented in a header).
       
   765 // -----------------------------------------------------------------------------
       
   766 //
       
   767  TPtrC16 CFLDFileListModel::MdcaPoint( TInt aIndex ) const
       
   768     {
       
   769 	TInt nullItemCount( iNullTextArray.Count() );
       
   770 	if( aIndex < nullItemCount )
       
   771 		{
       
   772 		// Points to a null item in the beginning of the list.
       
   773 		return iNullTextArray[aIndex]->Des();
       
   774 		}
       
   775 	TInt referenceListCount( iEntryReferences.Count() );
       
   776 	if( aIndex >= referenceListCount + nullItemCount )
       
   777 		{
       
   778 		// Points to a null item in the end of the list.
       
   779 		return iEndNullTextArray[aIndex - referenceListCount - nullItemCount]
       
   780                 ->Des();
       
   781 		}
       
   782 
       
   783 	// The index points to one of the directory entries
       
   784 	return iEntryReferences[aIndex - nullItemCount]
       
   785             ->FormattedPresentation().Des();
       
   786 	
       
   787     }
       
   788 
       
   789 // -----------------------------------------------------------------------------
       
   790 // CFLDFileListModel::MdcaCount
       
   791 // (other items were commented in a header).
       
   792 // -----------------------------------------------------------------------------
       
   793 //
       
   794  TInt CFLDFileListModel::MdcaCount() const
       
   795     {
       
   796 	return iNullTextArray.Count()
       
   797 	 + iEntryReferences.Count() + iEndNullTextArray.Count();
       
   798     }
       
   799 
       
   800 // -----------------------------------------------------------------------------
       
   801 // CFLDFileListModel::SetEntryFormatter
       
   802 // (other items were commented in a header).
       
   803 // -----------------------------------------------------------------------------
       
   804 //
       
   805  void CFLDFileListModel::SetEntryFormatter( MFLDEntryFormatter* aFormatter )
       
   806     {
       
   807     delete iEntryFormatter;
       
   808     iEntryFormatter = aFormatter;
       
   809     }
       
   810 
       
   811 // -----------------------------------------------------------------------------
       
   812 // CFLDFileListModel::InsertNullItemL
       
   813 // (other items were commented in a header).
       
   814 // -----------------------------------------------------------------------------
       
   815 //
       
   816  void CFLDFileListModel::InsertNullItemL( const TDesC& aItemText )
       
   817     {
       
   818     InsertNullItemL( aItemText, KNullDesC );
       
   819     }
       
   820 
       
   821 // -----------------------------------------------------------------------------
       
   822 // CFLDFileListModel::InsertNullItemL
       
   823 // (other items were commented in a header).
       
   824 // -----------------------------------------------------------------------------
       
   825 //
       
   826  void CFLDFileListModel::InsertNullItemL(
       
   827   const TDesC& aItemText, const TDesC& aFileName )
       
   828     {
       
   829 	HBufC* text = iEntryFormatter->FormatTextLC( aItemText );
       
   830 	// Takes ownership
       
   831 	User::LeaveIfError( iNullTextArray.Append( text ) ); 
       
   832 	CleanupStack::Pop( text );
       
   833 
       
   834 	text = aFileName.AllocLC();
       
   835 	// Takes ownership
       
   836 	User::LeaveIfError( iNullTextRingingToneArray.Append( text ) ); 
       
   837 	CleanupStack::Pop( text );
       
   838     }
       
   839 
       
   840 // -----------------------------------------------------------------------------
       
   841 // CFLDFileListModel::InsertEndNullItemL
       
   842 // (other items were commented in a header).
       
   843 // -----------------------------------------------------------------------------
       
   844 //
       
   845  void CFLDFileListModel::InsertEndNullItemL( const TDesC& aItemText )
       
   846     {
       
   847     InsertEndNullItemL( aItemText, KNullDesC );
       
   848     }
       
   849 
       
   850 // -----------------------------------------------------------------------------
       
   851 // CFLDFileListModel::InsertEndNullItemL
       
   852 // (other items were commented in a header).
       
   853 // -----------------------------------------------------------------------------
       
   854 //
       
   855  void CFLDFileListModel::InsertEndNullItemL(
       
   856  const TDesC& aItemText, const TDesC& aFileName )
       
   857     {
       
   858 	HBufC* text = iEntryFormatter->FormatTextLC( aItemText );
       
   859     // Takes ownership
       
   860 	User::LeaveIfError( iEndNullTextArray.Append( text ) ); 
       
   861 	CleanupStack::Pop( text );
       
   862 
       
   863 	text = aFileName.AllocLC();
       
   864 	// Takes ownership
       
   865 	User::LeaveIfError( iEndNullTextRingingToneArray.Append( text ) ); 
       
   866 	CleanupStack::Pop( text );
       
   867     }
       
   868 
       
   869 // -----------------------------------------------------------------------------
       
   870 // CFLDFileListModel::GetFileName
       
   871 // (other items were commented in a header).
       
   872 // -----------------------------------------------------------------------------
       
   873 //
       
   874  void CFLDFileListModel::GetFileName( TDes& aFileName, const TInt aIndex )
       
   875     {
       
   876 	TInt nullItemCount( iNullTextRingingToneArray.Count() );
       
   877 	if( aIndex < nullItemCount )
       
   878 		{
       
   879 		// Points to a null item in the beginning of the list.
       
   880 		aFileName.Copy( iNullTextRingingToneArray[aIndex]->Des() );
       
   881 		return;
       
   882 		}
       
   883 	TInt referenceListCount( iEntryReferences.Count() );
       
   884 	if( aIndex >= referenceListCount + nullItemCount )
       
   885 		{
       
   886 		// Points to a null item in the end of the list.
       
   887 		aFileName.Copy( iEndNullTextRingingToneArray[
       
   888 			aIndex - referenceListCount - nullItemCount]->Des() );
       
   889 		return;
       
   890 		}
       
   891 
       
   892 	// The index points to one of the directory entries
       
   893 	CFLDEntryReference& entry = *iEntryReferences[aIndex - nullItemCount];
       
   894 	aFileName.Copy( entry.PathAndMediaFileName().Des() );
       
   895 
       
   896     }
       
   897 
       
   898 // -----------------------------------------------------------------------------
       
   899 // CFLDFileListModel::FindFileL
       
   900 // (other items were commented in a header).
       
   901 // -----------------------------------------------------------------------------
       
   902 //
       
   903  TInt CFLDFileListModel::FindFileL( const TDesC& aFileName )
       
   904     {
       
   905 	TInt ret( KErrNotFound );
       
   906 	TInt count( MdcaCount() );
       
   907 	TFileName fileName( KNullDesC );
       
   908 	for( TInt index( 0 ); index < count; index++ )
       
   909 		{
       
   910 		GetFileNameAtIndex( index, fileName );
       
   911 		if( fileName.CompareF( aFileName ) == 0 )
       
   912 			{
       
   913 			ret = index;
       
   914 			break;
       
   915 			}
       
   916 		}
       
   917 	return ret;
       
   918     }
       
   919 
       
   920 // -----------------------------------------------------------------------------
       
   921 // CFLDFileListModel::GetFileNameAtIndex
       
   922 // (other items were commented in a header).
       
   923 // -----------------------------------------------------------------------------
       
   924 //
       
   925 void CFLDFileListModel::GetFileNameAtIndex(
       
   926  TInt aIndex, TDes& aFileName ) const
       
   927 	{
       
   928 	TInt nullItemCount( iNullTextRingingToneArray.Count() );
       
   929 	if( aIndex < nullItemCount )
       
   930 		{
       
   931 		// Points to a null item in the beginning of the list.
       
   932 		aFileName.Copy( iNullTextRingingToneArray[aIndex]->Des() );
       
   933 		return;
       
   934 		}
       
   935 	TInt referenceListCount( iEntryReferences.Count() );
       
   936 	if( aIndex >= referenceListCount + nullItemCount )
       
   937 		{
       
   938 		// Points to a null item in the end of the list.
       
   939 		aFileName.Copy( iEndNullTextRingingToneArray[
       
   940 			aIndex - referenceListCount - nullItemCount]->Des() );
       
   941 		return;
       
   942 		}
       
   943 
       
   944 	// The index points to one of the directory entries
       
   945 	CFLDEntryReference& entry = *iEntryReferences[aIndex - nullItemCount];
       
   946 	aFileName.Copy( entry.PathAndMediaFileName().Des() );
       
   947 	}
       
   948 
       
   949 // -----------------------------------------------------------------------------
       
   950 // CFLDFileListModel::MediaFileType
       
   951 // (other items were commented in a header).
       
   952 // -----------------------------------------------------------------------------
       
   953 //
       
   954 TInt32 CFLDFileListModel::MediaFileType( const TDesC& aFileName ) const
       
   955 	{
       
   956 	if( iRomTonesOnly )
       
   957 		{
       
   958 		// Model is empty, get media type from entry reference
       
   959 		for(TInt i = 0; i < iEntryReferences.Count(); i++)
       
   960 			{
       
   961 			if( aFileName.CompareF( iEntryReferences[i]->PathAndMediaFileName() ) == 0 )
       
   962 				{
       
   963 				return iEntryReferences[i]->MediaType();
       
   964 				}
       
   965 			}
       
   966 		}
       
   967 	else
       
   968 		{
       
   969 	    TInt countItems( iModel->ItemCount() );
       
   970 	    for( TInt i = 0; i < countItems; i++ )
       
   971 	        {
       
   972 	        // Get value of requested field and add item to the list box
       
   973 	        TPtrC fieldValue;
       
   974 	        TInt error( iModel->Item( i ).GetField(
       
   975 	         ECLFFieldIdFileNameAndPath, fieldValue ) );
       
   976 
       
   977 	        if( error == KErrNone )
       
   978 	            {
       
   979 	            if( aFileName.CompareF( fieldValue ) == 0 )
       
   980 		   			{
       
   981 		   			TInt32 mediaTypeValue;
       
   982 	        		TInt error( iModel->Item( i ).GetField(
       
   983 	        		 ECLFFieldIdMediaType, mediaTypeValue ) );
       
   984 
       
   985 	            	if( error == KErrNone )
       
   986 		            	{
       
   987 		            	return mediaTypeValue;
       
   988 		   				}
       
   989 		   			}
       
   990 	            }
       
   991 	        }
       
   992 		}
       
   993     return KErrNotFound;
       
   994 	}
       
   995 
       
   996 // -----------------------------------------------------------------------------
       
   997 // CFLDFileListModel::SetMaxFileSize
       
   998 // (other items were commented in a header).
       
   999 // -----------------------------------------------------------------------------
       
  1000 //
       
  1001 void CFLDFileListModel::SetMaxFileSize( const TInt aMaxFileSize )
       
  1002     {
       
  1003     iMaxFileSize = aMaxFileSize;
       
  1004     }
       
  1005 
       
  1006 // -----------------------------------------------------------------------------
       
  1007 // CFLDFileListModel::CheckToneFileSizeL
       
  1008 // (other items were commented in a header).
       
  1009 // -----------------------------------------------------------------------------
       
  1010 //
       
  1011 TInt CFLDFileListModel::CheckToneFileSizeL(
       
  1012  const TDesC& aFile, TInt aSizeLimit )
       
  1013     {
       
  1014 	RFs& fsSession( CCoeEnv::Static()->FsSession() );
       
  1015 	
       
  1016     // Get file size
       
  1017     TInt size = 0;
       
  1018     TInt error = KErrNone;
       
  1019 
       
  1020     TEntry entry;
       
  1021     if ( fsSession.Entry( aFile, entry ) == KErrNone )
       
  1022         {
       
  1023         size = entry.iSize;        
       
  1024         }
       
  1025 
       
  1026 	// Check. NOTE: now if file size couldn't be determined, check fails.
       
  1027 	if ( aSizeLimit  &&  size > aSizeLimit )
       
  1028 		{
       
  1029 		error = KErrTooBig;
       
  1030 		}
       
  1031 
       
  1032     return error;
       
  1033     }
       
  1034     
       
  1035     
       
  1036 //  End of File