c49e53cac4531d6b163b6d27c04f28283cd022af
[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                 // TODO Will be added after type changed to string
490 //              if(intValue & CONTACTS_NUMBER_TYPE_CUSTOM)
491 //              {
492 //                      contacts_record_get_str_p(child_record, _contacts_number.label, &charValue);
493 //                      if (charValue)
494 //                              phoneNumber->addType(CUSTOM);
495 //              }
496         }
497 }
498
499 void ContactObjectP2AConverter::exportEmailList()
500 {
501         int errorCode = 0;
502         unsigned int child_count = 0;
503
504         if(m_abstractContact->getEmailsNum() > 0)
505                 m_abstractContact->clearEmails();
506
507         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.email, &child_count);
508         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
509                 ThrowMsg(UnknownException, "getting email list (err:" << errorCode << ")");
510
511         if(child_count == 0 || errorCode == CONTACTS_ERROR_NO_DATA)
512         {
513                 //LogDebug("Platform contact don't have email list");
514                 return;
515         }
516
517         ContactEmailAddressArrayPtr emails = m_abstractContact->getEmails();
518
519         for(unsigned int i=0; i<child_count; i++)
520         {
521                 contacts_record_h child_record = NULL;
522
523                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.email, i, &child_record);
524                 if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
525                         continue;
526
527                 ContactEmailAddressPtr email(new ContactEmailAddress());
528
529                 char *charValue = NULL;
530                 bool boolValue = false;
531                 int intValue = 0;
532
533                 contacts_record_get_str_p(child_record, _contacts_email.email, &charValue);
534                 if (charValue)
535                 {
536                         email->setEmail(charValue);
537                         emails->push_back(email);
538                 }
539                 else
540                 {
541                         //LogDebug("Platform contact have a empty email address");
542                         continue;
543                 }
544
545                 contacts_record_get_bool(child_record, _contacts_email.is_default, &boolValue);
546                 email->setIsDefault(boolValue);
547
548                 contacts_record_get_int(child_record, _contacts_email.type, &intValue);
549                 if(intValue & CONTACTS_EMAIL_TYPE_HOME)
550                         email->addType(CONTACT_EMAIL_TYPE_HOME);
551                 if(intValue & CONTACTS_EMAIL_TYPE_WORK)
552                         email->addType(CONTACT_EMAIL_TYPE_WORK);
553
554                 // TODO Will be added after type changed to string
555 //              if(intValue & CONTACTS_EMAIL_TYPE_CUSTOM)
556 //              {
557 //                      contacts_record_get_str_p(child_record, _contacts_email.label, &charValue);
558 //                      if (charValue)
559 //                              email->addType(CUSTOM);
560 //              }
561         }
562 }
563
564 void ContactObjectP2AConverter::exportGrouprelList()
565 {
566         int errorCode = 0;
567         unsigned int child_count = 0;
568
569         if(m_abstractContact->getGroupIdsNum() > 0)
570                 m_abstractContact->clearGroupIds();
571
572         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.group_relation, &child_count);
573         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
574                 ThrowMsg(UnknownException, "getting group list (err:" << errorCode << ")");
575
576         if(child_count == 0 || errorCode == CONTACTS_ERROR_NO_DATA)
577         {
578                 //LogDebug("Platform contact don't have group list");
579                 return;
580         }
581
582         StringArrayPtr groupIds = m_abstractContact->getGroupIds();
583
584         for(unsigned int i=0; i<child_count; i++)
585         {
586                 contacts_record_h child_record = NULL;
587
588                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.group_relation, i, &child_record);
589                 if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
590                         continue;
591
592                 int groupId;
593
594                 contacts_record_get_int(child_record, _contacts_group_relation.group_id, &groupId);
595
596                 groupIds->push_back(ContactUtility::intToStr(groupId));
597         }
598 }
599
600 void ContactObjectP2AConverter::exportEventList()
601 {
602         int errorCode = 0;
603         unsigned int child_count = 0;
604
605         if(m_abstractContact->getAnniversariesNum() > 0)
606                 m_abstractContact->clearAnniversaries();
607
608         if(m_abstractContact->getBirthdayIsSet())
609                 m_abstractContact->unsetBirthday();
610
611         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.event, &child_count);
612         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
613                 ThrowMsg(UnknownException, "getting event list (err:" << errorCode << ")");
614
615         if(child_count == 0 || errorCode == CONTACTS_ERROR_NO_DATA)
616         {
617                 //LogDebug("Platform contact don't have group list");
618                 return;
619         }
620
621         ContactAnniversaryArrayPtr anniversaries = m_abstractContact->getAnniversaries();
622
623         for(unsigned int i=0; i<child_count; i++)
624         {
625                 contacts_record_h child_record = NULL;
626
627                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.event, i, &child_record);
628                 if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
629                         continue;
630
631                 char *charValue = NULL;
632                 int intValueType = 0;
633                 int intValueDate = 0;
634
635                 contacts_record_get_int(child_record, _contacts_event.date, &intValueDate);
636
637                 tm tmDate = {0, };
638
639                 Try {
640                         tmDate = ContactUtility::toDateTmFromDateDbInt(intValueDate);
641                 } Catch(Exception) {
642                         LogError("date has wrong value");
643                 }
644
645                 contacts_record_get_int(child_record, _contacts_event.type, &intValueType);
646                 if (intValueType == CONTACTS_EVENT_TYPE_BIRTH)
647                 {
648                         m_abstractContact->setBirthday(tmDate);
649                 }
650                 else if(intValueType == CONTACTS_EVENT_TYPE_ANNIVERSARY)
651                 {
652                         ContactAnniversaryPtr anniversary = ContactAnniversaryPtr(new ContactAnniversary());
653
654                         anniversary->setDate(tmDate);
655                         contacts_record_get_str_p(child_record, _contacts_event.label, &charValue);
656                         if(charValue)
657                                 anniversary->setLabel(charValue);
658
659                         anniversaries->push_back(anniversary);
660                 }
661         }
662 }
663
664 void ContactObjectP2AConverter::exportPostalList()
665 {
666         int errorCode = 0;
667         unsigned int child_count = 0;
668
669         if(m_abstractContact->getAddressesNum() > 0)
670                 m_abstractContact->clearAddresses();
671
672         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.address, &child_count);
673         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
674                 ThrowMsg(UnknownException, "getting address list (err:" << errorCode << ")");
675
676         if(child_count == 0 || errorCode == CONTACTS_ERROR_NO_DATA)
677         {
678                 //LogDebug("Platform contact don't have address list");
679                 return;
680         }
681
682         ContactAddressArrayPtr addresss = m_abstractContact->getAddresses();
683
684         for(unsigned int i=0; i<child_count; i++)
685         {
686                 contacts_record_h child_record = NULL;
687
688                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.address, i, &child_record);
689                 if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
690                         continue;
691
692                 ContactAddressPtr address(new ContactAddress());
693
694                 char *charValue = NULL;
695                 bool boolValue = false;
696                 int intValue = 0;
697
698                 addresss->push_back(address);
699
700                 contacts_record_get_str_p(child_record, _contacts_address.postal_code, &charValue);
701                 if (charValue)
702                         address->setPostalCode(charValue);
703
704                 contacts_record_get_str_p(child_record, _contacts_address.region, &charValue);
705                 if (charValue)
706                         address->setRegion(charValue);
707
708                 contacts_record_get_str_p(child_record, _contacts_address.locality, &charValue);
709                 if (charValue)
710                         address->setCity(charValue);
711
712                 contacts_record_get_str_p(child_record, _contacts_address.street, &charValue);
713                 if (charValue)
714                         address->setStreetAddress(charValue);
715
716                 contacts_record_get_str_p(child_record, _contacts_address.extended, &charValue);
717                 if (charValue)
718                         address->setAdditionalInformation(charValue);
719
720                 contacts_record_get_str_p(child_record, _contacts_address.country, &charValue);
721                 if (charValue)
722                         address->setCountry(charValue);
723
724                 contacts_record_get_bool(child_record, _contacts_address.is_default, &boolValue);
725                 address->setIsDefault(boolValue);
726
727                 contacts_record_get_int(child_record, _contacts_address.type, &intValue);
728                 if(intValue & CONTACTS_ADDRESS_TYPE_HOME)
729                         address->addType(CONTACT_ADDRESS_TYPE_HOME);
730                 if(intValue & CONTACTS_ADDRESS_TYPE_WORK)
731                         address->addType(CONTACT_ADDRESS_TYPE_WORK);
732
733                 // TODO Will be added after type changed to string
734 //              if(intValue & CONTACTS_ADDRESS_TYPE_CUSTOM)
735 //              {
736 //                      contacts_record_get_str_p(child_record, _contacts_address.label, &charValue);
737 //                      if (charValue)
738 //                              address->addType(CUSTOM);
739 //              }
740         }
741 }
742
743 void ContactObjectP2AConverter::exportWebAddrList()
744 {
745         int errorCode = 0;
746         unsigned int child_count = 0;
747
748         if(m_abstractContact->getUrlsNum() > 0)
749                 m_abstractContact->clearUrls();
750
751         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.url, &child_count);
752         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
753                 ThrowMsg(UnknownException, "getting address list (err:" << errorCode << ")");
754
755         if(child_count == 0 || errorCode == CONTACTS_ERROR_NO_DATA)
756         {
757                 //LogDebug("Platform contact don't have address list");
758                 return;
759         }
760
761         ContactWebSiteArrayPtr urls = m_abstractContact->getUrls();
762
763         for(unsigned int i=0; i<child_count; i++)
764         {
765                 contacts_record_h child_record = NULL;
766
767                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.url, i, &child_record);
768                 if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
769                         continue;
770
771                 ContactWebSitePtr url(new ContactWebSite());
772
773                 char *charValue = NULL;
774                 int intValue = 0;
775
776                 urls->push_back(url);
777
778                 contacts_record_get_str_p(child_record, _contacts_url.url, &charValue);
779                 if (charValue)
780                         url->setUrl(charValue);
781
782                 contacts_record_get_int(child_record, _contacts_url.type, &intValue);
783                 if(intValue == CONTACTS_URL_TYPE_HOME)
784                         url->setType(WEBSITE_TYPE_HOMEPAGE);
785                 else if(intValue == CONTACTS_URL_TYPE_WORK)
786                         url->setType(WEBSITE_TYPE_BLOG);
787
788                 // TODO Will be added after type changed to string
789 //              if(intValue & CONTACTS_URL_TYPE_CUSTOM)
790 //              {
791 //                      contacts_record_get_int(child_record, _contacts_url.label, &intValue);
792 //                      if (charValue)
793 //                              url->setType(CUSTOM);
794 //              }
795         }
796 }
797
798 void ContactObjectP2AConverter::exportNicknameList()
799 {
800         int errorCode = 0;
801         unsigned int child_count = 0;
802
803         ContactNamePtr contactName = m_abstractContact->getName();
804         if(contactName == NULL)
805         {
806                 contactName = ContactNamePtr(new ContactName());
807                 m_abstractContact->setName(contactName);
808         }
809
810         if(contactName->getNicknamesNum() > 0)
811                 contactName->clearNicknames();
812
813         errorCode = contacts_record_get_child_record_count(m_platformContact, _contacts_contact.nickname, &child_count);
814         if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
815                 ThrowMsg(UnknownException, "getting nickname list (err:" << errorCode << ")");
816
817         if(child_count == 0 || errorCode == CONTACTS_ERROR_NO_DATA)
818         {
819                 //LogDebug("Platform contact don't have nickname list");
820                 return;
821         }
822
823         StringArrayPtr nicknames = contactName->getNicknames();
824
825         for(unsigned int i=0; i<child_count; i++)
826         {
827                 contacts_record_h child_record = NULL;
828
829                 errorCode = contacts_record_get_child_record_at_p(m_platformContact, _contacts_contact.nickname, i, &child_record);
830                 if(errorCode != CONTACTS_ERROR_NONE && errorCode != CONTACTS_ERROR_NO_DATA)
831                         continue;
832
833                 char *charValue = NULL;
834
835                 contacts_record_get_str_p(child_record, _contacts_nickname.name, &charValue);
836                 if (charValue)
837                         nicknames->push_back(charValue);
838         }
839 }
840
841 } // Contact
842 } // DeviceAPI