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