|
1 // Copyright (c) 1997-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 #include "agsfilesearch.h" |
|
17 #include <f32file.h> |
|
18 // |
|
19 const TInt KNameArrayGranularity = 32; |
|
20 // |
|
21 |
|
22 CDesCArray* CAgnFileScanner::ListAgendaFilesL(RFs& aFs, const TUidType aFileType, const TDesC& aSearchPath) |
|
23 { |
|
24 CAgnFileScanner* scanner = new(ELeave) CAgnFileScanner(aFs, aFileType, aSearchPath); |
|
25 return scanner->ScanLD(); |
|
26 } |
|
27 |
|
28 CAgnFileScanner::CAgnFileScanner(RFs& aFs, const TUidType aFileType, const TDesC& aSearchPath) |
|
29 : iFs(aFs), iFileType(aFileType), iPath(aSearchPath) |
|
30 { |
|
31 } |
|
32 |
|
33 CAgnFileScanner::~CAgnFileScanner() |
|
34 { |
|
35 delete iDirStack; |
|
36 } |
|
37 |
|
38 CDesCArray* CAgnFileScanner::ScanLD() |
|
39 { |
|
40 CleanupStack::PushL(this); |
|
41 |
|
42 iDirStack = new(ELeave) CDesCArrayFlat(KNameArrayGranularity); |
|
43 GenerateRootDirsL(); |
|
44 |
|
45 CDesCArray* results = new(ELeave) CDesCArrayFlat(KNameArrayGranularity); |
|
46 CleanupStack::PushL(results); |
|
47 ScanForFilesL(*results); |
|
48 CleanupStack::Pop(results); |
|
49 CleanupStack::PopAndDestroy(this); |
|
50 |
|
51 return results; |
|
52 } |
|
53 |
|
54 void CAgnFileScanner::GenerateRootDirsL() |
|
55 { |
|
56 ASSERT(iDirStack); |
|
57 // Generate list of root dirs. |
|
58 TBuf<3> root; |
|
59 TDriveList driveList; |
|
60 User::LeaveIfError(iFs.DriveList(driveList)); |
|
61 TInt len = driveList.Length(); |
|
62 for (TInt index=0;index<len;++index) |
|
63 { |
|
64 if(driveList[index]) // Check drive exists. |
|
65 { |
|
66 root.Copy(TDriveUnit(index).Name()); |
|
67 root.Append(TChar(KPathDelimiter)); |
|
68 iDirStack->AppendL(root); |
|
69 } |
|
70 } |
|
71 } |
|
72 |
|
73 void CAgnFileScanner::ScanForFilesL(CDesCArray& aResults) |
|
74 { |
|
75 ASSERT(iDirStack); |
|
76 // Traverse the directory stack until all directories have been examined. |
|
77 // ScanDirectory may add new dirs to stack. |
|
78 TFileName filename; |
|
79 while(iDirStack->Count()) |
|
80 { |
|
81 // Pop the dir. |
|
82 filename.Copy(iDirStack->MdcaPoint(0)); |
|
83 iDirStack->Delete(0); |
|
84 if(iPath.Length()!=0) |
|
85 { |
|
86 filename.Delete(2,1); |
|
87 filename.Append(iPath); |
|
88 } |
|
89 else if(filename[filename.Length() - 1] != KPathDelimiter) |
|
90 {// Check it has a trailing '\' to identify it as a dir path. |
|
91 filename.Append(TChar(KPathDelimiter)); |
|
92 } |
|
93 TRAPD(err,ScanDirectoryL(filename, aResults));//c:\private\1234\aaa c:\private\1234\bbb |
|
94 if(err==KErrNoMemory) |
|
95 {//continue to search in each drive except for out of memory |
|
96 User::Leave(err); |
|
97 } |
|
98 } |
|
99 } |
|
100 |
|
101 void CAgnFileScanner::ScanDirectoryL(const TDesC& aDirPath, CDesCArray& aResults) |
|
102 {// Get a list of all files & subdirs in aDirPath (when the path is not specified). |
|
103 CDir* files = NULL; |
|
104 CDir* subdirs = NULL; |
|
105 |
|
106 User::LeaveIfError(iFs.GetDir(aDirPath, KEntryAttAllowUid, ESortByName, files, subdirs)); |
|
107 |
|
108 TRAPD(err,AppendFileNamesL(*files, aDirPath, aResults, &iFileType));//the result when the file is in root |
|
109 delete files; |
|
110 CleanupStack::PushL(subdirs); |
|
111 |
|
112 User::LeaveIfError(err); |
|
113 // Add files matching iFileType to results. |
|
114 if(iPath.Length()==0) |
|
115 // Add all subdirs to dir stack. |
|
116 { |
|
117 AppendFileNamesL(*subdirs, aDirPath, *iDirStack, NULL);//the result where the file is in sub it is reused in ScanForFilesL which might not needed in secured data |
|
118 } |
|
119 CleanupStack::PopAndDestroy(subdirs); |
|
120 } |
|
121 |
|
122 void CAgnFileScanner::AppendFileNamesL(const CDir& aFileSet, const TDesC& aDirPath, CDesCArray& aFileNames, const TUidType* aUid) const |
|
123 { |
|
124 TParse parse; |
|
125 TInt len = aFileSet.Count(); |
|
126 for(TInt idx=0;idx<len;++idx) |
|
127 { |
|
128 const TEntry& entry = aFileSet[idx]; |
|
129 User::LeaveIfError(parse.SetNoWild(entry.iName, &aDirPath, NULL)); |
|
130 if(aUid) |
|
131 { |
|
132 if(entry.iType==(*aUid)) |
|
133 { |
|
134 if(iPath.Length()>0) |
|
135 {//files are in server's private directory so the file should be in form drive:filename |
|
136 TFileName filename(parse.Drive()); |
|
137 filename.Append(parse.NameAndExt()); |
|
138 aFileNames.AppendL(filename); |
|
139 } |
|
140 else |
|
141 { |
|
142 aFileNames.AppendL(parse.FullName()); |
|
143 } |
|
144 } |
|
145 } |
|
146 else |
|
147 { |
|
148 aFileNames.AppendL(parse.FullName()); |
|
149 } |
|
150 } |
|
151 } |
|
152 |
|
153 |