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