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"
32 #include <dpl/log/log.h>
33 #include <dpl/exception.h>
34 #include <Commons/Exception.h>
35 #include <Commons/Regex.h>
36 #include "IAddressBook.h"
39 #include "ContactObjectA2PConverter.h"
40 #include "ContactObjectP2AConverter.h"
41 #include "ContactsSvcObjectConverter.h"
42 #include "ContactSearchEngine.h"
43 #include "ContactFilterValidator.h"
44 #include "ContactUtility.h"
46 #include "ContactQueue.h"
51 using namespace DeviceAPI::Tizen;
52 using namespace WrtDeviceApis::Commons;
55 AddressBook::AddressBook(bool isUnified) :
58 m_name("TEST_ADDRESS_BOOK"),
61 m_isUnifiedAddressBook(isUnified),
67 AddressBook::~AddressBook()
69 if(m_addressBookEmitters.size() != 0)
71 ContactsSvcChangeListenerManagerSingleton::Instance().unregisterContactsChangeListener(this);
75 std::string AddressBook::getId() const
77 if(m_isUnifiedAddressBook)
80 return ContactUtility::intToStr(m_id);
83 void AddressBook::setId(const std::string &value)
85 m_id = ContactUtility::strToInt(value);
88 std::string AddressBook::getAccountId() const
90 if(m_isUnifiedAddressBook)
93 return ContactUtility::intToStr(m_accountId);
96 void AddressBook::setAccountId(const std::string &value)
98 m_accountId = ContactUtility::strToInt(value);
101 void AddressBook::OnRequestReceived(const EventAddressBookGetPtr &event)
106 contacts_record_h contacts_record = NULL;
110 if(!event->getIdIsSet())
111 ThrowMsg(InvalidArgumentException, "Invalid contact id");
113 string contactId = event->getId();
115 int contactIdInt = ContactUtility::strToInt(contactId);
117 errorCode = contacts_db_get_record(_contacts_contact._uri, contactIdInt, &contacts_record);
118 if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
119 ThrowMsg(NotFoundException, "No contact (id:" << contactIdInt << ")");
121 if(!m_isUnifiedAddressBook)
123 int addressBookId = 0;
124 contacts_record_get_int(contacts_record, _contacts_contact.address_book_id, &addressBookId);
125 if(addressBookId != m_id)
126 ThrowMsg(NotFoundException, "No contact (id:" << contactIdInt << ") in this address book.");
129 ContactObjectP2AConverterPtr contactObjConverter(
130 new ContactObjectP2AConverter(contacts_record, false));
131 ContactPtr contact = contactObjConverter->getAbstractContact();
133 event->setContact(contact);
134 event->setResult(true);
136 Catch (NotFoundException)
138 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
139 event->setExceptionCode(ExceptionCodes::NotFoundException);
140 event->setResult(false);
142 Catch (PlatformException)
144 LogError("Error during getting contact : " << _rethrown_exception.GetMessage());
145 event->setExceptionCode(ExceptionCodes::PlatformException);
146 event->setResult(false);
148 Catch (InvalidArgumentException)
150 LogError("Invalid Arguments : " << _rethrown_exception.GetMessage());
151 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
152 event->setResult(false);
156 LogError("Error on platform : " << _rethrown_exception.GetMessage());
157 event->setExceptionCode(ExceptionCodes::UnknownException);
158 event->setResult(false);
161 if(contacts_record != NULL)
162 contacts_record_destroy(contacts_record, true);
165 void AddressBook::OnRequestReceived(const EventAddressBookAddPtr &event)
171 contacts_record_h contacts_record = NULL;
172 ContactPtr contact(NULL);
176 if(!event->getContactIsSet())
177 ThrowMsg(InvalidArgumentException, "Contacts were not set.");
179 contact = event->getContact();
181 ThrowMsg(InvalidArgumentException, "No contacts.");
182 id = ContactUtility::strToInt(contact->getId());
184 ThrowMsg(InvalidArgumentException, "invalid contact object : Id is already set");
187 Catch(InvalidArgumentException)
189 LogError("Invalid arguments for adding contacts : " << _rethrown_exception.GetMessage());
190 event->setResult(false);
191 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
197 DPL::SharedPtr<Contact> contactT =
198 DPL::StaticPointerCast<Contact>(contact);
200 ContactObjectA2PConverterPtr contactObjConverter(NULL);
202 contacts_record = contactT->getPlatformContactObject();
203 if(contacts_record == NULL)
205 contactObjConverter = ContactObjectA2PConverterPtr(
206 new ContactObjectA2PConverter(contact, false) );
207 contacts_record = contactObjConverter->getPlatformContact();
211 contactObjConverter = ContactObjectA2PConverterPtr(
212 new ContactObjectA2PConverter(contact, false, contacts_record) );
213 contacts_record = contactObjConverter->getPlatformContact();
217 if (!contacts_record)
219 LogError("Error during converting contact object");
220 ThrowMsg(PlatformException, "Error during converting contact object");
223 if(m_isUnifiedAddressBook)
224 errorCode = contacts_record_set_int(contacts_record, _contacts_contact.address_book_id, 0);
226 errorCode = contacts_record_set_int(contacts_record, _contacts_contact.address_book_id, m_id);
227 if(errorCode != CONTACTS_ERROR_NONE)
229 LogError("error code : " << errorCode);
230 ThrowMsg(PlatformException, "Error during executing contacts_db_insert_record()");
233 errorCode = contacts_db_insert_record(contacts_record, &contactId);
234 if(errorCode != CONTACTS_ERROR_NONE)
236 LogError("error code : " << errorCode);
237 ThrowMsg(PlatformException, "Error during executing contacts_db_insert_record()");
240 contacts_record = NULL;
241 errorCode = contacts_db_get_record(_contacts_contact._uri, contactId, &contacts_record);
242 if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
243 ThrowMsg(PlatformException, "No contact just inserted (id:" << contactId << ")");
245 ContactObjectP2AConverterPtr contactObjConverterForInserted(
246 new ContactObjectP2AConverter(contacts_record, false));
247 ContactPtr insertedContact = contactObjConverterForInserted->getAbstractContact();
248 contact->copy(insertedContact);
250 event->setResult(true);
251 event->setExceptionCode(ExceptionCodes::None);
253 Catch (PlatformException)
255 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
256 event->setResult(false);
257 event->setExceptionCode(ExceptionCodes::PlatformException);
261 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
262 event->setResult(false);
263 event->setExceptionCode(ExceptionCodes::UnknownException);
266 if(contacts_record != NULL)
267 contacts_record_destroy(contacts_record, true);
270 void AddressBook::AddressBookAddBatch(const EventAddressBookAddBatchPtr &event)
275 contacts_list_h contacts_list = NULL;
276 ContactArrayPtr contacts(NULL);
280 if(!event->getContactsIsSet())
281 ThrowMsg(InvalidArgumentException, "Contacts were not set.");
283 contacts = event->getContacts();
285 ThrowMsg(InvalidArgumentException, "No contacts.");
288 Catch(InvalidArgumentException)
290 ContactQueueManagerSingleton::Instance().decreaseQueueList();
291 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
293 ContactQueueManagerSingleton::Instance().pop();
296 LogError("Invalid arguments for adding contacts : " << _rethrown_exception.GetMessage());
297 event->setResult(false);
298 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
304 errorCode = contacts_list_create(&contacts_list);
305 if(errorCode != CONTACTS_ERROR_NONE)
307 ThrowMsg(PlatformException, "Fail to create contacts_list_h");
310 for(ContactArray::iterator i = contacts->begin(); i != contacts->end(); i++)
312 contacts_record_h contacts_record = NULL;
313 ContactPtr contact = *i;
314 ContactObjectA2PConverterPtr contactObjConverter(NULL);
318 DPL::SharedPtr<Contact> newContactT =
319 DPL::StaticPointerCast<Contact>(contact);
320 contacts_record = newContactT->getPlatformContactObject();
321 if(contacts_record == NULL)
323 errorCode = contacts_record_create(_contacts_contact._uri, &contacts_record);
324 if(errorCode != CONTACTS_ERROR_NONE)
326 ThrowMsg(PlatformException, "Error during creating contact record : " << errorCode);
330 contactObjConverter = ContactObjectA2PConverterPtr(
331 new ContactObjectA2PConverter(contact, false, contacts_record) );
332 contacts_record = contactObjConverter->getPlatformContact();
334 if(contacts_record == NULL)
336 ThrowMsg(PlatformException, "Error during converting contact object");
339 if(m_isUnifiedAddressBook)
340 errorCode = contacts_record_set_int(contacts_record, _contacts_contact.address_book_id, 0);
342 errorCode = contacts_record_set_int(contacts_record, _contacts_contact.address_book_id, m_id);
343 if(errorCode != CONTACTS_ERROR_NONE)
345 ThrowMsg(PlatformException, "Error during add address book id : " << errorCode);
348 errorCode = contacts_list_add(contacts_list, contacts_record);
349 if(errorCode != CONTACTS_ERROR_NONE)
351 ThrowMsg(PlatformException, "Error during add to list : " << errorCode);
356 ThrowMsg(NotFoundException, "Error during converting contact object");
357 // LogError("Error during converting contact object : " << _rethrown_exception.GetMessage());
362 KeySharePtrPair *keyPair = new KeySharePtrPair();
363 keyPair->key = m_eventMapAcc;
364 keyPair->addressBook = this;
365 errorCode = contacts_db_insert_records_async(contacts_list, contactsAddBatchResultCallback, (void*)keyPair);
366 if(errorCode != CONTACTS_ERROR_NONE)
369 ThrowMsg(PlatformException, "Error during contacts_db_insert_records_async");
372 errorCode = contacts_list_destroy(contacts_list, true);
373 contacts_list = NULL;
374 if(errorCode != CONTACTS_ERROR_NONE)
377 ThrowMsg(PlatformException, "Error during contacts_list_destroy");
380 pair<long, EventAddressBookAddBatchPtr> keyEventPair(m_eventMapAcc, event);
381 m_addBatchEventMap.insert(keyEventPair);
385 // event->switchToManualAnswer();
387 Catch (NotFoundException)
389 ContactQueueManagerSingleton::Instance().decreaseQueueList();
390 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
392 ContactQueueManagerSingleton::Instance().pop();
394 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
395 event->setResult(false);
396 event->setExceptionCode(ExceptionCodes::NotFoundException);
398 Catch (PlatformException)
400 ContactQueueManagerSingleton::Instance().decreaseQueueList();
401 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
403 ContactQueueManagerSingleton::Instance().pop();
405 LogError("Error during adding contacts : " << _rethrown_exception.GetMessage());
406 event->setResult(false);
407 event->setExceptionCode(ExceptionCodes::PlatformException);
411 ContactQueueManagerSingleton::Instance().decreaseQueueList();
412 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
414 ContactQueueManagerSingleton::Instance().pop();
416 LogError("Error during adding contacts : " << _rethrown_exception.GetMessage());
417 event->setResult(false);
418 event->setExceptionCode(ExceptionCodes::PlatformException);
421 if(contacts_list != NULL)
422 contacts_list_destroy(contacts_list, true);
425 void AddressBook::OnRequestReceived(const EventAddressBookAddBatchPtr &event)
431 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
433 AddressBookAddBatch(event);
435 ContactQueueManagerSingleton::Instance().push(ContactQueueManager::ADDBATCH, this, event);
437 ContactQueueManagerSingleton::Instance().increaseQueueList();
438 event->switchToManualAnswer();
442 LogError("Error during adding contacts : " << _rethrown_exception.GetMessage());
443 event->setResult(false);
444 event->setExceptionCode(ExceptionCodes::PlatformException);
448 void AddressBook::OnRequestReceived(const EventAddressBookUpdatePtr &event)
451 ContactPtr contact(NULL);
455 if(!event->getContactIsSet())
456 ThrowMsg(InvalidArgumentException, "Contacts were not set.");
458 contact = event->getContact();
460 ThrowMsg(InvalidArgumentException, "No contact.");
462 if(!contact->getIdIsSet() || !contact->getAddressBookIdIsSet())
463 ThrowMsg(InvalidArgumentException, "Contact has no ID");
465 if(!m_isUnifiedAddressBook && contact->getAddressBookId() != getId())
466 ThrowMsg(InvalidArgumentException, "Wrong address book ID");
468 Catch(InvalidArgumentException)
470 LogError("Invalid arguments for adding contacts : " << _rethrown_exception.GetMessage());
471 event->setResult(false);
472 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
480 DPL::SharedPtr<Contact> contactT =
481 DPL::StaticPointerCast<Contact>(contact);
483 ContactObjectA2PConverterPtr contactObjConverter(NULL);
485 contacts_record_h contacts_record = contactT->getPlatformContactObject();
486 if(contacts_record == NULL)
488 contactObjConverter = ContactObjectA2PConverterPtr(
489 new ContactObjectA2PConverter(contact, false) );
490 contacts_record = contactObjConverter->getPlatformContact();
494 contactObjConverter = ContactObjectA2PConverterPtr(
495 new ContactObjectA2PConverter(contact, false, contacts_record) );
496 contacts_record = contactObjConverter->getPlatformContact();
499 if (!contacts_record)
501 LogError("Error during converting contact object");
502 ThrowMsg(PlatformException, "Error during converting contact object");
505 errorCode = contacts_db_update_record(contacts_record);
506 if (errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
507 ThrowMsg(NotFoundException, "Error during executing contacts_db_update_record()");
508 else if (errorCode != CONTACTS_ERROR_NONE)
510 LogError("error code : " << errorCode);
511 ThrowMsg(PlatformException, "Error during executing contacts_db_update_record()");
514 Catch (NotFoundException)
516 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
517 event->setResult(false);
518 event->setExceptionCode(ExceptionCodes::NotFoundException);
521 Catch (PlatformException)
523 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
524 event->setResult(false);
525 event->setExceptionCode(ExceptionCodes::PlatformException);
530 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
531 event->setResult(false);
532 event->setExceptionCode(ExceptionCodes::PlatformException);
535 //m_latestVersion = get_contact_version();
537 event->setResult(true);
538 event->setExceptionCode(ExceptionCodes::None);
541 void AddressBook::AddressBookUpdateBatch(const EventAddressBookUpdateBatchPtr &event)
545 contacts_list_h contacts_list = NULL;
546 ContactArrayPtr contacts(NULL);
550 if(!event->getContactsIsSet())
551 ThrowMsg(InvalidArgumentException, "Contacts were not set.");
553 contacts = event->getContacts();
555 ThrowMsg(InvalidArgumentException, "No contacts.");
558 Catch(InvalidArgumentException)
560 ContactQueueManagerSingleton::Instance().decreaseQueueList();
561 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
563 ContactQueueManagerSingleton::Instance().pop();
565 LogError("Invalid arguments for updating contacts : " << _rethrown_exception.GetMessage());
566 event->setResult(false);
567 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
573 errorCode = contacts_list_create(&contacts_list);
574 if(errorCode != CONTACTS_ERROR_NONE)
576 ThrowMsg(PlatformException, "Fail to create contacts_list_h");
579 for(ContactArray::iterator i = contacts->begin(); i != contacts->end(); i++)
581 contacts_record_h contacts_record = NULL;
582 ContactPtr contact = *i;
583 ContactObjectA2PConverterPtr contactObjConverter(NULL);
588 ThrowMsg(InvalidArgumentException, "No contact.");
590 if(contact->getIdIsSet() == false)
591 ThrowMsg(InvalidArgumentException, "Contact has no ID");
593 if(!m_isUnifiedAddressBook && ( !contact->getAddressBookIdIsSet() || contact->getAddressBookId() != getId()))
594 ThrowMsg(InvalidArgumentException, "Wrong address book ID");
596 DPL::SharedPtr<Contact> newContactT =
597 DPL::StaticPointerCast<Contact>(contact);
598 contacts_record = newContactT->getPlatformContactObject();
599 if(contacts_record == NULL)
601 LogDebug("id : " << contact->getId());
602 int contactIdInt = ContactUtility::strToInt(contact->getId());
603 errorCode = contacts_db_get_record(_contacts_contact._uri, contactIdInt, &contacts_record);
604 if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
605 ThrowMsg(NotFoundException, "No contact (id:" << contactIdInt << ")");
607 contactObjConverter = ContactObjectA2PConverterPtr(
608 new ContactObjectA2PConverter(contact, false, contacts_record) );
609 contacts_record = contactObjConverter->getPlatformContact();
611 if(contacts_record == NULL)
613 ThrowMsg(PlatformException, "Error during converting contact object");
616 errorCode = contacts_list_add(contacts_list, contacts_record);
617 if(errorCode != CONTACTS_ERROR_NONE)
619 ThrowMsg(PlatformException, "Error during add to list");
624 ThrowMsg(NotFoundException, "Error during converting contact object");
625 // LogError("Error during converting contact object : " << _rethrown_exception.GetMessage());
630 KeySharePtrPair *keyPair = new KeySharePtrPair();
631 keyPair->key = m_eventMapAcc;
632 keyPair->addressBook = this;
633 errorCode = contacts_db_update_records_async(contacts_list, contactsUpdateBatchResultCallback, (void*)keyPair);
634 if(errorCode != CONTACTS_ERROR_NONE)
637 ThrowMsg(PlatformException, "Error during contacts_db_update_records_async");
640 errorCode = contacts_list_destroy(contacts_list, true);
641 contacts_list = NULL;
642 if(errorCode != CONTACTS_ERROR_NONE)
645 ThrowMsg(PlatformException, "Error during contacts_list_destroy");
648 pair<long, EventAddressBookUpdateBatchPtr> keyEventPair(m_eventMapAcc, event);
649 m_updateBatchEventMap.insert(keyEventPair);
653 // event->switchToManualAnswer();
655 Catch (NotFoundException)
657 ContactQueueManagerSingleton::Instance().decreaseQueueList();
658 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
660 ContactQueueManagerSingleton::Instance().pop();
662 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
663 event->setResult(false);
664 event->setExceptionCode(ExceptionCodes::NotFoundException);
666 Catch (PlatformException)
668 ContactQueueManagerSingleton::Instance().decreaseQueueList();
669 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
671 ContactQueueManagerSingleton::Instance().pop();
673 LogError("Error during adding contacts : " << _rethrown_exception.GetMessage());
674 event->setResult(false);
675 event->setExceptionCode(ExceptionCodes::PlatformException);
679 ContactQueueManagerSingleton::Instance().decreaseQueueList();
680 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
682 ContactQueueManagerSingleton::Instance().pop();
684 LogError("Error during adding contacts : " << _rethrown_exception.GetMessage());
685 event->setResult(false);
686 event->setExceptionCode(ExceptionCodes::PlatformException);
689 if(contacts_list != NULL)
690 contacts_list_destroy(contacts_list, false);
693 void AddressBook::OnRequestReceived(const EventAddressBookUpdateBatchPtr &event)
699 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
701 AddressBookUpdateBatch(event);
703 ContactQueueManagerSingleton::Instance().push(ContactQueueManager::UPATEBATCH, this, event);
705 ContactQueueManagerSingleton::Instance().increaseQueueList();
706 event->switchToManualAnswer();
710 LogError("Error during updating contacts : " << _rethrown_exception.GetMessage());
711 event->setResult(false);
712 event->setExceptionCode(ExceptionCodes::PlatformException);
716 void AddressBook::OnRequestReceived(const EventAddressBookRemovePtr &event)
723 if(!event->getContactIdIsSet())
724 ThrowMsg(InvalidArgumentException, "Contact id was not set.");
726 string contactIdStr = event->getContactId();
728 if(!ContactUtility::checkStrIsUInt(contactIdStr))
729 ThrowMsg(InvalidArgumentException, "Id is wrong (" << contactIdStr << ")" );
731 int contactId = ContactUtility::strToInt(contactIdStr);
733 if(!m_isUnifiedAddressBook)
735 contacts_record_h contacts_record = NULL;
736 errorCode = contacts_db_get_record(_contacts_simple_contact._uri, contactId, &contacts_record);
737 if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
738 ThrowMsg(PlatformException, "No contact id (id:" << contactId << ")");
740 int addressBookId = 0;
741 errorCode = contacts_record_get_int(contacts_record, _contacts_simple_contact.address_book_id, &addressBookId);
742 if(errorCode != CONTACTS_ERROR_NONE)
743 ThrowMsg(PlatformException, "Error while getting address book id");
745 if(addressBookId != m_id)
746 ThrowMsg(PlatformException, "Contact id (" << contactId << ") is not a member of this address book.");
749 errorCode = contacts_db_delete_record(_contacts_contact._uri, contactId);
750 if(errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
751 ThrowMsg(NotFoundException, "Error during executing contacts_db_delete_record()");
752 else if (errorCode != CONTACTS_ERROR_NONE)
753 ThrowMsg(PlatformException, "Error during executing contacts_db_delete_record()");
755 Catch (InvalidArgumentException)
757 LogError("Invalid contact id : " << _rethrown_exception.GetMessage());
758 event->setResult(false);
759 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
762 Catch (NotFoundException)
764 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
765 event->setResult(false);
766 event->setExceptionCode(ExceptionCodes::NotFoundException);
769 Catch (PlatformException)
771 LogError("Error during updating contact : " << _rethrown_exception.GetMessage());
772 event->setResult(false);
773 event->setExceptionCode(ExceptionCodes::PlatformException);
778 LogError("Error during updating contact : " << _rethrown_exception.GetMessage());
779 event->setResult(false);
780 event->setExceptionCode(ExceptionCodes::PlatformException);
784 event->setResult(true);
785 event->setExceptionCode(ExceptionCodes::None);
788 void AddressBook::AddressBookRemoveBatch(const EventAddressBookRemoveBatchPtr &event)
792 StringArrayPtr contactIds(NULL);
796 if(!event->getContactIdsIsSet())
797 ThrowMsg(InvalidArgumentException, "Contact IDs were not set.");
799 contactIds = event->getContactIds();
801 ThrowMsg(InvalidArgumentException, "No contact IDs.");
804 Catch(InvalidArgumentException)
806 ContactQueueManagerSingleton::Instance().decreaseQueueList();
807 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
809 ContactQueueManagerSingleton::Instance().pop();
811 LogError("Invalid arguments for removing contacts : " << _rethrown_exception.GetMessage());
812 event->setResult(false);
813 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
819 int *ids = new int[contactIds->size()];
820 int *tmpIds = new int[contactIds->size()];
823 if(errorCode != CONTACTS_ERROR_NONE)
825 ThrowMsg(PlatformException, "Fail to create contacts_list_h");
828 for(StringArray::iterator i = contactIds->begin(); i != contactIds->end(); i++)
830 string contactIdStr = *i;
836 if(!ContactUtility::checkStrIsUInt(contactIdStr))
837 ThrowMsg(InvalidArgumentException, "Id is wrong (" << contactIdStr << ")" );
839 contactId = ContactUtility::strToInt(contactIdStr);
841 if(!m_isUnifiedAddressBook)
843 contacts_record_h contacts_record = NULL;
844 errorCode = contacts_db_get_record(_contacts_simple_contact._uri, contactId, &contacts_record);
845 if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
846 ThrowMsg(PlatformException, "No contact id (id:" << contactId << ")");
848 int addressBookId = 0;
849 errorCode = contacts_record_get_int(contacts_record, _contacts_simple_contact.address_book_id, &addressBookId);
850 if(errorCode != CONTACTS_ERROR_NONE)
851 ThrowMsg(PlatformException, "Error while getting address book id");
853 if(addressBookId != m_id)
854 ThrowMsg(PlatformException, "Contact id (" << contactId << ") is not a member of this address book.");
857 ids[count] = contactId;
858 tmpIds[count] = contactId;
863 ThrowMsg(NotFoundException, "Error during converting contact object");
864 // LogError("Error during converting contact object : " << _rethrown_exception.GetMessage());
869 contacts_filter_h filter = NULL;
870 contacts_query_h query = NULL;
872 errorCode = contacts_query_create(_contacts_simple_contact._uri, &query);
873 if(errorCode != CONTACTS_ERROR_NONE)
874 ThrowMsg(PlatformException, "contacts_query_create error : " << errorCode << " (" << __FUNCTION__ << ")");
876 errorCode = contacts_filter_create( _contacts_simple_contact._uri, &filter );
877 if(errorCode != CONTACTS_ERROR_NONE)
878 ThrowMsg(PlatformException, "contacts_query_create error : " << errorCode << " (" << __FUNCTION__ << ")");
880 for(int i = 0; i < contactIds->size(); i++)
882 errorCode = contacts_filter_add_int(filter, _contacts_simple_contact.id, CONTACTS_MATCH_EQUAL, tmpIds[i]);
883 if(i == (contactIds->size() - 1))
886 errorCode = contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_OR);
887 if(errorCode != CONTACTS_ERROR_NONE)
888 ThrowMsg(PlatformException, "contacts_filter_add_operator error : " << errorCode << " (" << __FUNCTION__ << ")");
891 errorCode = contacts_query_set_filter(query, filter);
892 if(errorCode != CONTACTS_ERROR_NONE)
893 ThrowMsg(PlatformException, "contacts_query_set_filter error : " << errorCode << " (" << __FUNCTION__ << ")");
895 int record_count = 0;
896 errorCode = contacts_db_get_count_with_query(query, &record_count);
897 if(errorCode != CONTACTS_ERROR_NONE)
898 ThrowMsg(PlatformException, "contacts_db_get_count_with_query error : " << errorCode << " (" << __FUNCTION__ << ")");
901 contacts_filter_destroy(filter);
903 contacts_query_destroy(query);
905 if(contactIds->size() != (unsigned int)record_count)
906 ThrowMsg(InvalidArgumentException, "Ids' db count : " << record_count << " (" << __FUNCTION__ << ")");
908 KeySharePtrPair *keyPair = new KeySharePtrPair();
909 keyPair->key = m_eventMapAcc;
910 keyPair->addressBook = this;
911 errorCode = contacts_db_delete_records_async(_contacts_contact._uri, ids, count, contactsRemoveBatchResultCallback, (void*)keyPair);
920 if(errorCode != CONTACTS_ERROR_NONE)
923 ThrowMsg(PlatformException, "Error during add to list");
926 pair<long, EventAddressBookRemoveBatchPtr> keyEventPair(m_eventMapAcc, event);
927 m_removeBatchEventMap.insert(keyEventPair);
931 // event->switchToManualAnswer();
933 Catch (InvalidArgumentException)
935 ContactQueueManagerSingleton::Instance().decreaseQueueList();
936 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
938 ContactQueueManagerSingleton::Instance().pop();
940 LogError("Invalid contact id : " << _rethrown_exception.GetMessage());
941 event->setResult(false);
942 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
945 Catch (NotFoundException)
947 ContactQueueManagerSingleton::Instance().decreaseQueueList();
948 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
950 ContactQueueManagerSingleton::Instance().pop();
952 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
953 event->setResult(false);
954 event->setExceptionCode(ExceptionCodes::NotFoundException);
957 Catch (PlatformException)
959 ContactQueueManagerSingleton::Instance().decreaseQueueList();
960 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
962 ContactQueueManagerSingleton::Instance().pop();
964 LogError("Error during deleting contacts : " << _rethrown_exception.GetMessage());
965 event->setResult(false);
966 event->setExceptionCode(ExceptionCodes::PlatformException);
971 ContactQueueManagerSingleton::Instance().decreaseQueueList();
972 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
974 ContactQueueManagerSingleton::Instance().pop();
976 LogError("Error during deleting contacts : " << _rethrown_exception.GetMessage());
977 event->setResult(false);
978 event->setExceptionCode(ExceptionCodes::PlatformException);
983 void AddressBook::OnRequestReceived(const EventAddressBookRemoveBatchPtr &event)
989 bool isEmpty = ContactQueueManagerSingleton::Instance().empty();
991 AddressBookRemoveBatch(event);
993 ContactQueueManagerSingleton::Instance().push(ContactQueueManager::DELETEBATCH, this, event);
995 ContactQueueManagerSingleton::Instance().increaseQueueList();
996 event->switchToManualAnswer();
1000 LogError("Error during deleting contacts : " << _rethrown_exception.GetMessage());
1001 event->setResult(false);
1002 event->setExceptionCode(ExceptionCodes::PlatformException);
1006 void AddressBook::OnRequestReceived(const EventAddressBookFindPtr &event)
1008 LogDebug("entered");
1010 contacts_query_h query = NULL;
1011 contacts_filter_h filter = NULL;
1012 contacts_list_h contacts_list = NULL;
1014 ContactArrayPtr contacts = ContactArrayPtr(new ContactArray());
1018 ContactSearchEnginePtr searchEngine(new ContactSearchEngine());
1020 if(!m_isUnifiedAddressBook)
1021 searchEngine->setAddressBookId(m_id);
1023 if(event->getFilterIsSet())
1025 FilterPtr filter = event->getFilter();
1027 FilterValidatorPtr validator = ContactFilterValidatorFactory::getContactFilterValidator();
1028 bool success = filter->validate(validator);
1031 ThrowMsg(InvalidArgumentException, "Invalid filter arguments.");
1033 searchEngine->setCondition(filter);
1036 if (event->getSortModeIsSet())
1038 searchEngine->setSortMode(event->getSortMode());
1041 searchEngine->setSortMode();
1043 event->setContacts(searchEngine->getContactSearchResult());
1044 event->setResult(true);
1046 Catch (NotFoundException)
1048 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
1049 event->setExceptionCode(ExceptionCodes::NotFoundException);
1050 event->setResult(false);
1052 Catch (PlatformException)
1054 LogError("Error during finding contact : " << _rethrown_exception.GetMessage());
1055 event->setExceptionCode(ExceptionCodes::PlatformException);
1056 event->setResult(false);
1058 Catch (InvalidArgumentException)
1060 LogError("Invalid Arguments : " << _rethrown_exception.GetMessage());
1061 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1062 event->setResult(false);
1066 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
1067 event->setExceptionCode(ExceptionCodes::PlatformException);
1068 event->setResult(false);
1072 contacts_filter_destroy(filter);
1075 contacts_query_destroy(query);
1077 if(contacts_list != NULL)
1078 contacts_list_destroy(contacts_list, true);
1081 void AddressBook::OnRequestReceived(const EventAddressBookAddChangeListenerPtr &event)
1083 LogDebug("entered");
1087 if(!event->getEmitterIsSet())
1088 ThrowMsg(InvalidArgumentException, "Invalid arguments.");
1090 EventAddressBookChangeListenerEmitterPtr emitter = event->getEmitter();
1092 ThrowMsg(InvalidArgumentException, "Invalid arguments.");
1094 if(m_addressBookEmitters.size() == 0)
1096 LogDebug("Watch registered initially");
1098 if(m_isUnifiedAddressBook)
1099 ContactsSvcChangeListenerManagerSingleton::Instance().registerContactsChangeListener(this, -1);
1101 ContactsSvcChangeListenerManagerSingleton::Instance().registerContactsChangeListener(this, m_id);
1104 m_addressBookEmitters.attach(emitter);
1106 long id = ContactsSvcChangeListenerManagerSingleton::Instance().getWatchIdAndInc();
1107 m_watchIdMap[id] = emitter->getId();
1110 event->setResult(true);
1112 Catch (NotFoundException)
1114 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
1115 event->setExceptionCode(ExceptionCodes::NotFoundException);
1116 event->setResult(false);
1118 Catch (PlatformException)
1120 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
1121 event->setExceptionCode(ExceptionCodes::PlatformException);
1122 event->setResult(false);
1124 Catch (InvalidArgumentException)
1126 LogError("Invalid Arguments : " << _rethrown_exception.GetMessage());
1127 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1128 event->setResult(false);
1132 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
1133 event->setExceptionCode(ExceptionCodes::PlatformException);
1134 event->setResult(false);
1138 void AddressBook::OnRequestReceived(const EventAddressBookRemoveChangeListenerPtr &event)
1140 LogDebug("entered");
1144 if(!event->getIdIsSet())
1145 ThrowMsg(InvalidArgumentException, "Invalid arguments.");
1147 long id = event->getId();
1148 if(m_watchIdMap.find(id) == m_watchIdMap.end())
1149 ThrowMsg(NotFoundException, "No watchId : " << id);
1151 bool success = m_addressBookEmitters.detach(m_watchIdMap[id]);
1153 ThrowMsg(NotFoundException, "No watchId : " << id);
1155 m_watchIdMap.erase(id);
1157 if(m_addressBookEmitters.size() == 0)
1159 LogDebug("No watcher is registered. unsubscribing from contact service.");
1161 ContactsSvcChangeListenerManagerSingleton::Instance().unregisterContactsChangeListener(this);
1164 event->setResult(true);
1167 Catch (NotFoundException)
1169 LogError("WatchId doesn't exist : " << _rethrown_exception.GetMessage());
1170 event->setExceptionCode(ExceptionCodes::NotFoundException);
1171 event->setResult(false);
1173 Catch (PlatformException)
1175 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
1176 event->setExceptionCode(ExceptionCodes::PlatformException);
1177 event->setResult(false);
1179 Catch (InvalidArgumentException)
1181 LogError("Invalid Arguments : " << _rethrown_exception.GetMessage());
1182 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1183 event->setResult(false);
1187 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
1188 event->setExceptionCode(ExceptionCodes::PlatformException);
1189 event->setResult(false);
1193 void AddressBook::OnRequestReceived(const EventAddressBookGetGroupPtr &event)
1195 LogDebug("entered");
1198 contacts_record_h contacts_record = NULL;
1202 if(!event->getIdIsSet())
1203 ThrowMsg(InvalidArgumentException, "Invalid group id");
1205 string groupId = event->getId();
1207 int groupIdInt = ContactUtility::strToInt(groupId);
1209 errorCode = contacts_db_get_record(_contacts_group._uri, groupIdInt, &contacts_record);
1210 if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
1211 ThrowMsg(NotFoundException, "No group (id:" << groupIdInt << ")");
1213 if(!m_isUnifiedAddressBook)
1215 int addressBookId = 0;
1216 contacts_record_get_int(contacts_record, _contacts_group.address_book_id, &addressBookId);
1217 if(addressBookId != m_id)
1218 ThrowMsg(NotFoundException, "No group (id:" << groupIdInt << ") in this address book.");
1221 ContactGroupPtr group = ContactGroupPtr(new ContactGroup());
1223 ContactsSvcObjectConverter::convertToAbstract(contacts_record, group);
1225 event->setContactGroup(group);
1226 event->setResult(true);
1229 Catch (NotFoundException)
1231 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
1232 event->setExceptionCode(ExceptionCodes::NotFoundException);
1233 event->setResult(false);
1236 Catch (PlatformException)
1238 LogError("Error during getting contact : " << _rethrown_exception.GetMessage());
1239 event->setExceptionCode(ExceptionCodes::PlatformException);
1240 event->setResult(false);
1243 Catch (InvalidArgumentException)
1245 LogError("Invalid Arguments : " << _rethrown_exception.GetMessage());
1246 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1247 event->setResult(false);
1252 LogError("Error on platform : " << _rethrown_exception.GetMessage());
1253 event->setExceptionCode(ExceptionCodes::UnknownException);
1254 event->setResult(false);
1257 if(contacts_record != NULL)
1258 contacts_record_destroy(contacts_record, true);
1261 void AddressBook::OnRequestReceived(const EventAddressBookAddGroupPtr &event)
1263 LogDebug("entered");
1267 contacts_record_h contacts_record = NULL;
1268 ContactGroupPtr group(NULL);
1272 if(!event->getContactGroupIsSet())
1273 ThrowMsg(InvalidArgumentException, "group were not set.");
1275 group = event->getContactGroup();
1277 ThrowMsg(InvalidArgumentException, "No group.");
1279 if(group->getIdIsSet() || group->getAddressBookIdIsSet())
1280 ThrowMsg(InvalidArgumentException, "ContactGroup object is wrong");
1282 Catch(InvalidArgumentException)
1284 LogError("Invalid arguments for adding contacts : " << _rethrown_exception.GetMessage());
1285 event->setResult(false);
1286 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1292 errorCode = contacts_record_create(_contacts_group._uri, &contacts_record);
1293 if(errorCode != CONTACTS_ERROR_NONE)
1295 LogError("error code : " << errorCode);
1296 ThrowMsg(PlatformException, "Error during executing contacts_record_create()");
1299 if(m_isUnifiedAddressBook)
1300 errorCode = contacts_record_set_int(contacts_record, _contacts_group.address_book_id, 0);
1302 errorCode = contacts_record_set_int(contacts_record, _contacts_group.address_book_id, m_id);
1303 if(errorCode != CONTACTS_ERROR_NONE)
1305 LogError("error code : " << errorCode);
1306 ThrowMsg(PlatformException, "Error during executing contacts_record_set_int()");
1309 ContactsSvcObjectConverter::convertToPlatform(group, contacts_record);
1312 errorCode = contacts_db_insert_record(contacts_record, &groupId);
1313 if(errorCode != CONTACTS_ERROR_NONE)
1315 LogError("error code : " << errorCode);
1316 ThrowMsg(PlatformException, "Error during executing contacts_db_insert_record()");
1318 group->setId(groupId);
1319 group->setAddressBookId(m_id);
1321 event->setResult(true);
1322 event->setExceptionCode(ExceptionCodes::None);
1324 Catch (PlatformException)
1326 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
1327 event->setResult(false);
1328 event->setExceptionCode(ExceptionCodes::PlatformException);
1332 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
1333 event->setResult(false);
1334 event->setExceptionCode(ExceptionCodes::UnknownException);
1337 if(contacts_record != NULL)
1338 contacts_record_destroy(contacts_record, true);
1341 void AddressBook::OnRequestReceived(const EventAddressBookUpdateGroupPtr &event)
1343 LogDebug("entered");
1347 contacts_record_h contacts_record = NULL;
1348 ContactGroupPtr group(NULL);
1352 if(!event->getContactGroupIsSet())
1353 ThrowMsg(InvalidArgumentException, "group were not set.");
1355 group = event->getContactGroup();
1357 ThrowMsg(InvalidArgumentException, "No group.");
1359 if(!group->getIdIsSet() || !group->getAddressBookIdIsSet())
1360 ThrowMsg(InvalidArgumentException, "Contact Group is wrong");
1362 if(!m_isUnifiedAddressBook && group->getAddressBookId() != getId())
1363 ThrowMsg(InvalidArgumentException, "Wrong address book ID");
1365 Catch(InvalidArgumentException)
1367 LogError("Invalid arguments for updating group : " << _rethrown_exception.GetMessage());
1368 event->setResult(false);
1369 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1373 if(group->getReadOnly())
1375 LogWarning("Canceling updating readonly data.");
1376 event->setResult(false);
1377 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1383 int id = ContactUtility::strToInt(group->getId());
1384 LogDebug("Load platform object id : " << id);
1385 errorCode = contacts_db_get_record(_contacts_group._uri, id, &contacts_record);
1386 if (errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
1387 ThrowMsg(NotFoundException, "Error during executing contacts_db_get_record()");
1388 else if (errorCode != CONTACTS_ERROR_NONE)
1390 LogError("error code : " << errorCode);
1391 ThrowMsg(PlatformException, "Error during executing contacts_db_get_record()");
1394 LogDebug("Update group : " << id);
1395 ContactsSvcObjectConverter::convertToPlatform(group, contacts_record);
1397 errorCode = contacts_db_update_record(contacts_record);
1398 if (errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
1399 ThrowMsg(NotFoundException, "Error during executing contacts_db_update_record()");
1400 else if (errorCode != CONTACTS_ERROR_NONE)
1402 LogError("error code : " << errorCode);
1403 ThrowMsg(PlatformException, "Error during executing contacts_db_update_record()");
1406 event->setResult(true);
1407 event->setExceptionCode(ExceptionCodes::None);
1409 Catch (NotFoundException)
1411 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
1412 event->setResult(false);
1413 event->setExceptionCode(ExceptionCodes::NotFoundException);
1415 Catch (PlatformException)
1417 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
1418 event->setResult(false);
1419 event->setExceptionCode(ExceptionCodes::PlatformException);
1423 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
1424 event->setResult(false);
1425 event->setExceptionCode(ExceptionCodes::PlatformException);
1428 if(contacts_record != NULL)
1429 contacts_record_destroy(contacts_record, true);
1434 void AddressBook::OnRequestReceived(const EventAddressBookRemoveGroupPtr &event)
1436 LogDebug("entered");
1441 if(!event->getContactGroupIdIsSet())
1442 ThrowMsg(InvalidArgumentException, "group were not set.");
1444 string groupIdStr = event->getContactGroupId();
1446 if(!ContactUtility::checkStrIsUInt(groupIdStr))
1447 ThrowMsg(InvalidArgumentException, "Id is wrong (" << groupIdStr << ")" );
1449 int groupId = ContactUtility::strToInt(groupIdStr);
1451 if(!m_isUnifiedAddressBook)
1453 contacts_record_h contacts_record = NULL;
1454 errorCode = contacts_db_get_record(_contacts_group._uri, groupId, &contacts_record);
1455 if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
1456 ThrowMsg(PlatformException, "No group id (id:" << groupId << ")");
1458 int addressBookId = 0;
1459 errorCode = contacts_record_get_int(contacts_record, _contacts_group.address_book_id, &addressBookId);
1460 if(errorCode != CONTACTS_ERROR_NONE)
1461 ThrowMsg(PlatformException, "Error while getting address book id");
1463 if(addressBookId != m_id)
1464 ThrowMsg(PlatformException, "Contact id (" << groupId << ") is not a member of this address book.");
1467 errorCode = contacts_db_delete_record(_contacts_group._uri, groupId);
1468 if(errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
1469 ThrowMsg(NotFoundException, "Error during executing contacts_db_delete_record()");
1470 else if (errorCode != CONTACTS_ERROR_NONE)
1471 ThrowMsg(PlatformException, "Error during executing contacts_db_delete_record()");
1473 event->setResult(true);
1474 event->setExceptionCode(ExceptionCodes::None);
1476 Catch (InvalidArgumentException)
1478 LogError("Invalid group id : " << _rethrown_exception.GetMessage());
1479 event->setResult(false);
1480 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1482 Catch (NotFoundException)
1484 LogError("Group doesn't exist : " << _rethrown_exception.GetMessage());
1485 event->setResult(false);
1486 event->setExceptionCode(ExceptionCodes::NotFoundException);
1488 Catch (PlatformException)
1490 LogError("Error during removing group : " << _rethrown_exception.GetMessage());
1491 event->setResult(false);
1492 event->setExceptionCode(ExceptionCodes::PlatformException);
1496 LogError("Error during removing group : " << _rethrown_exception.GetMessage());
1497 event->setResult(false);
1498 event->setExceptionCode(ExceptionCodes::PlatformException);
1502 void AddressBook::OnRequestReceived(const EventAddressBookGetGroupsPtr &event)
1504 LogDebug("entered");
1508 ContactGroupArrayPtr groups(new ContactGroupArray());
1509 contacts_list_h groups_list = NULL;
1514 if(m_isUnifiedAddressBook)
1516 errorCode = contacts_db_get_all_records(_contacts_group._uri, 0, 0, &groups_list);
1517 if(errorCode != CONTACTS_ERROR_NONE)
1518 ThrowMsg(PlatformException, "Fail to get group list (ret:" << errorCode << ")");
1522 contacts_query_h query = NULL;
1523 contacts_filter_h filter = NULL;
1525 errorCode = contacts_query_create(_contacts_group._uri, &query);
1526 if(errorCode != CONTACTS_ERROR_NONE)
1527 ThrowMsg(PlatformException, "Fail to get contacts_query_create (ret:" << errorCode << ")");
1529 errorCode = contacts_filter_create(_contacts_group._uri, &filter);
1530 if(errorCode != CONTACTS_ERROR_NONE)
1531 ThrowMsg(PlatformException, "Fail to get contacts_filter_create (ret:" << errorCode << ")");
1533 errorCode = contacts_filter_add_int(filter, _contacts_group.address_book_id, CONTACTS_MATCH_EQUAL, m_id);
1534 if(errorCode != CONTACTS_ERROR_NONE)
1535 ThrowMsg(PlatformException, "Fail to get contacts_filter_add_int (ret:" << errorCode << ")");
1537 errorCode = contacts_query_set_filter(query, filter);
1538 if(errorCode != CONTACTS_ERROR_NONE)
1539 ThrowMsg(PlatformException, "Fail to get contacts_query_set_filter (ret:" << errorCode << ")");
1541 errorCode = contacts_db_get_records_with_query(query, 0, 0, &groups_list);
1542 if(errorCode != CONTACTS_ERROR_NONE)
1543 ThrowMsg(PlatformException, "Fail to get contacts_db_get_records_with_query (ret:" << errorCode << ")");
1545 errorCode = contacts_filter_destroy(filter);
1546 if(errorCode != CONTACTS_ERROR_NONE)
1547 ThrowMsg(PlatformException, "Fail to get contacts_filter_destroy (ret:" << errorCode << ")");
1549 errorCode = contacts_query_destroy(query);
1550 if(errorCode != CONTACTS_ERROR_NONE)
1551 ThrowMsg(PlatformException, "Fail to get contacts_query_destroy (ret:" << errorCode << ")");
1554 unsigned int record_count = 0;
1555 errorCode = contacts_list_get_count(groups_list, &record_count);
1556 if(errorCode != CONTACTS_ERROR_NONE)
1557 ThrowMsg(PlatformException, "Fail to get contacts_list_get_count (ret:" << errorCode << ")");
1559 contacts_list_first(groups_list);
1560 for(unsigned int i=0; i<record_count; i++)
1562 contacts_record_h contacts_record;
1563 errorCode = contacts_list_get_current_record_p(groups_list, &contacts_record);
1564 if(errorCode != CONTACTS_ERROR_NONE || contacts_record == NULL)
1566 LogWarning("Fail to get group record (ret:" << errorCode << ")");
1571 int addressBookId = 0;
1573 char *ringtoneURI = NULL;
1574 char *photoURI = NULL;
1575 bool isReadOnly = false;
1577 errorCode = contacts_record_get_int(contacts_record, _contacts_group.id, &id);
1578 if(errorCode != CONTACTS_ERROR_NONE)
1580 LogWarning("Fail to get id from address book (ret:" << errorCode << ")");
1584 errorCode = contacts_record_get_int(contacts_record, _contacts_group.address_book_id, &addressBookId);
1585 if(errorCode != CONTACTS_ERROR_NONE)
1587 LogWarning("Fail to get address book id from address book (ret:" << errorCode << ")");
1591 errorCode = contacts_record_get_str_p(contacts_record, _contacts_group.name, &name);
1592 if(errorCode != CONTACTS_ERROR_NONE)
1594 LogWarning("Fail to get name from address book (ret:" << errorCode << ")");
1598 errorCode = contacts_record_get_str_p(contacts_record, _contacts_group.image_path, &photoURI);
1599 if(errorCode != CONTACTS_ERROR_NONE)
1601 LogWarning("Fail to get image_path from address book (ret:" << errorCode << ")");
1605 errorCode = contacts_record_get_str_p(contacts_record, _contacts_group.ringtone_path, &ringtoneURI);
1606 if(errorCode != CONTACTS_ERROR_NONE)
1608 LogWarning("Fail to get ringtone_path from address book (ret:" << errorCode << ")");
1612 errorCode = contacts_record_get_bool(contacts_record, _contacts_group.is_read_only, &isReadOnly);
1613 if(errorCode != CONTACTS_ERROR_NONE)
1615 LogWarning("Fail to get is_read_only from address book (ret:" << errorCode << ")");
1619 ContactGroupPtr group = ContactGroupPtr(new ContactGroup());
1621 group->setAddressBookId(addressBookId);
1624 group->setName(name);
1628 if(photoURI != NULL)
1629 group->setPhotoURI(ContactUtility::convertPathToUri(photoURI));
1631 if(ringtoneURI != NULL)
1632 group->setRingtoneURI(ContactUtility::convertPathToUri(ringtoneURI));
1634 group->setReadOnly(isReadOnly);
1636 groups->push_back(group);
1638 errorCode = contacts_list_next(groups_list);
1639 if(errorCode != CONTACTS_ERROR_NONE)
1641 LogWarning("Fail to get next address book (ret:" << errorCode << ")");
1646 event->setContactGroups(groups);
1647 event->setResult(true);
1650 Catch (NotFoundException)
1652 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
1653 event->setExceptionCode(ExceptionCodes::NotFoundException);
1654 event->setResult(false);
1657 Catch (PlatformException)
1659 LogError("Error during getting contact : " << _rethrown_exception.GetMessage());
1660 event->setExceptionCode(ExceptionCodes::PlatformException);
1661 event->setResult(false);
1664 Catch (InvalidArgumentException)
1666 LogError("Invalid Arguments : " << _rethrown_exception.GetMessage());
1667 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
1668 event->setResult(false);
1673 LogError("Error on platform : " << _rethrown_exception.GetMessage());
1674 event->setExceptionCode(ExceptionCodes::UnknownException);
1675 event->setResult(false);
1678 if(groups_list != NULL)
1679 contacts_list_destroy(groups_list, true);
1682 void AddressBook::onContactsSvcContactsAdded(ContactArrayPtr &contacts)
1684 EventInfoAddressBookChangeAddedPtr contactsAdded(new EventInfoAddressBookChangeAdded());
1685 contactsAdded->setContacts(contacts);
1686 EventInfoAddressBookChangePtr event = DPL::StaticPointerCast<EventInfoAddressBookChange>(contactsAdded);
1687 EventAddressBookChangeListenerPtr listener(new EventAddressBookChangeListener(event));
1688 m_addressBookEmitters.emit(listener);
1691 void AddressBook::onContactsSvcContactsUpdated(ContactArrayPtr &contacts)
1693 EventInfoAddressBookChangeUpdatedPtr contactsUpdated(new EventInfoAddressBookChangeUpdated());
1694 contactsUpdated->setContacts(contacts);
1695 EventInfoAddressBookChangePtr event = DPL::StaticPointerCast<EventInfoAddressBookChange>(contactsUpdated);
1696 EventAddressBookChangeListenerPtr listener(new EventAddressBookChangeListener(event));
1697 m_addressBookEmitters.emit(listener);
1700 void AddressBook::onContactsSvcContactsRemoved(StringArrayPtr &contactIds)
1702 EventInfoAddressBookChangeRemovedPtr contactsRemoved(new EventInfoAddressBookChangeRemoved());
1703 contactsRemoved->setContactIds(contactIds);
1704 EventInfoAddressBookChangePtr event = DPL::StaticPointerCast<EventInfoAddressBookChange>(contactsRemoved);
1705 EventAddressBookChangeListenerPtr listener(new EventAddressBookChangeListener(event));
1706 m_addressBookEmitters.emit(listener);
1709 void AddressBook::contactsAddBatchResultCallback(int error, int *ids, unsigned int count, void *user_data)
1711 LogDebug("entered");
1712 if(user_data == NULL)
1714 LogError("user_data is NULL");
1718 KeySharePtrPair *keyPair = (KeySharePtrPair*)user_data;
1720 long key = keyPair->key;
1721 AddressBook *addressBook = keyPair->addressBook;
1725 addressBook->contactsAddBatchResultCallback(error, ids, count, key);
1728 void AddressBook::contactsAddBatchResultCallback(int error, int *ids, unsigned int count, long key)
1730 EventAddressBookAddBatchPtr event;
1732 EventAddressBookAddBatchMap::iterator iter;
1733 iter = m_addBatchEventMap.find(key);
1734 if(iter == m_addBatchEventMap.end())
1736 LogError("No event for key : " << key);
1740 event = iter->second;
1741 m_addBatchEventMap.erase(iter);
1743 if(error != CONTACTS_ERROR_NONE)
1745 LogError("contacts_db_insert_result_cb gives error : " << error);
1746 event->setResult(false);
1747 event->setExceptionCode(ExceptionCodes::PlatformException);
1748 EventRequestReceiver<EventAddressBookAddBatch>::ManualAnswer(event);
1753 ContactArrayPtr contacts(new ContactArray());
1755 ContactArrayPtr contacts = event->getContacts();
1758 for(unsigned int i=0; i<count; i++)
1761 int contactId = ids[i];
1762 LogDebug("contact id : " << contactId);
1764 contacts_record_h contacts_record = NULL;
1766 errorCode = contacts_db_get_record(_contacts_contact._uri, contactId, &contacts_record);
1767 if(errorCode != CONTACTS_ERROR_NONE)
1769 LogDebug("Fail contacts_db_get_record error _contacts_contact._uri : " << errorCode);
1774 ContactPtr absContact(NULL);
1776 ContactPtr absContact = contacts->at(i);
1782 ContactObjectP2AConverterPtr contactObjConverter(
1783 new ContactObjectP2AConverter(contacts_record, false) );
1785 ContactObjectP2AConverterPtr contactObjConverter(
1786 new ContactObjectP2AConverter(contacts_record, false, absContact) );
1788 absContact = contactObjConverter->getAbstractContact();
1792 LogError("Fail error on converting contact to abs contact : " << _rethrown_exception.GetMessage());
1797 if(absContact != NULL)
1798 contacts->push_back(absContact);
1801 if(contacts_record != NULL)
1802 contacts_record_destroy(contacts_record, true);
1805 event->setContacts(contacts);
1806 event->setResult(true);
1807 event->setExceptionCode(ExceptionCodes::None);
1808 EventRequestReceiver<EventAddressBookAddBatch>::ManualAnswer(event);
1811 void AddressBook::contactsUpdateBatchResultCallback(int error, void *user_data)
1813 LogDebug("entered");
1815 if(user_data == NULL)
1817 LogError("user_data is NULL");
1821 KeySharePtrPair *keyPair = (KeySharePtrPair*)user_data;
1823 long key = keyPair->key;
1824 AddressBook *addressBook = keyPair->addressBook;
1828 addressBook->contactsUpdateBatchResultCallback(error, key);
1831 void AddressBook::contactsUpdateBatchResultCallback(int error, long key)
1833 EventAddressBookUpdateBatchPtr event;
1835 EventAddressBookUpdateBatchMap::iterator iter;
1836 iter = m_updateBatchEventMap.find(key);
1837 if(iter == m_updateBatchEventMap.end())
1839 LogError("No event for key : " << key);
1843 event = iter->second;
1844 m_updateBatchEventMap.erase(iter);
1846 if(error != CONTACTS_ERROR_NONE)
1848 LogError("contacts_db_result_cb gives error : " << error);
1849 event->setResult(false);
1850 event->setExceptionCode(ExceptionCodes::PlatformException);
1851 EventRequestReceiver<EventAddressBookUpdateBatch>::ManualAnswer(event);
1855 event->setResult(true);
1856 event->setExceptionCode(ExceptionCodes::None);
1857 EventRequestReceiver<EventAddressBookUpdateBatch>::ManualAnswer(event);
1860 void AddressBook::contactsRemoveBatchResultCallback(int error, void *user_data)
1862 LogDebug("entered");
1864 if(user_data == NULL)
1866 LogError("user_data is NULL");
1870 KeySharePtrPair *keyPair = (KeySharePtrPair*)user_data;
1872 long key = keyPair->key;
1873 AddressBook *addressBook = keyPair->addressBook;
1877 addressBook->contactsRemoveBatchResultCallback(error, key);
1880 void AddressBook::contactsRemoveBatchResultCallback(int error, long key)
1882 EventAddressBookRemoveBatchPtr event;
1884 EventAddressBookRemoveBatchMap::iterator iter;
1885 iter = m_removeBatchEventMap.find(key);
1886 if(iter == m_removeBatchEventMap.end())
1888 LogError("No event for key : " << key);
1892 event = iter->second;
1893 m_removeBatchEventMap.erase(iter);
1895 if(error != CONTACTS_ERROR_NONE)
1897 LogError("contacts_db_result_cb gives error : " << error);
1898 event->setResult(false);
1899 event->setExceptionCode(ExceptionCodes::PlatformException);
1900 EventRequestReceiver<EventAddressBookRemoveBatch>::ManualAnswer(event);
1904 event->setResult(true);
1905 event->setExceptionCode(ExceptionCodes::None);
1906 EventRequestReceiver<EventAddressBookRemoveBatch>::ManualAnswer(event);