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