|
1 /* |
|
2 * Copyright (c) 2006-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: A class for finding contact duplicates from stores |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <CPbk2DuplicateContactFinder.h> |
|
20 |
|
21 // From Phonebook2 |
|
22 #include <MPbk2DuplicateContactObserver.h> |
|
23 #include <MPbk2ContactNameFormatter.h> |
|
24 |
|
25 // From Virtual Phonebook |
|
26 #include <MVPbkFieldType.h> |
|
27 #include <MVPbkBaseContactFieldCollection.h> |
|
28 #include <MVPbkStoreContact.h> |
|
29 #include <MVPbkContactFieldTextData.h> |
|
30 #include <MVPbkContactStore.h> |
|
31 #include <CVPbkContactManager.h> |
|
32 #include <MVPbkContactOperation.h> |
|
33 #include <MVPbkContactLinkArray.h> |
|
34 |
|
35 #include <VPbkEng.rsg> |
|
36 namespace |
|
37 { |
|
38 #ifdef _DEBUG |
|
39 enum TPanicCode |
|
40 { |
|
41 EPanic_RetrieveContactL_OOB = 1 |
|
42 }; |
|
43 |
|
44 void Panic( TPanicCode aPanic ) |
|
45 { |
|
46 _LIT( KPanicCat, "CPbk2DuplicateContactFinder" ); |
|
47 User::Panic( KPanicCat(), aPanic ); |
|
48 } |
|
49 #endif // _DEBUG |
|
50 } |
|
51 |
|
52 enum TPbk2DuplicateContactFinderState |
|
53 { |
|
54 ENoDuplicatesFound, |
|
55 EStartFind, |
|
56 ERetrieveContact, |
|
57 EComplete |
|
58 }; |
|
59 |
|
60 const TInt KNameFormattingFlags = |
|
61 MPbk2ContactNameFormatter::EPreserveLeadingSpaces; |
|
62 |
|
63 // --------------------------------------------------------------------------- |
|
64 // CPbk2DuplicateContactFinder::CPbk2DuplicateContactFinder |
|
65 // --------------------------------------------------------------------------- |
|
66 // |
|
67 CPbk2DuplicateContactFinder::CPbk2DuplicateContactFinder( |
|
68 CVPbkContactManager& aContactManager, |
|
69 MPbk2ContactNameFormatter& aNameFormatter, |
|
70 const MVPbkFieldTypeList& aFieldTypesForFind, |
|
71 RPointerArray<MVPbkStoreContact>& aDuplicateContacts ): |
|
72 CActive( EPriorityStandard ), |
|
73 iContactManager( aContactManager ), |
|
74 iNameFormatter( aNameFormatter ), |
|
75 iFieldTypesForFind( aFieldTypesForFind ), |
|
76 iFindText( KNullDesC ), |
|
77 iDuplicates( aDuplicateContacts ) |
|
78 { |
|
79 } |
|
80 |
|
81 |
|
82 // --------------------------------------------------------------------------- |
|
83 // CPbk2DuplicateContactFinder::ConstructL |
|
84 // --------------------------------------------------------------------------- |
|
85 // |
|
86 void CPbk2DuplicateContactFinder::ConstructL() |
|
87 { |
|
88 CActiveScheduler::Add( this ); |
|
89 } |
|
90 |
|
91 // --------------------------------------------------------------------------- |
|
92 // CPbk2DuplicateContactFinder::NewL |
|
93 // --------------------------------------------------------------------------- |
|
94 // |
|
95 EXPORT_C CPbk2DuplicateContactFinder* CPbk2DuplicateContactFinder::NewL( |
|
96 CVPbkContactManager& aContactManager, |
|
97 MPbk2ContactNameFormatter& aNameFormatter, |
|
98 const MVPbkFieldTypeList& aFieldTypesForFind, |
|
99 RPointerArray<MVPbkStoreContact>& aDuplicateContacts ) |
|
100 { |
|
101 CPbk2DuplicateContactFinder* self = |
|
102 new( ELeave ) CPbk2DuplicateContactFinder( aContactManager, |
|
103 aNameFormatter, aFieldTypesForFind, aDuplicateContacts ); |
|
104 CleanupStack::PushL( self ); |
|
105 self->ConstructL(); |
|
106 CleanupStack::Pop( self ); |
|
107 return self; |
|
108 } |
|
109 |
|
110 // --------------------------------------------------------------------------- |
|
111 // destructor |
|
112 // --------------------------------------------------------------------------- |
|
113 // |
|
114 CPbk2DuplicateContactFinder::~CPbk2DuplicateContactFinder() |
|
115 { |
|
116 Cancel(); |
|
117 delete iContactOperation; |
|
118 delete iFindResults; |
|
119 delete iContactTitle; |
|
120 } |
|
121 |
|
122 // --------------------------------------------------------------------------- |
|
123 // CPbk2DuplicateContactFinder::StartL |
|
124 // --------------------------------------------------------------------------- |
|
125 // |
|
126 EXPORT_C void CPbk2DuplicateContactFinder::StartL( |
|
127 const MVPbkBaseContact& aContact, |
|
128 MVPbkContactStore& aStore, |
|
129 MPbk2DuplicateContactObserver& aObserver, |
|
130 TInt aMaxDuplicatesToFind ) |
|
131 { |
|
132 iStore = &aStore; |
|
133 StartL( aContact, aObserver, aMaxDuplicatesToFind ); |
|
134 } |
|
135 |
|
136 // --------------------------------------------------------------------------- |
|
137 // CPbk2DuplicateContactFinder::StartL |
|
138 // --------------------------------------------------------------------------- |
|
139 // |
|
140 EXPORT_C void CPbk2DuplicateContactFinder::StartL( |
|
141 const MVPbkBaseContact& aContact, |
|
142 MPbk2DuplicateContactObserver& aObserver, |
|
143 TInt aMaxDuplicatesToFind ) |
|
144 { |
|
145 iObserver = &aObserver; |
|
146 iContactToCompare = &aContact; |
|
147 iContactIndex = 0; |
|
148 iMaxDuplicatesToFind = aMaxDuplicatesToFind; |
|
149 |
|
150 iState = ENoDuplicatesFound; |
|
151 iFindText.Set( FindText() ); |
|
152 if ( iFindText.Length() > 0 ) |
|
153 { |
|
154 iState = EStartFind; |
|
155 } |
|
156 IssueRequest(); |
|
157 } |
|
158 |
|
159 // --------------------------------------------------------------------------- |
|
160 // From class CActive. |
|
161 // CPbk2DuplicateContactFinder::RunL |
|
162 // --------------------------------------------------------------------------- |
|
163 // |
|
164 void CPbk2DuplicateContactFinder::RunL() |
|
165 { |
|
166 switch ( iState ) |
|
167 { |
|
168 case EStartFind: |
|
169 { |
|
170 StartFindL(); |
|
171 break; |
|
172 } |
|
173 case ERetrieveContact: |
|
174 { |
|
175 RetrieveContactL(); |
|
176 break; |
|
177 } |
|
178 case EComplete: |
|
179 { |
|
180 CompleteL(); |
|
181 break; |
|
182 } |
|
183 case ENoDuplicatesFound: // FALLTHROUGH |
|
184 default: |
|
185 { |
|
186 iObserver->DuplicateFindComplete(); |
|
187 break; |
|
188 } |
|
189 } |
|
190 } |
|
191 |
|
192 // --------------------------------------------------------------------------- |
|
193 // From class CActive. |
|
194 // CPbk2DuplicateContactFinder::DoCancel |
|
195 // --------------------------------------------------------------------------- |
|
196 // |
|
197 void CPbk2DuplicateContactFinder::DoCancel() |
|
198 { |
|
199 DestroyOperation(); |
|
200 } |
|
201 |
|
202 // --------------------------------------------------------------------------- |
|
203 // From class CActive. |
|
204 // CPbk2DuplicateContactFinder::RunError |
|
205 // --------------------------------------------------------------------------- |
|
206 // |
|
207 TInt CPbk2DuplicateContactFinder::RunError( TInt aError ) |
|
208 { |
|
209 iObserver->DuplicateFindFailed( aError ); |
|
210 return KErrNone; |
|
211 } |
|
212 |
|
213 // --------------------------------------------------------------------------- |
|
214 // From class MVPbkContactFindObserver. |
|
215 // CPbk2DuplicateContactFinder::FindCompleteL |
|
216 // --------------------------------------------------------------------------- |
|
217 // |
|
218 void CPbk2DuplicateContactFinder::FindCompleteL( |
|
219 MVPbkContactLinkArray* aResults ) |
|
220 { |
|
221 iFindResults = aResults; |
|
222 iState = ENoDuplicatesFound; |
|
223 if ( !LastContactRetrieved() ) |
|
224 { |
|
225 iState = ERetrieveContact; |
|
226 } |
|
227 |
|
228 IssueRequest(); |
|
229 } |
|
230 |
|
231 // --------------------------------------------------------------------------- |
|
232 // From class MVPbkContactFindObserver. |
|
233 // CPbk2DuplicateContactFinder::FindFailed |
|
234 // --------------------------------------------------------------------------- |
|
235 // |
|
236 void CPbk2DuplicateContactFinder::FindFailed( TInt aError ) |
|
237 { |
|
238 iObserver->DuplicateFindFailed( aError ); |
|
239 } |
|
240 |
|
241 // --------------------------------------------------------------------------- |
|
242 // From class MVPbkSingleContactOperationObserver. |
|
243 // CPbk2DuplicateContactFinder::VPbkSingleContactOperationComplete |
|
244 // --------------------------------------------------------------------------- |
|
245 // |
|
246 void CPbk2DuplicateContactFinder::VPbkSingleContactOperationComplete( |
|
247 MVPbkContactOperationBase& /*aOperation*/, |
|
248 MVPbkStoreContact* aContact ) |
|
249 { |
|
250 TRAPD( res, CheckDuplicateL( aContact ) ); |
|
251 |
|
252 ++iContactIndex; |
|
253 if ( res != KErrNone ) |
|
254 { |
|
255 iObserver->DuplicateFindFailed( res ); |
|
256 } |
|
257 else if ( LastContactRetrieved() ) |
|
258 { |
|
259 iState = EComplete; |
|
260 IssueRequest(); |
|
261 } |
|
262 else |
|
263 { |
|
264 // Continue retrieving |
|
265 IssueRequest(); |
|
266 } |
|
267 } |
|
268 |
|
269 // --------------------------------------------------------------------------- |
|
270 // From class MVPbkSingleContactOperationObserver. |
|
271 // CPbk2DuplicateContactFinder::VPbkSingleContactOperationFailed |
|
272 // --------------------------------------------------------------------------- |
|
273 // |
|
274 void CPbk2DuplicateContactFinder::VPbkSingleContactOperationFailed( |
|
275 MVPbkContactOperationBase& /*aOperation*/, TInt aError ) |
|
276 { |
|
277 iObserver->DuplicateFindFailed( aError ); |
|
278 } |
|
279 |
|
280 // --------------------------------------------------------------------------- |
|
281 // CPbk2DuplicateContactFinder::IssueRequest |
|
282 // --------------------------------------------------------------------------- |
|
283 // |
|
284 void CPbk2DuplicateContactFinder::IssueRequest() |
|
285 { |
|
286 TRequestStatus* st = &iStatus; |
|
287 User::RequestComplete( st, KErrNone ); |
|
288 SetActive(); |
|
289 } |
|
290 |
|
291 // --------------------------------------------------------------------------- |
|
292 // CPbk2DuplicateContactFinder::FindText |
|
293 // --------------------------------------------------------------------------- |
|
294 // |
|
295 TPtrC CPbk2DuplicateContactFinder::FindText() |
|
296 { |
|
297 const MVPbkBaseContactFieldCollection& fields = |
|
298 iContactToCompare->Fields(); |
|
299 // Find a text from the contact in the order of iFieldTypesForFind. |
|
300 // the first data that is found is used as a find text |
|
301 const TInt findTypeCount = iFieldTypesForFind.FieldTypeCount(); |
|
302 for ( TInt i = 0; i < findTypeCount; ++i ) |
|
303 { |
|
304 const MVPbkFieldType& findType = iFieldTypesForFind.FieldTypeAt( i ); |
|
305 const TInt fieldCount = fields.FieldCount(); |
|
306 for ( TInt j = 0; j < fieldCount; ++j ) |
|
307 { |
|
308 const MVPbkFieldType* type = |
|
309 fields.FieldAt( j ).BestMatchingFieldType(); |
|
310 if ( type && findType.IsSame( *type ) ) |
|
311 { |
|
312 const MVPbkContactFieldData& data = |
|
313 fields.FieldAt( j ).FieldData(); |
|
314 if ( data.DataType() == EVPbkFieldStorageTypeText ) |
|
315 { |
|
316 TPtrC text( MVPbkContactFieldTextData::Cast( |
|
317 data ).Text() ); |
|
318 if ( text.Length() > 0 ) |
|
319 { |
|
320 return text; |
|
321 } |
|
322 } |
|
323 } |
|
324 } |
|
325 } |
|
326 // No find text -> no duplicates for contact. |
|
327 return TPtrC( KNullDesC ); |
|
328 } |
|
329 |
|
330 // --------------------------------------------------------------------------- |
|
331 // CPbk2DuplicateContactFinder::FindOperationL |
|
332 // --------------------------------------------------------------------------- |
|
333 // |
|
334 MVPbkContactOperationBase* CPbk2DuplicateContactFinder::FindOperationL( |
|
335 const TDesC& aFindText ) |
|
336 { |
|
337 if ( iStore ) |
|
338 { |
|
339 MVPbkContactOperation* op = iStore->CreateFindOperationL( aFindText, |
|
340 iFieldTypesForFind, *this ); |
|
341 CleanupDeletePushL( op ); |
|
342 op->StartL(); |
|
343 CleanupStack::Pop(); // op |
|
344 return op; |
|
345 } |
|
346 else |
|
347 { |
|
348 return iContactManager.FindL( aFindText, |
|
349 iFieldTypesForFind, *this ); |
|
350 } |
|
351 } |
|
352 |
|
353 // --------------------------------------------------------------------------- |
|
354 // CPbk2DuplicateContactFinder::StartFindL |
|
355 // --------------------------------------------------------------------------- |
|
356 // |
|
357 void CPbk2DuplicateContactFinder::StartFindL() |
|
358 { |
|
359 delete iFindResults; |
|
360 iFindResults = NULL; |
|
361 |
|
362 delete iContactTitle; |
|
363 iContactTitle = NULL; |
|
364 iContactTitle = iNameFormatter.GetContactTitleL( |
|
365 iContactToCompare->Fields(), KNameFormattingFlags ); |
|
366 |
|
367 DestroyOperation(); |
|
368 iContactOperation = FindOperationL( iFindText ); |
|
369 } |
|
370 |
|
371 // --------------------------------------------------------------------------- |
|
372 // CPbk2DuplicateContactFinder::RetrieveContactL |
|
373 // --------------------------------------------------------------------------- |
|
374 // |
|
375 void CPbk2DuplicateContactFinder::RetrieveContactL() |
|
376 { |
|
377 DestroyOperation(); |
|
378 __ASSERT_DEBUG( iFindResults->Count() > iContactIndex, |
|
379 Panic( EPanic_RetrieveContactL_OOB ) ); |
|
380 iContactOperation = iContactManager.RetrieveContactL( |
|
381 iFindResults->At( iContactIndex ), *this ); |
|
382 } |
|
383 |
|
384 // --------------------------------------------------------------------------- |
|
385 // CPbk2DuplicateContactFinder::CompleteL |
|
386 // --------------------------------------------------------------------------- |
|
387 // |
|
388 void CPbk2DuplicateContactFinder::CompleteL() |
|
389 { |
|
390 iObserver->DuplicateFindComplete(); |
|
391 } |
|
392 |
|
393 // --------------------------------------------------------------------------- |
|
394 // CPbk2DuplicateContactFinder::LastContactRetrieved |
|
395 // --------------------------------------------------------------------------- |
|
396 // |
|
397 TBool CPbk2DuplicateContactFinder::LastContactRetrieved() |
|
398 { |
|
399 if ( iDuplicates.Count() >= iMaxDuplicatesToFind || |
|
400 iContactIndex >= iFindResults->Count() ) |
|
401 { |
|
402 return ETrue; |
|
403 } |
|
404 return EFalse; |
|
405 } |
|
406 |
|
407 // --------------------------------------------------------------------------- |
|
408 // CPbk2DuplicateContactFinder::DestroyOperation |
|
409 // --------------------------------------------------------------------------- |
|
410 // |
|
411 void CPbk2DuplicateContactFinder::DestroyOperation() |
|
412 { |
|
413 delete iContactOperation; |
|
414 iContactOperation = NULL; |
|
415 } |
|
416 |
|
417 // --------------------------------------------------------------------------- |
|
418 // CPbk2DuplicateContactFinder::CheckDuplicateL |
|
419 // --------------------------------------------------------------------------- |
|
420 // |
|
421 void CPbk2DuplicateContactFinder::CheckDuplicateL( |
|
422 MVPbkStoreContact* aContact ) |
|
423 { |
|
424 aContact->PushL(); |
|
425 |
|
426 TBool duplicate = EFalse; |
|
427 HBufC* candidateTitle = iNameFormatter.GetContactTitleOrNullL( |
|
428 aContact->Fields(), KNameFormattingFlags ); |
|
429 // If the aContact is not a group and it has a title, compare it with iContactTitle. |
|
430 // This is because some group has the same name with the contact and they can't be |
|
431 // considered as duplicate. |
|
432 if ( !aContact->Group() && candidateTitle ) |
|
433 { |
|
434 CleanupStack::PushL( candidateTitle ); |
|
435 if ( iContactTitle->CompareF( *candidateTitle ) == 0 ) |
|
436 { |
|
437 // Compare the first and last name. |
|
438 if ( IsFieldMatched( aContact, R_VPBK_FIELD_TYPE_FIRSTNAME ) && |
|
439 IsFieldMatched( aContact, R_VPBK_FIELD_TYPE_LASTNAME ) ) |
|
440 { |
|
441 duplicate = ETrue; |
|
442 } |
|
443 } |
|
444 CleanupStack::PopAndDestroy( candidateTitle ); |
|
445 } |
|
446 |
|
447 if ( duplicate ) |
|
448 { |
|
449 iDuplicates.AppendL( aContact ); |
|
450 CleanupStack::Pop(); // aContact |
|
451 } |
|
452 else |
|
453 { |
|
454 CleanupStack::PopAndDestroy(); // aContact |
|
455 } |
|
456 } |
|
457 |
|
458 TBool CPbk2DuplicateContactFinder::IsFieldMatched( const MVPbkBaseContact* aContact, TInt aFieldId ) |
|
459 { |
|
460 TBool result = EFalse; |
|
461 |
|
462 const MVPbkBaseContactField* compareContactField = FindFieldById( iContactToCompare, aFieldId ); |
|
463 const MVPbkBaseContactField* contactField = FindFieldById( aContact, aFieldId ); |
|
464 if ( NULL == compareContactField && NULL == contactField ) |
|
465 { |
|
466 result = ETrue; |
|
467 } |
|
468 else if ( compareContactField && contactField ) |
|
469 { |
|
470 const TDesC& compareContactFieldText = |
|
471 MVPbkContactFieldTextData::Cast(compareContactField->FieldData()).Text(); |
|
472 const TDesC& contactFieldText = |
|
473 MVPbkContactFieldTextData::Cast(compareContactField->FieldData()).Text(); |
|
474 if ( compareContactFieldText.CompareF( contactFieldText ) == 0 ) |
|
475 { |
|
476 result = ETrue; |
|
477 } |
|
478 } |
|
479 |
|
480 return result; |
|
481 } |
|
482 |
|
483 const MVPbkBaseContactField* CPbk2DuplicateContactFinder::FindFieldById( const MVPbkBaseContact* aContact, |
|
484 TInt aFieldId ) |
|
485 { |
|
486 const TInt fieldCount = aContact->Fields().FieldCount(); |
|
487 const MVPbkBaseContactFieldCollection& fieldSet = aContact->Fields(); |
|
488 for ( TInt i = 0; i < fieldCount; ++i ) |
|
489 { |
|
490 const MVPbkBaseContactField& field = fieldSet.FieldAt(i); |
|
491 TInt fieldTypeId = field.BestMatchingFieldType()->FieldTypeResId(); |
|
492 |
|
493 if ( fieldTypeId == aFieldId ) |
|
494 { |
|
495 return &field; |
|
496 } |
|
497 } |
|
498 return NULL; |
|
499 } |
|
500 |