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 contact id");
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 (id:" << contactIdInt << ")");
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 (id:" << contactIdInt << ") 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 : Id is already set");
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 (id:" << contactId << ")");
242 ContactObjectP2AConverterPtr contactObjConverterForInserted(
243 new ContactObjectP2AConverter(get_contacts_record, false));
244 ContactPtr insertedContact = contactObjConverterForInserted->getAbstractContact();
245 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);
277 if(!event->getContactsIsSet())
278 ThrowMsg(InvalidArgumentException, "Contacts were not set.");
280 contacts = event->getContacts();
282 ThrowMsg(InvalidArgumentException, "No contacts.");
285 Catch(InvalidArgumentException)
287 ContactQueueManagerSingleton::Instance().decreaseQueueList();
288 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
290 ContactQueueManagerSingleton::Instance().pop();
293 LoggerE("Invalid arguments for adding contacts : " << _rethrown_exception.GetMessage());
294 event->setResult(false);
295 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
301 errorCode = contacts_list_create(&contacts_list);
302 if(errorCode != CONTACTS_ERROR_NONE)
304 ThrowMsg(PlatformException, "Fail to create contacts_list_h");
307 for(ContactArray::iterator i = contacts->begin(); i != contacts->end(); i++)
309 contacts_record_h contacts_record = NULL;
310 ContactPtr contact = *i;
311 ContactObjectA2PConverterPtr contactObjConverter(NULL);
315 DPL::SharedPtr<Contact> newContactT =
316 DPL::StaticPointerCast<Contact>(contact);
317 contacts_record = newContactT->getPlatformContactObject();
318 if(contacts_record == NULL)
320 errorCode = contacts_record_create(_contacts_contact._uri, &contacts_record);
321 if(errorCode != CONTACTS_ERROR_NONE)
323 ThrowMsg(PlatformException, "Error during creating contact record : " << errorCode);
327 contactObjConverter = ContactObjectA2PConverterPtr(
328 new ContactObjectA2PConverter(contact, false, contacts_record) );
329 contacts_record = contactObjConverter->getPlatformContact();
331 if(contacts_record == NULL)
333 ThrowMsg(PlatformException, "Error during converting contact object");
336 if(m_isUnifiedAddressBook)
337 errorCode = contacts_record_set_int(contacts_record, _contacts_contact.address_book_id, 0);
339 errorCode = contacts_record_set_int(contacts_record, _contacts_contact.address_book_id, m_id);
340 if(errorCode != CONTACTS_ERROR_NONE)
342 ThrowMsg(PlatformException, "Error during add address book id : " << errorCode);
345 errorCode = contacts_list_add(contacts_list, contacts_record);
346 if(errorCode != CONTACTS_ERROR_NONE)
348 ThrowMsg(PlatformException, "Error during add to list : " << errorCode);
353 ThrowMsg(NotFoundException, "Error during converting contact object");
354 // LoggerE("Error during converting contact object : " << _rethrown_exception.GetMessage());
359 KeySharePtrPair *keyPair = new KeySharePtrPair();
360 keyPair->key = m_eventMapAcc;
361 keyPair->addressBook = this;
362 errorCode = contacts_db_insert_records_async(contacts_list, contactsAddBatchResultCallback, (void*)keyPair);
363 if(errorCode != CONTACTS_ERROR_NONE)
366 ThrowMsg(PlatformException, "Error during contacts_db_insert_records_async");
369 errorCode = contacts_list_destroy(contacts_list, true);
370 contacts_list = NULL;
371 if(errorCode != CONTACTS_ERROR_NONE)
374 ThrowMsg(PlatformException, "Error during contacts_list_destroy");
377 pair<long, EventAddressBookAddBatchPtr> keyEventPair(m_eventMapAcc, event);
378 m_addBatchEventMap.insert(keyEventPair);
382 // event->switchToManualAnswer();
384 Catch (NotFoundException)
386 ContactQueueManagerSingleton::Instance().decreaseQueueList();
387 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
389 ContactQueueManagerSingleton::Instance().pop();
391 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
392 event->setResult(false);
393 event->setExceptionCode(ExceptionCodes::NotFoundException);
395 Catch (PlatformException)
397 ContactQueueManagerSingleton::Instance().decreaseQueueList();
398 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
400 ContactQueueManagerSingleton::Instance().pop();
402 LoggerE("Error during adding contacts : " << _rethrown_exception.GetMessage());
403 event->setResult(false);
404 event->setExceptionCode(ExceptionCodes::PlatformException);
408 ContactQueueManagerSingleton::Instance().decreaseQueueList();
409 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
411 ContactQueueManagerSingleton::Instance().pop();
413 LoggerE("Error during adding contacts : " << _rethrown_exception.GetMessage());
414 event->setResult(false);
415 event->setExceptionCode(ExceptionCodes::PlatformException);
418 if(contacts_list != NULL)
419 contacts_list_destroy(contacts_list, true);
422 void AddressBook::OnRequestReceived(const EventAddressBookAddBatchPtr &event)
428 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
430 AddressBookAddBatch(event);
432 ContactQueueManagerSingleton::Instance().push(ContactQueueManager::ADDBATCH, this, event);
434 ContactQueueManagerSingleton::Instance().increaseQueueList();
435 event->switchToManualAnswer();
439 LoggerE("Error during adding contacts : " << _rethrown_exception.GetMessage());
440 event->setResult(false);
441 event->setExceptionCode(ExceptionCodes::PlatformException);
445 void AddressBook::OnRequestReceived(const EventAddressBookUpdatePtr &event)
448 ContactPtr contact(NULL);
452 if(!event->getContactIsSet())
453 ThrowMsg(InvalidArgumentException, "Contacts were not set.");
455 contact = event->getContact();
457 ThrowMsg(InvalidArgumentException, "No contact.");
459 if(!contact->getIdIsSet() || !contact->getAddressBookIdIsSet())
460 ThrowMsg(InvalidArgumentException, "Contact has no ID");
462 if(!m_isUnifiedAddressBook && contact->getAddressBookId() != getId())
463 ThrowMsg(InvalidArgumentException, "Wrong address book ID");
465 Catch(InvalidArgumentException)
467 LoggerE("Invalid arguments for adding contacts : " << _rethrown_exception.GetMessage());
468 event->setResult(false);
469 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
477 DPL::SharedPtr<Contact> contactT =
478 DPL::StaticPointerCast<Contact>(contact);
480 ContactObjectA2PConverterPtr contactObjConverter(NULL);
482 contacts_record_h contacts_record = contactT->getPlatformContactObject();
483 if(contacts_record == NULL)
485 contactObjConverter = ContactObjectA2PConverterPtr(
486 new ContactObjectA2PConverter(contact, false) );
487 contacts_record = contactObjConverter->getPlatformContact();
491 contactObjConverter = ContactObjectA2PConverterPtr(
492 new ContactObjectA2PConverter(contact, false, contacts_record) );
493 contacts_record = contactObjConverter->getPlatformContact();
496 if (!contacts_record)
498 LoggerE("Error during converting contact object");
499 ThrowMsg(PlatformException, "Error during converting contact object");
502 errorCode = contacts_db_update_record(contacts_record);
503 if (errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
504 ThrowMsg(NotFoundException, "Error during executing contacts_db_update_record()");
505 else if (errorCode != CONTACTS_ERROR_NONE)
507 LoggerE("error code : " << errorCode);
508 ThrowMsg(PlatformException, "Error during executing contacts_db_update_record()");
511 Catch (NotFoundException)
513 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
514 event->setResult(false);
515 event->setExceptionCode(ExceptionCodes::NotFoundException);
518 Catch (PlatformException)
520 LoggerE("Error during adding contact : " << _rethrown_exception.GetMessage());
521 event->setResult(false);
522 event->setExceptionCode(ExceptionCodes::PlatformException);
527 LoggerE("Error during adding contact : " << _rethrown_exception.GetMessage());
528 event->setResult(false);
529 event->setExceptionCode(ExceptionCodes::PlatformException);
532 //m_latestVersion = get_contact_version();
534 event->setResult(true);
535 event->setExceptionCode(ExceptionCodes::None);
538 void AddressBook::AddressBookUpdateBatch(const EventAddressBookUpdateBatchPtr &event)
542 contacts_list_h contacts_list = NULL;
543 ContactArrayPtr contacts(NULL);
547 if(!event->getContactsIsSet())
548 ThrowMsg(InvalidArgumentException, "Contacts were not set.");
550 contacts = event->getContacts();
552 ThrowMsg(InvalidArgumentException, "No contacts.");
555 Catch(InvalidArgumentException)
557 ContactQueueManagerSingleton::Instance().decreaseQueueList();
558 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
560 ContactQueueManagerSingleton::Instance().pop();
562 LoggerE("Invalid arguments for updating contacts : " << _rethrown_exception.GetMessage());
563 event->setResult(false);
564 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
570 errorCode = contacts_list_create(&contacts_list);
571 if(errorCode != CONTACTS_ERROR_NONE)
573 ThrowMsg(PlatformException, "Fail to create contacts_list_h");
576 for(ContactArray::iterator i = contacts->begin(); i != contacts->end(); i++)
578 contacts_record_h contacts_record = NULL;
579 ContactPtr contact = *i;
580 ContactObjectA2PConverterPtr contactObjConverter(NULL);
585 ThrowMsg(InvalidArgumentException, "No contact.");
587 if(contact->getIdIsSet() == false)
588 ThrowMsg(InvalidArgumentException, "Contact has no ID");
590 if(!m_isUnifiedAddressBook && ( !contact->getAddressBookIdIsSet() || contact->getAddressBookId() != getId()))
591 ThrowMsg(InvalidArgumentException, "Wrong address book ID");
593 DPL::SharedPtr<Contact> newContactT =
594 DPL::StaticPointerCast<Contact>(contact);
595 contacts_record = newContactT->getPlatformContactObject();
596 if(contacts_record == NULL)
598 LoggerD("id : " << contact->getId());
599 int contactIdInt = ContactUtility::strToInt(contact->getId());
600 errorCode = contacts_db_get_record(_contacts_contact._uri, contactIdInt, &contacts_record);
601 if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
602 ThrowMsg(NotFoundException, "No contact (id:" << contactIdInt << ")");
604 contactObjConverter = ContactObjectA2PConverterPtr(
605 new ContactObjectA2PConverter(contact, false, contacts_record) );
606 contacts_record = contactObjConverter->getPlatformContact();
608 if(contacts_record == NULL)
610 ThrowMsg(PlatformException, "Error during converting contact object");
613 errorCode = contacts_list_add(contacts_list, contacts_record);
614 if(errorCode != CONTACTS_ERROR_NONE)
616 ThrowMsg(PlatformException, "Error during add to list");
621 ThrowMsg(NotFoundException, "Error during converting contact object");
622 // LoggerE("Error during converting contact object : " << _rethrown_exception.GetMessage());
627 KeySharePtrPair *keyPair = new KeySharePtrPair();
628 keyPair->key = m_eventMapAcc;
629 keyPair->addressBook = this;
630 errorCode = contacts_db_update_records_async(contacts_list, contactsUpdateBatchResultCallback, (void*)keyPair);
631 if(errorCode != CONTACTS_ERROR_NONE)
634 ThrowMsg(PlatformException, "Error during contacts_db_update_records_async");
637 errorCode = contacts_list_destroy(contacts_list, true);
638 contacts_list = NULL;
639 if(errorCode != CONTACTS_ERROR_NONE)
642 ThrowMsg(PlatformException, "Error during contacts_list_destroy");
645 pair<long, EventAddressBookUpdateBatchPtr> keyEventPair(m_eventMapAcc, event);
646 m_updateBatchEventMap.insert(keyEventPair);
650 // event->switchToManualAnswer();
652 Catch (NotFoundException)
654 ContactQueueManagerSingleton::Instance().decreaseQueueList();
655 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
657 ContactQueueManagerSingleton::Instance().pop();
659 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
660 event->setResult(false);
661 event->setExceptionCode(ExceptionCodes::NotFoundException);
663 Catch (PlatformException)
665 ContactQueueManagerSingleton::Instance().decreaseQueueList();
666 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
668 ContactQueueManagerSingleton::Instance().pop();
670 LoggerE("Error during adding contacts : " << _rethrown_exception.GetMessage());
671 event->setResult(false);
672 event->setExceptionCode(ExceptionCodes::PlatformException);
676 ContactQueueManagerSingleton::Instance().decreaseQueueList();
677 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
679 ContactQueueManagerSingleton::Instance().pop();
681 LoggerE("Error during adding contacts : " << _rethrown_exception.GetMessage());
682 event->setResult(false);
683 event->setExceptionCode(ExceptionCodes::PlatformException);
686 if(contacts_list != NULL)
687 contacts_list_destroy(contacts_list, false);
690 void AddressBook::OnRequestReceived(const EventAddressBookUpdateBatchPtr &event)
696 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
698 AddressBookUpdateBatch(event);
700 ContactQueueManagerSingleton::Instance().push(ContactQueueManager::UPATEBATCH, this, event);
702 ContactQueueManagerSingleton::Instance().increaseQueueList();
703 event->switchToManualAnswer();
707 LoggerE("Error during updating contacts : " << _rethrown_exception.GetMessage());
708 event->setResult(false);
709 event->setExceptionCode(ExceptionCodes::PlatformException);
713 void AddressBook::OnRequestReceived(const EventAddressBookRemovePtr &event)
720 if(!event->getContactIdIsSet())
721 ThrowMsg(InvalidArgumentException, "Contact id was not set.");
723 string contactIdStr = event->getContactId();
725 if(!ContactUtility::checkStrIsUInt(contactIdStr))
726 ThrowMsg(InvalidArgumentException, "Id is wrong (" << contactIdStr << ")" );
728 int contactId = ContactUtility::strToInt(contactIdStr);
730 if(!m_isUnifiedAddressBook)
732 contacts_record_h contacts_record = NULL;
733 errorCode = contacts_db_get_record(_contacts_simple_contact._uri, contactId, &contacts_record);
734 if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
735 ThrowMsg(PlatformException, "No contact id (id:" << contactId << ")");
737 int addressBookId = 0;
738 errorCode = contacts_record_get_int(contacts_record, _contacts_simple_contact.address_book_id, &addressBookId);
739 if(errorCode != CONTACTS_ERROR_NONE)
740 ThrowMsg(PlatformException, "Error while getting address book id");
742 if(addressBookId != m_id)
743 ThrowMsg(PlatformException, "Contact id (" << contactId << ") is not a member of this address book.");
746 errorCode = contacts_db_delete_record(_contacts_contact._uri, contactId);
747 if(errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
748 ThrowMsg(NotFoundException, "Error during executing contacts_db_delete_record()");
749 else if (errorCode != CONTACTS_ERROR_NONE)
750 ThrowMsg(PlatformException, "Error during executing contacts_db_delete_record()");
752 Catch (InvalidArgumentException)
754 LoggerE("Invalid contact id : " << _rethrown_exception.GetMessage());
755 event->setResult(false);
756 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
759 Catch (NotFoundException)
761 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
762 event->setResult(false);
763 event->setExceptionCode(ExceptionCodes::NotFoundException);
766 Catch (PlatformException)
768 LoggerE("Error during updating contact : " << _rethrown_exception.GetMessage());
769 event->setResult(false);
770 event->setExceptionCode(ExceptionCodes::PlatformException);
775 LoggerE("Error during updating contact : " << _rethrown_exception.GetMessage());
776 event->setResult(false);
777 event->setExceptionCode(ExceptionCodes::PlatformException);
781 event->setResult(true);
782 event->setExceptionCode(ExceptionCodes::None);
785 void AddressBook::AddressBookRemoveBatch(const EventAddressBookRemoveBatchPtr &event)
789 StringArrayPtr contactIds(NULL);
793 if(!event->getContactIdsIsSet())
794 ThrowMsg(InvalidArgumentException, "Contact IDs were not set.");
796 contactIds = event->getContactIds();
798 ThrowMsg(InvalidArgumentException, "No contact IDs.");
801 Catch(InvalidArgumentException)
803 ContactQueueManagerSingleton::Instance().decreaseQueueList();
804 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
806 ContactQueueManagerSingleton::Instance().pop();
808 LoggerE("Invalid arguments for removing contacts : " << _rethrown_exception.GetMessage());
809 event->setResult(false);
810 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
816 int *ids = new int[contactIds->size()];
817 // int *tmpIds = new int[contactIds->size()];
820 if(errorCode != CONTACTS_ERROR_NONE)
822 ThrowMsg(PlatformException, "Fail to create contacts_list_h");
825 for(StringArray::iterator i = contactIds->begin(); i != contactIds->end(); i++)
827 string contactIdStr = *i;
833 if(!ContactUtility::checkStrIsUInt(contactIdStr))
834 ThrowMsg(InvalidArgumentException, "Id is wrong (" << contactIdStr << ")" );
836 contactId = ContactUtility::strToInt(contactIdStr);
838 if(!m_isUnifiedAddressBook)
840 contacts_record_h contacts_record = NULL;
841 errorCode = contacts_db_get_record(_contacts_simple_contact._uri, contactId, &contacts_record);
842 if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
843 ThrowMsg(PlatformException, "No contact id (id:" << contactId << ")");
845 int addressBookId = 0;
846 errorCode = contacts_record_get_int(contacts_record, _contacts_simple_contact.address_book_id, &addressBookId);
847 if(errorCode != CONTACTS_ERROR_NONE)
848 ThrowMsg(PlatformException, "Error while getting address book id");
850 if(addressBookId != m_id)
851 ThrowMsg(PlatformException, "Contact id (" << contactId << ") is not a member of this address book.");
854 ids[count] = contactId;
855 // tmpIds[count] = contactId;
860 ThrowMsg(NotFoundException, "Error during converting contact object");
861 // LoggerE("Error during converting contact object : " << _rethrown_exception.GetMessage());
866 contacts_filter_h filter = NULL;
867 contacts_query_h query = NULL;
869 errorCode = contacts_query_create(_contacts_simple_contact._uri, &query);
870 if(errorCode != CONTACTS_ERROR_NONE)
871 ThrowMsg(PlatformException, "contacts_query_create error : " << errorCode << " (" << __FUNCTION__ << ")");
873 errorCode = contacts_filter_create( _contacts_simple_contact._uri, &filter );
874 if(errorCode != CONTACTS_ERROR_NONE)
875 ThrowMsg(PlatformException, "contacts_query_create error : " << errorCode << " (" << __FUNCTION__ << ")");
877 for(int i = 0; i < contactIds->size(); i++)
879 errorCode = contacts_filter_add_int(filter, _contacts_simple_contact.id, CONTACTS_MATCH_EQUAL, tmpIds[i]);
880 if(i == (contactIds->size() - 1))
883 errorCode = contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_OR);
884 if(errorCode != CONTACTS_ERROR_NONE)
885 ThrowMsg(PlatformException, "contacts_filter_add_operator error : " << errorCode << " (" << __FUNCTION__ << ")");
888 errorCode = contacts_query_set_filter(query, filter);
889 if(errorCode != CONTACTS_ERROR_NONE)
890 ThrowMsg(PlatformException, "contacts_query_set_filter error : " << errorCode << " (" << __FUNCTION__ << ")");
892 int record_count = 0;
893 errorCode = contacts_db_get_count_with_query(query, &record_count);
894 if(errorCode != CONTACTS_ERROR_NONE)
895 ThrowMsg(PlatformException, "contacts_db_get_count_with_query error : " << errorCode << " (" << __FUNCTION__ << ")");
898 contacts_filter_destroy(filter);
900 contacts_query_destroy(query);
902 if(contactIds->size() != (unsigned int)record_count)
903 ThrowMsg(InvalidArgumentException, "Ids' db count : " << record_count << " (" << __FUNCTION__ << ")");
905 KeySharePtrPair *keyPair = new KeySharePtrPair();
906 keyPair->key = m_eventMapAcc;
907 keyPair->addressBook = this;
908 errorCode = contacts_db_delete_records_async(_contacts_contact._uri, ids, count, contactsRemoveBatchResultCallback, (void*)keyPair);
919 if(errorCode != CONTACTS_ERROR_NONE)
922 ThrowMsg(PlatformException, "Error during add to list");
925 pair<long, EventAddressBookRemoveBatchPtr> keyEventPair(m_eventMapAcc, event);
926 m_removeBatchEventMap.insert(keyEventPair);
930 // event->switchToManualAnswer();
932 Catch (InvalidArgumentException)
934 ContactQueueManagerSingleton::Instance().decreaseQueueList();
935 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
937 ContactQueueManagerSingleton::Instance().pop();
939 LoggerE("Invalid contact id : " << _rethrown_exception.GetMessage());
940 event->setResult(false);
941 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
944 Catch (NotFoundException)
946 ContactQueueManagerSingleton::Instance().decreaseQueueList();
947 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
949 ContactQueueManagerSingleton::Instance().pop();
951 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
952 event->setResult(false);
953 event->setExceptionCode(ExceptionCodes::NotFoundException);
956 Catch (PlatformException)
958 ContactQueueManagerSingleton::Instance().decreaseQueueList();
959 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
961 ContactQueueManagerSingleton::Instance().pop();
963 LoggerE("Error during deleting contacts : " << _rethrown_exception.GetMessage());
964 event->setResult(false);
965 event->setExceptionCode(ExceptionCodes::PlatformException);
970 ContactQueueManagerSingleton::Instance().decreaseQueueList();
971 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
973 ContactQueueManagerSingleton::Instance().pop();
975 LoggerE("Error during deleting contacts : " << _rethrown_exception.GetMessage());
976 event->setResult(false);
977 event->setExceptionCode(ExceptionCodes::PlatformException);
982 void AddressBook::OnRequestReceived(const EventAddressBookRemoveBatchPtr &event)
988 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
990 AddressBookRemoveBatch(event);
992 ContactQueueManagerSingleton::Instance().push(ContactQueueManager::DELETEBATCH, this, event);
994 ContactQueueManagerSingleton::Instance().increaseQueueList();
995 event->switchToManualAnswer();
999 LoggerE("Error during deleting contacts : " << _rethrown_exception.GetMessage());
1000 event->setResult(false);
1001 event->setExceptionCode(ExceptionCodes::PlatformException);
1005 void AddressBook::OnRequestReceived(const EventAddressBookFindPtr &event)
1009 contacts_query_h query = NULL;
1010 contacts_filter_h filter = NULL;
1011 contacts_list_h contacts_list = NULL;
1013 ContactArrayPtr contacts = ContactArrayPtr(new ContactArray());
1017 ContactSearchEnginePtr searchEngine(new ContactSearchEngine());
1019 if(!m_isUnifiedAddressBook)
1020 searchEngine->setAddressBookId(m_id);
1022 if(event->getFilterIsSet())
1024 FilterPtr filter = event->getFilter();
1026 FilterValidatorPtr validator = ContactFilterValidatorFactory::getContactFilterValidator();
1027 bool success = filter->validate(validator);
1030 ThrowMsg(InvalidArgumentException, "Invalid filter arguments.");
1032 searchEngine->setCondition(filter);
1035 if (event->getSortModeIsSet())
1037 searchEngine->setSortMode(event->getSortMode());
1040 searchEngine->setSortMode();
1042 event->setContacts(searchEngine->getContactSearchResult());
1043 event->setResult(true);
1045 Catch (NotFoundException)
1047 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
1048 event->setExceptionCode(ExceptionCodes::NotFoundException);
1049 event->setResult(false);
1051 Catch (PlatformException)
1053 LoggerE("Error during finding contact : " << _rethrown_exception.GetMessage());
1054 event->setExceptionCode(ExceptionCodes::PlatformException);
1055 event->setResult(false);
1057 Catch (InvalidArgumentException)
1059 LoggerE("Invalid Arguments : " << _rethrown_exception.GetMessage());
1060 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1061 event->setResult(false);
1065 LoggerE("Error during deleting contact : " << _rethrown_exception.GetMessage());
1066 event->setExceptionCode(ExceptionCodes::PlatformException);
1067 event->setResult(false);
1071 contacts_filter_destroy(filter);
1074 contacts_query_destroy(query);
1076 if(contacts_list != NULL)
1077 contacts_list_destroy(contacts_list, true);
1080 void AddressBook::OnRequestReceived(const EventAddressBookAddChangeListenerPtr &event)
1086 if(!event->getEmitterIsSet())
1087 ThrowMsg(InvalidArgumentException, "Invalid arguments.");
1089 EventAddressBookChangeListenerEmitterPtr emitter = event->getEmitter();
1091 ThrowMsg(InvalidArgumentException, "Invalid arguments.");
1093 if(m_addressBookEmitters.size() == 0)
1095 LoggerD("Watch registered initially");
1097 if(m_isUnifiedAddressBook)
1098 ContactsSvcChangeListenerManagerSingleton::Instance().registerContactsChangeListener(this, -1);
1100 ContactsSvcChangeListenerManagerSingleton::Instance().registerContactsChangeListener(this, m_id);
1103 m_addressBookEmitters.attach(emitter);
1105 long id = ContactsSvcChangeListenerManagerSingleton::Instance().getWatchIdAndInc();
1106 m_watchIdMap[id] = emitter->getId();
1109 event->setResult(true);
1111 Catch (NotFoundException)
1113 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
1114 event->setExceptionCode(ExceptionCodes::NotFoundException);
1115 event->setResult(false);
1117 Catch (PlatformException)
1119 LoggerE("Error during deleting contact : " << _rethrown_exception.GetMessage());
1120 event->setExceptionCode(ExceptionCodes::PlatformException);
1121 event->setResult(false);
1123 Catch (InvalidArgumentException)
1125 LoggerE("Invalid Arguments : " << _rethrown_exception.GetMessage());
1126 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1127 event->setResult(false);
1131 LoggerE("Error during deleting contact : " << _rethrown_exception.GetMessage());
1132 event->setExceptionCode(ExceptionCodes::PlatformException);
1133 event->setResult(false);
1137 void AddressBook::OnRequestReceived(const EventAddressBookRemoveChangeListenerPtr &event)
1143 if(!event->getIdIsSet())
1144 ThrowMsg(InvalidArgumentException, "Invalid arguments.");
1146 long id = event->getId();
1147 if(m_watchIdMap.find(id) == m_watchIdMap.end())
1148 ThrowMsg(NotFoundException, "No watchId : " << id);
1150 bool success = m_addressBookEmitters.detach(m_watchIdMap[id]);
1152 ThrowMsg(NotFoundException, "No watchId : " << id);
1154 m_watchIdMap.erase(id);
1156 if(m_addressBookEmitters.size() == 0)
1158 LoggerD("No watcher is registered. unsubscribing from contact service.");
1160 ContactsSvcChangeListenerManagerSingleton::Instance().unregisterContactsChangeListener(this);
1163 event->setResult(true);
1166 Catch (NotFoundException)
1168 LoggerE("WatchId doesn't exist : " << _rethrown_exception.GetMessage());
1169 event->setExceptionCode(ExceptionCodes::NotFoundException);
1170 event->setResult(false);
1172 Catch (PlatformException)
1174 LoggerE("Error during deleting contact : " << _rethrown_exception.GetMessage());
1175 event->setExceptionCode(ExceptionCodes::PlatformException);
1176 event->setResult(false);
1178 Catch (InvalidArgumentException)
1180 LoggerE("Invalid Arguments : " << _rethrown_exception.GetMessage());
1181 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1182 event->setResult(false);
1186 LoggerE("Error during deleting contact : " << _rethrown_exception.GetMessage());
1187 event->setExceptionCode(ExceptionCodes::PlatformException);
1188 event->setResult(false);
1192 void AddressBook::OnRequestReceived(const EventAddressBookGetGroupPtr &event)
1197 contacts_record_h contacts_record = NULL;
1201 if(!event->getIdIsSet())
1202 ThrowMsg(InvalidArgumentException, "Invalid group id");
1204 string groupId = event->getId();
1206 int groupIdInt = ContactUtility::strToInt(groupId);
1208 errorCode = contacts_db_get_record(_contacts_group._uri, groupIdInt, &contacts_record);
1209 if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
1210 ThrowMsg(NotFoundException, "No group (id:" << groupIdInt << ")");
1212 if(!m_isUnifiedAddressBook)
1214 int addressBookId = 0;
1215 contacts_record_get_int(contacts_record, _contacts_group.address_book_id, &addressBookId);
1216 if(addressBookId != m_id)
1217 ThrowMsg(NotFoundException, "No group (id:" << groupIdInt << ") in this address book.");
1220 ContactGroupPtr group = ContactGroupPtr(new ContactGroup());
1222 ContactsSvcObjectConverter::convertToAbstract(contacts_record, group);
1224 event->setContactGroup(group);
1225 event->setResult(true);
1228 Catch (NotFoundException)
1230 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
1231 event->setExceptionCode(ExceptionCodes::NotFoundException);
1232 event->setResult(false);
1235 Catch (PlatformException)
1237 LoggerE("Error during getting contact : " << _rethrown_exception.GetMessage());
1238 event->setExceptionCode(ExceptionCodes::PlatformException);
1239 event->setResult(false);
1242 Catch (InvalidArgumentException)
1244 LoggerE("Invalid Arguments : " << _rethrown_exception.GetMessage());
1245 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1246 event->setResult(false);
1251 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
1252 event->setExceptionCode(ExceptionCodes::UnknownException);
1253 event->setResult(false);
1256 if(contacts_record != NULL)
1257 contacts_record_destroy(contacts_record, true);
1260 void AddressBook::OnRequestReceived(const EventAddressBookAddGroupPtr &event)
1266 contacts_record_h contacts_record = NULL;
1267 ContactGroupPtr group(NULL);
1271 if(!event->getContactGroupIsSet())
1272 ThrowMsg(InvalidArgumentException, "group were not set.");
1274 group = event->getContactGroup();
1276 ThrowMsg(InvalidArgumentException, "No group.");
1278 if(group->getIdIsSet() || group->getAddressBookIdIsSet())
1279 ThrowMsg(InvalidArgumentException, "ContactGroup object is wrong");
1281 Catch(InvalidArgumentException)
1283 LoggerE("Invalid arguments for adding contacts : " << _rethrown_exception.GetMessage());
1284 event->setResult(false);
1285 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1291 errorCode = contacts_record_create(_contacts_group._uri, &contacts_record);
1292 if(errorCode != CONTACTS_ERROR_NONE)
1294 LoggerE("error code : " << errorCode);
1295 ThrowMsg(PlatformException, "Error during executing contacts_record_create()");
1298 if(m_isUnifiedAddressBook)
1299 errorCode = contacts_record_set_int(contacts_record, _contacts_group.address_book_id, 0);
1301 errorCode = contacts_record_set_int(contacts_record, _contacts_group.address_book_id, m_id);
1302 if(errorCode != CONTACTS_ERROR_NONE)
1304 LoggerE("error code : " << errorCode);
1305 ThrowMsg(PlatformException, "Error during executing contacts_record_set_int()");
1308 ContactsSvcObjectConverter::convertToPlatform(group, contacts_record);
1311 errorCode = contacts_db_insert_record(contacts_record, &groupId);
1312 if(errorCode != CONTACTS_ERROR_NONE)
1314 LoggerE("error code : " << errorCode);
1315 ThrowMsg(PlatformException, "Error during executing contacts_db_insert_record()");
1317 group->setId(groupId);
1318 group->setAddressBookId(m_id);
1320 event->setResult(true);
1321 event->setExceptionCode(ExceptionCodes::None);
1323 Catch (PlatformException)
1325 LoggerE("Error during adding contact : " << _rethrown_exception.GetMessage());
1326 event->setResult(false);
1327 event->setExceptionCode(ExceptionCodes::PlatformException);
1331 LoggerE("Error during adding contact : " << _rethrown_exception.GetMessage());
1332 event->setResult(false);
1333 event->setExceptionCode(ExceptionCodes::UnknownException);
1336 if(contacts_record != NULL)
1337 contacts_record_destroy(contacts_record, true);
1340 void AddressBook::OnRequestReceived(const EventAddressBookUpdateGroupPtr &event)
1346 contacts_record_h contacts_record = NULL;
1347 ContactGroupPtr group(NULL);
1351 if(!event->getContactGroupIsSet())
1352 ThrowMsg(InvalidArgumentException, "group were not set.");
1354 group = event->getContactGroup();
1356 ThrowMsg(InvalidArgumentException, "No group.");
1358 if(!group->getIdIsSet() || !group->getAddressBookIdIsSet())
1359 ThrowMsg(InvalidArgumentException, "Contact Group is wrong");
1361 if(!m_isUnifiedAddressBook && group->getAddressBookId() != getId())
1362 ThrowMsg(InvalidArgumentException, "Wrong address book ID");
1364 Catch(InvalidArgumentException)
1366 LoggerE("Invalid arguments for updating group : " << _rethrown_exception.GetMessage());
1367 event->setResult(false);
1368 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1372 if(group->getReadOnly())
1374 LoggerW("Canceling updating readonly data.");
1375 event->setResult(false);
1376 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1382 int id = ContactUtility::strToInt(group->getId());
1383 LoggerD("Load platform object id : " << id);
1384 errorCode = contacts_db_get_record(_contacts_group._uri, id, &contacts_record);
1385 if (errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
1386 ThrowMsg(NotFoundException, "Error during executing contacts_db_get_record()");
1387 else if (errorCode != CONTACTS_ERROR_NONE)
1389 LoggerE("error code : " << errorCode);
1390 ThrowMsg(PlatformException, "Error during executing contacts_db_get_record()");
1393 LoggerD("Update group : " << id);
1394 ContactsSvcObjectConverter::convertToPlatform(group, contacts_record);
1396 errorCode = contacts_db_update_record(contacts_record);
1397 if (errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
1398 ThrowMsg(NotFoundException, "Error during executing contacts_db_update_record()");
1399 else if (errorCode != CONTACTS_ERROR_NONE)
1401 LoggerE("error code : " << errorCode);
1402 ThrowMsg(PlatformException, "Error during executing contacts_db_update_record()");
1405 event->setResult(true);
1406 event->setExceptionCode(ExceptionCodes::None);
1408 Catch (NotFoundException)
1410 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
1411 event->setResult(false);
1412 event->setExceptionCode(ExceptionCodes::NotFoundException);
1414 Catch (PlatformException)
1416 LoggerE("Error during adding contact : " << _rethrown_exception.GetMessage());
1417 event->setResult(false);
1418 event->setExceptionCode(ExceptionCodes::PlatformException);
1422 LoggerE("Error during adding contact : " << _rethrown_exception.GetMessage());
1423 event->setResult(false);
1424 event->setExceptionCode(ExceptionCodes::PlatformException);
1427 if(contacts_record != NULL)
1428 contacts_record_destroy(contacts_record, true);
1433 void AddressBook::OnRequestReceived(const EventAddressBookRemoveGroupPtr &event)
1440 if(!event->getContactGroupIdIsSet())
1441 ThrowMsg(InvalidArgumentException, "group were not set.");
1443 string groupIdStr = event->getContactGroupId();
1445 if(!ContactUtility::checkStrIsUInt(groupIdStr))
1446 ThrowMsg(InvalidArgumentException, "Id is wrong (" << groupIdStr << ")" );
1448 int groupId = ContactUtility::strToInt(groupIdStr);
1450 if(!m_isUnifiedAddressBook)
1452 contacts_record_h contacts_record = NULL;
1453 errorCode = contacts_db_get_record(_contacts_group._uri, groupId, &contacts_record);
1454 if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
1455 ThrowMsg(PlatformException, "No group id (id:" << groupId << ")");
1457 int addressBookId = 0;
1458 errorCode = contacts_record_get_int(contacts_record, _contacts_group.address_book_id, &addressBookId);
1459 if(errorCode != CONTACTS_ERROR_NONE)
1460 ThrowMsg(PlatformException, "Error while getting address book id");
1462 if(addressBookId != m_id)
1463 ThrowMsg(PlatformException, "Contact id (" << groupId << ") is not a member of this address book.");
1466 errorCode = contacts_db_delete_record(_contacts_group._uri, groupId);
1467 if(errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
1468 ThrowMsg(NotFoundException, "Error during executing contacts_db_delete_record()");
1469 else if (errorCode != CONTACTS_ERROR_NONE)
1470 ThrowMsg(PlatformException, "Error during executing contacts_db_delete_record()");
1472 event->setResult(true);
1473 event->setExceptionCode(ExceptionCodes::None);
1475 Catch (InvalidArgumentException)
1477 LoggerE("Invalid group id : " << _rethrown_exception.GetMessage());
1478 event->setResult(false);
1479 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1481 Catch (NotFoundException)
1483 LoggerE("Group doesn't exist : " << _rethrown_exception.GetMessage());
1484 event->setResult(false);
1485 event->setExceptionCode(ExceptionCodes::NotFoundException);
1487 Catch (PlatformException)
1489 LoggerE("Error during removing group : " << _rethrown_exception.GetMessage());
1490 event->setResult(false);
1491 event->setExceptionCode(ExceptionCodes::PlatformException);
1495 LoggerE("Error during removing group : " << _rethrown_exception.GetMessage());
1496 event->setResult(false);
1497 event->setExceptionCode(ExceptionCodes::PlatformException);
1501 void AddressBook::OnRequestReceived(const EventAddressBookGetGroupsPtr &event)
1507 ContactGroupArrayPtr groups(new ContactGroupArray());
1508 contacts_list_h groups_list = NULL;
1513 if(m_isUnifiedAddressBook)
1515 errorCode = contacts_db_get_all_records(_contacts_group._uri, 0, 0, &groups_list);
1516 if(errorCode != CONTACTS_ERROR_NONE)
1517 ThrowMsg(PlatformException, "Fail to get group list (ret:" << errorCode << ")");
1521 contacts_query_h query = NULL;
1522 contacts_filter_h filter = NULL;
1524 errorCode = contacts_query_create(_contacts_group._uri, &query);
1525 if(errorCode != CONTACTS_ERROR_NONE)
1526 ThrowMsg(PlatformException, "Fail to get contacts_query_create (ret:" << errorCode << ")");
1528 errorCode = contacts_filter_create(_contacts_group._uri, &filter);
1529 if(errorCode != CONTACTS_ERROR_NONE)
1530 ThrowMsg(PlatformException, "Fail to get contacts_filter_create (ret:" << errorCode << ")");
1532 errorCode = contacts_filter_add_int(filter, _contacts_group.address_book_id, CONTACTS_MATCH_EQUAL, m_id);
1533 if(errorCode != CONTACTS_ERROR_NONE)
1534 ThrowMsg(PlatformException, "Fail to get contacts_filter_add_int (ret:" << errorCode << ")");
1536 errorCode = contacts_query_set_filter(query, filter);
1537 if(errorCode != CONTACTS_ERROR_NONE)
1538 ThrowMsg(PlatformException, "Fail to get contacts_query_set_filter (ret:" << errorCode << ")");
1540 errorCode = contacts_db_get_records_with_query(query, 0, 0, &groups_list);
1541 if(errorCode != CONTACTS_ERROR_NONE)
1542 ThrowMsg(PlatformException, "Fail to get contacts_db_get_records_with_query (ret:" << errorCode << ")");
1544 errorCode = contacts_filter_destroy(filter);
1545 if(errorCode != CONTACTS_ERROR_NONE)
1546 ThrowMsg(PlatformException, "Fail to get contacts_filter_destroy (ret:" << errorCode << ")");
1548 errorCode = contacts_query_destroy(query);
1549 if(errorCode != CONTACTS_ERROR_NONE)
1550 ThrowMsg(PlatformException, "Fail to get contacts_query_destroy (ret:" << errorCode << ")");
1553 unsigned int record_count = 0;
1554 errorCode = contacts_list_get_count(groups_list, &record_count);
1555 if(errorCode != CONTACTS_ERROR_NONE)
1556 ThrowMsg(PlatformException, "Fail to get contacts_list_get_count (ret:" << errorCode << ")");
1558 contacts_list_first(groups_list);
1559 for(unsigned int i=0; i<record_count; i++)
1561 contacts_record_h contacts_record;
1562 errorCode = contacts_list_get_current_record_p(groups_list, &contacts_record);
1563 if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
1565 LoggerW("Fail to get group record (ret:" << errorCode << ")");
1570 int addressBookId = 0;
1572 char *ringtoneURI = NULL;
1573 char *photoURI = NULL;
1574 bool isReadOnly = false;
1576 errorCode = contacts_record_get_int(contacts_record, _contacts_group.id, &id);
1577 if(errorCode != CONTACTS_ERROR_NONE)
1579 LoggerW("Fail to get id from address book (ret:" << errorCode << ")");
1583 errorCode = contacts_record_get_int(contacts_record, _contacts_group.address_book_id, &addressBookId);
1584 if(errorCode != CONTACTS_ERROR_NONE)
1586 LoggerW("Fail to get address book id from address book (ret:" << errorCode << ")");
1590 errorCode = contacts_record_get_str_p(contacts_record, _contacts_group.name, &name);
1591 if(errorCode != CONTACTS_ERROR_NONE)
1593 LoggerW("Fail to get name from address book (ret:" << errorCode << ")");
1597 errorCode = contacts_record_get_str_p(contacts_record, _contacts_group.image_path, &photoURI);
1598 if(errorCode != CONTACTS_ERROR_NONE)
1600 LoggerW("Fail to get image_path from address book (ret:" << errorCode << ")");
1604 errorCode = contacts_record_get_str_p(contacts_record, _contacts_group.ringtone_path, &ringtoneURI);
1605 if(errorCode != CONTACTS_ERROR_NONE)
1607 LoggerW("Fail to get ringtone_path from address book (ret:" << errorCode << ")");
1611 errorCode = contacts_record_get_bool(contacts_record, _contacts_group.is_read_only, &isReadOnly);
1612 if(errorCode != CONTACTS_ERROR_NONE)
1614 LoggerW("Fail to get is_read_only from address book (ret:" << errorCode << ")");
1618 ContactGroupPtr group = ContactGroupPtr(new ContactGroup());
1620 group->setAddressBookId(addressBookId);
1623 group->setName(name);
1627 if(photoURI != NULL)
1628 group->setPhotoURI(ContactUtility::convertPathToUri(photoURI));
1630 if(ringtoneURI != NULL)
1631 group->setRingtoneURI(ContactUtility::convertPathToUri(ringtoneURI));
1633 group->setReadOnly(isReadOnly);
1635 groups->push_back(group);
1637 errorCode = contacts_list_next(groups_list);
1638 if(errorCode != CONTACTS_ERROR_NONE)
1640 LoggerW("Fail to get next address book (ret:" << errorCode << ")");
1645 event->setContactGroups(groups);
1646 event->setResult(true);
1649 Catch (NotFoundException)
1651 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
1652 event->setExceptionCode(ExceptionCodes::NotFoundException);
1653 event->setResult(false);
1656 Catch (PlatformException)
1658 LoggerE("Error during getting contact : " << _rethrown_exception.GetMessage());
1659 event->setExceptionCode(ExceptionCodes::PlatformException);
1660 event->setResult(false);
1663 Catch (InvalidArgumentException)
1665 LoggerE("Invalid Arguments : " << _rethrown_exception.GetMessage());
1666 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1667 event->setResult(false);
1672 LoggerE("Error on platform : " << _rethrown_exception.GetMessage());
1673 event->setExceptionCode(ExceptionCodes::UnknownException);
1674 event->setResult(false);
1677 if(groups_list != NULL)
1678 contacts_list_destroy(groups_list, true);
1681 void AddressBook::onContactsSvcContactsAdded(ContactArrayPtr &contacts)
1683 EventInfoAddressBookChangeAddedPtr contactsAdded(new EventInfoAddressBookChangeAdded());
1684 contactsAdded->setContacts(contacts);
1685 EventInfoAddressBookChangePtr event = DPL::StaticPointerCast<EventInfoAddressBookChange>(contactsAdded);
1686 EventAddressBookChangeListenerPtr listener(new EventAddressBookChangeListener(event));
1687 m_addressBookEmitters.emit(listener);
1690 void AddressBook::onContactsSvcContactsUpdated(ContactArrayPtr &contacts)
1692 EventInfoAddressBookChangeUpdatedPtr contactsUpdated(new EventInfoAddressBookChangeUpdated());
1693 contactsUpdated->setContacts(contacts);
1694 EventInfoAddressBookChangePtr event = DPL::StaticPointerCast<EventInfoAddressBookChange>(contactsUpdated);
1695 EventAddressBookChangeListenerPtr listener(new EventAddressBookChangeListener(event));
1696 m_addressBookEmitters.emit(listener);
1699 void AddressBook::onContactsSvcContactsRemoved(StringArrayPtr &contactIds)
1701 EventInfoAddressBookChangeRemovedPtr contactsRemoved(new EventInfoAddressBookChangeRemoved());
1702 contactsRemoved->setContactIds(contactIds);
1703 EventInfoAddressBookChangePtr event = DPL::StaticPointerCast<EventInfoAddressBookChange>(contactsRemoved);
1704 EventAddressBookChangeListenerPtr listener(new EventAddressBookChangeListener(event));
1705 m_addressBookEmitters.emit(listener);
1708 void AddressBook::contactsAddBatchResultCallback(int error, int *ids, unsigned int count, void *user_data)
1711 if(user_data == NULL)
1713 LoggerE("user_data is NULL");
1717 KeySharePtrPair *keyPair = (KeySharePtrPair*)user_data;
1719 long key = keyPair->key;
1720 AddressBook *addressBook = keyPair->addressBook;
1724 addressBook->contactsAddBatchResultCallback(error, ids, count, key);
1727 void AddressBook::contactsAddBatchResultCallback(int error, int *ids, unsigned int count, long key)
1729 EventAddressBookAddBatchPtr event;
1731 EventAddressBookAddBatchMap::iterator iter;
1732 iter = m_addBatchEventMap.find(key);
1733 if(iter == m_addBatchEventMap.end())
1735 LoggerE("No event for key : " << key);
1739 event = iter->second;
1740 m_addBatchEventMap.erase(iter);
1742 if(error != CONTACTS_ERROR_NONE)
1744 LoggerE("contacts_db_insert_result_cb gives error : " << error);
1745 event->setResult(false);
1746 event->setExceptionCode(ExceptionCodes::PlatformException);
1747 EventRequestReceiver<EventAddressBookAddBatch>::ManualAnswer(event);
1752 ContactArrayPtr contacts(new ContactArray());
1754 ContactArrayPtr contacts = event->getContacts();
1757 for(unsigned int i=0; i<count; i++)
1760 int contactId = ids[i];
1761 LoggerD("contact id : " << contactId);
1763 contacts_record_h contacts_record = NULL;
1765 errorCode = contacts_db_get_record(_contacts_contact._uri, contactId, &contacts_record);
1766 if(errorCode != CONTACTS_ERROR_NONE)
1768 LoggerD("Fail contacts_db_get_record error _contacts_contact._uri : " << errorCode);
1773 ContactPtr absContact(NULL);
1775 ContactPtr absContact = contacts->at(i);
1781 ContactObjectP2AConverterPtr contactObjConverter(
1782 new ContactObjectP2AConverter(contacts_record, false) );
1784 ContactObjectP2AConverterPtr contactObjConverter(
1785 new ContactObjectP2AConverter(contacts_record, false, absContact) );
1787 absContact = contactObjConverter->getAbstractContact();
1791 LoggerE("Fail error on converting contact to abs contact : " << _rethrown_exception.GetMessage());
1796 if(absContact != NULL)
1797 contacts->push_back(absContact);
1800 if(contacts_record != NULL)
1801 contacts_record_destroy(contacts_record, true);
1804 event->setContacts(contacts);
1805 event->setResult(true);
1806 event->setExceptionCode(ExceptionCodes::None);
1807 EventRequestReceiver<EventAddressBookAddBatch>::ManualAnswer(event);
1810 void AddressBook::contactsUpdateBatchResultCallback(int error, void *user_data)
1814 if(user_data == NULL)
1816 LoggerE("user_data is NULL");
1820 KeySharePtrPair *keyPair = (KeySharePtrPair*)user_data;
1822 long key = keyPair->key;
1823 AddressBook *addressBook = keyPair->addressBook;
1827 addressBook->contactsUpdateBatchResultCallback(error, key);
1830 void AddressBook::contactsUpdateBatchResultCallback(int error, long key)
1832 EventAddressBookUpdateBatchPtr event;
1834 EventAddressBookUpdateBatchMap::iterator iter;
1835 iter = m_updateBatchEventMap.find(key);
1836 if(iter == m_updateBatchEventMap.end())
1838 LoggerE("No event for key : " << key);
1842 event = iter->second;
1843 m_updateBatchEventMap.erase(iter);
1845 if(error != CONTACTS_ERROR_NONE)
1847 LoggerE("contacts_db_result_cb gives error : " << error);
1848 event->setResult(false);
1849 event->setExceptionCode(ExceptionCodes::PlatformException);
1850 EventRequestReceiver<EventAddressBookUpdateBatch>::ManualAnswer(event);
1854 event->setResult(true);
1855 event->setExceptionCode(ExceptionCodes::None);
1856 EventRequestReceiver<EventAddressBookUpdateBatch>::ManualAnswer(event);
1859 void AddressBook::contactsRemoveBatchResultCallback(int error, void *user_data)
1863 if(user_data == NULL)
1865 LoggerE("user_data is NULL");
1869 KeySharePtrPair *keyPair = (KeySharePtrPair*)user_data;
1871 long key = keyPair->key;
1872 AddressBook *addressBook = keyPair->addressBook;
1876 addressBook->contactsRemoveBatchResultCallback(error, key);
1879 void AddressBook::contactsRemoveBatchResultCallback(int error, long key)
1881 EventAddressBookRemoveBatchPtr event;
1883 EventAddressBookRemoveBatchMap::iterator iter;
1884 iter = m_removeBatchEventMap.find(key);
1885 if(iter == m_removeBatchEventMap.end())
1887 LoggerE("No event for key : " << key);
1891 event = iter->second;
1892 m_removeBatchEventMap.erase(iter);
1894 if(error != CONTACTS_ERROR_NONE)
1896 LoggerE("contacts_db_result_cb gives error : " << error);
1897 event->setResult(false);
1898 event->setExceptionCode(ExceptionCodes::PlatformException);
1899 EventRequestReceiver<EventAddressBookRemoveBatch>::ManualAnswer(event);
1903 event->setResult(true);
1904 event->setExceptionCode(ExceptionCodes::None);
1905 EventRequestReceiver<EventAddressBookRemoveBatch>::ManualAnswer(event);