ac545af7e024e8b9d39ff9375d8910b2c74933fe
[framework/web/wrt-plugins-tizen.git] / src / Contact / AddressBook.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        AddressBook.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief
23  */
24
25 #include "AddressBook.h"
26
27 #include <contacts.h>
28
29 #include <ctime>
30 #include <cstdlib>
31 #include <cstddef>
32 #include <dpl/log/log.h>
33 #include <dpl/exception.h>
34 #include <Commons/Exception.h>
35 #include <Commons/Regex.h>
36 #include "IAddressBook.h"
37 #include "IContact.h"
38 #include "Contact.h"
39 #include "ContactObjectA2PConverter.h"
40 #include "ContactObjectP2AConverter.h"
41 #include "ContactsSvcObjectConverter.h"
42 #include "ContactSearchEngine.h"
43 #include "ContactFilterValidator.h"
44 #include "ContactUtility.h"
45
46 #include "ContactQueue.h"
47
48 namespace DeviceAPI {
49 namespace Contact {
50
51 using namespace DeviceAPI::Tizen;
52 using namespace WrtDeviceApis::Commons;
53 using namespace std;
54
55 AddressBook::AddressBook(bool isUnified) :
56                 IAddressBook(),
57                 m_id(0),
58                 m_name("TEST_ADDRESS_BOOK"),
59                 m_readOnly(false),
60                 m_accountId(0),
61                 m_isUnifiedAddressBook(isUnified),
62                 m_eventMapAcc(100)
63 {
64     LogDebug("entered");
65 }
66
67 AddressBook::~AddressBook()
68 {
69         if(m_addressBookEmitters.size() != 0)
70         {
71                 ContactsSvcChangeListenerManagerSingleton::Instance().unregisterContactsChangeListener(this);
72         }
73 }
74
75 std::string AddressBook::getId() const
76 {
77         if(m_isUnifiedAddressBook)
78                 return string("");
79
80         return ContactUtility::intToStr(m_id);
81 }
82
83 void AddressBook::setId(const std::string &value)
84 {
85         m_id = ContactUtility::strToInt(value);
86 }
87
88 std::string AddressBook::getAccountId() const
89 {
90         if(m_isUnifiedAddressBook)
91                 return string("");
92
93         return ContactUtility::intToStr(m_accountId);
94 }
95
96 void AddressBook::setAccountId(const std::string &value)
97 {
98         m_accountId = ContactUtility::strToInt(value);
99 }
100
101 void AddressBook::OnRequestReceived(const EventAddressBookGetPtr &event)
102 {
103         LogDebug("entered");
104
105         int errorCode = 0;
106         contacts_record_h contacts_record = NULL;
107
108         Try
109         {
110                 if(!event->getIdIsSet())
111                         ThrowMsg(InvalidArgumentException, "Invalid contact id");
112
113                 string contactId = event->getId();
114
115                 int contactIdInt = ContactUtility::strToInt(contactId);
116
117                 errorCode = contacts_db_get_record(_contacts_contact._uri, contactIdInt, &contacts_record);
118                 if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
119                         ThrowMsg(NotFoundException, "No contact (id:" << contactIdInt << ")");
120
121                 if(!m_isUnifiedAddressBook)
122                 {
123                         int addressBookId = 0;
124                         contacts_record_get_int(contacts_record, _contacts_contact.address_book_id, &addressBookId);
125                         if(addressBookId != m_id)
126                                 ThrowMsg(NotFoundException, "No contact (id:" << contactIdInt << ") in this address book.");
127                 }
128
129                 ContactObjectP2AConverterPtr contactObjConverter(
130                                 new ContactObjectP2AConverter(contacts_record, false));
131                 ContactPtr contact = contactObjConverter->getAbstractContact();
132
133                 event->setContact(contact);
134                 event->setResult(true);
135         }
136         Catch (NotFoundException)
137         {
138                 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
139                 event->setExceptionCode(ExceptionCodes::NotFoundException);
140                 event->setResult(false);
141         }
142         Catch (PlatformException)
143         {
144                 LogError("Error during getting contact : " << _rethrown_exception.GetMessage());
145                 event->setExceptionCode(ExceptionCodes::PlatformException);
146                 event->setResult(false);
147         }
148         Catch (InvalidArgumentException)
149         {
150                 LogError("Invalid Arguments : " << _rethrown_exception.GetMessage());
151                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
152                 event->setResult(false);
153         }
154         Catch(Exception)
155         {
156                 LogError("Error on platform : " << _rethrown_exception.GetMessage());
157                 event->setExceptionCode(ExceptionCodes::UnknownException);
158                 event->setResult(false);
159         }
160
161         if(contacts_record != NULL)
162                 contacts_record_destroy(contacts_record, true);
163 }
164
165 void AddressBook::OnRequestReceived(const EventAddressBookAddPtr &event)
166 {
167         LogDebug("entered");
168
169         int errorCode = 0;
170         int id = 0;
171         contacts_record_h contacts_record = NULL;
172         contacts_record_h get_contacts_record = NULL;
173         ContactPtr contact(NULL);
174
175         Try
176         {
177                 if(!event->getContactIsSet())
178                         ThrowMsg(InvalidArgumentException, "Contacts were not set.");
179
180                 contact = event->getContact();
181                 if(!contact)
182                         ThrowMsg(InvalidArgumentException, "No contacts.");
183                 id = ContactUtility::strToInt(contact->getId());
184                 if(id != 0)
185                         ThrowMsg(InvalidArgumentException, "invalid contact object : Id is already set");
186
187         }
188         Catch(InvalidArgumentException)
189         {
190                 LogError("Invalid arguments for adding contacts : " << _rethrown_exception.GetMessage());
191                 event->setResult(false);
192                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
193                 return;
194         }
195
196         Try
197         {
198                 DPL::SharedPtr<Contact> contactT =
199                                 DPL::StaticPointerCast<Contact>(contact);
200
201                 ContactObjectA2PConverterPtr contactObjConverter(NULL);
202
203                 contacts_record = contactT->getPlatformContactObject();
204                 if(contacts_record == NULL)
205                 {
206                         contactObjConverter = ContactObjectA2PConverterPtr(
207                                         new ContactObjectA2PConverter(contact, false) );
208                         contacts_record = contactObjConverter->getPlatformContact();
209                 }
210                 else
211                 {
212                         contactObjConverter = ContactObjectA2PConverterPtr(
213                                         new ContactObjectA2PConverter(contact, false, contacts_record) );
214                         contacts_record = contactObjConverter->getPlatformContact();
215                 }
216
217                 int contactId = 0;
218                 if (!contacts_record)
219                 {
220                         LogError("Error during converting contact object");
221                         ThrowMsg(PlatformException, "Error during converting contact object");
222                 }
223
224                 if(m_isUnifiedAddressBook)
225                         errorCode = contacts_record_set_int(contacts_record, _contacts_contact.address_book_id, 0);
226                 else
227                         errorCode = contacts_record_set_int(contacts_record, _contacts_contact.address_book_id, m_id);
228                 if(errorCode != CONTACTS_ERROR_NONE)
229                 {
230                         LogError("error code : " << errorCode);
231                         ThrowMsg(PlatformException, "Error during executing contacts_db_insert_record()");
232                 }
233
234                 errorCode = contacts_db_insert_record(contacts_record, &contactId);
235                 if(errorCode != CONTACTS_ERROR_NONE)
236                 {
237                         LogError("error code : " << errorCode);
238                         ThrowMsg(PlatformException, "Error during executing contacts_db_insert_record()");
239                 }
240
241                 errorCode = contacts_db_get_record(_contacts_contact._uri, contactId, &get_contacts_record);
242                 if(errorCode != CONTACTS_ERROR_NONE || get_contacts_record == NULL)
243                         ThrowMsg(PlatformException, "No contact just inserted  (id:" << contactId << ")");
244
245                 ContactObjectP2AConverterPtr contactObjConverterForInserted(
246                                 new ContactObjectP2AConverter(get_contacts_record, false));
247                 ContactPtr insertedContact = contactObjConverterForInserted->getAbstractContact();
248                 contact->copy(insertedContact);
249
250                 event->setResult(true);
251                 event->setExceptionCode(ExceptionCodes::None);
252         }
253         Catch (PlatformException)
254         {
255                 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
256                 event->setResult(false);
257                 event->setExceptionCode(ExceptionCodes::PlatformException);
258         }
259         Catch (Exception)
260         {
261                 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
262                 event->setResult(false);
263                 event->setExceptionCode(ExceptionCodes::UnknownException);
264         }
265
266         if(get_contacts_record != NULL)
267                 contacts_record_destroy(get_contacts_record, true);
268 }
269
270 void AddressBook::AddressBookAddBatch(const EventAddressBookAddBatchPtr &event)
271 {
272         LogDebug("entered");
273
274         int errorCode = 0;
275         contacts_list_h contacts_list = NULL;
276         ContactArrayPtr contacts(NULL);
277
278         Try
279         {
280                 if(!event->getContactsIsSet())
281                         ThrowMsg(InvalidArgumentException, "Contacts were not set.");
282
283                 contacts = event->getContacts();
284                 if(!contacts)
285                         ThrowMsg(InvalidArgumentException, "No contacts.");
286
287         }
288         Catch(InvalidArgumentException)
289         {
290                 ContactQueueManagerSingleton::Instance().decreaseQueueList();
291                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
292                 if(!isEmpty){
293                         ContactQueueManagerSingleton::Instance().pop();
294                 }
295
296                 LogError("Invalid arguments for adding contacts : " << _rethrown_exception.GetMessage());
297                 event->setResult(false);
298                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
299                 return;
300         }
301
302         Try
303         {
304                 errorCode = contacts_list_create(&contacts_list);
305                 if(errorCode != CONTACTS_ERROR_NONE)
306                 {
307                         ThrowMsg(PlatformException, "Fail to create contacts_list_h");
308                 }
309
310                 for(ContactArray::iterator i = contacts->begin(); i != contacts->end(); i++)
311                 {
312                         contacts_record_h contacts_record = NULL;
313                         ContactPtr contact = *i;
314                         ContactObjectA2PConverterPtr contactObjConverter(NULL);
315
316                         Try
317                         {
318                                 DPL::SharedPtr<Contact> newContactT =
319                                                 DPL::StaticPointerCast<Contact>(contact);
320                                 contacts_record = newContactT->getPlatformContactObject();
321                                 if(contacts_record == NULL)
322                                 {
323                                         errorCode = contacts_record_create(_contacts_contact._uri, &contacts_record);
324                                         if(errorCode != CONTACTS_ERROR_NONE)
325                                         {
326                                                 ThrowMsg(PlatformException, "Error during creating contact record : " << errorCode);
327                                         }
328                                 }
329
330                                 contactObjConverter = ContactObjectA2PConverterPtr(
331                                                 new ContactObjectA2PConverter(contact, false, contacts_record) );
332                                 contacts_record = contactObjConverter->getPlatformContact();
333
334                                 if(contacts_record == NULL)
335                                 {
336                                         ThrowMsg(PlatformException, "Error during converting contact object");
337                                 }
338
339                                 if(m_isUnifiedAddressBook)
340                                         errorCode = contacts_record_set_int(contacts_record, _contacts_contact.address_book_id, 0);
341                                 else
342                                         errorCode = contacts_record_set_int(contacts_record, _contacts_contact.address_book_id, m_id);
343                                 if(errorCode != CONTACTS_ERROR_NONE)
344                                 {
345                                         ThrowMsg(PlatformException, "Error during add address book id : " << errorCode);
346                                 }
347
348                                 errorCode = contacts_list_add(contacts_list, contacts_record);
349                                 if(errorCode != CONTACTS_ERROR_NONE)
350                                 {
351                                         ThrowMsg(PlatformException, "Error during add to list : " << errorCode);
352                                 }
353                         }
354                         Catch(Exception)
355                         {
356                                 ThrowMsg(NotFoundException, "Error during converting contact object");
357 //                              LogError("Error during converting contact object : " << _rethrown_exception.GetMessage());
358 //                              continue;
359                         }
360                 }
361
362                 KeySharePtrPair *keyPair = new KeySharePtrPair();
363                 keyPair->key = m_eventMapAcc;
364                 keyPair->addressBook = this;
365                 errorCode = contacts_db_insert_records_async(contacts_list, contactsAddBatchResultCallback, (void*)keyPair);
366                 if(errorCode != CONTACTS_ERROR_NONE)
367                 {
368                         delete keyPair;
369                         ThrowMsg(PlatformException, "Error during contacts_db_insert_records_async");
370                 }
371
372                 errorCode = contacts_list_destroy(contacts_list, true);
373                 contacts_list = NULL;
374                 if(errorCode != CONTACTS_ERROR_NONE)
375                 {
376                         delete keyPair;
377                         ThrowMsg(PlatformException, "Error during contacts_list_destroy");
378                 }
379
380                 pair<long, EventAddressBookAddBatchPtr> keyEventPair(m_eventMapAcc, event);
381                 m_addBatchEventMap.insert(keyEventPair);
382
383                 m_eventMapAcc++;
384
385 //              event->switchToManualAnswer();
386         }
387         Catch (NotFoundException)
388         {
389                 ContactQueueManagerSingleton::Instance().decreaseQueueList();
390                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
391                 if(!isEmpty){
392                         ContactQueueManagerSingleton::Instance().pop();
393                 }
394                 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
395                 event->setResult(false);
396                 event->setExceptionCode(ExceptionCodes::NotFoundException);
397         }
398         Catch (PlatformException)
399         {
400                 ContactQueueManagerSingleton::Instance().decreaseQueueList();
401                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
402                 if(!isEmpty){
403                         ContactQueueManagerSingleton::Instance().pop();
404                 }
405                 LogError("Error during adding contacts : " << _rethrown_exception.GetMessage());
406                 event->setResult(false);
407                 event->setExceptionCode(ExceptionCodes::PlatformException);
408         }
409         Catch (Exception)
410         {
411                 ContactQueueManagerSingleton::Instance().decreaseQueueList();
412                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
413                 if(!isEmpty){
414                         ContactQueueManagerSingleton::Instance().pop();
415                 }
416                 LogError("Error during adding contacts : " << _rethrown_exception.GetMessage());
417                 event->setResult(false);
418                 event->setExceptionCode(ExceptionCodes::PlatformException);
419         }
420
421         if(contacts_list != NULL)
422                 contacts_list_destroy(contacts_list, true);
423 }
424
425 void AddressBook::OnRequestReceived(const EventAddressBookAddBatchPtr &event)
426 {
427         LogDebug("entered");
428
429         Try
430         {
431                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
432                 if(isEmpty){
433                         AddressBookAddBatch(event);
434                 }else{
435                         ContactQueueManagerSingleton::Instance().push(ContactQueueManager::ADDBATCH, this, event);
436                 }
437                 ContactQueueManagerSingleton::Instance().increaseQueueList();
438                 event->switchToManualAnswer();
439         }
440         Catch (Exception)
441         {
442                 LogError("Error during adding contacts : " << _rethrown_exception.GetMessage());
443                 event->setResult(false);
444                 event->setExceptionCode(ExceptionCodes::PlatformException);
445         }
446 }
447
448 void AddressBook::OnRequestReceived(const EventAddressBookUpdatePtr &event)
449 {
450         LogDebug("entered");
451         ContactPtr contact(NULL);
452
453         Try
454         {
455                 if(!event->getContactIsSet())
456                         ThrowMsg(InvalidArgumentException, "Contacts were not set.");
457
458                 contact = event->getContact();
459                 if(!contact)
460                         ThrowMsg(InvalidArgumentException, "No contact.");
461
462                 if(!contact->getIdIsSet() || !contact->getAddressBookIdIsSet())
463                         ThrowMsg(InvalidArgumentException, "Contact has no ID");
464
465                 if(!m_isUnifiedAddressBook && contact->getAddressBookId() != getId())
466                         ThrowMsg(InvalidArgumentException, "Wrong address book ID");
467         }
468         Catch(InvalidArgumentException)
469         {
470                 LogError("Invalid arguments for adding contacts : " << _rethrown_exception.GetMessage());
471                 event->setResult(false);
472                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
473                 return;
474         }
475
476         Try
477         {
478                 int errorCode = 0;
479
480                 DPL::SharedPtr<Contact> contactT =
481                                 DPL::StaticPointerCast<Contact>(contact);
482
483                 ContactObjectA2PConverterPtr contactObjConverter(NULL);
484
485                 contacts_record_h contacts_record = contactT->getPlatformContactObject();
486                 if(contacts_record == NULL)
487                 {
488                         contactObjConverter = ContactObjectA2PConverterPtr(
489                                         new ContactObjectA2PConverter(contact, false) );
490                         contacts_record = contactObjConverter->getPlatformContact();
491                 }
492                 else
493                 {
494                         contactObjConverter = ContactObjectA2PConverterPtr(
495                                         new ContactObjectA2PConverter(contact, false, contacts_record) );
496                         contacts_record = contactObjConverter->getPlatformContact();
497                 }
498
499                 if (!contacts_record)
500                 {
501                         LogError("Error during converting contact object");
502                         ThrowMsg(PlatformException, "Error during converting contact object");
503                 }
504
505                 errorCode = contacts_db_update_record(contacts_record);
506                 if (errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
507                         ThrowMsg(NotFoundException, "Error during executing contacts_db_update_record()");
508                 else if (errorCode != CONTACTS_ERROR_NONE)
509                 {
510                         LogError("error code : " << errorCode);
511                         ThrowMsg(PlatformException, "Error during executing contacts_db_update_record()");
512                 }
513         }
514         Catch (NotFoundException)
515         {
516                 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
517                 event->setResult(false);
518                 event->setExceptionCode(ExceptionCodes::NotFoundException);
519                 return;
520         }
521         Catch (PlatformException)
522         {
523                 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
524                 event->setResult(false);
525                 event->setExceptionCode(ExceptionCodes::PlatformException);
526                 return;
527         }
528         Catch (Exception)
529         {
530                 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
531                 event->setResult(false);
532                 event->setExceptionCode(ExceptionCodes::PlatformException);
533                 return;
534         }
535         //m_latestVersion = get_contact_version();
536
537         event->setResult(true);
538         event->setExceptionCode(ExceptionCodes::None);
539 }
540
541 void AddressBook::AddressBookUpdateBatch(const EventAddressBookUpdateBatchPtr &event)
542 {
543         LogDebug("entered");
544         int errorCode = 0;
545         contacts_list_h contacts_list = NULL;
546         ContactArrayPtr contacts(NULL);
547
548         Try
549         {
550                 if(!event->getContactsIsSet())
551                         ThrowMsg(InvalidArgumentException, "Contacts were not set.");
552
553                 contacts = event->getContacts();
554                 if(!contacts)
555                         ThrowMsg(InvalidArgumentException, "No contacts.");
556
557         }
558         Catch(InvalidArgumentException)
559         {
560                 ContactQueueManagerSingleton::Instance().decreaseQueueList();
561                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
562                 if(!isEmpty){
563                         ContactQueueManagerSingleton::Instance().pop();
564                 }
565                 LogError("Invalid arguments for updating contacts : " << _rethrown_exception.GetMessage());
566                 event->setResult(false);
567                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
568                 return;
569         }
570
571         Try
572         {
573                 errorCode = contacts_list_create(&contacts_list);
574                 if(errorCode != CONTACTS_ERROR_NONE)
575                 {
576                         ThrowMsg(PlatformException, "Fail to create contacts_list_h");
577                 }
578
579                 for(ContactArray::iterator i = contacts->begin(); i != contacts->end(); i++)
580                 {
581                         contacts_record_h contacts_record = NULL;
582                         ContactPtr contact = *i;
583                         ContactObjectA2PConverterPtr contactObjConverter(NULL);
584
585                         Try
586                         {
587                                 if(!contact)
588                                         ThrowMsg(InvalidArgumentException, "No contact.");
589
590                                 if(contact->getIdIsSet() == false)
591                                         ThrowMsg(InvalidArgumentException, "Contact has no ID");
592
593                                 if(!m_isUnifiedAddressBook && ( !contact->getAddressBookIdIsSet() || contact->getAddressBookId() != getId()))
594                                         ThrowMsg(InvalidArgumentException, "Wrong address book ID");
595
596                                 DPL::SharedPtr<Contact> newContactT =
597                                                 DPL::StaticPointerCast<Contact>(contact);
598                                 contacts_record = newContactT->getPlatformContactObject();
599                                 if(contacts_record == NULL)
600                                 {
601                                         LogDebug("id : " << contact->getId());
602                                         int contactIdInt = ContactUtility::strToInt(contact->getId());
603                                         errorCode = contacts_db_get_record(_contacts_contact._uri, contactIdInt, &contacts_record);
604                                         if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
605                                                 ThrowMsg(NotFoundException, "No contact (id:" << contactIdInt << ")");
606                                 }
607                                 contactObjConverter = ContactObjectA2PConverterPtr(
608                                                 new ContactObjectA2PConverter(contact, false, contacts_record) );
609                                 contacts_record = contactObjConverter->getPlatformContact();
610
611                                 if(contacts_record == NULL)
612                                 {
613                                         ThrowMsg(PlatformException, "Error during converting contact object");
614                                 }
615
616                                 errorCode = contacts_list_add(contacts_list, contacts_record);
617                                 if(errorCode != CONTACTS_ERROR_NONE)
618                                 {
619                                         ThrowMsg(PlatformException, "Error during add to list");
620                                 }
621                         }
622                         Catch(Exception)
623                         {
624                                 ThrowMsg(NotFoundException, "Error during converting contact object");
625 //                              LogError("Error during converting contact object : " << _rethrown_exception.GetMessage());
626 //                              continue;
627                         }
628                 }
629
630                 KeySharePtrPair *keyPair = new KeySharePtrPair();
631                 keyPair->key = m_eventMapAcc;
632                 keyPair->addressBook = this;
633                 errorCode = contacts_db_update_records_async(contacts_list, contactsUpdateBatchResultCallback, (void*)keyPair);
634                 if(errorCode != CONTACTS_ERROR_NONE)
635                 {
636                         delete keyPair;
637                         ThrowMsg(PlatformException, "Error during contacts_db_update_records_async");
638                 }
639
640                 errorCode = contacts_list_destroy(contacts_list, true);
641                 contacts_list = NULL;
642                 if(errorCode != CONTACTS_ERROR_NONE)
643                 {
644                         delete keyPair;
645                         ThrowMsg(PlatformException, "Error during contacts_list_destroy");
646                 }
647
648                 pair<long, EventAddressBookUpdateBatchPtr> keyEventPair(m_eventMapAcc, event);
649                 m_updateBatchEventMap.insert(keyEventPair);
650
651                 m_eventMapAcc++;
652
653 //              event->switchToManualAnswer();
654         }
655         Catch (NotFoundException)
656         {
657                 ContactQueueManagerSingleton::Instance().decreaseQueueList();
658                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
659                 if(!isEmpty){
660                         ContactQueueManagerSingleton::Instance().pop();
661                 }
662                 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
663                 event->setResult(false);
664                 event->setExceptionCode(ExceptionCodes::NotFoundException);
665         }
666         Catch (PlatformException)
667         {
668                 ContactQueueManagerSingleton::Instance().decreaseQueueList();
669                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
670                 if(!isEmpty){
671                         ContactQueueManagerSingleton::Instance().pop();
672                 }
673                 LogError("Error during adding contacts : " << _rethrown_exception.GetMessage());
674                 event->setResult(false);
675                 event->setExceptionCode(ExceptionCodes::PlatformException);
676         }
677         Catch (Exception)
678         {
679                 ContactQueueManagerSingleton::Instance().decreaseQueueList();
680                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
681                 if(!isEmpty){
682                         ContactQueueManagerSingleton::Instance().pop();
683                 }
684                 LogError("Error during adding contacts : " << _rethrown_exception.GetMessage());
685                 event->setResult(false);
686                 event->setExceptionCode(ExceptionCodes::PlatformException);
687         }
688
689         if(contacts_list != NULL)
690                 contacts_list_destroy(contacts_list, false);
691 }
692
693 void AddressBook::OnRequestReceived(const EventAddressBookUpdateBatchPtr &event)
694 {
695         LogDebug("entered");
696
697         Try
698         {
699                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
700                 if(isEmpty){
701                         AddressBookUpdateBatch(event);
702                 }else{
703                         ContactQueueManagerSingleton::Instance().push(ContactQueueManager::UPATEBATCH, this, event);
704                 }
705                 ContactQueueManagerSingleton::Instance().increaseQueueList();
706                 event->switchToManualAnswer();
707         }
708         Catch (Exception)
709         {
710                 LogError("Error during updating contacts : " << _rethrown_exception.GetMessage());
711                 event->setResult(false);
712                 event->setExceptionCode(ExceptionCodes::PlatformException);
713         }
714 }
715
716 void AddressBook::OnRequestReceived(const EventAddressBookRemovePtr &event)
717 {
718         LogDebug("entered");
719
720         int errorCode = 0;
721         Try
722         {
723                 if(!event->getContactIdIsSet())
724                         ThrowMsg(InvalidArgumentException, "Contact id was not set.");
725
726                 string contactIdStr = event->getContactId();
727
728                 if(!ContactUtility::checkStrIsUInt(contactIdStr))
729                         ThrowMsg(InvalidArgumentException, "Id is wrong (" << contactIdStr << ")" );
730
731                 int contactId = ContactUtility::strToInt(contactIdStr);
732
733                 if(!m_isUnifiedAddressBook)
734                 {
735                         contacts_record_h contacts_record = NULL;
736                         errorCode = contacts_db_get_record(_contacts_simple_contact._uri, contactId, &contacts_record);
737                         if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
738                                 ThrowMsg(PlatformException, "No contact id (id:" << contactId << ")");
739
740                         int addressBookId = 0;
741                         errorCode = contacts_record_get_int(contacts_record, _contacts_simple_contact.address_book_id, &addressBookId);
742                         if(errorCode != CONTACTS_ERROR_NONE)
743                                 ThrowMsg(PlatformException, "Error while getting address book id");
744
745                         if(addressBookId != m_id)
746                                 ThrowMsg(PlatformException, "Contact id (" << contactId << ") is not a member of this address book.");
747                 }
748
749                 errorCode = contacts_db_delete_record(_contacts_contact._uri, contactId);
750                 if(errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
751                         ThrowMsg(NotFoundException, "Error during executing contacts_db_delete_record()");
752                 else if (errorCode != CONTACTS_ERROR_NONE)
753                         ThrowMsg(PlatformException, "Error during executing contacts_db_delete_record()");
754         }
755         Catch (InvalidArgumentException)
756         {
757                 LogError("Invalid contact id : " << _rethrown_exception.GetMessage());
758                 event->setResult(false);
759                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
760                 return;
761         }
762         Catch (NotFoundException)
763         {
764                 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
765                 event->setResult(false);
766                 event->setExceptionCode(ExceptionCodes::NotFoundException);
767                 return;
768         }
769         Catch (PlatformException)
770         {
771                 LogError("Error during updating contact : " << _rethrown_exception.GetMessage());
772                 event->setResult(false);
773                 event->setExceptionCode(ExceptionCodes::PlatformException);
774                 return;
775         }
776         Catch (Exception)
777         {
778                 LogError("Error during updating contact : " << _rethrown_exception.GetMessage());
779                 event->setResult(false);
780                 event->setExceptionCode(ExceptionCodes::PlatformException);
781                 return;
782         }
783
784         event->setResult(true);
785         event->setExceptionCode(ExceptionCodes::None);
786 }
787
788 void AddressBook::AddressBookRemoveBatch(const EventAddressBookRemoveBatchPtr &event)
789 {
790         LogDebug("entered");
791         int errorCode = 0;
792         StringArrayPtr contactIds(NULL);
793
794         Try
795         {
796                 if(!event->getContactIdsIsSet())
797                         ThrowMsg(InvalidArgumentException, "Contact IDs were not set.");
798
799                 contactIds = event->getContactIds();
800                 if(!contactIds)
801                         ThrowMsg(InvalidArgumentException, "No contact IDs.");
802
803         }
804         Catch(InvalidArgumentException)
805         {
806                 ContactQueueManagerSingleton::Instance().decreaseQueueList();
807                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
808                 if(!isEmpty){
809                         ContactQueueManagerSingleton::Instance().pop();
810                 }
811                 LogError("Invalid arguments for removing contacts : " << _rethrown_exception.GetMessage());
812                 event->setResult(false);
813                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
814                 return;
815         }
816
817         Try
818         {
819                 int *ids = new int[contactIds->size()];
820                 int *tmpIds = new int[contactIds->size()];
821                 int count = 0;
822
823                 if(errorCode != CONTACTS_ERROR_NONE)
824                 {
825                         ThrowMsg(PlatformException, "Fail to create contacts_list_h");
826                 }
827
828                 for(StringArray::iterator i = contactIds->begin(); i != contactIds->end(); i++)
829                 {
830                         string contactIdStr = *i;
831
832                         Try
833                         {
834                                 int contactId;
835
836                                 if(!ContactUtility::checkStrIsUInt(contactIdStr))
837                                         ThrowMsg(InvalidArgumentException, "Id is wrong (" << contactIdStr << ")" );
838
839                                 contactId = ContactUtility::strToInt(contactIdStr);
840
841                                 if(!m_isUnifiedAddressBook)
842                                 {
843                                         contacts_record_h contacts_record = NULL;
844                                         errorCode = contacts_db_get_record(_contacts_simple_contact._uri, contactId, &contacts_record);
845                                         if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
846                                                 ThrowMsg(PlatformException, "No contact id (id:" << contactId << ")");
847
848                                         int addressBookId = 0;
849                                         errorCode = contacts_record_get_int(contacts_record, _contacts_simple_contact.address_book_id, &addressBookId);
850                                         if(errorCode != CONTACTS_ERROR_NONE)
851                                                 ThrowMsg(PlatformException, "Error while getting address book id");
852
853                                         if(addressBookId != m_id)
854                                                 ThrowMsg(PlatformException, "Contact id (" << contactId << ") is not a member of this address book.");
855                                 }
856
857                                 ids[count] = contactId;
858                                 tmpIds[count] = contactId;
859                                 count++;
860                         }
861                         Catch(Exception)
862                         {
863                                 ThrowMsg(NotFoundException, "Error during converting contact object");
864 //                              LogError("Error during converting contact object : " << _rethrown_exception.GetMessage());
865 //                              continue;
866                         }
867                 }
868
869                 contacts_filter_h filter = NULL;
870                 contacts_query_h query = NULL;
871
872                 errorCode = contacts_query_create(_contacts_simple_contact._uri, &query);
873                 if(errorCode != CONTACTS_ERROR_NONE)
874                         ThrowMsg(PlatformException, "contacts_query_create error : " << errorCode << " (" << __FUNCTION__ << ")");
875
876                 errorCode = contacts_filter_create( _contacts_simple_contact._uri, &filter );
877                 if(errorCode != CONTACTS_ERROR_NONE)
878                         ThrowMsg(PlatformException, "contacts_query_create error : " << errorCode << " (" << __FUNCTION__ << ")");
879
880                 for(int i = 0; i < contactIds->size(); i++)
881                 {
882                         errorCode = contacts_filter_add_int(filter, _contacts_simple_contact.id, CONTACTS_MATCH_EQUAL, tmpIds[i]);
883                         if(i == (contactIds->size() - 1))
884                                 break;
885
886                         errorCode = contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_OR);
887                         if(errorCode != CONTACTS_ERROR_NONE)
888                                 ThrowMsg(PlatformException, "contacts_filter_add_operator error : " << errorCode << " (" << __FUNCTION__ << ")");
889                 }
890
891                 errorCode = contacts_query_set_filter(query, filter);
892                 if(errorCode != CONTACTS_ERROR_NONE)
893                         ThrowMsg(PlatformException, "contacts_query_set_filter error : " << errorCode << " (" << __FUNCTION__ << ")");
894
895                 int record_count = 0;
896                 errorCode = contacts_db_get_count_with_query(query, &record_count);
897                 if(errorCode != CONTACTS_ERROR_NONE)
898                         ThrowMsg(PlatformException, "contacts_db_get_count_with_query error : " << errorCode << " (" << __FUNCTION__ << ")");
899
900                 if(filter != NULL)
901                         contacts_filter_destroy(filter);
902                 if(query != NULL)
903                         contacts_query_destroy(query);
904
905                 if(contactIds->size() != (unsigned int)record_count)
906                         ThrowMsg(InvalidArgumentException, "Ids' db count  : " << record_count << " (" << __FUNCTION__ << ")");
907
908                 KeySharePtrPair *keyPair = new KeySharePtrPair();
909                 keyPair->key = m_eventMapAcc;
910                 keyPair->addressBook = this;
911                 errorCode = contacts_db_delete_records_async(_contacts_contact._uri, ids, count, contactsRemoveBatchResultCallback, (void*)keyPair);
912                 if(ids != NULL)
913                 {
914                         delete [] ids;
915                 }
916                 if(tmpIds != NULL)
917                 {
918                         delete [] tmpIds;
919                 }
920                 if(errorCode != CONTACTS_ERROR_NONE)
921                 {
922                         delete keyPair;
923                         ThrowMsg(PlatformException, "Error during add to list");
924                 }
925
926                 pair<long, EventAddressBookRemoveBatchPtr> keyEventPair(m_eventMapAcc, event);
927                 m_removeBatchEventMap.insert(keyEventPair);
928
929                 m_eventMapAcc++;
930
931 //              event->switchToManualAnswer();
932         }
933         Catch (InvalidArgumentException)
934         {
935                 ContactQueueManagerSingleton::Instance().decreaseQueueList();
936                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
937                 if(!isEmpty){
938                         ContactQueueManagerSingleton::Instance().pop();
939                 }
940                 LogError("Invalid contact id : " << _rethrown_exception.GetMessage());
941                 event->setResult(false);
942                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
943                 return;
944         }
945         Catch (NotFoundException)
946         {
947                 ContactQueueManagerSingleton::Instance().decreaseQueueList();
948                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
949                 if(!isEmpty){
950                         ContactQueueManagerSingleton::Instance().pop();
951                 }
952                 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
953                 event->setResult(false);
954                 event->setExceptionCode(ExceptionCodes::NotFoundException);
955                 return;
956         }
957         Catch (PlatformException)
958         {
959                 ContactQueueManagerSingleton::Instance().decreaseQueueList();
960                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
961                 if(!isEmpty){
962                         ContactQueueManagerSingleton::Instance().pop();
963                 }
964                 LogError("Error during deleting contacts : " << _rethrown_exception.GetMessage());
965                 event->setResult(false);
966                 event->setExceptionCode(ExceptionCodes::PlatformException);
967                 return;
968         }
969         Catch (Exception)
970         {
971                 ContactQueueManagerSingleton::Instance().decreaseQueueList();
972                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
973                 if(!isEmpty){
974                         ContactQueueManagerSingleton::Instance().pop();
975                 }
976                 LogError("Error during deleting contacts : " << _rethrown_exception.GetMessage());
977                 event->setResult(false);
978                 event->setExceptionCode(ExceptionCodes::PlatformException);
979                 return;
980         }
981 }
982
983 void AddressBook::OnRequestReceived(const EventAddressBookRemoveBatchPtr &event)
984 {
985         LogDebug("entered");
986
987         Try
988         {
989                 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
990                 if(isEmpty){
991                         AddressBookRemoveBatch(event);
992                 }else{
993                         ContactQueueManagerSingleton::Instance().push(ContactQueueManager::DELETEBATCH, this, event);
994                 }
995                 ContactQueueManagerSingleton::Instance().increaseQueueList();
996                 event->switchToManualAnswer();
997         }
998         Catch (Exception)
999         {
1000                 LogError("Error during deleting contacts : " << _rethrown_exception.GetMessage());
1001                 event->setResult(false);
1002                 event->setExceptionCode(ExceptionCodes::PlatformException);
1003         }
1004 }
1005
1006 void AddressBook::OnRequestReceived(const EventAddressBookFindPtr &event)
1007 {
1008         LogDebug("entered");
1009
1010         contacts_query_h query = NULL;
1011         contacts_filter_h filter = NULL;
1012         contacts_list_h contacts_list = NULL;
1013
1014         ContactArrayPtr contacts = ContactArrayPtr(new ContactArray());
1015
1016         Try
1017         {
1018                 ContactSearchEnginePtr searchEngine(new ContactSearchEngine());
1019
1020                 if(!m_isUnifiedAddressBook)
1021                         searchEngine->setAddressBookId(m_id);
1022
1023                 if(event->getFilterIsSet())
1024                 {
1025                         FilterPtr filter = event->getFilter();
1026
1027                         FilterValidatorPtr validator = ContactFilterValidatorFactory::getContactFilterValidator();
1028                         bool success = filter->validate(validator);
1029
1030                         if(!success)
1031                                 ThrowMsg(InvalidArgumentException, "Invalid filter arguments.");
1032
1033                         searchEngine->setCondition(filter);
1034                 }
1035
1036                 if (event->getSortModeIsSet())
1037                 {
1038                         searchEngine->setSortMode(event->getSortMode());
1039                 }
1040                 else
1041                         searchEngine->setSortMode();
1042
1043                 event->setContacts(searchEngine->getContactSearchResult());
1044                 event->setResult(true);
1045         }
1046         Catch (NotFoundException)
1047         {
1048                 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
1049                 event->setExceptionCode(ExceptionCodes::NotFoundException);
1050                 event->setResult(false);
1051         }
1052         Catch (PlatformException)
1053         {
1054                 LogError("Error during finding contact : " << _rethrown_exception.GetMessage());
1055                 event->setExceptionCode(ExceptionCodes::PlatformException);
1056                 event->setResult(false);
1057         }
1058         Catch (InvalidArgumentException)
1059         {
1060                 LogError("Invalid Arguments : " << _rethrown_exception.GetMessage());
1061                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1062                 event->setResult(false);
1063         }
1064         Catch (Exception)
1065         {
1066                 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
1067                 event->setExceptionCode(ExceptionCodes::PlatformException);
1068                 event->setResult(false);
1069         }
1070
1071         if(filter != NULL)
1072                 contacts_filter_destroy(filter);
1073
1074         if(query != NULL)
1075                 contacts_query_destroy(query);
1076
1077         if(contacts_list != NULL)
1078                 contacts_list_destroy(contacts_list, true);
1079 }
1080
1081 void AddressBook::OnRequestReceived(const EventAddressBookAddChangeListenerPtr &event)
1082 {
1083         LogDebug("entered");
1084
1085         Try
1086         {
1087                 if(!event->getEmitterIsSet())
1088                         ThrowMsg(InvalidArgumentException, "Invalid arguments.");
1089
1090                 EventAddressBookChangeListenerEmitterPtr emitter = event->getEmitter();
1091                 if(emitter == NULL)
1092                         ThrowMsg(InvalidArgumentException, "Invalid arguments.");
1093
1094                 if(m_addressBookEmitters.size() == 0)
1095                 {
1096                         LogDebug("Watch registered initially");
1097
1098                         if(m_isUnifiedAddressBook)
1099                                 ContactsSvcChangeListenerManagerSingleton::Instance().registerContactsChangeListener(this, -1);
1100                         else
1101                                 ContactsSvcChangeListenerManagerSingleton::Instance().registerContactsChangeListener(this, m_id);
1102                 }
1103
1104                 m_addressBookEmitters.attach(emitter);
1105
1106                 long id = ContactsSvcChangeListenerManagerSingleton::Instance().getWatchIdAndInc();
1107                 m_watchIdMap[id] = emitter->getId();
1108
1109             event->setId(id);
1110                 event->setResult(true);
1111         }
1112         Catch (NotFoundException)
1113         {
1114                 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
1115                 event->setExceptionCode(ExceptionCodes::NotFoundException);
1116                 event->setResult(false);
1117         }
1118         Catch (PlatformException)
1119         {
1120                 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
1121                 event->setExceptionCode(ExceptionCodes::PlatformException);
1122                 event->setResult(false);
1123         }
1124         Catch (InvalidArgumentException)
1125         {
1126                 LogError("Invalid Arguments : " << _rethrown_exception.GetMessage());
1127                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1128                 event->setResult(false);
1129         }
1130         Catch (Exception)
1131         {
1132                 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
1133                 event->setExceptionCode(ExceptionCodes::PlatformException);
1134                 event->setResult(false);
1135         }
1136 }
1137
1138 void AddressBook::OnRequestReceived(const EventAddressBookRemoveChangeListenerPtr &event)
1139 {
1140         LogDebug("entered");
1141
1142         Try
1143         {
1144                 if(!event->getIdIsSet())
1145                         ThrowMsg(InvalidArgumentException, "Invalid arguments.");
1146
1147                 long id = event->getId();
1148                 if(m_watchIdMap.find(id) == m_watchIdMap.end())
1149                         ThrowMsg(NotFoundException, "No watchId : " << id);
1150
1151                 bool success = m_addressBookEmitters.detach(m_watchIdMap[id]);
1152                 if(!success)
1153                         ThrowMsg(NotFoundException, "No watchId : " << id);
1154
1155                 m_watchIdMap.erase(id);
1156
1157                 if(m_addressBookEmitters.size() == 0)
1158                 {
1159                         LogDebug("No watcher is registered. unsubscribing from contact service.");
1160
1161                         ContactsSvcChangeListenerManagerSingleton::Instance().unregisterContactsChangeListener(this);
1162                 }
1163
1164                 event->setResult(true);
1165
1166         }
1167         Catch (NotFoundException)
1168         {
1169                 LogError("WatchId doesn't exist : " << _rethrown_exception.GetMessage());
1170                 event->setExceptionCode(ExceptionCodes::NotFoundException);
1171                 event->setResult(false);
1172         }
1173         Catch (PlatformException)
1174         {
1175                 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
1176                 event->setExceptionCode(ExceptionCodes::PlatformException);
1177                 event->setResult(false);
1178         }
1179         Catch (InvalidArgumentException)
1180         {
1181                 LogError("Invalid Arguments : " << _rethrown_exception.GetMessage());
1182                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1183                 event->setResult(false);
1184         }
1185         Catch (Exception)
1186         {
1187                 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
1188                 event->setExceptionCode(ExceptionCodes::PlatformException);
1189                 event->setResult(false);
1190         }
1191 }
1192
1193 void AddressBook::OnRequestReceived(const EventAddressBookGetGroupPtr &event)
1194 {
1195         LogDebug("entered");
1196
1197         int errorCode = 0;
1198         contacts_record_h contacts_record = NULL;
1199
1200         Try
1201         {
1202                 if(!event->getIdIsSet())
1203                         ThrowMsg(InvalidArgumentException, "Invalid group id");
1204
1205                 string groupId = event->getId();
1206
1207                 int groupIdInt = ContactUtility::strToInt(groupId);
1208
1209                 errorCode = contacts_db_get_record(_contacts_group._uri, groupIdInt, &contacts_record);
1210                 if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
1211                         ThrowMsg(NotFoundException, "No group (id:" << groupIdInt << ")");
1212
1213                 if(!m_isUnifiedAddressBook)
1214                 {
1215                         int addressBookId = 0;
1216                         contacts_record_get_int(contacts_record, _contacts_group.address_book_id, &addressBookId);
1217                         if(addressBookId != m_id)
1218                                 ThrowMsg(NotFoundException, "No group (id:" << groupIdInt << ") in this address book.");
1219                 }
1220
1221                 ContactGroupPtr group = ContactGroupPtr(new ContactGroup());
1222
1223                 ContactsSvcObjectConverter::convertToAbstract(contacts_record, group);
1224
1225                 event->setContactGroup(group);
1226                 event->setResult(true);
1227
1228         }
1229         Catch (NotFoundException)
1230         {
1231                 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
1232                 event->setExceptionCode(ExceptionCodes::NotFoundException);
1233                 event->setResult(false);
1234
1235         }
1236         Catch (PlatformException)
1237         {
1238                 LogError("Error during getting contact : " << _rethrown_exception.GetMessage());
1239                 event->setExceptionCode(ExceptionCodes::PlatformException);
1240                 event->setResult(false);
1241
1242         }
1243         Catch (InvalidArgumentException)
1244         {
1245                 LogError("Invalid Arguments : " << _rethrown_exception.GetMessage());
1246                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1247                 event->setResult(false);
1248
1249         }
1250         Catch(Exception)
1251         {
1252                 LogError("Error on platform : " << _rethrown_exception.GetMessage());
1253                 event->setExceptionCode(ExceptionCodes::UnknownException);
1254                 event->setResult(false);
1255         }
1256
1257         if(contacts_record != NULL)
1258                 contacts_record_destroy(contacts_record, true);
1259 }
1260
1261 void AddressBook::OnRequestReceived(const EventAddressBookAddGroupPtr &event)
1262 {
1263         LogDebug("entered");
1264
1265         int errorCode = 0;
1266
1267         contacts_record_h contacts_record = NULL;
1268         ContactGroupPtr group(NULL);
1269
1270         Try
1271         {
1272                 if(!event->getContactGroupIsSet())
1273                         ThrowMsg(InvalidArgumentException, "group were not set.");
1274
1275                 group = event->getContactGroup();
1276                 if(!group)
1277                         ThrowMsg(InvalidArgumentException, "No group.");
1278
1279                 if(group->getIdIsSet() || group->getAddressBookIdIsSet())
1280                         ThrowMsg(InvalidArgumentException, "ContactGroup object is wrong");
1281         }
1282         Catch(InvalidArgumentException)
1283         {
1284                 LogError("Invalid arguments for adding contacts : " << _rethrown_exception.GetMessage());
1285                 event->setResult(false);
1286                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1287                 return;
1288         }
1289
1290         Try
1291         {
1292                 errorCode = contacts_record_create(_contacts_group._uri, &contacts_record);
1293                 if(errorCode != CONTACTS_ERROR_NONE)
1294                 {
1295                         LogError("error code : " << errorCode);
1296                         ThrowMsg(PlatformException, "Error during executing contacts_record_create()");
1297                 }
1298
1299                 if(m_isUnifiedAddressBook)
1300                         errorCode = contacts_record_set_int(contacts_record, _contacts_group.address_book_id, 0);
1301                 else
1302                         errorCode = contacts_record_set_int(contacts_record, _contacts_group.address_book_id, m_id);
1303                 if(errorCode != CONTACTS_ERROR_NONE)
1304                 {
1305                         LogError("error code : " << errorCode);
1306                         ThrowMsg(PlatformException, "Error during executing contacts_record_set_int()");
1307                 }
1308
1309                 ContactsSvcObjectConverter::convertToPlatform(group, contacts_record);
1310
1311                 int groupId = 0;
1312                 errorCode = contacts_db_insert_record(contacts_record, &groupId);
1313                 if(errorCode != CONTACTS_ERROR_NONE)
1314                 {
1315                         LogError("error code : " << errorCode);
1316                         ThrowMsg(PlatformException, "Error during executing contacts_db_insert_record()");
1317                 }
1318                 group->setId(groupId);
1319                 group->setAddressBookId(m_id);
1320
1321                 event->setResult(true);
1322                 event->setExceptionCode(ExceptionCodes::None);
1323         }
1324         Catch (PlatformException)
1325         {
1326                 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
1327                 event->setResult(false);
1328                 event->setExceptionCode(ExceptionCodes::PlatformException);
1329         }
1330         Catch (Exception)
1331         {
1332                 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
1333                 event->setResult(false);
1334                 event->setExceptionCode(ExceptionCodes::UnknownException);
1335         }
1336
1337         if(contacts_record != NULL)
1338                 contacts_record_destroy(contacts_record, true);
1339 }
1340
1341 void AddressBook::OnRequestReceived(const EventAddressBookUpdateGroupPtr &event)
1342 {
1343         LogDebug("entered");
1344
1345         int errorCode = 0;
1346
1347         contacts_record_h contacts_record = NULL;
1348         ContactGroupPtr group(NULL);
1349
1350         Try
1351         {
1352                 if(!event->getContactGroupIsSet())
1353                         ThrowMsg(InvalidArgumentException, "group were not set.");
1354
1355                 group = event->getContactGroup();
1356                 if(!group)
1357                         ThrowMsg(InvalidArgumentException, "No group.");
1358
1359                 if(!group->getIdIsSet() || !group->getAddressBookIdIsSet())
1360                         ThrowMsg(InvalidArgumentException, "Contact Group is wrong");
1361
1362                 if(!m_isUnifiedAddressBook && group->getAddressBookId() != getId())
1363                         ThrowMsg(InvalidArgumentException, "Wrong address book ID");
1364         }
1365         Catch(InvalidArgumentException)
1366         {
1367                 LogError("Invalid arguments for updating group : " << _rethrown_exception.GetMessage());
1368                 event->setResult(false);
1369                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1370                 return;
1371         }
1372
1373         if(group->getReadOnly())
1374         {
1375                 LogWarning("Canceling updating readonly data.");
1376                 event->setResult(false);
1377                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1378                 return;
1379         }
1380
1381         Try
1382         {
1383                 int id = ContactUtility::strToInt(group->getId());
1384                 LogDebug("Load platform object id : " << id);
1385                 errorCode = contacts_db_get_record(_contacts_group._uri, id, &contacts_record);
1386                 if (errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
1387                         ThrowMsg(NotFoundException, "Error during executing contacts_db_get_record()");
1388                 else if (errorCode != CONTACTS_ERROR_NONE)
1389                 {
1390                         LogError("error code : " << errorCode);
1391                         ThrowMsg(PlatformException, "Error during executing contacts_db_get_record()");
1392                 }
1393
1394                 LogDebug("Update group : " << id);
1395                 ContactsSvcObjectConverter::convertToPlatform(group, contacts_record);
1396
1397                 errorCode = contacts_db_update_record(contacts_record);
1398                 if (errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
1399                         ThrowMsg(NotFoundException, "Error during executing contacts_db_update_record()");
1400                 else if (errorCode != CONTACTS_ERROR_NONE)
1401                 {
1402                         LogError("error code : " << errorCode);
1403                         ThrowMsg(PlatformException, "Error during executing contacts_db_update_record()");
1404                 }
1405
1406                 event->setResult(true);
1407                 event->setExceptionCode(ExceptionCodes::None);
1408         }
1409         Catch (NotFoundException)
1410         {
1411                 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
1412                 event->setResult(false);
1413                 event->setExceptionCode(ExceptionCodes::NotFoundException);
1414         }
1415         Catch (PlatformException)
1416         {
1417                 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
1418                 event->setResult(false);
1419                 event->setExceptionCode(ExceptionCodes::PlatformException);
1420         }
1421         Catch (Exception)
1422         {
1423                 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
1424                 event->setResult(false);
1425                 event->setExceptionCode(ExceptionCodes::PlatformException);
1426         }
1427
1428         if(contacts_record != NULL)
1429                 contacts_record_destroy(contacts_record, true);
1430
1431         return;
1432 }
1433
1434 void AddressBook::OnRequestReceived(const EventAddressBookRemoveGroupPtr &event)
1435 {
1436         LogDebug("entered");
1437
1438         int errorCode = 0;
1439         Try
1440         {
1441                 if(!event->getContactGroupIdIsSet())
1442                         ThrowMsg(InvalidArgumentException, "group were not set.");
1443
1444                 string groupIdStr = event->getContactGroupId();
1445
1446                 if(!ContactUtility::checkStrIsUInt(groupIdStr))
1447                         ThrowMsg(InvalidArgumentException, "Id is wrong (" << groupIdStr << ")" );
1448
1449                 int groupId = ContactUtility::strToInt(groupIdStr);
1450
1451                 if(!m_isUnifiedAddressBook)
1452                 {
1453                         contacts_record_h contacts_record = NULL;
1454                         errorCode = contacts_db_get_record(_contacts_group._uri, groupId, &contacts_record);
1455                         if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
1456                                 ThrowMsg(PlatformException, "No group id (id:" << groupId << ")");
1457
1458                         int addressBookId = 0;
1459                         errorCode = contacts_record_get_int(contacts_record, _contacts_group.address_book_id, &addressBookId);
1460                         if(errorCode != CONTACTS_ERROR_NONE)
1461                                 ThrowMsg(PlatformException, "Error while getting address book id");
1462
1463                         if(addressBookId != m_id)
1464                                 ThrowMsg(PlatformException, "Contact id (" << groupId << ") is not a member of this address book.");
1465                 }
1466
1467                 errorCode = contacts_db_delete_record(_contacts_group._uri, groupId);
1468                 if(errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
1469                         ThrowMsg(NotFoundException, "Error during executing contacts_db_delete_record()");
1470                 else if (errorCode != CONTACTS_ERROR_NONE)
1471                         ThrowMsg(PlatformException, "Error during executing contacts_db_delete_record()");
1472
1473                 event->setResult(true);
1474                 event->setExceptionCode(ExceptionCodes::None);
1475         }
1476         Catch (InvalidArgumentException)
1477         {
1478                 LogError("Invalid group id : " << _rethrown_exception.GetMessage());
1479                 event->setResult(false);
1480                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1481         }
1482         Catch (NotFoundException)
1483         {
1484                 LogError("Group doesn't exist : " << _rethrown_exception.GetMessage());
1485                 event->setResult(false);
1486                 event->setExceptionCode(ExceptionCodes::NotFoundException);
1487         }
1488         Catch (PlatformException)
1489         {
1490                 LogError("Error during removing group : " << _rethrown_exception.GetMessage());
1491                 event->setResult(false);
1492                 event->setExceptionCode(ExceptionCodes::PlatformException);
1493         }
1494         Catch (Exception)
1495         {
1496                 LogError("Error during removing group : " << _rethrown_exception.GetMessage());
1497                 event->setResult(false);
1498                 event->setExceptionCode(ExceptionCodes::PlatformException);
1499         }
1500 }
1501
1502 void AddressBook::OnRequestReceived(const EventAddressBookGetGroupsPtr &event)
1503 {
1504         LogDebug("entered");
1505
1506         int errorCode = 0;
1507
1508         ContactGroupArrayPtr groups(new ContactGroupArray());
1509         contacts_list_h groups_list = NULL;
1510
1511         Try
1512         {
1513
1514                 if(m_isUnifiedAddressBook)
1515                 {
1516                         errorCode = contacts_db_get_all_records(_contacts_group._uri, 0, 0, &groups_list);
1517                         if(errorCode != CONTACTS_ERROR_NONE)
1518                                 ThrowMsg(PlatformException, "Fail to get group list (ret:" << errorCode << ")");
1519                 }
1520                 else
1521                 {
1522                         contacts_query_h query = NULL;
1523                         contacts_filter_h filter = NULL;
1524
1525                         errorCode = contacts_query_create(_contacts_group._uri, &query);
1526                         if(errorCode != CONTACTS_ERROR_NONE)
1527                                 ThrowMsg(PlatformException, "Fail to get contacts_query_create (ret:" << errorCode << ")");
1528
1529                         errorCode = contacts_filter_create(_contacts_group._uri, &filter);
1530                         if(errorCode != CONTACTS_ERROR_NONE)
1531                                 ThrowMsg(PlatformException, "Fail to get contacts_filter_create (ret:" << errorCode << ")");
1532
1533                         errorCode = contacts_filter_add_int(filter, _contacts_group.address_book_id, CONTACTS_MATCH_EQUAL, m_id);
1534                         if(errorCode != CONTACTS_ERROR_NONE)
1535                                 ThrowMsg(PlatformException, "Fail to get contacts_filter_add_int (ret:" << errorCode << ")");
1536
1537                         errorCode = contacts_query_set_filter(query, filter);
1538                         if(errorCode != CONTACTS_ERROR_NONE)
1539                                 ThrowMsg(PlatformException, "Fail to get contacts_query_set_filter (ret:" << errorCode << ")");
1540
1541                         errorCode = contacts_db_get_records_with_query(query, 0, 0, &groups_list);
1542                         if(errorCode != CONTACTS_ERROR_NONE)
1543                                 ThrowMsg(PlatformException, "Fail to get contacts_db_get_records_with_query (ret:" << errorCode << ")");
1544
1545                         errorCode = contacts_filter_destroy(filter);
1546                         if(errorCode != CONTACTS_ERROR_NONE)
1547                                 ThrowMsg(PlatformException, "Fail to get contacts_filter_destroy (ret:" << errorCode << ")");
1548
1549                         errorCode = contacts_query_destroy(query);
1550                         if(errorCode != CONTACTS_ERROR_NONE)
1551                                 ThrowMsg(PlatformException, "Fail to get contacts_query_destroy (ret:" << errorCode << ")");
1552                 }
1553
1554                 unsigned int record_count = 0;
1555                 errorCode = contacts_list_get_count(groups_list, &record_count);
1556                 if(errorCode != CONTACTS_ERROR_NONE)
1557                         ThrowMsg(PlatformException, "Fail to get contacts_list_get_count (ret:" << errorCode << ")");
1558
1559                 contacts_list_first(groups_list);
1560                 for(unsigned int i=0; i<record_count; i++)
1561                 {
1562                         contacts_record_h contacts_record;
1563                         errorCode = contacts_list_get_current_record_p(groups_list, &contacts_record);
1564                         if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
1565                         {
1566                                 LogWarning("Fail to get group record (ret:" << errorCode << ")");
1567                                 continue;
1568                         }
1569
1570                         int id = 0;
1571                         int addressBookId = 0;
1572                         char *name = NULL;
1573                         char *ringtoneURI = NULL;
1574                         char *photoURI = NULL;
1575                         bool isReadOnly = false;
1576
1577                         errorCode = contacts_record_get_int(contacts_record, _contacts_group.id, &id);
1578                         if(errorCode != CONTACTS_ERROR_NONE)
1579                         {
1580                                 LogWarning("Fail to get id from address book (ret:" << errorCode << ")");
1581                                 continue;
1582                         }
1583
1584                         errorCode = contacts_record_get_int(contacts_record, _contacts_group.address_book_id, &addressBookId);
1585                         if(errorCode != CONTACTS_ERROR_NONE)
1586                         {
1587                                 LogWarning("Fail to get address book id from address book (ret:" << errorCode << ")");
1588                                 continue;
1589                         }
1590
1591                         errorCode = contacts_record_get_str_p(contacts_record, _contacts_group.name, &name);
1592                         if(errorCode != CONTACTS_ERROR_NONE)
1593                         {
1594                                 LogWarning("Fail to get name from address book (ret:" << errorCode << ")");
1595                                 continue;
1596                         }
1597
1598                         errorCode = contacts_record_get_str_p(contacts_record, _contacts_group.image_path, &photoURI);
1599                         if(errorCode != CONTACTS_ERROR_NONE)
1600                         {
1601                                 LogWarning("Fail to get image_path from address book (ret:" << errorCode << ")");
1602                                 continue;
1603                         }
1604
1605                         errorCode = contacts_record_get_str_p(contacts_record, _contacts_group.ringtone_path, &ringtoneURI);
1606                         if(errorCode != CONTACTS_ERROR_NONE)
1607                         {
1608                                 LogWarning("Fail to get ringtone_path from address book (ret:" << errorCode << ")");
1609                                 continue;
1610                         }
1611
1612                         errorCode = contacts_record_get_bool(contacts_record, _contacts_group.is_read_only, &isReadOnly);
1613                         if(errorCode != CONTACTS_ERROR_NONE)
1614                         {
1615                                 LogWarning("Fail to get is_read_only from address book (ret:" << errorCode << ")");
1616                                 continue;
1617                         }
1618
1619                         ContactGroupPtr group = ContactGroupPtr(new ContactGroup());
1620                         group->setId(id);
1621                         group->setAddressBookId(addressBookId);
1622
1623                         if(name != NULL)
1624                                 group->setName(name);
1625                         else
1626                                 group->setName("");
1627
1628                         if(photoURI != NULL)
1629                                 group->setPhotoURI(ContactUtility::convertPathToUri(photoURI));
1630
1631                         if(ringtoneURI != NULL)
1632                                 group->setRingtoneURI(ContactUtility::convertPathToUri(ringtoneURI));
1633
1634                         group->setReadOnly(isReadOnly);
1635
1636                         groups->push_back(group);
1637
1638                         errorCode = contacts_list_next(groups_list);
1639                         if(errorCode != CONTACTS_ERROR_NONE)
1640                         {
1641                                 LogWarning("Fail to get next address book (ret:" << errorCode << ")");
1642                                 break;
1643                         }
1644                 }
1645
1646                 event->setContactGroups(groups);
1647                 event->setResult(true);
1648
1649         }
1650         Catch (NotFoundException)
1651         {
1652                 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
1653                 event->setExceptionCode(ExceptionCodes::NotFoundException);
1654                 event->setResult(false);
1655
1656         }
1657         Catch (PlatformException)
1658         {
1659                 LogError("Error during getting contact : " << _rethrown_exception.GetMessage());
1660                 event->setExceptionCode(ExceptionCodes::PlatformException);
1661                 event->setResult(false);
1662
1663         }
1664         Catch (InvalidArgumentException)
1665         {
1666                 LogError("Invalid Arguments : " << _rethrown_exception.GetMessage());
1667                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1668                 event->setResult(false);
1669
1670         }
1671         Catch(Exception)
1672         {
1673                 LogError("Error on platform : " << _rethrown_exception.GetMessage());
1674                 event->setExceptionCode(ExceptionCodes::UnknownException);
1675                 event->setResult(false);
1676         }
1677
1678         if(groups_list != NULL)
1679                 contacts_list_destroy(groups_list, true);
1680 }
1681
1682 void AddressBook::onContactsSvcContactsAdded(ContactArrayPtr &contacts)
1683 {
1684         EventInfoAddressBookChangeAddedPtr contactsAdded(new EventInfoAddressBookChangeAdded());
1685         contactsAdded->setContacts(contacts);
1686         EventInfoAddressBookChangePtr event = DPL::StaticPointerCast<EventInfoAddressBookChange>(contactsAdded);
1687         EventAddressBookChangeListenerPtr listener(new EventAddressBookChangeListener(event));
1688         m_addressBookEmitters.emit(listener);
1689 }
1690
1691 void AddressBook::onContactsSvcContactsUpdated(ContactArrayPtr &contacts)
1692 {
1693         EventInfoAddressBookChangeUpdatedPtr contactsUpdated(new EventInfoAddressBookChangeUpdated());
1694         contactsUpdated->setContacts(contacts);
1695         EventInfoAddressBookChangePtr event = DPL::StaticPointerCast<EventInfoAddressBookChange>(contactsUpdated);
1696         EventAddressBookChangeListenerPtr listener(new EventAddressBookChangeListener(event));
1697         m_addressBookEmitters.emit(listener);
1698 }
1699
1700 void AddressBook::onContactsSvcContactsRemoved(StringArrayPtr &contactIds)
1701 {
1702         EventInfoAddressBookChangeRemovedPtr contactsRemoved(new EventInfoAddressBookChangeRemoved());
1703         contactsRemoved->setContactIds(contactIds);
1704         EventInfoAddressBookChangePtr event = DPL::StaticPointerCast<EventInfoAddressBookChange>(contactsRemoved);
1705         EventAddressBookChangeListenerPtr listener(new EventAddressBookChangeListener(event));
1706         m_addressBookEmitters.emit(listener);
1707 }
1708
1709 void AddressBook::contactsAddBatchResultCallback(int error, int *ids, unsigned int count, void *user_data)
1710 {
1711         LogDebug("entered");
1712         if(user_data == NULL)
1713         {
1714                 LogError("user_data is NULL");
1715                 return;
1716         }
1717
1718         KeySharePtrPair *keyPair = (KeySharePtrPair*)user_data;
1719
1720         long key = keyPair->key;
1721         AddressBook *addressBook = keyPair->addressBook;
1722
1723         delete keyPair;
1724
1725         addressBook->contactsAddBatchResultCallback(error, ids, count, key);
1726 }
1727
1728 void AddressBook::contactsAddBatchResultCallback(int error, int *ids, unsigned int count, long key)
1729 {
1730         EventAddressBookAddBatchPtr event;
1731
1732         EventAddressBookAddBatchMap::iterator iter;
1733         iter = m_addBatchEventMap.find(key);
1734         if(iter == m_addBatchEventMap.end())
1735         {
1736                 LogError("No event for key : " << key);
1737                 return;
1738         }
1739
1740         event = iter->second;
1741         m_addBatchEventMap.erase(iter);
1742
1743         if(error != CONTACTS_ERROR_NONE)
1744         {
1745                 LogError("contacts_db_insert_result_cb gives error : " << error);
1746                 event->setResult(false);
1747                 event->setExceptionCode(ExceptionCodes::PlatformException);
1748                 EventRequestReceiver<EventAddressBookAddBatch>::ManualAnswer(event);
1749                 return;
1750         }
1751
1752 #if 0
1753         ContactArrayPtr contacts(new ContactArray());
1754 #else
1755         ContactArrayPtr contacts = event->getContacts();
1756 #endif
1757
1758         for(unsigned int i=0; i<count; i++)
1759         {
1760                 int errorCode = 0;
1761                 int contactId = ids[i];
1762                 LogDebug("contact id : " << contactId);
1763
1764                 contacts_record_h contacts_record = NULL;
1765
1766                 errorCode = contacts_db_get_record(_contacts_contact._uri, contactId, &contacts_record);
1767                 if(errorCode != CONTACTS_ERROR_NONE)
1768                 {
1769                         LogDebug("Fail contacts_db_get_record error _contacts_contact._uri : " << errorCode);
1770                         continue;
1771                 }
1772
1773 #if 0
1774                 ContactPtr absContact(NULL);
1775 #else
1776                 ContactPtr absContact = contacts->at(i);
1777 #endif
1778
1779                 Try
1780                 {
1781 #if 0
1782                         ContactObjectP2AConverterPtr contactObjConverter(
1783                                         new ContactObjectP2AConverter(contacts_record, false) );
1784 #else
1785                         ContactObjectP2AConverterPtr contactObjConverter(
1786                                         new ContactObjectP2AConverter(contacts_record, false, absContact) );
1787 #endif
1788                         absContact = contactObjConverter->getAbstractContact();
1789                 }
1790                 Catch(Exception)
1791                 {
1792                         LogError("Fail error on converting contact to abs contact : " << _rethrown_exception.GetMessage());
1793                         continue;
1794                 }
1795
1796 #if 0
1797                 if(absContact != NULL)
1798                         contacts->push_back(absContact);
1799 #endif
1800
1801                 if(contacts_record != NULL)
1802                         contacts_record_destroy(contacts_record, true);
1803         }
1804
1805         event->setContacts(contacts);
1806         event->setResult(true);
1807         event->setExceptionCode(ExceptionCodes::None);
1808         EventRequestReceiver<EventAddressBookAddBatch>::ManualAnswer(event);
1809 }
1810
1811 void AddressBook::contactsUpdateBatchResultCallback(int error, void *user_data)
1812 {
1813         LogDebug("entered");
1814
1815         if(user_data == NULL)
1816         {
1817                 LogError("user_data is NULL");
1818                 return;
1819         }
1820
1821         KeySharePtrPair *keyPair = (KeySharePtrPair*)user_data;
1822
1823         long key = keyPair->key;
1824         AddressBook *addressBook = keyPair->addressBook;
1825
1826         delete keyPair;
1827
1828         addressBook->contactsUpdateBatchResultCallback(error, key);
1829 }
1830
1831 void AddressBook::contactsUpdateBatchResultCallback(int error, long key)
1832 {
1833         EventAddressBookUpdateBatchPtr event;
1834
1835         EventAddressBookUpdateBatchMap::iterator iter;
1836         iter = m_updateBatchEventMap.find(key);
1837         if(iter == m_updateBatchEventMap.end())
1838         {
1839                 LogError("No event for key : " << key);
1840                 return;
1841         }
1842
1843         event = iter->second;
1844         m_updateBatchEventMap.erase(iter);
1845
1846         if(error != CONTACTS_ERROR_NONE)
1847         {
1848                 LogError("contacts_db_result_cb gives error : " << error);
1849                 event->setResult(false);
1850                 event->setExceptionCode(ExceptionCodes::PlatformException);
1851                 EventRequestReceiver<EventAddressBookUpdateBatch>::ManualAnswer(event);
1852                 return;
1853         }
1854
1855         event->setResult(true);
1856         event->setExceptionCode(ExceptionCodes::None);
1857         EventRequestReceiver<EventAddressBookUpdateBatch>::ManualAnswer(event);
1858 }
1859
1860 void AddressBook::contactsRemoveBatchResultCallback(int error, void *user_data)
1861 {
1862         LogDebug("entered");
1863
1864         if(user_data == NULL)
1865         {
1866                 LogError("user_data is NULL");
1867                 return;
1868         }
1869
1870         KeySharePtrPair *keyPair = (KeySharePtrPair*)user_data;
1871
1872         long key = keyPair->key;
1873         AddressBook *addressBook = keyPair->addressBook;
1874
1875         delete keyPair;
1876
1877         addressBook->contactsRemoveBatchResultCallback(error, key);
1878 }
1879
1880 void AddressBook::contactsRemoveBatchResultCallback(int error, long key)
1881 {
1882         EventAddressBookRemoveBatchPtr event;
1883
1884         EventAddressBookRemoveBatchMap::iterator iter;
1885         iter = m_removeBatchEventMap.find(key);
1886         if(iter == m_removeBatchEventMap.end())
1887         {
1888                 LogError("No event for key : " << key);
1889                 return;
1890         }
1891
1892         event = iter->second;
1893         m_removeBatchEventMap.erase(iter);
1894
1895         if(error != CONTACTS_ERROR_NONE)
1896         {
1897                 LogError("contacts_db_result_cb gives error : " << error);
1898                 event->setResult(false);
1899                 event->setExceptionCode(ExceptionCodes::PlatformException);
1900                 EventRequestReceiver<EventAddressBookRemoveBatch>::ManualAnswer(event);
1901                 return;
1902         }
1903
1904         event->setResult(true);
1905         event->setExceptionCode(ExceptionCodes::None);
1906         EventRequestReceiver<EventAddressBookRemoveBatch>::ManualAnswer(event);
1907 }
1908
1909 } // Contact
1910 } // DeviceAPI