Fix the boiler plate codes
[framework/osp/social.git] / src / FScl_AddressbookManagerImpl.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_AddressbookManagerImpl.cpp
18  * @brief               This is the implementation for _AddressbookManagerImpl class.
19  *
20  * This file contains definitions of @e _AddressbookManagerImpl class.
21  */
22 #include <contacts.h>
23 #include <unique_ptr.h>
24 #include <FBaseColIListT.h>
25 #include <FBaseColArrayListT.h>
26 #include <FBaseColHashMapT.h>
27 #include <FBaseResult.h>
28 #include <FBaseLongLong.h>
29 #include <FBaseInteger.h>
30 #include <FIoFile.h>
31 #include <FSclAddressbook.h>
32 #include <FSclAddressbookManager.h>
33 #include <FSclIAddressbookChangeEventListener.h>
34 #include <FSclContact.h>
35 #include <FSclCategory.h>
36 #include <FSclUserProfile.h>
37 #include <FSclContactChangeInfo.h>
38 #include <FSclCategoryChangeInfo.h>
39 #include <FSclPerson.h>
40 #include <FSclPhoneNumberContact.h>
41 #include <FScl_PhoneNumberContactImpl.h>
42 #include <FSclEmailContact.h>
43 #include <FScl_EmailContactImpl.h>
44 #include <FSclAddressbookFilter.h>
45 #include <FSclIAddressbookEventListener.h>
46 #include <FSclIRecordEventListener.h>
47 #include <FBaseSysLog.h>
48 #include <FApp_AppInfo.h>
49 #include <FBase_StringConverter.h>
50 #include "FScl_AddressbookImpl.h"
51 #include "FScl_AddressbookFilterImpl.h"
52 #include "FScl_AddressbookManagerImpl.h"
53 #include "FScl_AddressbookUtil.h"
54 #include "FScl_RecordImpl.h"
55 #include "FScl_ContactImpl.h"
56 #include "FScl_CategoryImpl.h"
57 #include "FScl_PersonImpl.h"
58 #include "FScl_AddressbookUtil.h"
59 #include "FScl_ContactChangeInfoImpl.h"
60 #include "FScl_CategoryChangeInfoImpl.h"
61 #include "FScl_ContactDbMonitor.h"
62 #include "FScl_ContactDbConnector.h"
63 #include "FScl_UserProfileImpl.h"
64
65 using namespace Tizen::App;
66 using namespace Tizen::Base;
67 using namespace Tizen::Base::Collection;
68 using namespace Tizen::Io;
69 using namespace Tizen::Graphics;
70
71 namespace Tizen { namespace Social
72 {
73
74 _AddressbookManagerImpl::_AddressbookManagerImpl(void)
75         : __pIAddressbookEventListener(null)
76         , __pIAddressbookChangeEventListener(null)
77         , __dbVersionForContact(0)
78         , __dbVersionForGroup(0)
79         , __dbVersionForRelation(0)
80 {
81         // empty body.
82 }
83
84 _AddressbookManagerImpl::~_AddressbookManagerImpl(void)
85 {
86         if (__pIAddressbookEventListener != null || __pIAddressbookChangeEventListener != null)
87         {
88                 _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
89                 if (pContactDbMonitor != null)
90                 {
91                         pContactDbMonitor->RemoveListener(*this);
92                 }
93         }
94 }
95
96 result
97 _AddressbookManagerImpl::Construct(void)
98 {
99         result r = _ContactDbConnector::EnsureDbConnection();
100         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
101
102         return E_SUCCESS;
103 }
104
105 result
106 _AddressbookManagerImpl::SetEventListener(IAddressbookEventListener* pListener)
107 {
108         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
109
110         result r = E_SUCCESS;
111
112         if (pListener != null)
113         {
114                 if (__pIAddressbookEventListener == null && __pIAddressbookChangeEventListener == null)
115                 {
116                         _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
117                         SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
118
119                         r = pContactDbMonitor->AddListener(*this);
120                         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
121                 }
122
123                 __dbVersionForContact = GetLatestVersion();
124                 SysTryReturn(NID_SCL, __dbVersionForContact != -1, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
125
126                 __dbVersionForGroup = __dbVersionForContact;
127                 __dbVersionForRelation = __dbVersionForContact;
128
129                 __pIAddressbookChangeEventListener = null;
130                 __pIAddressbookEventListener = pListener;
131         }
132         else
133         {
134                 if (__pIAddressbookEventListener != null || __pIAddressbookChangeEventListener != null)
135                 {
136
137                         _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
138                         SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
139
140                         pContactDbMonitor->RemoveListener(*this);
141                 }
142
143                 __pIAddressbookEventListener = null;
144                 __pIAddressbookChangeEventListener = null;
145         }
146
147         return E_SUCCESS;
148 }
149
150 result
151 _AddressbookManagerImpl::SetAddressbookChangeEventListener(IAddressbookChangeEventListener* pListener)
152 {
153         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
154
155         result r = E_SUCCESS;
156
157         if (pListener != null)
158         {
159                 if (__pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
160                 {
161                         _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
162                         SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
163
164                         r = pContactDbMonitor->AddListener(*this);
165                         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
166                 }
167
168                 __dbVersionForContact = GetLatestVersion();
169                 SysTryReturn(NID_SCL, __dbVersionForContact != -1, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
170
171                 __dbVersionForGroup = __dbVersionForContact;
172                 __dbVersionForRelation = __dbVersionForContact;
173
174                 __pIAddressbookEventListener = null;
175                 __pIAddressbookChangeEventListener = pListener;
176         }
177         else
178         {
179                 if (__pIAddressbookChangeEventListener != null || __pIAddressbookEventListener != null)
180                 {
181
182                         _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
183                         SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
184
185                         pContactDbMonitor->RemoveListener(*this);
186                 }
187
188                 __pIAddressbookChangeEventListener = null;
189                 __pIAddressbookEventListener = null;
190         }
191
192         return E_SUCCESS;
193 }
194
195 Addressbook*
196 _AddressbookManagerImpl::CreateAddressbookN(AccountId accountId, const String& name)
197 {
198         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
199         SysTryReturn(NID_SCL, accountId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. account id is invalid.", GetErrorMessage(E_INVALID_ARG));
200         SysTryReturn(NID_SCL, !name.IsEmpty(), null, E_INVALID_ARG, "[%s] Invalid argument is used. The name is empty.", GetErrorMessage(E_INVALID_ARG));
201
202         ClearLastResult();
203
204         result r = E_SUCCESS;
205         int ret = CONTACTS_ERROR_NONE;
206         int recordId = 0;
207         contacts_record_h addressbookHandle = null;
208
209         std::unique_ptr<char[]> pNameString(_StringConverter::CopyToCharArrayN(name));
210         SysTryReturn(NID_SCL, pNameString !=null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
211
212         __Filter<__ContactsAddressbook> filter;
213         filter.Construct();
214         filter.AddString(_contacts_address_book.name, CONTACTS_MATCH_EXACTLY, pNameString.get());
215         if (accountId != 0)
216         {
217                 filter.AddOperator(CONTACTS_FILTER_OPERATOR_OR);
218                 filter.AddInt(_contacts_address_book.account_id, CONTACTS_MATCH_EQUAL, accountId);
219         }
220
221         __Query<__ContactsAddressbook> query;
222         query.Construct();
223         query.SetFilter(filter);
224
225         int count = _AddressbookUtil::GetCountWithQuery(query);
226         SysTryReturn(NID_SCL, count == 0, null, E_OBJ_ALREADY_EXIST, "[%s] The name is already being used by other addressbook.", GetErrorMessage(E_OBJ_ALREADY_EXIST));
227
228         std::unique_ptr<Addressbook> pAddressbook(new (std::nothrow) Addressbook());
229         SysTryReturn(NID_SCL, pAddressbook != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
230
231         r = pAddressbook->Construct();
232         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
233
234         ret = contacts_record_create(_contacts_address_book._uri, &addressbookHandle);
235         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
236         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
237
238         __ContactsRecordHandle recordHandle(addressbookHandle);
239
240         contacts_record_set_str(addressbookHandle, _contacts_address_book.name, pNameString.get());
241         contacts_record_set_int(addressbookHandle, _contacts_address_book.account_id, accountId);
242
243         ret = contacts_db_insert_record(addressbookHandle, &recordId);
244         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
245         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
246         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
247
248         _AddressbookImpl::GetInstance(*pAddressbook)->SetAccountId(accountId);
249         _AddressbookImpl::GetInstance(*pAddressbook)->SetName(name);
250         _AddressbookImpl::GetInstance(*pAddressbook)->SetId(recordId);
251
252         return pAddressbook.release();
253 }
254
255 result
256 _AddressbookManagerImpl::DeleteAddressbook(AddressbookId addressbookId)
257 {
258         SysTryReturn(NID_SCL, addressbookId > 0, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The addressbook ID is invalid or the default addressbook ID.", GetErrorMessage(E_INVALID_ARG));
259         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
260
261         int ret = contacts_db_delete_record(_contacts_address_book._uri, addressbookId);
262         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));
263         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The addressbook is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
264 //      SysTryReturnResult(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OPERATION_FAILED, "Failed to delete an addressbook.(%d)", ret); // temp
265
266         return E_SUCCESS;
267 }
268
269 IList*
270 _AddressbookManagerImpl::GetAddressbooksByAccountN(AccountId accountId) const
271 {
272         SysTryReturn(NID_SCL, accountId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. accountId = %d.", GetErrorMessage(E_INVALID_ARG), accountId);
273         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
274
275         ClearLastResult();
276
277         __Filter<__ContactsAddressbook> filter;
278         filter.Construct();
279         filter.AddInt(_contacts_address_book.account_id, CONTACTS_MATCH_EQUAL, accountId);
280
281         __Query<__ContactsAddressbook> query;
282         query.Construct();
283         query.SetFilter(filter);
284
285         IList* pAddressbooks = _AddressbookUtil::SearchWithQueryN<__ContactsAddressbook, Addressbook>(query);
286         SysTryReturn(NID_SCL, pAddressbooks != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
287
288         return pAddressbooks;
289 }
290
291 IList*
292 _AddressbookManagerImpl::GetAllAddressbooksN(void) const
293 {
294         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
295
296         ClearLastResult();
297
298         __Query<__ContactsAddressbook> query;
299         query.Construct();
300         query.SetSort(_contacts_address_book.name, true);
301
302         IList* pAddressbooks = _AddressbookUtil::SearchWithQueryN<__ContactsAddressbook, Addressbook>(query);
303         SysTryReturn(NID_SCL, pAddressbooks != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
304
305         return pAddressbooks;
306 }
307
308 Addressbook*
309 _AddressbookManagerImpl::GetAddressbookN(AddressbookId addressbookId) const
310 {
311         SysTryReturn(NID_SCL, addressbookId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. addressbookId.", GetErrorMessage(E_INVALID_ARG), addressbookId);
312         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
313
314         ClearLastResult();
315
316         result r = E_SUCCESS;
317         int ret = CONTACTS_ERROR_NONE;
318         contacts_record_h addressbookHandle = null;
319         int intValue = 0;
320         char* pCharValue = null;
321
322         std::unique_ptr<Addressbook> pAddressbook(new (std::nothrow) Addressbook());
323         SysTryReturn(NID_SCL, pAddressbook !=null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
324
325         r = pAddressbook->Construct();
326         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
327
328         ret = contacts_db_get_record(_contacts_address_book._uri, addressbookId, &addressbookHandle);
329         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
330         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The addressbook %d is not found.", GetErrorMessage(E_OBJ_NOT_FOUND), addressbookId);
331         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred. Addressbook Id(%d)", GetErrorMessage(E_SYSTEM), addressbookId);
332
333         _AddressbookImpl::GetInstance(*pAddressbook)->SetId(addressbookId);
334
335         contacts_record_get_int(addressbookHandle, _contacts_address_book.account_id, &intValue);
336         _AddressbookImpl::GetInstance(*pAddressbook)->SetAccountId(intValue);
337
338         contacts_record_get_str_p(addressbookHandle, _contacts_address_book.name, &pCharValue);
339         _AddressbookImpl::GetInstance(*pAddressbook)->SetName(pCharValue);
340
341         contacts_record_destroy(addressbookHandle, true);
342
343         return pAddressbook.release();
344 }
345
346 result
347 _AddressbookManagerImpl::AddContact(Contact& contact, AddressbookId addressbookId)
348 {
349         if (_ContactImpl::GetInstance(contact)->IsRemoved())
350         {
351                 result r = _ContactImpl::GetInstance(contact)->Invalidate();
352                 SysTryReturn(NID_SCL, r == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
353         }
354
355         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));
356         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));
357         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
358
359         int recordId = 0;
360         contacts_record_h recordHandle = null;
361
362         int ret = contacts_db_get_record(_contacts_address_book._uri, addressbookId, &recordHandle);
363         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
364         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The addressbook is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
365         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
366
367         contacts_record_destroy(recordHandle, true);
368
369         recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
370
371         contacts_record_set_int(recordHandle, _contacts_contact.address_book_id, addressbookId);
372
373         ret = contacts_db_insert_record(recordHandle, &recordId);
374         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));
375         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is insufficient.", GetErrorMessage(E_STORAGE_FULL));
376         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
377
378         ret = contacts_db_get_record(_contacts_contact._uri, recordId, &recordHandle);
379         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));
380         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));
381         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A sytem error has been occurred.", GetErrorMessage(E_SYSTEM));
382
383         _ContactImpl::GetInstance(contact)->SetContactRecordHandle(recordHandle);
384         _RecordImpl::GetInstance(contact)->SetRecordId(recordId);
385
386         return E_SUCCESS;
387
388 }
389
390 result
391 _AddressbookManagerImpl::AddCategory(Category& category, AddressbookId addressbookId)
392 {
393         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));
394         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
395
396         int recordId = 0;
397         contacts_record_h recordHandle = null;
398         std::unique_ptr<IListT<int> > pList(null);
399
400         recordHandle = _CategoryImpl::GetInstance(category)->GetRecordHandle();
401
402         contacts_record_set_int(recordHandle, _contacts_group.address_book_id, addressbookId);
403
404         int ret = contacts_db_insert_record(recordHandle, &recordId);
405         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));
406         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is insufficient.", GetErrorMessage(E_STORAGE_FULL));
407         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
408
409         ret = contacts_db_get_record(_contacts_group._uri, recordId, &recordHandle);
410         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));
411         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));
412         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A sytem error has been occurred.", GetErrorMessage(E_SYSTEM));
413
414         _CategoryImpl::GetInstance(category)->SetRecordHandle(recordHandle);
415         _RecordImpl::GetInstance(category)->SetRecordId(recordId);
416
417         pList.reset(_CategoryImpl::GetInstance(category)->GetAddedMembersN());
418         if (pList != null && pList->GetCount() > 0)
419         {
420                 std::unique_ptr<IEnumeratorT<int> > pEnum(pList->GetEnumeratorN());
421
422                 while (pEnum->MoveNext() == E_SUCCESS)
423                 {
424                         int tableId = -1;
425                         pEnum->GetCurrent(tableId);
426
427                         AddMemberToCategory(category.GetRecordId(), tableId);
428                 }
429
430                 _CategoryImpl::GetInstance(category)->ClearAddedMemberList();
431         }
432
433         return E_SUCCESS;
434
435 }
436
437 result
438 _AddressbookManagerImpl::RemoveContact(RecordId contactId)
439 {
440         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));
441         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
442
443         contacts_record_h recordHandle = null;
444
445         int intValue = 0;
446         int ret = contacts_db_get_record(_contacts_simple_contact._uri, contactId, &recordHandle);
447         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));
448         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));
449         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
450
451         contacts_record_get_int(recordHandle, _contacts_simple_contact.id, &intValue);
452
453         contacts_record_destroy(recordHandle, true);
454         SysTryReturn(NID_SCL, intValue == contactId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
455
456         ret = contacts_db_delete_record(_contacts_contact._uri, contactId);
457         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));
458         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));
459         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
460
461         return E_SUCCESS;
462 }
463
464 result
465 _AddressbookManagerImpl::RemoveCategory(RecordId categoryId)
466 {
467         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);
468         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
469          
470         int intValue = 0;
471         int ret = CONTACTS_ERROR_NONE;
472         contacts_record_h recordHandle = null;
473
474         ret = contacts_db_get_record(_contacts_group._uri, categoryId, &recordHandle);
475         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));
476         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));
477         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
478
479         __ContactsRecordHandle categoryHandle(recordHandle);
480
481         contacts_record_get_int(recordHandle, _contacts_group.id, &intValue);
482         SysTryReturn(NID_SCL, intValue == categoryId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
483
484         bool isReadOnly = false;
485         contacts_record_get_bool(recordHandle, _contacts_group.is_read_only, &isReadOnly);
486         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));
487
488         ret = contacts_db_delete_record(_contacts_group._uri, categoryId);
489         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));
490         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));
491 //      SysTryReturnResult(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OPERATION_FAILED, "Failed to delete a category.(%d)", ret); // temp
492
493         return E_SUCCESS;
494 }
495
496 result
497 _AddressbookManagerImpl::UpdateContact(const Contact& contact)
498 {
499         RecordId contactId = contact.GetRecordId();
500         SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The specified contactId is not INVALID_RECORD_ID.", GetErrorMessage(E_INVALID_ARG));
501         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));
502         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
503
504         contacts_record_h recordHandle = null;
505         
506         int intValue = 0;
507         int ret = contacts_db_get_record(_contacts_simple_contact._uri, contactId, &recordHandle);
508         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));
509         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));
510         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
511
512         contacts_record_get_int(recordHandle, _contacts_simple_contact.id, &intValue);
513
514         contacts_record_destroy(recordHandle, true);
515         SysTryReturn(NID_SCL, intValue == contactId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
516
517         recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
518
519         ret = contacts_db_update_record(recordHandle);
520         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));
521         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is insufficient.", GetErrorMessage(E_STORAGE_FULL));
522         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred. Failed to update a contact.", GetErrorMessage(E_SYSTEM));
523
524         ret = contacts_db_get_record(_contacts_contact._uri, contact.GetRecordId(), &recordHandle);
525         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));
526         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));
527         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
528
529         _ContactImpl::GetInstance(*const_cast<Contact*>(&contact))->SetContactRecordHandle(recordHandle);
530
531         return E_SUCCESS;
532 }
533
534 result
535 _AddressbookManagerImpl::UpdateCategory(const Category& category)
536 {
537         RecordId categoryId = category.GetRecordId();
538         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));
539         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));
540         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
541
542         contacts_record_h recordHandle = null;
543         int intValue = 0;
544
545         int ret = contacts_db_get_record(_contacts_group._uri, categoryId, &recordHandle);
546         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));
547         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The specified category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
548
549         contacts_record_get_int(recordHandle, _contacts_group.id, &intValue);
550
551         contacts_record_destroy(recordHandle, true);
552         SysTryReturn(NID_SCL, intValue == categoryId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
553
554         recordHandle = _CategoryImpl::GetInstance(category)->GetRecordHandle();
555
556         ret = contacts_db_update_record(recordHandle);
557         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));
558         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is insufficient.", GetErrorMessage(E_STORAGE_FULL));
559         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
560
561         ret = contacts_db_get_record(_contacts_group._uri, category.GetRecordId(), &recordHandle);
562         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The category is not found.");
563         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));
564         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
565
566         _CategoryImpl::GetInstance(*const_cast<Category*>(&category))->SetRecordHandle(recordHandle);
567
568         std::unique_ptr<IListT<int> > pList(_CategoryImpl::GetInstance(category)->GetAddedMembersN());
569         if (pList != null && pList->GetCount() > 0)
570         {
571                 int tableId = -1;
572                 std::unique_ptr<IEnumeratorT<int> > pEnum(pList->GetEnumeratorN());
573                 while (pEnum->MoveNext() == E_SUCCESS)
574                 {
575                         pEnum->GetCurrent(tableId);
576
577                         AddMemberToCategory(category.GetRecordId(), tableId);
578                 }
579
580                 const_cast<_CategoryImpl*>(_CategoryImpl::GetInstance(category))->ClearAddedMemberList();
581         }
582
583         pList.reset(_CategoryImpl::GetInstance(category)->GetRemovedMembersN());
584         if (pList != null && pList->GetCount() > 0)
585         {
586                 int tableId = -1;
587                 std::unique_ptr<IEnumeratorT<int> > pEnum(pList->GetEnumeratorN());
588                 while (pEnum->MoveNext() == E_SUCCESS)
589                 {
590                         pEnum->GetCurrent(tableId);
591
592                         RemoveMemberFromCategory(category.GetRecordId(), tableId);
593                 }
594
595                 const_cast<_CategoryImpl*>(_CategoryImpl::GetInstance(category))->ClearRemovedMemberList();
596         }
597
598         return E_SUCCESS;
599
600 }
601
602 result
603 _AddressbookManagerImpl::AddMemberToCategory(RecordId categoryId, RecordId contactId)
604 {
605
606         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);
607         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);
608         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
609
610         int ret = contacts_group_add_contact(categoryId, contactId);
611         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
612         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));
613         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
614
615         return E_SUCCESS;
616 }
617
618 result
619 _AddressbookManagerImpl::RemoveMemberFromCategory(RecordId categoryId, RecordId contactId)
620 {
621
622         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);
623         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);
624         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
625
626         int ret = contacts_group_remove_contact(categoryId, contactId);
627         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used.", 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, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
630
631         return E_SUCCESS;
632 }
633
634 IList*
635 _AddressbookManagerImpl::GetAllCategoriesN(void) const
636 {
637         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
638
639         ClearLastResult();
640
641         __Query<__ContactsGroup> query;
642         query.Construct();
643         query.SetSort(_contacts_group.name, true);
644
645         IList* pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsGroup, Category>(query);
646         SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
647
648         return pCategories;
649 }
650
651 IList*
652 _AddressbookManagerImpl::GetCategoriesByContactN(RecordId contactId) const
653 {
654         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
655         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));
656
657         ClearLastResult();
658
659         __Filter<__ContactsGroupRelation> filter;
660         filter.Construct();
661         filter.AddInt(_contacts_group_relation.contact_id, CONTACTS_MATCH_EQUAL, contactId);
662
663         __Query<__ContactsGroupRelation> query;
664         query.Construct();
665         query.SetFilter(filter);
666
667         IList* pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsGroupRelation, Category>(query);
668         SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
669
670         return pCategories;
671 }
672
673 IList*
674 _AddressbookManagerImpl::GetCategoriesByPersonN(PersonId personId) const
675 {
676         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
677
678         ClearLastResult();
679
680         __Filter<__ContactsContactGroupRel> filter;
681         filter.Construct();
682         filter.AddInt(_contacts_contact_grouprel.person_id, CONTACTS_MATCH_EQUAL, personId);
683         filter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
684         filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_GREATER_THAN, 0);
685
686         unsigned int propertyIds[] =
687         {
688                 _contacts_contact_grouprel.group_id,
689         };
690
691         __Query<__ContactsContactGroupRel> query;
692         query.Construct();
693         query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
694         query.SetFilter(filter);
695         query.SetSort(_contacts_contact_grouprel.group_name, true);
696         query.SetDistinct(true);
697
698         IList* pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Category>(query);
699         SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
700
701         return pCategories;
702 }
703
704 IList*
705 _AddressbookManagerImpl::GetAllContactsN(void) const
706 {
707         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
708
709         ClearLastResult();
710
711         __Query<__ContactsContact> query;
712         query.Construct();
713         query.SetSort(_contacts_contact.display_name, true);
714
715         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
716         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
717
718         return pContacts;
719 }
720
721 IList*
722 _AddressbookManagerImpl::GetContactsByCategoryN(RecordId categoryId) const
723 {
724         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));
725         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
726
727         ClearLastResult();
728
729         __Filter<__ContactsContactGroupRel> filter;
730         filter.Construct();
731         
732         if (categoryId != INVALID_RECORD_ID)
733         {
734                 filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_EQUAL, categoryId);
735         }
736         else
737         {
738                 filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_NONE, 0);
739         }
740
741         __Query<__ContactsContactGroupRel> query;
742         query.Construct();
743         query.SetFilter(filter);
744
745
746         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Contact>(query);
747         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
748
749         return pContacts;
750 }
751
752 IList*
753 _AddressbookManagerImpl::GetContactsByPersonN(PersonId personId) const
754 {
755         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
756
757         ClearLastResult();
758
759         __Filter<__ContactsContact> filter;
760         filter.Construct();
761         filter.AddInt(_contacts_contact.person_id, CONTACTS_MATCH_EQUAL, personId);
762
763         __Query<__ContactsContact> query;
764         query.Construct();
765         query.SetFilter(filter);
766         query.SetSort(_contacts_contact.display_name, true);
767
768         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
769         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
770
771         return pContacts;
772 }
773
774 IList*
775 _AddressbookManagerImpl::SearchContactsByEmailN(const String& email) const
776 {
777         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));
778         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
779
780         ClearLastResult();
781
782         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(email));
783         SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
784
785         __Filter<__ContactsContactEmail> filter;
786         filter.Construct();
787         filter.AddString(_contacts_contact_email.email, CONTACTS_MATCH_CONTAINS, pCharArray.get());
788
789         __Query<__ContactsContactEmail> query;
790         query.Construct();
791         query.SetFilter(filter);
792         query.SetSort(_contacts_contact_email.display_name, true);
793
794         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactEmail, Contact>(query);
795         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
796
797         return pContacts;
798
799 }
800
801 IList*
802 _AddressbookManagerImpl::SearchContactsByNameN(const String& name) const
803 {
804         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
805
806         ClearLastResult();
807
808         SysTryReturn(NID_SCL, !name.IsEmpty(), null, E_INVALID_ARG, "[%s] Invalid argument is used. The specified email is an name string.", GetErrorMessage(E_INVALID_ARG));
809
810         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(name));
811         SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
812
813         __Filter<__ContactsContact> filter;
814         filter.Construct();
815         filter.AddString(_contacts_contact.display_name, CONTACTS_MATCH_CONTAINS, pCharArray.get());
816
817         __Query<__ContactsContact> query;
818         query.Construct();
819         query.SetFilter(filter);
820         query.SetSort(_contacts_contact.display_name, true);
821
822         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
823         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
824
825         return pContacts;
826 }
827
828 IList*
829 _AddressbookManagerImpl::SearchContactsByPhoneNumberN(const String& phoneNumber) const
830 {
831         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
832
833         ClearLastResult();
834
835         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));
836
837         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(phoneNumber));
838         SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
839
840         __Filter<__ContactsContactNumber> filter;
841         filter.Construct();
842         filter.AddString(_contacts_contact_number.number, CONTACTS_MATCH_CONTAINS, pCharArray.get());
843
844         __Query<__ContactsContactNumber> query;
845         query.Construct();
846         query.SetFilter(filter);
847         query.SetDistinct(true);
848         query.SetSort(_contacts_contact_number.display_name, true);
849
850         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactNumber, Contact>(query);
851         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
852
853         return pContacts;
854 }
855
856 int
857 _AddressbookManagerImpl::GetCategoryCount(void) const
858 {
859         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
860
861         int count = -1;
862
863         ClearLastResult();
864
865         count = _AddressbookUtil::GetCount<__ContactsGroup>();
866         SysTryReturn(NID_SCL, count >= 0, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
867
868         return count;
869 }
870
871 int
872 _AddressbookManagerImpl::GetContactCount(void) const
873 {
874         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
875
876         int count = -1;
877
878         ClearLastResult();
879
880         count = _AddressbookUtil::GetCount<__ContactsContact>();
881         SysTryReturn(NID_SCL, count >= 0, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
882
883         return count;
884 }
885
886 Contact*
887 _AddressbookManagerImpl::GetContactN(RecordId contactId) const
888 {
889         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
890         SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. contactId = %d.", GetErrorMessage(E_INVALID_ARG), contactId);
891
892         ClearLastResult();
893
894         int intValue = 0;
895         contacts_record_h contactHandle = null;
896
897         std::unique_ptr<Contact> pContact(new (std::nothrow) Contact());
898         SysTryReturn(NID_SCL, pContact, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
899
900         int ret = contacts_db_get_record(_contacts_contact._uri, contactId, &contactHandle);
901         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
902         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
903         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
904
905         _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(contactHandle);
906
907         contacts_record_get_int(contactHandle, _contacts_contact.id, &intValue);
908         SysTryReturn(NID_SCL, intValue == contactId, null, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
909
910         _RecordImpl::GetInstance(*pContact)->SetRecordId(intValue);
911
912         return pContact.release();
913 }
914
915 Person*
916 _AddressbookManagerImpl::GetPersonN(PersonId personId) const
917 {
918         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
919         SysTryReturn(NID_SCL, personId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. personId = %d.", GetErrorMessage(E_INVALID_ARG), personId);
920
921         contacts_record_h recordHandle = null;
922
923         ClearLastResult();
924
925         int intValue = 0;
926         int ret = contacts_db_get_record(_contacts_person._uri, personId, &recordHandle);
927         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
928         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The person is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
929         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
930
931         __ContactsRecordHandle personRecord(recordHandle);
932
933         contacts_record_get_int(recordHandle, _contacts_person.id, &intValue);
934         SysTryReturn(NID_SCL, intValue == personId, null, E_OBJ_NOT_FOUND, "[%s] The person is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
935
936         Person* pPerson = __ContactsPerson::ConvertHandleTo<Person>(recordHandle);
937         SysTryReturn(NID_SCL, pPerson != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
938
939         return pPerson;
940 }
941
942 Category*
943 _AddressbookManagerImpl::GetCategoryN(RecordId categoryId) const
944 {
945         SysTryReturn(NID_SCL, categoryId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. categoryId = %d.", categoryId);
946         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
947
948         contacts_record_h recordHandle = null;
949
950         ClearLastResult();
951
952         int ret = contacts_db_get_record(_contacts_group._uri, categoryId, &recordHandle);
953         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
954         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
955         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
956
957         int intValue = 0;
958         contacts_record_get_int(recordHandle, _contacts_group.id, &intValue);
959         
960         std::unique_ptr<Category> pCategory(new (std::nothrow) Category());
961         SysTryReturn(NID_SCL, pCategory != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
962
963         SysTryReturn(NID_SCL, categoryId == intValue, null, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
964
965         __Filter<__ContactsGroupRelation> filter;
966         filter.Construct();
967         filter.AddInt(_contacts_group_relation.group_id, CONTACTS_MATCH_EQUAL, categoryId);
968
969         __Query<__ContactsGroupRelation> query;
970         query.Construct();
971         query.SetFilter(filter);
972
973         int count = _AddressbookUtil::GetCountWithQuery(query);
974         SysTryReturn(NID_SCL, count >= 0, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
975
976         _CategoryImpl::GetInstance(*pCategory)->SetRecordHandle(recordHandle);
977         _CategoryImpl::GetInstance(*pCategory)->SetMemberCount(count);
978         _RecordImpl::GetInstance(*pCategory)->SetRecordId(categoryId);
979
980         return pCategory.release();
981 }
982
983 int
984 _AddressbookManagerImpl::GetLatestVersion(void) const
985 {
986         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
987
988         int latestVersion = -1;
989
990         ClearLastResult();
991
992         int ret = contacts_db_get_current_version(&latestVersion);
993         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
994
995         return latestVersion;
996 }
997
998 IList*
999 _AddressbookManagerImpl::GetChangedContactsAfterN(int version, int& latestVersion) const
1000 {
1001         SysTryReturn(NID_SCL, version >= 0, null, E_INVALID_ARG, "[%s] Invalid arguent is used. version %d must be greater that or equal 0.", GetErrorMessage(E_INVALID_ARG), version);
1002         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1003
1004         ClearLastResult();
1005
1006         IList* pChangedContacts = _AddressbookUtil::SearchWithVersionN<__ContactsContactUpdatedInfo, ContactChangeInfo>(-1, version, latestVersion);
1007         SysTryReturn(NID_SCL, pChangedContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1008
1009         return pChangedContacts;
1010 }
1011
1012 IList*
1013 _AddressbookManagerImpl::GetChangedCategoriesAfterN(int version, int& latestVersion) const
1014 {
1015         SysTryReturn(NID_SCL, version >= 0, null, E_INVALID_ARG, "[%s] Invalid arguemnt is used. version %d must be greater that or equal 0.", GetErrorMessage(E_INVALID_ARG), version);
1016         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1017
1018         ClearLastResult();
1019
1020         int latestVersion1 = 0;
1021         int latestVersion2 = 0;
1022
1023         std::unique_ptr<IList, AllElementsDeleter> pChangedGroups(_AddressbookUtil::SearchWithVersionN<__ContactsGroupUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion1));
1024         SysTryReturn(NID_SCL, pChangedGroups != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1025
1026         std::unique_ptr<IList, AllElementsDeleter> pChangedRelations(_AddressbookUtil::SearchWithVersionN<__ContactsGroupRelUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion2));
1027         SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1028
1029         std::unique_ptr<ArrayList, AllElementsDeleter> pChangeList(new (std::nothrow) Tizen::Base::Collection::ArrayList());
1030         SysTryReturn(NID_SCL, pChangeList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1031
1032         result r = pChangeList->AddItems(*pChangedGroups);
1033         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1034
1035         r = pChangeList->AddItems(*pChangedRelations);
1036         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1037
1038         pChangedGroups->RemoveAll(false);
1039         pChangedRelations->RemoveAll(false);
1040
1041         latestVersion = latestVersion2 > latestVersion1 ? latestVersion2 : latestVersion1;
1042         
1043         return pChangeList.release();
1044
1045 }
1046
1047 IList*
1048 _AddressbookManagerImpl::GetChangedGroupsAfterN(int version, int& latestVersion) const
1049 {
1050         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);
1051         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1052
1053         ClearLastResult();
1054
1055         IList* pChangedRelations = _AddressbookUtil::SearchWithVersionN<__ContactsGroupUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion);
1056         SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1057
1058         return pChangedRelations;
1059 }
1060
1061 IList*
1062 _AddressbookManagerImpl::GetChangedRelationsAfterN(int version, int& latestVersion) const
1063 {
1064         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);
1065         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1066
1067         ClearLastResult();
1068
1069         IList* pChangedRelations = _AddressbookUtil::SearchWithVersionN<__ContactsGroupRelUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion);
1070         SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1071
1072         return pChangedRelations;
1073 }
1074
1075 void
1076 _AddressbookManagerImpl::OnContactChanged(void)
1077 {
1078         if (__pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
1079         {
1080                 return;
1081         }
1082
1083         IList* pChangedContactList = GetChangedContactsAfterN(__dbVersionForContact, __dbVersionForContact);
1084         SysTryReturnVoidResult(NID_SCL, pChangedContactList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1085
1086         if (pChangedContactList->GetCount() > 0)
1087         {
1088                 if (__pIAddressbookChangeEventListener != null)
1089                 {
1090                         __pIAddressbookChangeEventListener->OnContactsChanged(*pChangedContactList);
1091                 }
1092                 else
1093                 {
1094                         __pIAddressbookEventListener->OnContactsChanged(*pChangedContactList);
1095                 }
1096         }
1097
1098         pChangedContactList->RemoveAll(true);
1099         delete pChangedContactList;
1100 }
1101
1102 void
1103 _AddressbookManagerImpl::OnCategoryChanged(void)
1104 {
1105         if (__pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
1106         {
1107                 return;
1108         }
1109
1110         IList* pChangedCategoryList = GetChangedGroupsAfterN(__dbVersionForGroup, __dbVersionForGroup);
1111         SysTryReturnVoidResult(NID_SCL, pChangedCategoryList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1112
1113         if (pChangedCategoryList->GetCount() > 0)
1114         {
1115                 if (__pIAddressbookChangeEventListener != null)
1116                 {
1117                         __pIAddressbookChangeEventListener->OnCategoriesChanged(*pChangedCategoryList);
1118                 }
1119                 else
1120                 {
1121                         __pIAddressbookEventListener->OnCategoriesChanged(*pChangedCategoryList);
1122                 }
1123         }
1124
1125         pChangedCategoryList->RemoveAll(true);
1126         delete pChangedCategoryList;
1127 }
1128
1129 void
1130 _AddressbookManagerImpl::OnRelationChanged(void)
1131 {
1132         if (__pIAddressbookEventListener == null)
1133         {
1134                 return;
1135         }
1136
1137         IList* pChangedCategoryList = GetChangedRelationsAfterN(__dbVersionForRelation, __dbVersionForRelation);
1138         SysTryReturnVoidResult(NID_SCL, pChangedCategoryList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1139
1140         if (pChangedCategoryList->GetCount() > 0)
1141         {
1142                 if (__pIAddressbookEventListener != null)
1143                 {
1144                         __pIAddressbookEventListener->OnCategoriesChanged(*pChangedCategoryList);
1145                 }
1146         }
1147
1148         pChangedCategoryList->RemoveAll(true);
1149         delete pChangedCategoryList;
1150 }
1151
1152 result
1153 _AddressbookManagerImpl::RemovePerson(PersonId personId)
1154 {
1155         SysTryReturnResult(NID_SCL, personId > 0, E_INVALID_ARG, "[%s] Invalid argument is used. personId = %d.", GetErrorMessage(E_INVALID_ARG), personId);
1156         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1157
1158         contacts_record_h recordHandle = null;
1159
1160         int ret = contacts_db_get_record(_contacts_person._uri, personId, &recordHandle);
1161         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The person is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
1162         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));
1163         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred. Failed remove a person.", GetErrorMessage(E_SYSTEM));
1164
1165         contacts_record_destroy(recordHandle, true);
1166
1167         ret = contacts_db_delete_record(_contacts_person._uri, personId);
1168         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The person is not found.");
1169         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));
1170         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred. Failed remove a person.", GetErrorMessage(E_SYSTEM));
1171
1172         return E_SUCCESS;
1173 }
1174
1175 IList*
1176 _AddressbookManagerImpl::GetAllPersonsN(void) const
1177 {
1178         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1179
1180         ClearLastResult();
1181
1182         __Query<__ContactsPerson> query;
1183         query.Construct();
1184         query.SetSort(_contacts_person.display_name, true);
1185
1186         IList* pPersons = _AddressbookUtil::SearchWithQueryN<__ContactsPerson, Person>(query);
1187         SysTryReturn(NID_SCL, pPersons != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1188
1189         return pPersons;
1190 }
1191
1192 IList*
1193 _AddressbookManagerImpl::GetPersonsByCategoryN(RecordId categoryId) const
1194 {
1195         SysTryReturn(NID_SCL, categoryId >= INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. The specified categoryId is less than INVALID_RECORD_ID.", GetErrorMessage(E_INVALID_ARG));
1196         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1197
1198         ClearLastResult();
1199
1200         __Filter<__ContactsPersonGroupRel> filter;
1201         filter.Construct();
1202         if (categoryId != INVALID_RECORD_ID)
1203         {
1204                 filter.AddInt(_contacts_person_grouprel.group_id, CONTACTS_MATCH_EQUAL, categoryId);
1205         }
1206         else
1207         {
1208                 filter.AddInt(_contacts_person_grouprel.group_id, CONTACTS_MATCH_NONE, 0);
1209         }
1210
1211         unsigned int propertyIds[] =
1212         {
1213                 _contacts_person_grouprel.person_id,
1214                 _contacts_person_grouprel.display_name,
1215                 _contacts_person_grouprel.image_thumbnail_path,
1216                 _contacts_person_grouprel.ringtone_path,
1217                 _contacts_person_grouprel.is_favorite,
1218                 _contacts_person_grouprel.has_phonenumber,
1219                 _contacts_person_grouprel.has_email,
1220                 _contacts_person_grouprel.link_count,
1221         };
1222
1223         __Query<__ContactsPersonGroupRel> query;
1224         query.Construct();
1225         query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1226         query.SetFilter(filter);
1227         query.SetSort(_contacts_person_grouprel.display_name, true);
1228         query.SetDistinct(true);
1229
1230         IList* pPersons = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query);
1231         SysTryReturn(NID_SCL, pPersons != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1232
1233         return pPersons;
1234 }
1235
1236 IList*
1237 _AddressbookManagerImpl::GetFavoritePersonsN() const
1238 {
1239         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1240
1241         ClearLastResult();
1242
1243         __Filter<__ContactsPerson> filter;
1244         filter.Construct();
1245         filter.AddBool(_contacts_person.is_favorite, true);
1246
1247         __Query<__ContactsPerson> query;
1248         query.Construct();
1249         query.SetFilter(filter);
1250         query.SetSort(_contacts_person.display_name, true);
1251
1252         IList* pPersons = _AddressbookUtil::SearchWithQueryN<__ContactsPerson, Person>(query);
1253         SysTryReturn(NID_SCL, pPersons != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1254
1255         return pPersons;
1256 }
1257
1258 IList*
1259 _AddressbookManagerImpl::SearchPersonsN(const Tizen::Base::String& keyword) const
1260 {
1261         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1262         SysTryReturn(NID_SCL, !keyword.IsEmpty(), null, E_INVALID_ARG, "Invalid argument is used. keyword is empty string.", GetErrorMessage(E_INVALID_ARG));
1263
1264         ClearLastResult();
1265
1266         contacts_record_h currentRecord = null;
1267         std::unique_ptr<Person> pPerson(null);
1268
1269         std::unique_ptr<ArrayList, AllElementsDeleter> pPersonList(new (std::nothrow) ArrayList());
1270         SysTryReturn(NID_SCL, pPersonList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1271
1272         result r = pPersonList->Construct();
1273         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1274
1275         std::unique_ptr<__SearchResult<__ContactsPerson> > pSearchResult(_AddressbookUtil::Search<__ContactsPerson>(keyword));
1276         SysTryReturn(NID_SCL, pSearchResult != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1277
1278         while (pSearchResult->MoveNext() == E_SUCCESS)
1279         {
1280                 currentRecord = pSearchResult->GetCurrentRecord();
1281                 SysTryReturn(NID_SCL, currentRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1282
1283                 pPerson.reset(__ContactsPerson::ConvertHandleTo<Person>(currentRecord));
1284                 SysTryReturn(NID_SCL, pPerson != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1285
1286                 r = pPersonList->Add(*pPerson);
1287                 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
1288
1289                 pPerson.release();
1290         }
1291
1292         return pPersonList.release();
1293 }
1294
1295 result
1296 _AddressbookManagerImpl::SetPersonAsFavorite(PersonId personId, bool isFavorite)
1297 {
1298         SysTryReturn(NID_SCL, personId > 0, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. personId = %d.", GetErrorMessage(E_INVALID_ARG), personId);
1299         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1300
1301         bool boolValue = false;
1302         contacts_record_h personHandle = null;
1303         
1304         int ret = contacts_db_get_record(_contacts_person._uri, personId, &personHandle);
1305         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));
1306         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The person is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
1307
1308         __ContactsRecordHandle recordHandle(personHandle);
1309
1310         contacts_record_get_bool(personHandle, _contacts_person.is_favorite, &boolValue);
1311
1312         if (boolValue != isFavorite)
1313         {
1314                 contacts_record_set_bool(personHandle, _contacts_person.is_favorite, isFavorite);
1315
1316                 ret = contacts_db_update_record(personHandle);
1317                 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));
1318                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1319         }
1320
1321         return E_SUCCESS;
1322 }
1323
1324 result
1325 _AddressbookManagerImpl::MergePersons(PersonId sourcePersonId, PersonId targetPersonId)
1326 {
1327         SysTryReturn(NID_SCL, sourcePersonId > 0 && targetPersonId > 0, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. sourcePersonId %d and targetPersonId must be greater than 0.", GetErrorMessage(E_INVALID_ARG), sourcePersonId, targetPersonId);
1328         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1329
1330         int ret = contacts_person_link_person(targetPersonId, sourcePersonId);
1331         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1332         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1333
1334         return E_SUCCESS;
1335 }
1336
1337 result
1338 _AddressbookManagerImpl::UnlinkContact(PersonId personId, RecordId contactId, PersonId& newPersonId)
1339 {
1340         SysTryReturn(NID_SCL, personId > 0 && contactId != INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. personId %d must be greater thant 0 and contactId %d must not be INVALID_RECORD_ID.", GetErrorMessage(E_INVALID_ARG), personId, contactId);
1341         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1342
1343         int ret = contacts_person_unlink_contact(personId, contactId, &newPersonId);
1344         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));
1345         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1346
1347         return E_SUCCESS;
1348 }
1349
1350 IList*
1351 _AddressbookManagerImpl::SearchN(const AddressbookFilter& filter, unsigned long propertySortedBy, SortOrder sortOrder, int offset, int maxCount)
1352 {
1353         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1354
1355         ClearLastResult();
1356
1357         IList* pList = null;
1358         bool ascending = false;
1359         unsigned int viewSortPropertyId = 0;
1360
1361         AddressbookFilterType type = _AddressbookFilterImpl::GetInstance(filter)->GetType();
1362         contacts_filter_h filterHandle = _AddressbookFilterImpl::GetInstance(filter)->GetFilterHandle();
1363
1364         SysTryReturn(NID_SCL, propertySortedBy == 0 || _AddressbookFilterImpl::IsValidProperty(type, propertySortedBy), null, E_INVALID_ARG, "[%s] Invalid argument is used. The protertyToSort %d can not be used for property for sorting.", GetErrorMessage(E_INVALID_ARG), propertySortedBy);
1365
1366         if (propertySortedBy != 0 && sortOrder != SORT_ORDER_NONE)
1367         {
1368                 viewSortPropertyId = _AddressbookFilterImpl::GetViewPropertyId(type, propertySortedBy);
1369                 ascending = (sortOrder == SORT_ORDER_ASCENDING) ? true : false;
1370         }
1371
1372         switch(type)
1373         {
1374         case AB_FI_TYPE_ADDRESSBOOK:
1375                 {
1376                         __Filter<__ContactsAddressbook> filter;
1377                         filter.Construct(filterHandle);
1378
1379                         __Query<__ContactsAddressbook> query;
1380                         query.Construct();
1381                         query.SetFilter(filter);
1382
1383                         if (viewSortPropertyId != 0)
1384                         {
1385                                 query.SetSort(viewSortPropertyId, ascending);
1386                         }
1387
1388                         pList = _AddressbookUtil::SearchWithQueryN<__ContactsAddressbook, Addressbook>(query, offset, maxCount);
1389                 }
1390                 break;
1391         case AB_FI_TYPE_PERSON:
1392                 {
1393                         __Filter<__ContactsPersonGroupRel> filter;
1394                         filter.Construct(filterHandle);
1395
1396                         unsigned int propertyIds[] =
1397                         { _contacts_person_grouprel.person_id,
1398                                 _contacts_person_grouprel.addressbook_ids,
1399                                 _contacts_person_grouprel.is_favorite,
1400                                 _contacts_person_grouprel.has_phonenumber,
1401                                 _contacts_person_grouprel.has_email,
1402                                 _contacts_person_grouprel.image_thumbnail_path,
1403                                 _contacts_person_grouprel.ringtone_path,
1404                                 _contacts_person_grouprel.display_name
1405                         };
1406
1407                         __Query<__ContactsPersonGroupRel> query;
1408                         query.Construct();
1409                         query.SetFilter(filter);
1410                         query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1411                         query.SetDistinct(true);
1412
1413                         if (viewSortPropertyId != 0)
1414                         {
1415                                 query.SetSort(viewSortPropertyId, ascending);
1416                         }
1417
1418                         pList = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query, offset, maxCount);
1419                 }
1420                 break;
1421         case AB_FI_TYPE_CONTACT:
1422                 {
1423                         __Filter<__ContactsContact> filter;
1424                         filter.Construct(filterHandle);
1425
1426                         __Query<__ContactsContact> query;
1427                         query.Construct();
1428                         query.SetFilter(filter);
1429
1430                         if (viewSortPropertyId != 0)
1431                         {
1432                                 query.SetSort(viewSortPropertyId, ascending);
1433                         }
1434
1435                         pList = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query, offset, maxCount);
1436                 }
1437                 break;
1438         case AB_FI_TYPE_CATEGORY:
1439                 {
1440                         __Filter<__ContactsGroup> filter;
1441                         filter.Construct(filterHandle);
1442
1443                         __Query<__ContactsGroup> query;
1444                         query.Construct();
1445                         query.SetFilter(filter);
1446
1447                         if (viewSortPropertyId != 0)
1448                         {
1449                                 query.SetSort(viewSortPropertyId, ascending);
1450                         }
1451
1452                         pList = _AddressbookUtil::SearchWithQueryN<__ContactsGroup, Category>(query, offset, maxCount);
1453                 }
1454                 break;
1455         case AB_FI_TYPE_PHONE_CONTACT:
1456                 {
1457                         __Filter<__ContactsContactNumber> filter;
1458                         filter.Construct(filterHandle);
1459
1460                         __Query<__ContactsContactNumber> query;
1461                         query.Construct();
1462                         query.SetFilter(filter);
1463
1464                         if (viewSortPropertyId != 0)
1465                         {
1466                                 query.SetSort(viewSortPropertyId, ascending);
1467                         }
1468
1469                         pList = _AddressbookUtil::SearchWithQueryN<__ContactsContactNumber, PhoneNumberContact>(query, offset, maxCount);
1470                 }
1471                 break;
1472         case AB_FI_TYPE_EMAIL_CONTACT:
1473                 {
1474                         __Filter<__ContactsContactEmail> filter;
1475                         filter.Construct(filterHandle);
1476
1477                         __Query<__ContactsContactEmail> query;
1478                         query.Construct();
1479                         query.SetFilter(filter);
1480
1481                         if (viewSortPropertyId != 0)
1482                         {
1483                                 query.SetSort(viewSortPropertyId, ascending);
1484                         }
1485
1486                         pList = _AddressbookUtil::SearchWithQueryN<__ContactsContactEmail, EmailContact>(query, offset, maxCount);
1487                 }
1488                 break;
1489         default:
1490                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. The filter type is invalid", GetErrorMessage(E_INVALID_ARG));
1491                 pList = null;
1492         };
1493
1494         return pList;
1495 }
1496
1497 int
1498 _AddressbookManagerImpl::GetMatchedItemCount(const AddressbookFilter& filter)
1499 {
1500         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1501
1502         ClearLastResult();
1503
1504         int count = 0;
1505         AddressbookFilterType type = _AddressbookFilterImpl::GetInstance(filter)->GetType();
1506         contacts_filter_h filterHandle = _AddressbookFilterImpl::GetInstance(filter)->GetFilterHandle();
1507
1508         switch(type)
1509         {
1510         case AB_FI_TYPE_ADDRESSBOOK:
1511                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsAddressbook>(filterHandle);
1512                 break;
1513         case AB_FI_TYPE_PERSON:
1514                 {
1515                         __Filter<__ContactsPersonGroupRel> filter;
1516                         filter.Construct(filterHandle);
1517
1518                         unsigned int propertyIds[] = { _contacts_person_grouprel.person_id };
1519
1520                         __Query<__ContactsPersonGroupRel> query;
1521                         query.Construct();
1522                         query.SetFilter(filter);
1523                         query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1524                         query.SetDistinct(true);
1525
1526                         count = _AddressbookUtil::GetCountWithQuery(query);
1527                 }
1528
1529                 break;
1530         case AB_FI_TYPE_CONTACT:
1531                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContact>(filterHandle);
1532                 break;
1533         case AB_FI_TYPE_CATEGORY:
1534                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsGroup>(filterHandle);
1535                 break;
1536         case AB_FI_TYPE_PHONE_CONTACT:
1537                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContactNumber>(filterHandle);
1538                 break;
1539         case AB_FI_TYPE_EMAIL_CONTACT:
1540                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContactEmail>(filterHandle);
1541                 break;
1542         default:
1543                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the filter is invalid", GetErrorMessage(GetLastResult()));
1544                 count = 0;
1545         };
1546
1547         return count;
1548 }
1549
1550 bool
1551 _AddressbookManagerImpl::OnEachContact(contacts_record_h recordHandle, void* pUserData)
1552 {
1553         IList* pList = static_cast<IList*>(pUserData);
1554
1555         ClearLastResult();
1556
1557         std::unique_ptr<Contact> pContact(new (std::nothrow) Contact());
1558         SysTryReturn(NID_SCL, pContact != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1559
1560         contacts_record_h newRecordHandle = null;
1561
1562         contacts_record_clone(recordHandle, &newRecordHandle);
1563         SysTryReturn(NID_SCL, newRecordHandle != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1564
1565         _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(newRecordHandle);
1566
1567         result r = pList->Add(*pContact);
1568         SysTryReturn(NID_SCL, !IsFailed(r), false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1569
1570         pContact.release();
1571
1572         return true;    
1573 }
1574
1575 IList*
1576 _AddressbookManagerImpl::ParseContactsFromVcardN(const Tizen::Base::String& vcardPath)
1577 {
1578         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1579
1580         ClearLastResult();
1581
1582         File file;
1583         result r = file.Construct(vcardPath, "r");
1584         SysTryReturn(NID_SCL, r != E_INVALID_ARG, null, E_INVALID_ARG, "[%s] Invalid argument is used..", GetErrorMessage(E_INVALID_ARG));
1585         SysTryReturn(NID_SCL, r != E_ILLEGAL_ACCESS, null, E_ILLEGAL_ACCESS, "[%s] Access to the vcard file is denied due to insufficient permission.", GetErrorMessage(E_ILLEGAL_ACCESS));
1586         SysTryReturn(NID_SCL, r != E_FILE_NOT_FOUND, null, E_FILE_NOT_FOUND, "[%s] The specified file does not exist.", GetErrorMessage(E_FILE_NOT_FOUND));
1587         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1588
1589         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
1590
1591         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(vcardPath));
1592
1593         int ret = contacts_vcard_parse_to_contact_foreach(pCharArray.get(), OnEachContact, pList.get());
1594         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1595         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1596         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1597
1598         return pList.release();
1599 }
1600
1601 result
1602 _AddressbookManagerImpl::ExportPersonToVcard(const Person& person, const Tizen::Base::String& vcardPath)
1603 {
1604         bool exist = File::IsFileExist(vcardPath);
1605         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
1606         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
1607         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1608
1609         File file;
1610         result r = file.Construct(vcardPath, "w");
1611         SysTryReturn(NID_SCL, r != E_ILLEGAL_ACCESS, E_ILLEGAL_ACCESS, E_ILLEGAL_ACCESS, "[%s] Access to the vcard file is denied due to insufficient permission.", GetErrorMessage(E_ILLEGAL_ACCESS));
1612         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1613         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1614
1615         contacts_record_h personRecordHandle = null;
1616
1617         int ret = contacts_db_get_record(_contacts_person._uri, person.GetId(), &personRecordHandle);
1618         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The specified person does not exist.", GetErrorMessage(E_OBJ_NOT_FOUND));
1619         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));
1620         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1621
1622         __ContactsRecordHandle recordHandle(personRecordHandle);
1623
1624         char* pVcardStream = null;
1625         ret = contacts_vcard_make_from_person(personRecordHandle, &pVcardStream);
1626         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));
1627         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1628
1629         r = file.Write(pVcardStream, strlen(pVcardStream));
1630         free(pVcardStream);
1631         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1632         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1633
1634         return E_SUCCESS;
1635 }
1636
1637 result
1638 _AddressbookManagerImpl::ExportPersonsToVcard(const Tizen::Base::Collection::IList& personList, const Tizen::Base::String& vcardPath)
1639 {
1640         bool exist = File::IsFileExist(vcardPath);
1641         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
1642         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
1643         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1644
1645         int ret = CONTACTS_ERROR_NONE;
1646         char* pVcardStream = null;
1647         File file;
1648
1649         result r = file.Construct(vcardPath, "w");
1650         SysTryReturn(NID_SCL, r != E_ILLEGAL_ACCESS, E_ILLEGAL_ACCESS, E_ILLEGAL_ACCESS, "[%s] Access to the vcard file is denied due to insufficient permission.", GetErrorMessage(E_ILLEGAL_ACCESS));
1651         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1652         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1653
1654         contacts_record_h personRecordHandle = null;
1655         __ContactsRecordHandle recordHandle(null);
1656
1657         std::unique_ptr<IEnumerator> pEnum(personList.GetEnumeratorN());
1658         SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1659
1660         while (pEnum->MoveNext() == E_SUCCESS)
1661         {
1662                 Person* pPerson = static_cast<Person*>(pEnum->GetCurrent());
1663
1664                 ret = contacts_db_get_record(_contacts_person._uri, pPerson->GetId(), &personRecordHandle);
1665                 SysTryReturnResult(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, "The specified person does not exist.");
1666                 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));
1667                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1668
1669                 recordHandle.Reset(personRecordHandle);
1670
1671                 ret = contacts_vcard_make_from_person(personRecordHandle, &pVcardStream);
1672                 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));
1673                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1674
1675                 r = file.Write(pVcardStream, strlen(pVcardStream));
1676                 free(pVcardStream);
1677                 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1678                 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1679         }
1680
1681         return E_SUCCESS;
1682 }
1683
1684 result
1685 _AddressbookManagerImpl::ExportContactToVcard(const Contact& contact, const Tizen::Base::String& vcardPath)
1686 {
1687         bool exist = File::IsFileExist(vcardPath);
1688         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
1689         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
1690         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1691
1692         File file;
1693         result r = file.Construct(vcardPath, "w");
1694         SysTryReturn(NID_SCL, r != E_ILLEGAL_ACCESS, E_ILLEGAL_ACCESS, E_ILLEGAL_ACCESS, "[%s] Access to the vcard file is denied due to insufficient permission.", GetErrorMessage(E_ILLEGAL_ACCESS));
1695         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1696         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1697
1698         contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
1699
1700         char* pVcardStream = null;
1701
1702         int ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
1703         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));
1704         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1705
1706         r = file.Write(pVcardStream, strlen(pVcardStream));
1707         free(pVcardStream);
1708         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1709         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1710
1711         return E_SUCCESS;
1712 }
1713
1714 result
1715 _AddressbookManagerImpl::ExportContactsToVcard(const Tizen::Base::Collection::IList& contactList, const Tizen::Base::String& vcardPath)
1716 {
1717         bool exist = File::IsFileExist(vcardPath);
1718         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
1719         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
1720         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1721
1722         int ret = CONTACTS_ERROR_NONE;
1723         char* pVcardStream = null;
1724         File file;
1725         Contact* pContact = null;
1726
1727         result r = file.Construct(vcardPath, "w");
1728         SysTryReturn(NID_SCL, r != E_ILLEGAL_ACCESS, E_ILLEGAL_ACCESS, E_ILLEGAL_ACCESS, "[%s] Access to the vcard file is denied due to insufficient permission.", GetErrorMessage(E_ILLEGAL_ACCESS));
1729         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1730         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1731
1732         contacts_record_h recordHandle = null;
1733
1734         std::unique_ptr<IEnumerator> pEnum(contactList.GetEnumeratorN());
1735         SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1736
1737         while (pEnum->MoveNext() == E_SUCCESS)
1738         {
1739                 pContact = static_cast<Contact*>(pEnum->GetCurrent());
1740
1741                 recordHandle = _ContactImpl::GetInstance(*pContact)->GetContactRecordHandle();
1742
1743                 ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
1744                 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));
1745                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1746
1747                 r = file.Write(pVcardStream, strlen(pVcardStream));
1748                 free(pVcardStream);
1749                 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1750                 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1751         }
1752
1753         return E_SUCCESS;
1754 }
1755
1756 ByteBuffer*
1757 _AddressbookManagerImpl::ExportContactToVcardStreamN(const Contact& contact)
1758 {
1759         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1760
1761         ClearLastResult();
1762
1763         contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
1764
1765         char* pVcardStream = null;
1766         int ret = CONTACTS_ERROR_NONE;
1767
1768         ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
1769         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1770         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1771         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1772
1773         std::unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
1774         if (pByteBuffer == null)
1775         {
1776                 free(pVcardStream);
1777                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1778
1779                 return null;
1780         }
1781
1782         result r = pByteBuffer->Construct(strlen(pVcardStream));
1783         if (IsFailed(r))
1784         {
1785                 free(pVcardStream);
1786                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1787
1788                 return null;
1789         }
1790
1791         r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
1792         free(pVcardStream);
1793         SysTryReturn(NID_SCL, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1794         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1795
1796         return pByteBuffer.release();
1797 }
1798
1799 ByteBuffer*
1800 _AddressbookManagerImpl::ExportContactsToVcardStreamN(const Tizen::Base::Collection::IList& contactList)
1801 {
1802         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1803
1804         ClearLastResult();
1805
1806         char* pVcardStream = null;
1807         int ret = CONTACTS_ERROR_NONE;
1808         Contact* pContact = null;
1809         result r = E_SUCCESS;
1810         int capacity = 0;
1811
1812         contacts_record_h recordHandle = null;
1813
1814         std::unique_ptr<IEnumerator> pEnum(contactList.GetEnumeratorN());
1815         SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1816
1817         std::unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
1818         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1819
1820         r = pByteBuffer->Construct(capacity);
1821         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1822
1823         if (contactList.GetCount() == 0)
1824         {
1825                 return pByteBuffer.release();
1826         }
1827
1828         while (pEnum->MoveNext() == E_SUCCESS)
1829         {
1830                 pContact = static_cast<Contact*>(pEnum->GetCurrent());
1831
1832                 recordHandle = _ContactImpl::GetInstance(*pContact)->GetContactRecordHandle();
1833
1834                 ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
1835                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1836                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1837                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1838
1839                 capacity += strlen(pVcardStream);
1840                 r = pByteBuffer->ExpandCapacity(capacity);
1841                 if (IsFailed(r))
1842                 {
1843                         free(pVcardStream);
1844                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1845
1846                         return null;
1847                 }
1848
1849                 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
1850                 free(pVcardStream);
1851                 pVcardStream = null;
1852                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.: capacity(%d), size(%d)", GetErrorMessage(E_SYSTEM));
1853         }
1854
1855         return pByteBuffer.release();
1856 }
1857
1858 ByteBuffer*
1859 _AddressbookManagerImpl::ExportPersonToVcardStreamN(const Person& person)
1860 {
1861         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1862
1863         ClearLastResult();
1864
1865         int ret = CONTACTS_ERROR_NONE;
1866
1867         contacts_record_h personRecordHandle = null;
1868
1869         ret =contacts_db_get_record(_contacts_person._uri, person.GetId(), &personRecordHandle);
1870         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1871         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1872         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1873
1874         __ContactsRecordHandle recordHandle(personRecordHandle);
1875
1876         char* pVcardStream = null;
1877
1878         ret = contacts_vcard_make_from_person(personRecordHandle, &pVcardStream);
1879         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1880         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1881         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1882
1883         std::unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
1884         if (pByteBuffer == null)
1885         {
1886                 free(pVcardStream);
1887                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1888
1889                 return null;
1890         }
1891
1892         result r = pByteBuffer->Construct(strlen(pVcardStream));
1893         if (IsFailed(r))
1894         {
1895                 free(pVcardStream);
1896                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1897
1898                 return null;
1899         }
1900
1901         r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
1902         free(pVcardStream);
1903         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1904
1905         return pByteBuffer.release();
1906 }
1907
1908 ByteBuffer*
1909 _AddressbookManagerImpl::ExportPersonsToVcardStreamN(const Tizen::Base::Collection::IList& personList)
1910 {
1911         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1912
1913         ClearLastResult();
1914
1915         int ret = CONTACTS_ERROR_NONE;
1916         Person* pPerson = null;
1917         char* pVcardStream = null;
1918
1919         int capacity = 0;
1920
1921         contacts_record_h personRecordHandle = null;
1922         __ContactsRecordHandle recordHandle(null);
1923
1924         std::unique_ptr<IEnumerator> pEnum(personList.GetEnumeratorN());
1925         SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1926
1927         std::unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
1928         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1929
1930         result r = pByteBuffer->Construct(capacity);
1931         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1932
1933         if (personList.GetCount() == 0)
1934         {
1935                 return pByteBuffer.release();
1936         }
1937
1938         while (pEnum->MoveNext() == E_SUCCESS)
1939         {
1940                 pPerson = static_cast<Person*>(pEnum->GetCurrent());
1941
1942                 ret = contacts_db_get_record(_contacts_person._uri, pPerson->GetId(), &personRecordHandle);
1943                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1944                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1945                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1946
1947                 recordHandle.Reset(personRecordHandle);
1948
1949                 ret = contacts_vcard_make_from_person(personRecordHandle, &pVcardStream);
1950                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1951                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1952                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1953
1954                 capacity += strlen(pVcardStream);
1955                 r = pByteBuffer->ExpandCapacity(capacity);
1956                 if (IsFailed(r))
1957                 {
1958                         free(pVcardStream);
1959                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1960
1961                         return null;
1962                 }
1963
1964                 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
1965                 free(pVcardStream);
1966                 pVcardStream = null;
1967                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1968         }
1969
1970         return pByteBuffer.release();
1971 }
1972
1973 IList*
1974 _AddressbookManagerImpl::ParseVcardStreamN(const Tizen::Base::ByteBuffer& vcardStream)
1975 {
1976         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1977
1978         ClearLastResult();
1979
1980         contacts_list_h listHandle = null;
1981         result r = E_SUCCESS;
1982
1983         int ret = contacts_vcard_parse_to_contacts(reinterpret_cast<char*>(const_cast<byte*>(vcardStream.GetPointer())), &listHandle);
1984         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1985         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1986
1987         unsigned int count = 0;
1988         contacts_record_h recordHandle = null;
1989         contacts_list_get_count(listHandle, &count);
1990
1991         std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
1992         if (pList == null)
1993         {
1994                 contacts_list_destroy(listHandle, true);
1995                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1996
1997                 return null;
1998         }
1999
2000         r = pList->Construct(count);
2001         if (IsFailed(r))
2002         {
2003                 contacts_list_destroy(listHandle, true);
2004                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2005
2006                 return null;
2007         }
2008
2009         for (unsigned int i = 0; i < count; i++)
2010         {
2011                 std::unique_ptr<Contact> pContact(new (std::nothrow) Contact());
2012                 if (pContact == null)
2013                 {
2014                         contacts_list_destroy(listHandle, true);
2015                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2016
2017                         return null;
2018                 }
2019
2020                 r = pList->Add(pContact.get());
2021                 if (IsFailed(r))
2022                 {
2023                         contacts_list_destroy(listHandle, true);
2024                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2025
2026                         return null;
2027                 }
2028
2029                 pContact.release();
2030         }
2031
2032         std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
2033
2034         while (pEnum->MoveNext() == E_SUCCESS)
2035         {
2036                 Contact* pContact = static_cast <Contact*> (pEnum->GetCurrent());
2037
2038                 contacts_list_get_current_record_p(listHandle, &recordHandle);
2039                 _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(recordHandle);
2040
2041                 ret = contacts_list_next(listHandle);
2042         }
2043
2044         contacts_list_destroy(listHandle, false);
2045
2046         return pList.release();
2047 }
2048
2049 ByteBuffer*
2050 _AddressbookManagerImpl::ExportUserProfileToVcardStreamN(const UserProfile& userProfile)
2051 {
2052         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2053
2054         ClearLastResult();
2055
2056         char* pVcardStream = null;
2057         int ret = CONTACTS_ERROR_NONE;
2058         contacts_record_h recordHandle = null;
2059
2060         recordHandle = _UserProfileImpl::GetInstance(userProfile)->GetUserProfileHandle();
2061
2062         ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2063         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2064         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2065         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2066
2067         std::unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2068         if (pByteBuffer == null)
2069         {
2070                 free(pVcardStream);
2071                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2072
2073                 return null;
2074         }
2075         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2076
2077         result r = pByteBuffer->Construct(strlen(pVcardStream));
2078         if (IsFailed(r))
2079         {
2080                 free(pVcardStream);
2081                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2082
2083                 return null;
2084         }
2085
2086         r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2087         free(pVcardStream);
2088         SysTryReturn(NID_SCL, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2089         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2090
2091         return pByteBuffer.release();
2092 }
2093
2094 ByteBuffer*
2095 _AddressbookManagerImpl::ExportUserProfilesToVcardStreamN(const Tizen::Base::Collection::IList& userProfileList)
2096 {
2097         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2098
2099         ClearLastResult();
2100
2101         char* pVcardStream = null;
2102         int ret = CONTACTS_ERROR_NONE;
2103         UserProfile* pProfile = null;
2104         result r = E_SUCCESS;
2105         int capacity = 0;
2106
2107         std::unique_ptr<IEnumerator> pEnum(userProfileList.GetEnumeratorN());
2108         SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2109
2110         std::unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2111         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2112
2113         r = pByteBuffer->Construct(capacity);
2114         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2115
2116         if (userProfileList.GetCount() == 0)
2117         {
2118                 return pByteBuffer.release();
2119         }
2120
2121         while (pEnum->MoveNext() == E_SUCCESS)
2122         {
2123                 contacts_record_h recordHandle = null;
2124
2125                 pProfile = static_cast<UserProfile*>(pEnum->GetCurrent());
2126
2127                 recordHandle = _UserProfileImpl::GetInstance(*pProfile)->GetUserProfileHandle();
2128
2129                 ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2130                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2131                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2132                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2133
2134                 capacity += strlen(pVcardStream);
2135                 r = pByteBuffer->ExpandCapacity(capacity);
2136                 if (IsFailed(r))
2137                 {
2138                         free(pVcardStream);
2139                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2140
2141                         return null;
2142                 }
2143
2144                 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2145                 free(pVcardStream);
2146                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.: capacity(%d), size(%d)", GetErrorMessage(E_SYSTEM));
2147         }
2148
2149         return pByteBuffer.release();
2150 }
2151
2152 result
2153 _AddressbookManagerImpl::ExportUserProfileToVcard(const UserProfile& userProfile, const Tizen::Base::String& vcardPath)
2154 {
2155         bool exist = File::IsFileExist(vcardPath);
2156         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2157         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2158         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2159
2160         File file;
2161         result r = file.Construct(vcardPath, "w");
2162         SysTryReturn(NID_SCL, r != E_ILLEGAL_ACCESS, E_ILLEGAL_ACCESS, E_ILLEGAL_ACCESS, "[%s] Access to the vcard file is denied due to insufficient permission.", GetErrorMessage(E_ILLEGAL_ACCESS));
2163         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2164         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2165
2166         char* pVcardStream = null;
2167         contacts_record_h recordHandle = null;
2168
2169         recordHandle = _UserProfileImpl::GetInstance(userProfile)->GetUserProfileHandle();
2170
2171         int ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2172         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));
2173         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2174
2175         r = file.Write(pVcardStream, strlen(pVcardStream));
2176         free(pVcardStream);
2177         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2178         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2179
2180         return E_SUCCESS;
2181 }
2182
2183 result
2184 _AddressbookManagerImpl::ExportUserProfilesToVcard(const Tizen::Base::Collection::IList& userProfileList, const Tizen::Base::String& vcardPath)
2185 {
2186         bool exist = File::IsFileExist(vcardPath);
2187         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2188         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2189         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2190
2191         int ret = CONTACTS_ERROR_NONE;
2192         char* pVcardStream = null;
2193         File file;
2194         UserProfile* pProfile = null;
2195
2196         result r = file.Construct(vcardPath, "w");
2197         SysTryReturn(NID_SCL, r != E_ILLEGAL_ACCESS, E_ILLEGAL_ACCESS, E_ILLEGAL_ACCESS, "[%s] Access to the vcard file is denied due to insufficient permission.", GetErrorMessage(E_ILLEGAL_ACCESS));
2198         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2199         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2200
2201         std::unique_ptr<IEnumerator> pEnum(userProfileList.GetEnumeratorN());
2202         SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2203
2204         while (pEnum->MoveNext() == E_SUCCESS)
2205         {
2206                 contacts_record_h recordHandle = null;
2207
2208                 pProfile = static_cast<UserProfile*>(pEnum->GetCurrent());
2209
2210                 recordHandle = _UserProfileImpl::GetInstance(*pProfile)->GetUserProfileHandle();
2211
2212                 ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2213                 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));
2214                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2215
2216                 r = file.Write(pVcardStream, strlen(pVcardStream));
2217                 free(pVcardStream);
2218                 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2219                 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2220         }
2221
2222         return E_SUCCESS;
2223 }
2224
2225 IList*
2226 _AddressbookManagerImpl::GetAllUserProfilesN(void) const
2227 {
2228         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2229
2230         ClearLastResult();
2231
2232         __Query<__ContactsUserProfile> query;
2233         query.Construct();
2234         query.SetSort(_contacts_my_profile.display_name, true);
2235
2236         IList* pUserProfilesList = _AddressbookUtil::SearchWithQueryN<__ContactsUserProfile, UserProfile>(query);
2237         SysTryReturn(NID_SCL, pUserProfilesList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2238
2239         return pUserProfilesList;
2240 }
2241
2242 UserProfile*
2243 _AddressbookManagerImpl::GetUserProfileN(AddressbookId addressbookId) const
2244 {
2245         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2246         SysTryReturn(NID_SCL, addressbookId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. Addressbook Id(%d).", GetErrorMessage(E_INVALID_ARG), addressbookId);
2247
2248         ClearLastResult();
2249
2250         contacts_record_h addressbookHandle = null;
2251         int ret = contacts_db_get_record(_contacts_address_book._uri, addressbookId, &addressbookHandle);
2252         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2253         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The addressbook %d is not found.", GetErrorMessage(E_OBJ_NOT_FOUND), addressbookId);
2254         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred. Addressbook Id(%d)", GetErrorMessage(E_SYSTEM), addressbookId);
2255
2256         contacts_record_destroy(addressbookHandle, true);
2257
2258         __Filter<__ContactsUserProfile> filter;
2259         filter.Construct();
2260         filter.AddInt(_contacts_my_profile.address_book_id, CONTACTS_MATCH_EQUAL, addressbookId);
2261
2262         __Query<__ContactsUserProfile> query;
2263         query.Construct();
2264         query.SetFilter(filter);
2265
2266         std::unique_ptr<IList, AllElementsDeleter> pUserProfilesList(_AddressbookUtil::SearchWithQueryN<__ContactsUserProfile, UserProfile>(query));
2267         SysTryReturn(NID_SCL, pUserProfilesList.get() != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2268         SysTryReturn(NID_SCL, pUserProfilesList->GetCount() != 0, null, E_SUCCESS, "No UserProfile Set for this Addressbook.");
2269         SysTryReturn(NID_SCL, pUserProfilesList->GetCount() == 1, null, E_SYSTEM, "[%s] Propagating. More than one UserProfile not allowed.", GetErrorMessage(E_SYSTEM));
2270
2271         UserProfile* pProfile = new (std::nothrow) UserProfile(*(static_cast<UserProfile*>(pUserProfilesList->GetAt(0))));
2272         SysTryReturn(NID_SCL, pProfile != null, null, E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(E_OUT_OF_MEMORY));
2273
2274         return pProfile;
2275 }
2276
2277 _AddressbookManagerImpl*
2278 _AddressbookManagerImpl::GetInstance(AddressbookManager& addressbookManager)
2279 {
2280         return addressbookManager.__pAddressbookManagerImpl;
2281 }
2282
2283 const _AddressbookManagerImpl*
2284 _AddressbookManagerImpl::GetInstance(const AddressbookManager& addressbookManager)
2285 {
2286         return addressbookManager.__pAddressbookManagerImpl;
2287 }
2288
2289 }}  // Tizen::Social