summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/cntplsql/src/cntfilesearch.cpp
blob: 3de9a0f8da710cb420dfca9979e67ac7007aaf44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: 
*
*/


/**
 @file
 @internalComponent
 @released
*/

#include "cntfilesearch.h"

const TInt KNameArrayGranularity = 32;
                         
//Define the patchable data for database cache size here but which is actually used
//in CPplContactsFile.cpp. This is a must-be so that the compiler cannot perform any
//"constant folding" with the value   
EXPORT_C extern const TInt KContactsModelSqliteDbCacheSize = 0;
                         
_LIT(KSqLiteFilePrefix, "SQLite__");
const TInt KPrefixNameLength = 8;   // length of above prefix string

CDesCArray* CCntFileScanner::ListFilesL(RFs& aFs, TDriveUnit* aDriveUnit)
	{
	CCntFileScanner* scanner = new (ELeave) CCntFileScanner(aFs);  // gets pushed in first lines of ScanLD
	// coverity [memory_leak]
	return scanner->ScanLD(aDriveUnit);
	}


CCntFileScanner::CCntFileScanner(RFs& aFs)
	:
	iFs(aFs)
	{
	}


CCntFileScanner::~CCntFileScanner()
	{
	delete iDirStack;
	}


CDesCArray* CCntFileScanner::ScanLD(TDriveUnit* aDriveUnit)
	{
	CleanupStack::PushL(this);

	iFs.PrivatePath(iPath);
	iDirStack = new(ELeave) CDesCArrayFlat(KNameArrayGranularity);

	if (aDriveUnit)
		{
		AddDirL(*aDriveUnit);
		}
	else
		{
		GenerateRootDirsL();
		}

	CDesCArray* results = new(ELeave) CDesCArrayFlat(KNameArrayGranularity);
	CleanupStack::PushL(results);
	ScanForFilesL(*results);
	CleanupStack::Pop(results);
	CleanupStack::PopAndDestroy(this);

	return results;
	}


void CCntFileScanner::AddDirL(TDriveUnit& aDriveUnit)
	{
	ASSERT(iDirStack);
	TBuf<3> root;
	root.Copy(aDriveUnit.Name());
	root.Append(TChar(KPathDelimiter));
	iDirStack->AppendL(root);
	}

	
void CCntFileScanner::GenerateRootDirsL()
	{
	ASSERT(iDirStack);
	// Generate list of root dirs.
	TBuf<3> root;
	TDriveList driveList;
	User::LeaveIfError(iFs.DriveList(driveList));
	TInt len = driveList.Length();
	for (TInt index = 0; index < len; ++index)
		{
		if(driveList[index]) // Check drive exists.
			{
			root.Copy(TDriveUnit(index).Name());
			root.Append(TChar(KPathDelimiter));
			iDirStack->AppendL(root);
			}
		}
	}


void CCntFileScanner::ScanForFilesL(CDesCArray& aResults)
	{
	ASSERT(iDirStack);
	// Traverse the directory stack until all directories have been examined.
	
	TFileName filename;
	while(iDirStack->Count())
		{
		// Pop the dir.
		filename.Copy(iDirStack->MdcaPoint(0));
		iDirStack->Delete(0);
		if(iPath.Length()!=0)
			{
			filename.Delete(2,1);
			filename.Append(iPath);
			}
		else if(filename[filename.Length() - 1] != KPathDelimiter)
			{ // Check it has a trailing '\' to identify it as a dir path.
			filename.Append(TChar(KPathDelimiter));
			}
		TRAPD(err,ScanDirectoryL(filename, aResults));
		if(err==KErrNoMemory)
			{ // Continue to search in each drive except for out of memory.
			User::Leave(err);
			}
		}
	}


/**
Get a list of all files and subdirectories in aDirPath (when the path is not
specified).
*/
void CCntFileScanner::ScanDirectoryL(const TDesC& aDirPath, CDesCArray& aResults)
	{
	CDir* files = NULL;
	CDir* subdirs = NULL;

	User::LeaveIfError(iFs.GetDir(aDirPath, KEntryAttAllowUid, ESortByName, files, subdirs));

	TRAPD(err,AppendFileNamesL(*files, aDirPath, aResults));

	delete files;
	delete subdirs;
	User::LeaveIfError(err);
	}


void CCntFileScanner::AppendFileNamesL(const CDir& aFileSet, const TDesC& aDirPath, CDesCArray& aFileNames) const
	{
	TParse parse;
	TInt len = aFileSet.Count();
	
	for(TInt idx = 0; idx < len ; ++idx)
		{
		const TEntry& entry = aFileSet[idx];
		User::LeaveIfError(parse.SetNoWild(entry.iName, &aDirPath, NULL));
			
		TInt found = parse.NameAndExt().FindF(KSqLiteFilePrefix);
				
		if (found == 0 && iPath.Length() > 0)
			{
			// add drive, remove prefix, add to list of files
			TFileName filename(parse.Drive());
			filename.Append(parse.NameAndExt().Right(parse.NameAndExt().Length() - KPrefixNameLength));	
						
			aFileNames.AppendL(filename);
			}
			
		}
	}