Filters read-only addressbook
[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.AddFilter(*pRwAbFilter);
632         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
633         mainFilter.AddFilter(relFilter);
634
635         __Query<__ContactsContactGroupRel> query;
636         query.Construct();
637         query.SetFilter(mainFilter);
638
639         IList* pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Category>(query);
640         SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
641
642         return pCategories;
643 }
644
645 IList*
646 _AddressbookManagerImpl::GetCategoriesByPersonN(PersonId personId) const
647 {
648         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
649
650         ClearLastResult();
651
652         unique_ptr< __Filter<__ContactsContactGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactGroupRel>());
653
654         __Filter<__ContactsContactGroupRel> relFilter;
655         relFilter.Construct();
656         relFilter.AddInt(_contacts_contact_grouprel.person_id, CONTACTS_MATCH_EQUAL, personId);
657         relFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
658         relFilter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_GREATER_THAN, 0);
659
660         __Filter<__ContactsContactGroupRel> mainFilter;
661         mainFilter.AddFilter(*pRwAbFilter);
662         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
663         mainFilter.AddFilter(relFilter);
664
665         unsigned int propertyIds[] =
666         {
667                 _contacts_contact_grouprel.group_id,
668         };
669
670         __Query<__ContactsContactGroupRel> query;
671         query.Construct();
672         query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
673         query.SetFilter(mainFilter);
674         query.SetSort(_contacts_contact_grouprel.group_name, true);
675         query.SetDistinct(true);
676
677         IList* pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Category>(query);
678         SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
679
680         return pCategories;
681 }
682
683 IList*
684 _AddressbookManagerImpl::GetAllContactsN(void) const
685 {
686         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
687
688         ClearLastResult();
689
690         unique_ptr< __Filter<__ContactsContact> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContact>());
691
692         __Query<__ContactsContact> query;
693         query.Construct();
694         query.SetFilter(*pRwAbFilter);
695         query.SetSort(_contacts_contact.display_name, true);
696
697         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
698         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
699
700         return pContacts;
701 }
702
703 IList*
704 _AddressbookManagerImpl::GetContactsByCategoryN(RecordId categoryId) const
705 {
706         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));
707         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
708
709         ClearLastResult();
710
711         unique_ptr< __Filter<__ContactsContactGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactGroupRel>());
712
713         __Filter<__ContactsContactGroupRel> relFilter;
714         relFilter.Construct();
715         
716         if (categoryId != INVALID_RECORD_ID)
717         {
718                 relFilter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_EQUAL, categoryId);
719         }
720         else
721         {
722                 relFilter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_NONE, 0);
723         }
724
725         __Filter<__ContactsContactGroupRel> mainFilter;
726         mainFilter.Construct();
727
728         mainFilter.AddFilter(*pRwAbFilter);
729         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
730         mainFilter.AddFilter(relFilter);
731
732         __Query<__ContactsContactGroupRel> query;
733         query.Construct();
734         query.SetFilter(mainFilter);
735
736         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Contact>(query);
737         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
738
739         return pContacts;
740 }
741
742 IList*
743 _AddressbookManagerImpl::GetContactsByPersonN(PersonId personId) const
744 {
745         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
746
747         ClearLastResult();
748
749         unique_ptr< __Filter<__ContactsContact> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContact>());
750
751         __Filter<__ContactsContact> contactFilter;
752         contactFilter.Construct();
753         contactFilter.AddInt(_contacts_contact.person_id, CONTACTS_MATCH_EQUAL, personId);
754
755         __Filter<__ContactsContact> mainFilter;
756         mainFilter.Construct();
757
758         mainFilter.AddFilter(*pRwAbFilter);
759         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
760         mainFilter.AddFilter(contactFilter);
761
762         __Query<__ContactsContact> query;
763         query.Construct();
764         query.SetFilter(mainFilter);
765         query.SetSort(_contacts_contact.display_name, true);
766
767         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
768         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
769
770         return pContacts;
771 }
772
773 IList*
774 _AddressbookManagerImpl::SearchContactsByEmailN(const String& email) const
775 {
776         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));
777         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
778
779         ClearLastResult();
780
781         unique_ptr< __Filter<__ContactsContactEmail> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactEmail>());
782
783         unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(email));
784         SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
785
786         __Filter<__ContactsContactEmail> emailFilter;
787         emailFilter.Construct();
788         emailFilter.AddString(_contacts_contact_email.email, CONTACTS_MATCH_CONTAINS, pCharArray.get());
789
790         __Filter<__ContactsContactEmail> mainFilter;
791         mainFilter.Construct();
792
793         mainFilter.AddFilter(*pRwAbFilter);
794         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
795         mainFilter.AddFilter(emailFilter);
796
797         __Query<__ContactsContactEmail> query;
798         query.Construct();
799         query.SetFilter(mainFilter);
800         query.SetSort(_contacts_contact_email.display_name, true);
801
802         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactEmail, Contact>(query);
803         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
804
805         return pContacts;
806 }
807
808 IList*
809 _AddressbookManagerImpl::SearchContactsByNameN(const String& name) const
810 {
811         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
812
813         ClearLastResult();
814
815         unique_ptr< __Filter<__ContactsContact> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContact>());
816
817         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));
818
819         unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(name));
820         SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
821
822         __Filter<__ContactsContact> nameFilter;
823         nameFilter.Construct();
824         nameFilter.AddString(_contacts_contact.display_name, CONTACTS_MATCH_CONTAINS, pCharArray.get());
825
826         __Filter<__ContactsContact> mainFilter;
827         mainFilter.Construct();
828
829         mainFilter.AddFilter(*pRwAbFilter);
830         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
831         mainFilter.AddFilter(nameFilter);
832
833         __Query<__ContactsContact> query;
834         query.Construct();
835         query.SetFilter(mainFilter);
836         query.SetSort(_contacts_contact.display_name, true);
837
838         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
839         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
840
841         return pContacts;
842 }
843
844 IList*
845 _AddressbookManagerImpl::SearchContactsByPhoneNumberN(const String& phoneNumber) const
846 {
847         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
848
849         ClearLastResult();
850
851         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));
852
853         unique_ptr< __Filter<__ContactsContactNumber> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactNumber>());
854
855         unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(phoneNumber));
856         SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
857
858         __Filter<__ContactsContactNumber> numberFilter;
859         numberFilter.Construct();
860         numberFilter.AddString(_contacts_contact_number.number, CONTACTS_MATCH_CONTAINS, pCharArray.get());
861
862         __Filter<__ContactsContactNumber> mainFilter;
863         mainFilter.Construct();
864
865         mainFilter.AddFilter(*pRwAbFilter);
866         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
867         mainFilter.AddFilter(numberFilter);
868
869         __Query<__ContactsContactNumber> query;
870         query.Construct();
871         query.SetFilter(mainFilter);
872         query.SetDistinct(true);
873         query.SetSort(_contacts_contact_number.display_name, true);
874
875         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactNumber, Contact>(query);
876         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
877
878         return pContacts;
879 }
880
881 int
882 _AddressbookManagerImpl::GetCategoryCount(void) const
883 {
884         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
885
886         ClearLastResult();
887
888         unique_ptr< __Filter<__ContactsGroup> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsGroup>());
889
890         int count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsGroup>(pRwAbFilter->Get());
891         SysTryReturn(NID_SCL, count >= 0, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
892
893         return count;
894 }
895
896 int
897 _AddressbookManagerImpl::GetContactCount(void) const
898 {
899         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
900
901         ClearLastResult();
902
903         unique_ptr< __Filter<__ContactsContact> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContact>());
904
905         int count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContact>(pRwAbFilter->Get());
906         SysTryReturn(NID_SCL, count >= 0, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
907
908         return count;
909 }
910
911 Contact*
912 _AddressbookManagerImpl::GetContactN(RecordId contactId) const
913 {
914         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
915         SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. contactId = %d.", GetErrorMessage(E_INVALID_ARG), contactId);
916
917         ClearLastResult();
918
919         unique_ptr<ContactRecord, ContactRecordDeleter> pContactRecord(_AddressbookUtil::GetContactRecordN(_contacts_contact._uri, contactId));
920         SysTryReturn(NID_SCL, pContactRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
921
922         int intValue = 0;
923         contacts_record_get_int(pContactRecord.get(), _contacts_contact.address_book_id, &intValue);
924
925         unique_ptr<ContactRecord, ContactRecordDeleter> pAbRecord(_AddressbookUtil::GetContactRecordN(_contacts_address_book._uri, intValue));
926         SysTryReturn(NID_SCL, pAbRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
927
928         contacts_record_get_int(pAbRecord.get(), _contacts_address_book.mode, &intValue);
929         SysTryReturn(NID_SCL, intValue == 0, null, E_OBJ_NOT_FOUND, "[%s] Contact does not exist.", GetErrorMessage(E_OBJ_NOT_FOUND));
930
931         contacts_record_get_int(pContactRecord.get(), _contacts_contact.id, &intValue);
932         SysTryReturn(NID_SCL, intValue == contactId, null, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
933
934         unique_ptr<Contact> pContact(new (std::nothrow) Contact());
935         SysTryReturn(NID_SCL, pContact, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
936
937         _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(pContactRecord.release());
938         _RecordImpl::GetInstance(*pContact)->SetRecordId(intValue);
939
940         return pContact.release();
941 }
942
943 Person*
944 _AddressbookManagerImpl::GetPersonN(PersonId personId) const
945 {
946         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
947         SysTryReturn(NID_SCL, personId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. personId = %d.", GetErrorMessage(E_INVALID_ARG), personId);
948
949         ClearLastResult();
950
951         unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, personId));
952
953         int intValue = 0;
954         contacts_record_get_int(pPersonRecord.get(), _contacts_person.id, &intValue);
955         SysTryReturn(NID_SCL, intValue == personId, null, E_OBJ_NOT_FOUND, "[%s] The person is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
956
957         Person* pPerson = __ContactsPerson::ConvertHandleTo<Person>(pPersonRecord.get());
958         SysTryReturn(NID_SCL, pPerson != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
959
960         return pPerson;
961 }
962
963 Category*
964 _AddressbookManagerImpl::GetCategoryN(RecordId categoryId) const
965 {
966         SysTryReturn(NID_SCL, categoryId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. categoryId = %d.", categoryId);
967         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
968
969         ClearLastResult();
970
971         unique_ptr<ContactRecord, ContactRecordDeleter> pCategoryRecord(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, categoryId));
972         SysTryReturn(NID_SCL, pCategoryRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
973
974         int intValue = 0;
975
976         contacts_record_get_int(pCategoryRecord.get(), _contacts_group.id, &intValue);
977         SysTryReturn(NID_SCL, categoryId == intValue, null, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
978
979         contacts_record_get_int(pCategoryRecord.get(), _contacts_group.address_book_id, &intValue);
980
981         unique_ptr<ContactRecord, ContactRecordDeleter> pAbRecord(_AddressbookUtil::GetContactRecordN(_contacts_address_book._uri, intValue));
982         SysTryReturn(NID_SCL, pAbRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
983
984         contacts_record_get_int(pAbRecord.get(), _contacts_address_book.mode, &intValue);
985         SysTryReturn(NID_SCL, intValue == 0, null, E_OBJ_NOT_FOUND, "[%s] Category does not exist.", GetErrorMessage(E_OBJ_NOT_FOUND));
986
987         unique_ptr<Category> pCategory(new (std::nothrow) Category());
988         SysTryReturn(NID_SCL, pCategory != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
989
990         __Filter<__ContactsGroupRelation> filter;
991         filter.Construct();
992         filter.AddInt(_contacts_group_relation.group_id, CONTACTS_MATCH_EQUAL, categoryId);
993
994         __Query<__ContactsGroupRelation> query;
995         query.Construct();
996         query.SetFilter(filter);
997
998         int count = _AddressbookUtil::GetCountWithQuery(query);
999         SysTryReturn(NID_SCL, count >= 0, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1000
1001         _CategoryImpl::GetInstance(*pCategory)->SetRecordHandle(pCategoryRecord.release());
1002         _CategoryImpl::GetInstance(*pCategory)->SetMemberCount(count);
1003         _RecordImpl::GetInstance(*pCategory)->SetRecordId(categoryId);
1004
1005         return pCategory.release();
1006 }
1007
1008 int
1009 _AddressbookManagerImpl::GetLatestVersion(void) const
1010 {
1011         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1012
1013         int latestVersion = -1;
1014
1015         ClearLastResult();
1016
1017         int ret = contacts_db_get_current_version(&latestVersion);
1018         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1019
1020         return latestVersion;
1021 }
1022
1023 IList*
1024 _AddressbookManagerImpl::GetChangedContactsAfterN(int version, int& latestVersion) const
1025 {
1026         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);
1027         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1028
1029         ClearLastResult();
1030
1031         unique_ptr< IListT<AddressbookId> > pRwAbIdList(_AddressbookUtil::GetRwAbIdListN());
1032
1033         IList* pChangedContacts = _AddressbookUtil::SearchWithVersionN<__ContactsContactUpdatedInfo, ContactChangeInfo>(-1, version, latestVersion, pRwAbIdList.get());
1034         SysTryReturn(NID_SCL, pChangedContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1035
1036         return pChangedContacts;
1037 }
1038
1039 IList*
1040 _AddressbookManagerImpl::GetChangedCategoriesAfterN(int version, int& latestVersion) const
1041 {
1042         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);
1043         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1044
1045         ClearLastResult();
1046
1047         int latestVersion1 = 0;
1048         int latestVersion2 = 0;
1049
1050         unique_ptr< IListT<AddressbookId> > pRwAbIdList(_AddressbookUtil::GetRwAbIdListN());
1051
1052         unique_ptr<IList, AllElementsDeleter> pChangedGroups(_AddressbookUtil::SearchWithVersionN<__ContactsGroupUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion1, pRwAbIdList.get()));
1053         SysTryReturn(NID_SCL, pChangedGroups != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1054
1055         unique_ptr<IList, AllElementsDeleter> pChangedRelations(_AddressbookUtil::SearchWithVersionN<__ContactsGroupRelUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion2, pRwAbIdList.get()));
1056         SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1057
1058         unique_ptr<ArrayList, AllElementsDeleter> pChangeList(new (std::nothrow) Tizen::Base::Collection::ArrayList());
1059         SysTryReturn(NID_SCL, pChangeList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1060
1061         result r = pChangeList->AddItems(*pChangedGroups);
1062         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1063
1064         r = pChangeList->AddItems(*pChangedRelations);
1065         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1066
1067         pChangedGroups->RemoveAll(false);
1068         pChangedRelations->RemoveAll(false);
1069
1070         latestVersion = latestVersion2 > latestVersion1 ? latestVersion2 : latestVersion1;
1071         
1072         return pChangeList.release();
1073 }
1074
1075 IList*
1076 _AddressbookManagerImpl::GetChangedGroupsAfterN(int version, int& latestVersion) const
1077 {
1078         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);
1079         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1080
1081         ClearLastResult();
1082
1083         unique_ptr< IListT<AddressbookId> > pRwAbIdList(_AddressbookUtil::GetRwAbIdListN());
1084
1085         IList* pChangedRelations = _AddressbookUtil::SearchWithVersionN<__ContactsGroupUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion, pRwAbIdList.get());
1086         SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1087
1088         return pChangedRelations;
1089 }
1090
1091 IList*
1092 _AddressbookManagerImpl::GetChangedRelationsAfterN(int version, int& latestVersion) const
1093 {
1094         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);
1095         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1096
1097         ClearLastResult();
1098
1099         unique_ptr< IListT<AddressbookId> > pRwAbIdList(_AddressbookUtil::GetRwAbIdListN());
1100
1101         IList* pChangedRelations = _AddressbookUtil::SearchWithVersionN<__ContactsGroupRelUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion, pRwAbIdList.get());
1102         SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1103
1104         return pChangedRelations;
1105 }
1106
1107 void
1108 _AddressbookManagerImpl::OnContactChanged(void)
1109 {
1110         if (__pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
1111         {
1112                 return;
1113         }
1114
1115         IList* pChangedContactList = GetChangedContactsAfterN(__dbVersionForContact, __dbVersionForContact);
1116         SysTryReturnVoidResult(NID_SCL, pChangedContactList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1117
1118         if (pChangedContactList->GetCount() > 0)
1119         {
1120                 if (__pIAddressbookChangeEventListener != null)
1121                 {
1122                         __pIAddressbookChangeEventListener->OnContactsChanged(*pChangedContactList);
1123                 }
1124                 else
1125                 {
1126                         __pIAddressbookEventListener->OnContactsChanged(*pChangedContactList);
1127                 }
1128         }
1129
1130         pChangedContactList->RemoveAll(true);
1131         delete pChangedContactList;
1132 }
1133
1134 void
1135 _AddressbookManagerImpl::OnCategoryChanged(void)
1136 {
1137         if (__pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
1138         {
1139                 return;
1140         }
1141
1142         IList* pChangedCategoryList = GetChangedGroupsAfterN(__dbVersionForGroup, __dbVersionForGroup);
1143         SysTryReturnVoidResult(NID_SCL, pChangedCategoryList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1144
1145         if (pChangedCategoryList->GetCount() > 0)
1146         {
1147                 if (__pIAddressbookChangeEventListener != null)
1148                 {
1149                         __pIAddressbookChangeEventListener->OnCategoriesChanged(*pChangedCategoryList);
1150                 }
1151                 else
1152                 {
1153                         __pIAddressbookEventListener->OnCategoriesChanged(*pChangedCategoryList);
1154                 }
1155         }
1156
1157         pChangedCategoryList->RemoveAll(true);
1158         delete pChangedCategoryList;
1159 }
1160
1161 void
1162 _AddressbookManagerImpl::OnRelationChanged(void)
1163 {
1164         if (__pIAddressbookEventListener == null)
1165         {
1166                 return;
1167         }
1168
1169         IList* pChangedCategoryList = GetChangedRelationsAfterN(__dbVersionForRelation, __dbVersionForRelation);
1170         SysTryReturnVoidResult(NID_SCL, pChangedCategoryList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1171
1172         if (pChangedCategoryList->GetCount() > 0)
1173         {
1174                 if (__pIAddressbookEventListener != null)
1175                 {
1176                         __pIAddressbookEventListener->OnCategoriesChanged(*pChangedCategoryList);
1177                 }
1178         }
1179
1180         pChangedCategoryList->RemoveAll(true);
1181         delete pChangedCategoryList;
1182 }
1183
1184 result
1185 _AddressbookManagerImpl::RemovePerson(PersonId personId)
1186 {
1187         SysTryReturnResult(NID_SCL, personId > 0, E_INVALID_ARG, "[%s] Invalid argument is used. personId = %d.", GetErrorMessage(E_INVALID_ARG), personId);
1188         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1189
1190         unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, personId));
1191         SysTryReturn(NID_SCL, pPersonRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1192
1193         int ret = contacts_db_delete_record(_contacts_person._uri, personId);
1194         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.");
1195         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));
1196         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));
1197
1198         return E_SUCCESS;
1199 }
1200
1201 IList*
1202 _AddressbookManagerImpl::GetAllPersonsN(void) const
1203 {
1204         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1205
1206         ClearLastResult();
1207
1208         unique_ptr< __Filter<__ContactsPersonGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsPersonGroupRel>());
1209
1210 //      unsigned int propertyIds[] = { _contacts_person_grouprel.person_id };
1211         unsigned int propertyIds[] =
1212         {
1213                 _contacts_person_grouprel.person_id,
1214                 _contacts_person_grouprel.display_name,
1215                 _contacts_person_grouprel.image_thumbnail_path,
1216                 _contacts_person_grouprel.ringtone_path,
1217                 _contacts_person_grouprel.is_favorite,
1218                 _contacts_person_grouprel.has_phonenumber,
1219                 _contacts_person_grouprel.has_email,
1220                 _contacts_person_grouprel.addressbook_ids,
1221         };
1222
1223
1224         __Query<__ContactsPersonGroupRel> query;
1225         query.Construct();
1226         query.SetFilter(*pRwAbFilter);
1227         query.SetSort(_contacts_person.display_name, true);
1228         query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1229         query.SetDistinct(true);
1230
1231         IList* pPersons = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query);
1232         SysTryReturn(NID_SCL, pPersons != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1233
1234         return pPersons;
1235 }
1236
1237 IList*
1238 _AddressbookManagerImpl::GetPersonsByCategoryN(RecordId categoryId) const
1239 {
1240         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));
1241         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1242
1243         ClearLastResult();
1244
1245         unique_ptr< __Filter<__ContactsPersonGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsPersonGroupRel>());
1246
1247         __Filter<__ContactsPersonGroupRel> groupFilter;
1248         groupFilter.Construct();
1249         if (categoryId != INVALID_RECORD_ID)
1250         {
1251                 groupFilter.AddInt(_contacts_person_grouprel.group_id, CONTACTS_MATCH_EQUAL, categoryId);
1252         }
1253         else
1254         {
1255                 groupFilter.AddInt(_contacts_person_grouprel.group_id, CONTACTS_MATCH_NONE, 0);
1256         }
1257
1258         unsigned int propertyIds[] =
1259         {
1260                 _contacts_person_grouprel.person_id,
1261                 _contacts_person_grouprel.display_name,
1262                 _contacts_person_grouprel.image_thumbnail_path,
1263                 _contacts_person_grouprel.ringtone_path,
1264                 _contacts_person_grouprel.is_favorite,
1265                 _contacts_person_grouprel.has_phonenumber,
1266                 _contacts_person_grouprel.has_email,
1267                 _contacts_person_grouprel.addressbook_ids,
1268         };
1269
1270         __Filter<__ContactsPersonGroupRel> mainFilter;
1271         mainFilter.Construct();
1272
1273         mainFilter.AddFilter(*pRwAbFilter);
1274         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1275         mainFilter.AddFilter(groupFilter);
1276
1277
1278         __Query<__ContactsPersonGroupRel> query;
1279         query.Construct();
1280         query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1281         query.SetFilter(mainFilter);
1282         query.SetSort(_contacts_person_grouprel.display_name, true);
1283         query.SetDistinct(true);
1284
1285         IList* pPersons = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query);
1286         SysTryReturn(NID_SCL, pPersons != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1287
1288         return pPersons;
1289 }
1290
1291 IList*
1292 _AddressbookManagerImpl::GetFavoritePersonsN() const
1293 {
1294         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1295
1296         ClearLastResult();
1297
1298         __Filter<__ContactsPerson> filter;
1299         filter.Construct();
1300         filter.AddBool(_contacts_person.is_favorite, true);
1301
1302         __Query<__ContactsPerson> query;
1303         query.Construct();
1304         query.SetFilter(filter);
1305         query.SetSort(_contacts_person.display_name, true);
1306
1307         IList* pPersons = _AddressbookUtil::SearchWithQueryN<__ContactsPerson, Person>(query);
1308         SysTryReturn(NID_SCL, pPersons != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1309
1310         return pPersons;
1311 }
1312
1313 IList*
1314 _AddressbookManagerImpl::SearchPersonsN(const Tizen::Base::String& keyword) const
1315 {
1316         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1317         SysTryReturn(NID_SCL, !keyword.IsEmpty(), null, E_INVALID_ARG, "Invalid argument is used. keyword is empty string.", GetErrorMessage(E_INVALID_ARG));
1318
1319         ClearLastResult();
1320
1321         contacts_record_h currentRecord = null;
1322         unique_ptr<Person> pPerson(null);
1323
1324         unique_ptr<ArrayList, AllElementsDeleter> pPersonList(new (std::nothrow) ArrayList());
1325         SysTryReturn(NID_SCL, pPersonList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1326
1327         result r = pPersonList->Construct();
1328         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1329
1330         unique_ptr<__SearchResult<__ContactsPerson> > pSearchResult(_AddressbookUtil::Search<__ContactsPerson>(keyword));
1331         SysTryReturn(NID_SCL, pSearchResult != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1332
1333         while (pSearchResult->MoveNext() == E_SUCCESS)
1334         {
1335                 currentRecord = pSearchResult->GetCurrentRecord();
1336                 SysTryReturn(NID_SCL, currentRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1337
1338                 pPerson.reset(__ContactsPerson::ConvertHandleTo<Person>(currentRecord));
1339                 SysTryReturn(NID_SCL, pPerson != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1340
1341                 r = pPersonList->Add(*pPerson);
1342                 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
1343
1344                 pPerson.release();
1345         }
1346
1347         return pPersonList.release();
1348 }
1349
1350 result
1351 _AddressbookManagerImpl::SetPersonAsFavorite(PersonId personId, bool isFavorite)
1352 {
1353         SysTryReturn(NID_SCL, personId > 0, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. personId = %d.", GetErrorMessage(E_INVALID_ARG), personId);
1354         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1355
1356         unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, personId));
1357         SysTryReturn(NID_SCL, pPersonRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1358
1359         bool boolValue = false;
1360         contacts_record_get_bool(pPersonRecord.get(), _contacts_person.is_favorite, &boolValue);
1361
1362         if (boolValue != isFavorite)
1363         {
1364                 contacts_record_set_bool(pPersonRecord.get(), _contacts_person.is_favorite, isFavorite);
1365
1366                 int ret = contacts_db_update_record(pPersonRecord.get());
1367                 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));
1368                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1369         }
1370
1371         return E_SUCCESS;
1372 }
1373
1374 result
1375 _AddressbookManagerImpl::MergePersons(PersonId sourcePersonId, PersonId targetPersonId)
1376 {
1377         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);
1378         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1379
1380         int ret = contacts_person_link_person(targetPersonId, sourcePersonId);
1381         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1382         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1383
1384         return E_SUCCESS;
1385 }
1386
1387 result
1388 _AddressbookManagerImpl::UnlinkContact(PersonId personId, RecordId contactId, PersonId& newPersonId)
1389 {
1390         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);
1391         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1392
1393         int ret = contacts_person_unlink_contact(personId, contactId, &newPersonId);
1394         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));
1395         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1396
1397         return E_SUCCESS;
1398 }
1399
1400 IList*
1401 _AddressbookManagerImpl::SearchN(const AddressbookFilter& filter, unsigned long propertySortedBy, SortOrder sortOrder, int offset, int maxCount)
1402 {
1403         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1404
1405         ClearLastResult();
1406
1407         IList* pList = null;
1408         bool ascending = false;
1409         unsigned int viewSortPropertyId = 0;
1410
1411         AddressbookFilterType type = _AddressbookFilterImpl::GetInstance(filter)->GetType();
1412         contacts_filter_h filterHandle = _AddressbookFilterImpl::GetInstance(filter)->GetFilterHandle();
1413
1414         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);
1415
1416         if (propertySortedBy != 0 && sortOrder != SORT_ORDER_NONE)
1417         {
1418                 viewSortPropertyId = _AddressbookFilterImpl::GetViewPropertyId(type, propertySortedBy);
1419                 ascending = (sortOrder == SORT_ORDER_ASCENDING) ? true : false;
1420         }
1421
1422         switch(type)
1423         {
1424         case AB_FI_TYPE_ADDRESSBOOK:
1425                 {
1426                         unique_ptr< __Filter<__ContactsAddressbook> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsAddressbook>());
1427
1428                         __Filter<__ContactsAddressbook> abFilter;
1429                         abFilter.Construct(filterHandle);
1430
1431                         __Filter<__ContactsAddressbook> mainFilter;
1432                         mainFilter.Construct();
1433
1434                         mainFilter.AddFilter(*pRwAbFilter);
1435                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1436                         mainFilter.AddFilter(abFilter);
1437
1438                         __Query<__ContactsAddressbook> query;
1439                         query.Construct();
1440                         query.SetFilter(mainFilter);
1441
1442                         if (viewSortPropertyId != 0)
1443                         {
1444                                 query.SetSort(viewSortPropertyId, ascending);
1445                         }
1446
1447                         pList = _AddressbookUtil::SearchWithQueryN<__ContactsAddressbook, Addressbook>(query, offset, maxCount);
1448                 }
1449                 break;
1450         case AB_FI_TYPE_PERSON:
1451                 {
1452                         unique_ptr< __Filter<__ContactsPersonGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsPersonGroupRel>());
1453
1454                         __Filter<__ContactsPersonGroupRel> personFilter;
1455                         personFilter.Construct(filterHandle);
1456
1457                         __Filter<__ContactsPersonGroupRel> mainFilter;
1458                         mainFilter.Construct();
1459
1460                         mainFilter.AddFilter(*pRwAbFilter);
1461                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1462                         mainFilter.AddFilter(personFilter);
1463
1464                         unsigned int propertyIds[] =
1465                         { _contacts_person_grouprel.person_id,
1466                                 _contacts_person_grouprel.addressbook_ids,
1467                                 _contacts_person_grouprel.is_favorite,
1468                                 _contacts_person_grouprel.has_phonenumber,
1469                                 _contacts_person_grouprel.has_email,
1470                                 _contacts_person_grouprel.image_thumbnail_path,
1471                                 _contacts_person_grouprel.ringtone_path,
1472                                 _contacts_person_grouprel.display_name
1473                         };
1474
1475                         __Query<__ContactsPersonGroupRel> query;
1476                         query.Construct();
1477                         query.SetFilter(mainFilter);
1478                         query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1479                         query.SetDistinct(true);
1480
1481                         if (viewSortPropertyId != 0)
1482                         {
1483                                 query.SetSort(viewSortPropertyId, ascending);
1484                         }
1485
1486                         pList = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query, offset, maxCount);
1487                 }
1488                 break;
1489         case AB_FI_TYPE_CONTACT:
1490                 {
1491                         unique_ptr< __Filter<__ContactsContact> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContact>());
1492
1493                         __Filter<__ContactsContact> contactFilter;
1494                         contactFilter.Construct(filterHandle);
1495
1496                         __Filter<__ContactsContact> mainFilter;
1497                         mainFilter.Construct();
1498
1499                         mainFilter.AddFilter(*pRwAbFilter);
1500                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1501                         mainFilter.AddFilter(contactFilter);
1502
1503                         __Query<__ContactsContact> query;
1504                         query.Construct();
1505                         query.SetFilter(mainFilter);
1506
1507                         if (viewSortPropertyId != 0)
1508                         {
1509                                 query.SetSort(viewSortPropertyId, ascending);
1510                         }
1511
1512                         pList = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query, offset, maxCount);
1513                 }
1514                 break;
1515         case AB_FI_TYPE_CATEGORY:
1516                 {
1517                         unique_ptr< __Filter<__ContactsGroup> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsGroup>());
1518
1519                         __Filter<__ContactsGroup> groupFilter;
1520                         groupFilter.Construct(filterHandle);
1521
1522                         __Filter<__ContactsGroup> mainFilter;
1523                         mainFilter.Construct();
1524
1525                         mainFilter.AddFilter(*pRwAbFilter);
1526                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1527                         mainFilter.AddFilter(groupFilter);
1528
1529                         __Query<__ContactsGroup> query;
1530                         query.Construct();
1531                         query.SetFilter(mainFilter);
1532
1533                         if (viewSortPropertyId != 0)
1534                         {
1535                                 query.SetSort(viewSortPropertyId, ascending);
1536                         }
1537
1538                         pList = _AddressbookUtil::SearchWithQueryN<__ContactsGroup, Category>(query, offset, maxCount);
1539                 }
1540                 break;
1541         case AB_FI_TYPE_PHONE_CONTACT:
1542                 {
1543                         unique_ptr< __Filter<__ContactsContactNumber> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactNumber>());
1544
1545                         __Filter<__ContactsContactNumber> numberFilter;
1546                         numberFilter.Construct(filterHandle);
1547
1548                         __Filter<__ContactsContactNumber> mainFilter;
1549                         mainFilter.Construct();
1550
1551                         mainFilter.AddFilter(*pRwAbFilter);
1552                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1553                         mainFilter.AddFilter(numberFilter);
1554
1555                         __Query<__ContactsContactNumber> query;
1556                         query.Construct();
1557                         query.SetFilter(mainFilter);
1558
1559                         if (viewSortPropertyId != 0)
1560                         {
1561                                 query.SetSort(viewSortPropertyId, ascending);
1562                         }
1563
1564                         pList = _AddressbookUtil::SearchWithQueryN<__ContactsContactNumber, PhoneNumberContact>(query, offset, maxCount);
1565                 }
1566                 break;
1567         case AB_FI_TYPE_EMAIL_CONTACT:
1568                 {
1569                         unique_ptr< __Filter<__ContactsContactEmail> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactEmail>());
1570
1571                         __Filter<__ContactsContactEmail> emailFilter;
1572                         emailFilter.Construct(filterHandle);
1573
1574                         __Filter<__ContactsContactEmail> mainFilter;
1575                         mainFilter.Construct();
1576
1577                         mainFilter.AddFilter(*pRwAbFilter);
1578                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1579                         mainFilter.AddFilter(emailFilter);
1580
1581                         __Query<__ContactsContactEmail> query;
1582                         query.Construct();
1583                         query.SetFilter(mainFilter);
1584
1585                         if (viewSortPropertyId != 0)
1586                         {
1587                                 query.SetSort(viewSortPropertyId, ascending);
1588                         }
1589
1590                         pList = _AddressbookUtil::SearchWithQueryN<__ContactsContactEmail, EmailContact>(query, offset, maxCount);
1591                 }
1592                 break;
1593         default:
1594                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. The filter type is invalid", GetErrorMessage(E_INVALID_ARG));
1595                 pList = null;
1596         };
1597
1598         return pList;
1599 }
1600
1601 int
1602 _AddressbookManagerImpl::GetMatchedItemCount(const AddressbookFilter& filter)
1603 {
1604         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1605
1606         ClearLastResult();
1607
1608         int count = 0;
1609         AddressbookFilterType type = _AddressbookFilterImpl::GetInstance(filter)->GetType();
1610         contacts_filter_h filterHandle = _AddressbookFilterImpl::GetInstance(filter)->GetFilterHandle();
1611
1612         switch(type)
1613         {
1614         case AB_FI_TYPE_ADDRESSBOOK:
1615                 {
1616                         unique_ptr< __Filter<__ContactsAddressbook> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsAddressbook>());
1617
1618                         __Filter<__ContactsAddressbook> abFilter;
1619                         abFilter.Construct(filterHandle);
1620
1621                         __Filter<__ContactsAddressbook> mainFilter;
1622                         mainFilter.Construct();
1623                         mainFilter.AddFilter(*pRwAbFilter);
1624                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1625                         mainFilter.AddFilter(abFilter);
1626
1627                         count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsAddressbook>(mainFilter.Get());
1628                 }
1629                 break;
1630         case AB_FI_TYPE_PERSON:
1631                 {
1632                         unique_ptr< __Filter<__ContactsPersonGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsPersonGroupRel>());
1633
1634                         __Filter<__ContactsPersonGroupRel> personFilter;
1635                         personFilter.Construct(filterHandle);
1636
1637                         __Filter<__ContactsPersonGroupRel> mainFilter;
1638                         mainFilter.Construct();
1639                         mainFilter.AddFilter(*pRwAbFilter);
1640                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1641                         mainFilter.AddFilter(personFilter);
1642
1643                         unsigned int propertyIds[] = { _contacts_person_grouprel.person_id };
1644
1645                         __Query<__ContactsPersonGroupRel> query;
1646                         query.Construct();
1647                         query.SetFilter(mainFilter);
1648                         query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1649                         query.SetDistinct(true);
1650
1651                         count = _AddressbookUtil::GetCountWithQuery(query);
1652                 }
1653
1654                 break;
1655         case AB_FI_TYPE_CONTACT:
1656                 {
1657                         unique_ptr< __Filter<__ContactsContact> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContact>());
1658
1659                         __Filter<__ContactsContact> contactFilter;
1660                         contactFilter.Construct(filterHandle);
1661
1662                         __Filter<__ContactsContact> mainFilter;
1663                         mainFilter.Construct();
1664                         mainFilter.AddFilter(*pRwAbFilter);
1665                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1666                         mainFilter.AddFilter(contactFilter);
1667
1668                         count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContact>(mainFilter.Get());
1669                 }
1670                 break;
1671         case AB_FI_TYPE_CATEGORY:
1672                 {
1673                         unique_ptr< __Filter<__ContactsGroup> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsGroup>());
1674
1675                         __Filter<__ContactsGroup> groupFilter;
1676                         groupFilter.Construct(filterHandle);
1677
1678                         __Filter<__ContactsGroup> mainFilter;
1679                         mainFilter.Construct();
1680                         mainFilter.AddFilter(*pRwAbFilter);
1681                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1682                         mainFilter.AddFilter(groupFilter);
1683
1684                         count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsGroup>(mainFilter.Get());
1685                 }
1686                 break;
1687         case AB_FI_TYPE_PHONE_CONTACT:
1688                 {
1689                         unique_ptr< __Filter<__ContactsContactNumber> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactNumber>());
1690
1691                         __Filter<__ContactsContactNumber> numberFilter;
1692                         numberFilter.Construct(filterHandle);
1693
1694                         __Filter<__ContactsContactNumber> mainFilter;
1695                         mainFilter.Construct();
1696                         mainFilter.AddFilter(*pRwAbFilter);
1697                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1698                         mainFilter.AddFilter(numberFilter);
1699
1700                         count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContactNumber>(mainFilter.Get());
1701                 }
1702                 break;
1703         case AB_FI_TYPE_EMAIL_CONTACT:
1704                 {
1705                         unique_ptr< __Filter<__ContactsContactEmail> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactEmail>());
1706
1707                         __Filter<__ContactsContactEmail> emailFilter;
1708                         emailFilter.Construct(filterHandle);
1709
1710                         __Filter<__ContactsContactEmail> mainFilter;
1711                         mainFilter.Construct();
1712                         mainFilter.AddFilter(*pRwAbFilter);
1713                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1714                         mainFilter.AddFilter(emailFilter);
1715
1716                         count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContactEmail>(mainFilter.Get());
1717                 }
1718                 break;
1719         default:
1720                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the filter is invalid", GetErrorMessage(GetLastResult()));
1721                 count = 0;
1722         };
1723
1724         return count;
1725 }
1726
1727 bool
1728 _AddressbookManagerImpl::OnEachContact(contacts_record_h recordHandle, void* pUserData)
1729 {
1730         IList* pList = static_cast<IList*>(pUserData);
1731
1732         ClearLastResult();
1733
1734         unique_ptr<Contact> pContact(new (std::nothrow) Contact());
1735         SysTryReturn(NID_SCL, pContact != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1736
1737         contacts_record_h newRecordHandle = null;
1738
1739         contacts_record_clone(recordHandle, &newRecordHandle);
1740         SysTryReturn(NID_SCL, newRecordHandle != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1741
1742         _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(newRecordHandle);
1743
1744         result r = pList->Add(*pContact);
1745         SysTryReturn(NID_SCL, !IsFailed(r), false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1746
1747         pContact.release();
1748
1749         return true;    
1750 }
1751
1752 IList*
1753 _AddressbookManagerImpl::ParseContactsFromVcardN(const Tizen::Base::String& vcardPath)
1754 {
1755         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1756
1757         ClearLastResult();
1758
1759         File file;
1760         result r = file.Construct(vcardPath, "r");
1761         SysTryReturn(NID_SCL, r != E_INVALID_ARG, null, E_INVALID_ARG, "[%s] Invalid argument is used..", GetErrorMessage(E_INVALID_ARG));
1762         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));
1763         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));
1764         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1765
1766         unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
1767
1768         unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(vcardPath));
1769
1770         int ret = contacts_vcard_parse_to_contact_foreach(pCharArray.get(), OnEachContact, pList.get());
1771         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1772         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1773         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1774
1775         return pList.release();
1776 }
1777
1778 result
1779 _AddressbookManagerImpl::ExportPersonToVcard(const Person& person, const Tizen::Base::String& vcardPath)
1780 {
1781         bool exist = File::IsFileExist(vcardPath);
1782         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
1783         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
1784         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1785
1786         File file;
1787         result r = file.Construct(vcardPath, "w");
1788         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));
1789         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1790         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1791
1792         unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, person.GetId()));
1793         SysTryReturn(NID_SCL, pPersonRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1794
1795         char* pVcardStream = null;
1796         int ret = contacts_vcard_make_from_person(pPersonRecord.get(), &pVcardStream);
1797         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));
1798         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1799
1800         r = file.Write(pVcardStream, strlen(pVcardStream));
1801         free(pVcardStream);
1802         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1803         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1804
1805         return E_SUCCESS;
1806 }
1807
1808 result
1809 _AddressbookManagerImpl::ExportPersonsToVcard(const Tizen::Base::Collection::IList& personList, const Tizen::Base::String& vcardPath)
1810 {
1811         bool exist = File::IsFileExist(vcardPath);
1812         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
1813         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
1814         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1815
1816         int ret = CONTACTS_ERROR_NONE;
1817         char* pVcardStream = null;
1818         File file;
1819
1820         result r = file.Construct(vcardPath, "w");
1821         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));
1822         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1823         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1824
1825
1826         unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(null);
1827
1828         unique_ptr<IEnumerator> pEnum(personList.GetEnumeratorN());
1829         SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1830
1831         while (pEnum->MoveNext() == E_SUCCESS)
1832         {
1833                 Person* pPerson = static_cast<Person*>(pEnum->GetCurrent());
1834
1835                 pPersonRecord.reset(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, pPerson->GetId()));
1836                 SysTryReturn(NID_SCL, pPersonRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1837
1838                 ret = contacts_vcard_make_from_person(pPersonRecord.get(), &pVcardStream);
1839                 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));
1840                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1841
1842                 r = file.Write(pVcardStream, strlen(pVcardStream));
1843                 free(pVcardStream);
1844                 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1845                 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1846         }
1847
1848         return E_SUCCESS;
1849 }
1850
1851 result
1852 _AddressbookManagerImpl::ExportContactToVcard(const Contact& contact, const Tizen::Base::String& vcardPath)
1853 {
1854         bool exist = File::IsFileExist(vcardPath);
1855         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
1856         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
1857         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1858
1859         File file;
1860         result r = file.Construct(vcardPath, "w");
1861         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));
1862         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1863         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1864
1865         contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
1866
1867         char* pVcardStream = null;
1868
1869         int ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
1870         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));
1871         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1872
1873         r = file.Write(pVcardStream, strlen(pVcardStream));
1874         free(pVcardStream);
1875         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1876         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1877
1878         return E_SUCCESS;
1879 }
1880
1881 result
1882 _AddressbookManagerImpl::ExportContactsToVcard(const Tizen::Base::Collection::IList& contactList, const Tizen::Base::String& vcardPath)
1883 {
1884         bool exist = File::IsFileExist(vcardPath);
1885         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
1886         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
1887         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1888
1889         int ret = CONTACTS_ERROR_NONE;
1890         char* pVcardStream = null;
1891         File file;
1892         Contact* pContact = null;
1893
1894         result r = file.Construct(vcardPath, "w");
1895         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));
1896         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1897         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1898
1899         contacts_record_h recordHandle = null;
1900
1901         unique_ptr<IEnumerator> pEnum(contactList.GetEnumeratorN());
1902         SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1903
1904         while (pEnum->MoveNext() == E_SUCCESS)
1905         {
1906                 pContact = static_cast<Contact*>(pEnum->GetCurrent());
1907
1908                 recordHandle = _ContactImpl::GetInstance(*pContact)->GetContactRecordHandle();
1909
1910                 ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
1911                 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));
1912                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1913
1914                 r = file.Write(pVcardStream, strlen(pVcardStream));
1915                 free(pVcardStream);
1916                 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1917                 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1918         }
1919
1920         return E_SUCCESS;
1921 }
1922
1923 ByteBuffer*
1924 _AddressbookManagerImpl::ExportContactToVcardStreamN(const Contact& contact)
1925 {
1926         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1927
1928         ClearLastResult();
1929
1930         contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
1931
1932         char* pVcardStream = null;
1933         int ret = CONTACTS_ERROR_NONE;
1934
1935         ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
1936         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1937         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1938         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1939
1940         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
1941         if (pByteBuffer == null)
1942         {
1943                 free(pVcardStream);
1944                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1945
1946                 return null;
1947         }
1948
1949         result r = pByteBuffer->Construct(strlen(pVcardStream));
1950         if (IsFailed(r))
1951         {
1952                 free(pVcardStream);
1953                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1954
1955                 return null;
1956         }
1957
1958         r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
1959         free(pVcardStream);
1960         SysTryReturn(NID_SCL, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1961         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1962
1963         return pByteBuffer.release();
1964 }
1965
1966 ByteBuffer*
1967 _AddressbookManagerImpl::ExportContactsToVcardStreamN(const Tizen::Base::Collection::IList& contactList)
1968 {
1969         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1970
1971         ClearLastResult();
1972
1973         char* pVcardStream = null;
1974         int ret = CONTACTS_ERROR_NONE;
1975         Contact* pContact = null;
1976         result r = E_SUCCESS;
1977         int capacity = 0;
1978
1979         contacts_record_h recordHandle = null;
1980
1981         unique_ptr<IEnumerator> pEnum(contactList.GetEnumeratorN());
1982         SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1983
1984         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
1985         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1986
1987         r = pByteBuffer->Construct(capacity);
1988         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1989
1990         if (contactList.GetCount() == 0)
1991         {
1992                 return pByteBuffer.release();
1993         }
1994
1995         while (pEnum->MoveNext() == E_SUCCESS)
1996         {
1997                 pContact = static_cast<Contact*>(pEnum->GetCurrent());
1998
1999                 recordHandle = _ContactImpl::GetInstance(*pContact)->GetContactRecordHandle();
2000
2001                 ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
2002                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2003                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2004                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2005
2006                 capacity += strlen(pVcardStream);
2007                 r = pByteBuffer->ExpandCapacity(capacity);
2008                 if (IsFailed(r))
2009                 {
2010                         free(pVcardStream);
2011                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2012
2013                         return null;
2014                 }
2015
2016                 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2017                 free(pVcardStream);
2018                 pVcardStream = null;
2019                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.: capacity(%d), size(%d)", GetErrorMessage(E_SYSTEM));
2020         }
2021
2022         return pByteBuffer.release();
2023 }
2024
2025 ByteBuffer*
2026 _AddressbookManagerImpl::ExportPersonToVcardStreamN(const Person& person)
2027 {
2028         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2029
2030         ClearLastResult();
2031
2032         unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, person.GetId()));
2033         SysTryReturn(NID_SCL, GetLastResult() != E_OBJ_NOT_FOUND,  null, E_INVALID_ARG, "[%s] Person does not exist.", GetErrorMessage(E_INVALID_ARG));
2034         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS,  null, E_INVALID_ARG, "[%s] Person does not exist.", GetErrorMessage(E_INVALID_ARG));
2035
2036         char* pVcardStream = null;
2037         int ret = contacts_vcard_make_from_person(pPersonRecord.get(), &pVcardStream);
2038         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2039         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2040         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2041
2042         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2043         if (pByteBuffer == null)
2044         {
2045                 free(pVcardStream);
2046                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2047
2048                 return null;
2049         }
2050
2051         result r = pByteBuffer->Construct(strlen(pVcardStream));
2052         if (IsFailed(r))
2053         {
2054                 free(pVcardStream);
2055                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2056
2057                 return null;
2058         }
2059
2060         r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2061         free(pVcardStream);
2062         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2063
2064         return pByteBuffer.release();
2065 }
2066
2067 ByteBuffer*
2068 _AddressbookManagerImpl::ExportPersonsToVcardStreamN(const Tizen::Base::Collection::IList& personList)
2069 {
2070         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2071
2072         ClearLastResult();
2073
2074         int ret = CONTACTS_ERROR_NONE;
2075         Person* pPerson = null;
2076         char* pVcardStream = null;
2077
2078         int capacity = 0;
2079
2080         unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(null);
2081
2082         unique_ptr<IEnumerator> pEnum(personList.GetEnumeratorN());
2083         SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2084
2085         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2086         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2087
2088         result r = pByteBuffer->Construct(capacity);
2089         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2090
2091         if (personList.GetCount() == 0)
2092         {
2093                 return pByteBuffer.release();
2094         }
2095
2096         while (pEnum->MoveNext() == E_SUCCESS)
2097         {
2098                 pPerson = static_cast<Person*>(pEnum->GetCurrent());
2099
2100                 pPersonRecord.reset(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, pPerson->GetId()));
2101                 SysTryReturn(NID_SCL, GetLastResult() != E_OBJ_NOT_FOUND,  null, E_INVALID_ARG, "[%s] Person does not exist.", GetErrorMessage(E_INVALID_ARG));
2102                 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS,  null, E_INVALID_ARG, "[%s] Person does not exist.", GetErrorMessage(E_INVALID_ARG));
2103
2104                 ret = contacts_vcard_make_from_person(pPersonRecord.get(), &pVcardStream);
2105                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2106                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2107                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2108
2109                 capacity += strlen(pVcardStream);
2110                 r = pByteBuffer->ExpandCapacity(capacity);
2111                 if (IsFailed(r))
2112                 {
2113                         free(pVcardStream);
2114                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2115
2116                         return null;
2117                 }
2118
2119                 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2120                 free(pVcardStream);
2121                 pVcardStream = null;
2122                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2123         }
2124
2125         return pByteBuffer.release();
2126 }
2127
2128 IList*
2129 _AddressbookManagerImpl::ParseVcardStreamN(const Tizen::Base::ByteBuffer& vcardStream)
2130 {
2131         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2132
2133         ClearLastResult();
2134
2135         contacts_list_h listHandle = null;
2136         result r = E_SUCCESS;
2137
2138         int ret = contacts_vcard_parse_to_contacts(reinterpret_cast<char*>(const_cast<byte*>(vcardStream.GetPointer())), &listHandle);
2139         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2140         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2141
2142         unsigned int count = 0;
2143         contacts_record_h recordHandle = null;
2144         contacts_list_get_count(listHandle, &count);
2145
2146         unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
2147         if (pList == null)
2148         {
2149                 contacts_list_destroy(listHandle, true);
2150                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2151
2152                 return null;
2153         }
2154
2155         r = pList->Construct(count);
2156         if (IsFailed(r))
2157         {
2158                 contacts_list_destroy(listHandle, true);
2159                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2160
2161                 return null;
2162         }
2163
2164         for (unsigned int i = 0; i < count; i++)
2165         {
2166                 unique_ptr<Contact> pContact(new (std::nothrow) Contact());
2167                 if (pContact == null)
2168                 {
2169                         contacts_list_destroy(listHandle, true);
2170                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2171
2172                         return null;
2173                 }
2174
2175                 r = pList->Add(pContact.get());
2176                 if (IsFailed(r))
2177                 {
2178                         contacts_list_destroy(listHandle, true);
2179                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2180
2181                         return null;
2182                 }
2183
2184                 pContact.release();
2185         }
2186
2187         unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
2188
2189         while (pEnum->MoveNext() == E_SUCCESS)
2190         {
2191                 Contact* pContact = static_cast <Contact*> (pEnum->GetCurrent());
2192
2193                 contacts_list_get_current_record_p(listHandle, &recordHandle);
2194                 _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(recordHandle);
2195
2196                 ret = contacts_list_next(listHandle);
2197         }
2198
2199         contacts_list_destroy(listHandle, false);
2200
2201         return pList.release();
2202 }
2203
2204 ByteBuffer*
2205 _AddressbookManagerImpl::ExportUserProfileToVcardStreamN(const UserProfile& userProfile)
2206 {
2207         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2208
2209         ClearLastResult();
2210
2211         char* pVcardStream = null;
2212         int ret = CONTACTS_ERROR_NONE;
2213         contacts_record_h recordHandle = null;
2214
2215         recordHandle = _UserProfileImpl::GetInstance(userProfile)->GetUserProfileHandle();
2216
2217         ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2218         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2219         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2220         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2221
2222         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2223         if (pByteBuffer == null)
2224         {
2225                 free(pVcardStream);
2226                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2227
2228                 return null;
2229         }
2230         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2231
2232         result r = pByteBuffer->Construct(strlen(pVcardStream));
2233         if (IsFailed(r))
2234         {
2235                 free(pVcardStream);
2236                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2237
2238                 return null;
2239         }
2240
2241         r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2242         free(pVcardStream);
2243         SysTryReturn(NID_SCL, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2244         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2245
2246         return pByteBuffer.release();
2247 }
2248
2249 ByteBuffer*
2250 _AddressbookManagerImpl::ExportUserProfilesToVcardStreamN(const Tizen::Base::Collection::IList& userProfileList)
2251 {
2252         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2253
2254         ClearLastResult();
2255
2256         char* pVcardStream = null;
2257         int ret = CONTACTS_ERROR_NONE;
2258         UserProfile* pProfile = null;
2259         result r = E_SUCCESS;
2260         int capacity = 0;
2261
2262         unique_ptr<IEnumerator> pEnum(userProfileList.GetEnumeratorN());
2263         SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2264
2265         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2266         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2267
2268         r = pByteBuffer->Construct(capacity);
2269         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2270
2271         if (userProfileList.GetCount() == 0)
2272         {
2273                 return pByteBuffer.release();
2274         }
2275
2276         while (pEnum->MoveNext() == E_SUCCESS)
2277         {
2278                 contacts_record_h recordHandle = null;
2279
2280                 pProfile = static_cast<UserProfile*>(pEnum->GetCurrent());
2281
2282                 recordHandle = _UserProfileImpl::GetInstance(*pProfile)->GetUserProfileHandle();
2283
2284                 ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2285                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2286                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2287                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2288
2289                 capacity += strlen(pVcardStream);
2290                 r = pByteBuffer->ExpandCapacity(capacity);
2291                 if (IsFailed(r))
2292                 {
2293                         free(pVcardStream);
2294                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2295
2296                         return null;
2297                 }
2298
2299                 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2300                 free(pVcardStream);
2301                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.: capacity(%d), size(%d)", GetErrorMessage(E_SYSTEM));
2302         }
2303
2304         return pByteBuffer.release();
2305 }
2306
2307 result
2308 _AddressbookManagerImpl::ExportUserProfileToVcard(const UserProfile& userProfile, const Tizen::Base::String& vcardPath)
2309 {
2310         bool exist = File::IsFileExist(vcardPath);
2311         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2312         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2313         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2314
2315         File file;
2316         result r = file.Construct(vcardPath, "w");
2317         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));
2318         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2319         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2320
2321         char* pVcardStream = null;
2322         contacts_record_h recordHandle = null;
2323
2324         recordHandle = _UserProfileImpl::GetInstance(userProfile)->GetUserProfileHandle();
2325
2326         int ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2327         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));
2328         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2329
2330         r = file.Write(pVcardStream, strlen(pVcardStream));
2331         free(pVcardStream);
2332         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2333         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2334
2335         return E_SUCCESS;
2336 }
2337
2338 result
2339 _AddressbookManagerImpl::ExportUserProfilesToVcard(const Tizen::Base::Collection::IList& userProfileList, const Tizen::Base::String& vcardPath)
2340 {
2341         bool exist = File::IsFileExist(vcardPath);
2342         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2343         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2344         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2345
2346         int ret = CONTACTS_ERROR_NONE;
2347         char* pVcardStream = null;
2348         File file;
2349         UserProfile* pProfile = null;
2350
2351         result r = file.Construct(vcardPath, "w");
2352         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));
2353         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2354         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2355
2356         unique_ptr<IEnumerator> pEnum(userProfileList.GetEnumeratorN());
2357         SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2358
2359         while (pEnum->MoveNext() == E_SUCCESS)
2360         {
2361                 contacts_record_h recordHandle = null;
2362
2363                 pProfile = static_cast<UserProfile*>(pEnum->GetCurrent());
2364
2365                 recordHandle = _UserProfileImpl::GetInstance(*pProfile)->GetUserProfileHandle();
2366
2367                 ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2368                 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));
2369                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2370
2371                 r = file.Write(pVcardStream, strlen(pVcardStream));
2372                 free(pVcardStream);
2373                 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2374                 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2375         }
2376
2377         return E_SUCCESS;
2378 }
2379
2380 IList*
2381 _AddressbookManagerImpl::GetAllUserProfilesN(void) const
2382 {
2383         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2384
2385         ClearLastResult();
2386
2387         unique_ptr< __Filter<__ContactsUserProfile> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsUserProfile>());
2388         SysTryReturn(NID_SCL, pRwAbFilter != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2389
2390         __Query<__ContactsUserProfile> query2;
2391         query2.Construct();
2392         query2.SetFilter(*pRwAbFilter);
2393         query2.SetSort(_contacts_my_profile.display_name, true);
2394
2395         IList* pUserProfilesList = _AddressbookUtil::SearchWithQueryN<__ContactsUserProfile, UserProfile>(query2);
2396         SysTryReturn(NID_SCL, pUserProfilesList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2397
2398         return pUserProfilesList;
2399 }
2400
2401 UserProfile*
2402 _AddressbookManagerImpl::GetUserProfileN(AddressbookId addressbookId) const
2403 {
2404         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2405         SysTryReturn(NID_SCL, addressbookId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. Addressbook Id(%d).", GetErrorMessage(E_INVALID_ARG), addressbookId);
2406
2407         ClearLastResult();
2408
2409         int mode = 0;
2410         unique_ptr<ContactRecord, ContactRecordDeleter> pAbRecord(_AddressbookUtil::GetContactRecordN(_contacts_address_book._uri, addressbookId));
2411         SysTryReturn(NID_SCL, pAbRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2412
2413         contacts_record_get_int(pAbRecord.get(), _contacts_address_book.mode, &mode);
2414         SysTryReturn(NID_SCL, mode == 0, null, E_OBJ_NOT_FOUND, "[%s] The addressbook does not exist.", GetErrorMessage(E_OBJ_NOT_FOUND));
2415
2416         __Filter<__ContactsUserProfile> filter;
2417         filter.Construct();
2418         filter.AddInt(_contacts_my_profile.address_book_id, CONTACTS_MATCH_EQUAL, addressbookId);
2419
2420         __Query<__ContactsUserProfile> query;
2421         query.Construct();
2422         query.SetFilter(filter);
2423
2424         unique_ptr<IList, AllElementsDeleter> pUserProfilesList(_AddressbookUtil::SearchWithQueryN<__ContactsUserProfile, UserProfile>(query));
2425         SysTryReturn(NID_SCL, pUserProfilesList.get() != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2426         SysTryReturn(NID_SCL, pUserProfilesList->GetCount() != 0, null, E_SUCCESS, "No UserProfile Set for this Addressbook.");
2427         SysTryReturn(NID_SCL, pUserProfilesList->GetCount() == 1, null, E_SYSTEM, "[%s] Propagating. More than one UserProfile not allowed.", GetErrorMessage(E_SYSTEM));
2428
2429         UserProfile* pProfile = new (std::nothrow) UserProfile(*(static_cast<UserProfile*>(pUserProfilesList->GetAt(0))));
2430         SysTryReturn(NID_SCL, pProfile != null, null, E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(E_OUT_OF_MEMORY));
2431
2432         return pProfile;
2433 }
2434
2435 _AddressbookManagerImpl*
2436 _AddressbookManagerImpl::GetInstance(AddressbookManager& addressbookManager)
2437 {
2438         return addressbookManager.__pAddressbookManagerImpl;
2439 }
2440
2441 const _AddressbookManagerImpl*
2442 _AddressbookManagerImpl::GetInstance(const AddressbookManager& addressbookManager)
2443 {
2444         return addressbookManager.__pAddressbookManagerImpl;
2445 }
2446
2447 }}  // Tizen::Social