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