314b125b511b8d5711e2adbe42d5410b4cb21185
[platform/framework/native/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 <FApp.h>
25 #include <FBaseColIListT.h>
26 #include <FBaseColArrayListT.h>
27 #include <FBaseColHashMapT.h>
28 #include <FBaseResult.h>
29 #include <FBaseLongLong.h>
30 #include <FBaseInteger.h>
31 #include <FIoFile.h>
32 #include <FSclAddressbook.h>
33 #include <FSclAddressbookManager.h>
34 #include <FSclIAddressbookChangeEventListener.h>
35 #include <FSclContact.h>
36 #include <FSclCategory.h>
37 #include <FSclUserProfile.h>
38 #include <FSclContactChangeInfo.h>
39 #include <FSclCategoryChangeInfo.h>
40 #include <FSclPerson.h>
41 #include <FSclPhoneNumberContact.h>
42 #include <FScl_PhoneNumberContactImpl.h>
43 #include <FSclEmailContact.h>
44 #include <FScl_EmailContactImpl.h>
45 #include <FSclAddressbookFilter.h>
46 #include <FSclIAddressbookEventListener.h>
47 #include <FSclIRecordEventListener.h>
48 #include <FBaseSysLog.h>
49 #include <FApp_AppInfo.h>
50 #include <FBase_StringConverter.h>
51 #include "FScl_AddressbookImpl.h"
52 #include "FScl_AddressbookFilterImpl.h"
53 #include "FScl_AddressbookManagerImpl.h"
54 #include "FScl_AddressbookUtil.h"
55 #include "FScl_RecordImpl.h"
56 #include "FScl_ContactImpl.h"
57 #include "FScl_CategoryImpl.h"
58 #include "FScl_PersonImpl.h"
59 #include "FScl_AddressbookUtil.h"
60 #include "FScl_ContactChangeInfoImpl.h"
61 #include "FScl_CategoryChangeInfoImpl.h"
62 #include "FScl_ContactDbMonitor.h"
63 #include "FScl_ContactDbConnector.h"
64 #include "FScl_UserProfileImpl.h"
65
66 using namespace std;
67 using namespace Tizen::App;
68 using namespace Tizen::Base;
69 using namespace Tizen::Base::Collection;
70 using namespace Tizen::Io;
71 using namespace Tizen::Graphics;
72
73 namespace Tizen { namespace Social
74 {
75
76 _AddressbookManagerImpl::_AddressbookManagerImpl(void)
77         : __pIAddressbookEventListener(null)
78         , __pIAddressbookChangeEventListener(null)
79         , __dbVersionForContact(0)
80         , __dbVersionForGroup(0)
81         , __dbVersionForRelation(0)
82 {
83         // empty body.
84 }
85
86 _AddressbookManagerImpl::~_AddressbookManagerImpl(void)
87 {
88         if (__pIAddressbookEventListener != null || __pIAddressbookChangeEventListener != null)
89         {
90                 _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
91                 if (pContactDbMonitor != null)
92                 {
93                         pContactDbMonitor->RemoveListener(*this);
94                 }
95         }
96 }
97
98 result
99 _AddressbookManagerImpl::Construct(void)
100 {
101         result r = _ContactDbConnector::EnsureDbConnection();
102         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
103
104         return E_SUCCESS;
105 }
106
107 result
108 _AddressbookManagerImpl::SetEventListener(IAddressbookEventListener* pListener)
109 {
110         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
111
112         result r = E_SUCCESS;
113
114         if (pListener != null)
115         {
116                 if (__pIAddressbookEventListener == null && __pIAddressbookChangeEventListener == null)
117                 {
118                         _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
119                         SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
120
121                         r = pContactDbMonitor->AddListener(*this);
122                         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
123                 }
124
125                 __dbVersionForContact = GetLatestVersion();
126                 SysTryReturn(NID_SCL, __dbVersionForContact != -1, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
127
128                 __dbVersionForGroup = __dbVersionForContact;
129                 __dbVersionForRelation = __dbVersionForContact;
130
131                 __pIAddressbookChangeEventListener = null;
132                 __pIAddressbookEventListener = pListener;
133         }
134         else
135         {
136                 if (__pIAddressbookEventListener != null || __pIAddressbookChangeEventListener != null)
137                 {
138
139                         _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
140                         SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
141
142                         pContactDbMonitor->RemoveListener(*this);
143                 }
144
145                 __pIAddressbookEventListener = null;
146                 __pIAddressbookChangeEventListener = null;
147         }
148
149         return E_SUCCESS;
150 }
151
152 result
153 _AddressbookManagerImpl::SetAddressbookChangeEventListener(IAddressbookChangeEventListener* pListener)
154 {
155         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
156
157         result r = E_SUCCESS;
158
159         if (pListener != null)
160         {
161                 if (__pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
162                 {
163                         _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
164                         SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
165
166                         r = pContactDbMonitor->AddListener(*this);
167                         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
168                 }
169
170                 __dbVersionForContact = GetLatestVersion();
171                 SysTryReturn(NID_SCL, __dbVersionForContact != -1, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
172
173                 __dbVersionForGroup = __dbVersionForContact;
174                 __dbVersionForRelation = __dbVersionForContact;
175
176                 __pIAddressbookEventListener = null;
177                 __pIAddressbookChangeEventListener = pListener;
178         }
179         else
180         {
181                 if (__pIAddressbookChangeEventListener != null || __pIAddressbookEventListener != null)
182                 {
183
184                         _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
185                         SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
186
187                         pContactDbMonitor->RemoveListener(*this);
188                 }
189
190                 __pIAddressbookChangeEventListener = null;
191                 __pIAddressbookEventListener = null;
192         }
193
194         return E_SUCCESS;
195 }
196
197 Addressbook*
198 _AddressbookManagerImpl::CreateAddressbookN(AccountId accountId, const String& name)
199 {
200         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
201         SysTryReturn(NID_SCL, accountId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. account id is invalid.", GetErrorMessage(E_INVALID_ARG));
202         SysTryReturn(NID_SCL, !name.IsEmpty(), null, E_INVALID_ARG, "[%s] Invalid argument is used. The name is empty.", GetErrorMessage(E_INVALID_ARG));
203
204         ClearLastResult();
205
206         result r = E_SUCCESS;
207         int recordId = 0;
208
209         unique_ptr<char[]> pNameString(_StringConverter::CopyToCharArrayN(name));
210         SysTryReturn(NID_SCL, pNameString !=null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
211
212         __Filter<__ContactsAddressbook> filter;
213         filter.Construct();
214         filter.AddString(_contacts_address_book.name, CONTACTS_MATCH_EXACTLY, pNameString.get());
215         if (accountId != 0)
216         {
217                 filter.AddOperator(CONTACTS_FILTER_OPERATOR_OR);
218                 filter.AddInt(_contacts_address_book.account_id, CONTACTS_MATCH_EQUAL, accountId);
219         }
220
221         __Query<__ContactsAddressbook> query;
222         query.Construct();
223         query.SetFilter(filter);
224
225         int count = _AddressbookUtil::GetCountWithQuery(query);
226         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));
227
228         unique_ptr<Addressbook> pAddressbook(new (std::nothrow) Addressbook());
229         SysTryReturn(NID_SCL, pAddressbook != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
230
231         r = pAddressbook->Construct();
232         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
233
234         unique_ptr<ContactRecord, ContactRecordDeleter> pAbRecord(_AddressbookUtil::CreateContactRecordN(_contacts_address_book._uri));
235         SysTryReturn(NID_SCL, pAbRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
236
237         contacts_record_set_str(pAbRecord.get(), _contacts_address_book.name, pNameString.get());
238         contacts_record_set_int(pAbRecord.get(), _contacts_address_book.account_id, accountId);
239
240         r = _AddressbookUtil::InsertContactRecordN(pAbRecord.get(), recordId);
241         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
242
243         _AddressbookImpl::GetInstance(*pAddressbook)->SetAccountId(accountId);
244         _AddressbookImpl::GetInstance(*pAddressbook)->SetName(name);
245         _AddressbookImpl::GetInstance(*pAddressbook)->SetId(recordId);
246
247         return pAddressbook.release();
248 }
249
250 result
251 _AddressbookManagerImpl::DeleteAddressbook(AddressbookId addressbookId)
252 {
253         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));
254         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
255         result r = _AddressbookUtil::DeleteContactRecord(_contacts_address_book._uri, addressbookId);
256         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
257
258         return E_SUCCESS;
259 }
260
261 IList*
262 _AddressbookManagerImpl::GetAddressbooksByAccountN(AccountId accountId) const
263 {
264         SysTryReturn(NID_SCL, accountId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. account id is invalid.", GetErrorMessage(E_INVALID_ARG));
265         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
266
267         ClearLastResult();
268
269         IList* pAddressbooks = null;
270
271         __Filter<__ContactsAddressbook> accountFilter;
272         accountFilter.Construct();
273         accountFilter.AddInt(_contacts_address_book.account_id, CONTACTS_MATCH_EQUAL, accountId);
274
275         unique_ptr< __Filter<__ContactsAddressbook> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsAddressbook>());
276         if (pRwAbFilter->Get() == null)
277         {
278                 __Query<__ContactsAddressbook> query;
279                 query.Construct();
280                 query.SetFilter(accountFilter);
281
282                 pAddressbooks = _AddressbookUtil::SearchWithQueryN<__ContactsAddressbook, Addressbook>(query);
283                 SysTryReturn(NID_SCL, pAddressbooks != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
284         }
285         else
286         {
287                 __Filter<__ContactsAddressbook> mainFilter;
288                 mainFilter.Construct();
289
290                 mainFilter.AddFilter(*pRwAbFilter);
291                 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
292                 mainFilter.AddFilter(accountFilter);
293
294                 __Query<__ContactsAddressbook> query;
295                 query.Construct();
296                 query.SetFilter(mainFilter);
297
298                 pAddressbooks = _AddressbookUtil::SearchWithQueryN<__ContactsAddressbook, Addressbook>(query);
299                 SysTryReturn(NID_SCL, pAddressbooks != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
300         }
301
302         return pAddressbooks;
303 }
304
305 IList*
306 _AddressbookManagerImpl::GetAllAddressbooksN(void) const
307 {
308         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
309
310         ClearLastResult();
311
312         __Filter<__ContactsAddressbook> abFilter;
313         abFilter.Construct();
314         abFilter.AddInt(_contacts_address_book.mode, CONTACTS_MATCH_EQUAL, 0);
315
316         __Query<__ContactsAddressbook> query;
317         query.Construct();
318         query.SetFilter(abFilter);
319         query.SetSort(_contacts_address_book.name, true);
320
321         IList* pAddressbooks = _AddressbookUtil::SearchWithQueryN<__ContactsAddressbook, Addressbook>(query);
322         SysTryReturn(NID_SCL, pAddressbooks != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
323
324         return pAddressbooks;
325 }
326
327 Addressbook*
328 _AddressbookManagerImpl::GetAddressbookN(AddressbookId addressbookId) const
329 {
330         SysTryReturn(NID_SCL, addressbookId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. addressbookId.", GetErrorMessage(E_INVALID_ARG), addressbookId);
331         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
332
333         ClearLastResult();
334
335         result r = E_SUCCESS;
336         int intValue = 0;
337         char* pCharValue = null;
338
339         unique_ptr<ContactRecord, ContactRecordDeleter> pAbRecord(_AddressbookUtil::GetContactRecordN(_contacts_address_book._uri, addressbookId));
340         SysTryReturn(NID_SCL, pAbRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
341
342         contacts_record_get_int(pAbRecord.get(), _contacts_address_book.mode, &intValue);
343         SysTryReturn(NID_SCL, intValue == 0, null, E_OBJ_NOT_FOUND, "[%s] Addressbook does not exist.", GetErrorMessage(E_OBJ_NOT_FOUND));
344
345         unique_ptr<Addressbook> pAddressbook(new (std::nothrow) Addressbook());
346         SysTryReturn(NID_SCL, pAddressbook !=null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
347
348         r = pAddressbook->Construct();
349         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
350
351         _AddressbookImpl::GetInstance(*pAddressbook)->SetId(addressbookId);
352
353         contacts_record_get_int(pAbRecord.get(), _contacts_address_book.account_id, &intValue);
354         _AddressbookImpl::GetInstance(*pAddressbook)->SetAccountId(intValue);
355
356         contacts_record_get_str_p(pAbRecord.get(), _contacts_address_book.name, &pCharValue);
357         _AddressbookImpl::GetInstance(*pAddressbook)->SetName(pCharValue);
358
359         return pAddressbook.release();
360 }
361
362 result
363 _AddressbookManagerImpl::AddContact(Contact& contact, AddressbookId addressbookId)
364 {
365         if (_ContactImpl::GetInstance(contact)->IsRemoved())
366         {
367                 result r = _ContactImpl::GetInstance(contact)->Invalidate();
368                 SysTryReturn(NID_SCL, r == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
369         }
370
371         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));
372         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));
373         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
374
375         unique_ptr<ContactRecord, ContactRecordDeleter> pAbRecord(_AddressbookUtil::GetContactRecordN(_contacts_address_book._uri, addressbookId));
376         SysTryReturn(NID_SCL, pAbRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
377
378         int intValue = 0;
379         contacts_record_get_int(pAbRecord.get(), _contacts_address_book.mode, &intValue);
380         SysTryReturn(NID_SCL, intValue == 0, null, E_OBJ_NOT_FOUND, "[%s] Addressbook does not exist.", GetErrorMessage(E_OBJ_NOT_FOUND));
381
382         contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
383         contacts_record_set_int(recordHandle, _contacts_contact.address_book_id, addressbookId);
384
385         int recordId = 0;
386         result r = _AddressbookUtil::InsertContactRecordN(recordHandle, recordId);
387         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
388
389         unique_ptr<ContactRecord, ContactRecordDeleter> pContactRecord(_AddressbookUtil::GetContactRecordN(_contacts_contact._uri, recordId));
390         SysTryReturn(NID_SCL, pContactRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
391
392         _ContactImpl::GetInstance(contact)->SetContactRecordHandle(pContactRecord.release());
393         _RecordImpl::GetInstance(contact)->SetRecordId(recordId);
394
395         return E_SUCCESS;
396 }
397
398 result
399 _AddressbookManagerImpl::AddCategory(Category& category, AddressbookId addressbookId)
400 {
401         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));
402         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
403
404         unique_ptr<ContactRecord, ContactRecordDeleter> pAbRecord(_AddressbookUtil::GetContactRecordN(_contacts_address_book._uri, addressbookId));
405         SysTryReturn(NID_SCL, pAbRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
406
407         int intValue = 0;
408         contacts_record_get_int(pAbRecord.get(), _contacts_address_book.mode, &intValue);
409         SysTryReturn(NID_SCL, intValue == 0, null, E_OBJ_NOT_FOUND, "[%s] Addressbook does not exist.", GetErrorMessage(E_OBJ_NOT_FOUND));
410
411         contacts_record_h recordHandle = _CategoryImpl::GetInstance(category)->GetRecordHandle();
412         contacts_record_set_int(recordHandle, _contacts_group.address_book_id, addressbookId);
413
414         int recordId = 0;
415         result r = _AddressbookUtil::InsertContactRecordN(recordHandle, recordId);
416         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
417
418         unique_ptr<ContactRecord, ContactRecordDeleter> pCategoryRecord(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, recordId));
419         SysTryReturn(NID_SCL, pCategoryRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
420
421         _CategoryImpl::GetInstance(category)->SetRecordHandle(pCategoryRecord.release());
422         _RecordImpl::GetInstance(category)->SetRecordId(recordId);
423
424         unique_ptr<IListT<int> > pList(_CategoryImpl::GetInstance(category)->GetAddedMembersN());
425         if (pList != null && pList->GetCount() > 0)
426         {
427                 unique_ptr<IEnumeratorT<int> > pEnum(pList->GetEnumeratorN());
428
429                 while (pEnum->MoveNext() == E_SUCCESS)
430                 {
431                         int tableId = -1;
432                         pEnum->GetCurrent(tableId);
433
434                         AddMemberToCategory(category.GetRecordId(), tableId);
435                 }
436
437                 _CategoryImpl::GetInstance(category)->ClearAddedMemberList();
438         }
439
440         return E_SUCCESS;
441 }
442
443 result
444 _AddressbookManagerImpl::RemoveContact(RecordId contactId)
445 {
446         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));
447         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
448         result r = _AddressbookUtil::DeleteContactRecord(_contacts_contact._uri, contactId);
449         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
450
451         return E_SUCCESS;
452 }
453
454 result
455 _AddressbookManagerImpl::RemoveCategory(RecordId categoryId)
456 {
457         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));
458         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
459
460         unique_ptr<ContactRecord, ContactRecordDeleter> pCategoryRecord(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, categoryId));
461         SysTryReturn(NID_SCL, pCategoryRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
462
463         bool isReadOnly = false;
464         contacts_record_get_bool(pCategoryRecord.get(), _contacts_group.is_read_only, &isReadOnly);
465         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));
466
467         result r = _AddressbookUtil::DeleteContactRecord(_contacts_group._uri, categoryId);
468         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
469
470         return E_SUCCESS;
471 }
472
473 result
474 _AddressbookManagerImpl::UpdateContact(const Contact& contact)
475 {
476         RecordId contactId = contact.GetRecordId();
477         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));
478         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));
479         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
480
481         unique_ptr<ContactRecord, ContactRecordDeleter> pContactRecord(_AddressbookUtil::GetContactRecordN(_contacts_simple_contact._uri, contactId));
482         SysTryReturn(NID_SCL, pContactRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
483
484         int intValue = 0;
485         contacts_record_get_int(pContactRecord.get(), _contacts_simple_contact.id, &intValue);
486         SysTryReturn(NID_SCL, intValue == contactId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
487
488         contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
489         SysTryReturn(NID_SCL, recordHandle != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
490
491         result r = _AddressbookUtil::UpdateContactRecord(recordHandle);
492         if (r == E_OBJ_NOT_FOUND)
493         {
494                 unsigned int count = 0;
495                 unsigned int count2 = 0;
496
497                 contacts_record_h newRecordHandle = null;
498                 int ret = contacts_db_get_record(_contacts_contact._uri, contactId, &newRecordHandle);
499                 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));
500                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
501
502                 contacts_record_get_child_record_count(newRecordHandle, _contacts_contact.image, &count);
503                 contacts_record_get_child_record_count(recordHandle, _contacts_contact.image, &count2);
504
505                 contacts_record_h imageHandle = null;
506                 contacts_record_h imageHandle2 = null;
507
508                 int imageId = -1;
509                 int imageId2 = -1;
510
511                 for (int i = (int)count2 - 1; i >= 0; i--)
512                 {
513                         contacts_record_get_child_record_at_p(recordHandle, _contacts_contact.image, i, &imageHandle);
514                         contacts_record_get_int(imageHandle, _contacts_image.id, &imageId);
515                         if (imageId == 0)
516                         {
517                                 continue;
518                         }
519
520                         bool matchFound = false;
521
522                         for (unsigned int j = 0; j < count; j++)
523                         {
524                                 contacts_record_get_child_record_at_p(newRecordHandle, _contacts_contact.image, i, &imageHandle2);
525                                 contacts_record_get_int(imageHandle2, _contacts_image.id, &imageId2);
526                                 if (imageId == imageId2)
527                                 {
528                                         matchFound = true;
529                                         break;
530                                 }
531                         }
532
533                         if (!matchFound)
534                         {
535                                 contacts_record_remove_child_record(recordHandle, _contacts_contact.image, imageHandle);
536                         }
537                 }
538
539                 contacts_record_destroy(newRecordHandle, true);
540
541                 contacts_record_h copyRecordHandle = CopyContactRecordHandle(recordHandle);
542                 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
543
544                 contacts_record_h tempHandle = null;
545                 contacts_record_get_child_record_count(copyRecordHandle, _contacts_contact.image, &count);
546
547                 char* pCharValue = null;
548                 String tempPath;
549
550                 for (unsigned int i = 0; i < count; i++)
551                 {
552                         contacts_record_get_child_record_at_p(copyRecordHandle, _contacts_contact.image, i, &tempHandle);
553                         contacts_record_get_str(tempHandle, _contacts_image.path, &pCharValue);
554
555                         tempPath = Tizen::App::App::GetInstance()->GetAppRootPath() + L"data/temp";
556                         tempPath.Append((int)i);
557                         tempPath.Append(L".jpg");
558                         String servicePath(pCharValue);
559
560                         File::Copy(servicePath, tempPath, true);
561
562                         std::unique_ptr<char[]> pCharArray( _StringConverter::CopyToCharArrayN(tempPath));
563                         SysTryReturnResult(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
564
565                         contacts_record_set_str(tempHandle, _contacts_image.path, pCharArray.get());
566                         contacts_record_get_str(tempHandle, _contacts_image.path, &pCharValue);
567
568                         tempPath.Clear();
569                 }
570
571                 ret = contacts_db_replace_record(copyRecordHandle, contactId);
572                 contacts_record_destroy(copyRecordHandle, true);
573
574                 for (unsigned int i = 0; i < count; i++)
575                 {
576                         tempPath = Tizen::App::App::GetInstance()->GetAppRootPath() + L"data/temp";
577                         tempPath.Append((int)i);
578                         tempPath.Append(L".jpg");
579
580                         File::Remove(tempPath);
581
582                         tempPath.Clear();
583                 }
584                 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));
585                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is insufficient.", GetErrorMessage(E_STORAGE_FULL));
586                 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));
587         }
588         else
589         {
590                 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
591         }
592         pContactRecord.reset(_AddressbookUtil::GetContactRecordN(_contacts_contact._uri, contact.GetRecordId()));
593         SysTryReturn(NID_SCL, pContactRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
594
595         _ContactImpl::GetInstance(*const_cast<Contact*>(&contact))->SetContactRecordHandle(pContactRecord.release());
596
597         return E_SUCCESS;
598 }
599
600 result
601 _AddressbookManagerImpl::UpdateCategory(const Category& category)
602 {
603         RecordId categoryId = category.GetRecordId();
604         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));
605         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));
606         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
607
608         unique_ptr<ContactRecord, ContactRecordDeleter> pCategoryRecord(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, category.GetRecordId()));
609         SysTryReturn(NID_SCL, pCategoryRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
610
611         int intValue = 0;
612         contacts_record_get_int(pCategoryRecord.get(), _contacts_group.id, &intValue);
613         SysTryReturn(NID_SCL, intValue == categoryId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
614
615         contacts_record_h recordHandle = _CategoryImpl::GetInstance(category)->GetRecordHandle();
616
617         result r = _AddressbookUtil::UpdateContactRecord(recordHandle);
618         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
619
620         pCategoryRecord.reset(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, category.GetRecordId()));
621         SysTryReturn(NID_SCL, pCategoryRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
622
623         _CategoryImpl::GetInstance(*const_cast<Category*>(&category))->SetRecordHandle(pCategoryRecord.release());
624
625         unique_ptr<IListT<int> > pList(_CategoryImpl::GetInstance(category)->GetAddedMembersN());
626         if (pList != null && pList->GetCount() > 0)
627         {
628                 int tableId = -1;
629                 unique_ptr<IEnumeratorT<int> > pEnum(pList->GetEnumeratorN());
630                 while (pEnum->MoveNext() == E_SUCCESS)
631                 {
632                         pEnum->GetCurrent(tableId);
633
634                         AddMemberToCategory(category.GetRecordId(), tableId);
635                 }
636
637                 const_cast<_CategoryImpl*>(_CategoryImpl::GetInstance(category))->ClearAddedMemberList();
638         }
639
640         pList.reset(_CategoryImpl::GetInstance(category)->GetRemovedMembersN());
641         if (pList != null && pList->GetCount() > 0)
642         {
643                 int tableId = -1;
644                 unique_ptr<IEnumeratorT<int> > pEnum(pList->GetEnumeratorN());
645                 while (pEnum->MoveNext() == E_SUCCESS)
646                 {
647                         pEnum->GetCurrent(tableId);
648
649                         RemoveMemberFromCategory(category.GetRecordId(), tableId);
650                 }
651
652                 const_cast<_CategoryImpl*>(_CategoryImpl::GetInstance(category))->ClearRemovedMemberList();
653         }
654
655         return E_SUCCESS;
656 }
657
658 result
659 _AddressbookManagerImpl::AddMemberToCategory(RecordId categoryId, RecordId contactId)
660 {
661         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);
662         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);
663         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
664
665         result r = _AddressbookUtil::AddContactToCategory(categoryId, contactId);
666         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
667
668         return E_SUCCESS;
669 }
670
671 result
672 _AddressbookManagerImpl::RemoveMemberFromCategory(RecordId categoryId, RecordId contactId)
673 {
674         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);
675         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);
676         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
677
678         result r = _AddressbookUtil::RemoveContactFromCategory(categoryId, contactId);
679         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
680
681         return E_SUCCESS;
682 }
683
684 IList*
685 _AddressbookManagerImpl::GetAllCategoriesN(void) const
686 {
687         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
688
689         ClearLastResult();
690
691         unique_ptr< __Filter<__ContactsGroup> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsGroup>());
692
693         __Query<__ContactsGroup> query;
694         query.Construct();
695         query.SetSort(_contacts_group.name, true);
696
697         if (pRwAbFilter->Get() != null)
698         {
699                 query.SetFilter(*pRwAbFilter);
700         }
701
702         IList* pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsGroup, Category>(query);
703         SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
704
705         return pCategories;
706 }
707
708 IList*
709 _AddressbookManagerImpl::GetCategoriesByContactN(RecordId contactId) const
710 {
711         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
712         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));
713
714         ClearLastResult();
715         IList* pCategories = null;
716
717         unique_ptr< __Filter<__ContactsContactGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactGroupRel>());
718
719         __Filter<__ContactsContactGroupRel> relFilter;
720         relFilter.Construct();
721         relFilter.AddInt(_contacts_contact_grouprel.contact_id, CONTACTS_MATCH_EQUAL, contactId);
722
723         if (pRwAbFilter->Get() != null)
724         {
725                 __Filter<__ContactsContactGroupRel> mainFilter;
726                 mainFilter.Construct();
727                 mainFilter.AddFilter(*pRwAbFilter);
728                 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
729                 mainFilter.AddFilter(relFilter);
730
731                 __Query<__ContactsContactGroupRel> query;
732                 query.Construct();
733                 query.SetFilter(mainFilter);
734
735                 pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Category>(query);
736                 SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
737         }
738         else
739         {
740                 __Query<__ContactsContactGroupRel> query;
741                 query.Construct();
742                 query.SetFilter(relFilter);
743
744                 pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Category>(query);
745                 SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
746         }
747
748         return pCategories;
749 }
750
751 IList*
752 _AddressbookManagerImpl::GetCategoriesByPersonN(PersonId personId) const
753 {
754         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
755
756         ClearLastResult();
757
758         IList* pCategories = null;
759
760         unique_ptr< __Filter<__ContactsContactGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactGroupRel>());
761
762         __Filter<__ContactsContactGroupRel> relFilter;
763         relFilter.Construct();
764         relFilter.AddInt(_contacts_contact_grouprel.person_id, CONTACTS_MATCH_EQUAL, personId);
765         relFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
766         relFilter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_GREATER_THAN, 0);
767
768         unsigned int propertyIds[] =
769         {
770                 _contacts_contact_grouprel.group_id,
771         };
772
773         if (pRwAbFilter->Get() != null)
774         {
775
776                 __Filter<__ContactsContactGroupRel> mainFilter;
777                 mainFilter.Construct();
778                 mainFilter.AddFilter(*pRwAbFilter);
779                 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
780                 mainFilter.AddFilter(relFilter);
781
782
783                 __Query<__ContactsContactGroupRel> query;
784                 query.Construct();
785                 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
786                 query.SetFilter(mainFilter);
787                 query.SetSort(_contacts_contact_grouprel.group_name, true);
788                 query.SetDistinct(true);
789
790                 pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Category>(query);
791                 SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
792         }
793         else
794         {
795                 __Query<__ContactsContactGroupRel> query;
796                 query.Construct();
797                 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
798                 query.SetFilter(relFilter);
799                 query.SetSort(_contacts_contact_grouprel.group_name, true);
800                 query.SetDistinct(true);
801
802                 pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Category>(query);
803                 SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
804
805         }
806
807         return pCategories;
808 }
809
810 IList*
811 _AddressbookManagerImpl::GetAllContactsN(void) const
812 {
813         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
814
815         ClearLastResult();
816
817         unique_ptr< __Filter<__ContactsContact> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContact>());
818
819         __Query<__ContactsContact> query;
820         query.Construct();
821         query.SetSort(_contacts_contact.display_name, true);
822
823         if (pRwAbFilter->Get() != null)
824         {
825                 query.SetFilter(*pRwAbFilter);
826         }
827
828         IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
829         SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
830
831         return pContacts;
832 }
833
834 IList*
835 _AddressbookManagerImpl::GetContactsByCategoryN(RecordId categoryId) const
836 {
837         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));
838         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
839
840         ClearLastResult();
841
842         IList* pContacts = null;
843
844         unique_ptr< __Filter<__ContactsContactGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactGroupRel>());
845
846         __Filter<__ContactsContactGroupRel> relFilter;
847         relFilter.Construct();
848
849         if (categoryId != INVALID_RECORD_ID)
850         {
851                 relFilter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_EQUAL, categoryId);
852         }
853         else
854         {
855                 relFilter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_NONE, 0);
856         }
857
858         if (pRwAbFilter->Get() != null)
859         {
860                 __Filter<__ContactsContactGroupRel> mainFilter;
861                 mainFilter.Construct();
862
863                 mainFilter.AddFilter(*pRwAbFilter);
864                 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
865                 mainFilter.AddFilter(relFilter);
866
867                 __Query<__ContactsContactGroupRel> query;
868                 query.Construct();
869                 query.SetFilter(mainFilter);
870
871                 pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Contact>(query);
872                 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
873         }
874         else
875         {
876                 __Query<__ContactsContactGroupRel> query;
877                 query.Construct();
878                 query.SetFilter(relFilter);
879
880                 pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Contact>(query);
881                 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
882         }
883
884         return pContacts;
885 }
886
887 IList*
888 _AddressbookManagerImpl::GetContactsByPersonN(PersonId personId) const
889 {
890         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
891
892         ClearLastResult();
893
894         IList* pContacts = null;
895
896         unique_ptr< __Filter<__ContactsContact> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContact>());
897
898         __Filter<__ContactsContact> contactFilter;
899         contactFilter.Construct();
900         contactFilter.AddInt(_contacts_contact.person_id, CONTACTS_MATCH_EQUAL, personId);
901
902         if (pRwAbFilter->Get() != null)
903         {
904                 __Filter<__ContactsContact> mainFilter;
905                 mainFilter.Construct();
906
907                 mainFilter.AddFilter(*pRwAbFilter);
908                 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
909                 mainFilter.AddFilter(contactFilter);
910
911                 __Query<__ContactsContact> query;
912                 query.Construct();
913                 query.SetFilter(mainFilter);
914                 query.SetSort(_contacts_contact.display_name, true);
915
916                 pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
917                 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
918         }
919         else
920         {
921                 __Query<__ContactsContact> query;
922                 query.Construct();
923                 query.SetFilter(contactFilter);
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
930         return pContacts;
931 }
932
933 IList*
934 _AddressbookManagerImpl::SearchContactsByEmailN(const String& email) const
935 {
936         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));
937         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
938
939         ClearLastResult();
940
941         IList* pContacts = null;
942
943         unique_ptr< __Filter<__ContactsContactEmail> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactEmail>());
944
945         unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(email));
946         SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
947
948         __Filter<__ContactsContactEmail> emailFilter;
949         emailFilter.Construct();
950         emailFilter.AddString(_contacts_contact_email.email, CONTACTS_MATCH_CONTAINS, pCharArray.get());
951
952         if (pRwAbFilter->Get() != null)
953         {
954                 __Filter<__ContactsContactEmail> mainFilter;
955                 mainFilter.Construct();
956
957                 mainFilter.AddFilter(*pRwAbFilter);
958                 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
959                 mainFilter.AddFilter(emailFilter);
960
961                 __Query<__ContactsContactEmail> query;
962                 query.Construct();
963                 query.SetFilter(mainFilter);
964                 query.SetSort(_contacts_contact_email.display_name, true);
965
966                 pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactEmail, Contact>(query);
967                 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
968         }
969         else
970         {
971                 __Query<__ContactsContactEmail> query;
972                 query.Construct();
973                 query.SetFilter(emailFilter);
974                 query.SetSort(_contacts_contact_email.display_name, true);
975
976                 pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactEmail, Contact>(query);
977                 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
978
979         }
980
981         return pContacts;
982 }
983
984 IList*
985 _AddressbookManagerImpl::SearchContactsByNameN(const String& name) const
986 {
987         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
988
989         ClearLastResult();
990
991         IList* pContacts = null;
992
993         unique_ptr< __Filter<__ContactsContact> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContact>());
994
995         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));
996
997         unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(name));
998         SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
999
1000         __Filter<__ContactsContact> nameFilter;
1001         nameFilter.Construct();
1002         nameFilter.AddString(_contacts_contact.display_name, CONTACTS_MATCH_CONTAINS, pCharArray.get());
1003
1004         if (pRwAbFilter->Get() != null)
1005         {
1006                 __Filter<__ContactsContact> mainFilter;
1007                 mainFilter.Construct();
1008
1009                 mainFilter.AddFilter(*pRwAbFilter);
1010                 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1011                 mainFilter.AddFilter(nameFilter);
1012
1013                 __Query<__ContactsContact> query;
1014                 query.Construct();
1015                 query.SetFilter(mainFilter);
1016                 query.SetSort(_contacts_contact.display_name, true);
1017
1018                 pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
1019                 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1020         }
1021         else
1022         {
1023                 __Query<__ContactsContact> query;
1024                 query.Construct();
1025                 query.SetFilter(nameFilter);
1026                 query.SetSort(_contacts_contact.display_name, true);
1027
1028                 pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
1029                 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1030         }
1031
1032         return pContacts;
1033 }
1034
1035 IList*
1036 _AddressbookManagerImpl::SearchContactsByPhoneNumberN(const String& phoneNumber) const
1037 {
1038         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1039
1040         ClearLastResult();
1041
1042         IList* pContacts = null;
1043
1044         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));
1045
1046         unique_ptr< __Filter<__ContactsContactNumber> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactNumber>());
1047
1048         unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(phoneNumber));
1049         SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1050
1051         __Filter<__ContactsContactNumber> numberFilter;
1052         numberFilter.Construct();
1053         numberFilter.AddString(_contacts_contact_number.normalized_number, CONTACTS_MATCH_CONTAINS, pCharArray.get());
1054         numberFilter.AddOperator(CONTACTS_FILTER_OPERATOR_OR);
1055         numberFilter.AddString(_contacts_contact_number.number, CONTACTS_MATCH_CONTAINS, pCharArray.get());
1056
1057         if (pRwAbFilter->Get() != null)
1058         {
1059                 __Filter<__ContactsContactNumber> mainFilter;
1060                 mainFilter.Construct();
1061
1062                 mainFilter.AddFilter(*pRwAbFilter);
1063                 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1064                 mainFilter.AddFilter(numberFilter);
1065
1066                 __Query<__ContactsContactNumber> query;
1067                 query.Construct();
1068                 query.SetFilter(mainFilter);
1069                 query.SetDistinct(true);
1070                 query.SetSort(_contacts_contact_number.display_name, true);
1071
1072                 pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactNumber, Contact>(query);
1073                 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1074         }
1075         else
1076         {
1077                 __Query<__ContactsContactNumber> query;
1078                 query.Construct();
1079                 query.SetFilter(numberFilter);
1080                 query.SetDistinct(true);
1081                 query.SetSort(_contacts_contact_number.display_name, true);
1082
1083                 pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactNumber, Contact>(query);
1084                 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1085         }
1086
1087
1088         return pContacts;
1089 }
1090
1091 int
1092 _AddressbookManagerImpl::GetCategoryCount(void) const
1093 {
1094         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1095
1096         ClearLastResult();
1097
1098         unique_ptr< __Filter<__ContactsGroup> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsGroup>());
1099
1100         int count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsGroup>(pRwAbFilter->Get());
1101         SysTryReturn(NID_SCL, count >= 0, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1102
1103         return count;
1104 }
1105
1106 int
1107 _AddressbookManagerImpl::GetContactCount(void) const
1108 {
1109         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1110
1111         ClearLastResult();
1112
1113         unique_ptr< __Filter<__ContactsContact> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContact>());
1114
1115         int count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContact>(pRwAbFilter->Get());
1116         SysTryReturn(NID_SCL, count >= 0, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1117
1118         return count;
1119 }
1120
1121 Contact*
1122 _AddressbookManagerImpl::GetContactN(RecordId contactId) const
1123 {
1124         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1125         SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. contactId = %d.", GetErrorMessage(E_INVALID_ARG), contactId);
1126
1127         ClearLastResult();
1128
1129         unique_ptr<ContactRecord, ContactRecordDeleter> pContactRecord(_AddressbookUtil::GetContactRecordN(_contacts_contact._uri, contactId));
1130         SysTryReturn(NID_SCL, pContactRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1131
1132         int intValue = 0;
1133         contacts_record_get_int(pContactRecord.get(), _contacts_contact.address_book_id, &intValue);
1134
1135         unique_ptr<ContactRecord, ContactRecordDeleter> pAbRecord(_AddressbookUtil::GetContactRecordN(_contacts_address_book._uri, intValue));
1136         SysTryReturn(NID_SCL, pAbRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1137
1138         contacts_record_get_int(pAbRecord.get(), _contacts_address_book.mode, &intValue);
1139         SysTryReturn(NID_SCL, intValue == 0, null, E_OBJ_NOT_FOUND, "[%s] Contact does not exist.", GetErrorMessage(E_OBJ_NOT_FOUND));
1140
1141         contacts_record_get_int(pContactRecord.get(), _contacts_contact.id, &intValue);
1142         SysTryReturn(NID_SCL, intValue == contactId, null, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
1143
1144         unique_ptr<Contact> pContact(new (std::nothrow) Contact());
1145         SysTryReturn(NID_SCL, pContact, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1146
1147         _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(pContactRecord.release());
1148         _RecordImpl::GetInstance(*pContact)->SetRecordId(intValue);
1149
1150         return pContact.release();
1151 }
1152
1153 Person*
1154 _AddressbookManagerImpl::GetPersonN(PersonId personId) const
1155 {
1156         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1157         SysTryReturn(NID_SCL, personId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. personId = %d.", GetErrorMessage(E_INVALID_ARG), personId);
1158
1159         ClearLastResult();
1160
1161         unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, personId));
1162
1163         int intValue = 0;
1164         contacts_record_get_int(pPersonRecord.get(), _contacts_person.id, &intValue);
1165         SysTryReturn(NID_SCL, intValue == personId, null, E_OBJ_NOT_FOUND, "[%s] The person is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
1166
1167         Person* pPerson = __ContactsPerson::ConvertHandleTo<Person>(pPersonRecord.get());
1168         SysTryReturn(NID_SCL, pPerson != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1169
1170         return pPerson;
1171 }
1172
1173 Category*
1174 _AddressbookManagerImpl::GetCategoryN(RecordId categoryId) const
1175 {
1176         SysTryReturn(NID_SCL, categoryId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. categoryId = %d.", categoryId);
1177         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1178
1179         ClearLastResult();
1180
1181         unique_ptr<ContactRecord, ContactRecordDeleter> pCategoryRecord(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, categoryId));
1182         SysTryReturn(NID_SCL, pCategoryRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1183
1184         int intValue = 0;
1185
1186         contacts_record_get_int(pCategoryRecord.get(), _contacts_group.id, &intValue);
1187         SysTryReturn(NID_SCL, categoryId == intValue, null, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
1188
1189         contacts_record_get_int(pCategoryRecord.get(), _contacts_group.address_book_id, &intValue);
1190
1191         unique_ptr<ContactRecord, ContactRecordDeleter> pAbRecord(_AddressbookUtil::GetContactRecordN(_contacts_address_book._uri, intValue));
1192         SysTryReturn(NID_SCL, pAbRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1193
1194         contacts_record_get_int(pAbRecord.get(), _contacts_address_book.mode, &intValue);
1195         SysTryReturn(NID_SCL, intValue == 0, null, E_OBJ_NOT_FOUND, "[%s] Category does not exist.", GetErrorMessage(E_OBJ_NOT_FOUND));
1196
1197         unique_ptr<Category> pCategory(new (std::nothrow) Category());
1198         SysTryReturn(NID_SCL, pCategory != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1199
1200         __Filter<__ContactsGroupRelation> filter;
1201         filter.Construct();
1202         filter.AddInt(_contacts_group_relation.group_id, CONTACTS_MATCH_EQUAL, categoryId);
1203
1204         __Query<__ContactsGroupRelation> query;
1205         query.Construct();
1206         query.SetFilter(filter);
1207
1208         int count = _AddressbookUtil::GetCountWithQuery(query);
1209         SysTryReturn(NID_SCL, count >= 0, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1210
1211         _CategoryImpl::GetInstance(*pCategory)->SetRecordHandle(pCategoryRecord.release());
1212         _CategoryImpl::GetInstance(*pCategory)->SetMemberCount(count);
1213         _RecordImpl::GetInstance(*pCategory)->SetRecordId(categoryId);
1214
1215         return pCategory.release();
1216 }
1217
1218 int
1219 _AddressbookManagerImpl::GetLatestVersion(void) const
1220 {
1221         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1222
1223         int latestVersion = -1;
1224
1225         ClearLastResult();
1226
1227         int ret = contacts_db_get_current_version(&latestVersion);
1228         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1229
1230         return latestVersion;
1231 }
1232
1233 IList*
1234 _AddressbookManagerImpl::GetChangedContactsAfterN(int version, int& latestVersion) const
1235 {
1236         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);
1237         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1238
1239         ClearLastResult();
1240
1241         unique_ptr< IListT<AddressbookId> > pRwAbIdList(_AddressbookUtil::GetRwAbIdListN());
1242
1243         IList* pChangedContacts = _AddressbookUtil::SearchWithVersionN<__ContactsContactUpdatedInfo, ContactChangeInfo>(-1, version, latestVersion, pRwAbIdList.get());
1244         SysTryReturn(NID_SCL, pChangedContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1245
1246         return pChangedContacts;
1247 }
1248
1249 IList*
1250 _AddressbookManagerImpl::GetChangedCategoriesAfterN(int version, int& latestVersion) const
1251 {
1252         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);
1253         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1254
1255         ClearLastResult();
1256
1257         int latestVersion1 = 0;
1258         int latestVersion2 = 0;
1259
1260         unique_ptr< IListT<AddressbookId> > pRwAbIdList(_AddressbookUtil::GetRwAbIdListN());
1261
1262         unique_ptr<IList, AllElementsDeleter> pChangedGroups(_AddressbookUtil::SearchWithVersionN<__ContactsGroupUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion1, pRwAbIdList.get()));
1263         SysTryReturn(NID_SCL, pChangedGroups != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1264
1265         unique_ptr<IList, AllElementsDeleter> pChangedRelations(_AddressbookUtil::SearchWithVersionN<__ContactsGroupRelUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion2, pRwAbIdList.get()));
1266         SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1267
1268         unique_ptr<ArrayList, AllElementsDeleter> pChangeList(new (std::nothrow) Tizen::Base::Collection::ArrayList());
1269         SysTryReturn(NID_SCL, pChangeList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1270
1271         result r = pChangeList->AddItems(*pChangedGroups);
1272         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1273
1274         r = pChangeList->AddItems(*pChangedRelations);
1275         SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1276
1277         pChangedGroups->RemoveAll(false);
1278         pChangedRelations->RemoveAll(false);
1279
1280         latestVersion = latestVersion2 > latestVersion1 ? latestVersion2 : latestVersion1;
1281
1282         return pChangeList.release();
1283 }
1284
1285 IList*
1286 _AddressbookManagerImpl::GetChangedGroupsAfterN(int version, int& latestVersion) const
1287 {
1288         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);
1289         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1290
1291         ClearLastResult();
1292
1293         unique_ptr< IListT<AddressbookId> > pRwAbIdList(_AddressbookUtil::GetRwAbIdListN());
1294
1295         IList* pChangedRelations = _AddressbookUtil::SearchWithVersionN<__ContactsGroupUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion, pRwAbIdList.get());
1296         SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1297
1298         return pChangedRelations;
1299 }
1300
1301 IList*
1302 _AddressbookManagerImpl::GetChangedRelationsAfterN(int version, int& latestVersion) const
1303 {
1304         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);
1305         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1306
1307         ClearLastResult();
1308
1309         unique_ptr< IListT<AddressbookId> > pRwAbIdList(_AddressbookUtil::GetRwAbIdListN());
1310
1311         IList* pChangedRelations = _AddressbookUtil::SearchWithVersionN<__ContactsGroupRelUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion, pRwAbIdList.get());
1312         SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1313
1314         return pChangedRelations;
1315 }
1316
1317 void
1318 _AddressbookManagerImpl::OnContactChanged(void)
1319 {
1320         if (__pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
1321         {
1322                 return;
1323         }
1324
1325         IList* pChangedContactList = GetChangedContactsAfterN(__dbVersionForContact, __dbVersionForContact);
1326         SysTryReturnVoidResult(NID_SCL, pChangedContactList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1327
1328         if (pChangedContactList->GetCount() > 0)
1329         {
1330                 if (__pIAddressbookChangeEventListener != null)
1331                 {
1332                         __pIAddressbookChangeEventListener->OnContactsChanged(*pChangedContactList);
1333                 }
1334                 else
1335                 {
1336                         __pIAddressbookEventListener->OnContactsChanged(*pChangedContactList);
1337                 }
1338         }
1339
1340         pChangedContactList->RemoveAll(true);
1341         delete pChangedContactList;
1342 }
1343
1344 void
1345 _AddressbookManagerImpl::OnCategoryChanged(void)
1346 {
1347         if (__pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
1348         {
1349                 return;
1350         }
1351
1352         IList* pChangedCategoryList = GetChangedGroupsAfterN(__dbVersionForGroup, __dbVersionForGroup);
1353         SysTryReturnVoidResult(NID_SCL, pChangedCategoryList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1354
1355         if (pChangedCategoryList->GetCount() > 0)
1356         {
1357                 if (__pIAddressbookChangeEventListener != null)
1358                 {
1359                         __pIAddressbookChangeEventListener->OnCategoriesChanged(*pChangedCategoryList);
1360                 }
1361                 else
1362                 {
1363                         __pIAddressbookEventListener->OnCategoriesChanged(*pChangedCategoryList);
1364                 }
1365         }
1366
1367         pChangedCategoryList->RemoveAll(true);
1368         delete pChangedCategoryList;
1369 }
1370
1371 void
1372 _AddressbookManagerImpl::OnRelationChanged(void)
1373 {
1374         if (__pIAddressbookEventListener == null)
1375         {
1376                 return;
1377         }
1378
1379         IList* pChangedCategoryList = GetChangedRelationsAfterN(__dbVersionForRelation, __dbVersionForRelation);
1380         SysTryReturnVoidResult(NID_SCL, pChangedCategoryList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1381
1382         if (pChangedCategoryList->GetCount() > 0)
1383         {
1384                 if (__pIAddressbookEventListener != null)
1385                 {
1386                         __pIAddressbookEventListener->OnCategoriesChanged(*pChangedCategoryList);
1387                 }
1388         }
1389
1390         pChangedCategoryList->RemoveAll(true);
1391         delete pChangedCategoryList;
1392 }
1393
1394 result
1395 _AddressbookManagerImpl::RemovePerson(PersonId personId)
1396 {
1397         SysTryReturnResult(NID_SCL, personId > 0, E_INVALID_ARG, "[%s] Invalid argument is used. ", GetErrorMessage(E_INVALID_ARG));
1398         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1399
1400         result r = _AddressbookUtil::DeleteContactRecord(_contacts_person._uri, personId);
1401         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1402
1403         return E_SUCCESS;
1404 }
1405
1406 IList*
1407 _AddressbookManagerImpl::GetAllPersonsN(void) const
1408 {
1409         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1410
1411         ClearLastResult();
1412
1413         unique_ptr< __Filter<__ContactsPersonGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsPersonGroupRel>());
1414
1415         unsigned int propertyIds[] =
1416         {
1417                 _contacts_person_grouprel.person_id,
1418                 _contacts_person_grouprel.display_name,
1419                 _contacts_person_grouprel.image_thumbnail_path,
1420                 _contacts_person_grouprel.ringtone_path,
1421                 _contacts_person_grouprel.is_favorite,
1422                 _contacts_person_grouprel.has_phonenumber,
1423                 _contacts_person_grouprel.has_email,
1424                 _contacts_person_grouprel.addressbook_ids,
1425         };
1426
1427
1428         __Query<__ContactsPersonGroupRel> query;
1429         query.Construct();
1430         query.SetSort(_contacts_person.display_name, true);
1431         query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1432         query.SetDistinct(true);
1433
1434         if (pRwAbFilter->Get() != null)
1435         {
1436                 query.SetFilter(*pRwAbFilter);
1437         }
1438
1439         IList* pPersons = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query);
1440         SysTryReturn(NID_SCL, pPersons != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1441
1442         return pPersons;
1443 }
1444
1445 IList*
1446 _AddressbookManagerImpl::GetPersonsByCategoryN(RecordId categoryId) const
1447 {
1448         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));
1449         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1450
1451         ClearLastResult();
1452
1453         unique_ptr<ContactRecord, ContactRecordDeleter> pCategoryRecord(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, categoryId));
1454         SysTryReturn(NID_SCL, GetLastResult() != E_OBJ_NOT_FOUND, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1455         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, null, E_SYSTEM, "[%s] An system error has been occurred.", GetErrorMessage(E_SYSTEM));
1456
1457         IList* pPersons = null;
1458
1459         unique_ptr< __Filter<__ContactsPersonGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsPersonGroupRel>());
1460
1461         __Filter<__ContactsPersonGroupRel> groupFilter;
1462         groupFilter.Construct();
1463         if (categoryId != INVALID_RECORD_ID)
1464         {
1465                 groupFilter.AddInt(_contacts_person_grouprel.group_id, CONTACTS_MATCH_EQUAL, categoryId);
1466         }
1467         else
1468         {
1469                 groupFilter.AddInt(_contacts_person_grouprel.group_id, CONTACTS_MATCH_NONE, 0);
1470         }
1471
1472         unsigned int propertyIds[] =
1473         {
1474                 _contacts_person_grouprel.person_id,
1475                 _contacts_person_grouprel.display_name,
1476                 _contacts_person_grouprel.image_thumbnail_path,
1477                 _contacts_person_grouprel.ringtone_path,
1478                 _contacts_person_grouprel.is_favorite,
1479                 _contacts_person_grouprel.has_phonenumber,
1480                 _contacts_person_grouprel.has_email,
1481                 _contacts_person_grouprel.addressbook_ids,
1482         };
1483
1484         if (pRwAbFilter->Get() != null)
1485         {
1486                 __Filter<__ContactsPersonGroupRel> mainFilter;
1487                 mainFilter.Construct();
1488
1489                 mainFilter.AddFilter(*pRwAbFilter);
1490                 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1491                 mainFilter.AddFilter(groupFilter);
1492
1493                 __Query<__ContactsPersonGroupRel> query;
1494                 query.Construct();
1495                 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1496                 query.SetFilter(mainFilter);
1497                 query.SetSort(_contacts_person_grouprel.display_name, true);
1498                 query.SetDistinct(true);
1499
1500                 pPersons = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query);
1501                 SysTryReturn(NID_SCL, pPersons != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1502         }
1503         else
1504         {
1505                 __Query<__ContactsPersonGroupRel> query;
1506                 query.Construct();
1507                 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1508                 query.SetFilter(groupFilter);
1509                 query.SetSort(_contacts_person_grouprel.display_name, true);
1510                 query.SetDistinct(true);
1511
1512                 pPersons = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query);
1513                 SysTryReturn(NID_SCL, pPersons != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1514
1515         }
1516
1517         return pPersons;
1518 }
1519
1520 IList*
1521 _AddressbookManagerImpl::GetFavoritePersonsN() const
1522 {
1523         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1524
1525         ClearLastResult();
1526
1527         __Filter<__ContactsPerson> filter;
1528         filter.Construct();
1529         filter.AddBool(_contacts_person.is_favorite, true);
1530
1531         __Query<__ContactsPerson> query;
1532         query.Construct();
1533         query.SetFilter(filter);
1534         query.SetSort(_contacts_person.display_name, true);
1535
1536         IList* pPersons = _AddressbookUtil::SearchWithQueryN<__ContactsPerson, Person>(query);
1537         SysTryReturn(NID_SCL, pPersons != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1538
1539         return pPersons;
1540 }
1541
1542 IList*
1543 _AddressbookManagerImpl::SearchPersonsN(const Tizen::Base::String& keyword) const
1544 {
1545         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1546         SysTryReturn(NID_SCL, !keyword.IsEmpty(), null, E_INVALID_ARG, "Invalid argument is used. keyword is empty string.", GetErrorMessage(E_INVALID_ARG));
1547
1548         ClearLastResult();
1549
1550         contacts_record_h currentRecord = null;
1551         unique_ptr<Person> pPerson(null);
1552
1553         unique_ptr<ArrayList, AllElementsDeleter> pPersonList(new (std::nothrow) ArrayList());
1554         SysTryReturn(NID_SCL, pPersonList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1555
1556         result r = pPersonList->Construct();
1557         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1558
1559         unique_ptr<__SearchResult<__ContactsPerson> > pSearchResult(_AddressbookUtil::Search<__ContactsPerson>(keyword));
1560         SysTryReturn(NID_SCL, pSearchResult != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1561
1562         while (pSearchResult->MoveNext() == E_SUCCESS)
1563         {
1564                 currentRecord = pSearchResult->GetCurrentRecord();
1565                 SysTryReturn(NID_SCL, currentRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1566
1567                 pPerson.reset(__ContactsPerson::ConvertHandleTo<Person>(currentRecord));
1568                 SysTryReturn(NID_SCL, pPerson != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1569
1570                 r = pPersonList->Add(*pPerson);
1571                 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
1572
1573                 pPerson.release();
1574         }
1575
1576         return pPersonList.release();
1577 }
1578
1579 result
1580 _AddressbookManagerImpl::SetPersonAsFavorite(PersonId personId, bool isFavorite)
1581 {
1582         SysTryReturn(NID_SCL, personId > 0, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. personId = %d.", GetErrorMessage(E_INVALID_ARG), personId);
1583         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1584
1585         unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, personId));
1586         SysTryReturn(NID_SCL, pPersonRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1587
1588         bool boolValue = false;
1589         contacts_record_get_bool(pPersonRecord.get(), _contacts_person.is_favorite, &boolValue);
1590
1591         if (boolValue != isFavorite)
1592         {
1593                 contacts_record_set_bool(pPersonRecord.get(), _contacts_person.is_favorite, isFavorite);
1594
1595                 result r = _AddressbookUtil::UpdateContactRecord(pPersonRecord.get());
1596                 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1597         }
1598
1599         return E_SUCCESS;
1600 }
1601
1602 result
1603 _AddressbookManagerImpl::MergePersons(PersonId sourcePersonId, PersonId targetPersonId)
1604 {
1605         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);
1606         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1607
1608         int ret = contacts_person_link_person(targetPersonId, sourcePersonId);
1609         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1610         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1611
1612         return E_SUCCESS;
1613 }
1614
1615 result
1616 _AddressbookManagerImpl::UnlinkContact(PersonId personId, RecordId contactId, PersonId& newPersonId)
1617 {
1618         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);
1619         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1620
1621         int ret = contacts_person_unlink_contact(personId, contactId, &newPersonId);
1622         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));
1623         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1624
1625         return E_SUCCESS;
1626 }
1627
1628 IList*
1629 _AddressbookManagerImpl::SearchN(const AddressbookFilter& filter, unsigned long propertySortedBy, SortOrder sortOrder, int offset, int maxCount)
1630 {
1631         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1632
1633         ClearLastResult();
1634
1635         IList* pList = null;
1636         bool ascending = false;
1637         unsigned int viewSortPropertyId = 0;
1638
1639         AddressbookFilterType type = _AddressbookFilterImpl::GetInstance(filter)->GetType();
1640         contacts_filter_h filterHandle = _AddressbookFilterImpl::GetInstance(filter)->GetFilterHandle();
1641
1642         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);
1643
1644         if (propertySortedBy != 0 && sortOrder != SORT_ORDER_NONE)
1645         {
1646                 viewSortPropertyId = _AddressbookFilterImpl::GetViewPropertyId(type, propertySortedBy);
1647                 ascending = (sortOrder == SORT_ORDER_ASCENDING) ? true : false;
1648         }
1649
1650         switch(type)
1651         {
1652         case AB_FI_TYPE_ADDRESSBOOK:
1653                 {
1654                         unique_ptr< __Filter<__ContactsAddressbook> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsAddressbook>());
1655
1656                         __Filter<__ContactsAddressbook> abFilter;
1657                         abFilter.Construct(filterHandle);
1658
1659                         if (pRwAbFilter->Get() != null)
1660                         {
1661                                 __Filter<__ContactsAddressbook> mainFilter;
1662                                 mainFilter.Construct();
1663                                 mainFilter.AddFilter(*pRwAbFilter);
1664                                 if (abFilter.Get() != null)
1665                                 {
1666                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1667                                         mainFilter.AddFilter(abFilter);
1668                                 }
1669
1670                                 __Query<__ContactsAddressbook> query;
1671                                 query.Construct();
1672                                 query.SetFilter(mainFilter);
1673
1674                                 if (viewSortPropertyId != 0)
1675                                 {
1676                                         query.SetSort(viewSortPropertyId, ascending);
1677                                 }
1678
1679                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsAddressbook, Addressbook>(query, offset, maxCount);
1680                         }
1681                         else
1682                         {
1683                                 __Query<__ContactsAddressbook> query;
1684                                 query.Construct();
1685                                 query.SetFilter(abFilter);
1686
1687                                 if (viewSortPropertyId != 0)
1688                                 {
1689                                         query.SetSort(viewSortPropertyId, ascending);
1690                                 }
1691
1692                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsAddressbook, Addressbook>(query, offset, maxCount);
1693
1694                         }
1695                 }
1696                 break;
1697         case AB_FI_TYPE_PERSON:
1698                 {
1699                         unique_ptr< __Filter<__ContactsPersonGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsPersonGroupRel>());
1700
1701                         __Filter<__ContactsPersonGroupRel> personFilter;
1702                         personFilter.Construct(filterHandle);
1703
1704                         unsigned int propertyIds[] =
1705                         { _contacts_person_grouprel.person_id,
1706                                 _contacts_person_grouprel.addressbook_ids,
1707                                 _contacts_person_grouprel.is_favorite,
1708                                 _contacts_person_grouprel.has_phonenumber,
1709                                 _contacts_person_grouprel.has_email,
1710                                 _contacts_person_grouprel.image_thumbnail_path,
1711                                 _contacts_person_grouprel.ringtone_path,
1712                                 _contacts_person_grouprel.display_name
1713                         };
1714
1715                         if (pRwAbFilter->Get() != null)
1716                         {
1717                                 __Filter<__ContactsPersonGroupRel> mainFilter;
1718                                 mainFilter.Construct();
1719                                 mainFilter.AddFilter(*pRwAbFilter);
1720                                 if (personFilter.Get() != null)
1721                                 {
1722                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1723                                         mainFilter.AddFilter(personFilter);
1724                                 }
1725
1726                                 __Query<__ContactsPersonGroupRel> query;
1727                                 query.Construct();
1728                                 query.SetFilter(mainFilter);
1729                                 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1730                                 query.SetDistinct(true);
1731
1732                                 if (viewSortPropertyId != 0)
1733                                 {
1734                                         query.SetSort(viewSortPropertyId, ascending);
1735                                 }
1736
1737                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query, offset, maxCount);
1738                         }
1739                         else
1740                         {
1741                                 __Query<__ContactsPersonGroupRel> query;
1742                                 query.Construct();
1743                                 query.SetFilter(personFilter);
1744                                 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1745                                 query.SetDistinct(true);
1746
1747                                 if (viewSortPropertyId != 0)
1748                                 {
1749                                         query.SetSort(viewSortPropertyId, ascending);
1750                                 }
1751
1752                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query, offset, maxCount);
1753                         }
1754                 }
1755                 break;
1756         case AB_FI_TYPE_CONTACT:
1757                 {
1758                         unique_ptr< __Filter<__ContactsContact> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContact>());
1759
1760                         __Filter<__ContactsContact> contactFilter;
1761                         contactFilter.Construct(filterHandle);
1762
1763                         if (pRwAbFilter->Get() != null)
1764                         {
1765                                 __Filter<__ContactsContact> mainFilter;
1766                                 mainFilter.Construct();
1767                                 mainFilter.AddFilter(*pRwAbFilter);
1768                                 if (contactFilter.Get() != null)
1769                                 {
1770                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1771                                         mainFilter.AddFilter(contactFilter);
1772                                 }
1773
1774                                 __Query<__ContactsContact> query;
1775                                 query.Construct();
1776                                 query.SetFilter(mainFilter);
1777
1778                                 if (viewSortPropertyId != 0)
1779                                 {
1780                                         query.SetSort(viewSortPropertyId, ascending);
1781                                 }
1782
1783                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query, offset, maxCount);
1784                         }
1785                         else
1786                         {
1787                                 __Query<__ContactsContact> query;
1788                                 query.Construct();
1789                                 query.SetFilter(contactFilter);
1790
1791                                 if (viewSortPropertyId != 0)
1792                                 {
1793                                         query.SetSort(viewSortPropertyId, ascending);
1794                                 }
1795
1796                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query, offset, maxCount);
1797
1798                         }
1799                 }
1800                 break;
1801         case AB_FI_TYPE_CATEGORY:
1802                 {
1803                         unique_ptr< __Filter<__ContactsGroup> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsGroup>());
1804
1805                         __Filter<__ContactsGroup> groupFilter;
1806                         groupFilter.Construct(filterHandle);
1807
1808                         if (pRwAbFilter->Get() != null)
1809                         {
1810                                 __Filter<__ContactsGroup> mainFilter;
1811                                 mainFilter.Construct();
1812                                 mainFilter.AddFilter(*pRwAbFilter);
1813                                 if (groupFilter.Get() != null)
1814                                 {
1815                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1816                                         mainFilter.AddFilter(groupFilter);
1817                                 }
1818
1819                                 __Query<__ContactsGroup> query;
1820                                 query.Construct();
1821                                 query.SetFilter(mainFilter);
1822
1823                                 if (viewSortPropertyId != 0)
1824                                 {
1825                                         query.SetSort(viewSortPropertyId, ascending);
1826                                 }
1827
1828                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsGroup, Category>(query, offset, maxCount);
1829                         }
1830                         else
1831                         {
1832                                 __Query<__ContactsGroup> query;
1833                                 query.Construct();
1834                                 query.SetFilter(groupFilter);
1835
1836                                 if (viewSortPropertyId != 0)
1837                                 {
1838                                         query.SetSort(viewSortPropertyId, ascending);
1839                                 }
1840
1841                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsGroup, Category>(query, offset, maxCount);
1842                         }
1843                 }
1844                 break;
1845         case AB_FI_TYPE_PHONE_CONTACT:
1846                 {
1847                         unique_ptr< __Filter<__ContactsContactNumber> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactNumber>());
1848
1849                         __Filter<__ContactsContactNumber> numberFilter;
1850                         numberFilter.Construct(filterHandle);
1851
1852                         if (pRwAbFilter->Get() != null)
1853                         {
1854                                 __Filter<__ContactsContactNumber> mainFilter;
1855                                 mainFilter.Construct();
1856                                 mainFilter.AddFilter(*pRwAbFilter);
1857                                 if (numberFilter.Get() != null)
1858                                 {
1859                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1860                                         mainFilter.AddFilter(numberFilter);
1861                                 }
1862
1863                                 __Query<__ContactsContactNumber> query;
1864                                 query.Construct();
1865                                 query.SetFilter(mainFilter);
1866
1867                                 if (viewSortPropertyId != 0)
1868                                 {
1869                                         query.SetSort(viewSortPropertyId, ascending);
1870                                 }
1871
1872                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContactNumber, PhoneNumberContact>(query, offset, maxCount);
1873                         }
1874                         else
1875                         {
1876                                 __Query<__ContactsContactNumber> query;
1877                                 query.Construct();
1878                                 query.SetFilter(numberFilter);
1879
1880                                 if (viewSortPropertyId != 0)
1881                                 {
1882                                         query.SetSort(viewSortPropertyId, ascending);
1883                                 }
1884
1885                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContactNumber, PhoneNumberContact>(query, offset, maxCount);
1886                         }
1887                 }
1888                 break;
1889         case AB_FI_TYPE_EMAIL_CONTACT:
1890                 {
1891                         unique_ptr< __Filter<__ContactsContactEmail> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactEmail>());
1892
1893                         __Filter<__ContactsContactEmail> emailFilter;
1894                         emailFilter.Construct(filterHandle);
1895
1896                         if (pRwAbFilter->Get() != null)
1897                         {
1898                                 __Filter<__ContactsContactEmail> mainFilter;
1899                                 mainFilter.Construct();
1900                                 mainFilter.AddFilter(*pRwAbFilter);
1901                                 if (emailFilter.Get() != null)
1902                                 {
1903                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1904                                         mainFilter.AddFilter(emailFilter);
1905                                 }
1906
1907                                 __Query<__ContactsContactEmail> query;
1908                                 query.Construct();
1909                                 query.SetFilter(mainFilter);
1910
1911                                 if (viewSortPropertyId != 0)
1912                                 {
1913                                         query.SetSort(viewSortPropertyId, ascending);
1914                                 }
1915
1916                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContactEmail, EmailContact>(query, offset, maxCount);
1917                         }
1918                         else
1919                         {
1920                                 __Query<__ContactsContactEmail> query;
1921                                 query.Construct();
1922                                 query.SetFilter(emailFilter);
1923
1924                                 if (viewSortPropertyId != 0)
1925                                 {
1926                                         query.SetSort(viewSortPropertyId, ascending);
1927                                 }
1928
1929                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContactEmail, EmailContact>(query, offset, maxCount);
1930                         }
1931                 }
1932                 break;
1933         default:
1934                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. The filter type is invalid", GetErrorMessage(E_INVALID_ARG));
1935                 pList = null;
1936                 break;
1937         };
1938
1939         return pList;
1940 }
1941
1942 int
1943 _AddressbookManagerImpl::GetMatchedItemCount(const AddressbookFilter& filter)
1944 {
1945         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1946
1947         ClearLastResult();
1948
1949         int count = 0;
1950         AddressbookFilterType type = _AddressbookFilterImpl::GetInstance(filter)->GetType();
1951         contacts_filter_h filterHandle = _AddressbookFilterImpl::GetInstance(filter)->GetFilterHandle();
1952
1953         switch(type)
1954         {
1955         case AB_FI_TYPE_ADDRESSBOOK:
1956                 {
1957                         unique_ptr< __Filter<__ContactsAddressbook> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsAddressbook>());
1958
1959                         __Filter<__ContactsAddressbook> abFilter;
1960                         abFilter.Construct(filterHandle);
1961
1962                         if (pRwAbFilter->Get() != null)
1963                         {
1964                                 __Filter<__ContactsAddressbook> mainFilter;
1965                                 mainFilter.Construct();
1966                                 mainFilter.AddFilter(*pRwAbFilter);
1967                                 if (abFilter.Get() != null)
1968                                 {
1969                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1970                                         mainFilter.AddFilter(abFilter);
1971                                 }
1972
1973                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsAddressbook>(mainFilter.Get());
1974                         }
1975                         else
1976                         {
1977                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsAddressbook>(abFilter.Get());
1978                         }
1979                 }
1980                 break;
1981         case AB_FI_TYPE_PERSON:
1982                 {
1983                         unique_ptr< __Filter<__ContactsPersonGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsPersonGroupRel>());
1984
1985                         __Filter<__ContactsPersonGroupRel> personFilter;
1986                         personFilter.Construct(filterHandle);
1987
1988                         if (pRwAbFilter->Get() != null)
1989                         {
1990                                 __Filter<__ContactsPersonGroupRel> mainFilter;
1991                                 mainFilter.Construct();
1992                                 mainFilter.AddFilter(*pRwAbFilter);
1993                                 if (personFilter.Get() != null)
1994                                 {
1995                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1996                                         mainFilter.AddFilter(personFilter);
1997                                 }
1998
1999                                 unsigned int propertyIds[] = { _contacts_person_grouprel.person_id };
2000
2001                                 __Query<__ContactsPersonGroupRel> query;
2002                                 query.Construct();
2003                                 query.SetFilter(mainFilter);
2004                                 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
2005                                 query.SetDistinct(true);
2006
2007                                 count = _AddressbookUtil::GetCountWithQuery(query);
2008                         }
2009                         else
2010                         {
2011                                 unsigned int propertyIds[] = { _contacts_person_grouprel.person_id };
2012
2013                                 __Query<__ContactsPersonGroupRel> query;
2014                                 query.Construct();
2015                                 query.SetFilter(personFilter);
2016                                 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
2017                                 query.SetDistinct(true);
2018
2019                                 count = _AddressbookUtil::GetCountWithQuery(query);
2020
2021                         }
2022                 }
2023
2024                 break;
2025         case AB_FI_TYPE_CONTACT:
2026                 {
2027                         unique_ptr< __Filter<__ContactsContact> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContact>());
2028
2029                         __Filter<__ContactsContact> contactFilter;
2030                         contactFilter.Construct(filterHandle);
2031
2032                         if (pRwAbFilter->Get() != null)
2033                         {
2034                                 __Filter<__ContactsContact> mainFilter;
2035                                 mainFilter.Construct();
2036                                 mainFilter.AddFilter(*pRwAbFilter);
2037                                 if (contactFilter.Get() != null)
2038                                 {
2039                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
2040                                         mainFilter.AddFilter(contactFilter);
2041                                 }
2042
2043                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContact>(mainFilter.Get());
2044                         }
2045                         else
2046                         {
2047                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContact>(contactFilter.Get());
2048                         }
2049                 }
2050                 break;
2051         case AB_FI_TYPE_CATEGORY:
2052                 {
2053                         unique_ptr< __Filter<__ContactsGroup> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsGroup>());
2054
2055                         __Filter<__ContactsGroup> groupFilter;
2056                         groupFilter.Construct(filterHandle);
2057
2058                         if (pRwAbFilter->Get() != null)
2059                         {
2060                                 __Filter<__ContactsGroup> mainFilter;
2061                                 mainFilter.Construct();
2062                                 mainFilter.AddFilter(*pRwAbFilter);
2063                                 if (groupFilter.Get() != null)
2064                                 {
2065                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
2066                                         mainFilter.AddFilter(groupFilter);
2067                                 }
2068
2069                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsGroup>(mainFilter.Get());
2070                         }
2071                         else
2072                         {
2073                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsGroup>(groupFilter.Get());
2074                         }
2075                 }
2076                 break;
2077         case AB_FI_TYPE_PHONE_CONTACT:
2078                 {
2079                         unique_ptr< __Filter<__ContactsContactNumber> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactNumber>());
2080
2081                         __Filter<__ContactsContactNumber> numberFilter;
2082                         numberFilter.Construct(filterHandle);
2083
2084                         if (pRwAbFilter->Get() != null)
2085                         {
2086                                 __Filter<__ContactsContactNumber> mainFilter;
2087                                 mainFilter.Construct();
2088                                 mainFilter.AddFilter(*pRwAbFilter);
2089                                 if (numberFilter.Get() != null)
2090                                 {
2091                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
2092                                         mainFilter.AddFilter(numberFilter);
2093                                 }
2094
2095                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContactNumber>(mainFilter.Get());
2096                         }
2097                         else
2098                         {
2099                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContactNumber>(numberFilter.Get());
2100                         }
2101                 }
2102                 break;
2103         case AB_FI_TYPE_EMAIL_CONTACT:
2104                 {
2105                         unique_ptr< __Filter<__ContactsContactEmail> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactEmail>());
2106
2107                         __Filter<__ContactsContactEmail> emailFilter;
2108                         emailFilter.Construct(filterHandle);
2109
2110                         if (pRwAbFilter->Get() != null)
2111                         {
2112                                 __Filter<__ContactsContactEmail> mainFilter;
2113                                 mainFilter.Construct();
2114                                 mainFilter.AddFilter(*pRwAbFilter);
2115                                 if (emailFilter.Get() != null)
2116                                 {
2117                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
2118                                         mainFilter.AddFilter(emailFilter);
2119                                 }
2120
2121                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContactEmail>(mainFilter.Get());
2122                         }
2123                         else
2124                         {
2125                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContactEmail>(emailFilter.Get());
2126                         }
2127                 }
2128                 break;
2129         default:
2130                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the filter is invalid", GetErrorMessage(GetLastResult()));
2131                 count = 0;
2132                 break;
2133         };
2134
2135         return count;
2136 }
2137
2138 bool
2139 _AddressbookManagerImpl::OnEachContact(contacts_record_h recordHandle, void* pUserData)
2140 {
2141         IList* pList = static_cast<IList*>(pUserData);
2142
2143         ClearLastResult();
2144
2145         unique_ptr<Contact> pContact(new (std::nothrow) Contact());
2146         SysTryReturn(NID_SCL, pContact != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2147
2148         contacts_record_h newRecordHandle = null;
2149
2150         contacts_record_clone(recordHandle, &newRecordHandle);
2151         SysTryReturn(NID_SCL, newRecordHandle != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2152
2153         _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(newRecordHandle);
2154
2155         result r = pList->Add(*pContact);
2156         SysTryReturn(NID_SCL, !IsFailed(r), false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2157
2158         pContact.release();
2159
2160         return true;
2161 }
2162
2163 IList*
2164 _AddressbookManagerImpl::ParseContactsFromVcardN(const Tizen::Base::String& vcardPath)
2165 {
2166         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2167
2168         ClearLastResult();
2169
2170         File file;
2171         result r = file.Construct(vcardPath, "r");
2172         SysTryReturn(NID_SCL, r != E_INVALID_ARG, null, E_INVALID_ARG, "[%s] Invalid argument is used..", GetErrorMessage(E_INVALID_ARG));
2173         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));
2174         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));
2175         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2176
2177         unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
2178
2179         unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(vcardPath));
2180
2181         int ret = contacts_vcard_parse_to_contact_foreach(pCharArray.get(), OnEachContact, pList.get());
2182         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2183         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2184         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2185
2186         return pList.release();
2187 }
2188
2189 result
2190 _AddressbookManagerImpl::ExportPersonToVcard(const Person& person, const Tizen::Base::String& vcardPath)
2191 {
2192         bool exist = File::IsFileExist(vcardPath);
2193         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2194         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2195         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2196
2197         File file;
2198         result r = file.Construct(vcardPath, "w");
2199         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));
2200         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2201         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2202
2203         unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, person.GetId()));
2204         SysTryReturn(NID_SCL, pPersonRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2205
2206         char* pVcardStream = null;
2207         int ret = contacts_vcard_make_from_person(pPersonRecord.get(), &pVcardStream);
2208         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));
2209         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2210
2211         r = file.Write(pVcardStream, strlen(pVcardStream));
2212         free(pVcardStream);
2213         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2214         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2215
2216         return E_SUCCESS;
2217 }
2218
2219 result
2220 _AddressbookManagerImpl::ExportPersonsToVcard(const Tizen::Base::Collection::IList& personList, const Tizen::Base::String& vcardPath)
2221 {
2222         bool exist = File::IsFileExist(vcardPath);
2223         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2224         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2225         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2226
2227         int ret = CONTACTS_ERROR_NONE;
2228         char* pVcardStream = null;
2229         File file;
2230
2231         result r = file.Construct(vcardPath, "w");
2232         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));
2233         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2234         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2235
2236
2237         unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(null);
2238
2239         unique_ptr<IEnumerator> pEnum(personList.GetEnumeratorN());
2240         SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2241
2242         while (pEnum->MoveNext() == E_SUCCESS)
2243         {
2244                 Person* pPerson = static_cast<Person*>(pEnum->GetCurrent());
2245
2246                 pPersonRecord.reset(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, pPerson->GetId()));
2247                 SysTryReturn(NID_SCL, pPersonRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2248
2249                 ret = contacts_vcard_make_from_person(pPersonRecord.get(), &pVcardStream);
2250                 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));
2251                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2252
2253                 r = file.Write(pVcardStream, strlen(pVcardStream));
2254                 free(pVcardStream);
2255                 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2256                 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2257         }
2258
2259         return E_SUCCESS;
2260 }
2261
2262 result
2263 _AddressbookManagerImpl::ExportContactToVcard(const Contact& contact, const Tizen::Base::String& vcardPath)
2264 {
2265         bool exist = File::IsFileExist(vcardPath);
2266         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2267         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2268         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2269
2270         File file;
2271         result r = file.Construct(vcardPath, "w");
2272         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));
2273         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2274         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2275
2276         contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
2277
2278         char* pVcardStream = null;
2279
2280         int ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
2281         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));
2282         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2283
2284         r = file.Write(pVcardStream, strlen(pVcardStream));
2285         free(pVcardStream);
2286         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2287         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2288
2289         return E_SUCCESS;
2290 }
2291
2292 result
2293 _AddressbookManagerImpl::ExportContactsToVcard(const Tizen::Base::Collection::IList& contactList, const Tizen::Base::String& vcardPath)
2294 {
2295         bool exist = File::IsFileExist(vcardPath);
2296         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2297         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2298         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2299
2300         int ret = CONTACTS_ERROR_NONE;
2301         char* pVcardStream = null;
2302         File file;
2303         Contact* pContact = null;
2304
2305         result r = file.Construct(vcardPath, "w");
2306         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));
2307         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2308         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2309
2310         contacts_record_h recordHandle = null;
2311
2312         unique_ptr<IEnumerator> pEnum(contactList.GetEnumeratorN());
2313         SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2314
2315         while (pEnum->MoveNext() == E_SUCCESS)
2316         {
2317                 pContact = static_cast<Contact*>(pEnum->GetCurrent());
2318
2319                 recordHandle = _ContactImpl::GetInstance(*pContact)->GetContactRecordHandle();
2320
2321                 ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
2322                 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));
2323                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2324
2325                 r = file.Write(pVcardStream, strlen(pVcardStream));
2326                 free(pVcardStream);
2327                 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2328                 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2329         }
2330
2331         return E_SUCCESS;
2332 }
2333
2334 ByteBuffer*
2335 _AddressbookManagerImpl::ExportContactToVcardStreamN(const Contact& contact)
2336 {
2337         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2338
2339         ClearLastResult();
2340
2341         contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
2342
2343         char* pVcardStream = null;
2344         int ret = CONTACTS_ERROR_NONE;
2345
2346         ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
2347         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2348         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2349         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2350
2351         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2352         if (pByteBuffer == null)
2353         {
2354                 free(pVcardStream);
2355                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2356
2357                 return null;
2358         }
2359
2360         result r = pByteBuffer->Construct(strlen(pVcardStream));
2361         if (IsFailed(r))
2362         {
2363                 free(pVcardStream);
2364                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2365
2366                 return null;
2367         }
2368
2369         r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2370         free(pVcardStream);
2371         SysTryReturn(NID_SCL, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2372         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2373
2374         return pByteBuffer.release();
2375 }
2376
2377 ByteBuffer*
2378 _AddressbookManagerImpl::ExportContactsToVcardStreamN(const Tizen::Base::Collection::IList& contactList)
2379 {
2380         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2381
2382         ClearLastResult();
2383
2384         char* pVcardStream = null;
2385         int ret = CONTACTS_ERROR_NONE;
2386         Contact* pContact = null;
2387         result r = E_SUCCESS;
2388         int capacity = 0;
2389
2390         contacts_record_h recordHandle = null;
2391
2392         unique_ptr<IEnumerator> pEnum(contactList.GetEnumeratorN());
2393         SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2394
2395         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2396         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2397
2398         r = pByteBuffer->Construct(capacity);
2399         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2400
2401         if (contactList.GetCount() == 0)
2402         {
2403                 return pByteBuffer.release();
2404         }
2405
2406         while (pEnum->MoveNext() == E_SUCCESS)
2407         {
2408                 pContact = static_cast<Contact*>(pEnum->GetCurrent());
2409
2410                 recordHandle = _ContactImpl::GetInstance(*pContact)->GetContactRecordHandle();
2411
2412                 ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
2413                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2414                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2415                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2416
2417                 capacity += strlen(pVcardStream);
2418                 r = pByteBuffer->ExpandCapacity(capacity);
2419                 if (IsFailed(r))
2420                 {
2421                         free(pVcardStream);
2422                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2423
2424                         return null;
2425                 }
2426
2427                 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2428                 free(pVcardStream);
2429                 pVcardStream = null;
2430                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.: capacity(%d), size(%d)", GetErrorMessage(E_SYSTEM));
2431         }
2432
2433         return pByteBuffer.release();
2434 }
2435
2436 ByteBuffer*
2437 _AddressbookManagerImpl::ExportPersonToVcardStreamN(const Person& person)
2438 {
2439         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2440
2441         ClearLastResult();
2442
2443         unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, person.GetId()));
2444         SysTryReturn(NID_SCL, GetLastResult() != E_OBJ_NOT_FOUND,  null, E_INVALID_ARG, "[%s] Person does not exist.", GetErrorMessage(E_INVALID_ARG));
2445         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS,  null, E_INVALID_ARG, "[%s] Person does not exist.", GetErrorMessage(E_INVALID_ARG));
2446
2447         char* pVcardStream = null;
2448         int ret = contacts_vcard_make_from_person(pPersonRecord.get(), &pVcardStream);
2449         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2450         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2451         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2452
2453         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2454         if (pByteBuffer == null)
2455         {
2456                 free(pVcardStream);
2457                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2458
2459                 return null;
2460         }
2461
2462         result r = pByteBuffer->Construct(strlen(pVcardStream));
2463         if (IsFailed(r))
2464         {
2465                 free(pVcardStream);
2466                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2467
2468                 return null;
2469         }
2470
2471         r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2472         free(pVcardStream);
2473         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2474
2475         return pByteBuffer.release();
2476 }
2477
2478 ByteBuffer*
2479 _AddressbookManagerImpl::ExportPersonsToVcardStreamN(const Tizen::Base::Collection::IList& personList)
2480 {
2481         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2482
2483         ClearLastResult();
2484
2485         int ret = CONTACTS_ERROR_NONE;
2486         Person* pPerson = null;
2487         char* pVcardStream = null;
2488
2489         int capacity = 0;
2490
2491         unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(null);
2492
2493         unique_ptr<IEnumerator> pEnum(personList.GetEnumeratorN());
2494         SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2495
2496         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2497         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2498
2499         result r = pByteBuffer->Construct(capacity);
2500         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2501
2502         if (personList.GetCount() == 0)
2503         {
2504                 return pByteBuffer.release();
2505         }
2506
2507         while (pEnum->MoveNext() == E_SUCCESS)
2508         {
2509                 pPerson = static_cast<Person*>(pEnum->GetCurrent());
2510
2511                 pPersonRecord.reset(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, pPerson->GetId()));
2512                 SysTryReturn(NID_SCL, GetLastResult() != E_OBJ_NOT_FOUND,  null, E_INVALID_ARG, "[%s] Person does not exist.", GetErrorMessage(E_INVALID_ARG));
2513                 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS,  null, E_INVALID_ARG, "[%s] Person does not exist.", GetErrorMessage(E_INVALID_ARG));
2514
2515                 ret = contacts_vcard_make_from_person(pPersonRecord.get(), &pVcardStream);
2516                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2517                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2518                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2519
2520                 capacity += strlen(pVcardStream);
2521                 r = pByteBuffer->ExpandCapacity(capacity);
2522                 if (IsFailed(r))
2523                 {
2524                         free(pVcardStream);
2525                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2526
2527                         return null;
2528                 }
2529
2530                 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2531                 free(pVcardStream);
2532                 pVcardStream = null;
2533                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2534         }
2535
2536         return pByteBuffer.release();
2537 }
2538
2539 IList*
2540 _AddressbookManagerImpl::ParseVcardStreamN(const Tizen::Base::ByteBuffer& vcardStream)
2541 {
2542         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2543
2544         ClearLastResult();
2545
2546         contacts_list_h listHandle = null;
2547         result r = E_SUCCESS;
2548
2549         int ret = contacts_vcard_parse_to_contacts(reinterpret_cast<char*>(const_cast<byte*>(vcardStream.GetPointer())), &listHandle);
2550         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2551         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2552
2553         unsigned int count = 0;
2554         contacts_record_h recordHandle = null;
2555         contacts_list_get_count(listHandle, &count);
2556
2557         unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
2558         if (pList == null)
2559         {
2560                 contacts_list_destroy(listHandle, true);
2561                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2562
2563                 return null;
2564         }
2565
2566         r = pList->Construct(count);
2567         if (IsFailed(r))
2568         {
2569                 contacts_list_destroy(listHandle, true);
2570                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2571
2572                 return null;
2573         }
2574
2575         for (unsigned int i = 0; i < count; i++)
2576         {
2577                 unique_ptr<Contact> pContact(new (std::nothrow) Contact());
2578                 if (pContact == null)
2579                 {
2580                         contacts_list_destroy(listHandle, true);
2581                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2582
2583                         return null;
2584                 }
2585
2586                 r = pList->Add(pContact.get());
2587                 if (IsFailed(r))
2588                 {
2589                         contacts_list_destroy(listHandle, true);
2590                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2591
2592                         return null;
2593                 }
2594
2595                 pContact.release();
2596         }
2597
2598         unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
2599
2600         while (pEnum->MoveNext() == E_SUCCESS)
2601         {
2602                 Contact* pContact = static_cast <Contact*> (pEnum->GetCurrent());
2603
2604                 contacts_list_get_current_record_p(listHandle, &recordHandle);
2605                 _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(recordHandle);
2606
2607                 ret = contacts_list_next(listHandle);
2608         }
2609
2610         contacts_list_destroy(listHandle, false);
2611
2612         return pList.release();
2613 }
2614
2615 ByteBuffer*
2616 _AddressbookManagerImpl::ExportUserProfileToVcardStreamN(const UserProfile& userProfile)
2617 {
2618         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2619
2620         ClearLastResult();
2621
2622         char* pVcardStream = null;
2623         int ret = CONTACTS_ERROR_NONE;
2624         contacts_record_h recordHandle = null;
2625
2626         recordHandle = _UserProfileImpl::GetInstance(userProfile)->GetUserProfileHandle();
2627
2628         ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2629         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2630         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2631         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2632
2633         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2634         if (pByteBuffer == null)
2635         {
2636                 free(pVcardStream);
2637                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2638
2639                 return null;
2640         }
2641         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2642
2643         result r = pByteBuffer->Construct(strlen(pVcardStream));
2644         if (IsFailed(r))
2645         {
2646                 free(pVcardStream);
2647                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2648
2649                 return null;
2650         }
2651
2652         r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2653         free(pVcardStream);
2654         SysTryReturn(NID_SCL, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2655         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2656
2657         return pByteBuffer.release();
2658 }
2659
2660 ByteBuffer*
2661 _AddressbookManagerImpl::ExportUserProfilesToVcardStreamN(const Tizen::Base::Collection::IList& userProfileList)
2662 {
2663         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2664
2665         ClearLastResult();
2666
2667         char* pVcardStream = null;
2668         int ret = CONTACTS_ERROR_NONE;
2669         UserProfile* pProfile = null;
2670         result r = E_SUCCESS;
2671         int capacity = 0;
2672
2673         unique_ptr<IEnumerator> pEnum(userProfileList.GetEnumeratorN());
2674         SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2675
2676         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2677         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2678
2679         r = pByteBuffer->Construct(capacity);
2680         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2681
2682         if (userProfileList.GetCount() == 0)
2683         {
2684                 return pByteBuffer.release();
2685         }
2686
2687         while (pEnum->MoveNext() == E_SUCCESS)
2688         {
2689                 contacts_record_h recordHandle = null;
2690
2691                 pProfile = static_cast<UserProfile*>(pEnum->GetCurrent());
2692
2693                 recordHandle = _UserProfileImpl::GetInstance(*pProfile)->GetUserProfileHandle();
2694
2695                 ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2696                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2697                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2698                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2699
2700                 capacity += strlen(pVcardStream);
2701                 r = pByteBuffer->ExpandCapacity(capacity);
2702                 if (IsFailed(r))
2703                 {
2704                         free(pVcardStream);
2705                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2706
2707                         return null;
2708                 }
2709
2710                 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2711                 free(pVcardStream);
2712                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.: capacity(%d), size(%d)", GetErrorMessage(E_SYSTEM));
2713         }
2714
2715         return pByteBuffer.release();
2716 }
2717
2718 result
2719 _AddressbookManagerImpl::ExportUserProfileToVcard(const UserProfile& userProfile, const Tizen::Base::String& vcardPath)
2720 {
2721         bool exist = File::IsFileExist(vcardPath);
2722         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2723         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2724         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2725
2726         File file;
2727         result r = file.Construct(vcardPath, "w");
2728         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));
2729         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2730         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2731
2732         char* pVcardStream = null;
2733         contacts_record_h recordHandle = null;
2734
2735         recordHandle = _UserProfileImpl::GetInstance(userProfile)->GetUserProfileHandle();
2736
2737         int ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2738         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));
2739         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2740
2741         r = file.Write(pVcardStream, strlen(pVcardStream));
2742         free(pVcardStream);
2743         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2744         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2745
2746         return E_SUCCESS;
2747 }
2748
2749 result
2750 _AddressbookManagerImpl::ExportUserProfilesToVcard(const Tizen::Base::Collection::IList& userProfileList, const Tizen::Base::String& vcardPath)
2751 {
2752         bool exist = File::IsFileExist(vcardPath);
2753         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2754         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2755         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2756
2757         int ret = CONTACTS_ERROR_NONE;
2758         char* pVcardStream = null;
2759         File file;
2760         UserProfile* pProfile = null;
2761
2762         result r = file.Construct(vcardPath, "w");
2763         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));
2764         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2765         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2766
2767         unique_ptr<IEnumerator> pEnum(userProfileList.GetEnumeratorN());
2768         SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2769
2770         while (pEnum->MoveNext() == E_SUCCESS)
2771         {
2772                 contacts_record_h recordHandle = null;
2773
2774                 pProfile = static_cast<UserProfile*>(pEnum->GetCurrent());
2775
2776                 recordHandle = _UserProfileImpl::GetInstance(*pProfile)->GetUserProfileHandle();
2777
2778                 ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2779                 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));
2780                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2781
2782                 r = file.Write(pVcardStream, strlen(pVcardStream));
2783                 free(pVcardStream);
2784                 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2785                 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2786         }
2787
2788         return E_SUCCESS;
2789 }
2790
2791 IList*
2792 _AddressbookManagerImpl::GetAllUserProfilesN(void) const
2793 {
2794         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2795
2796         ClearLastResult();
2797
2798         unique_ptr< __Filter<__ContactsUserProfile> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsUserProfile>());
2799         SysTryReturn(NID_SCL, pRwAbFilter != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2800
2801         __Query<__ContactsUserProfile> query;
2802         query.Construct();
2803         query.SetSort(_contacts_my_profile.display_name, true);
2804
2805         if (pRwAbFilter->Get() != null)
2806         {
2807                 query.SetFilter(*pRwAbFilter);
2808         }
2809
2810         IList* pUserProfilesList = _AddressbookUtil::SearchWithQueryN<__ContactsUserProfile, UserProfile>(query);
2811         SysTryReturn(NID_SCL, pUserProfilesList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2812
2813         return pUserProfilesList;
2814 }
2815
2816 UserProfile*
2817 _AddressbookManagerImpl::GetUserProfileN(AddressbookId addressbookId) const
2818 {
2819         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2820         SysTryReturn(NID_SCL, addressbookId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. Addressbook Id(%d).", GetErrorMessage(E_INVALID_ARG), addressbookId);
2821
2822         ClearLastResult();
2823
2824         int mode = 0;
2825         unique_ptr<ContactRecord, ContactRecordDeleter> pAbRecord(_AddressbookUtil::GetContactRecordN(_contacts_address_book._uri, addressbookId));
2826         SysTryReturn(NID_SCL, pAbRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2827
2828         contacts_record_get_int(pAbRecord.get(), _contacts_address_book.mode, &mode);
2829         SysTryReturn(NID_SCL, mode == 0, null, E_OBJ_NOT_FOUND, "[%s] The addressbook does not exist.", GetErrorMessage(E_OBJ_NOT_FOUND));
2830
2831         __Filter<__ContactsUserProfile> filter;
2832         filter.Construct();
2833         filter.AddInt(_contacts_my_profile.address_book_id, CONTACTS_MATCH_EQUAL, addressbookId);
2834
2835         __Query<__ContactsUserProfile> query;
2836         query.Construct();
2837         query.SetFilter(filter);
2838
2839         unique_ptr<IList, AllElementsDeleter> pUserProfilesList(_AddressbookUtil::SearchWithQueryN<__ContactsUserProfile, UserProfile>(query));
2840         SysTryReturn(NID_SCL, pUserProfilesList.get() != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2841         SysTryReturn(NID_SCL, pUserProfilesList->GetCount() != 0, null, E_SUCCESS, "No UserProfile Set for this Addressbook.");
2842         SysTryReturn(NID_SCL, pUserProfilesList->GetCount() == 1, null, E_SYSTEM, "[%s] Propagating. More than one UserProfile not allowed.", GetErrorMessage(E_SYSTEM));
2843
2844         UserProfile* pProfile = new (std::nothrow) UserProfile(*(static_cast<UserProfile*>(pUserProfilesList->GetAt(0))));
2845         SysTryReturn(NID_SCL, pProfile != null, null, E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(E_OUT_OF_MEMORY));
2846
2847         return pProfile;
2848 }
2849
2850 contacts_record_h
2851 _AddressbookManagerImpl::CopyContactRecordHandle(contacts_record_h srcHandle)
2852 {
2853         ClearLastResult();
2854
2855         int ret = CONTACTS_ERROR_NONE;
2856         unsigned int i = 0;
2857         unsigned int count = 0;
2858         char* pCharValue = null;
2859         int intValue = 0;
2860         bool boolValue = false;
2861
2862         contacts_record_h contactHandle = null;
2863         contacts_record_h sourceRecordHandle = null;
2864         contacts_record_h destRecordHandle = null;
2865
2866         ret = contacts_record_create(_contacts_contact._uri, &contactHandle);
2867         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2868
2869         // favorite
2870         contacts_record_get_bool(sourceRecordHandle, _contacts_contact.is_favorite, &boolValue);
2871         contacts_record_set_bool(destRecordHandle, _contacts_contact.is_favorite, boolValue);
2872
2873         // uid
2874         contacts_record_get_str_p(sourceRecordHandle, _contacts_contact.uid, &pCharValue);
2875         contacts_record_set_str(destRecordHandle, _contacts_contact.uid, pCharValue);
2876
2877         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2878         // name
2879         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2880         contacts_record_get_child_record_count(srcHandle, _contacts_contact.name, &count);
2881         if (count > 0)
2882         {
2883                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.name, 0, &sourceRecordHandle);
2884
2885                 ret = contacts_record_create(_contacts_name._uri, &destRecordHandle);
2886                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2887
2888                 __ContactsRecordHandle nameHandle(destRecordHandle);
2889
2890                 // 1. first
2891                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.first, &pCharValue);
2892                 contacts_record_set_str(destRecordHandle, _contacts_name.first, pCharValue);
2893
2894                 // 2. last
2895                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.last, &pCharValue);
2896                 contacts_record_set_str(destRecordHandle, _contacts_name.last, pCharValue);
2897
2898                 // 3. addition
2899                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.addition, &pCharValue);
2900                 contacts_record_set_str(destRecordHandle, _contacts_name.addition, pCharValue);
2901
2902                 // 4. suffix
2903                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.suffix, &pCharValue);
2904                 contacts_record_set_str(destRecordHandle, _contacts_name.suffix, pCharValue);
2905
2906                 // 5. prefix
2907                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.prefix, &pCharValue);
2908                 contacts_record_set_str(destRecordHandle, _contacts_name.prefix, pCharValue);
2909
2910                 // 6. phonetic_first
2911                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.phonetic_first, &pCharValue);
2912                 contacts_record_set_str(destRecordHandle, _contacts_name.phonetic_first, pCharValue);
2913
2914                 // 7. phonetic_last
2915                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.phonetic_last, &pCharValue);
2916                 contacts_record_set_str(destRecordHandle, _contacts_name.phonetic_last, pCharValue);
2917
2918                 // 8. phonetic_middle
2919                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.phonetic_middle, &pCharValue);
2920                 contacts_record_set_str(destRecordHandle, _contacts_name.phonetic_middle, pCharValue);
2921
2922                 contacts_record_add_child_record(contactHandle, _contacts_contact.name, destRecordHandle);
2923
2924                 nameHandle.Release();
2925         }
2926
2927         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2928         // image
2929         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2930         contacts_record_get_child_record_count(srcHandle, _contacts_contact.image, &count);
2931         for (i = 0; i < count; i++)
2932         {
2933                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.image, i, &sourceRecordHandle);
2934
2935                 ret = contacts_record_create(_contacts_image._uri, &destRecordHandle);
2936                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2937
2938                 __ContactsRecordHandle imageHandle(destRecordHandle);
2939
2940                 contacts_record_get_int(sourceRecordHandle, _contacts_image.type, &intValue);
2941                 contacts_record_set_int(destRecordHandle, _contacts_image.type, intValue);
2942
2943                 contacts_record_get_str_p(sourceRecordHandle, _contacts_image.label, &pCharValue);
2944                 contacts_record_set_str(destRecordHandle, _contacts_image.label, pCharValue);
2945
2946                 contacts_record_get_str_p(sourceRecordHandle, _contacts_image.path, &pCharValue);
2947                 contacts_record_set_str(destRecordHandle, _contacts_image.path, pCharValue);
2948
2949                 contacts_record_add_child_record(contactHandle, _contacts_contact.image, destRecordHandle);
2950
2951                 imageHandle.Release();
2952         }
2953
2954         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2955         // company
2956         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2957         contacts_record_get_child_record_count(srcHandle, _contacts_contact.company, &count);
2958         for (i = 0; i < count; i++)
2959         {
2960                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.company, i, &sourceRecordHandle);
2961
2962                 ret = contacts_record_create(_contacts_company._uri, &destRecordHandle);
2963                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2964
2965                 __ContactsRecordHandle companyHandle(destRecordHandle);
2966
2967                 contacts_record_get_int(sourceRecordHandle, _contacts_company.type, &intValue);
2968                 contacts_record_set_int(destRecordHandle, _contacts_company.type, intValue);
2969
2970                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.name, &pCharValue);
2971                 contacts_record_set_str(destRecordHandle, _contacts_company.name, pCharValue);
2972
2973                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.department, &pCharValue);
2974                 contacts_record_set_str(destRecordHandle, _contacts_company.department, pCharValue);
2975
2976                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.job_title, &pCharValue);
2977                 contacts_record_set_str(destRecordHandle, _contacts_company.job_title, pCharValue);
2978
2979                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.assistant_name, &pCharValue);
2980                 contacts_record_set_str(destRecordHandle, _contacts_company.assistant_name, pCharValue);
2981
2982                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.role, &pCharValue);
2983                 contacts_record_set_str(destRecordHandle, _contacts_company.role, pCharValue);
2984
2985                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.logo, &pCharValue);
2986                 contacts_record_set_str(destRecordHandle, _contacts_company.logo, pCharValue);
2987
2988                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.location, &pCharValue);
2989                 contacts_record_set_str(destRecordHandle, _contacts_company.location, pCharValue);
2990
2991                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.description, &pCharValue);
2992                 contacts_record_set_str(destRecordHandle, _contacts_company.description, pCharValue);
2993
2994                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.phonetic_name, &pCharValue);
2995                 contacts_record_set_str(destRecordHandle, _contacts_company.phonetic_name, pCharValue);
2996
2997                 contacts_record_add_child_record(contactHandle, _contacts_contact.company, destRecordHandle);
2998
2999                 companyHandle.Release();
3000         }
3001
3002         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3003         // note
3004         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3005         contacts_record_get_child_record_count(srcHandle, _contacts_contact.note, &count);
3006         for (i = 0; i < count; i++)
3007         {
3008                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.note, i, &sourceRecordHandle);
3009
3010                 ret = contacts_record_create(_contacts_note._uri, &destRecordHandle);
3011                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3012
3013                 __ContactsRecordHandle noteHandle(destRecordHandle);
3014
3015                 contacts_record_get_str_p(sourceRecordHandle, _contacts_note.note, &pCharValue);
3016                 contacts_record_set_str(destRecordHandle, _contacts_note.note, pCharValue);
3017
3018                 contacts_record_add_child_record(contactHandle, _contacts_contact.note, destRecordHandle);
3019
3020                 noteHandle.Release();
3021         }
3022
3023         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3024         // phone number
3025         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3026         contacts_record_get_child_record_count(srcHandle, _contacts_contact.number, &count);
3027         for (i = 0; i < count; i++)
3028         {
3029                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.number, i, &sourceRecordHandle);
3030
3031                 ret = contacts_record_create(_contacts_number._uri, &destRecordHandle);
3032                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3033
3034                 __ContactsRecordHandle numberHandle(destRecordHandle);
3035
3036                 contacts_record_get_int(sourceRecordHandle, _contacts_number.type, &intValue);
3037                 contacts_record_set_int(destRecordHandle, _contacts_number.type, intValue);
3038
3039                 contacts_record_get_str_p(sourceRecordHandle, _contacts_number.label, &pCharValue);
3040                 contacts_record_set_str(destRecordHandle, _contacts_number.label, pCharValue);
3041
3042                 contacts_record_get_str_p(sourceRecordHandle, _contacts_number.number, &pCharValue);
3043                 contacts_record_set_str(destRecordHandle, _contacts_number.number, pCharValue);
3044
3045                 contacts_record_add_child_record(contactHandle, _contacts_contact.number, destRecordHandle);
3046
3047                 numberHandle.Release();
3048         }
3049
3050         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3051         // email
3052         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3053         contacts_record_get_child_record_count(srcHandle, _contacts_contact.email, &count);
3054         for (i = 0; i < count; i++)
3055         {
3056                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.email, i, &sourceRecordHandle);
3057
3058                 ret = contacts_record_create(_contacts_email._uri, &destRecordHandle);
3059                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3060
3061                 __ContactsRecordHandle emailHandle(destRecordHandle);
3062
3063                 contacts_record_get_int(sourceRecordHandle, _contacts_email.type, &intValue);
3064                 contacts_record_set_int(destRecordHandle, _contacts_email.type, intValue);
3065
3066                 contacts_record_get_str_p(sourceRecordHandle, _contacts_email.label, &pCharValue);
3067                 contacts_record_set_str(destRecordHandle, _contacts_email.label, pCharValue);
3068
3069                 contacts_record_get_str_p(sourceRecordHandle, _contacts_email.email, &pCharValue);
3070                 contacts_record_set_str(destRecordHandle, _contacts_email.email, pCharValue);
3071
3072                 contacts_record_add_child_record(contactHandle, _contacts_contact.email, destRecordHandle);
3073
3074                 emailHandle.Release();
3075
3076         }
3077
3078         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3079         // event
3080         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3081         contacts_record_get_child_record_count(srcHandle, _contacts_contact.event, &count);
3082         for (i = 0; i < count; i++)
3083         {
3084                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.event, i, &sourceRecordHandle);
3085
3086                 ret = contacts_record_create(_contacts_event._uri, &destRecordHandle);
3087                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3088
3089                 __ContactsRecordHandle eventHandle(destRecordHandle);
3090
3091                 contacts_record_get_int(sourceRecordHandle, _contacts_event.type, &intValue);
3092                 contacts_record_set_int(destRecordHandle, _contacts_event.type, intValue);
3093
3094                 contacts_record_get_str_p(sourceRecordHandle, _contacts_event.label, &pCharValue);
3095                 contacts_record_set_str(destRecordHandle, _contacts_event.label, pCharValue);
3096
3097                 contacts_record_get_int(sourceRecordHandle, _contacts_event.date, &intValue);
3098                 contacts_record_set_int(destRecordHandle, _contacts_event.date, intValue);
3099
3100                 contacts_record_add_child_record(contactHandle, _contacts_contact.event, destRecordHandle);
3101
3102                 eventHandle.Release();
3103         }
3104
3105         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3106         // im address
3107         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3108         contacts_record_get_child_record_count(srcHandle, _contacts_contact.messenger, &count);
3109         for (i = 0; i < count; i++)
3110         {
3111                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.messenger, i, &sourceRecordHandle);
3112
3113                 ret = contacts_record_create(_contacts_messenger._uri, &destRecordHandle);
3114                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3115
3116                 __ContactsRecordHandle imAddressHandle(destRecordHandle);
3117
3118                 contacts_record_get_int(sourceRecordHandle, _contacts_messenger.type, &intValue);
3119                 contacts_record_set_int(destRecordHandle, _contacts_messenger.type, intValue);
3120
3121                 contacts_record_get_str_p(sourceRecordHandle, _contacts_messenger.label, &pCharValue);
3122                 contacts_record_set_str(destRecordHandle, _contacts_messenger.label, pCharValue);
3123
3124                 contacts_record_get_str_p(sourceRecordHandle, _contacts_messenger.im_id, &pCharValue);
3125                 contacts_record_set_str(destRecordHandle, _contacts_messenger.im_id, pCharValue);
3126
3127                 contacts_record_add_child_record(contactHandle, _contacts_contact.messenger, destRecordHandle);
3128
3129                 imAddressHandle.Release();
3130         }
3131
3132         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3133         // address
3134         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3135         contacts_record_get_child_record_count(srcHandle, _contacts_contact.address, &count);
3136         for (i = 0; i < count; i++)
3137         {
3138                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.address, i, &sourceRecordHandle);
3139
3140                 ret = contacts_record_create(_contacts_address._uri, &destRecordHandle);
3141                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3142
3143                 __ContactsRecordHandle addressHandle(destRecordHandle);
3144
3145                 contacts_record_get_int(sourceRecordHandle, _contacts_address.type, &intValue);
3146                 contacts_record_set_int(destRecordHandle, _contacts_address.type, intValue);
3147
3148                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.label, &pCharValue);
3149                 contacts_record_set_str(destRecordHandle, _contacts_address.label, pCharValue);
3150
3151                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.postbox, &pCharValue);
3152                 contacts_record_set_str(destRecordHandle, _contacts_address.postbox, pCharValue);
3153
3154                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.extended, &pCharValue);
3155                 contacts_record_set_str(destRecordHandle, _contacts_address.extended, pCharValue);
3156
3157                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.street, &pCharValue);
3158                 contacts_record_set_str(destRecordHandle, _contacts_address.street, pCharValue);
3159
3160                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.locality, &pCharValue);
3161                 contacts_record_set_str(destRecordHandle, _contacts_address.locality, pCharValue);
3162
3163                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.region, &pCharValue);
3164                 contacts_record_set_str(destRecordHandle, _contacts_address.region, pCharValue);
3165
3166                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.postal_code, &pCharValue);
3167                 contacts_record_set_str(destRecordHandle, _contacts_address.postal_code, pCharValue);
3168
3169                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.country, &pCharValue);
3170                 contacts_record_set_str(destRecordHandle, _contacts_address.country, pCharValue);
3171
3172                 contacts_record_add_child_record(contactHandle, _contacts_contact.address, destRecordHandle);
3173
3174                 addressHandle.Release();
3175         }
3176
3177         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3178         // url
3179         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3180         contacts_record_get_child_record_count(srcHandle, _contacts_contact.url, &count);
3181         for (i = 0; i < count; i++)
3182         {
3183                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.url, i, &sourceRecordHandle);
3184
3185                 ret = contacts_record_create(_contacts_url._uri, &destRecordHandle);
3186                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3187
3188                 __ContactsRecordHandle urlHandle(destRecordHandle);
3189
3190                 contacts_record_get_int(sourceRecordHandle, _contacts_url.type, &intValue);
3191                 contacts_record_set_int(destRecordHandle, _contacts_url.type, intValue);
3192
3193                 contacts_record_get_str_p(sourceRecordHandle, _contacts_url.label, &pCharValue);
3194                 contacts_record_set_str(destRecordHandle, _contacts_url.label, pCharValue);
3195
3196                 contacts_record_get_str_p(sourceRecordHandle, _contacts_url.url, &pCharValue);
3197                 contacts_record_set_str(destRecordHandle, _contacts_url.url, pCharValue);
3198
3199                 contacts_record_add_child_record(contactHandle, _contacts_contact.url, destRecordHandle);
3200
3201                 urlHandle.Release();
3202         }
3203
3204         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3205         // nickname
3206         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3207         contacts_record_get_child_record_count(srcHandle, _contacts_contact.nickname, &count);
3208         for (i = 0; i < count; i++)
3209         {
3210                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.nickname, i, &sourceRecordHandle);
3211
3212                 ret = contacts_record_create(_contacts_nickname._uri, &destRecordHandle);
3213                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3214
3215                 __ContactsRecordHandle nicknameHandle(destRecordHandle);
3216
3217                 contacts_record_get_str_p(sourceRecordHandle, _contacts_nickname.name, &pCharValue);
3218                 contacts_record_set_str(destRecordHandle, _contacts_nickname.name, pCharValue);
3219
3220                 contacts_record_add_child_record(contactHandle, _contacts_contact.nickname, destRecordHandle);
3221
3222                 nicknameHandle.Release();
3223         }
3224
3225         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3226         // relationship
3227         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3228         contacts_record_get_child_record_count(srcHandle, _contacts_contact.relationship, &count);
3229         for (i = 0; i < count; i++)
3230         {
3231                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.relationship, i, &sourceRecordHandle);
3232
3233                 ret = contacts_record_create(_contacts_relationship._uri, &destRecordHandle);
3234                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3235
3236                 __ContactsRecordHandle relationshipHandle(destRecordHandle);
3237
3238                 contacts_record_get_str_p(sourceRecordHandle, _contacts_relationship.name, &pCharValue);
3239                 contacts_record_set_str(destRecordHandle, _contacts_relationship.name, pCharValue);
3240
3241                 contacts_record_add_child_record(contactHandle, _contacts_contact.relationship, destRecordHandle);
3242
3243                 relationshipHandle.Release();
3244         }
3245
3246         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3247         // app launch data
3248         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3249         contacts_record_get_child_record_count(srcHandle, _contacts_contact.profile, &count);
3250         for (i = 0; i < count; i++)
3251         {
3252                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.profile, i, &sourceRecordHandle);
3253
3254                 ret = contacts_record_create(_contacts_profile._uri, &destRecordHandle);
3255                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3256
3257                 __ContactsRecordHandle profileHandle(destRecordHandle);
3258
3259                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.text, &pCharValue);
3260                 contacts_record_set_str(destRecordHandle, _contacts_profile.text, pCharValue);
3261
3262                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.uid, &pCharValue);
3263                 contacts_record_set_str(destRecordHandle, _contacts_profile.uid, pCharValue);
3264
3265                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.app_id, &pCharValue);
3266                 contacts_record_set_str(destRecordHandle, _contacts_profile.app_id, pCharValue);
3267
3268                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.service_operation, &pCharValue);
3269                 contacts_record_set_str(destRecordHandle, _contacts_profile.service_operation, pCharValue);
3270
3271                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.uri, &pCharValue);
3272                 contacts_record_set_str(destRecordHandle, _contacts_profile.uri, pCharValue);
3273
3274                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.category, &pCharValue);
3275                 contacts_record_set_str(destRecordHandle, _contacts_profile.category, pCharValue);
3276
3277                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.mime, &pCharValue);
3278                 contacts_record_set_str(destRecordHandle, _contacts_profile.mime, pCharValue);
3279
3280                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.extra_data, &pCharValue);
3281                 contacts_record_set_str(destRecordHandle, _contacts_profile.extra_data, pCharValue);
3282
3283                 contacts_record_add_child_record(contactHandle, _contacts_contact.profile, destRecordHandle);
3284
3285                 profileHandle.Release();
3286         }
3287
3288         return contactHandle;
3289 }
3290
3291 _AddressbookManagerImpl*
3292 _AddressbookManagerImpl::GetInstance(AddressbookManager& addressbookManager)
3293 {
3294         return addressbookManager.__pAddressbookManagerImpl;
3295 }
3296
3297 const _AddressbookManagerImpl*
3298 _AddressbookManagerImpl::GetInstance(const AddressbookManager& addressbookManager)
3299 {
3300         return addressbookManager.__pAddressbookManagerImpl;
3301 }
3302
3303 }}  // Tizen::Social