983a14b10132b1e2926afe67e3096a00f71b9ef9
[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:" << errorCode);
171         }
172
173         const char *newValueStr = NULL;
174         char *oldValueStr = NULL;
175         string abstractValueStr;
176         string realPath;
177
178         // photoURI
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();
183
184         if(oldValueStr != NULL)
185         {
186                 if(!person->getPhotoURIIsSet() || person->getPhotoURI().empty())
187                 {
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)
193         {
194                 if(person->getPhotoURI().empty()){
195                         newValueStr = NULL;
196                 }else{
197                         newValueStr = abstractValueStr.c_str();
198                 }
199         }
200
201         if(newValueStr != NULL)
202         {
203                 int personId = ContactUtility::strToInt(person->getId());
204
205                 contacts_filter_h filter = NULL;
206                 contacts_query_h query = NULL;
207                 contacts_list_h list = NULL;
208                 bool is_success = false;
209                 int imageId = 0;
210
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__ << ")");
214
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__ << ")");
218
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__ << ")");
222
223                 errorCode = contacts_query_set_filter(query, filter);
224                 if(errorCode != CONTACTS_ERROR_NONE)
225                         ThrowMsg(PlatformException, "contacts_query_set_filter error : " << errorCode << " (" << __FUNCTION__ << ")");
226
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__ << ")");
230
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__ << ")");
235
236                 int targetId = 0;
237
238                 contacts_list_first(list);
239                 for(unsigned int i=0; i<record_count; i++)
240                 {
241                         contacts_record_h record;
242                         int id = 0;
243                         char *charValue = NULL;
244
245                         errorCode = contacts_list_get_current_record_p(list, &record);
246                         if(errorCode != CONTACTS_ERROR_NONE || record == NULL)
247                         {
248                                 LoggerW("contacts_list_get_current_record_p error : " << errorCode << " (" << __FUNCTION__ << ")");
249                                 continue;
250                         }
251
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);
254 /*
255                         if((charValue == NULL) && g_strcmp0(newValueStr, EMPTY_STRING) == 0){
256                                 is_success = true;
257                                 targetId = id;
258                                 break;
259                         }else
260 */
261                         if(g_strcmp0(charValue, newValueStr) == 0)
262                         {
263                                 is_success = true;
264                                 targetId = id;
265                                 break;
266                         }
267                         contacts_list_next(list);
268                 }
269
270                 if(filter != NULL)
271                         contacts_filter_destroy(filter);
272                 if(query != NULL)
273                         contacts_query_destroy(query);
274                 if(list != NULL)
275                         contacts_list_destroy(list, true);
276                 if(!is_success)
277                         ThrowMsg(PlatformException, "invalid photoURI path : " << errorCode << " (" << __FUNCTION__ << ")");
278
279                 filter = NULL;
280                 query = NULL;
281                 list = NULL;
282
283                 errorCode = contacts_query_create(_contacts_image._uri, &query);
284                 if(errorCode != CONTACTS_ERROR_NONE)
285                         ThrowMsg(PlatformException, "contacts_query_create error : " << errorCode << " (" << __FUNCTION__ << ")");
286
287                 errorCode = contacts_filter_create( _contacts_image._uri, &filter );
288                 if(errorCode != CONTACTS_ERROR_NONE)
289                         ThrowMsg(PlatformException, "contacts_query_create error : " << errorCode << " (" << __FUNCTION__ << ")");
290
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__ << ")");
294
295                 errorCode = contacts_query_set_filter(query, filter);
296                 if(errorCode != CONTACTS_ERROR_NONE)
297                         ThrowMsg(PlatformException, "contacts_query_set_filter error : " << errorCode << " (" << __FUNCTION__ << ")");
298
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__ << ")");
302
303                 record_count = 0;
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__ << ")");
307
308                 contacts_list_first(list);
309                 for(unsigned int i=0; i<record_count; i++)
310                 {
311                         contacts_record_h imageRecord;
312                         errorCode = contacts_list_get_current_record_p(list, &imageRecord);
313                         if(errorCode != CONTACTS_ERROR_NONE || imageRecord == NULL)
314                         {
315                                 LoggerW("contacts_list_get_current_record_p error : " << errorCode << " (" << __FUNCTION__ << ")");
316                                 continue;
317                         }
318
319                         errorCode = contacts_record_get_int(imageRecord, _contacts_image.id, &imageId);
320                         if(errorCode != CONTACTS_ERROR_NONE)
321                         {
322                                 LoggerW("contacts_record_get_int error : " << errorCode << " (" << __FUNCTION__ << ")");
323                                 continue;
324                         }
325
326                         errorCode = contacts_list_next(list);
327                         if(errorCode != CONTACTS_ERROR_NONE)
328                         {
329                                 LoggerW("contacts_list_next error : " << errorCode << " (" << __FUNCTION__ << ")");
330                                 break;
331                         }
332                 }
333
334                 if(filter != NULL)
335                         contacts_filter_destroy(filter);
336                 if(query != NULL)
337                         contacts_query_destroy(query);
338                 if(list != NULL)
339                         contacts_list_destroy(list, true);
340
341                 if(imageId != 0)
342                 {
343                         errorCode = contacts_person_set_default_property(CONTACTS_PERSON_PROPERTY_IMAGE, personId, imageId);
344                         if(errorCode != CONTACTS_ERROR_NONE)
345                         {
346                                 ThrowMsg(PlatformException, "importing photoURI E (err:" <<     errorCode);
347                         }
348                 }
349         }
350
351         // ringtoneURI
352         newValueStr = NULL;
353         oldValueStr = NULL;
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()))
360         {
361                 newValueStr = EMPTY_STRING;
362         }
363         else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
364         {
365                 newValueStr = abstractValueStr.c_str();
366         }
367
368         if(newValueStr != NULL)
369         {
370                 errorCode = contacts_record_set_str(record, _contacts_person.ringtone_path, newValueStr);
371                 if(errorCode != CONTACTS_ERROR_NONE)
372                 {
373                         ThrowMsg(PlatformException, "importing ringtoneURI E (err:" << errorCode);
374                 }
375         }
376
377         // displayContactId
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)
381 //      {
382 //              ThrowMsg(PlatformException, "importing displayContactId E (err:" <<
383 //                              errorCode << ", value:" << intValue << ")");
384 //      } // FIXME platform still has problem on this field.
385 }
386
387 void ContactsSvcObjectConverter::convertToAbstract(contacts_record_h record, ContactPtr& contact)
388 {
389
390 }
391
392 void ContactsSvcObjectConverter::convertToPlatform(ContactPtr& contact, contacts_record_h record)
393 {
394
395 }
396
397 void ContactsSvcObjectConverter::convertToAbstract(contacts_record_h record, ContactGroupPtr& group)
398 {
399         if(record == NULL)
400         {
401                 ThrowMsg(InvalidArgumentException, "Platform group object did not set");
402         }
403
404         if(group == NULL)
405         {
406                 ThrowMsg(InvalidArgumentException, "Abstract group object did not set");
407         }
408
409         char *charValue = NULL;
410         int intValue = 0;
411         bool boolValue = false;
412
413         int errorCode = 0;
414
415         // id
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 << ")");
419
420         group->setId(intValue);
421
422         // addressBookId
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 << ")");
426
427         group->setAddressBookId(intValue);
428
429
430         // name
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 << ")");
434
435         if (charValue)
436                 group->setName(charValue);
437         else
438         {
439                 if(group->getNameIsSet())
440                         group->unsetName();
441         }
442
443         // photoURI
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 << ")");
447
448         if (charValue)
449                 group->setPhotoURI(ContactUtility::convertPathToUri(charValue));
450         else
451         {
452                 if(group->getPhotoURIIsSet())
453                         group->unsetPhotoURI();
454         }
455
456         // ringtoneURI
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 << ")");
460
461         if (charValue)
462                 group->setRingtoneURI(ContactUtility::convertPathToUri(charValue));
463         else
464         {
465                 if(group->getRingtoneURIIsSet())
466                         group->unsetRingtoneURI();
467         }
468
469         // isFavorite
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 << ")");
473
474         group->setReadOnly(boolValue);
475 }
476
477 void ContactsSvcObjectConverter::convertToPlatform(ContactGroupPtr& group, contacts_record_h record)
478 {
479         if(record == NULL)
480         {
481                 ThrowMsg(InvalidArgumentException, "Platform group object did not set");
482         }
483
484         if(group == NULL)
485         {
486                 ThrowMsg(InvalidArgumentException, "Abstract group object did not set");
487         }
488
489         int errorCode = 0;
490
491 //      // addressBookId
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);
494 //
495 //      int intValue = group->getAddressBookId();
496 //      errorCode = contacts_record_set_int(record, _contacts_group.address_book_id, intValue);
497 //      if(errorCode != CONTACTS_ERROR_NONE)
498 //      {
499 //              ThrowMsg(PlatformException, "importing displayContactId E (err:" <<
500 //                              errorCode << ", value:" << intValue << ")");
501 //      }
502
503         const char *newValueStr = NULL;
504         char *oldValueStr = NULL;
505         string abstractValueStr;
506         string realPath;
507
508         // name
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()))
513         {
514                 newValueStr = EMPTY_STRING;
515         }
516         else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
517         {
518                 newValueStr = abstractValueStr.c_str();
519         }
520
521         if(newValueStr != NULL)
522         {
523                 errorCode = contacts_record_set_str(record, _contacts_group.name, newValueStr);
524                 if(errorCode != CONTACTS_ERROR_NONE)
525                 {
526                         ThrowMsg(PlatformException, "importing name E (err:" << errorCode);
527                 }
528         }
529
530         // photoURI
531         newValueStr = NULL;
532         oldValueStr = NULL;
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()))
539         {
540                 newValueStr = EMPTY_STRING;
541         }
542         else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
543         {
544                 newValueStr = abstractValueStr.c_str();
545         }
546
547         if(newValueStr != NULL)
548         {
549                 errorCode = contacts_record_set_str(record, _contacts_group.image_path, newValueStr);
550                 if(errorCode != CONTACTS_ERROR_NONE)
551                 {
552                         ThrowMsg(PlatformException, "importing photoURI E (err:" <<     errorCode);
553                 }
554         }
555
556         // ringtoneURI
557         newValueStr = NULL;
558         oldValueStr = NULL;
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()))
565         {
566                 newValueStr = EMPTY_STRING;
567         }
568         else if(oldValueStr == NULL || abstractValueStr != oldValueStr)
569         {
570                 newValueStr = abstractValueStr.c_str();
571         }
572
573         if(newValueStr != NULL)
574         {
575                 errorCode = contacts_record_set_str(record, _contacts_group.ringtone_path, newValueStr);
576                 if(errorCode != CONTACTS_ERROR_NONE)
577                 {
578                         ThrowMsg(PlatformException, "importing ringtoneURI E (err:" << errorCode);
579                 }
580         }
581 }
582
583 } // Contact
584 } // DeviceAPI