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