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.
17 * @file AddressBook.cpp
18 * @author Lukasz Marek (l.marek@samsung.com)
28 #include <dpl/log/log.h>
29 #include <Commons/Exception.h>
30 #include <Commons/Regex.h>
31 #include <Contact/IAddressBook.h>
32 #include <Contact/Contact.h>
33 #include <Filesystem/Manager.h>
34 #include "AddressBook.h"
35 #include "ContactWrapper.h"
37 namespace WrtDeviceApis {
42 bool AddressBook::m_isDaInit = false;
44 AddressBook::AddressBook(IAddressBookObject::BookType type) :
45 IAddressBookObject(type)
48 //platform layer seems to not support different storage types yet
50 if ( type == IAddressBookObject::PhoneBook ) {
51 LogDebug("PhoneBook");
53 else if ( type == IAddressBookObject::SIMBook ) {
57 LogError("wrong book type");
61 setDaClientIdByDaInit();
64 AddressBook::~AddressBook()
67 unsetDaClientIdByDaDeinit();
70 std::string AddressBook::getName() const
72 //return address book name
76 void AddressBook::setName(const std::string &value)
78 //set address book name
82 void AddressBook::daNotifyCb(user_notify_info_t *notify_info,
85 if (notify_info == NULL) {
86 LogDebug("notify_info is NULL!!");
90 if (notify_info->state == DA_STATE_FAILED || notify_info->state ==
92 if (user_data != NULL) {
93 DownImgInfo *downImgInfo;
94 downImgInfo = static_cast<DownImgInfo*>(user_data);
95 (downImgInfo->getWaitableEvent())->Signal();
100 void AddressBook::daGetDdInfoCb(user_dd_info_t* /*dd_info*/,
104 LogDebug("Entered!");
107 void AddressBook::daUpdateDownloadInfoCb(user_download_info_t *download_info,
110 LogDebug("Entered!");
112 if (download_info != NULL && user_data != NULL) {
113 if (download_info->saved_path) {
115 "download_info->saved_path : " << download_info->saved_path);
116 DownImgInfo *downImgInfo = static_cast<DownImgInfo*>(user_data);
117 std::string savedPath = download_info->saved_path;
118 downImgInfo->setdownloadedImgPath(savedPath);
119 (downImgInfo->getWaitableEvent())->Signal();
124 void AddressBook::setDaClientIdByDaInit(void)
126 LogDebug("Entered!! m_isDaInit: " << m_isDaInit);
128 LogDebug("DaInit already done!");
133 int da_ret = DA_INVALID_ID;
134 da_client_cb_t da_cb;
135 memset(&da_cb, 0, sizeof(da_client_cb_t));
137 da_cb.user_noti_cb = &daNotifyCb;
138 da_cb.send_dd_info_cb = &daGetDdInfoCb;
139 da_cb.update_dl_info_cb = &daUpdateDownloadInfoCb;
140 da_ret = da_init(&da_cb, DA_DOWNLOAD_MANAGING_METHOD_AUTO);
142 if (da_ret != DA_RESULT_OK) {
143 LogDebug("Error during da_init() da_ret: " << da_ret);
147 void AddressBook::unsetDaClientIdByDaDeinit(void)
149 LogDebug("Entered!! m_isDaInit: " << m_isDaInit);
156 std::string AddressBook::getRealPath(const std::string &path) const
158 //translate provided path to real filesystem path
161 LogDebug("path : " << path);
162 if (Commons::validate("^(http(s)?://)?\\w+.*$",
164 Commons::VALIDATE_MATCH_CASELESS))
166 // if path is URL then Img will be downloaded.
167 LogDebug("path is URL. Start Image download!");
168 return downloadImage(path);
170 //translate provided path to real filesystem path
171 Filesystem::Api::IPathPtr currentPath =
172 Filesystem::Api::IPath::create(path);
173 return currentPath->getFullPath();
176 Catch(Commons::Exception)
178 //probably path doesn't exist
179 LogError("invalid path");
181 //on error return empty string
182 return std::string();
185 std::string AddressBook::downloadImage(const std::string &imgUrl) const
189 LogDebug("URL : " << imgUrl);
191 DownImgInfo downImgInfo;
192 int da_ret = DA_INVALID_ID;
193 int download_id = -1;
195 DPL::WaitableEvent waitableEvent;
196 downImgInfo.setWaitableEvent(&waitableEvent);
197 da_ret = da_start_download_with_extension(
198 imgUrl.c_str(), &download_id,
199 DA_FEATURE_USER_DATA,
203 if (da_ret != DA_RESULT_OK) {
205 Commons::PlatformException,
206 "Error during da_start_download_with_extension() da_ret: "
209 DPL::WaitForSingleHandle(waitableEvent.GetHandle());
210 waitableEvent.Reset();
212 if ((downImgInfo.getdownloadedImgPath()).empty()) {
213 ThrowMsg(Commons::Exception, "Download is Fail!");
215 return downImgInfo.getdownloadedImgPath();
217 catch (const Commons::Exception &ex)
219 LogError("Probably invalid URL. " << ex.GetMessage());
221 //on error return empty string(file path)
222 return std::string();
225 std::vector<std::string> AddressBook::internalExportToVCard(
226 const std::vector<ContactPtr> &contacts) const
228 //container for result files
229 std::vector<std::string> result;
230 //path for currently exporting contact
237 std::fstream outputFile;
238 std::stringstream name;
239 //get current time to create unique file names
240 std::time_t t = std::time(0);
241 //iterate over all contacts to export
242 for (std::size_t i = 0; i < contacts.size(); ++i) {
243 //create new file name for current contact
245 name << "/Others/contacts_" << i << "_" << t << ".vcf";
246 //translate path for real filesystem
247 path = getRealPath(name.str());
249 LogError("Cannot convert path to real file system path");
250 return std::vector<std::string>();
252 LogDebug("filename: " << path);
255 //check if file exists and return if does
256 if (Filesystem::Manager::fileExists(path)) {
257 LogError("File already exists");
258 return std::vector<std::string>();
261 Catch(Commons::PlatformException)
263 //error during check, return
264 LogError("Cannot validate if file exists.");
265 return std::vector<std::string>();
267 //create and open new file
268 outputFile.open(path.c_str(), std::fstream::out | std::fstream::app);
269 if (!outputFile.fail()) {
270 //when failes is created then continue with processing a contact
271 //convert file into platform struct
272 ContactWrapperPtr wrapper(new ContactWrapper(m_bookType));
273 wrapper->setAbstractContact(contacts[i]);
274 contact = wrapper->getPlatformContact();
275 //call API function to get VCard stresm
277 contacts_svc_get_vcard_from_contact(contact,
279 //save results to file
280 outputFile.write(vcard_stream, strlen(vcard_stream));
282 //add file to results
283 result.push_back(name.str());
285 //return if cannot create file
286 LogError("cannot open file");
287 return std::vector<std::string>();
296 void AddressBook::internalAddGroup(const std::string &groupName) const
299 //dont add group when already exists
300 if (getGroupId(groupName) != -1) {
301 LogWarning("group already exists");
304 //create plaform struct describing group
305 CTSvalue *group = contacts_svc_value_new(CTS_VALUE_GROUP);
308 contacts_svc_value_set_str(group,
309 CTS_GROUP_VAL_NAME_STR,
311 //set default ring tone
312 contacts_svc_value_set_str(group, CTS_GROUP_VAL_RINGTONE_STR, "");
313 //try to add new group
314 if (contacts_svc_insert_group(0, group) < CTS_SUCCESS) {
315 //failed, free data and throw exception
316 contacts_svc_value_free(group);
317 LogError("contacts_svc_insert_group() Failed");
318 ThrowMsg(Commons::PlatformException,
319 "Error during executing contacts_svc_insert_group()");
322 contacts_svc_value_free(group);
325 ThrowMsg(Commons::PlatformException, "Group object not created");
329 void AddressBook::internalDeleteGroup(const std::string &groupName) const
332 int groupId = getGroupId(groupName);
334 LogDebug("deleting group, id " << groupId);
335 if (CTS_SUCCESS != contacts_svc_delete_group(groupId)) {
336 ThrowMsg(Commons::PlatformException,
337 "Error during deleting a group");
342 int AddressBook::getGroupId(const std::string &groupName)
344 LogDebug("entered with group name " << groupName);
345 CTSiter *iter = NULL;
346 if (CTS_SUCCESS == contacts_svc_get_list(CTS_LIST_ALL_GROUP, &iter)) {
347 while (CTS_SUCCESS == contacts_svc_iter_next(iter)) {
348 CTSvalue *group = contacts_svc_iter_get_info(iter);
350 const char *foundGroupName = contacts_svc_value_get_str(
352 CTS_LIST_GROUP_NAME_STR);
353 if (foundGroupName && groupName == foundGroupName) {
354 int groupId = contacts_svc_value_get_int(
356 CTS_LIST_GROUP_ID_INT);
357 contacts_svc_value_free(group);
358 contacts_svc_iter_remove(iter);
361 contacts_svc_value_free(group);
364 contacts_svc_iter_remove(iter);
369 std::vector<std::string> AddressBook::internalFindGroup(
370 const std::string &groupName) const
372 LogDebug("entered with group filter: " << groupName);
373 std::vector<std::string> result;
374 CTSiter *iter = NULL;
375 if (CTS_SUCCESS == contacts_svc_get_list(CTS_LIST_ALL_GROUP, &iter)) {
376 while (CTS_SUCCESS == contacts_svc_iter_next(iter)) {
377 CTSvalue *group = contacts_svc_iter_get_info(iter);
379 const char *foundGroupName = contacts_svc_value_get_str(
381 CTS_LIST_GROUP_NAME_STR);
382 if (foundGroupName) {
383 if (groupName.empty() ||
384 Commons::validate(groupName,
386 Commons::VALIDATE_MATCH_CASELESS))
388 LogDebug("found group " << foundGroupName);
389 result.push_back(foundGroupName);
392 contacts_svc_value_free(group);
395 contacts_svc_iter_remove(iter);
400 void AddressBook::internalAddContact(const ContactPtr &newContact) const
404 //translate provided path to real filesystem path
405 if (newContact->getPhotoIsSet()) {
406 std::string realpath = getRealPath(newContact->getPhotoUri());
407 newContact->setPhotoUri(realpath);
410 ContactWrapperPtr contactWrapper(new ContactWrapper(m_bookType));
411 contactWrapper->setAbstractContact(newContact);
412 CTSstruct* contact = contactWrapper->getPlatformContact();
414 LogError("Error during converting contact object");
415 ThrowMsg(Commons::PlatformException,
416 "Error during converting contact object");
418 int id = contacts_svc_insert_contact(0, contact);
420 LogError("error code " << id);
421 ThrowMsg(Commons::PlatformException,
422 "Error during executing contacts_svc_insert_contact()");
424 newContact->setId(id);
427 void AddressBook::internalUpdateContact(const Api::ContactPtr &updatedContact)
431 if (!updatedContact) {
432 ThrowMsg(Commons::PlatformException, "Contact is NULL");
434 if (!updatedContact->getIdIsSet()) {
435 ThrowMsg(Commons::PlatformException, "Contact is not saved yet");
437 LogDebug("update contact with id " << updatedContact->getId());
438 ContactWrapperPtr contactWrapper(new ContactWrapper(m_bookType));
439 contactWrapper->setAbstractContact(updatedContact);
440 CTSstruct* contact = contactWrapper->getPlatformContact();
442 LogError("Error during converting contact object");
443 ThrowMsg(Commons::PlatformException,
444 "Error during converting contact object");
446 if (CTS_SUCCESS != contacts_svc_update_contact(contact)) {
447 ThrowMsg(Commons::PlatformException,
448 "Error during executing contacts_svc_update_contact()");
450 LogDebug("update successful");
453 ContactPtr AddressBook::internalFindContactById(int id) const
455 LogDebug("entered with id " << id);
456 CTSstruct *contact = NULL;
457 int errorCode = contacts_svc_get_contact(id, &contact);
458 if (CTS_SUCCESS == errorCode) {
459 ContactWrapperPtr contactWrapper(new ContactWrapper(m_bookType));
460 contactWrapper->setPlatformContact(contact);
461 ContactPtr contact = contactWrapper->getAbstractContact();
463 ThrowMsg(Commons::PlatformException,
464 "Error during converting contact object");
467 } else if (CTS_ERR_DB_RECORD_NOT_FOUND == errorCode) {
468 LogWarning("record not found");
469 return ContactPtr(NULL);
471 ThrowMsg(Commons::PlatformException,
472 "Error during executing contacts_svc_get_contact()");
476 std::vector<ContactPtr> AddressBook::internalFindContactByFilter(
477 const ContactFilterPtr &filter,
480 bool caseSensitive) const
483 std::vector<ContactPtr> result;
485 CTSvalue *platformContact = NULL;
486 CTSiter *iter = NULL;
487 if (CTS_SUCCESS == contacts_svc_get_list(CTS_LIST_ALL_CONTACT, &iter)) {
488 while (CTS_SUCCESS == contacts_svc_iter_next(iter)) {
489 platformContact = contacts_svc_iter_get_info(iter);
490 if (platformContact) {
491 int contactId = contacts_svc_value_get_int(
493 CTS_LIST_CONTACT_ID_INT);
494 ContactPtr contact = internalFindContactById(contactId);
495 /*Compare the contact to the filter*/
496 if (contact && matchToFilter(filter, contact, caseSensitive)) {
497 LogDebug("found contact by filter");
498 if (count >= startIndex) {
499 result.push_back(contact);
503 contacts_svc_value_free(platformContact);
504 if (endIndex != -1 && count > endIndex) {
505 contacts_svc_iter_remove(iter);
510 contacts_svc_iter_remove(iter);
515 std::vector<ContactPtr> AddressBook::internalFindContactByGroup(
516 const std::string &groupName) const
518 LogDebug("entered with group: " << groupName);
519 std::vector<ContactPtr> result;
520 CTSvalue *platformContact = NULL;
521 CTSiter *iter = NULL;
522 if (CTS_SUCCESS == contacts_svc_get_list(CTS_LIST_ALL_CONTACT, &iter)) {
523 while (CTS_SUCCESS == contacts_svc_iter_next(iter)) {
524 platformContact = contacts_svc_iter_get_info(iter);
525 if (platformContact) {
526 int contactId = contacts_svc_value_get_int(
528 CTS_LIST_CONTACT_ID_INT);
529 ContactPtr contact = internalFindContactById(contactId);
531 std::vector<std::string>::const_iterator found =
532 find(contact->getGroups().begin(),
533 contact->getGroups().end(), groupName);
534 if (found != contact->getGroups().end()) {
535 LogDebug("found contact in group");
536 result.push_back(contact);
539 contacts_svc_value_free(platformContact);
542 contacts_svc_iter_remove(iter);
547 void AddressBook::internalDeleteContactById(int id) const
549 LogDebug("entered with id " << id);
551 CTSstruct *contact = NULL;
552 /*If the contact is not existed, throw NotFoundException*/
553 if (CTS_SUCCESS != contacts_svc_get_contact(id, &contact)) {
554 ThrowMsg(Commons::NotFoundException,
555 "Can't delete contact, contact doesn't exist. Error code");
558 if (CTS_SUCCESS != contacts_svc_delete_contact(id)) {
559 ThrowMsg(Commons::PlatformException,
560 "Error during executing contacts_svc_delete_contact()");
565 unsigned int AddressBook::internalGetNumberOfContacts() const
568 int result = contacts_svc_count_with_int(
569 CTS_GET_COUNT_CONTACTS_IN_ADDRESSBOOK,
572 ThrowMsg(Commons::PlatformException,
573 "Error during executing contacts_svc_count_with_int()");
578 std::vector<ContactPtr> AddressBook::mergeRecords(
579 const std::vector<ContactPtr> &contacts) const
582 std::vector<ContactPtr> merged;
583 std::vector<ContactPtr>::const_iterator it;
584 for (it = contacts.begin(); it != contacts.end(); ++it) {
585 ContactPtr copy = (*it)->clone();
586 /*Merge types of emails with the same email address*/
587 copy->setEmails(mergeEmails(copy->getEmails()));
588 /*Merge types of phonenumbers with the same number*/
589 copy->setPhoneNumbers(mergePhoneNumbers(copy->getPhoneNumbers()));
590 /*Merge types of addresses with the same address info*/
591 copy->setAddresses(mergeAddresses(copy->getAddresses()));
592 merged.push_back(copy);
597 std::vector<ContactEmailPtr> AddressBook::mergeEmails(
598 const std::vector<ContactEmailPtr> &emails) const
601 std::vector<ContactEmailPtr> merged;
602 std::vector<ContactEmailPtr>::const_iterator it;
603 std::vector<ContactEmailPtr>::const_iterator mergedIt;
604 std::vector<ContactEmail::ContactEmailType>::const_iterator typesIt;
606 for (it = emails.begin(); it != emails.end(); ++it) {
608 for (mergedIt = merged.begin(); mergedIt != merged.end(); ++mergedIt) {
609 if ((*mergedIt)->getEmail() == (*it)->getEmail()) {
610 LogDebug("found the same email " << (*mergedIt)->getEmail());
611 //the same emails, check types and add if needed
612 const std::vector<ContactEmail::ContactEmailType> &types =
614 for (typesIt = types.begin(); typesIt != types.end();
616 if (!(*mergedIt)->isTypeOf(*typesIt)) {
617 (*mergedIt)->addType(*typesIt);
618 LogInfo("merged email type " << *typesIt);
626 LogInfo("copy email");
627 merged.push_back((*it)->clone());
633 std::vector<ContactPhoneNumberPtr> AddressBook::mergePhoneNumbers(
634 const std::vector<ContactPhoneNumberPtr> &phoneNumbers) const
637 std::vector<ContactPhoneNumberPtr> merged;
638 std::vector<ContactPhoneNumberPtr>::const_iterator it;
639 std::vector<ContactPhoneNumberPtr>::const_iterator mergedIt;
640 std::vector<ContactPhoneNumber::ContactPhoneNumberType>::const_iterator
643 for (it = phoneNumbers.begin(); it != phoneNumbers.end(); ++it) {
645 for (mergedIt = merged.begin(); mergedIt != merged.end(); ++mergedIt) {
646 if ((*mergedIt)->getNumber() == (*it)->getNumber()) {
648 "found the same phone number " << (*mergedIt)->getNumber());
649 //the same emails, check types and add if needed
650 const std::vector<ContactPhoneNumber::ContactPhoneNumberType> &
651 types = (*it)->getTypes();
652 for (typesIt = types.begin(); typesIt != types.end();
654 if (!(*mergedIt)->isTypeOf(*typesIt)) {
655 LogInfo("merged phone type " << *typesIt);
656 (*mergedIt)->addType(*typesIt);
664 LogInfo("copy phone");
665 merged.push_back((*it)->clone());
671 std::vector<ContactAddressPtr> AddressBook::mergeAddresses(
672 const std::vector<ContactAddressPtr> &addresses) const
675 std::vector<ContactAddressPtr> merged;
676 std::vector<ContactAddressPtr>::const_iterator it;
677 std::vector<ContactAddressPtr>::const_iterator mergedIt;
678 std::vector<ContactAddress::ContactAddressType>::const_iterator typesIt;
680 for (it = addresses.begin(); it != addresses.end(); ++it) {
682 for (mergedIt = merged.begin(); mergedIt != merged.end(); ++mergedIt) {
683 if ((*mergedIt)->compareTo((*it))) {
684 LogDebug("found the same address");
685 //the same emails, check types and add if needed
686 const std::vector<ContactAddress::ContactAddressType> &types =
688 for (typesIt = types.begin(); typesIt != types.end();
690 if (!(*mergedIt)->isTypeOf(*typesIt)) {
691 LogInfo("merged address type " << *typesIt);
692 (*mergedIt)->addType(*typesIt);
700 LogInfo("copy address");
701 merged.push_back((*it)->clone());
707 bool AddressBook::matchToFilter(const ContactFilterPtr &filter,
708 const ContactPtr &contact,
709 bool caseSensitive) const
717 //pass all when no filter
720 unsigned int matchOptions =
721 caseSensitive ? 0 : Commons::VALIDATE_MATCH_CASELESS;
722 if (filter->getFullNameIsSet()) {
723 if (!Commons::validate(filter->getFullNameFilter(),
724 contact->getFullName(),
727 LogDebug("rejected FullName");
731 if (filter->getFirstNameIsSet()) {
732 if (!Commons::validate(filter->getFirstNameFilter(),
733 contact->getFirstName(),
736 LogDebug("rejected First Name");
740 if (filter->getLastNameIsSet()) {
741 if (!Commons::validate(filter->getLastNameFilter(),
742 contact->getLastName(),
745 LogDebug("rejected Last Name");
749 if (filter->getCompanyIsSet()) {
750 if (!Commons::validate(filter->getCompanyFilter(),
751 contact->getCompany(),
754 LogDebug("rejected company");
758 if (filter->getTitleIsSet()) {
759 if (!Commons::validate(filter->getTitleFilter(),
763 LogDebug("rejected title");
767 if (filter->getPhoneticNameIsSet()) {
768 if (!Commons::validate(filter->getPhoneticNameFilter(),
769 contact->getPhoneticName(),
772 LogDebug("rejected PhoneticName");
776 if (!matchToFilterEmail(filter, contact, caseSensitive)) {
779 if (!matchToFilterAddress(filter, contact, caseSensitive)) {
782 if (!matchToFilterPhoneNumber(filter, contact, caseSensitive)) {
785 if (!matchToFilterNickname(filter, contact, caseSensitive)) {
791 bool AddressBook::matchToFilterAddress(const Api::ContactFilterPtr &filter,
792 const Api::ContactPtr &contact,
793 bool caseSensitive) const
795 if (filter->getAddressIsSet()) {
797 unsigned int matchOptions =
798 caseSensitive ? 0 : Commons::VALIDATE_MATCH_CASELESS;
799 LogDebug("filtering by address");
800 const std::vector<ContactAddressPtr> &addressFilter =
801 filter->getAddressFilter();
802 const std::vector<ContactAddressPtr> &addresses = contact->getAddresses();
803 for (std::size_t i = 0; i < addressFilter.size(); ++i) {
805 const std::vector<ContactAddress::ContactAddressType> &
806 addrFilterTypes = addressFilter[i]->getTypes();
807 for (std::size_t j = 0; j < addresses.size(); ++j) {
808 if (!addrFilterTypes.empty()) {
809 if (!addresses[j]->isTypeOf(addressFilter[i]->getTypeFilter()))
811 LogDebug("rejected by Type!");
815 if (addressFilter[i]->getCountryIsSet()) {
816 if (!Commons::validate(addressFilter[i]->getCountry(),
817 addresses[j]->getCountry(),
820 LogDebug("rejected by address");
825 if (addressFilter[i]->getRegionIsSet()) {
826 if (!Commons::validate(addressFilter[i]->getRegion(),
827 addresses[j]->getRegion(),
830 LogDebug("rejected by address");
835 if (addressFilter[i]->getCountyIsSet()) {
836 if (!Commons::validate(addressFilter[i]->getCounty(),
837 addresses[j]->getCounty(),
840 LogDebug("rejected by address");
845 if (addressFilter[i]->getCityIsSet()) {
846 if (!Commons::validate(addressFilter[i]->getCity(),
847 addresses[j]->getCity(),
850 LogDebug("rejected by address");
855 if (addressFilter[i]->getStreetIsSet()) {
856 if (!Commons::validate(addressFilter[i]->getStreet(),
857 addresses[j]->getStreet(),
860 LogDebug("rejected by address");
865 if (addressFilter[i]->getStreetNumberIsSet()) {
866 if (!Commons::validate(addressFilter[i]->getStreetNumber(),
867 addresses[j]->getStreetNumber(),
870 LogDebug("rejected by address");
875 if (addressFilter[i]->getPremisesIsSet()) {
876 if (!Commons::validate(addressFilter[i]->getPremises(),
877 addresses[j]->getPremises(),
880 LogDebug("rejected by address");
885 if (addressFilter[i]->getAdditionalInformationIsSet()) {
886 if (!Commons::validate(
887 addressFilter[i]->getAdditionalInformation(),
888 addresses[j]->getAdditionalInformation(),
891 LogDebug("rejected by address");
896 if (addressFilter[i]->getPostalCodeIsSet()) {
897 if (!Commons::validate(addressFilter[i]->getPostalCode(),
898 addresses[j]->getPostalCode(),
901 LogDebug("rejected by address");
910 LogDebug("rejected by address");
918 bool AddressBook::matchToFilterEmail(const Api::ContactFilterPtr &filter,
919 const Api::ContactPtr &contact,
920 bool caseSensitive) const
922 if (filter->getEmailIsSet()) {
924 unsigned int matchOptions =
925 caseSensitive ? 0 : Commons::VALIDATE_MATCH_CASELESS;
926 LogDebug("filtering by email");
927 const std::vector<ContactEmailPtr> &emailFilter =
928 filter->getEmailFilter();
929 const std::vector<ContactEmailPtr> &emails = contact->getEmails();
930 for (std::size_t i = 0; i < emailFilter.size(); ++i) {
932 const std::vector<ContactEmail::ContactEmailType> &emailFilterTypes
933 = emailFilter[i]->getTypes();
934 for (std::size_t j = 0; j < emails.size(); ++j) {
935 if (!emailFilterTypes.empty()) {
936 if (!emails[j]->isTypeOf(emailFilter[i]->getTypeFilter()))
938 LogDebug("type error!!!");
942 if (!Commons::validate(emailFilter[i]->getEmail(),
943 emails[j]->getEmail(),
946 LogDebug("rejected by email, different email");
953 LogDebug("rejected by email, not found email");
961 bool AddressBook::matchToFilterPhoneNumber(const Api::ContactFilterPtr &filter,
962 const Api::ContactPtr &contact,
963 bool caseSensitive) const
965 if (filter->getPhoneNumberIsSet()) {
967 unsigned int matchOptions =
968 caseSensitive ? 0 : Commons::VALIDATE_MATCH_CASELESS;
969 LogDebug("filtering by phone number");
970 const std::vector<ContactPhoneNumberPtr> &phoneNumberFilter =
971 filter->getPhoneNumberFilter();
972 const std::vector<ContactPhoneNumberPtr> &phoneNumber =
973 contact->getPhoneNumbers();
974 for (std::size_t i = 0; i < phoneNumberFilter.size(); ++i) {
976 const std::vector<ContactPhoneNumber::ContactPhoneNumberType> &
977 phoneNumberFilterTypes = phoneNumberFilter[i]->getTypes();
978 for (std::size_t j = 0; j < phoneNumber.size(); ++j) {
979 if (!phoneNumberFilterTypes.empty()) {
980 if (!phoneNumber[j]->isTypeOf(phoneNumberFilter[i]->
983 LogDebug("rejected by phone type");
987 if (!Commons::validate(phoneNumberFilter[i]->getNumber(),
988 phoneNumber[j]->getNumber(),
991 LogDebug("rejected by phone number");
998 LogDebug("rejected by phone number");
1006 bool AddressBook::matchToFilterNickname(const Api::ContactFilterPtr &filter,
1007 const Api::ContactPtr &contact,
1008 bool caseSensitive) const
1010 if (filter->getNickNameIsSet()) {
1012 unsigned int matchOptions =
1013 caseSensitive ? 0 : Commons::VALIDATE_MATCH_CASELESS;
1014 LogDebug("filtering by nickname");
1015 const std::vector<std::string> &nicknameFilter =
1016 filter->getNickNameFilter();
1017 const std::vector<std::string> &nickname = contact->getNickNames();
1018 for (std::size_t i = 0; i < nicknameFilter.size(); ++i) {
1020 for (std::size_t j = 0; j < nickname.size(); ++j) {
1021 if (!Commons::validate(nicknameFilter[i],
1031 LogDebug("rejected by nickname");
1039 //**************************************
1041 //**************************************
1043 void AddressBook::OnRequestReceived(const Api::EventAddContactPtr &event)
1045 LogDebug("entered");
1048 const ContactPtr &contact = event->getContact();
1050 internalAddContact(contact);
1051 event->setResult(true);
1054 catch (const Commons::PlatformException &ex)
1056 LogError("Error during adding contact.");
1057 LogError(ex.GetMessage());
1061 void AddressBook::OnRequestReceived(const Api::EventUpdateContactPtr &event)
1063 LogDebug("entered");
1066 const ContactPtr &contact = event->getContact();
1068 internalUpdateContact(contact);
1069 event->setResult(true);
1072 catch (const Commons::PlatformException &ex)
1074 LogError("Error during updating contact.");
1075 LogError(ex.GetMessage());
1079 void AddressBook::OnRequestReceived(const Api::EventDeleteContactPtr &event)
1081 LogDebug("entered");
1084 const ContactFilterPtr &filter = event->getFilter();
1085 if (filter && filter->getIdIsSet()) {
1086 internalDeleteContactById(filter->getIdFilter());
1087 event->setResult(true);
1090 catch (const Commons::NotFoundException &ex)
1092 LogError("Contact doesn't exist");
1093 event->setResult(false);
1094 event->setExceptionCode(Commons::ExceptionCodes::NotFoundException);
1096 catch (const Commons::PlatformException &ex)
1098 LogError("Error during deleting contact.");
1099 LogError(ex.GetMessage());
1100 event->setResult(false);
1104 void AddressBook::OnRequestReceived(const Api::EventFindContactsPtr &event)
1106 LogDebug("entered");
1107 ContactFilterPtr filter = event->getFilter();
1110 if (filter && filter->getIdIsSet()) {
1111 LogDebug("searching for contact by id");
1112 ContactPtr contact = internalFindContactById(filter->getIdFilter());
1114 event->addContact(contact);
1116 event->setResult(true);
1117 } else if (filter && filter->getGroupIsSet()) {
1118 LogDebug("searching for contact by group");
1119 const std::vector<ContactPtr> &contacts =
1120 internalFindContactByGroup(filter->getGroupFilter());
1121 event->setContacts(contacts);
1122 event->setResult(true);
1124 LogDebug("searching for contact by filter");
1125 const std::vector<ContactPtr> &contacts =
1126 internalFindContactByFilter(filter,
1127 event->getStartIndex(),
1128 event->getEndIndex(), false);
1129 event->setContacts(contacts);
1130 event->setResult(true);
1132 if (!event->getUseSignleTypes()) {
1133 const std::vector<ContactPtr> &contacts = event->getContacts();
1134 std::vector<ContactPtr> merged = mergeRecords(contacts);
1135 event->setContacts(merged);
1138 catch (const Commons::PlatformException &ex)
1140 LogError("Error during getting contact.");
1141 LogError(ex.GetMessage());
1145 void AddressBook::OnRequestReceived(
1146 const Api::EventGetAddressBookItemCountPtr &event)
1148 LogDebug("entered");
1151 unsigned int count = internalGetNumberOfContacts();
1152 event->setNumberOfAddressBookItems(count);
1153 event->setResult(true);
1155 catch (const Commons::PlatformException &ex)
1157 LogError("Error during getting contact.");
1158 LogError(ex.GetMessage());
1162 void AddressBook::OnRequestReceived(const Api::EventAddGroupPtr &event)
1164 LogDebug("entered");
1167 internalAddGroup(event->getGroupName());
1168 event->setResult(true);
1170 catch (const Commons::PlatformException &ex)
1172 LogError("Error during adding group.");
1173 LogError(ex.GetMessage());
1177 void AddressBook::OnRequestReceived(const Api::EventDeleteGroupPtr &event)
1179 LogDebug("entered");
1182 internalDeleteGroup(event->getGroupName());
1183 event->setResult(true);
1185 catch (const Commons::PlatformException &ex)
1187 LogError("Error during getting contact.");
1188 LogError(ex.GetMessage());
1192 void AddressBook::OnRequestReceived(const Api::EventFindGroupPtr &event)
1194 LogDebug("entered");
1197 std::vector<std::string> groups = internalFindGroup(event->getGroupName());
1198 event->setFoundGroups(groups);
1199 event->setResult(true);
1201 catch (const Commons::PlatformException &ex)
1203 LogError("Error during getting groups.");
1204 LogError(ex.GetMessage());
1208 void AddressBook::OnRequestReceived(const Api::EventExportVCardPtr &event)
1210 LogDebug("entered");
1213 const std::vector<ContactPtr> &contacts = event->getContacts();
1214 LogDebug("number of contacts to export: " << contacts.size());
1215 if (contacts.size() == 0) {
1216 event->setResult(true);
1219 std::vector<std::string> paths = internalExportToVCard(contacts);
1220 if (paths.size() > 0) {
1221 event->setResult(true);
1222 event->setPathToFiles(paths);
1225 catch (const Commons::PlatformException &ex)
1227 LogError("Error during getting groups.");
1228 LogError(ex.GetMessage());