2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @file AddressBook.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
30 #include <dpl/log/log.h>
31 #include <dpl/exception.h>
32 #include <Commons/Exception.h>
33 #include <Commons/Regex.h>
34 #include <API/Contact/IAddressBook.h>
35 #include <API/Contact/IContact.h>
36 #include "AddressBook.h"
38 #include "ContactWrapper.h"
39 #include "ContactSearchEngine.h"
40 #include "ContactFilterValidator.h"
41 #include "DownloadManager.h"
47 using namespace TizenApis::Api::Contact;
48 using namespace TizenApis::Api::Tizen;
49 using namespace WrtDeviceApis::Commons;
52 AddressBook::AddressBook(IAddressBook::AddressBookType changeType) :
53 IAddressBook(changeType),
54 m_name("TEST_ADDRESS_BOOK"),
60 AddressBook::~AddressBook()
64 void AddressBook::OnRequestReceived(const EventAddressBookGetPtr &event)
69 if(!event->getIdIsSet())
70 ThrowMsg(InvalidArgumentException, "Invalid contact id");
72 string contactId = event->getId();
74 ContactPtr contact = internalGetById(contactId);
76 event->setContact(contact);
77 event->setResult(true);
79 } Catch (NotFoundException) {
80 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
81 event->setExceptionCode(ExceptionCodes::NotFoundException);
82 event->setResult(false);
84 } Catch (PlatformException) {
85 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
86 event->setExceptionCode(ExceptionCodes::PlatformException);
87 event->setResult(false);
89 } Catch (InvalidArgumentException) {
90 LogError("Invalid Arguments : " << _rethrown_exception.GetMessage());
91 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
92 event->setResult(false);
95 LogError("Error on platform : " << _rethrown_exception.GetMessage());
96 event->setExceptionCode(ExceptionCodes::UnknownException);
97 event->setResult(false);
101 void AddressBook::OnRequestReceived(const EventAddressBookAddPtr &event)
105 ContactPtr contact(NULL);
108 if(!event->getContactIsSet())
109 ThrowMsg(InvalidArgumentException, "Contacts were not set.");
111 contact = event->getContact();
113 ThrowMsg(InvalidArgumentException, "No contacts.");
115 } Catch(InvalidArgumentException) {
116 LogError("Invalid arguments for adding contacts : " << _rethrown_exception.GetMessage());
117 event->setResult(false);
118 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
124 internalAddContact(contact);
126 string id = contact->getId();
129 ContactPtr insertedContact = internalGetById(id);
130 contact->copy(insertedContact);
133 Catch (PlatformException) {
134 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
135 event->setResult(false);
136 event->setExceptionCode(ExceptionCodes::PlatformException);
140 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
141 event->setResult(false);
142 event->setExceptionCode(ExceptionCodes::UnknownException);
145 //m_latestVersion = get_contact_version();
147 event->setResult(true);
148 event->setExceptionCode(ExceptionCodes::None);
151 void AddressBook::OnRequestReceived(const EventAddressBookAddBatchPtr &event)
154 ContactArrayPtr contacts(NULL);
157 if(!event->getContactsIsSet())
158 ThrowMsg(InvalidArgumentException, "Contacts were not set.");
160 contacts = event->getContacts();
162 ThrowMsg(InvalidArgumentException, "No contacts.");
164 } Catch(InvalidArgumentException) {
165 LogError("Invalid arguments for adding contacts : " << _rethrown_exception.GetMessage());
166 event->setResult(false);
167 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
173 int ret = contacts_svc_begin_trans();
175 LogError("error code " << ret);
176 ThrowMsg(PlatformException, "Error during executing contacts_svc_begin_trans()");
179 for(ContactArray::iterator i = contacts->begin(); i != contacts->end(); i++)
181 ContactPtr contact = *i;
183 internalAddContact(contact);
185 ret = contacts_svc_end_trans(true);
187 LogError("error code " << ret);
188 ThrowMsg(PlatformException, "Error during executing contacts_svc_end_trans()");
191 for(ContactArray::iterator i = contacts->begin(); i != contacts->end(); i++)
193 ContactPtr contact = *i;
195 string id = contact->getId();
198 ContactPtr insertedContact = internalGetById(id);
199 contact->copy(insertedContact);
203 Catch (NotFoundException)
205 contacts_svc_end_trans(false);
206 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
207 event->setResult(false);
208 event->setExceptionCode(ExceptionCodes::NotFoundException);
211 Catch (PlatformException) {
212 contacts_svc_end_trans(false);
213 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
214 event->setResult(false);
215 event->setExceptionCode(ExceptionCodes::PlatformException);
219 contacts_svc_end_trans(false);
220 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
221 event->setResult(false);
222 event->setExceptionCode(ExceptionCodes::PlatformException);
225 //m_latestVersion = get_contact_version();
227 event->setResult(true);
228 event->setExceptionCode(ExceptionCodes::None);
231 void AddressBook::OnRequestReceived(const EventAddressBookUpdatePtr &event)
233 ContactPtr contact(NULL);
236 if(!event->getContactIsSet())
237 ThrowMsg(InvalidArgumentException, "Contacts were not set.");
239 contact = event->getContact();
241 ThrowMsg(InvalidArgumentException, "No contacts.");
243 } Catch(InvalidArgumentException) {
244 LogError("Invalid arguments for adding contacts : " << _rethrown_exception.GetMessage());
245 event->setResult(false);
246 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
252 internalAddContact(contact);
254 Catch (NotFoundException)
256 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
257 event->setResult(false);
258 event->setExceptionCode(ExceptionCodes::NotFoundException);
261 Catch (PlatformException) {
262 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
263 event->setResult(false);
264 event->setExceptionCode(ExceptionCodes::PlatformException);
268 LogError("Error during adding contact : " << _rethrown_exception.GetMessage());
269 event->setResult(false);
270 event->setExceptionCode(ExceptionCodes::PlatformException);
273 //m_latestVersion = get_contact_version();
275 event->setResult(true);
276 event->setExceptionCode(ExceptionCodes::None);
279 void AddressBook::OnRequestReceived(const EventAddressBookUpdateBatchPtr &event)
282 ContactArrayPtr contacts(NULL);
285 if(!event->getContactsIsSet())
286 ThrowMsg(InvalidArgumentException, "Contacts were not set.");
288 contacts = event->getContacts();
290 ThrowMsg(InvalidArgumentException, "No contacts.");
292 } Catch(InvalidArgumentException) {
293 LogError("Invalid arguments for updating contacts : " << _rethrown_exception.GetMessage());
294 event->setResult(false);
295 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
301 int ret = contacts_svc_begin_trans();
303 LogError("error code " << ret);
304 ThrowMsg(PlatformException, "Error during executing contacts_svc_begin_trans()");
307 for(ContactArray::iterator i = contacts->begin(); i != contacts->end(); i++)
309 ContactPtr contact = *i;
311 internalAddContact(contact);
313 contacts_svc_end_trans(true);
315 LogError("error code " << ret);
316 ThrowMsg(PlatformException, "Error during executing contacts_svc_end_trans()");
319 Catch (NotFoundException)
321 contacts_svc_end_trans(false);
322 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
323 event->setResult(false);
324 event->setExceptionCode(ExceptionCodes::NotFoundException);
327 Catch (PlatformException) {
328 contacts_svc_end_trans(false);
329 LogError("Error during updating contact : " << _rethrown_exception.GetMessage());
330 event->setResult(false);
331 event->setExceptionCode(ExceptionCodes::PlatformException);
335 LogError("Error during updating contact : " << _rethrown_exception.GetMessage());
336 event->setResult(false);
337 event->setExceptionCode(ExceptionCodes::PlatformException);
340 //m_latestVersion = get_contact_version();
342 event->setResult(true);
343 event->setExceptionCode(ExceptionCodes::None);
346 void AddressBook::OnRequestReceived(const EventAddressBookRemovePtr &event)
353 if(!event->getContactIdIsSet())
354 ThrowMsg(InvalidArgumentException, "Contacts were not set.");
356 string contactIdStr = event->getContactId();
358 if(!validate("^[0-9]+$", contactIdStr, VALIDATE_MATCH_CASELESS))
359 ThrowMsg(InvalidArgumentException, "Id is wrong (" << contactIdStr << ")" );
362 istringstream iss(contactIdStr);
365 ThrowMsg(InvalidArgumentException, "Id is wrong (" << contactIdStr << ")" );
368 internalDeleteContactById(contactId);
370 Catch (InvalidArgumentException)
372 LogError("Invalid contact id : " << _rethrown_exception.GetMessage());
373 event->setResult(false);
374 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
377 Catch (NotFoundException)
379 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
380 event->setResult(false);
381 event->setExceptionCode(ExceptionCodes::NotFoundException);
384 Catch (PlatformException) {
385 LogError("Error during updating contact : " << _rethrown_exception.GetMessage());
386 event->setResult(false);
387 event->setExceptionCode(ExceptionCodes::PlatformException);
391 LogError("Error during updating contact : " << _rethrown_exception.GetMessage());
392 event->setResult(false);
393 event->setExceptionCode(ExceptionCodes::PlatformException);
396 //m_latestVersion = get_contact_version();
398 event->setResult(true);
399 event->setExceptionCode(ExceptionCodes::None);
402 void AddressBook::OnRequestReceived(const EventAddressBookRemoveBatchPtr &event)
405 StringArrayPtr contactIds(NULL);
408 if(!event->getContactIdsIsSet())
409 ThrowMsg(InvalidArgumentException, "Contacts were not set.");
411 contactIds = event->getContactIds();
413 ThrowMsg(InvalidArgumentException, "No contacts.");
415 } Catch(InvalidArgumentException) {
416 LogError("Invalid arguments for updating contacts : " << _rethrown_exception.GetMessage());
417 event->setResult(false);
418 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
424 int ret = contacts_svc_begin_trans();
426 LogError("error code " << ret);
427 ThrowMsg(PlatformException, "Error during executing contacts_svc_begin_trans()");
429 for(StringArray::iterator i = contactIds->begin(); i != contactIds->end(); i++)
431 string contactIdStr = *i;
435 if(!validate("^[0-9]+$", contactIdStr, VALIDATE_MATCH_CASELESS))
436 ThrowMsg(InvalidArgumentException, "Id is wrong (" << contactIdStr << ")" );
439 istringstream iss(contactIdStr);
442 ThrowMsg(InvalidArgumentException, "Id is wrong (" << contactIdStr << ")" );
445 internalDeleteContactById(contactId);
447 contacts_svc_end_trans(true);
449 LogError("error code " << ret);
450 ThrowMsg(PlatformException, "Error during executing contacts_svc_end_trans()");
453 Catch (InvalidArgumentException)
455 contacts_svc_end_trans(false);
456 LogError("Invalid contact id : " << _rethrown_exception.GetMessage());
457 event->setResult(false);
458 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
461 Catch (NotFoundException)
463 contacts_svc_end_trans(false);
464 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
465 event->setResult(false);
466 event->setExceptionCode(ExceptionCodes::NotFoundException);
469 Catch (PlatformException) {
470 contacts_svc_end_trans(false);
471 LogError("Error during updating contact : " << _rethrown_exception.GetMessage());
472 event->setResult(false);
473 event->setExceptionCode(ExceptionCodes::PlatformException);
477 contacts_svc_end_trans(false);
478 LogError("Error during updating contact : " << _rethrown_exception.GetMessage());
479 event->setResult(false);
480 event->setExceptionCode(ExceptionCodes::PlatformException);
483 //m_latestVersion = get_contact_version();
485 event->setResult(true);
486 event->setExceptionCode(ExceptionCodes::None);
489 void AddressBook::OnRequestReceived(const EventAddressBookFindPtr &event)
493 ContactSearchEnginePtr searchEngine(new ContactSearchEngine(ContactSearchEngine::CONTACT_QUERY));
495 if(event->getFilterIsSet())
497 FilterPtr filter = event->getFilter();
499 FilterValidatorPtr validator = ContactFilterValidatorFactory::getContactFilterValidator();
500 bool success = filter->validate(validator);
503 ThrowMsg(InvalidArgumentException, "Invalid filter arguments.");
505 searchEngine->setCondition(filter);
508 // Currently not support attributeOfInterests
509 //if (event->getAttributesOfInterestIsSet())
510 // searchEngine->setAttributeOfInterest(event->getAttributesOfInterest());
512 // searchEngine->setAttributeOfInterest();
514 searchEngine->setAttributeOfInterest();
516 if (event->getSortModeIsSet())
518 searchEngine->setSortMode(event->getSortMode());
521 searchEngine->setSortMode();
523 event->setContacts(searchEngine->getContactSearchResult());
524 event->setResult(true);
526 } Catch (NotFoundException) {
527 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
528 event->setExceptionCode(ExceptionCodes::NotFoundException);
529 event->setResult(false);
531 } Catch (PlatformException) {
532 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
533 event->setExceptionCode(ExceptionCodes::PlatformException);
534 event->setResult(false);
536 } Catch (InvalidArgumentException) {
537 LogError("Invalid Arguments : " << _rethrown_exception.GetMessage());
538 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
539 event->setResult(false);
541 } Catch (Exception) {
542 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
543 event->setExceptionCode(ExceptionCodes::PlatformException);
544 event->setResult(false);
548 void AddressBook::OnRequestReceived(const EventAddressBookGetCategoriesPtr &event)
553 StringArrayPtr categories = StringArrayPtr(new StringArray());
555 CTSiter *iter = NULL;
556 if(CTS_SUCCESS != contacts_svc_get_list(CTS_LIST_ALL_GROUP, &iter))
557 ThrowMsg(PlatformException, "Fail to get categories list.");
559 while(CTS_SUCCESS == contacts_svc_iter_next(iter))
561 CTSvalue *group = contacts_svc_iter_get_info(iter);
565 const char *groupCStr = contacts_svc_value_get_str(group, CTS_LIST_GROUP_NAME_STR);
567 categories->push_back(string(groupCStr));
569 contacts_svc_value_free(group);
571 contacts_svc_iter_remove(iter);
573 event->setCategories(categories);
574 event->setResult(true);
576 } Catch (NotFoundException) {
577 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
578 event->setExceptionCode(ExceptionCodes::NotFoundException);
579 event->setResult(false);
581 } Catch (PlatformException) {
582 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
583 event->setExceptionCode(ExceptionCodes::PlatformException);
584 event->setResult(false);
586 } Catch (InvalidArgumentException) {
587 LogError("Invalid Arguments : " << _rethrown_exception.GetMessage());
588 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
589 event->setResult(false);
592 LogError("Error on platform : " << _rethrown_exception.GetMessage());
593 event->setExceptionCode(ExceptionCodes::UnknownException);
594 event->setResult(false);
598 void AddressBook::OnRequestReceived(const EventAddressBookAddChangeListenerPtr &event)
603 if(!event->getEmitterIsSet())
604 ThrowMsg(InvalidArgumentException, "Invalid arguments.");
606 EventAddressBookChangeListenerEmitterPtr emitter = event->getEmitter();
608 ThrowMsg(InvalidArgumentException, "Invalid arguments.");
610 DPL::Mutex::ScopedLock lock(&m_addressBookEmittersMutex);
612 if(m_addressBookEmitters.size() == 0)
614 LogDebug("Watch registered initially");
616 m_latestVersion = get_contact_version();
617 contacts_svc_subscribe_change(CTS_SUBSCRIBE_CONTACT_CHANGE,
618 contactsSvcContactChangedCallback, reinterpret_cast<void *>(this));
619 contacts_svc_subscribe_change(CTS_SUBSCRIBE_ADDRESSBOOK_CHANGE,
620 refresh_addressbook_list, reinterpret_cast<void *>(this));
623 m_addressBookEmitters.attach(emitter);
625 long id = ++m_watchIdAcc;
626 m_watchIdMap[id] = emitter->getId();
629 event->setResult(true);
631 } Catch (NotFoundException) {
632 LogError("Contact doesn't exist : " << _rethrown_exception.GetMessage());
633 event->setExceptionCode(ExceptionCodes::NotFoundException);
634 event->setResult(false);
636 } Catch (PlatformException) {
637 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
638 event->setExceptionCode(ExceptionCodes::PlatformException);
639 event->setResult(false);
641 } Catch (InvalidArgumentException) {
642 LogError("Invalid Arguments : " << _rethrown_exception.GetMessage());
643 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
644 event->setResult(false);
646 } Catch (Exception) {
647 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
648 event->setExceptionCode(ExceptionCodes::PlatformException);
649 event->setResult(false);
653 void AddressBook::OnRequestReceived(const EventAddressBookRemoveChangeListenerPtr &event)
658 if(!event->getIdIsSet())
659 ThrowMsg(InvalidArgumentException, "Invalid arguments.");
661 long id = event->getId();
662 if(m_watchIdMap.find(id) == m_watchIdMap.end())
663 ThrowMsg(NotFoundException, "No watchId : " << id);
665 DPL::Mutex::ScopedLock lock(&m_addressBookEmittersMutex);
666 bool success = m_addressBookEmitters.detach(m_watchIdMap[id]);
668 ThrowMsg(NotFoundException, "No watchId : " << id);
670 m_watchIdMap.erase(id);
672 if(m_addressBookEmitters.size() == 0)
674 LogDebug("No watcher is registered. unsubscribing from contact service.");
677 contacts_svc_unsubscribe_change(CTS_SUBSCRIBE_CONTACT_CHANGE,
678 contactsSvcContactChangedCallback);
679 contacts_svc_unsubscribe_change(CTS_SUBSCRIBE_ADDRESSBOOK_CHANGE,
680 refresh_addressbook_list);
683 event->setResult(true);
685 } Catch (NotFoundException) {
686 LogError("WatchId doesn't exist : " << _rethrown_exception.GetMessage());
687 event->setExceptionCode(ExceptionCodes::NotFoundException);
688 event->setResult(false);
690 } Catch (PlatformException) {
691 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
692 event->setExceptionCode(ExceptionCodes::PlatformException);
693 event->setResult(false);
695 } Catch (InvalidArgumentException) {
696 LogError("Invalid Arguments : " << _rethrown_exception.GetMessage());
697 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
698 event->setResult(false);
700 } Catch (Exception) {
701 LogError("Error during deleting contact : " << _rethrown_exception.GetMessage());
702 event->setExceptionCode(ExceptionCodes::PlatformException);
703 event->setResult(false);
708 void AddressBook::internalAddContact(const ContactPtr &newContact)
712 DPL::Mutex::ScopedLock lock(&m_contactEditingMutex);
714 //translate provided path to real filesystem path
715 if (newContact->getPhotoURIIsSet())
717 string realPath = DownloadManager::getInstance()->getRealPath(newContact->getPhotoURI());
718 if(realPath.length() <= MAXPATHLEN)
719 newContact->setPhotoURI(realPath);
722 DPL::SharedPtr<TizenApis::Platform::Contact::Contact> newContactT =
723 DPL::StaticPointerCast<TizenApis::Platform::Contact::Contact>(newContact);
725 ContactWrapperPtr contactWrapper(new ContactWrapper(m_bookType));
727 CTSstruct* contact = newContactT->getCTSStruct();
730 contactWrapper->setAbstractContact(newContact, false);
731 contact = contactWrapper->getPlatformContact();
735 contactWrapper->convertAbstractToPlatform(newContact, contact, false);
740 LogError("Error during converting contact object");
741 ThrowMsg(PlatformException, "Error during converting contact object");
744 if ( newContact->getIdIsSet() )
746 int ret = contacts_svc_update_contact(contact);
747 if (ret == CTS_ERR_DB_RECORD_NOT_FOUND)
748 ThrowMsg(NotFoundException, "Error during executing contacts_svc_update_contact()");
750 LogError("error code " << ret);
751 ThrowMsg(PlatformException, "Error during executing contacts_svc_update_contact()");
756 query_error error_code = QUERY_SUCCESS;
757 int addressBookId = 0;
760 string accountIdStr = newContact->getAccountId();
762 istringstream iss(accountIdStr);
765 LogDebug("accountId changeType is wrong. (id:" << accountIdStr << ")");
769 addressBookId = find_addressbook_id(accountId, &error_code);
770 if(error_code != QUERY_SUCCESS)
773 int id = contacts_svc_insert_contact(addressBookId, contact);
775 LogError("error code " << id);
776 ThrowMsg(PlatformException, "Error during executing contacts_svc_insert_contact()");
778 newContact->setId(id);
782 void AddressBook::internalDeleteContactById(int id)
784 LogDebug("entered with id " << id);
789 DPL::Mutex::ScopedLock lock(&m_contactEditingMutex);
791 int res = contacts_svc_delete_contact(id);
792 if (res == CTS_ERR_DB_RECORD_NOT_FOUND)
793 ThrowMsg(NotFoundException, "Error during executing contacts_svc_delete_contact()");
794 else if (res != CTS_SUCCESS)
795 ThrowMsg(PlatformException, "Error during executing contacts_svc_delete_contact()");
798 ContactPtr AddressBook::internalGetById(const std::string &contactId)
801 ThrowMsg(InvalidArgumentException, "contactId argument is wrong");
803 ContactSearchEnginePtr searchEngine(new ContactSearchEngine(ContactSearchEngine::CONTACT_QUERY));
805 searchEngine->setCondition(contactId);
806 searchEngine->setAttributeOfInterest();
807 searchEngine->setSortMode();
809 ContactArrayPtr contacts = searchEngine->getContactSearchResult();
811 if(contacts->size() == 0)
812 ThrowMsg(NotFoundException, "No contact with ID:" << contactId);
814 return contacts->at(0);
817 void AddressBook::contactsSvcContactChangedCallback(void *data)
819 AddressBook *addressBook = static_cast<AddressBook *>(data);
820 addressBook->contactsSvcContactChangedCallback();
823 void AddressBook::contactsSvcContactChangedCallback()
826 if(m_addressBookEmitters.size() == 0)
832 int changeVersion = 0;
834 bool contactInserted = false;
835 bool contactUpdated = false;
836 bool contactDeleted = false;
838 int *addressBookIds = NULL;
839 int addressBookIdCount = 0;
841 addressBookIds = get_all_addressbook_id(&addressBookIdCount);
843 EventInfoAddressBookChangeAddedPtr contactsAdded(new EventInfoAddressBookChangeAdded());
844 EventInfoAddressBookChangeUpdatedPtr contactsUpdated(new EventInfoAddressBookChangeUpdated());
845 EventInfoAddressBookChangeRemovedPtr contactsRemoved(new EventInfoAddressBookChangeRemoved());
847 ContactArrayPtr addedContacts(new ContactArray());
848 ContactArrayPtr updatedContacts(new ContactArray());
849 StringArrayPtr removedContactIds(new StringArray());
851 for(int i = 0; i < addressBookIdCount; i++)
853 int ret = contacts_svc_get_updated_contacts(addressBookIds[i], m_latestVersion, &iter);
854 if(ret == CTS_SUCCESS)
856 while (contacts_svc_iter_next(iter) == CTS_SUCCESS)
858 CTSstruct *contact= NULL;
859 CTSvalue *row_info = NULL;
860 row_info = contacts_svc_iter_get_info(iter);
862 changeId = contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_ID_INT);
863 changeType = contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_TYPE_INT);
864 changeVersion = contacts_svc_value_get_int(row_info, CTS_LIST_CHANGE_VER_INT);
866 if(changeType == CTS_OPERATION_INSERTED)
868 contactInserted = true;
869 contacts_svc_get_contact(changeId, &contact);
871 ContactWrapperPtr contactWrapper(new ContactWrapper(m_bookType));
872 contactWrapper->setPlatformContact(contact, false);
873 ContactPtr absContact = contactWrapper->getAbstractContact();
874 if(absContact != NULL)
875 addedContacts->push_back(absContact);
879 contacts_svc_struct_free(contact);
884 if(changeType == CTS_OPERATION_UPDATED)
886 contactUpdated = true;
887 contacts_svc_get_contact(changeId, &contact);
889 ContactWrapperPtr contactWrapper(new ContactWrapper(m_bookType));
890 contactWrapper->setPlatformContact(contact, false);
891 ContactPtr absContact = contactWrapper->getAbstractContact();
892 if(absContact != NULL)
893 updatedContacts->push_back(absContact);
897 contacts_svc_struct_free(contact);
902 if(changeType == CTS_OPERATION_DELETED)
904 contactDeleted = true;
906 std::stringstream oss;
908 removedContactIds->push_back(oss.str());
911 contacts_svc_value_free(row_info);
913 contacts_svc_iter_remove(iter);
917 m_latestVersion = get_contact_version();
920 free(addressBookIds);
924 DPL::Mutex::ScopedLock lock(&m_addressBookEmittersMutex);
925 contactsAdded->setContacts(addedContacts);
926 EventInfoAddressBookChangePtr event = DPL::StaticPointerCast<EventInfoAddressBookChange>(contactsAdded);
927 EventAddressBookChangeListenerPtr listener(new EventAddressBookChangeListener(event));
928 m_addressBookEmitters.emit(listener);
933 DPL::Mutex::ScopedLock lock(&m_addressBookEmittersMutex);
934 contactsUpdated->setContacts(updatedContacts);
935 EventInfoAddressBookChangePtr event = DPL::StaticPointerCast<EventInfoAddressBookChange>(contactsUpdated);
936 EventAddressBookChangeListenerPtr listener(new EventAddressBookChangeListener(event));
937 m_addressBookEmitters.emit(listener);
942 DPL::Mutex::ScopedLock lock(&m_addressBookEmittersMutex);
943 contactsRemoved->setContactIds(removedContactIds);
944 EventInfoAddressBookChangePtr event = DPL::StaticPointerCast<EventInfoAddressBookChange>(contactsRemoved);
945 EventAddressBookChangeListenerPtr listener(new EventAddressBookChangeListener(event));
946 m_addressBookEmitters.emit(listener);