9d61c18604fce69c99a8caa41fb87dab3936f419
[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                                 mainFilter.AddFilter(*pRwAbFilter);
1591                                 if (abFilter.Get() != null)
1592                                 {
1593                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1594                                         mainFilter.AddFilter(abFilter);
1595                                 }
1596
1597                                 __Query<__ContactsAddressbook> query;
1598                                 query.Construct();
1599                                 query.SetFilter(mainFilter);
1600
1601                                 if (viewSortPropertyId != 0)
1602                                 {
1603                                         query.SetSort(viewSortPropertyId, ascending);
1604                                 }
1605
1606                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsAddressbook, Addressbook>(query, offset, maxCount);
1607                         }
1608                         else
1609                         {
1610                                 __Query<__ContactsAddressbook> query;
1611                                 query.Construct();
1612                                 query.SetFilter(abFilter);
1613
1614                                 if (viewSortPropertyId != 0)
1615                                 {
1616                                         query.SetSort(viewSortPropertyId, ascending);
1617                                 }
1618
1619                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsAddressbook, Addressbook>(query, offset, maxCount);
1620
1621                         }
1622                 }
1623                 break;
1624         case AB_FI_TYPE_PERSON:
1625                 {
1626                         unique_ptr< __Filter<__ContactsPersonGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsPersonGroupRel>());
1627
1628                         __Filter<__ContactsPersonGroupRel> personFilter;
1629                         personFilter.Construct(filterHandle);
1630
1631                         unsigned int propertyIds[] =
1632                         { _contacts_person_grouprel.person_id,
1633                                 _contacts_person_grouprel.addressbook_ids,
1634                                 _contacts_person_grouprel.is_favorite,
1635                                 _contacts_person_grouprel.has_phonenumber,
1636                                 _contacts_person_grouprel.has_email,
1637                                 _contacts_person_grouprel.image_thumbnail_path,
1638                                 _contacts_person_grouprel.ringtone_path,
1639                                 _contacts_person_grouprel.display_name
1640                         };
1641
1642                         if (pRwAbFilter->Get() != null)
1643                         {
1644                                 __Filter<__ContactsPersonGroupRel> mainFilter;
1645                                 mainFilter.Construct();
1646                                 mainFilter.AddFilter(*pRwAbFilter);
1647                                 if (personFilter.Get() != null)
1648                                 {
1649                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1650                                         mainFilter.AddFilter(personFilter);
1651                                 }
1652
1653                                 __Query<__ContactsPersonGroupRel> query;
1654                                 query.Construct();
1655                                 query.SetFilter(mainFilter);
1656                                 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1657                                 query.SetDistinct(true);
1658
1659                                 if (viewSortPropertyId != 0)
1660                                 {
1661                                         query.SetSort(viewSortPropertyId, ascending);
1662                                 }
1663
1664                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query, offset, maxCount);
1665                         }
1666                         else
1667                         {
1668                                 __Query<__ContactsPersonGroupRel> query;
1669                                 query.Construct();
1670                                 query.SetFilter(personFilter);
1671                                 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1672                                 query.SetDistinct(true);
1673
1674                                 if (viewSortPropertyId != 0)
1675                                 {
1676                                         query.SetSort(viewSortPropertyId, ascending);
1677                                 }
1678
1679                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query, offset, maxCount);
1680                         }
1681                 }
1682                 break;
1683         case AB_FI_TYPE_CONTACT:
1684                 {
1685                         unique_ptr< __Filter<__ContactsContact> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContact>());
1686
1687                         __Filter<__ContactsContact> contactFilter;
1688                         contactFilter.Construct(filterHandle);
1689
1690                         if (pRwAbFilter->Get() != null)
1691                         {
1692                                 __Filter<__ContactsContact> mainFilter;
1693                                 mainFilter.Construct();
1694                                 mainFilter.AddFilter(*pRwAbFilter);
1695                                 if (contactFilter.Get() != null)
1696                                 {
1697                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1698                                         mainFilter.AddFilter(contactFilter);
1699                                 }
1700
1701                                 __Query<__ContactsContact> query;
1702                                 query.Construct();
1703                                 query.SetFilter(mainFilter);
1704
1705                                 if (viewSortPropertyId != 0)
1706                                 {
1707                                         query.SetSort(viewSortPropertyId, ascending);
1708                                 }
1709
1710                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query, offset, maxCount);
1711                         }
1712                         else
1713                         {
1714                                 __Query<__ContactsContact> query;
1715                                 query.Construct();
1716                                 query.SetFilter(contactFilter);
1717
1718                                 if (viewSortPropertyId != 0)
1719                                 {
1720                                         query.SetSort(viewSortPropertyId, ascending);
1721                                 }
1722
1723                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query, offset, maxCount);
1724
1725                         }
1726                 }
1727                 break;
1728         case AB_FI_TYPE_CATEGORY:
1729                 {
1730                         unique_ptr< __Filter<__ContactsGroup> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsGroup>());
1731
1732                         __Filter<__ContactsGroup> groupFilter;
1733                         groupFilter.Construct(filterHandle);
1734
1735                         if (pRwAbFilter->Get() != null)
1736                         {
1737                                 __Filter<__ContactsGroup> mainFilter;
1738                                 mainFilter.Construct();
1739                                 mainFilter.AddFilter(*pRwAbFilter);
1740                                 if (groupFilter.Get() != null)
1741                                 {
1742                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1743                                         mainFilter.AddFilter(groupFilter);
1744                                 }
1745
1746                                 __Query<__ContactsGroup> query;
1747                                 query.Construct();
1748                                 query.SetFilter(mainFilter);
1749
1750                                 if (viewSortPropertyId != 0)
1751                                 {
1752                                         query.SetSort(viewSortPropertyId, ascending);
1753                                 }
1754
1755                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsGroup, Category>(query, offset, maxCount);
1756                         }
1757                         else
1758                         {
1759                                 __Query<__ContactsGroup> query;
1760                                 query.Construct();
1761                                 query.SetFilter(groupFilter);
1762
1763                                 if (viewSortPropertyId != 0)
1764                                 {
1765                                         query.SetSort(viewSortPropertyId, ascending);
1766                                 }
1767
1768                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsGroup, Category>(query, offset, maxCount);
1769                         }
1770                 }
1771                 break;
1772         case AB_FI_TYPE_PHONE_CONTACT:
1773                 {
1774                         unique_ptr< __Filter<__ContactsContactNumber> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactNumber>());
1775
1776                         __Filter<__ContactsContactNumber> numberFilter;
1777                         numberFilter.Construct(filterHandle);
1778
1779                         if (pRwAbFilter->Get() != null)
1780                         {
1781                                 __Filter<__ContactsContactNumber> mainFilter;
1782                                 mainFilter.Construct();
1783                                 mainFilter.AddFilter(*pRwAbFilter);
1784                                 if (numberFilter.Get() != null)
1785                                 {
1786                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1787                                         mainFilter.AddFilter(numberFilter);
1788                                 }
1789
1790                                 __Query<__ContactsContactNumber> query;
1791                                 query.Construct();
1792                                 query.SetFilter(mainFilter);
1793
1794                                 if (viewSortPropertyId != 0)
1795                                 {
1796                                         query.SetSort(viewSortPropertyId, ascending);
1797                                 }
1798
1799                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContactNumber, PhoneNumberContact>(query, offset, maxCount);
1800                         }
1801                         else
1802                         {
1803                                 __Query<__ContactsContactNumber> query;
1804                                 query.Construct();
1805                                 query.SetFilter(numberFilter);
1806
1807                                 if (viewSortPropertyId != 0)
1808                                 {
1809                                         query.SetSort(viewSortPropertyId, ascending);
1810                                 }
1811
1812                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContactNumber, PhoneNumberContact>(query, offset, maxCount);
1813                         }
1814                 }
1815                 break;
1816         case AB_FI_TYPE_EMAIL_CONTACT:
1817                 {
1818                         unique_ptr< __Filter<__ContactsContactEmail> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactEmail>());
1819
1820                         __Filter<__ContactsContactEmail> emailFilter;
1821                         emailFilter.Construct(filterHandle);
1822
1823                         if (pRwAbFilter->Get() != null)
1824                         {
1825                                 __Filter<__ContactsContactEmail> mainFilter;
1826                                 mainFilter.Construct();
1827                                 mainFilter.AddFilter(*pRwAbFilter);
1828                                 if (emailFilter.Get() != null)
1829                                 {
1830                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1831                                         mainFilter.AddFilter(emailFilter);
1832                                 }
1833
1834                                 __Query<__ContactsContactEmail> query;
1835                                 query.Construct();
1836                                 query.SetFilter(mainFilter);
1837
1838                                 if (viewSortPropertyId != 0)
1839                                 {
1840                                         query.SetSort(viewSortPropertyId, ascending);
1841                                 }
1842
1843                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContactEmail, EmailContact>(query, offset, maxCount);
1844                         }
1845                         else
1846                         {
1847                                 __Query<__ContactsContactEmail> query;
1848                                 query.Construct();
1849                                 query.SetFilter(emailFilter);
1850
1851                                 if (viewSortPropertyId != 0)
1852                                 {
1853                                         query.SetSort(viewSortPropertyId, ascending);
1854                                 }
1855
1856                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContactEmail, EmailContact>(query, offset, maxCount);
1857                         }
1858                 }
1859                 break;
1860         default:
1861                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. The filter type is invalid", GetErrorMessage(E_INVALID_ARG));
1862                 pList = null;
1863         };
1864
1865         return pList;
1866 }
1867
1868 int
1869 _AddressbookManagerImpl::GetMatchedItemCount(const AddressbookFilter& filter)
1870 {
1871         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1872
1873         ClearLastResult();
1874
1875         int count = 0;
1876         AddressbookFilterType type = _AddressbookFilterImpl::GetInstance(filter)->GetType();
1877         contacts_filter_h filterHandle = _AddressbookFilterImpl::GetInstance(filter)->GetFilterHandle();
1878
1879         switch(type)
1880         {
1881         case AB_FI_TYPE_ADDRESSBOOK:
1882                 {
1883                         unique_ptr< __Filter<__ContactsAddressbook> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsAddressbook>());
1884
1885                         __Filter<__ContactsAddressbook> abFilter;
1886                         abFilter.Construct(filterHandle);
1887
1888                         if (pRwAbFilter->Get() != null)
1889                         {
1890                                 __Filter<__ContactsAddressbook> mainFilter;
1891                                 mainFilter.Construct();
1892                                 mainFilter.AddFilter(*pRwAbFilter);
1893                                 if (abFilter.Get() != null)
1894                                 {
1895                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1896                                         mainFilter.AddFilter(abFilter);
1897                                 }
1898
1899                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsAddressbook>(mainFilter.Get());
1900                         }
1901                         else
1902                         {
1903                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsAddressbook>(abFilter.Get());
1904                         }
1905                 }
1906                 break;
1907         case AB_FI_TYPE_PERSON:
1908                 {
1909                         unique_ptr< __Filter<__ContactsPersonGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsPersonGroupRel>());
1910
1911                         __Filter<__ContactsPersonGroupRel> personFilter;
1912                         personFilter.Construct(filterHandle);
1913
1914                         if (pRwAbFilter->Get() != null)
1915                         {
1916                                 __Filter<__ContactsPersonGroupRel> mainFilter;
1917                                 mainFilter.Construct();
1918                                 mainFilter.AddFilter(*pRwAbFilter);
1919                                 if (personFilter.Get() != null)
1920                                 {
1921                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1922                                         mainFilter.AddFilter(personFilter);
1923                                 }
1924
1925                                 unsigned int propertyIds[] = { _contacts_person_grouprel.person_id };
1926
1927                                 __Query<__ContactsPersonGroupRel> query;
1928                                 query.Construct();
1929                                 query.SetFilter(mainFilter);
1930                                 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1931                                 query.SetDistinct(true);
1932
1933                                 count = _AddressbookUtil::GetCountWithQuery(query);
1934                         }
1935                         else
1936                         {
1937                                 unsigned int propertyIds[] = { _contacts_person_grouprel.person_id };
1938
1939                                 __Query<__ContactsPersonGroupRel> query;
1940                                 query.Construct();
1941                                 query.SetFilter(personFilter);
1942                                 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1943                                 query.SetDistinct(true);
1944
1945                                 count = _AddressbookUtil::GetCountWithQuery(query);
1946
1947                         }
1948                 }
1949
1950                 break;
1951         case AB_FI_TYPE_CONTACT:
1952                 {
1953                         unique_ptr< __Filter<__ContactsContact> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContact>());
1954
1955                         __Filter<__ContactsContact> contactFilter;
1956                         contactFilter.Construct(filterHandle);
1957
1958                         if (pRwAbFilter->Get() != null)
1959                         {
1960                                 __Filter<__ContactsContact> mainFilter;
1961                                 mainFilter.Construct();
1962                                 mainFilter.AddFilter(*pRwAbFilter);
1963                                 if (contactFilter.Get() != null)
1964                                 {
1965                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1966                                         mainFilter.AddFilter(contactFilter);
1967                                 }
1968
1969                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContact>(mainFilter.Get());
1970                         }
1971                         else
1972                         {
1973                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContact>(contactFilter.Get());
1974                         }
1975                 }
1976                 break;
1977         case AB_FI_TYPE_CATEGORY:
1978                 {
1979                         unique_ptr< __Filter<__ContactsGroup> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsGroup>());
1980
1981                         __Filter<__ContactsGroup> groupFilter;
1982                         groupFilter.Construct(filterHandle);
1983
1984                         if (pRwAbFilter->Get() != null)
1985                         {
1986                                 __Filter<__ContactsGroup> mainFilter;
1987                                 mainFilter.Construct();
1988                                 mainFilter.AddFilter(*pRwAbFilter);
1989                                 if (groupFilter.Get() != null)
1990                                 {
1991                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1992                                         mainFilter.AddFilter(groupFilter);
1993                                 }
1994
1995                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsGroup>(mainFilter.Get());
1996                         }
1997                         else
1998                         {
1999                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsGroup>(groupFilter.Get());
2000                         }
2001                 }
2002                 break;
2003         case AB_FI_TYPE_PHONE_CONTACT:
2004                 {
2005                         unique_ptr< __Filter<__ContactsContactNumber> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactNumber>());
2006
2007                         __Filter<__ContactsContactNumber> numberFilter;
2008                         numberFilter.Construct(filterHandle);
2009
2010                         if (pRwAbFilter->Get() != null)
2011                         {
2012                                 __Filter<__ContactsContactNumber> mainFilter;
2013                                 mainFilter.Construct();
2014                                 mainFilter.AddFilter(*pRwAbFilter);
2015                                 if (numberFilter.Get() != null)
2016                                 {
2017                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
2018                                         mainFilter.AddFilter(numberFilter);
2019                                 }
2020
2021                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContactNumber>(mainFilter.Get());
2022                         }
2023                         else
2024                         {
2025                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContactNumber>(numberFilter.Get());
2026                         }
2027                 }
2028                 break;
2029         case AB_FI_TYPE_EMAIL_CONTACT:
2030                 {
2031                         unique_ptr< __Filter<__ContactsContactEmail> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactEmail>());
2032
2033                         __Filter<__ContactsContactEmail> emailFilter;
2034                         emailFilter.Construct(filterHandle);
2035
2036                         if (pRwAbFilter->Get() != null)
2037                         {
2038                                 __Filter<__ContactsContactEmail> mainFilter;
2039                                 mainFilter.Construct();
2040                                 mainFilter.AddFilter(*pRwAbFilter);
2041                                 if (emailFilter.Get() != null)
2042                                 {
2043                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
2044                                         mainFilter.AddFilter(emailFilter);
2045                                 }
2046
2047                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContactEmail>(mainFilter.Get());
2048                         }
2049                         else
2050                         {
2051                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContactEmail>(emailFilter.Get());
2052                         }
2053                 }
2054                 break;
2055         default:
2056                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the filter is invalid", GetErrorMessage(GetLastResult()));
2057                 count = 0;
2058         };
2059
2060         return count;
2061 }
2062
2063 bool
2064 _AddressbookManagerImpl::OnEachContact(contacts_record_h recordHandle, void* pUserData)
2065 {
2066         IList* pList = static_cast<IList*>(pUserData);
2067
2068         ClearLastResult();
2069
2070         unique_ptr<Contact> pContact(new (std::nothrow) Contact());
2071         SysTryReturn(NID_SCL, pContact != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2072
2073         contacts_record_h newRecordHandle = null;
2074
2075         contacts_record_clone(recordHandle, &newRecordHandle);
2076         SysTryReturn(NID_SCL, newRecordHandle != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2077
2078         _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(newRecordHandle);
2079
2080         result r = pList->Add(*pContact);
2081         SysTryReturn(NID_SCL, !IsFailed(r), false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2082
2083         pContact.release();
2084
2085         return true;    
2086 }
2087
2088 IList*
2089 _AddressbookManagerImpl::ParseContactsFromVcardN(const Tizen::Base::String& vcardPath)
2090 {
2091         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2092
2093         ClearLastResult();
2094
2095         File file;
2096         result r = file.Construct(vcardPath, "r");
2097         SysTryReturn(NID_SCL, r != E_INVALID_ARG, null, E_INVALID_ARG, "[%s] Invalid argument is used..", GetErrorMessage(E_INVALID_ARG));
2098         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));
2099         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));
2100         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2101
2102         unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
2103
2104         unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(vcardPath));
2105
2106         int ret = contacts_vcard_parse_to_contact_foreach(pCharArray.get(), OnEachContact, pList.get());
2107         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2108         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2109         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2110
2111         return pList.release();
2112 }
2113
2114 result
2115 _AddressbookManagerImpl::ExportPersonToVcard(const Person& person, 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         File file;
2123         result r = file.Construct(vcardPath, "w");
2124         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));
2125         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2126         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2127
2128         unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, person.GetId()));
2129         SysTryReturn(NID_SCL, pPersonRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2130
2131         char* pVcardStream = null;
2132         int ret = contacts_vcard_make_from_person(pPersonRecord.get(), &pVcardStream);
2133         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));
2134         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2135
2136         r = file.Write(pVcardStream, strlen(pVcardStream));
2137         free(pVcardStream);
2138         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2139         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2140
2141         return E_SUCCESS;
2142 }
2143
2144 result
2145 _AddressbookManagerImpl::ExportPersonsToVcard(const Tizen::Base::Collection::IList& personList, const Tizen::Base::String& vcardPath)
2146 {
2147         bool exist = File::IsFileExist(vcardPath);
2148         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2149         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2150         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2151
2152         int ret = CONTACTS_ERROR_NONE;
2153         char* pVcardStream = null;
2154         File file;
2155
2156         result r = file.Construct(vcardPath, "w");
2157         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));
2158         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2159         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2160
2161
2162         unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(null);
2163
2164         unique_ptr<IEnumerator> pEnum(personList.GetEnumeratorN());
2165         SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2166
2167         while (pEnum->MoveNext() == E_SUCCESS)
2168         {
2169                 Person* pPerson = static_cast<Person*>(pEnum->GetCurrent());
2170
2171                 pPersonRecord.reset(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, pPerson->GetId()));
2172                 SysTryReturn(NID_SCL, pPersonRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2173
2174                 ret = contacts_vcard_make_from_person(pPersonRecord.get(), &pVcardStream);
2175                 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));
2176                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2177
2178                 r = file.Write(pVcardStream, strlen(pVcardStream));
2179                 free(pVcardStream);
2180                 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2181                 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2182         }
2183
2184         return E_SUCCESS;
2185 }
2186
2187 result
2188 _AddressbookManagerImpl::ExportContactToVcard(const Contact& contact, 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         File file;
2196         result r = file.Construct(vcardPath, "w");
2197         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));
2198         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2199         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2200
2201         contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
2202
2203         char* pVcardStream = null;
2204
2205         int ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
2206         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));
2207         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2208
2209         r = file.Write(pVcardStream, strlen(pVcardStream));
2210         free(pVcardStream);
2211         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2212         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2213
2214         return E_SUCCESS;
2215 }
2216
2217 result
2218 _AddressbookManagerImpl::ExportContactsToVcard(const Tizen::Base::Collection::IList& contactList, const Tizen::Base::String& vcardPath)
2219 {
2220         bool exist = File::IsFileExist(vcardPath);
2221         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2222         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2223         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2224
2225         int ret = CONTACTS_ERROR_NONE;
2226         char* pVcardStream = null;
2227         File file;
2228         Contact* pContact = null;
2229
2230         result r = file.Construct(vcardPath, "w");
2231         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));
2232         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2233         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2234
2235         contacts_record_h recordHandle = null;
2236
2237         unique_ptr<IEnumerator> pEnum(contactList.GetEnumeratorN());
2238         SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2239
2240         while (pEnum->MoveNext() == E_SUCCESS)
2241         {
2242                 pContact = static_cast<Contact*>(pEnum->GetCurrent());
2243
2244                 recordHandle = _ContactImpl::GetInstance(*pContact)->GetContactRecordHandle();
2245
2246                 ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
2247                 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));
2248                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2249
2250                 r = file.Write(pVcardStream, strlen(pVcardStream));
2251                 free(pVcardStream);
2252                 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2253                 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2254         }
2255
2256         return E_SUCCESS;
2257 }
2258
2259 ByteBuffer*
2260 _AddressbookManagerImpl::ExportContactToVcardStreamN(const Contact& contact)
2261 {
2262         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2263
2264         ClearLastResult();
2265
2266         contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
2267
2268         char* pVcardStream = null;
2269         int ret = CONTACTS_ERROR_NONE;
2270
2271         ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
2272         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2273         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2274         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2275
2276         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2277         if (pByteBuffer == null)
2278         {
2279                 free(pVcardStream);
2280                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2281
2282                 return null;
2283         }
2284
2285         result r = pByteBuffer->Construct(strlen(pVcardStream));
2286         if (IsFailed(r))
2287         {
2288                 free(pVcardStream);
2289                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2290
2291                 return null;
2292         }
2293
2294         r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2295         free(pVcardStream);
2296         SysTryReturn(NID_SCL, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2297         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2298
2299         return pByteBuffer.release();
2300 }
2301
2302 ByteBuffer*
2303 _AddressbookManagerImpl::ExportContactsToVcardStreamN(const Tizen::Base::Collection::IList& contactList)
2304 {
2305         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2306
2307         ClearLastResult();
2308
2309         char* pVcardStream = null;
2310         int ret = CONTACTS_ERROR_NONE;
2311         Contact* pContact = null;
2312         result r = E_SUCCESS;
2313         int capacity = 0;
2314
2315         contacts_record_h recordHandle = null;
2316
2317         unique_ptr<IEnumerator> pEnum(contactList.GetEnumeratorN());
2318         SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2319
2320         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2321         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2322
2323         r = pByteBuffer->Construct(capacity);
2324         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2325
2326         if (contactList.GetCount() == 0)
2327         {
2328                 return pByteBuffer.release();
2329         }
2330
2331         while (pEnum->MoveNext() == E_SUCCESS)
2332         {
2333                 pContact = static_cast<Contact*>(pEnum->GetCurrent());
2334
2335                 recordHandle = _ContactImpl::GetInstance(*pContact)->GetContactRecordHandle();
2336
2337                 ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
2338                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2339                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2340                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2341
2342                 capacity += strlen(pVcardStream);
2343                 r = pByteBuffer->ExpandCapacity(capacity);
2344                 if (IsFailed(r))
2345                 {
2346                         free(pVcardStream);
2347                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2348
2349                         return null;
2350                 }
2351
2352                 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2353                 free(pVcardStream);
2354                 pVcardStream = null;
2355                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.: capacity(%d), size(%d)", GetErrorMessage(E_SYSTEM));
2356         }
2357
2358         return pByteBuffer.release();
2359 }
2360
2361 ByteBuffer*
2362 _AddressbookManagerImpl::ExportPersonToVcardStreamN(const Person& person)
2363 {
2364         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2365
2366         ClearLastResult();
2367
2368         unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, person.GetId()));
2369         SysTryReturn(NID_SCL, GetLastResult() != E_OBJ_NOT_FOUND,  null, E_INVALID_ARG, "[%s] Person does not exist.", GetErrorMessage(E_INVALID_ARG));
2370         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS,  null, E_INVALID_ARG, "[%s] Person does not exist.", GetErrorMessage(E_INVALID_ARG));
2371
2372         char* pVcardStream = null;
2373         int ret = contacts_vcard_make_from_person(pPersonRecord.get(), &pVcardStream);
2374         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2375         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2376         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2377
2378         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2379         if (pByteBuffer == null)
2380         {
2381                 free(pVcardStream);
2382                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2383
2384                 return null;
2385         }
2386
2387         result r = pByteBuffer->Construct(strlen(pVcardStream));
2388         if (IsFailed(r))
2389         {
2390                 free(pVcardStream);
2391                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2392
2393                 return null;
2394         }
2395
2396         r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2397         free(pVcardStream);
2398         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2399
2400         return pByteBuffer.release();
2401 }
2402
2403 ByteBuffer*
2404 _AddressbookManagerImpl::ExportPersonsToVcardStreamN(const Tizen::Base::Collection::IList& personList)
2405 {
2406         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2407
2408         ClearLastResult();
2409
2410         int ret = CONTACTS_ERROR_NONE;
2411         Person* pPerson = null;
2412         char* pVcardStream = null;
2413
2414         int capacity = 0;
2415
2416         unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(null);
2417
2418         unique_ptr<IEnumerator> pEnum(personList.GetEnumeratorN());
2419         SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2420
2421         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2422         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2423
2424         result r = pByteBuffer->Construct(capacity);
2425         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2426
2427         if (personList.GetCount() == 0)
2428         {
2429                 return pByteBuffer.release();
2430         }
2431
2432         while (pEnum->MoveNext() == E_SUCCESS)
2433         {
2434                 pPerson = static_cast<Person*>(pEnum->GetCurrent());
2435
2436                 pPersonRecord.reset(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, pPerson->GetId()));
2437                 SysTryReturn(NID_SCL, GetLastResult() != E_OBJ_NOT_FOUND,  null, E_INVALID_ARG, "[%s] Person does not exist.", GetErrorMessage(E_INVALID_ARG));
2438                 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS,  null, E_INVALID_ARG, "[%s] Person does not exist.", GetErrorMessage(E_INVALID_ARG));
2439
2440                 ret = contacts_vcard_make_from_person(pPersonRecord.get(), &pVcardStream);
2441                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2442                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2443                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2444
2445                 capacity += strlen(pVcardStream);
2446                 r = pByteBuffer->ExpandCapacity(capacity);
2447                 if (IsFailed(r))
2448                 {
2449                         free(pVcardStream);
2450                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2451
2452                         return null;
2453                 }
2454
2455                 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2456                 free(pVcardStream);
2457                 pVcardStream = null;
2458                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2459         }
2460
2461         return pByteBuffer.release();
2462 }
2463
2464 IList*
2465 _AddressbookManagerImpl::ParseVcardStreamN(const Tizen::Base::ByteBuffer& vcardStream)
2466 {
2467         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2468
2469         ClearLastResult();
2470
2471         contacts_list_h listHandle = null;
2472         result r = E_SUCCESS;
2473
2474         int ret = contacts_vcard_parse_to_contacts(reinterpret_cast<char*>(const_cast<byte*>(vcardStream.GetPointer())), &listHandle);
2475         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2476         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2477
2478         unsigned int count = 0;
2479         contacts_record_h recordHandle = null;
2480         contacts_list_get_count(listHandle, &count);
2481
2482         unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
2483         if (pList == null)
2484         {
2485                 contacts_list_destroy(listHandle, true);
2486                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2487
2488                 return null;
2489         }
2490
2491         r = pList->Construct(count);
2492         if (IsFailed(r))
2493         {
2494                 contacts_list_destroy(listHandle, true);
2495                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2496
2497                 return null;
2498         }
2499
2500         for (unsigned int i = 0; i < count; i++)
2501         {
2502                 unique_ptr<Contact> pContact(new (std::nothrow) Contact());
2503                 if (pContact == null)
2504                 {
2505                         contacts_list_destroy(listHandle, true);
2506                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2507
2508                         return null;
2509                 }
2510
2511                 r = pList->Add(pContact.get());
2512                 if (IsFailed(r))
2513                 {
2514                         contacts_list_destroy(listHandle, true);
2515                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2516
2517                         return null;
2518                 }
2519
2520                 pContact.release();
2521         }
2522
2523         unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
2524
2525         while (pEnum->MoveNext() == E_SUCCESS)
2526         {
2527                 Contact* pContact = static_cast <Contact*> (pEnum->GetCurrent());
2528
2529                 contacts_list_get_current_record_p(listHandle, &recordHandle);
2530                 _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(recordHandle);
2531
2532                 ret = contacts_list_next(listHandle);
2533         }
2534
2535         contacts_list_destroy(listHandle, false);
2536
2537         return pList.release();
2538 }
2539
2540 ByteBuffer*
2541 _AddressbookManagerImpl::ExportUserProfileToVcardStreamN(const UserProfile& userProfile)
2542 {
2543         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2544
2545         ClearLastResult();
2546
2547         char* pVcardStream = null;
2548         int ret = CONTACTS_ERROR_NONE;
2549         contacts_record_h recordHandle = null;
2550
2551         recordHandle = _UserProfileImpl::GetInstance(userProfile)->GetUserProfileHandle();
2552
2553         ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2554         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2555         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2556         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2557
2558         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2559         if (pByteBuffer == null)
2560         {
2561                 free(pVcardStream);
2562                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2563
2564                 return null;
2565         }
2566         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2567
2568         result r = pByteBuffer->Construct(strlen(pVcardStream));
2569         if (IsFailed(r))
2570         {
2571                 free(pVcardStream);
2572                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2573
2574                 return null;
2575         }
2576
2577         r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2578         free(pVcardStream);
2579         SysTryReturn(NID_SCL, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2580         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2581
2582         return pByteBuffer.release();
2583 }
2584
2585 ByteBuffer*
2586 _AddressbookManagerImpl::ExportUserProfilesToVcardStreamN(const Tizen::Base::Collection::IList& userProfileList)
2587 {
2588         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2589
2590         ClearLastResult();
2591
2592         char* pVcardStream = null;
2593         int ret = CONTACTS_ERROR_NONE;
2594         UserProfile* pProfile = null;
2595         result r = E_SUCCESS;
2596         int capacity = 0;
2597
2598         unique_ptr<IEnumerator> pEnum(userProfileList.GetEnumeratorN());
2599         SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2600
2601         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2602         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2603
2604         r = pByteBuffer->Construct(capacity);
2605         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2606
2607         if (userProfileList.GetCount() == 0)
2608         {
2609                 return pByteBuffer.release();
2610         }
2611
2612         while (pEnum->MoveNext() == E_SUCCESS)
2613         {
2614                 contacts_record_h recordHandle = null;
2615
2616                 pProfile = static_cast<UserProfile*>(pEnum->GetCurrent());
2617
2618                 recordHandle = _UserProfileImpl::GetInstance(*pProfile)->GetUserProfileHandle();
2619
2620                 ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2621                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2622                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2623                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2624
2625                 capacity += strlen(pVcardStream);
2626                 r = pByteBuffer->ExpandCapacity(capacity);
2627                 if (IsFailed(r))
2628                 {
2629                         free(pVcardStream);
2630                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2631
2632                         return null;
2633                 }
2634
2635                 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2636                 free(pVcardStream);
2637                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.: capacity(%d), size(%d)", GetErrorMessage(E_SYSTEM));
2638         }
2639
2640         return pByteBuffer.release();
2641 }
2642
2643 result
2644 _AddressbookManagerImpl::ExportUserProfileToVcard(const UserProfile& userProfile, const Tizen::Base::String& vcardPath)
2645 {
2646         bool exist = File::IsFileExist(vcardPath);
2647         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2648         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2649         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2650
2651         File file;
2652         result r = file.Construct(vcardPath, "w");
2653         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));
2654         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2655         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2656
2657         char* pVcardStream = null;
2658         contacts_record_h recordHandle = null;
2659
2660         recordHandle = _UserProfileImpl::GetInstance(userProfile)->GetUserProfileHandle();
2661
2662         int ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2663         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));
2664         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2665
2666         r = file.Write(pVcardStream, strlen(pVcardStream));
2667         free(pVcardStream);
2668         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2669         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2670
2671         return E_SUCCESS;
2672 }
2673
2674 result
2675 _AddressbookManagerImpl::ExportUserProfilesToVcard(const Tizen::Base::Collection::IList& userProfileList, const Tizen::Base::String& vcardPath)
2676 {
2677         bool exist = File::IsFileExist(vcardPath);
2678         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2679         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2680         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2681
2682         int ret = CONTACTS_ERROR_NONE;
2683         char* pVcardStream = null;
2684         File file;
2685         UserProfile* pProfile = null;
2686
2687         result r = file.Construct(vcardPath, "w");
2688         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));
2689         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2690         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2691
2692         unique_ptr<IEnumerator> pEnum(userProfileList.GetEnumeratorN());
2693         SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2694
2695         while (pEnum->MoveNext() == E_SUCCESS)
2696         {
2697                 contacts_record_h recordHandle = null;
2698
2699                 pProfile = static_cast<UserProfile*>(pEnum->GetCurrent());
2700
2701                 recordHandle = _UserProfileImpl::GetInstance(*pProfile)->GetUserProfileHandle();
2702
2703                 ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2704                 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));
2705                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2706
2707                 r = file.Write(pVcardStream, strlen(pVcardStream));
2708                 free(pVcardStream);
2709                 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2710                 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2711         }
2712
2713         return E_SUCCESS;
2714 }
2715
2716 IList*
2717 _AddressbookManagerImpl::GetAllUserProfilesN(void) const
2718 {
2719         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2720
2721         ClearLastResult();
2722
2723         unique_ptr< __Filter<__ContactsUserProfile> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsUserProfile>());
2724         SysTryReturn(NID_SCL, pRwAbFilter != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2725
2726         __Query<__ContactsUserProfile> query;
2727         query.Construct();
2728         query.SetSort(_contacts_my_profile.display_name, true);
2729
2730         if (pRwAbFilter->Get() != null)
2731         {
2732                 query.SetFilter(*pRwAbFilter);
2733         }
2734
2735         IList* pUserProfilesList = _AddressbookUtil::SearchWithQueryN<__ContactsUserProfile, UserProfile>(query);
2736         SysTryReturn(NID_SCL, pUserProfilesList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2737
2738         return pUserProfilesList;
2739 }
2740
2741 UserProfile*
2742 _AddressbookManagerImpl::GetUserProfileN(AddressbookId addressbookId) const
2743 {
2744         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2745         SysTryReturn(NID_SCL, addressbookId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. Addressbook Id(%d).", GetErrorMessage(E_INVALID_ARG), addressbookId);
2746
2747         ClearLastResult();
2748
2749         int mode = 0;
2750         unique_ptr<ContactRecord, ContactRecordDeleter> pAbRecord(_AddressbookUtil::GetContactRecordN(_contacts_address_book._uri, addressbookId));
2751         SysTryReturn(NID_SCL, pAbRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2752
2753         contacts_record_get_int(pAbRecord.get(), _contacts_address_book.mode, &mode);
2754         SysTryReturn(NID_SCL, mode == 0, null, E_OBJ_NOT_FOUND, "[%s] The addressbook does not exist.", GetErrorMessage(E_OBJ_NOT_FOUND));
2755
2756         __Filter<__ContactsUserProfile> filter;
2757         filter.Construct();
2758         filter.AddInt(_contacts_my_profile.address_book_id, CONTACTS_MATCH_EQUAL, addressbookId);
2759
2760         __Query<__ContactsUserProfile> query;
2761         query.Construct();
2762         query.SetFilter(filter);
2763
2764         unique_ptr<IList, AllElementsDeleter> pUserProfilesList(_AddressbookUtil::SearchWithQueryN<__ContactsUserProfile, UserProfile>(query));
2765         SysTryReturn(NID_SCL, pUserProfilesList.get() != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2766         SysTryReturn(NID_SCL, pUserProfilesList->GetCount() != 0, null, E_SUCCESS, "No UserProfile Set for this Addressbook.");
2767         SysTryReturn(NID_SCL, pUserProfilesList->GetCount() == 1, null, E_SYSTEM, "[%s] Propagating. More than one UserProfile not allowed.", GetErrorMessage(E_SYSTEM));
2768
2769         UserProfile* pProfile = new (std::nothrow) UserProfile(*(static_cast<UserProfile*>(pUserProfilesList->GetAt(0))));
2770         SysTryReturn(NID_SCL, pProfile != null, null, E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(E_OUT_OF_MEMORY));
2771
2772         return pProfile;
2773 }
2774
2775 _AddressbookManagerImpl*
2776 _AddressbookManagerImpl::GetInstance(AddressbookManager& addressbookManager)
2777 {
2778         return addressbookManager.__pAddressbookManagerImpl;
2779 }
2780
2781 const _AddressbookManagerImpl*
2782 _AddressbookManagerImpl::GetInstance(const AddressbookManager& addressbookManager)
2783 {
2784         return addressbookManager.__pAddressbookManagerImpl;
2785 }
2786
2787 }}  // Tizen::Social