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"
65 using namespace Tizen::App;
66 using namespace Tizen::Base;
67 using namespace Tizen::Base::Collection;
68 using namespace Tizen::Io;
69 using namespace Tizen::Graphics;
71 namespace Tizen { namespace Social
74 _AddressbookManagerImpl::_AddressbookManagerImpl(void)
75 : __pIAddressbookEventListener(null)
76 , __pIAddressbookChangeEventListener(null)
77 , __dbVersionForContact(0)
78 , __dbVersionForGroup(0)
79 , __dbVersionForRelation(0)
84 _AddressbookManagerImpl::~_AddressbookManagerImpl(void)
86 if (__pIAddressbookEventListener != null || __pIAddressbookChangeEventListener != null)
88 _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
89 if (pContactDbMonitor != null)
91 pContactDbMonitor->RemoveListener(*this);
97 _AddressbookManagerImpl::Construct(void)
99 result r = _ContactDbConnector::EnsureDbConnection();
100 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
106 _AddressbookManagerImpl::SetEventListener(IAddressbookEventListener* pListener)
108 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
110 result r = E_SUCCESS;
112 if (pListener != null)
114 if (__pIAddressbookEventListener == null && __pIAddressbookChangeEventListener == null)
116 _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
117 SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
119 r = pContactDbMonitor->AddListener(*this);
120 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
123 __dbVersionForContact = GetLatestVersion();
124 SysTryReturn(NID_SCL, __dbVersionForContact != -1, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
126 __dbVersionForGroup = __dbVersionForContact;
127 __dbVersionForRelation = __dbVersionForContact;
129 __pIAddressbookChangeEventListener = null;
130 __pIAddressbookEventListener = pListener;
134 if (__pIAddressbookEventListener != null || __pIAddressbookChangeEventListener != null)
137 _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
138 SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
140 pContactDbMonitor->RemoveListener(*this);
143 __pIAddressbookEventListener = null;
144 __pIAddressbookChangeEventListener = null;
151 _AddressbookManagerImpl::SetAddressbookChangeEventListener(IAddressbookChangeEventListener* pListener)
153 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
155 result r = E_SUCCESS;
157 if (pListener != null)
159 if (__pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
161 _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
162 SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
164 r = pContactDbMonitor->AddListener(*this);
165 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
168 __dbVersionForContact = GetLatestVersion();
169 SysTryReturn(NID_SCL, __dbVersionForContact != -1, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
171 __dbVersionForGroup = __dbVersionForContact;
172 __dbVersionForRelation = __dbVersionForContact;
174 __pIAddressbookEventListener = null;
175 __pIAddressbookChangeEventListener = pListener;
179 if (__pIAddressbookChangeEventListener != null || __pIAddressbookEventListener != null)
182 _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
183 SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
185 pContactDbMonitor->RemoveListener(*this);
188 __pIAddressbookChangeEventListener = null;
189 __pIAddressbookEventListener = null;
196 _AddressbookManagerImpl::CreateAddressbookN(AccountId accountId, const String& name)
198 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
199 SysTryReturn(NID_SCL, accountId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. account id is invalid.", GetErrorMessage(E_INVALID_ARG));
200 SysTryReturn(NID_SCL, !name.IsEmpty(), null, E_INVALID_ARG, "[%s] Invalid argument is used. The name is empty.", GetErrorMessage(E_INVALID_ARG));
204 result r = E_SUCCESS;
205 int ret = CONTACTS_ERROR_NONE;
207 contacts_record_h addressbookHandle = null;
209 std::unique_ptr<char[]> pNameString(_StringConverter::CopyToCharArrayN(name));
210 SysTryReturn(NID_SCL, pNameString !=null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
212 __Filter<__ContactsAddressbook> filter;
214 filter.AddString(_contacts_address_book.name, CONTACTS_MATCH_EXACTLY, pNameString.get());
217 filter.AddOperator(CONTACTS_FILTER_OPERATOR_OR);
218 filter.AddInt(_contacts_address_book.account_id, CONTACTS_MATCH_EQUAL, accountId);
221 __Query<__ContactsAddressbook> query;
223 query.SetFilter(filter);
225 int count = _AddressbookUtil::GetCountWithQuery(query);
226 SysTryReturn(NID_SCL, count == 0, null, E_OBJ_ALREADY_EXIST, "[%s] The name is already being used by other addressbook.", GetErrorMessage(E_OBJ_ALREADY_EXIST));
228 std::unique_ptr<Addressbook> pAddressbook(new (std::nothrow) Addressbook());
229 SysTryReturn(NID_SCL, pAddressbook != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
231 r = pAddressbook->Construct();
232 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
234 ret = contacts_record_create(_contacts_address_book._uri, &addressbookHandle);
235 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
236 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
238 __ContactsRecordHandle recordHandle(addressbookHandle);
240 contacts_record_set_str(addressbookHandle, _contacts_address_book.name, pNameString.get());
241 contacts_record_set_int(addressbookHandle, _contacts_address_book.account_id, accountId);
243 ret = contacts_db_insert_record(addressbookHandle, &recordId);
244 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
245 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
246 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
248 _AddressbookImpl::GetInstance(*pAddressbook)->SetAccountId(accountId);
249 _AddressbookImpl::GetInstance(*pAddressbook)->SetName(name);
250 _AddressbookImpl::GetInstance(*pAddressbook)->SetId(recordId);
252 return pAddressbook.release();
256 _AddressbookManagerImpl::DeleteAddressbook(AddressbookId addressbookId)
258 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));
259 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
261 int ret = contacts_db_delete_record(_contacts_address_book._uri, addressbookId);
262 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));
263 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));
264 // SysTryReturnResult(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OPERATION_FAILED, "Failed to delete an addressbook.(%d)", ret); // temp
270 _AddressbookManagerImpl::GetAddressbooksByAccountN(AccountId accountId) const
272 SysTryReturn(NID_SCL, accountId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. accountId = %d.", GetErrorMessage(E_INVALID_ARG), accountId);
273 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
277 __Filter<__ContactsAddressbook> filter;
279 filter.AddInt(_contacts_address_book.account_id, CONTACTS_MATCH_EQUAL, accountId);
281 __Query<__ContactsAddressbook> query;
283 query.SetFilter(filter);
285 IList* pAddressbooks = _AddressbookUtil::SearchWithQueryN<__ContactsAddressbook, Addressbook>(query);
286 SysTryReturn(NID_SCL, pAddressbooks != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
288 return pAddressbooks;
292 _AddressbookManagerImpl::GetAllAddressbooksN(void) const
294 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
298 __Query<__ContactsAddressbook> query;
300 query.SetSort(_contacts_address_book.name, true);
302 IList* pAddressbooks = _AddressbookUtil::SearchWithQueryN<__ContactsAddressbook, Addressbook>(query);
303 SysTryReturn(NID_SCL, pAddressbooks != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
305 return pAddressbooks;
309 _AddressbookManagerImpl::GetAddressbookN(AddressbookId addressbookId) const
311 SysTryReturn(NID_SCL, addressbookId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. addressbookId.", GetErrorMessage(E_INVALID_ARG), addressbookId);
312 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
316 result r = E_SUCCESS;
317 int ret = CONTACTS_ERROR_NONE;
318 contacts_record_h addressbookHandle = null;
320 char* pCharValue = null;
322 std::unique_ptr<Addressbook> pAddressbook(new (std::nothrow) Addressbook());
323 SysTryReturn(NID_SCL, pAddressbook !=null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
325 r = pAddressbook->Construct();
326 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
328 ret = contacts_db_get_record(_contacts_address_book._uri, addressbookId, &addressbookHandle);
329 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
330 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The addressbook %d is not found.", GetErrorMessage(E_OBJ_NOT_FOUND), addressbookId);
331 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred. Addressbook Id(%d)", GetErrorMessage(E_SYSTEM), addressbookId);
333 _AddressbookImpl::GetInstance(*pAddressbook)->SetId(addressbookId);
335 contacts_record_get_int(addressbookHandle, _contacts_address_book.account_id, &intValue);
336 _AddressbookImpl::GetInstance(*pAddressbook)->SetAccountId(intValue);
338 contacts_record_get_str_p(addressbookHandle, _contacts_address_book.name, &pCharValue);
339 _AddressbookImpl::GetInstance(*pAddressbook)->SetName(pCharValue);
341 contacts_record_destroy(addressbookHandle, true);
343 return pAddressbook.release();
347 _AddressbookManagerImpl::AddContact(Contact& contact, AddressbookId addressbookId)
349 if (_ContactImpl::GetInstance(contact)->IsRemoved())
351 result r = _ContactImpl::GetInstance(contact)->Invalidate();
352 SysTryReturn(NID_SCL, r == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
355 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));
356 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));
357 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
360 contacts_record_h recordHandle = null;
362 int ret = contacts_db_get_record(_contacts_address_book._uri, addressbookId, &recordHandle);
363 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));
364 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));
365 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
367 contacts_record_destroy(recordHandle, true);
369 recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
371 contacts_record_set_int(recordHandle, _contacts_contact.address_book_id, addressbookId);
373 ret = contacts_db_insert_record(recordHandle, &recordId);
374 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));
375 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is insufficient.", GetErrorMessage(E_STORAGE_FULL));
376 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
378 ret = contacts_db_get_record(_contacts_contact._uri, recordId, &recordHandle);
379 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));
380 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));
381 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A sytem error has been occurred.", GetErrorMessage(E_SYSTEM));
383 _ContactImpl::GetInstance(contact)->SetContactRecordHandle(recordHandle);
384 _RecordImpl::GetInstance(contact)->SetRecordId(recordId);
391 _AddressbookManagerImpl::AddCategory(Category& category, AddressbookId addressbookId)
393 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));
394 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
397 contacts_record_h recordHandle = null;
398 std::unique_ptr<IListT<int> > pList(null);
400 recordHandle = _CategoryImpl::GetInstance(category)->GetRecordHandle();
402 contacts_record_set_int(recordHandle, _contacts_group.address_book_id, addressbookId);
404 int ret = contacts_db_insert_record(recordHandle, &recordId);
405 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));
406 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is insufficient.", GetErrorMessage(E_STORAGE_FULL));
407 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
409 ret = contacts_db_get_record(_contacts_group._uri, recordId, &recordHandle);
410 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));
411 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));
412 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A sytem error has been occurred.", GetErrorMessage(E_SYSTEM));
414 _CategoryImpl::GetInstance(category)->SetRecordHandle(recordHandle);
415 _RecordImpl::GetInstance(category)->SetRecordId(recordId);
417 pList.reset(_CategoryImpl::GetInstance(category)->GetAddedMembersN());
418 if (pList != null && pList->GetCount() > 0)
420 std::unique_ptr<IEnumeratorT<int> > pEnum(pList->GetEnumeratorN());
422 while (pEnum->MoveNext() == E_SUCCESS)
425 pEnum->GetCurrent(tableId);
427 AddMemberToCategory(category.GetRecordId(), tableId);
430 _CategoryImpl::GetInstance(category)->ClearAddedMemberList();
438 _AddressbookManagerImpl::RemoveContact(RecordId contactId)
440 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));
441 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
443 contacts_record_h recordHandle = null;
446 int ret = contacts_db_get_record(_contacts_simple_contact._uri, contactId, &recordHandle);
447 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));
448 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));
449 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
451 contacts_record_get_int(recordHandle, _contacts_simple_contact.id, &intValue);
453 contacts_record_destroy(recordHandle, true);
454 SysTryReturn(NID_SCL, intValue == contactId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
456 ret = contacts_db_delete_record(_contacts_contact._uri, contactId);
457 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));
458 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));
459 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
465 _AddressbookManagerImpl::RemoveCategory(RecordId categoryId)
467 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);
468 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
471 int ret = CONTACTS_ERROR_NONE;
472 contacts_record_h recordHandle = null;
474 ret = contacts_db_get_record(_contacts_group._uri, categoryId, &recordHandle);
475 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));
476 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));
477 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
479 __ContactsRecordHandle categoryHandle(recordHandle);
481 contacts_record_get_int(recordHandle, _contacts_group.id, &intValue);
482 SysTryReturn(NID_SCL, intValue == categoryId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
484 bool isReadOnly = false;
485 contacts_record_get_bool(recordHandle, _contacts_group.is_read_only, &isReadOnly);
486 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));
488 ret = contacts_db_delete_record(_contacts_group._uri, categoryId);
489 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));
490 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));
491 // SysTryReturnResult(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OPERATION_FAILED, "Failed to delete a category.(%d)", ret); // temp
497 _AddressbookManagerImpl::UpdateContact(const Contact& contact)
499 RecordId contactId = contact.GetRecordId();
500 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));
501 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));
502 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
504 contacts_record_h recordHandle = null;
507 int ret = contacts_db_get_record(_contacts_simple_contact._uri, contactId, &recordHandle);
508 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));
509 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));
510 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
512 contacts_record_get_int(recordHandle, _contacts_simple_contact.id, &intValue);
514 contacts_record_destroy(recordHandle, true);
515 SysTryReturn(NID_SCL, intValue == contactId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
517 recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
519 ret = contacts_db_update_record(recordHandle);
520 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
521 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is insufficient.", GetErrorMessage(E_STORAGE_FULL));
522 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred. Failed to update a contact.", GetErrorMessage(E_SYSTEM));
524 ret = contacts_db_get_record(_contacts_contact._uri, contact.GetRecordId(), &recordHandle);
525 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));
526 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));
527 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
529 _ContactImpl::GetInstance(*const_cast<Contact*>(&contact))->SetContactRecordHandle(recordHandle);
535 _AddressbookManagerImpl::UpdateCategory(const Category& category)
537 RecordId categoryId = category.GetRecordId();
538 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));
539 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));
540 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
542 contacts_record_h recordHandle = null;
545 int ret = contacts_db_get_record(_contacts_group._uri, categoryId, &recordHandle);
546 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));
547 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The specified category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
549 contacts_record_get_int(recordHandle, _contacts_group.id, &intValue);
551 contacts_record_destroy(recordHandle, true);
552 SysTryReturn(NID_SCL, intValue == categoryId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
554 recordHandle = _CategoryImpl::GetInstance(category)->GetRecordHandle();
556 ret = contacts_db_update_record(recordHandle);
557 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));
558 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is insufficient.", GetErrorMessage(E_STORAGE_FULL));
559 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
561 ret = contacts_db_get_record(_contacts_group._uri, category.GetRecordId(), &recordHandle);
562 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The category is not found.");
563 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));
564 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
566 _CategoryImpl::GetInstance(*const_cast<Category*>(&category))->SetRecordHandle(recordHandle);
568 std::unique_ptr<IListT<int> > pList(_CategoryImpl::GetInstance(category)->GetAddedMembersN());
569 if (pList != null && pList->GetCount() > 0)
572 std::unique_ptr<IEnumeratorT<int> > pEnum(pList->GetEnumeratorN());
573 while (pEnum->MoveNext() == E_SUCCESS)
575 pEnum->GetCurrent(tableId);
577 AddMemberToCategory(category.GetRecordId(), tableId);
580 const_cast<_CategoryImpl*>(_CategoryImpl::GetInstance(category))->ClearAddedMemberList();
583 pList.reset(_CategoryImpl::GetInstance(category)->GetRemovedMembersN());
584 if (pList != null && pList->GetCount() > 0)
587 std::unique_ptr<IEnumeratorT<int> > pEnum(pList->GetEnumeratorN());
588 while (pEnum->MoveNext() == E_SUCCESS)
590 pEnum->GetCurrent(tableId);
592 RemoveMemberFromCategory(category.GetRecordId(), tableId);
595 const_cast<_CategoryImpl*>(_CategoryImpl::GetInstance(category))->ClearRemovedMemberList();
603 _AddressbookManagerImpl::AddMemberToCategory(RecordId categoryId, RecordId contactId)
606 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);
607 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);
608 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
610 int ret = contacts_group_add_contact(categoryId, contactId);
611 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
612 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));
613 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
619 _AddressbookManagerImpl::RemoveMemberFromCategory(RecordId categoryId, RecordId contactId)
622 SysTryReturn(NID_SCL, categoryId != INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. categoryId = %d.", GetErrorMessage(E_INVALID_ARG), categoryId);
623 SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. contactId = %d.", GetErrorMessage(E_INVALID_ARG), contactId);
624 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
626 int ret = contacts_group_remove_contact(categoryId, contactId);
627 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
628 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
629 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
635 _AddressbookManagerImpl::GetAllCategoriesN(void) const
637 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
641 __Query<__ContactsGroup> query;
643 query.SetSort(_contacts_group.name, true);
645 IList* pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsGroup, Category>(query);
646 SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
652 _AddressbookManagerImpl::GetCategoriesByContactN(RecordId contactId) const
654 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
655 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));
659 __Filter<__ContactsGroupRelation> filter;
661 filter.AddInt(_contacts_group_relation.contact_id, CONTACTS_MATCH_EQUAL, contactId);
663 __Query<__ContactsGroupRelation> query;
665 query.SetFilter(filter);
667 IList* pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsGroupRelation, Category>(query);
668 SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
674 _AddressbookManagerImpl::GetCategoriesByPersonN(PersonId personId) const
676 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
680 __Filter<__ContactsContactGroupRel> filter;
682 filter.AddInt(_contacts_contact_grouprel.person_id, CONTACTS_MATCH_EQUAL, personId);
683 filter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
684 filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_GREATER_THAN, 0);
686 unsigned int propertyIds[] =
688 _contacts_contact_grouprel.group_id,
691 __Query<__ContactsContactGroupRel> query;
693 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
694 query.SetFilter(filter);
695 query.SetSort(_contacts_contact_grouprel.group_name, true);
696 query.SetDistinct(true);
698 IList* pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Category>(query);
699 SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
705 _AddressbookManagerImpl::GetAllContactsN(void) const
707 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
711 __Query<__ContactsContact> query;
713 query.SetSort(_contacts_contact.display_name, true);
715 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
716 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
722 _AddressbookManagerImpl::GetContactsByCategoryN(RecordId categoryId) const
724 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));
725 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
729 __Filter<__ContactsContactGroupRel> filter;
732 if (categoryId != INVALID_RECORD_ID)
734 filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_EQUAL, categoryId);
738 filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_NONE, 0);
741 __Query<__ContactsContactGroupRel> query;
743 query.SetFilter(filter);
746 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Contact>(query);
747 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
753 _AddressbookManagerImpl::GetContactsByPersonN(PersonId personId) const
755 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
759 __Filter<__ContactsContact> filter;
761 filter.AddInt(_contacts_contact.person_id, CONTACTS_MATCH_EQUAL, personId);
763 __Query<__ContactsContact> query;
765 query.SetFilter(filter);
766 query.SetSort(_contacts_contact.display_name, true);
768 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
769 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
775 _AddressbookManagerImpl::SearchContactsByEmailN(const String& email) const
777 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));
778 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
782 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(email));
783 SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
785 __Filter<__ContactsContactEmail> filter;
787 filter.AddString(_contacts_contact_email.email, CONTACTS_MATCH_CONTAINS, pCharArray.get());
789 __Query<__ContactsContactEmail> query;
791 query.SetFilter(filter);
792 query.SetSort(_contacts_contact_email.display_name, true);
794 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactEmail, Contact>(query);
795 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
802 _AddressbookManagerImpl::SearchContactsByNameN(const String& name) const
804 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
808 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));
810 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(name));
811 SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
813 __Filter<__ContactsContact> filter;
815 filter.AddString(_contacts_contact.display_name, CONTACTS_MATCH_CONTAINS, pCharArray.get());
817 __Query<__ContactsContact> query;
819 query.SetFilter(filter);
820 query.SetSort(_contacts_contact.display_name, true);
822 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
823 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
829 _AddressbookManagerImpl::SearchContactsByPhoneNumberN(const String& phoneNumber) const
831 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
835 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));
837 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(phoneNumber));
838 SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
840 __Filter<__ContactsContactNumber> filter;
842 filter.AddString(_contacts_contact_number.number, CONTACTS_MATCH_CONTAINS, pCharArray.get());
844 __Query<__ContactsContactNumber> query;
846 query.SetFilter(filter);
847 query.SetDistinct(true);
848 query.SetSort(_contacts_contact_number.display_name, true);
850 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactNumber, Contact>(query);
851 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
857 _AddressbookManagerImpl::GetCategoryCount(void) const
859 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
865 count = _AddressbookUtil::GetCount<__ContactsGroup>();
866 SysTryReturn(NID_SCL, count >= 0, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
872 _AddressbookManagerImpl::GetContactCount(void) const
874 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
880 count = _AddressbookUtil::GetCount<__ContactsContact>();
881 SysTryReturn(NID_SCL, count >= 0, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
887 _AddressbookManagerImpl::GetContactN(RecordId contactId) const
889 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
890 SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. contactId = %d.", GetErrorMessage(E_INVALID_ARG), contactId);
895 contacts_record_h contactHandle = null;
897 std::unique_ptr<Contact> pContact(new (std::nothrow) Contact());
898 SysTryReturn(NID_SCL, pContact, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
900 int ret = contacts_db_get_record(_contacts_contact._uri, contactId, &contactHandle);
901 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
902 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
903 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
905 _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(contactHandle);
907 contacts_record_get_int(contactHandle, _contacts_contact.id, &intValue);
908 SysTryReturn(NID_SCL, intValue == contactId, null, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
910 _RecordImpl::GetInstance(*pContact)->SetRecordId(intValue);
912 return pContact.release();
916 _AddressbookManagerImpl::GetPersonN(PersonId personId) const
918 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
919 SysTryReturn(NID_SCL, personId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. personId = %d.", GetErrorMessage(E_INVALID_ARG), personId);
921 contacts_record_h recordHandle = null;
926 int ret = contacts_db_get_record(_contacts_person._uri, personId, &recordHandle);
927 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
928 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The person is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
929 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
931 __ContactsRecordHandle personRecord(recordHandle);
933 contacts_record_get_int(recordHandle, _contacts_person.id, &intValue);
934 SysTryReturn(NID_SCL, intValue == personId, null, E_OBJ_NOT_FOUND, "[%s] The person is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
936 Person* pPerson = __ContactsPerson::ConvertHandleTo<Person>(recordHandle);
937 SysTryReturn(NID_SCL, pPerson != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
943 _AddressbookManagerImpl::GetCategoryN(RecordId categoryId) const
945 SysTryReturn(NID_SCL, categoryId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. categoryId = %d.", categoryId);
946 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
948 contacts_record_h recordHandle = null;
952 int ret = contacts_db_get_record(_contacts_group._uri, categoryId, &recordHandle);
953 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
954 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
955 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
958 contacts_record_get_int(recordHandle, _contacts_group.id, &intValue);
960 std::unique_ptr<Category> pCategory(new (std::nothrow) Category());
961 SysTryReturn(NID_SCL, pCategory != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
963 SysTryReturn(NID_SCL, categoryId == intValue, null, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
965 __Filter<__ContactsGroupRelation> filter;
967 filter.AddInt(_contacts_group_relation.group_id, CONTACTS_MATCH_EQUAL, categoryId);
969 __Query<__ContactsGroupRelation> query;
971 query.SetFilter(filter);
973 int count = _AddressbookUtil::GetCountWithQuery(query);
974 SysTryReturn(NID_SCL, count >= 0, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
976 _CategoryImpl::GetInstance(*pCategory)->SetRecordHandle(recordHandle);
977 _CategoryImpl::GetInstance(*pCategory)->SetMemberCount(count);
978 _RecordImpl::GetInstance(*pCategory)->SetRecordId(categoryId);
980 return pCategory.release();
984 _AddressbookManagerImpl::GetLatestVersion(void) const
986 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
988 int latestVersion = -1;
992 int ret = contacts_db_get_current_version(&latestVersion);
993 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
995 return latestVersion;
999 _AddressbookManagerImpl::GetChangedContactsAfterN(int version, int& latestVersion) const
1001 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);
1002 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1006 IList* pChangedContacts = _AddressbookUtil::SearchWithVersionN<__ContactsContactUpdatedInfo, ContactChangeInfo>(-1, version, latestVersion);
1007 SysTryReturn(NID_SCL, pChangedContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1009 return pChangedContacts;
1013 _AddressbookManagerImpl::GetChangedCategoriesAfterN(int version, int& latestVersion) const
1015 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);
1016 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1020 int latestVersion1 = 0;
1021 int latestVersion2 = 0;
1023 std::unique_ptr<IList, AllElementsDeleter> pChangedGroups(_AddressbookUtil::SearchWithVersionN<__ContactsGroupUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion1));
1024 SysTryReturn(NID_SCL, pChangedGroups != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1026 std::unique_ptr<IList, AllElementsDeleter> pChangedRelations(_AddressbookUtil::SearchWithVersionN<__ContactsGroupRelUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion2));
1027 SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1029 std::unique_ptr<ArrayList, AllElementsDeleter> pChangeList(new (std::nothrow) Tizen::Base::Collection::ArrayList());
1030 SysTryReturn(NID_SCL, pChangeList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1032 result r = pChangeList->AddItems(*pChangedGroups);
1033 SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1035 r = pChangeList->AddItems(*pChangedRelations);
1036 SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1038 pChangedGroups->RemoveAll(false);
1039 pChangedRelations->RemoveAll(false);
1041 latestVersion = latestVersion2 > latestVersion1 ? latestVersion2 : latestVersion1;
1043 return pChangeList.release();
1048 _AddressbookManagerImpl::GetChangedGroupsAfterN(int version, int& latestVersion) const
1050 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);
1051 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1055 IList* pChangedRelations = _AddressbookUtil::SearchWithVersionN<__ContactsGroupUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion);
1056 SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1058 return pChangedRelations;
1062 _AddressbookManagerImpl::GetChangedRelationsAfterN(int version, int& latestVersion) const
1064 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);
1065 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1069 IList* pChangedRelations = _AddressbookUtil::SearchWithVersionN<__ContactsGroupRelUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion);
1070 SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1072 return pChangedRelations;
1076 _AddressbookManagerImpl::OnContactChanged(void)
1078 if (__pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
1083 IList* pChangedContactList = GetChangedContactsAfterN(__dbVersionForContact, __dbVersionForContact);
1084 SysTryReturnVoidResult(NID_SCL, pChangedContactList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1086 if (pChangedContactList->GetCount() > 0)
1088 if (__pIAddressbookChangeEventListener != null)
1090 __pIAddressbookChangeEventListener->OnContactsChanged(*pChangedContactList);
1094 __pIAddressbookEventListener->OnContactsChanged(*pChangedContactList);
1098 pChangedContactList->RemoveAll(true);
1099 delete pChangedContactList;
1103 _AddressbookManagerImpl::OnCategoryChanged(void)
1105 if (__pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
1110 IList* pChangedCategoryList = GetChangedGroupsAfterN(__dbVersionForGroup, __dbVersionForGroup);
1111 SysTryReturnVoidResult(NID_SCL, pChangedCategoryList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1113 if (pChangedCategoryList->GetCount() > 0)
1115 if (__pIAddressbookChangeEventListener != null)
1117 __pIAddressbookChangeEventListener->OnCategoriesChanged(*pChangedCategoryList);
1121 __pIAddressbookEventListener->OnCategoriesChanged(*pChangedCategoryList);
1125 pChangedCategoryList->RemoveAll(true);
1126 delete pChangedCategoryList;
1130 _AddressbookManagerImpl::OnRelationChanged(void)
1132 if (__pIAddressbookEventListener == null)
1137 IList* pChangedCategoryList = GetChangedRelationsAfterN(__dbVersionForRelation, __dbVersionForRelation);
1138 SysTryReturnVoidResult(NID_SCL, pChangedCategoryList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1140 if (pChangedCategoryList->GetCount() > 0)
1142 if (__pIAddressbookEventListener != null)
1144 __pIAddressbookEventListener->OnCategoriesChanged(*pChangedCategoryList);
1148 pChangedCategoryList->RemoveAll(true);
1149 delete pChangedCategoryList;
1153 _AddressbookManagerImpl::RemovePerson(PersonId personId)
1155 SysTryReturnResult(NID_SCL, personId > 0, E_INVALID_ARG, "[%s] Invalid argument is used. personId = %d.", GetErrorMessage(E_INVALID_ARG), personId);
1156 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1158 contacts_record_h recordHandle = null;
1160 int ret = contacts_db_get_record(_contacts_person._uri, personId, &recordHandle);
1161 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The person is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
1162 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));
1163 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));
1165 contacts_record_destroy(recordHandle, true);
1167 ret = contacts_db_delete_record(_contacts_person._uri, personId);
1168 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.");
1169 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));
1170 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));
1176 _AddressbookManagerImpl::GetAllPersonsN(void) const
1178 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1182 __Query<__ContactsPerson> query;
1184 query.SetSort(_contacts_person.display_name, true);
1186 IList* pPersons = _AddressbookUtil::SearchWithQueryN<__ContactsPerson, Person>(query);
1187 SysTryReturn(NID_SCL, pPersons != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1193 _AddressbookManagerImpl::GetPersonsByCategoryN(RecordId categoryId) const
1195 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));
1196 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1200 __Filter<__ContactsPersonGroupRel> filter;
1202 if (categoryId != INVALID_RECORD_ID)
1204 filter.AddInt(_contacts_person_grouprel.group_id, CONTACTS_MATCH_EQUAL, categoryId);
1208 filter.AddInt(_contacts_person_grouprel.group_id, CONTACTS_MATCH_NONE, 0);
1211 unsigned int propertyIds[] =
1213 _contacts_person_grouprel.person_id,
1214 _contacts_person_grouprel.display_name,
1215 _contacts_person_grouprel.image_thumbnail_path,
1216 _contacts_person_grouprel.ringtone_path,
1217 _contacts_person_grouprel.is_favorite,
1218 _contacts_person_grouprel.has_phonenumber,
1219 _contacts_person_grouprel.has_email,
1220 _contacts_person_grouprel.link_count,
1223 __Query<__ContactsPersonGroupRel> query;
1225 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1226 query.SetFilter(filter);
1227 query.SetSort(_contacts_person_grouprel.display_name, true);
1228 query.SetDistinct(true);
1230 IList* pPersons = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query);
1231 SysTryReturn(NID_SCL, pPersons != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1237 _AddressbookManagerImpl::GetFavoritePersonsN() const
1239 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1243 __Filter<__ContactsPerson> filter;
1245 filter.AddBool(_contacts_person.is_favorite, true);
1247 __Query<__ContactsPerson> query;
1249 query.SetFilter(filter);
1250 query.SetSort(_contacts_person.display_name, true);
1252 IList* pPersons = _AddressbookUtil::SearchWithQueryN<__ContactsPerson, Person>(query);
1253 SysTryReturn(NID_SCL, pPersons != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1259 _AddressbookManagerImpl::SearchPersonsN(const Tizen::Base::String& keyword) const
1261 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1262 SysTryReturn(NID_SCL, !keyword.IsEmpty(), null, E_INVALID_ARG, "Invalid argument is used. keyword is empty string.", GetErrorMessage(E_INVALID_ARG));
1266 contacts_record_h currentRecord = null;
1267 std::unique_ptr<Person> pPerson(null);
1269 std::unique_ptr<ArrayList, AllElementsDeleter> pPersonList(new (std::nothrow) ArrayList());
1270 SysTryReturn(NID_SCL, pPersonList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1272 result r = pPersonList->Construct();
1273 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1275 std::unique_ptr<__SearchResult<__ContactsPerson> > pSearchResult(_AddressbookUtil::Search<__ContactsPerson>(keyword));
1276 SysTryReturn(NID_SCL, pSearchResult != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1278 while (pSearchResult->MoveNext() == E_SUCCESS)
1280 currentRecord = pSearchResult->GetCurrentRecord();
1281 SysTryReturn(NID_SCL, currentRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1283 pPerson.reset(__ContactsPerson::ConvertHandleTo<Person>(currentRecord));
1284 SysTryReturn(NID_SCL, pPerson != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1286 r = pPersonList->Add(*pPerson);
1287 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
1292 return pPersonList.release();
1296 _AddressbookManagerImpl::SetPersonAsFavorite(PersonId personId, bool isFavorite)
1298 SysTryReturn(NID_SCL, personId > 0, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. personId = %d.", GetErrorMessage(E_INVALID_ARG), personId);
1299 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1301 bool boolValue = false;
1302 contacts_record_h personHandle = null;
1304 int ret = contacts_db_get_record(_contacts_person._uri, personId, &personHandle);
1305 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));
1306 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The person is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
1308 __ContactsRecordHandle recordHandle(personHandle);
1310 contacts_record_get_bool(personHandle, _contacts_person.is_favorite, &boolValue);
1312 if (boolValue != isFavorite)
1314 contacts_record_set_bool(personHandle, _contacts_person.is_favorite, isFavorite);
1316 ret = contacts_db_update_record(personHandle);
1317 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));
1318 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1325 _AddressbookManagerImpl::MergePersons(PersonId sourcePersonId, PersonId targetPersonId)
1327 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);
1328 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1330 int ret = contacts_person_link_person(targetPersonId, sourcePersonId);
1331 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1332 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1338 _AddressbookManagerImpl::UnlinkContact(PersonId personId, RecordId contactId, PersonId& newPersonId)
1340 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);
1341 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1343 int ret = contacts_person_unlink_contact(personId, contactId, &newPersonId);
1344 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));
1345 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1351 _AddressbookManagerImpl::SearchN(const AddressbookFilter& filter, unsigned long propertySortedBy, SortOrder sortOrder, int offset, int maxCount)
1353 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1357 IList* pList = null;
1358 bool ascending = false;
1359 unsigned int viewSortPropertyId = 0;
1361 AddressbookFilterType type = _AddressbookFilterImpl::GetInstance(filter)->GetType();
1362 contacts_filter_h filterHandle = _AddressbookFilterImpl::GetInstance(filter)->GetFilterHandle();
1364 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);
1366 if (propertySortedBy != 0 && sortOrder != SORT_ORDER_NONE)
1368 viewSortPropertyId = _AddressbookFilterImpl::GetViewPropertyId(type, propertySortedBy);
1369 ascending = (sortOrder == SORT_ORDER_ASCENDING) ? true : false;
1374 case AB_FI_TYPE_ADDRESSBOOK:
1376 __Filter<__ContactsAddressbook> filter;
1377 filter.Construct(filterHandle);
1379 __Query<__ContactsAddressbook> query;
1381 query.SetFilter(filter);
1383 if (viewSortPropertyId != 0)
1385 query.SetSort(viewSortPropertyId, ascending);
1388 pList = _AddressbookUtil::SearchWithQueryN<__ContactsAddressbook, Addressbook>(query, offset, maxCount);
1391 case AB_FI_TYPE_PERSON:
1393 __Filter<__ContactsPersonGroupRel> filter;
1394 filter.Construct(filterHandle);
1396 unsigned int propertyIds[] =
1397 { _contacts_person_grouprel.person_id,
1398 _contacts_person_grouprel.addressbook_ids,
1399 _contacts_person_grouprel.is_favorite,
1400 _contacts_person_grouprel.has_phonenumber,
1401 _contacts_person_grouprel.has_email,
1402 _contacts_person_grouprel.image_thumbnail_path,
1403 _contacts_person_grouprel.ringtone_path,
1404 _contacts_person_grouprel.display_name
1407 __Query<__ContactsPersonGroupRel> query;
1409 query.SetFilter(filter);
1410 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1411 query.SetDistinct(true);
1413 if (viewSortPropertyId != 0)
1415 query.SetSort(viewSortPropertyId, ascending);
1418 pList = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query, offset, maxCount);
1421 case AB_FI_TYPE_CONTACT:
1423 __Filter<__ContactsContact> filter;
1424 filter.Construct(filterHandle);
1426 __Query<__ContactsContact> query;
1428 query.SetFilter(filter);
1430 if (viewSortPropertyId != 0)
1432 query.SetSort(viewSortPropertyId, ascending);
1435 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query, offset, maxCount);
1438 case AB_FI_TYPE_CATEGORY:
1440 __Filter<__ContactsGroup> filter;
1441 filter.Construct(filterHandle);
1443 __Query<__ContactsGroup> query;
1445 query.SetFilter(filter);
1447 if (viewSortPropertyId != 0)
1449 query.SetSort(viewSortPropertyId, ascending);
1452 pList = _AddressbookUtil::SearchWithQueryN<__ContactsGroup, Category>(query, offset, maxCount);
1455 case AB_FI_TYPE_PHONE_CONTACT:
1457 __Filter<__ContactsContactNumber> filter;
1458 filter.Construct(filterHandle);
1460 __Query<__ContactsContactNumber> query;
1462 query.SetFilter(filter);
1464 if (viewSortPropertyId != 0)
1466 query.SetSort(viewSortPropertyId, ascending);
1469 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContactNumber, PhoneNumberContact>(query, offset, maxCount);
1472 case AB_FI_TYPE_EMAIL_CONTACT:
1474 __Filter<__ContactsContactEmail> filter;
1475 filter.Construct(filterHandle);
1477 __Query<__ContactsContactEmail> query;
1479 query.SetFilter(filter);
1481 if (viewSortPropertyId != 0)
1483 query.SetSort(viewSortPropertyId, ascending);
1486 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContactEmail, EmailContact>(query, offset, maxCount);
1490 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. The filter type is invalid", GetErrorMessage(E_INVALID_ARG));
1498 _AddressbookManagerImpl::GetMatchedItemCount(const AddressbookFilter& filter)
1500 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1505 AddressbookFilterType type = _AddressbookFilterImpl::GetInstance(filter)->GetType();
1506 contacts_filter_h filterHandle = _AddressbookFilterImpl::GetInstance(filter)->GetFilterHandle();
1510 case AB_FI_TYPE_ADDRESSBOOK:
1511 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsAddressbook>(filterHandle);
1513 case AB_FI_TYPE_PERSON:
1515 __Filter<__ContactsPersonGroupRel> filter;
1516 filter.Construct(filterHandle);
1518 unsigned int propertyIds[] = { _contacts_person_grouprel.person_id };
1520 __Query<__ContactsPersonGroupRel> query;
1522 query.SetFilter(filter);
1523 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1524 query.SetDistinct(true);
1526 count = _AddressbookUtil::GetCountWithQuery(query);
1530 case AB_FI_TYPE_CONTACT:
1531 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContact>(filterHandle);
1533 case AB_FI_TYPE_CATEGORY:
1534 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsGroup>(filterHandle);
1536 case AB_FI_TYPE_PHONE_CONTACT:
1537 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContactNumber>(filterHandle);
1539 case AB_FI_TYPE_EMAIL_CONTACT:
1540 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContactEmail>(filterHandle);
1543 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the filter is invalid", GetErrorMessage(GetLastResult()));
1551 _AddressbookManagerImpl::OnEachContact(contacts_record_h recordHandle, void* pUserData)
1553 IList* pList = static_cast<IList*>(pUserData);
1557 std::unique_ptr<Contact> pContact(new (std::nothrow) Contact());
1558 SysTryReturn(NID_SCL, pContact != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1560 contacts_record_h newRecordHandle = null;
1562 contacts_record_clone(recordHandle, &newRecordHandle);
1563 SysTryReturn(NID_SCL, newRecordHandle != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1565 _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(newRecordHandle);
1567 result r = pList->Add(*pContact);
1568 SysTryReturn(NID_SCL, !IsFailed(r), false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1576 _AddressbookManagerImpl::ParseContactsFromVcardN(const Tizen::Base::String& vcardPath)
1578 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1583 result r = file.Construct(vcardPath, "r");
1584 SysTryReturn(NID_SCL, r != E_INVALID_ARG, null, E_INVALID_ARG, "[%s] Invalid argument is used..", GetErrorMessage(E_INVALID_ARG));
1585 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));
1586 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));
1587 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1589 std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
1591 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(vcardPath));
1593 int ret = contacts_vcard_parse_to_contact_foreach(pCharArray.get(), OnEachContact, pList.get());
1594 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1595 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1596 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1598 return pList.release();
1602 _AddressbookManagerImpl::ExportPersonToVcard(const Person& person, const Tizen::Base::String& vcardPath)
1604 bool exist = File::IsFileExist(vcardPath);
1605 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
1606 SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
1607 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1610 result r = file.Construct(vcardPath, "w");
1611 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));
1612 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1613 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1615 contacts_record_h personRecordHandle = null;
1617 int ret = contacts_db_get_record(_contacts_person._uri, person.GetId(), &personRecordHandle);
1618 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The specified person does not exist.", GetErrorMessage(E_OBJ_NOT_FOUND));
1619 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));
1620 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1622 __ContactsRecordHandle recordHandle(personRecordHandle);
1624 char* pVcardStream = null;
1625 ret = contacts_vcard_make_from_person(personRecordHandle, &pVcardStream);
1626 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));
1627 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1629 r = file.Write(pVcardStream, strlen(pVcardStream));
1631 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1632 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1638 _AddressbookManagerImpl::ExportPersonsToVcard(const Tizen::Base::Collection::IList& personList, const Tizen::Base::String& vcardPath)
1640 bool exist = File::IsFileExist(vcardPath);
1641 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
1642 SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
1643 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1645 int ret = CONTACTS_ERROR_NONE;
1646 char* pVcardStream = null;
1649 result r = file.Construct(vcardPath, "w");
1650 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));
1651 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1652 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1654 contacts_record_h personRecordHandle = null;
1655 __ContactsRecordHandle recordHandle(null);
1657 std::unique_ptr<IEnumerator> pEnum(personList.GetEnumeratorN());
1658 SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1660 while (pEnum->MoveNext() == E_SUCCESS)
1662 Person* pPerson = static_cast<Person*>(pEnum->GetCurrent());
1664 ret = contacts_db_get_record(_contacts_person._uri, pPerson->GetId(), &personRecordHandle);
1665 SysTryReturnResult(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, "The specified person does not exist.");
1666 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));
1667 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1669 recordHandle.Reset(personRecordHandle);
1671 ret = contacts_vcard_make_from_person(personRecordHandle, &pVcardStream);
1672 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));
1673 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1675 r = file.Write(pVcardStream, strlen(pVcardStream));
1677 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1678 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1685 _AddressbookManagerImpl::ExportContactToVcard(const Contact& contact, const Tizen::Base::String& vcardPath)
1687 bool exist = File::IsFileExist(vcardPath);
1688 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
1689 SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
1690 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1693 result r = file.Construct(vcardPath, "w");
1694 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));
1695 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1696 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1698 contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
1700 char* pVcardStream = null;
1702 int ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
1703 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));
1704 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1706 r = file.Write(pVcardStream, strlen(pVcardStream));
1708 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1709 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1715 _AddressbookManagerImpl::ExportContactsToVcard(const Tizen::Base::Collection::IList& contactList, const Tizen::Base::String& vcardPath)
1717 bool exist = File::IsFileExist(vcardPath);
1718 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
1719 SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
1720 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1722 int ret = CONTACTS_ERROR_NONE;
1723 char* pVcardStream = null;
1725 Contact* pContact = null;
1727 result r = file.Construct(vcardPath, "w");
1728 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));
1729 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1730 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1732 contacts_record_h recordHandle = null;
1734 std::unique_ptr<IEnumerator> pEnum(contactList.GetEnumeratorN());
1735 SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1737 while (pEnum->MoveNext() == E_SUCCESS)
1739 pContact = static_cast<Contact*>(pEnum->GetCurrent());
1741 recordHandle = _ContactImpl::GetInstance(*pContact)->GetContactRecordHandle();
1743 ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
1744 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));
1745 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1747 r = file.Write(pVcardStream, strlen(pVcardStream));
1749 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1750 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1757 _AddressbookManagerImpl::ExportContactToVcardStreamN(const Contact& contact)
1759 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1763 contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
1765 char* pVcardStream = null;
1766 int ret = CONTACTS_ERROR_NONE;
1768 ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
1769 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1770 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1771 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1773 std::unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
1774 if (pByteBuffer == null)
1777 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1782 result r = pByteBuffer->Construct(strlen(pVcardStream));
1786 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1791 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
1793 SysTryReturn(NID_SCL, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1794 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1796 return pByteBuffer.release();
1800 _AddressbookManagerImpl::ExportContactsToVcardStreamN(const Tizen::Base::Collection::IList& contactList)
1802 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1806 char* pVcardStream = null;
1807 int ret = CONTACTS_ERROR_NONE;
1808 Contact* pContact = null;
1809 result r = E_SUCCESS;
1812 contacts_record_h recordHandle = null;
1814 std::unique_ptr<IEnumerator> pEnum(contactList.GetEnumeratorN());
1815 SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1817 std::unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
1818 SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1820 r = pByteBuffer->Construct(capacity);
1821 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1823 if (contactList.GetCount() == 0)
1825 return pByteBuffer.release();
1828 while (pEnum->MoveNext() == E_SUCCESS)
1830 pContact = static_cast<Contact*>(pEnum->GetCurrent());
1832 recordHandle = _ContactImpl::GetInstance(*pContact)->GetContactRecordHandle();
1834 ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
1835 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1836 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1837 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1839 capacity += strlen(pVcardStream);
1840 r = pByteBuffer->ExpandCapacity(capacity);
1844 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1849 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
1851 pVcardStream = null;
1852 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.: capacity(%d), size(%d)", GetErrorMessage(E_SYSTEM));
1855 return pByteBuffer.release();
1859 _AddressbookManagerImpl::ExportPersonToVcardStreamN(const Person& person)
1861 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1865 int ret = CONTACTS_ERROR_NONE;
1867 contacts_record_h personRecordHandle = null;
1869 ret =contacts_db_get_record(_contacts_person._uri, person.GetId(), &personRecordHandle);
1870 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1871 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1872 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1874 __ContactsRecordHandle recordHandle(personRecordHandle);
1876 char* pVcardStream = null;
1878 ret = contacts_vcard_make_from_person(personRecordHandle, &pVcardStream);
1879 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1880 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1881 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1883 std::unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
1884 if (pByteBuffer == null)
1887 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1892 result r = pByteBuffer->Construct(strlen(pVcardStream));
1896 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1901 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
1903 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1905 return pByteBuffer.release();
1909 _AddressbookManagerImpl::ExportPersonsToVcardStreamN(const Tizen::Base::Collection::IList& personList)
1911 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1915 int ret = CONTACTS_ERROR_NONE;
1916 Person* pPerson = null;
1917 char* pVcardStream = null;
1921 contacts_record_h personRecordHandle = null;
1922 __ContactsRecordHandle recordHandle(null);
1924 std::unique_ptr<IEnumerator> pEnum(personList.GetEnumeratorN());
1925 SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1927 std::unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
1928 SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1930 result r = pByteBuffer->Construct(capacity);
1931 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1933 if (personList.GetCount() == 0)
1935 return pByteBuffer.release();
1938 while (pEnum->MoveNext() == E_SUCCESS)
1940 pPerson = static_cast<Person*>(pEnum->GetCurrent());
1942 ret = contacts_db_get_record(_contacts_person._uri, pPerson->GetId(), &personRecordHandle);
1943 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1944 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1945 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1947 recordHandle.Reset(personRecordHandle);
1949 ret = contacts_vcard_make_from_person(personRecordHandle, &pVcardStream);
1950 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1951 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1952 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1954 capacity += strlen(pVcardStream);
1955 r = pByteBuffer->ExpandCapacity(capacity);
1959 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1964 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
1966 pVcardStream = null;
1967 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1970 return pByteBuffer.release();
1974 _AddressbookManagerImpl::ParseVcardStreamN(const Tizen::Base::ByteBuffer& vcardStream)
1976 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1980 contacts_list_h listHandle = null;
1981 result r = E_SUCCESS;
1983 int ret = contacts_vcard_parse_to_contacts(reinterpret_cast<char*>(const_cast<byte*>(vcardStream.GetPointer())), &listHandle);
1984 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1985 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1987 unsigned int count = 0;
1988 contacts_record_h recordHandle = null;
1989 contacts_list_get_count(listHandle, &count);
1991 std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
1994 contacts_list_destroy(listHandle, true);
1995 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2000 r = pList->Construct(count);
2003 contacts_list_destroy(listHandle, true);
2004 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2009 for (unsigned int i = 0; i < count; i++)
2011 std::unique_ptr<Contact> pContact(new (std::nothrow) Contact());
2012 if (pContact == null)
2014 contacts_list_destroy(listHandle, true);
2015 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2020 r = pList->Add(pContact.get());
2023 contacts_list_destroy(listHandle, true);
2024 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2032 std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
2034 while (pEnum->MoveNext() == E_SUCCESS)
2036 Contact* pContact = static_cast <Contact*> (pEnum->GetCurrent());
2038 contacts_list_get_current_record_p(listHandle, &recordHandle);
2039 _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(recordHandle);
2041 ret = contacts_list_next(listHandle);
2044 contacts_list_destroy(listHandle, false);
2046 return pList.release();
2050 _AddressbookManagerImpl::ExportUserProfileToVcardStreamN(const UserProfile& userProfile)
2052 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2056 char* pVcardStream = null;
2057 int ret = CONTACTS_ERROR_NONE;
2058 contacts_record_h recordHandle = null;
2060 recordHandle = _UserProfileImpl::GetInstance(userProfile)->GetUserProfileHandle();
2062 ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2063 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2064 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2065 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2067 std::unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2068 if (pByteBuffer == null)
2071 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2075 SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2077 result r = pByteBuffer->Construct(strlen(pVcardStream));
2081 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2086 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2088 SysTryReturn(NID_SCL, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2089 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2091 return pByteBuffer.release();
2095 _AddressbookManagerImpl::ExportUserProfilesToVcardStreamN(const Tizen::Base::Collection::IList& userProfileList)
2097 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2101 char* pVcardStream = null;
2102 int ret = CONTACTS_ERROR_NONE;
2103 UserProfile* pProfile = null;
2104 result r = E_SUCCESS;
2107 std::unique_ptr<IEnumerator> pEnum(userProfileList.GetEnumeratorN());
2108 SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2110 std::unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2111 SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2113 r = pByteBuffer->Construct(capacity);
2114 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2116 if (userProfileList.GetCount() == 0)
2118 return pByteBuffer.release();
2121 while (pEnum->MoveNext() == E_SUCCESS)
2123 contacts_record_h recordHandle = null;
2125 pProfile = static_cast<UserProfile*>(pEnum->GetCurrent());
2127 recordHandle = _UserProfileImpl::GetInstance(*pProfile)->GetUserProfileHandle();
2129 ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2130 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2131 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2132 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2134 capacity += strlen(pVcardStream);
2135 r = pByteBuffer->ExpandCapacity(capacity);
2139 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2144 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pVcardStream), 0, strlen(pVcardStream));
2146 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.: capacity(%d), size(%d)", GetErrorMessage(E_SYSTEM));
2149 return pByteBuffer.release();
2153 _AddressbookManagerImpl::ExportUserProfileToVcard(const UserProfile& userProfile, const Tizen::Base::String& vcardPath)
2155 bool exist = File::IsFileExist(vcardPath);
2156 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2157 SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2158 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2161 result r = file.Construct(vcardPath, "w");
2162 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));
2163 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2164 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2166 char* pVcardStream = null;
2167 contacts_record_h recordHandle = null;
2169 recordHandle = _UserProfileImpl::GetInstance(userProfile)->GetUserProfileHandle();
2171 int ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2172 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));
2173 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2175 r = file.Write(pVcardStream, strlen(pVcardStream));
2177 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2178 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2184 _AddressbookManagerImpl::ExportUserProfilesToVcard(const Tizen::Base::Collection::IList& userProfileList, const Tizen::Base::String& vcardPath)
2186 bool exist = File::IsFileExist(vcardPath);
2187 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2188 SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2189 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2191 int ret = CONTACTS_ERROR_NONE;
2192 char* pVcardStream = null;
2194 UserProfile* pProfile = null;
2196 result r = file.Construct(vcardPath, "w");
2197 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));
2198 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2199 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2201 std::unique_ptr<IEnumerator> pEnum(userProfileList.GetEnumeratorN());
2202 SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2204 while (pEnum->MoveNext() == E_SUCCESS)
2206 contacts_record_h recordHandle = null;
2208 pProfile = static_cast<UserProfile*>(pEnum->GetCurrent());
2210 recordHandle = _UserProfileImpl::GetInstance(*pProfile)->GetUserProfileHandle();
2212 ret = contacts_vcard_make_from_my_profile(recordHandle, &pVcardStream);
2213 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));
2214 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2216 r = file.Write(pVcardStream, strlen(pVcardStream));
2218 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2219 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2226 _AddressbookManagerImpl::GetAllUserProfilesN(void) const
2228 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2232 __Query<__ContactsUserProfile> query;
2234 query.SetSort(_contacts_my_profile.display_name, true);
2236 IList* pUserProfilesList = _AddressbookUtil::SearchWithQueryN<__ContactsUserProfile, UserProfile>(query);
2237 SysTryReturn(NID_SCL, pUserProfilesList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2239 return pUserProfilesList;
2243 _AddressbookManagerImpl::GetUserProfileN(AddressbookId addressbookId) const
2245 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2246 SysTryReturn(NID_SCL, addressbookId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. Addressbook Id(%d).", GetErrorMessage(E_INVALID_ARG), addressbookId);
2250 contacts_record_h addressbookHandle = null;
2251 int ret = contacts_db_get_record(_contacts_address_book._uri, addressbookId, &addressbookHandle);
2252 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2253 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The addressbook %d is not found.", GetErrorMessage(E_OBJ_NOT_FOUND), addressbookId);
2254 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred. Addressbook Id(%d)", GetErrorMessage(E_SYSTEM), addressbookId);
2256 contacts_record_destroy(addressbookHandle, true);
2258 __Filter<__ContactsUserProfile> filter;
2260 filter.AddInt(_contacts_my_profile.address_book_id, CONTACTS_MATCH_EQUAL, addressbookId);
2262 __Query<__ContactsUserProfile> query;
2264 query.SetFilter(filter);
2266 std::unique_ptr<IList, AllElementsDeleter> pUserProfilesList(_AddressbookUtil::SearchWithQueryN<__ContactsUserProfile, UserProfile>(query));
2267 SysTryReturn(NID_SCL, pUserProfilesList.get() != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2268 SysTryReturn(NID_SCL, pUserProfilesList->GetCount() != 0, null, E_SUCCESS, "No UserProfile Set for this Addressbook.");
2269 SysTryReturn(NID_SCL, pUserProfilesList->GetCount() == 1, null, E_SYSTEM, "[%s] Propagating. More than one UserProfile not allowed.", GetErrorMessage(E_SYSTEM));
2271 UserProfile* pProfile = new (std::nothrow) UserProfile(*(static_cast<UserProfile*>(pUserProfilesList->GetAt(0))));
2272 SysTryReturn(NID_SCL, pProfile != null, null, E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(E_OUT_OF_MEMORY));
2277 _AddressbookManagerImpl*
2278 _AddressbookManagerImpl::GetInstance(AddressbookManager& addressbookManager)
2280 return addressbookManager.__pAddressbookManagerImpl;
2283 const _AddressbookManagerImpl*
2284 _AddressbookManagerImpl::GetInstance(const AddressbookManager& addressbookManager)
2286 return addressbookManager.__pAddressbookManagerImpl;