c587be3626c3e96176ac1ca4090c579a17010890
[framework/web/wrt-plugins-tizen.git] / src / Contact / ContactObjectP2AConverter.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 "ContactObjectP2AConverter.h"
26 #include <vector>
27 #include <pcrecpp.h>
28 #include <Commons/Exception.h>
29 #include <Commons/Regex.h>
30 #include "AddressBook.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 ContactObjectP2AConverter::ContactObjectP2AConverter(contacts_record_h platformContact,
42                 bool forScratch) :
43                 m_platformContact(platformContact),
44                 m_abstractContact(ContactPtr(NULL)),
45                 m_forScratch(forScratch),
46                 m_convertFinished(false)
47 {
48 }
49
50 ContactObjectP2AConverter::ContactObjectP2AConverter(contacts_record_h platformContact,
51                 bool forScratch,
52                 ContactPtr &abstractContact) :
53                 m_platformContact(platformContact),
54                 m_abstractContact(abstractContact),
55                 m_forScratch(forScratch),
56                 m_convertFinished(false)
57 {
58 }
59
60 ContactObjectP2AConverter::~ContactObjectP2AConverter()
61 {
62 }
63
64 ContactPtr ContactObjectP2AConverter::getAbstractContact()
65 {
66         //LoggerD("enter");
67
68         if(m_platformContact == NULL)
69         {
70                 LoggerE("Platform contact object did not set");
71                 ThrowMsg(InvalidArgumentException, "Platform contact object did not set");
72         }
73
74         if(m_abstractContact == NULL)
75         {
76                 //LoggerD("Abstract contact object did not set, so a new object has been created");
77                 m_abstractContact = ContactPtr(new Contact());
78         }
79
80         //LoggerD("for scratch : " << m_forScratch);
81
82         Try
83         {
84                 if(m_convertFinished == false)
85                         convertToAbstractObject();
86         }
87         Catch(Exception)
88         {
89                 LoggerE("Error while converting - " << _rethrown_exception.GetMessage());
90                 ThrowMsg(PlatformException, "Fail to convert platform object to abstract object.");
91         }
92
93         return m_abstractContact;
94 }
95
96 void ContactObjectP2AConverter::convertToAbstractObject()
97 {
98         exportBaseInfoValue();
99         exportNameValue();
100         exportCompanyList();
101         exportNoteList();
102         exportNumberList();
103         exportEmailList();
104         exportGrouprelList();
105         exportEventList();
106         exportPostalList();
107         exportWebAddrList();
108         exportNicknameList();
109
110         m_convertFinished = true;
111 }
112
113 void ContactObjectP2AConverter::exportBaseInfoValue()
114 {
115         int errorCode = 0;
116
117         if(!m_forScratch)
118         {
119                 int id = 0;
120                 contacts_record_get_int(m_platformContact, _contacts_contact.id, &id);
121                 m_abstractContact->setId(id);
122
123                 int addressBookId = 0;
124                 contacts_record_get_int(m_platformContact, _contacts_contact.address_book_id, &addressBookId);
125                 m_abstractContact->setAddressBookId(addressBookId);
126
127                 int personId = 0;
128                 contacts_record_get_int(m_platformContact, _contacts_contact.person_id, &personId);
129                 m_abstractContact->setPersonId(personId);
130
131                 int changedTime = 0;
132                 contacts_record_get_int(m_platformContact, _contacts_contact.changed_time, &changedTime);
133                 time_t changedTimeT = static_cast<time_t>(changedTime);
134                 struct tm *changedTimeTm = gmtime(&changedTimeT);
135                 m_abstractContact->setLastUpdated(*changedTimeTm);
136
137                 bool isFavorite = false;
138                 errorCode = contacts_record_get_bool(m_platformContact, _contacts_contact.is_favorite, &isFavorite);
139                 if(errorCode != CONTACTS_ERROR_NONE)
140                         LoggerE("contacts_record_get_bool returned " << errorCode);
141                 m_abstractContact->setIsFavorite(isFavorite);
142         }
143
144         char *charValue = NULL;
145
146         contacts_record_get_str_p(m_platformContact, _contacts_contact.image_thumbnail_path, &charValue);
147         if (charValue)
148                 m_abstractContact->setPhotoURI(ContactUtility::convertPathToUri(charValue));
149         else
150         {
151                 if(m_abstractContact->getPhotoURIIsSet())
152                         m_abstractContact->unsetPhotoURI();
153         }
154
155         contacts_record_get_str_p(m_platformContact, _contacts_contact.ringtone_path, &charValue);
156         if (charValue)
157                 m_abstractContact->setRingtoneURI(ContactUtility::convertPathToUri(charValue));
158         else
159         {
160                 if(m_abstractContact->getRingtoneURIIsSet())
161                         m_abstractContact->unsetRingtoneURI();
162         }
163
164         if(!m_forScratch)
165         {
166                 contacts_record_get_str_p(m_platformContact, _contacts_contact.display_name, &charValue);
167                 if (charValue)
168                 {
169                         ContactNamePtr contactName = m_abstractContact->getName();
170                         if(contactName == NULL)
171                         {
172                                 contactName = ContactNamePtr(new ContactName());
173                                 m_abstractContact->setName(contactName);
174                         }
175                         contactName->setDisplayName(charValue);
176                 }
177 //              else
178 //                      ThrowMsg(UnknownException, "converting base data (no display name)");
179         }
180 }
181
182 void ContactObjectP2AConverter::exportNameValue()
183 {
184         int errorCode = 0;
185         contacts_record_h child_record = NULL;
186
187         errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.name, 0, &child_record);
188         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
189                 ThrowMsg(UnknownException, "getting name value (err:" << errorCode << ")");
190
191         if(child_record == NULL || errorCode == CONTACTS_ERROR_NO_DATA)
192         {
193                 //LoggerD("Platform contact don't have name value");
194                 ContactNamePtr contactName = m_abstractContact->getName();
195                 if(contactName != NULL)
196                 {
197                         if(contactName->getFirstNameIsSet())
198                                 contactName->unsetFirstName();
199
200                         if(contactName->getMiddleNameIsSet())
201                                 contactName->unsetMiddleName();
202
203                         if(contactName->getLastNameIsSet())
204                                 contactName->unsetLastName();
205
206                         if(contactName->getPrefixIsSet())
207                                 contactName->unsetPrefix();
208
209                         if(contactName->getSuffixIsSet())
210                                 contactName->unsetSuffix();
211
212                         if(contactName->getPhoneticFirstNameIsSet())
213                                 contactName->unsetPhoneticFirstName();
214
215                         if(contactName->getPhoneticLastNameIsSet())
216                                 contactName->unsetPhoneticLastName();
217                 }
218                 return;
219         }
220
221         ContactNamePtr contactName = m_abstractContact->getName();
222         if(contactName == NULL)
223         {
224                 contactName = ContactNamePtr(new ContactName());
225                 m_abstractContact->setName(contactName);
226         }
227
228         char *charValue = NULL;
229
230         contacts_record_get_str_p(child_record, _contacts_name.first, &charValue);
231         if (charValue)
232                 contactName->setFirstName(charValue);
233         else
234         {
235                 if(contactName->getFirstNameIsSet())
236                         contactName->unsetFirstName();
237         }
238
239         contacts_record_get_str_p(child_record, _contacts_name.addition, &charValue);
240         if (charValue)
241                 contactName->setMiddleName(charValue);
242         else
243         {
244                 if(contactName->getMiddleNameIsSet())
245                         contactName->unsetMiddleName();
246         }
247
248         contacts_record_get_str_p(child_record, _contacts_name.last, &charValue);
249         if (charValue)
250                 contactName->setLastName(charValue);
251         else
252         {
253                 if(contactName->getLastNameIsSet())
254                         contactName->unsetLastName();
255         }
256
257         contacts_record_get_str_p(child_record, _contacts_name.prefix, &charValue);
258         if (charValue)
259                 contactName->setPrefix(charValue);
260         else
261         {
262                 if(contactName->getPrefixIsSet())
263                         contactName->unsetPrefix();
264         }
265
266         contacts_record_get_str_p(child_record, _contacts_name.suffix, &charValue);
267         if (charValue)
268                 contactName->setSuffix(charValue);
269         else
270         {
271                 if(contactName->getSuffixIsSet())
272                         contactName->unsetSuffix();
273         }
274
275         contacts_record_get_str_p(child_record, _contacts_name.phonetic_first, &charValue);
276         if (charValue)
277                 contactName->setPhoneticFirstName(charValue);
278         else
279         {
280                 if(contactName->getPhoneticFirstNameIsSet())
281                         contactName->unsetPhoneticFirstName();
282         }
283
284         contacts_record_get_str_p(child_record, _contacts_name.phonetic_last, &charValue);
285         if (charValue)
286                 contactName->setPhoneticLastName(charValue);
287         else
288         {
289                 if(contactName->getPhoneticLastNameIsSet())
290                         contactName->unsetPhoneticLastName();
291         }
292 }
293
294 void ContactObjectP2AConverter::exportCompanyList()
295 {
296         int errorCode = 0;
297         unsigned int child_count = 0;
298
299         if(m_abstractContact->getOrganizationsNum() > 0)
300                 m_abstractContact->clearOrganizations();
301
302         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.company, &child_count);
303         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
304                 ThrowMsg(UnknownException, "getting company list (err:" << errorCode << ")");
305
306         if(child_count == 0 || errorCode == CONTACTS_ERROR_NO_DATA)
307         {
308                 //LoggerD("Platform contact don't have company list");
309                 return;
310         }
311
312         ContactOrganizationArrayPtr organizations = m_abstractContact->getOrganizations();
313
314         for(unsigned int i=0; i<child_count; i++)
315         {
316                 contacts_record_h child_record = NULL;
317
318                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.company, i, &child_record);
319                 if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
320                         continue;
321
322                 ContactOrganizationPtr organization(new ContactOrganization());
323
324                 organizations->push_back(organization);
325
326                 char *charValue = NULL;
327
328                 contacts_record_get_str_p(child_record, _contacts_company.name, &charValue);
329                 if (charValue)
330                         organization->setName(charValue);
331                 else
332                 {
333                         if(organization->getNameIsSet())
334                                 organization->unsetName();
335                 }
336
337                 contacts_record_get_str_p(child_record, _contacts_company.department, &charValue);
338                 if (charValue)
339                         organization->setDepartment(charValue);
340                 else
341                 {
342                         if(organization->getDepartmentIsSet())
343                                 organization->unsetDepartment();
344                 }
345
346                 contacts_record_get_str_p(child_record, _contacts_company.job_title, &charValue);
347                 if (charValue)
348                         organization->setTitle(charValue);
349                 else
350                 {
351                         if(organization->getTitleIsSet())
352                                 organization->unsetTitle();
353                 }
354
355                 contacts_record_get_str_p(child_record, _contacts_company.role, &charValue);
356                 if (charValue)
357                         organization->setRole(charValue);
358                 else
359                 {
360                         if(organization->getRoleIsSet())
361                                 organization->unsetRole();
362                 }
363
364                 contacts_record_get_str_p(child_record, _contacts_company.logo, &charValue);
365                 if (charValue)
366                         organization->setLogoURI(ContactUtility::convertPathToUri(charValue));
367                 else
368                 {
369                         if(organization->getLogoURIIsSet())
370                                 organization->unsetLogoURI();
371                 }
372         }
373 }
374
375 void ContactObjectP2AConverter::exportNoteList()
376 {
377         int errorCode = 0;
378         unsigned int child_count = 0;
379
380         if(m_abstractContact->getNotesNum() > 0)
381                 m_abstractContact->clearNotes();
382
383         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.note, &child_count);
384         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
385                 ThrowMsg(UnknownException, "getting note list (err:" << errorCode << ")");
386
387         if(child_count == 0 || errorCode == CONTACTS_ERROR_NO_DATA)
388         {
389                 //LoggerD("Platform contact don't have note list");
390                 return;
391         }
392
393         StringArrayPtr notes = m_abstractContact->getNotes();
394
395         for(unsigned int i=0; i<child_count; i++)
396         {
397                 contacts_record_h child_record = NULL;
398
399                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.note, i, &child_record);
400                 if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
401                         continue;
402
403                 char *charValue = NULL;
404
405                 contacts_record_get_str_p(child_record, _contacts_note.note, &charValue);
406                 if (charValue)
407                         notes->push_back(charValue);
408         }
409 }
410
411 void ContactObjectP2AConverter::exportNumberList()
412 {
413         int errorCode = 0;
414         unsigned int child_count = 0;
415
416         if(m_abstractContact->getPhoneNumbersNum() > 0)
417                 m_abstractContact->clearPhoneNumbers();
418
419         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.number, &child_count);
420         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
421                 ThrowMsg(UnknownException, "getting number list (err:" << errorCode << ")");
422
423         if(child_count == 0 || errorCode == CONTACTS_ERROR_NO_DATA)
424         {
425                 //LoggerD("Platform contact don't have number list");
426                 return;
427         }
428
429         ContactPhoneNumberArrayPtr phoneNumbers = m_abstractContact->getPhoneNumbers();
430
431         for(unsigned int i=0; i<child_count; i++)
432         {
433                 contacts_record_h child_record = NULL;
434
435                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.number, i, &child_record);
436                 if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
437                         continue;
438
439                 ContactPhoneNumberPtr phoneNumber(new ContactPhoneNumber());
440
441                 char *charValue = NULL;
442                 bool boolValue = false;
443                 int intValue = 0;
444
445                 contacts_record_get_str_p(child_record, _contacts_number.number, &charValue);
446                 if (charValue)
447                 {
448                         phoneNumber->setNumber(charValue);
449                         phoneNumbers->push_back(phoneNumber);
450                 }
451                 else
452                 {
453                         //LoggerD("Platform contact have a empty phone number");
454                         continue;
455                 }
456
457                 contacts_record_get_bool(child_record, _contacts_number.is_default, &boolValue);
458                 phoneNumber->setIsDefault(boolValue);
459
460                 contacts_record_get_int(child_record, _contacts_number.type, &intValue);
461                 if(intValue & CONTACTS_NUMBER_TYPE_HOME)
462                         phoneNumber->addType(CONTACT_PHONE_NUMBER_TYPE_HOME);
463                 if(intValue & CONTACTS_NUMBER_TYPE_WORK)
464                         phoneNumber->addType(CONTACT_PHONE_NUMBER_TYPE_WORK);
465                 if(intValue & CONTACTS_NUMBER_TYPE_VOICE)
466                         phoneNumber->addType(CONTACT_PHONE_NUMBER_TYPE_VOICE);
467                 if(intValue & CONTACTS_NUMBER_TYPE_FAX)
468                         phoneNumber->addType(CONTACT_PHONE_NUMBER_TYPE_FAX);
469                 if(intValue & CONTACTS_NUMBER_TYPE_MSG)
470                         phoneNumber->addType(CONTACT_PHONE_NUMBER_TYPE_MSG);
471                 if(intValue & CONTACTS_NUMBER_TYPE_CELL)
472                         phoneNumber->addType(CONTACT_PHONE_NUMBER_TYPE_CELL);
473                 if(intValue & CONTACTS_NUMBER_TYPE_PAGER)
474                         phoneNumber->addType(CONTACT_PHONE_NUMBER_TYPE_PAGER);
475                 if(intValue & CONTACTS_NUMBER_TYPE_BBS)
476                         phoneNumber->addType(CONTACT_PHONE_NUMBER_TYPE_BBS);
477                 if(intValue & CONTACTS_NUMBER_TYPE_MODEM)
478                         phoneNumber->addType(CONTACT_PHONE_NUMBER_TYPE_MODEM);
479                 if(intValue & CONTACTS_NUMBER_TYPE_CAR)
480                         phoneNumber->addType(CONTACT_PHONE_NUMBER_TYPE_CAR);
481                 if(intValue & CONTACTS_NUMBER_TYPE_ISDN)
482                         phoneNumber->addType(CONTACT_PHONE_NUMBER_TYPE_ISDN);
483                 if(intValue & CONTACTS_NUMBER_TYPE_VIDEO)
484                         phoneNumber->addType(CONTACT_PHONE_NUMBER_TYPE_VIDEO);
485                 if(intValue & CONTACTS_NUMBER_TYPE_PCS)
486                         phoneNumber->addType(CONTACT_PHONE_NUMBER_TYPE_PCS);
487
488                 if(phoneNumber->getTypes()->size() == 0)
489                         phoneNumber->addType(CONTACT_PHONE_NUMBER_TYPE_VOICE);
490
491                 // TODO Will be added after type changed to string
492 //              if(intValue & CONTACTS_NUMBER_TYPE_CUSTOM)
493 //              {
494 //                      contacts_record_get_str_p(child_record, _contacts_number.label, &charValue);
495 //                      if (charValue)
496 //                              phoneNumber->addType(CUSTOM);
497 //              }
498         }
499 }
500
501 void ContactObjectP2AConverter::exportEmailList()
502 {
503         int errorCode = 0;
504         unsigned int child_count = 0;
505
506         if(m_abstractContact->getEmailsNum() > 0)
507                 m_abstractContact->clearEmails();
508
509         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.email, &child_count);
510         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
511                 ThrowMsg(UnknownException, "getting email list (err:" << errorCode << ")");
512
513         if(child_count == 0 || errorCode == CONTACTS_ERROR_NO_DATA)
514         {
515                 //LoggerD("Platform contact don't have email list");
516                 return;
517         }
518
519         ContactEmailAddressArrayPtr emails = m_abstractContact->getEmails();
520
521         for(unsigned int i=0; i<child_count; i++)
522         {
523                 contacts_record_h child_record = NULL;
524
525                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.email, i, &child_record);
526                 if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
527                         continue;
528
529                 ContactEmailAddressPtr email(new ContactEmailAddress());
530
531                 char *charValue = NULL;
532                 bool boolValue = false;
533                 int intValue = 0;
534
535                 contacts_record_get_str_p(child_record, _contacts_email.email, &charValue);
536                 if (charValue)
537                 {
538                         email->setEmail(charValue);
539                         emails->push_back(email);
540                 }
541                 else
542                 {
543                         //LoggerD("Platform contact have a empty email address");
544                         continue;
545                 }
546
547                 contacts_record_get_bool(child_record, _contacts_email.is_default, &boolValue);
548                 email->setIsDefault(boolValue);
549
550                 contacts_record_get_int(child_record, _contacts_email.type, &intValue);
551                 if(intValue & CONTACTS_EMAIL_TYPE_HOME)
552                         email->addType(CONTACT_EMAIL_TYPE_HOME);
553                 if(intValue & CONTACTS_EMAIL_TYPE_WORK)
554                         email->addType(CONTACT_EMAIL_TYPE_WORK);
555
556                 if(email->getTypes()->size() == 0)
557                         email->addType(CONTACT_EMAIL_TYPE_HOME);
558
559                 // TODO Will be added after type changed to string
560 //              if(intValue & CONTACTS_EMAIL_TYPE_CUSTOM)
561 //              {
562 //                      contacts_record_get_str_p(child_record, _contacts_email.label, &charValue);
563 //                      if (charValue)
564 //                              email->addType(CUSTOM);
565 //              }
566         }
567 }
568
569 void ContactObjectP2AConverter::exportGrouprelList()
570 {
571         int errorCode = 0;
572         unsigned int child_count = 0;
573
574         if(m_abstractContact->getGroupIdsNum() > 0)
575                 m_abstractContact->clearGroupIds();
576
577         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.group_relation, &child_count);
578         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
579                 ThrowMsg(UnknownException, "getting group list (err:" << errorCode << ")");
580
581         if(child_count == 0 || errorCode == CONTACTS_ERROR_NO_DATA)
582         {
583                 //LoggerD("Platform contact don't have group list");
584                 return;
585         }
586
587         StringArrayPtr groupIds = m_abstractContact->getGroupIds();
588
589         for(unsigned int i=0; i<child_count; i++)
590         {
591                 contacts_record_h child_record = NULL;
592
593                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.group_relation, i, &child_record);
594                 if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
595                         continue;
596
597                 int groupId;
598
599                 contacts_record_get_int(child_record, _contacts_group_relation.group_id, &groupId);
600
601                 groupIds->push_back(ContactUtility::intToStr(groupId));
602         }
603 }
604
605 void ContactObjectP2AConverter::exportEventList()
606 {
607         int errorCode = 0;
608         unsigned int child_count = 0;
609
610         if(m_abstractContact->getAnniversariesNum() > 0)
611                 m_abstractContact->clearAnniversaries();
612
613         if(m_abstractContact->getBirthdayIsSet())
614                 m_abstractContact->unsetBirthday();
615
616         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.event, &child_count);
617         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
618                 ThrowMsg(UnknownException, "getting event list (err:" << errorCode << ")");
619
620         if(child_count == 0 || errorCode == CONTACTS_ERROR_NO_DATA)
621         {
622                 //LoggerD("Platform contact don't have group list");
623                 return;
624         }
625
626         ContactAnniversaryArrayPtr anniversaries = m_abstractContact->getAnniversaries();
627
628         for(unsigned int i=0; i<child_count; i++)
629         {
630                 contacts_record_h child_record = NULL;
631
632                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.event, i, &child_record);
633                 if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
634                         continue;
635
636                 char *charValue = NULL;
637                 int intValueType = 0;
638                 int intValueDate = 0;
639
640                 contacts_record_get_int(child_record, _contacts_event.date, &intValueDate);
641
642                 tm tmDate = {0, };
643
644                 Try {
645                         tmDate = ContactUtility::toDateTmFromDateDbInt(intValueDate);
646                 } Catch(Exception) {
647                         LoggerE("date has wrong value");
648                 }
649
650                 contacts_record_get_int(child_record, _contacts_event.type, &intValueType);
651                 if (intValueType == CONTACTS_EVENT_TYPE_BIRTH)
652                 {
653                         m_abstractContact->setBirthday(tmDate);
654                 }
655                 else if(intValueType == CONTACTS_EVENT_TYPE_ANNIVERSARY)
656                 {
657                         ContactAnniversaryPtr anniversary = ContactAnniversaryPtr(new ContactAnniversary());
658
659                         anniversary->setDate(tmDate);
660                         contacts_record_get_str_p(child_record, _contacts_event.label, &charValue);
661                         if(charValue)
662                                 anniversary->setLabel(charValue);
663
664                         anniversaries->push_back(anniversary);
665                 }else{
666                         ContactAnniversaryPtr anniversary = ContactAnniversaryPtr(new ContactAnniversary());
667
668                         anniversary->setDate(tmDate);
669                         contacts_record_get_str_p(child_record, _contacts_event.label, &charValue);
670                         if(charValue)
671                                 anniversary->setLabel(charValue);
672
673                         anniversaries->push_back(anniversary);
674                 }
675         }
676 }
677
678 void ContactObjectP2AConverter::exportPostalList()
679 {
680         int errorCode = 0;
681         unsigned int child_count = 0;
682
683         if(m_abstractContact->getAddressesNum() > 0)
684                 m_abstractContact->clearAddresses();
685
686         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.address, &child_count);
687         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
688                 ThrowMsg(UnknownException, "getting address list (err:" << errorCode << ")");
689
690         if(child_count == 0 || errorCode == CONTACTS_ERROR_NO_DATA)
691         {
692                 //LoggerD("Platform contact don't have address list");
693                 return;
694         }
695
696         ContactAddressArrayPtr addresss = m_abstractContact->getAddresses();
697
698         for(unsigned int i=0; i<child_count; i++)
699         {
700                 contacts_record_h child_record = NULL;
701
702                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.address, i, &child_record);
703                 if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
704                         continue;
705
706                 ContactAddressPtr address(new ContactAddress());
707
708                 char *charValue = NULL;
709                 bool boolValue = false;
710                 int intValue = 0;
711
712                 addresss->push_back(address);
713
714                 contacts_record_get_str_p(child_record, _contacts_address.postal_code, &charValue);
715                 if (charValue)
716                         address->setPostalCode(charValue);
717
718                 contacts_record_get_str_p(child_record, _contacts_address.region, &charValue);
719                 if (charValue)
720                         address->setRegion(charValue);
721
722                 contacts_record_get_str_p(child_record, _contacts_address.locality, &charValue);
723                 if (charValue)
724                         address->setCity(charValue);
725
726                 contacts_record_get_str_p(child_record, _contacts_address.street, &charValue);
727                 if (charValue)
728                         address->setStreetAddress(charValue);
729
730                 contacts_record_get_str_p(child_record, _contacts_address.extended, &charValue);
731                 if (charValue)
732                         address->setAdditionalInformation(charValue);
733
734                 contacts_record_get_str_p(child_record, _contacts_address.country, &charValue);
735                 if (charValue)
736                         address->setCountry(charValue);
737
738                 contacts_record_get_bool(child_record, _contacts_address.is_default, &boolValue);
739                 address->setIsDefault(boolValue);
740
741                 contacts_record_get_int(child_record, _contacts_address.type, &intValue);
742                 if(intValue & CONTACTS_ADDRESS_TYPE_HOME)
743                         address->addType(CONTACT_ADDRESS_TYPE_HOME);
744                 if(intValue & CONTACTS_ADDRESS_TYPE_WORK)
745                         address->addType(CONTACT_ADDRESS_TYPE_WORK);
746
747                 if(address->getTypes()->size() == 0)
748                         address->addType(CONTACT_ADDRESS_TYPE_HOME);
749
750                 // TODO Will be added after type changed to string
751 //              if(intValue & CONTACTS_ADDRESS_TYPE_CUSTOM)
752 //              {
753 //                      contacts_record_get_str_p(child_record, _contacts_address.label, &charValue);
754 //                      if (charValue)
755 //                              address->addType(CUSTOM);
756 //              }
757         }
758 }
759
760 void ContactObjectP2AConverter::exportWebAddrList()
761 {
762         int errorCode = 0;
763         unsigned int child_count = 0;
764
765         if(m_abstractContact->getUrlsNum() > 0)
766                 m_abstractContact->clearUrls();
767
768         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.url, &child_count);
769         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
770                 ThrowMsg(UnknownException, "getting address list (err:" << errorCode << ")");
771
772         if(child_count == 0 || errorCode == CONTACTS_ERROR_NO_DATA)
773         {
774                 //LoggerD("Platform contact don't have address list");
775                 return;
776         }
777
778         ContactWebSiteArrayPtr urls = m_abstractContact->getUrls();
779
780         for(unsigned int i=0; i<child_count; i++)
781         {
782                 contacts_record_h child_record = NULL;
783
784                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.url, i, &child_record);
785                 if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
786                         continue;
787
788                 ContactWebSitePtr url(new ContactWebSite());
789
790                 char *charValue = NULL;
791                 int intValue = 0;
792
793                 urls->push_back(url);
794
795                 contacts_record_get_str_p(child_record, _contacts_url.url, &charValue);
796                 if (charValue)
797                         url->setUrl(charValue);
798
799                 contacts_record_get_int(child_record, _contacts_url.type, &intValue);
800                 if(intValue == CONTACTS_URL_TYPE_HOME)
801                         url->setType(WEBSITE_TYPE_HOMEPAGE);
802                 if(intValue == CONTACTS_URL_TYPE_WORK)
803                         url->setType(WEBSITE_TYPE_BLOG);
804
805                 if(url->getTypeIsSet() == false)
806                         url->setType(WEBSITE_TYPE_HOMEPAGE);
807
808                 // TODO Will be added after type changed to string
809 //              if(intValue & CONTACTS_URL_TYPE_CUSTOM)
810 //              {
811 //                      contacts_record_get_int(child_record, _contacts_url.label, &intValue);
812 //                      if (charValue)
813 //                              url->setType(CUSTOM);
814 //              }
815         }
816 }
817
818 void ContactObjectP2AConverter::exportNicknameList()
819 {
820         int errorCode = 0;
821         unsigned int child_count = 0;
822
823         ContactNamePtr contactName = m_abstractContact->getName();
824         if(contactName == NULL)
825         {
826                 contactName = ContactNamePtr(new ContactName());
827                 m_abstractContact->setName(contactName);
828         }
829
830         if(contactName->getNicknamesNum() > 0)
831                 contactName->clearNicknames();
832
833         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.nickname, &child_count);
834         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
835                 ThrowMsg(UnknownException, "getting nickname list (err:" << errorCode << ")");
836
837         if(child_count == 0 || errorCode == CONTACTS_ERROR_NO_DATA)
838         {
839                 //LoggerD("Platform contact don't have nickname list");
840                 return;
841         }
842
843         StringArrayPtr nicknames = contactName->getNicknames();
844
845         for(unsigned int i=0; i<child_count; i++)
846         {
847                 contacts_record_h child_record = NULL;
848
849                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.nickname, i, &child_record);
850                 if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
851                         continue;
852
853                 char *charValue = NULL;
854
855                 contacts_record_get_str_p(child_record, _contacts_nickname.name, &charValue);
856                 if (charValue)
857                         nicknames->push_back(charValue);
858         }
859 }
860
861 } // Contact
862 } // DeviceAPI