|
1 /* |
|
2 * Copyright (c) 2002-2004 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: Xuikon hspshspsFileChangeListenerEmu listens to given file folder and notifies it observer (Only in Emulator environment) |
|
15 * if there is any file change. |
|
16 * |
|
17 * |
|
18 */ |
|
19 |
|
20 |
|
21 #include "hsps_builds_cfg.hrh" |
|
22 |
|
23 #ifdef _hsps_EMULATOR_SUPPORT_ |
|
24 |
|
25 #include "hspsfilechangelisteneremu.h" |
|
26 |
|
27 #ifdef HSPS_LOG_ACTIVE |
|
28 #include <hspslogbus.h> |
|
29 #endif |
|
30 |
|
31 // ============================ MEMBER FUNCTIONS =============================== |
|
32 |
|
33 // ----------------------------------------------------------------------------- |
|
34 // ChspsFileChangeListenerEmu::ChspsFileChangeListenerEmu |
|
35 // C++ default constructor can NOT contain any code, that |
|
36 // might leave. |
|
37 // ----------------------------------------------------------------------------- |
|
38 // |
|
39 ChspsFileChangeListenerEmu::ChspsFileChangeListenerEmu( RFs& aFs, |
|
40 MhspsEmuFileChangeObserver& aObserver, |
|
41 TEmuNotifyType aNotifyType) : |
|
42 CTimer(EPriorityLow), |
|
43 iFs( aFs ), |
|
44 iObserver( aObserver), |
|
45 iNotifyType( aNotifyType ) |
|
46 { |
|
47 } |
|
48 |
|
49 // ----------------------------------------------------------------------------- |
|
50 // ChspsFileChangeListenerEmu::ConstructL |
|
51 // Symbian 2nd phase constructor can leave. |
|
52 // ----------------------------------------------------------------------------- |
|
53 // |
|
54 void ChspsFileChangeListenerEmu::ConstructL( const TDesC& aFullPath ) |
|
55 { |
|
56 |
|
57 iFileList = new( ELeave ) CDesCArraySeg(KFilelistGranularity ); |
|
58 iState = EEmuStateIdle; |
|
59 |
|
60 if ( aFullPath.Length() > 0 ) |
|
61 { |
|
62 iFullPath.Append( aFullPath ); |
|
63 } |
|
64 CTimer::ConstructL(); |
|
65 CActiveScheduler::Add( this ); |
|
66 } |
|
67 |
|
68 // ----------------------------------------------------------------------------- |
|
69 // ChspsFileChangeListenerEmu::NewL |
|
70 // Two-phased constructor. |
|
71 // ----------------------------------------------------------------------------- |
|
72 // |
|
73 ChspsFileChangeListenerEmu* ChspsFileChangeListenerEmu::NewL( RFs& aFs, |
|
74 MhspsEmuFileChangeObserver& aObserver, |
|
75 TEmuNotifyType aNotifyType, |
|
76 const TDesC& aFullPath) |
|
77 { |
|
78 ChspsFileChangeListenerEmu* self = new(ELeave) ChspsFileChangeListenerEmu( aFs, |
|
79 aObserver, |
|
80 aNotifyType); |
|
81 CleanupStack::PushL( self ); |
|
82 self->ConstructL( aFullPath ); |
|
83 CleanupStack::Pop( self ); |
|
84 |
|
85 return self; |
|
86 } |
|
87 |
|
88 |
|
89 // ------------------------------------------------------------------------------ |
|
90 // ChspsFileChangeListenerEmu::~ChspsFileChangeListenerEmu |
|
91 // Destructor |
|
92 // ------------------------------------------------------------------------------ |
|
93 ChspsFileChangeListenerEmu::~ChspsFileChangeListenerEmu() |
|
94 { |
|
95 Cancel(); |
|
96 |
|
97 if (iFileList) |
|
98 { |
|
99 delete iFileList; |
|
100 iFileList = NULL; |
|
101 } |
|
102 |
|
103 } |
|
104 |
|
105 // ----------------------------------------------------------------------------- |
|
106 // ChspsFileChangeListenerEmu::RunL |
|
107 // From CActive. Called when asynchronous request is completed. |
|
108 // Notifies the observer |
|
109 // (other items were commented in a header). |
|
110 // ----------------------------------------------------------------------------- |
|
111 // |
|
112 void ChspsFileChangeListenerEmu::RunL() |
|
113 { |
|
114 TBool Diff(EFalse); |
|
115 TInt errCode = KErrNone; |
|
116 if (iStatus.Int() == KErrNone) |
|
117 { |
|
118 CDesCArraySeg* CurFileList = new( ELeave ) CDesCArraySeg(KFilelistGranularity ); |
|
119 CleanupStack::PushL(CurFileList); |
|
120 |
|
121 // Retrieve current list of files in filesystem |
|
122 TRAP(errCode, RetrieveFileListL(*CurFileList, iFullPath);) |
|
123 if (errCode) |
|
124 { |
|
125 #ifdef HSPS_LOG_ACTIVE |
|
126 if( iLogBus ) |
|
127 { |
|
128 iLogBus->LogText( _L( "ChspsFileChangeListenerEmu::RunL():RetrieveFileListL, Error=%d" ), |
|
129 errCode ); |
|
130 } |
|
131 #endif |
|
132 } |
|
133 |
|
134 // Compare files |
|
135 TRAP(errCode, Diff = CompareFilesL(*CurFileList);) |
|
136 if (errCode) |
|
137 { |
|
138 #ifdef HSPS_LOG_ACTIVE |
|
139 if( iLogBus ) |
|
140 { |
|
141 iLogBus->LogText( _L( "ChspsFileChangeListenerEmu::RunL():CompareFilesL, Error=%d" ), |
|
142 errCode ); |
|
143 } |
|
144 #endif |
|
145 } |
|
146 |
|
147 // If changes, call observer handle function |
|
148 if (Diff) |
|
149 { |
|
150 InformObserver(); |
|
151 } |
|
152 |
|
153 // Store current file situation |
|
154 TRAP(errCode, StoreFileListL(*CurFileList);) |
|
155 if (errCode) |
|
156 { |
|
157 #ifdef HSPS_LOG_ACTIVE |
|
158 if( iLogBus ) |
|
159 { |
|
160 iLogBus->LogText( _L( "ChspsFileChangeListenerEmu::RunL():StoreFileListL, Error=%d" ), |
|
161 errCode ); |
|
162 } |
|
163 #endif |
|
164 } |
|
165 |
|
166 CleanupStack::Pop( CurFileList ); |
|
167 if ( CurFileList ) |
|
168 { |
|
169 delete CurFileList; |
|
170 CurFileList = NULL; |
|
171 } |
|
172 |
|
173 // Restart timer |
|
174 After(KFileSystemPollingTime); |
|
175 } |
|
176 |
|
177 } |
|
178 |
|
179 // ----------------------------------------------------------------------------- |
|
180 // ChspsFileChangeListenerEmu::RunError |
|
181 // From CActive. Called when error occurred in asynchronous request |
|
182 // (other items were commented in a header). |
|
183 // ----------------------------------------------------------------------------- |
|
184 // |
|
185 TInt ChspsFileChangeListenerEmu::RunError( TInt /*aError*/ ) |
|
186 { |
|
187 return KErrNone; |
|
188 } |
|
189 |
|
190 // ----------------------------------------------------------------------------- |
|
191 // ChspsFileChangeListenerEmu::DoCancel() |
|
192 // Not implemented yet |
|
193 // (other items were commented in a header). |
|
194 // ----------------------------------------------------------------------------- |
|
195 // |
|
196 void ChspsFileChangeListenerEmu::DoCancel() |
|
197 { |
|
198 iState = EEmuStateIdle; |
|
199 CTimer::DoCancel(); |
|
200 } |
|
201 |
|
202 // ------------------------------------------------------------------------------ |
|
203 // ChspsFileChangeListenerEmu::StartL |
|
204 // Starts polling directory for changes |
|
205 // ------------------------------------------------------------------------------ |
|
206 // |
|
207 void ChspsFileChangeListenerEmu::StartL() |
|
208 { |
|
209 TInt errCode = KErrNone; |
|
210 |
|
211 if (iState == EEmuStateIdle) |
|
212 { |
|
213 CDesCArraySeg* CurFileList = new( ELeave ) CDesCArraySeg(KFilelistGranularity ); |
|
214 |
|
215 CleanupStack::PushL(CurFileList); |
|
216 |
|
217 // Retrieve current list of files in filesystem |
|
218 TRAP(errCode, RetrieveFileListL(*CurFileList, iFullPath);) |
|
219 if (errCode) |
|
220 { |
|
221 User::Leave(errCode); |
|
222 } |
|
223 |
|
224 // Store current file situation |
|
225 TRAP(errCode, StoreFileListL(*CurFileList);) |
|
226 if (errCode) |
|
227 { |
|
228 User::Leave(errCode); |
|
229 } |
|
230 |
|
231 CleanupStack::Pop( CurFileList ); |
|
232 if ( CurFileList ) |
|
233 { |
|
234 delete CurFileList; |
|
235 CurFileList = NULL; |
|
236 } |
|
237 |
|
238 // Start timer |
|
239 After(KFileSystemPollingTime); |
|
240 |
|
241 iState = EEmuStateRunning; |
|
242 } |
|
243 |
|
244 } |
|
245 |
|
246 // ------------------------------------------------------------------------------ |
|
247 // ChspsFileChangeListenerEmu::Stop |
|
248 // Stops polling of directory |
|
249 // ------------------------------------------------------------------------------ |
|
250 // |
|
251 void ChspsFileChangeListenerEmu::Stop() |
|
252 { |
|
253 Cancel(); |
|
254 } |
|
255 |
|
256 #ifdef HSPS_LOG_ACTIVE |
|
257 // ------------------------------------------------------------------------------ |
|
258 // ChspsFileChangeListenerEmu::SetLogBus |
|
259 // Set log bus to be used. |
|
260 // ------------------------------------------------------------------------------ |
|
261 // |
|
262 void ChspsFileChangeListenerEmu::SetLogBus( ChspsLogBus* aLogBus ) |
|
263 { |
|
264 iLogBus = aLogBus; |
|
265 } |
|
266 #endif |
|
267 |
|
268 |
|
269 // ------------------------------------------------------------------------------ |
|
270 // ChspsFileChangeListenerEmu::InformObserver |
|
271 // Informs observer of file change |
|
272 // ------------------------------------------------------------------------------ |
|
273 // |
|
274 void ChspsFileChangeListenerEmu::InformObserver() |
|
275 { |
|
276 iObserver.HandleEmuFileSystemChange(); |
|
277 } |
|
278 |
|
279 // ------------------------------------------------------------------------------ |
|
280 // ChspsFileChangeListenerEmu::RetrieveFileListL |
|
281 // Retrieves current list of files in filesystem |
|
282 // ------------------------------------------------------------------------------ |
|
283 // |
|
284 void ChspsFileChangeListenerEmu::RetrieveFileListL( CDesCArraySeg& aFileList, const TDesC& aPath ) |
|
285 { |
|
286 CDirScan* dir_list = CDirScan::NewL( iFs ); |
|
287 CleanupStack::PushL( dir_list ); |
|
288 |
|
289 dir_list->SetScanDataL( aPath, |
|
290 KEntryAttNormal, |
|
291 ESortByName | EAscending, |
|
292 CDirScan::EScanDownTree ); |
|
293 |
|
294 CDir* dirEntries = NULL; |
|
295 dir_list->NextL( dirEntries ); |
|
296 while( dirEntries != NULL ) |
|
297 { |
|
298 CleanupStack::PushL( dirEntries ); |
|
299 |
|
300 TInt iDirCount = dirEntries->Count(); |
|
301 for( TInt i = 0; i < iDirCount; i++ ) |
|
302 { |
|
303 const TEntry a = dirEntries->operator[]( i ); |
|
304 aFileList.AppendL( a.iName ); |
|
305 } |
|
306 |
|
307 CleanupStack::PopAndDestroy( dirEntries ); |
|
308 dirEntries = NULL; |
|
309 dir_list->NextL( dirEntries ); |
|
310 } |
|
311 |
|
312 CleanupStack::PopAndDestroy(dir_list); |
|
313 |
|
314 } |
|
315 |
|
316 // ------------------------------------------------------------------------------ |
|
317 // ChspsFileChangeListenerEmu::StoreFileListL |
|
318 // Stores list of files to memory |
|
319 // ------------------------------------------------------------------------------ |
|
320 // |
|
321 void ChspsFileChangeListenerEmu::StoreFileListL(CDesCArraySeg& aFileList) |
|
322 { |
|
323 |
|
324 if (iFileList) |
|
325 { |
|
326 iFileList->Reset(); |
|
327 |
|
328 for ( TInt i = 0; i < aFileList.Count(); i++) |
|
329 { |
|
330 TPtrC t = aFileList.MdcaPoint(i); |
|
331 iFileList->AppendL(t); |
|
332 } |
|
333 } |
|
334 } |
|
335 |
|
336 |
|
337 // ------------------------------------------------------------------------------ |
|
338 // ChspsFileChangeListenerEmu::CompareFilesL |
|
339 // Compares given filelist to filelist on memory |
|
340 // ------------------------------------------------------------------------------ |
|
341 // |
|
342 TBool ChspsFileChangeListenerEmu::CompareFilesL(CDesCArraySeg& aFileList) |
|
343 { |
|
344 TInt p = 0; |
|
345 TBool ret(EFalse); |
|
346 |
|
347 if (iFileList->Count() == aFileList.Count() ) |
|
348 { |
|
349 for (p = 0;p < iFileList->Count(); p++) |
|
350 { |
|
351 TBool found(EFalse); |
|
352 TInt i=0; |
|
353 while (!found && i<aFileList.Count()) |
|
354 { |
|
355 TPtrC t = iFileList->MdcaPoint(i); |
|
356 if (!(t.CompareF(aFileList.MdcaPoint(p)) ) ) |
|
357 { |
|
358 found=ETrue; |
|
359 break; |
|
360 } |
|
361 |
|
362 i++; |
|
363 } |
|
364 |
|
365 if (!found) |
|
366 { |
|
367 ret = ETrue; |
|
368 break; |
|
369 } |
|
370 } |
|
371 } |
|
372 else |
|
373 { |
|
374 ret = ETrue; |
|
375 } |
|
376 |
|
377 return ret; |
|
378 |
|
379 } |
|
380 |
|
381 #endif // _hsps_EMULATOR_SUPPORT_ |
|
382 |
|
383 // End of File |