Init Tizen 2.2.1
[framework/osp/social.git] / src / FScl_AddressbookImpl.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_AddressbookImpl.cpp
18  * @brief               This is the implementation for _AddressbookImpl class.
19  *
20  * This file contains definitions of @e _AddressbookImpl class.
21  */
22 #include <contacts.h>
23 #include <unique_ptr.h>
24 #include <FApp.h>
25 #include <FBaseColIListT.h>
26 #include <FBaseColArrayListT.h>
27 #include <FBaseResult.h>
28 #include <FBaseLongLong.h>
29 #include <FBaseInteger.h>
30 #include <FIoFile.h>
31 #include <FSclContact.h>
32 #include <FSclCategory.h>
33 #include <FSclUserProfile.h>
34 #include <FSclContactChangeInfo.h>
35 #include <FSclCategoryChangeInfo.h>
36 #include <FSclAddressbook.h>
37 #include <FSclIAddressbookEventListener.h>
38 #include <FSclIAddressbookChangeEventListener.h>
39 #include <FSclIRecordEventListener.h>
40 #include <FApp_AppInfo.h>
41 #include <FBaseSysLog.h>
42 #include <FBase_StringConverter.h>
43 #include "FScl_AddressbookImpl.h"
44 #include "FScl_AddressbookUtil.h"
45 #include "FScl_CategoryChangeInfoImpl.h"
46 #include "FScl_CategoryImpl.h"
47 #include "FScl_ContactChangeInfoImpl.h"
48 #include "FScl_ContactDbMonitor.h"
49 #include "FScl_ContactImpl.h"
50 #include "FScl_RecordImpl.h"
51 #include "FScl_UserProfileImpl.h"
52 #include "FScl_ContactDbConnector.h"
53
54 using namespace std;
55 using namespace Tizen::App;
56 using namespace Tizen::Base;
57 using namespace Tizen::Base::Collection;
58 using namespace Tizen::Graphics;
59 using namespace Tizen::Io;
60
61 namespace Tizen { namespace Social
62 {
63
64 _AddressbookImpl::_AddressbookImpl(void)
65         : __pIRecordEventListener(null)
66         , __pIAddressbookEventListener(null)
67         , __pIAddressbookChangeEventListener(null)
68         , __dbVersionForContact(0)
69         , __dbVersionForGroup(0)
70         , __dbVersionForRelation(0)
71         , __addressbookId(INVALID_ADDRESSBOOK_ID)
72         , __accountId(-1)
73 {
74         // empty body.
75 }
76
77 _AddressbookImpl::~_AddressbookImpl(void)
78 {
79         if (__pIAddressbookEventListener != null || __pIRecordEventListener != null || __pIAddressbookChangeEventListener != null)
80         {
81                 _ContactDbMonitor* pDbMonitor = _ContactDbMonitor::GetInstance();
82                 if (pDbMonitor != null)
83                 {
84                         pDbMonitor->RemoveListener(*this);
85                 }
86         }
87 }
88
89 result
90 _AddressbookImpl::Construct(void)
91 {
92         static AccountId accountId = 0;
93         static String name;
94
95         if (name.IsEmpty())
96         {
97                 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
98                 char *pName = null;
99
100                 unique_ptr<ContactRecord, ContactRecordDeleter> pAbRecord(_AddressbookUtil::GetContactRecordN(_contacts_address_book._uri, DEFAULT_ADDRESSBOOK_ID));
101                 SysTryReturn(NID_SCL, pAbRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
102                 contacts_record_get_str_p(pAbRecord.get(), _contacts_address_book.name, &pName);
103                 contacts_record_get_int(pAbRecord.get(), _contacts_address_book.account_id, &accountId);
104                 name = pName;
105         }
106
107         __addressbookId = DEFAULT_ADDRESSBOOK_ID;
108         __accountId = accountId;
109         __name = name;
110
111         return E_SUCCESS;
112 }
113
114 result
115 _AddressbookImpl::SetRecordEventListener(IRecordEventListener* pListener)
116 {
117         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
118
119         result r = E_SUCCESS;
120
121         if (pListener != null)
122         {
123                 if (__pIAddressbookEventListener == null && __pIRecordEventListener == null && __pIAddressbookChangeEventListener == null)
124                 {
125                         _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
126                         SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
127
128                         r = pContactDbMonitor->AddListener(*this);
129                         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
130                 }
131
132                 __dbVersionForContact = GetLatestVersion();
133                 SysTryReturn(NID_SCL, __dbVersionForContact != -1, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
134
135                 __dbVersionForGroup = __dbVersionForContact;
136                 __dbVersionForRelation = __dbVersionForContact;
137
138                 __pIAddressbookEventListener = null;
139                 __pIAddressbookChangeEventListener = null;
140                 __pIRecordEventListener = pListener;
141         }
142         else
143         {
144                 if (__pIAddressbookEventListener != null || __pIRecordEventListener != null || __pIAddressbookChangeEventListener != null)
145                 {
146                         _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
147                         SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()))
148                         pContactDbMonitor->RemoveListener(*this);
149                 }
150
151                 __pIAddressbookEventListener = null;
152                 __pIAddressbookChangeEventListener = null;
153                 __pIRecordEventListener = null;
154         }
155
156         return E_SUCCESS;
157 }
158
159 result
160 _AddressbookImpl::SetAddressbookEventListener(IAddressbookEventListener* pListener)
161 {
162         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
163
164         result r = E_SUCCESS;
165
166         if (pListener != null)
167         {
168                 if (__pIAddressbookEventListener == null && __pIRecordEventListener == null && __pIAddressbookChangeEventListener == null)
169                 {
170                         _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
171                         SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
172
173                         r = pContactDbMonitor->AddListener(*this);
174                         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
175                 }
176
177                 __dbVersionForContact = GetLatestVersion();
178                 SysTryReturn(NID_SCL, __dbVersionForContact != -1, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
179
180                 __dbVersionForGroup = __dbVersionForContact;
181                 __dbVersionForRelation = __dbVersionForContact;
182
183                 __pIRecordEventListener = null;
184                 __pIAddressbookChangeEventListener = null;
185                 __pIAddressbookEventListener = pListener;
186         }
187         else
188         {
189                 if (__pIAddressbookEventListener != null || __pIRecordEventListener != null || __pIAddressbookChangeEventListener != null)
190                 {
191                         _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
192                         SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
193
194                         pContactDbMonitor->RemoveListener(*this);
195                 }
196
197                 __pIAddressbookEventListener = null;
198                 __pIAddressbookChangeEventListener = null;
199                 __pIRecordEventListener = null;
200         }
201
202         return E_SUCCESS;
203 }
204
205 result
206 _AddressbookImpl::SetAddressbookChangeEventListener(IAddressbookChangeEventListener* pListener)
207 {
208         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
209
210         result r = E_SUCCESS;
211
212         if (pListener != null)
213         {
214                 if (__pIAddressbookEventListener == null && __pIAddressbookChangeEventListener == null && __pIRecordEventListener == null)
215                 {
216                         _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
217                         SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
218
219                         r = pContactDbMonitor->AddListener(*this);
220                         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
221                 }
222
223                 __dbVersionForContact = GetLatestVersion();
224                 SysTryReturn(NID_SCL, __dbVersionForContact != -1, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
225
226                 __dbVersionForGroup = __dbVersionForContact;
227                 __dbVersionForRelation = __dbVersionForContact;
228
229                 __pIRecordEventListener = null;
230                 __pIAddressbookEventListener = null;
231                 __pIAddressbookChangeEventListener = pListener;
232         }
233         else
234         {
235                 if (__pIAddressbookEventListener != null || __pIAddressbookChangeEventListener != null || __pIRecordEventListener != null)
236                 {
237                         _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
238                         SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
239
240                         pContactDbMonitor->RemoveListener(*this);
241                 }
242
243                 __pIAddressbookEventListener = null;
244                 __pIAddressbookChangeEventListener = null;
245                 __pIRecordEventListener = null;
246         }
247
248         return E_SUCCESS;
249 }
250
251 void
252 _AddressbookImpl::SetId(AddressbookId addressbookId)
253 {
254         __addressbookId = addressbookId;
255 }
256
257 void
258 _AddressbookImpl::SetAccountId(AccountId accountId)
259 {
260         __accountId = accountId;
261 }
262
263 void
264 _AddressbookImpl::SetName(const String& name)
265 {
266         __name = name;
267 }
268
269 AddressbookId
270 _AddressbookImpl::GetId(void) const
271 {
272         return __addressbookId;
273 }
274
275 AccountId
276 _AddressbookImpl::GetAccountId(void) const
277 {
278         return __accountId;
279 }
280
281 String
282 _AddressbookImpl::GetName(void) const
283 {
284         return __name;
285 }
286
287 result
288 _AddressbookImpl::AddContact(Contact& contact)
289 {
290         if (_ContactImpl::GetInstance(contact)->IsRemoved())
291         {
292                 result r = _ContactImpl::GetInstance(contact)->Invalidate();
293                 SysTryReturn(NID_SCL, r == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
294         }
295
296         SysTryReturn(NID_SCL, contact.GetRecordId() == INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The ID of specified contact is not INVALID_RECORD_ID.", GetErrorMessage(E_INVALID_ARG));
297         SysTryReturn(NID_SCL, !_ContactImpl::GetInstance(contact)->IsEmpty(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The specified contact does not have any property.", GetErrorMessage(E_INVALID_ARG));
298         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
299
300         contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
301         contacts_record_set_int(recordHandle, _contacts_contact.address_book_id, __addressbookId);
302
303         int recordId = 0;
304         result r = _AddressbookUtil::InsertContactRecordN(recordHandle, recordId);
305         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
306
307         unique_ptr<ContactRecord, ContactRecordDeleter> pContactRecord(_AddressbookUtil::GetContactRecordN(_contacts_contact._uri, recordId));
308         SysTryReturn(NID_SCL, pContactRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
309
310         _ContactImpl::GetInstance(contact)->SetContactRecordHandle(pContactRecord.release());
311         _RecordImpl::GetInstance(contact)->SetRecordId(recordId);
312
313         return E_SUCCESS;
314 }
315
316 result
317 _AddressbookImpl::AddCategory(Category& category)
318 {
319         SysTryReturn(NID_SCL, category.GetRecordId() == INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The specified categoryId is not INVALID_RECORD_ID.", GetErrorMessage(E_INVALID_ARG));
320         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
321
322         contacts_record_h recordHandle = null;
323         unique_ptr<IListT<int> > pList(null);
324
325         recordHandle = _CategoryImpl::GetInstance(category)->GetRecordHandle();
326         contacts_record_set_int(recordHandle, _contacts_group.address_book_id, __addressbookId);
327
328         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
329         {
330                 // It is not allowed to have a same name with other.
331                 int count = 0;
332                 char* pCharValue = null;
333                 contacts_record_get_str_p(recordHandle, _contacts_group.name, &pCharValue);
334
335                 __Filter<__ContactsGroup> filter;
336                 filter.Construct();
337                 filter.AddInt(_contacts_group.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
338                 filter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
339                 filter.AddString(_contacts_group.name, CONTACTS_MATCH_EXACTLY, pCharValue);
340
341                 __Query<__ContactsGroup> query;
342                 query.Construct();
343                 query.SetFilter(filter);
344
345                 count = _AddressbookUtil::GetCountWithQuery(query);
346                 SysTryReturn(NID_SCL, count >= 0, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
347                 SysTryReturn(NID_SCL, count == 0, E_OBJ_ALREADY_EXIST, E_OBJ_ALREADY_EXIST, "[%s] The category name is already being used by other category.", GetErrorMessage(E_OBJ_ALREADY_EXIST));
348         }
349
350         int recordId = 0;
351         result r = _AddressbookUtil::InsertContactRecordN(recordHandle, recordId);
352         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
353
354         unique_ptr<ContactRecord, ContactRecordDeleter> pCategoryRecord(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, recordId));
355         SysTryReturn(NID_SCL, pCategoryRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
356
357         _CategoryImpl::GetInstance(category)->SetRecordHandle(pCategoryRecord.release());
358         _RecordImpl::GetInstance(category)->SetRecordId(recordId);
359
360         pList.reset(_CategoryImpl::GetInstance(category)->GetAddedMembersN());
361         if (pList != null && pList->GetCount() > 0)
362         {
363                 unique_ptr<IEnumeratorT<int> > pEnum(pList->GetEnumeratorN());
364
365                 while (pEnum->MoveNext() == E_SUCCESS)
366                 {
367                         int tableId = -1;
368                         pEnum->GetCurrent(tableId);
369
370                         AddMemberToCategory(category.GetRecordId(), tableId);
371                 }
372
373                 _CategoryImpl::GetInstance(category)->ClearAddedMemberList();
374         }
375
376         return E_SUCCESS;
377 }
378
379 result
380 _AddressbookImpl::RemoveContact(RecordId contactId)
381 {
382         SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The specified contactId is invalid", GetErrorMessage(E_INVALID_ARG));
383         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
384         result r = _AddressbookUtil::DeleteContactRecord(_contacts_contact._uri, contactId);
385         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
386
387         return E_SUCCESS;
388 }
389
390 result
391 _AddressbookImpl::RemoveContact(Contact& contact)
392 {
393         SysTryReturn(NID_SCL, contact.GetRecordId() != INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The ID of specified contact is not INVALID_RECORD_ID", GetErrorMessage(E_INVALID_ARG));
394         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
395
396         result r = RemoveContact(contact.GetRecordId());
397         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
398
399         _ContactImpl::GetInstance(contact)->SetAsRemoved();
400         _RecordImpl::GetInstance(contact)->SetRecordId(INVALID_RECORD_ID);
401
402         return E_SUCCESS;
403 }
404
405 result
406 _AddressbookImpl::RemoveCategory(RecordId categoryId)
407 {
408         SysTryReturn(NID_SCL, categoryId != INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. categoryId = %d.", GetErrorMessage(E_INVALID_ARG), categoryId);
409         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
410         unique_ptr<ContactRecord, ContactRecordDeleter> pCategoryRecord(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, categoryId));
411         SysTryReturn(NID_SCL, pCategoryRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
412
413         bool isReadOnly = false;
414         contacts_record_get_bool(pCategoryRecord.get(), _contacts_group.is_read_only, &isReadOnly);
415         SysTryReturn(NID_SCL, !isReadOnly, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The specified category is a default category.", GetErrorMessage(E_INVALID_ARG));
416
417         result r = _AddressbookUtil::DeleteContactRecord(_contacts_group._uri, categoryId);
418         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
419
420         return E_SUCCESS;
421 }
422
423 result
424 _AddressbookImpl::UpdateContact(const Contact& contact)
425 {
426         RecordId contactId = contact.GetRecordId();
427         SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
428         SysTryReturn(NID_SCL, !_ContactImpl::GetInstance(contact)->IsEmpty(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The specified contact does not have any property.", GetErrorMessage(E_INVALID_ARG));
429         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
430
431         unique_ptr<ContactRecord, ContactRecordDeleter> pContactRecord(_AddressbookUtil::GetContactRecordN(_contacts_simple_contact._uri, contactId));
432         SysTryReturn(NID_SCL, pContactRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
433
434         int intValue = 0;
435         contacts_record_get_int(pContactRecord.get(), _contacts_simple_contact.id, &intValue);
436         SysTryReturn(NID_SCL, intValue == contactId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
437
438         contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
439         SysTryReturn(NID_SCL, recordHandle != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
440
441         result r = _AddressbookUtil::UpdateContactRecord(recordHandle);
442         if (r == E_OBJ_NOT_FOUND)
443         {
444                 unsigned int count = 0;
445                 unsigned int count2 = 0;
446
447                 contacts_record_h newRecordHandle = null;
448                 int ret = contacts_db_get_record(_contacts_contact._uri, contactId, &newRecordHandle);
449                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
450                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
451
452                 contacts_record_get_child_record_count(newRecordHandle, _contacts_contact.image, &count);
453                 contacts_record_get_child_record_count(recordHandle, _contacts_contact.image, &count2);
454
455                 contacts_record_h imageHandle = null;
456                 contacts_record_h imageHandle2 = null;
457
458                 int imageId = -1;
459                 int imageId2 = -1;
460
461                 for (int i = (int)count2 - 1; i >= 0; i--)
462                 {
463                         contacts_record_get_child_record_at_p(recordHandle, _contacts_contact.image, i, &imageHandle);
464                         contacts_record_get_int(imageHandle, _contacts_image.id, &imageId);
465                         if (imageId == 0)
466                         {
467                                 continue;
468                         }
469
470                         bool matchFound = false;
471
472                         for (unsigned int j = 0; j < count; j++)
473                         {
474                                 contacts_record_get_child_record_at_p(newRecordHandle, _contacts_contact.image, i, &imageHandle2);
475                                 contacts_record_get_int(imageHandle2, _contacts_image.id, &imageId2);
476                                 if (imageId == imageId2)
477                                 {
478                                         matchFound = true;
479                                         break;
480                                 }
481                         }
482
483                         if (!matchFound)
484                         {
485                                 contacts_record_remove_child_record(recordHandle, _contacts_contact.image, imageHandle);
486                         }
487                 }
488
489                 contacts_record_destroy(newRecordHandle, true);
490
491                 contacts_record_h copyRecordHandle = CopyContactRecordHandle(recordHandle);
492                 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
493
494                 contacts_record_h tempHandle = null;
495                 contacts_record_get_child_record_count(copyRecordHandle, _contacts_contact.image, &count);
496
497                 char* pCharValue = null;
498                 String tempPath;
499
500                 for (unsigned int i = 0; i < count; i++)
501                 {
502                         contacts_record_get_child_record_at_p(copyRecordHandle, _contacts_contact.image, i, &tempHandle);
503                         contacts_record_get_str(tempHandle, _contacts_image.path, &pCharValue);
504
505                         tempPath = Tizen::App::App::GetInstance()->GetAppRootPath() + L"data/temp";
506                         tempPath.Append((int)i);
507                         tempPath.Append(L".jpg");
508                         String servicePath(pCharValue);
509
510                         File::Copy(servicePath, tempPath, true);
511
512                         std::unique_ptr<char[]> pCharArray( _StringConverter::CopyToCharArrayN(tempPath));
513                         SysTryReturnResult(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
514
515                         contacts_record_set_str(tempHandle, _contacts_image.path, pCharArray.get());
516                         contacts_record_get_str(tempHandle, _contacts_image.path, &pCharValue);
517
518                         tempPath.Clear();
519                 }
520
521                 ret = contacts_db_replace_record(copyRecordHandle, contactId);
522                 contacts_record_destroy(copyRecordHandle, true);
523
524                 for (unsigned int i = 0; i < count; i++)
525                 {
526                         tempPath = Tizen::App::App::GetInstance()->GetAppRootPath() + L"data/temp";
527                         tempPath.Append((int)i);
528                         tempPath.Append(L".jpg");
529
530                         File::Remove(tempPath);
531
532                         tempPath.Clear();
533                 }
534                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
535                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is insufficient.", GetErrorMessage(E_STORAGE_FULL));
536                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
537         }
538         else
539         {
540                 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
541         }
542         pContactRecord.reset(_AddressbookUtil::GetContactRecordN(_contacts_contact._uri, contact.GetRecordId()));
543         SysTryReturn(NID_SCL, pContactRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
544
545         _ContactImpl::GetInstance(*const_cast<Contact*>(&contact))->SetContactRecordHandle(pContactRecord.release());
546
547         return E_SUCCESS;
548 }
549
550 result
551 _AddressbookImpl::UpdateCategory(const Category& category)
552 {
553         RecordId categoryId = category.GetRecordId();
554         SysTryReturn(NID_SCL, categoryId != INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The specified category is invalid.", GetErrorMessage(E_INVALID_ARG));
555         SysTryReturn(NID_SCL, !category.GetName().IsEmpty(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The specified category does not have name.", GetErrorMessage(E_INVALID_ARG));
556         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
557
558         unique_ptr<ContactRecord, ContactRecordDeleter> pCategoryRecord(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, category.GetRecordId()));
559         SysTryReturn(NID_SCL, pCategoryRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
560
561         int intValue = 0;
562         contacts_record_get_int(pCategoryRecord.get(), _contacts_group.id, &intValue);
563         SysTryReturn(NID_SCL, intValue == categoryId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
564
565         contacts_record_h recordHandle = _CategoryImpl::GetInstance(category)->GetRecordHandle();
566
567         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
568         {
569                 int count = 0;
570                 char* pCharValue = null;
571
572                 contacts_record_get_str_p(recordHandle, _contacts_group.name, &pCharValue);
573                 contacts_record_get_int(recordHandle, _contacts_group.id, &intValue);
574
575                 __Filter<__ContactsGroup> filter1;
576                 filter1.Construct();
577                 filter1.AddInt(_contacts_group.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
578                 filter1.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
579                 filter1.AddString(_contacts_group.name, CONTACTS_MATCH_EXACTLY, pCharValue);
580
581                 __Filter<__ContactsGroup> filter2;
582                 filter2.Construct();
583                 filter2.AddInt(_contacts_group.id, CONTACTS_MATCH_LESS_THAN, intValue);
584                 filter2.AddOperator(CONTACTS_FILTER_OPERATOR_OR);
585                 filter2.AddInt(_contacts_group.id, CONTACTS_MATCH_GREATER_THAN, intValue);
586
587                 filter1.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
588                 filter1.AddFilter(filter2);
589
590                 __Query<__ContactsGroup> query;
591                 query.Construct();
592                 query.SetFilter(filter1);
593
594                 count = _AddressbookUtil::GetCountWithQuery(query);
595                 SysTryReturn(NID_SCL, count >= 0, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
596                 SysTryReturn(NID_SCL, count == 0, E_OBJ_ALREADY_EXIST, E_OBJ_ALREADY_EXIST, "[%s] The category name is alread being used by other category.", GetErrorMessage(E_OBJ_ALREADY_EXIST));
597         }
598
599         result r = _AddressbookUtil::UpdateContactRecord(recordHandle);
600         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
601
602         pCategoryRecord.reset(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, category.GetRecordId()));
603         SysTryReturn(NID_SCL, pCategoryRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
604
605         _CategoryImpl::GetInstance(*const_cast<Category*>(&category))->SetRecordHandle(pCategoryRecord.release());
606
607         std::unique_ptr<IListT<int> > pList(_CategoryImpl::GetInstance(category)->GetAddedMembersN());
608         if (pList != null && pList->GetCount() > 0)
609         {
610                 int tableId = -1;
611                 std::unique_ptr<IEnumeratorT<int> > pEnum(pList->GetEnumeratorN());
612                 while (pEnum->MoveNext() == E_SUCCESS)
613                 {
614                         pEnum->GetCurrent(tableId);
615
616                         AddMemberToCategory(category.GetRecordId(), tableId);
617                 }
618
619                 const_cast<_CategoryImpl*>(_CategoryImpl::GetInstance(category))->ClearAddedMemberList();
620         }
621
622         pList.reset(_CategoryImpl::GetInstance(category)->GetRemovedMembersN());
623         if (pList != null && pList->GetCount() > 0)
624         {
625                 int tableId = -1;
626                 std::unique_ptr<IEnumeratorT<int> > pEnum(pList->GetEnumeratorN());
627                 while (pEnum->MoveNext() == E_SUCCESS)
628                 {
629                         pEnum->GetCurrent(tableId);
630
631                         RemoveMemberFromCategory(category.GetRecordId(), tableId);
632                 }
633
634                 const_cast<_CategoryImpl*>(_CategoryImpl::GetInstance(category))->ClearRemovedMemberList();
635         }
636
637         return E_SUCCESS;
638 }
639
640 result
641 _AddressbookImpl::AddMemberToCategory(RecordId categoryId, RecordId contactId)
642 {
643         SysTryReturn(NID_SCL, categoryId != INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. categoryId = %d.", GetErrorMessage(E_INVALID_ARG), categoryId);
644         SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. contactId = %d.", GetErrorMessage(E_INVALID_ARG), contactId);
645         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
646
647         int addressbookId = 0;
648         unique_ptr<ContactRecord, ContactRecordDeleter> pContactRecord(_AddressbookUtil::GetContactRecordN(_contacts_simple_contact._uri, contactId));
649         SysTryReturn(NID_SCL, pContactRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
650         contacts_record_get_int(pContactRecord.get(), _contacts_simple_contact.address_book_id, &addressbookId);
651
652         SysTryReturn(NID_SCL, addressbookId == __addressbookId, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The contact does not exist in this addresbook.", GetErrorMessage(E_INVALID_ARG));
653
654         result r = _AddressbookUtil::AddContactToCategory(categoryId, contactId);
655         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
656
657         return E_SUCCESS;
658 }
659
660 result
661 _AddressbookImpl::RemoveMemberFromCategory(RecordId categoryId, RecordId contactId)
662 {
663         SysTryReturn(NID_SCL, categoryId != INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. categoryId = %d.", GetErrorMessage(E_INVALID_ARG), categoryId);
664         SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. contactId = %d.", GetErrorMessage(E_INVALID_ARG), contactId);
665         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
666
667         int addressbookId = 0;
668         unique_ptr<ContactRecord, ContactRecordDeleter> pContactRecord(_AddressbookUtil::GetContactRecordN(_contacts_simple_contact._uri, contactId));
669         SysTryReturn(NID_SCL, pContactRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
670         contacts_record_get_int(pContactRecord.get(), _contacts_simple_contact.address_book_id, &addressbookId);
671
672         SysTryReturn(NID_SCL, addressbookId == __addressbookId, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The contact does not exist in this addresbook.", GetErrorMessage(E_INVALID_ARG));
673
674         unique_ptr<ContactRecord, ContactRecordDeleter> pCategoryRecord(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, categoryId));
675         SysTryReturn(NID_SCL, pCategoryRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
676         contacts_record_get_int(pCategoryRecord.get(), _contacts_group.address_book_id, &addressbookId);
677
678         SysTryReturn(NID_SCL, addressbookId == __addressbookId, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The category does not exist in this addresbook.", GetErrorMessage(E_INVALID_ARG));
679
680         result r = _AddressbookUtil::RemoveContactFromCategory(categoryId, contactId);
681         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
682
683         return E_SUCCESS;
684 }
685
686 IList*
687 _AddressbookImpl::GetAllCategoriesN(void) const
688 {
689         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
690
691         ClearLastResult();
692
693         __Filter<__ContactsGroup> filter;
694         filter.Construct();
695         filter.AddInt(_contacts_group.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
696
697         __Query<__ContactsGroup> query;
698         query.Construct();
699         query.SetFilter(filter);
700         query.SetSort(_contacts_group.name, true);
701
702
703         IList* pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsGroup, Category>(query);
704         SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
705
706         return pCategories;
707 }
708
709 IList*
710 _AddressbookImpl::GetCategoriesByContactN(RecordId contactId) const
711 {
712         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
713         SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. The specified contact is invalid.", GetErrorMessage(E_INVALID_ARG));
714
715         ClearLastResult();
716
717         __Filter<__ContactsGroupRelation> filter;
718         filter.Construct();
719         filter.AddInt(_contacts_group_relation.contact_id, CONTACTS_MATCH_EQUAL, contactId);
720
721         __Query<__ContactsGroupRelation> query;
722         query.Construct();
723         query.SetFilter(filter);
724         query.SetSort(_contacts_group_relation.name, true);
725
726         IList* pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsGroupRelation, Category>(query);
727         SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
728
729         return pCategories;
730 }
731
732 IList*
733 _AddressbookImpl::GetAllContactsN(void) const
734 {
735         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
736
737         ClearLastResult();
738
739         __Filter<__ContactsContact> filter;
740         filter.Construct();
741         filter.AddInt(_contacts_contact.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
742
743         __Query<__ContactsContact> query;
744         query.Construct();
745         query.SetFilter(filter);
746         query.SetSort(_contacts_contact.display_name, true);
747
748         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
749         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
750
751         return pContacts;
752 }
753
754 IList*
755 _AddressbookImpl::GetContactsByCategoryN(RecordId categoryId) const
756 {
757         SysTryReturn(NID_SCL, categoryId >= INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. The category ID must be greater than or equal to INVALID_RECORD_ID.", GetErrorMessage(E_INVALID_ARG));
758         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
759
760         ClearLastResult();
761
762         __Filter<__ContactsContactGroupRel> filter;
763         filter.Construct();
764         if (categoryId != INVALID_RECORD_ID)
765         {
766                 filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_EQUAL, categoryId);
767         }
768         else
769         {
770                 filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_NONE, 0);
771         }
772
773         __Query<__ContactsContactGroupRel> query;
774         query.Construct();
775         query.SetFilter(filter);
776         query.SetSort(_contacts_contact_grouprel.display_name, true);
777
778         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Contact>(query);
779         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
780
781         return pContacts;
782 }
783
784 IList*
785 _AddressbookImpl::GetContactsN(int pageNo, int countPerPage) const
786 {
787         SysTryReturn(NID_SCL, pageNo > 0 && countPerPage > 0, null, E_OUT_OF_RANGE, "[%s] pageNo(%d) or countPerPage(%d) is less than 1.", GetErrorMessage(E_OUT_OF_RANGE), pageNo, countPerPage);
788         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
789
790         int offset = (pageNo - 1)*countPerPage;
791         int limit = countPerPage;
792
793         ClearLastResult();
794
795         __Filter<__ContactsContact> filter;
796         filter.Construct();
797         filter.AddInt(_contacts_contact.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
798
799         __Query<__ContactsContact> query;
800         query.Construct();
801         query.SetFilter(filter);
802         query.SetSort(_contacts_contact.display_name, true);
803
804         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query, offset, limit);
805         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
806
807         return pContacts;
808 }
809
810 IList*
811 _AddressbookImpl::GetContactsInN(const Category& category, int pageNo, int countPerPage) const
812 {
813         SysTryReturn(NID_SCL, category.GetRecordId() != INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. The specified category is invalid.", GetErrorMessage(E_INVALID_ARG));
814         SysTryReturn(NID_SCL, pageNo > 0 && countPerPage > 0, null, E_OUT_OF_RANGE, "[%s] pageNo(%d) or countPerPage(%d) is less than 1.", GetErrorMessage(E_OUT_OF_RANGE), pageNo, countPerPage);
815         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
816
817         int offset = (pageNo - 1)*countPerPage;
818         int limit = countPerPage;
819
820         ClearLastResult();
821
822         __Filter<__ContactsContactGroupRel> filter;
823         filter.Construct();
824         filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_EQUAL, category.GetRecordId());
825
826         __Query<__ContactsContactGroupRel> query;
827         query.Construct();
828         query.SetFilter(filter);
829         query.SetSort(_contacts_contact_grouprel.display_name, true);
830
831         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Contact>(query, offset, limit);
832         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
833
834         return pContacts;
835 }
836
837 IList*
838 _AddressbookImpl::SearchContactsByEmailN(const String& email) const
839 {
840         SysTryReturn(NID_SCL, !email.IsEmpty(), null, E_INVALID_ARG, "[%s] Invalid argument is used. The specified email is an empty string.", GetErrorMessage(E_INVALID_ARG));
841         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
842
843         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
844         {
845                 SysTryReturn(NID_SCL, email.GetLength() <= MAX_EMAIL_LENGTH, null, E_INVALID_ARG, "[%s] Invalid argument is used. The length of email is greater than MAX_EMAIL_LENGTH.", GetErrorMessage(E_INVALID_ARG));
846         }
847
848         ClearLastResult();
849
850         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(email));
851         SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
852
853         __Filter<__ContactsContactEmail> filter;
854         filter.Construct();
855         filter.AddInt(_contacts_contact_email.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
856         filter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
857         filter.AddString(_contacts_contact_email.email, CONTACTS_MATCH_CONTAINS, pCharArray.get());
858
859         unsigned int projectionIds[1];
860         projectionIds[0] = _contacts_contact_email.contact_id;
861
862         __Query<__ContactsContactEmail> query;
863         query.Construct();
864         query.SetProjection(projectionIds, 1);
865         query.SetFilter(filter);
866         query.SetDistinct(true);
867         query.SetSort(_contacts_contact_email.display_name, true);
868
869         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactEmail, Contact>(query);
870         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
871
872         return pContacts;
873 }
874
875 IList*
876 _AddressbookImpl::SearchContactsByNameN(const String& name) const
877 {
878         SysTryReturn(NID_SCL, !name.IsEmpty(), null, E_INVALID_ARG, "[%s] Invalid argument is used. The specified email is an empty string.", GetErrorMessage(E_INVALID_ARG));
879         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
880
881         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
882         {
883                 SysTryReturn(NID_SCL, name.GetLength() <= MAX_CONTACT_NAME_LENGTH, null, E_INVALID_ARG, "[%s] Invalid argument is used. The length of name is greater than MAX_CONTACT_NAME_LENGTH.", GetErrorMessage(E_INVALID_ARG));
884         }
885
886         ClearLastResult();
887
888         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(name));
889         SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
890
891         __Filter<__ContactsContact> filter;
892         filter.Construct();
893         filter.AddInt(_contacts_contact.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
894         filter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
895         filter.AddString(_contacts_contact.display_name, CONTACTS_MATCH_CONTAINS, pCharArray.get());
896
897         __Query<__ContactsContact> query;
898         query.Construct();
899         query.SetFilter(filter);
900         query.SetSort(_contacts_contact.display_name, true);
901         query.SetDistinct(true);
902
903         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
904         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
905
906         return pContacts;
907 }
908
909
910 IList*
911 _AddressbookImpl::SearchContactsByPhoneNumberN(const String& phoneNumber) const
912 {
913         SysTryReturn(NID_SCL, !phoneNumber.IsEmpty(), null, E_INVALID_ARG, "[%s] Invalid argument is used. The specified phoneNumber is an empty string.", GetErrorMessage(E_INVALID_ARG));
914         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
915
916
917         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
918         {
919                 int count = phoneNumber.GetLength();
920                 SysTryReturn(NID_SCL, count >= MIN_PHONENUMBER_QUERY_LENGTH && count <= MAX_PHONE_NUMBER_LENGTH, null, E_INVALID_ARG, "[%s] Invalid argument is used. The length of phoneNumber is shorter than MIN_PHONENUMBER_QUERY_LENGTH or greater than MAX_PHONE_NUMBER_LENGTH.", GetErrorMessage(E_INVALID_ARG));
921         }
922
923         ClearLastResult();
924
925         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(phoneNumber));
926         SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
927
928         __Filter<__ContactsContactNumber> subFilter;
929         subFilter.Construct();
930         subFilter.AddString(_contacts_contact_number.normalized_number, CONTACTS_MATCH_CONTAINS, pCharArray.get());
931         subFilter.AddOperator(CONTACTS_FILTER_OPERATOR_OR);
932         subFilter.AddString(_contacts_contact_number.number, CONTACTS_MATCH_CONTAINS, pCharArray.get());
933
934         __Filter<__ContactsContactNumber> filter;
935         filter.Construct();
936         filter.AddInt(_contacts_contact_number.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
937         filter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
938         filter.AddFilter(subFilter);
939
940         unsigned int projectionIds[1];
941         projectionIds[0] = _contacts_contact_number.contact_id;
942
943         __Query<__ContactsContactNumber> query;
944         query.Construct();
945         query.SetProjection(projectionIds, 1);
946         query.SetFilter(filter);
947         query.SetDistinct(true);
948         query.SetSort(_contacts_contact_number.display_name, true);
949
950         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactNumber, Contact>(query);
951         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
952
953         return pContacts;
954 }
955
956 int
957 _AddressbookImpl::GetCategoryCount(void) const
958 {
959         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
960
961         int count = -1;
962
963         ClearLastResult();
964
965         __Filter<__ContactsGroup> filter;
966         filter.Construct();
967         filter.AddInt(_contacts_group.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
968
969         __Query<__ContactsGroup> query;
970         query.Construct();
971         query.SetFilter(filter);
972
973         count = _AddressbookUtil::GetCountWithQuery(query);
974         SysTryReturn(NID_SCL, count >= 0, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
975
976         return count;
977 }
978
979 int
980 _AddressbookImpl::GetContactCount(void) const
981 {
982         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
983
984         int count = -1;
985
986         ClearLastResult();
987
988         __Filter<__ContactsContact> filter;
989         filter.Construct();
990         filter.AddInt(_contacts_contact.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
991
992         __Query<__ContactsContact> query;
993         query.Construct();
994         query.SetFilter(filter);
995
996         count = _AddressbookUtil::GetCountWithQuery(query);
997         SysTryReturn(NID_SCL, count >= 0, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
998
999         return count;
1000 }
1001
1002 Contact*
1003 _AddressbookImpl::GetContactN(RecordId contactId) const
1004 {
1005         SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. contactId = %d.", GetErrorMessage(E_INVALID_ARG), contactId);
1006         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1007
1008         ClearLastResult();
1009
1010         unique_ptr<ContactRecord, ContactRecordDeleter> pContactRecord(_AddressbookUtil::GetContactRecordN(_contacts_contact._uri, contactId));
1011         SysTryReturn(NID_SCL, pContactRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1012
1013         int intValue = 0;
1014         contacts_record_get_int(pContactRecord.get(), _contacts_contact.id, &intValue);
1015         SysTryReturn(NID_SCL, intValue == contactId, null, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
1016
1017         unique_ptr<Contact> pContact(new (std::nothrow) Contact());
1018         SysTryReturn(NID_SCL, pContact, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1019
1020         _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(pContactRecord.release());
1021         _RecordImpl::GetInstance(*pContact)->SetRecordId(intValue);
1022
1023         return pContact.release();
1024 }
1025
1026 Category*
1027 _AddressbookImpl::GetCategoryN(RecordId categoryId) const
1028 {
1029         SysTryReturn(NID_SCL, categoryId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. categoryId = %d.", GetErrorMessage(E_INVALID_ARG), categoryId);
1030         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1031
1032         ClearLastResult();
1033
1034         unique_ptr<ContactRecord, ContactRecordDeleter> pCategoryRecord(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, categoryId));
1035         SysTryReturn(NID_SCL, pCategoryRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1036
1037         int intValue = 0;
1038
1039         contacts_record_get_int(pCategoryRecord.get(), _contacts_group.id, &intValue);
1040         SysTryReturn(NID_SCL, categoryId == intValue, null, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
1041
1042         unique_ptr<Category> pCategory(new (std::nothrow) Category());
1043         SysTryReturn(NID_SCL, pCategory != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1044
1045         __Filter<__ContactsGroupRelation> filter;
1046         filter.Construct();
1047         filter.AddInt(_contacts_group_relation.group_id, CONTACTS_MATCH_EQUAL, categoryId);
1048
1049         __Query<__ContactsGroupRelation> query;
1050         query.Construct();
1051         query.SetFilter(filter);
1052
1053         int count = _AddressbookUtil::GetCountWithQuery(query);
1054         SysTryReturn(NID_SCL, count >= 0, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1055
1056         _CategoryImpl::GetInstance(*pCategory)->SetRecordHandle(pCategoryRecord.release());
1057         _CategoryImpl::GetInstance(*pCategory)->SetMemberCount(count);
1058         _RecordImpl::GetInstance(*pCategory)->SetRecordId(categoryId);
1059
1060         return pCategory.release();
1061 }
1062
1063 int
1064 _AddressbookImpl::GetLatestVersion(void) const
1065 {
1066         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1067
1068         int latestVersion = -1;
1069
1070         ClearLastResult();
1071
1072         int ret = contacts_db_get_current_version(&latestVersion);
1073         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, -1, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1074         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1075
1076         return latestVersion;
1077 }
1078
1079 IList*
1080 _AddressbookImpl::GetChangedContactsAfterN(int version, int& latestVersion) const
1081 {
1082         SysTryReturn(NID_SCL, version >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. version %d must be greater that or equal 0.", GetErrorMessage(E_INVALID_ARG), version);
1083         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1084
1085         ClearLastResult();
1086
1087         IList* pChangedContacts = _AddressbookUtil::SearchWithVersionN<__ContactsContactUpdatedInfo, ContactChangeInfo>(__addressbookId, version, latestVersion);
1088         SysTryReturn(NID_SCL, pChangedContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1089
1090         return pChangedContacts;
1091 }
1092
1093 IList*
1094 _AddressbookImpl::GetChangedCategoriesAfterN(int version, int& latestVersion) const
1095 {
1096         SysTryReturn(NID_SCL, version >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. version %d must be greater that or equal 0.", GetErrorMessage(E_INVALID_ARG), version);
1097         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1098
1099         ClearLastResult();
1100
1101         int latestVersion1 = 0;
1102         int latestVersion2 = 0;
1103
1104         std::unique_ptr<IList, AllElementsDeleter> pChangedGroups(_AddressbookUtil::SearchWithVersionN<__ContactsGroupUpdatedInfo, CategoryChangeInfo>(__addressbookId, version, latestVersion1));
1105         SysTryReturn(NID_SCL, pChangedGroups != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1106
1107         std::unique_ptr<IList, AllElementsDeleter> pChangedRelations(_AddressbookUtil::SearchWithVersionN<__ContactsGroupRelUpdatedInfo, CategoryChangeInfo>(__addressbookId, version, latestVersion2));
1108         SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1109
1110         std::unique_ptr<ArrayList, AllElementsDeleter> pChangeList(new (std::nothrow) Tizen::Base::Collection::ArrayList());
1111         SysTryReturn(NID_SCL, pChangeList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1112
1113         result r = pChangeList->AddItems(*pChangedGroups);
1114         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1115
1116         r = pChangeList->AddItems(*pChangedRelations);
1117         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1118
1119         pChangedGroups->RemoveAll(false);
1120         pChangedRelations->RemoveAll(false);
1121
1122         latestVersion = latestVersion2 > latestVersion1 ? latestVersion2 : latestVersion1;
1123
1124         return pChangeList.release();
1125 }
1126
1127 IList*
1128 _AddressbookImpl::GetChangedContactInfoListN(int version, int& latestVersion) const
1129 {
1130         SysTryReturn(NID_SCL, version >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. version %d must be greater that or equal 0.", GetErrorMessage(E_INVALID_ARG), version);
1131         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1132
1133         ClearLastResult();
1134
1135         IList* pChangedContacts = _AddressbookUtil::SearchWithVersionN<__ContactsContactUpdatedInfo, ContactChangeInfo>(__addressbookId, version, latestVersion);
1136         SysTryReturn(NID_SCL, pChangedContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1137
1138         return pChangedContacts;
1139 }
1140
1141 IList*
1142 _AddressbookImpl::GetChangedCategoryInfoListN(int version, int& latestVersion) const
1143 {
1144         return GetChangedGroupsAfterN(version, latestVersion);
1145 }
1146
1147 IList*
1148 _AddressbookImpl::GetChangedGroupsAfterN(int version, int& latestVersion) const
1149 {
1150         SysTryReturn(NID_SCL, version >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. version %d must be greater that or equal 0.", GetErrorMessage(E_INVALID_ARG), version);
1151         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1152
1153         ClearLastResult();
1154
1155         IList* pChangedGroups = _AddressbookUtil::SearchWithVersionN<__ContactsGroupUpdatedInfo, CategoryChangeInfo>(__addressbookId, version, latestVersion);
1156         SysTryReturn(NID_SCL, pChangedGroups != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1157
1158         return pChangedGroups;
1159 }
1160
1161 IList*
1162 _AddressbookImpl::GetChangedRelationsAfterN(int version, int& latestVersion) const
1163 {
1164         SysTryReturn(NID_SCL, version >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. version %d must be greater that or equal 0.", GetErrorMessage(E_INVALID_ARG), version);
1165         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1166
1167         ClearLastResult();
1168
1169         IList* pChangedRelations = _AddressbookUtil::SearchWithVersionN<__ContactsGroupRelUpdatedInfo, CategoryChangeInfo>(__addressbookId, version, latestVersion);
1170         SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1171
1172         return pChangedRelations;
1173 }
1174
1175 result
1176 _AddressbookImpl::AddContacts(const Tizen::Base::Collection::IList& contactList, Tizen::Base::Collection::IListT<RecordId>* pContactIdList)
1177 {
1178         SysTryReturn(NID_SCL, contactList.GetCount() != 0, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. contactList is empty.", GetErrorMessage(E_INVALID_ARG));
1179         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1180
1181         std::unique_ptr<IEnumerator> pContactEnum(contactList.GetEnumeratorN());
1182         SysTryReturnResult(NID_SCL, pContactEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1183
1184         Contact* pContact = null;
1185         int ret = CONTACTS_ERROR_NONE;
1186         int* pContactIds = null;
1187         unsigned int contactsCount = 0;
1188
1189         contacts_record_h recordHandle = null;
1190         contacts_list_h listHandle = null;
1191
1192         ret = contacts_list_create(&listHandle);
1193         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1194
1195         while (pContactEnum->MoveNext() == E_SUCCESS)
1196         {
1197                 pContact = static_cast<Contact*>(pContactEnum->GetCurrent());
1198
1199                 if (_ContactImpl::GetInstance(*pContact)->IsEmpty())
1200                 {
1201                         contacts_list_destroy(listHandle, false);
1202                         SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. Contact is empty.", GetErrorMessage(E_INVALID_ARG));
1203                         return E_INVALID_ARG;
1204                 }
1205                 if (pContact->GetRecordId() != INVALID_RECORD_ID)
1206                 {
1207                         contacts_list_destroy(listHandle, false);
1208                         SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. Contact ID is invalid.", GetErrorMessage(E_INVALID_ARG));
1209                         return E_INVALID_ARG;
1210                 }
1211
1212                 recordHandle = _ContactImpl::GetInstance(*pContact)->GetContactRecordHandle();
1213                 contacts_record_set_int(recordHandle, _contacts_contact.address_book_id, __addressbookId);
1214                 contacts_list_add(listHandle, recordHandle);
1215         }
1216
1217         ret = contacts_db_insert_records(listHandle, &pContactIds, &contactsCount);
1218         contacts_list_destroy(listHandle, false);
1219         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1220         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1221         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1222         SysTryReturn(NID_SCL, pContactIds, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1223
1224         int* pTemp = pContactIds;
1225
1226         if (pContactIdList != null)
1227         {
1228                 for (unsigned int i = 0; i < contactsCount; i++)
1229                 {
1230                         pContactIdList->Add(*pTemp);
1231                         pTemp++;
1232                 }
1233         }
1234
1235         free(pContactIds);
1236
1237         return E_SUCCESS;
1238 }
1239
1240 result
1241 _AddressbookImpl::UpdateContacts(const Tizen::Base::Collection::IList& contactList)
1242 {
1243         SysTryReturn(NID_SCL, contactList.GetCount() != 0, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. contactList is empty.", GetErrorMessage(E_INVALID_ARG));
1244         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1245
1246         std::unique_ptr<IEnumerator> pContactEnum(contactList.GetEnumeratorN());
1247         SysTryReturnResult(NID_SCL, pContactEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1248
1249         Contact* pContact = null;
1250         int ret = CONTACTS_ERROR_NONE;
1251         contacts_record_h recordHandle = null;
1252         contacts_list_h listHandle = null;
1253
1254         ret = contacts_list_create(&listHandle);
1255         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1256
1257         while (pContactEnum->MoveNext() == E_SUCCESS)
1258         {
1259                 pContact = static_cast<Contact*>(pContactEnum->GetCurrent());
1260
1261                 if (_ContactImpl::GetInstance(*pContact)->IsEmpty())
1262                 {
1263                         contacts_list_destroy(listHandle, false);
1264                         SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. Contact is empty.", GetErrorMessage(E_INVALID_ARG));
1265                         return E_INVALID_ARG;
1266                 }
1267                 if (pContact->GetRecordId() == INVALID_RECORD_ID)
1268                 {
1269                         contacts_list_destroy(listHandle, false);
1270                         SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. Contact ID is invalid.", GetErrorMessage(E_INVALID_ARG));
1271                         return E_INVALID_ARG;
1272                 }
1273
1274                 recordHandle = _ContactImpl::GetInstance(*pContact)->GetContactRecordHandle();
1275                 contacts_list_add(listHandle, recordHandle);
1276         }
1277
1278         result r = _AddressbookUtil::UpdateContactRecords(listHandle);
1279         contacts_list_destroy(listHandle, false);
1280         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1281
1282         return E_SUCCESS;
1283 }
1284
1285 result
1286 _AddressbookImpl::RemoveContacts(const Tizen::Base::Collection::IListT<RecordId>& contactIdList)
1287 {
1288         SysTryReturn(NID_SCL, contactIdList.GetCount() != 0, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. contactIdList is empty.", GetErrorMessage(E_INVALID_ARG));
1289         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1290
1291         std::unique_ptr<IEnumeratorT<RecordId> > pContactIdEnum(contactIdList.GetEnumeratorN());
1292         SysTryReturnResult(NID_SCL, pContactIdEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1293
1294         RecordId contactId = INVALID_RECORD_ID;
1295         int ret = CONTACTS_ERROR_NONE;
1296         int count = contactIdList.GetCount();
1297         std::unique_ptr<int[]> pRecordIds(new (std::nothrow) int[count]);
1298         int* pInt = pRecordIds.get();
1299         int* pTmp = pInt;
1300
1301         while (pContactIdEnum->MoveNext() == E_SUCCESS)
1302         {
1303                 pContactIdEnum->GetCurrent(contactId);
1304                 SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. Contact id is invalid record id.", GetErrorMessage(E_INVALID_ARG));
1305
1306                 *pTmp = contactId;
1307                 pTmp++;
1308         }
1309
1310         ret = contacts_db_delete_records(_contacts_contact._uri, pInt, count);
1311         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1312         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE || ret == CONTACTS_ERROR_NO_DATA, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1313
1314         return E_SUCCESS;
1315 }
1316
1317 result
1318 _AddressbookImpl::SetUserProfile(const UserProfile* pUserProfile)
1319 {
1320         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1321
1322         int recordId = 0;
1323         int existingRecordId = 0;
1324         int ret = CONTACTS_ERROR_NONE;
1325         result r = E_SUCCESS;
1326         contacts_record_h recordHandle = null;
1327
1328         std::unique_ptr<UserProfile> pExistingProfile(GetUserProfileN());
1329         SysTryReturn(NID_SCL, pExistingProfile != null || GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1330
1331         if (pExistingProfile != null)
1332         {
1333                 recordHandle = _UserProfileImpl::GetInstance(*pExistingProfile)->GetUserProfileHandle();
1334                 contacts_record_get_int(recordHandle, _contacts_my_profile.id, &existingRecordId);
1335         }
1336
1337         if (pUserProfile != null)
1338         {
1339                 SysTryReturn(NID_SCL, !((_UserProfileImpl::GetInstance(*pUserProfile))->IsEmpty()), E_SYSTEM, E_SYSTEM, "[%s] UserProfile is Empty.", GetErrorMessage(E_SYSTEM));
1340
1341                 recordHandle = _UserProfileImpl::GetInstance(*pUserProfile)->GetUserProfileHandle();
1342                 contacts_record_get_int(recordHandle, _contacts_my_profile.id, &recordId);
1343                 if (recordId == 0)
1344                 {
1345                         if (pExistingProfile != null)
1346                         {
1347                                 r = _AddressbookUtil::DeleteContactRecord(_contacts_my_profile._uri, existingRecordId);
1348                                 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1349                         }
1350
1351                         contacts_record_h newRecordHandle = null;
1352                         ret = contacts_record_clone(recordHandle, &newRecordHandle);
1353                         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1354
1355                         unique_ptr<ContactRecord, ContactRecordDeleter> pContactRecord(newRecordHandle);
1356                         SysTryReturn(NID_SCL, pContactRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1357
1358                         ret = contacts_record_set_int(pContactRecord.get(), _contacts_my_profile.address_book_id, __addressbookId);
1359                         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1360
1361                         int recordId = 0;
1362                         result r = _AddressbookUtil::InsertContactRecordN(pContactRecord.get(), recordId);
1363                         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1364                 }
1365                 else if (recordId > 0)
1366                 {
1367                         if (recordId == existingRecordId)
1368                         {
1369                                 contacts_record_h newRecordHandle = null;
1370                                 ret = contacts_record_clone(recordHandle, &newRecordHandle);
1371                                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1372
1373                                 result r = _AddressbookUtil::UpdateContactRecord(newRecordHandle);
1374                                 contacts_record_destroy(newRecordHandle, true);
1375                                 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1376                         }
1377                         else
1378                         {
1379                                 if (pExistingProfile != null)
1380                                 {
1381                                         r = _AddressbookUtil::DeleteContactRecord(_contacts_my_profile._uri, existingRecordId);
1382                                         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1383                                 }
1384
1385                                 unique_ptr<ContactRecord, ContactRecordDeleter> pContactRecord(CopyMyProfileRecordHandle(recordHandle));
1386                                 SysTryReturn(NID_SCL, pContactRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1387
1388                                 ret = contacts_record_set_int(pContactRecord.get(), _contacts_my_profile.address_book_id, __addressbookId);
1389                                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1390
1391                                 int recordId = 0;
1392
1393                                 r = _AddressbookUtil::InsertContactRecordN(pContactRecord.get(), recordId);
1394                                 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1395                         }
1396                 }
1397         }
1398         else
1399         {
1400                 if (pExistingProfile != null)
1401                 {
1402                         r = _AddressbookUtil::DeleteContactRecord(_contacts_my_profile._uri, existingRecordId);
1403                         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1404                 }
1405         }
1406
1407         return E_SUCCESS;
1408 }
1409
1410 UserProfile*
1411 _AddressbookImpl::GetUserProfileN(void) const
1412 {
1413         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1414
1415         ClearLastResult();
1416
1417         __Filter<__ContactsUserProfile> filter;
1418         filter.Construct();
1419         filter.AddInt(_contacts_my_profile.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
1420
1421         __Query<__ContactsUserProfile> query;
1422         query.Construct();
1423         query.SetFilter(filter);
1424
1425         std::unique_ptr<IList, AllElementsDeleter> pUserProfilesList(_AddressbookUtil::SearchWithQueryN<__ContactsUserProfile, UserProfile>(query));
1426         SysTryReturn(NID_SCL, pUserProfilesList.get() != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1427         SysTryReturn(NID_SCL, pUserProfilesList->GetCount() != 0, null, E_SUCCESS, "No UserProfile Set for this Addressbook.");
1428         SysTryReturn(NID_SCL, pUserProfilesList->GetCount() == 1, null, E_SYSTEM, "[%s] Propagating. More than one UserProfile not allowed.", GetErrorMessage(E_SYSTEM));
1429
1430         UserProfile* pProfile = new (std::nothrow) UserProfile(*(static_cast<UserProfile*>(pUserProfilesList->GetAt(0))));
1431         SysTryReturn(NID_SCL, pProfile != null, null, E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(E_OUT_OF_MEMORY));
1432
1433         return pProfile;
1434 }
1435
1436 bool
1437 _AddressbookImpl::IsUserProfileChangedAfter(int version) const
1438 {
1439         SysTryReturn(NID_SCL, version >= 0, false, E_INVALID_ARG, "[%s] Invalid argument is used. version %d must be greater that or equal 0.", GetErrorMessage(E_INVALID_ARG), version);
1440         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1441
1442         UserProfile* pExistingProfile = GetUserProfileN();
1443         if (pExistingProfile == null)
1444         {
1445                 return false;
1446         }
1447
1448         delete pExistingProfile;
1449
1450         ClearLastResult();
1451
1452         int latestVersion = 0;
1453         int count = _AddressbookUtil::GetChangedItemCountByVersion<__ContactsMyProfileUpdatedInfo>(__addressbookId, version, latestVersion);
1454         SysTryReturn(NID_SCL, count >= 0, false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1455
1456         if (count > 0)
1457         {
1458                 return true;
1459         }
1460
1461         return false;
1462 }
1463
1464 contacts_record_h
1465 _AddressbookImpl::CopyMyProfileRecordHandle(contacts_record_h srcHandle)
1466 {
1467         ClearLastResult();
1468
1469         int ret = CONTACTS_ERROR_NONE;
1470         int intValue = 0;
1471         unsigned int count = 0;
1472         unsigned int i = 0;
1473         char* pCharValue = null;
1474
1475         contacts_record_h contactHandle = null;
1476         contacts_record_h srcRecordHandle = null;
1477         contacts_record_h dstRecordHandle = null;
1478
1479         ret = contacts_record_create(_contacts_my_profile._uri, &contactHandle);
1480         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1481
1482         // name
1483         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.name, &count);
1484         if (count > 0)
1485         {
1486                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.name, 0, &srcRecordHandle);
1487
1488                 ret = contacts_record_create(_contacts_name._uri, &dstRecordHandle);
1489                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1490
1491                 __ContactsRecordHandle nameHandle(dstRecordHandle);
1492
1493                 contacts_record_get_str_p(srcRecordHandle, _contacts_name.first, &pCharValue);
1494                 contacts_record_set_str(dstRecordHandle, _contacts_name.first, pCharValue);
1495
1496                 contacts_record_get_str_p(srcRecordHandle, _contacts_name.last, &pCharValue);
1497                 contacts_record_set_str(dstRecordHandle, _contacts_name.last, pCharValue);
1498
1499                 contacts_record_get_str_p(srcRecordHandle, _contacts_name.addition, &pCharValue);
1500                 contacts_record_set_str(dstRecordHandle, _contacts_name.addition, pCharValue);
1501
1502                 contacts_record_get_str_p(srcRecordHandle, _contacts_name.suffix, &pCharValue);
1503                 contacts_record_set_str(dstRecordHandle, _contacts_name.suffix, pCharValue);
1504
1505                 contacts_record_get_str_p(srcRecordHandle, _contacts_name.prefix, &pCharValue);
1506                 contacts_record_set_str(dstRecordHandle, _contacts_name.prefix, pCharValue);
1507
1508                 contacts_record_get_str_p(srcRecordHandle, _contacts_name.phonetic_first, &pCharValue);
1509                 contacts_record_set_str(dstRecordHandle, _contacts_name.phonetic_first, pCharValue);
1510
1511                 contacts_record_get_str_p(srcRecordHandle, _contacts_name.phonetic_middle, &pCharValue);
1512                 contacts_record_set_str(dstRecordHandle, _contacts_name.phonetic_middle, pCharValue);
1513
1514                 contacts_record_get_str_p(srcRecordHandle, _contacts_name.phonetic_last, &pCharValue);
1515                 contacts_record_set_str(dstRecordHandle, _contacts_name.phonetic_last, pCharValue);
1516
1517                 contacts_record_add_child_record(contactHandle, _contacts_my_profile.name, dstRecordHandle);
1518
1519                 nameHandle.Release();
1520         }
1521
1522         // company
1523         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.company, &count);
1524         for (i = 0; i < count; i++)
1525         {
1526                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.company, i, &srcRecordHandle);
1527
1528                 ret = contacts_record_create(_contacts_company._uri, &dstRecordHandle);
1529                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1530
1531                 __ContactsRecordHandle companyHandle(dstRecordHandle);
1532
1533                 contacts_record_get_int(srcRecordHandle, _contacts_company.type, &intValue);
1534                 contacts_record_set_int(dstRecordHandle, _contacts_company.type, intValue);
1535
1536                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.name, &pCharValue);
1537                 contacts_record_set_str(dstRecordHandle, _contacts_company.name, pCharValue);
1538
1539                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.department, &pCharValue);
1540                 contacts_record_set_str(dstRecordHandle, _contacts_company.department, pCharValue);
1541
1542                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.job_title, &pCharValue);
1543                 contacts_record_set_str(dstRecordHandle, _contacts_company.job_title, pCharValue);
1544
1545                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.assistant_name, &pCharValue);
1546                 contacts_record_set_str(dstRecordHandle, _contacts_company.assistant_name, pCharValue);
1547
1548                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.role, &pCharValue);
1549                 contacts_record_set_str(dstRecordHandle, _contacts_company.role, pCharValue);
1550
1551                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.logo, &pCharValue);
1552                 contacts_record_set_str(dstRecordHandle, _contacts_company.logo, pCharValue);
1553
1554                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.location, &pCharValue);
1555                 contacts_record_set_str(dstRecordHandle, _contacts_company.location, pCharValue);
1556
1557                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.description, &pCharValue);
1558                 contacts_record_set_str(dstRecordHandle, _contacts_company.description, pCharValue);
1559
1560                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.phonetic_name, &pCharValue);
1561                 contacts_record_set_str(dstRecordHandle, _contacts_company.phonetic_name, pCharValue);
1562
1563                 contacts_record_add_child_record(contactHandle, _contacts_my_profile.company, dstRecordHandle);
1564
1565                 companyHandle.Release();
1566         }
1567
1568         // note
1569         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.note, &count);
1570         for (i = 0; i < count; i++)
1571         {
1572                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.note, i, &srcRecordHandle);
1573
1574                 ret = contacts_record_create(_contacts_note._uri, &dstRecordHandle);
1575                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1576
1577                 __ContactsRecordHandle noteHandle(dstRecordHandle);
1578
1579                 contacts_record_get_str_p(srcRecordHandle, _contacts_note.note, &pCharValue);
1580                 contacts_record_set_str(dstRecordHandle, _contacts_note.note, pCharValue);
1581
1582                 contacts_record_add_child_record(contactHandle, _contacts_my_profile.note, dstRecordHandle);
1583
1584                 noteHandle.Release();
1585         }
1586
1587         // number
1588         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.number, &count);
1589         for (i = 0; i < count; i++)
1590         {
1591                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.number, i, &srcRecordHandle);
1592
1593                 ret = contacts_record_create(_contacts_number._uri, &dstRecordHandle);
1594                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1595
1596                 __ContactsRecordHandle numberHandle(dstRecordHandle);
1597
1598                 contacts_record_get_int(srcRecordHandle, _contacts_number.type, &intValue);
1599                 contacts_record_set_int(dstRecordHandle, _contacts_number.type, intValue);
1600
1601                 contacts_record_get_str_p(srcRecordHandle, _contacts_number.label, &pCharValue);
1602                 contacts_record_set_str(dstRecordHandle, _contacts_number.label, pCharValue);
1603
1604                 contacts_record_get_str_p(srcRecordHandle, _contacts_number.number, &pCharValue);
1605                 contacts_record_set_str(dstRecordHandle, _contacts_number.number, pCharValue);
1606
1607                 contacts_record_add_child_record(contactHandle, _contacts_my_profile.number, dstRecordHandle);
1608
1609                 numberHandle.Release();
1610         }
1611
1612         // email
1613         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.email, &count);
1614         for (i = 0; i < count; i++)
1615         {
1616                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.email, i, &srcRecordHandle);
1617
1618                 ret = contacts_record_create(_contacts_email._uri, &dstRecordHandle);
1619                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1620
1621                 __ContactsRecordHandle emailHandle(dstRecordHandle);
1622
1623                 contacts_record_get_int(srcRecordHandle, _contacts_email.type, &intValue);
1624                 contacts_record_set_int(dstRecordHandle, _contacts_email.type, intValue);
1625
1626                 contacts_record_get_str_p(srcRecordHandle, _contacts_email.label, &pCharValue);
1627                 contacts_record_set_str(dstRecordHandle, _contacts_email.label, pCharValue);
1628
1629                 contacts_record_get_str_p(srcRecordHandle, _contacts_email.email, &pCharValue);
1630                 contacts_record_set_str(dstRecordHandle, _contacts_email.email, pCharValue);
1631
1632                 contacts_record_add_child_record(contactHandle, _contacts_my_profile.email, dstRecordHandle);
1633
1634                 emailHandle.Release();
1635
1636         }
1637
1638         // event
1639         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.event, &count);
1640         for (i = 0; i < count; i++)
1641         {
1642                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.event, i, &srcRecordHandle);
1643
1644                 ret = contacts_record_create(_contacts_event._uri, &dstRecordHandle);
1645                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1646
1647                 __ContactsRecordHandle eventHandle(dstRecordHandle);
1648
1649                 contacts_record_get_int(srcRecordHandle, _contacts_event.type, &intValue);
1650                 contacts_record_set_int(dstRecordHandle, _contacts_event.type, intValue);
1651
1652                 contacts_record_get_str_p(srcRecordHandle, _contacts_event.label, &pCharValue);
1653                 contacts_record_set_str(dstRecordHandle, _contacts_event.label, pCharValue);
1654
1655                 contacts_record_get_int(srcRecordHandle, _contacts_event.date, &intValue);
1656                 contacts_record_set_int(dstRecordHandle, _contacts_event.date, intValue);
1657
1658                 contacts_record_add_child_record(contactHandle, _contacts_my_profile.event, dstRecordHandle);
1659
1660                 eventHandle.Release();
1661         }
1662
1663         // im address
1664         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.messenger, &count);
1665         for (i = 0; i < count; i++)
1666         {
1667                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.messenger, i, &srcRecordHandle);
1668
1669                 ret = contacts_record_create(_contacts_messenger._uri, &dstRecordHandle);
1670                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1671
1672                 __ContactsRecordHandle imAddressHandle(dstRecordHandle);
1673
1674                 contacts_record_get_int(srcRecordHandle, _contacts_messenger.type, &intValue);
1675                 contacts_record_set_int(dstRecordHandle, _contacts_messenger.type, intValue);
1676
1677                 contacts_record_get_str_p(srcRecordHandle, _contacts_messenger.label, &pCharValue);
1678                 contacts_record_set_str(dstRecordHandle, _contacts_messenger.label, pCharValue);
1679
1680                 contacts_record_get_str_p(srcRecordHandle, _contacts_messenger.im_id, &pCharValue);
1681                 contacts_record_set_str(dstRecordHandle, _contacts_messenger.im_id, pCharValue);
1682
1683                 contacts_record_add_child_record(contactHandle, _contacts_my_profile.messenger, dstRecordHandle);
1684
1685                 imAddressHandle.Release();
1686         }
1687
1688         // address
1689         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.address, &count);
1690         for (i = 0; i < count; i++)
1691         {
1692                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.address, i, &srcRecordHandle);
1693
1694                 ret = contacts_record_create(_contacts_address._uri, &dstRecordHandle);
1695                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1696
1697                 __ContactsRecordHandle addressHandle(dstRecordHandle);
1698
1699                 contacts_record_get_int(srcRecordHandle, _contacts_address.type, &intValue);
1700                 contacts_record_set_int(dstRecordHandle, _contacts_address.type, intValue);
1701
1702                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.label, &pCharValue);
1703                 contacts_record_set_str(dstRecordHandle, _contacts_address.label, pCharValue);
1704
1705                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.postbox, &pCharValue);
1706                 contacts_record_set_str(dstRecordHandle, _contacts_address.postbox, pCharValue);
1707
1708                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.extended, &pCharValue);
1709                 contacts_record_set_str(dstRecordHandle, _contacts_address.extended, pCharValue);
1710
1711                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.street, &pCharValue);
1712                 contacts_record_set_str(dstRecordHandle, _contacts_address.street, pCharValue);
1713
1714                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.locality, &pCharValue);
1715                 contacts_record_set_str(dstRecordHandle, _contacts_address.locality, pCharValue);
1716
1717                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.region, &pCharValue);
1718                 contacts_record_set_str(dstRecordHandle, _contacts_address.region, pCharValue);
1719
1720                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.postal_code, &pCharValue);
1721                 contacts_record_set_str(dstRecordHandle, _contacts_address.postal_code, pCharValue);
1722
1723                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.country, &pCharValue);
1724                 contacts_record_set_str(dstRecordHandle, _contacts_address.country, pCharValue);
1725
1726                 contacts_record_add_child_record(contactHandle, _contacts_my_profile.address, dstRecordHandle);
1727
1728                 addressHandle.Release();
1729         }
1730
1731         // url
1732         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.url, &count);
1733         for (i = 0; i < count; i++)
1734         {
1735                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.url, i, &srcRecordHandle);
1736
1737                 ret = contacts_record_create(_contacts_url._uri, &dstRecordHandle);
1738                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1739
1740                 __ContactsRecordHandle urlHandle(dstRecordHandle);
1741
1742                 contacts_record_get_int(srcRecordHandle, _contacts_url.type, &intValue);
1743                 contacts_record_set_int(dstRecordHandle, _contacts_url.type, intValue);
1744
1745                 contacts_record_get_str_p(srcRecordHandle, _contacts_url.label, &pCharValue);
1746                 contacts_record_set_str(dstRecordHandle, _contacts_url.label, pCharValue);
1747
1748                 contacts_record_get_str_p(srcRecordHandle, _contacts_url.url, &pCharValue);
1749                 contacts_record_set_str(dstRecordHandle, _contacts_url.url, pCharValue);
1750
1751                 contacts_record_add_child_record(contactHandle, _contacts_my_profile.url, dstRecordHandle);
1752
1753                 urlHandle.Release();
1754         }
1755
1756         // nickname
1757         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.nickname, &count);
1758         for (i = 0; i < count; i++)
1759         {
1760                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.nickname, i, &srcRecordHandle);
1761
1762                 ret = contacts_record_create(_contacts_nickname._uri, &dstRecordHandle);
1763                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1764
1765                 __ContactsRecordHandle nicknameHandle(dstRecordHandle);
1766
1767                 contacts_record_get_str_p(srcRecordHandle, _contacts_nickname.name, &pCharValue);
1768                 contacts_record_set_str(dstRecordHandle, _contacts_nickname.name, pCharValue);
1769
1770                 contacts_record_add_child_record(contactHandle, _contacts_my_profile.nickname, dstRecordHandle);
1771
1772                 nicknameHandle.Release();
1773         }
1774
1775         // relationship
1776         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.relationship, &count);
1777         for (i = 0; i < count; i++)
1778         {
1779                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.relationship, i, &srcRecordHandle);
1780
1781                 ret = contacts_record_create(_contacts_relationship._uri, &dstRecordHandle);
1782                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1783
1784                 __ContactsRecordHandle relationshipHandle(dstRecordHandle);
1785
1786                 contacts_record_get_str_p(srcRecordHandle, _contacts_relationship.name, &pCharValue);
1787                 contacts_record_set_str(dstRecordHandle, _contacts_relationship.name, pCharValue);
1788
1789                 contacts_record_add_child_record(contactHandle, _contacts_my_profile.relationship, dstRecordHandle);
1790
1791                 relationshipHandle.Release();
1792         }
1793
1794         // image
1795         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.image, &count);
1796
1797         int recordId = 0;
1798         contacts_record_get_int(srcHandle, _contacts_my_profile.id, &recordId);
1799
1800         if (count > 0 && recordId == 0)
1801         {
1802                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.image, 0, &srcRecordHandle);
1803
1804                 ret = contacts_record_create(_contacts_image._uri, &dstRecordHandle);
1805                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1806
1807                 __ContactsRecordHandle imageHandle(dstRecordHandle);
1808
1809                 contacts_record_get_int(srcRecordHandle, _contacts_image.type, &intValue);
1810                 contacts_record_set_int(dstRecordHandle, _contacts_image.type, intValue);
1811
1812                 contacts_record_get_str_p(srcRecordHandle, _contacts_image.label, &pCharValue);
1813                 contacts_record_set_str(dstRecordHandle, _contacts_image.label, pCharValue);
1814
1815                 contacts_record_get_str_p(srcRecordHandle, _contacts_image.path, &pCharValue);
1816                 contacts_record_set_str(dstRecordHandle, _contacts_image.path, pCharValue);
1817
1818                 contacts_record_add_child_record(contactHandle, _contacts_contact.image, dstRecordHandle);
1819
1820                 imageHandle.Release();
1821         }
1822
1823         return contactHandle;
1824 }
1825
1826 contacts_record_h
1827 _AddressbookImpl::CopyContactRecordHandle(contacts_record_h srcHandle)
1828 {
1829         ClearLastResult();
1830
1831         int ret = CONTACTS_ERROR_NONE;
1832         unsigned int i = 0;
1833         unsigned int count = 0;
1834         char* pCharValue = null;
1835         int intValue = 0;
1836         bool boolValue = false;
1837
1838         contacts_record_h contactHandle = null;
1839         contacts_record_h sourceRecordHandle = null;
1840         contacts_record_h destRecordHandle = null;
1841
1842         ret = contacts_record_create(_contacts_contact._uri, &contactHandle);
1843         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1844
1845         // favorite
1846         contacts_record_get_bool(srcHandle, _contacts_contact.is_favorite, &boolValue);
1847         contacts_record_set_bool(contactHandle, _contacts_contact.is_favorite, boolValue);
1848
1849         // uid
1850         contacts_record_get_str_p(srcHandle, _contacts_contact.uid, &pCharValue);
1851         contacts_record_set_str(contactHandle, _contacts_contact.uid, pCharValue);
1852
1853         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
1854         // name
1855         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
1856         contacts_record_get_child_record_count(srcHandle, _contacts_contact.name, &count);
1857         if (count > 0)
1858         {
1859                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.name, 0, &sourceRecordHandle);
1860
1861                 ret = contacts_record_create(_contacts_name._uri, &destRecordHandle);
1862                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1863
1864                 __ContactsRecordHandle nameHandle(destRecordHandle);
1865
1866                 // 1. first
1867                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.first, &pCharValue);
1868                 contacts_record_set_str(destRecordHandle, _contacts_name.first, pCharValue);
1869
1870                 // 2. last
1871                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.last, &pCharValue);
1872                 contacts_record_set_str(destRecordHandle, _contacts_name.last, pCharValue);
1873
1874                 // 3. addition
1875                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.addition, &pCharValue);
1876                 contacts_record_set_str(destRecordHandle, _contacts_name.addition, pCharValue);
1877
1878                 // 4. suffix
1879                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.suffix, &pCharValue);
1880                 contacts_record_set_str(destRecordHandle, _contacts_name.suffix, pCharValue);
1881
1882                 // 5. prefix
1883                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.prefix, &pCharValue);
1884                 contacts_record_set_str(destRecordHandle, _contacts_name.prefix, pCharValue);
1885
1886                 // 6. phonetic_first
1887                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.phonetic_first, &pCharValue);
1888                 contacts_record_set_str(destRecordHandle, _contacts_name.phonetic_first, pCharValue);
1889
1890                 // 7. phonetic_last
1891                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.phonetic_last, &pCharValue);
1892                 contacts_record_set_str(destRecordHandle, _contacts_name.phonetic_last, pCharValue);
1893
1894                 // 8. phonetic_middle
1895                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.phonetic_middle, &pCharValue);
1896                 contacts_record_set_str(destRecordHandle, _contacts_name.phonetic_middle, pCharValue);
1897
1898                 contacts_record_add_child_record(contactHandle, _contacts_contact.name, destRecordHandle);
1899
1900                 nameHandle.Release();
1901         }
1902
1903         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
1904         // image
1905         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
1906         contacts_record_get_child_record_count(srcHandle, _contacts_contact.image, &count);
1907         for (i = 0; i < count; i++)
1908         {
1909                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.image, i, &sourceRecordHandle);
1910
1911                 ret = contacts_record_create(_contacts_image._uri, &destRecordHandle);
1912                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1913
1914                 __ContactsRecordHandle imageHandle(destRecordHandle);
1915
1916                 contacts_record_get_int(sourceRecordHandle, _contacts_image.type, &intValue);
1917                 contacts_record_set_int(destRecordHandle, _contacts_image.type, intValue);
1918
1919                 contacts_record_get_str_p(sourceRecordHandle, _contacts_image.label, &pCharValue);
1920                 contacts_record_set_str(destRecordHandle, _contacts_image.label, pCharValue);
1921
1922                 contacts_record_get_str_p(sourceRecordHandle, _contacts_image.path, &pCharValue);
1923                 contacts_record_set_str(destRecordHandle, _contacts_image.path, pCharValue);
1924
1925                 contacts_record_add_child_record(contactHandle, _contacts_contact.image, destRecordHandle);
1926
1927                 imageHandle.Release();
1928         }
1929
1930         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
1931         // company
1932         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
1933         contacts_record_get_child_record_count(srcHandle, _contacts_contact.company, &count);
1934         for (i = 0; i < count; i++)
1935         {
1936                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.company, i, &sourceRecordHandle);
1937
1938                 ret = contacts_record_create(_contacts_company._uri, &destRecordHandle);
1939                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1940
1941                 __ContactsRecordHandle companyHandle(destRecordHandle);
1942
1943                 contacts_record_get_int(sourceRecordHandle, _contacts_company.type, &intValue);
1944                 contacts_record_set_int(destRecordHandle, _contacts_company.type, intValue);
1945
1946                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.name, &pCharValue);
1947                 contacts_record_set_str(destRecordHandle, _contacts_company.name, pCharValue);
1948
1949                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.department, &pCharValue);
1950                 contacts_record_set_str(destRecordHandle, _contacts_company.department, pCharValue);
1951
1952                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.job_title, &pCharValue);
1953                 contacts_record_set_str(destRecordHandle, _contacts_company.job_title, pCharValue);
1954
1955                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.assistant_name, &pCharValue);
1956                 contacts_record_set_str(destRecordHandle, _contacts_company.assistant_name, pCharValue);
1957
1958                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.role, &pCharValue);
1959                 contacts_record_set_str(destRecordHandle, _contacts_company.role, pCharValue);
1960
1961                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.logo, &pCharValue);
1962                 contacts_record_set_str(destRecordHandle, _contacts_company.logo, pCharValue);
1963
1964                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.location, &pCharValue);
1965                 contacts_record_set_str(destRecordHandle, _contacts_company.location, pCharValue);
1966
1967                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.description, &pCharValue);
1968                 contacts_record_set_str(destRecordHandle, _contacts_company.description, pCharValue);
1969
1970                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.phonetic_name, &pCharValue);
1971                 contacts_record_set_str(destRecordHandle, _contacts_company.phonetic_name, pCharValue);
1972
1973                 contacts_record_add_child_record(contactHandle, _contacts_contact.company, destRecordHandle);
1974
1975                 companyHandle.Release();
1976         }
1977
1978         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
1979         // note
1980         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
1981         contacts_record_get_child_record_count(srcHandle, _contacts_contact.note, &count);
1982         for (i = 0; i < count; i++)
1983         {
1984                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.note, i, &sourceRecordHandle);
1985
1986                 ret = contacts_record_create(_contacts_note._uri, &destRecordHandle);
1987                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1988
1989                 __ContactsRecordHandle noteHandle(destRecordHandle);
1990
1991                 contacts_record_get_str_p(sourceRecordHandle, _contacts_note.note, &pCharValue);
1992                 contacts_record_set_str(destRecordHandle, _contacts_note.note, pCharValue);
1993
1994                 contacts_record_add_child_record(contactHandle, _contacts_contact.note, destRecordHandle);
1995
1996                 noteHandle.Release();
1997         }
1998
1999         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2000         // phone number
2001         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2002         contacts_record_get_child_record_count(srcHandle, _contacts_contact.number, &count);
2003         for (i = 0; i < count; i++)
2004         {
2005                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.number, i, &sourceRecordHandle);
2006
2007                 ret = contacts_record_create(_contacts_number._uri, &destRecordHandle);
2008                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2009
2010                 __ContactsRecordHandle numberHandle(destRecordHandle);
2011
2012                 contacts_record_get_int(sourceRecordHandle, _contacts_number.type, &intValue);
2013                 contacts_record_set_int(destRecordHandle, _contacts_number.type, intValue);
2014
2015                 contacts_record_get_str_p(sourceRecordHandle, _contacts_number.label, &pCharValue);
2016                 contacts_record_set_str(destRecordHandle, _contacts_number.label, pCharValue);
2017
2018                 contacts_record_get_str_p(sourceRecordHandle, _contacts_number.number, &pCharValue);
2019                 contacts_record_set_str(destRecordHandle, _contacts_number.number, pCharValue);
2020
2021                 contacts_record_add_child_record(contactHandle, _contacts_contact.number, destRecordHandle);
2022
2023                 numberHandle.Release();
2024         }
2025
2026         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2027         // email
2028         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2029         contacts_record_get_child_record_count(srcHandle, _contacts_contact.email, &count);
2030         for (i = 0; i < count; i++)
2031         {
2032                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.email, i, &sourceRecordHandle);
2033
2034                 ret = contacts_record_create(_contacts_email._uri, &destRecordHandle);
2035                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2036
2037                 __ContactsRecordHandle emailHandle(destRecordHandle);
2038
2039                 contacts_record_get_int(sourceRecordHandle, _contacts_email.type, &intValue);
2040                 contacts_record_set_int(destRecordHandle, _contacts_email.type, intValue);
2041
2042                 contacts_record_get_str_p(sourceRecordHandle, _contacts_email.label, &pCharValue);
2043                 contacts_record_set_str(destRecordHandle, _contacts_email.label, pCharValue);
2044
2045                 contacts_record_get_str_p(sourceRecordHandle, _contacts_email.email, &pCharValue);
2046                 contacts_record_set_str(destRecordHandle, _contacts_email.email, pCharValue);
2047
2048                 contacts_record_add_child_record(contactHandle, _contacts_contact.email, destRecordHandle);
2049
2050                 emailHandle.Release();
2051
2052         }
2053
2054         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2055         // event
2056         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2057         contacts_record_get_child_record_count(srcHandle, _contacts_contact.event, &count);
2058         for (i = 0; i < count; i++)
2059         {
2060                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.event, i, &sourceRecordHandle);
2061
2062                 ret = contacts_record_create(_contacts_event._uri, &destRecordHandle);
2063                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2064
2065                 __ContactsRecordHandle eventHandle(destRecordHandle);
2066
2067                 contacts_record_get_int(sourceRecordHandle, _contacts_event.type, &intValue);
2068                 contacts_record_set_int(destRecordHandle, _contacts_event.type, intValue);
2069
2070                 contacts_record_get_str_p(sourceRecordHandle, _contacts_event.label, &pCharValue);
2071                 contacts_record_set_str(destRecordHandle, _contacts_event.label, pCharValue);
2072
2073                 contacts_record_get_int(sourceRecordHandle, _contacts_event.date, &intValue);
2074                 contacts_record_set_int(destRecordHandle, _contacts_event.date, intValue);
2075
2076                 contacts_record_add_child_record(contactHandle, _contacts_contact.event, destRecordHandle);
2077
2078                 eventHandle.Release();
2079         }
2080
2081         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2082         // im address
2083         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2084         contacts_record_get_child_record_count(srcHandle, _contacts_contact.messenger, &count);
2085         for (i = 0; i < count; i++)
2086         {
2087                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.messenger, i, &sourceRecordHandle);
2088
2089                 ret = contacts_record_create(_contacts_messenger._uri, &destRecordHandle);
2090                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2091
2092                 __ContactsRecordHandle imAddressHandle(destRecordHandle);
2093
2094                 contacts_record_get_int(sourceRecordHandle, _contacts_messenger.type, &intValue);
2095                 contacts_record_set_int(destRecordHandle, _contacts_messenger.type, intValue);
2096
2097                 contacts_record_get_str_p(sourceRecordHandle, _contacts_messenger.label, &pCharValue);
2098                 contacts_record_set_str(destRecordHandle, _contacts_messenger.label, pCharValue);
2099
2100                 contacts_record_get_str_p(sourceRecordHandle, _contacts_messenger.im_id, &pCharValue);
2101                 contacts_record_set_str(destRecordHandle, _contacts_messenger.im_id, pCharValue);
2102
2103                 contacts_record_add_child_record(contactHandle, _contacts_contact.messenger, destRecordHandle);
2104
2105                 imAddressHandle.Release();
2106         }
2107
2108         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2109         // address
2110         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2111         contacts_record_get_child_record_count(srcHandle, _contacts_contact.address, &count);
2112         for (i = 0; i < count; i++)
2113         {
2114                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.address, i, &sourceRecordHandle);
2115
2116                 ret = contacts_record_create(_contacts_address._uri, &destRecordHandle);
2117                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2118
2119                 __ContactsRecordHandle addressHandle(destRecordHandle);
2120
2121                 contacts_record_get_int(sourceRecordHandle, _contacts_address.type, &intValue);
2122                 contacts_record_set_int(destRecordHandle, _contacts_address.type, intValue);
2123
2124                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.label, &pCharValue);
2125                 contacts_record_set_str(destRecordHandle, _contacts_address.label, pCharValue);
2126
2127                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.postbox, &pCharValue);
2128                 contacts_record_set_str(destRecordHandle, _contacts_address.postbox, pCharValue);
2129
2130                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.extended, &pCharValue);
2131                 contacts_record_set_str(destRecordHandle, _contacts_address.extended, pCharValue);
2132
2133                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.street, &pCharValue);
2134                 contacts_record_set_str(destRecordHandle, _contacts_address.street, pCharValue);
2135
2136                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.locality, &pCharValue);
2137                 contacts_record_set_str(destRecordHandle, _contacts_address.locality, pCharValue);
2138
2139                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.region, &pCharValue);
2140                 contacts_record_set_str(destRecordHandle, _contacts_address.region, pCharValue);
2141
2142                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.postal_code, &pCharValue);
2143                 contacts_record_set_str(destRecordHandle, _contacts_address.postal_code, pCharValue);
2144
2145                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.country, &pCharValue);
2146                 contacts_record_set_str(destRecordHandle, _contacts_address.country, pCharValue);
2147
2148                 contacts_record_add_child_record(contactHandle, _contacts_contact.address, destRecordHandle);
2149
2150                 addressHandle.Release();
2151         }
2152
2153         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2154         // url
2155         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2156         contacts_record_get_child_record_count(srcHandle, _contacts_contact.url, &count);
2157         for (i = 0; i < count; i++)
2158         {
2159                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.url, i, &sourceRecordHandle);
2160
2161                 ret = contacts_record_create(_contacts_url._uri, &destRecordHandle);
2162                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2163
2164                 __ContactsRecordHandle urlHandle(destRecordHandle);
2165
2166                 contacts_record_get_int(sourceRecordHandle, _contacts_url.type, &intValue);
2167                 contacts_record_set_int(destRecordHandle, _contacts_url.type, intValue);
2168
2169                 contacts_record_get_str_p(sourceRecordHandle, _contacts_url.label, &pCharValue);
2170                 contacts_record_set_str(destRecordHandle, _contacts_url.label, pCharValue);
2171
2172                 contacts_record_get_str_p(sourceRecordHandle, _contacts_url.url, &pCharValue);
2173                 contacts_record_set_str(destRecordHandle, _contacts_url.url, pCharValue);
2174
2175                 contacts_record_add_child_record(contactHandle, _contacts_contact.url, destRecordHandle);
2176
2177                 urlHandle.Release();
2178         }
2179
2180         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2181         // nickname
2182         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2183         contacts_record_get_child_record_count(srcHandle, _contacts_contact.nickname, &count);
2184         for (i = 0; i < count; i++)
2185         {
2186                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.nickname, i, &sourceRecordHandle);
2187
2188                 ret = contacts_record_create(_contacts_nickname._uri, &destRecordHandle);
2189                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2190
2191                 __ContactsRecordHandle nicknameHandle(destRecordHandle);
2192
2193                 contacts_record_get_str_p(sourceRecordHandle, _contacts_nickname.name, &pCharValue);
2194                 contacts_record_set_str(destRecordHandle, _contacts_nickname.name, pCharValue);
2195
2196                 contacts_record_add_child_record(contactHandle, _contacts_contact.nickname, destRecordHandle);
2197
2198                 nicknameHandle.Release();
2199         }
2200
2201         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2202         // relationship
2203         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2204         contacts_record_get_child_record_count(srcHandle, _contacts_contact.relationship, &count);
2205         for (i = 0; i < count; i++)
2206         {
2207                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.relationship, i, &sourceRecordHandle);
2208
2209                 ret = contacts_record_create(_contacts_relationship._uri, &destRecordHandle);
2210                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2211
2212                 __ContactsRecordHandle relationshipHandle(destRecordHandle);
2213
2214                 contacts_record_get_str_p(sourceRecordHandle, _contacts_relationship.name, &pCharValue);
2215                 contacts_record_set_str(destRecordHandle, _contacts_relationship.name, pCharValue);
2216
2217                 contacts_record_add_child_record(contactHandle, _contacts_contact.relationship, destRecordHandle);
2218
2219                 relationshipHandle.Release();
2220         }
2221
2222         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2223         // app launch data
2224         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2225         contacts_record_get_child_record_count(srcHandle, _contacts_contact.profile, &count);
2226         for (i = 0; i < count; i++)
2227         {
2228                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.profile, i, &sourceRecordHandle);
2229
2230                 ret = contacts_record_create(_contacts_profile._uri, &destRecordHandle);
2231                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2232
2233                 __ContactsRecordHandle profileHandle(destRecordHandle);
2234
2235                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.text, &pCharValue);
2236                 contacts_record_set_str(destRecordHandle, _contacts_profile.text, pCharValue);
2237
2238                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.uid, &pCharValue);
2239                 contacts_record_set_str(destRecordHandle, _contacts_profile.uid, pCharValue);
2240
2241                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.app_id, &pCharValue);
2242                 contacts_record_set_str(destRecordHandle, _contacts_profile.app_id, pCharValue);
2243
2244                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.service_operation, &pCharValue);
2245                 contacts_record_set_str(destRecordHandle, _contacts_profile.service_operation, pCharValue);
2246
2247                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.uri, &pCharValue);
2248                 contacts_record_set_str(destRecordHandle, _contacts_profile.uri, pCharValue);
2249
2250                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.category, &pCharValue);
2251                 contacts_record_set_str(destRecordHandle, _contacts_profile.category, pCharValue);
2252
2253                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.mime, &pCharValue);
2254                 contacts_record_set_str(destRecordHandle, _contacts_profile.mime, pCharValue);
2255
2256                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.extra_data, &pCharValue);
2257                 contacts_record_set_str(destRecordHandle, _contacts_profile.extra_data, pCharValue);
2258
2259                 contacts_record_add_child_record(contactHandle, _contacts_contact.profile, destRecordHandle);
2260
2261                 profileHandle.Release();
2262         }
2263
2264         return contactHandle;
2265 }
2266
2267 void
2268 _AddressbookImpl::OnContactChanged(void)
2269 {
2270         if (__pIRecordEventListener == null && __pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
2271         {
2272                 return;
2273         }
2274
2275         std::unique_ptr<IList, AllElementsDeleter> pChangedContactList(GetChangedContactsAfterN(__dbVersionForContact, __dbVersionForContact));
2276         SysTryReturnVoidResult(NID_SCL, pChangedContactList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2277
2278         if (pChangedContactList->GetCount() > 0)
2279         {
2280                 if (__pIAddressbookEventListener != null)
2281                 {
2282                         __pIAddressbookEventListener->OnContactsChanged(*pChangedContactList);
2283                 }
2284                 else if (__pIAddressbookChangeEventListener != null)
2285                 {
2286                         __pIAddressbookChangeEventListener->OnContactsChanged(*pChangedContactList);
2287                 }
2288                 else
2289                 {
2290                         RecordEventType recordEventType = RECORD_ADDED;
2291                         std::unique_ptr<IEnumerator> pEnum(pChangedContactList->GetEnumeratorN());
2292                         std::unique_ptr<Contact> pContact(null);
2293
2294                         while (pEnum->MoveNext() == E_SUCCESS)
2295                         {
2296                                 ContactChangeInfo* pContactChagneInfo = static_cast<ContactChangeInfo*>(pEnum->GetCurrent());
2297
2298                                 if (pContactChagneInfo->GetChangeType() == RECORD_CHANGE_TYPE_ADDED)
2299                                 {
2300                                         recordEventType = RECORD_ADDED;
2301                                         pContact.reset(GetContactN(pContactChagneInfo->GetContactId()));
2302                                         if (pContact == null)
2303                                         {
2304                                                 if (GetLastResult() == E_OBJ_NOT_FOUND)
2305                                                 {
2306                                                         continue;
2307                                                 }
2308                                                 else
2309                                                 {
2310                                                         break;
2311                                                 }
2312                                         }
2313                                 }
2314                                 else if (pContactChagneInfo->GetChangeType() == RECORD_CHANGE_TYPE_UPDATED)
2315                                 {
2316                                         recordEventType = RECORD_UPDATED;
2317                                         pContact.reset(GetContactN(pContactChagneInfo->GetContactId()));
2318                                         if (pContact == null)
2319                                         {
2320                                                 if (GetLastResult() == E_OBJ_NOT_FOUND)
2321                                                 {
2322                                                         continue;
2323                                                 }
2324                                                 else
2325                                                 {
2326                                                         break;
2327                                                 }
2328                                         }
2329                                 }
2330                                 else
2331                                 {
2332                                         recordEventType = RECORD_REMOVED;
2333                                         pContact.reset(new (std::nothrow) Contact());
2334                                         if (pContact == null)
2335                                         {
2336                                                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2337                                                 break;
2338                                         }
2339
2340                                         _RecordImpl::GetInstance(*pContact)->SetRecordId(pContactChagneInfo->GetContactId());
2341                                 }
2342
2343                                 __pIRecordEventListener->OnRecordChangedN(recordEventType, RECORD_TYPE_CONTACT, *pContact, null, null);
2344                         }
2345                 }
2346
2347         }
2348 }
2349
2350 void
2351 _AddressbookImpl::OnCategoryChanged(void)
2352 {
2353         if (__pIRecordEventListener == null && __pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
2354         {
2355                 return;
2356         }
2357
2358         std::unique_ptr<IList, AllElementsDeleter> pChangedCategoryList(GetChangedGroupsAfterN(__dbVersionForGroup, __dbVersionForGroup));
2359         SysTryReturnVoidResult(NID_SCL, pChangedCategoryList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2360
2361         if (pChangedCategoryList->GetCount() > 0)
2362         {
2363                 if (__pIAddressbookEventListener != null)
2364                 {
2365                         __pIAddressbookEventListener->OnCategoriesChanged(*pChangedCategoryList);
2366                 }
2367                 else if (__pIAddressbookChangeEventListener != null)
2368                 {
2369                         __pIAddressbookChangeEventListener->OnCategoriesChanged(*pChangedCategoryList);
2370                 }
2371                 else
2372                 {
2373                         RecordEventType recordEventType = RECORD_ADDED;
2374                         std::unique_ptr<IEnumerator> pEnum(pChangedCategoryList->GetEnumeratorN());
2375                         std::unique_ptr<Category> pCategory(null);
2376
2377                         while (pEnum->MoveNext() == E_SUCCESS)
2378                         {
2379                                 CategoryChangeInfo* pCategoryChagneInfo = static_cast<CategoryChangeInfo*>(pEnum->GetCurrent());
2380
2381                                 if (pCategoryChagneInfo->GetChangeType() == RECORD_CHANGE_TYPE_ADDED)
2382                                 {
2383                                         recordEventType = RECORD_ADDED;
2384                                         pCategory.reset(GetCategoryN(pCategoryChagneInfo->GetCategoryId()));
2385                                         if (pCategory == null)
2386                                         {
2387                                                 if (GetLastResult() == E_OBJ_NOT_FOUND)
2388                                                 {
2389                                                         continue;
2390                                                 }
2391                                                 else
2392                                                 {
2393                                                         break;
2394                                                 }
2395                                         }
2396                                 }
2397                                 else if (pCategoryChagneInfo->GetChangeType() == RECORD_CHANGE_TYPE_UPDATED)
2398                                 {
2399                                         recordEventType = RECORD_UPDATED;
2400                                         pCategory.reset(GetCategoryN(pCategoryChagneInfo->GetCategoryId()));
2401                                         if (pCategory == null)
2402                                         {
2403                                                 if (GetLastResult() == E_OBJ_NOT_FOUND)
2404                                                 {
2405                                                         continue;
2406                                                 }
2407                                                 else
2408                                                 {
2409                                                         break;
2410                                                 }
2411                                         }
2412                                 }
2413                                 else
2414                                 {
2415                                         recordEventType = RECORD_REMOVED;
2416                                         pCategory.reset(new (std::nothrow) Category());
2417                                         if (pCategory == null)
2418                                         {
2419                                                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2420                                                 break;
2421                                         }
2422
2423                                         _RecordImpl::GetInstance(*pCategory)->SetRecordId(pCategoryChagneInfo->GetCategoryId());
2424                                 }
2425
2426                                 __pIRecordEventListener->OnRecordChangedN(recordEventType, RECORD_TYPE_CATEGORY, *pCategory, null, null);
2427                         }
2428                 }
2429
2430         }
2431 }
2432
2433 void
2434 _AddressbookImpl::OnRelationChanged(void)
2435 {
2436         if (__pIAddressbookEventListener == null && __pIRecordEventListener == null)
2437         {
2438                 return;
2439         }
2440
2441         std::unique_ptr<IList, AllElementsDeleter> pChangedCategoryList(GetChangedRelationsAfterN(__dbVersionForRelation, __dbVersionForRelation));
2442         SysTryReturnVoidResult(NID_SCL, pChangedCategoryList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2443
2444         if (pChangedCategoryList->GetCount() > 0)
2445         {
2446                 if (__pIAddressbookEventListener != null)
2447                 {
2448                         __pIAddressbookEventListener->OnCategoriesChanged(*pChangedCategoryList);
2449                 }
2450                 else
2451                 {
2452                         RecordEventType recordEventType = RECORD_ADDED;
2453                         std::unique_ptr<IEnumerator> pEnum(pChangedCategoryList->GetEnumeratorN());
2454                         std::unique_ptr<Category> pCategory(null);
2455
2456                         while (pEnum->MoveNext() == E_SUCCESS)
2457                         {
2458                                 CategoryChangeInfo* pCategoryChagneInfo = static_cast<CategoryChangeInfo*>(pEnum->GetCurrent());
2459
2460                                 recordEventType = RECORD_UPDATED;
2461                                 pCategory.reset(GetCategoryN(pCategoryChagneInfo->GetCategoryId()));
2462                                 if (pCategory == null)
2463                                 {
2464                                         if (GetLastResult() == E_OBJ_NOT_FOUND)
2465                                         {
2466                                                 continue;
2467                                         }
2468                                         else
2469                                         {
2470                                                 break;
2471                                         }
2472                                 }
2473
2474                                 __pIRecordEventListener->OnRecordChangedN(recordEventType, RECORD_TYPE_CATEGORY, *pCategory, null, null);
2475                         }
2476                 }
2477         }
2478 }
2479
2480 _AddressbookImpl*
2481 _AddressbookImpl::GetInstance(Addressbook& addressbook)
2482 {
2483         return addressbook.__pAddressbookImpl;
2484 }
2485
2486 const _AddressbookImpl*
2487 _AddressbookImpl::GetInstance(const Addressbook& addressbook)
2488 {
2489         return addressbook.__pAddressbookImpl;
2490 }
2491
2492 }}  // Tizen::Social