|
1 // Copyright (c) 1999-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 "MSVIDS.H" |
|
17 #include "MSVPANIC.H" |
|
18 #include "MSVUIDS.H" |
|
19 #include "MTCLREG.H" |
|
20 #include "MSVFIND.H" |
|
21 #include "msvchild.h" |
|
22 |
|
23 const TInt KMessageFindResultGranularity = 8; |
|
24 const TInt KMtmArrayGranularity = 4; |
|
25 |
|
26 //********************************** |
|
27 // CMsvChildMessages |
|
28 //********************************** |
|
29 |
|
30 CMsvChildMessages* CMsvChildMessages::NewL(CMsvSession& aSession, TInt aPriority) |
|
31 { |
|
32 CMsvChildMessages* self = new(ELeave)CMsvChildMessages(aSession, aPriority); |
|
33 CleanupStack::PushL(self); |
|
34 self->ConstructL(); |
|
35 CleanupStack::Pop(); // self |
|
36 return self; |
|
37 } |
|
38 |
|
39 CMsvChildMessages::CMsvChildMessages(CMsvSession& aSession, TInt aPriority) |
|
40 : CMsgActive(aPriority), iSession(aSession) |
|
41 { |
|
42 } |
|
43 |
|
44 CMsvChildMessages::~CMsvChildMessages() |
|
45 { |
|
46 Cancel(); |
|
47 delete iFolders; |
|
48 delete iSelection; |
|
49 delete iFilter; |
|
50 } |
|
51 |
|
52 void CMsvChildMessages::ConstructL() |
|
53 { |
|
54 iFolders = new(ELeave)CMsvEntrySelection; |
|
55 iSelection = new(ELeave)CMsvEntrySelection; |
|
56 iFilter = CMsvEntryFilter::NewL(); |
|
57 |
|
58 CActiveScheduler::Add(this); |
|
59 } |
|
60 |
|
61 void CMsvChildMessages::StartL(TMsvId aParentId, CMsvEntrySelection& aChildren, TRequestStatus& aStatus) |
|
62 { |
|
63 __ASSERT_DEBUG(!aChildren.Count(), PanicServer(EMsvSelectionNotEmpty)); |
|
64 |
|
65 // Check we were given the root, a folder, or a service |
|
66 TMsvId service; |
|
67 TMsvEntry entry; |
|
68 User::LeaveIfError(iSession.GetEntry(aParentId, service, entry)); |
|
69 if (entry.iType != KUidMsvRootEntry && entry.iType != KUidMsvServiceEntry && entry.iType != KUidMsvFolderEntry) |
|
70 User::Leave(KErrArgument); |
|
71 |
|
72 iChildren = &aChildren; |
|
73 iFolders->Reset(); |
|
74 iFolders->AppendL(aParentId); |
|
75 |
|
76 Queue(aStatus); |
|
77 TRequestStatus* status=&iStatus; |
|
78 User::RequestComplete(status, KErrNone); |
|
79 SetActive(); |
|
80 } |
|
81 |
|
82 void CMsvChildMessages::DoRunL() |
|
83 { |
|
84 // Get folder to process |
|
85 TInt id = iFolders->At(0); |
|
86 iFolders->Delete(0); |
|
87 |
|
88 // Get services |
|
89 iFilter->SetType(KUidMsvServiceEntry); |
|
90 iSession.GetChildIdsL(id, *iFilter, *iSelection); |
|
91 if (iSelection->Count()) |
|
92 { |
|
93 iFolders->InsertL(0, iSelection->Back(0), iSelection->Count()); |
|
94 iSelection->Reset(); |
|
95 } |
|
96 |
|
97 // Get folders |
|
98 iFilter->SetType(KUidMsvFolderEntry); |
|
99 iSession.GetChildIdsL(id, *iFilter, *iSelection); |
|
100 if (iSelection->Count()) |
|
101 { |
|
102 iFolders->InsertL(0, iSelection->Back(0), iSelection->Count()); |
|
103 iSelection->Reset(); |
|
104 } |
|
105 |
|
106 // Get messages |
|
107 iFilter->SetType(KUidMsvMessageEntry); |
|
108 iSession.GetChildIdsL(id, *iFilter, *iSelection); |
|
109 if (iSelection->Count()) |
|
110 { |
|
111 iChildren->InsertL(0, iSelection->Back(0), iSelection->Count()); |
|
112 iSelection->Reset(); |
|
113 } |
|
114 |
|
115 // Is there anything more to process? |
|
116 if (iFolders->Count()) |
|
117 { |
|
118 TRequestStatus* status=&iStatus; |
|
119 User::RequestComplete(status,KErrNone); |
|
120 SetActive(); |
|
121 } |
|
122 } |
|
123 |
|
124 //********************************** |
|
125 // TMsvFindResult |
|
126 //********************************** |
|
127 |
|
128 /** Default constructor. */ |
|
129 EXPORT_C TMsvFindResult::TMsvFindResult() : |
|
130 iPartList(0) |
|
131 { |
|
132 } |
|
133 |
|
134 /** Constructor specifying the results of a search. |
|
135 |
|
136 @param aPartList The parts of the message that contain the search text |
|
137 @param aId The entry Id of the message that contains the search text |
|
138 */ |
|
139 EXPORT_C TMsvFindResult::TMsvFindResult(TMsvPartList aPartList, TMsvId aId) |
|
140 : iPartList(aPartList), iId(aId) |
|
141 { |
|
142 } |
|
143 |
|
144 //********************************** |
|
145 // CMsvFindResultSelection |
|
146 //********************************** |
|
147 |
|
148 EXPORT_C CMsvFindResultSelection::CMsvFindResultSelection() |
|
149 : CArrayFixFlat<TMsvFindResult>(KMessageFindResultGranularity) |
|
150 /** Constructs an empty results collection. */ |
|
151 { |
|
152 } |
|
153 |
|
154 EXPORT_C CMsvFindResultSelection* CMsvFindResultSelection::CopyL() const |
|
155 // |
|
156 // Create a new instance of the result selection that is an exact copy |
|
157 // |
|
158 /** Creates a copy of the results of the text search operation. |
|
159 |
|
160 The function leaves if memory cannot be allocated for the new object. |
|
161 |
|
162 @return Pointer to the new copy of the results of the text search operation. */ |
|
163 { |
|
164 CMsvFindResultSelection* newSelection = CopyLC(); |
|
165 CleanupStack::Pop(); |
|
166 return newSelection; |
|
167 } |
|
168 |
|
169 EXPORT_C CMsvFindResultSelection* CMsvFindResultSelection::CopyLC() const |
|
170 // |
|
171 // Create a new instance of the result selection that is an exact copy |
|
172 // and push this onto the cleanup stack |
|
173 // |
|
174 /** Creates a copy of the results of the text search operation and puts a pointer |
|
175 to the new object onto the cleanup stack. |
|
176 |
|
177 The function leaves if memory cannot be allocated for the new object. |
|
178 |
|
179 @return Pointer to the new copy of the results of the text search operation. */ |
|
180 { |
|
181 CMsvFindResultSelection* newSelection = new(ELeave)CMsvFindResultSelection; |
|
182 CleanupStack::PushL(newSelection); |
|
183 if (Count()) |
|
184 { |
|
185 newSelection->ResizeL(Count()); |
|
186 Mem::Copy(newSelection->Back(0), Back(0), Count() * sizeof(TMsvFindResult)); |
|
187 } |
|
188 return newSelection; |
|
189 } |
|
190 |
|
191 EXPORT_C TInt CMsvFindResultSelection::Find(TMsvId aId) const |
|
192 // |
|
193 // Find the position of a result belonging to an particular entry |
|
194 // Returns KErrNotFound if there is no such result |
|
195 // |
|
196 /** Returns the index of the entry corresponding to the specified entry Id. |
|
197 |
|
198 @param aId The entry Id. |
|
199 @return The index of the entry within the search results which has the specified |
|
200 entry Id. KErrNotFound, if there is no matching entry. */ |
|
201 { |
|
202 TInt count = Count(); |
|
203 if (!count) |
|
204 return KErrNotFound; |
|
205 |
|
206 const TMsvFindResult* ptr = End(0); |
|
207 while (count--) |
|
208 if ((--ptr)->iId == aId) |
|
209 break; |
|
210 |
|
211 return count; |
|
212 } |
|
213 |
|
214 //********************************** |
|
215 // TMsvFindOperationProgress |
|
216 //********************************** |
|
217 |
|
218 /** Default constructor. |
|
219 |
|
220 The data members are set to 0 or NULL values. */ |
|
221 EXPORT_C TMsvFindOperationProgress::TMsvFindOperationProgress() |
|
222 : iError(KErrNone), iCompleted(0), iRemaining(0), iCurrentId(KMsvNullIndexEntryId) |
|
223 { |
|
224 } |
|
225 |
|
226 //********************************** |
|
227 // CMsvFindOperation |
|
228 //********************************** |
|
229 |
|
230 EXPORT_C CMsvFindOperation* CMsvFindOperation::FindInChildrenL(CMsvSession& aSession, const TDesC& aTextToFind, TMsvId aParentId, TMsvPartList aPartList, TRequestStatus& aObserverRequestStatus) |
|
231 /** Creates a new search operation to search for text within a specified root, |
|
232 folder or service. |
|
233 |
|
234 Searching for messages is done recursively through all child services and |
|
235 folders. All messages found are searched for text. |
|
236 |
|
237 @param aSession An open session with the Message Server. |
|
238 @param aTextToFind The text to be found. The length of the text to be found |
|
239 must not be greater than KMsvMaxFindTextLength, otherwise the function raises |
|
240 a MSGS 306 panic. |
|
241 @param aParentId The entry Id of the root, a folder or a service. If this is |
|
242 the Id of the root entry, then all messages stored in the message store on |
|
243 the Symbian OS phone are searched. The function leaves with the KErrArgument |
|
244 code if this is neither the entry Id of the root, nor a folder nor a service. |
|
245 @param aPartList The parts of the messages which are to be searched. |
|
246 @param aObserverRequestStatus The request status object. This is set when the |
|
247 search operation is complete. The is set to: KErrNone, if the search operation |
|
248 completes successfully. |
|
249 @return Pointer to the new search operation object. */ |
|
250 { |
|
251 CMsvFindOperation* self = new(ELeave) CMsvFindOperation(aSession, aTextToFind, aPartList, aObserverRequestStatus); |
|
252 CleanupStack::PushL(self); |
|
253 self->ConstructFindInChildrenL(aParentId); |
|
254 CleanupStack::Pop(); // self |
|
255 return self; |
|
256 } |
|
257 |
|
258 EXPORT_C CMsvFindOperation* CMsvFindOperation::FindInSelectionL(CMsvSession& aSession, const TDesC& aTextToFind, const CMsvEntrySelection& aSelection, TMsvPartList aPartList, TRequestStatus& aObserverRequestStatus) |
|
259 /** Creates a new search operation to search for text within a specified selection |
|
260 of messages. |
|
261 |
|
262 The function leaves with the KErrArgument code if the first entry in the selection |
|
263 is not recognised as a message. |
|
264 |
|
265 @param aSession An open session with the Message Server. |
|
266 @param aTextToFind The text to be found. The length of the text to be found |
|
267 must not be greater than KMsvMaxFindTextLength, otherwise the function raises |
|
268 a MSGS 306 panic. |
|
269 @param aSelection A selection of messages to search. The function raises a |
|
270 MSGS 258 panic if the selection is empty (i.e. there are zero messages in |
|
271 the selection). |
|
272 @param aPartList The parts of the messages which are to be searched. |
|
273 @param aObserverRequestStatus The request status object. This is set when the |
|
274 search operation is complete. The is set to: KErrNone, if the search operation |
|
275 completes successfully. KErrArgument, if any of the entries in the selection, |
|
276 apart from the first, cannot be recognised as a message. |
|
277 @return Pointer to the new search operation object. */ |
|
278 { |
|
279 CMsvFindOperation* self = new(ELeave) CMsvFindOperation(aSession, aTextToFind, aPartList, aObserverRequestStatus); |
|
280 CleanupStack::PushL(self); |
|
281 self->ConstructFindInSelectionL(aSelection); |
|
282 CleanupStack::Pop(); // self |
|
283 return self; |
|
284 } |
|
285 |
|
286 /** Constructor, specifying search parameters. |
|
287 |
|
288 @param aSession An open session with the Message Server |
|
289 @param aTextToFind The text to be found. The length of the text to be found |
|
290 must not be greater than KMsvMaxFindTextLength. |
|
291 @param aPartList The parts of the messages which are to be searched. |
|
292 @param aObserverRequestStatus Request status of an observer to signal when the search is |
|
293 complete. |
|
294 @panic MSGS 306 aTextToFind is longer than KMsvMaxFindTextLength (debug builds only). In release |
|
295 builds, a USER 11 panic will occur. |
|
296 */ |
|
297 EXPORT_C CMsvFindOperation::CMsvFindOperation(CMsvSession& aSession, const TDesC& aTextToFind, TMsvPartList aPartList, TRequestStatus& aObserverRequestStatus) |
|
298 : CMsvOperation(aSession, EPriorityStandard, aObserverRequestStatus), iPartList(aPartList) |
|
299 { |
|
300 __ASSERT_DEBUG(iTextToFind.Length() <= KMsvMaxFindTextLength, PanicServer(EMsvTooMuchFindTextSpecified)); |
|
301 iTextToFind.Copy(aTextToFind); |
|
302 } |
|
303 |
|
304 void CMsvFindOperation::ConstructL() |
|
305 { |
|
306 iSelection = new(ELeave)CMsvEntrySelection; |
|
307 iClientRegistry = CClientMtmRegistry::NewL(iMsvSession); |
|
308 iMtmArray = new(ELeave)CArrayPtrFlat<CBaseMtm>(KMtmArrayGranularity); |
|
309 iFindResultSel = new(ELeave)CMsvFindResultSelection; |
|
310 |
|
311 CActiveScheduler::Add(this); |
|
312 } |
|
313 |
|
314 /** Second phase constructor, for constructing a search of an entry |
|
315 and of all its children. |
|
316 |
|
317 @param aId The entry Id of the root, folder or service to search |
|
318 */ |
|
319 EXPORT_C void CMsvFindOperation::ConstructFindInChildrenL(TMsvId aId) |
|
320 { |
|
321 ConstructL(); |
|
322 iCurrentId = aId; |
|
323 iState = EMsvExpandingFolders; |
|
324 StartL(); |
|
325 } |
|
326 |
|
327 /** Second phase constructor, for constructing a search of a specified |
|
328 selection of messages. |
|
329 |
|
330 @param aSelection A selection of messages to search. |
|
331 @panic MSGS 258 The selection is empty |
|
332 */ |
|
333 EXPORT_C void CMsvFindOperation::ConstructFindInSelectionL(const CMsvEntrySelection& aSelection) |
|
334 { |
|
335 __ASSERT_ALWAYS(aSelection.Count(), PanicServer(EMsvEmptySelection)); |
|
336 |
|
337 ConstructL(); |
|
338 iSelection->AppendL(aSelection.Back(0), aSelection.Count()); |
|
339 StartL(); |
|
340 } |
|
341 |
|
342 /** Destructor. */ |
|
343 EXPORT_C CMsvFindOperation::~CMsvFindOperation() |
|
344 { |
|
345 Cancel(); |
|
346 |
|
347 delete iSelection; |
|
348 delete iFindResultSel; |
|
349 delete iClientRegistry; |
|
350 delete iChildMessages; |
|
351 |
|
352 // We've probably already deleted this array |
|
353 if (iMtmArray) |
|
354 { |
|
355 iMtmArray->ResetAndDestroy(); |
|
356 delete iMtmArray; |
|
357 } |
|
358 } |
|
359 |
|
360 EXPORT_C const TDesC8& CMsvFindOperation::ProgressL() |
|
361 /** Returns progress information. |
|
362 |
|
363 Progress information supplies sufficient data to drive a progress gauge for |
|
364 the text search operation and also identifies the message currently being |
|
365 searched. It does not provide any information about the search progress within |
|
366 a message. |
|
367 |
|
368 @return A reference to a descriptor holding progress information. Progress |
|
369 information is encapsulated by a TMsvFindOperationProgress type object and |
|
370 this is returned as a package buffer, a TPckgBuf<TMsvFindOperationProgress> |
|
371 object. */ |
|
372 { |
|
373 return iProgress; |
|
374 } |
|
375 |
|
376 EXPORT_C const TDesC8& CMsvFindOperation::FinalProgress() |
|
377 /** Returns progress information after the search operation is complete. |
|
378 |
|
379 The function returns the same information as ProgressL() but can only be called |
|
380 after the search operation is complete. The function raises a MSGS 285 panic |
|
381 if it is called while the search operation is still in progress. |
|
382 |
|
383 @return A reference to a descriptor holding progress information. Progress |
|
384 information is encapsulated by a TMsvFindOperationProgress type object and |
|
385 this is returned as a package buffer, a TPckgBuf<TMsvFindOperationProgress> |
|
386 object */ |
|
387 { |
|
388 __ASSERT_ALWAYS(!IsActive(), PanicServer(EMsvActiveInFinalProgress)); |
|
389 return iProgress; |
|
390 } |
|
391 |
|
392 EXPORT_C void CMsvFindOperation::DoCancel() |
|
393 { |
|
394 if(iChildMessages!=NULL) |
|
395 { |
|
396 iChildMessages->Cancel(); |
|
397 } |
|
398 Complete(KErrCancel); |
|
399 } |
|
400 |
|
401 void CMsvFindOperation::StartL() |
|
402 { |
|
403 if (iState == EMsvExpandingFolders) |
|
404 { |
|
405 // Get list of children |
|
406 iChildMessages = CMsvChildMessages::NewL(iMsvSession, EPriorityStandard); |
|
407 iChildMessages->StartL(iCurrentId, *iSelection, iStatus); |
|
408 } |
|
409 else |
|
410 { |
|
411 // Get the next entry to process |
|
412 iCurrentId = iSelection->At(0); |
|
413 iProgress().iCurrentId = iCurrentId; |
|
414 iSelection->Delete(0); |
|
415 |
|
416 TMsvId service; |
|
417 User::LeaveIfError(iMsvSession.GetEntry(iCurrentId, service, iEntry)); |
|
418 |
|
419 // Check the entry is a message |
|
420 if (iEntry.iType != KUidMsvMessageEntry) |
|
421 User::Leave(KErrArgument); |
|
422 |
|
423 TRequestStatus* status=&iStatus; |
|
424 User::RequestComplete(status,KErrNone); |
|
425 } |
|
426 |
|
427 iObserverRequestStatus = KRequestPending; |
|
428 SetActive(); |
|
429 } |
|
430 |
|
431 EXPORT_C void CMsvFindOperation::RunL() |
|
432 { |
|
433 TRAPD(error, DoRunL()); |
|
434 if (error != KErrNone) |
|
435 Complete(error); |
|
436 } |
|
437 |
|
438 void CMsvFindOperation::DoRunL() |
|
439 { |
|
440 if (iState == EMsvExpandingFolders) |
|
441 { |
|
442 if (iStatus != KErrNone) |
|
443 { |
|
444 Complete(iStatus.Int()); |
|
445 return; |
|
446 } |
|
447 } |
|
448 else |
|
449 { |
|
450 // Give derived class a chance to prevent searching in invalid entries |
|
451 if (IsValid(iEntry)) |
|
452 { |
|
453 // Trap any errors so that the find operation can continue |
|
454 // should we encounter a corrupt entry or an message where the body cannot be loaded |
|
455 TRAPD(error, FindL()); |
|
456 if (error != KErrCorrupt && error != KErrNotFound) |
|
457 User::LeaveIfError(error); |
|
458 } |
|
459 |
|
460 // Update progress |
|
461 iProgress().iCompleted++; |
|
462 } |
|
463 |
|
464 iState = EMsvFindingText; |
|
465 iProgress().iRemaining = iSelection->Count(); |
|
466 |
|
467 if (iSelection->Count()) |
|
468 StartL(); |
|
469 else |
|
470 Complete(KErrNone); |
|
471 } |
|
472 |
|
473 void CMsvFindOperation::FindL() |
|
474 { |
|
475 TMsvPartList parts = 0; |
|
476 |
|
477 // Note: Empty search string IS valid |
|
478 if (iPartList && iTextToFind.Length()) |
|
479 { |
|
480 // See if the mtm is in our array |
|
481 CBaseMtm* baseMtm = NULL; |
|
482 TInt count = iMtmArray->Count(); |
|
483 while(count--) |
|
484 { |
|
485 if (iMtmArray->At(count)->Type() == iEntry.iMtm) |
|
486 { |
|
487 baseMtm = iMtmArray->At(count); |
|
488 baseMtm->SwitchCurrentEntryL(iEntry.Id()); |
|
489 break; |
|
490 } |
|
491 } |
|
492 |
|
493 // Create a new mtm |
|
494 if (!baseMtm) |
|
495 { |
|
496 baseMtm = iClientRegistry->NewMtmL(iEntry.iMtm); |
|
497 CleanupStack::PushL(baseMtm); |
|
498 iMtmArray->AppendL(baseMtm); |
|
499 CleanupStack::Pop(); // baseMtm |
|
500 |
|
501 // Note: SetCurrentEntry takes ownership on entry |
|
502 CMsvEntry* entry = CMsvEntry::NewL(iMsvSession, iEntry.Id(), TMsvSelectionOrdering()); |
|
503 baseMtm->SetCurrentEntryL(entry); |
|
504 } |
|
505 |
|
506 TRAPD(error, baseMtm->LoadMessageL()); |
|
507 if (error == KErrNotSupported) // not all MTMs support LoadMessageL() |
|
508 return; |
|
509 User::LeaveIfError(error); |
|
510 |
|
511 // Perform the search |
|
512 parts = baseMtm->Find(iTextToFind, iPartList); |
|
513 if (parts == KMsvMessagePartNone) |
|
514 return; |
|
515 } |
|
516 |
|
517 TMsvFindResult result(parts, iEntry.Id()); |
|
518 iFindResultSel->AppendL(result); |
|
519 } |
|
520 |
|
521 void CMsvFindOperation::Complete(TInt aStatus) |
|
522 { |
|
523 iProgress().iError = aStatus; |
|
524 TRequestStatus* st = &iObserverRequestStatus; |
|
525 User::RequestComplete(st, aStatus); |
|
526 |
|
527 // Release the mtm's as soon as possible |
|
528 if (iMtmArray) |
|
529 { |
|
530 iMtmArray->ResetAndDestroy(); |
|
531 delete iMtmArray; |
|
532 iMtmArray = NULL; |
|
533 } |
|
534 } |
|
535 |
|
536 TBool CMsvFindOperation::IsValid(const TMsvEntry&/* aEntry*/) const |
|
537 // |
|
538 // This can be overridden to prevent searching in certain messages |
|
539 // Allows you to find by date, size, mtm etc. |
|
540 // |
|
541 /** Determines whether a message is to be included in the text search operation. |
|
542 |
|
543 The function acts as a filter that decides whether a message should be included |
|
544 in the text search operation. The function is called before the message is |
|
545 searched. |
|
546 |
|
547 The default implementation always returns true. |
|
548 |
|
549 Clients can provide their own implementation. |
|
550 |
|
551 Note: |
|
552 Messages which are not included in the search are still included in the count |
|
553 of entries completed. |
|
554 |
|
555 @param aEntry The entry Id for a message. |
|
556 @return True, if the message is to be searched. False if the message is not |
|
557 to be searched. */ |
|
558 { |
|
559 return ETrue; |
|
560 } |