wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Contact / ContactsSvcObjectConverter.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /*
19  * @file        ContactObjectP2AConverter.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief       Converter (contacts_record_h -> ContactPtr)
23  */
24
25 #include "ContactsSvcObjectConverter.h"
26
27 #include <vector>
28 #include <pcrecpp.h>
29 #include <Commons/Exception.h>
30 #include <Commons/Regex.h>
31 #include "Contact.h"
32 #include "ContactUtility.h"
33 #include <Logger.h>
34
35 namespace DeviceAPI {
36 namespace Contact {
37
38 using namespace WrtDeviceApis::Commons;
39 using namespace std;
40
41 static const char *EMPTY_STRING = "";
42
43 ContactsSvcObjectConverter::ContactsSvcObjectConverter()
44 {
45 }
46
47 ContactsSvcObjectConverter::~ContactsSvcObjectConverter()
48 {
49 }
50
51 void ContactsSvcObjectConverter::convertToAbstract(contacts_record_h record, PersonPtr& person)
52 {
53         if(record == NULL)
54         {
55                 ThrowMsg(InvalidArgumentException, "Platform person object did not set");
56         }
57
58         if(person == NULL)
59         {
60                 ThrowMsg(InvalidArgumentException, "Abstract person object did not set");
61         }
62
63         char *charValue = NULL;
64         int intValue = 0;
65         bool boolValue = false;
66
67         int errorCode = 0;
68
69         // id
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 << ")");
73
74         person->setId(intValue);
75
76         // displayName
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 << ")");
80
81         if (charValue)
82                 person->setDisplayName(charValue);
83         else
84         {
85                 if(person->getDisplayNameIsSet())
86                         person->unsetDisplayName();
87         }
88
89         // contactCount
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 << ")");
93
94         person->setContactCount(intValue);
95
96         // hasPhoneNumber
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 << ")");
100
101         person->setHasPhoneNumber(boolValue);
102
103         // hasEmail
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 << ")");
107
108         person->setHasEmail(boolValue);
109
110         // isFavorite
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 << ")");
114
115         person->setIsFavorite(boolValue);
116
117         // photoURI
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 << ")");
121
122         if (charValue)
123                 person->setPhotoURI(ContactUtility::convertPathToUri(charValue));
124         else
125         {
126                 if(person->getPhotoURIIsSet())
127                         person->unsetPhotoURI();
128         }
129
130         // ringtoneURI
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 << ")");
134
135         if (charValue)
136                 person->setRingtoneURI(ContactUtility::convertPathToUri(charValue));
137         else
138         {
139                 if(person->getRingtoneURIIsSet())
140                         person->unsetRingtoneURI();
141         }
142
143         // displayContactId
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 << ")");
147
148         person->setDisplayContactId(intValue);
149 }
150
151 void ContactsSvcObjectConverter::convertToPlatform(PersonPtr& person, contacts_record_h record)
152 {
153         if(record == NULL)
154         {
155                 ThrowMsg(InvalidArgumentException, "Platform person object did not set");
156         }
157
158         if(person == NULL)
159         {
160                 ThrowMsg(InvalidArgumentException, "Abstract person object did not set");
161         }
162
163         int errorCode = 0;
164
165         // isFavorite
166         bool boolValue = person->getIsFavorite();
167         errorCode = contacts_record_set_bool(record, _contacts_person.is_favorite, boolValue);
168         if(errorCode != CONTACTS_ERROR_NONE)
169         {
170                 ThrowMsg(PlatformException, "importing isFavorite E (err:" <<
171                                 errorCode << ", value:" << boolValue << ")");
172         }
173
174         const char *newValueStr = NULL;
175         char *oldValueStr = NULL;
176         string abstractValueStr;
177         string realPath;
178
179         // photoURI
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();
184
185         if(oldValueStr != NULL)
186         {
187                 if(!person->getPhotoURIIsSet() || person->getPhotoURI().empty())
188                 {
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)
194         {
195                 if(person->getPhotoURI().empty()){
196                         newValueStr = NULL;
197                 }else{
198                         newValueStr = abstractValueStr.c_str();
199                 }
200         }
201
202         if(newValueStr != NULL)
203         {
204                 LoggerD("update photoURI : " << newValueStr);
205                 int personId = ContactUtility::strToInt(person->getId());
206
207                 contacts_filter_h filter = NULL;
208                 contacts_query_h query = NULL;
209                 contacts_list_h list = NULL;
210                 bool is_success = false;
211                 int imageId = 0;
212
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__ << ")");
216
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__ << ")");
220
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__ << ")");
224
225                 errorCode = contacts_query_set_filter(query, filter);
226                 if(errorCode != CONTACTS_ERROR_NONE)
227                         ThrowMsg(PlatformException, "contacts_query_set_filter error : " << errorCode << " (" << __FUNCTION__ << ")");
228
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__ << ")");
232
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__ << ")");
237
238                 int targetId = 0;
239
240                 contacts_list_first(list);
241                 for(unsigned int i=0; i<record_count; i++)
242                 {
243                         contacts_record_h record;
244                         int id = 0;
245                         char *charValue = NULL;
246
247                         errorCode = contacts_list_get_current_record_p(list, &record);
248                         if(errorCode != CONTACTS_ERROR_NONE || record == NULL)
249                         {
250                                 LoggerW("contacts_list_get_current_record_p error : " << errorCode << " (" << __FUNCTION__ << ")");
251                                 continue;
252                         }
253
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);
258 /*
259                         if((charValue == NULL) && g_strcmp0(newValueStr, EMPTY_STRING) == 0){
260                                 is_success = true;
261                                 targetId = id;
262                                 break;
263                         }else
264 */
265                         if(g_strcmp0(charValue, newValueStr) == 0)
266                         {
267                                 is_success = true;
268                                 targetId = id;
269                                 break;
270                         }
271                         contacts_list_next(list);
272                 }
273
274                 if(filter != NULL)
275                         contacts_filter_destroy(filter);
276                 if(query != NULL)
277                         contacts_query_destroy(query);
278                 if(list != NULL)
279                         contacts_list_destroy(list, true);
280                 if(!is_success)
281                         ThrowMsg(PlatformException, "invalid photoURI path : " << errorCode << " (" << __FUNCTION__ << ")");
282
283                 filter = NULL;
284                 query = NULL;
285                 list = NULL;
286
287                 LoggerD("targetId : " << targetId);
288
289                 errorCode = contacts_query_create(_contacts_image._uri, &query);
290                 if(errorCode != CONTACTS_ERROR_NONE)
291                         ThrowMsg(PlatformException, "contacts_query_create error : " << errorCode << " (" << __FUNCTION__ << ")");
292
293                 errorCode = contacts_filter_create( _contacts_image._uri, &filter );
294                 if(errorCode != CONTACTS_ERROR_NONE)
295                         ThrowMsg(PlatformException, "contacts_query_create error : " << errorCode << " (" << __FUNCTION__ << ")");
296
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__ << ")");
300
301                 errorCode = contacts_query_set_filter(query, filter);
302                 if(errorCode != CONTACTS_ERROR_NONE)
303                         ThrowMsg(PlatformException, "contacts_query_set_filter error : " << errorCode << " (" << __FUNCTION__ << ")");
304
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__ << ")");
308
309                 record_count = 0;
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__ << ")");
313
314                 contacts_list_first(list);
315                 for(unsigned int i=0; i<record_count; i++)
316                 {
317                         contacts_record_h imageRecord;
318                         errorCode = contacts_list_get_current_record_p(list, &imageRecord);
319                         if(errorCode != CONTACTS_ERROR_NONE || imageRecord == NULL)
320                         {
321                                 LoggerW("contacts_list_get_current_record_p error : " << errorCode << " (" << __FUNCTION__ << ")");
322                                 continue;
323                         }
324
325                         errorCode = contacts_record_get_int(imageRecord, _contacts_image.id, &imageId);
326                         if(errorCode != CONTACTS_ERROR_NONE)
327                         {
328                                 LoggerW("contacts_record_get_int error : " << errorCode << " (" << __FUNCTION__ << ")");
329                                 continue;
330                         }
331
332                         errorCode = contacts_list_next(list);
333                         if(errorCode != CONTACTS_ERROR_NONE)
334                         {
335                                 LoggerW("contacts_list_next error : " << errorCode << " (" << __FUNCTION__ << ")");
336                                 break;
337                         }
338                 }
339
340                 if(filter != NULL)
341                         contacts_filter_destroy(filter);
342                 if(query != NULL)
343                         contacts_query_destroy(query);
344                 if(list != NULL)
345                         contacts_list_destroy(list, true);
346
347                 if(imageId != 0)
348                 {
349                         errorCode = contacts_person_set_default_property(CONTACTS_PERSON_PROPERTY_IMAGE, personId, imageId);
350                         if(errorCode != CONTACTS_ERROR_NONE)
351                         {
352                                 ThrowMsg(PlatformException, "importing photoURI E (err:" <<
353                                                 errorCode << ", str:" << abstractValueStr << ")");
354                         }
355                 }
356         }
357
358         // ringtoneURI
359         newValueStr = NULL;
360         oldValueStr = NULL;
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()))
367         {
368                 newValueStr = EMPTY_STRING;
369         }
370         else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
371         {
372                 newValueStr = abstractValueStr.c_str();
373         }
374
375         if(newValueStr != NULL)
376         {
377                 errorCode = contacts_record_set_str(record, _contacts_person.ringtone_path, newValueStr);
378                 if(errorCode != CONTACTS_ERROR_NONE)
379                 {
380                         ThrowMsg(PlatformException, "importing ringtoneURI E (err:" <<
381                                         errorCode << ", str:" << abstractValueStr << ")");
382                 }
383         }
384
385         // displayContactId
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)
389 //      {
390 //              ThrowMsg(PlatformException, "importing displayContactId E (err:" <<
391 //                              errorCode << ", value:" << intValue << ")");
392 //      } // FIXME platform still has problem on this field.
393 }
394
395 void ContactsSvcObjectConverter::convertToAbstract(contacts_record_h record, ContactPtr& contact)
396 {
397
398 }
399
400 void ContactsSvcObjectConverter::convertToPlatform(ContactPtr& contact, contacts_record_h record)
401 {
402
403 }
404
405 void ContactsSvcObjectConverter::convertToAbstract(contacts_record_h record, ContactGroupPtr& group)
406 {
407         if(record == NULL)
408         {
409                 ThrowMsg(InvalidArgumentException, "Platform group object did not set");
410         }
411
412         if(group == NULL)
413         {
414                 ThrowMsg(InvalidArgumentException, "Abstract group object did not set");
415         }
416
417         char *charValue = NULL;
418         int intValue = 0;
419         bool boolValue = false;
420
421         int errorCode = 0;
422
423         // id
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 << ")");
427
428         group->setId(intValue);
429
430         // addressBookId
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 << ")");
434
435         group->setAddressBookId(intValue);
436
437
438         // name
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 << ")");
442
443         if (charValue)
444                 group->setName(charValue);
445         else
446         {
447                 if(group->getNameIsSet())
448                         group->unsetName();
449         }
450
451         // photoURI
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 << ")");
455
456         if (charValue)
457                 group->setPhotoURI(ContactUtility::convertPathToUri(charValue));
458         else
459         {
460                 if(group->getPhotoURIIsSet())
461                         group->unsetPhotoURI();
462         }
463
464         // ringtoneURI
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 << ")");
468
469         if (charValue)
470                 group->setRingtoneURI(ContactUtility::convertPathToUri(charValue));
471         else
472         {
473                 if(group->getRingtoneURIIsSet())
474                         group->unsetRingtoneURI();
475         }
476
477         // isFavorite
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 << ")");
481
482         group->setReadOnly(boolValue);
483 }
484
485 void ContactsSvcObjectConverter::convertToPlatform(ContactGroupPtr& group, contacts_record_h record)
486 {
487         if(record == NULL)
488         {
489                 ThrowMsg(InvalidArgumentException, "Platform group object did not set");
490         }
491
492         if(group == NULL)
493         {
494                 ThrowMsg(InvalidArgumentException, "Abstract group object did not set");
495         }
496
497         int errorCode = 0;
498
499 //      // addressBookId
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);
502 //
503 //      int intValue = group->getAddressBookId();
504 //      errorCode = contacts_record_set_int(record, _contacts_group.address_book_id, intValue);
505 //      if(errorCode != CONTACTS_ERROR_NONE)
506 //      {
507 //              ThrowMsg(PlatformException, "importing displayContactId E (err:" <<
508 //                              errorCode << ", value:" << intValue << ")");
509 //      }
510
511         const char *newValueStr = NULL;
512         char *oldValueStr = NULL;
513         string abstractValueStr;
514         string realPath;
515
516         // name
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()))
521         {
522                 newValueStr = EMPTY_STRING;
523         }
524         else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
525         {
526                 newValueStr = abstractValueStr.c_str();
527         }
528
529         if(newValueStr != NULL)
530         {
531                 errorCode = contacts_record_set_str(record, _contacts_group.name, newValueStr);
532                 if(errorCode != CONTACTS_ERROR_NONE)
533                 {
534                         ThrowMsg(PlatformException, "importing name E (err:" <<
535                                         errorCode << ", str:" << abstractValueStr << ")");
536                 }
537         }
538
539         // photoURI
540         newValueStr = NULL;
541         oldValueStr = NULL;
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()))
548         {
549                 newValueStr = EMPTY_STRING;
550         }
551         else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
552         {
553                 newValueStr = abstractValueStr.c_str();
554         }
555
556         if(newValueStr != NULL)
557         {
558                 errorCode = contacts_record_set_str(record, _contacts_group.image_path, newValueStr);
559                 if(errorCode != CONTACTS_ERROR_NONE)
560                 {
561                         ThrowMsg(PlatformException, "importing photoURI E (err:" <<
562                                         errorCode << ", str:" << abstractValueStr << ")");
563                 }
564         }
565
566         // ringtoneURI
567         newValueStr = NULL;
568         oldValueStr = NULL;
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()))
575         {
576                 newValueStr = EMPTY_STRING;
577         }
578         else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
579         {
580                 newValueStr = abstractValueStr.c_str();
581         }
582
583         if(newValueStr != NULL)
584         {
585                 errorCode = contacts_record_set_str(record, _contacts_group.ringtone_path, newValueStr);
586                 if(errorCode != CONTACTS_ERROR_NONE)
587                 {
588                         ThrowMsg(PlatformException, "importing ringtoneURI E (err:" <<
589                                         errorCode << ", str:" << abstractValueStr << ")");
590                 }
591         }
592 }
593
594 } // Contact
595 } // DeviceAPI