|
1 /* |
|
2 * Copyright (c) 2007 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: Operation that searches string from email. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #include "emailtrace.h" |
|
21 #include "ipsplgheaders.h" |
|
22 |
|
23 /** Snippet length, for HTML tags. */ |
|
24 const TInt KIpsPlgSnippetLen = 300; |
|
25 // Defines the number of mails handled in one scheduler round |
|
26 const TInt KIpsPlgSearchMailsInRound = 10; |
|
27 |
|
28 // ======== CONSTRUCTORS & DESTRUCTOR ======== |
|
29 |
|
30 // --------------------------------------------------------------------------- |
|
31 // CIpsPlgSearchOp::NewL() |
|
32 // --------------------------------------------------------------------------- |
|
33 // |
|
34 CIpsPlgSearchOp* CIpsPlgSearchOp::NewL( |
|
35 CIpsPlgSearch& aObserver ) |
|
36 { |
|
37 FUNC_LOG; |
|
38 CIpsPlgSearchOp* self = CIpsPlgSearchOp::NewLC( aObserver ); |
|
39 CleanupStack::Pop( self ); |
|
40 return self; |
|
41 } |
|
42 |
|
43 // --------------------------------------------------------------------------- |
|
44 // CIpsPlgSearchOp::NewLC() |
|
45 // --------------------------------------------------------------------------- |
|
46 // |
|
47 CIpsPlgSearchOp* CIpsPlgSearchOp::NewLC( |
|
48 CIpsPlgSearch& aObserver ) |
|
49 { |
|
50 FUNC_LOG; |
|
51 CIpsPlgSearchOp* self = new( ELeave ) CIpsPlgSearchOp( aObserver ); |
|
52 CleanupStack::PushL( self ); |
|
53 self->ConstructL(); |
|
54 return self; |
|
55 } |
|
56 |
|
57 // --------------------------------------------------------------------------- |
|
58 // CIpsPlgSearchOp::~CIpsPlgSearchOp() |
|
59 // --------------------------------------------------------------------------- |
|
60 // |
|
61 CIpsPlgSearchOp::~CIpsPlgSearchOp() |
|
62 { |
|
63 FUNC_LOG; |
|
64 delete iMessage; |
|
65 iMessage = NULL; |
|
66 delete iHeader; |
|
67 iHeader = NULL; |
|
68 if ( iSearcher ) |
|
69 { |
|
70 iSearcher->Cleanup(); |
|
71 } |
|
72 delete iSearcher; |
|
73 iSearcher = NULL; |
|
74 } |
|
75 |
|
76 // --------------------------------------------------------------------------- |
|
77 // CIpsPlgSearchOp::CIpsPlgSearchOp() |
|
78 // --------------------------------------------------------------------------- |
|
79 // |
|
80 CIpsPlgSearchOp::CIpsPlgSearchOp( |
|
81 CIpsPlgSearch& aObserver ) |
|
82 : |
|
83 CActive( EPriorityStandard ), |
|
84 iObserver( aObserver ), |
|
85 iMessage( NULL ) |
|
86 { |
|
87 FUNC_LOG; |
|
88 CActiveScheduler::Add( this ); |
|
89 } |
|
90 |
|
91 // --------------------------------------------------------------------------- |
|
92 // CIpsPlgSearchOp::ConstructL() |
|
93 // --------------------------------------------------------------------------- |
|
94 // |
|
95 void CIpsPlgSearchOp::ConstructL() |
|
96 { |
|
97 FUNC_LOG; |
|
98 iState = EStarted; |
|
99 |
|
100 iSearcher = CIpsPlgTextSearcher::NewL( *this ); |
|
101 iSearcher->SetParametersL( |
|
102 iObserver.SearchStringsL(), |
|
103 EIpsPlgCriteriaOperationOR, |
|
104 EFalse, |
|
105 KIpsPlgSnippetLen ); |
|
106 |
|
107 ActivateAndComplete(); |
|
108 } |
|
109 |
|
110 // ======== MEMBER FUNCTIONS ======== |
|
111 |
|
112 // --------------------------------------------------------------------------- |
|
113 // From class CIpsPlgBaseOperation. |
|
114 // CIpsPlgSearchOp::DoCancel() |
|
115 // --------------------------------------------------------------------------- |
|
116 // |
|
117 void CIpsPlgSearchOp::DoCancel() |
|
118 { |
|
119 FUNC_LOG; |
|
120 iSearcher->Cleanup(); |
|
121 iState = ECanceled; |
|
122 } |
|
123 |
|
124 // --------------------------------------------------------------------------- |
|
125 // From class CIpsPlgBaseOperation. |
|
126 // CIpsPlgSearchOp::RunL() |
|
127 // --------------------------------------------------------------------------- |
|
128 // |
|
129 void CIpsPlgSearchOp::RunL() |
|
130 { |
|
131 FUNC_LOG; |
|
132 switch ( iState ) |
|
133 { |
|
134 case EStarted: |
|
135 { |
|
136 // Collect all message id's in the mailbox and sort them. |
|
137 iObserver.CollectMessagesL(); |
|
138 iObserver.Sort(); |
|
139 iState = ERunning; |
|
140 ActivateAndComplete(); |
|
141 break; |
|
142 } |
|
143 case ERunning: |
|
144 { |
|
145 for ( TInt i(0); i < KIpsPlgSearchMailsInRound; i++ ) |
|
146 { |
|
147 if ( NextMailL() ) |
|
148 { |
|
149 // Mail found, read the content and search for the string. |
|
150 // It must be made sure, that the search string contains valid |
|
151 // data to search the body, i.e. character set must match. |
|
152 // The following line is commented out since it currently doesn't do anything |
|
153 // HandleHtmlL(); |
|
154 SearchL(); |
|
155 } |
|
156 else |
|
157 { |
|
158 FinalizeL(); |
|
159 return; |
|
160 } |
|
161 } |
|
162 |
|
163 iState = ERunning; |
|
164 ActivateAndComplete(); |
|
165 break; |
|
166 } |
|
167 default: |
|
168 { |
|
169 break; |
|
170 } |
|
171 } |
|
172 |
|
173 } |
|
174 |
|
175 // --------------------------------------------------------------------------- |
|
176 // From class CIpsPlgBaseOperation. |
|
177 // CIpsPlgSearchOp::RunError() |
|
178 // --------------------------------------------------------------------------- |
|
179 // |
|
180 TInt CIpsPlgSearchOp::RunError( TInt /* aError */ ) |
|
181 { |
|
182 FUNC_LOG; |
|
183 return KErrGeneral; |
|
184 } |
|
185 |
|
186 // --------------------------------------------------------------------------- |
|
187 // CIpsPlgSearchOp::NextMailL() |
|
188 // --------------------------------------------------------------------------- |
|
189 // |
|
190 TBool CIpsPlgSearchOp::NextMailL() |
|
191 { |
|
192 FUNC_LOG; |
|
193 // In case the operation is cancelled, do not try to any extra steps. |
|
194 if ( iState == ERunning ) |
|
195 { |
|
196 // Delete object before creating new. Previous object is not needed. |
|
197 delete iMessage; |
|
198 iMessage = NULL; |
|
199 delete iHeader; |
|
200 iHeader = NULL; |
|
201 iMessage = iObserver.NextMessageL(); |
|
202 |
|
203 // If message is not set, the search is finished. |
|
204 // Run down the operation. |
|
205 if ( !iMessage ) |
|
206 { |
|
207 iState = EFinished; |
|
208 return EFalse; |
|
209 } |
|
210 |
|
211 // Retrieve the message header also. |
|
212 iHeader = iObserver.MessageHeaderL(); |
|
213 } |
|
214 else |
|
215 { |
|
216 // Stop the loop, cancel should not continue. |
|
217 return EFalse; |
|
218 } |
|
219 return ETrue; |
|
220 } |
|
221 |
|
222 // --------------------------------------------------------------------------- |
|
223 // CIpsPlgSearchOp::HandleHtmlL() |
|
224 // --------------------------------------------------------------------------- |
|
225 // |
|
226 void CIpsPlgSearchOp::HandleHtmlL() |
|
227 { |
|
228 FUNC_LOG; |
|
229 if ( iState == ERunning ) |
|
230 { |
|
231 } |
|
232 } |
|
233 |
|
234 // --------------------------------------------------------------------------- |
|
235 // CIpsPlgSearchOp::SearchBodyL() |
|
236 // --------------------------------------------------------------------------- |
|
237 // |
|
238 void CIpsPlgSearchOp::SearchBodyL() |
|
239 { |
|
240 FUNC_LOG; |
|
241 if ( iState == ERunning ) |
|
242 { |
|
243 // Just small amount of code to find the strings from the body. |
|
244 CParaFormatLayer* para = CParaFormatLayer::NewL(); |
|
245 CleanupStack::PushL( para ); |
|
246 CCharFormatLayer* chars = CCharFormatLayer::NewL(); |
|
247 CleanupStack::PushL( chars ); |
|
248 CRichText* txt = CRichText::NewL( para, chars ); |
|
249 CleanupStack::PushL( txt ); |
|
250 |
|
251 iMessage->GetBodyTextL( |
|
252 iObserver.CurrentMessage().Id(), |
|
253 CImEmailMessage::EThisMessageOnly, |
|
254 *txt, |
|
255 *para, |
|
256 *chars ); |
|
257 |
|
258 RBuf temp; |
|
259 temp.CreateL( txt->DocumentLength() ); |
|
260 CleanupClosePushL( temp ); |
|
261 |
|
262 txt->Extract( temp ); |
|
263 SearchFromStringL( temp ); |
|
264 |
|
265 CleanupStack::PopAndDestroy( &temp ); |
|
266 CleanupStack::PopAndDestroy( txt ); |
|
267 CleanupStack::PopAndDestroy( chars ); |
|
268 CleanupStack::PopAndDestroy( para ); |
|
269 } |
|
270 } |
|
271 |
|
272 // --------------------------------------------------------------------------- |
|
273 // CIpsPlgSearchOp::SearchFromArrayL() |
|
274 // --------------------------------------------------------------------------- |
|
275 // |
|
276 TBool CIpsPlgSearchOp::SearchFromArrayL( const CDesCArray& aArray ) |
|
277 { |
|
278 FUNC_LOG; |
|
279 TBool found( EFalse ); |
|
280 for ( TInt item(0); iState == ERunning && item < aArray.Count() && !found; item++ ) |
|
281 { |
|
282 found = SearchFromStringL( aArray[item] ); |
|
283 } |
|
284 return found; |
|
285 } |
|
286 |
|
287 // --------------------------------------------------------------------------- |
|
288 // CIpsPlgSearchOp::SearchFromStringL() |
|
289 // --------------------------------------------------------------------------- |
|
290 // |
|
291 TBool CIpsPlgSearchOp::SearchFromStringL( const TDesC& aString ) |
|
292 { |
|
293 FUNC_LOG; |
|
294 TBool found( EFalse ); |
|
295 if ( iState == ERunning ) |
|
296 { |
|
297 found = iSearcher->SearchL( aString ); |
|
298 } |
|
299 return found; |
|
300 } |
|
301 |
|
302 |
|
303 // --------------------------------------------------------------------------- |
|
304 // CIpsPlgSearchOp::SearchL() |
|
305 // --------------------------------------------------------------------------- |
|
306 // |
|
307 void CIpsPlgSearchOp::SearchL() |
|
308 { |
|
309 FUNC_LOG; |
|
310 if ( iState == ERunning ) |
|
311 { |
|
312 iSearcher->Cleanup(); |
|
313 |
|
314 // Mail is fetched, search from entire mail. |
|
315 if ( iHeader ) |
|
316 { |
|
317 SearchFromEntireMailL(); |
|
318 } |
|
319 // Mail not fetched, search only from subject. |
|
320 else |
|
321 { |
|
322 SearchFromSubjectL(); |
|
323 } |
|
324 iState = ( iState == EHit ) ? ERunning : iState; |
|
325 } |
|
326 } |
|
327 |
|
328 // --------------------------------------------------------------------------- |
|
329 // CIpsPlgSearchOp::HitL() |
|
330 // --------------------------------------------------------------------------- |
|
331 // |
|
332 void CIpsPlgSearchOp::HitL( |
|
333 const TDesC& /* aSnippet */, |
|
334 TInt /* aSnippetCharPos */, |
|
335 TBool /* aStartIncomplete */, |
|
336 TBool /* aEndIncomplete */ ) |
|
337 { |
|
338 FUNC_LOG; |
|
339 iObserver.MatchFoundL(); |
|
340 iState = EHit; |
|
341 } |
|
342 |
|
343 // --------------------------------------------------------------------------- |
|
344 // CIpsPlgSearchOp::FinalizeL() |
|
345 // --------------------------------------------------------------------------- |
|
346 // |
|
347 void CIpsPlgSearchOp::FinalizeL() |
|
348 { |
|
349 FUNC_LOG; |
|
350 // In case of cancel, the stop should not be indicated. |
|
351 if ( iState == EFinished ) |
|
352 { |
|
353 iObserver.SearchFinishedL(); |
|
354 iState = EStopped; |
|
355 } |
|
356 } |
|
357 |
|
358 // --------------------------------------------------------------------------- |
|
359 // CIpsPlgSearchOp::ActivateAndComplete() |
|
360 // --------------------------------------------------------------------------- |
|
361 // |
|
362 void CIpsPlgSearchOp::ActivateAndComplete( ) |
|
363 { |
|
364 FUNC_LOG; |
|
365 iStatus = KRequestPending; |
|
366 SetActive(); |
|
367 TRequestStatus* status = &iStatus; |
|
368 User::RequestComplete( status, KErrNone ); |
|
369 } |
|
370 |
|
371 // --------------------------------------------------------------------------- |
|
372 // CIpsPlgSearchOp::SearchFromEntireMailL( ) |
|
373 // --------------------------------------------------------------------------- |
|
374 // |
|
375 void CIpsPlgSearchOp::SearchFromEntireMailL( ) |
|
376 { |
|
377 FUNC_LOG; |
|
378 if ( SearchFromStringL( iHeader->Subject() ) ) |
|
379 { |
|
380 return; |
|
381 } |
|
382 if ( SearchFromStringL( iHeader->From() ) ) |
|
383 { |
|
384 return; |
|
385 } |
|
386 if ( SearchFromArrayL( iHeader->ToRecipients() ) ) |
|
387 { |
|
388 return; |
|
389 } |
|
390 if ( SearchFromArrayL( iHeader->CcRecipients() ) ) |
|
391 { |
|
392 return; |
|
393 } |
|
394 SearchBodyL(); |
|
395 } |
|
396 |
|
397 // --------------------------------------------------------------------------- |
|
398 // CIpsPlgSearchOp::SearchFromSubjectL( ) |
|
399 // --------------------------------------------------------------------------- |
|
400 // |
|
401 void CIpsPlgSearchOp::SearchFromSubjectL( ) |
|
402 { |
|
403 FUNC_LOG; |
|
404 RBuf subject; |
|
405 CleanupClosePushL( subject ); |
|
406 iObserver.GetSubjectL( subject ); |
|
407 TBool found = SearchFromStringL( subject ); |
|
408 CleanupStack::PopAndDestroy( &subject ); |
|
409 |
|
410 if ( !found ) |
|
411 { |
|
412 RBuf sender; |
|
413 CleanupClosePushL( sender ); |
|
414 iObserver.GetSenderL( sender ); |
|
415 found = SearchFromStringL( sender ); |
|
416 CleanupStack::PopAndDestroy( &sender ); |
|
417 } |
|
418 } |
|
419 |
|
420 // End of File |
|
421 |