1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies). |
1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies). |
2 // All rights reserved. |
2 // All rights reserved. |
3 // This component and the accompanying materials are made available |
3 // This component and the accompanying materials are made available |
4 // under the terms of the License "Eclipse Public License v1.0" |
4 // under the terms of "Eclipse Public License v1.0" |
5 // which accompanies this distribution, and is available |
5 // which accompanies this distribution, and is available |
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
7 // |
7 // |
8 // Initial Contributors: |
8 // Initial Contributors: |
9 // Nokia Corporation - initial contribution. |
9 // Nokia Corporation - initial contribution. |
10 // |
10 // |
11 // Contributors: |
11 // Contributors: |
12 // |
12 // |
13 // Description: |
13 // Description: |
|
14 // apfrec.cpp |
14 // |
15 // |
15 |
16 |
16 #include <apfrec.h> |
17 #include <apfrec.h> |
17 #include <e32uid.h> |
18 #include <e32uid.h> |
18 #include <f32file.h> |
19 #include <f32file.h> |
19 #include <apadef.h> |
20 #include <apadef.h> |
20 #include "APFSTD.H" // Panics etc. |
21 #include <ecom/ecom.h> |
21 |
22 |
22 #ifdef USING_ECOM_RECOGS |
|
23 #include <ecom/ecom.h> |
|
24 #endif |
|
25 |
|
26 // |
|
27 // class TRecognizer |
|
28 // |
|
29 |
|
30 CApaScanningFileRecognizer::TRecognizer::TRecognizer(HBufC* aName) |
|
31 : iName(aName) |
|
32 { |
|
33 } |
|
34 |
|
35 /////////////////////////////// |
|
36 // CApaRecognizerDll |
|
37 /////////////////////////////// |
|
38 |
|
39 class CApaRecognizerDll : public CBase |
|
40 { |
|
41 public: |
|
42 CApaRecognizerDll(HBufC* aName); |
|
43 ~CApaRecognizerDll(); // closes lib and deletes next |
|
44 |
|
45 enum TPresence { ENotPresent, EIsPresent, ESuperseded }; |
|
46 inline TPresence Present() const { return iPresent; } |
|
47 inline void Present(TPresence aPresence) { iPresent = aPresence; } |
|
48 |
|
49 inline CApaFileRecognizerType* Recognizer() { return iRec; } |
|
50 inline void SetRecognizer(CApaFileRecognizerType& aRec) { iRec = &aRec; } |
|
51 public: |
|
52 //lint --e{1925} Suppress "public data member" - bad OO but handy |
|
53 CApaRecognizerDll* iNext; |
|
54 CApaScanningFileRecognizer::TRecognizer iId; // cached so that this data can be accessed from other threads (RLibrary cant be used in this case) |
|
55 private: |
|
56 RLibrary iLibrary; |
|
57 TPresence iPresent; |
|
58 CApaFileRecognizerType* iRec; // not ownership |
|
59 }; |
|
60 |
|
61 |
|
62 CApaRecognizerDll::~CApaRecognizerDll() |
|
63 { |
|
64 delete iId.iName; |
|
65 iLibrary.Close(); |
|
66 delete iNext; |
|
67 iRec = NULL; |
|
68 } |
|
69 |
|
70 CApaRecognizerDll::CApaRecognizerDll(HBufC* aName) |
|
71 : iId(aName) |
|
72 { |
|
73 } |
|
74 |
|
75 #ifdef USING_ECOM_RECOGS |
|
76 // CleanupEComArray function is used for cleanup support |
23 // CleanupEComArray function is used for cleanup support |
77 // of locally declared arrays. |
24 // of locally declared arrays. |
78 void CleanUpEComInfoArray(TAny* aInfoArray) |
25 void CleanUpEComInfoArray(TAny* aInfoArray) |
79 { |
26 { |
80 RImplInfoPtrArray* infoArray = (static_cast<RImplInfoPtrArray*>(aInfoArray)); |
27 RImplInfoPtrArray* infoArray = (static_cast<RImplInfoPtrArray*>(aInfoArray)); |
81 infoArray->ResetAndDestroy(); |
28 infoArray->ResetAndDestroy(); |
82 infoArray->Close(); |
29 infoArray->Close(); |
83 } |
30 } |
84 #endif |
|
85 |
31 |
86 // class CApaBackupOperationObserver |
|
87 CApaScanningFileRecognizer::CApaBackupOperationObserver::CApaBackupOperationObserver(CApaScanningFileRecognizer& aRecognizer) |
|
88 : iRecognizer(aRecognizer) |
|
89 { |
|
90 TRAP_IGNORE(iSession = CBaBackupSessionWrapper::NewL()); |
|
91 } |
|
92 |
|
93 CApaScanningFileRecognizer::CApaBackupOperationObserver::~CApaBackupOperationObserver() |
|
94 { |
|
95 if (iSession) |
|
96 { |
|
97 iSession->DeRegisterBackupOperationObserver(*this); |
|
98 delete iSession; |
|
99 } |
|
100 } |
|
101 |
|
102 void CApaScanningFileRecognizer::CApaBackupOperationObserver::RegisterObserverL() |
|
103 { |
|
104 if (iSession) |
|
105 { |
|
106 iSession->RegisterBackupOperationObserverL(*this); |
|
107 } |
|
108 } |
|
109 |
|
110 void CApaScanningFileRecognizer::CApaBackupOperationObserver::HandleBackupOperationEventL(const TBackupOperationAttributes& aBackupOperationAttributes) |
|
111 { |
|
112 if ((aBackupOperationAttributes.iFileFlag == MBackupObserver::EReleaseLockReadOnly || |
|
113 aBackupOperationAttributes.iFileFlag == MBackupObserver::EReleaseLockNoAccess) |
|
114 && aBackupOperationAttributes.iOperation == EStart) |
|
115 { |
|
116 // release recognizers not in rom |
|
117 CApaRecognizerDll* rec=iRecognizer.iRecognizerLib; |
|
118 while (rec) |
|
119 { |
|
120 CApaRecognizerDll* next=rec->iNext; |
|
121 TDriveUnit drive(rec->iId.iDrive); |
|
122 const TChar driveName=drive.Name()[0]; |
|
123 if(driveName != TChar('Z')) |
|
124 { |
|
125 iRecognizer.RemoveRecognizer(*rec);// ignore errors |
|
126 } |
|
127 rec = next; |
|
128 } |
|
129 } |
|
130 |
|
131 if (aBackupOperationAttributes.iFileFlag == MBackupObserver::ETakeLock && |
|
132 (aBackupOperationAttributes.iOperation == EEnd || |
|
133 aBackupOperationAttributes.iOperation == EAbort)) |
|
134 { |
|
135 iRecognizer.ScanForRecognizersL(); |
|
136 } |
|
137 } |
|
138 |
|
139 TInt CApaScanningFileRecognizer::CApaBackupOperationObserver::UpdateCounter() const |
|
140 { |
|
141 return iUpdateCounter; |
|
142 } |
|
143 |
|
144 void CApaScanningFileRecognizer::CApaBackupOperationObserver::SetUpdateCounter(TInt aValue) |
|
145 { |
|
146 iUpdateCounter = aValue; |
|
147 } |
|
148 |
|
149 |
|
150 /////////////////////////////// |
|
151 // CApaScanningFileRecognizer |
|
152 /////////////////////////////// |
|
153 |
|
154 |
|
155 EXPORT_C CApaScanningFileRecognizer* CApaScanningFileRecognizer::NewL(RFs& aFs,MApaAppStarter* aAppStarter) |
|
156 { |
|
157 CApaScanningFileRecognizer* self=new(ELeave) CApaScanningFileRecognizer(aFs,aAppStarter); |
|
158 CleanupStack::PushL(self); |
|
159 self->ConstructL(); |
|
160 self->ScanForRecognizersL(); |
|
161 CleanupStack::Pop(); // self |
|
162 return self; |
|
163 } |
|
164 |
|
165 |
|
166 EXPORT_C CApaScanningFileRecognizer::CApaScanningFileRecognizer(RFs& aFs,MApaAppStarter* aAppStarter) |
|
167 : CApaFileRecognizer(aFs), iAppStarter(aAppStarter) |
|
168 {} |
|
169 |
|
170 |
|
171 EXPORT_C void CApaScanningFileRecognizer::ConstructL() |
|
172 { |
|
173 iObserver = new (ELeave) CApaBackupOperationObserver(*this); |
|
174 iObserver->RegisterObserverL(); |
|
175 } |
|
176 |
|
177 |
|
178 EXPORT_C CApaScanningFileRecognizer::~CApaScanningFileRecognizer() |
|
179 { |
|
180 DestroyRecognizerList(); |
|
181 delete iRecognizerLib; |
|
182 delete iObserver; |
|
183 iAppStarter = NULL; |
|
184 } |
|
185 |
|
186 EXPORT_C void CApaScanningFileRecognizer::ScanForRecognizersL() |
|
187 { |
|
188 // set all recognizers to not present - pending rediscoivery |
|
189 CApaRecognizerDll* rec=iRecognizerLib; |
|
190 while (rec) |
|
191 { |
|
192 rec->Present(CApaRecognizerDll::ENotPresent); |
|
193 rec = rec->iNext; |
|
194 } |
|
195 |
|
196 ScanForEcomRecognizersL(); |
|
197 // |
|
198 // remove any recognizers that are no longer present |
|
199 rec=iRecognizerLib; |
|
200 while (rec) |
|
201 { |
|
202 CApaRecognizerDll* next=rec->iNext; |
|
203 if (rec->Present()==CApaRecognizerDll::ENotPresent) |
|
204 RemoveRecognizer(*rec); |
|
205 rec = next; |
|
206 } |
|
207 } |
|
208 |
|
209 |
|
210 TInt CApaScanningFileRecognizer::RemoveRecognizer(CApaRecognizerDll& aDll) |
|
211 { |
|
212 // try to remove the recognizer from the list |
|
213 TInt ret=RemoveFileRecognizerType(aDll.Recognizer()); |
|
214 if (ret!=KErrNone && ret!=KErrNotFound) |
|
215 return ret; |
|
216 // |
|
217 // get a handle to the previous entry in the list |
|
218 CApaRecognizerDll* prev=NULL; |
|
219 CApaRecognizerDll* dll=iRecognizerLib; |
|
220 while (dll && (dll != &aDll)) |
|
221 { |
|
222 prev = dll; |
|
223 dll = dll->iNext; |
|
224 } |
|
225 |
|
226 if (!dll) |
|
227 { |
|
228 Panic(EPanicLibraryNotInList); |
|
229 } |
|
230 else |
|
231 { |
|
232 // remove the dll |
|
233 if (prev) |
|
234 prev->iNext = dll->iNext; |
|
235 else |
|
236 iRecognizerLib = dll->iNext; |
|
237 dll->iNext = NULL; |
|
238 delete dll; |
|
239 iObserver->SetUpdateCounter( iObserver->UpdateCounter()+ 1 ); |
|
240 } |
|
241 return KErrNone; |
|
242 } |
|
243 |
|
244 EXPORT_C TInt CApaScanningFileRecognizer::RecognizerCount() |
|
245 { |
|
246 CApaRecognizerDll* lib=iRecognizerLib; |
|
247 TInt count=0; |
|
248 while (lib) |
|
249 { |
|
250 count++; |
|
251 lib = lib->iNext; |
|
252 } |
|
253 return count; |
|
254 } |
|
255 |
|
256 |
|
257 EXPORT_C CApaScanningFileRecognizer::CRecognizerArray* CApaScanningFileRecognizer::RecognizerListLC()const |
|
258 { |
|
259 CRecognizerArray* list=new(ELeave) CArrayFixFlat<TRecognizer>(1); |
|
260 CleanupStack::PushL(list); |
|
261 CApaRecognizerDll* dll=iRecognizerLib; |
|
262 while (dll) |
|
263 { |
|
264 list->AppendL(dll->iId); |
|
265 dll = dll->iNext; |
|
266 } |
|
267 return list; |
|
268 } |
|
269 |
|
270 EXPORT_C const CApaScanningFileRecognizer::TRecognizer& CApaScanningFileRecognizer::operator[](TInt aIndex)const |
|
271 { |
|
272 __ASSERT_ALWAYS(aIndex>=0,Panic(EPanicNegativeIndex)); |
|
273 // scan to correct dll |
|
274 TInt num=0; |
|
275 CApaRecognizerDll* dll=iRecognizerLib; |
|
276 while (dll && num++<aIndex) |
|
277 dll = dll->iNext; |
|
278 __ASSERT_ALWAYS(dll,Panic(EPanicIndexOutOfRange)); |
|
279 // |
|
280 // get info from dll |
|
281 return dll->iId; //lint !e613 Suppress possible use of null pointer - asserted above |
|
282 } |
|
283 |
|
284 EXPORT_C TInt CApaScanningFileRecognizer::UpdateCounter()const |
|
285 { |
|
286 return iObserver->UpdateCounter(); |
|
287 } |
|
288 |
|
289 EXPORT_C void CApaScanningFileRecognizer::SetEcomRecognizersFromListL(const CRecognizerArray& aList) |
|
290 { |
|
291 //for ecom style |
|
292 for (TInt i=0 ; i<aList.Count() ; i++) |
|
293 { |
|
294 TDriveUnit drive(aList[i].iDrive); |
|
295 TRAPD(ret, LoadEcomRecognizerL(drive,aList[i].iUid)); |
|
296 if (ret==KErrNoMemory) |
|
297 { |
|
298 User::Leave(ret); |
|
299 } |
|
300 } |
|
301 } |
|
302 |
|
303 EXPORT_C void CApaScanningFileRecognizer::SetEcomRecognizerL(const TRecognizer& aRecognizer) |
|
304 { |
|
305 //for ecom style |
|
306 TDriveUnit drive(aRecognizer.iDrive); |
|
307 LoadEcomRecognizerL(drive,aRecognizer.iUid); |
|
308 } |
|
309 |
|
310 //to find the name of ecom style file recognizer corresponding to aImplUID |
|
311 #ifdef USING_ECOM_RECOGS |
|
312 HBufC* RecognizerNameL(TUid aImplUID) |
|
313 { |
|
314 RImplInfoPtrArray implementationInfoArray; |
|
315 TCleanupItem cleanup(CleanUpEComInfoArray, &implementationInfoArray); |
|
316 CleanupStack::PushL(cleanup); |
|
317 REComSession::ListImplementationsL(KUidFileRecogInterfaceUid,implementationInfoArray); |
|
318 const TInt availCount = implementationInfoArray.Count(); |
|
319 for (TInt ii=0;ii<availCount;++ii) |
|
320 { |
|
321 CImplementationInformation* info = implementationInfoArray[ii]; |
|
322 TUid uid = info->ImplementationUid(); |
|
323 if(uid.iUid == aImplUID.iUid ) |
|
324 { |
|
325 HBufC* recogName = info->DisplayName().AllocL(); |
|
326 CleanupStack::PopAndDestroy(&implementationInfoArray); |
|
327 return recogName; |
|
328 } |
|
329 } |
|
330 CleanupStack::PopAndDestroy(&implementationInfoArray); |
|
331 return KNullDesC().AllocL(); |
|
332 } |
|
333 |
|
334 //load the ecom file recognizer |
|
335 void CApaScanningFileRecognizer::LoadEcomRecognizerL(TDriveUnit& aDrive,TUid aUid) |
|
336 { |
|
337 // check we haven't loaded this one already |
|
338 CApaRecognizerDll* lib=iRecognizerLib; |
|
339 CApaRecognizerDll* last=NULL; // the previous one in the list |
|
340 while (lib) |
|
341 { |
|
342 if (lib->iId.iUid==aUid) |
|
343 {// we may have to override one |
|
344 if (lib->Present()!=CApaRecognizerDll::ENotPresent) |
|
345 { |
|
346 return; // already found |
|
347 } |
|
348 else |
|
349 { |
|
350 lib->Present(CApaRecognizerDll::EIsPresent); |
|
351 return; |
|
352 } |
|
353 } |
|
354 last = lib; |
|
355 lib = lib->iNext; |
|
356 } |
|
357 // load the library |
|
358 HBufC* recogName=RecognizerNameL(aUid); |
|
359 CleanupStack::PushL(recogName); |
|
360 lib = new(ELeave) CApaRecognizerDll(recogName); |
|
361 CleanupStack::Pop(recogName); |
|
362 CleanupStack::PushL(lib); |
|
363 TRAPD(error,lib->SetRecognizer(*CApaFileRecognizerType::CreateFileRecognizerL(aUid))); |
|
364 if(!error && lib->Recognizer()) |
|
365 { |
|
366 lib->Present(CApaRecognizerDll::EIsPresent); |
|
367 SetAppStarter(lib->Recognizer(),iAppStarter); |
|
368 AddFileRecognizerType(lib->Recognizer()); |
|
369 lib->iId.iUid = aUid; |
|
370 lib->iId.iDrive = aDrive; |
|
371 iObserver->SetUpdateCounter( iObserver->UpdateCounter() + 1 ); |
|
372 // |
|
373 // add lib to the library list |
|
374 if (last) |
|
375 { |
|
376 while (last->iNext) |
|
377 { |
|
378 last=last->iNext; |
|
379 } |
|
380 __ASSERT_ALWAYS(last->iNext==NULL,Panic(EPanicIndexOutOfRange)); |
|
381 last->iNext = lib; |
|
382 } |
|
383 else |
|
384 { |
|
385 iRecognizerLib = lib; |
|
386 } |
|
387 CleanupStack::Pop(lib); // lib |
|
388 } |
|
389 else |
|
390 { |
|
391 CleanupStack::PopAndDestroy(lib); // lib |
|
392 } |
|
393 } |
|
394 #else |
|
395 void CApaScanningFileRecognizer::LoadEcomRecognizerL(TDriveUnit&,TUid) |
|
396 { |
|
397 } //lint !e1762 Suppress member function could be made const |
|
398 |
|
399 #endif |
|
400 |
|
401 void CApaScanningFileRecognizer::ScanForEcomRecognizersL() |
|
402 { |
|
403 #ifdef USING_ECOM_RECOGS |
|
404 //scan ecom plugin |
|
405 RImplInfoPtrArray implementationArray; |
|
406 TCleanupItem cleanup(CleanUpEComInfoArray, &implementationArray); |
|
407 CleanupStack::PushL(cleanup); |
|
408 REComSession::ListImplementationsL(KUidFileRecogInterfaceUid,implementationArray); |
|
409 const TInt availCount = implementationArray.Count(); |
|
410 for (TInt count=0;count<availCount;++count) |
|
411 { |
|
412 const CImplementationInformation* info = implementationArray[count]; |
|
413 TUid uid = info->ImplementationUid(); |
|
414 TDriveUnit drive=info-> Drive(); |
|
415 TRAP_IGNORE(LoadEcomRecognizerL(drive,uid)); |
|
416 } |
|
417 CleanupStack::PopAndDestroy(&implementationArray); |
|
418 #endif |
|
419 } //lint !e1762 Suppress member function could be made const |
|