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