browserui/browser/FeedsSrc/FeedsFileSearchAgent.cpp
branchRCL_3
changeset 65 8e6fa1719340
parent 0 84ad3b177aa3
equal deleted inserted replaced
64:6385c4c93049 65:8e6fa1719340
       
     1 /*
       
     2 * Copyright (c) 2007 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 the License "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 
       
    20 // INCLUDE FILES
       
    21 
       
    22 #include "FeedsFileSearchAgent.h"
       
    23 #include <fbs.h>
       
    24 
       
    25 // EXTERNAL DATA STRUCTURES
       
    26 
       
    27 // EXTERNAL FUNCTION PROTOTYPES
       
    28 
       
    29 // CONSTANTS
       
    30 
       
    31 // MACROS
       
    32 
       
    33 // LOCAL CONSTANTS AND MACROS
       
    34 
       
    35 // MODULE DATA STRUCTURES
       
    36 
       
    37 // LOCAL FUNCTION PROTOTYPES
       
    38 
       
    39 // FORWARD DECLARATIONS
       
    40 
       
    41 // ============================= LOCAL FUNCTIONS ===============================
       
    42 
       
    43 
       
    44 // ============================ MEMBER FUNCTIONS ===============================
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // CFeedsFileSearchAgent::CFileSearchAgent
       
    48 // C++ default constructor can NOT contain any code, that
       
    49 // might leave.
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 CFeedsFileSearchAgent::CFeedsFileSearchAgent(MFeedsFileSearchAgentCallback& aCallback)
       
    53     : iCallback(&aCallback), iCancelSearch(EFalse)
       
    54     {
       
    55     }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // CFeedsFileSearchAgent::ConstructL
       
    59 // Symbian 2nd phase constructor can leave.
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 void CFeedsFileSearchAgent::ConstructL()
       
    63     {
       
    64     TDriveList driveList;
       
    65 	TChar driveLetter;
       
    66     
       
    67     // we'll keep two lists. one for the drives in the system, the 
       
    68 	// other for the list of found files
       
    69     iDriveEntryList = new(ELeave) CDriveEntryList(KMaxDrives);
       
    70     iFileEntryList = new(ELeave) CFileEntryList(FEEDS_FILE_SEARCH_AGENT_MAX_RESULTS);
       
    71    	// Create a directory scan object.
       
    72     iScan = CDirScan::NewL(iFs);
       
    73     iLazyCaller = CIdle::NewL(CActive::EPriorityIdle );
       
    74 			
       
    75 	// Connect to fs for FS ops, etc
       
    76 	User::LeaveIfError(iFs.Connect());
       
    77     	
       
    78 	// populate list of drives
       
    79 	GetDriveListL();
       
    80     }
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CFeedsFileSearchAgent::NewL
       
    84 // Two-phased constructor.
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 CFeedsFileSearchAgent* CFeedsFileSearchAgent::NewL(MFeedsFileSearchAgentCallback& aCallback)
       
    88     {
       
    89 	CFeedsFileSearchAgent* self = new( ELeave ) CFeedsFileSearchAgent(aCallback);
       
    90 
       
    91     CleanupStack::PushL( self );
       
    92     self->ConstructL();
       
    93     CleanupStack::Pop();
       
    94 
       
    95     return self;
       
    96     }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // CFeedsFileSearchAgent::~CFeedsFileSearchAgent()
       
   100 // Destructor
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 CFeedsFileSearchAgent::~CFeedsFileSearchAgent()
       
   104     {
       
   105     CancelSearch();
       
   106    	iFs.Close();
       
   107     delete iScan;
       
   108    	delete iLazyCaller;
       
   109 	delete iDriveEntryList;
       
   110 	delete iFileEntryList;
       
   111     }
       
   112 
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CFeedsFileSearchAgent::StartSearchingL
       
   116 //
       
   117 //
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 void CFeedsFileSearchAgent::StartSearchingL()
       
   121     {
       
   122 	iSearchDriveIndex = -1;
       
   123 	
       
   124    	// Return control to the CIdle
       
   125     // The searching work will be done in LazyCallBack.
       
   126     iLazyCaller->Start(TCallBack(CFeedsFileSearchAgent::LazyCallBack,this));
       
   127 
       
   128     }
       
   129 
       
   130 // -----------------------------------------------------------------------------
       
   131 // CFeedsFileSearchAgent::GetFileEntry
       
   132 //
       
   133 //
       
   134 // -----------------------------------------------------------------------------
       
   135 //
       
   136 TFileEntry* CFeedsFileSearchAgent::GetFileEntry(const TInt aIndex)
       
   137 	{
       
   138 	// for a client, return a pointer to a file that was found given
       
   139 	// it's index within the list of found files
       
   140 	if(aIndex >= iFileEntryList->Count())
       
   141 		{
       
   142 		return NULL;
       
   143 		}
       
   144 	else
       
   145 		{
       
   146 		return &(iFileEntryList->At(aIndex));
       
   147 		}
       
   148 	}
       
   149 
       
   150 // -----------------------------------------------------------------------------
       
   151 // CFeedsFileSearchAgent::GetDriveListL
       
   152 //
       
   153 //
       
   154 // -----------------------------------------------------------------------------
       
   155 //
       
   156 void CFeedsFileSearchAgent::GetDriveListL()
       
   157 	{
       
   158 	TDriveList driveList;
       
   159    	
       
   160    	// Setup List of drives
       
   161    	// DriveList will return an array of 26 bytes.
       
   162    	// nonzero entries means there is media present in 
       
   163    	// that drive letter. 
       
   164     User::LeaveIfError(iFs.DriveList(driveList));
       
   165     for (TInt i=0; i<KMaxDrives; i++)
       
   166 		{
       
   167     	if (driveList[i])
       
   168         	{
       
   169         	TDriveEntry driveEntry;
       
   170         	
       
   171         	// Assign a drive letter to the drive entry.
       
   172         	// In the returned list of drives, index 0 corresponds to A:\,
       
   173         	// 1 to B:\, etc.
       
   174             driveEntry.iLetter = 'A' + i;
       
   175             
       
   176             // map the letter back to the drive number. This should be the 
       
   177             // same as "i"
       
   178             iFs.CharToDrive(driveEntry.iLetter, driveEntry.iNumber);
       
   179             
       
   180  			// Add to the list of drives that we keep
       
   181  			iDriveEntryList->AppendL(driveEntry);
       
   182             }
       
   183         }
       
   184 	}
       
   185 	
       
   186 // -----------------------------------------------------------------------------
       
   187 // CFeedsFileSearchAgent::DoSearchFilesL
       
   188 //
       
   189 // Searches a given path or paths for a filename and add it to this class' list
       
   190 // of found files. The filename can contain wildcards.
       
   191 // -----------------------------------------------------------------------------
       
   192 //	
       
   193 TInt CFeedsFileSearchAgent::DoSearchFiles(const TDesC& aFileName, const TDesC& aPath)
       
   194 	{
       
   195 	TFindFile fileFinder(iFs);
       
   196 	CDir* dir = NULL;
       
   197 	
       
   198 	// given a semicolon-separated lists of paths to search in,
       
   199 	// look for the filename (that can include wildcards) found in that path or paths
       
   200 	// This first search will allocate memory for dir and will contain the list of
       
   201 	// files found within the first path given
       
   202 	TInt err = fileFinder.FindWildByPath(aFileName, &aPath, dir);
       
   203 	
       
   204 	while(err == KErrNone)
       
   205 		{
       
   206 		// For the current path given,
       
   207 		// loop through list of found items within that path 
       
   208 		for (TInt i=0; i<dir->Count(); i++)
       
   209 			{
       
   210 			TEntry entry = (*dir)[i];
       
   211 
       
   212 			// create and setup TParse object, useful for later extracting parts
       
   213 			// of the found files used by clients of this class
       
   214 			TParse parsedName;
       
   215 			parsedName.Set(entry.iName, &fileFinder.File(), NULL);
       
   216 			
       
   217 			// as a sanity check,
       
   218 			// make sure the drive, path are valid and
       
   219 			// that the drive found matches the path given
       
   220 			if(parsedName.Drive().Length() && aPath.Length() && parsedName.Drive()[0] == aPath[0])
       
   221 				{
       
   222 				// the wildcard search may have found directories that match
       
   223 				// exlude these from our list because we use this function to 
       
   224 				// record files only 
       
   225 				if(!entry.IsDir())
       
   226 					{
       
   227 					// Create a fileEntry based upon the found file
       
   228 					// FullName will be the complete path specifier for the file
       
   229 					// while entry will be just the name within the directory					
       
   230 					TFileEntry fileEntry;
       
   231 					fileEntry.iPath = parsedName.FullName();
       
   232 					fileEntry.iEntry = entry;
       
   233 					
       
   234 					// copy this entry into to our list of found files
       
   235 					TRAP(err, iFileEntryList->AppendL(fileEntry));
       
   236 					
       
   237 					// If we have exceeded our maximum number of possible entries,
       
   238 					// or we're cancelling an ongoing search
       
   239 					// then stop adding to the list
       
   240 					if(iFileEntryList->Count() >= FEEDS_FILE_SEARCH_AGENT_MAX_RESULTS ||
       
   241 						iCancelSearch)	
       
   242 						{
       
   243 						// cleanup dir
       
   244 						if(dir != NULL)
       
   245 							{
       
   246 							delete(dir);
       
   247 							dir = NULL;
       
   248 							}
       
   249 						return err;
       
   250 						}
       
   251 					}
       
   252 				}
       
   253 			}
       
   254 			
       
   255 			//
       
   256 			// We have completed looking at the results for this dir. Look
       
   257 			// At the results where we left off, for the next path given. 
       
   258 			// If there is no next path, KErrNotFound will be returned, we'll 
       
   259 			// complete the search cycle.
       
   260 			//
       
   261 			if(dir != NULL)
       
   262 				{				
       
   263 				delete(dir);
       
   264 				dir = NULL;
       
   265 				}
       
   266 			err = fileFinder.FindWild(dir);
       
   267 		}
       
   268 
       
   269 		// Cleanup the final instance of dir that has been allocated
       
   270 		if(dir != NULL)
       
   271 			{			
       
   272 			delete(dir);
       
   273 			dir = NULL;
       
   274 			}
       
   275 		return err;
       
   276 	}
       
   277 	
       
   278 // -----------------------------------------------------------------------------
       
   279 // CFeedsFileSearchAgent::DoSearchFilesRecursive
       
   280 //
       
   281 // Starting from a given path, search the filesystem recursively for a given
       
   282 // filename. The filename may contain wildcards.
       
   283 // -----------------------------------------------------------------------------
       
   284 //	
       
   285 	
       
   286 TInt CFeedsFileSearchAgent::DoSearchFilesRecursive(const TDesC& aFileName)
       
   287     {
       
   288     TInt err(KErrNone);
       
   289     CDir* dir = NULL;
       
   290 
       
   291     // Find the entries from the next directory in the hierarchy
       
   292     // or NULL/error if there are none
       
   293     TRAP(err, iScan->NextL(dir));
       
   294 
       
   295 	// done, break out of loop
       
   296     if(!dir || (err != KErrNone) )
       
   297         {
       
   298         return KErrGeneral;
       
   299         }
       
   300 
       
   301 	// loop through the number of found directories
       
   302 	for(TInt i=0; i < dir->Count(); i++)
       
   303         {
       
   304         TEntry entry = (*dir)[i];
       
   305 
       
   306         // The search will find directories as well as files. We're
       
   307         // only interested here in directories.
       
   308         if(entry.IsDir())
       
   309             {
       
   310             // From the scan, get the full path including the drive letter
       
   311             // being scanned.
       
   312             TFileName path(iScan->FullPath());
       
   313 
       
   314             if(path.Length())
       
   315                 {
       
   316                 // If valid, append a slash to the end of the directory, 
       
   317                 // and then search for the desired filename given this path
       
   318                 path.Append(entry.iName);
       
   319                 path.Append(_L("\\"));
       
   320                 DoSearchFiles(aFileName,path);
       
   321                 }
       
   322             }
       
   323         }
       
   324 
       
   325     // Delete the dir that was allocated in this iteration
       
   326     if(dir != NULL)
       
   327         {
       
   328         delete(dir);
       
   329         dir = NULL;
       
   330         }
       
   331 
       
   332     return err;
       
   333     }
       
   334 
       
   335 // -----------------------------------------------------------------------------
       
   336 // CFeedsFileSearchAgent::LazyCallBack
       
   337 //
       
   338 // This function calls StartSearchFile instead of calling it from StartSearchingL function
       
   339 // To ensure it doesn't called if user has cancelled the request
       
   340 // -----------------------------------------------------------------------------
       
   341 //
       
   342 TBool CFeedsFileSearchAgent::LazyCallBack(TAny* aPtr)
       
   343     {
       
   344     CFeedsFileSearchAgent* ptr = (CFeedsFileSearchAgent*)aPtr;
       
   345     // if we've cancelled the search
       
   346     // then stop adding to the list
       
   347     if(!ptr->iCancelSearch)
       
   348         {
       
   349         return ptr->StartSearchFile();    
       
   350         }
       
   351     else
       
   352         {
       
   353         return EFalse;
       
   354         }    
       
   355     }
       
   356 
       
   357 // -----------------------------------------------------------------------------
       
   358 // CFeedsFileSearchAgent::StartSearchFile
       
   359 //
       
   360 // starts the search of *.OPML Files.
       
   361 // -----------------------------------------------------------------------------
       
   362 //
       
   363 TBool CFeedsFileSearchAgent::StartSearchFile()
       
   364     {
       
   365     // Quit looping and don't try to scan if we've already exceeded the number of 
       
   366     // possible file entries
       
   367     // or if we've cancelled the search
       
   368     if(iFileEntryList->Count() >= FEEDS_FILE_SEARCH_AGENT_MAX_RESULTS ||
       
   369             iCancelSearch)
       
   370         {
       
   371         TRAP_IGNORE(iCallback->FeedsFileSearchCompleteL( iFileEntryList->Count() ));
       
   372         return EFalse;	
       
   373         }
       
   374     TInt retVal = KErrGeneral;
       
   375     if(iSearchDriveIndex != -1)
       
   376         {
       
   377         retVal = DoSearchFilesRecursive( _L("*.opml"));
       
   378         }
       
   379     if(retVal != KErrNone)
       
   380         {
       
   381         iSearchDriveIndex++;
       
   382         if(iSearchDriveIndex < iDriveEntryList->Count())
       
   383             {
       
   384             TDriveEntry driveEntry = iDriveEntryList->At(iSearchDriveIndex);
       
   385             TBuf<5> driveRoot;
       
   386 
       
   387             driveRoot.Append(driveEntry.iLetter);
       
   388             driveRoot.Append(_L(":\\"));    	
       
   389 
       
   390             // Search the base of the drive and also search recursively
       
   391             // through it's folder hierarchy.
       
   392             DoSearchFiles(_L("*.opml"),driveRoot);
       
   393             TRAP_IGNORE(iScan->SetScanDataL(driveRoot, KEntryAttDir|KEntryAttMatchMask, ESortByName | EAscending | EDirsFirst));
       
   394             return ETrue;
       
   395             }
       
   396         else
       
   397             {
       
   398             TRAP_IGNORE(iCallback->FeedsFileSearchCompleteL( iFileEntryList->Count() ));
       
   399             return EFalse;
       
   400             }
       
   401         }
       
   402     else
       
   403         {
       
   404         return ETrue;
       
   405         }
       
   406     }
       
   407 
       
   408 // -----------------------------------------------------------------------------
       
   409 // CFeedsFileSearchAgent::CancelSearch.
       
   410 //
       
   411 // Cancel the ongoing search.
       
   412 // -----------------------------------------------------------------------------
       
   413 //	    
       
   414 void CFeedsFileSearchAgent::CancelSearch()
       
   415     {
       
   416     if(iLazyCaller->IsActive())
       
   417         {
       
   418         iLazyCaller->Cancel();
       
   419         }
       
   420     iCancelSearch = ETrue;   
       
   421     }
       
   422 
       
   423 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   424 
       
   425 
       
   426 //  End of File