Fix code for TDIS-5396
[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         SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. The specified contact is invalid.", GetErrorMessage(E_INVALID_ARG));
703
704         ClearLastResult();
705
706         __Filter<__ContactsGroupRelation> filter;
707         filter.Construct();
708         filter.AddInt(_contacts_group_relation.contact_id, CONTACTS_MATCH_EQUAL, contactId);
709
710         __Query<__ContactsGroupRelation> query;
711         query.Construct();
712         query.SetFilter(filter);
713         query.SetSort(_contacts_group_relation.name, true);
714
715         IList* pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsGroupRelation, Category>(query);
716         SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
717
718         return pCategories;
719 }
720
721 IList*
722 _AddressbookImpl::GetAllContactsN(void) const
723 {
724         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
725
726         ClearLastResult();
727
728         __Filter<__ContactsContact> filter;
729         filter.Construct();
730         filter.AddInt(_contacts_contact.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
731
732         __Query<__ContactsContact> query;
733         query.Construct();
734         query.SetFilter(filter);
735         query.SetSort(_contacts_contact.display_name, true);
736
737         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
738         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
739
740         return pContacts;
741 }
742
743 IList*
744 _AddressbookImpl::GetContactsByCategoryN(RecordId categoryId) const
745 {
746         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));
747         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
748
749         ClearLastResult();
750
751         __Filter<__ContactsContactGroupRel> filter;
752         filter.Construct();
753         if (categoryId != INVALID_RECORD_ID)
754         {
755                 filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_EQUAL, categoryId);
756         }
757         else
758         {
759                 filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_NONE, 0);
760         }
761
762         __Query<__ContactsContactGroupRel> query;
763         query.Construct();
764         query.SetFilter(filter);
765         query.SetSort(_contacts_contact_grouprel.display_name, true);
766
767         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Contact>(query);
768         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
769
770         return pContacts;
771 }
772
773 IList*
774 _AddressbookImpl::GetContactsN(int pageNo, int countPerPage) const
775 {
776         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);
777         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
778
779         int offset = (pageNo - 1)*countPerPage;
780         int limit = countPerPage;
781
782         ClearLastResult();
783
784         __Filter<__ContactsContact> filter;
785         filter.Construct();
786         filter.AddInt(_contacts_contact.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
787
788         __Query<__ContactsContact> query;
789         query.Construct();
790         query.SetFilter(filter);
791         query.SetSort(_contacts_contact.display_name, true);
792
793         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query, offset, limit);
794         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
795
796         return pContacts;
797 }
798
799 IList*
800 _AddressbookImpl::GetContactsInN(const Category& category, int pageNo, int countPerPage) const
801 {
802         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));
803         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);
804         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
805
806         int offset = (pageNo - 1)*countPerPage;
807         int limit = countPerPage;
808
809         ClearLastResult();
810
811         __Filter<__ContactsContactGroupRel> filter;
812         filter.Construct();
813         filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_EQUAL, category.GetRecordId());
814
815         __Query<__ContactsContactGroupRel> query;
816         query.Construct();
817         query.SetFilter(filter);
818         query.SetSort(_contacts_contact_grouprel.display_name, true);
819
820         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Contact>(query, offset, limit);
821         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
822
823         return pContacts;
824 }
825
826 IList*
827 _AddressbookImpl::SearchContactsByEmailN(const String& email) const
828 {
829         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));
830         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
831
832         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
833         {
834                 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));
835         }
836
837         ClearLastResult();
838
839         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(email));
840         SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
841
842         __Filter<__ContactsContactEmail> filter;
843         filter.Construct();
844         filter.AddInt(_contacts_contact_email.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
845         filter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
846         filter.AddString(_contacts_contact_email.email, CONTACTS_MATCH_CONTAINS, pCharArray.get());
847
848         unsigned int projectionIds[1];
849         projectionIds[0] = _contacts_contact_email.contact_id;
850
851         __Query<__ContactsContactEmail> query;
852         query.Construct();
853         query.SetProjection(projectionIds, 1);
854         query.SetFilter(filter);
855         query.SetDistinct(true);
856         query.SetSort(_contacts_contact_email.display_name, true);
857
858         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactEmail, Contact>(query);
859         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
860
861         return pContacts;
862 }
863
864 IList*
865 _AddressbookImpl::SearchContactsByNameN(const String& name) const
866 {
867         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));
868         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
869
870         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
871         {
872                 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));
873         }
874
875
876         ClearLastResult();
877
878         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(name));
879         SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
880
881         __Filter<__ContactsContact> filter;
882         filter.Construct();
883         filter.AddInt(_contacts_contact.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
884         filter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
885         filter.AddString(_contacts_contact.display_name, CONTACTS_MATCH_CONTAINS, pCharArray.get());
886
887         __Query<__ContactsContact> query;
888         query.Construct();
889         query.SetFilter(filter);
890         query.SetSort(_contacts_contact.display_name, true);
891         query.SetDistinct(true);
892
893         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
894         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
895
896         return pContacts;
897 }
898
899
900 IList*
901 _AddressbookImpl::SearchContactsByPhoneNumberN(const String& phoneNumber) const
902 {
903         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));
904         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
905
906
907         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
908         {
909                 int count = phoneNumber.GetLength();
910                 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));
911         }
912
913         ClearLastResult();
914
915         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(phoneNumber));
916         SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
917
918         __Filter<__ContactsContactNumber> filter;
919         filter.Construct();
920         filter.AddInt(_contacts_contact_number.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
921         filter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
922         filter.AddString(_contacts_contact_number.number, CONTACTS_MATCH_CONTAINS, pCharArray.get());
923
924         unsigned int projectionIds[1];
925         projectionIds[0] = _contacts_contact_number.contact_id;
926
927         __Query<__ContactsContactNumber> query;
928         query.Construct();
929         query.SetProjection(projectionIds, 1);
930         query.SetFilter(filter);
931         query.SetDistinct(true);
932         query.SetSort(_contacts_contact_number.display_name, true);
933
934         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactNumber, Contact>(query);
935         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
936
937         return pContacts;
938 }
939
940 int
941 _AddressbookImpl::GetCategoryCount(void) const
942 {
943         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
944
945         int count = -1;
946
947         ClearLastResult();
948
949         __Filter<__ContactsGroup> filter;
950         filter.Construct();
951         filter.AddInt(_contacts_group.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
952
953         __Query<__ContactsGroup> query;
954         query.Construct();
955         query.SetFilter(filter);
956
957         count = _AddressbookUtil::GetCountWithQuery(query);
958         SysTryReturn(NID_SCL, count >= 0, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
959
960         return count;
961 }
962
963 int
964 _AddressbookImpl::GetContactCount(void) const
965 {
966         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
967
968         int count = -1;
969
970         ClearLastResult();
971
972         __Filter<__ContactsContact> filter;
973         filter.Construct();
974         filter.AddInt(_contacts_contact.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
975
976         __Query<__ContactsContact> query;
977         query.Construct();
978         query.SetFilter(filter);
979
980         count = _AddressbookUtil::GetCountWithQuery(query);
981         SysTryReturn(NID_SCL, count >= 0, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
982
983         return count;
984 }
985
986 Contact*
987 _AddressbookImpl::GetContactN(RecordId contactId) const
988 {
989         SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. contactId = %d.", GetErrorMessage(E_INVALID_ARG), contactId);
990         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
991
992         int intValue = 0;
993         contacts_record_h contactHandle = null;
994
995         ClearLastResult();
996
997         std::unique_ptr<Contact> pContact(new (std::nothrow) Contact());
998         SysTryReturn(NID_SCL, pContact, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
999
1000         int ret = contacts_db_get_record(_contacts_contact._uri, contactId, &contactHandle);
1001         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
1002         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1003         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM)); 
1004
1005         _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(contactHandle);
1006
1007         contacts_record_get_int(contactHandle, _contacts_contact.id, &intValue);
1008         SysTryReturn(NID_SCL, intValue == contactId, null, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
1009
1010         _RecordImpl::GetInstance(*pContact)->SetRecordId(intValue);
1011
1012         return pContact.release();
1013 }
1014
1015 Category*
1016 _AddressbookImpl::GetCategoryN(RecordId categoryId) const
1017 {
1018         SysTryReturn(NID_SCL, categoryId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. categoryId = %d.", GetErrorMessage(E_INVALID_ARG), categoryId);
1019         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1020
1021         contacts_record_h recordHandle = null;
1022
1023         ClearLastResult();
1024
1025         int intValue = 0;
1026         int ret = contacts_db_get_record(_contacts_group._uri, categoryId, &recordHandle);
1027         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
1028         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1029         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1030
1031         std::unique_ptr<Category> pCategory(new (std::nothrow) Category());
1032         SysTryReturn(NID_SCL, pCategory != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1033
1034         contacts_record_get_int(recordHandle, _contacts_group.id, &intValue);
1035         SysTryReturn(NID_SCL, intValue == categoryId, null, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
1036
1037         __Filter<__ContactsGroupRelation> filter;
1038         filter.Construct();
1039         filter.AddInt(_contacts_group_relation.group_id, CONTACTS_MATCH_EQUAL, categoryId);
1040
1041         __Query<__ContactsGroupRelation> query;
1042         query.Construct();
1043         query.SetFilter(filter);
1044
1045         int count = _AddressbookUtil::GetCountWithQuery(query);
1046         SysTryReturn(NID_SCL, count >= 0, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1047
1048         _CategoryImpl::GetInstance(*pCategory)->SetRecordHandle(recordHandle);
1049         _CategoryImpl::GetInstance(*pCategory)->SetMemberCount(count);
1050         _RecordImpl::GetInstance(*pCategory)->SetRecordId(categoryId);
1051
1052         return pCategory.release();
1053 }
1054
1055 int
1056 _AddressbookImpl::GetLatestVersion(void) const
1057 {
1058         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1059
1060         int latestVersion = -1;
1061
1062         ClearLastResult();
1063
1064         int ret = contacts_db_get_current_version(&latestVersion);
1065         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, -1, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1066         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1067
1068         return latestVersion;
1069 }
1070
1071 IList*
1072 _AddressbookImpl::GetChangedContactsAfterN(int version, int& latestVersion) const
1073 {
1074         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);
1075         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1076
1077         IList* pChangedContacts = _AddressbookUtil::SearchWithVersionN<__ContactsContactUpdatedInfo, ContactChangeInfo>(__addressbookId, version, latestVersion);
1078         SysTryReturn(NID_SCL, pChangedContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1079
1080         return pChangedContacts;
1081 }
1082
1083 IList*
1084 _AddressbookImpl::GetChangedCategoriesAfterN(int version, int& latestVersion) const
1085 {
1086         SysTryReturn(NID_SCL, version >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. version %d must be greater that or equal 0.", GetErrorMessage(E_INVALID_ARG), version);
1087         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1088
1089         int latestVersion1 = 0;
1090         int latestVersion2 = 0;
1091
1092         std::unique_ptr<IList, AllElementsDeleter> pChangedGroups(_AddressbookUtil::SearchWithVersionN<__ContactsGroupUpdatedInfo, CategoryChangeInfo>(__addressbookId, version, latestVersion1));
1093         SysTryReturn(NID_SCL, pChangedGroups != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1094
1095         std::unique_ptr<IList, AllElementsDeleter> pChangedRelations(_AddressbookUtil::SearchWithVersionN<__ContactsGroupRelUpdatedInfo, CategoryChangeInfo>(__addressbookId, version, latestVersion2));
1096         SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1097
1098         std::unique_ptr<ArrayList, AllElementsDeleter> pChangeList(new (std::nothrow) Tizen::Base::Collection::ArrayList());
1099         SysTryReturn(NID_SCL, pChangeList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1100
1101         result r = pChangeList->AddItems(*pChangedGroups);
1102         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1103
1104         r = pChangeList->AddItems(*pChangedRelations);
1105         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1106
1107         pChangedGroups->RemoveAll(false);
1108         pChangedRelations->RemoveAll(false);
1109
1110         latestVersion = latestVersion2 > latestVersion1 ? latestVersion2 : latestVersion1;
1111         
1112         return pChangeList.release();
1113 }
1114
1115 IList*
1116 _AddressbookImpl::GetChangedContactInfoListN(int version, int& latestVersion) const
1117 {
1118         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);
1119         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1120
1121         IList* pChangedContacts = _AddressbookUtil::SearchWithVersionN<__ContactsContactUpdatedInfo, ContactChangeInfo>(__addressbookId, version, latestVersion);
1122         SysTryReturn(NID_SCL, pChangedContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1123
1124         return pChangedContacts;
1125 }
1126
1127 IList*
1128 _AddressbookImpl::GetChangedCategoryInfoListN(int version, int& latestVersion) const
1129 {
1130         return GetChangedGroupsAfterN(version, latestVersion);
1131 }
1132
1133 IList*
1134 _AddressbookImpl::GetChangedGroupsAfterN(int version, int& latestVersion) const
1135 {
1136         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);
1137         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1138
1139         IList* pChangedGroups = _AddressbookUtil::SearchWithVersionN<__ContactsGroupUpdatedInfo, CategoryChangeInfo>(__addressbookId, version, latestVersion);
1140         SysTryReturn(NID_SCL, pChangedGroups != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1141
1142         return pChangedGroups;
1143 }
1144
1145 IList*
1146 _AddressbookImpl::GetChangedRelationsAfterN(int version, int& latestVersion) const
1147 {
1148         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);
1149         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1150
1151         ClearLastResult();
1152
1153         IList* pChangedRelations = _AddressbookUtil::SearchWithVersionN<__ContactsGroupRelUpdatedInfo, CategoryChangeInfo>(__addressbookId, version, latestVersion);
1154         SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1155
1156         return pChangedRelations;
1157 }
1158
1159 result
1160 _AddressbookImpl::AddContacts(const Tizen::Base::Collection::IList& contactList, Tizen::Base::Collection::IListT<RecordId>* pContactIdList)
1161 {
1162         SysTryReturn(NID_SCL, contactList.GetCount() != 0, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. contactList is empty.", GetErrorMessage(E_INVALID_ARG));
1163         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1164
1165         std::unique_ptr<IEnumerator> pContactEnum(contactList.GetEnumeratorN());
1166         SysTryReturnResult(NID_SCL, pContactEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1167
1168         Contact* pContact = null;
1169         int ret = CONTACTS_ERROR_NONE;
1170         int* pContactIds = null;
1171         unsigned int contactsCount = 0;
1172
1173         contacts_record_h recordHandle = null;
1174         contacts_list_h listHandle = null;
1175
1176         ret = contacts_list_create(&listHandle);
1177         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1178
1179         while (pContactEnum->MoveNext() == E_SUCCESS)
1180         {
1181                 pContact = static_cast<Contact*>(pContactEnum->GetCurrent());
1182
1183                 if (_ContactImpl::GetInstance(*pContact)->IsEmpty())
1184                 {
1185                         contacts_list_destroy(listHandle, false);
1186                         SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. Contact is empty.", GetErrorMessage(E_INVALID_ARG));
1187                         return E_INVALID_ARG;
1188                 }
1189                 if (pContact->GetRecordId() != INVALID_RECORD_ID)
1190                 {
1191                         contacts_list_destroy(listHandle, false);
1192                         SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. Contact ID is invalid.", GetErrorMessage(E_INVALID_ARG));
1193                         return E_INVALID_ARG;
1194                 }
1195
1196                 recordHandle = _ContactImpl::GetInstance(*pContact)->GetContactRecordHandle();
1197                 contacts_record_set_int(recordHandle, _contacts_contact.address_book_id, __addressbookId);
1198                 contacts_list_add(listHandle, recordHandle);
1199         }
1200
1201         ret = contacts_db_insert_records(listHandle, &pContactIds, &contactsCount);
1202         contacts_list_destroy(listHandle, false);
1203         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));
1204         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1205         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1206         SysTryReturn(NID_SCL, pContactIds, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1207
1208         int* pTemp = pContactIds;
1209
1210         if (pContactIdList != null)
1211         {
1212                 for (unsigned int i = 0; i < contactsCount; i++)
1213                 {
1214                         pContactIdList->Add(*pTemp);
1215                         pTemp++;
1216                 }
1217         }
1218
1219         free(pContactIds);
1220
1221         return E_SUCCESS;
1222 }
1223
1224 result
1225 _AddressbookImpl::UpdateContacts(const Tizen::Base::Collection::IList& contactList)
1226 {
1227         SysTryReturn(NID_SCL, contactList.GetCount() != 0, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. contactList is empty.", GetErrorMessage(E_INVALID_ARG));
1228         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1229
1230         std::unique_ptr<IEnumerator> pContactEnum(contactList.GetEnumeratorN());
1231         SysTryReturnResult(NID_SCL, pContactEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1232
1233         Contact* pContact = null;
1234         int ret = CONTACTS_ERROR_NONE;
1235         contacts_record_h recordHandle = null;
1236         contacts_list_h listHandle = null;
1237
1238         ret = contacts_list_create(&listHandle);
1239         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1240
1241         while (pContactEnum->MoveNext() == E_SUCCESS)
1242         {
1243                 pContact = static_cast<Contact*>(pContactEnum->GetCurrent());
1244
1245                 if (_ContactImpl::GetInstance(*pContact)->IsEmpty())
1246                 {
1247                         contacts_list_destroy(listHandle, false);
1248                         SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. Contact is empty.", GetErrorMessage(E_INVALID_ARG));
1249                         return E_INVALID_ARG;
1250                 }
1251                 if (pContact->GetRecordId() == INVALID_RECORD_ID)
1252                 {
1253                         contacts_list_destroy(listHandle, false);
1254                         SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. Contact ID is invalid.", GetErrorMessage(E_INVALID_ARG));
1255                         return E_INVALID_ARG;
1256                 }
1257
1258                 recordHandle = _ContactImpl::GetInstance(*pContact)->GetContactRecordHandle();
1259                 contacts_list_add(listHandle, recordHandle);
1260         }
1261
1262         ret = contacts_db_update_records(listHandle);
1263         contacts_list_destroy(listHandle, false);
1264         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));
1265         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1266         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));
1267         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1268
1269         return E_SUCCESS;
1270 }
1271
1272 result
1273 _AddressbookImpl::RemoveContacts(const Tizen::Base::Collection::IListT<RecordId>& contactIdList)
1274 {
1275         SysTryReturn(NID_SCL, contactIdList.GetCount() != 0, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. contactIdList is empty.", GetErrorMessage(E_INVALID_ARG));
1276         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1277
1278         std::unique_ptr<IEnumeratorT<RecordId> > pContactIdEnum(contactIdList.GetEnumeratorN());
1279         SysTryReturnResult(NID_SCL, pContactIdEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1280
1281         RecordId contactId = INVALID_RECORD_ID;
1282         int ret = CONTACTS_ERROR_NONE;
1283         int count = contactIdList.GetCount();
1284         std::unique_ptr<int[]> pRecordIds(new (std::nothrow) int[count]);
1285         int* pInt = pRecordIds.get();
1286         int* pTmp = pInt;
1287
1288         while (pContactIdEnum->MoveNext() == E_SUCCESS)
1289         {
1290                 pContactIdEnum->GetCurrent(contactId);
1291                 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));
1292
1293                 *pTmp = contactId;
1294                 pTmp++;
1295         }
1296
1297         ret = contacts_db_delete_records(_contacts_contact._uri, pInt, count);
1298         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));
1299         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));
1300
1301         return E_SUCCESS;
1302 }
1303
1304 result
1305 _AddressbookImpl::SetUserProfile(const UserProfile* pUserProfile)
1306 {
1307         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1308
1309         int recordId = 0;
1310         int existingRecordId = 0;
1311         int ret = CONTACTS_ERROR_NONE;
1312         result r = E_SUCCESS;
1313         contacts_record_h recordHandle = null;
1314
1315         std::unique_ptr<UserProfile> pExistingProfile(GetUserProfileN());
1316         SysTryReturn(NID_SCL, pExistingProfile != null || GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1317
1318         if (pExistingProfile != null)
1319         {
1320                 recordHandle = _UserProfileImpl::GetInstance(*pExistingProfile)->GetUserProfileHandle();
1321                 contacts_record_get_int(recordHandle, _contacts_my_profile.id, &existingRecordId);
1322         }
1323
1324         if (pUserProfile != null)
1325         {
1326                 SysTryReturn(NID_SCL, !((_UserProfileImpl::GetInstance(*pUserProfile))->IsEmpty()), E_SYSTEM, E_SYSTEM, "[%s] UserProfile is Empty.", GetErrorMessage(E_SYSTEM));
1327
1328                 recordHandle = _UserProfileImpl::GetInstance(*pUserProfile)->GetUserProfileHandle();
1329                 contacts_record_get_int(recordHandle, _contacts_my_profile.id, &recordId);
1330                 if (recordId == 0)
1331                 {
1332                         if (pExistingProfile != null)
1333                         {
1334                                 ret = contacts_db_delete_record(_contacts_my_profile._uri, existingRecordId);
1335                                 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));
1336                                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1337                         }
1338
1339                         contacts_record_h newRecordHandle = null;
1340                         ret = contacts_record_clone(recordHandle, &newRecordHandle);
1341                         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));
1342
1343                         ret = contacts_record_set_int(newRecordHandle, _contacts_my_profile.address_book_id, __addressbookId);
1344                         if (ret != CONTACTS_ERROR_NONE)
1345                         {
1346                                 contacts_record_destroy(newRecordHandle, true);
1347                                 SysLogException(NID_SCL, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1348                                 return E_SYSTEM;
1349                         }
1350
1351                         ret = contacts_db_insert_record(newRecordHandle, &recordId);
1352                         contacts_record_destroy(newRecordHandle, true);
1353                         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1354                         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1355                 }
1356                 else if (recordId > 0)
1357                 {
1358                         if (recordId == existingRecordId)
1359                         {
1360                                 contacts_record_h newRecordHandle = null;
1361                                 ret = contacts_record_clone(recordHandle, &newRecordHandle);
1362                                 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));
1363
1364                                 ret = contacts_db_update_record(newRecordHandle);
1365                                 contacts_record_destroy(newRecordHandle, true);
1366                                 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));
1367                                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1368                         }
1369                         else
1370                         {
1371                                 if (pExistingProfile != null)
1372                                 {
1373                                         ret = contacts_db_delete_record(_contacts_my_profile._uri, existingRecordId);
1374                                         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));
1375                                         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1376                                 }
1377
1378                                 contacts_record_h newRecordHandle = null;
1379                                 ret = contacts_record_create(_contacts_my_profile._uri, &newRecordHandle);
1380                                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1381
1382                                 r = CopyMyProfileContents(recordHandle, newRecordHandle);
1383                                 if (r != E_SUCCESS)
1384                                 {
1385                                         contacts_record_destroy(newRecordHandle, true);
1386                                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1387                                         return E_OUT_OF_MEMORY;
1388
1389                                 }
1390
1391                                 ret = contacts_record_set_int(newRecordHandle, _contacts_my_profile.address_book_id, __addressbookId);
1392                                 if (ret != CONTACTS_ERROR_NONE)
1393                                 {
1394                                         contacts_record_destroy(newRecordHandle, true);
1395                                         SysLogException(NID_SCL, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1396                                         return E_SYSTEM;
1397                                 }
1398
1399                                 ret = contacts_db_insert_record(newRecordHandle, &recordId);
1400                                 contacts_record_destroy(newRecordHandle, true);
1401                                 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));
1402                                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1403                         }
1404                 }
1405         }
1406         else
1407         {
1408                 if (pExistingProfile != null)
1409                 {
1410                         ret = contacts_db_delete_record(_contacts_my_profile._uri, existingRecordId);
1411                         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));
1412                         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1413                 }
1414         }
1415
1416         return E_SUCCESS;
1417 }
1418
1419 UserProfile*
1420 _AddressbookImpl::GetUserProfileN(void) const
1421 {
1422         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1423
1424         ClearLastResult();
1425
1426         __Filter<__ContactsUserProfile> filter;
1427         filter.Construct();
1428         filter.AddInt(_contacts_my_profile.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
1429
1430         __Query<__ContactsUserProfile> query;
1431         query.Construct();
1432         query.SetFilter(filter);
1433
1434         std::unique_ptr<IList, AllElementsDeleter> pUserProfilesList(_AddressbookUtil::SearchWithQueryN<__ContactsUserProfile, UserProfile>(query));
1435         SysTryReturn(NID_SCL, pUserProfilesList.get() != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1436         SysTryReturn(NID_SCL, pUserProfilesList->GetCount() != 0, null, E_SUCCESS, "No UserProfile Set for this Addressbook.");
1437         SysTryReturn(NID_SCL, pUserProfilesList->GetCount() == 1, null, E_SYSTEM, "[%s] Propagating. More than one UserProfile not allowed.", GetErrorMessage(E_SYSTEM));
1438
1439         UserProfile* pProfile = new (std::nothrow) UserProfile(*(static_cast<UserProfile*>(pUserProfilesList->GetAt(0))));
1440         SysTryReturn(NID_SCL, pProfile != null, null, E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(E_OUT_OF_MEMORY));
1441
1442         return pProfile;
1443 }
1444
1445 bool
1446 _AddressbookImpl::IsUserProfileChangedAfter(int version) const
1447 {
1448         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);
1449         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1450
1451         UserProfile* pExistingProfile = GetUserProfileN();
1452         if (pExistingProfile == null)
1453         {
1454                 return false;
1455         }
1456
1457         delete pExistingProfile;
1458
1459         ClearLastResult();
1460
1461         int latestVersion = 0;
1462         int count = _AddressbookUtil::GetChangedItemCountByVersion<__ContactsMyProfileUpdatedInfo>(__addressbookId, version, latestVersion);
1463         SysTryReturn(NID_SCL, count >= 0, false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1464
1465         if (count > 0)
1466         {
1467                 return true;
1468         }
1469
1470         return false;
1471 }
1472
1473 result
1474 _AddressbookImpl::CopyMyProfileContents(contacts_record_h srcHandle, contacts_record_h dstHandle)
1475 {
1476         int ret = CONTACTS_ERROR_NONE;
1477         int intValue = 0;
1478         unsigned int count = 0;
1479         unsigned int i = 0;
1480         char* pCharValue = null;
1481
1482         contacts_record_h srcRecordHandle = null;
1483         contacts_record_h dstRecordHandle = null;
1484
1485         // name
1486         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.name, &count);
1487         if (count > 0)
1488         {
1489                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.name, 0, &srcRecordHandle);
1490
1491                 ret = contacts_record_create(_contacts_name._uri, &dstRecordHandle);
1492                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1493
1494                 __ContactsRecordHandle nameHandle(dstRecordHandle);
1495
1496                 contacts_record_get_str(srcRecordHandle, _contacts_name.first, &pCharValue);
1497                 contacts_record_set_str(dstRecordHandle, _contacts_name.first, pCharValue);
1498
1499                 contacts_record_get_str(srcRecordHandle, _contacts_name.last, &pCharValue);
1500                 contacts_record_set_str(dstRecordHandle, _contacts_name.last, pCharValue);
1501
1502                 contacts_record_get_str(srcRecordHandle, _contacts_name.addition, &pCharValue);
1503                 contacts_record_set_str(dstRecordHandle, _contacts_name.addition, pCharValue);
1504
1505                 contacts_record_get_str(srcRecordHandle, _contacts_name.suffix, &pCharValue);
1506                 contacts_record_set_str(dstRecordHandle, _contacts_name.suffix, pCharValue);
1507
1508                 contacts_record_get_str(srcRecordHandle, _contacts_name.prefix, &pCharValue);
1509                 contacts_record_set_str(dstRecordHandle, _contacts_name.prefix, pCharValue);
1510
1511                 contacts_record_get_str(srcRecordHandle, _contacts_name.phonetic_first, &pCharValue);
1512                 contacts_record_set_str(dstRecordHandle, _contacts_name.phonetic_first, pCharValue);
1513
1514                 contacts_record_get_str(srcRecordHandle, _contacts_name.phonetic_middle, &pCharValue);
1515                 contacts_record_set_str(dstRecordHandle, _contacts_name.phonetic_middle, pCharValue);
1516
1517                 contacts_record_get_str(srcRecordHandle, _contacts_name.phonetic_last, &pCharValue);
1518                 contacts_record_set_str(dstRecordHandle, _contacts_name.phonetic_last, pCharValue);
1519
1520                 contacts_record_add_child_record(dstHandle, _contacts_my_profile.name, dstRecordHandle);
1521
1522                 nameHandle.Release();
1523         }
1524
1525         // company
1526         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.company, &count);
1527         for (i = 0; i < count; i++)
1528         {
1529                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.company, i, &srcRecordHandle);
1530
1531                 ret = contacts_record_create(_contacts_company._uri, &dstRecordHandle);
1532                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1533
1534                 __ContactsRecordHandle companyHandle(dstRecordHandle);
1535
1536                 contacts_record_get_int(srcRecordHandle, _contacts_company.type, &intValue);
1537                 contacts_record_set_int(dstRecordHandle, _contacts_company.type, intValue);
1538
1539                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.name, &pCharValue);
1540                 contacts_record_set_str(dstRecordHandle, _contacts_company.name, pCharValue);
1541
1542                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.department, &pCharValue);
1543                 contacts_record_set_str(dstRecordHandle, _contacts_company.department, pCharValue);
1544
1545                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.job_title, &pCharValue);
1546                 contacts_record_set_str(dstRecordHandle, _contacts_company.job_title, pCharValue);
1547
1548                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.assistant_name, &pCharValue);
1549                 contacts_record_set_str(dstRecordHandle, _contacts_company.assistant_name, pCharValue);
1550
1551                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.role, &pCharValue);
1552                 contacts_record_set_str(dstRecordHandle, _contacts_company.role, pCharValue);
1553
1554                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.logo, &pCharValue);
1555                 contacts_record_set_str(dstRecordHandle, _contacts_company.logo, pCharValue);
1556
1557                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.location, &pCharValue);
1558                 contacts_record_set_str(dstRecordHandle, _contacts_company.location, pCharValue);
1559
1560                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.description, &pCharValue);
1561                 contacts_record_set_str(dstRecordHandle, _contacts_company.description, pCharValue);
1562
1563                 contacts_record_get_str_p(srcRecordHandle, _contacts_company.phonetic_name, &pCharValue);
1564                 contacts_record_set_str(dstRecordHandle, _contacts_company.phonetic_name, pCharValue);
1565
1566                 contacts_record_add_child_record(dstHandle, _contacts_my_profile.company, dstRecordHandle);
1567
1568                 companyHandle.Release();
1569         }
1570
1571         // note
1572         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.note, &count);
1573         for (i = 0; i < count; i++)
1574         {
1575                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.note, i, &srcRecordHandle);
1576
1577                 ret = contacts_record_create(_contacts_note._uri, &dstRecordHandle);
1578                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1579
1580                 __ContactsRecordHandle noteHandle(dstRecordHandle);
1581
1582                 contacts_record_get_str_p(srcRecordHandle, _contacts_note.note, &pCharValue);
1583                 contacts_record_set_str(dstRecordHandle, _contacts_note.note, pCharValue);
1584
1585                 contacts_record_add_child_record(dstHandle, _contacts_my_profile.note, dstRecordHandle);
1586
1587                 noteHandle.Release();
1588         }
1589
1590         // number
1591         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.number, &count);
1592         for (i = 0; i < count; i++)
1593         {
1594                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.number, i, &srcRecordHandle);
1595
1596                 ret = contacts_record_create(_contacts_number._uri, &dstRecordHandle);
1597                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1598
1599                 __ContactsRecordHandle numberHandle(dstRecordHandle);
1600
1601                 contacts_record_get_int(srcRecordHandle, _contacts_number.type, &intValue);
1602                 contacts_record_set_int(dstRecordHandle, _contacts_number.type, intValue);
1603
1604                 contacts_record_get_str_p(srcRecordHandle, _contacts_number.label, &pCharValue);
1605                 contacts_record_set_str(dstRecordHandle, _contacts_number.label, pCharValue);
1606
1607                 contacts_record_get_str_p(srcRecordHandle, _contacts_number.number, &pCharValue);
1608                 contacts_record_set_str(dstRecordHandle, _contacts_number.number, pCharValue);
1609
1610                 contacts_record_add_child_record(dstHandle, _contacts_my_profile.number, dstRecordHandle);
1611
1612                 numberHandle.Release();
1613         }
1614
1615         // email
1616         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.email, &count);
1617         for (i = 0; i < count; i++)
1618         {
1619                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.email, i, &srcRecordHandle);
1620
1621                 ret = contacts_record_create(_contacts_email._uri, &dstRecordHandle);
1622                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1623
1624                 __ContactsRecordHandle emailHandle(dstRecordHandle);
1625
1626                 contacts_record_get_int(srcRecordHandle, _contacts_email.type, &intValue);
1627                 contacts_record_set_int(dstRecordHandle, _contacts_email.type, intValue);
1628
1629                 contacts_record_get_str_p(srcRecordHandle, _contacts_email.label, &pCharValue);
1630                 contacts_record_set_str(dstRecordHandle, _contacts_email.label, pCharValue);
1631
1632                 contacts_record_get_str_p(srcRecordHandle, _contacts_email.email, &pCharValue);
1633                 contacts_record_set_str(dstRecordHandle, _contacts_email.email, pCharValue);
1634
1635                 contacts_record_add_child_record(dstHandle, _contacts_my_profile.email, dstRecordHandle);
1636
1637                 emailHandle.Release();
1638
1639         }
1640
1641         // event
1642         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.event, &count);
1643         for (i = 0; i < count; i++)
1644         {
1645                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.event, i, &srcRecordHandle);
1646
1647                 ret = contacts_record_create(_contacts_event._uri, &dstRecordHandle);
1648                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1649
1650                 __ContactsRecordHandle eventHandle(dstRecordHandle);
1651
1652                 contacts_record_get_int(srcRecordHandle, _contacts_event.type, &intValue);
1653                 contacts_record_set_int(dstRecordHandle, _contacts_event.type, intValue);
1654
1655                 contacts_record_get_str_p(srcRecordHandle, _contacts_event.label, &pCharValue);
1656                 contacts_record_set_str(dstRecordHandle, _contacts_event.label, pCharValue);
1657
1658                 contacts_record_get_int(srcRecordHandle, _contacts_event.date, &intValue);
1659                 contacts_record_set_int(dstRecordHandle, _contacts_event.date, intValue);
1660
1661                 contacts_record_add_child_record(dstHandle, _contacts_my_profile.event, dstRecordHandle);
1662
1663                 eventHandle.Release();
1664         }
1665
1666         // im address
1667         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.messenger, &count);
1668         for (i = 0; i < count; i++)
1669         {
1670                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.messenger, i, &srcRecordHandle);
1671
1672                 ret = contacts_record_create(_contacts_messenger._uri, &dstRecordHandle);
1673                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1674
1675                 __ContactsRecordHandle imAddressHandle(dstRecordHandle);
1676
1677                 contacts_record_get_int(srcRecordHandle, _contacts_messenger.type, &intValue);
1678                 contacts_record_set_int(dstRecordHandle, _contacts_messenger.type, intValue);
1679
1680                 contacts_record_get_str_p(srcRecordHandle, _contacts_messenger.label, &pCharValue);
1681                 contacts_record_set_str(dstRecordHandle, _contacts_messenger.label, pCharValue);
1682
1683                 contacts_record_get_str_p(srcRecordHandle, _contacts_messenger.im_id, &pCharValue);
1684                 contacts_record_set_str(dstRecordHandle, _contacts_messenger.im_id, pCharValue);
1685
1686                 contacts_record_add_child_record(dstHandle, _contacts_my_profile.messenger, dstRecordHandle);
1687
1688                 imAddressHandle.Release();
1689         }
1690
1691         // address
1692         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.address, &count);
1693         for (i = 0; i < count; i++)
1694         {
1695                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.address, i, &srcRecordHandle);
1696
1697                 ret = contacts_record_create(_contacts_address._uri, &dstRecordHandle);
1698                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1699
1700                 __ContactsRecordHandle addressHandle(dstRecordHandle);
1701
1702                 contacts_record_get_int(srcRecordHandle, _contacts_address.type, &intValue);
1703                 contacts_record_set_int(dstRecordHandle, _contacts_address.type, intValue);
1704
1705                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.label, &pCharValue);
1706                 contacts_record_set_str(dstRecordHandle, _contacts_address.label, pCharValue);
1707
1708                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.postbox, &pCharValue);
1709                 contacts_record_set_str(dstRecordHandle, _contacts_address.postbox, pCharValue);
1710
1711                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.extended, &pCharValue);
1712                 contacts_record_set_str(dstRecordHandle, _contacts_address.extended, pCharValue);
1713
1714                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.street, &pCharValue);
1715                 contacts_record_set_str(dstRecordHandle, _contacts_address.street, pCharValue);
1716
1717                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.locality, &pCharValue);
1718                 contacts_record_set_str(dstRecordHandle, _contacts_address.locality, pCharValue);
1719
1720                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.region, &pCharValue);
1721                 contacts_record_set_str(dstRecordHandle, _contacts_address.region, pCharValue);
1722
1723                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.postal_code, &pCharValue);
1724                 contacts_record_set_str(dstRecordHandle, _contacts_address.postal_code, pCharValue);
1725
1726                 contacts_record_get_str_p(srcRecordHandle, _contacts_address.country, &pCharValue);
1727                 contacts_record_set_str(dstRecordHandle, _contacts_address.country, pCharValue);
1728
1729                 contacts_record_add_child_record(dstHandle, _contacts_my_profile.address, dstRecordHandle);
1730
1731                 addressHandle.Release();
1732         }
1733
1734         // url
1735         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.url, &count);
1736         for (i = 0; i < count; i++)
1737         {
1738                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.url, i, &srcRecordHandle);
1739
1740                 ret = contacts_record_create(_contacts_url._uri, &dstRecordHandle);
1741                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1742
1743                 __ContactsRecordHandle urlHandle(dstRecordHandle);
1744
1745                 contacts_record_get_int(srcRecordHandle, _contacts_url.type, &intValue);
1746                 contacts_record_set_int(dstRecordHandle, _contacts_url.type, intValue);
1747
1748                 contacts_record_get_str_p(srcRecordHandle, _contacts_url.label, &pCharValue);
1749                 contacts_record_set_str(dstRecordHandle, _contacts_url.label, pCharValue);
1750
1751                 contacts_record_get_str_p(srcRecordHandle, _contacts_url.url, &pCharValue);
1752                 contacts_record_set_str(dstRecordHandle, _contacts_url.url, pCharValue);
1753
1754                 contacts_record_add_child_record(dstHandle, _contacts_my_profile.url, dstRecordHandle);
1755
1756                 urlHandle.Release();
1757         }
1758
1759         // nickname
1760         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.nickname, &count);
1761         for (i = 0; i < count; i++)
1762         {
1763                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.nickname, i, &srcRecordHandle);
1764
1765                 ret = contacts_record_create(_contacts_nickname._uri, &dstRecordHandle);
1766                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1767
1768                 __ContactsRecordHandle nicknameHandle(dstRecordHandle);
1769
1770                 contacts_record_get_str_p(srcRecordHandle, _contacts_nickname.name, &pCharValue);
1771                 contacts_record_set_str(dstRecordHandle, _contacts_nickname.name, pCharValue);
1772
1773                 contacts_record_add_child_record(dstHandle, _contacts_my_profile.nickname, dstRecordHandle);
1774
1775                 nicknameHandle.Release();
1776         }
1777
1778         // relationship
1779         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.relationship, &count);
1780         for (i = 0; i < count; i++)
1781         {
1782                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.relationship, i, &srcRecordHandle);
1783
1784                 ret = contacts_record_create(_contacts_relationship._uri, &dstRecordHandle);
1785                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1786
1787                 __ContactsRecordHandle relationshipHandle(dstRecordHandle);
1788
1789                 contacts_record_get_str_p(srcRecordHandle, _contacts_relationship.name, &pCharValue);
1790                 contacts_record_set_str(dstRecordHandle, _contacts_relationship.name, pCharValue);
1791
1792                 contacts_record_add_child_record(dstHandle, _contacts_my_profile.relationship, dstRecordHandle);
1793
1794                 relationshipHandle.Release();
1795         }
1796
1797         // image
1798         contacts_record_get_child_record_count(srcHandle, _contacts_my_profile.image, &count);
1799
1800         int recordId = 0;
1801         contacts_record_get_int(srcHandle, _contacts_my_profile.id, &recordId);
1802
1803         if (count > 0 && recordId == 0)
1804         {
1805                 contacts_record_get_child_record_at_p(srcHandle, _contacts_my_profile.image, 0, &srcRecordHandle);
1806
1807                 ret = contacts_record_create(_contacts_image._uri, &dstRecordHandle);
1808                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1809
1810                 __ContactsRecordHandle imageHandle(dstRecordHandle);
1811
1812                 contacts_record_get_int(srcRecordHandle, _contacts_image.type, &intValue);
1813                 contacts_record_set_int(dstRecordHandle, _contacts_image.type, intValue);
1814
1815                 contacts_record_get_str_p(srcRecordHandle, _contacts_image.label, &pCharValue);
1816                 contacts_record_set_str(dstRecordHandle, _contacts_image.label, pCharValue);
1817
1818                 contacts_record_get_str_p(srcRecordHandle, _contacts_image.path, &pCharValue);
1819                 contacts_record_set_str(dstRecordHandle, _contacts_image.path, pCharValue);
1820
1821                 contacts_record_add_child_record(dstHandle, _contacts_contact.image, dstRecordHandle);
1822
1823                 imageHandle.Release();
1824         }
1825
1826         return E_SUCCESS;
1827 }
1828
1829 void
1830 _AddressbookImpl::OnContactChanged(void)
1831 {
1832         if (__pIRecordEventListener == null && __pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
1833         {
1834                 return;
1835         }
1836
1837         std::unique_ptr<IList, AllElementsDeleter> pChangedContactList(GetChangedContactsAfterN(__dbVersionForContact, __dbVersionForContact));
1838         SysTryReturnVoidResult(NID_SCL, pChangedContactList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1839
1840         if (pChangedContactList->GetCount() > 0)
1841         {
1842                 if (__pIAddressbookEventListener != null)
1843                 {
1844                         __pIAddressbookEventListener->OnContactsChanged(*pChangedContactList);
1845                 }
1846                 else if (__pIAddressbookChangeEventListener != null)
1847                 {
1848                         __pIAddressbookChangeEventListener->OnContactsChanged(*pChangedContactList);
1849                 }
1850                 else
1851                 {
1852                         RecordEventType recordEventType = RECORD_ADDED;
1853                         std::unique_ptr<IEnumerator> pEnum(pChangedContactList->GetEnumeratorN());
1854                         std::unique_ptr<Contact> pContact(null);
1855
1856                         while (pEnum->MoveNext() == E_SUCCESS)
1857                         {
1858                                 ContactChangeInfo* pContactChagneInfo = static_cast<ContactChangeInfo*>(pEnum->GetCurrent());
1859
1860                                 if (pContactChagneInfo->GetChangeType() == RECORD_CHANGE_TYPE_ADDED)
1861                                 {
1862                                         recordEventType = RECORD_ADDED;
1863                                         pContact.reset(GetContactN(pContactChagneInfo->GetContactId()));
1864                                         if (pContact == null)
1865                                         {
1866                                                 if (GetLastResult() == E_OBJ_NOT_FOUND)
1867                                                 {
1868                                                         continue;
1869                                                 }
1870                                                 else
1871                                                 {
1872                                                         break;
1873                                                 }
1874                                         }
1875                                 }
1876                                 else if (pContactChagneInfo->GetChangeType() == RECORD_CHANGE_TYPE_UPDATED)
1877                                 {
1878                                         recordEventType = RECORD_UPDATED;
1879                                         pContact.reset(GetContactN(pContactChagneInfo->GetContactId()));
1880                                         if (pContact == null)
1881                                         {
1882                                                 if (GetLastResult() == E_OBJ_NOT_FOUND)
1883                                                 {
1884                                                         continue;
1885                                                 }
1886                                                 else
1887                                                 {
1888                                                         break;
1889                                                 }
1890                                         }
1891                                 }
1892                                 else
1893                                 {
1894                                         recordEventType = RECORD_REMOVED;
1895                                         pContact.reset(new (std::nothrow) Contact());
1896                                         if (pContact == null)
1897                                         {
1898                                                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1899                                                 break;
1900                                         }
1901
1902                                         _RecordImpl::GetInstance(*pContact)->SetRecordId(pContactChagneInfo->GetContactId());
1903                                 }
1904
1905                                 __pIRecordEventListener->OnRecordChangedN(recordEventType, RECORD_TYPE_CONTACT, *pContact, null, null);
1906                         }
1907                 }
1908
1909         }
1910 }
1911
1912 void
1913 _AddressbookImpl::OnCategoryChanged(void)
1914 {
1915         if (__pIRecordEventListener == null && __pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
1916         {
1917                 return;
1918         }
1919
1920         std::unique_ptr<IList, AllElementsDeleter> pChangedCategoryList(GetChangedGroupsAfterN(__dbVersionForGroup, __dbVersionForGroup));
1921         SysTryReturnVoidResult(NID_SCL, pChangedCategoryList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1922
1923         if (pChangedCategoryList->GetCount() > 0)
1924         {
1925                 if (__pIAddressbookEventListener != null)
1926                 {
1927                         __pIAddressbookEventListener->OnCategoriesChanged(*pChangedCategoryList);
1928                 }
1929                 else if (__pIAddressbookChangeEventListener != null)
1930                 {
1931                         __pIAddressbookChangeEventListener->OnCategoriesChanged(*pChangedCategoryList);
1932                 }
1933                 else
1934                 {
1935                         RecordEventType recordEventType = RECORD_ADDED;
1936                         std::unique_ptr<IEnumerator> pEnum(pChangedCategoryList->GetEnumeratorN());
1937                         std::unique_ptr<Category> pCategory(null);
1938
1939                         while (pEnum->MoveNext() == E_SUCCESS)
1940                         {
1941                                 CategoryChangeInfo* pCategoryChagneInfo = static_cast<CategoryChangeInfo*>(pEnum->GetCurrent());
1942
1943                                 if (pCategoryChagneInfo->GetChangeType() == RECORD_CHANGE_TYPE_ADDED)
1944                                 {
1945                                         recordEventType = RECORD_ADDED;
1946                                         pCategory.reset(GetCategoryN(pCategoryChagneInfo->GetCategoryId()));
1947                                         if (pCategory == null)
1948                                         {
1949                                                 if (GetLastResult() == E_OBJ_NOT_FOUND)
1950                                                 {
1951                                                         continue;
1952                                                 }
1953                                                 else
1954                                                 {
1955                                                         break;
1956                                                 }
1957                                         }
1958                                 }
1959                                 else if (pCategoryChagneInfo->GetChangeType() == RECORD_CHANGE_TYPE_UPDATED)
1960                                 {
1961                                         recordEventType = RECORD_UPDATED;
1962                                         pCategory.reset(GetCategoryN(pCategoryChagneInfo->GetCategoryId()));
1963                                         if (pCategory == null)
1964                                         {
1965                                                 if (GetLastResult() == E_OBJ_NOT_FOUND)
1966                                                 {
1967                                                         continue;
1968                                                 }
1969                                                 else
1970                                                 {
1971                                                         break;
1972                                                 }
1973                                         }
1974                                 }
1975                                 else
1976                                 {
1977                                         recordEventType = RECORD_REMOVED;
1978                                         pCategory.reset(new (std::nothrow) Category());
1979                                         if (pCategory == null)
1980                                         {
1981                                                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1982                                                 break;
1983                                         }
1984
1985                                         _RecordImpl::GetInstance(*pCategory)->SetRecordId(pCategoryChagneInfo->GetCategoryId());
1986                                 }
1987
1988                                 __pIRecordEventListener->OnRecordChangedN(recordEventType, RECORD_TYPE_CATEGORY, *pCategory, null, null);
1989                         }
1990                 }
1991
1992         }
1993 }
1994
1995 void
1996 _AddressbookImpl::OnRelationChanged(void)
1997 {
1998         if (__pIAddressbookEventListener == null && __pIRecordEventListener == null)
1999         {
2000                 return;
2001         }
2002
2003         std::unique_ptr<IList, AllElementsDeleter> pChangedCategoryList(GetChangedRelationsAfterN(__dbVersionForRelation, __dbVersionForRelation));
2004         SysTryReturnVoidResult(NID_SCL, pChangedCategoryList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2005
2006         if (pChangedCategoryList->GetCount() > 0)
2007         {
2008                 if (__pIAddressbookEventListener != null)
2009                 {
2010                         __pIAddressbookEventListener->OnCategoriesChanged(*pChangedCategoryList);
2011                 }
2012                 else
2013                 {
2014                         RecordEventType recordEventType = RECORD_ADDED;
2015                         std::unique_ptr<IEnumerator> pEnum(pChangedCategoryList->GetEnumeratorN());
2016                         std::unique_ptr<Category> pCategory(null);
2017
2018                         while (pEnum->MoveNext() == E_SUCCESS)
2019                         {
2020                                 CategoryChangeInfo* pCategoryChagneInfo = static_cast<CategoryChangeInfo*>(pEnum->GetCurrent());
2021
2022                                 recordEventType = RECORD_UPDATED;
2023                                 pCategory.reset(GetCategoryN(pCategoryChagneInfo->GetCategoryId()));
2024                                 if (pCategory == null)
2025                                 {
2026                                         if (GetLastResult() == E_OBJ_NOT_FOUND)
2027                                         {
2028                                                 continue;
2029                                         }
2030                                         else
2031                                         {
2032                                                 break;
2033                                         }
2034                                 }
2035
2036                                 __pIRecordEventListener->OnRecordChangedN(recordEventType, RECORD_TYPE_CATEGORY, *pCategory, null, null);
2037                         }
2038                 }
2039         }
2040 }
2041
2042 _AddressbookImpl*
2043 _AddressbookImpl::GetInstance(Addressbook& addressbook)
2044 {
2045         return addressbook.__pAddressbookImpl;
2046 }
2047
2048 const _AddressbookImpl*
2049 _AddressbookImpl::GetInstance(const Addressbook& addressbook)
2050 {
2051         return addressbook.__pAddressbookImpl;
2052 }
2053
2054 }}  // Tizen::Social