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