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