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 ContactObjectP2AConverter.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @brief Converter (contacts_record_h -> ContactPtr)
25 #include "ContactsSvcObjectConverter.h"
29 #include <Commons/Exception.h>
30 #include <Commons/Regex.h>
32 #include "ContactUtility.h"
38 using namespace WrtDeviceApis::Commons;
41 static const char *EMPTY_STRING = "";
43 ContactsSvcObjectConverter::ContactsSvcObjectConverter()
47 ContactsSvcObjectConverter::~ContactsSvcObjectConverter()
51 void ContactsSvcObjectConverter::convertToAbstract(contacts_record_h record, PersonPtr& person)
55 ThrowMsg(InvalidArgumentException, "Platform person object did not set");
60 ThrowMsg(InvalidArgumentException, "Abstract person object did not set");
63 char *charValue = NULL;
65 bool boolValue = false;
70 errorCode = contacts_record_get_int(record, _contacts_person.id, &intValue);
71 if(errorCode != CONTACTS_ERROR_NONE)
72 ThrowMsg(PlatformException, "error on contacts_record_get_int (id) (errorCode:" << errorCode << ")");
74 person->setId(intValue);
77 errorCode = contacts_record_get_str_p(record, _contacts_person.display_name, &charValue);
78 if(errorCode != CONTACTS_ERROR_NONE)
79 ThrowMsg(PlatformException, "error on contacts_record_get_str_p (display_name) (errorCode:" << errorCode << ")");
82 person->setDisplayName(charValue);
85 if(person->getDisplayNameIsSet())
86 person->unsetDisplayName();
90 errorCode = contacts_record_get_int(record, _contacts_person.link_count, &intValue);
91 if(errorCode != CONTACTS_ERROR_NONE)
92 ThrowMsg(PlatformException, "error on contacts_record_get_int (link_count) (errorCode:" << errorCode << ")");
94 person->setContactCount(intValue);
97 errorCode = contacts_record_get_bool(record, _contacts_person.has_phonenumber, &boolValue);
98 if(errorCode != CONTACTS_ERROR_NONE)
99 ThrowMsg(PlatformException, "error on contacts_record_get_bool (has_phonenumber) (errorCode:" << errorCode << ")");
101 person->setHasPhoneNumber(boolValue);
104 errorCode = contacts_record_get_bool(record, _contacts_person.has_email, &boolValue);
105 if(errorCode != CONTACTS_ERROR_NONE)
106 ThrowMsg(PlatformException, "error on contacts_record_get_bool (has_email) (errorCode:" << errorCode << ")");
108 person->setHasEmail(boolValue);
111 errorCode = contacts_record_get_bool(record, _contacts_person.is_favorite, &boolValue);
112 if(errorCode != CONTACTS_ERROR_NONE)
113 ThrowMsg(PlatformException, "error on contacts_record_get_bool (is_favorite) (errorCode:" << errorCode << ")");
115 person->setIsFavorite(boolValue);
118 errorCode = contacts_record_get_str_p(record, _contacts_person.image_thumbnail_path, &charValue);
119 if(errorCode != CONTACTS_ERROR_NONE)
120 ThrowMsg(PlatformException, "error on contacts_record_get_str_p (image_thumbnail_path) (errorCode:" << errorCode << ")");
123 person->setPhotoURI(ContactUtility::convertPathToUri(charValue));
126 if(person->getPhotoURIIsSet())
127 person->unsetPhotoURI();
131 errorCode = contacts_record_get_str_p(record, _contacts_person.ringtone_path, &charValue);
132 if(errorCode != CONTACTS_ERROR_NONE)
133 ThrowMsg(PlatformException, "error on contacts_record_get_str_p (ringtone_path) (errorCode:" << errorCode << ")");
136 person->setRingtoneURI(ContactUtility::convertPathToUri(charValue));
139 if(person->getRingtoneURIIsSet())
140 person->unsetRingtoneURI();
144 errorCode = contacts_record_get_int(record, _contacts_person.display_contact_id, &intValue);
145 if(errorCode != CONTACTS_ERROR_NONE)
146 ThrowMsg(PlatformException, "error on contacts_record_get_int (display_contact_id) (errorCode:" << errorCode << ")");
148 person->setDisplayContactId(intValue);
151 void ContactsSvcObjectConverter::convertToPlatform(PersonPtr& person, contacts_record_h record)
155 ThrowMsg(InvalidArgumentException, "Platform person object did not set");
160 ThrowMsg(InvalidArgumentException, "Abstract person object did not set");
166 bool boolValue = person->getIsFavorite();
167 errorCode = contacts_record_set_bool(record, _contacts_person.is_favorite, boolValue);
168 if(errorCode != CONTACTS_ERROR_NONE)
170 ThrowMsg(PlatformException, "importing isFavorite E (err:" <<
171 errorCode << ", value:" << boolValue << ")");
174 const char *newValueStr = NULL;
175 char *oldValueStr = NULL;
176 string abstractValueStr;
180 contacts_record_get_str_p(record, _contacts_person.image_thumbnail_path, &oldValueStr);
181 realPath = ContactUtility::convertUriToPath(person->getPhotoURI());
182 person->setPhotoURIRealPath(realPath);
183 abstractValueStr = person->getPhotoURIRealPath();
185 if(oldValueStr != NULL)
187 if(!person->getPhotoURIIsSet() || person->getPhotoURI().empty())
189 newValueStr = EMPTY_STRING;
190 ThrowMsg(PlatformException, "Set empty image is not allowed");
191 }else if(g_strcmp0(oldValueStr, abstractValueStr.c_str()) != 0)
192 newValueStr = abstractValueStr.c_str();
193 }else //if(oldValueStr == NULL)
195 if(person->getPhotoURI().empty()){
198 newValueStr = abstractValueStr.c_str();
202 if(newValueStr != NULL)
204 LoggerD("update photoURI : " << newValueStr);
205 int personId = ContactUtility::strToInt(person->getId());
207 contacts_filter_h filter = NULL;
208 contacts_query_h query = NULL;
209 contacts_list_h list = NULL;
210 bool is_success = false;
213 errorCode = contacts_query_create(_contacts_simple_contact._uri, &query);
214 if(errorCode != CONTACTS_ERROR_NONE)
215 ThrowMsg(PlatformException, "contacts_query_create error : " << errorCode << " (" << __FUNCTION__ << ")");
217 errorCode = contacts_filter_create( _contacts_simple_contact._uri, &filter );
218 if(errorCode != CONTACTS_ERROR_NONE)
219 ThrowMsg(PlatformException, "contacts_query_create error : " << errorCode << " (" << __FUNCTION__ << ")");
221 errorCode = contacts_filter_add_int(filter, _contacts_simple_contact.person_id, CONTACTS_MATCH_EQUAL, personId);
222 if(errorCode != CONTACTS_ERROR_NONE)
223 ThrowMsg(PlatformException, "contacts_query_create error : " << errorCode << " (" << __FUNCTION__ << ")");
225 errorCode = contacts_query_set_filter(query, filter);
226 if(errorCode != CONTACTS_ERROR_NONE)
227 ThrowMsg(PlatformException, "contacts_query_set_filter error : " << errorCode << " (" << __FUNCTION__ << ")");
229 errorCode = contacts_db_get_records_with_query(query, 0, 0, &list);
230 if(errorCode != CONTACTS_ERROR_NONE)
231 ThrowMsg(PlatformException, "contacts_db_get_count_with_query error : " << errorCode << " (" << __FUNCTION__ << ")");
233 unsigned int record_count = 0;
234 errorCode = contacts_list_get_count(list, &record_count);
235 if(errorCode != CONTACTS_ERROR_NONE)
236 ThrowMsg(PlatformException, "Fail to get contacts_list_get_count : " << errorCode << " (" << __FUNCTION__ << ")");
240 contacts_list_first(list);
241 for(unsigned int i=0; i<record_count; i++)
243 contacts_record_h record;
245 char *charValue = NULL;
247 errorCode = contacts_list_get_current_record_p(list, &record);
248 if(errorCode != CONTACTS_ERROR_NONE || record == NULL)
250 LoggerW("contacts_list_get_current_record_p error : " << errorCode << " (" << __FUNCTION__ << ")");
254 contacts_record_get_int(record, _contacts_simple_contact.id, &id);
255 contacts_record_get_str_p(record, _contacts_simple_contact.image_thumbnail_path, &charValue);
256 LoggerD("id : " << id);
257 LoggerD("charValue : " << charValue);
259 if((charValue == NULL) && g_strcmp0(newValueStr, EMPTY_STRING) == 0){
265 if(g_strcmp0(charValue, newValueStr) == 0)
271 contacts_list_next(list);
275 contacts_filter_destroy(filter);
277 contacts_query_destroy(query);
279 contacts_list_destroy(list, true);
281 ThrowMsg(PlatformException, "invalid photoURI path : " << errorCode << " (" << __FUNCTION__ << ")");
287 LoggerD("targetId : " << targetId);
289 errorCode = contacts_query_create(_contacts_image._uri, &query);
290 if(errorCode != CONTACTS_ERROR_NONE)
291 ThrowMsg(PlatformException, "contacts_query_create error : " << errorCode << " (" << __FUNCTION__ << ")");
293 errorCode = contacts_filter_create( _contacts_image._uri, &filter );
294 if(errorCode != CONTACTS_ERROR_NONE)
295 ThrowMsg(PlatformException, "contacts_query_create error : " << errorCode << " (" << __FUNCTION__ << ")");
297 errorCode = contacts_filter_add_int(filter, _contacts_image.contact_id, CONTACTS_MATCH_EQUAL, targetId);
298 if(errorCode != CONTACTS_ERROR_NONE)
299 ThrowMsg(PlatformException, "contacts_filter_add_operator error : " << errorCode << " (" << __FUNCTION__ << ")");
301 errorCode = contacts_query_set_filter(query, filter);
302 if(errorCode != CONTACTS_ERROR_NONE)
303 ThrowMsg(PlatformException, "contacts_query_set_filter error : " << errorCode << " (" << __FUNCTION__ << ")");
305 errorCode = contacts_db_get_records_with_query(query, 0, 0, &list);
306 if(errorCode != CONTACTS_ERROR_NONE)
307 ThrowMsg(PlatformException, "contacts_db_get_count_with_query error : " << errorCode << " (" << __FUNCTION__ << ")");
310 errorCode = contacts_list_get_count(list, &record_count);
311 if(errorCode != CONTACTS_ERROR_NONE)
312 ThrowMsg(PlatformException, "Fail to get contacts_list_get_count : " << errorCode << " (" << __FUNCTION__ << ")");
314 contacts_list_first(list);
315 for(unsigned int i=0; i<record_count; i++)
317 contacts_record_h imageRecord;
318 errorCode = contacts_list_get_current_record_p(list, &imageRecord);
319 if(errorCode != CONTACTS_ERROR_NONE || imageRecord == NULL)
321 LoggerW("contacts_list_get_current_record_p error : " << errorCode << " (" << __FUNCTION__ << ")");
325 errorCode = contacts_record_get_int(imageRecord, _contacts_image.id, &imageId);
326 if(errorCode != CONTACTS_ERROR_NONE)
328 LoggerW("contacts_record_get_int error : " << errorCode << " (" << __FUNCTION__ << ")");
332 errorCode = contacts_list_next(list);
333 if(errorCode != CONTACTS_ERROR_NONE)
335 LoggerW("contacts_list_next error : " << errorCode << " (" << __FUNCTION__ << ")");
341 contacts_filter_destroy(filter);
343 contacts_query_destroy(query);
345 contacts_list_destroy(list, true);
349 errorCode = contacts_person_set_default_property(CONTACTS_PERSON_PROPERTY_IMAGE, personId, imageId);
350 if(errorCode != CONTACTS_ERROR_NONE)
352 ThrowMsg(PlatformException, "importing photoURI E (err:" <<
353 errorCode << ", str:" << abstractValueStr << ")");
361 contacts_record_get_str_p(record, _contacts_person.ringtone_path, &oldValueStr);
362 realPath = ContactUtility::convertUriToPath(person->getRingtoneURI());
363 person->setRingtoneURIRealPath(realPath);
364 abstractValueStr = person->getRingtoneURIRealPath();
365 if(oldValueStr != NULL &&
366 (!person->getRingtoneURIIsSet() || person->getRingtoneURI().empty()))
368 newValueStr = EMPTY_STRING;
370 else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
372 newValueStr = abstractValueStr.c_str();
375 if(newValueStr != NULL)
377 errorCode = contacts_record_set_str(record, _contacts_person.ringtone_path, newValueStr);
378 if(errorCode != CONTACTS_ERROR_NONE)
380 ThrowMsg(PlatformException, "importing ringtoneURI E (err:" <<
381 errorCode << ", str:" << abstractValueStr << ")");
386 // int intValue = ContactUtility::strToInt(person->getDisplayContactId());
387 // errorCode = contacts_record_set_int(record, _contacts_person.display_contact_id, intValue);
388 // if(errorCode != CONTACTS_ERROR_NONE)
390 // ThrowMsg(PlatformException, "importing displayContactId E (err:" <<
391 // errorCode << ", value:" << intValue << ")");
392 // } // FIXME platform still has problem on this field.
395 void ContactsSvcObjectConverter::convertToAbstract(contacts_record_h record, ContactPtr& contact)
400 void ContactsSvcObjectConverter::convertToPlatform(ContactPtr& contact, contacts_record_h record)
405 void ContactsSvcObjectConverter::convertToAbstract(contacts_record_h record, ContactGroupPtr& group)
409 ThrowMsg(InvalidArgumentException, "Platform group object did not set");
414 ThrowMsg(InvalidArgumentException, "Abstract group object did not set");
417 char *charValue = NULL;
419 bool boolValue = false;
424 errorCode = contacts_record_get_int(record, _contacts_group.id, &intValue);
425 if(errorCode != CONTACTS_ERROR_NONE)
426 ThrowMsg(PlatformException, "error on contacts_record_get_int (id) (errorCode:" << errorCode << ")");
428 group->setId(intValue);
431 errorCode = contacts_record_get_int(record, _contacts_group.address_book_id, &intValue);
432 if(errorCode != CONTACTS_ERROR_NONE)
433 ThrowMsg(PlatformException, "error on contacts_record_get_int (address_book_id) (errorCode:" << errorCode << ")");
435 group->setAddressBookId(intValue);
439 errorCode = contacts_record_get_str_p(record, _contacts_group.name, &charValue);
440 if(errorCode != CONTACTS_ERROR_NONE)
441 ThrowMsg(PlatformException, "error on contacts_record_get_str_p (name) (errorCode:" << errorCode << ")");
444 group->setName(charValue);
447 if(group->getNameIsSet())
452 errorCode = contacts_record_get_str_p(record, _contacts_group.image_path, &charValue);
453 if(errorCode != CONTACTS_ERROR_NONE)
454 ThrowMsg(PlatformException, "error on contacts_record_get_str_p (image_path) (errorCode:" << errorCode << ")");
457 group->setPhotoURI(ContactUtility::convertPathToUri(charValue));
460 if(group->getPhotoURIIsSet())
461 group->unsetPhotoURI();
465 errorCode = contacts_record_get_str_p(record, _contacts_group.ringtone_path, &charValue);
466 if(errorCode != CONTACTS_ERROR_NONE)
467 ThrowMsg(PlatformException, "error on contacts_record_get_str_p (ringtone_path) (errorCode:" << errorCode << ")");
470 group->setRingtoneURI(ContactUtility::convertPathToUri(charValue));
473 if(group->getRingtoneURIIsSet())
474 group->unsetRingtoneURI();
478 errorCode = contacts_record_get_bool(record, _contacts_group.is_read_only, &boolValue);
479 if(errorCode != CONTACTS_ERROR_NONE)
480 ThrowMsg(PlatformException, "error on contacts_record_get_bool (is_read_only) (errorCode:" << errorCode << ")");
482 group->setReadOnly(boolValue);
485 void ContactsSvcObjectConverter::convertToPlatform(ContactGroupPtr& group, contacts_record_h record)
489 ThrowMsg(InvalidArgumentException, "Platform group object did not set");
494 ThrowMsg(InvalidArgumentException, "Abstract group object did not set");
500 // //int addressBookId = 0; // TODO Check what happen if address_book_id is not set
501 // //contacts_record_get_int(record, _contacts_group.address_book_id, &addressBookId);
503 // int intValue = group->getAddressBookId();
504 // errorCode = contacts_record_set_int(record, _contacts_group.address_book_id, intValue);
505 // if(errorCode != CONTACTS_ERROR_NONE)
507 // ThrowMsg(PlatformException, "importing displayContactId E (err:" <<
508 // errorCode << ", value:" << intValue << ")");
511 const char *newValueStr = NULL;
512 char *oldValueStr = NULL;
513 string abstractValueStr;
517 contacts_record_get_str_p(record, _contacts_group.name, &oldValueStr);
518 abstractValueStr = group->getName();
519 if(oldValueStr != NULL &&
520 (!group->getNameIsSet() || group->getName().empty()))
522 newValueStr = EMPTY_STRING;
524 else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
526 newValueStr = abstractValueStr.c_str();
529 if(newValueStr != NULL)
531 errorCode = contacts_record_set_str(record, _contacts_group.name, newValueStr);
532 if(errorCode != CONTACTS_ERROR_NONE)
534 ThrowMsg(PlatformException, "importing name E (err:" <<
535 errorCode << ", str:" << abstractValueStr << ")");
542 contacts_record_get_str_p(record, _contacts_group.image_path, &oldValueStr);
543 realPath = ContactUtility::convertUriToPath(group->getPhotoURI());
544 group->setPhotoURIRealPath(realPath);
545 abstractValueStr = group->getPhotoURIRealPath();
546 if(oldValueStr != NULL &&
547 (!group->getPhotoURIIsSet() || group->getPhotoURI().empty()))
549 newValueStr = EMPTY_STRING;
551 else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
553 newValueStr = abstractValueStr.c_str();
556 if(newValueStr != NULL)
558 errorCode = contacts_record_set_str(record, _contacts_group.image_path, newValueStr);
559 if(errorCode != CONTACTS_ERROR_NONE)
561 ThrowMsg(PlatformException, "importing photoURI E (err:" <<
562 errorCode << ", str:" << abstractValueStr << ")");
569 contacts_record_get_str_p(record, _contacts_group.ringtone_path, &oldValueStr);
570 realPath = ContactUtility::convertUriToPath(group->getRingtoneURI());
571 group->setRingtoneURIRealPath(realPath);
572 abstractValueStr = group->getRingtoneURIRealPath();
573 if(oldValueStr != NULL &&
574 (!group->getRingtoneURIIsSet() || group->getRingtoneURI().empty()))
576 newValueStr = EMPTY_STRING;
578 else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
580 newValueStr = abstractValueStr.c_str();
583 if(newValueStr != NULL)
585 errorCode = contacts_record_set_str(record, _contacts_group.ringtone_path, newValueStr);
586 if(errorCode != CONTACTS_ERROR_NONE)
588 ThrowMsg(PlatformException, "importing ringtoneURI E (err:" <<
589 errorCode << ", str:" << abstractValueStr << ")");