phonebookengines/contactsmodel/cntplsql/src/cntfilesearch.cpp
branchRCL_3
changeset 20 f4a778e096c2
parent 0 e686773b3f54
equal deleted inserted replaced
19:5b6f26637ad3 20:f4a778e096c2
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalComponent
       
    19  @released
       
    20 */
       
    21 
       
    22 #include "cntfilesearch.h"
       
    23 
       
    24 const TInt KNameArrayGranularity = 32;
       
    25                          
       
    26 //Define the patchable data for database cache size here but which is actually used
       
    27 //in CPplContactsFile.cpp. This is a must-be so that the compiler cannot perform any
       
    28 //"constant folding" with the value   
       
    29 EXPORT_C extern const TInt KContactsModelSqliteDbCacheSize = 0;
       
    30                          
       
    31 _LIT(KSqLiteFilePrefix, "SQLite__");
       
    32 const TInt KPrefixNameLength = 8;   // length of above prefix string
       
    33 
       
    34 CDesCArray* CCntFileScanner::ListFilesL(RFs& aFs, TDriveUnit* aDriveUnit)
       
    35 	{
       
    36 	CCntFileScanner* scanner = new (ELeave) CCntFileScanner(aFs);  // gets pushed in first lines of ScanLD
       
    37 	// coverity [memory_leak]
       
    38 	return scanner->ScanLD(aDriveUnit);
       
    39 	}
       
    40 
       
    41 
       
    42 CCntFileScanner::CCntFileScanner(RFs& aFs)
       
    43 	:
       
    44 	iFs(aFs)
       
    45 	{
       
    46 	}
       
    47 
       
    48 
       
    49 CCntFileScanner::~CCntFileScanner()
       
    50 	{
       
    51 	delete iDirStack;
       
    52 	}
       
    53 
       
    54 
       
    55 CDesCArray* CCntFileScanner::ScanLD(TDriveUnit* aDriveUnit)
       
    56 	{
       
    57 	CleanupStack::PushL(this);
       
    58 
       
    59 	iFs.PrivatePath(iPath);
       
    60 	iDirStack = new(ELeave) CDesCArrayFlat(KNameArrayGranularity);
       
    61 
       
    62 	if (aDriveUnit)
       
    63 		{
       
    64 		AddDirL(*aDriveUnit);
       
    65 		}
       
    66 	else
       
    67 		{
       
    68 		GenerateRootDirsL();
       
    69 		}
       
    70 
       
    71 	CDesCArray* results = new(ELeave) CDesCArrayFlat(KNameArrayGranularity);
       
    72 	CleanupStack::PushL(results);
       
    73 	ScanForFilesL(*results);
       
    74 	CleanupStack::Pop(results);
       
    75 	CleanupStack::PopAndDestroy(this);
       
    76 
       
    77 	return results;
       
    78 	}
       
    79 
       
    80 
       
    81 void CCntFileScanner::AddDirL(TDriveUnit& aDriveUnit)
       
    82 	{
       
    83 	ASSERT(iDirStack);
       
    84 	TBuf<3> root;
       
    85 	root.Copy(aDriveUnit.Name());
       
    86 	root.Append(TChar(KPathDelimiter));
       
    87 	iDirStack->AppendL(root);
       
    88 	}
       
    89 
       
    90 	
       
    91 void CCntFileScanner::GenerateRootDirsL()
       
    92 	{
       
    93 	ASSERT(iDirStack);
       
    94 	// Generate list of root dirs.
       
    95 	TBuf<3> root;
       
    96 	TDriveList driveList;
       
    97 	User::LeaveIfError(iFs.DriveList(driveList));
       
    98 	TInt len = driveList.Length();
       
    99 	for (TInt index = 0; index < len; ++index)
       
   100 		{
       
   101 		if(driveList[index]) // Check drive exists.
       
   102 			{
       
   103 			root.Copy(TDriveUnit(index).Name());
       
   104 			root.Append(TChar(KPathDelimiter));
       
   105 			iDirStack->AppendL(root);
       
   106 			}
       
   107 		}
       
   108 	}
       
   109 
       
   110 
       
   111 void CCntFileScanner::ScanForFilesL(CDesCArray& aResults)
       
   112 	{
       
   113 	ASSERT(iDirStack);
       
   114 	// Traverse the directory stack until all directories have been examined.
       
   115 	
       
   116 	TFileName filename;
       
   117 	while(iDirStack->Count())
       
   118 		{
       
   119 		// Pop the dir.
       
   120 		filename.Copy(iDirStack->MdcaPoint(0));
       
   121 		iDirStack->Delete(0);
       
   122 		if(iPath.Length()!=0)
       
   123 			{
       
   124 			filename.Delete(2,1);
       
   125 			filename.Append(iPath);
       
   126 			}
       
   127 		else if(filename[filename.Length() - 1] != KPathDelimiter)
       
   128 			{ // Check it has a trailing '\' to identify it as a dir path.
       
   129 			filename.Append(TChar(KPathDelimiter));
       
   130 			}
       
   131 		TRAPD(err,ScanDirectoryL(filename, aResults));
       
   132 		if(err==KErrNoMemory)
       
   133 			{ // Continue to search in each drive except for out of memory.
       
   134 			User::Leave(err);
       
   135 			}
       
   136 		}
       
   137 	}
       
   138 
       
   139 
       
   140 /**
       
   141 Get a list of all files and subdirectories in aDirPath (when the path is not
       
   142 specified).
       
   143 */
       
   144 void CCntFileScanner::ScanDirectoryL(const TDesC& aDirPath, CDesCArray& aResults)
       
   145 	{
       
   146 	CDir* files = NULL;
       
   147 	CDir* subdirs = NULL;
       
   148 
       
   149 	User::LeaveIfError(iFs.GetDir(aDirPath, KEntryAttAllowUid, ESortByName, files, subdirs));
       
   150 
       
   151 	TRAPD(err,AppendFileNamesL(*files, aDirPath, aResults));
       
   152 
       
   153 	delete files;
       
   154 	delete subdirs;
       
   155 	User::LeaveIfError(err);
       
   156 	}
       
   157 
       
   158 
       
   159 void CCntFileScanner::AppendFileNamesL(const CDir& aFileSet, const TDesC& aDirPath, CDesCArray& aFileNames) const
       
   160 	{
       
   161 	TParse parse;
       
   162 	TInt len = aFileSet.Count();
       
   163 	
       
   164 	for(TInt idx = 0; idx < len ; ++idx)
       
   165 		{
       
   166 		const TEntry& entry = aFileSet[idx];
       
   167 		User::LeaveIfError(parse.SetNoWild(entry.iName, &aDirPath, NULL));
       
   168 			
       
   169 		TInt found = parse.NameAndExt().FindF(KSqLiteFilePrefix);
       
   170 				
       
   171 		if (found == 0 && iPath.Length() > 0)
       
   172 			{
       
   173 			// add drive, remove prefix, add to list of files
       
   174 			TFileName filename(parse.Drive());
       
   175 			filename.Append(parse.NameAndExt().Right(parse.NameAndExt().Length() - KPrefixNameLength));	
       
   176 						
       
   177 			aFileNames.AppendL(filename);
       
   178 			}
       
   179 			
       
   180 		}
       
   181 	}