fixed DCM-1707
[framework/osp/social.git] / src / FScl_ContactImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 /**
17  * @file                FScl_ContactImpl.cpp
18  * @brief               This is the implementation for _ContactImpl class.
19  *
20  * This file contains definitions of _ContactImpl class.
21  */
22 #include <FBaseString.h>
23 #include <FBaseColArrayList.h>
24 #include <FBaseSysLog.h>
25 #include <FIoFile.h>
26 #include <FMediaImage.h>
27 #include <FSclAddress.h>
28 #include <FSclPhoneNumber.h>
29 #include <FSclEmail.h>
30 #include <FSclUrl.h>
31 #include <FSclImAddress.h>
32 #include <FSclOrganization.h>
33 #include <FSclContactEvent.h>
34 #include <FSclRelationship.h>
35 #include <FSclContact.h>
36 #include <FApp_AppInfo.h>
37 #include <FBase_StringConverter.h>
38 #include "FScl_ContactDbConnector.h"
39 #include "FScl_PhoneNumberImpl.h"
40 #include "FScl_ContactImpl.h"
41 #include "FScl_AddressbookUtil.h"
42 #include "FScl_UrlImpl.h"
43 #include "FScl_EmailImpl.h"
44 #include "FScl_AddressImpl.h"
45 #include "FScl_ImAddressImpl.h"
46 #include "FScl_OrganizationImpl.h"
47 #include "FScl_ContactEventImpl.h"
48
49 using namespace Tizen::App;
50 using namespace Tizen::Base;
51 using namespace Tizen::Base::Collection;
52 using namespace Tizen::Graphics;
53 using namespace Tizen::Media;
54 using namespace Tizen::Io;
55
56 namespace Tizen { namespace Social
57 {
58
59 extern const wchar_t OTHER_LABEL[] = L"Other";
60
61 const int __CONTACT_CHANGED_TIME_YEAR_OFFSET  = 1900;
62 const int __CONTACT_CHANGED_TIME_MONTH_OFFSET = 1;
63
64 const int __CONTACT_MOD_YEAR = 10000;
65 const int __CONTACT_MOD_MONTH = 100;
66
67 #define __PARSE_DATE(date, year, month, day)                    \
68         do                                                      \
69         {                                                       \
70                 int temp = date;                                \
71                 year = temp/__CONTACT_MOD_YEAR;                 \
72                 temp -= year*__CONTACT_MOD_YEAR;                \
73                 month = temp/__CONTACT_MOD_MONTH;               \
74                 day = temp - month*__CONTACT_MOD_MONTH;         \
75         }while(0)
76
77 #define __CONVERT_DATE_TO_DATETIME(date, dateTime)              \
78         do                                                      \
79         {                                                       \
80                 int temp = date;                                \
81                 int year = 0;                                   \
82                 int month = 0;                                  \
83                 int day = 0;                                    \
84                 year = temp/__CONTACT_MOD_YEAR;                 \
85                 temp -= year*__CONTACT_MOD_YEAR;                \
86                 month = temp/__CONTACT_MOD_MONTH;               \
87                 day = temp - month*__CONTACT_MOD_MONTH;         \
88                 dateTime.SetValue(year, month, day, 0, 0, 0);   \
89         }while(0)
90
91
92 #define __CONVERT_DATETIME_TO_DATE(dateTime, date)                                                                              \
93         do                                                                                                                      \
94         {                                                                                                                       \
95                 date = dateTime.GetYear()*__CONTACT_MOD_YEAR + dateTime.GetMonth()*__CONTACT_MOD_MONTH + dateTime.GetDay();     \
96         }while(0)
97
98 _ContactImpl::_ContactImpl(void)
99         : __contactHandle(null)
100         , __isRemoved(false)
101 {
102         contacts_record_h contactHandle = null;
103
104         SysTryReturnVoidResult(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
105
106         int ret = contacts_record_create(_contacts_contact._uri, &contactHandle);
107         SysTryReturnVoidResult(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
108
109         __contactHandle = contactHandle;
110 }
111
112 _ContactImpl::_ContactImpl(const _ContactImpl& rhs)
113         : __contactHandle(null)
114 {
115         contacts_record_h contactHandle = null;
116
117         SysTryReturnVoidResult(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
118
119         int ret = contacts_record_clone(rhs.__contactHandle, &contactHandle);
120         SysTryReturnVoidResult(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
121
122         __contactHandle = contactHandle;
123         __isRemoved = rhs.__isRemoved;
124 }
125
126 _ContactImpl::~_ContactImpl(void)
127 {
128         if (__contactHandle != null)
129         {
130                 contacts_record_destroy(__contactHandle, true);
131         }
132 }
133
134 _ContactImpl&
135 _ContactImpl::operator =(const _ContactImpl& rhs)
136 {
137         if (this == &rhs)
138         {
139                 return *this;
140         }
141
142         contacts_record_h contactHandle = null;
143
144         int ret = contacts_record_clone(rhs.__contactHandle, &contactHandle);
145         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, *this, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
146
147         contacts_record_destroy(__contactHandle, true);
148         __contactHandle = contactHandle;
149
150         __isRemoved = rhs.__isRemoved;
151
152         return *this;
153 }
154
155 void
156 _ContactImpl::SetContactRecordHandle(contacts_record_h contactHandle)
157 {
158         contacts_record_destroy(__contactHandle, true);
159
160         __contactHandle = contactHandle;
161 }
162
163 contacts_record_h
164 _ContactImpl::GetContactRecordHandle(void) const
165 {
166         return __contactHandle;
167 }
168
169 result
170 _ContactImpl::SetThumbnailPath(const Tizen::Base::String& filePath)
171 {
172         SysTryReturn(NID_SCL, filePath.IsEmpty() || File::IsFileExist(filePath), E_FILE_NOT_FOUND, E_FILE_NOT_FOUND, "[%s] The specified file is not found.", GetErrorMessage(E_FILE_NOT_FOUND));
173
174         unsigned int count = 0;
175         contacts_record_h imageHandle = null;
176
177         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.image, &count);
178         if (count > 0)
179         {
180                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.image, 0, &imageHandle);
181
182                 if (!filePath.IsEmpty())
183                 {
184                         std::unique_ptr<char[]> pCharArray( _StringConverter::CopyToCharArrayN(filePath));
185                         SysTryReturnResult(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
186
187                         contacts_record_set_str(imageHandle, _contacts_image.path, pCharArray.get());
188                 }
189                 else
190                 {
191                         contacts_record_remove_child_record(__contactHandle, _contacts_contact.image, imageHandle);
192                         contacts_record_destroy(imageHandle, true);
193                 }
194         }
195         else
196         {
197                 if (!filePath.IsEmpty())
198                 {
199                         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(filePath));
200                         SysTryReturnResult(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
201
202                         int ret = contacts_record_create(_contacts_image._uri, &imageHandle);
203                         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
204
205                         contacts_record_set_str(imageHandle, _contacts_image.path, pCharArray.get());
206
207                         contacts_record_add_child_record(__contactHandle, _contacts_contact.image, imageHandle);
208                 }
209         }
210
211         return E_SUCCESS;
212 }
213
214 String
215 _ContactImpl::GetThumbnailPath(void) const
216 {
217         unsigned int count = 0;
218
219         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.image, &count);
220         if (count == 0)
221         {
222                 return String(L"");
223         }
224
225         char* pCharValue = null;
226         contacts_record_h imageHandle = null;
227
228         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.image, 0, &imageHandle);
229         contacts_record_get_str_p(imageHandle, _contacts_image.path, &pCharValue);
230
231         return String(pCharValue);
232 }
233
234 Bitmap*
235 _ContactImpl::GetThumbnailN(void) const
236 {
237         unsigned int count = 0;
238         char* pCharValue = null;
239         contacts_record_h imageHandle = null;
240
241         ClearLastResult();
242
243         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.image, &count);
244         if (count == 0)
245         {
246                 return null;
247         }
248
249         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.image, 0, &imageHandle);
250         contacts_record_get_str_p(imageHandle, _contacts_image.path, &pCharValue);
251
252         String thumbnailPath(pCharValue);
253
254         Image image;
255         result r = image.Construct();
256         SysTryReturn(NID_SCL, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
257         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] Failed to construct Image.", GetErrorMessage(E_SYSTEM));
258
259         ImageFormat imageFormat = image.GetImageFormat(thumbnailPath);
260         SysTryReturn(NID_SCL, GetLastResult() != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
261         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, null, E_SYSTEM, "[%s] Failed to get the format of the thumbnail image.", GetErrorMessage(E_SYSTEM));
262
263         BitmapPixelFormat bitmapPixelFormat = BITMAP_PIXEL_FORMAT_RGB565;
264
265         switch (imageFormat)
266         {
267         case IMG_FORMAT_JPG:
268                 //fall through
269         case IMG_FORMAT_GIF:
270                 //fall through
271         case IMG_FORMAT_BMP:
272                 bitmapPixelFormat = BITMAP_PIXEL_FORMAT_RGB565;
273                 break;
274         case IMG_FORMAT_PNG:
275                 bitmapPixelFormat = BITMAP_PIXEL_FORMAT_ARGB8888;
276                 break;
277         default:
278                 SysLogException(NID_SCL, E_SYSTEM, "[%s] Unsupported image format.", GetErrorMessage(E_SYSTEM));
279                 return null;
280         }
281
282         Bitmap* pBitmap = image.DecodeN(thumbnailPath, bitmapPixelFormat);
283         SysTryReturn(NID_SCL, GetLastResult() != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. ", GetErrorMessage(E_OUT_OF_MEMORY));
284         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred. ", GetErrorMessage(E_SYSTEM));
285
286         return pBitmap;
287 }
288
289 result
290 _ContactImpl::GetValue(const ContactPropertyId id, String& value) const
291 {
292         unsigned int count = 0;
293         char* pCharValue = null;
294
295         switch (id)
296         {
297         case CONTACT_PROPERTY_ID_FIRST_NAME:
298                 {
299                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.name, &count);
300                         if (count > 0)
301                         {
302                                 contacts_record_h nameHandle = null;
303
304                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.name, 0, &nameHandle);
305                                 contacts_record_get_str_p(nameHandle, _contacts_name.first, &pCharValue);
306                         }
307
308                         value = pCharValue;
309                 }
310                 break;
311         case CONTACT_PROPERTY_ID_LAST_NAME:
312                 {
313                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.name, &count);
314                         if (count > 0)
315                         {
316                                 contacts_record_h nameHandle = null;
317
318                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.name, 0, &nameHandle);
319                                 contacts_record_get_str_p(nameHandle, _contacts_name.last, &pCharValue);
320                         }
321
322                         value = pCharValue;
323                 }
324                 break;
325         case CONTACT_PROPERTY_ID_DISPLAY_NAME:
326                         contacts_record_get_str_p(__contactHandle, _contacts_contact.display_name, &pCharValue);
327                         value = pCharValue;
328                 break;
329         case CONTACT_PROPERTY_ID_NICK_NAME:
330                 {
331                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.nickname, &count);
332                         if (count > 0)
333                         {
334                                 contacts_record_h nicknameHandle = null;
335
336                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.nickname, 0, &nicknameHandle);
337                                 contacts_record_get_str_p(nicknameHandle, _contacts_nickname.name, &pCharValue);
338                         }
339
340                         value = pCharValue;
341                 }
342                 break;
343         case CONTACT_PROPERTY_ID_MIDDLE_NAME:
344                 {
345                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.name, &count);
346                         if (count > 0)
347                         {
348                                 contacts_record_h nameHandle = null;
349
350                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.name, 0, &nameHandle);
351                                 contacts_record_get_str_p(nameHandle, _contacts_name.addition, &pCharValue);
352                         }
353
354                         value = pCharValue;
355                 }
356                 break;
357         case CONTACT_PROPERTY_ID_NAME_PREFIX:
358                 {
359                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.name, &count);
360                         if (count > 0)
361                         {
362                                 contacts_record_h nameHandle = null;
363
364                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.name, 0, &nameHandle);
365                                 contacts_record_get_str_p(nameHandle, _contacts_name.prefix, &pCharValue);
366                         }
367
368                         value = pCharValue;
369                 }
370                 break;
371         case CONTACT_PROPERTY_ID_NAME_SUFFIX:
372                 {
373                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.name, &count);
374                         if (count > 0)
375                         {
376                                 contacts_record_h nameHandle = null;
377
378                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.name, 0, &nameHandle);
379                                 contacts_record_get_str_p(nameHandle, _contacts_name.suffix, &pCharValue);
380                         }
381
382                         value = pCharValue;
383                 }
384                 break;
385         case CONTACT_PROPERTY_ID_JOB_TITLE:
386                 {
387                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.company, &count);
388                         if (count > 0)
389                         {
390                                 contacts_record_h companyHandle = null;
391
392                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.company, 0, &companyHandle);
393                                 contacts_record_get_str_p(companyHandle, _contacts_company.job_title, &pCharValue);
394                         }
395
396                         value = pCharValue;
397                 }
398                 break;
399         case CONTACT_PROPERTY_ID_COMPANY:
400                 {
401                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.company, &count);
402                         if (count > 0)
403                         {
404                                 contacts_record_h companyHandle = null;
405
406                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.company, 0, &companyHandle);
407                                 contacts_record_get_str_p(companyHandle, _contacts_company.name, &pCharValue);
408                         }
409
410                         value = pCharValue;
411                 }
412                 break;
413         case CONTACT_PROPERTY_ID_NOTE:
414                 {
415                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.note, &count);
416                         if (count > 0)
417                         {
418                                 contacts_record_h noteHandle = null;
419
420                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.note, 0, &noteHandle);
421                                 contacts_record_get_str_p(noteHandle, _contacts_note.note, &pCharValue);
422                         }
423
424                         value = pCharValue;
425                 }
426                 break;
427         case CONTACT_PROPERTY_ID_RINGTONE:
428                 contacts_record_get_str_p(__contactHandle, _contacts_contact.ringtone_path, &pCharValue);
429                 value = pCharValue;
430                 break;
431         case CONTACT_PROPERTY_ID_THUMBNAIL:
432                 {
433                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.image, &count);
434                         if (count > 0)
435                         {
436                                 contacts_record_h imageHandle = null;
437
438                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.image, 0, &imageHandle);
439                                 contacts_record_get_str_p(imageHandle, _contacts_image.path, &pCharValue);
440                         }
441                 }
442                 value = pCharValue;
443                 break;
444         case CONTACT_PROPERTY_ID_PHONETIC_FIRST_NAME:
445                 {
446                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.name, &count);
447                         if (count > 0)
448                         {
449                                 contacts_record_h phoneticNameHandle = null;
450
451                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.name, 0, &phoneticNameHandle);
452                                 contacts_record_get_str_p(phoneticNameHandle, _contacts_name.phonetic_first, &pCharValue);
453                         }
454
455                         value = pCharValue;
456                 }
457                 break;
458         case CONTACT_PROPERTY_ID_PHONETIC_LAST_NAME:
459                 {
460                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.name, &count);
461                         if (count > 0)
462                         {
463                                 contacts_record_h phoneticNameHandle = null;
464
465                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.name, 0, &phoneticNameHandle);
466                                 contacts_record_get_str_p(phoneticNameHandle, _contacts_name.phonetic_last, &pCharValue);
467                         }
468
469                         value = pCharValue;
470                 }
471                 break;
472         case CONTACT_PROPERTY_ID_PHONETIC_MIDDLE_NAME:
473                 {
474                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.name, &count);
475                         if (count > 0)
476                         {
477                                 contacts_record_h phoneticNameHandle = null;
478
479                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.name, 0, &phoneticNameHandle);
480                                 contacts_record_get_str_p(phoneticNameHandle, _contacts_name.phonetic_middle, &pCharValue);
481                         }
482
483                         value = pCharValue;
484                 }
485                 break;
486         case CONTACT_PROPERTY_ID_UID:
487                 {
488                         contacts_record_get_str_p(__contactHandle, _contacts_contact.uid, &pCharValue);
489                         value = pCharValue;
490                 }
491                 break;
492
493         default:
494                 value = String(L"");
495                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. id=%d", GetErrorMessage(E_INVALID_ARG), id);
496                 return E_INVALID_ARG;
497         }
498
499         return E_SUCCESS;
500 }
501
502 result
503 _ContactImpl::GetValue(const ContactPropertyId id, DateTime& value) const
504 {
505         int intValue = 0;
506         DateTime dataTime;
507         unsigned int count = 0;
508
509         value = DateTime::GetMinValue();
510
511         switch (id)
512         {
513         case CONTACT_PROPERTY_ID_BIRTHDAY:
514                 {
515                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.event, &count);
516                         if (count > 0)
517                         {
518                                 int year = 0;
519                                 int month = 0;
520                                 int day = 0;
521
522                                 contacts_record_h eventHandle = null;
523                                 for (unsigned int i = 0; i < count; i++)
524                                 {
525                                         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.event, i, &eventHandle);
526                                         contacts_record_get_int(eventHandle, _contacts_event.type, &intValue);
527                                         if (intValue == CONTACTS_EVENT_TYPE_BIRTH)
528                                         {
529                                                 contacts_record_get_int(eventHandle, _contacts_event.date, &intValue);
530
531                                                 __PARSE_DATE(intValue, year, month, day);
532
533                                                 value.SetValue(year, month, day, 0, 0, 0);
534                                                 break;
535                                         }
536                                 }
537                         }
538                 }
539                 break;
540
541         case CONTACT_PROPERTY_ID_ANNIVERSARY:
542                 {
543                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.event, &count);
544                         if (count > 0)
545                         {
546                                 int year = 0;
547                                 int month = 0;
548                                 int day = 0;
549
550                                 contacts_record_h eventHandle = null;
551                                 for (unsigned int i = 0; i < count; i++)
552                                 {
553                                         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.event, i, &eventHandle);
554                                         contacts_record_get_int(eventHandle, _contacts_event.type, &intValue);
555                                         if (intValue == CONTACTS_EVENT_TYPE_ANNIVERSARY)
556                                         {
557                                                 contacts_record_get_int(eventHandle, _contacts_event.date, &intValue);
558
559                                                 __PARSE_DATE(intValue, year, month, day);
560
561                                                 value.SetValue(year, month, day, 0, 0, 0);
562                                                 break;
563                                         }
564                                 }
565                         }
566                 }
567                 break;
568
569         case CONTACT_PROPERTY_ID_LAST_REVISION:
570                 {
571                         struct tm ts;
572                         int intValue = 0;
573
574                         contacts_record_get_int(__contactHandle, _contacts_contact.changed_time, &intValue);
575                         gmtime_r((time_t *)intValue, &ts);
576
577                         value.SetValue(ts.tm_year + __CONTACT_CHANGED_TIME_YEAR_OFFSET, ts.tm_mon + __CONTACT_CHANGED_TIME_MONTH_OFFSET, ts.tm_mday, ts.tm_hour, ts.tm_min, ts.tm_sec);
578                 }
579                 break;
580
581         default:
582                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. id=%d.", GetErrorMessage(E_INVALID_ARG), id);
583                 return E_INVALID_ARG;
584         }
585
586         return E_SUCCESS;
587 }
588
589 bool
590 _ContactImpl::IsEmptyCompany(contacts_record_h companyHandle)
591 {
592         char* pCharValue = null;
593
594         contacts_record_get_str_p(companyHandle, _contacts_company.name, &pCharValue);
595         if (pCharValue != null)
596         {
597                 return false;
598         }
599
600         contacts_record_get_str_p(companyHandle, _contacts_company.job_title, &pCharValue);
601         if (pCharValue != null)
602         {
603                 return false;
604         }
605
606         contacts_record_get_str_p(companyHandle, _contacts_company.department, &pCharValue);
607         if (pCharValue != null)
608         {
609                 return false;
610         }
611
612         contacts_record_get_str_p(companyHandle, _contacts_company.role, &pCharValue);
613         if (pCharValue != null)
614         {
615                 return false;
616         }
617
618         contacts_record_get_str_p(companyHandle, _contacts_company.assistant_name, &pCharValue);
619         if (pCharValue != null)
620         {
621                 return false;
622         }
623
624         contacts_record_get_str_p(companyHandle, _contacts_company.description, &pCharValue);
625         if (pCharValue != null)
626         {
627                 return false;
628         }
629
630         contacts_record_get_str_p(companyHandle, _contacts_company.location, &pCharValue);
631         if (pCharValue != null)
632         {
633                 return false;
634         }
635
636         contacts_record_get_str_p(companyHandle, _contacts_company.phonetic_name, &pCharValue);
637         if (pCharValue != null)
638         {
639                 return false;
640         }
641
642         contacts_record_get_str_p(companyHandle, _contacts_company.logo, &pCharValue);
643         if (pCharValue != null)
644         {
645                 return false;
646         }
647
648         return true;
649 }
650
651 bool
652 _ContactImpl::IsEmptyName(contacts_record_h nameHandle)
653 {
654         char* pCharValue = null;
655
656         contacts_record_get_str_p(nameHandle, _contacts_name.first, &pCharValue);
657         if (pCharValue != null)
658         {
659                 return false;
660         }
661
662         contacts_record_get_str_p(nameHandle, _contacts_name.last, &pCharValue);
663         if (pCharValue != null)
664         {
665                 return false;
666         }
667
668         contacts_record_get_str_p(nameHandle, _contacts_name.addition, &pCharValue);
669         if (pCharValue != null)
670         {
671                 return false;
672         }
673
674         contacts_record_get_str_p(nameHandle, _contacts_name.prefix, &pCharValue);
675         if (pCharValue != null)
676         {
677                 return false;
678         }
679
680         contacts_record_get_str_p(nameHandle, _contacts_name.suffix, &pCharValue);
681         if (pCharValue != null)
682         {
683                 return false;
684         }
685
686         contacts_record_get_str_p(nameHandle, _contacts_name.phonetic_first, &pCharValue);
687         if (pCharValue != null)
688         {
689                 return false;
690         }
691
692         contacts_record_get_str_p(nameHandle, _contacts_name.phonetic_last, &pCharValue);
693         if (pCharValue != null)
694         {
695                 return false;
696         }
697
698         contacts_record_get_str_p(nameHandle, _contacts_name.phonetic_middle, &pCharValue);
699         if (pCharValue != null)
700         {
701                 return false;
702         }
703
704         return true;
705 }
706
707 result
708 _ContactImpl::SetValue(ContactPropertyId id, const String& value)
709 {
710         SysTryReturn(NID_SCL, id != CONTACT_PROPERTY_ID_THUMBNAIL, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. Thumbnail cannot be set using this.", GetErrorMessage(E_INVALID_ARG));
711         SysTryReturn(NID_SCL, id != CONTACT_PROPERTY_ID_DISPLAY_NAME, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. Display cannot be set using this.", GetErrorMessage(E_INVALID_ARG));
712
713         int ret = CONTACTS_ERROR_NONE;
714         unsigned int count = 0;
715
716         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
717         {
718                 int maxLength = GetMaxLength(id);
719                 SysTryReturn(NID_SCL, value.GetLength() <= maxLength || maxLength == -1 , E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The length of the value exceeds the maximum length.", GetErrorMessage(E_INVALID_ARG));
720         }
721
722         switch (id)
723         {
724         case CONTACT_PROPERTY_ID_FIRST_NAME:
725                 {
726                         contacts_record_h nameHandle = null;
727                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.name, &count);
728                         if (count > 0)
729                         {
730                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.name, 0, &nameHandle);
731
732                                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(value));
733                                 contacts_record_set_str(nameHandle, _contacts_name.first, pCharArray.get());
734
735                                 if (value.IsEmpty() && IsEmptyName(nameHandle))
736                                 {
737                                         contacts_record_remove_child_record(__contactHandle, _contacts_contact.name, nameHandle);
738                                         contacts_record_destroy(nameHandle, true);
739                                 }
740                         }
741                         else
742                         {
743                                 if (!value.IsEmpty())
744                                 {
745                                         ret = contacts_record_create(_contacts_name._uri, &nameHandle);
746                                         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
747
748                                         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(value));
749                                         contacts_record_set_str(nameHandle, _contacts_name.first, pCharArray.get());
750
751                                         contacts_record_add_child_record(__contactHandle, _contacts_contact.name, nameHandle);
752                                 }
753                         }
754                 }
755                 break;
756
757         case CONTACT_PROPERTY_ID_MIDDLE_NAME:
758                 {
759                         contacts_record_h nameHandle = null;
760                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.name, &count);
761                         if (count > 0)
762                         {
763                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.name, 0, &nameHandle);
764
765                                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(value));
766                                 contacts_record_set_str(nameHandle, _contacts_name.addition, pCharArray.get());
767
768                                 if (value.IsEmpty() && IsEmptyName(nameHandle))
769                                 {
770                                         contacts_record_remove_child_record(__contactHandle, _contacts_contact.name, nameHandle);
771                                         contacts_record_destroy(nameHandle, true);
772                                 }
773                         }
774                         else
775                         {
776                                 if (!value.IsEmpty())
777                                 {
778                                         ret = contacts_record_create(_contacts_name._uri, &nameHandle);
779                                         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
780
781                                         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(value));
782                                         contacts_record_set_str(nameHandle, _contacts_name.addition, pCharArray.get());
783
784                                         contacts_record_add_child_record(__contactHandle, _contacts_contact.name, nameHandle);
785                                 }
786                         }
787
788                 }
789
790                 break;
791
792         case CONTACT_PROPERTY_ID_LAST_NAME:
793                 {
794                         contacts_record_h nameHandle = null;
795                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.name, &count);
796                         if (count > 0)
797                         {
798                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.name, 0, &nameHandle);
799
800                                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(value));
801                                 contacts_record_set_str(nameHandle, _contacts_name.last, pCharArray.get());
802
803                                 if (value.IsEmpty() && IsEmptyName(nameHandle))
804                                 {
805                                         contacts_record_remove_child_record(__contactHandle, _contacts_contact.name, nameHandle);
806                                         contacts_record_destroy(nameHandle, true);
807                                 }
808                         }
809                         else
810                         {
811                                 if (!value.IsEmpty())
812                                 {
813                                         ret = contacts_record_create(_contacts_name._uri, &nameHandle);
814                                         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
815
816                                         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(value));
817                                         contacts_record_set_str(nameHandle, _contacts_name.last, pCharArray.get());
818
819                                         contacts_record_add_child_record(__contactHandle, _contacts_contact.name, nameHandle);
820                                 }
821                         }
822
823                 }
824
825                 break;
826         case CONTACT_PROPERTY_ID_NAME_SUFFIX:
827                 {
828                         contacts_record_h nameHandle = null;
829                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.name, &count);
830                         if (count > 0)
831                         {
832                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.name, 0, &nameHandle);
833
834                                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(value));
835                                 contacts_record_set_str(nameHandle, _contacts_name.suffix, pCharArray.get());
836
837                                 if (value.IsEmpty() && IsEmptyName(nameHandle))
838                                 {
839                                         contacts_record_remove_child_record(__contactHandle, _contacts_contact.name, nameHandle);
840                                         contacts_record_destroy(nameHandle, true);
841                                 }
842
843                         }
844                         else
845                         {
846                                 if (!value.IsEmpty())
847                                 {
848                                         ret = contacts_record_create(_contacts_name._uri, &nameHandle);
849
850                                         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
851
852                                         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(value));
853                                         contacts_record_set_str(nameHandle, _contacts_name.suffix, pCharArray.get());
854
855                                         contacts_record_add_child_record(__contactHandle, _contacts_contact.name, nameHandle);
856                                 }
857                         }
858
859                 }
860
861                 break;
862
863         case CONTACT_PROPERTY_ID_NAME_PREFIX:
864                 {
865                         contacts_record_h nameHandle = null;
866                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.name, &count);
867                         if (count > 0)
868                         {
869                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.name, 0, &nameHandle);
870
871                                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(value));
872                                 contacts_record_set_str(nameHandle, _contacts_name.prefix, pCharArray.get());
873
874                                 if (value.IsEmpty() && IsEmptyName(nameHandle))
875                                 {
876                                         contacts_record_remove_child_record(__contactHandle, _contacts_contact.name, nameHandle);
877                                         contacts_record_destroy(nameHandle, true);
878                                 }
879                         }
880                         else
881                         {
882                                 if (!value.IsEmpty())
883                                 {
884                                         ret = contacts_record_create(_contacts_name._uri, &nameHandle);
885                                         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
886
887                                         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(value));
888                                         contacts_record_set_str(nameHandle, _contacts_name.prefix, pCharArray.get());
889
890                                         contacts_record_add_child_record(__contactHandle, _contacts_contact.name, nameHandle);
891                                 }
892                         }
893
894                 }
895
896                 break;
897
898         case CONTACT_PROPERTY_ID_NICK_NAME:
899                 {
900                         contacts_record_h nicknameHandle = null;
901                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.nickname, &count);
902                         if (count > 0)
903                         {
904                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.nickname, 0, &nicknameHandle);
905                                 if (!value.IsEmpty())
906                                 {
907                                         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(value));
908                                         contacts_record_set_str(nicknameHandle, _contacts_nickname.name, pCharArray.get());
909                                 }
910                                 else
911                                 {
912                                         while (true)
913                                         {
914                                                 ret = contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.nickname, 0, &nicknameHandle);
915                                                 if (ret != CONTACTS_ERROR_NONE)
916                                                 {
917                                                         break;
918                                                 }
919
920                                                 contacts_record_remove_child_record(__contactHandle, _contacts_contact.nickname, nicknameHandle);
921                                                 contacts_record_destroy(nicknameHandle, true);
922                                         }
923                                 }
924                         }
925                         else
926                         {
927                                 if (!value.IsEmpty())
928                                 {
929                                         ret = contacts_record_create(_contacts_nickname._uri, &nicknameHandle);
930                                         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
931
932                                         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(value));
933                                         contacts_record_set_str(nicknameHandle, _contacts_nickname.name, pCharArray.get());
934
935                                         contacts_record_add_child_record(__contactHandle, _contacts_contact.nickname, nicknameHandle);
936                                 }
937                         }
938
939                 }
940
941                 break;
942
943         case CONTACT_PROPERTY_ID_JOB_TITLE:
944                 {
945                         contacts_record_h companyHandle = null;
946                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.company, &count);
947                         if (count > 0)
948                         {
949                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.company, 0, &companyHandle);
950
951                                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(value));
952                                 contacts_record_set_str(companyHandle, _contacts_company.job_title, pCharArray.get());
953
954                                 if (value.IsEmpty() && IsEmptyCompany(companyHandle))
955                                 {
956                                         while (true)
957                                         {
958                                                 ret = contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.company, 0, &companyHandle);
959                                                 if (ret != CONTACTS_ERROR_NONE)
960                                                 {
961                                                         break;
962                                                 }
963
964                                                 contacts_record_remove_child_record(__contactHandle, _contacts_contact.company, companyHandle);
965                                                 contacts_record_destroy(companyHandle, true);
966                                         }
967                                 }
968                         }
969                         else
970                         {
971                                 if (!value.IsEmpty())
972                                 {
973                                         ret = contacts_record_create(_contacts_company._uri, &companyHandle);
974                                         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
975
976                                         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(value));
977                                         contacts_record_set_str(companyHandle, _contacts_company.job_title, pCharArray.get());
978
979                                         contacts_record_add_child_record(__contactHandle, _contacts_contact.company, companyHandle);
980                                 }
981                         }
982
983                 }
984                 break;
985
986         case CONTACT_PROPERTY_ID_COMPANY:
987                 {
988                         contacts_record_h companyHandle = null;
989                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.company, &count);
990                         if (count > 0)
991                         {
992                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.company, 0, &companyHandle);
993
994                                 std::unique_ptr<char[]> pCharArray( _StringConverter::CopyToCharArrayN(value));
995                                 contacts_record_set_str(companyHandle, _contacts_company.name, pCharArray.get());
996
997                                 if (value.IsEmpty() && IsEmptyCompany(companyHandle))
998                                 {
999                                         while (true)
1000                                         {
1001                                                 ret = contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.company, 0, &companyHandle);
1002                                                 if (ret != CONTACTS_ERROR_NONE)
1003                                                 {
1004                                                         break;
1005                                                 }
1006
1007                                                 contacts_record_remove_child_record(__contactHandle, _contacts_contact.company, companyHandle);
1008                                                 contacts_record_destroy(companyHandle, true);
1009                                         }
1010
1011                                 }
1012                         }
1013                         else
1014                         {
1015                                 if (!value.IsEmpty())
1016                                 {
1017                                         ret = contacts_record_create(_contacts_company._uri, &companyHandle);
1018                                         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1019
1020                                         std::unique_ptr<char[]> pCharArray( _StringConverter::CopyToCharArrayN(value));
1021                                         contacts_record_set_str(companyHandle, _contacts_company.name, pCharArray.get());
1022
1023                                         contacts_record_add_child_record(__contactHandle, _contacts_contact.company, companyHandle);
1024                                 }
1025                         }
1026
1027                 }
1028                 break;
1029
1030         case CONTACT_PROPERTY_ID_NOTE:
1031                 {
1032                         contacts_record_h noteHandle = null;
1033                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.note, &count);
1034                         if (count > 0)
1035                         {
1036                                 if (!value.IsEmpty())
1037                                 {
1038                                         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.note, 0, &noteHandle);
1039
1040                                         std::unique_ptr<char[]> pCharArray( _StringConverter::CopyToCharArrayN(value));
1041                                         contacts_record_set_str(noteHandle, _contacts_note.note, pCharArray.get());
1042                                 }
1043                                 else
1044                                 {
1045                                         while (true)
1046                                         {
1047                                                 ret = contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.note, 0, &noteHandle);
1048                                                 if (ret != CONTACTS_ERROR_NONE)
1049                                                 {
1050                                                         break;
1051                                                 }
1052
1053                                                 contacts_record_remove_child_record(__contactHandle, _contacts_contact.note, noteHandle);
1054                                                 contacts_record_destroy(noteHandle, true);
1055                                         }
1056                                 }
1057                         }
1058                         else
1059                         {
1060                                 if (!value.IsEmpty())
1061                                 {
1062                                         ret = contacts_record_create(_contacts_note._uri, &noteHandle);
1063                                         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1064
1065                                         std::unique_ptr<char[]> pCharArray( _StringConverter::CopyToCharArrayN(value));
1066                                         contacts_record_set_str(noteHandle, _contacts_note.note, pCharArray.get());
1067
1068                                         contacts_record_add_child_record(__contactHandle, _contacts_contact.note, noteHandle);
1069                                 }
1070                         }
1071                 }
1072                 break;
1073
1074         case CONTACT_PROPERTY_ID_RINGTONE:
1075         {
1076                 if (!value.IsEmpty() && !File::IsFileExist(value))
1077                 {
1078                         SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. The specified ringtone file does not exist.", GetErrorMessage(E_INVALID_ARG));
1079                         return E_INVALID_ARG;
1080                 }
1081
1082                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(value));
1083
1084                 contacts_record_set_str(__contactHandle, _contacts_contact.ringtone_path, pCharArray.get());
1085         }
1086         break;
1087
1088         case CONTACT_PROPERTY_ID_PHONETIC_FIRST_NAME:
1089                 {
1090                         contacts_record_h phoneticNameHandle = null;
1091                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.name, &count);
1092                         if (count > 0)
1093                         {
1094                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.name, 0, &phoneticNameHandle);
1095
1096                                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(value));
1097                                 contacts_record_set_str(phoneticNameHandle, _contacts_name.phonetic_first, pCharArray.get());
1098
1099                                 if (value.IsEmpty() && IsEmptyName(phoneticNameHandle))
1100                                 {
1101                                         contacts_record_remove_child_record(__contactHandle, _contacts_contact.name, phoneticNameHandle);
1102                                         contacts_record_destroy(phoneticNameHandle, true);
1103                                 }
1104                         }
1105                         else
1106                         {
1107                                 if (!value.IsEmpty())
1108                                 {
1109                                         ret = contacts_record_create(_contacts_name._uri, &phoneticNameHandle);
1110                                         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1111
1112                                         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(value));
1113                                         contacts_record_set_str(phoneticNameHandle, _contacts_name.phonetic_first, pCharArray.get());
1114
1115                                         contacts_record_add_child_record(__contactHandle, _contacts_contact.name, phoneticNameHandle);
1116                                 }
1117                         }
1118                 }
1119                 break;
1120         case CONTACT_PROPERTY_ID_PHONETIC_LAST_NAME:
1121                 {
1122                         contacts_record_h phoneticNameHandle = null;
1123                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.name, &count);
1124                         if (count > 0)
1125                         {
1126                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.name, 0, &phoneticNameHandle);
1127
1128                                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(value));
1129                                 contacts_record_set_str(phoneticNameHandle, _contacts_name.phonetic_last, pCharArray.get());
1130
1131                                 if (value.IsEmpty() && IsEmptyName(phoneticNameHandle))
1132                                 {
1133                                         contacts_record_remove_child_record(__contactHandle, _contacts_contact.name, phoneticNameHandle);
1134                                         contacts_record_destroy(phoneticNameHandle, true);
1135                                 }
1136                         }
1137                         else
1138                         {
1139                                 if (!value.IsEmpty())
1140                                 {
1141                                         ret = contacts_record_create(_contacts_name._uri, &phoneticNameHandle);
1142                                         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1143
1144                                         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(value));
1145                                         contacts_record_set_str(phoneticNameHandle, _contacts_name.phonetic_last, pCharArray.get());
1146
1147                                         contacts_record_add_child_record(__contactHandle, _contacts_contact.name, phoneticNameHandle);
1148                                 }
1149                         }
1150                 }
1151                 break;
1152         case CONTACT_PROPERTY_ID_PHONETIC_MIDDLE_NAME:
1153                 {
1154                         contacts_record_h phoneticNameHandle = null;
1155                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.name, &count);
1156                         if (count > 0)
1157                         {
1158                                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.name, 0, &phoneticNameHandle);
1159
1160                                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(value));
1161                                 contacts_record_set_str(phoneticNameHandle, _contacts_name.phonetic_middle, pCharArray.get());
1162
1163                                 if (value.IsEmpty() && IsEmptyName(phoneticNameHandle))
1164                                 {
1165                                         contacts_record_remove_child_record(__contactHandle, _contacts_contact.name, phoneticNameHandle);
1166                                         contacts_record_destroy(phoneticNameHandle, true);
1167                                 }
1168                         }
1169                         else
1170                         {
1171                                 if (!value.IsEmpty())
1172                                 {
1173                                         ret = contacts_record_create(_contacts_name._uri, &phoneticNameHandle);
1174                                         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1175
1176                                         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(value));
1177                                         contacts_record_set_str(phoneticNameHandle, _contacts_name.phonetic_middle, pCharArray.get());
1178
1179                                         contacts_record_add_child_record(__contactHandle, _contacts_contact.name, phoneticNameHandle);
1180                                 }
1181                         }
1182                 }
1183                 break;
1184         case CONTACT_PROPERTY_ID_UID:
1185                 {
1186                         if (!value.IsEmpty())
1187                         {
1188                                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(value));
1189                                 contacts_record_set_str(__contactHandle, _contacts_contact.uid, pCharArray.get());
1190                         }
1191                         else
1192                         {
1193                                 contacts_record_set_str(__contactHandle, _contacts_contact.uid, null);
1194                         }
1195                 }
1196                 break;
1197         default:
1198                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. id=%d.", GetErrorMessage(E_INVALID_ARG), id);
1199                 return E_INVALID_ARG;
1200         }
1201
1202         return E_SUCCESS;
1203 }
1204
1205 result
1206 _ContactImpl::SetValue(ContactPropertyId id, const DateTime& value)
1207 {
1208         SysTryReturn(NID_SCL, id != CONTACT_PROPERTY_ID_LAST_REVISION, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. Thumbnail cannot be set using this.", GetErrorMessage(E_INVALID_ARG));
1209
1210         unsigned int count = 0;
1211         int intValue = 0;
1212         int ret = CONTACTS_ERROR_NONE;
1213
1214         switch (id)
1215         {
1216         case CONTACT_PROPERTY_ID_BIRTHDAY:
1217                 {
1218                         bool found = false;
1219                         int date = 0;
1220                         contacts_record_h eventHandle = null;
1221
1222                         __CONVERT_DATETIME_TO_DATE(value, date);
1223
1224                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.event, &count);
1225                         if (count > 0)
1226                         {
1227                                 for (unsigned int i = 0; i < count; i++)
1228                                 {
1229                                         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.event, i, &eventHandle);
1230                                         contacts_record_get_int(eventHandle, _contacts_event.type, &intValue);
1231                                         if (intValue == CONTACTS_EVENT_TYPE_BIRTH)
1232                                         {
1233                                                 contacts_record_set_int(eventHandle, _contacts_event.date, date);
1234                                                 found = true;
1235                                                 break;
1236                                         }
1237                                 }
1238                         }
1239
1240                         if (!found)
1241                         {
1242                                 ret = contacts_record_create(_contacts_event._uri, &eventHandle);
1243                                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1244
1245                                 contacts_record_set_int(eventHandle, _contacts_event.type, CONTACTS_EVENT_TYPE_BIRTH);
1246                                 contacts_record_set_int(eventHandle, _contacts_event.date, date);
1247
1248                                 contacts_record_add_child_record(__contactHandle, _contacts_contact.event, eventHandle);
1249                         }
1250                 }
1251
1252                 break;
1253
1254         case CONTACT_PROPERTY_ID_ANNIVERSARY:
1255                 {
1256                         bool found = false;
1257                         int date = 0;
1258                         contacts_record_h eventHandle = null;
1259
1260                         __CONVERT_DATETIME_TO_DATE(value, date);
1261
1262                         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.event, &count);
1263                         if (count > 0)
1264                         {
1265                                 for (unsigned int i = 0; i < count; i++)
1266                                 {
1267                                         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.event, i, &eventHandle);
1268                                         contacts_record_get_int(eventHandle, _contacts_event.type, &intValue);
1269                                         if (intValue == CONTACTS_EVENT_TYPE_ANNIVERSARY)
1270                                         {
1271                                                 contacts_record_set_int(eventHandle, _contacts_event.date, date);
1272                                                 found = true;
1273                                                 break;
1274                                         }
1275                                 }
1276                         }
1277
1278                         if (!found)
1279                         {
1280                                 ret = contacts_record_create(_contacts_event._uri, &eventHandle);
1281                                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1282
1283                                 contacts_record_set_int(eventHandle, _contacts_event.type, CONTACTS_EVENT_TYPE_ANNIVERSARY);
1284                                 contacts_record_set_int(eventHandle, _contacts_event.date, date);
1285
1286                                 contacts_record_add_child_record(__contactHandle, _contacts_contact.event, eventHandle);
1287                         }
1288                 }
1289
1290                 break;
1291
1292         default:
1293                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. id=%d.", GetErrorMessage(E_INVALID_ARG), id);
1294                 return E_INVALID_ARG;
1295         }
1296
1297         return E_SUCCESS;
1298 }
1299
1300 result
1301 _ContactImpl::AddPhoneNumber(const PhoneNumber& phoneNumber)
1302 {
1303         SysTryReturn(NID_SCL, !phoneNumber.GetPhoneNumber().IsEmpty(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The phoneNumber is empty.", GetErrorMessage(E_INVALID_ARG));
1304         SysTryReturn(NID_SCL, !__isRemoved || Invalidate() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1305
1306         int type = 0;
1307         String stringValue;
1308         contacts_record_h numberHandle = null;
1309
1310         int ret = contacts_record_create(_contacts_number._uri, &numberHandle);
1311         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1312
1313         __ContactsRecordHandle recordHandle(numberHandle);
1314
1315         switch (_PhoneNumberImpl::GetInstance(phoneNumber)->GetType())
1316         {
1317                 case PHONENUMBER_TYPE_HOME:
1318                         type = CONTACTS_NUMBER_TYPE_HOME | CONTACTS_NUMBER_TYPE_VOICE;
1319                         break;
1320                 case PHONENUMBER_TYPE_WORK:
1321                         type = CONTACTS_NUMBER_TYPE_WORK | CONTACTS_NUMBER_TYPE_VOICE;
1322                         break;
1323                 case PHONENUMBER_TYPE_MOBILE:
1324                         type = CONTACTS_NUMBER_TYPE_CELL;
1325                         break;
1326                 case PHONENUMBER_TYPE_HOME_FAX:
1327                         type = CONTACTS_NUMBER_TYPE_FAX | CONTACTS_NUMBER_TYPE_HOME;
1328                         break;
1329                 case PHONENUMBER_TYPE_WORK_FAX:
1330                         type = CONTACTS_NUMBER_TYPE_FAX | CONTACTS_NUMBER_TYPE_WORK;
1331                         break;
1332                 case PHONENUMBER_TYPE_PAGER:
1333                         type = CONTACTS_NUMBER_TYPE_PAGER;
1334                         break;
1335                 case PHONENUMBER_TYPE_CUSTOM:
1336                         type = CONTACTS_NUMBER_TYPE_CUSTOM;
1337                         break;
1338                 case PHONENUMBER_TYPE_ASSISTANT:
1339                         type = CONTACTS_NUMBER_TYPE_ASSISTANT;
1340                         break;
1341                 case PHONENUMBER_TYPE_OTHER:
1342                 default:
1343                         type = CONTACTS_NUMBER_TYPE_OTHER;
1344                         break;
1345         }
1346
1347         contacts_record_set_int(numberHandle, _contacts_number.type, type);
1348
1349         stringValue = _PhoneNumberImpl::GetInstance(phoneNumber)->GetLabel();
1350
1351         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
1352         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1353
1354         contacts_record_set_str(numberHandle, _contacts_number.label, pCharArray.get());
1355
1356         stringValue = _PhoneNumberImpl::GetInstance(phoneNumber)->GetPhoneNumber();
1357         pCharArray.reset(_StringConverter::CopyToCharArrayN(stringValue));
1358         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1359
1360         contacts_record_set_str(numberHandle, _contacts_number.number, pCharArray.get());
1361
1362         ret = contacts_record_add_child_record(__contactHandle, _contacts_contact.number, numberHandle);
1363         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1364
1365         recordHandle.Release();
1366
1367         return E_SUCCESS;
1368 }
1369
1370 result
1371 _ContactImpl::AddNickname(const String& nickname)
1372 {
1373         SysTryReturn(NID_SCL, !nickname.IsEmpty(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The nickname is an empty string.", GetErrorMessage(E_INVALID_ARG));
1374         SysTryReturn(NID_SCL, !__isRemoved || !IsFailed(Invalidate()), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1375         contacts_record_h nicknameHandle = null;
1376
1377         int ret = contacts_record_create(_contacts_nickname._uri, &nicknameHandle);
1378         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1379
1380         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(nickname));
1381         SysTryReturnResult(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1382
1383         contacts_record_set_str(nicknameHandle, _contacts_nickname.name, pCharArray.get());
1384
1385         ret = contacts_record_add_child_record(__contactHandle, _contacts_contact.nickname, nicknameHandle);
1386         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1387
1388         return E_SUCCESS;
1389 }
1390
1391 result
1392 _ContactImpl::AddNote(const String& note)
1393 {
1394         SysTryReturn(NID_SCL, !note.IsEmpty(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The note is an empty string.", GetErrorMessage(E_INVALID_ARG));
1395         SysTryReturn(NID_SCL, !__isRemoved || !IsFailed(Invalidate()), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1396         contacts_record_h noteHandle = null;
1397
1398         int ret = contacts_record_create(_contacts_note._uri, &noteHandle);
1399         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1400
1401         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(note));
1402         SysTryReturnResult(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1403
1404         contacts_record_set_str(noteHandle, _contacts_note.note, pCharArray.get());
1405
1406         ret = contacts_record_add_child_record(__contactHandle, _contacts_contact.note, noteHandle);
1407         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1408
1409         return E_SUCCESS;
1410 }
1411
1412 result
1413 _ContactImpl::AddEmail(const Email& email)
1414 {
1415         SysTryReturn(NID_SCL, !email.GetEmail().IsEmpty(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The email is empty.", GetErrorMessage(E_INVALID_ARG));
1416         SysTryReturn(NID_SCL, !__isRemoved || !IsFailed(Invalidate()), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1417
1418         int type = 0;
1419         String stringValue;
1420         contacts_record_h emailHandle = null;
1421
1422         int ret = contacts_record_create(_contacts_email._uri, &emailHandle);
1423         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1424
1425         __ContactsRecordHandle recordHandle(emailHandle);
1426
1427         switch (_EmailImpl::GetInstance(email)->GetType())
1428         {
1429                 case EMAIL_TYPE_PERSONAL:
1430                         type = CONTACTS_EMAIL_TYPE_HOME;
1431                         break;
1432                 case EMAIL_TYPE_WORK:
1433                         type = CONTACTS_EMAIL_TYPE_WORK;
1434                         break;
1435                 case EMAIL_TYPE_CUSTOM:
1436                         type = CONTACTS_EMAIL_TYPE_CUSTOM;
1437                         break;
1438                 case EMAIL_TYPE_MOBILE:
1439                         type = CONTACTS_EMAIL_TYPE_MOBILE;
1440                         break;
1441                 case EMAIL_TYPE_OTHER:
1442                         // fall through
1443                 default:
1444                         type = CONTACTS_EMAIL_TYPE_OTHER;
1445                         break;
1446         }
1447
1448         contacts_record_set_int(emailHandle, _contacts_email.type, type);
1449
1450         stringValue = _EmailImpl::GetInstance(email)->GetLabel();
1451         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
1452         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1453
1454         contacts_record_set_str(emailHandle, _contacts_email.label, pCharArray.get());
1455
1456         stringValue = _EmailImpl::GetInstance(email)->GetEmail();
1457         pCharArray.reset(_StringConverter::CopyToCharArrayN(stringValue));
1458         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1459
1460         contacts_record_set_str(emailHandle, _contacts_email.email, pCharArray.get());
1461
1462         ret = contacts_record_add_child_record(__contactHandle, _contacts_contact.email, emailHandle);
1463         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1464
1465         recordHandle.Release();
1466
1467         return E_SUCCESS;
1468 }
1469
1470 result
1471 _ContactImpl::AddUrl(const Url& url)
1472 {
1473         SysTryReturn(NID_SCL, !url.GetUrl().IsEmpty(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The url is empty.", GetErrorMessage(E_INVALID_ARG));
1474         SysTryReturn(NID_SCL, !__isRemoved || !IsFailed(Invalidate()), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1475
1476         int type = 0;
1477         String stringValue;
1478         contacts_record_h urlHandle = null;
1479
1480         int ret = contacts_record_create(_contacts_url._uri, &urlHandle);
1481         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1482
1483         __ContactsRecordHandle recordHandle(urlHandle);
1484
1485         switch (_UrlImpl::GetInstance(url)->GetType())
1486         {
1487                 case URL_TYPE_PERSONAL:
1488                         type = CONTACTS_URL_TYPE_HOME;
1489                         break;
1490                 case URL_TYPE_WORK:
1491                         type = CONTACTS_URL_TYPE_WORK;
1492                         break;
1493                 case URL_TYPE_CUSTOM:
1494                         type = CONTACTS_URL_TYPE_CUSTOM;
1495                         break;
1496                 case URL_TYPE_OTHER:
1497                         // fall through
1498                 default:
1499                         type = CONTACTS_URL_TYPE_OTHER;
1500                         break;
1501         }
1502
1503         // set type
1504         contacts_record_set_int(urlHandle, _contacts_url.type, type);
1505
1506         // set label
1507         stringValue = _UrlImpl::GetInstance(url)->GetLabel();
1508         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
1509         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1510
1511         contacts_record_set_str(urlHandle, _contacts_url.label, pCharArray.get());
1512
1513         // set url
1514         stringValue = _UrlImpl::GetInstance(url)->GetUrl();
1515         pCharArray.reset(_StringConverter::CopyToCharArrayN(stringValue));
1516         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1517
1518         contacts_record_set_str(urlHandle, _contacts_url.url, pCharArray.get());
1519
1520         ret = contacts_record_add_child_record(__contactHandle, _contacts_contact.url, urlHandle);
1521         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1522
1523         recordHandle.Release();
1524
1525         return E_SUCCESS;
1526 }
1527
1528 result
1529 _ContactImpl::AddAddress(const Address& address)
1530 {
1531         SysTryReturn(NID_SCL, !_AddressImpl::GetInstance(address)->IsEmpty() ,E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The address is empty.", GetErrorMessage(E_INVALID_ARG));
1532         SysTryReturn(NID_SCL, !__isRemoved || !IsFailed(Invalidate()), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1533
1534         int type = 0;
1535         String stringValue;
1536         contacts_record_h addressHandle = null;
1537
1538         int ret = contacts_record_create(_contacts_address._uri, &addressHandle);
1539         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1540
1541         __ContactsRecordHandle recordHandle(addressHandle);
1542
1543         switch (_AddressImpl::GetInstance(address)->GetType())
1544         {
1545                 case ADDRESS_TYPE_HOME:
1546                         type = CONTACTS_ADDRESS_TYPE_HOME;
1547                         break;
1548                 case ADDRESS_TYPE_WORK:
1549                         type = CONTACTS_ADDRESS_TYPE_WORK;
1550                         break;
1551                 case ADDRESS_TYPE_CUSTOM:
1552                         type = CONTACTS_ADDRESS_TYPE_CUSTOM;
1553                         break;
1554                 case ADDRESS_TYPE_OTHER:
1555                         // fall through
1556                 default:
1557                         type = CONTACTS_ADDRESS_TYPE_OTHER;
1558                         break;
1559         }
1560
1561         contacts_record_set_int(addressHandle, _contacts_address.type, type);
1562
1563         stringValue = _AddressImpl::GetInstance(address)->GetLabel();
1564         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
1565         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1566
1567         contacts_record_set_str(addressHandle, _contacts_address.label, pCharArray.get());
1568
1569         stringValue = _AddressImpl::GetInstance(address)->GetCity();
1570         if (!stringValue.IsEmpty())
1571         {
1572                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
1573                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1574
1575                 contacts_record_set_str(addressHandle, _contacts_address.locality, pCharArray.get());
1576         }
1577
1578
1579         stringValue = _AddressImpl::GetInstance(address)->GetCountry();
1580         if (!stringValue.IsEmpty())
1581         {
1582                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
1583                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1584
1585                 contacts_record_set_str(addressHandle, _contacts_address.country, pCharArray.get());
1586         }
1587
1588         stringValue = _AddressImpl::GetInstance(address)->GetExtended();
1589         if (!stringValue.IsEmpty())
1590         {
1591                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
1592                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1593
1594                 contacts_record_set_str(addressHandle, _contacts_address.extended, pCharArray.get());
1595         }
1596
1597         stringValue = _AddressImpl::GetInstance(address)->GetPostalCode();
1598         if (!stringValue.IsEmpty())
1599         {
1600                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
1601                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1602
1603                 contacts_record_set_str(addressHandle, _contacts_address.postal_code, pCharArray.get());
1604         }
1605
1606         stringValue = _AddressImpl::GetInstance(address)->GetPostOfficeBoxNumber();
1607         if (!stringValue.IsEmpty())
1608         {
1609                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
1610                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1611
1612                 contacts_record_set_str(addressHandle, _contacts_address.postbox, pCharArray.get());
1613         }
1614
1615         stringValue = _AddressImpl::GetInstance(address)->GetState();
1616         if (!stringValue.IsEmpty())
1617         {
1618                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
1619                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1620
1621                 contacts_record_set_str(addressHandle, _contacts_address.region, pCharArray.get());
1622         }
1623
1624         stringValue = _AddressImpl::GetInstance(address)->GetStreet();
1625         if (!stringValue.IsEmpty())
1626         {
1627                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
1628                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1629
1630                 contacts_record_set_str(addressHandle, _contacts_address.street, pCharArray.get());
1631         }
1632
1633         ret = contacts_record_add_child_record(__contactHandle, _contacts_contact.address, addressHandle);
1634         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1635
1636         recordHandle.Release();
1637
1638         return E_SUCCESS;
1639 }
1640
1641 result
1642 _ContactImpl::AddImAddress(const ImAddress& imAddress)
1643 {
1644         SysTryReturn(NID_SCL, !imAddress.GetImAddress().IsEmpty(), E_INVALID_ARG, E_INVALID_ARG, "[%] Invalid argument is used. The imAddress is empty.", GetErrorMessage(E_INVALID_ARG));
1645         SysTryReturn(NID_SCL, !__isRemoved || !IsFailed(Invalidate()), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1646
1647         int type = 0;
1648         String stringValue;
1649         contacts_record_h messengerHandle = null;
1650
1651         std::unique_ptr<char[]> pCharArray(null);
1652
1653         int ret = contacts_record_create(_contacts_messenger._uri, &messengerHandle);
1654         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1655
1656         __ContactsRecordHandle recordHandle(messengerHandle);
1657
1658         stringValue = _ImAddressImpl::GetInstance(imAddress)->GetServiceProviderName();
1659
1660         if (stringValue == IM_ADDRESS_GOOGLE_TALK)
1661         {
1662                 type = CONTACTS_MESSENGER_TYPE_GOOGLE;
1663         }
1664         else if (stringValue == IM_ADDRESS_MSN)
1665         {
1666                 type = CONTACTS_MESSENGER_TYPE_WLM;
1667         }
1668         else if (stringValue == IM_ADDRESS_ICQ)
1669         {
1670                 type = CONTACTS_MESSENGER_TYPE_ICQ;
1671         }
1672         else if (stringValue == IM_ADDRESS_AIM)
1673         {
1674                 type = CONTACTS_MESSENGER_TYPE_AIM;
1675         }
1676         else if (stringValue == IM_ADDRESS_YAHOO)
1677         {
1678                 type = CONTACTS_MESSENGER_TYPE_YAHOO;
1679         }
1680         else if (stringValue == IM_ADDRESS_QQ)
1681         {
1682                 type = CONTACTS_MESSENGER_TYPE_QQ;
1683         }
1684         else if (stringValue == IM_ADDRESS_SKYPE)
1685         {
1686                 type = CONTACTS_MESSENGER_TYPE_SKYPE;
1687         }
1688         else if (stringValue == IM_ADDRESS_JABBER)
1689         {
1690                 type = CONTACTS_MESSENGER_TYPE_JABBER;
1691         }
1692         else
1693         {
1694                 type = CONTACTS_MESSENGER_TYPE_CUSTOM;
1695         }
1696
1697         contacts_record_set_int(messengerHandle, _contacts_messenger.type, type);
1698         if (type == CONTACTS_MESSENGER_TYPE_CUSTOM)
1699         {
1700                 pCharArray.reset(_StringConverter::CopyToCharArrayN(stringValue));
1701                 SysTryReturn(NID_SCL, pCharArray != null,  E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1702
1703                 contacts_record_set_str(messengerHandle, _contacts_messenger.label, pCharArray.get());
1704         }
1705
1706         stringValue = _ImAddressImpl::GetInstance(imAddress)->GetImAddress();
1707         pCharArray.reset(_StringConverter::CopyToCharArrayN(stringValue));
1708         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1709
1710         contacts_record_set_str(messengerHandle, _contacts_messenger.im_id, pCharArray.get());
1711
1712         ret = contacts_record_add_child_record(__contactHandle, _contacts_contact.messenger, messengerHandle);
1713         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1714
1715         recordHandle.Release();
1716
1717         return E_SUCCESS;
1718 }
1719 result
1720 _ContactImpl::AddRelationship(const Relationship& relationship)
1721 {
1722         SysTryReturn(NID_SCL, !relationship.GetRelativeName().IsEmpty(), E_INVALID_ARG, E_INVALID_ARG, "[%] Invalid argument is used. The relationship is empty.", GetErrorMessage(E_INVALID_ARG));
1723         SysTryReturn(NID_SCL, !__isRemoved || !IsFailed(Invalidate()), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1724
1725         int intValue = 0;
1726         contacts_record_h relationshipHandle = null;
1727
1728         int ret = contacts_record_create(_contacts_relationship._uri, &relationshipHandle);
1729         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1730
1731         __ContactsRecordHandle recordHandle(relationshipHandle);
1732
1733         switch (relationship.GetType())
1734         {
1735                 case CONTACT_RELATIONSHIP_TYPE_ASSISTANT:
1736                         intValue = CONTACTS_RELATIONSHIP_TYPE_ASSISTANT;
1737                         break;
1738                 case CONTACT_RELATIONSHIP_TYPE_BROTHER:
1739                         intValue = CONTACTS_RELATIONSHIP_TYPE_BROTHER;
1740                         break;
1741                 case CONTACT_RELATIONSHIP_TYPE_CHILD:
1742                         intValue = CONTACTS_RELATIONSHIP_TYPE_CHILD;
1743                         break;
1744                 case CONTACT_RELATIONSHIP_TYPE_DOMESTIC_PARTNER:
1745                         intValue = CONTACTS_RELATIONSHIP_TYPE_DOMESTIC_PARTNER;
1746                         break;
1747                 case CONTACT_RELATIONSHIP_TYPE_FATHER:
1748                         intValue = CONTACTS_RELATIONSHIP_TYPE_FATHER;
1749                         break;
1750                 case CONTACT_RELATIONSHIP_TYPE_FRIEND:
1751                         intValue = CONTACTS_RELATIONSHIP_TYPE_FRIEND;
1752                         break;
1753                 case CONTACT_RELATIONSHIP_TYPE_MANAGER:
1754                         intValue = CONTACTS_RELATIONSHIP_TYPE_MANAGER;
1755                         break;
1756                 case CONTACT_RELATIONSHIP_TYPE_MOTHER:
1757                         intValue = CONTACTS_RELATIONSHIP_TYPE_MOTHER;
1758                         break;
1759                 case CONTACT_RELATIONSHIP_TYPE_PARENT:
1760                         intValue = CONTACTS_RELATIONSHIP_TYPE_PARENT;
1761                         break;
1762                 case CONTACT_RELATIONSHIP_TYPE_PARTNER:
1763                         intValue = CONTACTS_RELATIONSHIP_TYPE_PARTNER;
1764                         break;
1765                 case CONTACT_RELATIONSHIP_TYPE_REFERRED_BY:
1766                         intValue = CONTACTS_RELATIONSHIP_TYPE_REFERRED_BY;
1767                         break;
1768                 case CONTACT_RELATIONSHIP_TYPE_RELATIVE:
1769                         intValue = CONTACTS_RELATIONSHIP_TYPE_RELATIVE;
1770                         break;
1771                 case CONTACT_RELATIONSHIP_TYPE_SISTER:
1772                         intValue = CONTACTS_RELATIONSHIP_TYPE_SISTER;
1773                         break;
1774                 case CONTACT_RELATIONSHIP_TYPE_SPOUSE:
1775                         intValue = CONTACTS_RELATIONSHIP_TYPE_SPOUSE;
1776                         break;
1777                 case CONTACT_RELATIONSHIP_TYPE_CUSTOM:
1778                         intValue = CONTACTS_RELATIONSHIP_TYPE_CUSTOM;
1779                         break;
1780                 default:
1781                         intValue = CONTACTS_RELATIONSHIP_TYPE_OTHER;
1782                         break;
1783         }
1784
1785         // type
1786         contacts_record_set_int(relationshipHandle, _contacts_relationship.type, intValue);
1787
1788         // label
1789         String stringValue = relationship.GetLabel();
1790         if (!stringValue.IsEmpty())
1791         {
1792                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
1793                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1794
1795                 contacts_record_set_str(relationshipHandle, _contacts_relationship.label, pCharArray.get());
1796         }
1797
1798         // name
1799         stringValue = relationship.GetRelativeName();
1800         if (!stringValue.IsEmpty())
1801         {
1802                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
1803                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1804
1805                 contacts_record_set_str(relationshipHandle, _contacts_relationship.name, pCharArray.get());
1806         }
1807
1808         ret = contacts_record_add_child_record(__contactHandle, _contacts_contact.relationship, relationshipHandle);
1809         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1810
1811         recordHandle.Release();
1812
1813         return E_SUCCESS;
1814
1815 }
1816
1817 result
1818 _ContactImpl::AddEvent(const ContactEvent& event)
1819 {
1820         SysTryReturn(NID_SCL, !__isRemoved || !IsFailed(Invalidate()), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1821         SysTryReturn(NID_SCL, _ContactEventImpl::GetInstance(event)->IsDateChanged(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The datetime of the event has not been set.", GetErrorMessage(E_INVALID_ARG));
1822
1823         int type = 0;
1824         int intValue = 0;
1825         String stringValue;
1826         contacts_record_h eventHandle = null;
1827
1828         int ret = contacts_record_create(_contacts_event._uri, &eventHandle);
1829         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1830
1831         __ContactsRecordHandle recordHandle(eventHandle);
1832
1833         switch (event.GetType())
1834         {
1835                 case CONTACT_EVENT_TYPE_ANNIVERSARY:
1836                         type = CONTACTS_EVENT_TYPE_ANNIVERSARY;
1837                         break;
1838                 case CONTACT_EVENT_TYPE_BIRTHDAY:
1839                         type = CONTACTS_EVENT_TYPE_BIRTH;
1840                         break;
1841                 case CONTACT_EVENT_TYPE_CUSTOM:
1842                         type = CONTACTS_EVENT_TYPE_CUSTOM;
1843                         break;
1844                 case CONTACT_EVENT_TYPE_OTHER:
1845                         // fall through
1846                 default:
1847                         type = CONTACTS_EVENT_TYPE_OTHER;
1848                         break;
1849         }
1850
1851         // type
1852         contacts_record_set_int(eventHandle, _contacts_event.type, type);
1853
1854         // label
1855         stringValue = event.GetLabel();
1856         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
1857         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1858
1859         contacts_record_set_str(eventHandle, _contacts_event.label, pCharArray.get());
1860
1861         // date
1862         DateTime dateValue = event.GetDate();
1863         intValue = dateValue.GetYear()*10000 + dateValue.GetMonth()*100 + dateValue.GetDay();
1864         contacts_record_set_int(eventHandle, _contacts_event.date, intValue);
1865
1866         ret = contacts_record_add_child_record(__contactHandle, _contacts_contact.event, eventHandle);
1867         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1868
1869         recordHandle.Release();
1870
1871         return E_SUCCESS;
1872 }
1873
1874 result
1875 _ContactImpl::AddOrganization(const Organization& organization)
1876 {
1877         SysTryReturn(NID_SCL
1878                                           ,     !organization.GetName().IsEmpty() ||
1879                                                 !organization.GetJobTitle().IsEmpty() ||
1880                                                 !organization.GetDepartment().IsEmpty() ||
1881                                                 !organization.GetRole().IsEmpty() ||
1882                                                 !organization.GetAgent().IsEmpty() ||
1883                                                 !organization.GetDescription().IsEmpty() ||
1884                                                 !organization.GetLocation().IsEmpty() ||
1885                                                 !organization.GetPhoneticName().IsEmpty() ||
1886                                                 !organization.GetLogoPath().IsEmpty()
1887                                           ,E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The organization is empty.", GetErrorMessage(E_INVALID_ARG));
1888         SysTryReturn(NID_SCL, !__isRemoved || Invalidate() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1889
1890         contacts_record_h organizationHandle = null;
1891
1892         int ret = contacts_record_create(_contacts_company._uri, &organizationHandle);
1893         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1894
1895         __ContactsRecordHandle recordHandle(organizationHandle);
1896
1897         // name
1898         String stringValue = organization.GetName();
1899         if (!stringValue.IsEmpty())
1900         {
1901                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
1902                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1903
1904                 contacts_record_set_str(organizationHandle, _contacts_company.name, pCharArray.get());
1905         }
1906
1907         // job title
1908         stringValue = organization.GetJobTitle();
1909         if (!stringValue.IsEmpty())
1910         {
1911                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
1912                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1913
1914                 contacts_record_set_str(organizationHandle, _contacts_company.job_title, pCharArray.get());
1915         }
1916
1917         // department
1918         stringValue = organization.GetDepartment();
1919         if (!stringValue.IsEmpty())
1920         {
1921                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
1922                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1923
1924                 contacts_record_set_str(organizationHandle, _contacts_company.department, pCharArray.get());
1925         }
1926
1927         // role
1928         stringValue = organization.GetRole();
1929         if (!stringValue.IsEmpty())
1930         {
1931                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
1932                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1933
1934                 contacts_record_set_str(organizationHandle, _contacts_company.role, pCharArray.get());
1935         }
1936
1937         // agent
1938         stringValue = organization.GetAgent();
1939         if (!stringValue.IsEmpty())
1940         {
1941                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
1942                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1943
1944                 contacts_record_set_str(organizationHandle, _contacts_company.assistant_name, pCharArray.get());
1945         }
1946
1947         // type
1948         int type = 0;
1949
1950         switch (organization.GetType())
1951         {
1952                 case ORGANIZATION_TYPE_WORK:
1953                         type = CONTACTS_COMPANY_TYPE_WORK;
1954                         break;
1955                 case ORGANIZATION_TYPE_CUSTOM:
1956                         type = CONTACTS_COMPANY_TYPE_CUSTOM;
1957                         break;
1958                 case ORGANIZATION_TYPE_OTHER:
1959                         // fall through
1960                 default:
1961                         type = CONTACTS_COMPANY_TYPE_OTHER;
1962                         break;
1963         }
1964
1965         contacts_record_set_int(organizationHandle, _contacts_company.type, type);
1966
1967         // label
1968         stringValue = organization.GetLabel();
1969         if (!stringValue.IsEmpty())
1970         {
1971                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
1972                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1973
1974                 contacts_record_set_str(organizationHandle, _contacts_company.label, pCharArray.get());
1975         }
1976
1977         // description
1978         stringValue = organization.GetDescription();
1979         if (!stringValue.IsEmpty())
1980         {
1981                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
1982                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1983
1984                 contacts_record_set_str(organizationHandle, _contacts_company.description, pCharArray.get());
1985         }
1986
1987         // location
1988         stringValue = organization.GetLocation();
1989         if (!stringValue.IsEmpty())
1990         {
1991                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
1992                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1993
1994                 contacts_record_set_str(organizationHandle, _contacts_company.location, pCharArray.get());
1995         }
1996
1997         // phonetic name
1998         stringValue = organization.GetPhoneticName();
1999         if (!stringValue.IsEmpty())
2000         {
2001                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2002                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2003
2004                 contacts_record_set_str(organizationHandle, _contacts_company.phonetic_name, pCharArray.get());
2005         }
2006
2007         // logo path
2008         if (_OrganizationImpl::GetInstance(organization)->IsLogoPathChanged() == true)
2009         {
2010                 stringValue = organization.GetLogoPath();
2011                 if (!stringValue.IsEmpty())
2012                 {
2013                         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2014                         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2015
2016                         contacts_record_set_str(organizationHandle, _contacts_company.logo, pCharArray.get());
2017                 }
2018         }
2019
2020         ret = contacts_record_add_child_record(__contactHandle, _contacts_contact.company, organizationHandle);
2021         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2022
2023         recordHandle.Release();
2024
2025         return E_SUCCESS;
2026 }
2027
2028 result
2029 _ContactImpl::RemoveAt(ContactMultiPropertyId id, int index)
2030 {
2031         SysTryReturn(NID_SCL, index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index %d must be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), index);
2032
2033         unsigned int count = 0;
2034         contacts_record_h recordHandle = null;
2035
2036         switch (id)
2037         {
2038         case CONTACT_MPROPERTY_ID_PHONE_NUMBERS:
2039                 contacts_record_get_child_record_count(__contactHandle, _contacts_contact.number, &count);
2040                 SysTryReturn(NID_SCL, count > (unsigned int) index, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index %d must be less than the current count of phone numbers %d.", GetErrorMessage(E_OUT_OF_RANGE), index, count);
2041
2042                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.number, index, &recordHandle);
2043                 contacts_record_remove_child_record(__contactHandle, _contacts_contact.number, recordHandle);
2044                 contacts_record_destroy(recordHandle, true);
2045
2046                 break;
2047         case CONTACT_MPROPERTY_ID_EMAILS:
2048                 contacts_record_get_child_record_count(__contactHandle, _contacts_contact.email, &count);
2049                 SysTryReturn(NID_SCL, count > (unsigned int) index, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index %d must be less than the current count of emails %d.", GetErrorMessage(E_OUT_OF_RANGE), index, count);
2050
2051                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.email, index, &recordHandle);
2052                 contacts_record_remove_child_record(__contactHandle, _contacts_contact.email, recordHandle);
2053                 contacts_record_destroy(recordHandle, true);
2054
2055                 break;
2056         case CONTACT_MPROPERTY_ID_URLS:
2057                 contacts_record_get_child_record_count(__contactHandle, _contacts_contact.url, &count);
2058                 SysTryReturn(NID_SCL, count > (unsigned int) index, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index %d must be less than the current count of urls %d.", GetErrorMessage(E_OUT_OF_RANGE), index, count);
2059
2060                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.url, index, &recordHandle);
2061                 contacts_record_remove_child_record(__contactHandle, _contacts_contact.url, recordHandle);
2062                 contacts_record_destroy(recordHandle, true);
2063
2064                 break;
2065         case CONTACT_MPROPERTY_ID_ADDRESSES:
2066                 contacts_record_get_child_record_count(__contactHandle, _contacts_contact.address, &count);
2067                 SysTryReturn(NID_SCL, count > (unsigned int) index, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index %d must be less than the current count of addresses %d.", GetErrorMessage(E_OUT_OF_RANGE), index, count);
2068
2069                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.address, index, &recordHandle);
2070                 contacts_record_remove_child_record(__contactHandle, _contacts_contact.address, recordHandle);
2071                 contacts_record_destroy(recordHandle, true);
2072
2073                 break;
2074         case CONTACT_MPROPERTY_ID_IMADDRESSES:
2075                 contacts_record_get_child_record_count(__contactHandle, _contacts_contact.messenger, &count);
2076                 SysTryReturn(NID_SCL, count > (unsigned int) index, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index %d must be less than the current count of IM addresses %d.", GetErrorMessage(E_OUT_OF_RANGE), index, count);
2077
2078                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.messenger, index, &recordHandle);
2079                 contacts_record_remove_child_record(__contactHandle, _contacts_contact.messenger, recordHandle);
2080                 contacts_record_destroy(recordHandle, true);
2081
2082                 break;
2083         case CONTACT_MPROPERTY_ID_ORGANIZATIONS:
2084                 contacts_record_get_child_record_count(__contactHandle, _contacts_contact.company, &count);
2085                 SysTryReturn(NID_SCL, count > (unsigned int) index, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index %d must be less than the current count of organizations %d.", GetErrorMessage(E_OUT_OF_RANGE), index, count);
2086
2087                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.company, index, &recordHandle);
2088                 contacts_record_remove_child_record(__contactHandle, _contacts_contact.company, recordHandle);
2089                 contacts_record_destroy(recordHandle, true);
2090
2091                 break;
2092         case CONTACT_MPROPERTY_ID_EVENTS:
2093                 contacts_record_get_child_record_count(__contactHandle, _contacts_contact.event, &count);
2094                 SysTryReturn(NID_SCL, count > (unsigned int) index, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index %d must be less than the current count of events %d.", GetErrorMessage(E_OUT_OF_RANGE), index, count);
2095
2096                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.event, index, &recordHandle);
2097                 contacts_record_remove_child_record(__contactHandle, _contacts_contact.event, recordHandle);
2098                 contacts_record_destroy(recordHandle, true);
2099
2100                 break;
2101         case CONTACT_MPROPERTY_ID_RELATIONSHIPS:
2102                 contacts_record_get_child_record_count(__contactHandle, _contacts_contact.relationship, &count);
2103                 SysTryReturn(NID_SCL, count > (unsigned int) index, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index %d must be less than the current count of relationships %d.", GetErrorMessage(E_OUT_OF_RANGE), index, count);
2104
2105                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.relationship, index, &recordHandle);
2106                 contacts_record_remove_child_record(__contactHandle, _contacts_contact.relationship, recordHandle);
2107                 contacts_record_destroy(recordHandle, true);
2108
2109                 break;
2110         case CONTACT_MPROPERTY_ID_NOTES:
2111                 contacts_record_get_child_record_count(__contactHandle, _contacts_contact.note, &count);
2112                 SysTryReturn(NID_SCL, count > (unsigned int) index, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index %d must be less than the current count of notes %d.", GetErrorMessage(E_OUT_OF_RANGE), index, count);
2113
2114                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.note, index, &recordHandle);
2115                 contacts_record_remove_child_record(__contactHandle, _contacts_contact.note, recordHandle);
2116                 contacts_record_destroy(recordHandle, true);
2117
2118                 break;
2119         case CONTACT_MPROPERTY_ID_NICKNAMES:
2120                 contacts_record_get_child_record_count(__contactHandle, _contacts_contact.nickname, &count);
2121                 SysTryReturn(NID_SCL, count > (unsigned int) index, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index %d must be less than the current count of nicknames %d.", GetErrorMessage(E_OUT_OF_RANGE), index, count);
2122
2123                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.nickname, index, &recordHandle);
2124                 contacts_record_remove_child_record(__contactHandle, _contacts_contact.nickname, recordHandle);
2125                 contacts_record_destroy(recordHandle, true);
2126
2127                 break;
2128         default:
2129                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. id=%d.", GetErrorMessage(E_INVALID_ARG), id);
2130                 return E_INVALID_ARG;
2131         }
2132
2133         return E_SUCCESS;
2134 }
2135
2136 IList*
2137 _ContactImpl::GetValuesN(const ContactMultiPropertyId id)
2138 {
2139         IList* pList = null;
2140
2141         SetLastResult(E_SUCCESS);
2142
2143         switch (id)
2144         {
2145         case CONTACT_MPROPERTY_ID_PHONE_NUMBERS:
2146                 pList = GetPhoneNumbersN();
2147                 break;
2148
2149         case CONTACT_MPROPERTY_ID_EMAILS:
2150                 pList = GetEmailsN();
2151                 break;
2152
2153         case CONTACT_MPROPERTY_ID_URLS:
2154                 pList = GetUrlsN();
2155                 break;
2156
2157         case CONTACT_MPROPERTY_ID_ADDRESSES:
2158                 pList = GetAddressesN();
2159                 break;
2160
2161         case CONTACT_MPROPERTY_ID_IMADDRESSES:
2162                 pList = GetImAddressesN();
2163                 break;
2164
2165         case CONTACT_MPROPERTY_ID_ORGANIZATIONS:
2166                 pList = GetOrganizationsN();
2167                 break;
2168
2169         case CONTACT_MPROPERTY_ID_EVENTS:
2170                 pList = GetEventsN();
2171                 break;
2172
2173         case CONTACT_MPROPERTY_ID_RELATIONSHIPS:
2174                 pList = GetRelationshipsN();
2175                 break;
2176
2177         case CONTACT_MPROPERTY_ID_NOTES:
2178                 pList = GetNotesN();
2179                 break;
2180
2181         case CONTACT_MPROPERTY_ID_NICKNAMES:
2182                 pList = GetNicknamesN();
2183                 break;
2184         default:
2185                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. id=%d.", GetErrorMessage(E_INVALID_ARG), id);
2186                 return null;
2187         }
2188
2189         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2190
2191         return pList;
2192 }
2193
2194 result
2195 _ContactImpl::SetPhoneNumberAt(int index, const PhoneNumber& phoneNumber)
2196 {
2197         SysTryReturn(NID_SCL, index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index %d must be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), index);
2198         SysTryReturn(NID_SCL, !phoneNumber.GetPhoneNumber().IsEmpty(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The phoneNumber is string.", GetErrorMessage(E_INVALID_ARG));
2199         SysTryReturn(NID_SCL, !__isRemoved || Invalidate() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2200
2201         unsigned int count = 0;
2202         contacts_record_h recordHandle = null;
2203         String stringValue;
2204         int type = 0;
2205         int oriType = 0;
2206         PhoneNumberType phoneNumberType = PHONENUMBER_TYPE_HOME;
2207
2208         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.number, &count);
2209         SysTryReturn(NID_SCL, count > (unsigned int) index, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index=%d must be less than the current count(%d) of phone numbers.", GetErrorMessage(E_OUT_OF_RANGE), index, count);
2210
2211         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.number, index, &recordHandle);
2212
2213
2214         stringValue = _PhoneNumberImpl::GetInstance(phoneNumber)->GetLabel();
2215
2216         switch (_PhoneNumberImpl::GetInstance(phoneNumber)->GetType())
2217         {
2218                 case PHONENUMBER_TYPE_HOME:
2219                         type = CONTACTS_NUMBER_TYPE_HOME | CONTACTS_NUMBER_TYPE_VOICE;
2220                         break;
2221                 case PHONENUMBER_TYPE_WORK:
2222                         type = CONTACTS_NUMBER_TYPE_WORK | CONTACTS_NUMBER_TYPE_VOICE;
2223                         break;
2224                 case PHONENUMBER_TYPE_MOBILE:
2225                         type = CONTACTS_NUMBER_TYPE_CELL;
2226                         break;
2227                 case PHONENUMBER_TYPE_HOME_FAX:
2228                         type = CONTACTS_NUMBER_TYPE_FAX | CONTACTS_NUMBER_TYPE_HOME;
2229                         break;
2230                 case PHONENUMBER_TYPE_WORK_FAX:
2231                         type = CONTACTS_NUMBER_TYPE_FAX | CONTACTS_NUMBER_TYPE_WORK;
2232                         break;
2233                 case PHONENUMBER_TYPE_PAGER:
2234                         type = CONTACTS_NUMBER_TYPE_PAGER;
2235                         break;
2236                 case PHONENUMBER_TYPE_CUSTOM:
2237                         type = CONTACTS_NUMBER_TYPE_CUSTOM;
2238                         break;
2239                 case PHONENUMBER_TYPE_ASSISTANT:
2240                         type = CONTACTS_NUMBER_TYPE_ASSISTANT;
2241                         break;
2242                 case PHONENUMBER_TYPE_OTHER:
2243                         contacts_record_get_int(recordHandle, _contacts_number.type, &oriType);
2244
2245                         phoneNumberType = GetNativePhoneNumberType(oriType);
2246                         if (phoneNumberType == PHONENUMBER_TYPE_OTHER)
2247                         {
2248                                 type = oriType;
2249                         }
2250                         else
2251                         {
2252                                 type = CONTACTS_NUMBER_TYPE_OTHER;
2253                         }
2254                         break;
2255                 default:
2256                         type = CONTACTS_NUMBER_TYPE_OTHER;
2257                         break;
2258         }
2259
2260         // set type
2261         contacts_record_set_int(recordHandle, _contacts_number.type, type);
2262
2263         // set label
2264         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2265         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2266
2267         contacts_record_set_str(recordHandle, _contacts_number.label, pCharArray.get());
2268
2269         // set phone number
2270         stringValue = _PhoneNumberImpl::GetInstance(phoneNumber)->GetPhoneNumber();
2271         pCharArray.reset(_StringConverter::CopyToCharArrayN(stringValue));
2272         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2273
2274         contacts_record_set_str(recordHandle, _contacts_number.number, pCharArray.get());
2275
2276         return E_SUCCESS;
2277 }
2278
2279 result
2280 _ContactImpl::SetNicknameAt(int index, const String& nickname)
2281 {
2282         SysTryReturn(NID_SCL, index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index %d must be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), index);
2283         SysTryReturn(NID_SCL, !nickname.IsEmpty(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The nickname is an empty string.", GetErrorMessage(E_INVALID_ARG));
2284         SysTryReturn(NID_SCL, !__isRemoved || Invalidate() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2285
2286         unsigned int count = 0;
2287         contacts_record_h nicknameHandle = null;
2288
2289         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.nickname, &count);
2290         SysTryReturn(NID_SCL, count > (unsigned int) index, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index=%d must be less than the current count(%d) of nicknames.", GetErrorMessage(E_OUT_OF_RANGE), index, count);
2291
2292         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.nickname, index, &nicknameHandle);
2293
2294         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(nickname));
2295         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2296
2297         contacts_record_set_str(nicknameHandle, _contacts_nickname.name, pCharArray.get());
2298
2299         return E_SUCCESS;
2300 }
2301
2302 result
2303 _ContactImpl::SetNoteAt(int index, const String& note)
2304 {
2305         SysTryReturn(NID_SCL, index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index %d must be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), index);
2306         SysTryReturn(NID_SCL, !note.IsEmpty(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The note is an empty string.", GetErrorMessage(E_INVALID_ARG));
2307         SysTryReturn(NID_SCL, !__isRemoved || Invalidate() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2308
2309         unsigned int count = 0;
2310         contacts_record_h noteHandle = null;
2311
2312         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.note, &count);
2313         SysTryReturn(NID_SCL, count > (unsigned int) index, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index=%d must be less than the current count(%d) of notes.", GetErrorMessage(E_OUT_OF_RANGE), index, count);
2314
2315         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.note, index, &noteHandle);
2316
2317         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(note));
2318         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2319
2320         contacts_record_set_str(noteHandle, _contacts_note.note, pCharArray.get());
2321
2322         return E_SUCCESS;
2323 }
2324
2325 result
2326 _ContactImpl::SetOrganizationAt(int index, const Organization& organization)
2327 {
2328         SysTryReturn(NID_SCL, index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index %d must be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), index);
2329         SysTryReturn(NID_SCL
2330                                           ,     !organization.GetName().IsEmpty() ||
2331                                                 !organization.GetJobTitle().IsEmpty() ||
2332                                                 !organization.GetDepartment().IsEmpty() ||
2333                                                 !organization.GetRole().IsEmpty() ||
2334                                                 !organization.GetAgent().IsEmpty() ||
2335                                                 !organization.GetDescription().IsEmpty() ||
2336                                                 !organization.GetLocation().IsEmpty() ||
2337                                                 !organization.GetPhoneticName().IsEmpty() ||
2338                                                 !organization.GetLogoPath().IsEmpty()
2339                                           ,E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The organization is empty.", GetErrorMessage(E_INVALID_ARG));
2340         SysTryReturn(NID_SCL, !__isRemoved || Invalidate() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2341
2342         unsigned int count = 0;
2343         contacts_record_h organizationHandle = null;
2344
2345         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.company, &count);
2346         SysTryReturn(NID_SCL, count > (unsigned int) index, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index=%d must be less than the current count(%d) of organizations.", GetErrorMessage(E_OUT_OF_RANGE), index, count);
2347
2348
2349         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.company, index, &organizationHandle);
2350
2351         // name
2352         String stringValue = organization.GetName();
2353         if (!stringValue.IsEmpty())
2354         {
2355                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2356                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2357
2358                 contacts_record_set_str(organizationHandle, _contacts_company.name, pCharArray.get());
2359         }
2360         else
2361         {
2362                 contacts_record_set_str(organizationHandle, _contacts_company.name, null);
2363         }
2364
2365         // job title
2366         stringValue = organization.GetJobTitle();
2367         if (!stringValue.IsEmpty())
2368         {
2369                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2370                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2371
2372                 contacts_record_set_str(organizationHandle, _contacts_company.job_title, pCharArray.get());
2373         }
2374         else
2375         {
2376                 contacts_record_set_str(organizationHandle, _contacts_company.job_title, null);
2377         }
2378
2379         // department
2380         stringValue = organization.GetDepartment();
2381         if (!stringValue.IsEmpty())
2382         {
2383                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2384                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2385
2386                 contacts_record_set_str(organizationHandle, _contacts_company.department, pCharArray.get());
2387         }
2388         else
2389         {
2390                 contacts_record_set_str(organizationHandle, _contacts_company.department, null);
2391         }
2392
2393         // role
2394         stringValue = organization.GetRole();
2395         if (!stringValue.IsEmpty())
2396         {
2397                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2398                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2399
2400                 contacts_record_set_str(organizationHandle, _contacts_company.role, pCharArray.get());
2401         }
2402         else
2403         {
2404                 contacts_record_set_str(organizationHandle, _contacts_company.role, null);
2405         }
2406
2407         // agent
2408         stringValue = organization.GetAgent();
2409         if (!stringValue.IsEmpty())
2410         {
2411                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2412                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2413
2414                 contacts_record_set_str(organizationHandle, _contacts_company.assistant_name, pCharArray.get());
2415         }
2416         else
2417         {
2418                 contacts_record_set_str(organizationHandle, _contacts_company.assistant_name, null);
2419         }
2420
2421         // type
2422         int type = 0;
2423
2424         switch (organization.GetType())
2425         {
2426                 case ORGANIZATION_TYPE_WORK:
2427                         type = CONTACTS_COMPANY_TYPE_WORK;
2428                         break;
2429                 case ORGANIZATION_TYPE_CUSTOM:
2430                         type = CONTACTS_COMPANY_TYPE_CUSTOM;
2431                         break;
2432                 case ORGANIZATION_TYPE_OTHER:
2433                         // fall through
2434                 default:
2435                         type = CONTACTS_COMPANY_TYPE_OTHER;
2436                         break;
2437         }
2438
2439         contacts_record_set_int(organizationHandle, _contacts_company.type, type);
2440
2441         // label
2442         stringValue = organization.GetLabel();
2443         if (!stringValue.IsEmpty())
2444         {
2445                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2446                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2447
2448                 contacts_record_set_str(organizationHandle, _contacts_company.label, pCharArray.get());
2449         }
2450         else
2451         {
2452                 contacts_record_set_str(organizationHandle, _contacts_company.label, null);
2453         }
2454
2455         // description
2456         stringValue = organization.GetDescription();
2457         if (!stringValue.IsEmpty())
2458         {
2459                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2460                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2461
2462                 contacts_record_set_str(organizationHandle, _contacts_company.description, pCharArray.get());
2463         }
2464         else
2465         {
2466                 contacts_record_set_str(organizationHandle, _contacts_company.description, null);
2467         }
2468
2469         // location
2470         stringValue = organization.GetLocation();
2471         if (!stringValue.IsEmpty())
2472         {
2473                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2474                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2475
2476                 contacts_record_set_str(organizationHandle, _contacts_company.location, pCharArray.get());
2477         }
2478         else
2479         {
2480                 contacts_record_set_str(organizationHandle, _contacts_company.location, null);
2481         }
2482
2483         // phonetic name
2484         stringValue = organization.GetPhoneticName();
2485         if (!stringValue.IsEmpty())
2486         {
2487                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2488                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2489
2490                 contacts_record_set_str(organizationHandle, _contacts_company.phonetic_name, pCharArray.get());
2491         }
2492         else
2493         {
2494                 contacts_record_set_str(organizationHandle, _contacts_company.phonetic_name, null);
2495         }
2496
2497         // logo path
2498         if (_OrganizationImpl::GetInstance(organization)->IsLogoPathChanged() == true)
2499         {
2500                 stringValue = organization.GetLogoPath();
2501                 if (!stringValue.IsEmpty())
2502                 {
2503                         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2504                         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2505
2506                         contacts_record_set_str(organizationHandle, _contacts_company.logo, pCharArray.get());
2507                 }
2508                 else
2509                 {
2510                         contacts_record_set_str(organizationHandle, _contacts_company.logo, null);
2511                 }
2512         }
2513
2514         return E_SUCCESS;
2515 }
2516
2517 result
2518 _ContactImpl::SetEventAt(int index, const ContactEvent& event)
2519 {
2520         SysTryReturn(NID_SCL, index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index %d must be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), index);
2521         SysTryReturn(NID_SCL, !__isRemoved || Invalidate() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2522         SysTryReturn(NID_SCL, _ContactEventImpl::GetInstance(event)->IsDateChanged(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The datetime of the event has not been set.", GetErrorMessage(E_INVALID_ARG));
2523
2524         int type = 0;
2525         int intValue = 0;
2526         unsigned int count = 0;
2527         String stringValue;
2528         contacts_record_h eventHandle = null;
2529
2530         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.event, &count);
2531         SysTryReturn(NID_SCL, count > (unsigned int) index, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index=%d must be less than the current count(%d) of events.", GetErrorMessage(E_OUT_OF_RANGE), index, count);
2532
2533         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.event, index, &eventHandle);
2534
2535         switch (event.GetType())
2536         {
2537                 case CONTACT_EVENT_TYPE_ANNIVERSARY:
2538                         type = CONTACTS_EVENT_TYPE_ANNIVERSARY;
2539                         break;
2540                 case CONTACT_EVENT_TYPE_BIRTHDAY:
2541                         type = CONTACTS_EVENT_TYPE_BIRTH;
2542                         break;
2543                 case CONTACT_EVENT_TYPE_CUSTOM:
2544                         type = CONTACTS_EVENT_TYPE_CUSTOM;
2545                         break;
2546                 case CONTACT_EVENT_TYPE_OTHER:
2547                         // fall through
2548                 default:
2549                         type = CONTACTS_EVENT_TYPE_OTHER;
2550                         break;
2551         }
2552
2553         // type
2554         contacts_record_set_int(eventHandle, _contacts_event.type, type);
2555
2556         // label
2557         stringValue = event.GetLabel();
2558
2559         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2560         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2561
2562         contacts_record_set_str(eventHandle, _contacts_event.label, pCharArray.get());
2563
2564         // date
2565         DateTime dateValue = event.GetDate();
2566         intValue = dateValue.GetYear()*10000 + dateValue.GetMonth()*100 + dateValue.GetDay();
2567         contacts_record_set_int(eventHandle, _contacts_event.date, intValue);
2568
2569         return E_SUCCESS;
2570 }
2571
2572 result
2573 _ContactImpl::SetRelationshipAt(int index, const Relationship& relationship)
2574 {
2575         SysTryReturn(NID_SCL, index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index %d must be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), index);
2576         SysTryReturn(NID_SCL, !relationship.GetRelativeName().IsEmpty(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The relationship is empty.", GetErrorMessage(E_INVALID_ARG));
2577         SysTryReturn(NID_SCL, !__isRemoved || Invalidate() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2578
2579         int intValue = 0;
2580         unsigned int count = 0;
2581         contacts_record_h relationshipHandle = null;
2582
2583         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.relationship, &count);
2584         SysTryReturn(NID_SCL, count > (unsigned int)index, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index=%d must be less than the current count(%d) of relationships.", GetErrorMessage(E_OUT_OF_RANGE), index, count);
2585
2586         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.relationship, index, &relationshipHandle);
2587
2588         switch (relationship.GetType())
2589         {
2590                 case CONTACT_RELATIONSHIP_TYPE_ASSISTANT:
2591                         intValue = CONTACTS_RELATIONSHIP_TYPE_ASSISTANT;
2592                         break;
2593                 case CONTACT_RELATIONSHIP_TYPE_BROTHER:
2594                         intValue = CONTACTS_RELATIONSHIP_TYPE_BROTHER;
2595                         break;
2596                 case CONTACT_RELATIONSHIP_TYPE_CHILD:
2597                         intValue = CONTACTS_RELATIONSHIP_TYPE_CHILD;
2598                         break;
2599                 case CONTACT_RELATIONSHIP_TYPE_DOMESTIC_PARTNER:
2600                         intValue = CONTACTS_RELATIONSHIP_TYPE_DOMESTIC_PARTNER;
2601                         break;
2602                 case CONTACT_RELATIONSHIP_TYPE_FATHER:
2603                         intValue = CONTACTS_RELATIONSHIP_TYPE_FATHER;
2604                         break;
2605                 case CONTACT_RELATIONSHIP_TYPE_FRIEND:
2606                         intValue = CONTACTS_RELATIONSHIP_TYPE_FRIEND;
2607                         break;
2608                 case CONTACT_RELATIONSHIP_TYPE_MANAGER:
2609                         intValue = CONTACTS_RELATIONSHIP_TYPE_MANAGER;
2610                         break;
2611                 case CONTACT_RELATIONSHIP_TYPE_MOTHER:
2612                         intValue = CONTACTS_RELATIONSHIP_TYPE_MOTHER;
2613                         break;
2614                 case CONTACT_RELATIONSHIP_TYPE_PARENT:
2615                         intValue = CONTACTS_RELATIONSHIP_TYPE_PARENT;
2616                         break;
2617                 case CONTACT_RELATIONSHIP_TYPE_PARTNER:
2618                         intValue = CONTACTS_RELATIONSHIP_TYPE_PARTNER;
2619                         break;
2620                 case CONTACT_RELATIONSHIP_TYPE_REFERRED_BY:
2621                         intValue = CONTACTS_RELATIONSHIP_TYPE_REFERRED_BY;
2622                         break;
2623                 case CONTACT_RELATIONSHIP_TYPE_RELATIVE:
2624                         intValue = CONTACTS_RELATIONSHIP_TYPE_RELATIVE;
2625                         break;
2626                 case CONTACT_RELATIONSHIP_TYPE_SISTER:
2627                         intValue = CONTACTS_RELATIONSHIP_TYPE_SISTER;
2628                         break;
2629                 case CONTACT_RELATIONSHIP_TYPE_SPOUSE:
2630                         intValue = CONTACTS_RELATIONSHIP_TYPE_SPOUSE;
2631                         break;
2632                 case CONTACT_RELATIONSHIP_TYPE_CUSTOM:
2633                         intValue = CONTACTS_RELATIONSHIP_TYPE_CUSTOM;
2634                         break;
2635                 default:
2636                         intValue = CONTACTS_RELATIONSHIP_TYPE_OTHER;
2637                         break;
2638         }
2639
2640         // type
2641         contacts_record_set_int(relationshipHandle, _contacts_relationship.type, intValue);
2642
2643         // label
2644         String stringValue = relationship.GetLabel();
2645         if (!stringValue.IsEmpty())
2646         {
2647                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2648                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2649
2650                 contacts_record_set_str(relationshipHandle, _contacts_relationship.label, pCharArray.get());
2651         }
2652         else
2653         {
2654                 contacts_record_set_str(relationshipHandle, _contacts_relationship.label, null);
2655         }
2656
2657         // name
2658         stringValue = relationship.GetRelativeName();
2659         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2660         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2661
2662         contacts_record_set_str(relationshipHandle, _contacts_relationship.name, pCharArray.get());
2663
2664         return E_SUCCESS;
2665 }
2666
2667 result
2668 _ContactImpl::SetEmailAt(int index, const Email& email)
2669 {
2670         SysTryReturn(NID_SCL, index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index %d must be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), index);
2671         SysTryReturn(NID_SCL, !email.GetEmail().IsEmpty(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The email is empty.", GetErrorMessage(E_INVALID_ARG));
2672         SysTryReturn(NID_SCL, !__isRemoved || Invalidate() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2673
2674         unsigned int count = 0;
2675         contacts_record_h emailHandle = null;
2676         String stringValue;
2677         int type = 0;
2678         int oriType = 0;
2679
2680         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.email, &count);
2681         SysTryReturn(NID_SCL, count > (unsigned int) index, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index=%d must be less than the current count(%d) of emails.", GetErrorMessage(E_OUT_OF_RANGE), index, count);
2682
2683         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.email, index, &emailHandle);
2684
2685         stringValue = _EmailImpl::GetInstance(email)->GetLabel();
2686
2687         switch (_EmailImpl::GetInstance(email)->GetType())
2688         {
2689                 case EMAIL_TYPE_PERSONAL:
2690                         type = CONTACTS_EMAIL_TYPE_HOME;
2691                         break;
2692                 case EMAIL_TYPE_WORK:
2693                         type = CONTACTS_EMAIL_TYPE_WORK;
2694                         break;
2695                 case EMAIL_TYPE_CUSTOM:
2696                         type = CONTACTS_EMAIL_TYPE_CUSTOM;
2697                         break;
2698                 case EMAIL_TYPE_MOBILE:
2699                         type = CONTACTS_EMAIL_TYPE_MOBILE;
2700                         break;
2701                 case EMAIL_TYPE_OTHER:
2702                         contacts_record_get_int(emailHandle, _contacts_email.type, &oriType);
2703                         if (oriType == CONTACTS_EMAIL_TYPE_CUSTOM && (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat()))
2704                         {
2705                                 char* pCharValue = null;
2706
2707                                 contacts_record_get_str_p(emailHandle, _contacts_email.label, &pCharValue);
2708
2709                                 stringValue = pCharValue;
2710                                 type = CONTACTS_EMAIL_TYPE_CUSTOM;
2711                         }
2712                         else if (oriType == CONTACTS_EMAIL_TYPE_MOBILE && _AppInfo::GetApiVersion() < _API_VERSION_2_1)
2713                         {
2714                                 type = CONTACTS_EMAIL_TYPE_MOBILE;
2715                         }
2716                         else
2717                         {
2718                                 type = CONTACTS_EMAIL_TYPE_OTHER;
2719                         }
2720                         break;
2721                 default:
2722                         type = CONTACTS_EMAIL_TYPE_OTHER;
2723                         break;
2724         }
2725
2726         // set type
2727         contacts_record_set_int(emailHandle, _contacts_email.type, type);
2728
2729         // set label
2730         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2731         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2732
2733         contacts_record_set_str(emailHandle, _contacts_email.label, pCharArray.get());
2734
2735         // set email
2736         stringValue = _EmailImpl::GetInstance(email)->GetEmail();
2737         pCharArray.reset(_StringConverter::CopyToCharArrayN(stringValue));
2738         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2739
2740         contacts_record_set_str(emailHandle, _contacts_email.email, pCharArray.get());
2741
2742         return E_SUCCESS;
2743 }
2744
2745 result
2746 _ContactImpl::SetUrlAt(int index, const Url& url)
2747 {
2748         SysTryReturn(NID_SCL, index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index %d must be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), index);
2749         SysTryReturn(NID_SCL, !url.GetUrl().IsEmpty(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The url is empty.", GetErrorMessage(E_INVALID_ARG));
2750         SysTryReturn(NID_SCL, !__isRemoved || Invalidate() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2751
2752         unsigned int count = 0;
2753         contacts_record_h recordHandle = null;
2754         String stringValue;
2755         int type = 0;
2756         int oriType = 0;
2757
2758         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.url, &count);
2759         SysTryReturn(NID_SCL, count > (unsigned int)index, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index=%d must be less than the current count(%d) of urls.", GetErrorMessage(E_OUT_OF_RANGE), index, count);
2760
2761         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.url, index, &recordHandle);
2762
2763
2764         stringValue = _UrlImpl::GetInstance(url)->GetLabel();
2765
2766         switch (_UrlImpl::GetInstance(url)->GetType())
2767         {
2768                 case URL_TYPE_PERSONAL:
2769                         type = CONTACTS_URL_TYPE_HOME;
2770                         break;
2771                 case URL_TYPE_WORK:
2772                         type = CONTACTS_URL_TYPE_WORK;
2773                         break;
2774                 case URL_TYPE_CUSTOM:
2775                         type = CONTACTS_URL_TYPE_CUSTOM;
2776                         break;
2777                 case URL_TYPE_OTHER:
2778                         contacts_record_get_int(recordHandle, _contacts_url.type, &oriType);
2779
2780                         if (oriType == CONTACTS_URL_TYPE_CUSTOM && (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat()))
2781                         {
2782                                 char* pCharValue = null;
2783
2784                                 contacts_record_get_str_p(recordHandle, _contacts_url.label, &pCharValue);
2785
2786                                 stringValue = pCharValue;
2787                                 type = CONTACTS_URL_TYPE_CUSTOM;
2788                         }
2789                         else
2790                         {
2791                                 type = CONTACTS_URL_TYPE_OTHER;
2792                         }
2793                         break;
2794                 default:
2795                         type = CONTACTS_URL_TYPE_OTHER;
2796                         break;
2797         }
2798
2799         // set type
2800         contacts_record_set_int(recordHandle, _contacts_url.type, type);
2801
2802         // set label
2803         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2804         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2805
2806         contacts_record_set_str(recordHandle, _contacts_url.label, pCharArray.get());
2807
2808         // set url
2809         stringValue = _UrlImpl::GetInstance(url)->GetUrl();
2810         pCharArray.reset(_StringConverter::CopyToCharArrayN(stringValue));
2811         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2812
2813         contacts_record_set_str(recordHandle, _contacts_url.url, pCharArray.get());
2814
2815         return E_SUCCESS;
2816 }
2817
2818 result
2819 _ContactImpl::SetAddressAt(int index, const Address& address)
2820 {
2821         SysTryReturn(NID_SCL, index >= 0, E_OUT_OF_MEMORY, E_OUT_OF_RANGE, "[%s] index %d must be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), index);
2822         SysTryReturn(NID_SCL,
2823                                           !address.GetCity().IsEmpty() ||
2824                                           !address.GetCountry().IsEmpty() ||
2825                                           !address.GetExtended().IsEmpty() ||
2826                                           !address.GetPostalCode().IsEmpty() ||
2827                                           !address.GetPostOfficeBoxNumber().IsEmpty() ||
2828                                           !address.GetState().IsEmpty() ||
2829                                           !address.GetStreet().IsEmpty(),
2830                                           E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The address is empty.", GetErrorMessage(E_INVALID_ARG));
2831         SysTryReturn(NID_SCL, !__isRemoved || Invalidate() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2832
2833         unsigned int count = 0;
2834         contacts_record_h recordHandle = null;
2835         int type = 0;
2836         int oriType = 0;
2837         String stringValue;
2838
2839         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.address, &count);
2840         SysTryReturn(NID_SCL, count > (unsigned int)index, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index %d must be less than the current count of addresses %d.", GetErrorMessage(E_OUT_OF_RANGE), index, count);
2841
2842         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.address, index, &recordHandle);
2843
2844         stringValue = _AddressImpl::GetInstance(address)->GetLabel();
2845
2846         switch (_AddressImpl::GetInstance(address)->GetType())
2847         {
2848                 case ADDRESS_TYPE_HOME:
2849                         type = CONTACTS_ADDRESS_TYPE_HOME;
2850                         break;
2851                 case ADDRESS_TYPE_WORK:
2852                         type = CONTACTS_ADDRESS_TYPE_WORK;
2853                         break;
2854                 case ADDRESS_TYPE_CUSTOM:
2855                         type = CONTACTS_ADDRESS_TYPE_CUSTOM;
2856                         break;
2857                 case ADDRESS_TYPE_OTHER:
2858                         contacts_record_get_int(recordHandle, _contacts_address.type, &oriType);
2859
2860                         if (oriType == CONTACTS_ADDRESS_TYPE_CUSTOM && (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat()))
2861                         {
2862                                 char* pCharValue = null;
2863
2864                                 contacts_record_get_str_p(recordHandle, _contacts_address.label, &pCharValue);
2865
2866                                 stringValue = pCharValue;
2867                                 type = CONTACTS_ADDRESS_TYPE_CUSTOM;
2868                         }
2869                         else
2870                         {
2871                                 type = CONTACTS_ADDRESS_TYPE_OTHER;
2872                         }
2873                         break;
2874                 default:
2875                         type = CONTACTS_ADDRESS_TYPE_OTHER;
2876                         break;
2877         }
2878
2879         // set type
2880         contacts_record_set_int(recordHandle, _contacts_address.type, type);
2881
2882         // set label
2883         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2884         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2885
2886         contacts_record_set_str(recordHandle, _contacts_address.label, pCharArray.get());
2887
2888         // address
2889         stringValue = _AddressImpl::GetInstance(address)->GetCity();
2890         if (!stringValue.IsEmpty())
2891         {
2892                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2893                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2894
2895                 contacts_record_set_str(recordHandle, _contacts_address.locality, pCharArray.get());
2896         }
2897         else
2898         {
2899                 contacts_record_set_str(recordHandle, _contacts_address.locality, null);
2900         }
2901
2902         stringValue = _AddressImpl::GetInstance(address)->GetCountry();
2903         if (!stringValue.IsEmpty())
2904         {
2905                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2906                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2907
2908                 contacts_record_set_str(recordHandle, _contacts_address.country, pCharArray.get());
2909         }
2910         else
2911         {
2912                 contacts_record_set_str(recordHandle, _contacts_address.country, null);
2913         }
2914
2915         stringValue = _AddressImpl::GetInstance(address)->GetExtended();
2916         if (!stringValue.IsEmpty())
2917         {
2918                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2919                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2920
2921                 contacts_record_set_str(recordHandle, _contacts_address.extended, pCharArray.get());
2922         }
2923         else
2924         {
2925                 contacts_record_set_str(recordHandle, _contacts_address.extended, null);
2926         }
2927
2928         stringValue = _AddressImpl::GetInstance(address)->GetPostalCode();
2929         if (!stringValue.IsEmpty())
2930         {
2931                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2932                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2933
2934                 contacts_record_set_str(recordHandle, _contacts_address.postal_code, pCharArray.get());
2935         }
2936         else
2937         {
2938                 contacts_record_set_str(recordHandle, _contacts_address.postal_code, null);
2939         }
2940
2941         stringValue = _AddressImpl::GetInstance(address)->GetPostOfficeBoxNumber();
2942         if (!stringValue.IsEmpty())
2943         {
2944                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2945                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2946
2947                 contacts_record_set_str(recordHandle, _contacts_address.postbox, pCharArray.get());
2948         }
2949         else
2950         {
2951                 contacts_record_set_str(recordHandle, _contacts_address.postbox, null);
2952         }
2953
2954         stringValue = _AddressImpl::GetInstance(address)->GetState();
2955         if (!stringValue.IsEmpty())
2956         {
2957                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2958                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2959
2960                 contacts_record_set_str(recordHandle, _contacts_address.region, pCharArray.get());
2961         }
2962         else
2963         {
2964                 contacts_record_set_str(recordHandle, _contacts_address.region, null);
2965         }
2966
2967         stringValue = _AddressImpl::GetInstance(address)->GetStreet();
2968         if (!stringValue.IsEmpty())
2969         {
2970                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2971                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2972
2973                 contacts_record_set_str(recordHandle, _contacts_address.street, pCharArray.get());
2974         }
2975         else
2976         {
2977                 contacts_record_set_str(recordHandle, _contacts_address.street, null);
2978         }
2979
2980         return E_SUCCESS;
2981 }
2982
2983 result
2984 _ContactImpl::SetImAddressAt(int index, const ImAddress& imAddress)
2985 {
2986         SysTryReturn(NID_SCL, index >= 0, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index %d must be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), index);
2987         SysTryReturn(NID_SCL, !imAddress.GetImAddress().IsEmpty(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The imAddress is empty.", GetErrorMessage(E_INVALID_ARG));
2988         SysTryReturn(NID_SCL, !__isRemoved || Invalidate() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2989
2990         unsigned int count = 0;
2991         contacts_record_h messengerHandle = null;
2992         String stringValue;
2993         int type = 0;
2994         std::unique_ptr<char[]> pCharArray(null);
2995
2996         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.messenger, &count);
2997         SysTryReturn(NID_SCL, count > (unsigned int)index, E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[%s] index=%d must be less than the current count(%d) of IM addresses.", GetErrorMessage(E_OUT_OF_RANGE), index, count);
2998
2999         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.messenger, index, &messengerHandle);
3000
3001         stringValue = _ImAddressImpl::GetInstance(imAddress)->GetServiceProviderName();
3002
3003         if (stringValue == IM_ADDRESS_GOOGLE_TALK)
3004         {
3005                 type = CONTACTS_MESSENGER_TYPE_GOOGLE;
3006         }
3007         else if (stringValue == IM_ADDRESS_MSN)
3008         {
3009                 type = CONTACTS_MESSENGER_TYPE_WLM;
3010         }
3011         else if (stringValue == IM_ADDRESS_ICQ)
3012         {
3013                 type = CONTACTS_MESSENGER_TYPE_ICQ;
3014         }
3015         else if (stringValue == IM_ADDRESS_AIM)
3016         {
3017                 type = CONTACTS_MESSENGER_TYPE_AIM;
3018         }
3019         else if (stringValue == IM_ADDRESS_YAHOO)
3020         {
3021                 type = CONTACTS_MESSENGER_TYPE_YAHOO;
3022         }
3023         else if (stringValue == IM_ADDRESS_QQ)
3024         {
3025                 type = CONTACTS_MESSENGER_TYPE_QQ;
3026         }
3027         else if (stringValue == IM_ADDRESS_SKYPE)
3028         {
3029                 type = CONTACTS_MESSENGER_TYPE_SKYPE;
3030         }
3031         else if (stringValue == IM_ADDRESS_JABBER)
3032         {
3033                 type = CONTACTS_MESSENGER_TYPE_JABBER;
3034         }
3035         else
3036         {
3037                 type = CONTACTS_MESSENGER_TYPE_CUSTOM;
3038         }
3039
3040         contacts_record_set_int(messengerHandle, _contacts_messenger.type, type);
3041         if (type == CONTACTS_MESSENGER_TYPE_CUSTOM)
3042         {
3043                 pCharArray.reset(_StringConverter::CopyToCharArrayN(stringValue));
3044                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3045
3046                 contacts_record_set_str(messengerHandle, _contacts_messenger.label, pCharArray.get());
3047         }
3048
3049         stringValue = _ImAddressImpl::GetInstance(imAddress)->GetImAddress();
3050         pCharArray.reset(_StringConverter::CopyToCharArrayN(stringValue));
3051         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3052
3053         contacts_record_set_str(messengerHandle, _contacts_messenger.im_id, pCharArray.get());
3054
3055         return E_SUCCESS;
3056 }
3057
3058 int
3059 _ContactImpl::GetMaxLength(ContactPropertyId id)
3060 {
3061         switch (id)
3062         {
3063         case CONTACT_PROPERTY_ID_FIRST_NAME:
3064                 //fall through
3065         case CONTACT_PROPERTY_ID_LAST_NAME:
3066                 //fall through
3067         case CONTACT_PROPERTY_ID_DISPLAY_NAME:
3068                 //fall through
3069         case CONTACT_PROPERTY_ID_NICK_NAME:
3070                 //fall through
3071         case CONTACT_PROPERTY_ID_MIDDLE_NAME:
3072                 return MAX_CONTACT_NAME_LENGTH;
3073
3074         case CONTACT_PROPERTY_ID_THUMBNAIL:
3075                 return -1;
3076
3077         case CONTACT_PROPERTY_ID_JOB_TITLE:
3078                 return MAX_CONTACT_JOB_TITLE_LENGTH;
3079
3080         case CONTACT_PROPERTY_ID_COMPANY:
3081                 return MAX_CONTACT_COMPANY_LENGTH;
3082
3083         case CONTACT_PROPERTY_ID_NOTE:
3084                 return MAX_CONTACT_NOTE_LENGTH;
3085
3086         case CONTACT_PROPERTY_ID_RINGTONE:
3087                 return -1;
3088
3089         case CONTACT_PROPERTY_ID_PHONETIC_FIRST_NAME:
3090                 //fall through
3091         case CONTACT_PROPERTY_ID_PHONETIC_LAST_NAME:
3092                 //fall through
3093         case CONTACT_PROPERTY_ID_PHONETIC_MIDDLE_NAME:
3094                 return MAX_CONTACT_NAME_LENGTH;
3095
3096         default:
3097                 return -1;
3098         }
3099 }
3100
3101 IList*
3102 _ContactImpl::GetOrganizationsN(void) const
3103 {
3104         result r = E_SUCCESS;
3105         contacts_record_h organizationHandle = null;
3106         int ret = CONTACTS_ERROR_NONE;
3107         char* pCharValue = null;
3108         int intValue = 0;
3109         unsigned int count = 0;
3110         std::unique_ptr<Organization> pOrganization(null);
3111
3112         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
3113         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3114
3115         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.company, &count);
3116
3117         r = pList->Construct(count);
3118         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3119
3120         for (unsigned int i = 0; i < count; i++)
3121         {
3122                 ret = contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.company, i, &organizationHandle);
3123                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
3124
3125                 pOrganization.reset(new (std::nothrow) Organization());
3126                 SysTryReturn(NID_SCL, pOrganization != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3127
3128                 // name
3129                 contacts_record_get_str_p(organizationHandle, _contacts_company.name, &pCharValue);
3130                 pOrganization->SetName(pCharValue);
3131
3132                 // job title
3133                 contacts_record_get_str_p(organizationHandle, _contacts_company.job_title, &pCharValue);
3134                 pOrganization->SetJobTitle(pCharValue);
3135
3136                 // department
3137                 contacts_record_get_str_p(organizationHandle, _contacts_company.department, &pCharValue);
3138                 pOrganization->SetDepartment(pCharValue);
3139
3140                 // role
3141                 contacts_record_get_str_p(organizationHandle, _contacts_company.role, &pCharValue);
3142                 pOrganization->SetRole(pCharValue);
3143
3144                 // agent
3145                 contacts_record_get_str_p(organizationHandle, _contacts_company.assistant_name, &pCharValue);
3146                 pOrganization->SetAgent(pCharValue);
3147
3148                 // type
3149                 contacts_record_get_int(organizationHandle, _contacts_company.type, &intValue);
3150                 switch (intValue)
3151                 {
3152                         case CONTACTS_COMPANY_TYPE_WORK:
3153                                 pOrganization->SetType(ORGANIZATION_TYPE_WORK);
3154                                 break;
3155                         case CONTACTS_COMPANY_TYPE_CUSTOM:
3156                                 pOrganization->SetType(ORGANIZATION_TYPE_CUSTOM);
3157                                 break;
3158                         case CONTACTS_COMPANY_TYPE_OTHER:
3159                                 // fall through
3160                         default:
3161                                 pOrganization->SetType(ORGANIZATION_TYPE_OTHER);
3162                                 break;
3163                 }
3164
3165                 // label
3166                 contacts_record_get_str_p(organizationHandle, _contacts_company.label, &pCharValue);
3167                 pOrganization->SetLabel(pCharValue);
3168
3169                 // description
3170                 contacts_record_get_str_p(organizationHandle, _contacts_company.description, &pCharValue);
3171                 pOrganization->SetDescription(pCharValue);
3172
3173                 // location
3174                 contacts_record_get_str_p(organizationHandle, _contacts_company.location, &pCharValue);
3175                 pOrganization->SetLocation(pCharValue);
3176
3177                 // phonetic name
3178                 contacts_record_get_str_p(organizationHandle, _contacts_company.phonetic_name, &pCharValue);
3179                 pOrganization->SetPhoneticName(pCharValue);
3180
3181                 // logo path
3182                 contacts_record_get_str_p(organizationHandle, _contacts_company.logo, &pCharValue);
3183                 _OrganizationImpl::GetInstance(*pOrganization)->SetLogoPath(pCharValue);
3184
3185                 r = pList->Add(*pOrganization);
3186                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3187
3188                 pOrganization.release();
3189         }
3190
3191         return pList.release();
3192 }
3193
3194 IList*
3195 _ContactImpl::GetRelationshipsN(void) const
3196 {
3197         result r = E_SUCCESS;
3198         contacts_record_h relationshipHandle = null;
3199         int ret = CONTACTS_ERROR_NONE;
3200         int intValue = 0;
3201         char* pCharValue = null;
3202         unsigned int count = 0;
3203         std::unique_ptr<Relationship> pRelationship(null);
3204
3205         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
3206         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3207
3208         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.relationship, &count);
3209
3210         r = pList->Construct(count);
3211         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3212
3213         for (unsigned int i = 0; i < count; i++)
3214         {
3215                 ret = contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.relationship, i, &relationshipHandle);
3216                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
3217
3218                 pRelationship.reset(new (std::nothrow) Relationship());
3219                 SysTryReturn(NID_SCL, pRelationship != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3220
3221                 // type
3222                 contacts_record_get_int(relationshipHandle, _contacts_relationship.type, &intValue);
3223                 switch (intValue)
3224                 {
3225                 case CONTACTS_RELATIONSHIP_TYPE_ASSISTANT:
3226                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_ASSISTANT);
3227                         break;
3228                 case CONTACTS_RELATIONSHIP_TYPE_BROTHER:
3229                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_BROTHER);
3230                         break;
3231                 case CONTACTS_RELATIONSHIP_TYPE_CHILD:
3232                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_CHILD);
3233                         break;
3234                 case CONTACTS_RELATIONSHIP_TYPE_DOMESTIC_PARTNER:
3235                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_DOMESTIC_PARTNER);
3236                         break;
3237                 case CONTACTS_RELATIONSHIP_TYPE_FATHER:
3238                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_FATHER);
3239                         break;
3240                 case CONTACTS_RELATIONSHIP_TYPE_FRIEND:
3241                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_FRIEND);
3242                         break;
3243                 case CONTACTS_RELATIONSHIP_TYPE_MANAGER:
3244                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_MANAGER);
3245                         break;
3246                 case CONTACTS_RELATIONSHIP_TYPE_MOTHER:
3247                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_MOTHER);
3248                         break;
3249                 case CONTACTS_RELATIONSHIP_TYPE_PARENT:
3250                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_PARENT);
3251                         break;
3252                 case CONTACTS_RELATIONSHIP_TYPE_PARTNER:
3253                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_PARTNER);
3254                         break;
3255                 case CONTACTS_RELATIONSHIP_TYPE_REFERRED_BY:
3256                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_REFERRED_BY);
3257                         break;
3258                 case CONTACTS_RELATIONSHIP_TYPE_RELATIVE:
3259                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_RELATIVE);
3260                         break;
3261                 case CONTACTS_RELATIONSHIP_TYPE_SISTER:
3262                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_SISTER);
3263                         break;
3264                 case CONTACTS_RELATIONSHIP_TYPE_SPOUSE:
3265                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_SPOUSE);
3266                         break;
3267                 case CONTACTS_RELATIONSHIP_TYPE_CUSTOM:
3268                         // fall through
3269                 default:
3270                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_CUSTOM);
3271                         break;
3272                 }
3273
3274                 // label
3275                 contacts_record_get_str_p(relationshipHandle, _contacts_relationship.label, &pCharValue);
3276                 pRelationship->SetLabel(pCharValue);
3277
3278                 // name
3279                 contacts_record_get_str_p(relationshipHandle, _contacts_relationship.name, &pCharValue);
3280                 pRelationship->SetRelativeName(pCharValue);
3281
3282                 r = pList->Add(*pRelationship);
3283                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3284
3285                 pRelationship.release();
3286         }
3287
3288         return pList.release();
3289 }
3290
3291 IList*
3292 _ContactImpl::GetEventsN(void) const
3293 {
3294         result r = E_SUCCESS;
3295         char* pCharValue = null;
3296         int intValue = 0;
3297         unsigned int count = 0;
3298         contacts_record_h eventHandle = null;
3299         std::unique_ptr<ContactEvent> pEvent(null);
3300
3301         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
3302         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3303
3304         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.event, &count);
3305
3306         r = pList->Construct(count);
3307         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3308
3309         for (unsigned int i = 0; i < count; i++)
3310         {
3311                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.event, i, &eventHandle);
3312
3313                 pEvent.reset(new (std::nothrow) ContactEvent());
3314                 SysTryReturn(NID_SCL, pEvent != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3315
3316                 // label
3317                 contacts_record_get_str_p(eventHandle, _contacts_event.label, &pCharValue);
3318                 pEvent->SetLabel(pCharValue);
3319
3320                 // type
3321                 contacts_record_get_int(eventHandle, _contacts_event.type, &intValue);
3322                 switch (intValue)
3323                 {
3324                 case CONTACTS_EVENT_TYPE_BIRTH:
3325                         pEvent->SetType(CONTACT_EVENT_TYPE_BIRTHDAY);
3326                         break;
3327                 case CONTACTS_EVENT_TYPE_ANNIVERSARY:
3328                         pEvent->SetType(CONTACT_EVENT_TYPE_ANNIVERSARY);
3329                         break;
3330                 case CONTACTS_URL_TYPE_CUSTOM:
3331                         pEvent->SetType(CONTACT_EVENT_TYPE_CUSTOM);
3332                         break;
3333                 default:
3334                         pEvent->SetType(CONTACT_EVENT_TYPE_OTHER);
3335                         break;
3336                 }
3337
3338                 DateTime dateTime;
3339
3340                 contacts_record_get_int(eventHandle, _contacts_event.date, &intValue);
3341                 __CONVERT_DATE_TO_DATETIME(intValue, dateTime);
3342                 pEvent->SetDate(dateTime);
3343
3344                 pList->Add(*pEvent);
3345                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3346
3347                 pEvent.release();
3348         }
3349
3350         return pList.release();
3351 }
3352
3353 IList*
3354 _ContactImpl::GetNotesN(void) const
3355 {
3356         result r = E_SUCCESS;
3357         char* pCharValue = null;
3358         unsigned int count = 0;
3359         contacts_record_h noteHandle = null;
3360         std::unique_ptr<String> pNote(null);
3361
3362         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
3363         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3364
3365         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.note, &count);
3366
3367         r = pList->Construct(count);
3368         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3369
3370         for (unsigned int i = 0; i < count; i++)
3371         {
3372                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.note, i, &noteHandle);
3373
3374                 contacts_record_get_str_p(noteHandle, _contacts_note.note, &pCharValue);
3375
3376                 pNote.reset(new (std::nothrow) String(pCharValue));
3377                 SysTryReturn(NID_SCL, pNote != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3378
3379                 pList->Add(*pNote);
3380                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3381
3382                 pNote.release();
3383         }
3384
3385         return pList.release();
3386 }
3387
3388 IList*
3389 _ContactImpl::GetNicknamesN(void) const
3390 {
3391         result r = E_SUCCESS;
3392         char* pCharValue = null;
3393         unsigned int count = 0;
3394         contacts_record_h nicknameHandle = null;
3395         std::unique_ptr<String> pNickname(null);
3396
3397         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
3398         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3399
3400         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.nickname, &count);
3401
3402         r = pList->Construct(count);
3403         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3404
3405         for (unsigned int i = 0; i < count; i++)
3406         {
3407                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.nickname, i, &nicknameHandle);
3408
3409                 contacts_record_get_str_p(nicknameHandle, _contacts_nickname.name, &pCharValue);
3410         
3411                 pNickname.reset(new (std::nothrow) String(pCharValue));
3412                 SysTryReturn(NID_SCL, pNickname != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3413
3414                 pList->Add(*pNickname);
3415                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3416
3417                 pNickname.release();
3418         }
3419
3420         return pList.release();
3421 }
3422
3423 PhoneNumberType
3424 _ContactImpl::GetNativePhoneNumberType(int coreType) const
3425 {
3426         PhoneNumberType type = PHONENUMBER_TYPE_HOME;
3427
3428         switch (coreType)
3429         {
3430                 case CONTACTS_NUMBER_TYPE_HOME:
3431                         // fall through
3432                 case CONTACTS_NUMBER_TYPE_HOME | CONTACTS_NUMBER_TYPE_VOICE:
3433                         type = PHONENUMBER_TYPE_HOME;
3434                         break;
3435                 case CONTACTS_NUMBER_TYPE_WORK:
3436                         // fall through
3437                 case CONTACTS_NUMBER_TYPE_WORK | CONTACTS_NUMBER_TYPE_VOICE:
3438                         type = PHONENUMBER_TYPE_WORK;
3439                         break;
3440                 case CONTACTS_NUMBER_TYPE_CELL:
3441                         type = PHONENUMBER_TYPE_MOBILE;
3442                         break;
3443                 case CONTACTS_NUMBER_TYPE_FAX:
3444                         // fall through
3445                 case CONTACTS_NUMBER_TYPE_FAX | CONTACTS_NUMBER_TYPE_HOME:
3446                         type = PHONENUMBER_TYPE_HOME_FAX;
3447                         break;
3448                 case CONTACTS_NUMBER_TYPE_FAX | CONTACTS_NUMBER_TYPE_WORK:
3449                         type = PHONENUMBER_TYPE_WORK_FAX;
3450                         break;
3451                 case CONTACTS_NUMBER_TYPE_PAGER:
3452                         type = PHONENUMBER_TYPE_PAGER;
3453                         break;
3454                 case CONTACTS_NUMBER_TYPE_CUSTOM:
3455                         type = PHONENUMBER_TYPE_CUSTOM;
3456                         break;
3457                 case CONTACTS_NUMBER_TYPE_ASSISTANT:
3458                         if (_AppInfo::GetApiVersion() < _API_VERSION_2_1)
3459                         {
3460                                 type = PHONENUMBER_TYPE_OTHER;
3461                         }
3462                         else
3463                         {
3464                                 type = PHONENUMBER_TYPE_ASSISTANT;
3465                         }
3466                         break;
3467                 case CONTACTS_NUMBER_TYPE_OTHER:
3468                         type = PHONENUMBER_TYPE_OTHER;
3469                         break;
3470                 default:
3471                         if (coreType & CONTACTS_NUMBER_TYPE_FAX)
3472                         {
3473                                 type = PHONENUMBER_TYPE_HOME_FAX;
3474                         }
3475                         else if (coreType & CONTACTS_NUMBER_TYPE_CELL)
3476                         {
3477                                 type = PHONENUMBER_TYPE_MOBILE;
3478                         }
3479                         else if (coreType & CONTACTS_NUMBER_TYPE_PAGER)
3480                         {
3481                                 type = PHONENUMBER_TYPE_PAGER;
3482                         }
3483                         else if (coreType & CONTACTS_NUMBER_TYPE_HOME)
3484                         {
3485                                 type = PHONENUMBER_TYPE_HOME;
3486                         }
3487                         else if (coreType & CONTACTS_NUMBER_TYPE_WORK)
3488                         {
3489                                 type = PHONENUMBER_TYPE_WORK;
3490                         }
3491                         else if (coreType & CONTACTS_NUMBER_TYPE_VOICE)
3492                         {
3493                                 type = PHONENUMBER_TYPE_HOME;
3494                         }
3495                         else
3496                         {
3497                                 type = PHONENUMBER_TYPE_OTHER;
3498                         }
3499                         break;
3500         }
3501
3502         return type;
3503 }
3504
3505 IList*
3506 _ContactImpl::GetPhoneNumbersN(void) const
3507 {
3508         result r = E_SUCCESS;
3509         unsigned int count = 0;
3510         contacts_record_h numberHandle = null;
3511         int intValue = 0;
3512         char* pCharValue = null;
3513         PhoneNumberType type = PHONENUMBER_TYPE_HOME;
3514         std::unique_ptr<PhoneNumber> pPhoneNumber(null);
3515
3516         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
3517         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3518
3519
3520         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.number, &count);
3521
3522         contacts_record_get_int(__contactHandle, _contacts_contact.id, &intValue);
3523
3524         r = pList->Construct(count);
3525         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3526
3527         for (unsigned int i = 0; i < count; i++)
3528         {
3529                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.number, i, &numberHandle);
3530
3531                 pPhoneNumber.reset(new (std::nothrow) PhoneNumber());
3532                 SysTryReturn(NID_SCL, pPhoneNumber != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3533
3534                 contacts_record_get_int(numberHandle, _contacts_number.id, &intValue);
3535                 _PhoneNumberImpl::GetInstance(*pPhoneNumber)->SetRecordId(intValue);
3536
3537                 contacts_record_get_str_p(numberHandle, _contacts_number.label, &pCharValue);
3538                 _PhoneNumberImpl::GetInstance(*pPhoneNumber)->SetLabel(pCharValue);
3539
3540                 contacts_record_get_int(numberHandle, _contacts_number.type, &intValue);
3541
3542                 type = GetNativePhoneNumberType(intValue);
3543
3544                 _PhoneNumberImpl::GetInstance(*pPhoneNumber)->SetType(type);
3545
3546                 contacts_record_get_str_p(numberHandle, _contacts_number.number, &pCharValue);
3547                 _PhoneNumberImpl::GetInstance(*pPhoneNumber)->SetPhoneNumber(pCharValue);
3548
3549                 r = pList->Add(*pPhoneNumber);
3550                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3551
3552                 pPhoneNumber.release();
3553         }
3554
3555         return pList.release();
3556 }
3557
3558 IList*
3559 _ContactImpl::GetEmailsN(void) const
3560 {
3561         result r = E_SUCCESS;
3562         contacts_record_h currentHandle = null;
3563         int ret = CONTACTS_ERROR_NONE;
3564         int intValue = 0;
3565         unsigned int count = 0;
3566         char* pCharValue = null;
3567         EmailType type = EMAIL_TYPE_PERSONAL;
3568         std::unique_ptr<Email> pEmail(null);
3569
3570         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
3571         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3572
3573         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.email, &count);
3574
3575         r = pList->Construct(count);
3576         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3577
3578         for (unsigned int i = 0; i < count; i++)
3579         {
3580                 ret = contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.email, i, &currentHandle);
3581                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM))
3582
3583                 pEmail.reset(new (std::nothrow) Email());
3584                 SysTryReturn(NID_SCL, pEmail != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3585
3586                 contacts_record_get_int(currentHandle, _contacts_email.id, &intValue);
3587                 _EmailImpl::GetInstance(*pEmail)->SetRecordId(intValue);
3588
3589                 contacts_record_get_str_p(currentHandle, _contacts_email.label, &pCharValue);
3590                 _EmailImpl::GetInstance(*pEmail)->SetLabel(pCharValue);
3591
3592                 contacts_record_get_int(currentHandle, _contacts_email.type, &intValue);
3593                 switch (intValue)
3594                 {
3595                 case CONTACTS_EMAIL_TYPE_HOME:
3596                         type = EMAIL_TYPE_PERSONAL;
3597                         break;
3598                 case CONTACTS_EMAIL_TYPE_WORK:
3599                         type = EMAIL_TYPE_WORK;
3600                         break;
3601                 case CONTACTS_EMAIL_TYPE_CUSTOM:
3602                         type = EMAIL_TYPE_CUSTOM;
3603                         break;
3604                 case CONTACTS_EMAIL_TYPE_MOBILE:
3605                         if (_AppInfo::GetApiVersion() < _API_VERSION_2_1)
3606                         {
3607                                 type = EMAIL_TYPE_OTHER;
3608                         }
3609                         else
3610                         {
3611                                 type = EMAIL_TYPE_MOBILE;
3612                         }
3613                         break;
3614                 default:
3615                         type = EMAIL_TYPE_OTHER;
3616                         break;
3617                 }
3618
3619                 _EmailImpl::GetInstance(*pEmail)->SetType(type);
3620
3621                 contacts_record_get_str_p(currentHandle, _contacts_email.email, &pCharValue);
3622                 _EmailImpl::GetInstance(*pEmail)->SetEmail(pCharValue);
3623
3624                 r = pList->Add(*pEmail);
3625                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3626
3627                 pEmail.release();
3628         }
3629
3630         return pList.release();
3631 }
3632
3633 IList*
3634 _ContactImpl::GetUrlsN(void) const
3635 {
3636         result r = E_SUCCESS;
3637         char* pCharValue = null;
3638         int intValue = 0;
3639         unsigned int count = 0;
3640         UrlType type = URL_TYPE_PERSONAL;
3641         contacts_record_h urlHandle = null;
3642         std::unique_ptr<Url> pUrl(null);
3643
3644         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
3645         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3646
3647         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.url, &count);
3648
3649         r = pList->Construct(count);
3650         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3651
3652         for (unsigned int i = 0; i < count; i++)
3653         {
3654                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.url, i, &urlHandle);
3655
3656                 pUrl.reset(new (std::nothrow) Url());
3657                 SysTryReturn(NID_SCL, pUrl != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3658
3659                 contacts_record_get_str_p(urlHandle, _contacts_url.label, &pCharValue);
3660                 pUrl->SetLabel(pCharValue);
3661
3662                 contacts_record_get_int(urlHandle, _contacts_url.type, &intValue);
3663                 switch (intValue)
3664                 {
3665                 case CONTACTS_URL_TYPE_HOME:
3666                         type = URL_TYPE_PERSONAL;
3667                         break;
3668                 case CONTACTS_URL_TYPE_WORK:
3669                         type = URL_TYPE_WORK;
3670                         break;
3671                 case CONTACTS_URL_TYPE_CUSTOM:
3672                         type = URL_TYPE_CUSTOM;
3673                         break;
3674                 default:
3675                         type = URL_TYPE_OTHER;
3676                         break;
3677                 }
3678
3679                 pUrl->SetType(type);
3680
3681                 contacts_record_get_str_p(urlHandle, _contacts_url.url, &pCharValue);
3682                 _UrlImpl::GetInstance(*pUrl)->SetUrl(pCharValue);
3683
3684                 pList->Add(*pUrl);
3685                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3686
3687                 pUrl.release();
3688         }
3689
3690         return pList.release();
3691 }
3692
3693 IList*
3694 _ContactImpl::GetAddressesN(void) const
3695 {
3696         result r = E_SUCCESS;
3697         char* pCharValue = null;
3698         int intValue = 0;
3699         unsigned int count = 0;
3700         contacts_record_h addressHandle = 0;
3701         std::unique_ptr<Address> pAddress(null);
3702
3703         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
3704         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3705
3706         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.address, &count);
3707
3708         r = pList->Construct(count);
3709         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3710
3711
3712         for (unsigned int i = 0; i < count; i++)
3713         {
3714                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.address, i, &addressHandle);
3715
3716                 pAddress.reset(new (std::nothrow) Address());
3717                 SysTryReturn(NID_SCL, pAddress != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3718
3719                 contacts_record_get_str_p(addressHandle, _contacts_address.label, &pCharValue);
3720                 pAddress->SetLabel(pCharValue);
3721
3722                 contacts_record_get_int(addressHandle, _contacts_address.type, &intValue);
3723                 switch (intValue)
3724                 {
3725                 case CONTACTS_ADDRESS_TYPE_HOME:
3726                         pAddress->SetType(ADDRESS_TYPE_HOME);
3727                         break;
3728                 case CONTACTS_ADDRESS_TYPE_WORK:
3729                         pAddress->SetType(ADDRESS_TYPE_WORK);
3730                         break;
3731                 case CONTACTS_ADDRESS_TYPE_CUSTOM:
3732                         pAddress->SetType(ADDRESS_TYPE_CUSTOM);
3733                         break;
3734                 default:
3735                         pAddress->SetType(ADDRESS_TYPE_OTHER);
3736                         break;
3737                 }
3738
3739                 // 1. country
3740                 contacts_record_get_str_p(addressHandle, _contacts_address.country, &pCharValue);
3741                 _AddressImpl::GetInstance(*pAddress)->SetCountry(pCharValue);
3742
3743                 // 2. region
3744                 contacts_record_get_str_p(addressHandle, _contacts_address.region, &pCharValue);
3745                 _AddressImpl::GetInstance(*pAddress)->SetState(pCharValue);
3746
3747                 // 3. city
3748                 contacts_record_get_str_p(addressHandle, _contacts_address.locality, &pCharValue);
3749                 _AddressImpl::GetInstance(*pAddress)->SetCity(pCharValue);
3750
3751                 // 4. street
3752                 contacts_record_get_str_p(addressHandle, _contacts_address.street, &pCharValue);
3753                 _AddressImpl::GetInstance(*pAddress)->SetStreet(pCharValue);
3754
3755                 // 5. extended
3756                 contacts_record_get_str_p(addressHandle, _contacts_address.extended, &pCharValue);
3757                 _AddressImpl::GetInstance(*pAddress)->SetExtended(pCharValue);
3758
3759                 // 6. postbox
3760                 contacts_record_get_str_p(addressHandle, _contacts_address.postbox, &pCharValue);
3761                 _AddressImpl::GetInstance(*pAddress)->SetPostOfficeBoxNumber(pCharValue);
3762
3763                 // 7. postal code
3764                 contacts_record_get_str_p(addressHandle, _contacts_address.postal_code, &pCharValue);
3765                 _AddressImpl::GetInstance(*pAddress)->SetPostalCode(pCharValue);
3766
3767                 r = pList->Add(*pAddress);
3768                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3769
3770                 pAddress.release();
3771         }
3772
3773         return pList.release();
3774 }
3775
3776 IList*
3777 _ContactImpl::GetImAddressesN(void) const
3778 {
3779         result r = E_SUCCESS;
3780         char* pCharValue = null;
3781         int intValue = 0;
3782         contacts_record_h messengerHandle = null;
3783         unsigned int count = 0;
3784         std::unique_ptr<ImAddress> pImAddress(null);
3785
3786         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
3787         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3788
3789         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.messenger, &count);
3790
3791         r = pList->Construct(count);
3792         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3793
3794
3795         for (unsigned int i = 0; i < count; i++)
3796         {
3797                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.messenger, i, &messengerHandle);
3798
3799                 pImAddress.reset(new (std::nothrow) ImAddress());
3800                 SysTryReturn(NID_SCL, pImAddress != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3801
3802                 contacts_record_get_int(messengerHandle, _contacts_messenger.type, &intValue);
3803                 switch (intValue)
3804                 {
3805                 case CONTACTS_MESSENGER_TYPE_GOOGLE:
3806                         pImAddress->SetServiceProviderName(IM_ADDRESS_GOOGLE_TALK);
3807                         break;
3808                 case CONTACTS_MESSENGER_TYPE_WLM:
3809                         pImAddress->SetServiceProviderName(IM_ADDRESS_MSN);
3810                         break;
3811                 case CONTACTS_MESSENGER_TYPE_ICQ:
3812                         pImAddress->SetServiceProviderName(IM_ADDRESS_ICQ);
3813                         break;
3814                 case CONTACTS_MESSENGER_TYPE_AIM:
3815                         pImAddress->SetServiceProviderName(IM_ADDRESS_AIM);
3816                         break;
3817                 case CONTACTS_MESSENGER_TYPE_YAHOO:
3818                         pImAddress->SetServiceProviderName(IM_ADDRESS_YAHOO);
3819                         break;
3820                 case CONTACTS_MESSENGER_TYPE_QQ:
3821                         pImAddress->SetServiceProviderName(IM_ADDRESS_QQ);
3822                         break;
3823                 case CONTACTS_MESSENGER_TYPE_SKYPE:
3824                         pImAddress->SetServiceProviderName(IM_ADDRESS_SKYPE);
3825                         break;
3826                 case CONTACTS_MESSENGER_TYPE_JABBER:
3827                         pImAddress->SetServiceProviderName(IM_ADDRESS_JABBER);
3828                         break;
3829                 case CONTACTS_MESSENGER_TYPE_CUSTOM:
3830                         // fall through
3831                 default:
3832                         contacts_record_get_str_p(messengerHandle, _contacts_messenger.label, &pCharValue);
3833                         pImAddress->SetServiceProviderName(pCharValue);
3834                         break;
3835                 }
3836
3837                 contacts_record_get_str_p(messengerHandle, _contacts_messenger.im_id, &pCharValue);
3838                 _ImAddressImpl::GetInstance(*pImAddress)->SetImAddress(pCharValue);
3839
3840                 r = pList->Add(*pImAddress);
3841                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3842
3843                 pImAddress.release();
3844         }
3845
3846         return pList.release();
3847 }
3848
3849 bool
3850 _ContactImpl::IsEmpty(void) const
3851 {
3852         char* pCharValue = null;
3853         unsigned int count = 0;
3854
3855         contacts_record_get_str_p(__contactHandle, _contacts_contact.ringtone_path, &pCharValue);
3856         if (pCharValue != null)
3857         {
3858                 return false;
3859         }
3860
3861         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.number, &count);
3862         if (count > 0)
3863         {
3864                 return false;
3865         }
3866
3867         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.email, &count);
3868         if (count > 0)
3869         {
3870                 return false;
3871         }
3872
3873         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.url, &count);
3874         if (count > 0)
3875         {
3876                 return false;
3877         }
3878
3879         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.address, &count);
3880         if (count > 0)
3881         {
3882                 return false;
3883         }
3884
3885         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.messenger, &count);
3886         if (count > 0)
3887         {
3888                 return false;
3889         }
3890
3891         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.name, &count);
3892         if (count > 0)
3893         {
3894                 return false;
3895         }
3896
3897         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.company, &count);
3898         if (count > 0)
3899         {
3900                 return false;
3901         }
3902
3903         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.event, &count);
3904         if (count > 0)
3905         {
3906                 return false;
3907         }
3908
3909         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.note, &count);
3910         if (count > 0)
3911         {
3912                 return false;
3913         }
3914
3915         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.nickname, &count);
3916         if (count > 0)
3917         {
3918                 return false;
3919         }
3920
3921         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.image, &count);
3922         if (count > 0)
3923         {
3924                 return false;
3925         }
3926
3927         contacts_record_get_str_p(__contactHandle, _contacts_contact.uid, &pCharValue);
3928         if (pCharValue != null)
3929         {
3930                 return false;
3931         }
3932
3933         return true;
3934 }
3935
3936 AddressbookId
3937 _ContactImpl::GetAddressbookId(void) const
3938 {
3939         int addressbookId = 0;
3940
3941         contacts_record_get_int(__contactHandle, _contacts_contact.address_book_id, &addressbookId);
3942
3943         return addressbookId;
3944 }
3945
3946 PersonId
3947 _ContactImpl::GetPersonId(void) const
3948 {
3949         int personId = 0;
3950
3951         contacts_record_get_int(__contactHandle, _contacts_contact.person_id, &personId);
3952
3953         return personId;
3954 }
3955
3956 void
3957 _ContactImpl::SetAsRemoved(void)
3958 {
3959         __isRemoved = true;
3960 }
3961
3962 bool
3963 _ContactImpl::IsRemoved(void) const
3964 {
3965         return __isRemoved;
3966 }
3967
3968 bool
3969 _ContactImpl::IsFavorite(void) const
3970 {
3971         bool isFavorite = false;
3972         contacts_record_get_bool(__contactHandle, _contacts_contact.is_favorite, &isFavorite);
3973
3974         return isFavorite;
3975 }
3976
3977 void
3978 _ContactImpl::SetAsFavorite(bool isFavorite)
3979 {
3980         contacts_record_set_bool(__contactHandle, _contacts_contact.is_favorite, isFavorite);
3981 }
3982
3983 result
3984 _ContactImpl::Invalidate(void)
3985 {
3986         int ret = CONTACTS_ERROR_NONE;
3987         unsigned int i = 0;
3988         unsigned int count = 0;
3989         char* pCharValue = null;
3990         int intValue = 0;
3991         bool boolValue = false;
3992
3993         contacts_record_h contactHandle = null;
3994         contacts_record_h sourceRecordHandle = null;
3995         contacts_record_h destRecordHandle = null;
3996
3997         ret = contacts_record_create(_contacts_contact._uri, &contactHandle);
3998         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3999
4000         __ContactsRecordHandle recordHandle(contactHandle);
4001
4002         // favorite
4003         contacts_record_get_bool(sourceRecordHandle, _contacts_contact.is_favorite, &boolValue);
4004         contacts_record_set_bool(destRecordHandle, _contacts_contact.is_favorite, boolValue);
4005
4006         // uid
4007         contacts_record_get_str_p(sourceRecordHandle, _contacts_contact.uid, &pCharValue);
4008         contacts_record_set_str(destRecordHandle, _contacts_contact.uid, pCharValue);
4009
4010         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4011         // name
4012         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4013         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.name, &count);
4014         if (count > 0)
4015         {
4016                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.name, 0, &sourceRecordHandle);
4017
4018                 ret = contacts_record_create(_contacts_name._uri, &destRecordHandle);
4019                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
4020
4021                 __ContactsRecordHandle nameHandle(destRecordHandle);
4022
4023                 // 1. first
4024                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.first, &pCharValue);
4025                 contacts_record_set_str(destRecordHandle, _contacts_name.first, pCharValue);
4026
4027                 // 2. last
4028                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.last, &pCharValue);
4029                 contacts_record_set_str(destRecordHandle, _contacts_name.last, pCharValue);
4030
4031                 // 3. addition
4032                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.addition, &pCharValue);
4033                 contacts_record_set_str(destRecordHandle, _contacts_name.addition, pCharValue);
4034
4035                 // 4. suffix
4036                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.suffix, &pCharValue);
4037                 contacts_record_set_str(destRecordHandle, _contacts_name.suffix, pCharValue);
4038
4039                 // 5. prefix
4040                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.prefix, &pCharValue);
4041                 contacts_record_set_str(destRecordHandle, _contacts_name.prefix, pCharValue);
4042
4043                 // 6. phonetic_first
4044                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.phonetic_first, &pCharValue);
4045                 contacts_record_set_str(destRecordHandle, _contacts_name.phonetic_first, pCharValue);
4046
4047                 // 7. phonetic_last
4048                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.phonetic_last, &pCharValue);
4049                 contacts_record_set_str(destRecordHandle, _contacts_name.phonetic_last, pCharValue);
4050
4051                 // 8. phonetic_middle
4052                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.phonetic_middle, &pCharValue);
4053                 contacts_record_set_str(destRecordHandle, _contacts_name.phonetic_middle, pCharValue);
4054
4055                 contacts_record_add_child_record(contactHandle, _contacts_contact.name, destRecordHandle);
4056
4057                 nameHandle.Release();
4058         }
4059
4060         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4061         // image
4062         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4063         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.image, &count);
4064         if (count > 0)
4065         {
4066                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.image, 0, &sourceRecordHandle);
4067
4068                 ret = contacts_record_create(_contacts_image._uri, &destRecordHandle);
4069                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
4070
4071                 __ContactsRecordHandle imageHandle(destRecordHandle);
4072
4073                 contacts_record_get_int(sourceRecordHandle, _contacts_image.type, &intValue);
4074                 contacts_record_set_int(destRecordHandle, _contacts_image.type, intValue);
4075
4076                 contacts_record_get_str_p(sourceRecordHandle, _contacts_image.label, &pCharValue);
4077                 contacts_record_set_str(destRecordHandle, _contacts_image.label, pCharValue);
4078
4079                 contacts_record_get_str_p(sourceRecordHandle, _contacts_image.path, &pCharValue);
4080                 contacts_record_set_str(destRecordHandle, _contacts_image.path, pCharValue);
4081
4082                 contacts_record_add_child_record(contactHandle, _contacts_contact.image, destRecordHandle);
4083
4084                 imageHandle.Release();
4085         }
4086
4087         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4088         // company
4089         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4090         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.company, &count);
4091         for (i = 0; i < count; i++)
4092         {
4093                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.company, i, &sourceRecordHandle);
4094
4095                 ret = contacts_record_create(_contacts_company._uri, &destRecordHandle);
4096                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
4097
4098                 __ContactsRecordHandle companyHandle(destRecordHandle);
4099
4100                 contacts_record_get_int(sourceRecordHandle, _contacts_company.type, &intValue);
4101                 contacts_record_set_int(destRecordHandle, _contacts_company.type, intValue);
4102
4103                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.name, &pCharValue);
4104                 contacts_record_set_str(destRecordHandle, _contacts_company.name, pCharValue);
4105
4106                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.department, &pCharValue);
4107                 contacts_record_set_str(destRecordHandle, _contacts_company.department, pCharValue);
4108
4109                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.job_title, &pCharValue);
4110                 contacts_record_set_str(destRecordHandle, _contacts_company.job_title, pCharValue);
4111
4112                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.assistant_name, &pCharValue);
4113                 contacts_record_set_str(destRecordHandle, _contacts_company.assistant_name, pCharValue);
4114
4115                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.role, &pCharValue);
4116                 contacts_record_set_str(destRecordHandle, _contacts_company.role, pCharValue);
4117
4118                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.logo, &pCharValue);
4119                 contacts_record_set_str(destRecordHandle, _contacts_company.logo, pCharValue);
4120
4121                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.location, &pCharValue);
4122                 contacts_record_set_str(destRecordHandle, _contacts_company.location, pCharValue);
4123
4124                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.description, &pCharValue);
4125                 contacts_record_set_str(destRecordHandle, _contacts_company.description, pCharValue);
4126
4127                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.phonetic_name, &pCharValue);
4128                 contacts_record_set_str(destRecordHandle, _contacts_company.phonetic_name, pCharValue);
4129
4130                 contacts_record_add_child_record(contactHandle, _contacts_contact.company, destRecordHandle);
4131
4132                 companyHandle.Release();
4133         }
4134
4135         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4136         // note
4137         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4138         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.note, &count);
4139         for (i = 0; i < count; i++)
4140         {
4141                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.note, i, &sourceRecordHandle);
4142
4143                 ret = contacts_record_create(_contacts_note._uri, &destRecordHandle);
4144                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
4145
4146                 __ContactsRecordHandle noteHandle(destRecordHandle);
4147
4148                 contacts_record_get_str_p(sourceRecordHandle, _contacts_note.note, &pCharValue);
4149                 contacts_record_set_str(destRecordHandle, _contacts_note.note, pCharValue);
4150
4151                 contacts_record_add_child_record(contactHandle, _contacts_contact.note, destRecordHandle);
4152
4153                 noteHandle.Release();
4154         }
4155
4156         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4157         // phone number
4158         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4159         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.number, &count);
4160         for (i = 0; i < count; i++)
4161         {
4162                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.number, i, &sourceRecordHandle);
4163
4164                 ret = contacts_record_create(_contacts_number._uri, &destRecordHandle);
4165                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
4166
4167                 __ContactsRecordHandle numberHandle(destRecordHandle);
4168
4169                 contacts_record_get_int(sourceRecordHandle, _contacts_number.type, &intValue);
4170                 contacts_record_set_int(destRecordHandle, _contacts_number.type, intValue);
4171
4172                 contacts_record_get_str_p(sourceRecordHandle, _contacts_number.label, &pCharValue);
4173                 contacts_record_set_str(destRecordHandle, _contacts_number.label, pCharValue);
4174
4175                 contacts_record_get_str_p(sourceRecordHandle, _contacts_number.number, &pCharValue);
4176                 contacts_record_set_str(destRecordHandle, _contacts_number.number, pCharValue);
4177
4178                 contacts_record_add_child_record(contactHandle, _contacts_contact.number, destRecordHandle);
4179
4180                 numberHandle.Release();
4181         }
4182
4183         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4184         // email
4185         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4186         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.email, &count);
4187         for (i = 0; i < count; i++)
4188         {
4189                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.email, i, &sourceRecordHandle);
4190
4191                 ret = contacts_record_create(_contacts_email._uri, &destRecordHandle);
4192                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
4193
4194                 __ContactsRecordHandle emailHandle(destRecordHandle);
4195
4196                 contacts_record_get_int(sourceRecordHandle, _contacts_email.type, &intValue);
4197                 contacts_record_set_int(destRecordHandle, _contacts_email.type, intValue);
4198
4199                 contacts_record_get_str_p(sourceRecordHandle, _contacts_email.label, &pCharValue);
4200                 contacts_record_set_str(destRecordHandle, _contacts_email.label, pCharValue);
4201
4202                 contacts_record_get_str_p(sourceRecordHandle, _contacts_email.email, &pCharValue);
4203                 contacts_record_set_str(destRecordHandle, _contacts_email.email, pCharValue);
4204
4205                 contacts_record_add_child_record(contactHandle, _contacts_contact.email, destRecordHandle);
4206
4207                 emailHandle.Release();
4208
4209         }
4210
4211         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4212         // event
4213         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4214         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.event, &count);
4215         for (i = 0; i < count; i++)
4216         {
4217                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.event, i, &sourceRecordHandle);
4218
4219                 ret = contacts_record_create(_contacts_event._uri, &destRecordHandle);
4220                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
4221
4222                 __ContactsRecordHandle eventHandle(destRecordHandle);
4223
4224                 contacts_record_get_int(sourceRecordHandle, _contacts_event.type, &intValue);
4225                 contacts_record_set_int(destRecordHandle, _contacts_event.type, intValue);
4226
4227                 contacts_record_get_str_p(sourceRecordHandle, _contacts_event.label, &pCharValue);
4228                 contacts_record_set_str(destRecordHandle, _contacts_event.label, pCharValue);
4229
4230                 contacts_record_get_int(sourceRecordHandle, _contacts_event.date, &intValue);
4231                 contacts_record_set_int(destRecordHandle, _contacts_event.date, intValue);
4232
4233                 contacts_record_add_child_record(contactHandle, _contacts_contact.event, destRecordHandle);
4234
4235                 eventHandle.Release();
4236         }
4237
4238         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4239         // im address
4240         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4241         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.messenger, &count);
4242         for (i = 0; i < count; i++)
4243         {
4244                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.messenger, i, &sourceRecordHandle);
4245
4246                 ret = contacts_record_create(_contacts_messenger._uri, &destRecordHandle);
4247                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
4248
4249                 __ContactsRecordHandle imAddressHandle(destRecordHandle);
4250
4251                 contacts_record_get_int(sourceRecordHandle, _contacts_messenger.type, &intValue);
4252                 contacts_record_set_int(destRecordHandle, _contacts_messenger.type, intValue);
4253
4254                 contacts_record_get_str_p(sourceRecordHandle, _contacts_messenger.label, &pCharValue);
4255                 contacts_record_set_str(destRecordHandle, _contacts_messenger.label, pCharValue);
4256
4257                 contacts_record_get_str_p(sourceRecordHandle, _contacts_messenger.im_id, &pCharValue);
4258                 contacts_record_set_str(destRecordHandle, _contacts_messenger.im_id, pCharValue);
4259
4260                 contacts_record_add_child_record(contactHandle, _contacts_contact.messenger, destRecordHandle);
4261
4262                 imAddressHandle.Release();
4263         }
4264
4265         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4266         // address
4267         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4268         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.address, &count);
4269         for (i = 0; i < count; i++)
4270         {
4271                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.address, i, &sourceRecordHandle);
4272
4273                 ret = contacts_record_create(_contacts_address._uri, &destRecordHandle);
4274                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
4275
4276                 __ContactsRecordHandle addressHandle(destRecordHandle);
4277
4278                 contacts_record_get_int(sourceRecordHandle, _contacts_address.type, &intValue);
4279                 contacts_record_set_int(destRecordHandle, _contacts_address.type, intValue);
4280
4281                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.label, &pCharValue);
4282                 contacts_record_set_str(destRecordHandle, _contacts_address.label, pCharValue);
4283
4284                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.postbox, &pCharValue);
4285                 contacts_record_set_str(destRecordHandle, _contacts_address.postbox, pCharValue);
4286
4287                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.extended, &pCharValue);
4288                 contacts_record_set_str(destRecordHandle, _contacts_address.extended, pCharValue);
4289
4290                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.street, &pCharValue);
4291                 contacts_record_set_str(destRecordHandle, _contacts_address.street, pCharValue);
4292
4293                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.locality, &pCharValue);
4294                 contacts_record_set_str(destRecordHandle, _contacts_address.locality, pCharValue);
4295
4296                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.region, &pCharValue);
4297                 contacts_record_set_str(destRecordHandle, _contacts_address.region, pCharValue);
4298
4299                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.postal_code, &pCharValue);
4300                 contacts_record_set_str(destRecordHandle, _contacts_address.postal_code, pCharValue);
4301
4302                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.country, &pCharValue);
4303                 contacts_record_set_str(destRecordHandle, _contacts_address.country, pCharValue);
4304
4305                 contacts_record_add_child_record(contactHandle, _contacts_contact.address, destRecordHandle);
4306
4307                 addressHandle.Release();
4308         }
4309
4310         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4311         // url
4312         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4313         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.url, &count);
4314         for (i = 0; i < count; i++)
4315         {
4316                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.url, i, &sourceRecordHandle);
4317
4318                 ret = contacts_record_create(_contacts_url._uri, &destRecordHandle);
4319                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
4320
4321                 __ContactsRecordHandle urlHandle(destRecordHandle);
4322
4323                 contacts_record_get_int(sourceRecordHandle, _contacts_url.type, &intValue);
4324                 contacts_record_set_int(destRecordHandle, _contacts_url.type, intValue);
4325
4326                 contacts_record_get_str_p(sourceRecordHandle, _contacts_url.label, &pCharValue);
4327                 contacts_record_set_str(destRecordHandle, _contacts_url.label, pCharValue);
4328
4329                 contacts_record_get_str_p(sourceRecordHandle, _contacts_url.url, &pCharValue);
4330                 contacts_record_set_str(destRecordHandle, _contacts_url.url, pCharValue);
4331
4332                 contacts_record_add_child_record(contactHandle, _contacts_contact.url, destRecordHandle);
4333
4334                 urlHandle.Release();
4335         }
4336
4337         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4338         // nickname
4339         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4340         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.nickname, &count);
4341         for (i = 0; i < count; i++)
4342         {
4343                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.nickname, i, &sourceRecordHandle);
4344
4345                 ret = contacts_record_create(_contacts_nickname._uri, &destRecordHandle);
4346                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
4347
4348                 __ContactsRecordHandle nicknameHandle(destRecordHandle);
4349
4350                 contacts_record_get_str_p(sourceRecordHandle, _contacts_nickname.name, &pCharValue);
4351                 contacts_record_set_str(destRecordHandle, _contacts_nickname.name, pCharValue);
4352
4353                 contacts_record_add_child_record(contactHandle, _contacts_contact.nickname, destRecordHandle);
4354
4355                 nicknameHandle.Release();
4356         }
4357
4358         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4359         // relationship
4360         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4361         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.relationship, &count);
4362         for (i = 0; i < count; i++)
4363         {
4364                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.relationship, i, &sourceRecordHandle);
4365
4366                 ret = contacts_record_create(_contacts_relationship._uri, &destRecordHandle);
4367                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
4368
4369                 __ContactsRecordHandle relationshipHandle(destRecordHandle);
4370
4371                 contacts_record_get_str_p(sourceRecordHandle, _contacts_relationship.name, &pCharValue);
4372                 contacts_record_set_str(destRecordHandle, _contacts_relationship.name, pCharValue);
4373
4374                 contacts_record_add_child_record(contactHandle, _contacts_contact.relationship, destRecordHandle);
4375
4376                 relationshipHandle.Release();
4377         }
4378
4379         contacts_record_destroy(__contactHandle, true);
4380         __contactHandle = contactHandle;
4381
4382         __isRemoved = false;
4383
4384         recordHandle.Release();
4385
4386         return E_SUCCESS;
4387 }
4388
4389 const _ContactImpl*
4390 _ContactImpl::GetInstance(const Contact& contact)
4391 {
4392         return contact.__pContactImpl;
4393 }
4394
4395 _ContactImpl*
4396 _ContactImpl::GetInstance(Contact& contact)
4397 {
4398         return contact.__pContactImpl;
4399 }
4400
4401 }} // Tizen::Social