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