|
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 the License "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: Implementation of the add contact observer functionality. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 //User include |
|
20 #include "addcontactobserver.h" |
|
21 #include "contactcallback.h" |
|
22 |
|
23 //System include |
|
24 #include "contactservice.h"// for iContactService |
|
25 #include <mvpbkcontactstore.h>// for iContactStore |
|
26 #include <mvpbkstorecontact.h>// used in many function to get the store contact |
|
27 #include <mvpbkcontactoperationbase.h>//for observer |
|
28 #include <mvpbkcontactlink.h>//for param in ContactsSaved() |
|
29 #include <MVPbkContactGroup.h> |
|
30 #include <MVPbkContactFieldTextData.h> |
|
31 #include <MVPbkContactFieldData.h> |
|
32 #include <MVPbkContactFieldBinaryData.h> |
|
33 #include <MVPbkContactFieldDateTimeData.h> |
|
34 |
|
35 /* |
|
36 ------------------------------------------------------------------------------------- |
|
37 CAddContactObserver::NewL( CContactService* aContactService, |
|
38 MVPbkContactStore* aContactStore, |
|
39 CSingleContact* aStoreContact, |
|
40 const TDesC8& aGroupId, |
|
41 const TDesC& aGroupLabel, |
|
42 TInt aTransId, |
|
43 MContactCallback* aCallback) |
|
44 Description : Static method to create instance of add contact observer. |
|
45 Return values : CAddContactObserver* |
|
46 ------------------------------------------------------------------------------------- |
|
47 */ |
|
48 CAddContactObserver* CAddContactObserver::NewL(CContactService* aContactService, |
|
49 MVPbkContactStore* aContactStore, |
|
50 CSingleContact* aStoreContact, |
|
51 const TDesC8& aGroupId, |
|
52 const TDesC& aGroupLabel, |
|
53 TInt aTransId, |
|
54 MContactCallback* aCallback) |
|
55 { |
|
56 |
|
57 CAddContactObserver* self = new( ELeave ) |
|
58 CAddContactObserver(aContactService, |
|
59 aContactStore, |
|
60 aStoreContact, |
|
61 aTransId, |
|
62 aCallback); |
|
63 |
|
64 |
|
65 CleanupStack::PushL(self); |
|
66 self->ConstructL(aGroupId,aGroupLabel); |
|
67 CleanupStack::Pop(self); |
|
68 return self; |
|
69 } |
|
70 |
|
71 /* |
|
72 ------------------------------------------------------------------------------------- |
|
73 CAddContactObserver::CAddContactObserver(CVPbkContactManager* aContactManager, |
|
74 MVPbkContactStore* aContactStore, |
|
75 CSingleContact* aStoreContact, |
|
76 TInt aTransId, |
|
77 MContactCallback* aCallback) |
|
78 Description : This is the default constructor |
|
79 Return values : N/A |
|
80 ------------------------------------------------------------------------------------- |
|
81 */ |
|
82 |
|
83 CAddContactObserver::CAddContactObserver( CContactService* aContactService, |
|
84 MVPbkContactStore* aContactStore, |
|
85 CSingleContact* aStoreContact, |
|
86 TInt aTransId, |
|
87 MContactCallback* aCallback ) |
|
88 { |
|
89 iContactService = aContactService; |
|
90 iContactStore = aContactStore; |
|
91 iStoreContact = aStoreContact; |
|
92 iGroupId = NULL; |
|
93 iGroupLabel = NULL; |
|
94 iTransId = aTransId; |
|
95 iCallback = aCallback; |
|
96 iLockFlag = EFalse; |
|
97 iContact = NULL; |
|
98 iGroup = NULL; |
|
99 iOp = NULL; |
|
100 iErrorID = 0; |
|
101 } |
|
102 |
|
103 void CAddContactObserver::ConstructL(const TDesC8& aGroupId, |
|
104 const TDesC& aGroupLabel) |
|
105 { |
|
106 iGroupId = HBufC8 :: NewL(aGroupId.Length()); |
|
107 *iGroupId = aGroupId; |
|
108 |
|
109 iGroupLabel = HBufC :: NewL(aGroupLabel.Length()); |
|
110 *iGroupLabel = aGroupLabel; |
|
111 } |
|
112 |
|
113 |
|
114 /* |
|
115 ------------------------------------------------------------------------------------- |
|
116 CAddContactObserver::~CAddContactObserver() |
|
117 Description : Destructor |
|
118 Return values : N/A |
|
119 ------------------------------------------------------------------------------------- |
|
120 */ |
|
121 |
|
122 CAddContactObserver::~CAddContactObserver() |
|
123 { |
|
124 delete iGroupId; |
|
125 delete iGroupLabel; |
|
126 delete iContact; |
|
127 delete iGroup; |
|
128 if(iOp) |
|
129 { |
|
130 delete iOp; |
|
131 } |
|
132 |
|
133 if(iContactStore) |
|
134 { |
|
135 iContactStore->Close(*this); |
|
136 } |
|
137 } |
|
138 |
|
139 /* |
|
140 ------------------------------------------------------------------------------------- |
|
141 CAddContactObserver::Cancel() |
|
142 Description : Cancel implementation relevant to AddObserver |
|
143 Return values : KErrorNone on Success and KErrGeneral on Failure |
|
144 ------------------------------------------------------------------------------------- |
|
145 */ |
|
146 |
|
147 void CAddContactObserver::Cancel() |
|
148 { |
|
149 iCallback->HandleReturnValue(EOpCancel, KErrNone, iTransId); |
|
150 delete this; |
|
151 } |
|
152 |
|
153 /* |
|
154 ------------------------------------------------------------------------------------- |
|
155 CAddContactObserver::CreateAndAddFieldToContactL() |
|
156 Description : This method creates the contact field and adds to the data base. |
|
157 Return values : N/A |
|
158 ------------------------------------------------------------------------------------- |
|
159 */ |
|
160 void CAddContactObserver::CreateAndAddFieldToContactL() |
|
161 { |
|
162 TPtrC8 fieldKey(KNullDesC8); |
|
163 TPtrC label(KNullDesC); |
|
164 TPtrC value(KNullDesC); |
|
165 iErrorID = 0; |
|
166 |
|
167 TInt fieldCount = iStoreContact->FieldCount(); |
|
168 const CVPbkContactManager* contactManager = &(iContactService->GetContactManager()); |
|
169 for(TInt index = 0; index < fieldCount; index++) |
|
170 { |
|
171 CSingleContactField* field = iStoreContact->FieldAt(index); |
|
172 |
|
173 field->GetFieldDataL(fieldKey, label, value); |
|
174 TInt fieldTypeResId = CSearchFields::GetIdFromFieldKey(fieldKey); |
|
175 iErrorID = fieldTypeResId; |
|
176 MVPbkStoreContactField* cntField = |
|
177 iContact->CreateFieldLC( * contactManager->FieldTypes().Find( fieldTypeResId ) ); |
|
178 |
|
179 iErrorID = 0; |
|
180 |
|
181 TVPbkFieldStorageType storageType = cntField->FieldData().DataType(); |
|
182 |
|
183 if( EVPbkFieldStorageTypeBinary == storageType ) |
|
184 { |
|
185 //(MVPbkContactFieldBinaryData::Cast(cntField->FieldData())).SetBinaryDataL( value8bit ); |
|
186 } |
|
187 else if( EVPbkFieldStorageTypeDateTime == storageType ) |
|
188 { |
|
189 TTime timeVal = field->GetDateTime(); |
|
190 (MVPbkContactFieldDateTimeData::Cast(cntField->FieldData())).SetDateTime( timeVal ); |
|
191 } |
|
192 else |
|
193 { |
|
194 (MVPbkContactFieldTextData::Cast(cntField->FieldData())).SetTextL( value ); |
|
195 } |
|
196 |
|
197 TRAP_IGNORE(cntField->SetFieldLabelL(label)); |
|
198 |
|
199 iContact->AddFieldL(cntField); |
|
200 |
|
201 CleanupStack::Pop(); //cntField |
|
202 } |
|
203 } |
|
204 |
|
205 |
|
206 /* |
|
207 ------------------------------------------------------------------------------------- |
|
208 CAddContactObserver::DoAddL() |
|
209 Description : Called for 1) Adding a new contact/group to the contacts database. |
|
210 2) Editing a contact/group from the contacts database. |
|
211 Return values : N/A |
|
212 ------------------------------------------------------------------------------------- |
|
213 */ |
|
214 void CAddContactObserver::DoAddL() |
|
215 { |
|
216 //if iStoreContact is non-null pointer then it is a new contact or existing contact. |
|
217 if( iStoreContact ) |
|
218 { |
|
219 //get contact id from the contact. |
|
220 TPtrC8 cntId = iStoreContact->GetContactId(); |
|
221 //If cntId is null then it is new contact, get the fields from the CSingleContact |
|
222 //create new contact, add the fields to it and commit the contact. |
|
223 if( cntId == KNullDesC8 ) |
|
224 { |
|
225 iContact = iContactStore->CreateNewContactLC(); |
|
226 CleanupStack::Pop(); //contact |
|
227 |
|
228 CreateAndAddFieldToContactL(); |
|
229 |
|
230 iContact->CommitL(*this); |
|
231 |
|
232 } |
|
233 else //Edit contact : retrieve contact and update the fields and commit. |
|
234 { |
|
235 const CVPbkContactManager* contactManager = &(iContactService->GetContactManager()); |
|
236 MVPbkContactLinkArray* linkArray = contactManager->CreateLinksLC(cntId); |
|
237 const MVPbkContactLink* link = &(linkArray->At(0)); |
|
238 CleanupStack::Pop(); //linkArray |
|
239 MVPbkContactOperationBase* iOp =contactManager->RetrieveContactL(*link, *this); |
|
240 } |
|
241 } |
|
242 //group id is not null and grp label is not null then exiting grp, |
|
243 //retrieve the grp,and edit the label. |
|
244 else if( *iGroupId != KNullDesC8 && *iGroupLabel != KNullDesC ) |
|
245 { |
|
246 const CVPbkContactManager* contactManager = &(iContactService->GetContactManager()); |
|
247 MVPbkContactLinkArray* linkArray = contactManager->CreateLinksLC(*iGroupId); |
|
248 const MVPbkContactLink* link = &(linkArray->At(0)); |
|
249 CleanupStack::Pop(); //linkArray |
|
250 contactManager->RetrieveContactL(*link, *this); |
|
251 } |
|
252 //if grp id is null and grp label is non-null then, create new grp and set the label. |
|
253 else if( *iGroupId == KNullDesC8 && *iGroupLabel != KNullDesC ) |
|
254 { |
|
255 iGroup = iContactStore->CreateNewContactGroupLC(); |
|
256 iGroup->SetGroupLabelL(*iGroupLabel); |
|
257 iGroup->CommitL( *this ); |
|
258 CleanupStack::Pop(); //group |
|
259 } |
|
260 } |
|
261 |
|
262 /* |
|
263 ------------------------------------------------------------------------------------- |
|
264 CAddContactObserver::DoCommitL() |
|
265 Description : Commit contact to the contacts database. |
|
266 Return values : TInt |
|
267 ------------------------------------------------------------------------------------- |
|
268 */ |
|
269 TInt CAddContactObserver::DoCommitL( TContactOpResult aResult ) |
|
270 { |
|
271 TInt retVal = KErrGeneral; |
|
272 |
|
273 //if lockflag is true, opcode is EContactLock, group id is not null |
|
274 //and grp label is not null then exiting grp, get ptr to grp from |
|
275 //contact and edit the label and commit the same. |
|
276 if( iLockFlag && |
|
277 aResult.iOpCode == EContactLock && |
|
278 ((*iGroupId != KNullDesC8) && (*iGroupLabel != KNullDesC)) ) |
|
279 { |
|
280 (iContact->Group())->SetGroupLabelL(*iGroupLabel); |
|
281 (iContact->Group())->CommitL(*this); |
|
282 //set lock flag to false, as it is done. |
|
283 iLockFlag = EFalse; |
|
284 //set return value to ErrNone...completed successfully. |
|
285 retVal = KErrNone; |
|
286 } |
|
287 //lock is success, so update the fields in the contact and commit. |
|
288 else if( iLockFlag && aResult.iOpCode == EContactLock ) |
|
289 { |
|
290 //remove all the fields from the original contact. |
|
291 iContact->RemoveAllFields(); |
|
292 |
|
293 //add new fields to the contact; |
|
294 CreateAndAddFieldToContactL( ); |
|
295 |
|
296 //added all the fields...commit the contact. |
|
297 iContact->CommitL( *this ); |
|
298 //set lock flag to false, as it is done. |
|
299 iLockFlag = EFalse; |
|
300 //set return value to ErrNone...completed successfully. |
|
301 retVal = KErrNone; |
|
302 } |
|
303 else if( aResult.iOpCode == EContactCommit ) |
|
304 { |
|
305 retVal = KErrNone; |
|
306 } |
|
307 |
|
308 return retVal; |
|
309 } |
|
310 |
|
311 /* |
|
312 ------------------------------------------------------------------------------------- |
|
313 void CAddContactObserver::DoEditContactOrGroupL(MVPbkStoreContact* aContact) |
|
314 Description : This method is used to edit the group,it locks |
|
315 the contact to be edited and performs the edit operation. |
|
316 Return values : N/A |
|
317 ------------------------------------------------------------------------------------- |
|
318 */ |
|
319 void CAddContactObserver::DoEditContactOrGroupL(MVPbkStoreContact* aContact) |
|
320 { |
|
321 //Edit contact : lock the contact for edit operation. |
|
322 iLockFlag = ETrue; |
|
323 aContact->LockL(*this); |
|
324 if(!iContact ) |
|
325 { |
|
326 iContact = aContact; |
|
327 } |
|
328 } |
|
329 |
|
330 |
|
331 /* |
|
332 ------------------------------------------------------------------------------------- |
|
333 CAddContactObserver::ContactOperationCompleted( TContactOpResult aResult ) |
|
334 Description : Virtual Phonebook Observer method called back once |
|
335 the contact operation is completed successfully. |
|
336 Return values : N/A |
|
337 ------------------------------------------------------------------------------------- |
|
338 */ |
|
339 void CAddContactObserver::ContactOperationCompleted( TContactOpResult aResult ) |
|
340 { |
|
341 //if lock operation is successfull then it enters here... |
|
342 //call DoCommitL(), to commit the contact into the contact store. |
|
343 TRAPD(error, error = DoCommitL(aResult)); |
|
344 |
|
345 //notify the user with the status of the operation. |
|
346 if((aResult.iOpCode != EContactLock) || (error != KErrNone)) |
|
347 { |
|
348 iContactService->RequestComplete(iTransId); |
|
349 iCallback->HandleReturnValue(EOpComplete, error, iTransId); |
|
350 delete this; |
|
351 } |
|
352 } |
|
353 |
|
354 /* |
|
355 ------------------------------------------------------------------------------------- |
|
356 CAddContactObserver::ContactOperationFailed(TContactOp aOpCode, |
|
357 TInt aErrorCode, |
|
358 TBool aErrorNotified) |
|
359 Description : Virtual Phonebook Observer method called back if |
|
360 the contact operation is Failed. |
|
361 Return values : N/A |
|
362 ------------------------------------------------------------------------------------- |
|
363 */ |
|
364 void CAddContactObserver::ContactOperationFailed(TContactOp /*aOpCode*/, |
|
365 TInt aErrorCode, |
|
366 TBool /*aErrorNotified*/) |
|
367 { |
|
368 //operation failed....handle the error |
|
369 iContactService->RequestComplete(iTransId); |
|
370 iCallback->HandleReturnValue(EOpError, aErrorCode, iTransId); |
|
371 delete this; |
|
372 } |
|
373 |
|
374 /* |
|
375 ------------------------------------------------------------------------------------- |
|
376 CAddContactObserver::ContactOperationFailed(TContactOp aOpCode, |
|
377 TInt aErrorCode, |
|
378 TBool aErrorNotified) |
|
379 Description : Virtual Phonebook Observer method called back when OpenL(), |
|
380 the database open operation is successfull. |
|
381 Return values : N/A |
|
382 ------------------------------------------------------------------------------------- |
|
383 */ |
|
384 void CAddContactObserver::StoreReady(MVPbkContactStore& aContactStore) |
|
385 { |
|
386 iContactStore = &aContactStore; |
|
387 |
|
388 TRAPD(error, DoAddL()); |
|
389 |
|
390 if( error != KErrNone ) |
|
391 { |
|
392 iContactService->RequestComplete(iTransId); |
|
393 if(error == KErrNotSupported && iErrorID != 0) |
|
394 { |
|
395 iCallback->HandleReturnValue(EInvalidKey, iErrorID, iTransId); |
|
396 } |
|
397 else |
|
398 { |
|
399 iCallback->HandleReturnValue(EOpComplete, error, iTransId); |
|
400 } |
|
401 |
|
402 delete this; |
|
403 } |
|
404 |
|
405 } |
|
406 |
|
407 /* |
|
408 ------------------------------------------------------------------------------------- |
|
409 CAddContactObserver::StoreUnavailable(MVPbkContactStore& aContactStore, |
|
410 TInt aReason) |
|
411 Description : Virtual Phonebook Observer method called back when OpenL(), |
|
412 the database open operation is failed due to unavailable store. |
|
413 Return values : N/A |
|
414 ------------------------------------------------------------------------------------- |
|
415 */ |
|
416 void CAddContactObserver::StoreUnavailable(MVPbkContactStore& /*aContactStore*/, |
|
417 TInt aReason) |
|
418 { |
|
419 //store open failed.....handle the error |
|
420 iContactService->RequestComplete(iTransId); |
|
421 iCallback->HandleReturnValue(EOpError, aReason, iTransId); |
|
422 |
|
423 delete this; |
|
424 } |
|
425 |
|
426 /* |
|
427 ------------------------------------------------------------------------------------- |
|
428 CAddContactObserver::HandleStoreEventL(MVPbkContactStore& aContactStore, |
|
429 TVPbkContactStoreEvent aStoreEvent) |
|
430 Description : Virtual Phonebook Observer method Not used here. |
|
431 Return values : N/A |
|
432 ------------------------------------------------------------------------------------- |
|
433 */ |
|
434 void CAddContactObserver::HandleStoreEventL(MVPbkContactStore& /*aContactStore*/, |
|
435 TVPbkContactStoreEvent /*aStoreEvent*/) |
|
436 { |
|
437 //do nothing here |
|
438 } |
|
439 |
|
440 /* |
|
441 ------------------------------------------------------------------------------------- |
|
442 CAddContactObserver::VPbkSingleContactOperationComplete |
|
443 (MVPbkContactOperationBase& aOperation, |
|
444 MVPbkStoreContact* aContact) |
|
445 Description : Virtual Phonebook Observer method called back when |
|
446 Single Contact Operation completes successfully.. |
|
447 Return values : N/A |
|
448 ------------------------------------------------------------------------------------- |
|
449 */ |
|
450 |
|
451 void CAddContactObserver::VPbkSingleContactOperationComplete |
|
452 (MVPbkContactOperationBase& aOperation, |
|
453 MVPbkStoreContact* aContact) |
|
454 |
|
455 { |
|
456 delete &aOperation; |
|
457 TRAPD(error, DoEditContactOrGroupL(aContact)); |
|
458 if(KErrNone != error) |
|
459 { |
|
460 iContactService->RequestComplete(iTransId); |
|
461 iCallback->HandleReturnValue(EOpComplete, error, iTransId); |
|
462 delete this; |
|
463 } |
|
464 } |
|
465 |
|
466 /* |
|
467 ------------------------------------------------------------------------------------- |
|
468 CAddContactObserver::VPbkSingleContactOperationFailed |
|
469 (MVPbkContactOperationBase& aOperation, |
|
470 TInt aError) |
|
471 Description : Virtual Phonebook Observer method called back when |
|
472 Single Contact Operation fails. |
|
473 Return values : N/A |
|
474 ------------------------------------------------------------------------------------- |
|
475 */ |
|
476 void CAddContactObserver::VPbkSingleContactOperationFailed |
|
477 (MVPbkContactOperationBase& aOperation, |
|
478 TInt aError) |
|
479 { |
|
480 delete &aOperation; |
|
481 iContactService->RequestComplete(iTransId); |
|
482 iCallback->HandleReturnValue(EOpError, aError, iTransId); |
|
483 |
|
484 delete this; |
|
485 } |