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