|
1 /* |
|
2 * Copyright (c) 2006 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: Test support file tool |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #include <e32std.h> |
|
21 #include <f32file.h> |
|
22 #include <s32file.h> |
|
23 |
|
24 #include "prfwtestfiletool.h" |
|
25 #include "prfwtestfilesrvmsg.h" |
|
26 #include "ximpeventcodec.h" |
|
27 #include "prfwtestfileobserver.h" |
|
28 |
|
29 /* Hierarchy under C:/temp/prfw/: |
|
30 * /protocoluid/instancenumber/Plugin\ |
|
31 * /protocoluid/instancenumber/SrvMsg\ |
|
32 */ |
|
33 |
|
34 _LIT( KFileToolMainDirFull, "c:\\temp\\prfw" ); |
|
35 |
|
36 _LIT( KFileToolBackslash, "\\" ); |
|
37 _LIT( KFileToolDirUid, "%d" ); |
|
38 _LIT( KInstanceAsNumber, "%d" ); |
|
39 |
|
40 _LIT( KFileToolPluginDirBase, "Plugin" ); |
|
41 _LIT( KFileToolSrvDirBase, "SrvMsg" ); |
|
42 |
|
43 _LIT( KFileToolWildcard, "file*.prfw" ); |
|
44 _LIT( KFileToolFileBase, "file%d.prfw" ); |
|
45 |
|
46 // ======== LOCAL FUNCTIONS ======== |
|
47 |
|
48 // ======== MEMBER FUNCTIONS ======== |
|
49 |
|
50 // --------------------------------------------------------------------------- |
|
51 // ?description_if_needed |
|
52 // --------------------------------------------------------------------------- |
|
53 // |
|
54 CXIMPTestFileTool::CXIMPTestFileTool( TUint32 aUid ) : |
|
55 iUid( aUid ) |
|
56 { |
|
57 iFs.Connect(); |
|
58 } |
|
59 |
|
60 // --------------------------------------------------------------------------- |
|
61 // ?description_if_needed |
|
62 // --------------------------------------------------------------------------- |
|
63 // |
|
64 void CXIMPTestFileTool::ConstructL( const TDesC16& aInstanceId ) |
|
65 { |
|
66 iTimer = CPeriodic::NewL( CActive::EPriorityStandard ); |
|
67 iInstance = aInstanceId.AllocL(); |
|
68 } |
|
69 |
|
70 // --------------------------------------------------------------------------- |
|
71 // ?description_if_needed |
|
72 // --------------------------------------------------------------------------- |
|
73 // |
|
74 void CXIMPTestFileTool::ConstructL( TInt aInstanceIdAsNum ) |
|
75 { |
|
76 TInt instanceLength = NumLenInChars( aInstanceIdAsNum ); |
|
77 HBufC* instance = HBufC::NewLC( instanceLength ); |
|
78 instance->Des().AppendFormat( KInstanceAsNumber, aInstanceIdAsNum ); |
|
79 ConstructL( *instance ); |
|
80 CleanupStack::PopAndDestroy( instance ); |
|
81 } |
|
82 |
|
83 // --------------------------------------------------------------------------- |
|
84 // ?description_if_needed |
|
85 // --------------------------------------------------------------------------- |
|
86 // |
|
87 EXPORT_C CXIMPTestFileTool* CXIMPTestFileTool::NewL( TUint32 aUid, const TDesC16& aInstanceId ) |
|
88 { |
|
89 CXIMPTestFileTool* self = new( ELeave ) CXIMPTestFileTool( aUid ); |
|
90 CleanupStack::PushL( self ); |
|
91 self->ConstructL( aInstanceId ); |
|
92 CleanupStack::Pop( self ); |
|
93 return self; |
|
94 } |
|
95 |
|
96 // --------------------------------------------------------------------------- |
|
97 // ?description_if_needed |
|
98 // --------------------------------------------------------------------------- |
|
99 // |
|
100 EXPORT_C CXIMPTestFileTool* CXIMPTestFileTool::NewL( TUint32 aUid, TInt aInstanceIdAsNum ) |
|
101 { |
|
102 CXIMPTestFileTool* self = new( ELeave ) CXIMPTestFileTool( aUid ); |
|
103 CleanupStack::PushL( self ); |
|
104 self->ConstructL( aInstanceIdAsNum ); |
|
105 CleanupStack::Pop( self ); |
|
106 return self; |
|
107 } |
|
108 |
|
109 |
|
110 // --------------------------------------------------------------------------- |
|
111 // ?description_if_needed |
|
112 // --------------------------------------------------------------------------- |
|
113 // |
|
114 CXIMPTestFileTool::~CXIMPTestFileTool() |
|
115 { |
|
116 iFs.Close(); |
|
117 if ( iTimer ) |
|
118 { |
|
119 iTimer->Deque(); |
|
120 } |
|
121 delete iTimer; |
|
122 iObservers.Close(); |
|
123 delete iInstance; |
|
124 } |
|
125 |
|
126 // --------------------------------------------------------------------------- |
|
127 // Remove the entire prfw directory |
|
128 // --------------------------------------------------------------------------- |
|
129 // |
|
130 EXPORT_C void CXIMPTestFileTool::CleanAllL() |
|
131 { |
|
132 RFs fs; |
|
133 fs.Connect(); |
|
134 CleanupClosePushL( fs ); |
|
135 |
|
136 CFileMan* fileMan = CFileMan::NewL( fs ); |
|
137 CleanupStack::PushL( fileMan ); |
|
138 |
|
139 // remove the files and the directory |
|
140 fileMan->RmDir( KFileToolMainDirFull ); |
|
141 |
|
142 CleanupStack::PopAndDestroy( 2 ); // fileman, fs |
|
143 } |
|
144 |
|
145 // --------------------------------------------------------------------------- |
|
146 // Prepare everything, after this objects can be externalized and |
|
147 // internalized |
|
148 // --------------------------------------------------------------------------- |
|
149 // |
|
150 EXPORT_C void CXIMPTestFileTool::PrepareL() |
|
151 { |
|
152 // create the directory structure |
|
153 HBufC* dirName = GetDirNameAndBackslashLC( KFileToolPluginDirBase ); |
|
154 iFs.MkDirAll( *dirName ); |
|
155 CleanupStack::PopAndDestroy( dirName ); |
|
156 |
|
157 dirName = GetDirNameAndBackslashLC( KFileToolSrvDirBase ); |
|
158 iFs.MkDirAll( *dirName ); |
|
159 CleanupStack::PopAndDestroy( dirName ); |
|
160 } |
|
161 |
|
162 // --------------------------------------------------------------------------- |
|
163 // Write the file to correct directory. |
|
164 // --------------------------------------------------------------------------- |
|
165 // |
|
166 EXPORT_C void CXIMPTestFileTool::PluginStoreL( const TDesC8& aExternalizedObject ) |
|
167 { |
|
168 HBufC* fileName = GetFileNameLC( |
|
169 KFileToolPluginDirBase, |
|
170 iObjIndex ); |
|
171 |
|
172 RFileWriteStream out; |
|
173 CleanupClosePushL( out ); |
|
174 out.Create( iFs, *fileName, EFileWrite|EFileStream|EFileShareAny ); |
|
175 |
|
176 // write the file |
|
177 TUint32 len = aExternalizedObject.Length(); |
|
178 out.WriteUint32L( len ); |
|
179 out.WriteL( aExternalizedObject ); |
|
180 CleanupStack::PopAndDestroy(); // out |
|
181 |
|
182 CleanupStack::PopAndDestroy( fileName ); // fileName |
|
183 |
|
184 // next file will have a new index |
|
185 iObjIndex++; |
|
186 } |
|
187 |
|
188 // --------------------------------------------------------------------------- |
|
189 // Restore the file from given directory and given index |
|
190 // --------------------------------------------------------------------------- |
|
191 // |
|
192 EXPORT_C CXIMPApiEventBase* CXIMPTestFileTool::PluginRestoreLC( |
|
193 TInt aObjIndex ) |
|
194 { |
|
195 HBufC* fileName = GetFileNameLC( |
|
196 KFileToolPluginDirBase, |
|
197 aObjIndex ); |
|
198 |
|
199 // try to instantiate it |
|
200 RFileReadStream in; |
|
201 CleanupClosePushL( in ); |
|
202 |
|
203 in.Open( iFs, *fileName, EFileStream ); |
|
204 TInt len = in.ReadUint32L(); |
|
205 HBufC8* buffer = HBufC8::NewLC( len ); |
|
206 TPtr8 ptr = buffer->Des(); |
|
207 in.ReadL( ptr ); |
|
208 |
|
209 // instantiate the object |
|
210 TInt32 dummy = 0; |
|
211 CXIMPApiEventBase* object = XIMPEventCodec::UnPackL( *buffer, dummy ); |
|
212 |
|
213 CleanupStack::PopAndDestroy( 2 ); // buffer, in |
|
214 |
|
215 // delete the just read object |
|
216 iFs.Delete( *fileName ); |
|
217 |
|
218 CleanupStack::PopAndDestroy( fileName ); |
|
219 |
|
220 CleanupStack::PushL( object ); |
|
221 return object; |
|
222 } |
|
223 |
|
224 // --------------------------------------------------------------------------- |
|
225 // Write the server message object to correct directory. |
|
226 // --------------------------------------------------------------------------- |
|
227 // |
|
228 EXPORT_C void CXIMPTestFileTool::SrvMsgStoreL( |
|
229 CXIMPTestFileSrvMsg* aSrvMsg ) |
|
230 { |
|
231 TInt nextObjIndex = NumSrvMsgL(); |
|
232 HBufC* fileName = GetFileNameLC( |
|
233 KFileToolSrvDirBase, |
|
234 nextObjIndex); |
|
235 |
|
236 RFileWriteStream out; |
|
237 CleanupClosePushL( out ); |
|
238 TInt retVal = out.Create( iFs, *fileName, EFileWrite|EFileStream|EFileShareAny ); |
|
239 User::LeaveIfError( retVal ); |
|
240 |
|
241 aSrvMsg->ExternalizeL( out ); |
|
242 |
|
243 CleanupStack::PopAndDestroy( 2, fileName ); // out, fileName |
|
244 |
|
245 // next file will have a new index |
|
246 //iSrvObjIndex++; |
|
247 } |
|
248 |
|
249 // --------------------------------------------------------------------------- |
|
250 // Restore the file from given directory and given index |
|
251 // --------------------------------------------------------------------------- |
|
252 // |
|
253 EXPORT_C CXIMPTestFileSrvMsg* CXIMPTestFileTool::SrvMsgRestoreLC( |
|
254 TInt aObjIndex ) |
|
255 { |
|
256 CXIMPTestFileSrvMsg* msg = CXIMPTestFileSrvMsg::NewLC(); |
|
257 |
|
258 HBufC* fileName = GetFileNameLC( |
|
259 KFileToolSrvDirBase, |
|
260 aObjIndex ); |
|
261 |
|
262 // try to instantiate it |
|
263 RFileReadStream in; |
|
264 CleanupClosePushL( in ); |
|
265 |
|
266 in.Open( iFs, *fileName, EFileStream ); |
|
267 |
|
268 msg->InternalizeL( in ); |
|
269 |
|
270 CleanupStack::PopAndDestroy(); // in |
|
271 |
|
272 // delete the just read object |
|
273 iFs.Delete( *fileName ); |
|
274 |
|
275 CleanupStack::PopAndDestroy( fileName ); // fileName |
|
276 return msg; |
|
277 } |
|
278 |
|
279 // --------------------------------------------------------------------------- |
|
280 // Common file counting helper |
|
281 // --------------------------------------------------------------------------- |
|
282 // |
|
283 TInt CXIMPTestFileTool::DoNumObjectsL( const TDesC& aTemplate ) |
|
284 { |
|
285 CDir* dir = GetDirListL( aTemplate ); |
|
286 TInt count = dir->Count(); |
|
287 delete dir; |
|
288 return count; |
|
289 } |
|
290 |
|
291 // --------------------------------------------------------------------------- |
|
292 // Number of objects (files) in a given directory |
|
293 // --------------------------------------------------------------------------- |
|
294 // |
|
295 EXPORT_C TInt CXIMPTestFileTool::NumObjectsL() |
|
296 { |
|
297 return DoNumObjectsL( KFileToolPluginDirBase ); |
|
298 } |
|
299 |
|
300 // --------------------------------------------------------------------------- |
|
301 // Number of server-originated objects (files) in a given directory |
|
302 // --------------------------------------------------------------------------- |
|
303 // |
|
304 EXPORT_C TInt CXIMPTestFileTool::NumSrvMsgL() |
|
305 { |
|
306 // returns the number of objects in the directory |
|
307 // get file listing and derive the count from that |
|
308 return DoNumObjectsL( KFileToolSrvDirBase ); |
|
309 } |
|
310 |
|
311 // --------------------------------------------------------------------------- |
|
312 // Instace id |
|
313 // --------------------------------------------------------------------------- |
|
314 // |
|
315 EXPORT_C const TDesC16& CXIMPTestFileTool::InstanceId() const |
|
316 { |
|
317 return *iInstance; |
|
318 } |
|
319 |
|
320 // --------------------------------------------------------------------------- |
|
321 // Length of number in characters (e.g. 501 --> 3) |
|
322 // --------------------------------------------------------------------------- |
|
323 // |
|
324 TInt CXIMPTestFileTool::NumLenInChars( TInt aNum ) |
|
325 { |
|
326 TInt len = 0; |
|
327 TInt tmp = aNum; |
|
328 do |
|
329 { |
|
330 tmp /= 10; // number is base-10 |
|
331 len++; |
|
332 } |
|
333 while ( tmp > 0 ); |
|
334 return len; |
|
335 } |
|
336 |
|
337 // --------------------------------------------------------------------------- |
|
338 // Gets properly expanded directory name with backslash appended |
|
339 // --------------------------------------------------------------------------- |
|
340 // |
|
341 HBufC* CXIMPTestFileTool::GetDirNameAndBackslashLC( |
|
342 const TDesC& aTemplate ) |
|
343 { |
|
344 TInt uidLength = NumLenInChars( iUid ); |
|
345 TInt instanceLength = iInstance->Length(); |
|
346 |
|
347 HBufC* dirName = HBufC::NewLC( |
|
348 KFileToolMainDirFull().Length() |
|
349 + KFileToolBackslash().Length() |
|
350 + KFileToolDirUid().Length() |
|
351 -2 // "%d" gets modified to be the number |
|
352 + KFileToolBackslash().Length() |
|
353 + uidLength + instanceLength |
|
354 + KFileToolBackslash().Length() |
|
355 + aTemplate.Length() |
|
356 + KFileToolBackslash().Length() // added for later use by GetDirNameAndBackslashLC. |
|
357 ); |
|
358 |
|
359 dirName->Des().Append( KFileToolMainDirFull ); |
|
360 dirName->Des().Append( KFileToolBackslash ); |
|
361 dirName->Des().AppendFormat( KFileToolDirUid, iUid ); |
|
362 dirName->Des().Append( KFileToolBackslash ); |
|
363 dirName->Des().Append( *iInstance ); |
|
364 dirName->Des().Append( KFileToolBackslash ); |
|
365 dirName->Des().Append( aTemplate ); |
|
366 |
|
367 dirName->Des().Append( KFileToolBackslash ); |
|
368 iFs.MkDirAll( *dirName ); |
|
369 return dirName; |
|
370 } |
|
371 |
|
372 // --------------------------------------------------------------------------- |
|
373 // Get full path wildcard for files residing in a directory |
|
374 // with given index |
|
375 // --------------------------------------------------------------------------- |
|
376 // |
|
377 HBufC* CXIMPTestFileTool::GetWildcardLC( |
|
378 const TDesC& aDirTemplate ) |
|
379 { |
|
380 HBufC* dirName = GetDirNameAndBackslashLC( aDirTemplate ); |
|
381 HBufC* wildcard = HBufC::NewLC( |
|
382 dirName->Length() + KFileToolWildcard().Length() ); |
|
383 wildcard->Des().Append( *dirName ); |
|
384 wildcard->Des().Append( KFileToolWildcard ); |
|
385 |
|
386 CleanupStack::Pop( 2 ); // wildcard, dirName |
|
387 delete dirName; |
|
388 |
|
389 CleanupStack::PushL( wildcard ); |
|
390 return wildcard; |
|
391 } |
|
392 |
|
393 // --------------------------------------------------------------------------- |
|
394 // Get a list of files in a given directory |
|
395 // --------------------------------------------------------------------------- |
|
396 // |
|
397 CDir* CXIMPTestFileTool::GetDirListL( const TDesC& aTemplate ) |
|
398 { |
|
399 HBufC* wildcard = GetWildcardLC( aTemplate ); |
|
400 |
|
401 CDir* dirList = NULL; |
|
402 iFs.GetDir( *wildcard, KEntryAttNormal, ESortByName, dirList ); |
|
403 CleanupStack::PopAndDestroy( wildcard ); |
|
404 |
|
405 return dirList; |
|
406 } |
|
407 |
|
408 // --------------------------------------------------------------------------- |
|
409 // Get fully qualified file name |
|
410 // --------------------------------------------------------------------------- |
|
411 // |
|
412 HBufC* CXIMPTestFileTool::GetFileNameLC( |
|
413 const TDesC& aDirTemplate, |
|
414 TInt aObjIndex ) |
|
415 { |
|
416 // create file name |
|
417 HBufC* dirName = GetDirNameAndBackslashLC( aDirTemplate ); |
|
418 |
|
419 TInt numLen = NumLenInChars( aObjIndex ); |
|
420 HBufC* fileName = HBufC::NewLC( |
|
421 dirName->Length() |
|
422 + KFileToolFileBase().Length() |
|
423 + numLen |
|
424 - 2 // "%d" gets modified to be the number |
|
425 ); |
|
426 fileName->Des().Append( *dirName ); |
|
427 fileName->Des().AppendFormat( KFileToolFileBase, aObjIndex ); |
|
428 |
|
429 CleanupStack::Pop( fileName ); |
|
430 CleanupStack::PopAndDestroy( dirName ); |
|
431 CleanupStack::PushL( fileName ); |
|
432 |
|
433 // now we have the filename |
|
434 return fileName; |
|
435 } |
|
436 |
|
437 const TInt KSecond = 1000000; |
|
438 |
|
439 // --------------------------------------------------------------------------- |
|
440 // CXIMPTestFileTool::RegisterObserverL |
|
441 // --------------------------------------------------------------------------- |
|
442 // |
|
443 void CXIMPTestFileTool::RegisterObserverL( MXIMPTestFileObserver* aObserver ) |
|
444 { |
|
445 iObservers.AppendL( aObserver ); |
|
446 if( !iTimer->IsActive() ) |
|
447 { |
|
448 TCallBack callback( Process, this ); |
|
449 iTimer->Start( KSecond, KSecond, callback ); |
|
450 } |
|
451 } |
|
452 |
|
453 // --------------------------------------------------------------------------- |
|
454 // CXIMPTestFileTool::UnregisterObserver |
|
455 // --------------------------------------------------------------------------- |
|
456 // |
|
457 void CXIMPTestFileTool::UnregisterObserver( MXIMPTestFileObserver* aObserver ) |
|
458 { |
|
459 TInt index = iObservers.Find( aObserver ); |
|
460 if( index != KErrNotFound ) |
|
461 { |
|
462 iObservers.Remove( index ); |
|
463 } |
|
464 if( iObservers.Count() == 0 ) |
|
465 { |
|
466 iTimer->Cancel(); |
|
467 } |
|
468 } |
|
469 |
|
470 |
|
471 // --------------------------------------------------------------------------- |
|
472 // CXIMPTestFileTool::RunL |
|
473 // --------------------------------------------------------------------------- |
|
474 // |
|
475 TInt CXIMPTestFileTool::Process( TAny* aMyself ) |
|
476 { |
|
477 TRAPD( ignore, ( static_cast< CXIMPTestFileTool* >( aMyself ) )->DoProcessL() ); |
|
478 return 0; // ignored by CPeriodic |
|
479 } |
|
480 |
|
481 // --------------------------------------------------------------------------- |
|
482 // CXIMPTestFileTool::RunL |
|
483 // --------------------------------------------------------------------------- |
|
484 // |
|
485 void CXIMPTestFileTool::DoProcessL() |
|
486 { |
|
487 TInt countOfSrvMsg = NumSrvMsgL(); |
|
488 TInt countOfObservers = iObservers.Count(); |
|
489 |
|
490 for( TInt a = 0; a < countOfSrvMsg; ++a ) |
|
491 { |
|
492 CXIMPTestFileSrvMsg* newMsg = SrvMsgRestoreLC( a ); |
|
493 for( TInt b = 0;b < countOfObservers; ++b ) |
|
494 { |
|
495 iObservers[ b ]->HandleL( newMsg->SrvMsgId(), newMsg->PayloadL() ); |
|
496 } |
|
497 CleanupStack::PopAndDestroy( newMsg ); |
|
498 } |
|
499 } |
|
500 |
|
501 |
|
502 // End of file |