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:" << errorCode);
173 const char *newValueStr = NULL;
174 char *oldValueStr = NULL;
175 string abstractValueStr;
179 contacts_record_get_str_p(record, _contacts_person.image_thumbnail_path, &oldValueStr);
180 realPath = ContactUtility::convertUriToPath(person->getPhotoURI());
181 person->setPhotoURIRealPath(realPath);
182 abstractValueStr = person->getPhotoURIRealPath();
184 if(oldValueStr != NULL)
186 if(!person->getPhotoURIIsSet() || person->getPhotoURI().empty())
188 newValueStr = EMPTY_STRING;
189 ThrowMsg(PlatformException, "Set empty image is not allowed");
190 }else if(g_strcmp0(oldValueStr, abstractValueStr.c_str()) != 0)
191 newValueStr = abstractValueStr.c_str();
192 }else //if(oldValueStr == NULL)
194 if(person->getPhotoURI().empty()){
197 newValueStr = abstractValueStr.c_str();
201 if(newValueStr != NULL)
203 int personId = ContactUtility::strToInt(person->getId());
205 contacts_filter_h filter = NULL;
206 contacts_query_h query = NULL;
207 contacts_list_h list = NULL;
208 bool is_success = false;
211 errorCode = contacts_query_create(_contacts_simple_contact._uri, &query);
212 if(errorCode != CONTACTS_ERROR_NONE)
213 ThrowMsg(PlatformException, "contacts_query_create error : " << errorCode << " (" << __FUNCTION__ << ")");
215 errorCode = contacts_filter_create( _contacts_simple_contact._uri, &filter );
216 if(errorCode != CONTACTS_ERROR_NONE)
217 ThrowMsg(PlatformException, "contacts_query_create error : " << errorCode << " (" << __FUNCTION__ << ")");
219 errorCode = contacts_filter_add_int(filter, _contacts_simple_contact.person_id, CONTACTS_MATCH_EQUAL, personId);
220 if(errorCode != CONTACTS_ERROR_NONE)
221 ThrowMsg(PlatformException, "contacts_query_create error : " << errorCode << " (" << __FUNCTION__ << ")");
223 errorCode = contacts_query_set_filter(query, filter);
224 if(errorCode != CONTACTS_ERROR_NONE)
225 ThrowMsg(PlatformException, "contacts_query_set_filter error : " << errorCode << " (" << __FUNCTION__ << ")");
227 errorCode = contacts_db_get_records_with_query(query, 0, 0, &list);
228 if(errorCode != CONTACTS_ERROR_NONE)
229 ThrowMsg(PlatformException, "contacts_db_get_count_with_query error : " << errorCode << " (" << __FUNCTION__ << ")");
231 unsigned int record_count = 0;
232 errorCode = contacts_list_get_count(list, &record_count);
233 if(errorCode != CONTACTS_ERROR_NONE)
234 ThrowMsg(PlatformException, "Fail to get contacts_list_get_count : " << errorCode << " (" << __FUNCTION__ << ")");
238 contacts_list_first(list);
239 for(unsigned int i=0; i<record_count; i++)
241 contacts_record_h record;
243 char *charValue = NULL;
245 errorCode = contacts_list_get_current_record_p(list, &record);
246 if(errorCode != CONTACTS_ERROR_NONE || record == NULL)
248 LoggerW("contacts_list_get_current_record_p error : " << errorCode << " (" << __FUNCTION__ << ")");
252 contacts_record_get_int(record, _contacts_simple_contact.id, &id);
253 contacts_record_get_str_p(record, _contacts_simple_contact.image_thumbnail_path, &charValue);
255 if((charValue == NULL) && g_strcmp0(newValueStr, EMPTY_STRING) == 0){
261 if(g_strcmp0(charValue, newValueStr) == 0)
267 contacts_list_next(list);
271 contacts_filter_destroy(filter);
273 contacts_query_destroy(query);
275 contacts_list_destroy(list, true);
277 ThrowMsg(PlatformException, "invalid photoURI path : " << errorCode << " (" << __FUNCTION__ << ")");
283 errorCode = contacts_query_create(_contacts_image._uri, &query);
284 if(errorCode != CONTACTS_ERROR_NONE)
285 ThrowMsg(PlatformException, "contacts_query_create error : " << errorCode << " (" << __FUNCTION__ << ")");
287 errorCode = contacts_filter_create( _contacts_image._uri, &filter );
288 if(errorCode != CONTACTS_ERROR_NONE)
289 ThrowMsg(PlatformException, "contacts_query_create error : " << errorCode << " (" << __FUNCTION__ << ")");
291 errorCode = contacts_filter_add_int(filter, _contacts_image.contact_id, CONTACTS_MATCH_EQUAL, targetId);
292 if(errorCode != CONTACTS_ERROR_NONE)
293 ThrowMsg(PlatformException, "contacts_filter_add_operator error : " << errorCode << " (" << __FUNCTION__ << ")");
295 errorCode = contacts_query_set_filter(query, filter);
296 if(errorCode != CONTACTS_ERROR_NONE)
297 ThrowMsg(PlatformException, "contacts_query_set_filter error : " << errorCode << " (" << __FUNCTION__ << ")");
299 errorCode = contacts_db_get_records_with_query(query, 0, 0, &list);
300 if(errorCode != CONTACTS_ERROR_NONE)
301 ThrowMsg(PlatformException, "contacts_db_get_count_with_query error : " << errorCode << " (" << __FUNCTION__ << ")");
304 errorCode = contacts_list_get_count(list, &record_count);
305 if(errorCode != CONTACTS_ERROR_NONE)
306 ThrowMsg(PlatformException, "Fail to get contacts_list_get_count : " << errorCode << " (" << __FUNCTION__ << ")");
308 contacts_list_first(list);
309 for(unsigned int i=0; i<record_count; i++)
311 contacts_record_h imageRecord;
312 errorCode = contacts_list_get_current_record_p(list, &imageRecord);
313 if(errorCode != CONTACTS_ERROR_NONE || imageRecord == NULL)
315 LoggerW("contacts_list_get_current_record_p error : " << errorCode << " (" << __FUNCTION__ << ")");
319 errorCode = contacts_record_get_int(imageRecord, _contacts_image.id, &imageId);
320 if(errorCode != CONTACTS_ERROR_NONE)
322 LoggerW("contacts_record_get_int error : " << errorCode << " (" << __FUNCTION__ << ")");
326 errorCode = contacts_list_next(list);
327 if(errorCode != CONTACTS_ERROR_NONE)
329 LoggerW("contacts_list_next error : " << errorCode << " (" << __FUNCTION__ << ")");
335 contacts_filter_destroy(filter);
337 contacts_query_destroy(query);
339 contacts_list_destroy(list, true);
343 errorCode = contacts_person_set_default_property(CONTACTS_PERSON_PROPERTY_IMAGE, personId, imageId);
344 if(errorCode != CONTACTS_ERROR_NONE)
346 ThrowMsg(PlatformException, "importing photoURI E (err:" << errorCode);
354 contacts_record_get_str_p(record, _contacts_person.ringtone_path, &oldValueStr);
355 realPath = ContactUtility::convertUriToPath(person->getRingtoneURI());
356 person->setRingtoneURIRealPath(realPath);
357 abstractValueStr = person->getRingtoneURIRealPath();
358 if(oldValueStr != NULL &&
359 (!person->getRingtoneURIIsSet() || person->getRingtoneURI().empty()))
361 newValueStr = EMPTY_STRING;
363 else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
365 newValueStr = abstractValueStr.c_str();
368 if(newValueStr != NULL)
370 errorCode = contacts_record_set_str(record, _contacts_person.ringtone_path, newValueStr);
371 if(errorCode != CONTACTS_ERROR_NONE)
373 ThrowMsg(PlatformException, "importing ringtoneURI E (err:" << errorCode);
378 // int intValue = ContactUtility::strToInt(person->getDisplayContactId());
379 // errorCode = contacts_record_set_int(record, _contacts_person.display_contact_id, intValue);
380 // if(errorCode != CONTACTS_ERROR_NONE)
382 // ThrowMsg(PlatformException, "importing displayContactId E (err:" <<
383 // errorCode << ", value:" << intValue << ")");
384 // } // FIXME platform still has problem on this field.
387 void ContactsSvcObjectConverter::convertToAbstract(contacts_record_h record, ContactPtr& contact)
392 void ContactsSvcObjectConverter::convertToPlatform(ContactPtr& contact, contacts_record_h record)
397 void ContactsSvcObjectConverter::convertToAbstract(contacts_record_h record, ContactGroupPtr& group)
401 ThrowMsg(InvalidArgumentException, "Platform group object did not set");
406 ThrowMsg(InvalidArgumentException, "Abstract group object did not set");
409 char *charValue = NULL;
411 bool boolValue = false;
416 errorCode = contacts_record_get_int(record, _contacts_group.id, &intValue);
417 if(errorCode != CONTACTS_ERROR_NONE)
418 ThrowMsg(PlatformException, "error on contacts_record_get_int (id) (errorCode:" << errorCode << ")");
420 group->setId(intValue);
423 errorCode = contacts_record_get_int(record, _contacts_group.address_book_id, &intValue);
424 if(errorCode != CONTACTS_ERROR_NONE)
425 ThrowMsg(PlatformException, "error on contacts_record_get_int (address_book_id) (errorCode:" << errorCode << ")");
427 group->setAddressBookId(intValue);
431 errorCode = contacts_record_get_str_p(record, _contacts_group.name, &charValue);
432 if(errorCode != CONTACTS_ERROR_NONE)
433 ThrowMsg(PlatformException, "error on contacts_record_get_str_p (name) (errorCode:" << errorCode << ")");
436 group->setName(charValue);
439 if(group->getNameIsSet())
444 errorCode = contacts_record_get_str_p(record, _contacts_group.image_path, &charValue);
445 if(errorCode != CONTACTS_ERROR_NONE)
446 ThrowMsg(PlatformException, "error on contacts_record_get_str_p (image_path) (errorCode:" << errorCode << ")");
449 group->setPhotoURI(ContactUtility::convertPathToUri(charValue));
452 if(group->getPhotoURIIsSet())
453 group->unsetPhotoURI();
457 errorCode = contacts_record_get_str_p(record, _contacts_group.ringtone_path, &charValue);
458 if(errorCode != CONTACTS_ERROR_NONE)
459 ThrowMsg(PlatformException, "error on contacts_record_get_str_p (ringtone_path) (errorCode:" << errorCode << ")");
462 group->setRingtoneURI(ContactUtility::convertPathToUri(charValue));
465 if(group->getRingtoneURIIsSet())
466 group->unsetRingtoneURI();
470 errorCode = contacts_record_get_bool(record, _contacts_group.is_read_only, &boolValue);
471 if(errorCode != CONTACTS_ERROR_NONE)
472 ThrowMsg(PlatformException, "error on contacts_record_get_bool (is_read_only) (errorCode:" << errorCode << ")");
474 group->setReadOnly(boolValue);
477 void ContactsSvcObjectConverter::convertToPlatform(ContactGroupPtr& group, contacts_record_h record)
481 ThrowMsg(InvalidArgumentException, "Platform group object did not set");
486 ThrowMsg(InvalidArgumentException, "Abstract group object did not set");
492 // //int addressBookId = 0; // TODO Check what happen if address_book_id is not set
493 // //contacts_record_get_int(record, _contacts_group.address_book_id, &addressBookId);
495 // int intValue = group->getAddressBookId();
496 // errorCode = contacts_record_set_int(record, _contacts_group.address_book_id, intValue);
497 // if(errorCode != CONTACTS_ERROR_NONE)
499 // ThrowMsg(PlatformException, "importing displayContactId E (err:" <<
500 // errorCode << ", value:" << intValue << ")");
503 const char *newValueStr = NULL;
504 char *oldValueStr = NULL;
505 string abstractValueStr;
509 contacts_record_get_str_p(record, _contacts_group.name, &oldValueStr);
510 abstractValueStr = group->getName();
511 if(oldValueStr != NULL &&
512 (!group->getNameIsSet() || group->getName().empty()))
514 newValueStr = EMPTY_STRING;
516 else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
518 newValueStr = abstractValueStr.c_str();
521 if(newValueStr != NULL)
523 errorCode = contacts_record_set_str(record, _contacts_group.name, newValueStr);
524 if(errorCode != CONTACTS_ERROR_NONE)
526 ThrowMsg(PlatformException, "importing name E (err:" << errorCode);
533 contacts_record_get_str_p(record, _contacts_group.image_path, &oldValueStr);
534 realPath = ContactUtility::convertUriToPath(group->getPhotoURI());
535 group->setPhotoURIRealPath(realPath);
536 abstractValueStr = group->getPhotoURIRealPath();
537 if(oldValueStr != NULL &&
538 (!group->getPhotoURIIsSet() || group->getPhotoURI().empty()))
540 newValueStr = EMPTY_STRING;
542 else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
544 newValueStr = abstractValueStr.c_str();
547 if(newValueStr != NULL)
549 errorCode = contacts_record_set_str(record, _contacts_group.image_path, newValueStr);
550 if(errorCode != CONTACTS_ERROR_NONE)
552 ThrowMsg(PlatformException, "importing photoURI E (err:" << errorCode);
559 contacts_record_get_str_p(record, _contacts_group.ringtone_path, &oldValueStr);
560 realPath = ContactUtility::convertUriToPath(group->getRingtoneURI());
561 group->setRingtoneURIRealPath(realPath);
562 abstractValueStr = group->getRingtoneURIRealPath();
563 if(oldValueStr != NULL &&
564 (!group->getRingtoneURIIsSet() || group->getRingtoneURI().empty()))
566 newValueStr = EMPTY_STRING;
568 else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
570 newValueStr = abstractValueStr.c_str();
573 if(newValueStr != NULL)
575 errorCode = contacts_record_set_str(record, _contacts_group.ringtone_path, newValueStr);
576 if(errorCode != CONTACTS_ERROR_NONE)
578 ThrowMsg(PlatformException, "importing ringtoneURI E (err:" << errorCode);