|
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: Holds the contact information in memory. It maintains a |
|
15 * master array of all contacts. It also has 10 pools corresponding |
|
16 * to each key id in the keyboard. Based on numeric key char of |
|
17 * first char of firstname/ lastname a contact is added to one of the |
|
18 * pools. Implements MDataStoreObserver interface functions to |
|
19 * add/ remove contacts. |
|
20 * |
|
21 */ |
|
22 |
|
23 |
|
24 // INCLUDE FILES |
|
25 #include <MVPbkContactLink.h> |
|
26 |
|
27 #include "CPsData.h" |
|
28 #include "CPcsCache.h" |
|
29 #include "CPcsDebug.h" |
|
30 #include "CWords.h" |
|
31 #include "CPcsAlgorithm1Utils.h" |
|
32 |
|
33 |
|
34 // ============================== MEMBER FUNCTIONS ============================ |
|
35 |
|
36 // ---------------------------------------------------------------------------- |
|
37 // CPcsCache::NewL |
|
38 // Two Phase Construction |
|
39 // ---------------------------------------------------------------------------- |
|
40 CPcsCache* CPcsCache::NewL(TDesC& aURI, CPcsKeyMap& aKeyMap, TUint8 aUriId) |
|
41 { |
|
42 PRINT ( _L("Enter CPcsCache::NewL") ); |
|
43 |
|
44 CPcsCache* instance= new ( ELeave ) CPcsCache(); |
|
45 |
|
46 CleanupStack::PushL( instance ); |
|
47 |
|
48 instance->ConstructL(aURI, aKeyMap, aUriId); |
|
49 |
|
50 CleanupStack::Pop( instance ); |
|
51 |
|
52 PRINT ( _L("End CPcsCache::NewL") ); |
|
53 |
|
54 return instance; |
|
55 } |
|
56 |
|
57 // ---------------------------------------------------------------------------- |
|
58 // CPcsCache::CPcsCache |
|
59 // Constructor |
|
60 // ---------------------------------------------------------------------------- |
|
61 CPcsCache::CPcsCache() |
|
62 { |
|
63 PRINT ( _L("Enter CPcsCache::CPcsCache") ); |
|
64 PRINT ( _L("End CPcsCache::CPcsCache") ); |
|
65 } |
|
66 |
|
67 // ---------------------------------------------------------------------------- |
|
68 // CPcsCache::ConstructL |
|
69 // 2nd Phase Constructor |
|
70 // ---------------------------------------------------------------------------- |
|
71 void CPcsCache::ConstructL(TDesC& aURI, CPcsKeyMap& aKeyMap, TUint8 aUriId) |
|
72 { |
|
73 PRINT ( _L("Enter CPcsCache::ConstructL") ); |
|
74 |
|
75 iURI = aURI.AllocL(); |
|
76 iUriId = aUriId; |
|
77 //Update the caching status for this cache |
|
78 iCacheStatus = ECachingNotStarted; |
|
79 |
|
80 keyMap = &aKeyMap; |
|
81 |
|
82 // Populate keyArr |
|
83 for(TInt i= 0; i <aKeyMap.PoolCount();i++ ) |
|
84 { |
|
85 RPointerArray<CPcsPoolElement> *keyMap = new (ELeave) RPointerArray<CPcsPoolElement>(1); |
|
86 keyArr.InsertL(keyMap,i); |
|
87 } |
|
88 |
|
89 |
|
90 PRINT ( _L("End CPcsCache::ConstructL") ); |
|
91 } |
|
92 |
|
93 // ---------------------------------------------------------------------------- |
|
94 // CPcsCache::~CPcsCache |
|
95 // Destructor |
|
96 // ---------------------------------------------------------------------------- |
|
97 CPcsCache::~CPcsCache() |
|
98 { |
|
99 PRINT ( _L("Enter CPcsCache::~CPcsCache") ); |
|
100 |
|
101 if ( iURI ) |
|
102 delete iURI; |
|
103 |
|
104 // Loop thru cache info and free and the data elements |
|
105 THashMapIter<TInt, TInt> iter(cacheInfo); |
|
106 |
|
107 do |
|
108 { |
|
109 TInt* id = const_cast<TInt*>(iter.NextKey()); |
|
110 |
|
111 if ( id == NULL ) |
|
112 break; |
|
113 |
|
114 TInt* poolMap = iter.CurrentValue(); |
|
115 |
|
116 if ( poolMap == NULL ) |
|
117 { |
|
118 continue; |
|
119 } |
|
120 |
|
121 CPsData *data = NULL; |
|
122 for ( int keyIndex = 0; keyIndex < keyArr.Count(); keyIndex++ ) |
|
123 { |
|
124 TBool present = GetPoolMap(*poolMap, keyIndex); |
|
125 |
|
126 if ( ! present ) |
|
127 { |
|
128 continue; |
|
129 } |
|
130 |
|
131 RPointerArray<CPcsPoolElement> tmpKeyMap = *(keyArr[keyIndex]); |
|
132 for ( int arrayIndex = 0; |
|
133 arrayIndex < tmpKeyMap.Count(); |
|
134 arrayIndex++ ) |
|
135 { |
|
136 CPcsPoolElement *element = tmpKeyMap[arrayIndex]; |
|
137 TInt localId = element->GetPsData()->Id(); |
|
138 if ( *id == localId ) |
|
139 { |
|
140 data = element->GetPsData(); |
|
141 delete element; |
|
142 keyArr[keyIndex]->Remove(arrayIndex); |
|
143 } |
|
144 } |
|
145 }; |
|
146 |
|
147 // Remove this element from master pool |
|
148 for ( int arrayIndex = 0; |
|
149 arrayIndex < masterPool.Count(); |
|
150 arrayIndex++ ) |
|
151 { |
|
152 CPsData *dataElement = masterPool[arrayIndex]; |
|
153 TInt localId = dataElement->Id(); |
|
154 if ( *id == localId ) |
|
155 { |
|
156 masterPool.Remove(arrayIndex); |
|
157 } |
|
158 } |
|
159 |
|
160 if( data ) |
|
161 { |
|
162 delete data; |
|
163 } |
|
164 |
|
165 } |
|
166 while (1); |
|
167 |
|
168 for(TInt i= 0; i <keyArr.Count();i++ ) |
|
169 { |
|
170 keyArr[i]->ResetAndDestroy(); |
|
171 delete keyArr[i]; |
|
172 keyArr[i] = NULL; |
|
173 } |
|
174 |
|
175 masterPool.ResetAndDestroy(); |
|
176 |
|
177 cacheInfo.Close(); |
|
178 |
|
179 keyArr.Reset(); |
|
180 iDataFields.Reset(); |
|
181 iSortOrder.Reset(); |
|
182 iIndexOrder.Reset(); |
|
183 |
|
184 PRINT ( _L("End CPcsCache::~CPcsCache") ); |
|
185 } |
|
186 |
|
187 // ---------------------------------------------------------------------------- |
|
188 // CPcsCache::GetContactsForKeyL |
|
189 // Get list of pool elements specific to a pool |
|
190 // ---------------------------------------------------------------------------- |
|
191 void CPcsCache::GetContactsForKeyL(TInt aKeyId, RPointerArray<CPcsPoolElement>& aData) |
|
192 { |
|
193 PRINT ( _L("Enter CPcsCache::GetContactsForKeyL") ); |
|
194 |
|
195 RPointerArray<CPcsPoolElement> arr = *keyArr[aKeyId]; |
|
196 for ( int i = 0; i < arr.Count(); i++ ) |
|
197 { |
|
198 CPcsPoolElement* value = arr[i]; |
|
199 aData.AppendL(value); |
|
200 } |
|
201 |
|
202 PRINT ( _L("End CPcsCache::GetContactsForKeyL") ); |
|
203 } |
|
204 |
|
205 // ---------------------------------------------------------------------------- |
|
206 // CPcsCache::GetAllContentsL |
|
207 // Get all data elements in this cache |
|
208 // ---------------------------------------------------------------------------- |
|
209 void CPcsCache::GetAllContentsL(RPointerArray<CPsData>& aData) |
|
210 { |
|
211 PRINT ( _L("Enter CPcsCache::GetAllContentsL") ); |
|
212 |
|
213 for ( int i = 0; i < masterPool.Count(); i++ ) |
|
214 { |
|
215 CPsData* value = masterPool[i]; |
|
216 aData.AppendL(value); |
|
217 } |
|
218 |
|
219 PRINT ( _L("End CPcsCache::GetAllContentsL") ); |
|
220 } |
|
221 |
|
222 |
|
223 // ---------------------------------------------------------------------------- |
|
224 // CPcsCache::AddToPool |
|
225 // Adds a contact to cache |
|
226 // ---------------------------------------------------------------------------- |
|
227 void CPcsCache::AddToPoolL(TInt& aPoolMap, CPsData& aData) |
|
228 { |
|
229 // Temp hash to remember the location of pool elements |
|
230 // First TInt = Pool |
|
231 // Second TInt = Location in the pool |
|
232 // Required for memory optimization so that more than one pool |
|
233 // element doesn't get added for the same data |
|
234 RHashMap<TInt, TInt> elementHash; |
|
235 TLinearOrder<CPcsPoolElement> rule( CPcsPoolElement::CompareByData ); |
|
236 |
|
237 // Parse thru each data element |
|
238 for ( int dataIndex = 0; dataIndex < aData.DataElementCount(); dataIndex++ ) |
|
239 { |
|
240 // Stores first key for each word |
|
241 RArray<TUint> firstKey; |
|
242 |
|
243 // Recover the first character |
|
244 if ( aData.Data(dataIndex) && aData.Data(dataIndex)->Length() != 0 ) |
|
245 { |
|
246 // Split the data into words |
|
247 CWords* words = CWords::NewLC(*aData.Data(dataIndex)); |
|
248 |
|
249 // Store the first numeric key for each word |
|
250 for ( int i = 0; i < words->MdcaCount(); i++ ) |
|
251 { |
|
252 TChar firstChar = (words->MdcaPoint(i))[0]; |
|
253 firstKey.Append(firstChar); |
|
254 } |
|
255 |
|
256 CleanupStack::PopAndDestroy(words); |
|
257 } |
|
258 |
|
259 for ( TInt wordIndex = 0; wordIndex < firstKey.Count(); wordIndex++ ) |
|
260 { |
|
261 TInt arrayIndex =keyMap->PoolIdForCharacter(firstKey[wordIndex]); |
|
262 |
|
263 CPcsPoolElement* element = NULL; |
|
264 |
|
265 // Check if an element already exists in the pool for this data |
|
266 TInt* loc = NULL; |
|
267 loc = elementHash.Find(arrayIndex); |
|
268 if ( loc != NULL ) |
|
269 { |
|
270 // Exists. Then recover ... |
|
271 RPointerArray<CPcsPoolElement> tmpKeyMap = *(keyArr[arrayIndex]); |
|
272 element = tmpKeyMap[*loc]; |
|
273 } |
|
274 |
|
275 if ( element == NULL ) // Pool element doesn't exist. Create new ... |
|
276 { |
|
277 element = CPcsPoolElement::NewL(aData); |
|
278 element->ClearDataMatchAttribute(); |
|
279 element->SetDataMatch(dataIndex); |
|
280 |
|
281 // Insert to pool |
|
282 keyArr[arrayIndex]->InsertInOrderAllowRepeatsL(element, rule); |
|
283 TInt index = keyArr[arrayIndex]->FindInOrderL(element, rule); |
|
284 |
|
285 // Set the bit for this pool |
|
286 SetPoolMap(aPoolMap, arrayIndex); |
|
287 |
|
288 // Store the array index in the temp hash |
|
289 elementHash.InsertL(arrayIndex, index ); |
|
290 } |
|
291 else // Pool element exists. Just alter the data match attribute |
|
292 { |
|
293 element->SetDataMatch(dataIndex); |
|
294 |
|
295 // Set the bit for this pool |
|
296 SetPoolMap(aPoolMap, arrayIndex); |
|
297 } |
|
298 |
|
299 |
|
300 } // for 2 loop |
|
301 |
|
302 firstKey.Reset(); |
|
303 |
|
304 } // for 1 loop |
|
305 |
|
306 elementHash.Close(); |
|
307 } |
|
308 |
|
309 // --------------------------------------------------------------------- |
|
310 // CPcsCache::AddToCacheL |
|
311 // |
|
312 // --------------------------------------------------------------------- |
|
313 void CPcsCache::AddToCacheL( CPsData& aData ) |
|
314 { |
|
315 // Protect against duplicate items getting added |
|
316 if ( cacheInfo.Find(aData.Id()) != NULL ) |
|
317 { |
|
318 return; |
|
319 } |
|
320 |
|
321 // Include this element in the pool |
|
322 TInt poolMap = 0; |
|
323 AddToPoolL(poolMap, aData); |
|
324 cacheInfo.InsertL(aData.Id(), poolMap); |
|
325 |
|
326 // Include this element in master pool |
|
327 TLinearOrder<CPsData> rule( CPcsAlgorithm1Utils::CompareDataBySortOrderL ); |
|
328 masterPool.InsertInOrderAllowRepeatsL(&aData, rule); |
|
329 } |
|
330 |
|
331 // --------------------------------------------------------------------- |
|
332 // CPcsCache::RemoveContactL |
|
333 // |
|
334 // --------------------------------------------------------------------- |
|
335 void CPcsCache::RemoveFromCacheL( TInt aItemId ) |
|
336 { |
|
337 CPsData *data = NULL; |
|
338 |
|
339 TInt* poolMap = cacheInfo.Find(aItemId); |
|
340 |
|
341 if ( poolMap == NULL ) |
|
342 { |
|
343 return; |
|
344 } |
|
345 |
|
346 // Remove this element from pools |
|
347 for ( int keyIndex = 0; keyIndex < keyArr.Count(); keyIndex++ ) |
|
348 { |
|
349 TBool present = GetPoolMap(*poolMap, keyIndex); |
|
350 |
|
351 if ( ! present ) |
|
352 { |
|
353 continue; |
|
354 } |
|
355 |
|
356 RPointerArray<CPcsPoolElement> tmpKeyMap = *(keyArr[keyIndex]); |
|
357 for ( int arrayIndex = 0; |
|
358 arrayIndex < tmpKeyMap.Count(); |
|
359 arrayIndex++ ) |
|
360 { |
|
361 CPcsPoolElement *element = tmpKeyMap[arrayIndex]; |
|
362 TInt id = element->GetPsData()->Id(); |
|
363 if ( id == aItemId ) |
|
364 { |
|
365 data = element->GetPsData(); |
|
366 delete element; |
|
367 keyArr[keyIndex]->Remove(arrayIndex); |
|
368 } |
|
369 } |
|
370 }; |
|
371 |
|
372 // Remove this element from master pool |
|
373 for ( int arrayIndex = 0; |
|
374 arrayIndex < masterPool.Count(); |
|
375 arrayIndex++ ) |
|
376 { |
|
377 CPsData *dataElement = masterPool[arrayIndex]; |
|
378 TInt id = dataElement->Id(); |
|
379 if ( id == aItemId ) |
|
380 { |
|
381 masterPool.Remove(arrayIndex); |
|
382 } |
|
383 } |
|
384 |
|
385 // Delete data |
|
386 if ( data ) |
|
387 { |
|
388 delete data; |
|
389 data = NULL; |
|
390 } |
|
391 |
|
392 // Clear up cache information |
|
393 cacheInfo.Remove(aItemId); |
|
394 } |
|
395 |
|
396 // --------------------------------------------------------------------- |
|
397 // CPcsCache::RemoveAllFromCacheL |
|
398 // |
|
399 // --------------------------------------------------------------------- |
|
400 void CPcsCache::RemoveAllFromCacheL() |
|
401 { |
|
402 PRINT ( _L("Enter CPcsCache::RemoveAllFromCacheL") ); |
|
403 |
|
404 |
|
405 for(TInt i= 0; i <keyArr.Count();i++ ) |
|
406 { |
|
407 keyArr[i]->ResetAndDestroy(); |
|
408 |
|
409 } |
|
410 |
|
411 masterPool.ResetAndDestroy(); |
|
412 cacheInfo.Close(); |
|
413 |
|
414 PRINT ( _L("End CPcsCache::RemoveAllFromCacheL") ); |
|
415 } |
|
416 |
|
417 // --------------------------------------------------------------------- |
|
418 // CPcsCache::SetPoolMap |
|
419 // |
|
420 // --------------------------------------------------------------------- |
|
421 void CPcsCache::SetPoolMap(TInt& aPoolMap, TInt arrayIndex) |
|
422 { |
|
423 TReal val; |
|
424 Math::Pow(val, 2, arrayIndex); |
|
425 |
|
426 aPoolMap |= (TInt)val; |
|
427 } |
|
428 |
|
429 // --------------------------------------------------------------------- |
|
430 // CPcsCache::GetPoolMap |
|
431 // |
|
432 // --------------------------------------------------------------------- |
|
433 TBool CPcsCache::GetPoolMap(TInt& aPoolMap, TInt arrayIndex) |
|
434 { |
|
435 TReal val; |
|
436 Math::Pow(val, 2, arrayIndex); |
|
437 |
|
438 return (aPoolMap & (TInt)val); |
|
439 } |
|
440 |
|
441 // --------------------------------------------------------------------- |
|
442 // CPcsCache::GetURI |
|
443 // |
|
444 // --------------------------------------------------------------------- |
|
445 TDesC& CPcsCache::GetURI() |
|
446 { |
|
447 return (*iURI); |
|
448 } |
|
449 |
|
450 // --------------------------------------------------------------------- |
|
451 // CPcsCache::SetDataFields |
|
452 // |
|
453 // --------------------------------------------------------------------- |
|
454 void CPcsCache::SetDataFields(RArray<TInt>& aDataFields) |
|
455 { |
|
456 for (TInt i(0); i < aDataFields.Count(); i++) |
|
457 { |
|
458 iDataFields.Append(aDataFields[i]); |
|
459 } |
|
460 } |
|
461 |
|
462 // --------------------------------------------------------------------- |
|
463 // CPcsCache::GetDataFields |
|
464 // |
|
465 // --------------------------------------------------------------------- |
|
466 void CPcsCache::GetDataFields(RArray<TInt>& aDataFields) |
|
467 { |
|
468 for (TInt i(0); i < iDataFields.Count(); i++) |
|
469 { |
|
470 aDataFields.Append(iDataFields[i]); |
|
471 } |
|
472 } |
|
473 |
|
474 // --------------------------------------------------------------------- |
|
475 // CPcsCache::UpdateCacheStatus |
|
476 // |
|
477 // --------------------------------------------------------------------- |
|
478 void CPcsCache::UpdateCacheStatus(TInt aStatus) |
|
479 { |
|
480 iCacheStatus = aStatus; |
|
481 } |
|
482 |
|
483 // --------------------------------------------------------------------- |
|
484 // CPcsCache::GetCacheStatus |
|
485 // |
|
486 // --------------------------------------------------------------------- |
|
487 TInt CPcsCache::GetCacheStatus() |
|
488 { |
|
489 return iCacheStatus; |
|
490 } |
|
491 |
|
492 // --------------------------------------------------------------------- |
|
493 // CPcsCache::GetUriId |
|
494 // |
|
495 // --------------------------------------------------------------------- |
|
496 TUint8 CPcsCache::GetUriId() |
|
497 { |
|
498 return iUriId; |
|
499 } |
|
500 |
|
501 // --------------------------------------------------------------------- |
|
502 // CPcsCache::GetUri |
|
503 // |
|
504 // --------------------------------------------------------------------- |
|
505 HBufC* CPcsCache::GetUri() |
|
506 { |
|
507 return iURI; |
|
508 } |
|
509 |
|
510 // --------------------------------------------------------------------- |
|
511 // CPcsCache::SetSortOrder |
|
512 // |
|
513 // --------------------------------------------------------------------- |
|
514 void CPcsCache::SetSortOrder(RArray<TInt>& aSortOrder) |
|
515 { |
|
516 PRINT ( _L("Enter CPcsCache::SetSortOrder") ); |
|
517 |
|
518 iSortOrder.Reset(); |
|
519 |
|
520 for (TInt i(0); i < aSortOrder.Count(); i++) |
|
521 { |
|
522 iSortOrder.Append(aSortOrder[i]); |
|
523 } |
|
524 |
|
525 ComputeIndexOrder(); |
|
526 |
|
527 PRINT ( _L("End CPcsCache::SetSortOrder") ); |
|
528 } |
|
529 |
|
530 // --------------------------------------------------------------------- |
|
531 // CPcsCache::GetSortOrder |
|
532 // |
|
533 // --------------------------------------------------------------------- |
|
534 void CPcsCache::GetSortOrder(RArray<TInt>& aSortOrder) |
|
535 { |
|
536 aSortOrder.Reset(); |
|
537 |
|
538 for (TInt i(0); i < iSortOrder.Count(); i++) |
|
539 { |
|
540 aSortOrder.Append(iSortOrder[i]); |
|
541 } |
|
542 } |
|
543 |
|
544 |
|
545 // --------------------------------------------------------------------- |
|
546 // CPcsCache::GetIndexOrder |
|
547 // |
|
548 // --------------------------------------------------------------------- |
|
549 void CPcsCache::GetIndexOrder(RArray<TInt>& aIndexOrder) |
|
550 { |
|
551 aIndexOrder.Reset(); |
|
552 |
|
553 for (TInt i(0); i < iIndexOrder.Count(); i++) |
|
554 { |
|
555 aIndexOrder.Append(iIndexOrder[i]); |
|
556 } |
|
557 } |
|
558 |
|
559 // --------------------------------------------------------------------- |
|
560 // CPcsCache::ComputeIndexOrder |
|
561 // |
|
562 // --------------------------------------------------------------------- |
|
563 void CPcsCache::ComputeIndexOrder() |
|
564 { |
|
565 iIndexOrder.Reset(); |
|
566 |
|
567 for ( int i = 0; i < iSortOrder.Count(); i++ ) |
|
568 { |
|
569 for ( int j = 0; j < iDataFields.Count(); j++ ) |
|
570 { |
|
571 if ( iSortOrder[i] == iDataFields[j] ) |
|
572 { |
|
573 iIndexOrder.Append(j); |
|
574 break; |
|
575 } |
|
576 } |
|
577 } |
|
578 } |
|
579 |
|
580 // --------------------------------------------------------------------- |
|
581 // CPcsCache::ResortdataInPools |
|
582 // |
|
583 // --------------------------------------------------------------------- |
|
584 void CPcsCache::ResortdataInPoolsL() |
|
585 { |
|
586 // copy masterPool data into masterPoolBackup |
|
587 for (TInt i = 0; i < masterPool.Count(); i++ ) |
|
588 { |
|
589 masterPoolBackup.Append( masterPool[i] ); |
|
590 } |
|
591 //Now reset the key array |
|
592 for (TInt i = 0; i < keyArr.Count(); i++ ) |
|
593 { |
|
594 keyArr[i]->ResetAndDestroy(); |
|
595 } |
|
596 masterPool.Reset(); |
|
597 cacheInfo.Close(); |
|
598 //now add data again from the masterPoolBackup |
|
599 for (TInt i = 0; i < masterPoolBackup.Count(); i++ ) |
|
600 { |
|
601 CPsData* temp = static_cast<CPsData*>(masterPoolBackup[i]); |
|
602 AddToCacheL( *temp ); |
|
603 } |
|
604 masterPoolBackup.Reset(); |
|
605 } |
|
606 // End of file |