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