|
1 // Copyright (c) 2006-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 |
|
17 #include <cntdb.h> |
|
18 #include "pbapserver.h" |
|
19 #include "pbappbhandlecache.h" |
|
20 #include "btaccesshostlog.h" |
|
21 |
|
22 |
|
23 /*static*/ CPbapPbHandleCache* CPbapPbHandleCache::NewL(CContactDatabase& aDatabase, MPbHandleCacheObserver& aObserver) |
|
24 { |
|
25 LOG_STATIC_FUNC |
|
26 CPbapPbHandleCache* self=new(ELeave) CPbapPbHandleCache(aDatabase, aObserver); |
|
27 CleanupStack::PushL(self); |
|
28 self->ConstructL(); |
|
29 CleanupStack::Pop(self); |
|
30 return self; |
|
31 } |
|
32 |
|
33 |
|
34 CPbapPbHandleCache::CPbapPbHandleCache(CContactDatabase& aDatabase, MPbHandleCacheObserver& aObserver) |
|
35 : iDatabase(aDatabase), iObserver(aObserver) |
|
36 { |
|
37 LOG_FUNC |
|
38 } |
|
39 |
|
40 |
|
41 CPbapPbHandleCache::~CPbapPbHandleCache() |
|
42 { |
|
43 LOG_FUNC |
|
44 iHandleList.Close(); |
|
45 delete iChangeNotifier; |
|
46 } |
|
47 |
|
48 |
|
49 void CPbapPbHandleCache::ConstructL() |
|
50 { |
|
51 LOG_FUNC |
|
52 iChangeNotifier = CContactChangeNotifier::NewL(iDatabase, this); |
|
53 CreateHandleListL(); |
|
54 } |
|
55 |
|
56 |
|
57 TInt CPbapPbHandleCache::HandleAtL(TUint aIndex) const |
|
58 { |
|
59 LOG_FUNC |
|
60 if(aIndex >= Count()) |
|
61 { |
|
62 __ASSERT_DEBUG(EFalse, Panic(EPbapServerPanicIndexOutOfBoundry)); |
|
63 User::Leave(KErrOverflow); |
|
64 } |
|
65 return iHandleList[aIndex].iHandle; |
|
66 } |
|
67 |
|
68 TContactItemId CPbapPbHandleCache::ContactIdAtL(TUint aIndex) const |
|
69 { |
|
70 LOG_FUNC |
|
71 if(aIndex >= Count()) |
|
72 { |
|
73 __ASSERT_DEBUG(EFalse, Panic(EPbapServerPanicIndexOutOfBoundry)); |
|
74 User::Leave(KErrOverflow); |
|
75 } |
|
76 return iHandleList[aIndex].iContactId; |
|
77 } |
|
78 |
|
79 |
|
80 TInt CPbapPbHandleCache::FindHandle(TInt aHandle) const |
|
81 { |
|
82 LOG_FUNC |
|
83 // the array is ordered on handle so can use fast binary search |
|
84 TPbHandleAndId item(aHandle, KNullContactId); |
|
85 return iHandleList.FindInOrder(item, TLinearOrder<TPbHandleAndId>(CompareHandles)); |
|
86 } |
|
87 |
|
88 |
|
89 TInt CPbapPbHandleCache::FindContactId(TContactItemId aContactId) const |
|
90 { |
|
91 LOG_FUNC |
|
92 // slow sequential search of array required for contact ids |
|
93 TPbHandleAndId item(KErrNotFound, aContactId); |
|
94 return iHandleList.Find(item, TIdentityRelation<TPbHandleAndId>(MatchContactIds)); |
|
95 } |
|
96 |
|
97 |
|
98 /*static*/ |
|
99 TInt CPbapPbHandleCache::CompareHandles(const TPbHandleAndId& aFirst, const TPbHandleAndId& aSecond) |
|
100 { |
|
101 LOG_STATIC_FUNC |
|
102 return (aFirst.iHandle - aSecond.iHandle); |
|
103 } |
|
104 |
|
105 /*static*/ |
|
106 TBool CPbapPbHandleCache::MatchContactIds(const TPbHandleAndId& aFirst, const TPbHandleAndId& aSecond) |
|
107 { |
|
108 LOG_STATIC_FUNC |
|
109 return (aFirst.iContactId == aSecond.iContactId); |
|
110 } |
|
111 |
|
112 void CPbapPbHandleCache::CreateHandleListL() |
|
113 { |
|
114 LOG_FUNC |
|
115 |
|
116 iHandleList.Reset(); |
|
117 |
|
118 // add own card handle item |
|
119 TContactItemId ownCardId = iDatabase.OwnCardId(); |
|
120 TPbHandleAndId ownCardItem(KOwnCardHandle, ownCardId); |
|
121 iHandleList.AppendL(ownCardItem); |
|
122 |
|
123 // only include contact cards and the own card when building list |
|
124 iDatabase.SetDbViewContactType(KUidContactCard); |
|
125 |
|
126 // iterate through database building sorted list of handles |
|
127 // (there should be a faster way of getting all the ids in the database, |
|
128 // calling CContactDatabase::SortedItemsL() without a sort order used to |
|
129 // be the quickest method but that has been deprecated) |
|
130 TContactIter iter(iDatabase); |
|
131 TContactItemId contactId = iter.FirstL(); |
|
132 TInt handle = 1; |
|
133 while (contactId != KNullContactId) |
|
134 { |
|
135 if (contactId != ownCardId) |
|
136 { |
|
137 TPbHandleAndId item(handle, contactId); |
|
138 iHandleList.AppendL(item); |
|
139 ++handle; // increment handle |
|
140 } |
|
141 contactId = iter.NextL(); |
|
142 } |
|
143 } |
|
144 |
|
145 |
|
146 void CPbapPbHandleCache::HandleDatabaseEventL(TContactDbObserverEvent aEvent) |
|
147 { |
|
148 LOG_FUNC |
|
149 switch(aEvent.iType) |
|
150 { |
|
151 case EContactDbObserverEventContactChanged: |
|
152 { |
|
153 // notify observer that a contact in the cache has changed |
|
154 iObserver.HandleCacheChanged(); |
|
155 } |
|
156 break; |
|
157 case EContactDbObserverEventContactAdded: |
|
158 { |
|
159 // add contact id to cache (ignoring insert errors) and notify observer |
|
160 HandleContactAddedL(aEvent.iContactId); |
|
161 iObserver.HandleCacheChanged(); |
|
162 } |
|
163 break; |
|
164 case EContactDbObserverEventContactDeleted: |
|
165 { |
|
166 // remove contact id from cache and notify observer |
|
167 HandleContactDeleted(aEvent.iContactId); |
|
168 iObserver.HandleCacheChanged(); |
|
169 } |
|
170 break; |
|
171 case EContactDbObserverEventOwnCardChanged: |
|
172 { |
|
173 // the own card has been modified or another contact |
|
174 // has been assigned as the own card |
|
175 HandleOwnCardChangedL(); |
|
176 iObserver.HandleCacheChanged(); |
|
177 } |
|
178 break; |
|
179 case EContactDbObserverEventOwnCardDeleted: |
|
180 { |
|
181 // reset own card id back to KNullContactId |
|
182 HandleOwnCardDeleted(); |
|
183 iObserver.HandleCacheChanged(); |
|
184 } |
|
185 break; |
|
186 case EContactDbObserverEventUnknownChanges: |
|
187 { |
|
188 // the database has changed but we do not know which |
|
189 // contacts were affected, only choice is to rebuild handles cache |
|
190 #ifdef _DEBUG |
|
191 TRAPD(error, CreateHandleListL()); |
|
192 if(error != KErrNone) |
|
193 { |
|
194 __ASSERT_DEBUG(EFalse, Panic(EPbapServerPanicUnexpectedError)); |
|
195 User::Leave(error); |
|
196 } |
|
197 #else |
|
198 TRAP_IGNORE(CreateHandleListL()); |
|
199 #endif // _DEBUG |
|
200 iObserver.HandleCacheChanged(); |
|
201 } |
|
202 break; |
|
203 default: |
|
204 break; |
|
205 } |
|
206 } |
|
207 |
|
208 |
|
209 void CPbapPbHandleCache::HandleContactAddedL(TContactItemId aContactId) |
|
210 { |
|
211 LOG_FUNC |
|
212 TPbHandleAndId item(NextAvailableHandleL(), aContactId); |
|
213 iHandleList.AppendL(item); |
|
214 } |
|
215 |
|
216 |
|
217 void CPbapPbHandleCache::HandleContactDeleted(TContactItemId aContactId) |
|
218 { |
|
219 LOG_FUNC |
|
220 TInt index= FindContactId(aContactId); |
|
221 if (index != KErrNotFound) |
|
222 { |
|
223 iHandleList.Remove(index); |
|
224 } |
|
225 } |
|
226 |
|
227 |
|
228 void CPbapPbHandleCache::HandleOwnCardChangedL() |
|
229 { |
|
230 LOG_FUNC |
|
231 TContactItemId ownCardId=iDatabase.OwnCardId(); |
|
232 TInt ownCardItemIndex = FindHandle(KOwnCardHandle); // always present |
|
233 if(ownCardItemIndex == KErrNotFound) |
|
234 { |
|
235 __ASSERT_DEBUG(EFalse, Panic(EPbapServerPanicOwnCardNotFound)); |
|
236 User::Leave(KErrNotFound); |
|
237 } |
|
238 TContactItemId currentOwnCardId = ContactIdAtL(ownCardItemIndex); |
|
239 if (ownCardId != currentOwnCardId) |
|
240 { |
|
241 if (currentOwnCardId != KNullContactId) |
|
242 { |
|
243 // current own card now just another contact so assign to a new handle |
|
244 TPbHandleAndId item(NextAvailableHandleL(), currentOwnCardId); |
|
245 iHandleList.Append(item); // ignore the append error |
|
246 } |
|
247 |
|
248 // the new own card may already exist in the handle list, if so |
|
249 // remove it |
|
250 if (ownCardId != KNullContactId) |
|
251 { |
|
252 TInt index = FindContactId(ownCardId); |
|
253 if (index != KErrNotFound) |
|
254 { |
|
255 iHandleList.Remove(index); |
|
256 } |
|
257 } |
|
258 |
|
259 // replace contact id associated with own card handle |
|
260 ownCardItemIndex = FindHandle(KOwnCardHandle); |
|
261 iHandleList[ownCardItemIndex].iContactId = ownCardId; |
|
262 } |
|
263 } |
|
264 |
|
265 |
|
266 void CPbapPbHandleCache::HandleOwnCardDeleted() |
|
267 { |
|
268 LOG_FUNC |
|
269 // reset own card id to null id |
|
270 TInt ownCardItemIndex = FindHandle(KOwnCardHandle); |
|
271 iHandleList[ownCardItemIndex].iContactId = KNullContactId; |
|
272 } |
|
273 |
|
274 |
|
275 TInt CPbapPbHandleCache::NextAvailableHandleL() const |
|
276 { |
|
277 LOG_FUNC |
|
278 TInt lastHandle = HandleAtL(iHandleList.Count()-1); |
|
279 return (lastHandle + 1); // just increment handle |
|
280 } |