Initialize Tizen 2.3
[framework/osp/social.git] / src / FScl_AddressbookManagerImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 /**
17  * @file                FScl_AddressbookManagerImpl.cpp
18  * @brief               This is the implementation for _AddressbookManagerImpl class.
19  *
20  * This file contains definitions of @e _AddressbookManagerImpl class.
21  */
22 #include <contacts.h>
23 #include <unique_ptr.h>
24 #include <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         if (categoryId > INVALID_RECORD_ID)
1454         {
1455                 unique_ptr<ContactRecord, ContactRecordDeleter> pCategoryRecord(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, categoryId));
1456                 SysTryReturn(NID_SCL, GetLastResult() != E_OBJ_NOT_FOUND, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1457                 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, null, E_SYSTEM, "[%s] An system error has been occurred.", GetErrorMessage(E_SYSTEM));
1458         }
1459
1460         IList* pPersons = null;
1461
1462         unique_ptr< __Filter<__ContactsPersonGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsPersonGroupRel>());
1463
1464         __Filter<__ContactsPersonGroupRel> groupFilter;
1465         groupFilter.Construct();
1466         if (categoryId != INVALID_RECORD_ID)
1467         {
1468                 groupFilter.AddInt(_contacts_person_grouprel.group_id, CONTACTS_MATCH_EQUAL, categoryId);
1469         }
1470         else
1471         {
1472                 groupFilter.AddInt(_contacts_person_grouprel.group_id, CONTACTS_MATCH_NONE, 0);
1473         }
1474
1475         unsigned int propertyIds[] =
1476         {
1477                 _contacts_person_grouprel.person_id,
1478                 _contacts_person_grouprel.display_name,
1479                 _contacts_person_grouprel.image_thumbnail_path,
1480                 _contacts_person_grouprel.ringtone_path,
1481                 _contacts_person_grouprel.is_favorite,
1482                 _contacts_person_grouprel.has_phonenumber,
1483                 _contacts_person_grouprel.has_email,
1484                 _contacts_person_grouprel.addressbook_ids,
1485         };
1486
1487         if (pRwAbFilter->Get() != null)
1488         {
1489                 __Filter<__ContactsPersonGroupRel> mainFilter;
1490                 mainFilter.Construct();
1491
1492                 mainFilter.AddFilter(*pRwAbFilter);
1493                 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1494                 mainFilter.AddFilter(groupFilter);
1495
1496                 __Query<__ContactsPersonGroupRel> query;
1497                 query.Construct();
1498                 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1499                 query.SetFilter(mainFilter);
1500                 query.SetSort(_contacts_person_grouprel.display_name, true);
1501                 query.SetDistinct(true);
1502
1503                 pPersons = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query);
1504                 SysTryReturn(NID_SCL, pPersons != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1505         }
1506         else
1507         {
1508                 __Query<__ContactsPersonGroupRel> query;
1509                 query.Construct();
1510                 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1511                 query.SetFilter(groupFilter);
1512                 query.SetSort(_contacts_person_grouprel.display_name, true);
1513                 query.SetDistinct(true);
1514
1515                 pPersons = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query);
1516                 SysTryReturn(NID_SCL, pPersons != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1517
1518         }
1519
1520         return pPersons;
1521 }
1522
1523 IList*
1524 _AddressbookManagerImpl::GetFavoritePersonsN() const
1525 {
1526         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1527
1528         ClearLastResult();
1529
1530         __Filter<__ContactsPerson> filter;
1531         filter.Construct();
1532         filter.AddBool(_contacts_person.is_favorite, true);
1533
1534         __Query<__ContactsPerson> query;
1535         query.Construct();
1536         query.SetFilter(filter);
1537         query.SetSort(_contacts_person.display_name, true);
1538
1539         IList* pPersons = _AddressbookUtil::SearchWithQueryN<__ContactsPerson, Person>(query);
1540         SysTryReturn(NID_SCL, pPersons != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1541
1542         return pPersons;
1543 }
1544
1545 IList*
1546 _AddressbookManagerImpl::SearchPersonsN(const Tizen::Base::String& keyword) const
1547 {
1548         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1549         SysTryReturn(NID_SCL, !keyword.IsEmpty(), null, E_INVALID_ARG, "Invalid argument is used. keyword is empty string.", GetErrorMessage(E_INVALID_ARG));
1550
1551         ClearLastResult();
1552
1553         contacts_record_h currentRecord = null;
1554         unique_ptr<Person> pPerson(null);
1555
1556         unique_ptr<ArrayList, AllElementsDeleter> pPersonList(new (std::nothrow) ArrayList());
1557         SysTryReturn(NID_SCL, pPersonList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1558
1559         result r = pPersonList->Construct();
1560         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1561
1562         unique_ptr<__SearchResult<__ContactsPerson> > pSearchResult(_AddressbookUtil::Search<__ContactsPerson>(keyword));
1563         SysTryReturn(NID_SCL, pSearchResult != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1564
1565         while (pSearchResult->MoveNext() == E_SUCCESS)
1566         {
1567                 currentRecord = pSearchResult->GetCurrentRecord();
1568                 SysTryReturn(NID_SCL, currentRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1569
1570                 pPerson.reset(__ContactsPerson::ConvertHandleTo<Person>(currentRecord));
1571                 SysTryReturn(NID_SCL, pPerson != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1572
1573                 r = pPersonList->Add(*pPerson);
1574                 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
1575
1576                 pPerson.release();
1577         }
1578
1579         return pPersonList.release();
1580 }
1581
1582 result
1583 _AddressbookManagerImpl::SetPersonAsFavorite(PersonId personId, bool isFavorite)
1584 {
1585         SysTryReturn(NID_SCL, personId > 0, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. personId = %d.", GetErrorMessage(E_INVALID_ARG), personId);
1586         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1587
1588         unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, personId));
1589         SysTryReturn(NID_SCL, pPersonRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1590
1591         bool boolValue = false;
1592         contacts_record_get_bool(pPersonRecord.get(), _contacts_person.is_favorite, &boolValue);
1593
1594         if (boolValue != isFavorite)
1595         {
1596                 contacts_record_set_bool(pPersonRecord.get(), _contacts_person.is_favorite, isFavorite);
1597
1598                 result r = _AddressbookUtil::UpdateContactRecord(pPersonRecord.get());
1599                 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
1600         }
1601
1602         return E_SUCCESS;
1603 }
1604
1605 result
1606 _AddressbookManagerImpl::MergePersons(PersonId sourcePersonId, PersonId targetPersonId)
1607 {
1608         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);
1609         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1610
1611         int ret = contacts_person_link_person(targetPersonId, sourcePersonId);
1612         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1613         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1614
1615         return E_SUCCESS;
1616 }
1617
1618 result
1619 _AddressbookManagerImpl::UnlinkContact(PersonId personId, RecordId contactId, PersonId& newPersonId)
1620 {
1621         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);
1622         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1623
1624         int ret = contacts_person_unlink_contact(personId, contactId, &newPersonId);
1625         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));
1626         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1627
1628         return E_SUCCESS;
1629 }
1630
1631 IList*
1632 _AddressbookManagerImpl::SearchN(const AddressbookFilter& filter, unsigned long propertySortedBy, SortOrder sortOrder, int offset, int maxCount)
1633 {
1634         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1635
1636         ClearLastResult();
1637
1638         IList* pList = null;
1639         bool ascending = false;
1640         unsigned int viewSortPropertyId = 0;
1641
1642         AddressbookFilterType type = _AddressbookFilterImpl::GetInstance(filter)->GetType();
1643         contacts_filter_h filterHandle = _AddressbookFilterImpl::GetInstance(filter)->GetFilterHandle();
1644
1645         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);
1646
1647         if (propertySortedBy != 0 && sortOrder != SORT_ORDER_NONE)
1648         {
1649                 viewSortPropertyId = _AddressbookFilterImpl::GetViewPropertyId(type, propertySortedBy);
1650                 ascending = (sortOrder == SORT_ORDER_ASCENDING) ? true : false;
1651         }
1652
1653         switch(type)
1654         {
1655         case AB_FI_TYPE_ADDRESSBOOK:
1656                 {
1657                         unique_ptr< __Filter<__ContactsAddressbook> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsAddressbook>());
1658
1659                         __Filter<__ContactsAddressbook> abFilter;
1660                         abFilter.Construct(filterHandle);
1661
1662                         if (pRwAbFilter->Get() != null)
1663                         {
1664                                 __Filter<__ContactsAddressbook> mainFilter;
1665                                 mainFilter.Construct();
1666                                 mainFilter.AddFilter(*pRwAbFilter);
1667                                 if (abFilter.Get() != null)
1668                                 {
1669                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1670                                         mainFilter.AddFilter(abFilter);
1671                                 }
1672
1673                                 __Query<__ContactsAddressbook> query;
1674                                 query.Construct();
1675                                 query.SetFilter(mainFilter);
1676
1677                                 if (viewSortPropertyId != 0)
1678                                 {
1679                                         query.SetSort(viewSortPropertyId, ascending);
1680                                 }
1681
1682                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsAddressbook, Addressbook>(query, offset, maxCount);
1683                         }
1684                         else
1685                         {
1686                                 __Query<__ContactsAddressbook> query;
1687                                 query.Construct();
1688                                 query.SetFilter(abFilter);
1689
1690                                 if (viewSortPropertyId != 0)
1691                                 {
1692                                         query.SetSort(viewSortPropertyId, ascending);
1693                                 }
1694
1695                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsAddressbook, Addressbook>(query, offset, maxCount);
1696
1697                         }
1698                 }
1699                 break;
1700         case AB_FI_TYPE_PERSON:
1701                 {
1702                         unique_ptr< __Filter<__ContactsPersonGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsPersonGroupRel>());
1703
1704                         __Filter<__ContactsPersonGroupRel> personFilter;
1705                         personFilter.Construct(filterHandle);
1706
1707                         unsigned int propertyIds[] =
1708                         { _contacts_person_grouprel.person_id,
1709                                 _contacts_person_grouprel.addressbook_ids,
1710                                 _contacts_person_grouprel.is_favorite,
1711                                 _contacts_person_grouprel.has_phonenumber,
1712                                 _contacts_person_grouprel.has_email,
1713                                 _contacts_person_grouprel.image_thumbnail_path,
1714                                 _contacts_person_grouprel.ringtone_path,
1715                                 _contacts_person_grouprel.display_name
1716                         };
1717
1718                         if (pRwAbFilter->Get() != null)
1719                         {
1720                                 __Filter<__ContactsPersonGroupRel> mainFilter;
1721                                 mainFilter.Construct();
1722                                 mainFilter.AddFilter(*pRwAbFilter);
1723                                 if (personFilter.Get() != null)
1724                                 {
1725                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1726                                         mainFilter.AddFilter(personFilter);
1727                                 }
1728
1729                                 __Query<__ContactsPersonGroupRel> query;
1730                                 query.Construct();
1731                                 query.SetFilter(mainFilter);
1732                                 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1733                                 query.SetDistinct(true);
1734
1735                                 if (viewSortPropertyId != 0)
1736                                 {
1737                                         query.SetSort(viewSortPropertyId, ascending);
1738                                 }
1739
1740                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query, offset, maxCount);
1741                         }
1742                         else
1743                         {
1744                                 __Query<__ContactsPersonGroupRel> query;
1745                                 query.Construct();
1746                                 query.SetFilter(personFilter);
1747                                 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1748                                 query.SetDistinct(true);
1749
1750                                 if (viewSortPropertyId != 0)
1751                                 {
1752                                         query.SetSort(viewSortPropertyId, ascending);
1753                                 }
1754
1755                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query, offset, maxCount);
1756                         }
1757                 }
1758                 break;
1759         case AB_FI_TYPE_CONTACT:
1760                 {
1761                         unique_ptr< __Filter<__ContactsContact> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContact>());
1762
1763                         __Filter<__ContactsContact> contactFilter;
1764                         contactFilter.Construct(filterHandle);
1765
1766                         if (pRwAbFilter->Get() != null)
1767                         {
1768                                 __Filter<__ContactsContact> mainFilter;
1769                                 mainFilter.Construct();
1770                                 mainFilter.AddFilter(*pRwAbFilter);
1771                                 if (contactFilter.Get() != null)
1772                                 {
1773                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1774                                         mainFilter.AddFilter(contactFilter);
1775                                 }
1776
1777                                 __Query<__ContactsContact> query;
1778                                 query.Construct();
1779                                 query.SetFilter(mainFilter);
1780
1781                                 if (viewSortPropertyId != 0)
1782                                 {
1783                                         query.SetSort(viewSortPropertyId, ascending);
1784                                 }
1785
1786                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query, offset, maxCount);
1787                         }
1788                         else
1789                         {
1790                                 __Query<__ContactsContact> query;
1791                                 query.Construct();
1792                                 query.SetFilter(contactFilter);
1793
1794                                 if (viewSortPropertyId != 0)
1795                                 {
1796                                         query.SetSort(viewSortPropertyId, ascending);
1797                                 }
1798
1799                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query, offset, maxCount);
1800
1801                         }
1802                 }
1803                 break;
1804         case AB_FI_TYPE_CATEGORY:
1805                 {
1806                         unique_ptr< __Filter<__ContactsGroup> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsGroup>());
1807
1808                         __Filter<__ContactsGroup> groupFilter;
1809                         groupFilter.Construct(filterHandle);
1810
1811                         if (pRwAbFilter->Get() != null)
1812                         {
1813                                 __Filter<__ContactsGroup> mainFilter;
1814                                 mainFilter.Construct();
1815                                 mainFilter.AddFilter(*pRwAbFilter);
1816                                 if (groupFilter.Get() != null)
1817                                 {
1818                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1819                                         mainFilter.AddFilter(groupFilter);
1820                                 }
1821
1822                                 __Query<__ContactsGroup> query;
1823                                 query.Construct();
1824                                 query.SetFilter(mainFilter);
1825
1826                                 if (viewSortPropertyId != 0)
1827                                 {
1828                                         query.SetSort(viewSortPropertyId, ascending);
1829                                 }
1830
1831                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsGroup, Category>(query, offset, maxCount);
1832                         }
1833                         else
1834                         {
1835                                 __Query<__ContactsGroup> query;
1836                                 query.Construct();
1837                                 query.SetFilter(groupFilter);
1838
1839                                 if (viewSortPropertyId != 0)
1840                                 {
1841                                         query.SetSort(viewSortPropertyId, ascending);
1842                                 }
1843
1844                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsGroup, Category>(query, offset, maxCount);
1845                         }
1846                 }
1847                 break;
1848         case AB_FI_TYPE_PHONE_CONTACT:
1849                 {
1850                         unique_ptr< __Filter<__ContactsContactNumber> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactNumber>());
1851
1852                         __Filter<__ContactsContactNumber> numberFilter;
1853                         numberFilter.Construct(filterHandle);
1854
1855                         if (pRwAbFilter->Get() != null)
1856                         {
1857                                 __Filter<__ContactsContactNumber> mainFilter;
1858                                 mainFilter.Construct();
1859                                 mainFilter.AddFilter(*pRwAbFilter);
1860                                 if (numberFilter.Get() != null)
1861                                 {
1862                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1863                                         mainFilter.AddFilter(numberFilter);
1864                                 }
1865
1866                                 __Query<__ContactsContactNumber> query;
1867                                 query.Construct();
1868                                 query.SetFilter(mainFilter);
1869
1870                                 if (viewSortPropertyId != 0)
1871                                 {
1872                                         query.SetSort(viewSortPropertyId, ascending);
1873                                 }
1874
1875                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContactNumber, PhoneNumberContact>(query, offset, maxCount);
1876                         }
1877                         else
1878                         {
1879                                 __Query<__ContactsContactNumber> query;
1880                                 query.Construct();
1881                                 query.SetFilter(numberFilter);
1882
1883                                 if (viewSortPropertyId != 0)
1884                                 {
1885                                         query.SetSort(viewSortPropertyId, ascending);
1886                                 }
1887
1888                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContactNumber, PhoneNumberContact>(query, offset, maxCount);
1889                         }
1890                 }
1891                 break;
1892         case AB_FI_TYPE_EMAIL_CONTACT:
1893                 {
1894                         unique_ptr< __Filter<__ContactsContactEmail> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactEmail>());
1895
1896                         __Filter<__ContactsContactEmail> emailFilter;
1897                         emailFilter.Construct(filterHandle);
1898
1899                         if (pRwAbFilter->Get() != null)
1900                         {
1901                                 __Filter<__ContactsContactEmail> mainFilter;
1902                                 mainFilter.Construct();
1903                                 mainFilter.AddFilter(*pRwAbFilter);
1904                                 if (emailFilter.Get() != null)
1905                                 {
1906                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1907                                         mainFilter.AddFilter(emailFilter);
1908                                 }
1909
1910                                 __Query<__ContactsContactEmail> query;
1911                                 query.Construct();
1912                                 query.SetFilter(mainFilter);
1913
1914                                 if (viewSortPropertyId != 0)
1915                                 {
1916                                         query.SetSort(viewSortPropertyId, ascending);
1917                                 }
1918
1919                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContactEmail, EmailContact>(query, offset, maxCount);
1920                         }
1921                         else
1922                         {
1923                                 __Query<__ContactsContactEmail> query;
1924                                 query.Construct();
1925                                 query.SetFilter(emailFilter);
1926
1927                                 if (viewSortPropertyId != 0)
1928                                 {
1929                                         query.SetSort(viewSortPropertyId, ascending);
1930                                 }
1931
1932                                 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContactEmail, EmailContact>(query, offset, maxCount);
1933                         }
1934                 }
1935                 break;
1936         default:
1937                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. The filter type is invalid", GetErrorMessage(E_INVALID_ARG));
1938                 pList = null;
1939                 break;
1940         };
1941
1942         return pList;
1943 }
1944
1945 int
1946 _AddressbookManagerImpl::GetMatchedItemCount(const AddressbookFilter& filter)
1947 {
1948         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1949
1950         ClearLastResult();
1951
1952         int count = 0;
1953         AddressbookFilterType type = _AddressbookFilterImpl::GetInstance(filter)->GetType();
1954         contacts_filter_h filterHandle = _AddressbookFilterImpl::GetInstance(filter)->GetFilterHandle();
1955
1956         switch(type)
1957         {
1958         case AB_FI_TYPE_ADDRESSBOOK:
1959                 {
1960                         unique_ptr< __Filter<__ContactsAddressbook> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsAddressbook>());
1961
1962                         __Filter<__ContactsAddressbook> abFilter;
1963                         abFilter.Construct(filterHandle);
1964
1965                         if (pRwAbFilter->Get() != null)
1966                         {
1967                                 __Filter<__ContactsAddressbook> mainFilter;
1968                                 mainFilter.Construct();
1969                                 mainFilter.AddFilter(*pRwAbFilter);
1970                                 if (abFilter.Get() != null)
1971                                 {
1972                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1973                                         mainFilter.AddFilter(abFilter);
1974                                 }
1975
1976                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsAddressbook>(mainFilter.Get());
1977                         }
1978                         else
1979                         {
1980                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsAddressbook>(abFilter.Get());
1981                         }
1982                 }
1983                 break;
1984         case AB_FI_TYPE_PERSON:
1985                 {
1986                         unique_ptr< __Filter<__ContactsPersonGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsPersonGroupRel>());
1987
1988                         __Filter<__ContactsPersonGroupRel> personFilter;
1989                         personFilter.Construct(filterHandle);
1990
1991                         if (pRwAbFilter->Get() != null)
1992                         {
1993                                 __Filter<__ContactsPersonGroupRel> mainFilter;
1994                                 mainFilter.Construct();
1995                                 mainFilter.AddFilter(*pRwAbFilter);
1996                                 if (personFilter.Get() != null)
1997                                 {
1998                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1999                                         mainFilter.AddFilter(personFilter);
2000                                 }
2001
2002                                 unsigned int propertyIds[] = { _contacts_person_grouprel.person_id };
2003
2004                                 __Query<__ContactsPersonGroupRel> query;
2005                                 query.Construct();
2006                                 query.SetFilter(mainFilter);
2007                                 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
2008                                 query.SetDistinct(true);
2009
2010                                 count = _AddressbookUtil::GetCountWithQuery(query);
2011                         }
2012                         else
2013                         {
2014                                 unsigned int propertyIds[] = { _contacts_person_grouprel.person_id };
2015
2016                                 __Query<__ContactsPersonGroupRel> query;
2017                                 query.Construct();
2018                                 query.SetFilter(personFilter);
2019                                 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
2020                                 query.SetDistinct(true);
2021
2022                                 count = _AddressbookUtil::GetCountWithQuery(query);
2023
2024                         }
2025                 }
2026
2027                 break;
2028         case AB_FI_TYPE_CONTACT:
2029                 {
2030                         unique_ptr< __Filter<__ContactsContact> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContact>());
2031
2032                         __Filter<__ContactsContact> contactFilter;
2033                         contactFilter.Construct(filterHandle);
2034
2035                         if (pRwAbFilter->Get() != null)
2036                         {
2037                                 __Filter<__ContactsContact> mainFilter;
2038                                 mainFilter.Construct();
2039                                 mainFilter.AddFilter(*pRwAbFilter);
2040                                 if (contactFilter.Get() != null)
2041                                 {
2042                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
2043                                         mainFilter.AddFilter(contactFilter);
2044                                 }
2045
2046                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContact>(mainFilter.Get());
2047                         }
2048                         else
2049                         {
2050                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContact>(contactFilter.Get());
2051                         }
2052                 }
2053                 break;
2054         case AB_FI_TYPE_CATEGORY:
2055                 {
2056                         unique_ptr< __Filter<__ContactsGroup> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsGroup>());
2057
2058                         __Filter<__ContactsGroup> groupFilter;
2059                         groupFilter.Construct(filterHandle);
2060
2061                         if (pRwAbFilter->Get() != null)
2062                         {
2063                                 __Filter<__ContactsGroup> mainFilter;
2064                                 mainFilter.Construct();
2065                                 mainFilter.AddFilter(*pRwAbFilter);
2066                                 if (groupFilter.Get() != null)
2067                                 {
2068                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
2069                                         mainFilter.AddFilter(groupFilter);
2070                                 }
2071
2072                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsGroup>(mainFilter.Get());
2073                         }
2074                         else
2075                         {
2076                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsGroup>(groupFilter.Get());
2077                         }
2078                 }
2079                 break;
2080         case AB_FI_TYPE_PHONE_CONTACT:
2081                 {
2082                         unique_ptr< __Filter<__ContactsContactNumber> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactNumber>());
2083
2084                         __Filter<__ContactsContactNumber> numberFilter;
2085                         numberFilter.Construct(filterHandle);
2086
2087                         if (pRwAbFilter->Get() != null)
2088                         {
2089                                 __Filter<__ContactsContactNumber> mainFilter;
2090                                 mainFilter.Construct();
2091                                 mainFilter.AddFilter(*pRwAbFilter);
2092                                 if (numberFilter.Get() != null)
2093                                 {
2094                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
2095                                         mainFilter.AddFilter(numberFilter);
2096                                 }
2097
2098                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContactNumber>(mainFilter.Get());
2099                         }
2100                         else
2101                         {
2102                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContactNumber>(numberFilter.Get());
2103                         }
2104                 }
2105                 break;
2106         case AB_FI_TYPE_EMAIL_CONTACT:
2107                 {
2108                         unique_ptr< __Filter<__ContactsContactEmail> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactEmail>());
2109
2110                         __Filter<__ContactsContactEmail> emailFilter;
2111                         emailFilter.Construct(filterHandle);
2112
2113                         if (pRwAbFilter->Get() != null)
2114                         {
2115                                 __Filter<__ContactsContactEmail> mainFilter;
2116                                 mainFilter.Construct();
2117                                 mainFilter.AddFilter(*pRwAbFilter);
2118                                 if (emailFilter.Get() != null)
2119                                 {
2120                                         mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
2121                                         mainFilter.AddFilter(emailFilter);
2122                                 }
2123
2124                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContactEmail>(mainFilter.Get());
2125                         }
2126                         else
2127                         {
2128                                 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContactEmail>(emailFilter.Get());
2129                         }
2130                 }
2131                 break;
2132         default:
2133                 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the filter is invalid", GetErrorMessage(GetLastResult()));
2134                 count = 0;
2135                 break;
2136         };
2137
2138         return count;
2139 }
2140
2141 bool
2142 _AddressbookManagerImpl::OnEachContact(contacts_record_h recordHandle, void* pUserData)
2143 {
2144         IList* pList = static_cast<IList*>(pUserData);
2145
2146         ClearLastResult();
2147
2148         unique_ptr<Contact> pContact(new (std::nothrow) Contact());
2149         SysTryReturn(NID_SCL, pContact != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2150
2151         contacts_record_h newRecordHandle = null;
2152
2153         contacts_record_clone(recordHandle, &newRecordHandle);
2154         SysTryReturn(NID_SCL, newRecordHandle != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2155
2156         _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(newRecordHandle);
2157
2158         result r = pList->Add(*pContact);
2159         SysTryReturn(NID_SCL, !IsFailed(r), false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2160
2161         pContact.release();
2162
2163         return true;
2164 }
2165
2166 IList*
2167 _AddressbookManagerImpl::ParseContactsFromVcardN(const Tizen::Base::String& vcardPath)
2168 {
2169         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2170
2171         ClearLastResult();
2172
2173         File file;
2174         result r = file.Construct(vcardPath, "r");
2175         SysTryReturn(NID_SCL, r != E_INVALID_ARG, null, E_INVALID_ARG, "[%s] Invalid argument is used..", GetErrorMessage(E_INVALID_ARG));
2176         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));
2177         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));
2178         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2179
2180         unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
2181
2182         unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(vcardPath));
2183
2184         int ret = contacts_vcard_parse_to_contact_foreach(pCharArray.get(), OnEachContact, pList.get());
2185         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2186         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2187         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2188
2189         return pList.release();
2190 }
2191
2192 result
2193 _AddressbookManagerImpl::ExportPersonToVcard(const Person& person, const Tizen::Base::String& vcardPath)
2194 {
2195         bool exist = File::IsFileExist(vcardPath);
2196         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2197         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2198         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2199
2200         File file;
2201         result r = file.Construct(vcardPath, "w");
2202         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));
2203         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2204         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2205
2206         unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, person.GetId()));
2207         SysTryReturn(NID_SCL, pPersonRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2208
2209         char* pVcardStream = null;
2210         int ret = contacts_vcard_make_from_person(pPersonRecord.get(), &pVcardStream);
2211         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));
2212         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2213
2214         r = file.Write(pVcardStream, strlen(pVcardStream));
2215         free(pVcardStream);
2216         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2217         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2218
2219         return E_SUCCESS;
2220 }
2221
2222 result
2223 _AddressbookManagerImpl::ExportPersonsToVcard(const Tizen::Base::Collection::IList& personList, const Tizen::Base::String& vcardPath)
2224 {
2225         bool exist = File::IsFileExist(vcardPath);
2226         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2227         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2228         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2229
2230         int ret = CONTACTS_ERROR_NONE;
2231         char* pVcardStream = null;
2232         File file;
2233
2234         result r = file.Construct(vcardPath, "w");
2235         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));
2236         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2237         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2238
2239
2240         unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(null);
2241
2242         unique_ptr<IEnumerator> pEnum(personList.GetEnumeratorN());
2243         SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2244
2245         while (pEnum->MoveNext() == E_SUCCESS)
2246         {
2247                 Person* pPerson = static_cast<Person*>(pEnum->GetCurrent());
2248
2249                 pPersonRecord.reset(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, pPerson->GetId()));
2250                 SysTryReturn(NID_SCL, pPersonRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2251
2252                 ret = contacts_vcard_make_from_person(pPersonRecord.get(), &pVcardStream);
2253                 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));
2254                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2255
2256                 r = file.Write(pVcardStream, strlen(pVcardStream));
2257                 free(pVcardStream);
2258                 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2259                 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2260         }
2261
2262         return E_SUCCESS;
2263 }
2264
2265 result
2266 _AddressbookManagerImpl::ExportContactToVcard(const Contact& contact, const Tizen::Base::String& vcardPath)
2267 {
2268         bool exist = File::IsFileExist(vcardPath);
2269         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2270         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2271         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2272
2273         File file;
2274         result r = file.Construct(vcardPath, "w");
2275         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));
2276         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2277         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2278
2279         contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
2280
2281         char* pVcardStream = null;
2282
2283         int ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
2284         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));
2285         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2286
2287         r = file.Write(pVcardStream, strlen(pVcardStream));
2288         free(pVcardStream);
2289         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2290         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2291
2292         return E_SUCCESS;
2293 }
2294
2295 result
2296 _AddressbookManagerImpl::ExportContactsToVcard(const Tizen::Base::Collection::IList& contactList, const Tizen::Base::String& vcardPath)
2297 {
2298         bool exist = File::IsFileExist(vcardPath);
2299         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2300         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2301         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2302
2303         int ret = CONTACTS_ERROR_NONE;
2304         char* pVcardStream = null;
2305         File file;
2306         Contact* pContact = null;
2307
2308         result r = file.Construct(vcardPath, "w");
2309         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));
2310         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2311         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2312
2313         contacts_record_h recordHandle = null;
2314
2315         unique_ptr<IEnumerator> pEnum(contactList.GetEnumeratorN());
2316         SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2317
2318         while (pEnum->MoveNext() == E_SUCCESS)
2319         {
2320                 pContact = static_cast<Contact*>(pEnum->GetCurrent());
2321
2322                 recordHandle = _ContactImpl::GetInstance(*pContact)->GetContactRecordHandle();
2323
2324                 ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
2325                 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));
2326                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2327
2328                 r = file.Write(pVcardStream, strlen(pVcardStream));
2329                 free(pVcardStream);
2330                 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2331                 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2332         }
2333
2334         return E_SUCCESS;
2335 }
2336
2337 ByteBuffer*
2338 _AddressbookManagerImpl::ExportContactToVcardStreamN(const Contact& contact)
2339 {
2340         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2341
2342         ClearLastResult();
2343
2344         contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
2345
2346         char* pVcardStream = null;
2347         int ret = CONTACTS_ERROR_NONE;
2348
2349         ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
2350         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2351         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2352         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2353
2354         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2355         if (pByteBuffer == null)
2356         {
2357                 free(pVcardStream);
2358                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2359
2360                 return null;
2361         }
2362
2363         result r = pByteBuffer->Construct(strlen(pVcardStream));
2364         if (IsFailed(r))
2365         {
2366                 free(pVcardStream);
2367                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2368
2369                 return null;
2370         }
2371
2372         r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2373         free(pVcardStream);
2374         SysTryReturn(NID_SCL, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2375         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2376
2377         return pByteBuffer.release();
2378 }
2379
2380 ByteBuffer*
2381 _AddressbookManagerImpl::ExportContactsToVcardStreamN(const Tizen::Base::Collection::IList& contactList)
2382 {
2383         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2384
2385         ClearLastResult();
2386
2387         char* pVcardStream = null;
2388         int ret = CONTACTS_ERROR_NONE;
2389         Contact* pContact = null;
2390         result r = E_SUCCESS;
2391         int capacity = 0;
2392
2393         contacts_record_h recordHandle = null;
2394
2395         unique_ptr<IEnumerator> pEnum(contactList.GetEnumeratorN());
2396         SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2397
2398         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2399         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2400
2401         r = pByteBuffer->Construct(capacity);
2402         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2403
2404         if (contactList.GetCount() == 0)
2405         {
2406                 return pByteBuffer.release();
2407         }
2408
2409         while (pEnum->MoveNext() == E_SUCCESS)
2410         {
2411                 pContact = static_cast<Contact*>(pEnum->GetCurrent());
2412
2413                 recordHandle = _ContactImpl::GetInstance(*pContact)->GetContactRecordHandle();
2414
2415                 ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
2416                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2417                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2418                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2419
2420                 capacity += strlen(pVcardStream);
2421                 r = pByteBuffer->ExpandCapacity(capacity);
2422                 if (IsFailed(r))
2423                 {
2424                         free(pVcardStream);
2425                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2426
2427                         return null;
2428                 }
2429
2430                 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2431                 free(pVcardStream);
2432                 pVcardStream = null;
2433                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.: capacity(%d), size(%d)", GetErrorMessage(E_SYSTEM));
2434         }
2435
2436         return pByteBuffer.release();
2437 }
2438
2439 ByteBuffer*
2440 _AddressbookManagerImpl::ExportPersonToVcardStreamN(const Person& person)
2441 {
2442         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2443
2444         ClearLastResult();
2445
2446         unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, person.GetId()));
2447         SysTryReturn(NID_SCL, GetLastResult() != E_OBJ_NOT_FOUND,  null, E_INVALID_ARG, "[%s] Person does not exist.", GetErrorMessage(E_INVALID_ARG));
2448         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS,  null, E_INVALID_ARG, "[%s] Person does not exist.", GetErrorMessage(E_INVALID_ARG));
2449
2450         char* pVcardStream = null;
2451         int ret = contacts_vcard_make_from_person(pPersonRecord.get(), &pVcardStream);
2452         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2453         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2454         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2455
2456         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2457         if (pByteBuffer == null)
2458         {
2459                 free(pVcardStream);
2460                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2461
2462                 return null;
2463         }
2464
2465         result r = pByteBuffer->Construct(strlen(pVcardStream));
2466         if (IsFailed(r))
2467         {
2468                 free(pVcardStream);
2469                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2470
2471                 return null;
2472         }
2473
2474         r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2475         free(pVcardStream);
2476         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2477
2478         return pByteBuffer.release();
2479 }
2480
2481 ByteBuffer*
2482 _AddressbookManagerImpl::ExportPersonsToVcardStreamN(const Tizen::Base::Collection::IList& personList)
2483 {
2484         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2485
2486         ClearLastResult();
2487
2488         int ret = CONTACTS_ERROR_NONE;
2489         Person* pPerson = null;
2490         char* pVcardStream = null;
2491
2492         int capacity = 0;
2493
2494         unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(null);
2495
2496         unique_ptr<IEnumerator> pEnum(personList.GetEnumeratorN());
2497         SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2498
2499         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2500         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2501
2502         result r = pByteBuffer->Construct(capacity);
2503         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2504
2505         if (personList.GetCount() == 0)
2506         {
2507                 return pByteBuffer.release();
2508         }
2509
2510         while (pEnum->MoveNext() == E_SUCCESS)
2511         {
2512                 pPerson = static_cast<Person*>(pEnum->GetCurrent());
2513
2514                 pPersonRecord.reset(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, pPerson->GetId()));
2515                 SysTryReturn(NID_SCL, GetLastResult() != E_OBJ_NOT_FOUND,  null, E_INVALID_ARG, "[%s] Person does not exist.", GetErrorMessage(E_INVALID_ARG));
2516                 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS,  null, E_INVALID_ARG, "[%s] Person does not exist.", GetErrorMessage(E_INVALID_ARG));
2517
2518                 ret = contacts_vcard_make_from_person(pPersonRecord.get(), &pVcardStream);
2519                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2520                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2521                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2522
2523                 capacity += strlen(pVcardStream);
2524                 r = pByteBuffer->ExpandCapacity(capacity);
2525                 if (IsFailed(r))
2526                 {
2527                         free(pVcardStream);
2528                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2529
2530                         return null;
2531                 }
2532
2533                 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2534                 free(pVcardStream);
2535                 pVcardStream = null;
2536                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2537         }
2538
2539         return pByteBuffer.release();
2540 }
2541
2542 IList*
2543 _AddressbookManagerImpl::ParseVcardStreamN(const Tizen::Base::ByteBuffer& vcardStream)
2544 {
2545         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2546
2547         ClearLastResult();
2548
2549         contacts_list_h listHandle = null;
2550         result r = E_SUCCESS;
2551
2552         int ret = contacts_vcard_parse_to_contacts(reinterpret_cast<char*>(const_cast<byte*>(vcardStream.GetPointer())), &listHandle);
2553         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2554         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2555
2556         unsigned int count = 0;
2557         contacts_record_h recordHandle = null;
2558         contacts_list_get_count(listHandle, &count);
2559
2560         unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
2561         if (pList == null)
2562         {
2563                 contacts_list_destroy(listHandle, true);
2564                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2565
2566                 return null;
2567         }
2568
2569         r = pList->Construct(count);
2570         if (IsFailed(r))
2571         {
2572                 contacts_list_destroy(listHandle, true);
2573                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2574
2575                 return null;
2576         }
2577
2578         for (unsigned int i = 0; i < count; i++)
2579         {
2580                 unique_ptr<Contact> pContact(new (std::nothrow) Contact());
2581                 if (pContact == null)
2582                 {
2583                         contacts_list_destroy(listHandle, true);
2584                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2585
2586                         return null;
2587                 }
2588
2589                 r = pList->Add(pContact.get());
2590                 if (IsFailed(r))
2591                 {
2592                         contacts_list_destroy(listHandle, true);
2593                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2594
2595                         return null;
2596                 }
2597
2598                 pContact.release();
2599         }
2600
2601         unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
2602
2603         while (pEnum->MoveNext() == E_SUCCESS)
2604         {
2605                 Contact* pContact = static_cast <Contact*> (pEnum->GetCurrent());
2606
2607                 contacts_list_get_current_record_p(listHandle, &recordHandle);
2608                 _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(recordHandle);
2609
2610                 ret = contacts_list_next(listHandle);
2611         }
2612
2613         contacts_list_destroy(listHandle, false);
2614
2615         return pList.release();
2616 }
2617
2618 ByteBuffer*
2619 _AddressbookManagerImpl::ExportUserProfileToVcardStreamN(const UserProfile& userProfile)
2620 {
2621         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2622
2623         ClearLastResult();
2624
2625         char* pVcardStream = null;
2626         int ret = CONTACTS_ERROR_NONE;
2627         contacts_record_h recordHandle = null;
2628
2629         recordHandle = _UserProfileImpl::GetInstance(userProfile)->GetUserProfileHandle();
2630
2631         ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2632         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2633         SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2634         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2635
2636         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2637         if (pByteBuffer == null)
2638         {
2639                 free(pVcardStream);
2640                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2641
2642                 return null;
2643         }
2644         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2645
2646         result r = pByteBuffer->Construct(strlen(pVcardStream));
2647         if (IsFailed(r))
2648         {
2649                 free(pVcardStream);
2650                 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2651
2652                 return null;
2653         }
2654
2655         r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2656         free(pVcardStream);
2657         SysTryReturn(NID_SCL, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2658         SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2659
2660         return pByteBuffer.release();
2661 }
2662
2663 ByteBuffer*
2664 _AddressbookManagerImpl::ExportUserProfilesToVcardStreamN(const Tizen::Base::Collection::IList& userProfileList)
2665 {
2666         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2667
2668         ClearLastResult();
2669
2670         char* pVcardStream = null;
2671         int ret = CONTACTS_ERROR_NONE;
2672         UserProfile* pProfile = null;
2673         result r = E_SUCCESS;
2674         int capacity = 0;
2675
2676         unique_ptr<IEnumerator> pEnum(userProfileList.GetEnumeratorN());
2677         SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2678
2679         unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2680         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2681
2682         r = pByteBuffer->Construct(capacity);
2683         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2684
2685         if (userProfileList.GetCount() == 0)
2686         {
2687                 return pByteBuffer.release();
2688         }
2689
2690         while (pEnum->MoveNext() == E_SUCCESS)
2691         {
2692                 contacts_record_h recordHandle = null;
2693
2694                 pProfile = static_cast<UserProfile*>(pEnum->GetCurrent());
2695
2696                 recordHandle = _UserProfileImpl::GetInstance(*pProfile)->GetUserProfileHandle();
2697
2698                 ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2699                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2700                 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2701                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2702
2703                 capacity += strlen(pVcardStream);
2704                 r = pByteBuffer->ExpandCapacity(capacity);
2705                 if (IsFailed(r))
2706                 {
2707                         free(pVcardStream);
2708                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2709
2710                         return null;
2711                 }
2712
2713                 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2714                 free(pVcardStream);
2715                 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.: capacity(%d), size(%d)", GetErrorMessage(E_SYSTEM));
2716         }
2717
2718         return pByteBuffer.release();
2719 }
2720
2721 result
2722 _AddressbookManagerImpl::ExportUserProfileToVcard(const UserProfile& userProfile, const Tizen::Base::String& vcardPath)
2723 {
2724         bool exist = File::IsFileExist(vcardPath);
2725         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2726         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2727         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2728
2729         File file;
2730         result r = file.Construct(vcardPath, "w");
2731         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));
2732         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2733         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2734
2735         char* pVcardStream = null;
2736         contacts_record_h recordHandle = null;
2737
2738         recordHandle = _UserProfileImpl::GetInstance(userProfile)->GetUserProfileHandle();
2739
2740         int ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2741         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));
2742         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2743
2744         r = file.Write(pVcardStream, strlen(pVcardStream));
2745         free(pVcardStream);
2746         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2747         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2748
2749         return E_SUCCESS;
2750 }
2751
2752 result
2753 _AddressbookManagerImpl::ExportUserProfilesToVcard(const Tizen::Base::Collection::IList& userProfileList, const Tizen::Base::String& vcardPath)
2754 {
2755         bool exist = File::IsFileExist(vcardPath);
2756         SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2757         SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2758         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2759
2760         int ret = CONTACTS_ERROR_NONE;
2761         char* pVcardStream = null;
2762         File file;
2763         UserProfile* pProfile = null;
2764
2765         result r = file.Construct(vcardPath, "w");
2766         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));
2767         SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2768         SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2769
2770         unique_ptr<IEnumerator> pEnum(userProfileList.GetEnumeratorN());
2771         SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2772
2773         while (pEnum->MoveNext() == E_SUCCESS)
2774         {
2775                 contacts_record_h recordHandle = null;
2776
2777                 pProfile = static_cast<UserProfile*>(pEnum->GetCurrent());
2778
2779                 recordHandle = _UserProfileImpl::GetInstance(*pProfile)->GetUserProfileHandle();
2780
2781                 ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2782                 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));
2783                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2784
2785                 r = file.Write(pVcardStream, strlen(pVcardStream));
2786                 free(pVcardStream);
2787                 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2788                 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2789         }
2790
2791         return E_SUCCESS;
2792 }
2793
2794 IList*
2795 _AddressbookManagerImpl::GetAllUserProfilesN(void) const
2796 {
2797         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2798
2799         ClearLastResult();
2800
2801         unique_ptr< __Filter<__ContactsUserProfile> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsUserProfile>());
2802         SysTryReturn(NID_SCL, pRwAbFilter != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2803
2804         __Query<__ContactsUserProfile> query;
2805         query.Construct();
2806         query.SetSort(_contacts_my_profile.display_name, true);
2807
2808         if (pRwAbFilter->Get() != null)
2809         {
2810                 query.SetFilter(*pRwAbFilter);
2811         }
2812
2813         IList* pUserProfilesList = _AddressbookUtil::SearchWithQueryN<__ContactsUserProfile, UserProfile>(query);
2814         SysTryReturn(NID_SCL, pUserProfilesList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2815
2816         return pUserProfilesList;
2817 }
2818
2819 UserProfile*
2820 _AddressbookManagerImpl::GetUserProfileN(AddressbookId addressbookId) const
2821 {
2822         SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2823         SysTryReturn(NID_SCL, addressbookId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. Addressbook Id(%d).", GetErrorMessage(E_INVALID_ARG), addressbookId);
2824
2825         ClearLastResult();
2826
2827         int mode = 0;
2828         unique_ptr<ContactRecord, ContactRecordDeleter> pAbRecord(_AddressbookUtil::GetContactRecordN(_contacts_address_book._uri, addressbookId));
2829         SysTryReturn(NID_SCL, pAbRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2830
2831         contacts_record_get_int(pAbRecord.get(), _contacts_address_book.mode, &mode);
2832         SysTryReturn(NID_SCL, mode == 0, null, E_OBJ_NOT_FOUND, "[%s] The addressbook does not exist.", GetErrorMessage(E_OBJ_NOT_FOUND));
2833
2834         __Filter<__ContactsUserProfile> filter;
2835         filter.Construct();
2836         filter.AddInt(_contacts_my_profile.address_book_id, CONTACTS_MATCH_EQUAL, addressbookId);
2837
2838         __Query<__ContactsUserProfile> query;
2839         query.Construct();
2840         query.SetFilter(filter);
2841
2842         unique_ptr<IList, AllElementsDeleter> pUserProfilesList(_AddressbookUtil::SearchWithQueryN<__ContactsUserProfile, UserProfile>(query));
2843         SysTryReturn(NID_SCL, pUserProfilesList.get() != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2844         SysTryReturn(NID_SCL, pUserProfilesList->GetCount() != 0, null, E_SUCCESS, "No UserProfile Set for this Addressbook.");
2845         SysTryReturn(NID_SCL, pUserProfilesList->GetCount() == 1, null, E_SYSTEM, "[%s] Propagating. More than one UserProfile not allowed.", GetErrorMessage(E_SYSTEM));
2846
2847         UserProfile* pProfile = new (std::nothrow) UserProfile(*(static_cast<UserProfile*>(pUserProfilesList->GetAt(0))));
2848         SysTryReturn(NID_SCL, pProfile != null, null, E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(E_OUT_OF_MEMORY));
2849
2850         return pProfile;
2851 }
2852
2853 contacts_record_h
2854 _AddressbookManagerImpl::CopyContactRecordHandle(contacts_record_h srcHandle)
2855 {
2856         ClearLastResult();
2857
2858         int ret = CONTACTS_ERROR_NONE;
2859         unsigned int i = 0;
2860         unsigned int count = 0;
2861         char* pCharValue = null;
2862         int intValue = 0;
2863         bool boolValue = false;
2864
2865         contacts_record_h contactHandle = null;
2866         contacts_record_h sourceRecordHandle = null;
2867         contacts_record_h destRecordHandle = null;
2868
2869         ret = contacts_record_create(_contacts_contact._uri, &contactHandle);
2870         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2871
2872         // favorite
2873         contacts_record_get_bool(sourceRecordHandle, _contacts_contact.is_favorite, &boolValue);
2874         contacts_record_set_bool(destRecordHandle, _contacts_contact.is_favorite, boolValue);
2875
2876         // uid
2877         contacts_record_get_str_p(sourceRecordHandle, _contacts_contact.uid, &pCharValue);
2878         contacts_record_set_str(destRecordHandle, _contacts_contact.uid, pCharValue);
2879
2880         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2881         // name
2882         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2883         contacts_record_get_child_record_count(srcHandle, _contacts_contact.name, &count);
2884         if (count > 0)
2885         {
2886                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.name, 0, &sourceRecordHandle);
2887
2888                 ret = contacts_record_create(_contacts_name._uri, &destRecordHandle);
2889                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2890
2891                 __ContactsRecordHandle nameHandle(destRecordHandle);
2892
2893                 // 1. first
2894                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.first, &pCharValue);
2895                 contacts_record_set_str(destRecordHandle, _contacts_name.first, pCharValue);
2896
2897                 // 2. last
2898                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.last, &pCharValue);
2899                 contacts_record_set_str(destRecordHandle, _contacts_name.last, pCharValue);
2900
2901                 // 3. addition
2902                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.addition, &pCharValue);
2903                 contacts_record_set_str(destRecordHandle, _contacts_name.addition, pCharValue);
2904
2905                 // 4. suffix
2906                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.suffix, &pCharValue);
2907                 contacts_record_set_str(destRecordHandle, _contacts_name.suffix, pCharValue);
2908
2909                 // 5. prefix
2910                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.prefix, &pCharValue);
2911                 contacts_record_set_str(destRecordHandle, _contacts_name.prefix, pCharValue);
2912
2913                 // 6. phonetic_first
2914                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.phonetic_first, &pCharValue);
2915                 contacts_record_set_str(destRecordHandle, _contacts_name.phonetic_first, pCharValue);
2916
2917                 // 7. phonetic_last
2918                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.phonetic_last, &pCharValue);
2919                 contacts_record_set_str(destRecordHandle, _contacts_name.phonetic_last, pCharValue);
2920
2921                 // 8. phonetic_middle
2922                 contacts_record_get_str_p(sourceRecordHandle, _contacts_name.phonetic_middle, &pCharValue);
2923                 contacts_record_set_str(destRecordHandle, _contacts_name.phonetic_middle, pCharValue);
2924
2925                 contacts_record_add_child_record(contactHandle, _contacts_contact.name, destRecordHandle);
2926
2927                 nameHandle.Release();
2928         }
2929
2930         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2931         // image
2932         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2933         contacts_record_get_child_record_count(srcHandle, _contacts_contact.image, &count);
2934         for (i = 0; i < count; i++)
2935         {
2936                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.image, i, &sourceRecordHandle);
2937
2938                 ret = contacts_record_create(_contacts_image._uri, &destRecordHandle);
2939                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2940
2941                 __ContactsRecordHandle imageHandle(destRecordHandle);
2942
2943                 contacts_record_get_int(sourceRecordHandle, _contacts_image.type, &intValue);
2944                 contacts_record_set_int(destRecordHandle, _contacts_image.type, intValue);
2945
2946                 contacts_record_get_str_p(sourceRecordHandle, _contacts_image.label, &pCharValue);
2947                 contacts_record_set_str(destRecordHandle, _contacts_image.label, pCharValue);
2948
2949                 contacts_record_get_str_p(sourceRecordHandle, _contacts_image.path, &pCharValue);
2950                 contacts_record_set_str(destRecordHandle, _contacts_image.path, pCharValue);
2951
2952                 contacts_record_add_child_record(contactHandle, _contacts_contact.image, destRecordHandle);
2953
2954                 imageHandle.Release();
2955         }
2956
2957         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2958         // company
2959         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
2960         contacts_record_get_child_record_count(srcHandle, _contacts_contact.company, &count);
2961         for (i = 0; i < count; i++)
2962         {
2963                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.company, i, &sourceRecordHandle);
2964
2965                 ret = contacts_record_create(_contacts_company._uri, &destRecordHandle);
2966                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2967
2968                 __ContactsRecordHandle companyHandle(destRecordHandle);
2969
2970                 contacts_record_get_int(sourceRecordHandle, _contacts_company.type, &intValue);
2971                 contacts_record_set_int(destRecordHandle, _contacts_company.type, intValue);
2972
2973                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.name, &pCharValue);
2974                 contacts_record_set_str(destRecordHandle, _contacts_company.name, pCharValue);
2975
2976                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.department, &pCharValue);
2977                 contacts_record_set_str(destRecordHandle, _contacts_company.department, pCharValue);
2978
2979                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.job_title, &pCharValue);
2980                 contacts_record_set_str(destRecordHandle, _contacts_company.job_title, pCharValue);
2981
2982                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.assistant_name, &pCharValue);
2983                 contacts_record_set_str(destRecordHandle, _contacts_company.assistant_name, pCharValue);
2984
2985                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.role, &pCharValue);
2986                 contacts_record_set_str(destRecordHandle, _contacts_company.role, pCharValue);
2987
2988                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.logo, &pCharValue);
2989                 contacts_record_set_str(destRecordHandle, _contacts_company.logo, pCharValue);
2990
2991                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.location, &pCharValue);
2992                 contacts_record_set_str(destRecordHandle, _contacts_company.location, pCharValue);
2993
2994                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.description, &pCharValue);
2995                 contacts_record_set_str(destRecordHandle, _contacts_company.description, pCharValue);
2996
2997                 contacts_record_get_str_p(sourceRecordHandle, _contacts_company.phonetic_name, &pCharValue);
2998                 contacts_record_set_str(destRecordHandle, _contacts_company.phonetic_name, pCharValue);
2999
3000                 contacts_record_add_child_record(contactHandle, _contacts_contact.company, destRecordHandle);
3001
3002                 companyHandle.Release();
3003         }
3004
3005         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3006         // note
3007         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3008         contacts_record_get_child_record_count(srcHandle, _contacts_contact.note, &count);
3009         for (i = 0; i < count; i++)
3010         {
3011                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.note, i, &sourceRecordHandle);
3012
3013                 ret = contacts_record_create(_contacts_note._uri, &destRecordHandle);
3014                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3015
3016                 __ContactsRecordHandle noteHandle(destRecordHandle);
3017
3018                 contacts_record_get_str_p(sourceRecordHandle, _contacts_note.note, &pCharValue);
3019                 contacts_record_set_str(destRecordHandle, _contacts_note.note, pCharValue);
3020
3021                 contacts_record_add_child_record(contactHandle, _contacts_contact.note, destRecordHandle);
3022
3023                 noteHandle.Release();
3024         }
3025
3026         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3027         // phone number
3028         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3029         contacts_record_get_child_record_count(srcHandle, _contacts_contact.number, &count);
3030         for (i = 0; i < count; i++)
3031         {
3032                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.number, i, &sourceRecordHandle);
3033
3034                 ret = contacts_record_create(_contacts_number._uri, &destRecordHandle);
3035                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3036
3037                 __ContactsRecordHandle numberHandle(destRecordHandle);
3038
3039                 contacts_record_get_int(sourceRecordHandle, _contacts_number.type, &intValue);
3040                 contacts_record_set_int(destRecordHandle, _contacts_number.type, intValue);
3041
3042                 contacts_record_get_str_p(sourceRecordHandle, _contacts_number.label, &pCharValue);
3043                 contacts_record_set_str(destRecordHandle, _contacts_number.label, pCharValue);
3044
3045                 contacts_record_get_str_p(sourceRecordHandle, _contacts_number.number, &pCharValue);
3046                 contacts_record_set_str(destRecordHandle, _contacts_number.number, pCharValue);
3047
3048                 contacts_record_add_child_record(contactHandle, _contacts_contact.number, destRecordHandle);
3049
3050                 numberHandle.Release();
3051         }
3052
3053         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3054         // email
3055         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3056         contacts_record_get_child_record_count(srcHandle, _contacts_contact.email, &count);
3057         for (i = 0; i < count; i++)
3058         {
3059                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.email, i, &sourceRecordHandle);
3060
3061                 ret = contacts_record_create(_contacts_email._uri, &destRecordHandle);
3062                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3063
3064                 __ContactsRecordHandle emailHandle(destRecordHandle);
3065
3066                 contacts_record_get_int(sourceRecordHandle, _contacts_email.type, &intValue);
3067                 contacts_record_set_int(destRecordHandle, _contacts_email.type, intValue);
3068
3069                 contacts_record_get_str_p(sourceRecordHandle, _contacts_email.label, &pCharValue);
3070                 contacts_record_set_str(destRecordHandle, _contacts_email.label, pCharValue);
3071
3072                 contacts_record_get_str_p(sourceRecordHandle, _contacts_email.email, &pCharValue);
3073                 contacts_record_set_str(destRecordHandle, _contacts_email.email, pCharValue);
3074
3075                 contacts_record_add_child_record(contactHandle, _contacts_contact.email, destRecordHandle);
3076
3077                 emailHandle.Release();
3078
3079         }
3080
3081         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3082         // event
3083         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3084         contacts_record_get_child_record_count(srcHandle, _contacts_contact.event, &count);
3085         for (i = 0; i < count; i++)
3086         {
3087                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.event, i, &sourceRecordHandle);
3088
3089                 ret = contacts_record_create(_contacts_event._uri, &destRecordHandle);
3090                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3091
3092                 __ContactsRecordHandle eventHandle(destRecordHandle);
3093
3094                 contacts_record_get_int(sourceRecordHandle, _contacts_event.type, &intValue);
3095                 contacts_record_set_int(destRecordHandle, _contacts_event.type, intValue);
3096
3097                 contacts_record_get_str_p(sourceRecordHandle, _contacts_event.label, &pCharValue);
3098                 contacts_record_set_str(destRecordHandle, _contacts_event.label, pCharValue);
3099
3100                 contacts_record_get_int(sourceRecordHandle, _contacts_event.date, &intValue);
3101                 contacts_record_set_int(destRecordHandle, _contacts_event.date, intValue);
3102
3103                 contacts_record_add_child_record(contactHandle, _contacts_contact.event, destRecordHandle);
3104
3105                 eventHandle.Release();
3106         }
3107
3108         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3109         // im address
3110         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3111         contacts_record_get_child_record_count(srcHandle, _contacts_contact.messenger, &count);
3112         for (i = 0; i < count; i++)
3113         {
3114                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.messenger, i, &sourceRecordHandle);
3115
3116                 ret = contacts_record_create(_contacts_messenger._uri, &destRecordHandle);
3117                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3118
3119                 __ContactsRecordHandle imAddressHandle(destRecordHandle);
3120
3121                 contacts_record_get_int(sourceRecordHandle, _contacts_messenger.type, &intValue);
3122                 contacts_record_set_int(destRecordHandle, _contacts_messenger.type, intValue);
3123
3124                 contacts_record_get_str_p(sourceRecordHandle, _contacts_messenger.label, &pCharValue);
3125                 contacts_record_set_str(destRecordHandle, _contacts_messenger.label, pCharValue);
3126
3127                 contacts_record_get_str_p(sourceRecordHandle, _contacts_messenger.im_id, &pCharValue);
3128                 contacts_record_set_str(destRecordHandle, _contacts_messenger.im_id, pCharValue);
3129
3130                 contacts_record_add_child_record(contactHandle, _contacts_contact.messenger, destRecordHandle);
3131
3132                 imAddressHandle.Release();
3133         }
3134
3135         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3136         // address
3137         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3138         contacts_record_get_child_record_count(srcHandle, _contacts_contact.address, &count);
3139         for (i = 0; i < count; i++)
3140         {
3141                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.address, i, &sourceRecordHandle);
3142
3143                 ret = contacts_record_create(_contacts_address._uri, &destRecordHandle);
3144                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3145
3146                 __ContactsRecordHandle addressHandle(destRecordHandle);
3147
3148                 contacts_record_get_int(sourceRecordHandle, _contacts_address.type, &intValue);
3149                 contacts_record_set_int(destRecordHandle, _contacts_address.type, intValue);
3150
3151                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.label, &pCharValue);
3152                 contacts_record_set_str(destRecordHandle, _contacts_address.label, pCharValue);
3153
3154                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.postbox, &pCharValue);
3155                 contacts_record_set_str(destRecordHandle, _contacts_address.postbox, pCharValue);
3156
3157                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.extended, &pCharValue);
3158                 contacts_record_set_str(destRecordHandle, _contacts_address.extended, pCharValue);
3159
3160                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.street, &pCharValue);
3161                 contacts_record_set_str(destRecordHandle, _contacts_address.street, pCharValue);
3162
3163                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.locality, &pCharValue);
3164                 contacts_record_set_str(destRecordHandle, _contacts_address.locality, pCharValue);
3165
3166                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.region, &pCharValue);
3167                 contacts_record_set_str(destRecordHandle, _contacts_address.region, pCharValue);
3168
3169                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.postal_code, &pCharValue);
3170                 contacts_record_set_str(destRecordHandle, _contacts_address.postal_code, pCharValue);
3171
3172                 contacts_record_get_str_p(sourceRecordHandle, _contacts_address.country, &pCharValue);
3173                 contacts_record_set_str(destRecordHandle, _contacts_address.country, pCharValue);
3174
3175                 contacts_record_add_child_record(contactHandle, _contacts_contact.address, destRecordHandle);
3176
3177                 addressHandle.Release();
3178         }
3179
3180         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3181         // url
3182         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3183         contacts_record_get_child_record_count(srcHandle, _contacts_contact.url, &count);
3184         for (i = 0; i < count; i++)
3185         {
3186                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.url, i, &sourceRecordHandle);
3187
3188                 ret = contacts_record_create(_contacts_url._uri, &destRecordHandle);
3189                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3190
3191                 __ContactsRecordHandle urlHandle(destRecordHandle);
3192
3193                 contacts_record_get_int(sourceRecordHandle, _contacts_url.type, &intValue);
3194                 contacts_record_set_int(destRecordHandle, _contacts_url.type, intValue);
3195
3196                 contacts_record_get_str_p(sourceRecordHandle, _contacts_url.label, &pCharValue);
3197                 contacts_record_set_str(destRecordHandle, _contacts_url.label, pCharValue);
3198
3199                 contacts_record_get_str_p(sourceRecordHandle, _contacts_url.url, &pCharValue);
3200                 contacts_record_set_str(destRecordHandle, _contacts_url.url, pCharValue);
3201
3202                 contacts_record_add_child_record(contactHandle, _contacts_contact.url, destRecordHandle);
3203
3204                 urlHandle.Release();
3205         }
3206
3207         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3208         // nickname
3209         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3210         contacts_record_get_child_record_count(srcHandle, _contacts_contact.nickname, &count);
3211         for (i = 0; i < count; i++)
3212         {
3213                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.nickname, i, &sourceRecordHandle);
3214
3215                 ret = contacts_record_create(_contacts_nickname._uri, &destRecordHandle);
3216                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3217
3218                 __ContactsRecordHandle nicknameHandle(destRecordHandle);
3219
3220                 contacts_record_get_str_p(sourceRecordHandle, _contacts_nickname.name, &pCharValue);
3221                 contacts_record_set_str(destRecordHandle, _contacts_nickname.name, pCharValue);
3222
3223                 contacts_record_add_child_record(contactHandle, _contacts_contact.nickname, destRecordHandle);
3224
3225                 nicknameHandle.Release();
3226         }
3227
3228         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3229         // relationship
3230         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3231         contacts_record_get_child_record_count(srcHandle, _contacts_contact.relationship, &count);
3232         for (i = 0; i < count; i++)
3233         {
3234                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.relationship, i, &sourceRecordHandle);
3235
3236                 ret = contacts_record_create(_contacts_relationship._uri, &destRecordHandle);
3237                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3238
3239                 __ContactsRecordHandle relationshipHandle(destRecordHandle);
3240
3241                 contacts_record_get_str_p(sourceRecordHandle, _contacts_relationship.name, &pCharValue);
3242                 contacts_record_set_str(destRecordHandle, _contacts_relationship.name, pCharValue);
3243
3244                 contacts_record_add_child_record(contactHandle, _contacts_contact.relationship, destRecordHandle);
3245
3246                 relationshipHandle.Release();
3247         }
3248
3249         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3250         // app launch data
3251         /////////////////////////////////////////////////////////////////////////////////////////////////////////////
3252         contacts_record_get_child_record_count(srcHandle, _contacts_contact.profile, &count);
3253         for (i = 0; i < count; i++)
3254         {
3255                 contacts_record_get_child_record_at_p(srcHandle, _contacts_contact.profile, i, &sourceRecordHandle);
3256
3257                 ret = contacts_record_create(_contacts_profile._uri, &destRecordHandle);
3258                 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
3259
3260                 __ContactsRecordHandle profileHandle(destRecordHandle);
3261
3262                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.text, &pCharValue);
3263                 contacts_record_set_str(destRecordHandle, _contacts_profile.text, pCharValue);
3264
3265                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.uid, &pCharValue);
3266                 contacts_record_set_str(destRecordHandle, _contacts_profile.uid, pCharValue);
3267
3268                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.app_id, &pCharValue);
3269                 contacts_record_set_str(destRecordHandle, _contacts_profile.app_id, pCharValue);
3270
3271                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.service_operation, &pCharValue);
3272                 contacts_record_set_str(destRecordHandle, _contacts_profile.service_operation, pCharValue);
3273
3274                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.uri, &pCharValue);
3275                 contacts_record_set_str(destRecordHandle, _contacts_profile.uri, pCharValue);
3276
3277                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.category, &pCharValue);
3278                 contacts_record_set_str(destRecordHandle, _contacts_profile.category, pCharValue);
3279
3280                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.mime, &pCharValue);
3281                 contacts_record_set_str(destRecordHandle, _contacts_profile.mime, pCharValue);
3282
3283                 contacts_record_get_str_p(sourceRecordHandle, _contacts_profile.extra_data, &pCharValue);
3284                 contacts_record_set_str(destRecordHandle, _contacts_profile.extra_data, pCharValue);
3285
3286                 contacts_record_add_child_record(contactHandle, _contacts_contact.profile, destRecordHandle);
3287
3288                 profileHandle.Release();
3289         }
3290
3291         return contactHandle;
3292 }
3293
3294 _AddressbookManagerImpl*
3295 _AddressbookManagerImpl::GetInstance(AddressbookManager& addressbookManager)
3296 {
3297         return addressbookManager.__pAddressbookManagerImpl;
3298 }
3299
3300 const _AddressbookManagerImpl*
3301 _AddressbookManagerImpl::GetInstance(const AddressbookManager& addressbookManager)
3302 {
3303         return addressbookManager.__pAddressbookManagerImpl;
3304 }
3305
3306 }}  // Tizen::Social