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