2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file FScl_AddressbookManagerImpl.cpp
18 * @brief This is the implementation for _AddressbookManagerImpl class.
20 * This file contains definitions of @e _AddressbookManagerImpl class.
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>
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"
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;
72 namespace Tizen { namespace Social
75 _AddressbookManagerImpl::_AddressbookManagerImpl(void)
76 : __pIAddressbookEventListener(null)
77 , __pIAddressbookChangeEventListener(null)
78 , __dbVersionForContact(0)
79 , __dbVersionForGroup(0)
80 , __dbVersionForRelation(0)
85 _AddressbookManagerImpl::~_AddressbookManagerImpl(void)
87 if (__pIAddressbookEventListener != null || __pIAddressbookChangeEventListener != null)
89 _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
90 if (pContactDbMonitor != null)
92 pContactDbMonitor->RemoveListener(*this);
98 _AddressbookManagerImpl::Construct(void)
100 result r = _ContactDbConnector::EnsureDbConnection();
101 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
107 _AddressbookManagerImpl::SetEventListener(IAddressbookEventListener* pListener)
109 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
111 result r = E_SUCCESS;
113 if (pListener != null)
115 if (__pIAddressbookEventListener == null && __pIAddressbookChangeEventListener == null)
117 _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
118 SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
120 r = pContactDbMonitor->AddListener(*this);
121 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
124 __dbVersionForContact = GetLatestVersion();
125 SysTryReturn(NID_SCL, __dbVersionForContact != -1, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
127 __dbVersionForGroup = __dbVersionForContact;
128 __dbVersionForRelation = __dbVersionForContact;
130 __pIAddressbookChangeEventListener = null;
131 __pIAddressbookEventListener = pListener;
135 if (__pIAddressbookEventListener != null || __pIAddressbookChangeEventListener != null)
138 _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
139 SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
141 pContactDbMonitor->RemoveListener(*this);
144 __pIAddressbookEventListener = null;
145 __pIAddressbookChangeEventListener = null;
152 _AddressbookManagerImpl::SetAddressbookChangeEventListener(IAddressbookChangeEventListener* pListener)
154 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
156 result r = E_SUCCESS;
158 if (pListener != null)
160 if (__pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
162 _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
163 SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
165 r = pContactDbMonitor->AddListener(*this);
166 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
169 __dbVersionForContact = GetLatestVersion();
170 SysTryReturn(NID_SCL, __dbVersionForContact != -1, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
172 __dbVersionForGroup = __dbVersionForContact;
173 __dbVersionForRelation = __dbVersionForContact;
175 __pIAddressbookEventListener = null;
176 __pIAddressbookChangeEventListener = pListener;
180 if (__pIAddressbookChangeEventListener != null || __pIAddressbookEventListener != null)
183 _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
184 SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
186 pContactDbMonitor->RemoveListener(*this);
189 __pIAddressbookChangeEventListener = null;
190 __pIAddressbookEventListener = null;
197 _AddressbookManagerImpl::CreateAddressbookN(AccountId accountId, const String& name)
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));
205 result r = E_SUCCESS;
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));
211 __Filter<__ContactsAddressbook> filter;
213 filter.AddString(_contacts_address_book.name, CONTACTS_MATCH_EXACTLY, pNameString.get());
216 filter.AddOperator(CONTACTS_FILTER_OPERATOR_OR);
217 filter.AddInt(_contacts_address_book.account_id, CONTACTS_MATCH_EQUAL, accountId);
220 __Query<__ContactsAddressbook> query;
222 query.SetFilter(filter);
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));
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));
230 r = pAddressbook->Construct();
231 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
233 unique_ptr<ContactRecord, ContactRecordDeleter> pAbRecord(_AddressbookUtil::CreateContactRecordN(_contacts_address_book._uri));
234 SysTryReturn(NID_SCL, pAbRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
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);
239 r = _AddressbookUtil::InsertContactRecordN(pAbRecord.get(), recordId);
240 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
242 _AddressbookImpl::GetInstance(*pAddressbook)->SetAccountId(accountId);
243 _AddressbookImpl::GetInstance(*pAddressbook)->SetName(name);
244 _AddressbookImpl::GetInstance(*pAddressbook)->SetId(recordId);
246 return pAddressbook.release();
250 _AddressbookManagerImpl::DeleteAddressbook(AddressbookId addressbookId)
252 SysTryReturn(NID_SCL, addressbookId > 0, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is 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()));
255 int ret = contacts_db_delete_record(_contacts_address_book._uri, addressbookId);
256 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));
257 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The addressbook is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
258 // SysTryReturnResult(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OPERATION_FAILED, "Failed to delete an addressbook.(%d)", ret); // temp
264 _AddressbookManagerImpl::GetAddressbooksByAccountN(AccountId accountId) const
266 SysTryReturn(NID_SCL, accountId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. account id is invalid.", GetErrorMessage(E_INVALID_ARG));
267 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
271 __Filter<__ContactsAddressbook> accountFilter;
272 accountFilter.Construct();
273 accountFilter.AddInt(_contacts_address_book.account_id, CONTACTS_MATCH_EQUAL, accountId);
275 unique_ptr< __Filter<__ContactsAddressbook> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsAddressbook>());
277 __Filter<__ContactsAddressbook> mainFilter;
278 mainFilter.Construct();
280 mainFilter.AddFilter(*pRwAbFilter);
281 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
282 mainFilter.AddFilter(accountFilter);
284 __Query<__ContactsAddressbook> query;
286 query.SetFilter(mainFilter);
288 IList* pAddressbooks = _AddressbookUtil::SearchWithQueryN<__ContactsAddressbook, Addressbook>(query);
289 SysTryReturn(NID_SCL, pAddressbooks != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
291 return pAddressbooks;
295 _AddressbookManagerImpl::GetAllAddressbooksN(void) const
297 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
301 unique_ptr< __Filter<__ContactsAddressbook> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsAddressbook>());
303 __Query<__ContactsAddressbook> query;
305 query.SetFilter(*pRwAbFilter);
306 query.SetSort(_contacts_address_book.name, true);
308 IList* pAddressbooks = _AddressbookUtil::SearchWithQueryN<__ContactsAddressbook, Addressbook>(query);
309 SysTryReturn(NID_SCL, pAddressbooks != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
311 return pAddressbooks;
315 _AddressbookManagerImpl::GetAddressbookN(AddressbookId addressbookId) const
317 SysTryReturn(NID_SCL, addressbookId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. addressbookId.", GetErrorMessage(E_INVALID_ARG), addressbookId);
318 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
322 result r = E_SUCCESS;
324 char* pCharValue = null;
326 unique_ptr<ContactRecord, ContactRecordDeleter> pAbRecord(_AddressbookUtil::GetContactRecordN(_contacts_address_book._uri, addressbookId));
327 SysTryReturn(NID_SCL, pAbRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
329 contacts_record_get_int(pAbRecord.get(), _contacts_address_book.mode, &intValue);
330 SysTryReturn(NID_SCL, intValue == 0, null, E_OBJ_NOT_FOUND, "[%s] Addressbook does not exist.", GetErrorMessage(E_OBJ_NOT_FOUND));
332 unique_ptr<Addressbook> pAddressbook(new (std::nothrow) Addressbook());
333 SysTryReturn(NID_SCL, pAddressbook !=null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
335 r = pAddressbook->Construct();
336 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
338 _AddressbookImpl::GetInstance(*pAddressbook)->SetId(addressbookId);
340 contacts_record_get_int(pAbRecord.get(), _contacts_address_book.account_id, &intValue);
341 _AddressbookImpl::GetInstance(*pAddressbook)->SetAccountId(intValue);
343 contacts_record_get_str_p(pAbRecord.get(), _contacts_address_book.name, &pCharValue);
344 _AddressbookImpl::GetInstance(*pAddressbook)->SetName(pCharValue);
346 return pAddressbook.release();
350 _AddressbookManagerImpl::AddContact(Contact& contact, AddressbookId addressbookId)
352 if (_ContactImpl::GetInstance(contact)->IsRemoved())
354 result r = _ContactImpl::GetInstance(contact)->Invalidate();
355 SysTryReturn(NID_SCL, r == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
358 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));
359 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));
360 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
362 unique_ptr<ContactRecord, ContactRecordDeleter> pAbRecord(_AddressbookUtil::GetContactRecordN(_contacts_address_book._uri, addressbookId));
363 SysTryReturn(NID_SCL, pAbRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
366 contacts_record_get_int(pAbRecord.get(), _contacts_address_book.mode, &intValue);
367 SysTryReturn(NID_SCL, intValue == 0, null, E_OBJ_NOT_FOUND, "[%s] Addressbook does not exist.", GetErrorMessage(E_OBJ_NOT_FOUND));
369 contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
370 contacts_record_set_int(recordHandle, _contacts_contact.address_book_id, addressbookId);
373 result r = _AddressbookUtil::InsertContactRecordN(recordHandle, recordId);
374 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
376 unique_ptr<ContactRecord, ContactRecordDeleter> pContactRecord(_AddressbookUtil::GetContactRecordN(_contacts_contact._uri, recordId));
377 SysTryReturn(NID_SCL, pContactRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
379 _ContactImpl::GetInstance(contact)->SetContactRecordHandle(pContactRecord.release());
380 _RecordImpl::GetInstance(contact)->SetRecordId(recordId);
386 _AddressbookManagerImpl::AddCategory(Category& category, AddressbookId addressbookId)
388 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));
389 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
391 unique_ptr<ContactRecord, ContactRecordDeleter> pAbRecord(_AddressbookUtil::GetContactRecordN(_contacts_address_book._uri, addressbookId));
392 SysTryReturn(NID_SCL, pAbRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
395 contacts_record_get_int(pAbRecord.get(), _contacts_address_book.mode, &intValue);
396 SysTryReturn(NID_SCL, intValue == 0, null, E_OBJ_NOT_FOUND, "[%s] Addressbook does not exist.", GetErrorMessage(E_OBJ_NOT_FOUND));
398 contacts_record_h recordHandle = _CategoryImpl::GetInstance(category)->GetRecordHandle();
399 contacts_record_set_int(recordHandle, _contacts_group.address_book_id, addressbookId);
402 result r = _AddressbookUtil::InsertContactRecordN(recordHandle, recordId);
403 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
405 unique_ptr<ContactRecord, ContactRecordDeleter> pCategoryRecord(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, recordId));
406 SysTryReturn(NID_SCL, pCategoryRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
408 _CategoryImpl::GetInstance(category)->SetRecordHandle(pCategoryRecord.release());
409 _RecordImpl::GetInstance(category)->SetRecordId(recordId);
411 unique_ptr<IListT<int> > pList(_CategoryImpl::GetInstance(category)->GetAddedMembersN());
412 if (pList != null && pList->GetCount() > 0)
414 unique_ptr<IEnumeratorT<int> > pEnum(pList->GetEnumeratorN());
416 while (pEnum->MoveNext() == E_SUCCESS)
419 pEnum->GetCurrent(tableId);
421 AddMemberToCategory(category.GetRecordId(), tableId);
424 _CategoryImpl::GetInstance(category)->ClearAddedMemberList();
431 _AddressbookManagerImpl::RemoveContact(RecordId contactId)
433 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));
434 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
436 unique_ptr<ContactRecord, ContactRecordDeleter> pContactRecord(_AddressbookUtil::GetContactRecordN(_contacts_simple_contact._uri, contactId));
437 SysTryReturn(NID_SCL, pContactRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
440 contacts_record_get_int(pContactRecord.get(), _contacts_simple_contact.id, &intValue);
441 SysTryReturn(NID_SCL, intValue == contactId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
443 int ret = contacts_db_delete_record(_contacts_contact._uri, contactId);
444 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));
445 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
446 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
452 _AddressbookManagerImpl::RemoveCategory(RecordId categoryId)
454 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);
455 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
457 unique_ptr<ContactRecord, ContactRecordDeleter> pCategoryRecord(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, categoryId));
458 SysTryReturn(NID_SCL, pCategoryRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
461 contacts_record_get_int(pCategoryRecord.get(), _contacts_group.id, &intValue);
462 SysTryReturn(NID_SCL, intValue == categoryId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
464 bool isReadOnly = false;
465 contacts_record_get_bool(pCategoryRecord.get(), _contacts_group.is_read_only, &isReadOnly);
466 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));
468 int ret = contacts_db_delete_record(_contacts_group._uri, categoryId);
469 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));
470 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
471 // SysTryReturnResult(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OPERATION_FAILED, "Failed to delete a category.(%d)", ret); // temp
477 _AddressbookManagerImpl::UpdateContact(const Contact& contact)
479 RecordId contactId = contact.GetRecordId();
480 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));
481 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));
482 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
484 unique_ptr<ContactRecord, ContactRecordDeleter> pContactRecord(_AddressbookUtil::GetContactRecordN(_contacts_simple_contact._uri, contactId));
485 SysTryReturn(NID_SCL, pContactRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
488 contacts_record_get_int(pContactRecord.get(), _contacts_simple_contact.id, &intValue);
489 SysTryReturn(NID_SCL, intValue == contactId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
491 contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
493 int ret = contacts_db_update_record(recordHandle);
494 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));
495 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is insufficient.", GetErrorMessage(E_STORAGE_FULL));
496 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));
498 pContactRecord.reset(_AddressbookUtil::GetContactRecordN(_contacts_contact._uri, contact.GetRecordId()));
499 SysTryReturn(NID_SCL, pContactRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
501 _ContactImpl::GetInstance(*const_cast<Contact*>(&contact))->SetContactRecordHandle(pContactRecord.release());
507 _AddressbookManagerImpl::UpdateCategory(const Category& category)
509 RecordId categoryId = category.GetRecordId();
510 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));
511 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));
512 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
514 unique_ptr<ContactRecord, ContactRecordDeleter> pCategoryRecord(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, category.GetRecordId()));
515 SysTryReturn(NID_SCL, pCategoryRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
518 contacts_record_get_int(pCategoryRecord.get(), _contacts_group.id, &intValue);
519 SysTryReturn(NID_SCL, intValue == categoryId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
521 contacts_record_h recordHandle = _CategoryImpl::GetInstance(category)->GetRecordHandle();
523 int ret = contacts_db_update_record(recordHandle);
524 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));
525 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is insufficient.", GetErrorMessage(E_STORAGE_FULL));
526 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
528 pCategoryRecord.reset(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, category.GetRecordId()));
529 SysTryReturn(NID_SCL, pCategoryRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
531 _CategoryImpl::GetInstance(*const_cast<Category*>(&category))->SetRecordHandle(pCategoryRecord.release());
533 unique_ptr<IListT<int> > pList(_CategoryImpl::GetInstance(category)->GetAddedMembersN());
534 if (pList != null && pList->GetCount() > 0)
537 unique_ptr<IEnumeratorT<int> > pEnum(pList->GetEnumeratorN());
538 while (pEnum->MoveNext() == E_SUCCESS)
540 pEnum->GetCurrent(tableId);
542 AddMemberToCategory(category.GetRecordId(), tableId);
545 const_cast<_CategoryImpl*>(_CategoryImpl::GetInstance(category))->ClearAddedMemberList();
548 pList.reset(_CategoryImpl::GetInstance(category)->GetRemovedMembersN());
549 if (pList != null && pList->GetCount() > 0)
552 unique_ptr<IEnumeratorT<int> > pEnum(pList->GetEnumeratorN());
553 while (pEnum->MoveNext() == E_SUCCESS)
555 pEnum->GetCurrent(tableId);
557 RemoveMemberFromCategory(category.GetRecordId(), tableId);
560 const_cast<_CategoryImpl*>(_CategoryImpl::GetInstance(category))->ClearRemovedMemberList();
567 _AddressbookManagerImpl::AddMemberToCategory(RecordId categoryId, RecordId contactId)
569 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);
570 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);
571 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
573 int ret = contacts_group_add_contact(categoryId, contactId);
574 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
575 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));
576 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
582 _AddressbookManagerImpl::RemoveMemberFromCategory(RecordId categoryId, RecordId contactId)
584 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);
585 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);
586 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
588 int ret = contacts_group_remove_contact(categoryId, contactId);
589 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
590 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));
591 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
597 _AddressbookManagerImpl::GetAllCategoriesN(void) const
599 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
603 unique_ptr< __Filter<__ContactsGroup> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsGroup>());
605 __Query<__ContactsGroup> query;
607 query.SetFilter(*pRwAbFilter);
608 query.SetSort(_contacts_group.name, true);
610 IList* pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsGroup, Category>(query);
611 SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
617 _AddressbookManagerImpl::GetCategoriesByContactN(RecordId contactId) const
619 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
620 SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. The specified contact is invalid.", GetErrorMessage(E_INVALID_ARG));
624 unique_ptr< __Filter<__ContactsContactGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactGroupRel>());
626 __Filter<__ContactsContactGroupRel> relFilter;
627 relFilter.Construct();
628 relFilter.AddInt(_contacts_contact_grouprel.contact_id, CONTACTS_MATCH_EQUAL, contactId);
630 __Filter<__ContactsContactGroupRel> mainFilter;
631 mainFilter.Construct();
632 mainFilter.AddFilter(*pRwAbFilter);
633 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
634 mainFilter.AddFilter(relFilter);
636 __Query<__ContactsContactGroupRel> query;
638 query.SetFilter(mainFilter);
640 IList* pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Category>(query);
641 SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
647 _AddressbookManagerImpl::GetCategoriesByPersonN(PersonId personId) const
649 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
653 unique_ptr< __Filter<__ContactsContactGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactGroupRel>());
655 __Filter<__ContactsContactGroupRel> relFilter;
656 relFilter.Construct();
657 relFilter.AddInt(_contacts_contact_grouprel.person_id, CONTACTS_MATCH_EQUAL, personId);
658 relFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
659 relFilter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_GREATER_THAN, 0);
661 __Filter<__ContactsContactGroupRel> mainFilter;
662 mainFilter.Construct();
663 mainFilter.AddFilter(*pRwAbFilter);
664 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
665 mainFilter.AddFilter(relFilter);
667 unsigned int propertyIds[] =
669 _contacts_contact_grouprel.group_id,
672 __Query<__ContactsContactGroupRel> query;
674 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
675 query.SetFilter(mainFilter);
676 query.SetSort(_contacts_contact_grouprel.group_name, true);
677 query.SetDistinct(true);
679 IList* pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Category>(query);
680 SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
686 _AddressbookManagerImpl::GetAllContactsN(void) const
688 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
692 unique_ptr< __Filter<__ContactsContact> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContact>());
694 __Query<__ContactsContact> query;
696 query.SetFilter(*pRwAbFilter);
697 query.SetSort(_contacts_contact.display_name, true);
699 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
700 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
706 _AddressbookManagerImpl::GetContactsByCategoryN(RecordId categoryId) const
708 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));
709 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
713 unique_ptr< __Filter<__ContactsContactGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactGroupRel>());
715 __Filter<__ContactsContactGroupRel> relFilter;
716 relFilter.Construct();
718 if (categoryId != INVALID_RECORD_ID)
720 relFilter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_EQUAL, categoryId);
724 relFilter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_NONE, 0);
727 __Filter<__ContactsContactGroupRel> mainFilter;
728 mainFilter.Construct();
730 mainFilter.AddFilter(*pRwAbFilter);
731 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
732 mainFilter.AddFilter(relFilter);
734 __Query<__ContactsContactGroupRel> query;
736 query.SetFilter(mainFilter);
738 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Contact>(query);
739 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
745 _AddressbookManagerImpl::GetContactsByPersonN(PersonId personId) const
747 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
751 unique_ptr< __Filter<__ContactsContact> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContact>());
753 __Filter<__ContactsContact> contactFilter;
754 contactFilter.Construct();
755 contactFilter.AddInt(_contacts_contact.person_id, CONTACTS_MATCH_EQUAL, personId);
757 __Filter<__ContactsContact> mainFilter;
758 mainFilter.Construct();
760 mainFilter.AddFilter(*pRwAbFilter);
761 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
762 mainFilter.AddFilter(contactFilter);
764 __Query<__ContactsContact> query;
766 query.SetFilter(mainFilter);
767 query.SetSort(_contacts_contact.display_name, true);
769 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
770 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
776 _AddressbookManagerImpl::SearchContactsByEmailN(const String& email) const
778 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));
779 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
783 unique_ptr< __Filter<__ContactsContactEmail> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactEmail>());
785 unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(email));
786 SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
788 __Filter<__ContactsContactEmail> emailFilter;
789 emailFilter.Construct();
790 emailFilter.AddString(_contacts_contact_email.email, CONTACTS_MATCH_CONTAINS, pCharArray.get());
792 __Filter<__ContactsContactEmail> mainFilter;
793 mainFilter.Construct();
795 mainFilter.AddFilter(*pRwAbFilter);
796 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
797 mainFilter.AddFilter(emailFilter);
799 __Query<__ContactsContactEmail> query;
801 query.SetFilter(mainFilter);
802 query.SetSort(_contacts_contact_email.display_name, true);
804 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactEmail, Contact>(query);
805 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
811 _AddressbookManagerImpl::SearchContactsByNameN(const String& name) const
813 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
817 unique_ptr< __Filter<__ContactsContact> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContact>());
819 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));
821 unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(name));
822 SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
824 __Filter<__ContactsContact> nameFilter;
825 nameFilter.Construct();
826 nameFilter.AddString(_contacts_contact.display_name, CONTACTS_MATCH_CONTAINS, pCharArray.get());
828 __Filter<__ContactsContact> mainFilter;
829 mainFilter.Construct();
831 mainFilter.AddFilter(*pRwAbFilter);
832 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
833 mainFilter.AddFilter(nameFilter);
835 __Query<__ContactsContact> query;
837 query.SetFilter(mainFilter);
838 query.SetSort(_contacts_contact.display_name, true);
840 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
841 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
847 _AddressbookManagerImpl::SearchContactsByPhoneNumberN(const String& phoneNumber) const
849 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
853 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));
855 unique_ptr< __Filter<__ContactsContactNumber> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactNumber>());
857 unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(phoneNumber));
858 SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
860 __Filter<__ContactsContactNumber> numberFilter;
861 numberFilter.Construct();
862 numberFilter.AddString(_contacts_contact_number.number, CONTACTS_MATCH_CONTAINS, pCharArray.get());
864 __Filter<__ContactsContactNumber> mainFilter;
865 mainFilter.Construct();
867 mainFilter.AddFilter(*pRwAbFilter);
868 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
869 mainFilter.AddFilter(numberFilter);
871 __Query<__ContactsContactNumber> query;
873 query.SetFilter(mainFilter);
874 query.SetDistinct(true);
875 query.SetSort(_contacts_contact_number.display_name, true);
877 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactNumber, Contact>(query);
878 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
884 _AddressbookManagerImpl::GetCategoryCount(void) const
886 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
890 unique_ptr< __Filter<__ContactsGroup> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsGroup>());
892 int count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsGroup>(pRwAbFilter->Get());
893 SysTryReturn(NID_SCL, count >= 0, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
899 _AddressbookManagerImpl::GetContactCount(void) const
901 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
905 unique_ptr< __Filter<__ContactsContact> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContact>());
907 int count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContact>(pRwAbFilter->Get());
908 SysTryReturn(NID_SCL, count >= 0, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
914 _AddressbookManagerImpl::GetContactN(RecordId contactId) const
916 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
917 SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. contactId = %d.", GetErrorMessage(E_INVALID_ARG), contactId);
921 unique_ptr<ContactRecord, ContactRecordDeleter> pContactRecord(_AddressbookUtil::GetContactRecordN(_contacts_contact._uri, contactId));
922 SysTryReturn(NID_SCL, pContactRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
925 contacts_record_get_int(pContactRecord.get(), _contacts_contact.address_book_id, &intValue);
927 unique_ptr<ContactRecord, ContactRecordDeleter> pAbRecord(_AddressbookUtil::GetContactRecordN(_contacts_address_book._uri, intValue));
928 SysTryReturn(NID_SCL, pAbRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
930 contacts_record_get_int(pAbRecord.get(), _contacts_address_book.mode, &intValue);
931 SysTryReturn(NID_SCL, intValue == 0, null, E_OBJ_NOT_FOUND, "[%s] Contact does not exist.", GetErrorMessage(E_OBJ_NOT_FOUND));
933 contacts_record_get_int(pContactRecord.get(), _contacts_contact.id, &intValue);
934 SysTryReturn(NID_SCL, intValue == contactId, null, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
936 unique_ptr<Contact> pContact(new (std::nothrow) Contact());
937 SysTryReturn(NID_SCL, pContact, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
939 _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(pContactRecord.release());
940 _RecordImpl::GetInstance(*pContact)->SetRecordId(intValue);
942 return pContact.release();
946 _AddressbookManagerImpl::GetPersonN(PersonId personId) const
948 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
949 SysTryReturn(NID_SCL, personId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. personId = %d.", GetErrorMessage(E_INVALID_ARG), personId);
953 unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, personId));
956 contacts_record_get_int(pPersonRecord.get(), _contacts_person.id, &intValue);
957 SysTryReturn(NID_SCL, intValue == personId, null, E_OBJ_NOT_FOUND, "[%s] The person is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
959 Person* pPerson = __ContactsPerson::ConvertHandleTo<Person>(pPersonRecord.get());
960 SysTryReturn(NID_SCL, pPerson != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
966 _AddressbookManagerImpl::GetCategoryN(RecordId categoryId) const
968 SysTryReturn(NID_SCL, categoryId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. categoryId = %d.", categoryId);
969 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
973 unique_ptr<ContactRecord, ContactRecordDeleter> pCategoryRecord(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, categoryId));
974 SysTryReturn(NID_SCL, pCategoryRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
978 contacts_record_get_int(pCategoryRecord.get(), _contacts_group.id, &intValue);
979 SysTryReturn(NID_SCL, categoryId == intValue, null, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
981 contacts_record_get_int(pCategoryRecord.get(), _contacts_group.address_book_id, &intValue);
983 unique_ptr<ContactRecord, ContactRecordDeleter> pAbRecord(_AddressbookUtil::GetContactRecordN(_contacts_address_book._uri, intValue));
984 SysTryReturn(NID_SCL, pAbRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
986 contacts_record_get_int(pAbRecord.get(), _contacts_address_book.mode, &intValue);
987 SysTryReturn(NID_SCL, intValue == 0, null, E_OBJ_NOT_FOUND, "[%s] Category does not exist.", GetErrorMessage(E_OBJ_NOT_FOUND));
989 unique_ptr<Category> pCategory(new (std::nothrow) Category());
990 SysTryReturn(NID_SCL, pCategory != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
992 __Filter<__ContactsGroupRelation> filter;
994 filter.AddInt(_contacts_group_relation.group_id, CONTACTS_MATCH_EQUAL, categoryId);
996 __Query<__ContactsGroupRelation> query;
998 query.SetFilter(filter);
1000 int count = _AddressbookUtil::GetCountWithQuery(query);
1001 SysTryReturn(NID_SCL, count >= 0, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1003 _CategoryImpl::GetInstance(*pCategory)->SetRecordHandle(pCategoryRecord.release());
1004 _CategoryImpl::GetInstance(*pCategory)->SetMemberCount(count);
1005 _RecordImpl::GetInstance(*pCategory)->SetRecordId(categoryId);
1007 return pCategory.release();
1011 _AddressbookManagerImpl::GetLatestVersion(void) const
1013 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1015 int latestVersion = -1;
1019 int ret = contacts_db_get_current_version(&latestVersion);
1020 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1022 return latestVersion;
1026 _AddressbookManagerImpl::GetChangedContactsAfterN(int version, int& latestVersion) const
1028 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);
1029 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1033 unique_ptr< IListT<AddressbookId> > pRwAbIdList(_AddressbookUtil::GetRwAbIdListN());
1035 IList* pChangedContacts = _AddressbookUtil::SearchWithVersionN<__ContactsContactUpdatedInfo, ContactChangeInfo>(-1, version, latestVersion, pRwAbIdList.get());
1036 SysTryReturn(NID_SCL, pChangedContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1038 return pChangedContacts;
1042 _AddressbookManagerImpl::GetChangedCategoriesAfterN(int version, int& latestVersion) const
1044 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);
1045 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1049 int latestVersion1 = 0;
1050 int latestVersion2 = 0;
1052 unique_ptr< IListT<AddressbookId> > pRwAbIdList(_AddressbookUtil::GetRwAbIdListN());
1054 unique_ptr<IList, AllElementsDeleter> pChangedGroups(_AddressbookUtil::SearchWithVersionN<__ContactsGroupUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion1, pRwAbIdList.get()));
1055 SysTryReturn(NID_SCL, pChangedGroups != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1057 unique_ptr<IList, AllElementsDeleter> pChangedRelations(_AddressbookUtil::SearchWithVersionN<__ContactsGroupRelUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion2, pRwAbIdList.get()));
1058 SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1060 unique_ptr<ArrayList, AllElementsDeleter> pChangeList(new (std::nothrow) Tizen::Base::Collection::ArrayList());
1061 SysTryReturn(NID_SCL, pChangeList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1063 result r = pChangeList->AddItems(*pChangedGroups);
1064 SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1066 r = pChangeList->AddItems(*pChangedRelations);
1067 SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1069 pChangedGroups->RemoveAll(false);
1070 pChangedRelations->RemoveAll(false);
1072 latestVersion = latestVersion2 > latestVersion1 ? latestVersion2 : latestVersion1;
1074 return pChangeList.release();
1078 _AddressbookManagerImpl::GetChangedGroupsAfterN(int version, int& latestVersion) const
1080 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);
1081 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1085 unique_ptr< IListT<AddressbookId> > pRwAbIdList(_AddressbookUtil::GetRwAbIdListN());
1087 IList* pChangedRelations = _AddressbookUtil::SearchWithVersionN<__ContactsGroupUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion, pRwAbIdList.get());
1088 SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1090 return pChangedRelations;
1094 _AddressbookManagerImpl::GetChangedRelationsAfterN(int version, int& latestVersion) const
1096 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);
1097 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1101 unique_ptr< IListT<AddressbookId> > pRwAbIdList(_AddressbookUtil::GetRwAbIdListN());
1103 IList* pChangedRelations = _AddressbookUtil::SearchWithVersionN<__ContactsGroupRelUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion, pRwAbIdList.get());
1104 SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1106 return pChangedRelations;
1110 _AddressbookManagerImpl::OnContactChanged(void)
1112 if (__pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
1117 IList* pChangedContactList = GetChangedContactsAfterN(__dbVersionForContact, __dbVersionForContact);
1118 SysTryReturnVoidResult(NID_SCL, pChangedContactList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1120 if (pChangedContactList->GetCount() > 0)
1122 if (__pIAddressbookChangeEventListener != null)
1124 __pIAddressbookChangeEventListener->OnContactsChanged(*pChangedContactList);
1128 __pIAddressbookEventListener->OnContactsChanged(*pChangedContactList);
1132 pChangedContactList->RemoveAll(true);
1133 delete pChangedContactList;
1137 _AddressbookManagerImpl::OnCategoryChanged(void)
1139 if (__pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
1144 IList* pChangedCategoryList = GetChangedGroupsAfterN(__dbVersionForGroup, __dbVersionForGroup);
1145 SysTryReturnVoidResult(NID_SCL, pChangedCategoryList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1147 if (pChangedCategoryList->GetCount() > 0)
1149 if (__pIAddressbookChangeEventListener != null)
1151 __pIAddressbookChangeEventListener->OnCategoriesChanged(*pChangedCategoryList);
1155 __pIAddressbookEventListener->OnCategoriesChanged(*pChangedCategoryList);
1159 pChangedCategoryList->RemoveAll(true);
1160 delete pChangedCategoryList;
1164 _AddressbookManagerImpl::OnRelationChanged(void)
1166 if (__pIAddressbookEventListener == null)
1171 IList* pChangedCategoryList = GetChangedRelationsAfterN(__dbVersionForRelation, __dbVersionForRelation);
1172 SysTryReturnVoidResult(NID_SCL, pChangedCategoryList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1174 if (pChangedCategoryList->GetCount() > 0)
1176 if (__pIAddressbookEventListener != null)
1178 __pIAddressbookEventListener->OnCategoriesChanged(*pChangedCategoryList);
1182 pChangedCategoryList->RemoveAll(true);
1183 delete pChangedCategoryList;
1187 _AddressbookManagerImpl::RemovePerson(PersonId personId)
1189 SysTryReturnResult(NID_SCL, personId > 0, E_INVALID_ARG, "[%s] Invalid argument is used. personId = %d.", GetErrorMessage(E_INVALID_ARG), personId);
1190 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1192 unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, personId));
1193 SysTryReturn(NID_SCL, pPersonRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1195 int ret = contacts_db_delete_record(_contacts_person._uri, personId);
1196 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The person is not found.");
1197 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));
1198 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred. Failed remove a person.", GetErrorMessage(E_SYSTEM));
1204 _AddressbookManagerImpl::GetAllPersonsN(void) const
1206 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1210 unique_ptr< __Filter<__ContactsPersonGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsPersonGroupRel>());
1212 // unsigned int propertyIds[] = { _contacts_person_grouprel.person_id };
1213 unsigned int propertyIds[] =
1215 _contacts_person_grouprel.person_id,
1216 _contacts_person_grouprel.display_name,
1217 _contacts_person_grouprel.image_thumbnail_path,
1218 _contacts_person_grouprel.ringtone_path,
1219 _contacts_person_grouprel.is_favorite,
1220 _contacts_person_grouprel.has_phonenumber,
1221 _contacts_person_grouprel.has_email,
1222 _contacts_person_grouprel.addressbook_ids,
1226 __Query<__ContactsPersonGroupRel> query;
1228 query.SetFilter(*pRwAbFilter);
1229 query.SetSort(_contacts_person.display_name, true);
1230 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1231 query.SetDistinct(true);
1233 IList* pPersons = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query);
1234 SysTryReturn(NID_SCL, pPersons != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1240 _AddressbookManagerImpl::GetPersonsByCategoryN(RecordId categoryId) const
1242 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));
1243 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1247 unique_ptr< __Filter<__ContactsPersonGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsPersonGroupRel>());
1249 __Filter<__ContactsPersonGroupRel> groupFilter;
1250 groupFilter.Construct();
1251 if (categoryId != INVALID_RECORD_ID)
1253 groupFilter.AddInt(_contacts_person_grouprel.group_id, CONTACTS_MATCH_EQUAL, categoryId);
1257 groupFilter.AddInt(_contacts_person_grouprel.group_id, CONTACTS_MATCH_NONE, 0);
1260 unsigned int propertyIds[] =
1262 _contacts_person_grouprel.person_id,
1263 _contacts_person_grouprel.display_name,
1264 _contacts_person_grouprel.image_thumbnail_path,
1265 _contacts_person_grouprel.ringtone_path,
1266 _contacts_person_grouprel.is_favorite,
1267 _contacts_person_grouprel.has_phonenumber,
1268 _contacts_person_grouprel.has_email,
1269 _contacts_person_grouprel.addressbook_ids,
1272 __Filter<__ContactsPersonGroupRel> mainFilter;
1273 mainFilter.Construct();
1275 mainFilter.AddFilter(*pRwAbFilter);
1276 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1277 mainFilter.AddFilter(groupFilter);
1280 __Query<__ContactsPersonGroupRel> query;
1282 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1283 query.SetFilter(mainFilter);
1284 query.SetSort(_contacts_person_grouprel.display_name, true);
1285 query.SetDistinct(true);
1287 IList* pPersons = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query);
1288 SysTryReturn(NID_SCL, pPersons != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1294 _AddressbookManagerImpl::GetFavoritePersonsN() const
1296 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1300 __Filter<__ContactsPerson> filter;
1302 filter.AddBool(_contacts_person.is_favorite, true);
1304 __Query<__ContactsPerson> query;
1306 query.SetFilter(filter);
1307 query.SetSort(_contacts_person.display_name, true);
1309 IList* pPersons = _AddressbookUtil::SearchWithQueryN<__ContactsPerson, Person>(query);
1310 SysTryReturn(NID_SCL, pPersons != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1316 _AddressbookManagerImpl::SearchPersonsN(const Tizen::Base::String& keyword) const
1318 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1319 SysTryReturn(NID_SCL, !keyword.IsEmpty(), null, E_INVALID_ARG, "Invalid argument is used. keyword is empty string.", GetErrorMessage(E_INVALID_ARG));
1323 contacts_record_h currentRecord = null;
1324 unique_ptr<Person> pPerson(null);
1326 unique_ptr<ArrayList, AllElementsDeleter> pPersonList(new (std::nothrow) ArrayList());
1327 SysTryReturn(NID_SCL, pPersonList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1329 result r = pPersonList->Construct();
1330 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1332 unique_ptr<__SearchResult<__ContactsPerson> > pSearchResult(_AddressbookUtil::Search<__ContactsPerson>(keyword));
1333 SysTryReturn(NID_SCL, pSearchResult != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1335 while (pSearchResult->MoveNext() == E_SUCCESS)
1337 currentRecord = pSearchResult->GetCurrentRecord();
1338 SysTryReturn(NID_SCL, currentRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1340 pPerson.reset(__ContactsPerson::ConvertHandleTo<Person>(currentRecord));
1341 SysTryReturn(NID_SCL, pPerson != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1343 r = pPersonList->Add(*pPerson);
1344 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
1349 return pPersonList.release();
1353 _AddressbookManagerImpl::SetPersonAsFavorite(PersonId personId, bool isFavorite)
1355 SysTryReturn(NID_SCL, personId > 0, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. personId = %d.", GetErrorMessage(E_INVALID_ARG), personId);
1356 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1358 unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, personId));
1359 SysTryReturn(NID_SCL, pPersonRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1361 bool boolValue = false;
1362 contacts_record_get_bool(pPersonRecord.get(), _contacts_person.is_favorite, &boolValue);
1364 if (boolValue != isFavorite)
1366 contacts_record_set_bool(pPersonRecord.get(), _contacts_person.is_favorite, isFavorite);
1368 int ret = contacts_db_update_record(pPersonRecord.get());
1369 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));
1370 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1377 _AddressbookManagerImpl::MergePersons(PersonId sourcePersonId, PersonId targetPersonId)
1379 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);
1380 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1382 int ret = contacts_person_link_person(targetPersonId, sourcePersonId);
1383 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1384 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1390 _AddressbookManagerImpl::UnlinkContact(PersonId personId, RecordId contactId, PersonId& newPersonId)
1392 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);
1393 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1395 int ret = contacts_person_unlink_contact(personId, contactId, &newPersonId);
1396 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));
1397 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1403 _AddressbookManagerImpl::SearchN(const AddressbookFilter& filter, unsigned long propertySortedBy, SortOrder sortOrder, int offset, int maxCount)
1405 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1409 IList* pList = null;
1410 bool ascending = false;
1411 unsigned int viewSortPropertyId = 0;
1413 AddressbookFilterType type = _AddressbookFilterImpl::GetInstance(filter)->GetType();
1414 contacts_filter_h filterHandle = _AddressbookFilterImpl::GetInstance(filter)->GetFilterHandle();
1416 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);
1418 if (propertySortedBy != 0 && sortOrder != SORT_ORDER_NONE)
1420 viewSortPropertyId = _AddressbookFilterImpl::GetViewPropertyId(type, propertySortedBy);
1421 ascending = (sortOrder == SORT_ORDER_ASCENDING) ? true : false;
1426 case AB_FI_TYPE_ADDRESSBOOK:
1428 unique_ptr< __Filter<__ContactsAddressbook> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsAddressbook>());
1430 __Filter<__ContactsAddressbook> abFilter;
1431 abFilter.Construct(filterHandle);
1433 __Filter<__ContactsAddressbook> mainFilter;
1434 mainFilter.Construct();
1436 mainFilter.AddFilter(*pRwAbFilter);
1437 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1438 mainFilter.AddFilter(abFilter);
1440 __Query<__ContactsAddressbook> query;
1442 query.SetFilter(mainFilter);
1444 if (viewSortPropertyId != 0)
1446 query.SetSort(viewSortPropertyId, ascending);
1449 pList = _AddressbookUtil::SearchWithQueryN<__ContactsAddressbook, Addressbook>(query, offset, maxCount);
1452 case AB_FI_TYPE_PERSON:
1454 unique_ptr< __Filter<__ContactsPersonGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsPersonGroupRel>());
1456 __Filter<__ContactsPersonGroupRel> personFilter;
1457 personFilter.Construct(filterHandle);
1459 __Filter<__ContactsPersonGroupRel> mainFilter;
1460 mainFilter.Construct();
1462 mainFilter.AddFilter(*pRwAbFilter);
1463 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1464 mainFilter.AddFilter(personFilter);
1466 unsigned int propertyIds[] =
1467 { _contacts_person_grouprel.person_id,
1468 _contacts_person_grouprel.addressbook_ids,
1469 _contacts_person_grouprel.is_favorite,
1470 _contacts_person_grouprel.has_phonenumber,
1471 _contacts_person_grouprel.has_email,
1472 _contacts_person_grouprel.image_thumbnail_path,
1473 _contacts_person_grouprel.ringtone_path,
1474 _contacts_person_grouprel.display_name
1477 __Query<__ContactsPersonGroupRel> query;
1479 query.SetFilter(mainFilter);
1480 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1481 query.SetDistinct(true);
1483 if (viewSortPropertyId != 0)
1485 query.SetSort(viewSortPropertyId, ascending);
1488 pList = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query, offset, maxCount);
1491 case AB_FI_TYPE_CONTACT:
1493 unique_ptr< __Filter<__ContactsContact> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContact>());
1495 __Filter<__ContactsContact> contactFilter;
1496 contactFilter.Construct(filterHandle);
1498 __Filter<__ContactsContact> mainFilter;
1499 mainFilter.Construct();
1501 mainFilter.AddFilter(*pRwAbFilter);
1502 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1503 mainFilter.AddFilter(contactFilter);
1505 __Query<__ContactsContact> query;
1507 query.SetFilter(mainFilter);
1509 if (viewSortPropertyId != 0)
1511 query.SetSort(viewSortPropertyId, ascending);
1514 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query, offset, maxCount);
1517 case AB_FI_TYPE_CATEGORY:
1519 unique_ptr< __Filter<__ContactsGroup> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsGroup>());
1521 __Filter<__ContactsGroup> groupFilter;
1522 groupFilter.Construct(filterHandle);
1524 __Filter<__ContactsGroup> mainFilter;
1525 mainFilter.Construct();
1527 mainFilter.AddFilter(*pRwAbFilter);
1528 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1529 mainFilter.AddFilter(groupFilter);
1531 __Query<__ContactsGroup> query;
1533 query.SetFilter(mainFilter);
1535 if (viewSortPropertyId != 0)
1537 query.SetSort(viewSortPropertyId, ascending);
1540 pList = _AddressbookUtil::SearchWithQueryN<__ContactsGroup, Category>(query, offset, maxCount);
1543 case AB_FI_TYPE_PHONE_CONTACT:
1545 unique_ptr< __Filter<__ContactsContactNumber> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactNumber>());
1547 __Filter<__ContactsContactNumber> numberFilter;
1548 numberFilter.Construct(filterHandle);
1550 __Filter<__ContactsContactNumber> mainFilter;
1551 mainFilter.Construct();
1553 mainFilter.AddFilter(*pRwAbFilter);
1554 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1555 mainFilter.AddFilter(numberFilter);
1557 __Query<__ContactsContactNumber> query;
1559 query.SetFilter(mainFilter);
1561 if (viewSortPropertyId != 0)
1563 query.SetSort(viewSortPropertyId, ascending);
1566 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContactNumber, PhoneNumberContact>(query, offset, maxCount);
1569 case AB_FI_TYPE_EMAIL_CONTACT:
1571 unique_ptr< __Filter<__ContactsContactEmail> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactEmail>());
1573 __Filter<__ContactsContactEmail> emailFilter;
1574 emailFilter.Construct(filterHandle);
1576 __Filter<__ContactsContactEmail> mainFilter;
1577 mainFilter.Construct();
1579 mainFilter.AddFilter(*pRwAbFilter);
1580 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1581 mainFilter.AddFilter(emailFilter);
1583 __Query<__ContactsContactEmail> query;
1585 query.SetFilter(mainFilter);
1587 if (viewSortPropertyId != 0)
1589 query.SetSort(viewSortPropertyId, ascending);
1592 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContactEmail, EmailContact>(query, offset, maxCount);
1596 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. The filter type is invalid", GetErrorMessage(E_INVALID_ARG));
1604 _AddressbookManagerImpl::GetMatchedItemCount(const AddressbookFilter& filter)
1606 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1611 AddressbookFilterType type = _AddressbookFilterImpl::GetInstance(filter)->GetType();
1612 contacts_filter_h filterHandle = _AddressbookFilterImpl::GetInstance(filter)->GetFilterHandle();
1616 case AB_FI_TYPE_ADDRESSBOOK:
1618 unique_ptr< __Filter<__ContactsAddressbook> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsAddressbook>());
1620 __Filter<__ContactsAddressbook> abFilter;
1621 abFilter.Construct(filterHandle);
1623 __Filter<__ContactsAddressbook> mainFilter;
1624 mainFilter.Construct();
1625 mainFilter.AddFilter(*pRwAbFilter);
1626 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1627 mainFilter.AddFilter(abFilter);
1629 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsAddressbook>(mainFilter.Get());
1632 case AB_FI_TYPE_PERSON:
1634 unique_ptr< __Filter<__ContactsPersonGroupRel> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsPersonGroupRel>());
1636 __Filter<__ContactsPersonGroupRel> personFilter;
1637 personFilter.Construct(filterHandle);
1639 __Filter<__ContactsPersonGroupRel> mainFilter;
1640 mainFilter.Construct();
1641 mainFilter.AddFilter(*pRwAbFilter);
1642 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1643 mainFilter.AddFilter(personFilter);
1645 unsigned int propertyIds[] = { _contacts_person_grouprel.person_id };
1647 __Query<__ContactsPersonGroupRel> query;
1649 query.SetFilter(mainFilter);
1650 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1651 query.SetDistinct(true);
1653 count = _AddressbookUtil::GetCountWithQuery(query);
1657 case AB_FI_TYPE_CONTACT:
1659 unique_ptr< __Filter<__ContactsContact> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContact>());
1661 __Filter<__ContactsContact> contactFilter;
1662 contactFilter.Construct(filterHandle);
1664 __Filter<__ContactsContact> mainFilter;
1665 mainFilter.Construct();
1666 mainFilter.AddFilter(*pRwAbFilter);
1667 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1668 mainFilter.AddFilter(contactFilter);
1670 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContact>(mainFilter.Get());
1673 case AB_FI_TYPE_CATEGORY:
1675 unique_ptr< __Filter<__ContactsGroup> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsGroup>());
1677 __Filter<__ContactsGroup> groupFilter;
1678 groupFilter.Construct(filterHandle);
1680 __Filter<__ContactsGroup> mainFilter;
1681 mainFilter.Construct();
1682 mainFilter.AddFilter(*pRwAbFilter);
1683 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1684 mainFilter.AddFilter(groupFilter);
1686 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsGroup>(mainFilter.Get());
1689 case AB_FI_TYPE_PHONE_CONTACT:
1691 unique_ptr< __Filter<__ContactsContactNumber> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactNumber>());
1693 __Filter<__ContactsContactNumber> numberFilter;
1694 numberFilter.Construct(filterHandle);
1696 __Filter<__ContactsContactNumber> mainFilter;
1697 mainFilter.Construct();
1698 mainFilter.AddFilter(*pRwAbFilter);
1699 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1700 mainFilter.AddFilter(numberFilter);
1702 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContactNumber>(mainFilter.Get());
1705 case AB_FI_TYPE_EMAIL_CONTACT:
1707 unique_ptr< __Filter<__ContactsContactEmail> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsContactEmail>());
1709 __Filter<__ContactsContactEmail> emailFilter;
1710 emailFilter.Construct(filterHandle);
1712 __Filter<__ContactsContactEmail> mainFilter;
1713 mainFilter.Construct();
1714 mainFilter.AddFilter(*pRwAbFilter);
1715 mainFilter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
1716 mainFilter.AddFilter(emailFilter);
1718 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContactEmail>(mainFilter.Get());
1722 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the filter is invalid", GetErrorMessage(GetLastResult()));
1730 _AddressbookManagerImpl::OnEachContact(contacts_record_h recordHandle, void* pUserData)
1732 IList* pList = static_cast<IList*>(pUserData);
1736 unique_ptr<Contact> pContact(new (std::nothrow) Contact());
1737 SysTryReturn(NID_SCL, pContact != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1739 contacts_record_h newRecordHandle = null;
1741 contacts_record_clone(recordHandle, &newRecordHandle);
1742 SysTryReturn(NID_SCL, newRecordHandle != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1744 _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(newRecordHandle);
1746 result r = pList->Add(*pContact);
1747 SysTryReturn(NID_SCL, !IsFailed(r), false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1755 _AddressbookManagerImpl::ParseContactsFromVcardN(const Tizen::Base::String& vcardPath)
1757 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1762 result r = file.Construct(vcardPath, "r");
1763 SysTryReturn(NID_SCL, r != E_INVALID_ARG, null, E_INVALID_ARG, "[%s] Invalid argument is used..", GetErrorMessage(E_INVALID_ARG));
1764 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));
1765 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));
1766 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1768 unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
1770 unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(vcardPath));
1772 int ret = contacts_vcard_parse_to_contact_foreach(pCharArray.get(), OnEachContact, pList.get());
1773 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1774 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1775 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1777 return pList.release();
1781 _AddressbookManagerImpl::ExportPersonToVcard(const Person& person, const Tizen::Base::String& vcardPath)
1783 bool exist = File::IsFileExist(vcardPath);
1784 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
1785 SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
1786 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1789 result r = file.Construct(vcardPath, "w");
1790 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));
1791 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1792 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1794 unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, person.GetId()));
1795 SysTryReturn(NID_SCL, pPersonRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1797 char* pVcardStream = null;
1798 int ret = contacts_vcard_make_from_person(pPersonRecord.get(), &pVcardStream);
1799 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));
1800 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1802 r = file.Write(pVcardStream, strlen(pVcardStream));
1804 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1805 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1811 _AddressbookManagerImpl::ExportPersonsToVcard(const Tizen::Base::Collection::IList& personList, const Tizen::Base::String& vcardPath)
1813 bool exist = File::IsFileExist(vcardPath);
1814 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
1815 SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
1816 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1818 int ret = CONTACTS_ERROR_NONE;
1819 char* pVcardStream = null;
1822 result r = file.Construct(vcardPath, "w");
1823 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));
1824 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1825 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1828 unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(null);
1830 unique_ptr<IEnumerator> pEnum(personList.GetEnumeratorN());
1831 SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1833 while (pEnum->MoveNext() == E_SUCCESS)
1835 Person* pPerson = static_cast<Person*>(pEnum->GetCurrent());
1837 pPersonRecord.reset(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, pPerson->GetId()));
1838 SysTryReturn(NID_SCL, pPersonRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1840 ret = contacts_vcard_make_from_person(pPersonRecord.get(), &pVcardStream);
1841 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));
1842 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1844 r = file.Write(pVcardStream, strlen(pVcardStream));
1846 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1847 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1854 _AddressbookManagerImpl::ExportContactToVcard(const Contact& contact, const Tizen::Base::String& vcardPath)
1856 bool exist = File::IsFileExist(vcardPath);
1857 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
1858 SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
1859 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1862 result r = file.Construct(vcardPath, "w");
1863 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));
1864 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1865 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1867 contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
1869 char* pVcardStream = null;
1871 int ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
1872 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));
1873 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1875 r = file.Write(pVcardStream, strlen(pVcardStream));
1877 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1878 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1884 _AddressbookManagerImpl::ExportContactsToVcard(const Tizen::Base::Collection::IList& contactList, const Tizen::Base::String& vcardPath)
1886 bool exist = File::IsFileExist(vcardPath);
1887 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
1888 SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
1889 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1891 int ret = CONTACTS_ERROR_NONE;
1892 char* pVcardStream = null;
1894 Contact* pContact = null;
1896 result r = file.Construct(vcardPath, "w");
1897 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));
1898 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1899 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1901 contacts_record_h recordHandle = null;
1903 unique_ptr<IEnumerator> pEnum(contactList.GetEnumeratorN());
1904 SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1906 while (pEnum->MoveNext() == E_SUCCESS)
1908 pContact = static_cast<Contact*>(pEnum->GetCurrent());
1910 recordHandle = _ContactImpl::GetInstance(*pContact)->GetContactRecordHandle();
1912 ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
1913 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));
1914 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1916 r = file.Write(pVcardStream, strlen(pVcardStream));
1918 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1919 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1926 _AddressbookManagerImpl::ExportContactToVcardStreamN(const Contact& contact)
1928 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1932 contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
1934 char* pVcardStream = null;
1935 int ret = CONTACTS_ERROR_NONE;
1937 ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
1938 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1939 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1940 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1942 unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
1943 if (pByteBuffer == null)
1946 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1951 result r = pByteBuffer->Construct(strlen(pVcardStream));
1955 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1960 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
1962 SysTryReturn(NID_SCL, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1963 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1965 return pByteBuffer.release();
1969 _AddressbookManagerImpl::ExportContactsToVcardStreamN(const Tizen::Base::Collection::IList& contactList)
1971 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1975 char* pVcardStream = null;
1976 int ret = CONTACTS_ERROR_NONE;
1977 Contact* pContact = null;
1978 result r = E_SUCCESS;
1981 contacts_record_h recordHandle = null;
1983 unique_ptr<IEnumerator> pEnum(contactList.GetEnumeratorN());
1984 SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1986 unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
1987 SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1989 r = pByteBuffer->Construct(capacity);
1990 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1992 if (contactList.GetCount() == 0)
1994 return pByteBuffer.release();
1997 while (pEnum->MoveNext() == E_SUCCESS)
1999 pContact = static_cast<Contact*>(pEnum->GetCurrent());
2001 recordHandle = _ContactImpl::GetInstance(*pContact)->GetContactRecordHandle();
2003 ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
2004 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2005 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2006 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2008 capacity += strlen(pVcardStream);
2009 r = pByteBuffer->ExpandCapacity(capacity);
2013 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2018 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2020 pVcardStream = null;
2021 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.: capacity(%d), size(%d)", GetErrorMessage(E_SYSTEM));
2024 return pByteBuffer.release();
2028 _AddressbookManagerImpl::ExportPersonToVcardStreamN(const Person& person)
2030 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2034 unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, person.GetId()));
2035 SysTryReturn(NID_SCL, GetLastResult() != E_OBJ_NOT_FOUND, null, E_INVALID_ARG, "[%s] Person does not exist.", GetErrorMessage(E_INVALID_ARG));
2036 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, null, E_INVALID_ARG, "[%s] Person does not exist.", GetErrorMessage(E_INVALID_ARG));
2038 char* pVcardStream = null;
2039 int ret = contacts_vcard_make_from_person(pPersonRecord.get(), &pVcardStream);
2040 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2041 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2042 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2044 unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2045 if (pByteBuffer == null)
2048 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2053 result r = pByteBuffer->Construct(strlen(pVcardStream));
2057 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2062 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2064 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2066 return pByteBuffer.release();
2070 _AddressbookManagerImpl::ExportPersonsToVcardStreamN(const Tizen::Base::Collection::IList& personList)
2072 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2076 int ret = CONTACTS_ERROR_NONE;
2077 Person* pPerson = null;
2078 char* pVcardStream = null;
2082 unique_ptr<ContactRecord, ContactRecordDeleter> pPersonRecord(null);
2084 unique_ptr<IEnumerator> pEnum(personList.GetEnumeratorN());
2085 SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2087 unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2088 SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2090 result r = pByteBuffer->Construct(capacity);
2091 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2093 if (personList.GetCount() == 0)
2095 return pByteBuffer.release();
2098 while (pEnum->MoveNext() == E_SUCCESS)
2100 pPerson = static_cast<Person*>(pEnum->GetCurrent());
2102 pPersonRecord.reset(_AddressbookUtil::GetContactRecordN(_contacts_person._uri, pPerson->GetId()));
2103 SysTryReturn(NID_SCL, GetLastResult() != E_OBJ_NOT_FOUND, null, E_INVALID_ARG, "[%s] Person does not exist.", GetErrorMessage(E_INVALID_ARG));
2104 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, null, E_INVALID_ARG, "[%s] Person does not exist.", GetErrorMessage(E_INVALID_ARG));
2106 ret = contacts_vcard_make_from_person(pPersonRecord.get(), &pVcardStream);
2107 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2108 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2109 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2111 capacity += strlen(pVcardStream);
2112 r = pByteBuffer->ExpandCapacity(capacity);
2116 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2121 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2123 pVcardStream = null;
2124 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2127 return pByteBuffer.release();
2131 _AddressbookManagerImpl::ParseVcardStreamN(const Tizen::Base::ByteBuffer& vcardStream)
2133 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2137 contacts_list_h listHandle = null;
2138 result r = E_SUCCESS;
2140 int ret = contacts_vcard_parse_to_contacts(reinterpret_cast<char*>(const_cast<byte*>(vcardStream.GetPointer())), &listHandle);
2141 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2142 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2144 unsigned int count = 0;
2145 contacts_record_h recordHandle = null;
2146 contacts_list_get_count(listHandle, &count);
2148 unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
2151 contacts_list_destroy(listHandle, true);
2152 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2157 r = pList->Construct(count);
2160 contacts_list_destroy(listHandle, true);
2161 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2166 for (unsigned int i = 0; i < count; i++)
2168 unique_ptr<Contact> pContact(new (std::nothrow) Contact());
2169 if (pContact == null)
2171 contacts_list_destroy(listHandle, true);
2172 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2177 r = pList->Add(pContact.get());
2180 contacts_list_destroy(listHandle, true);
2181 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2189 unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
2191 while (pEnum->MoveNext() == E_SUCCESS)
2193 Contact* pContact = static_cast <Contact*> (pEnum->GetCurrent());
2195 contacts_list_get_current_record_p(listHandle, &recordHandle);
2196 _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(recordHandle);
2198 ret = contacts_list_next(listHandle);
2201 contacts_list_destroy(listHandle, false);
2203 return pList.release();
2207 _AddressbookManagerImpl::ExportUserProfileToVcardStreamN(const UserProfile& userProfile)
2209 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2213 char* pVcardStream = null;
2214 int ret = CONTACTS_ERROR_NONE;
2215 contacts_record_h recordHandle = null;
2217 recordHandle = _UserProfileImpl::GetInstance(userProfile)->GetUserProfileHandle();
2219 ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2220 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2221 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2222 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2224 unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2225 if (pByteBuffer == null)
2228 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2232 SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2234 result r = pByteBuffer->Construct(strlen(pVcardStream));
2238 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2243 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2245 SysTryReturn(NID_SCL, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2246 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2248 return pByteBuffer.release();
2252 _AddressbookManagerImpl::ExportUserProfilesToVcardStreamN(const Tizen::Base::Collection::IList& userProfileList)
2254 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2258 char* pVcardStream = null;
2259 int ret = CONTACTS_ERROR_NONE;
2260 UserProfile* pProfile = null;
2261 result r = E_SUCCESS;
2264 unique_ptr<IEnumerator> pEnum(userProfileList.GetEnumeratorN());
2265 SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2267 unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2268 SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2270 r = pByteBuffer->Construct(capacity);
2271 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2273 if (userProfileList.GetCount() == 0)
2275 return pByteBuffer.release();
2278 while (pEnum->MoveNext() == E_SUCCESS)
2280 contacts_record_h recordHandle = null;
2282 pProfile = static_cast<UserProfile*>(pEnum->GetCurrent());
2284 recordHandle = _UserProfileImpl::GetInstance(*pProfile)->GetUserProfileHandle();
2286 ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2287 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2288 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2289 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2291 capacity += strlen(pVcardStream);
2292 r = pByteBuffer->ExpandCapacity(capacity);
2296 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2301 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2303 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.: capacity(%d), size(%d)", GetErrorMessage(E_SYSTEM));
2306 return pByteBuffer.release();
2310 _AddressbookManagerImpl::ExportUserProfileToVcard(const UserProfile& userProfile, const Tizen::Base::String& vcardPath)
2312 bool exist = File::IsFileExist(vcardPath);
2313 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2314 SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2315 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2318 result r = file.Construct(vcardPath, "w");
2319 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));
2320 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2321 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2323 char* pVcardStream = null;
2324 contacts_record_h recordHandle = null;
2326 recordHandle = _UserProfileImpl::GetInstance(userProfile)->GetUserProfileHandle();
2328 int ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2329 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));
2330 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2332 r = file.Write(pVcardStream, strlen(pVcardStream));
2334 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2335 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2341 _AddressbookManagerImpl::ExportUserProfilesToVcard(const Tizen::Base::Collection::IList& userProfileList, const Tizen::Base::String& vcardPath)
2343 bool exist = File::IsFileExist(vcardPath);
2344 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2345 SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2346 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2348 int ret = CONTACTS_ERROR_NONE;
2349 char* pVcardStream = null;
2351 UserProfile* pProfile = null;
2353 result r = file.Construct(vcardPath, "w");
2354 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));
2355 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2356 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2358 unique_ptr<IEnumerator> pEnum(userProfileList.GetEnumeratorN());
2359 SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2361 while (pEnum->MoveNext() == E_SUCCESS)
2363 contacts_record_h recordHandle = null;
2365 pProfile = static_cast<UserProfile*>(pEnum->GetCurrent());
2367 recordHandle = _UserProfileImpl::GetInstance(*pProfile)->GetUserProfileHandle();
2369 ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2370 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));
2371 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2373 r = file.Write(pVcardStream, strlen(pVcardStream));
2375 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2376 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2383 _AddressbookManagerImpl::GetAllUserProfilesN(void) const
2385 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2389 unique_ptr< __Filter<__ContactsUserProfile> > pRwAbFilter(_AddressbookUtil::GetRwAbFilterN<__ContactsUserProfile>());
2390 SysTryReturn(NID_SCL, pRwAbFilter != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2392 __Query<__ContactsUserProfile> query2;
2394 query2.SetFilter(*pRwAbFilter);
2395 query2.SetSort(_contacts_my_profile.display_name, true);
2397 IList* pUserProfilesList = _AddressbookUtil::SearchWithQueryN<__ContactsUserProfile, UserProfile>(query2);
2398 SysTryReturn(NID_SCL, pUserProfilesList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2400 return pUserProfilesList;
2404 _AddressbookManagerImpl::GetUserProfileN(AddressbookId addressbookId) const
2406 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2407 SysTryReturn(NID_SCL, addressbookId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. Addressbook Id(%d).", GetErrorMessage(E_INVALID_ARG), addressbookId);
2412 unique_ptr<ContactRecord, ContactRecordDeleter> pAbRecord(_AddressbookUtil::GetContactRecordN(_contacts_address_book._uri, addressbookId));
2413 SysTryReturn(NID_SCL, pAbRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2415 contacts_record_get_int(pAbRecord.get(), _contacts_address_book.mode, &mode);
2416 SysTryReturn(NID_SCL, mode == 0, null, E_OBJ_NOT_FOUND, "[%s] The addressbook does not exist.", GetErrorMessage(E_OBJ_NOT_FOUND));
2418 __Filter<__ContactsUserProfile> filter;
2420 filter.AddInt(_contacts_my_profile.address_book_id, CONTACTS_MATCH_EQUAL, addressbookId);
2422 __Query<__ContactsUserProfile> query;
2424 query.SetFilter(filter);
2426 unique_ptr<IList, AllElementsDeleter> pUserProfilesList(_AddressbookUtil::SearchWithQueryN<__ContactsUserProfile, UserProfile>(query));
2427 SysTryReturn(NID_SCL, pUserProfilesList.get() != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2428 SysTryReturn(NID_SCL, pUserProfilesList->GetCount() != 0, null, E_SUCCESS, "No UserProfile Set for this Addressbook.");
2429 SysTryReturn(NID_SCL, pUserProfilesList->GetCount() == 1, null, E_SYSTEM, "[%s] Propagating. More than one UserProfile not allowed.", GetErrorMessage(E_SYSTEM));
2431 UserProfile* pProfile = new (std::nothrow) UserProfile(*(static_cast<UserProfile*>(pUserProfilesList->GetAt(0))));
2432 SysTryReturn(NID_SCL, pProfile != null, null, E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(E_OUT_OF_MEMORY));
2437 _AddressbookManagerImpl*
2438 _AddressbookManagerImpl::GetInstance(AddressbookManager& addressbookManager)
2440 return addressbookManager.__pAddressbookManagerImpl;
2443 const _AddressbookManagerImpl*
2444 _AddressbookManagerImpl::GetInstance(const AddressbookManager& addressbookManager)
2446 return addressbookManager.__pAddressbookManagerImpl;