|
1 /* |
|
2 * Copyright (c) 2005 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: Contact Database observer |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 // INCLUDE FILES |
|
21 #include <e32std.h> |
|
22 #include "CPEngListObserver.h" |
|
23 |
|
24 #include "CPEngNWSessionSlotID2.h" |
|
25 #include "CPEngContactListNotifier2.h" |
|
26 #include "CPEngContactListStore2.h" |
|
27 #include "MPEngContactList2.h" |
|
28 #include "MPEngContactListProperties.h" |
|
29 #include "MPEngContactItem.h" |
|
30 #include "PEngWVContactListExtensions2.h" |
|
31 |
|
32 #include "MPEngContactMapper.h" |
|
33 |
|
34 |
|
35 // LOCAL CLASS DEFINITION |
|
36 NONSHARABLE_CLASS( TPEngContactListNameWrapper ) : public MDesC16Array |
|
37 { |
|
38 public: |
|
39 TPEngContactListNameWrapper( |
|
40 RPointerArray<MPEngContactList2>& aContactLists ) |
|
41 : iContactLists( aContactLists ) |
|
42 { |
|
43 } |
|
44 public: // from MDesC16Array |
|
45 TInt MdcaCount() const |
|
46 { |
|
47 return iContactLists.Count(); |
|
48 } |
|
49 |
|
50 TPtrC16 MdcaPoint( TInt aIndex ) const |
|
51 { |
|
52 return iContactLists[ aIndex ]->ListProperties().Name(); |
|
53 } |
|
54 |
|
55 public: // data |
|
56 RPointerArray<MPEngContactList2>& iContactLists; |
|
57 }; |
|
58 |
|
59 // ============================ MEMBER FUNCTIONS =============================== |
|
60 |
|
61 // ----------------------------------------------------------------------------- |
|
62 // CPEngListObserver::CPEngListObserver |
|
63 // C++ default constructor can NOT contain any code, that |
|
64 // might leave. |
|
65 // ----------------------------------------------------------------------------- |
|
66 // |
|
67 CPEngListObserver::CPEngListObserver( |
|
68 MPEngContactMapper& aMapper ) |
|
69 : iContactMapper( aMapper ) |
|
70 |
|
71 { |
|
72 } |
|
73 |
|
74 // ----------------------------------------------------------------------------- |
|
75 // CPEngListObserver::ConstructL |
|
76 // Symbian 2nd phase constructor can leave. |
|
77 // ----------------------------------------------------------------------------- |
|
78 // |
|
79 void CPEngListObserver::ConstructL( |
|
80 const CPEngNWSessionSlotID2& aSessionId ) |
|
81 { |
|
82 iSessionId = aSessionId.CloneL(); |
|
83 iListStore = CPEngContactListStore2::NewL( aSessionId ); |
|
84 iListNotifier = CPEngContactListNotifier2::NewL( aSessionId ); |
|
85 User::LeaveIfError( iListNotifier->AddObserver( *this ) ); |
|
86 RefreshSupportedListsL(); |
|
87 } |
|
88 |
|
89 // ----------------------------------------------------------------------------- |
|
90 // CPEngListObserver::NewLC |
|
91 // Two-phased constructor. |
|
92 // ----------------------------------------------------------------------------- |
|
93 // |
|
94 CPEngListObserver* CPEngListObserver::NewLC( |
|
95 MPEngContactMapper& aMapper, |
|
96 const CPEngNWSessionSlotID2& aSessionId ) |
|
97 |
|
98 { |
|
99 CPEngListObserver* self = new( ELeave ) CPEngListObserver( aMapper ); |
|
100 |
|
101 CleanupStack::PushL( self ); |
|
102 self->ConstructL( aSessionId ); |
|
103 |
|
104 return self; |
|
105 } |
|
106 |
|
107 // Destructor |
|
108 CPEngListObserver::~CPEngListObserver() |
|
109 { |
|
110 iContactLists.Reset(); |
|
111 delete iListNotifier; |
|
112 delete iListStore; |
|
113 delete iSessionId; |
|
114 } |
|
115 |
|
116 // ============================================================================= |
|
117 // ===============Functions of MPEngContactListObserver2 class ======================== |
|
118 // ============================================================================= |
|
119 // ----------------------------------------------------------------------------- |
|
120 |
|
121 // ----------------------------------------------------------------------------- |
|
122 // CPEngListObserver::HandleContactListChangeL() |
|
123 // Contact list change handler |
|
124 // (other items were commented in a header). |
|
125 // ----------------------------------------------------------------------------- |
|
126 // |
|
127 void CPEngListObserver::HandleContactListChangeL( |
|
128 CPEngContactListNotifier2& /* aNotifier */, |
|
129 const TDesC& aContactListName ) |
|
130 { |
|
131 // was it contact list store changed? |
|
132 if ( aContactListName == KNullDesC ) |
|
133 { |
|
134 RefreshSupportedListsL(); |
|
135 return; |
|
136 } |
|
137 RemapContactListL( ContactListL( aContactListName ) ); |
|
138 } |
|
139 |
|
140 |
|
141 // ----------------------------------------------------------------------------- |
|
142 // CPEngListObserver::HandleContactListError() |
|
143 // Notification failure handler. |
|
144 // (other items were commented in a header). |
|
145 // ----------------------------------------------------------------------------- |
|
146 // |
|
147 void CPEngListObserver::HandleContactListError( |
|
148 TInt /*aError*/, |
|
149 CPEngContactListNotifier2& /*aNotifier*/ ) |
|
150 { |
|
151 // no error handling |
|
152 } |
|
153 |
|
154 // ============================================================================= |
|
155 // ===============Functions of the base class ================================== |
|
156 // ============================================================================= |
|
157 |
|
158 // ----------------------------------------------------------------------------- |
|
159 // CPEngListObserver::UpdateMappingL() |
|
160 // Update mapping for gived WV Id |
|
161 // (other items were commented in a header). |
|
162 // ----------------------------------------------------------------------------- |
|
163 // |
|
164 void CPEngListObserver::UpdateMappingL( |
|
165 const TDesC& aWVId, |
|
166 const CContactIdArray* aContactIds ) |
|
167 { |
|
168 TInt count( iContactLists.Count() ); |
|
169 for ( TInt x( 0 ) ; x < count ; ++x ) |
|
170 { |
|
171 MPEngContactList2& contactList = *( iContactLists[ x ] ); |
|
172 TInt index( contactList.FindContact( aWVId ) ); |
|
173 if ( index != KErrNotFound ) |
|
174 { |
|
175 RemapContactItemL( contactList.ContactItem( index ), aContactIds ); |
|
176 } |
|
177 } |
|
178 } |
|
179 |
|
180 // ----------------------------------------------------------------------------- |
|
181 // CPEngListObserver::ReMappAllContactsL() |
|
182 // Do full re-mapping of all contact list |
|
183 // (other items were commented in a header). |
|
184 // ----------------------------------------------------------------------------- |
|
185 // |
|
186 void CPEngListObserver::ReMappAllContactsL() |
|
187 { |
|
188 TInt count( iContactLists.Count() ); |
|
189 for ( TInt x( 0 ) ; x < count ; ++x ) |
|
190 { |
|
191 RemapContactListL( *iContactLists[ x ], ETrue ); |
|
192 } |
|
193 } |
|
194 |
|
195 // ----------------------------------------------------------------------------- |
|
196 // CPEngListObserver::SessionId() |
|
197 // Session slot Id |
|
198 // (other items were commented in a header). |
|
199 // ----------------------------------------------------------------------------- |
|
200 // |
|
201 const CPEngNWSessionSlotID2& CPEngListObserver::SessionId() const |
|
202 { |
|
203 return *iSessionId; |
|
204 } |
|
205 |
|
206 // ----------------------------------------------------------------------------- |
|
207 // CPEngListObserver::RefreshSupportedListsL |
|
208 // Build list of supported contact lists |
|
209 // (other items were commented in a header). |
|
210 // ----------------------------------------------------------------------------- |
|
211 // |
|
212 void CPEngListObserver::RefreshSupportedListsL() |
|
213 { |
|
214 iContactLists.Reset(); |
|
215 iListNotifier->Stop(); |
|
216 const MDesC16Array& lists = iListStore->AllContactListsL(); |
|
217 TInt count( lists.MdcaCount() ); |
|
218 for ( TInt x( 0 ) ; x < count ; ++x ) |
|
219 { |
|
220 if ( EFalse != iListStore->ContactListPropertiesL( |
|
221 lists.MdcaPoint( x ) |
|
222 ).Property( |
|
223 KPEngContactToContactDbMapping, |
|
224 KPEngCntLstPropertyExternalPermanent ) |
|
225 ) |
|
226 { |
|
227 MPEngContactList2& contactList = iListStore->ContactListL( |
|
228 lists.MdcaPoint( x ) ); |
|
229 iContactLists.AppendL( &contactList ); |
|
230 RemapContactListL( contactList ); |
|
231 } |
|
232 } |
|
233 TPEngContactListNameWrapper listNames( iContactLists ); |
|
234 User::LeaveIfError( iListNotifier->Start( listNames ) ); |
|
235 } |
|
236 |
|
237 // ----------------------------------------------------------------------------- |
|
238 // CPEngListObserver::ContactListL |
|
239 // Find Contact list |
|
240 // (other items were commented in a header). |
|
241 // ----------------------------------------------------------------------------- |
|
242 // |
|
243 MPEngContactList2& CPEngListObserver::ContactListL( |
|
244 const TDesC& aContactList ) |
|
245 { |
|
246 TInt count( iContactLists.Count() ); |
|
247 for ( TInt x( 0 ) ; x < count ; ++x ) |
|
248 { |
|
249 if ( KErrNone |
|
250 == |
|
251 aContactList.CompareF( |
|
252 iContactLists[ x ]->ListProperties().Name() ) |
|
253 ) |
|
254 { |
|
255 return *( iContactLists[ x ] ); |
|
256 } |
|
257 } |
|
258 User::Leave( KErrNotFound ); |
|
259 // just for compiler |
|
260 return *( iContactLists[ 0 ] ); |
|
261 } |
|
262 |
|
263 |
|
264 // ----------------------------------------------------------------------------- |
|
265 // CPEngListObserver::RemapContactListL |
|
266 // Remap whole contact list |
|
267 // (other items were commented in a header). |
|
268 // ----------------------------------------------------------------------------- |
|
269 // |
|
270 void CPEngListObserver::RemapContactListL( |
|
271 MPEngContactList2& aContactList, |
|
272 TBool aForceUpdate /* EFalse */ ) |
|
273 { |
|
274 aContactList.SuspendUpdateNotification(); |
|
275 TInt count ( aContactList.Count() ); |
|
276 for ( TInt x( 0 ) ; x < count ; ++x ) |
|
277 { |
|
278 MPEngContactItem& contact = aContactList.ContactItem( x ); |
|
279 TPtrC8 value; |
|
280 if ( aForceUpdate |
|
281 || |
|
282 ( KErrNotFound == contact.GetProperty( |
|
283 KPEngCntItemExtensionMappedContactDbIds, |
|
284 KPEngCntItemPropertyExternalCached, |
|
285 value ) ) |
|
286 ) |
|
287 { |
|
288 // not mapped yet, map |
|
289 CContactIdArray* contactIds = |
|
290 iContactMapper.MapWvIdToContactIdLC( contact.Id() ); |
|
291 RemapContactItemL( contact, contactIds ); |
|
292 CleanupStack::PopAndDestroy( contactIds ); |
|
293 } |
|
294 } |
|
295 aContactList.ReleaseUpdateNotification(); |
|
296 } |
|
297 |
|
298 // ----------------------------------------------------------------------------- |
|
299 // CPEngListObserver::RemapContactListL |
|
300 // Remap one Contact Item with given contact db ids |
|
301 // (other items were commented in a header). |
|
302 // ----------------------------------------------------------------------------- |
|
303 // |
|
304 void CPEngListObserver::RemapContactItemL( |
|
305 MPEngContactItem& aContactItem, |
|
306 const CContactIdArray* aContactIds ) |
|
307 { |
|
308 if ( aContactIds ) |
|
309 { |
|
310 HBufC8* packedIds = |
|
311 PEngContactItemExtensionCodec2::PackContactIdsLC( |
|
312 *aContactIds ); |
|
313 aContactItem.SetPropertyL( KPEngCntItemExtensionMappedContactDbIds, |
|
314 KPEngCntItemPropertyExternalCached, |
|
315 *packedIds ); |
|
316 |
|
317 CleanupStack::PopAndDestroy( packedIds ); |
|
318 } |
|
319 else |
|
320 { |
|
321 aContactItem.SetPropertyL( KPEngCntItemExtensionMappedContactDbIds, |
|
322 KPEngCntItemPropertyExternalCached, |
|
323 KNullDesC8 ); |
|
324 } |
|
325 aContactItem.DeletePropertyL( KPEngCntItemExtensionMappingCleaning, |
|
326 KPEngCntItemPropertyExternalCached ); |
|
327 } |
|
328 |
|
329 // End of File |