2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 * @file FScl_AddressbookManagerImpl.cpp
19 * @brief This is the implementation for _AddressbookManagerImpl class.
21 * This file contains definitions of @e _AddressbookManagerImpl class.
24 #include <unique_ptr.h>
25 #include <FBaseColIListT.h>
26 #include <FBaseColArrayListT.h>
27 #include <FBaseColHashMapT.h>
28 #include <FBaseResult.h>
29 #include <FBaseLongLong.h>
30 #include <FBaseInteger.h>
32 #include <FSclAddressbook.h>
33 #include <FSclAddressbookManager.h>
34 #include <FSclIAddressbookChangeEventListener.h>
35 #include <FSclContact.h>
36 #include <FSclCategory.h>
37 #include <FSclUserProfile.h>
38 #include <FSclContactChangeInfo.h>
39 #include <FSclCategoryChangeInfo.h>
40 #include <FSclPerson.h>
41 #include <FSclPhoneNumberContact.h>
42 #include <FScl_PhoneNumberContactImpl.h>
43 #include <FSclEmailContact.h>
44 #include <FScl_EmailContactImpl.h>
45 #include <FSclAddressbookFilter.h>
46 #include <FSclIAddressbookEventListener.h>
47 #include <FSclIRecordEventListener.h>
48 #include <FBaseSysLog.h>
49 #include <FApp_AppInfo.h>
50 #include <FBase_StringConverter.h>
51 #include "FScl_AddressbookImpl.h"
52 #include "FScl_AddressbookFilterImpl.h"
53 #include "FScl_AddressbookManagerImpl.h"
54 #include "FScl_AddressbookUtil.h"
55 #include "FScl_RecordImpl.h"
56 #include "FScl_ContactImpl.h"
57 #include "FScl_CategoryImpl.h"
58 #include "FScl_PersonImpl.h"
59 #include "FScl_AddressbookUtil.h"
60 #include "FScl_ContactChangeInfoImpl.h"
61 #include "FScl_CategoryChangeInfoImpl.h"
62 #include "FScl_ContactDbMonitor.h"
63 #include "FScl_ContactDbConnector.h"
64 #include "FScl_UserProfileImpl.h"
66 using namespace Tizen::App;
67 using namespace Tizen::Base;
68 using namespace Tizen::Base::Collection;
69 using namespace Tizen::Io;
70 using namespace Tizen::Graphics;
72 namespace Tizen { namespace Social
75 _AddressbookManagerImpl::_AddressbookManagerImpl(void)
76 : __pIAddressbookEventListener(null)
77 , __pIAddressbookChangeEventListener(null)
78 , __dbVersionForContact(0)
79 , __dbVersionForGroup(0)
80 , __dbVersionForRelation(0)
85 _AddressbookManagerImpl::~_AddressbookManagerImpl(void)
87 if (__pIAddressbookEventListener != null || __pIAddressbookChangeEventListener != null)
89 _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
90 if (pContactDbMonitor != null)
92 pContactDbMonitor->RemoveListener(*this);
98 _AddressbookManagerImpl::Construct(void)
100 result r = _ContactDbConnector::EnsureDbConnection();
101 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
107 _AddressbookManagerImpl::SetEventListener(IAddressbookEventListener* pListener)
109 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
111 result r = E_SUCCESS;
113 if (pListener != null)
115 if (__pIAddressbookEventListener == null && __pIAddressbookChangeEventListener == null)
117 _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
118 SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
120 r = pContactDbMonitor->AddListener(*this);
121 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
124 __dbVersionForContact = GetLatestVersion();
125 SysTryReturn(NID_SCL, __dbVersionForContact != -1, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
127 __dbVersionForGroup = __dbVersionForContact;
128 __dbVersionForRelation = __dbVersionForContact;
130 __pIAddressbookChangeEventListener = null;
131 __pIAddressbookEventListener = pListener;
135 if (__pIAddressbookEventListener != null || __pIAddressbookChangeEventListener != null)
138 _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
139 SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
141 pContactDbMonitor->RemoveListener(*this);
144 __pIAddressbookEventListener = null;
145 __pIAddressbookChangeEventListener = null;
152 _AddressbookManagerImpl::SetAddressbookChangeEventListener(IAddressbookChangeEventListener* pListener)
154 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
156 result r = E_SUCCESS;
158 if (pListener != null)
160 if (__pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
162 _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
163 SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
165 r = pContactDbMonitor->AddListener(*this);
166 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
169 __dbVersionForContact = GetLatestVersion();
170 SysTryReturn(NID_SCL, __dbVersionForContact != -1, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
172 __dbVersionForGroup = __dbVersionForContact;
173 __dbVersionForRelation = __dbVersionForContact;
175 __pIAddressbookEventListener = null;
176 __pIAddressbookChangeEventListener = pListener;
180 if (__pIAddressbookChangeEventListener != null || __pIAddressbookEventListener != null)
183 _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
184 SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
186 pContactDbMonitor->RemoveListener(*this);
189 __pIAddressbookChangeEventListener = null;
190 __pIAddressbookEventListener = null;
197 _AddressbookManagerImpl::CreateAddressbookN(AccountId accountId, const String& name)
199 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
200 SysTryReturn(NID_SCL, accountId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. account id is invalid.", GetErrorMessage(E_INVALID_ARG));
201 SysTryReturn(NID_SCL, !name.IsEmpty(), null, E_INVALID_ARG, "[%s] Invalid argument is used. The name is empty.", GetErrorMessage(E_INVALID_ARG));
203 result r = E_SUCCESS;
204 int ret = CONTACTS_ERROR_NONE;
206 contacts_record_h addressbookHandle = null;
208 std::unique_ptr<char[]> pNameString(_StringConverter::CopyToCharArrayN(name));
209 SysTryReturn(NID_SCL, pNameString !=null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
211 __Filter<__ContactsAddressbook> filter;
213 filter.AddString(_contacts_address_book.name, CONTACTS_MATCH_EXACTLY, pNameString.get());
215 __Query<__ContactsAddressbook> query;
217 query.SetFilter(filter);
219 int count = _AddressbookUtil::GetCountWithQuery(query);
220 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));
222 std::unique_ptr<Addressbook> pAddressbook(new (std::nothrow) Addressbook());
223 SysTryReturn(NID_SCL, pAddressbook != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
225 r = pAddressbook->Construct();
226 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
228 ret = contacts_record_create(_contacts_address_book._uri, &addressbookHandle);
229 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
230 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
232 __ContactsRecordHandle recordHandle(addressbookHandle);
234 contacts_record_set_str(addressbookHandle, _contacts_address_book.name, pNameString.get());
235 contacts_record_set_int(addressbookHandle, _contacts_address_book.account_id, accountId);
237 ret = contacts_db_insert_record(addressbookHandle, &recordId);
238 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
239 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
240 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
242 _AddressbookImpl::GetInstance(*pAddressbook)->SetAccountId(accountId);
243 _AddressbookImpl::GetInstance(*pAddressbook)->SetName(name);
244 _AddressbookImpl::GetInstance(*pAddressbook)->SetId(recordId);
246 return pAddressbook.release();
250 _AddressbookManagerImpl::DeleteAddressbook(AddressbookId addressbookId)
252 SysTryReturn(NID_SCL, addressbookId > 0, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The addressbook ID is invalid or the default addressbook ID.", GetErrorMessage(E_INVALID_ARG));
253 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
255 int ret = contacts_db_delete_record(_contacts_address_book._uri, addressbookId);
256 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
257 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The addressbook is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
258 // SysTryReturnResult(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OPERATION_FAILED, "Failed to delete an addressbook.(%d)", ret); // temp
264 _AddressbookManagerImpl::GetAddressbooksByAccountN(AccountId accountId) const
266 SysTryReturn(NID_SCL, accountId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. accountId = %d.", GetErrorMessage(E_INVALID_ARG), accountId);
267 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
269 __Filter<__ContactsAddressbook> filter;
271 filter.AddInt(_contacts_address_book.account_id, CONTACTS_MATCH_EQUAL, accountId);
273 __Query<__ContactsAddressbook> query;
275 query.SetFilter(filter);
277 IList* pAddressbooks = _AddressbookUtil::SearchWithQueryN<__ContactsAddressbook, Addressbook>(query);
278 SysTryReturn(NID_SCL, pAddressbooks != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
280 return pAddressbooks;
284 _AddressbookManagerImpl::GetAllAddressbooksN(void) const
286 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
288 __Query<__ContactsAddressbook> query;
290 query.SetSort(_contacts_address_book.name, true);
292 IList* pAddressbooks = _AddressbookUtil::SearchWithQueryN<__ContactsAddressbook, Addressbook>(query);
293 SysTryReturn(NID_SCL, pAddressbooks != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
295 return pAddressbooks;
299 _AddressbookManagerImpl::GetAddressbookN(AddressbookId addressbookId) const
301 SysTryReturn(NID_SCL, addressbookId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. addressbookId.", GetErrorMessage(E_INVALID_ARG), addressbookId);
302 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
304 result r = E_SUCCESS;
305 int ret = CONTACTS_ERROR_NONE;
306 contacts_record_h addressbookHandle = null;
308 char* pCharValue = null;
310 std::unique_ptr<Addressbook> pAddressbook(new (std::nothrow) Addressbook());
311 SysTryReturn(NID_SCL, pAddressbook !=null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
313 r = pAddressbook->Construct();
314 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
317 ret = contacts_db_get_record(_contacts_address_book._uri, addressbookId, &addressbookHandle);
318 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
319 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);
320 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred. Addressbook Id(%d)", GetErrorMessage(E_SYSTEM), addressbookId);
322 _AddressbookImpl::GetInstance(*pAddressbook)->SetId(addressbookId);
324 contacts_record_get_int(addressbookHandle, _contacts_address_book.account_id, &intValue);
325 _AddressbookImpl::GetInstance(*pAddressbook)->SetAccountId(intValue);
327 contacts_record_get_str_p(addressbookHandle, _contacts_address_book.name, &pCharValue);
328 _AddressbookImpl::GetInstance(*pAddressbook)->SetName(pCharValue);
330 contacts_record_destroy(addressbookHandle, true);
332 return pAddressbook.release();
336 _AddressbookManagerImpl::AddContact(Contact& contact, AddressbookId addressbookId)
338 if (_ContactImpl::GetInstance(contact)->IsRemoved())
340 result r = _ContactImpl::GetInstance(contact)->Invalidate();
341 SysTryReturn(NID_SCL, r == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
344 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));
345 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));
346 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
350 contacts_record_h recordHandle = null;
352 int ret = contacts_db_get_record(_contacts_address_book._uri, addressbookId, &recordHandle);
353 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));
354 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));
355 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
357 contacts_record_destroy(recordHandle, true);
359 recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
361 contacts_record_set_int(recordHandle, _contacts_contact.address_book_id, addressbookId);
363 ret = contacts_db_insert_record(recordHandle, &recordId);
364 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));
365 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is insufficient.", GetErrorMessage(E_STORAGE_FULL));
366 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
368 ret = contacts_db_get_record(_contacts_contact._uri, recordId, &recordHandle);
369 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));
370 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));
371 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A sytem error has been occurred.", GetErrorMessage(E_SYSTEM));
373 _ContactImpl::GetInstance(contact)->SetContactRecordHandle(recordHandle);
374 _RecordImpl::GetInstance(contact)->SetRecordId(recordId);
381 _AddressbookManagerImpl::AddCategory(Category& category, AddressbookId addressbookId)
383 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));
384 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
387 contacts_record_h recordHandle = null;
388 std::unique_ptr<IListT<int> > pList(null);
390 recordHandle = _CategoryImpl::GetInstance(category)->GetRecordHandle();
392 contacts_record_set_int(recordHandle, _contacts_group.address_book_id, addressbookId);
394 int ret = contacts_db_insert_record(recordHandle, &recordId);
395 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));
396 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is insufficient.", GetErrorMessage(E_STORAGE_FULL));
397 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
399 ret = contacts_db_get_record(_contacts_group._uri, recordId, &recordHandle);
400 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));
401 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));
402 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A sytem error has been occurred.", GetErrorMessage(E_SYSTEM));
404 _CategoryImpl::GetInstance(category)->SetRecordHandle(recordHandle);
405 _RecordImpl::GetInstance(category)->SetRecordId(recordId);
407 pList.reset(_CategoryImpl::GetInstance(category)->GetAddedMembersN());
408 if (pList != null && pList->GetCount() > 0)
410 std::unique_ptr<IEnumeratorT<int> > pEnum(pList->GetEnumeratorN());
412 while (pEnum->MoveNext() == E_SUCCESS)
415 pEnum->GetCurrent(tableId);
417 AddMemberToCategory(category.GetRecordId(), tableId);
420 _CategoryImpl::GetInstance(category)->ClearAddedMemberList();
428 _AddressbookManagerImpl::RemoveContact(RecordId contactId)
430 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));
431 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
433 contacts_record_h recordHandle = null;
436 int ret = contacts_db_get_record(_contacts_simple_contact._uri, contactId, &recordHandle);
437 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));
438 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));
439 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
441 contacts_record_get_int(recordHandle, _contacts_simple_contact.id, &intValue);
443 contacts_record_destroy(recordHandle, true);
444 SysTryReturn(NID_SCL, intValue == contactId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
446 ret = contacts_db_delete_record(_contacts_contact._uri, contactId);
447 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));
448 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));
449 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
455 _AddressbookManagerImpl::RemoveCategory(RecordId categoryId)
457 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);
458 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
461 int ret = CONTACTS_ERROR_NONE;
462 contacts_record_h recordHandle = null;
464 ret = contacts_db_get_record(_contacts_group._uri, categoryId, &recordHandle);
465 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));
466 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));
467 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
469 __ContactsRecordHandle categoryHandle(recordHandle);
471 contacts_record_get_int(recordHandle, _contacts_group.id, &intValue);
472 SysTryReturn(NID_SCL, intValue == categoryId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
474 bool isReadOnly = false;
475 contacts_record_get_bool(recordHandle, _contacts_group.is_read_only, &isReadOnly);
476 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));
478 ret = contacts_db_delete_record(_contacts_group._uri, categoryId);
479 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));
480 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));
481 // SysTryReturnResult(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OPERATION_FAILED, "Failed to delete a category.(%d)", ret); // temp
487 _AddressbookManagerImpl::UpdateContact(const Contact& contact)
489 RecordId contactId = contact.GetRecordId();
490 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));
491 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));
492 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
494 contacts_record_h recordHandle = null;
497 int ret = contacts_db_get_record(_contacts_simple_contact._uri, contactId, &recordHandle);
498 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));
499 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
500 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
502 contacts_record_get_int(recordHandle, _contacts_simple_contact.id, &intValue);
504 contacts_record_destroy(recordHandle, true);
505 SysTryReturn(NID_SCL, intValue == contactId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
507 recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
509 ret = contacts_db_update_record(recordHandle);
510 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));
511 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is insufficient.", GetErrorMessage(E_STORAGE_FULL));
512 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));
514 ret = contacts_db_get_record(_contacts_contact._uri, contact.GetRecordId(), &recordHandle);
515 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));
516 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));
517 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
519 _ContactImpl::GetInstance(*const_cast<Contact*>(&contact))->SetContactRecordHandle(recordHandle);
525 _AddressbookManagerImpl::UpdateCategory(const Category& category)
527 RecordId categoryId = category.GetRecordId();
528 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));
529 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));
530 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
532 contacts_record_h recordHandle = null;
535 int ret = contacts_db_get_record(_contacts_group._uri, categoryId, &recordHandle);
536 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));
537 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));
539 contacts_record_get_int(recordHandle, _contacts_group.id, &intValue);
541 contacts_record_destroy(recordHandle, true);
542 SysTryReturn(NID_SCL, intValue == categoryId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
544 recordHandle = _CategoryImpl::GetInstance(category)->GetRecordHandle();
546 ret = contacts_db_update_record(recordHandle);
547 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
548 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is insufficient.", GetErrorMessage(E_STORAGE_FULL));
549 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
551 ret = contacts_db_get_record(_contacts_group._uri, category.GetRecordId(), &recordHandle);
552 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.");
553 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));
554 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
556 _CategoryImpl::GetInstance(*const_cast<Category*>(&category))->SetRecordHandle(recordHandle);
558 std::unique_ptr<IListT<int> > pList(_CategoryImpl::GetInstance(category)->GetAddedMembersN());
559 if (pList != null && pList->GetCount() > 0)
562 std::unique_ptr<IEnumeratorT<int> > pEnum(pList->GetEnumeratorN());
563 while (pEnum->MoveNext() == E_SUCCESS)
565 pEnum->GetCurrent(tableId);
567 AddMemberToCategory(category.GetRecordId(), tableId);
570 const_cast<_CategoryImpl*>(_CategoryImpl::GetInstance(category))->ClearAddedMemberList();
573 pList.reset(_CategoryImpl::GetInstance(category)->GetRemovedMembersN());
574 if (pList != null && pList->GetCount() > 0)
577 std::unique_ptr<IEnumeratorT<int> > pEnum(pList->GetEnumeratorN());
578 while (pEnum->MoveNext() == E_SUCCESS)
580 pEnum->GetCurrent(tableId);
582 RemoveMemberFromCategory(category.GetRecordId(), tableId);
585 const_cast<_CategoryImpl*>(_CategoryImpl::GetInstance(category))->ClearRemovedMemberList();
593 _AddressbookManagerImpl::AddMemberToCategory(RecordId categoryId, RecordId contactId)
596 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);
597 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);
598 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
600 int ret = contacts_group_add_contact(categoryId, contactId);
601 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
602 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));
603 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
609 _AddressbookManagerImpl::RemoveMemberFromCategory(RecordId categoryId, RecordId contactId)
612 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);
613 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);
614 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
616 int ret = contacts_group_remove_contact(categoryId, contactId);
617 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
618 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));
619 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
625 _AddressbookManagerImpl::GetAllCategoriesN(void) const
627 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
631 __Query<__ContactsGroup> query;
633 query.SetSort(_contacts_group.name, true);
635 IList* pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsGroup, Category>(query);
636 SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
642 _AddressbookManagerImpl::GetCategoriesByContactN(RecordId contactId) const
644 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
648 __Filter<__ContactsGroupRelation> filter;
650 filter.AddInt(_contacts_group_relation.contact_id, CONTACTS_MATCH_EQUAL, contactId);
652 __Query<__ContactsGroupRelation> query;
654 query.SetFilter(filter);
656 IList* pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsGroupRelation, Category>(query);
657 SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
663 _AddressbookManagerImpl::GetCategoriesByPersonN(PersonId personId) const
665 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
669 __Filter<__ContactsContactGroupRel> filter;
671 filter.AddInt(_contacts_contact_grouprel.person_id, CONTACTS_MATCH_EQUAL, personId);
672 filter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
673 filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_GREATER_THAN, 0);
675 unsigned int propertyIds[] =
677 _contacts_contact_grouprel.group_id,
680 __Query<__ContactsContactGroupRel> query;
682 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
683 query.SetFilter(filter);
684 query.SetSort(_contacts_contact_grouprel.group_name, true);
685 query.SetDistinct(true);
687 IList* pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Category>(query);
688 SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
694 _AddressbookManagerImpl::GetAllContactsN(void) const
696 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
700 __Query<__ContactsContact> query;
702 query.SetSort(_contacts_contact.display_name, true);
704 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
705 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
711 _AddressbookManagerImpl::GetContactsByCategoryN(RecordId categoryId) const
713 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));
714 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
718 __Filter<__ContactsContactGroupRel> filter;
721 if (categoryId != INVALID_RECORD_ID)
723 filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_EQUAL, categoryId);
727 filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_NONE, 0);
730 __Query<__ContactsContactGroupRel> query;
732 query.SetFilter(filter);
735 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Contact>(query);
736 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
742 _AddressbookManagerImpl::GetContactsByPersonN(PersonId personId) const
744 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
748 __Filter<__ContactsContact> filter;
750 filter.AddInt(_contacts_contact.person_id, CONTACTS_MATCH_EQUAL, personId);
752 __Query<__ContactsContact> query;
754 query.SetFilter(filter);
755 query.SetSort(_contacts_contact.display_name, true);
757 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
758 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
764 _AddressbookManagerImpl::SearchContactsByEmailN(const String& email) const
766 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));
767 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
771 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(email));
772 SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
774 __Filter<__ContactsContactEmail> filter;
776 filter.AddString(_contacts_contact_email.email, CONTACTS_MATCH_CONTAINS, pCharArray.get());
778 __Query<__ContactsContactEmail> query;
780 query.SetFilter(filter);
781 query.SetSort(_contacts_contact_email.display_name, true);
783 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactEmail, Contact>(query);
784 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
791 _AddressbookManagerImpl::SearchContactsByNameN(const String& name) const
793 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
797 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));
799 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(name));
800 SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
802 __Filter<__ContactsContact> filter;
804 filter.AddString(_contacts_contact.display_name, CONTACTS_MATCH_CONTAINS, pCharArray.get());
806 __Query<__ContactsContact> query;
808 query.SetFilter(filter);
809 query.SetSort(_contacts_contact.display_name, true);
811 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
812 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
818 _AddressbookManagerImpl::SearchContactsByPhoneNumberN(const String& phoneNumber) const
820 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
824 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));
826 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(phoneNumber));
827 SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
829 __Filter<__ContactsContactNumber> filter;
831 filter.AddString(_contacts_contact_number.number, CONTACTS_MATCH_CONTAINS, pCharArray.get());
833 __Query<__ContactsContactNumber> query;
835 query.SetFilter(filter);
836 query.SetDistinct(true);
837 query.SetSort(_contacts_contact_number.display_name, true);
839 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactNumber, Contact>(query);
840 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
846 _AddressbookManagerImpl::GetCategoryCount(void) const
848 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
854 count = _AddressbookUtil::GetCount<__ContactsGroup>();
855 SysTryReturn(NID_SCL, count >= 0, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
861 _AddressbookManagerImpl::GetContactCount(void) const
863 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
869 count = _AddressbookUtil::GetCount<__ContactsContact>();
870 SysTryReturn(NID_SCL, count >= 0, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
876 _AddressbookManagerImpl::GetContactN(RecordId contactId) const
878 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
880 SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. contactId = %d.", GetErrorMessage(E_INVALID_ARG), contactId);
883 contacts_record_h contactHandle = null;
885 std::unique_ptr<Contact> pContact(new (std::nothrow) Contact());
886 SysTryReturn(NID_SCL, pContact, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
888 int ret = contacts_db_get_record(_contacts_contact._uri, contactId, &contactHandle);
889 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
890 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
891 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
893 _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(contactHandle);
895 contacts_record_get_int(contactHandle, _contacts_contact.id, &intValue);
896 SysTryReturn(NID_SCL, intValue == contactId, null, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
898 _RecordImpl::GetInstance(*pContact)->SetRecordId(intValue);
900 return pContact.release();
904 _AddressbookManagerImpl::GetPersonN(PersonId personId) const
906 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
908 SysTryReturn(NID_SCL, personId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. personId = %d.", GetErrorMessage(E_INVALID_ARG), personId);
910 contacts_record_h recordHandle = null;
913 int ret = contacts_db_get_record(_contacts_person._uri, personId, &recordHandle);
914 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
915 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The person is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
916 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
918 __ContactsRecordHandle personRecord(recordHandle);
920 contacts_record_get_int(recordHandle, _contacts_person.id, &intValue);
921 SysTryReturn(NID_SCL, intValue == personId, null, E_OBJ_NOT_FOUND, "[%s] The person is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
923 Person* pPerson = __ContactsPerson::ConvertHandleTo<Person>(recordHandle);
924 SysTryReturn(NID_SCL, pPerson != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
930 _AddressbookManagerImpl::GetCategoryN(RecordId categoryId) const
932 SysTryReturn(NID_SCL, categoryId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. categoryId = %d.", categoryId);
933 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
936 contacts_record_h recordHandle = null;
940 int ret = contacts_db_get_record(_contacts_group._uri, categoryId, &recordHandle);
941 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
942 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
943 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
946 contacts_record_get_int(recordHandle, _contacts_group.id, &intValue);
948 std::unique_ptr<Category> pCategory(new (std::nothrow) Category());
949 SysTryReturn(NID_SCL, pCategory != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
951 SysTryReturn(NID_SCL, categoryId == intValue, null, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
953 __Filter<__ContactsGroupRelation> filter;
955 filter.AddInt(_contacts_group_relation.group_id, CONTACTS_MATCH_EQUAL, categoryId);
957 __Query<__ContactsGroupRelation> query;
959 query.SetFilter(filter);
961 int count = _AddressbookUtil::GetCountWithQuery(query);
962 SysTryReturn(NID_SCL, count >= 0, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
964 _CategoryImpl::GetInstance(*pCategory)->SetRecordHandle(recordHandle);
965 _CategoryImpl::GetInstance(*pCategory)->SetMemberCount(count);
966 _RecordImpl::GetInstance(*pCategory)->SetRecordId(categoryId);
968 return pCategory.release();
972 _AddressbookManagerImpl::GetLatestVersion(void) const
974 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
976 int latestVersion = -1;
980 int ret = contacts_db_get_current_version(&latestVersion);
981 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
983 return latestVersion;
987 _AddressbookManagerImpl::GetChangedContactsAfterN(int version, int& latestVersion) const
989 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);
990 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
993 IList* pChangedContacts = _AddressbookUtil::SearchWithVersionN<__ContactsContactUpdatedInfo, ContactChangeInfo>(-1, version, latestVersion);
994 SysTryReturn(NID_SCL, pChangedContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
996 return pChangedContacts;
1000 _AddressbookManagerImpl::GetChangedCategoriesAfterN(int version, int& latestVersion) const
1002 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);
1003 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1007 int latestVersion1 = 0;
1008 int latestVersion2 = 0;
1010 std::unique_ptr<IList, AllElementsDeleter> pChangedGroups(_AddressbookUtil::SearchWithVersionN<__ContactsGroupUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion1));
1011 SysTryReturn(NID_SCL, pChangedGroups != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1013 std::unique_ptr<IList, AllElementsDeleter> pChangedRelations(_AddressbookUtil::SearchWithVersionN<__ContactsGroupRelUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion2));
1014 SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1016 std::unique_ptr<ArrayList, AllElementsDeleter> pChangeList(new (std::nothrow) Tizen::Base::Collection::ArrayList());
1017 SysTryReturn(NID_SCL, pChangeList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1019 result r = pChangeList->AddItems(*pChangedGroups);
1020 SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1022 r = pChangeList->AddItems(*pChangedRelations);
1023 SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1025 pChangedGroups->RemoveAll(false);
1026 pChangedRelations->RemoveAll(false);
1028 latestVersion = latestVersion2 > latestVersion1 ? latestVersion2 : latestVersion1;
1030 return pChangeList.release();
1035 _AddressbookManagerImpl::GetChangedGroupsAfterN(int version, int& latestVersion) const
1037 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);
1038 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1042 IList* pChangedRelations = _AddressbookUtil::SearchWithVersionN<__ContactsGroupUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion);
1043 SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1045 return pChangedRelations;
1049 _AddressbookManagerImpl::GetChangedRelationsAfterN(int version, int& latestVersion) const
1051 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);
1052 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1056 IList* pChangedRelations = _AddressbookUtil::SearchWithVersionN<__ContactsGroupRelUpdatedInfo, CategoryChangeInfo>(-1, version, latestVersion);
1057 SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1059 return pChangedRelations;
1063 _AddressbookManagerImpl::OnContactChanged(void)
1065 if (__pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
1070 IList* pChangedContactList = GetChangedContactsAfterN(__dbVersionForContact, __dbVersionForContact);
1071 SysTryReturnVoidResult(NID_SCL, pChangedContactList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1073 if (pChangedContactList->GetCount() > 0)
1075 if (__pIAddressbookChangeEventListener != null)
1077 __pIAddressbookChangeEventListener->OnContactsChanged(*pChangedContactList);
1081 __pIAddressbookEventListener->OnContactsChanged(*pChangedContactList);
1085 pChangedContactList->RemoveAll(true);
1086 delete pChangedContactList;
1090 _AddressbookManagerImpl::OnCategoryChanged(void)
1092 if (__pIAddressbookChangeEventListener == null && __pIAddressbookEventListener == null)
1097 IList* pChangedCategoryList = GetChangedGroupsAfterN(__dbVersionForGroup, __dbVersionForGroup);
1098 SysTryReturnVoidResult(NID_SCL, pChangedCategoryList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1100 if (pChangedCategoryList->GetCount() > 0)
1102 if (__pIAddressbookChangeEventListener != null)
1104 __pIAddressbookChangeEventListener->OnCategoriesChanged(*pChangedCategoryList);
1108 __pIAddressbookEventListener->OnCategoriesChanged(*pChangedCategoryList);
1112 pChangedCategoryList->RemoveAll(true);
1113 delete pChangedCategoryList;
1117 _AddressbookManagerImpl::OnRelationChanged(void)
1119 if (__pIAddressbookEventListener == null)
1124 IList* pChangedCategoryList = GetChangedRelationsAfterN(__dbVersionForRelation, __dbVersionForRelation);
1125 SysTryReturnVoidResult(NID_SCL, pChangedCategoryList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1127 if (pChangedCategoryList->GetCount() > 0)
1129 if (__pIAddressbookEventListener != null)
1131 __pIAddressbookEventListener->OnCategoriesChanged(*pChangedCategoryList);
1135 pChangedCategoryList->RemoveAll(true);
1136 delete pChangedCategoryList;
1140 _AddressbookManagerImpl::RemovePerson(PersonId personId)
1142 SysTryReturnResult(NID_SCL, personId > 0, E_INVALID_ARG, "[%s] Invalid argument is used. personId = %d.", GetErrorMessage(E_INVALID_ARG), personId);
1143 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1145 contacts_record_h recordHandle = null;
1147 int ret = contacts_db_get_record(_contacts_person._uri, personId, &recordHandle);
1148 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));
1149 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));
1150 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));
1152 contacts_record_destroy(recordHandle, true);
1154 ret = contacts_db_delete_record(_contacts_person._uri, personId);
1155 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.");
1156 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));
1157 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));
1163 _AddressbookManagerImpl::GetAllPersonsN(void) const
1165 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1169 __Query<__ContactsPerson> query;
1171 query.SetSort(_contacts_person.display_name, true);
1173 IList* pPersons = _AddressbookUtil::SearchWithQueryN<__ContactsPerson, Person>(query);
1174 SysTryReturn(NID_SCL, pPersons != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1180 _AddressbookManagerImpl::GetPersonsByCategoryN(RecordId categoryId) const
1182 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));
1183 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1187 __Filter<__ContactsPersonGroupRel> filter;
1189 if (categoryId != INVALID_RECORD_ID)
1191 filter.AddInt(_contacts_person_grouprel.group_id, CONTACTS_MATCH_EQUAL, categoryId);
1195 filter.AddInt(_contacts_person_grouprel.group_id, CONTACTS_MATCH_NONE, 0);
1198 unsigned int propertyIds[] =
1200 _contacts_person_grouprel.person_id,
1201 _contacts_person_grouprel.display_name,
1202 _contacts_person_grouprel.image_thumbnail_path,
1203 _contacts_person_grouprel.ringtone_path,
1204 _contacts_person_grouprel.is_favorite,
1205 _contacts_person_grouprel.has_phonenumber,
1206 _contacts_person_grouprel.has_email,
1207 _contacts_person_grouprel.link_count,
1210 __Query<__ContactsPersonGroupRel> query;
1212 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1213 query.SetFilter(filter);
1214 query.SetSort(_contacts_person_grouprel.display_name, true);
1215 query.SetDistinct(true);
1217 IList* pPersons = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query);
1218 SysTryReturn(NID_SCL, pPersons != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1224 _AddressbookManagerImpl::GetFavoritePersonsN() const
1226 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1230 __Filter<__ContactsPerson> filter;
1232 filter.AddBool(_contacts_person.is_favorite, true);
1234 __Query<__ContactsPerson> query;
1236 query.SetFilter(filter);
1237 query.SetSort(_contacts_person.display_name, true);
1239 IList* pPersons = _AddressbookUtil::SearchWithQueryN<__ContactsPerson, Person>(query);
1240 SysTryReturn(NID_SCL, pPersons != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1246 _AddressbookManagerImpl::SearchPersonsN(const Tizen::Base::String& keyword) const
1248 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1250 contacts_record_h currentRecord = null;
1251 std::unique_ptr<Person> pPerson(null);
1253 std::unique_ptr<ArrayList, AllElementsDeleter> pPersonList(new (std::nothrow) ArrayList());
1254 SysTryReturn(NID_SCL, pPersonList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1256 result r = pPersonList->Construct();
1257 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1259 std::unique_ptr<__SearchResult<__ContactsPerson> > pSearchResult(_AddressbookUtil::Search<__ContactsPerson>(keyword));
1260 SysTryReturn(NID_SCL, pSearchResult != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1262 while (pSearchResult->MoveNext() == E_SUCCESS)
1264 currentRecord = pSearchResult->GetCurrentRecord();
1265 SysTryReturn(NID_SCL, currentRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1267 pPerson.reset(__ContactsPerson::ConvertHandleTo<Person>(currentRecord));
1268 SysTryReturn(NID_SCL, pPerson != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1270 r = pPersonList->Add(*pPerson);
1271 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
1276 return pPersonList.release();
1280 _AddressbookManagerImpl::SetPersonAsFavorite(PersonId personId, bool isFavorite)
1282 SysTryReturn(NID_SCL, personId > 0, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. personId = %d.", GetErrorMessage(E_INVALID_ARG), personId);
1283 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1285 bool boolValue = false;
1286 contacts_record_h personHandle = null;
1288 int ret = contacts_db_get_record(_contacts_person._uri, personId, &personHandle);
1289 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));
1290 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));
1292 __ContactsRecordHandle recordHandle(personHandle);
1294 contacts_record_get_bool(personHandle, _contacts_person.is_favorite, &boolValue);
1296 if (boolValue != isFavorite)
1298 contacts_record_set_bool(personHandle, _contacts_person.is_favorite, isFavorite);
1300 ret = contacts_db_update_record(personHandle);
1301 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));
1302 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1309 _AddressbookManagerImpl::MergePersons(PersonId sourcePersonId, PersonId targetPersonId)
1311 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);
1312 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1314 int ret = contacts_person_link_person(targetPersonId, sourcePersonId);
1315 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1316 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1322 _AddressbookManagerImpl::UnlinkContact(PersonId personId, RecordId contactId, PersonId& newPersonId)
1324 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);
1325 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1327 int ret = contacts_person_unlink_contact(personId, contactId, &newPersonId);
1328 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));
1329 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1335 _AddressbookManagerImpl::SearchN(const AddressbookFilter& filter, unsigned long propertySortedBy, SortOrder sortOrder, int offset, int maxCount)
1337 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1339 IList* pList = null;
1340 bool ascending = false;
1341 unsigned int viewSortPropertyId = 0;
1343 AddressbookFilterType type = _AddressbookFilterImpl::GetInstance(filter)->GetType();
1344 contacts_filter_h filterHandle = _AddressbookFilterImpl::GetInstance(filter)->GetFilterHandle();
1346 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);
1348 if (propertySortedBy != 0 && sortOrder != SORT_ORDER_NONE)
1350 viewSortPropertyId = _AddressbookFilterImpl::GetViewPropertyId(type, propertySortedBy);
1351 ascending = (sortOrder == SORT_ORDER_ASCENDING) ? true : false;
1356 case AB_FI_TYPE_ADDRESSBOOK:
1358 __Filter<__ContactsAddressbook> filter;
1359 filter.Construct(filterHandle);
1361 __Query<__ContactsAddressbook> query;
1363 query.SetFilter(filter);
1365 if (viewSortPropertyId != 0)
1367 query.SetSort(viewSortPropertyId, ascending);
1370 pList = _AddressbookUtil::SearchWithQueryN<__ContactsAddressbook, Addressbook>(query, offset, maxCount);
1373 case AB_FI_TYPE_PERSON:
1375 __Filter<__ContactsPersonGroupRel> filter;
1376 filter.Construct(filterHandle);
1378 unsigned int propertyIds[] =
1379 { _contacts_person_grouprel.person_id,
1380 _contacts_person_grouprel.addressbook_ids,
1381 _contacts_person_grouprel.is_favorite,
1382 _contacts_person_grouprel.has_phonenumber,
1383 _contacts_person_grouprel.has_email,
1384 _contacts_person_grouprel.image_thumbnail_path,
1385 _contacts_person_grouprel.ringtone_path,
1386 _contacts_person_grouprel.display_name
1389 __Query<__ContactsPersonGroupRel> query;
1391 query.SetFilter(filter);
1392 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1393 query.SetDistinct(true);
1395 if (viewSortPropertyId != 0)
1397 query.SetSort(viewSortPropertyId, ascending);
1400 pList = _AddressbookUtil::SearchWithQueryN<__ContactsPersonGroupRel, Person>(query, offset, maxCount);
1403 case AB_FI_TYPE_CONTACT:
1405 __Filter<__ContactsContact> filter;
1406 filter.Construct(filterHandle);
1408 __Query<__ContactsContact> query;
1410 query.SetFilter(filter);
1412 if (viewSortPropertyId != 0)
1414 query.SetSort(viewSortPropertyId, ascending);
1417 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query, offset, maxCount);
1420 case AB_FI_TYPE_CATEGORY:
1422 __Filter<__ContactsGroup> filter;
1423 filter.Construct(filterHandle);
1425 __Query<__ContactsGroup> query;
1427 query.SetFilter(filter);
1429 if (viewSortPropertyId != 0)
1431 query.SetSort(viewSortPropertyId, ascending);
1434 pList = _AddressbookUtil::SearchWithQueryN<__ContactsGroup, Category>(query, offset, maxCount);
1437 case AB_FI_TYPE_PHONE_CONTACT:
1439 __Filter<__ContactsContactNumber> filter;
1440 filter.Construct(filterHandle);
1442 __Query<__ContactsContactNumber> query;
1444 query.SetFilter(filter);
1446 if (viewSortPropertyId != 0)
1448 query.SetSort(viewSortPropertyId, ascending);
1451 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContactNumber, PhoneNumberContact>(query, offset, maxCount);
1454 case AB_FI_TYPE_EMAIL_CONTACT:
1456 __Filter<__ContactsContactEmail> filter;
1457 filter.Construct(filterHandle);
1459 __Query<__ContactsContactEmail> query;
1461 query.SetFilter(filter);
1463 if (viewSortPropertyId != 0)
1465 query.SetSort(viewSortPropertyId, ascending);
1468 pList = _AddressbookUtil::SearchWithQueryN<__ContactsContactEmail, EmailContact>(query, offset, maxCount);
1472 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. The filter type is invalid", GetErrorMessage(E_INVALID_ARG));
1480 _AddressbookManagerImpl::GetMatchedItemCount(const AddressbookFilter& filter)
1482 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1485 AddressbookFilterType type = _AddressbookFilterImpl::GetInstance(filter)->GetType();
1486 contacts_filter_h filterHandle = _AddressbookFilterImpl::GetInstance(filter)->GetFilterHandle();
1490 case AB_FI_TYPE_ADDRESSBOOK:
1491 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsAddressbook>(filterHandle);
1493 case AB_FI_TYPE_PERSON:
1495 __Filter<__ContactsPersonGroupRel> filter;
1496 filter.Construct(filterHandle);
1498 unsigned int propertyIds[] = { _contacts_person_grouprel.person_id };
1500 __Query<__ContactsPersonGroupRel> query;
1502 query.SetFilter(filter);
1503 query.SetProjection(propertyIds, sizeof(propertyIds)/sizeof(unsigned int));
1504 query.SetDistinct(true);
1506 count = _AddressbookUtil::GetCountWithQuery(query);
1510 case AB_FI_TYPE_CONTACT:
1511 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContact>(filterHandle);
1513 case AB_FI_TYPE_CATEGORY:
1514 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsGroup>(filterHandle);
1516 case AB_FI_TYPE_PHONE_CONTACT:
1517 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContactNumber>(filterHandle);
1519 case AB_FI_TYPE_EMAIL_CONTACT:
1520 count = _AddressbookUtil::GetMatchedItemCountWithFilter<__ContactsContactEmail>(filterHandle);
1523 SysLogException(NID_SCL, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the filter is invalid", GetErrorMessage(GetLastResult()));
1531 _AddressbookManagerImpl::OnEachContact(contacts_record_h recordHandle, void* pUserData)
1533 IList* pList = static_cast<IList*>(pUserData);
1537 std::unique_ptr<Contact> pContact(new (std::nothrow) Contact());
1538 SysTryReturn(NID_SCL, pContact != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1540 contacts_record_h newRecordHandle = null;
1542 contacts_record_clone(recordHandle, &newRecordHandle);
1543 SysTryReturn(NID_SCL, newRecordHandle != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1545 _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(newRecordHandle);
1547 result r = pList->Add(*pContact);
1548 SysTryReturn(NID_SCL, !IsFailed(r), false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1556 _AddressbookManagerImpl::ParseContactsFromVcardN(const Tizen::Base::String& vcardPath)
1558 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1563 result r = file.Construct(vcardPath, "r");
1564 SysTryReturn(NID_SCL, r != E_INVALID_ARG, null, E_INVALID_ARG, "[%s] Invalid argument is used..", GetErrorMessage(E_INVALID_ARG));
1565 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));
1566 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));
1567 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1569 std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
1571 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(vcardPath));
1573 int ret = contacts_vcard_parse_to_contact_foreach(pCharArray.get(), OnEachContact, pList.get());
1574 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1575 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1576 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1578 return pList.release();
1582 _AddressbookManagerImpl::ExportPersonToVcard(const Person& person, const Tizen::Base::String& vcardPath)
1584 bool exist = File::IsFileExist(vcardPath);
1585 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
1586 SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
1587 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1590 result r = file.Construct(vcardPath, "w");
1591 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));
1592 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1593 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1595 contacts_record_h personRecordHandle = null;
1597 int ret = contacts_db_get_record(_contacts_person._uri, person.GetId(), &personRecordHandle);
1598 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));
1599 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));
1600 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1602 __ContactsRecordHandle recordHandle(personRecordHandle);
1604 char* pVcardStream = null;
1605 ret = contacts_vcard_make_from_person(personRecordHandle, &pVcardStream);
1606 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));
1607 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1609 r = file.Write(pVcardStream, strlen(pVcardStream));
1611 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1612 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1618 _AddressbookManagerImpl::ExportPersonsToVcard(const Tizen::Base::Collection::IList& personList, const Tizen::Base::String& vcardPath)
1620 bool exist = File::IsFileExist(vcardPath);
1621 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
1622 SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
1623 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1625 int ret = CONTACTS_ERROR_NONE;
1626 char* pVcardStream = null;
1629 result r = file.Construct(vcardPath, "w");
1630 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));
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));
1634 contacts_record_h personRecordHandle = null;
1635 __ContactsRecordHandle recordHandle(null);
1637 std::unique_ptr<IEnumerator> pEnum(personList.GetEnumeratorN());
1638 SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1640 while (pEnum->MoveNext() == E_SUCCESS)
1642 Person* pPerson = static_cast<Person*>(pEnum->GetCurrent());
1644 ret = contacts_db_get_record(_contacts_person._uri, pPerson->GetId(), &personRecordHandle);
1645 SysTryReturnResult(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, "The specified person does not exist.");
1646 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));
1647 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1649 recordHandle.Reset(personRecordHandle);
1651 ret = contacts_vcard_make_from_person(personRecordHandle, &pVcardStream);
1652 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));
1653 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1655 r = file.Write(pVcardStream, strlen(pVcardStream));
1657 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1658 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1665 _AddressbookManagerImpl::ExportContactToVcard(const Contact& contact, const Tizen::Base::String& vcardPath)
1667 bool exist = File::IsFileExist(vcardPath);
1668 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
1669 SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
1670 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1673 result r = file.Construct(vcardPath, "w");
1674 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));
1675 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1676 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1678 contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
1680 char* pVcardStream = null;
1682 int ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
1683 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));
1684 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1686 r = file.Write(pVcardStream, strlen(pVcardStream));
1688 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1689 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1695 _AddressbookManagerImpl::ExportContactsToVcard(const Tizen::Base::Collection::IList& contactList, const Tizen::Base::String& vcardPath)
1697 bool exist = File::IsFileExist(vcardPath);
1698 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
1699 SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
1700 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1702 int ret = CONTACTS_ERROR_NONE;
1703 char* pVcardStream = null;
1705 Contact* pContact = null;
1707 result r = file.Construct(vcardPath, "w");
1708 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));
1709 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
1710 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1712 contacts_record_h recordHandle = null;
1714 std::unique_ptr<IEnumerator> pEnum(contactList.GetEnumeratorN());
1715 SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1717 while (pEnum->MoveNext() == E_SUCCESS)
1719 pContact = static_cast<Contact*>(pEnum->GetCurrent());
1721 recordHandle = _ContactImpl::GetInstance(*pContact)->GetContactRecordHandle();
1723 ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
1724 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));
1725 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1727 r = file.Write(pVcardStream, strlen(pVcardStream));
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));
1737 _AddressbookManagerImpl::ExportContactToVcardStreamN(const Contact& contact)
1739 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1741 contacts_record_h recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
1743 char* pVcardStream = null;
1744 int ret = CONTACTS_ERROR_NONE;
1746 ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
1747 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1748 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1750 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1752 std::unique_ptr<char> pChar(pVcardStream);
1754 std::unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
1755 SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1757 result r = pByteBuffer->Construct(strlen(pChar.get()));
1758 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1760 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pChar.get()), 0, strlen(pChar.get()));
1761 SysTryReturn(NID_SCL, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1762 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1764 return pByteBuffer.release();
1768 _AddressbookManagerImpl::ExportContactsToVcardStreamN(const Tizen::Base::Collection::IList& contactList)
1770 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1772 char* pVcardStream = null;
1773 int ret = CONTACTS_ERROR_NONE;
1774 Contact* pContact = null;
1775 result r = E_SUCCESS;
1778 contacts_record_h recordHandle = null;
1779 std::unique_ptr<char> pChar(null);
1781 std::unique_ptr<IEnumerator> pEnum(contactList.GetEnumeratorN());
1782 SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1784 std::unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
1785 SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1787 r = pByteBuffer->Construct(capacity);
1788 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1790 if (contactList.GetCount() == 0)
1792 return pByteBuffer.release();
1795 while (pEnum->MoveNext() == E_SUCCESS)
1797 pContact = static_cast<Contact*>(pEnum->GetCurrent());
1799 recordHandle = _ContactImpl::GetInstance(*pContact)->GetContactRecordHandle();
1801 ret = contacts_vcard_make_from_contact(recordHandle, &pVcardStream);
1802 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1803 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1805 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1807 pChar.reset(pVcardStream);
1809 capacity += strlen(pChar.get());
1810 r = pByteBuffer->ExpandCapacity(capacity);
1811 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1813 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pChar.get()), 0, strlen(pChar.get()));
1814 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.: capacity(%d), size(%d)", GetErrorMessage(E_SYSTEM));
1817 return pByteBuffer.release();
1821 _AddressbookManagerImpl::ExportPersonToVcardStreamN(const Person& person)
1823 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1825 int ret = CONTACTS_ERROR_NONE;
1827 contacts_record_h personRecordHandle = null;
1829 ret =contacts_db_get_record(_contacts_person._uri, person.GetId(), &personRecordHandle);
1830 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1831 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1832 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1834 __ContactsRecordHandle recordHandle(personRecordHandle);
1836 char* pVcardStream = null;
1838 ret = contacts_vcard_make_from_person(personRecordHandle, &pVcardStream);
1839 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1840 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1841 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1843 std::unique_ptr<char> pChar(pVcardStream);
1845 std::unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
1846 SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1848 result r = pByteBuffer->Construct(strlen(pChar.get()));
1849 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1851 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pChar.get()), 0, strlen(pChar.get()));
1852 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1854 return pByteBuffer.release();
1858 _AddressbookManagerImpl::ExportPersonsToVcardStreamN(const Tizen::Base::Collection::IList& personList)
1860 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1862 int ret = CONTACTS_ERROR_NONE;
1863 Person* pPerson = null;
1864 char* pVcardStream = null;
1868 contacts_record_h personRecordHandle = null;
1869 __ContactsRecordHandle recordHandle(null);
1870 std::unique_ptr<char> pChar(null);
1872 std::unique_ptr<IEnumerator> pEnum(personList.GetEnumeratorN());
1873 SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1875 std::unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
1876 SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1878 result r = pByteBuffer->Construct(capacity);
1879 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1881 if (personList.GetCount() == 0)
1883 return pByteBuffer.release();
1886 while (pEnum->MoveNext() == E_SUCCESS)
1888 pPerson = static_cast<Person*>(pEnum->GetCurrent());
1890 ret = contacts_db_get_record(_contacts_person._uri, pPerson->GetId(), &personRecordHandle);
1891 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1892 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1893 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1895 recordHandle.Reset(personRecordHandle);
1897 ret = contacts_vcard_make_from_person(personRecordHandle, &pVcardStream);
1898 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1899 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1900 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1902 pChar.reset(pVcardStream);
1904 capacity += strlen(pChar.get());
1905 r = pByteBuffer->ExpandCapacity(capacity);
1906 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1908 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pChar.get()), 0, strlen(pChar.get()));
1909 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1912 return pByteBuffer.release();
1916 _AddressbookManagerImpl::ParseVcardStreamN(const Tizen::Base::ByteBuffer& vcardStream)
1918 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1920 contacts_list_h listHandle = null;
1921 result r = E_SUCCESS;
1923 int ret = contacts_vcard_parse_to_contacts(reinterpret_cast<char*>(const_cast<byte*>(vcardStream.GetPointer())), &listHandle);
1924 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1925 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1927 unsigned int count = 0;
1928 contacts_record_h recordHandle = null;
1929 contacts_list_get_count(listHandle, &count);
1931 std::unique_ptr<ArrayList, AllElementsDeleter> pList(new (std::nothrow) ArrayList());
1932 SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1934 r = pList->Construct(count);
1935 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1937 for (int i = 0; i < count; i++)
1939 std::unique_ptr<Contact> pContact(new (std::nothrow) Contact());
1940 SysTryReturn(NID_SCL, pContact != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1942 contacts_list_get_current_record_p(listHandle, &recordHandle);
1944 _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(recordHandle);
1945 r = pList->Add(pContact.get());
1946 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1950 ret = contacts_list_next(listHandle);
1951 if (ret != CONTACTS_ERROR_NONE)
1957 return pList.release();
1960 Tizen::Base::ByteBuffer*
1961 _AddressbookManagerImpl::ExportUserProfileToVcardStreamN(const UserProfile& userProfile)
1963 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1965 char* pVcardStream = null;
1966 int ret = CONTACTS_ERROR_NONE;
1967 Contact tempContact;
1968 contacts_record_h contactHandle = null;
1970 result r = _AddressbookUtil::ConvertUserProfileToContact(userProfile, tempContact);
1971 SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1973 contactHandle = _ContactImpl::GetInstance(tempContact)->GetContactRecordHandle();
1975 ret = contacts_vcard_make_from_contact(contactHandle, &pVcardStream);
1976 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
1977 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1978 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1980 std::unique_ptr<char> pChar(pVcardStream);
1982 std::unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
1983 SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1985 r = pByteBuffer->Construct(strlen(pChar.get()));
1986 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1988 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pChar.get()), 0, strlen(pChar.get()));
1989 SysTryReturn(NID_SCL, r != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1990 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1992 return pByteBuffer.release();
1995 Tizen::Base::ByteBuffer*
1996 _AddressbookManagerImpl::ExportUserProfilesToVcardStreamN(const Tizen::Base::Collection::IList& userProfileList)
1998 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2000 char* pVcardStream = null;
2001 int ret = CONTACTS_ERROR_NONE;
2002 UserProfile* pProfile = null;
2003 result r = E_SUCCESS;
2006 std::unique_ptr<char> pChar(null);
2008 std::unique_ptr<IEnumerator> pEnum(userProfileList.GetEnumeratorN());
2009 SysTryReturn(NID_SCL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2011 std::unique_ptr<ByteBuffer> pByteBuffer(new (std::nothrow) ByteBuffer);
2012 SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2014 r = pByteBuffer->Construct(capacity);
2015 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2017 if (userProfileList.GetCount() == 0)
2019 return pByteBuffer.release();
2022 while (pEnum->MoveNext() == E_SUCCESS)
2024 Contact tempContact;
2025 contacts_record_h contactHandle = null;
2027 pProfile = static_cast<UserProfile*>(pEnum->GetCurrent());
2029 r = _AddressbookUtil::ConvertUserProfileToContact(*pProfile, tempContact);
2030 SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
2032 contactHandle = _ContactImpl::GetInstance(tempContact)->GetContactRecordHandle();
2034 ret = contacts_vcard_make_from_contact(contactHandle, &pVcardStream);
2035 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, null, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
2036 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2037 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2039 pChar.reset(pVcardStream);
2041 capacity += strlen(pChar.get());
2042 r = pByteBuffer->ExpandCapacity(capacity);
2043 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2045 r = pByteBuffer->SetArray(reinterpret_cast<byte*>(pChar.get()), 0, strlen(pChar.get()));
2046 SysTryReturn(NID_SCL, r == E_SUCCESS, null, E_SYSTEM, "[%s] A system error has been occurred.: capacity(%d), size(%d)", GetErrorMessage(E_SYSTEM));
2049 return pByteBuffer.release();
2053 _AddressbookManagerImpl::ExportUserProfileToVcard(const UserProfile& userProfile, const Tizen::Base::String& vcardPath)
2055 bool exist = File::IsFileExist(vcardPath);
2056 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2057 SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2058 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2061 result r = file.Construct(vcardPath, "w");
2062 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));
2063 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2064 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2066 char* pVcardStream = null;
2067 Contact tempContact;
2068 contacts_record_h contactHandle = null;
2070 r = _AddressbookUtil::ConvertUserProfileToContact(userProfile, tempContact);
2071 SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
2073 contactHandle = _ContactImpl::GetInstance(tempContact)->GetContactRecordHandle();
2075 int ret = contacts_vcard_make_from_contact(contactHandle, &pVcardStream);
2076 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));
2077 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2079 r = file.Write(pVcardStream, strlen(pVcardStream));
2081 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2082 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2088 _AddressbookManagerImpl::ExportUserProfilesToVcard(const Tizen::Base::Collection::IList& userProfileList, const Tizen::Base::String& vcardPath)
2090 bool exist = File::IsFileExist(vcardPath);
2091 SysTryReturn(NID_SCL, GetLastResult() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating..", GetErrorMessage(GetLastResult()));
2092 SysTryReturn(NID_SCL, !exist, E_FILE_ALREADY_EXIST, E_FILE_ALREADY_EXIST, "[%s] The specified vcard file already exist.", GetErrorMessage(E_FILE_ALREADY_EXIST));
2093 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2095 int ret = CONTACTS_ERROR_NONE;
2096 char* pVcardStream = null;
2098 UserProfile* pProfile = null;
2100 result r = file.Construct(vcardPath, "w");
2101 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));
2102 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2103 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2105 std::unique_ptr<IEnumerator> pEnum(userProfileList.GetEnumeratorN());
2106 SysTryReturnResult(NID_SCL, pEnum != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2108 while (pEnum->MoveNext() == E_SUCCESS)
2110 Contact tempContact;
2111 contacts_record_h contactHandle = null;
2113 pProfile = static_cast<UserProfile*>(pEnum->GetCurrent());
2115 r = _AddressbookUtil::ConvertUserProfileToContact(*pProfile, tempContact);
2116 SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
2118 contactHandle = _ContactImpl::GetInstance(tempContact)->GetContactRecordHandle();
2120 ret = contacts_vcard_make_from_contact(contactHandle, &pVcardStream);
2121 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));
2122 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2124 r = file.Write(pVcardStream, strlen(pVcardStream));
2126 SysTryReturn(NID_SCL, r != E_STORAGE_FULL, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is full.", GetErrorMessage(E_STORAGE_FULL));
2127 SysTryReturn(NID_SCL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
2133 Tizen::Base::Collection::IList*
2134 _AddressbookManagerImpl::GetAllUserProfilesN(void) const
2136 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2140 __Query<__ContactsUserProfile> query;
2142 query.SetSort(_contacts_my_profile.display_name, true);
2144 IList* pUserProfilesList = _AddressbookUtil::SearchWithQueryN<__ContactsUserProfile, UserProfile>(query);
2145 SysTryReturn(NID_SCL, pUserProfilesList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2147 return pUserProfilesList;
2151 _AddressbookManagerImpl::GetUserProfileN(AddressbookId addressbookId) const
2153 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2154 SysTryReturn(NID_SCL, addressbookId >= 0, null, E_INVALID_ARG, "[%s] Invalid argument is used. Addressbook Id(%d).", GetErrorMessage(E_INVALID_ARG), addressbookId);
2158 contacts_record_h addressbookHandle = null;
2159 int ret = contacts_db_get_record(_contacts_address_book._uri, addressbookId, &addressbookHandle);
2160 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
2161 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);
2162 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred. Addressbook Id(%d)", GetErrorMessage(E_SYSTEM), addressbookId);
2164 contacts_record_destroy(addressbookHandle, true);
2166 __Filter<__ContactsUserProfile> filter;
2168 filter.AddInt(_contacts_my_profile.address_book_id, CONTACTS_MATCH_EQUAL, addressbookId);
2170 __Query<__ContactsUserProfile> query;
2172 query.SetFilter(filter);
2174 std::unique_ptr<IList, AllElementsDeleter> pUserProfilesList(_AddressbookUtil::SearchWithQueryN<__ContactsUserProfile, UserProfile>(query));
2175 SysTryReturn(NID_SCL, pUserProfilesList.get() != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
2176 SysTryReturn(NID_SCL, pUserProfilesList->GetCount() != 0, null, E_SUCCESS, "No UserProfile Set for this Addressbook.");
2177 SysTryReturn(NID_SCL, pUserProfilesList->GetCount() == 1, null, E_SYSTEM, "[%s] Propagating. More than one UserProfile not allowed.", GetErrorMessage(E_SYSTEM));
2179 UserProfile* pProfile = new (std::nothrow) UserProfile(*(static_cast<UserProfile*>(pUserProfilesList->GetAt(0))));
2180 SysTryReturn(NID_SCL, pProfile != null, null, E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(E_OUT_OF_MEMORY));
2185 _AddressbookManagerImpl*
2186 _AddressbookManagerImpl::GetInstance(AddressbookManager& addressbookManager)
2188 return addressbookManager.__pAddressbookManagerImpl;
2191 const _AddressbookManagerImpl*
2192 _AddressbookManagerImpl::GetInstance(const AddressbookManager& addressbookManager)
2194 return addressbookManager.__pAddressbookManagerImpl;