Merge "Export internal APIs needed by samsung-socil" into tizen_2.1
[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
2207         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.number, &count);
2208         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);
2209
2210         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.number, index, &recordHandle);
2211
2212
2213         stringValue = _PhoneNumberImpl::GetInstance(phoneNumber)->GetLabel();
2214
2215         switch (_PhoneNumberImpl::GetInstance(phoneNumber)->GetType())
2216         {
2217                 case PHONENUMBER_TYPE_HOME:
2218                         type = CONTACTS_NUMBER_TYPE_HOME | CONTACTS_NUMBER_TYPE_VOICE;
2219                         break;
2220                 case PHONENUMBER_TYPE_WORK:
2221                         type = CONTACTS_NUMBER_TYPE_WORK | CONTACTS_NUMBER_TYPE_VOICE;
2222                         break;
2223                 case PHONENUMBER_TYPE_MOBILE:
2224                         type = CONTACTS_NUMBER_TYPE_CELL;
2225                         break;
2226                 case PHONENUMBER_TYPE_HOME_FAX:
2227                         type = CONTACTS_NUMBER_TYPE_FAX | CONTACTS_NUMBER_TYPE_HOME;
2228                         break;
2229                 case PHONENUMBER_TYPE_WORK_FAX:
2230                         type = CONTACTS_NUMBER_TYPE_FAX | CONTACTS_NUMBER_TYPE_WORK;
2231                         break;
2232                 case PHONENUMBER_TYPE_PAGER:
2233                         type = CONTACTS_NUMBER_TYPE_PAGER;
2234                         break;
2235                 case PHONENUMBER_TYPE_CUSTOM:
2236                         type = CONTACTS_NUMBER_TYPE_CUSTOM;
2237                         break;
2238                 case PHONENUMBER_TYPE_ASSISTANT:
2239                         type = CONTACTS_NUMBER_TYPE_ASSISTANT;
2240                         break;
2241                 case PHONENUMBER_TYPE_OTHER:
2242                         contacts_record_get_int(recordHandle, _contacts_number.type, &oriType);
2243
2244                         if (oriType == (CONTACTS_NUMBER_TYPE_HOME | CONTACTS_NUMBER_TYPE_VOICE)
2245                                 || oriType == (CONTACTS_NUMBER_TYPE_WORK | CONTACTS_NUMBER_TYPE_VOICE)
2246                                 || oriType == CONTACTS_NUMBER_TYPE_CELL
2247                                 || oriType == (CONTACTS_NUMBER_TYPE_FAX | CONTACTS_NUMBER_TYPE_HOME)
2248                                 || oriType == (CONTACTS_NUMBER_TYPE_FAX | CONTACTS_NUMBER_TYPE_WORK)
2249                                 || oriType == CONTACTS_NUMBER_TYPE_PAGER)
2250                         {
2251                                 type = CONTACTS_NUMBER_TYPE_OTHER;
2252                         }
2253                         else if (oriType == CONTACTS_NUMBER_TYPE_CUSTOM)
2254                         {
2255                                 if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
2256                                 {
2257                                         char* pCharValue = null;
2258
2259                                         contacts_record_get_str_p(recordHandle, _contacts_number.label, &pCharValue);
2260
2261                                         stringValue = pCharValue;
2262                                         type = CONTACTS_NUMBER_TYPE_CUSTOM;
2263
2264                                 }
2265                                 else
2266                                 {
2267                                         type = CONTACTS_NUMBER_TYPE_OTHER;
2268                                 }
2269                         }
2270                         else if (oriType == CONTACTS_NUMBER_TYPE_ASSISTANT)
2271                         {
2272                                 if (_AppInfo::GetApiVersion() < _API_VERSION_2_1)
2273                                 {
2274                                         type = CONTACTS_NUMBER_TYPE_ASSISTANT;
2275                                 }
2276                                 else
2277                                 {
2278                                         type = CONTACTS_NUMBER_TYPE_OTHER;
2279                                 }
2280                         }
2281                         else
2282                         {
2283                                 type = oriType;
2284                         }
2285                         break;
2286                 default:
2287                         type = CONTACTS_NUMBER_TYPE_OTHER;
2288                         break;
2289         }
2290
2291         // set type
2292         contacts_record_set_int(recordHandle, _contacts_number.type, type);
2293
2294         // set label
2295         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2296         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2297
2298         contacts_record_set_str(recordHandle, _contacts_number.label, pCharArray.get());
2299
2300         // set phone number
2301         stringValue = _PhoneNumberImpl::GetInstance(phoneNumber)->GetPhoneNumber();
2302         pCharArray.reset(_StringConverter::CopyToCharArrayN(stringValue));
2303         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2304
2305         contacts_record_set_str(recordHandle, _contacts_number.number, pCharArray.get());
2306
2307         return E_SUCCESS;
2308 }
2309
2310 result
2311 _ContactImpl::SetNicknameAt(int index, const String& nickname)
2312 {
2313         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);
2314         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));
2315         SysTryReturn(NID_SCL, !__isRemoved || Invalidate() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2316
2317         unsigned int count = 0;
2318         contacts_record_h nicknameHandle = null;
2319
2320         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.nickname, &count);
2321         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);
2322
2323         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.nickname, index, &nicknameHandle);
2324
2325         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(nickname));
2326         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2327
2328         contacts_record_set_str(nicknameHandle, _contacts_nickname.name, pCharArray.get());
2329
2330         return E_SUCCESS;
2331 }
2332
2333 result
2334 _ContactImpl::SetNoteAt(int index, const String& note)
2335 {
2336         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);
2337         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));
2338         SysTryReturn(NID_SCL, !__isRemoved || Invalidate() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2339
2340         unsigned int count = 0;
2341         contacts_record_h noteHandle = null;
2342
2343         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.note, &count);
2344         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);
2345
2346         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.note, index, &noteHandle);
2347
2348         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(note));
2349         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2350
2351         contacts_record_set_str(noteHandle, _contacts_note.note, pCharArray.get());
2352
2353         return E_SUCCESS;
2354 }
2355
2356 result
2357 _ContactImpl::SetOrganizationAt(int index, const Organization& organization)
2358 {
2359         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);
2360         SysTryReturn(NID_SCL
2361                                           ,     !organization.GetName().IsEmpty() ||
2362                                                 !organization.GetJobTitle().IsEmpty() ||
2363                                                 !organization.GetDepartment().IsEmpty() ||
2364                                                 !organization.GetRole().IsEmpty() ||
2365                                                 !organization.GetAgent().IsEmpty() ||
2366                                                 !organization.GetDescription().IsEmpty() ||
2367                                                 !organization.GetLocation().IsEmpty() ||
2368                                                 !organization.GetPhoneticName().IsEmpty() ||
2369                                                 !organization.GetLogoPath().IsEmpty()
2370                                           ,E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The organization is empty.", GetErrorMessage(E_INVALID_ARG));
2371         SysTryReturn(NID_SCL, !__isRemoved || Invalidate() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2372
2373         unsigned int count = 0;
2374         contacts_record_h organizationHandle = null;
2375
2376         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.company, &count);
2377         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);
2378
2379
2380         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.company, index, &organizationHandle);
2381
2382         // name
2383         String stringValue = organization.GetName();
2384         if (!stringValue.IsEmpty())
2385         {
2386                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2387                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2388
2389                 contacts_record_set_str(organizationHandle, _contacts_company.name, pCharArray.get());
2390         }
2391         else
2392         {
2393                 contacts_record_set_str(organizationHandle, _contacts_company.name, null);
2394         }
2395
2396         // job title
2397         stringValue = organization.GetJobTitle();
2398         if (!stringValue.IsEmpty())
2399         {
2400                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2401                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2402
2403                 contacts_record_set_str(organizationHandle, _contacts_company.job_title, pCharArray.get());
2404         }
2405         else
2406         {
2407                 contacts_record_set_str(organizationHandle, _contacts_company.job_title, null);
2408         }
2409
2410         // department
2411         stringValue = organization.GetDepartment();
2412         if (!stringValue.IsEmpty())
2413         {
2414                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2415                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2416
2417                 contacts_record_set_str(organizationHandle, _contacts_company.department, pCharArray.get());
2418         }
2419         else
2420         {
2421                 contacts_record_set_str(organizationHandle, _contacts_company.department, null);
2422         }
2423
2424         // role
2425         stringValue = organization.GetRole();
2426         if (!stringValue.IsEmpty())
2427         {
2428                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2429                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2430
2431                 contacts_record_set_str(organizationHandle, _contacts_company.role, pCharArray.get());
2432         }
2433         else
2434         {
2435                 contacts_record_set_str(organizationHandle, _contacts_company.role, null);
2436         }
2437
2438         // agent
2439         stringValue = organization.GetAgent();
2440         if (!stringValue.IsEmpty())
2441         {
2442                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2443                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2444
2445                 contacts_record_set_str(organizationHandle, _contacts_company.assistant_name, pCharArray.get());
2446         }
2447         else
2448         {
2449                 contacts_record_set_str(organizationHandle, _contacts_company.assistant_name, null);
2450         }
2451
2452         // type
2453         int type = 0;
2454
2455         switch (organization.GetType())
2456         {
2457                 case ORGANIZATION_TYPE_WORK:
2458                         type = CONTACTS_COMPANY_TYPE_WORK;
2459                         break;
2460                 case ORGANIZATION_TYPE_CUSTOM:
2461                         type = CONTACTS_COMPANY_TYPE_CUSTOM;
2462                         break;
2463                 case ORGANIZATION_TYPE_OTHER:
2464                         // fall through
2465                 default:
2466                         type = CONTACTS_COMPANY_TYPE_OTHER;
2467                         break;
2468         }
2469
2470         contacts_record_set_int(organizationHandle, _contacts_company.type, type);
2471
2472         // label
2473         stringValue = organization.GetLabel();
2474         if (!stringValue.IsEmpty())
2475         {
2476                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2477                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2478
2479                 contacts_record_set_str(organizationHandle, _contacts_company.label, pCharArray.get());
2480         }
2481         else
2482         {
2483                 contacts_record_set_str(organizationHandle, _contacts_company.label, null);
2484         }
2485
2486         // description
2487         stringValue = organization.GetDescription();
2488         if (!stringValue.IsEmpty())
2489         {
2490                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2491                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2492
2493                 contacts_record_set_str(organizationHandle, _contacts_company.description, pCharArray.get());
2494         }
2495         else
2496         {
2497                 contacts_record_set_str(organizationHandle, _contacts_company.description, null);
2498         }
2499
2500         // location
2501         stringValue = organization.GetLocation();
2502         if (!stringValue.IsEmpty())
2503         {
2504                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2505                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2506
2507                 contacts_record_set_str(organizationHandle, _contacts_company.location, pCharArray.get());
2508         }
2509         else
2510         {
2511                 contacts_record_set_str(organizationHandle, _contacts_company.location, null);
2512         }
2513
2514         // phonetic name
2515         stringValue = organization.GetPhoneticName();
2516         if (!stringValue.IsEmpty())
2517         {
2518                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2519                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2520
2521                 contacts_record_set_str(organizationHandle, _contacts_company.phonetic_name, pCharArray.get());
2522         }
2523         else
2524         {
2525                 contacts_record_set_str(organizationHandle, _contacts_company.phonetic_name, null);
2526         }
2527
2528         // logo path
2529         if (_OrganizationImpl::GetInstance(organization)->IsLogoPathChanged() == true)
2530         {
2531                 stringValue = organization.GetLogoPath();
2532                 if (!stringValue.IsEmpty())
2533                 {
2534                         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2535                         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2536
2537                         contacts_record_set_str(organizationHandle, _contacts_company.logo, pCharArray.get());
2538                 }
2539                 else
2540                 {
2541                         contacts_record_set_str(organizationHandle, _contacts_company.logo, null);
2542                 }
2543         }
2544
2545         return E_SUCCESS;
2546 }
2547
2548 result
2549 _ContactImpl::SetEventAt(int index, const ContactEvent& event)
2550 {
2551         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);
2552         SysTryReturn(NID_SCL, !__isRemoved || Invalidate() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2553         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));
2554
2555         int type = 0;
2556         int intValue = 0;
2557         unsigned int count = 0;
2558         String stringValue;
2559         contacts_record_h eventHandle = null;
2560
2561         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.event, &count);
2562         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);
2563
2564         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.event, index, &eventHandle);
2565
2566         switch (event.GetType())
2567         {
2568                 case CONTACT_EVENT_TYPE_ANNIVERSARY:
2569                         type = CONTACTS_EVENT_TYPE_ANNIVERSARY;
2570                         break;
2571                 case CONTACT_EVENT_TYPE_BIRTHDAY:
2572                         type = CONTACTS_EVENT_TYPE_BIRTH;
2573                         break;
2574                 case CONTACT_EVENT_TYPE_CUSTOM:
2575                         type = CONTACTS_EVENT_TYPE_CUSTOM;
2576                         break;
2577                 case CONTACT_EVENT_TYPE_OTHER:
2578                         // fall through
2579                 default:
2580                         type = CONTACTS_EVENT_TYPE_OTHER;
2581                         break;
2582         }
2583
2584         // type
2585         contacts_record_set_int(eventHandle, _contacts_event.type, type);
2586
2587         // label
2588         stringValue = event.GetLabel();
2589
2590         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2591         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2592
2593         contacts_record_set_str(eventHandle, _contacts_event.label, pCharArray.get());
2594
2595         // date
2596         DateTime dateValue = event.GetDate();
2597         intValue = dateValue.GetYear()*10000 + dateValue.GetMonth()*100 + dateValue.GetDay();
2598         contacts_record_set_int(eventHandle, _contacts_event.date, intValue);
2599
2600         return E_SUCCESS;
2601 }
2602
2603 result
2604 _ContactImpl::SetRelationshipAt(int index, const Relationship& relationship)
2605 {
2606         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);
2607         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));
2608         SysTryReturn(NID_SCL, !__isRemoved || Invalidate() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2609
2610         int intValue = 0;
2611         unsigned int count = 0;
2612         contacts_record_h relationshipHandle = null;
2613
2614         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.relationship, &count);
2615         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);
2616
2617         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.relationship, index, &relationshipHandle);
2618
2619         switch (relationship.GetType())
2620         {
2621                 case CONTACT_RELATIONSHIP_TYPE_ASSISTANT:
2622                         intValue = CONTACTS_RELATIONSHIP_TYPE_ASSISTANT;
2623                         break;
2624                 case CONTACT_RELATIONSHIP_TYPE_BROTHER:
2625                         intValue = CONTACTS_RELATIONSHIP_TYPE_BROTHER;
2626                         break;
2627                 case CONTACT_RELATIONSHIP_TYPE_CHILD:
2628                         intValue = CONTACTS_RELATIONSHIP_TYPE_CHILD;
2629                         break;
2630                 case CONTACT_RELATIONSHIP_TYPE_DOMESTIC_PARTNER:
2631                         intValue = CONTACTS_RELATIONSHIP_TYPE_DOMESTIC_PARTNER;
2632                         break;
2633                 case CONTACT_RELATIONSHIP_TYPE_FATHER:
2634                         intValue = CONTACTS_RELATIONSHIP_TYPE_FATHER;
2635                         break;
2636                 case CONTACT_RELATIONSHIP_TYPE_FRIEND:
2637                         intValue = CONTACTS_RELATIONSHIP_TYPE_FRIEND;
2638                         break;
2639                 case CONTACT_RELATIONSHIP_TYPE_MANAGER:
2640                         intValue = CONTACTS_RELATIONSHIP_TYPE_MANAGER;
2641                         break;
2642                 case CONTACT_RELATIONSHIP_TYPE_MOTHER:
2643                         intValue = CONTACTS_RELATIONSHIP_TYPE_MOTHER;
2644                         break;
2645                 case CONTACT_RELATIONSHIP_TYPE_PARENT:
2646                         intValue = CONTACTS_RELATIONSHIP_TYPE_PARENT;
2647                         break;
2648                 case CONTACT_RELATIONSHIP_TYPE_PARTNER:
2649                         intValue = CONTACTS_RELATIONSHIP_TYPE_PARTNER;
2650                         break;
2651                 case CONTACT_RELATIONSHIP_TYPE_REFERRED_BY:
2652                         intValue = CONTACTS_RELATIONSHIP_TYPE_REFERRED_BY;
2653                         break;
2654                 case CONTACT_RELATIONSHIP_TYPE_RELATIVE:
2655                         intValue = CONTACTS_RELATIONSHIP_TYPE_RELATIVE;
2656                         break;
2657                 case CONTACT_RELATIONSHIP_TYPE_SISTER:
2658                         intValue = CONTACTS_RELATIONSHIP_TYPE_SISTER;
2659                         break;
2660                 case CONTACT_RELATIONSHIP_TYPE_SPOUSE:
2661                         intValue = CONTACTS_RELATIONSHIP_TYPE_SPOUSE;
2662                         break;
2663                 case CONTACT_RELATIONSHIP_TYPE_CUSTOM:
2664                         intValue = CONTACTS_RELATIONSHIP_TYPE_CUSTOM;
2665                         break;
2666                 default:
2667                         intValue = CONTACTS_RELATIONSHIP_TYPE_OTHER;
2668                         break;
2669         }
2670
2671         // type
2672         contacts_record_set_int(relationshipHandle, _contacts_relationship.type, intValue);
2673
2674         // label
2675         String stringValue = relationship.GetLabel();
2676         if (!stringValue.IsEmpty())
2677         {
2678                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2679                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2680
2681                 contacts_record_set_str(relationshipHandle, _contacts_relationship.label, pCharArray.get());
2682         }
2683         else
2684         {
2685                 contacts_record_set_str(relationshipHandle, _contacts_relationship.label, null);
2686         }
2687
2688         // name
2689         stringValue = relationship.GetRelativeName();
2690         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2691         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2692
2693         contacts_record_set_str(relationshipHandle, _contacts_relationship.name, pCharArray.get());
2694
2695         return E_SUCCESS;
2696 }
2697
2698 result
2699 _ContactImpl::SetEmailAt(int index, const Email& email)
2700 {
2701         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);
2702         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));
2703         SysTryReturn(NID_SCL, !__isRemoved || Invalidate() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2704
2705         unsigned int count = 0;
2706         contacts_record_h emailHandle = null;
2707         String stringValue;
2708         int type = 0;
2709         int oriType = 0;
2710
2711         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.email, &count);
2712         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);
2713
2714         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.email, index, &emailHandle);
2715
2716         stringValue = _EmailImpl::GetInstance(email)->GetLabel();
2717
2718         switch (_EmailImpl::GetInstance(email)->GetType())
2719         {
2720                 case EMAIL_TYPE_PERSONAL:
2721                         type = CONTACTS_EMAIL_TYPE_HOME;
2722                         break;
2723                 case EMAIL_TYPE_WORK:
2724                         type = CONTACTS_EMAIL_TYPE_WORK;
2725                         break;
2726                 case EMAIL_TYPE_CUSTOM:
2727                         type = CONTACTS_EMAIL_TYPE_CUSTOM;
2728                         break;
2729                 case EMAIL_TYPE_MOBILE:
2730                         type = CONTACTS_EMAIL_TYPE_MOBILE;
2731                         break;
2732                 case EMAIL_TYPE_OTHER:
2733                         contacts_record_get_int(emailHandle, _contacts_email.type, &oriType);
2734                         if (oriType == CONTACTS_EMAIL_TYPE_CUSTOM && (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat()))
2735                         {
2736                                 char* pCharValue = null;
2737
2738                                 contacts_record_get_str_p(emailHandle, _contacts_email.label, &pCharValue);
2739
2740                                 stringValue = pCharValue;
2741                                 type = CONTACTS_EMAIL_TYPE_CUSTOM;
2742                         }
2743                         else if (oriType == CONTACTS_EMAIL_TYPE_MOBILE && _AppInfo::GetApiVersion() < _API_VERSION_2_1)
2744                         {
2745                                 type = CONTACTS_EMAIL_TYPE_MOBILE;
2746                         }
2747                         else
2748                         {
2749                                 type = CONTACTS_EMAIL_TYPE_OTHER;
2750                         }
2751                         break;
2752                 default:
2753                         type = CONTACTS_EMAIL_TYPE_OTHER;
2754                         break;
2755         }
2756
2757         // set type
2758         contacts_record_set_int(emailHandle, _contacts_email.type, type);
2759
2760         // set label
2761         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2762         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2763
2764         contacts_record_set_str(emailHandle, _contacts_email.label, pCharArray.get());
2765
2766         // set email
2767         stringValue = _EmailImpl::GetInstance(email)->GetEmail();
2768         pCharArray.reset(_StringConverter::CopyToCharArrayN(stringValue));
2769         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2770
2771         contacts_record_set_str(emailHandle, _contacts_email.email, pCharArray.get());
2772
2773         return E_SUCCESS;
2774 }
2775
2776 result
2777 _ContactImpl::SetUrlAt(int index, const Url& url)
2778 {
2779         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);
2780         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));
2781         SysTryReturn(NID_SCL, !__isRemoved || Invalidate() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2782
2783         unsigned int count = 0;
2784         contacts_record_h recordHandle = null;
2785         String stringValue;
2786         int type = 0;
2787         int oriType = 0;
2788
2789         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.url, &count);
2790         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);
2791
2792         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.url, index, &recordHandle);
2793
2794
2795         stringValue = _UrlImpl::GetInstance(url)->GetLabel();
2796
2797         switch (_UrlImpl::GetInstance(url)->GetType())
2798         {
2799                 case URL_TYPE_PERSONAL:
2800                         type = CONTACTS_URL_TYPE_HOME;
2801                         break;
2802                 case URL_TYPE_WORK:
2803                         type = CONTACTS_URL_TYPE_WORK;
2804                         break;
2805                 case URL_TYPE_CUSTOM:
2806                         type = CONTACTS_URL_TYPE_CUSTOM;
2807                         break;
2808                 case URL_TYPE_OTHER:
2809                         contacts_record_get_int(recordHandle, _contacts_url.type, &oriType);
2810
2811                         if (oriType == CONTACTS_URL_TYPE_CUSTOM && (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat()))
2812                         {
2813                                 char* pCharValue = null;
2814
2815                                 contacts_record_get_str_p(recordHandle, _contacts_url.label, &pCharValue);
2816
2817                                 stringValue = pCharValue;
2818                                 type = CONTACTS_URL_TYPE_CUSTOM;
2819                         }
2820                         else
2821                         {
2822                                 type = CONTACTS_URL_TYPE_OTHER;
2823                         }
2824                         break;
2825                 default:
2826                         type = CONTACTS_URL_TYPE_OTHER;
2827                         break;
2828         }
2829
2830         // set type
2831         contacts_record_set_int(recordHandle, _contacts_url.type, type);
2832
2833         // set label
2834         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2835         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2836
2837         contacts_record_set_str(recordHandle, _contacts_url.label, pCharArray.get());
2838
2839         // set url
2840         stringValue = _UrlImpl::GetInstance(url)->GetUrl();
2841         pCharArray.reset(_StringConverter::CopyToCharArrayN(stringValue));
2842         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2843
2844         contacts_record_set_str(recordHandle, _contacts_url.url, pCharArray.get());
2845
2846         return E_SUCCESS;
2847 }
2848
2849 result
2850 _ContactImpl::SetAddressAt(int index, const Address& address)
2851 {
2852         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);
2853         SysTryReturn(NID_SCL,
2854                                           !address.GetCity().IsEmpty() ||
2855                                           !address.GetCountry().IsEmpty() ||
2856                                           !address.GetExtended().IsEmpty() ||
2857                                           !address.GetPostalCode().IsEmpty() ||
2858                                           !address.GetPostOfficeBoxNumber().IsEmpty() ||
2859                                           !address.GetState().IsEmpty() ||
2860                                           !address.GetStreet().IsEmpty(),
2861                                           E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The address is empty.", GetErrorMessage(E_INVALID_ARG));
2862         SysTryReturn(NID_SCL, !__isRemoved || Invalidate() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2863
2864         unsigned int count = 0;
2865         contacts_record_h recordHandle = null;
2866         int type = 0;
2867         int oriType = 0;
2868         String stringValue;
2869
2870         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.address, &count);
2871         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);
2872
2873         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.address, index, &recordHandle);
2874
2875         stringValue = _AddressImpl::GetInstance(address)->GetLabel();
2876
2877         switch (_AddressImpl::GetInstance(address)->GetType())
2878         {
2879                 case ADDRESS_TYPE_HOME:
2880                         type = CONTACTS_ADDRESS_TYPE_HOME;
2881                         break;
2882                 case ADDRESS_TYPE_WORK:
2883                         type = CONTACTS_ADDRESS_TYPE_WORK;
2884                         break;
2885                 case ADDRESS_TYPE_CUSTOM:
2886                         type = CONTACTS_ADDRESS_TYPE_CUSTOM;
2887                         break;
2888                 case ADDRESS_TYPE_OTHER:
2889                         contacts_record_get_int(recordHandle, _contacts_address.type, &oriType);
2890
2891                         if (oriType == CONTACTS_ADDRESS_TYPE_CUSTOM && (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat()))
2892                         {
2893                                 char* pCharValue = null;
2894
2895                                 contacts_record_get_str_p(recordHandle, _contacts_address.label, &pCharValue);
2896
2897                                 stringValue = pCharValue;
2898                                 type = CONTACTS_ADDRESS_TYPE_CUSTOM;
2899                         }
2900                         else
2901                         {
2902                                 type = CONTACTS_ADDRESS_TYPE_OTHER;
2903                         }
2904                         break;
2905                 default:
2906                         type = CONTACTS_ADDRESS_TYPE_OTHER;
2907                         break;
2908         }
2909
2910         // set type
2911         contacts_record_set_int(recordHandle, _contacts_address.type, type);
2912
2913         // set label
2914         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2915         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2916
2917         contacts_record_set_str(recordHandle, _contacts_address.label, pCharArray.get());
2918
2919         // address
2920         stringValue = _AddressImpl::GetInstance(address)->GetCity();
2921         if (!stringValue.IsEmpty())
2922         {
2923                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2924                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2925
2926                 contacts_record_set_str(recordHandle, _contacts_address.locality, pCharArray.get());
2927         }
2928         else
2929         {
2930                 contacts_record_set_str(recordHandle, _contacts_address.locality, null);
2931         }
2932
2933         stringValue = _AddressImpl::GetInstance(address)->GetCountry();
2934         if (!stringValue.IsEmpty())
2935         {
2936                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2937                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2938
2939                 contacts_record_set_str(recordHandle, _contacts_address.country, pCharArray.get());
2940         }
2941         else
2942         {
2943                 contacts_record_set_str(recordHandle, _contacts_address.country, null);
2944         }
2945
2946         stringValue = _AddressImpl::GetInstance(address)->GetExtended();
2947         if (!stringValue.IsEmpty())
2948         {
2949                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2950                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2951
2952                 contacts_record_set_str(recordHandle, _contacts_address.extended, pCharArray.get());
2953         }
2954         else
2955         {
2956                 contacts_record_set_str(recordHandle, _contacts_address.extended, null);
2957         }
2958
2959         stringValue = _AddressImpl::GetInstance(address)->GetPostalCode();
2960         if (!stringValue.IsEmpty())
2961         {
2962                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2963                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2964
2965                 contacts_record_set_str(recordHandle, _contacts_address.postal_code, pCharArray.get());
2966         }
2967         else
2968         {
2969                 contacts_record_set_str(recordHandle, _contacts_address.postal_code, null);
2970         }
2971
2972         stringValue = _AddressImpl::GetInstance(address)->GetPostOfficeBoxNumber();
2973         if (!stringValue.IsEmpty())
2974         {
2975                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2976                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2977
2978                 contacts_record_set_str(recordHandle, _contacts_address.postbox, pCharArray.get());
2979         }
2980         else
2981         {
2982                 contacts_record_set_str(recordHandle, _contacts_address.postbox, null);
2983         }
2984
2985         stringValue = _AddressImpl::GetInstance(address)->GetState();
2986         if (!stringValue.IsEmpty())
2987         {
2988                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
2989                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2990
2991                 contacts_record_set_str(recordHandle, _contacts_address.region, pCharArray.get());
2992         }
2993         else
2994         {
2995                 contacts_record_set_str(recordHandle, _contacts_address.region, null);
2996         }
2997
2998         stringValue = _AddressImpl::GetInstance(address)->GetStreet();
2999         if (!stringValue.IsEmpty())
3000         {
3001                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(stringValue));
3002                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3003
3004                 contacts_record_set_str(recordHandle, _contacts_address.street, pCharArray.get());
3005         }
3006         else
3007         {
3008                 contacts_record_set_str(recordHandle, _contacts_address.street, null);
3009         }
3010
3011         return E_SUCCESS;
3012 }
3013
3014 result
3015 _ContactImpl::SetImAddressAt(int index, const ImAddress& imAddress)
3016 {
3017         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);
3018         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));
3019         SysTryReturn(NID_SCL, !__isRemoved || Invalidate() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3020
3021         unsigned int count = 0;
3022         contacts_record_h messengerHandle = null;
3023         String stringValue;
3024         int type = 0;
3025         std::unique_ptr<char[]> pCharArray(null);
3026
3027         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.messenger, &count);
3028         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);
3029
3030         contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.messenger, index, &messengerHandle);
3031
3032         stringValue = _ImAddressImpl::GetInstance(imAddress)->GetServiceProviderName();
3033
3034         if (stringValue == IM_ADDRESS_GOOGLE_TALK)
3035         {
3036                 type = CONTACTS_MESSENGER_TYPE_GOOGLE;
3037         }
3038         else if (stringValue == IM_ADDRESS_MSN)
3039         {
3040                 type = CONTACTS_MESSENGER_TYPE_WLM;
3041         }
3042         else if (stringValue == IM_ADDRESS_ICQ)
3043         {
3044                 type = CONTACTS_MESSENGER_TYPE_ICQ;
3045         }
3046         else if (stringValue == IM_ADDRESS_AIM)
3047         {
3048                 type = CONTACTS_MESSENGER_TYPE_AIM;
3049         }
3050         else if (stringValue == IM_ADDRESS_YAHOO)
3051         {
3052                 type = CONTACTS_MESSENGER_TYPE_YAHOO;
3053         }
3054         else if (stringValue == IM_ADDRESS_QQ)
3055         {
3056                 type = CONTACTS_MESSENGER_TYPE_QQ;
3057         }
3058         else if (stringValue == IM_ADDRESS_SKYPE)
3059         {
3060                 type = CONTACTS_MESSENGER_TYPE_SKYPE;
3061         }
3062         else if (stringValue == IM_ADDRESS_JABBER)
3063         {
3064                 type = CONTACTS_MESSENGER_TYPE_JABBER;
3065         }
3066         else
3067         {
3068                 type = CONTACTS_MESSENGER_TYPE_CUSTOM;
3069         }
3070
3071         contacts_record_set_int(messengerHandle, _contacts_messenger.type, type);
3072         if (type == CONTACTS_MESSENGER_TYPE_CUSTOM)
3073         {
3074                 pCharArray.reset(_StringConverter::CopyToCharArrayN(stringValue));
3075                 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3076
3077                 contacts_record_set_str(messengerHandle, _contacts_messenger.label, pCharArray.get());
3078         }
3079
3080         stringValue = _ImAddressImpl::GetInstance(imAddress)->GetImAddress();
3081         pCharArray.reset(_StringConverter::CopyToCharArrayN(stringValue));
3082         SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3083
3084         contacts_record_set_str(messengerHandle, _contacts_messenger.im_id, pCharArray.get());
3085
3086         return E_SUCCESS;
3087 }
3088
3089 int
3090 _ContactImpl::GetMaxLength(ContactPropertyId id)
3091 {
3092         switch (id)
3093         {
3094         case CONTACT_PROPERTY_ID_FIRST_NAME:
3095                 //fall through
3096         case CONTACT_PROPERTY_ID_LAST_NAME:
3097                 //fall through
3098         case CONTACT_PROPERTY_ID_DISPLAY_NAME:
3099                 //fall through
3100         case CONTACT_PROPERTY_ID_NICK_NAME:
3101                 //fall through
3102         case CONTACT_PROPERTY_ID_MIDDLE_NAME:
3103                 return MAX_CONTACT_NAME_LENGTH;
3104
3105         case CONTACT_PROPERTY_ID_THUMBNAIL:
3106                 return -1;
3107
3108         case CONTACT_PROPERTY_ID_JOB_TITLE:
3109                 return MAX_CONTACT_JOB_TITLE_LENGTH;
3110
3111         case CONTACT_PROPERTY_ID_COMPANY:
3112                 return MAX_CONTACT_COMPANY_LENGTH;
3113
3114         case CONTACT_PROPERTY_ID_NOTE:
3115                 return MAX_CONTACT_NOTE_LENGTH;
3116
3117         case CONTACT_PROPERTY_ID_RINGTONE:
3118                 return -1;
3119
3120         case CONTACT_PROPERTY_ID_PHONETIC_FIRST_NAME:
3121                 //fall through
3122         case CONTACT_PROPERTY_ID_PHONETIC_LAST_NAME:
3123                 //fall through
3124         case CONTACT_PROPERTY_ID_PHONETIC_MIDDLE_NAME:
3125                 return MAX_CONTACT_NAME_LENGTH;
3126
3127         default:
3128                 return -1;
3129         }
3130 }
3131
3132 IList*
3133 _ContactImpl::GetOrganizationsN(void) const
3134 {
3135         result r = E_SUCCESS;
3136         contacts_record_h organizationHandle = null;
3137         int ret = CONTACTS_ERROR_NONE;
3138         char* pCharValue = null;
3139         int intValue = 0;
3140         unsigned int count = 0;
3141         std::unique_ptr<Organization> pOrganization(null);
3142
3143         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
3144         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3145
3146         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.company, &count);
3147
3148         r = pList->Construct(count);
3149         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3150
3151         for (unsigned int i = 0; i < count; i++)
3152         {
3153                 ret = contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.company, i, &organizationHandle);
3154                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
3155
3156                 pOrganization.reset(new (std::nothrow) Organization());
3157                 SysTryReturn(NID_SCL, pOrganization != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3158
3159                 // name
3160                 contacts_record_get_str_p(organizationHandle, _contacts_company.name, &pCharValue);
3161                 pOrganization->SetName(pCharValue);
3162
3163                 // job title
3164                 contacts_record_get_str_p(organizationHandle, _contacts_company.job_title, &pCharValue);
3165                 pOrganization->SetJobTitle(pCharValue);
3166
3167                 // department
3168                 contacts_record_get_str_p(organizationHandle, _contacts_company.department, &pCharValue);
3169                 pOrganization->SetDepartment(pCharValue);
3170
3171                 // role
3172                 contacts_record_get_str_p(organizationHandle, _contacts_company.role, &pCharValue);
3173                 pOrganization->SetRole(pCharValue);
3174
3175                 // agent
3176                 contacts_record_get_str_p(organizationHandle, _contacts_company.assistant_name, &pCharValue);
3177                 pOrganization->SetAgent(pCharValue);
3178
3179                 // type
3180                 contacts_record_get_int(organizationHandle, _contacts_company.type, &intValue);
3181                 switch (intValue)
3182                 {
3183                         case CONTACTS_COMPANY_TYPE_WORK:
3184                                 pOrganization->SetType(ORGANIZATION_TYPE_WORK);
3185                                 break;
3186                         case CONTACTS_COMPANY_TYPE_CUSTOM:
3187                                 pOrganization->SetType(ORGANIZATION_TYPE_CUSTOM);
3188                                 break;
3189                         case CONTACTS_COMPANY_TYPE_OTHER:
3190                                 // fall through
3191                         default:
3192                                 pOrganization->SetType(ORGANIZATION_TYPE_OTHER);
3193                                 break;
3194                 }
3195
3196                 // label
3197                 contacts_record_get_str_p(organizationHandle, _contacts_company.label, &pCharValue);
3198                 pOrganization->SetLabel(pCharValue);
3199
3200                 // description
3201                 contacts_record_get_str_p(organizationHandle, _contacts_company.description, &pCharValue);
3202                 pOrganization->SetDescription(pCharValue);
3203
3204                 // location
3205                 contacts_record_get_str_p(organizationHandle, _contacts_company.location, &pCharValue);
3206                 pOrganization->SetLocation(pCharValue);
3207
3208                 // phonetic name
3209                 contacts_record_get_str_p(organizationHandle, _contacts_company.phonetic_name, &pCharValue);
3210                 pOrganization->SetPhoneticName(pCharValue);
3211
3212                 // logo path
3213                 contacts_record_get_str_p(organizationHandle, _contacts_company.logo, &pCharValue);
3214                 _OrganizationImpl::GetInstance(*pOrganization)->SetLogoPath(pCharValue);
3215
3216                 r = pList->Add(*pOrganization);
3217                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3218
3219                 pOrganization.release();
3220         }
3221
3222         return pList.release();
3223 }
3224
3225 IList*
3226 _ContactImpl::GetRelationshipsN(void) const
3227 {
3228         result r = E_SUCCESS;
3229         contacts_record_h relationshipHandle = null;
3230         int ret = CONTACTS_ERROR_NONE;
3231         int intValue = 0;
3232         char* pCharValue = null;
3233         unsigned int count = 0;
3234         std::unique_ptr<Relationship> pRelationship(null);
3235
3236         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
3237         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3238
3239         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.relationship, &count);
3240
3241         r = pList->Construct(count);
3242         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3243
3244         for (unsigned int i = 0; i < count; i++)
3245         {
3246                 ret = contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.relationship, i, &relationshipHandle);
3247                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
3248
3249                 pRelationship.reset(new (std::nothrow) Relationship());
3250                 SysTryReturn(NID_SCL, pRelationship != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3251
3252                 // type
3253                 contacts_record_get_int(relationshipHandle, _contacts_relationship.type, &intValue);
3254                 switch (intValue)
3255                 {
3256                 case CONTACTS_RELATIONSHIP_TYPE_ASSISTANT:
3257                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_ASSISTANT);
3258                         break;
3259                 case CONTACTS_RELATIONSHIP_TYPE_BROTHER:
3260                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_BROTHER);
3261                         break;
3262                 case CONTACTS_RELATIONSHIP_TYPE_CHILD:
3263                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_CHILD);
3264                         break;
3265                 case CONTACTS_RELATIONSHIP_TYPE_DOMESTIC_PARTNER:
3266                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_DOMESTIC_PARTNER);
3267                         break;
3268                 case CONTACTS_RELATIONSHIP_TYPE_FATHER:
3269                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_FATHER);
3270                         break;
3271                 case CONTACTS_RELATIONSHIP_TYPE_FRIEND:
3272                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_FRIEND);
3273                         break;
3274                 case CONTACTS_RELATIONSHIP_TYPE_MANAGER:
3275                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_MANAGER);
3276                         break;
3277                 case CONTACTS_RELATIONSHIP_TYPE_MOTHER:
3278                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_MOTHER);
3279                         break;
3280                 case CONTACTS_RELATIONSHIP_TYPE_PARENT:
3281                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_PARENT);
3282                         break;
3283                 case CONTACTS_RELATIONSHIP_TYPE_PARTNER:
3284                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_PARTNER);
3285                         break;
3286                 case CONTACTS_RELATIONSHIP_TYPE_REFERRED_BY:
3287                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_REFERRED_BY);
3288                         break;
3289                 case CONTACTS_RELATIONSHIP_TYPE_RELATIVE:
3290                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_RELATIVE);
3291                         break;
3292                 case CONTACTS_RELATIONSHIP_TYPE_SISTER:
3293                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_SISTER);
3294                         break;
3295                 case CONTACTS_RELATIONSHIP_TYPE_SPOUSE:
3296                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_SPOUSE);
3297                         break;
3298                 case CONTACTS_RELATIONSHIP_TYPE_CUSTOM:
3299                         // fall through
3300                 default:
3301                         pRelationship->SetType(CONTACT_RELATIONSHIP_TYPE_CUSTOM);
3302                         break;
3303                 }
3304
3305                 // label
3306                 contacts_record_get_str_p(relationshipHandle, _contacts_relationship.label, &pCharValue);
3307                 pRelationship->SetLabel(pCharValue);
3308
3309                 // name
3310                 contacts_record_get_str_p(relationshipHandle, _contacts_relationship.name, &pCharValue);
3311                 pRelationship->SetRelativeName(pCharValue);
3312
3313                 r = pList->Add(*pRelationship);
3314                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3315
3316                 pRelationship.release();
3317         }
3318
3319         return pList.release();
3320 }
3321
3322 IList*
3323 _ContactImpl::GetEventsN(void) const
3324 {
3325         result r = E_SUCCESS;
3326         char* pCharValue = null;
3327         int intValue = 0;
3328         unsigned int count = 0;
3329         contacts_record_h eventHandle = null;
3330         std::unique_ptr<ContactEvent> pEvent(null);
3331
3332         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
3333         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3334
3335         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.event, &count);
3336
3337         r = pList->Construct(count);
3338         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3339
3340         for (unsigned int i = 0; i < count; i++)
3341         {
3342                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.event, i, &eventHandle);
3343
3344                 pEvent.reset(new (std::nothrow) ContactEvent());
3345                 SysTryReturn(NID_SCL, pEvent != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3346
3347                 // label
3348                 contacts_record_get_str_p(eventHandle, _contacts_event.label, &pCharValue);
3349                 pEvent->SetLabel(pCharValue);
3350
3351                 // type
3352                 contacts_record_get_int(eventHandle, _contacts_event.type, &intValue);
3353                 switch (intValue)
3354                 {
3355                 case CONTACTS_EVENT_TYPE_BIRTH:
3356                         pEvent->SetType(CONTACT_EVENT_TYPE_BIRTHDAY);
3357                         break;
3358                 case CONTACTS_EVENT_TYPE_ANNIVERSARY:
3359                         pEvent->SetType(CONTACT_EVENT_TYPE_ANNIVERSARY);
3360                         break;
3361                 case CONTACTS_URL_TYPE_CUSTOM:
3362                         pEvent->SetType(CONTACT_EVENT_TYPE_CUSTOM);
3363                         break;
3364                 default:
3365                         pEvent->SetType(CONTACT_EVENT_TYPE_OTHER);
3366                         break;
3367                 }
3368
3369                 DateTime dateTime;
3370
3371                 contacts_record_get_int(eventHandle, _contacts_event.date, &intValue);
3372                 __CONVERT_DATE_TO_DATETIME(intValue, dateTime);
3373                 pEvent->SetDate(dateTime);
3374
3375                 pList->Add(*pEvent);
3376                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3377
3378                 pEvent.release();
3379         }
3380
3381         return pList.release();
3382 }
3383
3384 IList*
3385 _ContactImpl::GetNotesN(void) const
3386 {
3387         result r = E_SUCCESS;
3388         char* pCharValue = null;
3389         unsigned int count = 0;
3390         contacts_record_h noteHandle = null;
3391         std::unique_ptr<String> pNote(null);
3392
3393         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
3394         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3395
3396         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.note, &count);
3397
3398         r = pList->Construct(count);
3399         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3400
3401         for (unsigned int i = 0; i < count; i++)
3402         {
3403                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.note, i, &noteHandle);
3404
3405                 contacts_record_get_str_p(noteHandle, _contacts_note.note, &pCharValue);
3406
3407                 pNote.reset(new (std::nothrow) String(pCharValue));
3408                 SysTryReturn(NID_SCL, pNote != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3409
3410                 pList->Add(*pNote);
3411                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3412
3413                 pNote.release();
3414         }
3415
3416         return pList.release();
3417 }
3418
3419 IList*
3420 _ContactImpl::GetNicknamesN(void) const
3421 {
3422         result r = E_SUCCESS;
3423         char* pCharValue = null;
3424         unsigned int count = 0;
3425         contacts_record_h nicknameHandle = null;
3426         std::unique_ptr<String> pNickname(null);
3427
3428         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
3429         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3430
3431         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.nickname, &count);
3432
3433         r = pList->Construct(count);
3434         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3435
3436         for (unsigned int i = 0; i < count; i++)
3437         {
3438                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.nickname, i, &nicknameHandle);
3439
3440                 contacts_record_get_str_p(nicknameHandle, _contacts_nickname.name, &pCharValue);
3441         
3442                 pNickname.reset(new (std::nothrow) String(pCharValue));
3443                 SysTryReturn(NID_SCL, pNickname != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3444
3445                 pList->Add(*pNickname);
3446                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3447
3448                 pNickname.release();
3449         }
3450
3451         return pList.release();
3452 }
3453
3454 IList*
3455 _ContactImpl::GetPhoneNumbersN(void) const
3456 {
3457         result r = E_SUCCESS;
3458         unsigned int count = 0;
3459         contacts_record_h numberHandle = null;
3460         int intValue = 0;
3461         char* pCharValue = null;
3462         PhoneNumberType type = PHONENUMBER_TYPE_HOME;
3463         std::unique_ptr<PhoneNumber> pPhoneNumber(null);
3464
3465         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
3466         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3467
3468
3469         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.number, &count);
3470
3471         contacts_record_get_int(__contactHandle, _contacts_contact.id, &intValue);
3472
3473         r = pList->Construct(count);
3474         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3475
3476         for (unsigned int i = 0; i < count; i++)
3477         {
3478                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.number, i, &numberHandle);
3479
3480                 pPhoneNumber.reset(new (std::nothrow) PhoneNumber());
3481                 SysTryReturn(NID_SCL, pPhoneNumber != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3482
3483                 contacts_record_get_int(numberHandle, _contacts_number.id, &intValue);
3484                 _PhoneNumberImpl::GetInstance(*pPhoneNumber)->SetRecordId(intValue);
3485
3486                 contacts_record_get_str_p(numberHandle, _contacts_number.label, &pCharValue);
3487                 _PhoneNumberImpl::GetInstance(*pPhoneNumber)->SetLabel(pCharValue);
3488
3489                 contacts_record_get_int(numberHandle, _contacts_number.type, &intValue);
3490
3491                 switch (intValue)
3492                 {
3493                         case CONTACTS_NUMBER_TYPE_HOME:
3494                                 // fall through
3495                         case CONTACTS_NUMBER_TYPE_HOME | CONTACTS_NUMBER_TYPE_VOICE:
3496                                 type = PHONENUMBER_TYPE_HOME;
3497                                 break;
3498                         case CONTACTS_NUMBER_TYPE_WORK:
3499                                 // fall through
3500                         case CONTACTS_NUMBER_TYPE_WORK | CONTACTS_NUMBER_TYPE_VOICE:
3501                                 type = PHONENUMBER_TYPE_WORK;
3502                                 break;
3503                         case CONTACTS_NUMBER_TYPE_CELL:
3504                                 type = PHONENUMBER_TYPE_MOBILE;
3505                                 break;
3506                         case CONTACTS_NUMBER_TYPE_FAX:
3507                                 // fall through
3508                         case CONTACTS_NUMBER_TYPE_FAX | CONTACTS_NUMBER_TYPE_HOME:
3509                                 type = PHONENUMBER_TYPE_HOME_FAX;
3510                                 break;
3511                         case CONTACTS_NUMBER_TYPE_FAX | CONTACTS_NUMBER_TYPE_WORK:
3512                                 type = PHONENUMBER_TYPE_WORK_FAX;
3513                                 break;
3514                         case CONTACTS_NUMBER_TYPE_PAGER:
3515                                 type = PHONENUMBER_TYPE_PAGER;
3516                                 break;
3517                         case CONTACTS_NUMBER_TYPE_CUSTOM:
3518                                 type = PHONENUMBER_TYPE_CUSTOM;
3519                                 break;
3520                         case CONTACTS_NUMBER_TYPE_ASSISTANT:
3521                                 if (_AppInfo::GetApiVersion() < _API_VERSION_2_1)
3522                                 {
3523                                         type = PHONENUMBER_TYPE_OTHER;
3524                                 }
3525                                 else
3526                                 {
3527                                         type = PHONENUMBER_TYPE_ASSISTANT;
3528                                 }
3529                                 break;
3530                         case CONTACTS_NUMBER_TYPE_OTHER:
3531                                 type = PHONENUMBER_TYPE_OTHER;
3532                                 break;
3533                         default:
3534                                 if (intValue & CONTACTS_NUMBER_TYPE_FAX)
3535                                 {
3536                                         type = PHONENUMBER_TYPE_HOME_FAX;
3537                                 }
3538                                 else if (intValue & CONTACTS_NUMBER_TYPE_CELL)
3539                                 {
3540                                         type = PHONENUMBER_TYPE_MOBILE;
3541                                 }
3542                                 else if (intValue & CONTACTS_NUMBER_TYPE_PAGER)
3543                                 {
3544                                         type = PHONENUMBER_TYPE_PAGER;
3545                                 }
3546                                 else if (intValue & CONTACTS_NUMBER_TYPE_HOME)
3547                                 {
3548                                         type = PHONENUMBER_TYPE_HOME;
3549                                 }
3550                                 else if (intValue & CONTACTS_NUMBER_TYPE_WORK)
3551                                 {
3552                                         type = PHONENUMBER_TYPE_WORK;
3553                                 }
3554                                 else if (intValue & CONTACTS_NUMBER_TYPE_VOICE)
3555                                 {
3556                                         type = PHONENUMBER_TYPE_HOME;
3557                                 }
3558                                 else
3559                                 {
3560                                         type = PHONENUMBER_TYPE_OTHER;
3561                                 }
3562                                 break;
3563                 }
3564
3565                 _PhoneNumberImpl::GetInstance(*pPhoneNumber)->SetType(type);
3566
3567                 contacts_record_get_str_p(numberHandle, _contacts_number.number, &pCharValue);
3568                 _PhoneNumberImpl::GetInstance(*pPhoneNumber)->SetPhoneNumber(pCharValue);
3569
3570                 r = pList->Add(*pPhoneNumber);
3571                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3572
3573                 pPhoneNumber.release();
3574         }
3575
3576         return pList.release();
3577 }
3578
3579 IList*
3580 _ContactImpl::GetEmailsN(void) const
3581 {
3582         result r = E_SUCCESS;
3583         contacts_record_h currentHandle = null;
3584         int ret = CONTACTS_ERROR_NONE;
3585         int intValue = 0;
3586         unsigned int count = 0;
3587         char* pCharValue = null;
3588         EmailType type = EMAIL_TYPE_PERSONAL;
3589         std::unique_ptr<Email> pEmail(null);
3590
3591         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
3592         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3593
3594         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.email, &count);
3595
3596         r = pList->Construct(count);
3597         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3598
3599         for (unsigned int i = 0; i < count; i++)
3600         {
3601                 ret = contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.email, i, &currentHandle);
3602                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM))
3603
3604                 pEmail.reset(new (std::nothrow) Email());
3605                 SysTryReturn(NID_SCL, pEmail != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3606
3607                 contacts_record_get_int(currentHandle, _contacts_email.id, &intValue);
3608                 _EmailImpl::GetInstance(*pEmail)->SetRecordId(intValue);
3609
3610                 contacts_record_get_str_p(currentHandle, _contacts_email.label, &pCharValue);
3611                 _EmailImpl::GetInstance(*pEmail)->SetLabel(pCharValue);
3612
3613                 contacts_record_get_int(currentHandle, _contacts_email.type, &intValue);
3614                 switch (intValue)
3615                 {
3616                 case CONTACTS_EMAIL_TYPE_HOME:
3617                         type = EMAIL_TYPE_PERSONAL;
3618                         break;
3619                 case CONTACTS_EMAIL_TYPE_WORK:
3620                         type = EMAIL_TYPE_WORK;
3621                         break;
3622                 case CONTACTS_EMAIL_TYPE_CUSTOM:
3623                         type = EMAIL_TYPE_CUSTOM;
3624                         break;
3625                 case CONTACTS_EMAIL_TYPE_MOBILE:
3626                         if (_AppInfo::GetApiVersion() < _API_VERSION_2_1)
3627                         {
3628                                 type = EMAIL_TYPE_OTHER;
3629                         }
3630                         else
3631                         {
3632                                 type = EMAIL_TYPE_MOBILE;
3633                         }
3634                         break;
3635                 default:
3636                         type = EMAIL_TYPE_OTHER;
3637                         break;
3638                 }
3639
3640                 _EmailImpl::GetInstance(*pEmail)->SetType(type);
3641
3642                 contacts_record_get_str_p(currentHandle, _contacts_email.email, &pCharValue);
3643                 _EmailImpl::GetInstance(*pEmail)->SetEmail(pCharValue);
3644
3645                 r = pList->Add(*pEmail);
3646                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3647
3648                 pEmail.release();
3649         }
3650
3651         return pList.release();
3652 }
3653
3654 IList*
3655 _ContactImpl::GetUrlsN(void) const
3656 {
3657         result r = E_SUCCESS;
3658         char* pCharValue = null;
3659         int intValue = 0;
3660         unsigned int count = 0;
3661         UrlType type = URL_TYPE_PERSONAL;
3662         contacts_record_h urlHandle = null;
3663         std::unique_ptr<Url> pUrl(null);
3664
3665         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
3666         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3667
3668         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.url, &count);
3669
3670         r = pList->Construct(count);
3671         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3672
3673         for (unsigned int i = 0; i < count; i++)
3674         {
3675                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.url, i, &urlHandle);
3676
3677                 pUrl.reset(new (std::nothrow) Url());
3678                 SysTryReturn(NID_SCL, pUrl != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3679
3680                 contacts_record_get_str_p(urlHandle, _contacts_url.label, &pCharValue);
3681                 pUrl->SetLabel(pCharValue);
3682
3683                 contacts_record_get_int(urlHandle, _contacts_url.type, &intValue);
3684                 switch (intValue)
3685                 {
3686                 case CONTACTS_URL_TYPE_HOME:
3687                         type = URL_TYPE_PERSONAL;
3688                         break;
3689                 case CONTACTS_URL_TYPE_WORK:
3690                         type = URL_TYPE_WORK;
3691                         break;
3692                 case CONTACTS_URL_TYPE_CUSTOM:
3693                         type = URL_TYPE_CUSTOM;
3694                         break;
3695                 default:
3696                         type = URL_TYPE_OTHER;
3697                         break;
3698                 }
3699
3700                 pUrl->SetType(type);
3701
3702                 contacts_record_get_str_p(urlHandle, _contacts_url.url, &pCharValue);
3703                 _UrlImpl::GetInstance(*pUrl)->SetUrl(pCharValue);
3704
3705                 pList->Add(*pUrl);
3706                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3707
3708                 pUrl.release();
3709         }
3710
3711         return pList.release();
3712 }
3713
3714 IList*
3715 _ContactImpl::GetAddressesN(void) const
3716 {
3717         result r = E_SUCCESS;
3718         char* pCharValue = null;
3719         int intValue = 0;
3720         unsigned int count = 0;
3721         contacts_record_h addressHandle = 0;
3722         std::unique_ptr<Address> pAddress(null);
3723
3724         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
3725         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3726
3727         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.address, &count);
3728
3729         r = pList->Construct(count);
3730         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3731
3732
3733         for (unsigned int i = 0; i < count; i++)
3734         {
3735                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.address, i, &addressHandle);
3736
3737                 pAddress.reset(new (std::nothrow) Address());
3738                 SysTryReturn(NID_SCL, pAddress != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3739
3740                 contacts_record_get_str_p(addressHandle, _contacts_address.label, &pCharValue);
3741                 pAddress->SetLabel(pCharValue);
3742
3743                 contacts_record_get_int(addressHandle, _contacts_address.type, &intValue);
3744                 switch (intValue)
3745                 {
3746                 case CONTACTS_ADDRESS_TYPE_HOME:
3747                         pAddress->SetType(ADDRESS_TYPE_HOME);
3748                         break;
3749                 case CONTACTS_ADDRESS_TYPE_WORK:
3750                         pAddress->SetType(ADDRESS_TYPE_WORK);
3751                         break;
3752                 case CONTACTS_ADDRESS_TYPE_CUSTOM:
3753                         pAddress->SetType(ADDRESS_TYPE_CUSTOM);
3754                         break;
3755                 default:
3756                         pAddress->SetType(ADDRESS_TYPE_OTHER);
3757                         break;
3758                 }
3759
3760                 // 1. country
3761                 contacts_record_get_str_p(addressHandle, _contacts_address.country, &pCharValue);
3762                 _AddressImpl::GetInstance(*pAddress)->SetCountry(pCharValue);
3763
3764                 // 2. region
3765                 contacts_record_get_str_p(addressHandle, _contacts_address.region, &pCharValue);
3766                 _AddressImpl::GetInstance(*pAddress)->SetState(pCharValue);
3767
3768                 // 3. city
3769                 contacts_record_get_str_p(addressHandle, _contacts_address.locality, &pCharValue);
3770                 _AddressImpl::GetInstance(*pAddress)->SetCity(pCharValue);
3771
3772                 // 4. street
3773                 contacts_record_get_str_p(addressHandle, _contacts_address.street, &pCharValue);
3774                 _AddressImpl::GetInstance(*pAddress)->SetStreet(pCharValue);
3775
3776                 // 5. extended
3777                 contacts_record_get_str_p(addressHandle, _contacts_address.extended, &pCharValue);
3778                 _AddressImpl::GetInstance(*pAddress)->SetExtended(pCharValue);
3779
3780                 // 6. postbox
3781                 contacts_record_get_str_p(addressHandle, _contacts_address.postbox, &pCharValue);
3782                 _AddressImpl::GetInstance(*pAddress)->SetPostOfficeBoxNumber(pCharValue);
3783
3784                 // 7. postal code
3785                 contacts_record_get_str_p(addressHandle, _contacts_address.postal_code, &pCharValue);
3786                 _AddressImpl::GetInstance(*pAddress)->SetPostalCode(pCharValue);
3787
3788                 r = pList->Add(*pAddress);
3789                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3790
3791                 pAddress.release();
3792         }
3793
3794         return pList.release();
3795 }
3796
3797 IList*
3798 _ContactImpl::GetImAddressesN(void) const
3799 {
3800         result r = E_SUCCESS;
3801         char* pCharValue = null;
3802         int intValue = 0;
3803         contacts_record_h messengerHandle = null;
3804         unsigned int count = 0;
3805         std::unique_ptr<ImAddress> pImAddress(null);
3806
3807         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
3808         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3809
3810         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.messenger, &count);
3811
3812         r = pList->Construct(count);
3813         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3814
3815
3816         for (unsigned int i = 0; i < count; i++)
3817         {
3818                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.messenger, i, &messengerHandle);
3819
3820                 pImAddress.reset(new (std::nothrow) ImAddress());
3821                 SysTryReturn(NID_SCL, pImAddress != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3822
3823                 contacts_record_get_int(messengerHandle, _contacts_messenger.type, &intValue);
3824                 switch (intValue)
3825                 {
3826                 case CONTACTS_MESSENGER_TYPE_GOOGLE:
3827                         pImAddress->SetServiceProviderName(IM_ADDRESS_GOOGLE_TALK);
3828                         break;
3829                 case CONTACTS_MESSENGER_TYPE_WLM:
3830                         pImAddress->SetServiceProviderName(IM_ADDRESS_MSN);
3831                         break;
3832                 case CONTACTS_MESSENGER_TYPE_ICQ:
3833                         pImAddress->SetServiceProviderName(IM_ADDRESS_ICQ);
3834                         break;
3835                 case CONTACTS_MESSENGER_TYPE_AIM:
3836                         pImAddress->SetServiceProviderName(IM_ADDRESS_AIM);
3837                         break;
3838                 case CONTACTS_MESSENGER_TYPE_YAHOO:
3839                         pImAddress->SetServiceProviderName(IM_ADDRESS_YAHOO);
3840                         break;
3841                 case CONTACTS_MESSENGER_TYPE_QQ:
3842                         pImAddress->SetServiceProviderName(IM_ADDRESS_QQ);
3843                         break;
3844                 case CONTACTS_MESSENGER_TYPE_SKYPE:
3845                         pImAddress->SetServiceProviderName(IM_ADDRESS_SKYPE);
3846                         break;
3847                 case CONTACTS_MESSENGER_TYPE_JABBER:
3848                         pImAddress->SetServiceProviderName(IM_ADDRESS_JABBER);
3849                         break;
3850                 case CONTACTS_MESSENGER_TYPE_CUSTOM:
3851                         // fall through
3852                 default:
3853                         contacts_record_get_str_p(messengerHandle, _contacts_messenger.label, &pCharValue);
3854                         pImAddress->SetServiceProviderName(pCharValue);
3855                         break;
3856                 }
3857
3858                 contacts_record_get_str_p(messengerHandle, _contacts_messenger.im_id, &pCharValue);
3859                 _ImAddressImpl::GetInstance(*pImAddress)->SetImAddress(pCharValue);
3860
3861                 r = pList->Add(*pImAddress);
3862                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3863
3864                 pImAddress.release();
3865         }
3866
3867         return pList.release();
3868 }
3869
3870 bool
3871 _ContactImpl::IsEmpty(void) const
3872 {
3873         char* pCharValue = null;
3874         unsigned int count = 0;
3875
3876         contacts_record_get_str_p(__contactHandle, _contacts_contact.ringtone_path, &pCharValue);
3877         if (pCharValue != null)
3878         {
3879                 return false;
3880         }
3881
3882         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.number, &count);
3883         if (count > 0)
3884         {
3885                 return false;
3886         }
3887
3888         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.email, &count);
3889         if (count > 0)
3890         {
3891                 return false;
3892         }
3893
3894         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.url, &count);
3895         if (count > 0)
3896         {
3897                 return false;
3898         }
3899
3900         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.address, &count);
3901         if (count > 0)
3902         {
3903                 return false;
3904         }
3905
3906         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.messenger, &count);
3907         if (count > 0)
3908         {
3909                 return false;
3910         }
3911
3912         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.name, &count);
3913         if (count > 0)
3914         {
3915                 return false;
3916         }
3917
3918         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.company, &count);
3919         if (count > 0)
3920         {
3921                 return false;
3922         }
3923
3924         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.event, &count);
3925         if (count > 0)
3926         {
3927                 return false;
3928         }
3929
3930         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.note, &count);
3931         if (count > 0)
3932         {
3933                 return false;
3934         }
3935
3936         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.nickname, &count);
3937         if (count > 0)
3938         {
3939                 return false;
3940         }
3941
3942         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.image, &count);
3943         if (count > 0)
3944         {
3945                 return false;
3946         }
3947
3948         contacts_record_get_str_p(__contactHandle, _contacts_contact.uid, &pCharValue);
3949         if (pCharValue != null)
3950         {
3951                 return false;
3952         }
3953
3954         return true;
3955 }
3956
3957 AddressbookId
3958 _ContactImpl::GetAddressbookId(void) const
3959 {
3960         int addressbookId = 0;
3961
3962         contacts_record_get_int(__contactHandle, _contacts_contact.address_book_id, &addressbookId);
3963
3964         return addressbookId;
3965 }
3966
3967 PersonId
3968 _ContactImpl::GetPersonId(void) const
3969 {
3970         int personId = 0;
3971
3972         contacts_record_get_int(__contactHandle, _contacts_contact.person_id, &personId);
3973
3974         return personId;
3975 }
3976
3977 void
3978 _ContactImpl::SetAsRemoved(void)
3979 {
3980         __isRemoved = true;
3981 }
3982
3983 bool
3984 _ContactImpl::IsRemoved(void) const
3985 {
3986         return __isRemoved;
3987 }
3988
3989 bool
3990 _ContactImpl::IsFavorite(void) const
3991 {
3992         bool isFavorite = false;
3993         contacts_record_get_bool(__contactHandle, _contacts_contact.is_favorite, &isFavorite);
3994
3995         return isFavorite;
3996 }
3997
3998 void
3999 _ContactImpl::SetAsFavorite(bool isFavorite)
4000 {
4001         contacts_record_set_bool(__contactHandle, _contacts_contact.is_favorite, isFavorite);
4002 }
4003
4004 result
4005 _ContactImpl::Invalidate(void)
4006 {
4007         int ret = CONTACTS_ERROR_NONE;
4008         unsigned int i = 0;
4009         unsigned int count = 0;
4010         char* pCharValue = null;
4011         int intValue = 0;
4012         bool boolValue = false;
4013
4014         contacts_record_h contactHandle = null;
4015         contacts_record_h sourceRecordHandle = null;
4016         contacts_record_h destRecordHandle = null;
4017
4018         ret = contacts_record_create(_contacts_contact._uri, &contactHandle);
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 recordHandle(contactHandle);
4022
4023         // favorite
4024         contacts_record_get_bool(sourceRecordHandle, _contacts_contact.is_favorite, &boolValue);
4025         contacts_record_set_bool(destRecordHandle, _contacts_contact.is_favorite, boolValue);
4026
4027         // uid
4028         contacts_record_get_str_p(sourceRecordHandle, _contacts_contact.uid, &pCharValue);
4029         contacts_record_set_str(destRecordHandle, _contacts_contact.uid, pCharValue);
4030
4031         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4032         // name
4033         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4034         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.name, &count);
4035         if (count > 0)
4036         {
4037                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.name, 0, &sourceRecordHandle);
4038
4039                 ret = contacts_record_create(_contacts_name._uri, &destRecordHandle);
4040                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
4041
4042                 __ContactsRecordHandle nameHandle(destRecordHandle);
4043
4044                 // 1. first
4045                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.first, &pCharValue);
4046                 contacts_record_set_str(destRecordHandle, _contacts_name.first, pCharValue);
4047
4048                 // 2. last
4049                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.last, &pCharValue);
4050                 contacts_record_set_str(destRecordHandle, _contacts_name.last, pCharValue);
4051
4052                 // 3. addition
4053                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.addition, &pCharValue);
4054                 contacts_record_set_str(destRecordHandle, _contacts_name.addition, pCharValue);
4055
4056                 // 4. suffix
4057                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.suffix, &pCharValue);
4058                 contacts_record_set_str(destRecordHandle, _contacts_name.suffix, pCharValue);
4059
4060                 // 5. prefix
4061                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.prefix, &pCharValue);
4062                 contacts_record_set_str(destRecordHandle, _contacts_name.prefix, pCharValue);
4063
4064                 // 6. phonetic_first
4065                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.phonetic_first, &pCharValue);
4066                 contacts_record_set_str(destRecordHandle, _contacts_name.phonetic_first, pCharValue);
4067
4068                 // 7. phonetic_last
4069                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.phonetic_last, &pCharValue);
4070                 contacts_record_set_str(destRecordHandle, _contacts_name.phonetic_last, pCharValue);
4071
4072                 // 8. phonetic_middle
4073                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.phonetic_middle, &pCharValue);
4074                 contacts_record_set_str(destRecordHandle, _contacts_name.phonetic_middle, pCharValue);
4075
4076                 contacts_record_add_child_record(contactHandle, _contacts_contact.name, destRecordHandle);
4077
4078                 nameHandle.Release();
4079         }
4080
4081         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4082         // image
4083         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4084         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.image, &count);
4085         if (count > 0)
4086         {
4087                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.image, 0, &sourceRecordHandle);
4088
4089                 ret = contacts_record_create(_contacts_image._uri, &destRecordHandle);
4090                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
4091
4092                 __ContactsRecordHandle imageHandle(destRecordHandle);
4093
4094                 contacts_record_get_int(sourceRecordHandle, _contacts_image.type, &intValue);
4095                 contacts_record_set_int(destRecordHandle, _contacts_image.type, intValue);
4096
4097                 contacts_record_get_str_p(sourceRecordHandle, _contacts_image.label, &pCharValue);
4098                 contacts_record_set_str(destRecordHandle, _contacts_image.label, pCharValue);
4099
4100                 contacts_record_get_str_p(sourceRecordHandle, _contacts_image.path, &pCharValue);
4101                 contacts_record_set_str(destRecordHandle, _contacts_image.path, pCharValue);
4102
4103                 contacts_record_add_child_record(contactHandle, _contacts_contact.image, destRecordHandle);
4104
4105                 imageHandle.Release();
4106         }
4107
4108         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4109         // company
4110         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4111         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.company, &count);
4112         for (i = 0; i < count; i++)
4113         {
4114                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.company, i, &sourceRecordHandle);
4115
4116                 ret = contacts_record_create(_contacts_company._uri, &destRecordHandle);
4117                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
4118
4119                 __ContactsRecordHandle companyHandle(destRecordHandle);
4120
4121                 contacts_record_get_int(sourceRecordHandle, _contacts_company.type, &intValue);
4122                 contacts_record_set_int(destRecordHandle, _contacts_company.type, intValue);
4123
4124                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.name, &pCharValue);
4125                 contacts_record_set_str(destRecordHandle, _contacts_company.name, pCharValue);
4126
4127                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.department, &pCharValue);
4128                 contacts_record_set_str(destRecordHandle, _contacts_company.department, pCharValue);
4129
4130                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.job_title, &pCharValue);
4131                 contacts_record_set_str(destRecordHandle, _contacts_company.job_title, pCharValue);
4132
4133                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.assistant_name, &pCharValue);
4134                 contacts_record_set_str(destRecordHandle, _contacts_company.assistant_name, pCharValue);
4135
4136                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.role, &pCharValue);
4137                 contacts_record_set_str(destRecordHandle, _contacts_company.role, pCharValue);
4138
4139                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.logo, &pCharValue);
4140                 contacts_record_set_str(destRecordHandle, _contacts_company.logo, pCharValue);
4141
4142                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.location, &pCharValue);
4143                 contacts_record_set_str(destRecordHandle, _contacts_company.location, pCharValue);
4144
4145                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.description, &pCharValue);
4146                 contacts_record_set_str(destRecordHandle, _contacts_company.description, pCharValue);
4147
4148                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.phonetic_name, &pCharValue);
4149                 contacts_record_set_str(destRecordHandle, _contacts_company.phonetic_name, pCharValue);
4150
4151                 contacts_record_add_child_record(contactHandle, _contacts_contact.company, destRecordHandle);
4152
4153                 companyHandle.Release();
4154         }
4155
4156         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4157         // note
4158         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4159         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.note, &count);
4160         for (i = 0; i < count; i++)
4161         {
4162                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.note, i, &sourceRecordHandle);
4163
4164                 ret = contacts_record_create(_contacts_note._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 noteHandle(destRecordHandle);
4168
4169                 contacts_record_get_str_p(sourceRecordHandle, _contacts_note.note, &pCharValue);
4170                 contacts_record_set_str(destRecordHandle, _contacts_note.note, pCharValue);
4171
4172                 contacts_record_add_child_record(contactHandle, _contacts_contact.note, destRecordHandle);
4173
4174                 noteHandle.Release();
4175         }
4176
4177         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4178         // phone number
4179         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4180         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.number, &count);
4181         for (i = 0; i < count; i++)
4182         {
4183                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.number, i, &sourceRecordHandle);
4184
4185                 ret = contacts_record_create(_contacts_number._uri, &destRecordHandle);
4186                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
4187
4188                 __ContactsRecordHandle numberHandle(destRecordHandle);
4189
4190                 contacts_record_get_int(sourceRecordHandle, _contacts_number.type, &intValue);
4191                 contacts_record_set_int(destRecordHandle, _contacts_number.type, intValue);
4192
4193                 contacts_record_get_str_p(sourceRecordHandle, _contacts_number.label, &pCharValue);
4194                 contacts_record_set_str(destRecordHandle, _contacts_number.label, pCharValue);
4195
4196                 contacts_record_get_str_p(sourceRecordHandle, _contacts_number.number, &pCharValue);
4197                 contacts_record_set_str(destRecordHandle, _contacts_number.number, pCharValue);
4198
4199                 contacts_record_add_child_record(contactHandle, _contacts_contact.number, destRecordHandle);
4200
4201                 numberHandle.Release();
4202         }
4203
4204         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4205         // email
4206         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4207         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.email, &count);
4208         for (i = 0; i < count; i++)
4209         {
4210                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.email, i, &sourceRecordHandle);
4211
4212                 ret = contacts_record_create(_contacts_email._uri, &destRecordHandle);
4213                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
4214
4215                 __ContactsRecordHandle emailHandle(destRecordHandle);
4216
4217                 contacts_record_get_int(sourceRecordHandle, _contacts_email.type, &intValue);
4218                 contacts_record_set_int(destRecordHandle, _contacts_email.type, intValue);
4219
4220                 contacts_record_get_str_p(sourceRecordHandle, _contacts_email.label, &pCharValue);
4221                 contacts_record_set_str(destRecordHandle, _contacts_email.label, pCharValue);
4222
4223                 contacts_record_get_str_p(sourceRecordHandle, _contacts_email.email, &pCharValue);
4224                 contacts_record_set_str(destRecordHandle, _contacts_email.email, pCharValue);
4225
4226                 contacts_record_add_child_record(contactHandle, _contacts_contact.email, destRecordHandle);
4227
4228                 emailHandle.Release();
4229
4230         }
4231
4232         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4233         // event
4234         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4235         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.event, &count);
4236         for (i = 0; i < count; i++)
4237         {
4238                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.event, i, &sourceRecordHandle);
4239
4240                 ret = contacts_record_create(_contacts_event._uri, &destRecordHandle);
4241                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
4242
4243                 __ContactsRecordHandle eventHandle(destRecordHandle);
4244
4245                 contacts_record_get_int(sourceRecordHandle, _contacts_event.type, &intValue);
4246                 contacts_record_set_int(destRecordHandle, _contacts_event.type, intValue);
4247
4248                 contacts_record_get_str_p(sourceRecordHandle, _contacts_event.label, &pCharValue);
4249                 contacts_record_set_str(destRecordHandle, _contacts_event.label, pCharValue);
4250
4251                 contacts_record_get_int(sourceRecordHandle, _contacts_event.date, &intValue);
4252                 contacts_record_set_int(destRecordHandle, _contacts_event.date, intValue);
4253
4254                 contacts_record_add_child_record(contactHandle, _contacts_contact.event, destRecordHandle);
4255
4256                 eventHandle.Release();
4257         }
4258
4259         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4260         // im address
4261         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4262         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.messenger, &count);
4263         for (i = 0; i < count; i++)
4264         {
4265                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.messenger, i, &sourceRecordHandle);
4266
4267                 ret = contacts_record_create(_contacts_messenger._uri, &destRecordHandle);
4268                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
4269
4270                 __ContactsRecordHandle imAddressHandle(destRecordHandle);
4271
4272                 contacts_record_get_int(sourceRecordHandle, _contacts_messenger.type, &intValue);
4273                 contacts_record_set_int(destRecordHandle, _contacts_messenger.type, intValue);
4274
4275                 contacts_record_get_str_p(sourceRecordHandle, _contacts_messenger.label, &pCharValue);
4276                 contacts_record_set_str(destRecordHandle, _contacts_messenger.label, pCharValue);
4277
4278                 contacts_record_get_str_p(sourceRecordHandle, _contacts_messenger.im_id, &pCharValue);
4279                 contacts_record_set_str(destRecordHandle, _contacts_messenger.im_id, pCharValue);
4280
4281                 contacts_record_add_child_record(contactHandle, _contacts_contact.messenger, destRecordHandle);
4282
4283                 imAddressHandle.Release();
4284         }
4285
4286         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4287         // address
4288         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4289         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.address, &count);
4290         for (i = 0; i < count; i++)
4291         {
4292                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.address, i, &sourceRecordHandle);
4293
4294                 ret = contacts_record_create(_contacts_address._uri, &destRecordHandle);
4295                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
4296
4297                 __ContactsRecordHandle addressHandle(destRecordHandle);
4298
4299                 contacts_record_get_int(sourceRecordHandle, _contacts_address.type, &intValue);
4300                 contacts_record_set_int(destRecordHandle, _contacts_address.type, intValue);
4301
4302                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.label, &pCharValue);
4303                 contacts_record_set_str(destRecordHandle, _contacts_address.label, pCharValue);
4304
4305                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.postbox, &pCharValue);
4306                 contacts_record_set_str(destRecordHandle, _contacts_address.postbox, pCharValue);
4307
4308                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.extended, &pCharValue);
4309                 contacts_record_set_str(destRecordHandle, _contacts_address.extended, pCharValue);
4310
4311                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.street, &pCharValue);
4312                 contacts_record_set_str(destRecordHandle, _contacts_address.street, pCharValue);
4313
4314                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.locality, &pCharValue);
4315                 contacts_record_set_str(destRecordHandle, _contacts_address.locality, pCharValue);
4316
4317                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.region, &pCharValue);
4318                 contacts_record_set_str(destRecordHandle, _contacts_address.region, pCharValue);
4319
4320                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.postal_code, &pCharValue);
4321                 contacts_record_set_str(destRecordHandle, _contacts_address.postal_code, pCharValue);
4322
4323                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.country, &pCharValue);
4324                 contacts_record_set_str(destRecordHandle, _contacts_address.country, pCharValue);
4325
4326                 contacts_record_add_child_record(contactHandle, _contacts_contact.address, destRecordHandle);
4327
4328                 addressHandle.Release();
4329         }
4330
4331         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4332         // url
4333         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4334         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.url, &count);
4335         for (i = 0; i < count; i++)
4336         {
4337                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.url, i, &sourceRecordHandle);
4338
4339                 ret = contacts_record_create(_contacts_url._uri, &destRecordHandle);
4340                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
4341
4342                 __ContactsRecordHandle urlHandle(destRecordHandle);
4343
4344                 contacts_record_get_int(sourceRecordHandle, _contacts_url.type, &intValue);
4345                 contacts_record_set_int(destRecordHandle, _contacts_url.type, intValue);
4346
4347                 contacts_record_get_str_p(sourceRecordHandle, _contacts_url.label, &pCharValue);
4348                 contacts_record_set_str(destRecordHandle, _contacts_url.label, pCharValue);
4349
4350                 contacts_record_get_str_p(sourceRecordHandle, _contacts_url.url, &pCharValue);
4351                 contacts_record_set_str(destRecordHandle, _contacts_url.url, pCharValue);
4352
4353                 contacts_record_add_child_record(contactHandle, _contacts_contact.url, destRecordHandle);
4354
4355                 urlHandle.Release();
4356         }
4357
4358         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4359         // nickname
4360         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4361         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.nickname, &count);
4362         for (i = 0; i < count; i++)
4363         {
4364                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.nickname, i, &sourceRecordHandle);
4365
4366                 ret = contacts_record_create(_contacts_nickname._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 nicknameHandle(destRecordHandle);
4370
4371                 contacts_record_get_str_p(sourceRecordHandle, _contacts_nickname.name, &pCharValue);
4372                 contacts_record_set_str(destRecordHandle, _contacts_nickname.name, pCharValue);
4373
4374                 contacts_record_add_child_record(contactHandle, _contacts_contact.nickname, destRecordHandle);
4375
4376                 nicknameHandle.Release();
4377         }
4378
4379         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4380         // relationship
4381         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
4382         contacts_record_get_child_record_count(__contactHandle, _contacts_contact.relationship, &count);
4383         for (i = 0; i < count; i++)
4384         {
4385                 contacts_record_get_child_record_at_p(__contactHandle, _contacts_contact.relationship, i, &sourceRecordHandle);
4386
4387                 ret = contacts_record_create(_contacts_relationship._uri, &destRecordHandle);
4388                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
4389
4390                 __ContactsRecordHandle relationshipHandle(destRecordHandle);
4391
4392                 contacts_record_get_str_p(sourceRecordHandle, _contacts_relationship.name, &pCharValue);
4393                 contacts_record_set_str(destRecordHandle, _contacts_relationship.name, pCharValue);
4394
4395                 contacts_record_add_child_record(contactHandle, _contacts_contact.relationship, destRecordHandle);
4396
4397                 relationshipHandle.Release();
4398         }
4399
4400         contacts_record_destroy(__contactHandle, true);
4401         __contactHandle = contactHandle;
4402
4403         __isRemoved = false;
4404
4405         recordHandle.Release();
4406
4407         return E_SUCCESS;
4408 }
4409
4410 const _ContactImpl*
4411 _ContactImpl::GetInstance(const Contact& contact)
4412 {
4413         return contact.__pContactImpl;
4414 }
4415
4416 _ContactImpl*
4417 _ContactImpl::GetInstance(Contact& contact)
4418 {
4419         return contact.__pContactImpl;
4420 }
4421
4422 }} // Tizen::Social