2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file AddressBook.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
25 #include "AddressBook.h"
30 #include <dpl/exception.h>
31 #include <Commons/Exception.h>
32 #include <Commons/Regex.h>
33 #include "IAddressBook.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"
48 using namespace DeviceAPI::Tizen;
49 using namespace WrtDeviceApis::Commons;
52 AddressBook::AddressBook(bool isUnified) :
55 m_name("TEST_ADDRESS_BOOK"),
58 m_isUnifiedAddressBook(isUnified),
64 AddressBook::~AddressBook()
66 if(m_addressBookEmitters.size() != 0)
68 ContactsSvcChangeListenerManagerSingleton::Instance().unregisterContactsChangeListener(this);
72 std::string AddressBook::getId() const
74 if(m_isUnifiedAddressBook)
77 return ContactUtility::intToStr(m_id);
80 void AddressBook::setId(const std::string &value)
82 m_id = ContactUtility::strToInt(value);
85 std::string AddressBook::getAccountId() const
87 if(m_isUnifiedAddressBook)
90 return ContactUtility::intToStr(m_accountId);
93 void AddressBook::setAccountId(const std::string &value)
95 m_accountId = ContactUtility::strToInt(value);
98 void AddressBook::OnRequestReceived(const EventAddressBookGetPtr &event)
103 contacts_record_h contacts_record = NULL;
107 if(!event->getIdIsSet())
108 ThrowMsg(InvalidArgumentException, "Invalid");
110 string contactId = event->getId();
112 int contactIdInt = ContactUtility::strToInt(contactId);
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 ");
118 if(!m_isUnifiedAddressBook)
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.");
126 ContactObjectP2AConverterPtr contactObjConverter(
127 new ContactObjectP2AConverter(contacts_record, false));
128 ContactPtr contact = contactObjConverter->getAbstractContact();
130 event->setContact(contact);
131 event->setResult(true);
133 Catch (NotFoundException)
135 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
136 event->setExceptionCode(ExceptionCodes::NotFoundException);
137 event->setResult(false);
139 Catch (PlatformException)
141 LoggerE("Error during getting contact : " << _rethrown_exception.GetMessage());
142 event->setExceptionCode(ExceptionCodes::PlatformException);
143 event->setResult(false);
145 Catch (InvalidArgumentException)
147 LoggerE("Invalid Arguments : " << _rethrown_exception.GetMessage());
148 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
149 event->setResult(false);
153 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
154 event->setExceptionCode(ExceptionCodes::UnknownException);
155 event->setResult(false);
158 if(contacts_record != NULL)
159 contacts_record_destroy(contacts_record, true);
162 void AddressBook::OnRequestReceived(const EventAddressBookAddPtr &event)
168 contacts_record_h contacts_record = NULL;
169 contacts_record_h get_contacts_record = NULL;
170 ContactPtr contact(NULL);
174 if(!event->getContactIsSet())
175 ThrowMsg(InvalidArgumentException, "Contacts were not set.");
177 contact = event->getContact();
179 ThrowMsg(InvalidArgumentException, "No contacts.");
180 id = ContactUtility::strToInt(contact->getId());
182 ThrowMsg(InvalidArgumentException, "invalid contact object");
185 Catch(InvalidArgumentException)
187 LoggerE("Invalid arguments for adding contacts : " << _rethrown_exception.GetMessage());
188 event->setResult(false);
189 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
195 DPL::SharedPtr<Contact> contactT =
196 DPL::StaticPointerCast<Contact>(contact);
198 ContactObjectA2PConverterPtr contactObjConverter(NULL);
200 contacts_record = contactT->getPlatformContactObject();
201 if(contacts_record == NULL)
203 contactObjConverter = ContactObjectA2PConverterPtr(
204 new ContactObjectA2PConverter(contact, false) );
205 contacts_record = contactObjConverter->getPlatformContact();
209 contactObjConverter = ContactObjectA2PConverterPtr(
210 new ContactObjectA2PConverter(contact, false, contacts_record) );
211 contacts_record = contactObjConverter->getPlatformContact();
215 if (!contacts_record)
217 LoggerE("Error during converting contact object");
218 ThrowMsg(PlatformException, "Error during converting contact object");
221 if(m_isUnifiedAddressBook)
222 errorCode = contacts_record_set_int(contacts_record, _contacts_contact.address_book_id, 0);
224 errorCode = contacts_record_set_int(contacts_record, _contacts_contact.address_book_id, m_id);
225 if(errorCode != CONTACTS_ERROR_NONE)
227 LoggerE("error code : " << errorCode);
228 ThrowMsg(PlatformException, "Error during executing contacts_db_insert_record()");
231 errorCode = contacts_db_insert_record(contacts_record, &contactId);
232 if(errorCode != CONTACTS_ERROR_NONE)
234 LoggerE("error code : " << errorCode);
235 ThrowMsg(PlatformException, "Error during executing contacts_db_insert_record()");
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");
242 ContactObjectP2AConverterPtr contactObjConverterForInserted(
243 new ContactObjectP2AConverter(get_contacts_record, false));
244 ContactPtr insertedContact = contactObjConverterForInserted->getAbstractContact();
246 contact->copy(insertedContact);
247 event->setResult(true);
248 event->setExceptionCode(ExceptionCodes::None);
250 Catch (PlatformException)
252 LoggerE("Error during adding contact : " << _rethrown_exception.GetMessage());
253 event->setResult(false);
254 event->setExceptionCode(ExceptionCodes::PlatformException);
258 LoggerE("Error during adding contact : " << _rethrown_exception.GetMessage());
259 event->setResult(false);
260 event->setExceptionCode(ExceptionCodes::UnknownException);
263 if(get_contacts_record != NULL)
264 contacts_record_destroy(get_contacts_record, true);
267 void AddressBook::AddressBookAddBatch(const EventAddressBookAddBatchPtr &event)
272 contacts_list_h contacts_list = NULL;
273 ContactArrayPtr contacts(NULL);
275 if(!event->getContactsIsSet())
276 ThrowMsg(InvalidArgumentException, "Contacts were not set.");
278 contacts = event->getContacts();
280 ThrowMsg(InvalidArgumentException, "No contacts.");
282 errorCode = contacts_list_create(&contacts_list);
283 if(errorCode != CONTACTS_ERROR_NONE)
285 ThrowMsg(PlatformException, "Fail to create contacts_list_h");
288 for(ContactArray::iterator i = contacts->begin(); i != contacts->end(); i++)
290 contacts_record_h contacts_record = NULL;
291 ContactPtr contact = *i;
292 ContactObjectA2PConverterPtr contactObjConverter(NULL);
296 DPL::SharedPtr<Contact> newContactT =
297 DPL::StaticPointerCast<Contact>(contact);
298 contacts_record = newContactT->getPlatformContactObject();
299 if(contacts_record == NULL)
301 errorCode = contacts_record_create(_contacts_contact._uri, &contacts_record);
302 if(errorCode != CONTACTS_ERROR_NONE)
304 ThrowMsg(PlatformException, "Error during creating contact record : " << errorCode);
308 contactObjConverter = ContactObjectA2PConverterPtr(
309 new ContactObjectA2PConverter(contact, false, contacts_record) );
310 contacts_record = contactObjConverter->getPlatformContact();
312 if(contacts_record == NULL)
314 ThrowMsg(PlatformException, "Error during converting contact object");
317 if(m_isUnifiedAddressBook)
318 errorCode = contacts_record_set_int(contacts_record, _contacts_contact.address_book_id, 0);
320 errorCode = contacts_record_set_int(contacts_record, _contacts_contact.address_book_id, m_id);
321 if(errorCode != CONTACTS_ERROR_NONE)
323 ThrowMsg(PlatformException, "Error during add address book : " << errorCode);
326 errorCode = contacts_list_add(contacts_list, contacts_record);
327 if(errorCode != CONTACTS_ERROR_NONE)
329 ThrowMsg(PlatformException, "Error during add to list : " << errorCode);
332 Catch(InvalidArgumentException)
334 if(contacts_list != NULL)
335 contacts_list_destroy(contacts_list, true);
337 ThrowMsg(InvalidArgumentException, "Error during converting contact object");
338 // LoggerE("Error during converting contact object : " << _rethrown_exception.GetMessage());
341 Catch(PlatformException)
343 if(contacts_list != NULL)
344 contacts_list_destroy(contacts_list, true);
346 ThrowMsg(PlatformException, "Error during converting contact object");
347 // LoggerE("Error during converting contact object : " << _rethrown_exception.GetMessage());
352 if(contacts_list != NULL)
353 contacts_list_destroy(contacts_list, true);
355 ThrowMsg(Exception, "Error during converting contact object");
356 // LoggerE("Error during converting contact object : " << _rethrown_exception.GetMessage());
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)
368 if(contacts_list != NULL)
369 contacts_list_destroy(contacts_list, true);
371 ThrowMsg(PlatformException, "Error during contacts_db_insert_records_async");
374 errorCode = contacts_list_destroy(contacts_list, true);
375 contacts_list = NULL;
376 if(errorCode != CONTACTS_ERROR_NONE)
379 if(contacts_list != NULL)
380 contacts_list_destroy(contacts_list, true);
382 ThrowMsg(PlatformException, "Error during contacts_list_destroy");
385 pair<long, EventAddressBookAddBatchPtr> keyEventPair(m_eventMapAcc, event);
386 m_addBatchEventMap.insert(keyEventPair);
392 void AddressBook::OnRequestReceived(const EventAddressBookAddBatchPtr &event)
398 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
400 AddressBookAddBatch(event);
402 ContactQueueManagerSingleton::Instance().push(ContactQueueManager::ADDBATCH, this, event);
404 ContactQueueManagerSingleton::Instance().increaseQueueList();
405 event->switchToManualAnswer();
407 Catch (NotFoundException)
409 LoggerE("NotFoundException during adding contacts : " << _rethrown_exception.GetMessage());
410 event->setResult(false);
411 event->setExceptionCode(ExceptionCodes::NotFoundException);
413 Catch (PlatformException)
415 LoggerE("PlatformException during adding contacts : " << _rethrown_exception.GetMessage());
416 event->setResult(false);
417 event->setExceptionCode(ExceptionCodes::PlatformException);
419 Catch(InvalidArgumentException)
421 LoggerE("InvalidArgumentException during adding contacts : " << _rethrown_exception.GetMessage());
422 event->setResult(false);
423 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
427 LoggerE("Error during adding contacts : " << _rethrown_exception.GetMessage());
428 event->setResult(false);
429 event->setExceptionCode(ExceptionCodes::PlatformException);
433 void AddressBook::OnRequestReceived(const EventAddressBookUpdatePtr &event)
436 ContactPtr contact(NULL);
440 if(!event->getContactIsSet())
441 ThrowMsg(InvalidArgumentException, "Contacts were not set.");
443 contact = event->getContact();
445 ThrowMsg(InvalidArgumentException, "No contact.");
447 if(!contact->getIdIsSet() || !contact->getAddressBookIdIsSet())
448 ThrowMsg(InvalidArgumentException, "Invalid Contact");
450 if(!m_isUnifiedAddressBook && contact->getAddressBookId() != getId())
451 ThrowMsg(InvalidArgumentException, "Wrong address book");
453 Catch(InvalidArgumentException)
455 LoggerE("Invalid arguments for adding contacts : " << _rethrown_exception.GetMessage());
456 event->setResult(false);
457 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
465 DPL::SharedPtr<Contact> contactT =
466 DPL::StaticPointerCast<Contact>(contact);
468 ContactObjectA2PConverterPtr contactObjConverter(NULL);
470 contacts_record_h contacts_record = contactT->getPlatformContactObject();
471 if(contacts_record == NULL)
473 contactObjConverter = ContactObjectA2PConverterPtr(
474 new ContactObjectA2PConverter(contact, false) );
475 contacts_record = contactObjConverter->getPlatformContact();
479 contactObjConverter = ContactObjectA2PConverterPtr(
480 new ContactObjectA2PConverter(contact, false, contacts_record) );
481 contacts_record = contactObjConverter->getPlatformContact();
484 if (!contacts_record)
486 LoggerE("Error during converting contact object");
487 ThrowMsg(PlatformException, "Error during converting contact object");
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)
495 LoggerE("error code : " << errorCode);
496 ThrowMsg(PlatformException, "Error during executing contacts_db_update_record()");
499 Catch (NotFoundException)
501 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
502 event->setResult(false);
503 event->setExceptionCode(ExceptionCodes::NotFoundException);
505 Catch (PlatformException)
507 LoggerE("Error during adding contact : " << _rethrown_exception.GetMessage());
508 event->setResult(false);
509 event->setExceptionCode(ExceptionCodes::PlatformException);
513 LoggerE("Error during adding contact : " << _rethrown_exception.GetMessage());
514 event->setResult(false);
515 event->setExceptionCode(ExceptionCodes::PlatformException);
517 //m_latestVersion = get_contact_version();
519 event->setResult(true);
520 event->setExceptionCode(ExceptionCodes::None);
523 void AddressBook::AddressBookUpdateBatch(const EventAddressBookUpdateBatchPtr &event)
527 contacts_list_h contacts_list = NULL;
528 ContactArrayPtr contacts(NULL);
530 if(!event->getContactsIsSet())
531 ThrowMsg(InvalidArgumentException, "Contacts were not set.");
533 contacts = event->getContacts();
535 ThrowMsg(InvalidArgumentException, "No contacts.");
537 errorCode = contacts_list_create(&contacts_list);
538 if(errorCode != CONTACTS_ERROR_NONE)
540 if(contacts_list != NULL)
541 contacts_list_destroy(contacts_list, false);
543 ThrowMsg(PlatformException, "Fail to create contacts_list_h");
546 for(ContactArray::iterator i = contacts->begin(); i != contacts->end(); i++)
548 contacts_record_h contacts_record = NULL;
549 ContactPtr contact = *i;
550 ContactObjectA2PConverterPtr contactObjConverter(NULL);
555 ThrowMsg(InvalidArgumentException, "No contact.");
557 if(contact->getIdIsSet() == false)
558 ThrowMsg(InvalidArgumentException, "Invalid Contact");
560 if(!m_isUnifiedAddressBook && ( !contact->getAddressBookIdIsSet() || contact->getAddressBookId() != getId()))
561 ThrowMsg(InvalidArgumentException, "Wrong address book");
563 DPL::SharedPtr<Contact> newContactT = DPL::StaticPointerCast<Contact>(contact);
564 contacts_record = newContactT->getPlatformContactObject();
565 if(contacts_record == NULL)
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");
572 contactObjConverter = ContactObjectA2PConverterPtr(
573 new ContactObjectA2PConverter(contact, false, contacts_record) );
574 contacts_record = contactObjConverter->getPlatformContact();
576 if(contacts_record == NULL)
577 ThrowMsg(PlatformException, "Error during converting contact object");
579 errorCode = contacts_list_add(contacts_list, contacts_record);
580 if(errorCode != CONTACTS_ERROR_NONE)
581 ThrowMsg(PlatformException, "Error during add to list");
583 Catch(NotFoundException)
585 if(contacts_list != NULL)
586 contacts_list_destroy(contacts_list, false);
588 ThrowMsg(NotFoundException, "Error during converting contact object");
589 // LoggerE("Error during converting contact object : " << _rethrown_exception.GetMessage());
592 Catch(InvalidArgumentException)
594 if(contacts_list != NULL)
595 contacts_list_destroy(contacts_list, false);
597 ThrowMsg(InvalidArgumentException, "Error during converting contact object");
598 // LoggerE("Error during converting contact object : " << _rethrown_exception.GetMessage());
601 Catch(PlatformException)
603 if(contacts_list != NULL)
604 contacts_list_destroy(contacts_list, false);
606 ThrowMsg(PlatformException, "Error during converting contact object");
607 // LoggerE("Error during converting contact object : " << _rethrown_exception.GetMessage());
612 if(contacts_list != NULL)
613 contacts_list_destroy(contacts_list, false);
615 ThrowMsg(Exception, "Error during converting contact object");
616 // LoggerE("Error during converting contact object : " << _rethrown_exception.GetMessage());
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)
628 if(contacts_list != NULL)
629 contacts_list_destroy(contacts_list, false);
631 ThrowMsg(PlatformException, "Error during contacts_db_update_records_async");
634 errorCode = contacts_list_destroy(contacts_list, true);
635 contacts_list = NULL;
636 if(errorCode != CONTACTS_ERROR_NONE)
639 if(contacts_list != NULL)
640 contacts_list_destroy(contacts_list, false);
642 ThrowMsg(PlatformException, "Error during contacts_list_destroy");
645 pair<long, EventAddressBookUpdateBatchPtr> keyEventPair(m_eventMapAcc, event);
646 m_updateBatchEventMap.insert(keyEventPair);
651 void AddressBook::OnRequestReceived(const EventAddressBookUpdateBatchPtr &event)
657 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
659 AddressBookUpdateBatch(event);
661 ContactQueueManagerSingleton::Instance().push(ContactQueueManager::UPATEBATCH, this, event);
663 ContactQueueManagerSingleton::Instance().increaseQueueList();
664 event->switchToManualAnswer();
666 Catch (NotFoundException)
668 LoggerE("NotFoundException during updating contacts : " << _rethrown_exception.GetMessage());
669 event->setResult(false);
670 event->setExceptionCode(ExceptionCodes::NotFoundException);
672 Catch (PlatformException)
674 LoggerE("PlatformException during updating contacts : " << _rethrown_exception.GetMessage());
675 event->setResult(false);
676 event->setExceptionCode(ExceptionCodes::PlatformException);
678 Catch(InvalidArgumentException)
680 LoggerE("InvalidArgumentException during updating contacts : " << _rethrown_exception.GetMessage());
681 event->setResult(false);
682 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
686 LoggerE("Exception during updating contacts : " << _rethrown_exception.GetMessage());
687 event->setResult(false);
688 event->setExceptionCode(ExceptionCodes::PlatformException);
692 void AddressBook::OnRequestReceived(const EventAddressBookRemovePtr &event)
699 if(!event->getContactIdIsSet())
700 ThrowMsg(InvalidArgumentException, "Contact id was not set.");
702 string contactIdStr = event->getContactId();
704 if(!ContactUtility::checkStrIsUInt(contactIdStr))
705 ThrowMsg(InvalidArgumentException, "Id is wrong" );
707 int contactId = ContactUtility::strToInt(contactIdStr);
709 if(!m_isUnifiedAddressBook)
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 ");
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");
721 if(addressBookId != m_id)
722 ThrowMsg(PlatformException, "Contact is not a member of this address book.");
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()");
731 Catch (InvalidArgumentException)
733 LoggerE("Invalid contact id : " << _rethrown_exception.GetMessage());
734 event->setResult(false);
735 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
738 Catch (NotFoundException)
740 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
741 event->setResult(false);
742 event->setExceptionCode(ExceptionCodes::NotFoundException);
745 Catch (PlatformException)
747 LoggerE("Error during deleting contact : " << _rethrown_exception.GetMessage());
748 event->setResult(false);
749 event->setExceptionCode(ExceptionCodes::PlatformException);
754 LoggerE("Error during deleting contact : " << _rethrown_exception.GetMessage());
755 event->setResult(false);
756 event->setExceptionCode(ExceptionCodes::PlatformException);
760 event->setResult(true);
761 event->setExceptionCode(ExceptionCodes::None);
764 void AddressBook::AddressBookRemoveBatch(const EventAddressBookRemoveBatchPtr &event)
768 StringArrayPtr contactIds(NULL);
770 if(!event->getContactIdsIsSet())
771 ThrowMsg(InvalidArgumentException, "Contact IDs were not set.");
773 contactIds = event->getContactIds();
775 ThrowMsg(InvalidArgumentException, "Invalid contacts");
777 int *ids = new int[contactIds->size()];
778 // int *tmpIds = new int[contactIds->size()];
781 if(errorCode != CONTACTS_ERROR_NONE)
783 ThrowMsg(PlatformException, "Fail to create contacts_list_h");
786 for(StringArray::iterator i = contactIds->begin(); i != contactIds->end(); i++)
788 string contactIdStr = *i;
794 if(!ContactUtility::checkStrIsUInt(contactIdStr))
795 ThrowMsg(InvalidArgumentException, "Wrong" );
797 contactId = ContactUtility::strToInt(contactIdStr);
799 if(!m_isUnifiedAddressBook)
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");
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");
811 if(addressBookId != m_id)
812 ThrowMsg(PlatformException, "Contact is not a member of this address book.");
815 ids[count] = contactId;
816 // tmpIds[count] = contactId;
821 ThrowMsg(NotFoundException, "Error during converting contact object");
822 // LoggerE("Error during converting contact object : " << _rethrown_exception.GetMessage());
827 contacts_filter_h filter = NULL;
828 contacts_query_h query = NULL;
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__ << ")");
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__ << ")");
838 for(int i = 0; i < contactIds->size(); i++)
840 errorCode = contacts_filter_add_int(filter, _contacts_simple_contact.id, CONTACTS_MATCH_EQUAL, tmpIds[i]);
841 if(i == (contactIds->size() - 1))
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__ << ")");
849 errorCode = contacts_query_set_filter(query, filter);
850 if(errorCode != CONTACTS_ERROR_NONE)
851 ThrowMsg(PlatformException, "contacts_query_set_filter error : " << errorCode << " (" << __FUNCTION__ << ")");
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__ << ")");
859 contacts_filter_destroy(filter);
861 contacts_query_destroy(query);
863 if(contactIds->size() != (unsigned int)record_count)
864 ThrowMsg(InvalidArgumentException, "Ids' db count : " << record_count << " (" << __FUNCTION__ << ")");
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);
880 if(errorCode != CONTACTS_ERROR_NONE)
883 ThrowMsg(PlatformException, "Error during add to list");
886 pair<long, EventAddressBookRemoveBatchPtr> keyEventPair(m_eventMapAcc, event);
887 m_removeBatchEventMap.insert(keyEventPair);
892 void AddressBook::OnRequestReceived(const EventAddressBookRemoveBatchPtr &event)
898 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
900 AddressBookRemoveBatch(event);
902 ContactQueueManagerSingleton::Instance().push(ContactQueueManager::DELETEBATCH, this, event);
904 ContactQueueManagerSingleton::Instance().increaseQueueList();
905 event->switchToManualAnswer();
907 Catch (NotFoundException)
909 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
910 event->setResult(false);
911 event->setExceptionCode(ExceptionCodes::NotFoundException);
913 Catch (PlatformException)
915 LoggerE("Error during deleting contacts : " << _rethrown_exception.GetMessage());
916 event->setResult(false);
917 event->setExceptionCode(ExceptionCodes::PlatformException);
919 Catch(InvalidArgumentException)
921 LoggerE("Invalid contact id : " << _rethrown_exception.GetMessage());
922 event->setResult(false);
923 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
927 LoggerE("Error during deleting contacts : " << _rethrown_exception.GetMessage());
928 event->setResult(false);
929 event->setExceptionCode(ExceptionCodes::PlatformException);
933 void AddressBook::OnRequestReceived(const EventAddressBookFindPtr &event)
937 contacts_query_h query = NULL;
938 contacts_filter_h filter = NULL;
939 contacts_list_h contacts_list = NULL;
941 ContactArrayPtr contacts = ContactArrayPtr(new ContactArray());
945 ContactSearchEnginePtr searchEngine(new ContactSearchEngine());
947 if(!m_isUnifiedAddressBook)
948 searchEngine->setAddressBookId(m_id);
950 if(event->getFilterIsSet())
952 FilterPtr filter = event->getFilter();
954 FilterValidatorPtr validator = ContactFilterValidatorFactory::getContactFilterValidator();
955 bool success = filter->validate(validator);
958 ThrowMsg(InvalidArgumentException, "Invalid filter arguments.");
960 searchEngine->setCondition(filter);
963 if (event->getSortModeIsSet())
965 searchEngine->setSortMode(event->getSortMode());
968 searchEngine->setSortMode();
970 event->setContacts(searchEngine->getContactSearchResult());
971 event->setResult(true);
973 Catch (NotFoundException)
975 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
976 event->setExceptionCode(ExceptionCodes::NotFoundException);
977 event->setResult(false);
979 Catch (PlatformException)
981 LoggerE("Error during finding contact : " << _rethrown_exception.GetMessage());
982 event->setExceptionCode(ExceptionCodes::PlatformException);
983 event->setResult(false);
985 Catch (InvalidArgumentException)
987 LoggerE("Invalid Arguments : " << _rethrown_exception.GetMessage());
988 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
989 event->setResult(false);
993 LoggerE("Error during deleting contact : " << _rethrown_exception.GetMessage());
994 event->setExceptionCode(ExceptionCodes::PlatformException);
995 event->setResult(false);
999 contacts_filter_destroy(filter);
1002 contacts_query_destroy(query);
1004 if(contacts_list != NULL)
1005 contacts_list_destroy(contacts_list, true);
1008 void AddressBook::OnRequestReceived(const EventAddressBookAddChangeListenerPtr &event)
1014 if(!event->getEmitterIsSet())
1015 ThrowMsg(InvalidArgumentException, "Invalid arguments.");
1017 EventAddressBookChangeListenerEmitterPtr emitter = event->getEmitter();
1019 ThrowMsg(InvalidArgumentException, "Invalid arguments.");
1021 if(m_addressBookEmitters.size() == 0)
1023 LoggerD("Watch registered initially");
1025 if(m_isUnifiedAddressBook)
1026 ContactsSvcChangeListenerManagerSingleton::Instance().registerContactsChangeListener(this, -1);
1028 ContactsSvcChangeListenerManagerSingleton::Instance().registerContactsChangeListener(this, m_id);
1031 m_addressBookEmitters.attach(emitter);
1033 long id = ContactsSvcChangeListenerManagerSingleton::Instance().getWatchIdAndInc();
1034 m_watchIdMap[id] = emitter->getId();
1037 event->setResult(true);
1039 Catch (NotFoundException)
1041 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
1042 event->setExceptionCode(ExceptionCodes::NotFoundException);
1043 event->setResult(false);
1045 Catch (PlatformException)
1047 LoggerE("Error during deleting contact : " << _rethrown_exception.GetMessage());
1048 event->setExceptionCode(ExceptionCodes::PlatformException);
1049 event->setResult(false);
1051 Catch (InvalidArgumentException)
1053 LoggerE("Invalid Arguments : " << _rethrown_exception.GetMessage());
1054 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1055 event->setResult(false);
1059 LoggerE("Error during deleting contact : " << _rethrown_exception.GetMessage());
1060 event->setExceptionCode(ExceptionCodes::PlatformException);
1061 event->setResult(false);
1065 void AddressBook::OnRequestReceived(const EventAddressBookRemoveChangeListenerPtr &event)
1071 if(!event->getIdIsSet())
1072 ThrowMsg(InvalidArgumentException, "Invalid arguments.");
1074 long id = event->getId();
1075 if(m_watchIdMap.find(id) == m_watchIdMap.end())
1076 ThrowMsg(NotFoundException, "No watchId 1: ");
1078 bool success = m_addressBookEmitters.detach(m_watchIdMap[id]);
1080 ThrowMsg(NotFoundException, "No watchId 2 ");
1082 m_watchIdMap.erase(id);
1084 if(m_addressBookEmitters.size() == 0)
1086 LoggerD("No watcher is registered. unsubscribing from contact service.");
1088 ContactsSvcChangeListenerManagerSingleton::Instance().unregisterContactsChangeListener(this);
1091 event->setResult(true);
1094 Catch (NotFoundException)
1096 LoggerE("WatchId doesn't exist : " << _rethrown_exception.GetMessage());
1097 event->setExceptionCode(ExceptionCodes::NotFoundException);
1098 event->setResult(false);
1100 Catch (PlatformException)
1102 LoggerE("Error during deleting contact : " << _rethrown_exception.GetMessage());
1103 event->setExceptionCode(ExceptionCodes::PlatformException);
1104 event->setResult(false);
1106 Catch (InvalidArgumentException)
1108 LoggerE("Invalid Arguments : " << _rethrown_exception.GetMessage());
1109 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1110 event->setResult(false);
1114 LoggerE("Error during deleting contact : " << _rethrown_exception.GetMessage());
1115 event->setExceptionCode(ExceptionCodes::PlatformException);
1116 event->setResult(false);
1120 void AddressBook::OnRequestReceived(const EventAddressBookGetGroupPtr &event)
1125 contacts_record_h contacts_record = NULL;
1129 if(!event->getIdIsSet())
1130 ThrowMsg(InvalidArgumentException, "Invalid group");
1132 string groupId = event->getId();
1134 int groupIdInt = ContactUtility::strToInt(groupId);
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");
1140 if(!m_isUnifiedAddressBook)
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.");
1148 ContactGroupPtr group = ContactGroupPtr(new ContactGroup());
1150 ContactsSvcObjectConverter::convertToAbstract(contacts_record, group);
1152 event->setContactGroup(group);
1153 event->setResult(true);
1156 Catch (NotFoundException)
1158 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
1159 event->setExceptionCode(ExceptionCodes::NotFoundException);
1160 event->setResult(false);
1163 Catch (PlatformException)
1165 LoggerE("Error during getting contact : " << _rethrown_exception.GetMessage());
1166 event->setExceptionCode(ExceptionCodes::PlatformException);
1167 event->setResult(false);
1170 Catch (InvalidArgumentException)
1172 LoggerE("Invalid Arguments : " << _rethrown_exception.GetMessage());
1173 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1174 event->setResult(false);
1179 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
1180 event->setExceptionCode(ExceptionCodes::UnknownException);
1181 event->setResult(false);
1184 if(contacts_record != NULL)
1185 contacts_record_destroy(contacts_record, true);
1188 void AddressBook::OnRequestReceived(const EventAddressBookAddGroupPtr &event)
1194 contacts_record_h contacts_record = NULL;
1195 ContactGroupPtr group(NULL);
1199 if(!event->getContactGroupIsSet())
1200 ThrowMsg(InvalidArgumentException, "group were not set.");
1202 group = event->getContactGroup();
1204 ThrowMsg(InvalidArgumentException, "No group.");
1206 if(group->getIdIsSet() || group->getAddressBookIdIsSet())
1207 ThrowMsg(InvalidArgumentException, "ContactGroup object is wrong");
1209 Catch(InvalidArgumentException)
1211 LoggerE("Invalid arguments for adding contacts : " << _rethrown_exception.GetMessage());
1212 event->setResult(false);
1213 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1219 errorCode = contacts_record_create(_contacts_group._uri, &contacts_record);
1220 if(errorCode != CONTACTS_ERROR_NONE)
1222 LoggerE("error code : " << errorCode);
1223 ThrowMsg(PlatformException, "Error during executing contacts_record_create()");
1226 if(m_isUnifiedAddressBook)
1227 errorCode = contacts_record_set_int(contacts_record, _contacts_group.address_book_id, 0);
1229 errorCode = contacts_record_set_int(contacts_record, _contacts_group.address_book_id, m_id);
1230 if(errorCode != CONTACTS_ERROR_NONE)
1232 LoggerE("error code : " << errorCode);
1233 ThrowMsg(PlatformException, "Error during executing contacts_record_set_int()");
1236 ContactsSvcObjectConverter::convertToPlatform(group, contacts_record);
1239 errorCode = contacts_db_insert_record(contacts_record, &groupId);
1240 if(errorCode != CONTACTS_ERROR_NONE)
1242 LoggerE("error code : " << errorCode);
1243 ThrowMsg(PlatformException, "Error during executing contacts_db_insert_record()");
1245 group->setId(groupId);
1246 group->setAddressBookId(m_id);
1248 event->setResult(true);
1249 event->setExceptionCode(ExceptionCodes::None);
1251 Catch (PlatformException)
1253 LoggerE("Error during adding contact : " << _rethrown_exception.GetMessage());
1254 event->setResult(false);
1255 event->setExceptionCode(ExceptionCodes::PlatformException);
1259 LoggerE("Error during adding contact : " << _rethrown_exception.GetMessage());
1260 event->setResult(false);
1261 event->setExceptionCode(ExceptionCodes::UnknownException);
1264 if(contacts_record != NULL)
1265 contacts_record_destroy(contacts_record, true);
1268 void AddressBook::OnRequestReceived(const EventAddressBookUpdateGroupPtr &event)
1274 contacts_record_h contacts_record = NULL;
1275 ContactGroupPtr group(NULL);
1279 if(!event->getContactGroupIsSet())
1280 ThrowMsg(InvalidArgumentException, "group were not set.");
1282 group = event->getContactGroup();
1284 ThrowMsg(InvalidArgumentException, "No group.");
1286 if(!group->getIdIsSet() || !group->getAddressBookIdIsSet())
1287 ThrowMsg(InvalidArgumentException, "Contact Group is wrong");
1289 if(!m_isUnifiedAddressBook && group->getAddressBookId() != getId())
1290 ThrowMsg(InvalidArgumentException, "Wrong address book");
1292 Catch(InvalidArgumentException)
1294 LoggerE("Invalid arguments for updating group : " << _rethrown_exception.GetMessage());
1295 event->setResult(false);
1296 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1300 if(group->getReadOnly())
1302 LoggerW("Canceling updating readonly data.");
1303 event->setResult(false);
1304 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
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)
1316 LoggerE("error code : " << errorCode);
1317 ThrowMsg(PlatformException, "Error during executing contacts_db_get_record()");
1320 ContactsSvcObjectConverter::convertToPlatform(group, contacts_record);
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)
1327 LoggerE("error code : " << errorCode);
1328 ThrowMsg(PlatformException, "Error during executing contacts_db_update_record()");
1331 event->setResult(true);
1332 event->setExceptionCode(ExceptionCodes::None);
1334 Catch (NotFoundException)
1336 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
1337 event->setResult(false);
1338 event->setExceptionCode(ExceptionCodes::NotFoundException);
1340 Catch (PlatformException)
1342 LoggerE("Error during adding contact : " << _rethrown_exception.GetMessage());
1343 event->setResult(false);
1344 event->setExceptionCode(ExceptionCodes::PlatformException);
1348 LoggerE("Error during adding contact : " << _rethrown_exception.GetMessage());
1349 event->setResult(false);
1350 event->setExceptionCode(ExceptionCodes::PlatformException);
1353 if(contacts_record != NULL)
1354 contacts_record_destroy(contacts_record, true);
1359 void AddressBook::OnRequestReceived(const EventAddressBookRemoveGroupPtr &event)
1366 if(!event->getContactGroupIdIsSet())
1367 ThrowMsg(InvalidArgumentException, "group were not set.");
1369 string groupIdStr = event->getContactGroupId();
1371 if(!ContactUtility::checkStrIsUInt(groupIdStr))
1372 ThrowMsg(InvalidArgumentException, "wrong" );
1374 int groupId = ContactUtility::strToInt(groupIdStr);
1376 if(!m_isUnifiedAddressBook)
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");
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");
1388 if(addressBookId != m_id)
1389 ThrowMsg(PlatformException, "Contact is not a member of this address book.");
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()");
1398 event->setResult(true);
1399 event->setExceptionCode(ExceptionCodes::None);
1401 Catch (InvalidArgumentException)
1403 LoggerE("Invalid group id : " << _rethrown_exception.GetMessage());
1404 event->setResult(false);
1405 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1407 Catch (NotFoundException)
1409 LoggerE("Group doesn't exist : " << _rethrown_exception.GetMessage());
1410 event->setResult(false);
1411 event->setExceptionCode(ExceptionCodes::NotFoundException);
1413 Catch (PlatformException)
1415 LoggerE("Error during removing group : " << _rethrown_exception.GetMessage());
1416 event->setResult(false);
1417 event->setExceptionCode(ExceptionCodes::PlatformException);
1421 LoggerE("Error during removing group : " << _rethrown_exception.GetMessage());
1422 event->setResult(false);
1423 event->setExceptionCode(ExceptionCodes::PlatformException);
1427 void AddressBook::OnRequestReceived(const EventAddressBookGetGroupsPtr &event)
1433 ContactGroupArrayPtr groups(new ContactGroupArray());
1434 contacts_list_h groups_list = NULL;
1439 if(m_isUnifiedAddressBook)
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 << ")");
1447 contacts_query_h query = NULL;
1448 contacts_filter_h filter = NULL;
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 << ")");
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 << ")");
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 << ")");
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 << ")");
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 << ")");
1470 errorCode = contacts_filter_destroy(filter);
1471 if(errorCode != CONTACTS_ERROR_NONE)
1472 ThrowMsg(PlatformException, "Fail to get contacts_filter_destroy (ret:" << errorCode << ")");
1474 errorCode = contacts_query_destroy(query);
1475 if(errorCode != CONTACTS_ERROR_NONE)
1476 ThrowMsg(PlatformException, "Fail to get contacts_query_destroy (ret:" << errorCode << ")");
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 << ")");
1484 contacts_list_first(groups_list);
1485 for(unsigned int i=0; i<record_count; i++)
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)
1491 LoggerW("Fail to get group record (ret:" << errorCode << ")");
1496 int addressBookId = 0;
1498 char *ringtoneURI = NULL;
1499 char *photoURI = NULL;
1500 bool isReadOnly = false;
1502 errorCode = contacts_record_get_int(contacts_record, _contacts_group.id, &id);
1503 if(errorCode != CONTACTS_ERROR_NONE)
1505 LoggerW("Fail to get id from address book (ret:" << errorCode << ")");
1509 errorCode = contacts_record_get_int(contacts_record, _contacts_group.address_book_id, &addressBookId);
1510 if(errorCode != CONTACTS_ERROR_NONE)
1512 LoggerW("Fail to get address book id from address book (ret:" << errorCode << ")");
1516 errorCode = contacts_record_get_str_p(contacts_record, _contacts_group.name, &name);
1517 if(errorCode != CONTACTS_ERROR_NONE)
1519 LoggerW("Fail to get name from address book (ret:" << errorCode << ")");
1523 errorCode = contacts_record_get_str_p(contacts_record, _contacts_group.image_path, &photoURI);
1524 if(errorCode != CONTACTS_ERROR_NONE)
1526 LoggerW("Fail to get image_path from address book (ret:" << errorCode << ")");
1530 errorCode = contacts_record_get_str_p(contacts_record, _contacts_group.ringtone_path, &ringtoneURI);
1531 if(errorCode != CONTACTS_ERROR_NONE)
1533 LoggerW("Fail to get ringtone_path from address book (ret:" << errorCode << ")");
1537 errorCode = contacts_record_get_bool(contacts_record, _contacts_group.is_read_only, &isReadOnly);
1538 if(errorCode != CONTACTS_ERROR_NONE)
1540 LoggerW("Fail to get is_read_only from address book (ret:" << errorCode << ")");
1544 ContactGroupPtr group = ContactGroupPtr(new ContactGroup());
1546 group->setAddressBookId(addressBookId);
1549 group->setName(name);
1553 if(photoURI != NULL)
1554 group->setPhotoURI(ContactUtility::convertPathToUri(photoURI));
1556 if(ringtoneURI != NULL)
1557 group->setRingtoneURI(ContactUtility::convertPathToUri(ringtoneURI));
1559 group->setReadOnly(isReadOnly);
1561 groups->push_back(group);
1563 errorCode = contacts_list_next(groups_list);
1564 if(errorCode != CONTACTS_ERROR_NONE)
1566 LoggerW("Fail to get next address book (ret:" << errorCode << ")");
1571 event->setContactGroups(groups);
1572 event->setResult(true);
1575 Catch (NotFoundException)
1577 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
1578 event->setExceptionCode(ExceptionCodes::NotFoundException);
1579 event->setResult(false);
1582 Catch (PlatformException)
1584 LoggerE("Error during getting contact : " << _rethrown_exception.GetMessage());
1585 event->setExceptionCode(ExceptionCodes::PlatformException);
1586 event->setResult(false);
1589 Catch (InvalidArgumentException)
1591 LoggerE("Invalid Arguments : " << _rethrown_exception.GetMessage());
1592 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1593 event->setResult(false);
1598 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
1599 event->setExceptionCode(ExceptionCodes::UnknownException);
1600 event->setResult(false);
1603 if(groups_list != NULL)
1604 contacts_list_destroy(groups_list, true);
1607 void AddressBook::onContactsSvcContactsAdded(ContactArrayPtr &contacts)
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);
1616 void AddressBook::onContactsSvcContactsUpdated(ContactArrayPtr &contacts)
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);
1625 void AddressBook::onContactsSvcContactsRemoved(StringArrayPtr &contactIds)
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);
1634 void AddressBook::contactsAddBatchResultCallback(int error, int *ids, unsigned int count, void *user_data)
1637 if(user_data == NULL)
1639 LoggerE("user_data is NULL");
1643 KeySharePtrPair *keyPair = (KeySharePtrPair*)user_data;
1645 long key = keyPair->key;
1646 AddressBook *addressBook = keyPair->addressBook;
1650 addressBook->contactsAddBatchResultCallback(error, ids, count, key);
1653 void AddressBook::contactsAddBatchResultCallback(int error, int *ids, unsigned int count, long key)
1655 EventAddressBookAddBatchPtr event;
1657 EventAddressBookAddBatchMap::iterator iter;
1658 iter = m_addBatchEventMap.find(key);
1659 if(iter == m_addBatchEventMap.end())
1661 LoggerE("No event for key : " << key);
1665 event = iter->second;
1666 m_addBatchEventMap.erase(iter);
1668 if(error != CONTACTS_ERROR_NONE)
1670 LoggerE("contacts_db_insert_result_cb gives error : " << error);
1671 event->setResult(false);
1672 event->setExceptionCode(ExceptionCodes::PlatformException);
1673 EventRequestReceiver<EventAddressBookAddBatch>::ManualAnswer(event);
1678 ContactArrayPtr contacts(new ContactArray());
1680 ContactArrayPtr contacts = event->getContacts();
1683 for(unsigned int i=0; i<count; i++)
1686 int contactId = ids[i];
1688 contacts_record_h contacts_record = NULL;
1690 errorCode = contacts_db_get_record(_contacts_contact._uri, contactId, &contacts_record);
1691 if(errorCode != CONTACTS_ERROR_NONE)
1693 LoggerD("Fail contacts_db_get_record error _contacts_contact._uri : " << errorCode);
1698 ContactPtr absContact(NULL);
1700 ContactPtr absContact = contacts->at(i);
1706 ContactObjectP2AConverterPtr contactObjConverter(
1707 new ContactObjectP2AConverter(contacts_record, false) );
1709 ContactObjectP2AConverterPtr contactObjConverter(
1710 new ContactObjectP2AConverter(contacts_record, false, absContact) );
1712 absContact = contactObjConverter->getAbstractContact();
1716 LoggerE("Fail error on converting contact to abs contact : " << _rethrown_exception.GetMessage());
1721 if(absContact != NULL)
1722 contacts->push_back(absContact);
1725 if(contacts_record != NULL)
1726 contacts_record_destroy(contacts_record, true);
1729 event->setContacts(contacts);
1730 event->setResult(true);
1731 event->setExceptionCode(ExceptionCodes::None);
1732 EventRequestReceiver<EventAddressBookAddBatch>::ManualAnswer(event);
1735 void AddressBook::contactsUpdateBatchResultCallback(int error, void *user_data)
1739 if(user_data == NULL)
1741 LoggerE("user_data is NULL");
1745 KeySharePtrPair *keyPair = (KeySharePtrPair*)user_data;
1747 long key = keyPair->key;
1748 AddressBook *addressBook = keyPair->addressBook;
1752 addressBook->contactsUpdateBatchResultCallback(error, key);
1755 void AddressBook::contactsUpdateBatchResultCallback(int error, long key)
1757 EventAddressBookUpdateBatchPtr event;
1759 EventAddressBookUpdateBatchMap::iterator iter;
1760 iter = m_updateBatchEventMap.find(key);
1761 if(iter == m_updateBatchEventMap.end())
1763 LoggerE("No event for key : " << key);
1767 event = iter->second;
1768 m_updateBatchEventMap.erase(iter);
1770 if(error != CONTACTS_ERROR_NONE)
1772 LoggerE("contacts_db_result_cb gives error : " << error);
1773 event->setResult(false);
1774 event->setExceptionCode(ExceptionCodes::PlatformException);
1775 EventRequestReceiver<EventAddressBookUpdateBatch>::ManualAnswer(event);
1779 event->setResult(true);
1780 event->setExceptionCode(ExceptionCodes::None);
1781 EventRequestReceiver<EventAddressBookUpdateBatch>::ManualAnswer(event);
1784 void AddressBook::contactsRemoveBatchResultCallback(int error, void *user_data)
1788 if(user_data == NULL)
1790 LoggerE("user_data is NULL");
1794 KeySharePtrPair *keyPair = (KeySharePtrPair*)user_data;
1796 long key = keyPair->key;
1797 AddressBook *addressBook = keyPair->addressBook;
1801 addressBook->contactsRemoveBatchResultCallback(error, key);
1804 void AddressBook::contactsRemoveBatchResultCallback(int error, long key)
1806 EventAddressBookRemoveBatchPtr event;
1808 EventAddressBookRemoveBatchMap::iterator iter;
1809 iter = m_removeBatchEventMap.find(key);
1810 if(iter == m_removeBatchEventMap.end())
1812 LoggerE("No event for key : " << key);
1816 event = iter->second;
1817 m_removeBatchEventMap.erase(iter);
1819 if(error != CONTACTS_ERROR_NONE)
1821 LoggerE("contacts_db_result_cb gives error : " << error);
1822 event->setResult(false);
1823 event->setExceptionCode(ExceptionCodes::PlatformException);
1824 EventRequestReceiver<EventAddressBookRemoveBatch>::ManualAnswer(event);
1828 event->setResult(true);
1829 event->setExceptionCode(ExceptionCodes::None);
1830 EventRequestReceiver<EventAddressBookRemoveBatch>::ManualAnswer(event);