8076da7baa158e2d570ce1269e3c1fba20876499
[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         int ret = contacts_group_add_contact(categoryId, contactId);
655         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
656         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));
657         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
658
659         return E_SUCCESS;
660 }
661
662 result
663 _AddressbookImpl::RemoveMemberFromCategory(RecordId categoryId, RecordId contactId)
664 {
665         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);
666         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);
667         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
668
669         int addressbookId = 0;
670         unique_ptr<ContactRecord, ContactRecordDeleter> pContactRecord(_AddressbookUtil::GetContactRecordN(_contacts_simple_contact._uri, contactId));
671         SysTryReturn(NID_SCL, pContactRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
672         contacts_record_get_int(pContactRecord.get(), _contacts_simple_contact.address_book_id, &addressbookId);
673
674         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));
675
676         unique_ptr<ContactRecord, ContactRecordDeleter> pCategoryRecord(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, categoryId));
677         SysTryReturn(NID_SCL, pCategoryRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
678         contacts_record_get_int(pCategoryRecord.get(), _contacts_group.address_book_id, &addressbookId);
679
680         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));
681
682         int ret = contacts_group_remove_contact(categoryId, contactId);
683         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
684         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));
685         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
686
687         return E_SUCCESS;
688 }
689
690 IList*
691 _AddressbookImpl::GetAllCategoriesN(void) const
692 {
693         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
694
695         ClearLastResult();
696
697         __Filter<__ContactsGroup> filter;
698         filter.Construct();
699         filter.AddInt(_contacts_group.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
700
701         __Query<__ContactsGroup> query;
702         query.Construct();
703         query.SetFilter(filter);
704         query.SetSort(_contacts_group.name, true);
705
706
707         IList* pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsGroup, Category>(query);
708         SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
709
710         return pCategories;
711 }
712
713 IList*
714 _AddressbookImpl::GetCategoriesByContactN(RecordId contactId) const
715 {
716         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
717         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));
718
719         ClearLastResult();
720
721         __Filter<__ContactsGroupRelation> filter;
722         filter.Construct();
723         filter.AddInt(_contacts_group_relation.contact_id, CONTACTS_MATCH_EQUAL, contactId);
724
725         __Query<__ContactsGroupRelation> query;
726         query.Construct();
727         query.SetFilter(filter);
728         query.SetSort(_contacts_group_relation.name, true);
729
730         IList* pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsGroupRelation, Category>(query);
731         SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
732
733         return pCategories;
734 }
735
736 IList*
737 _AddressbookImpl::GetAllContactsN(void) const
738 {
739         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
740
741         ClearLastResult();
742
743         __Filter<__ContactsContact> filter;
744         filter.Construct();
745         filter.AddInt(_contacts_contact.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
746
747         __Query<__ContactsContact> query;
748         query.Construct();
749         query.SetFilter(filter);
750         query.SetSort(_contacts_contact.display_name, true);
751
752         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
753         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
754
755         return pContacts;
756 }
757
758 IList*
759 _AddressbookImpl::GetContactsByCategoryN(RecordId categoryId) const
760 {
761         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));
762         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
763
764         ClearLastResult();
765
766         __Filter<__ContactsContactGroupRel> filter;
767         filter.Construct();
768         if (categoryId != INVALID_RECORD_ID)
769         {
770                 filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_EQUAL, categoryId);
771         }
772         else
773         {
774                 filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_NONE, 0);
775         }
776
777         __Query<__ContactsContactGroupRel> query;
778         query.Construct();
779         query.SetFilter(filter);
780         query.SetSort(_contacts_contact_grouprel.display_name, true);
781
782         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Contact>(query);
783         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
784
785         return pContacts;
786 }
787
788 IList*
789 _AddressbookImpl::GetContactsN(int pageNo, int countPerPage) const
790 {
791         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);
792         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
793
794         int offset = (pageNo - 1)*countPerPage;
795         int limit = countPerPage;
796
797         ClearLastResult();
798
799         __Filter<__ContactsContact> filter;
800         filter.Construct();
801         filter.AddInt(_contacts_contact.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
802
803         __Query<__ContactsContact> query;
804         query.Construct();
805         query.SetFilter(filter);
806         query.SetSort(_contacts_contact.display_name, true);
807
808         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query, offset, limit);
809         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
810
811         return pContacts;
812 }
813
814 IList*
815 _AddressbookImpl::GetContactsInN(const Category& category, int pageNo, int countPerPage) const
816 {
817         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));
818         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);
819         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
820
821         int offset = (pageNo - 1)*countPerPage;
822         int limit = countPerPage;
823
824         ClearLastResult();
825
826         __Filter<__ContactsContactGroupRel> filter;
827         filter.Construct();
828         filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_EQUAL, category.GetRecordId());
829
830         __Query<__ContactsContactGroupRel> query;
831         query.Construct();
832         query.SetFilter(filter);
833         query.SetSort(_contacts_contact_grouprel.display_name, true);
834
835         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Contact>(query, offset, limit);
836         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
837
838         return pContacts;
839 }
840
841 IList*
842 _AddressbookImpl::SearchContactsByEmailN(const String& email) const
843 {
844         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));
845         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
846
847         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
848         {
849                 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));
850         }
851
852         ClearLastResult();
853
854         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(email));
855         SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
856
857         __Filter<__ContactsContactEmail> filter;
858         filter.Construct();
859         filter.AddInt(_contacts_contact_email.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
860         filter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
861         filter.AddString(_contacts_contact_email.email, CONTACTS_MATCH_CONTAINS, pCharArray.get());
862
863         unsigned int projectionIds[1];
864         projectionIds[0] = _contacts_contact_email.contact_id;
865
866         __Query<__ContactsContactEmail> query;
867         query.Construct();
868         query.SetProjection(projectionIds, 1);
869         query.SetFilter(filter);
870         query.SetDistinct(true);
871         query.SetSort(_contacts_contact_email.display_name, true);
872
873         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactEmail, Contact>(query);
874         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
875
876         return pContacts;
877 }
878
879 IList*
880 _AddressbookImpl::SearchContactsByNameN(const String& name) const
881 {
882         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));
883         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
884
885         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
886         {
887                 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));
888         }
889
890         ClearLastResult();
891
892         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(name));
893         SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
894
895         __Filter<__ContactsContact> filter;
896         filter.Construct();
897         filter.AddInt(_contacts_contact.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
898         filter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
899         filter.AddString(_contacts_contact.display_name, CONTACTS_MATCH_CONTAINS, pCharArray.get());
900
901         __Query<__ContactsContact> query;
902         query.Construct();
903         query.SetFilter(filter);
904         query.SetSort(_contacts_contact.display_name, true);
905         query.SetDistinct(true);
906
907         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
908         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
909
910         return pContacts;
911 }
912
913
914 IList*
915 _AddressbookImpl::SearchContactsByPhoneNumberN(const String& phoneNumber) const
916 {
917         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));
918         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
919
920
921         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
922         {
923                 int count = phoneNumber.GetLength();
924                 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));
925         }
926
927         ClearLastResult();
928
929         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(phoneNumber));
930         SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
931
932         __Filter<__ContactsContactNumber> subFilter;
933         subFilter.Construct();
934         subFilter.AddString(_contacts_contact_number.normalized_number, CONTACTS_MATCH_CONTAINS, pCharArray.get());
935         subFilter.AddOperator(CONTACTS_FILTER_OPERATOR_OR);
936         subFilter.AddString(_contacts_contact_number.number, CONTACTS_MATCH_CONTAINS, pCharArray.get());
937
938         __Filter<__ContactsContactNumber> filter;
939         filter.Construct();
940         filter.AddInt(_contacts_contact_number.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
941         filter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
942         filter.AddFilter(subFilter);
943
944         unsigned int projectionIds[1];
945         projectionIds[0] = _contacts_contact_number.contact_id;
946
947         __Query<__ContactsContactNumber> query;
948         query.Construct();
949         query.SetProjection(projectionIds, 1);
950         query.SetFilter(filter);
951         query.SetDistinct(true);
952         query.SetSort(_contacts_contact_number.display_name, true);
953
954         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactNumber, Contact>(query);
955         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
956
957         return pContacts;
958 }
959
960 int
961 _AddressbookImpl::GetCategoryCount(void) const
962 {
963         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
964
965         int count = -1;
966
967         ClearLastResult();
968
969         __Filter<__ContactsGroup> filter;
970         filter.Construct();
971         filter.AddInt(_contacts_group.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
972
973         __Query<__ContactsGroup> query;
974         query.Construct();
975         query.SetFilter(filter);
976
977         count = _AddressbookUtil::GetCountWithQuery(query);
978         SysTryReturn(NID_SCL, count >= 0, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
979
980         return count;
981 }
982
983 int
984 _AddressbookImpl::GetContactCount(void) const
985 {
986         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
987
988         int count = -1;
989
990         ClearLastResult();
991
992         __Filter<__ContactsContact> filter;
993         filter.Construct();
994         filter.AddInt(_contacts_contact.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
995
996         __Query<__ContactsContact> query;
997         query.Construct();
998         query.SetFilter(filter);
999
1000         count = _AddressbookUtil::GetCountWithQuery(query);
1001         SysTryReturn(NID_SCL, count >= 0, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1002
1003         return count;
1004 }
1005
1006 Contact*
1007 _AddressbookImpl::GetContactN(RecordId contactId) const
1008 {
1009         SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. contactId = %d.", GetErrorMessage(E_INVALID_ARG), contactId);
1010         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1011
1012         ClearLastResult();
1013
1014         unique_ptr<ContactRecord, ContactRecordDeleter> pContactRecord(_AddressbookUtil::GetContactRecordN(_contacts_contact._uri, contactId));
1015         SysTryReturn(NID_SCL, pContactRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1016
1017         int intValue = 0;
1018         contacts_record_get_int(pContactRecord.get(), _contacts_contact.id, &intValue);
1019         SysTryReturn(NID_SCL, intValue == contactId, null, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
1020
1021         unique_ptr<Contact> pContact(new (std::nothrow) Contact());
1022         SysTryReturn(NID_SCL, pContact, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1023
1024         _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(pContactRecord.release());
1025         _RecordImpl::GetInstance(*pContact)->SetRecordId(intValue);
1026
1027         return pContact.release();
1028 }
1029
1030 Category*
1031 _AddressbookImpl::GetCategoryN(RecordId categoryId) const
1032 {
1033         SysTryReturn(NID_SCL, categoryId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. categoryId = %d.", GetErrorMessage(E_INVALID_ARG), categoryId);
1034         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1035
1036         ClearLastResult();
1037
1038         unique_ptr<ContactRecord, ContactRecordDeleter> pCategoryRecord(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, categoryId));
1039         SysTryReturn(NID_SCL, pCategoryRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1040
1041         int intValue = 0;
1042
1043         contacts_record_get_int(pCategoryRecord.get(), _contacts_group.id, &intValue);
1044         SysTryReturn(NID_SCL, categoryId == intValue, null, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
1045
1046         unique_ptr<Category> pCategory(new (std::nothrow) Category());
1047         SysTryReturn(NID_SCL, pCategory != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1048
1049         __Filter<__ContactsGroupRelation> filter;
1050         filter.Construct();
1051         filter.AddInt(_contacts_group_relation.group_id, CONTACTS_MATCH_EQUAL, categoryId);
1052
1053         __Query<__ContactsGroupRelation> query;
1054         query.Construct();
1055         query.SetFilter(filter);
1056
1057         int count = _AddressbookUtil::GetCountWithQuery(query);
1058         SysTryReturn(NID_SCL, count >= 0, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1059
1060         _CategoryImpl::GetInstance(*pCategory)->SetRecordHandle(pCategoryRecord.release());
1061         _CategoryImpl::GetInstance(*pCategory)->SetMemberCount(count);
1062         _RecordImpl::GetInstance(*pCategory)->SetRecordId(categoryId);
1063
1064         return pCategory.release();
1065 }
1066
1067 int
1068 _AddressbookImpl::GetLatestVersion(void) const
1069 {
1070         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1071
1072         int latestVersion = -1;
1073
1074         ClearLastResult();
1075
1076         int ret = contacts_db_get_current_version(&latestVersion);
1077         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, -1, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1078         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1079
1080         return latestVersion;
1081 }
1082
1083 IList*
1084 _AddressbookImpl::GetChangedContactsAfterN(int version, int& latestVersion) const
1085 {
1086         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);
1087         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1088
1089         ClearLastResult();
1090
1091         IList* pChangedContacts = _AddressbookUtil::SearchWithVersionN<__ContactsContactUpdatedInfo, ContactChangeInfo>(__addressbookId, version, latestVersion);
1092         SysTryReturn(NID_SCL, pChangedContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1093
1094         return pChangedContacts;
1095 }
1096
1097 IList*
1098 _AddressbookImpl::GetChangedCategoriesAfterN(int version, int& latestVersion) const
1099 {
1100         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);
1101         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1102
1103         ClearLastResult();
1104
1105         int latestVersion1 = 0;
1106         int latestVersion2 = 0;
1107
1108         std::unique_ptr<IList, AllElementsDeleter> pChangedGroups(_AddressbookUtil::SearchWithVersionN<__ContactsGroupUpdatedInfo, CategoryChangeInfo>(__addressbookId, version, latestVersion1));
1109         SysTryReturn(NID_SCL, pChangedGroups != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1110
1111         std::unique_ptr<IList, AllElementsDeleter> pChangedRelations(_AddressbookUtil::SearchWithVersionN<__ContactsGroupRelUpdatedInfo, CategoryChangeInfo>(__addressbookId, version, latestVersion2));
1112         SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1113
1114         std::unique_ptr<ArrayList, AllElementsDeleter> pChangeList(new (std::nothrow) Tizen::Base::Collection::ArrayList());
1115         SysTryReturn(NID_SCL, pChangeList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1116
1117         result r = pChangeList->AddItems(*pChangedGroups);
1118         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1119
1120         r = pChangeList->AddItems(*pChangedRelations);
1121         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1122
1123         pChangedGroups->RemoveAll(false);
1124         pChangedRelations->RemoveAll(false);
1125
1126         latestVersion = latestVersion2 > latestVersion1 ? latestVersion2 : latestVersion1;
1127
1128         return pChangeList.release();
1129 }
1130
1131 IList*
1132 _AddressbookImpl::GetChangedContactInfoListN(int version, int& latestVersion) const
1133 {
1134         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);
1135         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1136
1137         ClearLastResult();
1138
1139         IList* pChangedContacts = _AddressbookUtil::SearchWithVersionN<__ContactsContactUpdatedInfo, ContactChangeInfo>(__addressbookId, version, latestVersion);
1140         SysTryReturn(NID_SCL, pChangedContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1141
1142         return pChangedContacts;
1143 }
1144
1145 IList*
1146 _AddressbookImpl::GetChangedCategoryInfoListN(int version, int& latestVersion) const
1147 {
1148         return GetChangedGroupsAfterN(version, latestVersion);
1149 }
1150
1151 IList*
1152 _AddressbookImpl::GetChangedGroupsAfterN(int version, int& latestVersion) const
1153 {
1154         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);
1155         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1156
1157         ClearLastResult();
1158
1159         IList* pChangedGroups = _AddressbookUtil::SearchWithVersionN<__ContactsGroupUpdatedInfo, CategoryChangeInfo>(__addressbookId, version, latestVersion);
1160         SysTryReturn(NID_SCL, pChangedGroups != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1161
1162         return pChangedGroups;
1163 }
1164
1165 IList*
1166 _AddressbookImpl::GetChangedRelationsAfterN(int version, int& latestVersion) const
1167 {
1168         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);
1169         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1170
1171         ClearLastResult();
1172
1173         IList* pChangedRelations = _AddressbookUtil::SearchWithVersionN<__ContactsGroupRelUpdatedInfo, CategoryChangeInfo>(__addressbookId, version, latestVersion);
1174         SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1175
1176         return pChangedRelations;
1177 }
1178
1179 result
1180 _AddressbookImpl::AddContacts(const Tizen::Base::Collection::IList& contactList, Tizen::Base::Collection::IListT<RecordId>* pContactIdList)
1181 {
1182         SysTryReturn(NID_SCL, contactList.GetCount() != 0, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. contactList is empty.", GetErrorMessage(E_INVALID_ARG));
1183         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1184
1185         std::unique_ptr<IEnumerator> pContactEnum(contactList.GetEnumeratorN());
1186         SysTryReturnResult(NID_SCL, pContactEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1187
1188         Contact* pContact = null;
1189         int ret = CONTACTS_ERROR_NONE;
1190         int* pContactIds = null;
1191         unsigned int contactsCount = 0;
1192
1193         contacts_record_h recordHandle = null;
1194         contacts_list_h listHandle = null;
1195
1196         ret = contacts_list_create(&listHandle);
1197         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1198
1199         while (pContactEnum->MoveNext() == E_SUCCESS)
1200         {
1201                 pContact = static_cast<Contact*>(pContactEnum->GetCurrent());
1202
1203                 if (_ContactImpl::GetInstance(*pContact)->IsEmpty())
1204                 {
1205                         contacts_list_destroy(listHandle, false);
1206                         SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. Contact is empty.", GetErrorMessage(E_INVALID_ARG));
1207                         return E_INVALID_ARG;
1208                 }
1209                 if (pContact->GetRecordId() != INVALID_RECORD_ID)
1210                 {
1211                         contacts_list_destroy(listHandle, false);
1212                         SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. Contact ID is invalid.", GetErrorMessage(E_INVALID_ARG));
1213                         return E_INVALID_ARG;
1214                 }
1215
1216                 recordHandle = _ContactImpl::GetInstance(*pContact)->GetContactRecordHandle();
1217                 contacts_record_set_int(recordHandle, _contacts_contact.address_book_id, __addressbookId);
1218                 contacts_list_add(listHandle, recordHandle);
1219         }
1220
1221         ret = contacts_db_insert_records(listHandle, &pContactIds, &contactsCount);
1222         contacts_list_destroy(listHandle, false);
1223         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));
1224         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1225         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1226         SysTryReturn(NID_SCL, pContactIds, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1227
1228         int* pTemp = pContactIds;
1229
1230         if (pContactIdList != null)
1231         {
1232                 for (unsigned int i = 0; i < contactsCount; i++)
1233                 {
1234                         pContactIdList->Add(*pTemp);
1235                         pTemp++;
1236                 }
1237         }
1238
1239         free(pContactIds);
1240
1241         return E_SUCCESS;
1242 }
1243
1244 result
1245 _AddressbookImpl::UpdateContacts(const Tizen::Base::Collection::IList& contactList)
1246 {
1247         SysTryReturn(NID_SCL, contactList.GetCount() != 0, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. contactList is empty.", GetErrorMessage(E_INVALID_ARG));
1248         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1249
1250         std::unique_ptr<IEnumerator> pContactEnum(contactList.GetEnumeratorN());
1251         SysTryReturnResult(NID_SCL, pContactEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1252
1253         Contact* pContact = null;
1254         int ret = CONTACTS_ERROR_NONE;
1255         contacts_record_h recordHandle = null;
1256         contacts_list_h listHandle = null;
1257
1258         ret = contacts_list_create(&listHandle);
1259         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1260
1261         while (pContactEnum->MoveNext() == E_SUCCESS)
1262         {
1263                 pContact = static_cast<Contact*>(pContactEnum->GetCurrent());
1264
1265                 if (_ContactImpl::GetInstance(*pContact)->IsEmpty())
1266                 {
1267                         contacts_list_destroy(listHandle, false);
1268                         SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. Contact is empty.", GetErrorMessage(E_INVALID_ARG));
1269                         return E_INVALID_ARG;
1270                 }
1271                 if (pContact->GetRecordId() == INVALID_RECORD_ID)
1272                 {
1273                         contacts_list_destroy(listHandle, false);
1274                         SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. Contact ID is invalid.", GetErrorMessage(E_INVALID_ARG));
1275                         return E_INVALID_ARG;
1276                 }
1277
1278                 recordHandle = _ContactImpl::GetInstance(*pContact)->GetContactRecordHandle();
1279                 contacts_list_add(listHandle, recordHandle);
1280         }
1281
1282         result r = _AddressbookUtil::UpdateContactRecords(listHandle);
1283         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1284         contacts_list_destroy(listHandle, false);
1285
1286         return E_SUCCESS;
1287 }
1288
1289 result
1290 _AddressbookImpl::RemoveContacts(const Tizen::Base::Collection::IListT<RecordId>& contactIdList)
1291 {
1292         SysTryReturn(NID_SCL, contactIdList.GetCount() != 0, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. contactIdList is empty.", GetErrorMessage(E_INVALID_ARG));
1293         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1294
1295         std::unique_ptr<IEnumeratorT<RecordId> > pContactIdEnum(contactIdList.GetEnumeratorN());
1296         SysTryReturnResult(NID_SCL, pContactIdEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1297
1298         RecordId contactId = INVALID_RECORD_ID;
1299         int ret = CONTACTS_ERROR_NONE;
1300         int count = contactIdList.GetCount();
1301         std::unique_ptr<int[]> pRecordIds(new (std::nothrow) int[count]);
1302         int* pInt = pRecordIds.get();
1303         int* pTmp = pInt;
1304
1305         while (pContactIdEnum->MoveNext() == E_SUCCESS)
1306         {
1307                 pContactIdEnum->GetCurrent(contactId);
1308                 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));
1309
1310                 *pTmp = contactId;
1311                 pTmp++;
1312         }
1313
1314         ret = contacts_db_delete_records(_contacts_contact._uri, pInt, count);
1315         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));
1316         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));
1317
1318         return E_SUCCESS;
1319 }
1320
1321 result
1322 _AddressbookImpl::SetUserProfile(const UserProfile* pUserProfile)
1323 {
1324         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1325
1326         int recordId = 0;
1327         int existingRecordId = 0;
1328         int ret = CONTACTS_ERROR_NONE;
1329         result r = E_SUCCESS;
1330         contacts_record_h recordHandle = null;
1331
1332         std::unique_ptr<UserProfile> pExistingProfile(GetUserProfileN());
1333         SysTryReturn(NID_SCL, pExistingProfile != null || GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1334
1335         if (pExistingProfile != null)
1336         {
1337                 recordHandle = _UserProfileImpl::GetInstance(*pExistingProfile)->GetUserProfileHandle();
1338                 contacts_record_get_int(recordHandle, _contacts_my_profile.id, &existingRecordId);
1339         }
1340
1341         if (pUserProfile != null)
1342         {
1343                 SysTryReturn(NID_SCL, !((_UserProfileImpl::GetInstance(*pUserProfile))->IsEmpty()), E_SYSTEM, E_SYSTEM, "[%s] UserProfile is Empty.", GetErrorMessage(E_SYSTEM));
1344
1345                 recordHandle = _UserProfileImpl::GetInstance(*pUserProfile)->GetUserProfileHandle();
1346                 contacts_record_get_int(recordHandle, _contacts_my_profile.id, &recordId);
1347                 if (recordId == 0)
1348                 {
1349                         if (pExistingProfile != null)
1350                         {
1351                                 r = _AddressbookUtil::DeleteContactRecord(_contacts_my_profile._uri, existingRecordId);
1352                                 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1353                         }
1354
1355                         contacts_record_h newRecordHandle = null;
1356                         ret = contacts_record_clone(recordHandle, &newRecordHandle);
1357                         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));
1358
1359                         ret = contacts_record_set_int(newRecordHandle, _contacts_my_profile.address_book_id, __addressbookId);
1360                         if (ret != CONTACTS_ERROR_NONE)
1361                         {
1362                                 contacts_record_destroy(newRecordHandle, true);
1363                                 SysLogException(NID_SCL, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1364                                 return E_SYSTEM;
1365                         }
1366
1367                         int recordId = 0;
1368                         result r = _AddressbookUtil::InsertContactRecordN(newRecordHandle, recordId);
1369                         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1370                 }
1371                 else if (recordId > 0)
1372                 {
1373                         if (recordId == existingRecordId)
1374                         {
1375                                 contacts_record_h newRecordHandle = null;
1376                                 ret = contacts_record_clone(recordHandle, &newRecordHandle);
1377                                 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));
1378
1379                                 result r = _AddressbookUtil::UpdateContactRecord(newRecordHandle);
1380                                 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1381                                 contacts_record_destroy(newRecordHandle, true);
1382                         }
1383                         else
1384                         {
1385                                 if (pExistingProfile != null)
1386                                 {
1387                                         r = _AddressbookUtil::DeleteContactRecord(_contacts_my_profile._uri, existingRecordId);
1388                                         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1389                                 }
1390
1391                                 contacts_record_h newRecordHandle = null;
1392                                 ret = contacts_record_create(_contacts_my_profile._uri, &newRecordHandle);
1393                                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1394
1395                                 r = CopyMyProfileContents(recordHandle, newRecordHandle);
1396                                 if (r != E_SUCCESS)
1397                                 {
1398                                         contacts_record_destroy(newRecordHandle, true);
1399                                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1400                                         return E_OUT_OF_MEMORY;
1401
1402                                 }
1403
1404                                 ret = contacts_record_set_int(newRecordHandle, _contacts_my_profile.address_book_id, __addressbookId);
1405                                 if (ret != CONTACTS_ERROR_NONE)
1406                                 {
1407                                         contacts_record_destroy(newRecordHandle, true);
1408                                         SysLogException(NID_SCL, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1409                                         return E_SYSTEM;
1410                                 }
1411
1412                                 int recordId = 0;
1413                                 result r = _AddressbookUtil::InsertContactRecordN(newRecordHandle, recordId);
1414                                 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1415                         }
1416                 }
1417         }
1418         else
1419         {
1420                 if (pExistingProfile != null)
1421                 {
1422                         r = _AddressbookUtil::DeleteContactRecord(_contacts_my_profile._uri, existingRecordId);
1423                         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1424                 }
1425         }
1426
1427         return E_SUCCESS;
1428 }
1429
1430 UserProfile*
1431 _AddressbookImpl::GetUserProfileN(void) const
1432 {
1433         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1434
1435         ClearLastResult();
1436
1437         __Filter<__ContactsUserProfile> filter;
1438         filter.Construct();
1439         filter.AddInt(_contacts_my_profile.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
1440
1441         __Query<__ContactsUserProfile> query;
1442         query.Construct();
1443         query.SetFilter(filter);
1444
1445         std::unique_ptr<IList, AllElementsDeleter> pUserProfilesList(_AddressbookUtil::SearchWithQueryN<__ContactsUserProfile, UserProfile>(query));
1446         SysTryReturn(NID_SCL, pUserProfilesList.get() != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1447         SysTryReturn(NID_SCL, pUserProfilesList->GetCount() != 0, null, E_SUCCESS, "No UserProfile Set for this Addressbook.");
1448         SysTryReturn(NID_SCL, pUserProfilesList->GetCount() == 1, null, E_SYSTEM, "[%s] Propagating. More than one UserProfile not allowed.", GetErrorMessage(E_SYSTEM));
1449
1450         UserProfile* pProfile = new (std::nothrow) UserProfile(*(static_cast<UserProfile*>(pUserProfilesList->GetAt(0))));
1451         SysTryReturn(NID_SCL, pProfile != null, null, E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(E_OUT_OF_MEMORY));
1452
1453         return pProfile;
1454 }
1455
1456 bool
1457 _AddressbookImpl::IsUserProfileChangedAfter(int version) const
1458 {
1459         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);
1460         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1461
1462         UserProfile* pExistingProfile = GetUserProfileN();
1463         if (pExistingProfile == null)
1464         {
1465                 return false;
1466         }
1467
1468         delete pExistingProfile;
1469
1470         ClearLastResult();
1471
1472         int latestVersion = 0;
1473         int count = _AddressbookUtil::GetChangedItemCountByVersion<__ContactsMyProfileUpdatedInfo>(__addressbookId, version, latestVersion);
1474         SysTryReturn(NID_SCL, count >= 0, false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1475
1476         if (count > 0)
1477         {
1478                 return true;
1479         }
1480
1481         return false;
1482 }
1483
1484 result
1485 _AddressbookImpl::CopyMyProfileContents(contacts_record_h srcHandle, contacts_record_h dstHandle)
1486 {
1487         int ret = CONTACTS_ERROR_NONE;
1488         int intValue = 0;
1489         unsigned int count = 0;
1490         unsigned int i = 0;
1491         char* pCharValue = null;
1492
1493         contacts_record_h srcRecordHandle = null;
1494         contacts_record_h dstRecordHandle = null;
1495
1496         // name
1497         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.name, &count);
1498         if (count > 0)
1499         {
1500                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.name, 0, &srcRecordHandle);
1501
1502                 ret = contacts_record_create(_contacts_name._uri, &dstRecordHandle);
1503                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1504
1505                 __ContactsRecordHandle nameHandle(dstRecordHandle);
1506
1507                 contacts_record_get_str(srcRecordHandle, _contacts_name.first, &pCharValue);
1508                 contacts_record_set_str(dstRecordHandle, _contacts_name.first, pCharValue);
1509
1510                 contacts_record_get_str(srcRecordHandle, _contacts_name.last, &pCharValue);
1511                 contacts_record_set_str(dstRecordHandle, _contacts_name.last, pCharValue);
1512
1513                 contacts_record_get_str(srcRecordHandle, _contacts_name.addition, &pCharValue);
1514                 contacts_record_set_str(dstRecordHandle, _contacts_name.addition, pCharValue);
1515
1516                 contacts_record_get_str(srcRecordHandle, _contacts_name.suffix, &pCharValue);
1517                 contacts_record_set_str(dstRecordHandle, _contacts_name.suffix, pCharValue);
1518
1519                 contacts_record_get_str(srcRecordHandle, _contacts_name.prefix, &pCharValue);
1520                 contacts_record_set_str(dstRecordHandle, _contacts_name.prefix, pCharValue);
1521
1522                 contacts_record_get_str(srcRecordHandle, _contacts_name.phonetic_first, &pCharValue);
1523                 contacts_record_set_str(dstRecordHandle, _contacts_name.phonetic_first, pCharValue);
1524
1525                 contacts_record_get_str(srcRecordHandle, _contacts_name.phonetic_middle, &pCharValue);
1526                 contacts_record_set_str(dstRecordHandle, _contacts_name.phonetic_middle, pCharValue);
1527
1528                 contacts_record_get_str(srcRecordHandle, _contacts_name.phonetic_last, &pCharValue);
1529                 contacts_record_set_str(dstRecordHandle, _contacts_name.phonetic_last, pCharValue);
1530
1531                 contacts_record_add_child_record(dstHandle, _contacts_my_profile.name, dstRecordHandle);
1532
1533                 nameHandle.Release();
1534         }
1535
1536         // company
1537         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.company, &count);
1538         for (i = 0; i < count; i++)
1539         {
1540                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.company, i, &srcRecordHandle);
1541
1542                 ret = contacts_record_create(_contacts_company._uri, &dstRecordHandle);
1543                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1544
1545                 __ContactsRecordHandle companyHandle(dstRecordHandle);
1546
1547                 contacts_record_get_int(srcRecordHandle, _contacts_company.type, &intValue);
1548                 contacts_record_set_int(dstRecordHandle, _contacts_company.type, intValue);
1549
1550                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.name, &pCharValue);
1551                 contacts_record_set_str(dstRecordHandle, _contacts_company.name, pCharValue);
1552
1553                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.department, &pCharValue);
1554                 contacts_record_set_str(dstRecordHandle, _contacts_company.department, pCharValue);
1555
1556                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.job_title, &pCharValue);
1557                 contacts_record_set_str(dstRecordHandle, _contacts_company.job_title, pCharValue);
1558
1559                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.assistant_name, &pCharValue);
1560                 contacts_record_set_str(dstRecordHandle, _contacts_company.assistant_name, pCharValue);
1561
1562                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.role, &pCharValue);
1563                 contacts_record_set_str(dstRecordHandle, _contacts_company.role, pCharValue);
1564
1565                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.logo, &pCharValue);
1566                 contacts_record_set_str(dstRecordHandle, _contacts_company.logo, pCharValue);
1567
1568                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.location, &pCharValue);
1569                 contacts_record_set_str(dstRecordHandle, _contacts_company.location, pCharValue);
1570
1571                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.description, &pCharValue);
1572                 contacts_record_set_str(dstRecordHandle, _contacts_company.description, pCharValue);
1573
1574                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.phonetic_name, &pCharValue);
1575                 contacts_record_set_str(dstRecordHandle, _contacts_company.phonetic_name, pCharValue);
1576
1577                 contacts_record_add_child_record(dstHandle, _contacts_my_profile.company, dstRecordHandle);
1578
1579                 companyHandle.Release();
1580         }
1581
1582         // note
1583         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.note, &count);
1584         for (i = 0; i < count; i++)
1585         {
1586                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.note, i, &srcRecordHandle);
1587
1588                 ret = contacts_record_create(_contacts_note._uri, &dstRecordHandle);
1589                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1590
1591                 __ContactsRecordHandle noteHandle(dstRecordHandle);
1592
1593                 contacts_record_get_str_p(srcRecordHandle, _contacts_note.note, &pCharValue);
1594                 contacts_record_set_str(dstRecordHandle, _contacts_note.note, pCharValue);
1595
1596                 contacts_record_add_child_record(dstHandle, _contacts_my_profile.note, dstRecordHandle);
1597
1598                 noteHandle.Release();
1599         }
1600
1601         // number
1602         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.number, &count);
1603         for (i = 0; i < count; i++)
1604         {
1605                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.number, i, &srcRecordHandle);
1606
1607                 ret = contacts_record_create(_contacts_number._uri, &dstRecordHandle);
1608                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1609
1610                 __ContactsRecordHandle numberHandle(dstRecordHandle);
1611
1612                 contacts_record_get_int(srcRecordHandle, _contacts_number.type, &intValue);
1613                 contacts_record_set_int(dstRecordHandle, _contacts_number.type, intValue);
1614
1615                 contacts_record_get_str_p(srcRecordHandle, _contacts_number.label, &pCharValue);
1616                 contacts_record_set_str(dstRecordHandle, _contacts_number.label, pCharValue);
1617
1618                 contacts_record_get_str_p(srcRecordHandle, _contacts_number.number, &pCharValue);
1619                 contacts_record_set_str(dstRecordHandle, _contacts_number.number, pCharValue);
1620
1621                 contacts_record_add_child_record(dstHandle, _contacts_my_profile.number, dstRecordHandle);
1622
1623                 numberHandle.Release();
1624         }
1625
1626         // email
1627         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.email, &count);
1628         for (i = 0; i < count; i++)
1629         {
1630                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.email, i, &srcRecordHandle);
1631
1632                 ret = contacts_record_create(_contacts_email._uri, &dstRecordHandle);
1633                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1634
1635                 __ContactsRecordHandle emailHandle(dstRecordHandle);
1636
1637                 contacts_record_get_int(srcRecordHandle, _contacts_email.type, &intValue);
1638                 contacts_record_set_int(dstRecordHandle, _contacts_email.type, intValue);
1639
1640                 contacts_record_get_str_p(srcRecordHandle, _contacts_email.label, &pCharValue);
1641                 contacts_record_set_str(dstRecordHandle, _contacts_email.label, pCharValue);
1642
1643                 contacts_record_get_str_p(srcRecordHandle, _contacts_email.email, &pCharValue);
1644                 contacts_record_set_str(dstRecordHandle, _contacts_email.email, pCharValue);
1645
1646                 contacts_record_add_child_record(dstHandle, _contacts_my_profile.email, dstRecordHandle);
1647
1648                 emailHandle.Release();
1649
1650         }
1651
1652         // event
1653         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.event, &count);
1654         for (i = 0; i < count; i++)
1655         {
1656                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.event, i, &srcRecordHandle);
1657
1658                 ret = contacts_record_create(_contacts_event._uri, &dstRecordHandle);
1659                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1660
1661                 __ContactsRecordHandle eventHandle(dstRecordHandle);
1662
1663                 contacts_record_get_int(srcRecordHandle, _contacts_event.type, &intValue);
1664                 contacts_record_set_int(dstRecordHandle, _contacts_event.type, intValue);
1665
1666                 contacts_record_get_str_p(srcRecordHandle, _contacts_event.label, &pCharValue);
1667                 contacts_record_set_str(dstRecordHandle, _contacts_event.label, pCharValue);
1668
1669                 contacts_record_get_int(srcRecordHandle, _contacts_event.date, &intValue);
1670                 contacts_record_set_int(dstRecordHandle, _contacts_event.date, intValue);
1671
1672                 contacts_record_add_child_record(dstHandle, _contacts_my_profile.event, dstRecordHandle);
1673
1674                 eventHandle.Release();
1675         }
1676
1677         // im address
1678         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.messenger, &count);
1679         for (i = 0; i < count; i++)
1680         {
1681                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.messenger, i, &srcRecordHandle);
1682
1683                 ret = contacts_record_create(_contacts_messenger._uri, &dstRecordHandle);
1684                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1685
1686                 __ContactsRecordHandle imAddressHandle(dstRecordHandle);
1687
1688                 contacts_record_get_int(srcRecordHandle, _contacts_messenger.type, &intValue);
1689                 contacts_record_set_int(dstRecordHandle, _contacts_messenger.type, intValue);
1690
1691                 contacts_record_get_str_p(srcRecordHandle, _contacts_messenger.label, &pCharValue);
1692                 contacts_record_set_str(dstRecordHandle, _contacts_messenger.label, pCharValue);
1693
1694                 contacts_record_get_str_p(srcRecordHandle, _contacts_messenger.im_id, &pCharValue);
1695                 contacts_record_set_str(dstRecordHandle, _contacts_messenger.im_id, pCharValue);
1696
1697                 contacts_record_add_child_record(dstHandle, _contacts_my_profile.messenger, dstRecordHandle);
1698
1699                 imAddressHandle.Release();
1700         }
1701
1702         // address
1703         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.address, &count);
1704         for (i = 0; i < count; i++)
1705         {
1706                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.address, i, &srcRecordHandle);
1707
1708                 ret = contacts_record_create(_contacts_address._uri, &dstRecordHandle);
1709                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1710
1711                 __ContactsRecordHandle addressHandle(dstRecordHandle);
1712
1713                 contacts_record_get_int(srcRecordHandle, _contacts_address.type, &intValue);
1714                 contacts_record_set_int(dstRecordHandle, _contacts_address.type, intValue);
1715
1716                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.label, &pCharValue);
1717                 contacts_record_set_str(dstRecordHandle, _contacts_address.label, pCharValue);
1718
1719                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.postbox, &pCharValue);
1720                 contacts_record_set_str(dstRecordHandle, _contacts_address.postbox, pCharValue);
1721
1722                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.extended, &pCharValue);
1723                 contacts_record_set_str(dstRecordHandle, _contacts_address.extended, pCharValue);
1724
1725                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.street, &pCharValue);
1726                 contacts_record_set_str(dstRecordHandle, _contacts_address.street, pCharValue);
1727
1728                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.locality, &pCharValue);
1729                 contacts_record_set_str(dstRecordHandle, _contacts_address.locality, pCharValue);
1730
1731                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.region, &pCharValue);
1732                 contacts_record_set_str(dstRecordHandle, _contacts_address.region, pCharValue);
1733
1734                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.postal_code, &pCharValue);
1735                 contacts_record_set_str(dstRecordHandle, _contacts_address.postal_code, pCharValue);
1736
1737                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.country, &pCharValue);
1738                 contacts_record_set_str(dstRecordHandle, _contacts_address.country, pCharValue);
1739
1740                 contacts_record_add_child_record(dstHandle, _contacts_my_profile.address, dstRecordHandle);
1741
1742                 addressHandle.Release();
1743         }
1744
1745         // url
1746         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.url, &count);
1747         for (i = 0; i < count; i++)
1748         {
1749                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.url, i, &srcRecordHandle);
1750
1751                 ret = contacts_record_create(_contacts_url._uri, &dstRecordHandle);
1752                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1753
1754                 __ContactsRecordHandle urlHandle(dstRecordHandle);
1755
1756                 contacts_record_get_int(srcRecordHandle, _contacts_url.type, &intValue);
1757                 contacts_record_set_int(dstRecordHandle, _contacts_url.type, intValue);
1758
1759                 contacts_record_get_str_p(srcRecordHandle, _contacts_url.label, &pCharValue);
1760                 contacts_record_set_str(dstRecordHandle, _contacts_url.label, pCharValue);
1761
1762                 contacts_record_get_str_p(srcRecordHandle, _contacts_url.url, &pCharValue);
1763                 contacts_record_set_str(dstRecordHandle, _contacts_url.url, pCharValue);
1764
1765                 contacts_record_add_child_record(dstHandle, _contacts_my_profile.url, dstRecordHandle);
1766
1767                 urlHandle.Release();
1768         }
1769
1770         // nickname
1771         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.nickname, &count);
1772         for (i = 0; i < count; i++)
1773         {
1774                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.nickname, i, &srcRecordHandle);
1775
1776                 ret = contacts_record_create(_contacts_nickname._uri, &dstRecordHandle);
1777                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1778
1779                 __ContactsRecordHandle nicknameHandle(dstRecordHandle);
1780
1781                 contacts_record_get_str_p(srcRecordHandle, _contacts_nickname.name, &pCharValue);
1782                 contacts_record_set_str(dstRecordHandle, _contacts_nickname.name, pCharValue);
1783
1784                 contacts_record_add_child_record(dstHandle, _contacts_my_profile.nickname, dstRecordHandle);
1785
1786                 nicknameHandle.Release();
1787         }
1788
1789         // relationship
1790         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.relationship, &count);
1791         for (i = 0; i < count; i++)
1792         {
1793                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.relationship, i, &srcRecordHandle);
1794
1795                 ret = contacts_record_create(_contacts_relationship._uri, &dstRecordHandle);
1796                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1797
1798                 __ContactsRecordHandle relationshipHandle(dstRecordHandle);
1799
1800                 contacts_record_get_str_p(srcRecordHandle, _contacts_relationship.name, &pCharValue);
1801                 contacts_record_set_str(dstRecordHandle, _contacts_relationship.name, pCharValue);
1802
1803                 contacts_record_add_child_record(dstHandle, _contacts_my_profile.relationship, dstRecordHandle);
1804
1805                 relationshipHandle.Release();
1806         }
1807
1808         // image
1809         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.image, &count);
1810
1811         int recordId = 0;
1812         contacts_record_get_int(srcHandle, _contacts_my_profile.id, &recordId);
1813
1814         if (count > 0 && recordId == 0)
1815         {
1816                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.image, 0, &srcRecordHandle);
1817
1818                 ret = contacts_record_create(_contacts_image._uri, &dstRecordHandle);
1819                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1820
1821                 __ContactsRecordHandle imageHandle(dstRecordHandle);
1822
1823                 contacts_record_get_int(srcRecordHandle, _contacts_image.type, &intValue);
1824                 contacts_record_set_int(dstRecordHandle, _contacts_image.type, intValue);
1825
1826                 contacts_record_get_str_p(srcRecordHandle, _contacts_image.label, &pCharValue);
1827                 contacts_record_set_str(dstRecordHandle, _contacts_image.label, pCharValue);
1828
1829                 contacts_record_get_str_p(srcRecordHandle, _contacts_image.path, &pCharValue);
1830                 contacts_record_set_str(dstRecordHandle, _contacts_image.path, pCharValue);
1831
1832                 contacts_record_add_child_record(dstHandle, _contacts_contact.image, dstRecordHandle);
1833
1834                 imageHandle.Release();
1835         }
1836
1837         return E_SUCCESS;
1838 }
1839
1840 contacts_record_h
1841 _AddressbookImpl::CopyContactRecordHandle(contacts_record_h srcHandle)
1842 {
1843         ClearLastResult();
1844
1845         int ret = CONTACTS_ERROR_NONE;
1846         unsigned int i = 0;
1847         unsigned int count = 0;
1848         char* pCharValue = null;
1849         int intValue = 0;
1850         bool boolValue = false;
1851
1852         contacts_record_h contactHandle = null;
1853         contacts_record_h sourceRecordHandle = null;
1854         contacts_record_h destRecordHandle = null;
1855
1856         ret = contacts_record_create(_contacts_contact._uri, &contactHandle);
1857         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1858
1859         // favorite
1860         contacts_record_get_bool(srcHandle, _contacts_contact.is_favorite, &boolValue);
1861         contacts_record_set_bool(contactHandle, _contacts_contact.is_favorite, boolValue);
1862
1863         // uid
1864         contacts_record_get_str_p(srcHandle, _contacts_contact.uid, &pCharValue);
1865         contacts_record_set_str(contactHandle, _contacts_contact.uid, pCharValue);
1866
1867         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
1868         // name
1869         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
1870         contacts_record_get_child_record_count(srcHandle, _contacts_contact.name, &count);
1871         if (count > 0)
1872         {
1873                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.name, 0, &sourceRecordHandle);
1874
1875                 ret = contacts_record_create(_contacts_name._uri, &destRecordHandle);
1876                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1877
1878                 __ContactsRecordHandle nameHandle(destRecordHandle);
1879
1880                 // 1. first
1881                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.first, &pCharValue);
1882                 contacts_record_set_str(destRecordHandle, _contacts_name.first, pCharValue);
1883
1884                 // 2. last
1885                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.last, &pCharValue);
1886                 contacts_record_set_str(destRecordHandle, _contacts_name.last, pCharValue);
1887
1888                 // 3. addition
1889                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.addition, &pCharValue);
1890                 contacts_record_set_str(destRecordHandle, _contacts_name.addition, pCharValue);
1891
1892                 // 4. suffix
1893                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.suffix, &pCharValue);
1894                 contacts_record_set_str(destRecordHandle, _contacts_name.suffix, pCharValue);
1895
1896                 // 5. prefix
1897                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.prefix, &pCharValue);
1898                 contacts_record_set_str(destRecordHandle, _contacts_name.prefix, pCharValue);
1899
1900                 // 6. phonetic_first
1901                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.phonetic_first, &pCharValue);
1902                 contacts_record_set_str(destRecordHandle, _contacts_name.phonetic_first, pCharValue);
1903
1904                 // 7. phonetic_last
1905                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.phonetic_last, &pCharValue);
1906                 contacts_record_set_str(destRecordHandle, _contacts_name.phonetic_last, pCharValue);
1907
1908                 // 8. phonetic_middle
1909                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.phonetic_middle, &pCharValue);
1910                 contacts_record_set_str(destRecordHandle, _contacts_name.phonetic_middle, pCharValue);
1911
1912                 contacts_record_add_child_record(contactHandle, _contacts_contact.name, destRecordHandle);
1913
1914                 nameHandle.Release();
1915         }
1916
1917         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
1918         // image
1919         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
1920         contacts_record_get_child_record_count(srcHandle, _contacts_contact.image, &count);
1921         for (i = 0; i < count; i++)
1922         {
1923                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.image, i, &sourceRecordHandle);
1924
1925                 ret = contacts_record_create(_contacts_image._uri, &destRecordHandle);
1926                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1927
1928                 __ContactsRecordHandle imageHandle(destRecordHandle);
1929
1930                 contacts_record_get_int(sourceRecordHandle, _contacts_image.type, &intValue);
1931                 contacts_record_set_int(destRecordHandle, _contacts_image.type, intValue);
1932
1933                 contacts_record_get_str_p(sourceRecordHandle, _contacts_image.label, &pCharValue);
1934                 contacts_record_set_str(destRecordHandle, _contacts_image.label, pCharValue);
1935
1936                 contacts_record_get_str_p(sourceRecordHandle, _contacts_image.path, &pCharValue);
1937                 contacts_record_set_str(destRecordHandle, _contacts_image.path, pCharValue);
1938
1939                 contacts_record_add_child_record(contactHandle, _contacts_contact.image, destRecordHandle);
1940
1941                 imageHandle.Release();
1942         }
1943
1944         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
1945         // company
1946         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
1947         contacts_record_get_child_record_count(srcHandle, _contacts_contact.company, &count);
1948         for (i = 0; i < count; i++)
1949         {
1950                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.company, i, &sourceRecordHandle);
1951
1952                 ret = contacts_record_create(_contacts_company._uri, &destRecordHandle);
1953                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1954
1955                 __ContactsRecordHandle companyHandle(destRecordHandle);
1956
1957                 contacts_record_get_int(sourceRecordHandle, _contacts_company.type, &intValue);
1958                 contacts_record_set_int(destRecordHandle, _contacts_company.type, intValue);
1959
1960                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.name, &pCharValue);
1961                 contacts_record_set_str(destRecordHandle, _contacts_company.name, pCharValue);
1962
1963                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.department, &pCharValue);
1964                 contacts_record_set_str(destRecordHandle, _contacts_company.department, pCharValue);
1965
1966                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.job_title, &pCharValue);
1967                 contacts_record_set_str(destRecordHandle, _contacts_company.job_title, pCharValue);
1968
1969                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.assistant_name, &pCharValue);
1970                 contacts_record_set_str(destRecordHandle, _contacts_company.assistant_name, pCharValue);
1971
1972                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.role, &pCharValue);
1973                 contacts_record_set_str(destRecordHandle, _contacts_company.role, pCharValue);
1974
1975                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.logo, &pCharValue);
1976                 contacts_record_set_str(destRecordHandle, _contacts_company.logo, pCharValue);
1977
1978                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.location, &pCharValue);
1979                 contacts_record_set_str(destRecordHandle, _contacts_company.location, pCharValue);
1980
1981                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.description, &pCharValue);
1982                 contacts_record_set_str(destRecordHandle, _contacts_company.description, pCharValue);
1983
1984                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.phonetic_name, &pCharValue);
1985                 contacts_record_set_str(destRecordHandle, _contacts_company.phonetic_name, pCharValue);
1986
1987                 contacts_record_add_child_record(contactHandle, _contacts_contact.company, destRecordHandle);
1988
1989                 companyHandle.Release();
1990         }
1991
1992         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
1993         // note
1994         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
1995         contacts_record_get_child_record_count(srcHandle, _contacts_contact.note, &count);
1996         for (i = 0; i < count; i++)
1997         {
1998                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.note, i, &sourceRecordHandle);
1999
2000                 ret = contacts_record_create(_contacts_note._uri, &destRecordHandle);
2001                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2002
2003                 __ContactsRecordHandle noteHandle(destRecordHandle);
2004
2005                 contacts_record_get_str_p(sourceRecordHandle, _contacts_note.note, &pCharValue);
2006                 contacts_record_set_str(destRecordHandle, _contacts_note.note, pCharValue);
2007
2008                 contacts_record_add_child_record(contactHandle, _contacts_contact.note, destRecordHandle);
2009
2010                 noteHandle.Release();
2011         }
2012
2013         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2014         // phone number
2015         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2016         contacts_record_get_child_record_count(srcHandle, _contacts_contact.number, &count);
2017         for (i = 0; i < count; i++)
2018         {
2019                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.number, i, &sourceRecordHandle);
2020
2021                 ret = contacts_record_create(_contacts_number._uri, &destRecordHandle);
2022                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2023
2024                 __ContactsRecordHandle numberHandle(destRecordHandle);
2025
2026                 contacts_record_get_int(sourceRecordHandle, _contacts_number.type, &intValue);
2027                 contacts_record_set_int(destRecordHandle, _contacts_number.type, intValue);
2028
2029                 contacts_record_get_str_p(sourceRecordHandle, _contacts_number.label, &pCharValue);
2030                 contacts_record_set_str(destRecordHandle, _contacts_number.label, pCharValue);
2031
2032                 contacts_record_get_str_p(sourceRecordHandle, _contacts_number.number, &pCharValue);
2033                 contacts_record_set_str(destRecordHandle, _contacts_number.number, pCharValue);
2034
2035                 contacts_record_add_child_record(contactHandle, _contacts_contact.number, destRecordHandle);
2036
2037                 numberHandle.Release();
2038         }
2039
2040         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2041         // email
2042         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2043         contacts_record_get_child_record_count(srcHandle, _contacts_contact.email, &count);
2044         for (i = 0; i < count; i++)
2045         {
2046                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.email, i, &sourceRecordHandle);
2047
2048                 ret = contacts_record_create(_contacts_email._uri, &destRecordHandle);
2049                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2050
2051                 __ContactsRecordHandle emailHandle(destRecordHandle);
2052
2053                 contacts_record_get_int(sourceRecordHandle, _contacts_email.type, &intValue);
2054                 contacts_record_set_int(destRecordHandle, _contacts_email.type, intValue);
2055
2056                 contacts_record_get_str_p(sourceRecordHandle, _contacts_email.label, &pCharValue);
2057                 contacts_record_set_str(destRecordHandle, _contacts_email.label, pCharValue);
2058
2059                 contacts_record_get_str_p(sourceRecordHandle, _contacts_email.email, &pCharValue);
2060                 contacts_record_set_str(destRecordHandle, _contacts_email.email, pCharValue);
2061
2062                 contacts_record_add_child_record(contactHandle, _contacts_contact.email, destRecordHandle);
2063
2064                 emailHandle.Release();
2065
2066         }
2067
2068         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2069         // event
2070         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2071         contacts_record_get_child_record_count(srcHandle, _contacts_contact.event, &count);
2072         for (i = 0; i < count; i++)
2073         {
2074                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.event, i, &sourceRecordHandle);
2075
2076                 ret = contacts_record_create(_contacts_event._uri, &destRecordHandle);
2077                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2078
2079                 __ContactsRecordHandle eventHandle(destRecordHandle);
2080
2081                 contacts_record_get_int(sourceRecordHandle, _contacts_event.type, &intValue);
2082                 contacts_record_set_int(destRecordHandle, _contacts_event.type, intValue);
2083
2084                 contacts_record_get_str_p(sourceRecordHandle, _contacts_event.label, &pCharValue);
2085                 contacts_record_set_str(destRecordHandle, _contacts_event.label, pCharValue);
2086
2087                 contacts_record_get_int(sourceRecordHandle, _contacts_event.date, &intValue);
2088                 contacts_record_set_int(destRecordHandle, _contacts_event.date, intValue);
2089
2090                 contacts_record_add_child_record(contactHandle, _contacts_contact.event, destRecordHandle);
2091
2092                 eventHandle.Release();
2093         }
2094
2095         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2096         // im address
2097         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2098         contacts_record_get_child_record_count(srcHandle, _contacts_contact.messenger, &count);
2099         for (i = 0; i < count; i++)
2100         {
2101                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.messenger, i, &sourceRecordHandle);
2102
2103                 ret = contacts_record_create(_contacts_messenger._uri, &destRecordHandle);
2104                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2105
2106                 __ContactsRecordHandle imAddressHandle(destRecordHandle);
2107
2108                 contacts_record_get_int(sourceRecordHandle, _contacts_messenger.type, &intValue);
2109                 contacts_record_set_int(destRecordHandle, _contacts_messenger.type, intValue);
2110
2111                 contacts_record_get_str_p(sourceRecordHandle, _contacts_messenger.label, &pCharValue);
2112                 contacts_record_set_str(destRecordHandle, _contacts_messenger.label, pCharValue);
2113
2114                 contacts_record_get_str_p(sourceRecordHandle, _contacts_messenger.im_id, &pCharValue);
2115                 contacts_record_set_str(destRecordHandle, _contacts_messenger.im_id, pCharValue);
2116
2117                 contacts_record_add_child_record(contactHandle, _contacts_contact.messenger, destRecordHandle);
2118
2119                 imAddressHandle.Release();
2120         }
2121
2122         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2123         // address
2124         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2125         contacts_record_get_child_record_count(srcHandle, _contacts_contact.address, &count);
2126         for (i = 0; i < count; i++)
2127         {
2128                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.address, i, &sourceRecordHandle);
2129
2130                 ret = contacts_record_create(_contacts_address._uri, &destRecordHandle);
2131                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2132
2133                 __ContactsRecordHandle addressHandle(destRecordHandle);
2134
2135                 contacts_record_get_int(sourceRecordHandle, _contacts_address.type, &intValue);
2136                 contacts_record_set_int(destRecordHandle, _contacts_address.type, intValue);
2137
2138                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.label, &pCharValue);
2139                 contacts_record_set_str(destRecordHandle, _contacts_address.label, pCharValue);
2140
2141                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.postbox, &pCharValue);
2142                 contacts_record_set_str(destRecordHandle, _contacts_address.postbox, pCharValue);
2143
2144                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.extended, &pCharValue);
2145                 contacts_record_set_str(destRecordHandle, _contacts_address.extended, pCharValue);
2146
2147                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.street, &pCharValue);
2148                 contacts_record_set_str(destRecordHandle, _contacts_address.street, pCharValue);
2149
2150                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.locality, &pCharValue);
2151                 contacts_record_set_str(destRecordHandle, _contacts_address.locality, pCharValue);
2152
2153                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.region, &pCharValue);
2154                 contacts_record_set_str(destRecordHandle, _contacts_address.region, pCharValue);
2155
2156                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.postal_code, &pCharValue);
2157                 contacts_record_set_str(destRecordHandle, _contacts_address.postal_code, pCharValue);
2158
2159                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.country, &pCharValue);
2160                 contacts_record_set_str(destRecordHandle, _contacts_address.country, pCharValue);
2161
2162                 contacts_record_add_child_record(contactHandle, _contacts_contact.address, destRecordHandle);
2163
2164                 addressHandle.Release();
2165         }
2166
2167         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2168         // url
2169         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2170         contacts_record_get_child_record_count(srcHandle, _contacts_contact.url, &count);
2171         for (i = 0; i < count; i++)
2172         {
2173                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.url, i, &sourceRecordHandle);
2174
2175                 ret = contacts_record_create(_contacts_url._uri, &destRecordHandle);
2176                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2177
2178                 __ContactsRecordHandle urlHandle(destRecordHandle);
2179
2180                 contacts_record_get_int(sourceRecordHandle, _contacts_url.type, &intValue);
2181                 contacts_record_set_int(destRecordHandle, _contacts_url.type, intValue);
2182
2183                 contacts_record_get_str_p(sourceRecordHandle, _contacts_url.label, &pCharValue);
2184                 contacts_record_set_str(destRecordHandle, _contacts_url.label, pCharValue);
2185
2186                 contacts_record_get_str_p(sourceRecordHandle, _contacts_url.url, &pCharValue);
2187                 contacts_record_set_str(destRecordHandle, _contacts_url.url, pCharValue);
2188
2189                 contacts_record_add_child_record(contactHandle, _contacts_contact.url, destRecordHandle);
2190
2191                 urlHandle.Release();
2192         }
2193
2194         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2195         // nickname
2196         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2197         contacts_record_get_child_record_count(srcHandle, _contacts_contact.nickname, &count);
2198         for (i = 0; i < count; i++)
2199         {
2200                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.nickname, i, &sourceRecordHandle);
2201
2202                 ret = contacts_record_create(_contacts_nickname._uri, &destRecordHandle);
2203                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2204
2205                 __ContactsRecordHandle nicknameHandle(destRecordHandle);
2206
2207                 contacts_record_get_str_p(sourceRecordHandle, _contacts_nickname.name, &pCharValue);
2208                 contacts_record_set_str(destRecordHandle, _contacts_nickname.name, pCharValue);
2209
2210                 contacts_record_add_child_record(contactHandle, _contacts_contact.nickname, destRecordHandle);
2211
2212                 nicknameHandle.Release();
2213         }
2214
2215         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2216         // relationship
2217         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2218         contacts_record_get_child_record_count(srcHandle, _contacts_contact.relationship, &count);
2219         for (i = 0; i < count; i++)
2220         {
2221                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.relationship, i, &sourceRecordHandle);
2222
2223                 ret = contacts_record_create(_contacts_relationship._uri, &destRecordHandle);
2224                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2225
2226                 __ContactsRecordHandle relationshipHandle(destRecordHandle);
2227
2228                 contacts_record_get_str_p(sourceRecordHandle, _contacts_relationship.name, &pCharValue);
2229                 contacts_record_set_str(destRecordHandle, _contacts_relationship.name, pCharValue);
2230
2231                 contacts_record_add_child_record(contactHandle, _contacts_contact.relationship, destRecordHandle);
2232
2233                 relationshipHandle.Release();
2234         }
2235
2236         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2237         // app launch data
2238         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2239         contacts_record_get_child_record_count(srcHandle, _contacts_contact.profile, &count);
2240         for (i = 0; i < count; i++)
2241         {
2242                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.profile, i, &sourceRecordHandle);
2243
2244                 ret = contacts_record_create(_contacts_profile._uri, &destRecordHandle);
2245                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2246
2247                 __ContactsRecordHandle profileHandle(destRecordHandle);
2248
2249                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.text, &pCharValue);
2250                 contacts_record_set_str(destRecordHandle, _contacts_profile.text, pCharValue);
2251
2252                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.uid, &pCharValue);
2253                 contacts_record_set_str(destRecordHandle, _contacts_profile.uid, pCharValue);
2254
2255                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.app_id, &pCharValue);
2256                 contacts_record_set_str(destRecordHandle, _contacts_profile.app_id, pCharValue);
2257
2258                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.service_operation, &pCharValue);
2259                 contacts_record_set_str(destRecordHandle, _contacts_profile.service_operation, pCharValue);
2260
2261                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.uri, &pCharValue);
2262                 contacts_record_set_str(destRecordHandle, _contacts_profile.uri, pCharValue);
2263
2264                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.category, &pCharValue);
2265                 contacts_record_set_str(destRecordHandle, _contacts_profile.category, pCharValue);
2266
2267                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.mime, &pCharValue);
2268                 contacts_record_set_str(destRecordHandle, _contacts_profile.mime, pCharValue);
2269
2270                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.extra_data, &pCharValue);
2271                 contacts_record_set_str(destRecordHandle, _contacts_profile.extra_data, pCharValue);
2272
2273                 contacts_record_add_child_record(contactHandle, _contacts_contact.profile, destRecordHandle);
2274
2275                 profileHandle.Release();
2276         }
2277
2278         return contactHandle;
2279 }
2280
2281 void
2282 _AddressbookImpl::OnContactChanged(void)
2283 {
2284         if (__pIRecordEventListener == null && __pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
2285         {
2286                 return;
2287         }
2288
2289         std::unique_ptr<IList, AllElementsDeleter> pChangedContactList(GetChangedContactsAfterN(__dbVersionForContact, __dbVersionForContact));
2290         SysTryReturnVoidResult(NID_SCL, pChangedContactList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2291
2292         if (pChangedContactList->GetCount() > 0)
2293         {
2294                 if (__pIAddressbookEventListener != null)
2295                 {
2296                         __pIAddressbookEventListener->OnContactsChanged(*pChangedContactList);
2297                 }
2298                 else if (__pIAddressbookChangeEventListener != null)
2299                 {
2300                         __pIAddressbookChangeEventListener->OnContactsChanged(*pChangedContactList);
2301                 }
2302                 else
2303                 {
2304                         RecordEventType recordEventType = RECORD_ADDED;
2305                         std::unique_ptr<IEnumerator> pEnum(pChangedContactList->GetEnumeratorN());
2306                         std::unique_ptr<Contact> pContact(null);
2307
2308                         while (pEnum->MoveNext() == E_SUCCESS)
2309                         {
2310                                 ContactChangeInfo* pContactChagneInfo = static_cast<ContactChangeInfo*>(pEnum->GetCurrent());
2311
2312                                 if (pContactChagneInfo->GetChangeType() == RECORD_CHANGE_TYPE_ADDED)
2313                                 {
2314                                         recordEventType = RECORD_ADDED;
2315                                         pContact.reset(GetContactN(pContactChagneInfo->GetContactId()));
2316                                         if (pContact == null)
2317                                         {
2318                                                 if (GetLastResult() == E_OBJ_NOT_FOUND)
2319                                                 {
2320                                                         continue;
2321                                                 }
2322                                                 else
2323                                                 {
2324                                                         break;
2325                                                 }
2326                                         }
2327                                 }
2328                                 else if (pContactChagneInfo->GetChangeType() == RECORD_CHANGE_TYPE_UPDATED)
2329                                 {
2330                                         recordEventType = RECORD_UPDATED;
2331                                         pContact.reset(GetContactN(pContactChagneInfo->GetContactId()));
2332                                         if (pContact == null)
2333                                         {
2334                                                 if (GetLastResult() == E_OBJ_NOT_FOUND)
2335                                                 {
2336                                                         continue;
2337                                                 }
2338                                                 else
2339                                                 {
2340                                                         break;
2341                                                 }
2342                                         }
2343                                 }
2344                                 else
2345                                 {
2346                                         recordEventType = RECORD_REMOVED;
2347                                         pContact.reset(new (std::nothrow) Contact());
2348                                         if (pContact == null)
2349                                         {
2350                                                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2351                                                 break;
2352                                         }
2353
2354                                         _RecordImpl::GetInstance(*pContact)->SetRecordId(pContactChagneInfo->GetContactId());
2355                                 }
2356
2357                                 __pIRecordEventListener->OnRecordChangedN(recordEventType, RECORD_TYPE_CONTACT, *pContact, null, null);
2358                         }
2359                 }
2360
2361         }
2362 }
2363
2364 void
2365 _AddressbookImpl::OnCategoryChanged(void)
2366 {
2367         if (__pIRecordEventListener == null && __pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
2368         {
2369                 return;
2370         }
2371
2372         std::unique_ptr<IList, AllElementsDeleter> pChangedCategoryList(GetChangedGroupsAfterN(__dbVersionForGroup, __dbVersionForGroup));
2373         SysTryReturnVoidResult(NID_SCL, pChangedCategoryList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2374
2375         if (pChangedCategoryList->GetCount() > 0)
2376         {
2377                 if (__pIAddressbookEventListener != null)
2378                 {
2379                         __pIAddressbookEventListener->OnCategoriesChanged(*pChangedCategoryList);
2380                 }
2381                 else if (__pIAddressbookChangeEventListener != null)
2382                 {
2383                         __pIAddressbookChangeEventListener->OnCategoriesChanged(*pChangedCategoryList);
2384                 }
2385                 else
2386                 {
2387                         RecordEventType recordEventType = RECORD_ADDED;
2388                         std::unique_ptr<IEnumerator> pEnum(pChangedCategoryList->GetEnumeratorN());
2389                         std::unique_ptr<Category> pCategory(null);
2390
2391                         while (pEnum->MoveNext() == E_SUCCESS)
2392                         {
2393                                 CategoryChangeInfo* pCategoryChagneInfo = static_cast<CategoryChangeInfo*>(pEnum->GetCurrent());
2394
2395                                 if (pCategoryChagneInfo->GetChangeType() == RECORD_CHANGE_TYPE_ADDED)
2396                                 {
2397                                         recordEventType = RECORD_ADDED;
2398                                         pCategory.reset(GetCategoryN(pCategoryChagneInfo->GetCategoryId()));
2399                                         if (pCategory == null)
2400                                         {
2401                                                 if (GetLastResult() == E_OBJ_NOT_FOUND)
2402                                                 {
2403                                                         continue;
2404                                                 }
2405                                                 else
2406                                                 {
2407                                                         break;
2408                                                 }
2409                                         }
2410                                 }
2411                                 else if (pCategoryChagneInfo->GetChangeType() == RECORD_CHANGE_TYPE_UPDATED)
2412                                 {
2413                                         recordEventType = RECORD_UPDATED;
2414                                         pCategory.reset(GetCategoryN(pCategoryChagneInfo->GetCategoryId()));
2415                                         if (pCategory == null)
2416                                         {
2417                                                 if (GetLastResult() == E_OBJ_NOT_FOUND)
2418                                                 {
2419                                                         continue;
2420                                                 }
2421                                                 else
2422                                                 {
2423                                                         break;
2424                                                 }
2425                                         }
2426                                 }
2427                                 else
2428                                 {
2429                                         recordEventType = RECORD_REMOVED;
2430                                         pCategory.reset(new (std::nothrow) Category());
2431                                         if (pCategory == null)
2432                                         {
2433                                                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2434                                                 break;
2435                                         }
2436
2437                                         _RecordImpl::GetInstance(*pCategory)->SetRecordId(pCategoryChagneInfo->GetCategoryId());
2438                                 }
2439
2440                                 __pIRecordEventListener->OnRecordChangedN(recordEventType, RECORD_TYPE_CATEGORY, *pCategory, null, null);
2441                         }
2442                 }
2443
2444         }
2445 }
2446
2447 void
2448 _AddressbookImpl::OnRelationChanged(void)
2449 {
2450         if (__pIAddressbookEventListener == null && __pIRecordEventListener == null)
2451         {
2452                 return;
2453         }
2454
2455         std::unique_ptr<IList, AllElementsDeleter> pChangedCategoryList(GetChangedRelationsAfterN(__dbVersionForRelation, __dbVersionForRelation));
2456         SysTryReturnVoidResult(NID_SCL, pChangedCategoryList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2457
2458         if (pChangedCategoryList->GetCount() > 0)
2459         {
2460                 if (__pIAddressbookEventListener != null)
2461                 {
2462                         __pIAddressbookEventListener->OnCategoriesChanged(*pChangedCategoryList);
2463                 }
2464                 else
2465                 {
2466                         RecordEventType recordEventType = RECORD_ADDED;
2467                         std::unique_ptr<IEnumerator> pEnum(pChangedCategoryList->GetEnumeratorN());
2468                         std::unique_ptr<Category> pCategory(null);
2469
2470                         while (pEnum->MoveNext() == E_SUCCESS)
2471                         {
2472                                 CategoryChangeInfo* pCategoryChagneInfo = static_cast<CategoryChangeInfo*>(pEnum->GetCurrent());
2473
2474                                 recordEventType = RECORD_UPDATED;
2475                                 pCategory.reset(GetCategoryN(pCategoryChagneInfo->GetCategoryId()));
2476                                 if (pCategory == null)
2477                                 {
2478                                         if (GetLastResult() == E_OBJ_NOT_FOUND)
2479                                         {
2480                                                 continue;
2481                                         }
2482                                         else
2483                                         {
2484                                                 break;
2485                                         }
2486                                 }
2487
2488                                 __pIRecordEventListener->OnRecordChangedN(recordEventType, RECORD_TYPE_CATEGORY, *pCategory, null, null);
2489                         }
2490                 }
2491         }
2492 }
2493
2494 _AddressbookImpl*
2495 _AddressbookImpl::GetInstance(Addressbook& addressbook)
2496 {
2497         return addressbook.__pAddressbookImpl;
2498 }
2499
2500 const _AddressbookImpl*
2501 _AddressbookImpl::GetInstance(const Addressbook& addressbook)
2502 {
2503         return addressbook.__pAddressbookImpl;
2504 }
2505
2506 }}  // Tizen::Social