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