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