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