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