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_AddressbookImpl.cpp
19 * @brief This is the implementation for _AddressbookImpl class.
21 * This file contains definitions of @e _AddressbookImpl class.
24 #include <FBaseColIListT.h>
25 #include <FBaseColArrayListT.h>
26 #include <FBaseResult.h>
27 #include <FBaseLongLong.h>
28 #include <FBaseInteger.h>
29 #include <FSclContact.h>
30 #include <FSclCategory.h>
31 #include <FSclContactChangeInfo.h>
32 #include <FSclCategoryChangeInfo.h>
33 #include <FSclAddressbook.h>
34 #include <FSclIAddressbookEventListener.h>
35 #include <FSclIRecordEventListener.h>
36 #include <FApp_AppInfo.h>
37 #include <FBaseSysLog.h>
38 #include <FBase_StringConverter.h>
39 #include "FScl_AddressbookImpl.h"
40 #include "FScl_AddressbookUtil.h"
41 #include "FScl_CategoryChangeInfoImpl.h"
42 #include "FScl_CategoryImpl.h"
43 #include "FScl_ContactChangeInfoImpl.h"
44 #include "FScl_ContactDbMonitor.h"
45 #include "FScl_ContactImpl.h"
46 #include "FScl_RecordImpl.h"
47 #include "FScl_ContactDbConnector.h"
49 using namespace Tizen::App;
50 using namespace Tizen::Base;
51 using namespace Tizen::Base::Collection;
52 using namespace Tizen::Graphics;
54 namespace Tizen { namespace Social
57 _AddressbookImpl::_AddressbookImpl(void)
58 : __pIRecordEventListener(null)
59 , __pIAddressbookEventListener(null)
60 , __dbVersionForContact(0)
61 , __dbVersionForGroup(0)
62 , __dbVersionForRelation(0)
63 , __addressbookId(INVALID_ADDRESSBOOK_ID)
69 _AddressbookImpl::~_AddressbookImpl(void)
71 if (__pIAddressbookEventListener != null || __pIRecordEventListener != null)
73 _ContactDbMonitor* pDbMonitor = _ContactDbMonitor::GetInstance();
74 if (pDbMonitor != null)
76 pDbMonitor->RemoveListener(*this);
82 _AddressbookImpl::Construct(void)
84 static AccountId accountId = 0;
89 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
92 contacts_record_h recordHandle = null;
93 int ret = contacts_db_get_record(_contacts_address_book._uri, DEFAULT_ADDRESSBOOK_ID, &recordHandle);
94 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
95 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred. Addressbook Id(%d)", GetErrorMessage(E_SYSTEM), DEFAULT_ADDRESSBOOK_ID);
97 contacts_record_get_str_p(recordHandle, _contacts_address_book.name, &pName);
98 contacts_record_get_int(recordHandle, _contacts_address_book.account_id, &accountId);
102 contacts_record_destroy(recordHandle, true);
105 __addressbookId = DEFAULT_ADDRESSBOOK_ID;
106 __accountId = accountId;
113 _AddressbookImpl::SetRecordEventListener(IRecordEventListener* pListener)
115 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
117 result r = E_SUCCESS;
119 if (pListener != null)
121 if (__pIAddressbookEventListener == null && __pIRecordEventListener == null)
123 __dbVersionForContact = GetLatestVersion();
124 SysTryReturn(NID_SCL, __dbVersionForContact != -1, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
126 __dbVersionForGroup = __dbVersionForContact;
127 __dbVersionForRelation = __dbVersionForContact;
129 _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
130 SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
132 r = pContactDbMonitor->AddListener(*this);
133 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
136 __pIAddressbookEventListener = null;
137 __pIRecordEventListener = const_cast<IRecordEventListener*>(pListener);
141 if (__pIAddressbookEventListener != null || __pIRecordEventListener != null)
143 _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
144 SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()))
145 pContactDbMonitor->RemoveListener(*this);
148 __pIAddressbookEventListener = null;
149 __pIRecordEventListener = null;
156 _AddressbookImpl::SetAddressbookEventListener(IAddressbookEventListener* pListener)
158 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
160 result r = E_SUCCESS;
162 if (pListener != null)
164 if (__pIAddressbookEventListener == null && __pIRecordEventListener == null)
166 __dbVersionForContact = GetLatestVersion();
167 SysTryReturn(NID_SCL, __dbVersionForContact != -1, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
169 __dbVersionForGroup = __dbVersionForContact;
170 __dbVersionForRelation = __dbVersionForContact;
172 _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
173 SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
175 r = pContactDbMonitor->AddListener(*this);
176 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
179 __pIRecordEventListener = null;
180 __pIAddressbookEventListener = pListener;
184 if (__pIAddressbookEventListener != null || __pIRecordEventListener != null)
186 _ContactDbMonitor* pContactDbMonitor = _ContactDbMonitor::GetInstance();
187 SysTryReturn(NID_SCL, pContactDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
189 pContactDbMonitor->RemoveListener(*this);
192 __pIAddressbookEventListener = null;
193 __pIRecordEventListener = null;
200 _AddressbookImpl::SetId(AddressbookId addressbookId)
202 __addressbookId = addressbookId;
206 _AddressbookImpl::SetAccountId(AccountId accountId)
208 __accountId = accountId;
212 _AddressbookImpl::SetName(const String& name)
218 _AddressbookImpl::GetId(void) const
220 return __addressbookId;
224 _AddressbookImpl::GetAccountId(void) const
230 _AddressbookImpl::GetName(void) const
236 _AddressbookImpl::AddContact(Contact& contact)
238 if (_ContactImpl::GetInstance(contact)->IsRemoved())
240 result r = _ContactImpl::GetInstance(contact)->Invalidate();
241 SysTryReturn(NID_SCL, r == E_SUCCESS, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
244 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));
245 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));
246 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
249 contacts_record_h recordHandle = null;
251 recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
253 contacts_record_set_int(recordHandle, _contacts_contact.address_book_id, __addressbookId);
255 int ret = contacts_db_insert_record(recordHandle, &recordId);
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_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
259 ret = contacts_db_get_record(_contacts_contact._uri, recordId, &recordHandle);
260 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The contact is not found.");
261 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OBJ_NOT_FOUND, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
262 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
264 _ContactImpl::GetInstance(contact)->SetContactRecordHandle(recordHandle);
265 _RecordImpl::GetInstance(contact)->SetRecordId(recordId);
271 _AddressbookImpl::AddCategory(Category& category)
273 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));
274 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
277 contacts_record_h recordHandle = null;
278 std::unique_ptr<IListT<int> > pList(null);
280 recordHandle = _CategoryImpl::GetInstance(category)->GetRecordHandle();
282 if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
284 // It is not allowed to have a same name with other.
286 char* pCharValue = null;
287 contacts_record_get_str_p(recordHandle, _contacts_group.name, &pCharValue);
289 __Filter<__ContactsGroup> filter;
291 filter.AddInt(_contacts_group.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
292 filter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
293 filter.AddString(_contacts_group.name, CONTACTS_MATCH_EXACTLY, pCharValue);
295 __Query<__ContactsGroup> query;
297 query.SetFilter(filter);
299 count = _AddressbookUtil::GetCountWithQuery(query);
300 SysTryReturn(NID_SCL, count >= 0, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
301 SysTryReturn(NID_SCL, count == 0, E_OBJ_ALREADY_EXIST, E_OBJ_ALREADY_EXIST, "[%s] The category name is already being used by other category.", GetErrorMessage(E_OBJ_ALREADY_EXIST));
304 recordHandle = _CategoryImpl::GetInstance(category)->GetRecordHandle();
306 contacts_record_set_int(recordHandle, _contacts_group.address_book_id, __addressbookId);
308 int ret = contacts_db_insert_record(recordHandle, &recordId);
309 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));
310 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
312 ret = contacts_db_get_record(_contacts_group._uri, recordId, &recordHandle);
313 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));
314 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));
315 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%d] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
317 _CategoryImpl::GetInstance(category)->SetRecordHandle(recordHandle);
318 _RecordImpl::GetInstance(category)->SetRecordId(recordId);
320 pList.reset(_CategoryImpl::GetInstance(category)->GetAddedMembersN());
321 if (pList != null && pList->GetCount() > 0)
323 std::unique_ptr<IEnumeratorT<int> > pEnum(pList->GetEnumeratorN());
325 while (pEnum->MoveNext() == E_SUCCESS)
328 pEnum->GetCurrent(tableId);
330 AddMemberToCategory(category.GetRecordId(), tableId);
333 _CategoryImpl::GetInstance(category)->ClearAddedMemberList();
340 _AddressbookImpl::RemoveContact(RecordId contactId)
342 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);
343 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
346 int ret = CONTACTS_ERROR_NONE;
347 contacts_record_h recordHandle = null;
349 ret = contacts_db_get_record(_contacts_simple_contact._uri, contactId, &recordHandle);
350 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));
351 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));
352 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
354 contacts_record_get_int(recordHandle, _contacts_simple_contact.id, &intValue);
356 contacts_record_destroy(recordHandle, true);
358 SysTryReturn(NID_SCL, intValue == contactId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
360 ret = contacts_db_delete_record(_contacts_contact._uri, contactId);
361 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));
362 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));
363 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
369 _AddressbookImpl::RemoveContact(Contact& contact)
371 SysTryReturn(NID_SCL, contact.GetRecordId() != INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The ID of specified contact is not INVALID_RECORD_ID", GetErrorMessage(E_INVALID_ARG));
372 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
374 result r = RemoveContact(contact.GetRecordId());
375 SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
377 _ContactImpl::GetInstance(contact)->SetAsRemoved();
378 _RecordImpl::GetInstance(contact)->SetRecordId(INVALID_RECORD_ID);
384 _AddressbookImpl::RemoveCategory(RecordId categoryId)
386 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);
387 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
390 int ret = CONTACTS_ERROR_NONE;
391 contacts_record_h recordHandle = null;
393 ret = contacts_db_get_record(_contacts_group._uri, categoryId, &recordHandle);
394 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.");
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_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
398 __ContactsRecordHandle categoryHandle(recordHandle);
400 contacts_record_get_int(recordHandle, _contacts_group.id, &intValue);
401 SysTryReturn(NID_SCL, intValue == categoryId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
403 bool isReadOnly = false;
404 contacts_record_get_bool(recordHandle, _contacts_group.is_read_only, &isReadOnly);
405 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));
407 ret = contacts_db_delete_record(_contacts_group._uri, categoryId);
408 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));
409 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));
410 //SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed remove a category.");
416 _AddressbookImpl::UpdateContact(const Contact& contact)
418 RecordId contactId = contact.GetRecordId();
419 SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
420 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));
421 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
423 contacts_record_h recordHandle = null;
426 int ret = contacts_db_get_record(_contacts_simple_contact._uri, contactId, &recordHandle);
427 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));
428 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));
429 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
431 contacts_record_get_int(recordHandle, _contacts_simple_contact.id, &intValue);
433 contacts_record_destroy(recordHandle, true);
434 SysTryReturn(NID_SCL, intValue == contactId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
436 recordHandle = _ContactImpl::GetInstance(contact)->GetContactRecordHandle();
438 ret = contacts_db_update_record(recordHandle);
439 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));
440 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));
441 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
443 ret = contacts_db_get_record(_contacts_contact._uri, contact.GetRecordId(), &recordHandle);
444 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));
445 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));
446 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
448 _ContactImpl::GetInstance(*const_cast<Contact*>(&contact))->SetContactRecordHandle(recordHandle);
454 _AddressbookImpl::UpdateCategory(const Category& category)
456 RecordId categoryId = category.GetRecordId();
457 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));
458 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));
459 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
461 contacts_record_h recordHandle = null;
463 int ret = CONTACTS_ERROR_NONE;
465 ret = contacts_db_get_record(_contacts_group._uri, category.GetRecordId(), &recordHandle);
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_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
468 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
470 contacts_record_get_int(recordHandle, _contacts_group.id, &intValue);
472 contacts_record_destroy(recordHandle, true);
474 SysTryReturn(NID_SCL, intValue == categoryId, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
476 recordHandle = _CategoryImpl::GetInstance(category)->GetRecordHandle();
478 if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
481 char* pCharValue = null;
483 contacts_record_get_str_p(recordHandle, _contacts_group.name, &pCharValue);
484 contacts_record_get_int(recordHandle, _contacts_group.id, &intValue);
486 __Filter<__ContactsGroup> filter1;
488 filter1.AddInt(_contacts_group.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
489 filter1.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
490 filter1.AddString(_contacts_group.name, CONTACTS_MATCH_EXACTLY, pCharValue);
492 __Filter<__ContactsGroup> filter2;
494 filter2.AddInt(_contacts_group.id, CONTACTS_MATCH_LESS_THAN, intValue);
495 filter2.AddOperator(CONTACTS_FILTER_OPERATOR_OR);
496 filter2.AddInt(_contacts_group.id, CONTACTS_MATCH_GREATER_THAN, intValue);
498 filter1.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
499 filter1.AddFilter(filter2);
501 __Query<__ContactsGroup> query;
503 query.SetFilter(filter1);
505 count = _AddressbookUtil::GetCountWithQuery(query);
506 SysTryReturn(NID_SCL, count >= 0, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
507 SysTryReturn(NID_SCL, count == 0, E_OBJ_ALREADY_EXIST, E_OBJ_ALREADY_EXIST, "[%s] The category name is alread being used by other category.", GetErrorMessage(E_OBJ_ALREADY_EXIST));
510 ret = contacts_db_update_record(recordHandle);
511 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));
512 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));
513 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
515 ret = contacts_db_get_record(_contacts_group._uri, category.GetRecordId(), &recordHandle);
516 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));
517 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));
518 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
520 _CategoryImpl::GetInstance(*const_cast<Category*>(&category))->SetRecordHandle(recordHandle);
522 std::unique_ptr<IListT<int> > pList(_CategoryImpl::GetInstance(category)->GetAddedMembersN());
523 if (pList != null && pList->GetCount() > 0)
526 std::unique_ptr<IEnumeratorT<int> > pEnum(pList->GetEnumeratorN());
527 while (pEnum->MoveNext() == E_SUCCESS)
529 pEnum->GetCurrent(tableId);
531 AddMemberToCategory(category.GetRecordId(), tableId);
534 const_cast<_CategoryImpl*>(_CategoryImpl::GetInstance(category))->ClearAddedMemberList();
537 pList.reset(_CategoryImpl::GetInstance(category)->GetRemovedMembersN());
538 if (pList != null && pList->GetCount() > 0)
541 std::unique_ptr<IEnumeratorT<int> > pEnum(pList->GetEnumeratorN());
542 while (pEnum->MoveNext() == E_SUCCESS)
544 pEnum->GetCurrent(tableId);
546 RemoveMemberFromCategory(category.GetRecordId(), tableId);
549 const_cast<_CategoryImpl*>(_CategoryImpl::GetInstance(category))->ClearRemovedMemberList();
556 _AddressbookImpl::AddMemberToCategory(RecordId categoryId, RecordId contactId)
558 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);
559 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);
560 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
562 contacts_record_h recordHandle = null;
563 int addressbookId = 0;
564 int ret = contacts_db_get_record(_contacts_simple_contact._uri, contactId, &recordHandle);
565 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The contact does not exist.", GetErrorMessage(E_INVALID_ARG));
566 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));
567 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
568 contacts_record_get_int(recordHandle, _contacts_simple_contact.address_book_id, &addressbookId);
569 contacts_record_destroy(recordHandle, true);
571 SysTryReturn(NID_SCL, addressbookId == __addressbookId, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The contact does not exist in this addresbook.", GetErrorMessage(E_INVALID_ARG));
573 ret = contacts_group_add_contact(categoryId, contactId);
574 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
575 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
576 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
582 _AddressbookImpl::RemoveMemberFromCategory(RecordId categoryId, RecordId contactId)
584 SysTryReturn(NID_SCL, categoryId != INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. categoryId = %d.", GetErrorMessage(E_INVALID_ARG), categoryId);
585 SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. contactId = %d.", GetErrorMessage(E_INVALID_ARG), contactId);
586 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
588 contacts_record_h recordHandle = null;
589 int addressbookId = 0;
590 int ret = contacts_db_get_record(_contacts_simple_contact._uri, contactId, &recordHandle);
591 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The contact does not exist.", GetErrorMessage(E_INVALID_ARG));
592 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));
593 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
594 contacts_record_get_int(recordHandle, _contacts_simple_contact.address_book_id, &addressbookId);
595 contacts_record_destroy(recordHandle, true);
597 SysTryReturn(NID_SCL, addressbookId == __addressbookId, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The contact does not exist in this addresbook.", GetErrorMessage(E_INVALID_ARG));
599 ret = contacts_db_get_record(_contacts_group._uri, categoryId, &recordHandle);
600 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The category does not exist.", GetErrorMessage(E_INVALID_ARG));
601 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));
602 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
604 contacts_record_get_int(recordHandle, _contacts_group.address_book_id, &addressbookId);
605 contacts_record_destroy(recordHandle, true);
607 SysTryReturn(NID_SCL, addressbookId == __addressbookId, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The category does not exist in this addresbook.", GetErrorMessage(E_INVALID_ARG));
610 ret = contacts_group_remove_contact(categoryId, contactId);
611 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
612 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
613 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
619 _AddressbookImpl::GetAllCategoriesN(void) const
621 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
625 __Filter<__ContactsGroup> filter;
627 filter.AddInt(_contacts_group.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
629 __Query<__ContactsGroup> query;
631 query.SetFilter(filter);
632 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 _AddressbookImpl::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);
655 query.SetSort(_contacts_group_relation.name, true);
657 IList* pCategories = _AddressbookUtil::SearchWithQueryN<__ContactsGroupRelation, Category>(query);
658 SysTryReturn(NID_SCL, pCategories != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
664 _AddressbookImpl::GetAllContactsN(void) const
666 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
670 __Filter<__ContactsContact> filter;
672 filter.AddInt(_contacts_contact.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
674 __Query<__ContactsContact> query;
676 query.SetFilter(filter);
677 query.SetSort(_contacts_contact.display_name, true);
679 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
680 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
686 _AddressbookImpl::GetContactsByCategoryN(RecordId categoryId) const
688 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));
689 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
693 __Filter<__ContactsContactGroupRel> filter;
695 if (categoryId != INVALID_RECORD_ID)
697 filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_EQUAL, categoryId);
701 filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_NONE, 0);
704 __Query<__ContactsContactGroupRel> query;
706 query.SetFilter(filter);
707 query.SetSort(_contacts_contact_grouprel.display_name, true);
709 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Contact>(query);
710 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
716 _AddressbookImpl::GetContactsN(int pageNo, int countPerPage) const
718 SysTryReturn(NID_SCL, pageNo > 0 && countPerPage > 0, null, E_OUT_OF_RANGE, "[%s] pageNo(%d) or countPerPage(%d) is less than 1.", GetErrorMessage(E_OUT_OF_RANGE), pageNo, countPerPage);
719 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
721 int offset = (pageNo - 1)*countPerPage;
722 int limit = countPerPage;
726 __Filter<__ContactsContact> filter;
728 filter.AddInt(_contacts_contact.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
730 __Query<__ContactsContact> query;
732 query.SetFilter(filter);
733 query.SetSort(_contacts_contact.display_name, true);
735 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query, offset, limit);
736 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
742 _AddressbookImpl::GetContactsInN(const Category& category, int pageNo, int countPerPage) const
744 SysTryReturn(NID_SCL, category.GetRecordId() != INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. The specified category is invalid.", GetErrorMessage(E_INVALID_ARG));
745 SysTryReturn(NID_SCL, pageNo > 0 && countPerPage > 0, null, E_OUT_OF_RANGE, "[%s] pageNo(%d) or countPerPage(%d) is less than 1.", GetErrorMessage(E_OUT_OF_RANGE), pageNo, countPerPage);
746 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
748 int offset = (pageNo - 1)*countPerPage;
749 int limit = countPerPage;
753 __Filter<__ContactsContactGroupRel> filter;
755 filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_EQUAL, category.GetRecordId());
757 __Query<__ContactsContactGroupRel> query;
759 query.SetFilter(filter);
760 query.SetSort(_contacts_contact_grouprel.display_name, true);
762 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactGroupRel, Contact>(query, offset, limit);
763 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
769 _AddressbookImpl::SearchContactsByEmailN(const String& email) const
771 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));
772 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
774 if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
776 SysTryReturn(NID_SCL, email.GetLength() <= MAX_EMAIL_LENGTH, null, E_INVALID_ARG, "[%s] Invalid argument is used. The length of email is greater than MAX_EMAIL_LENGTH.", GetErrorMessage(E_INVALID_ARG));
781 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(email));
782 SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
784 __Filter<__ContactsContactEmail> filter;
786 filter.AddInt(_contacts_contact_email.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
787 filter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
788 filter.AddString(_contacts_contact_email.email, CONTACTS_MATCH_CONTAINS, pCharArray.get());
790 unsigned int projectionIds[1];
791 projectionIds[0] = _contacts_contact_email.contact_id;
793 __Query<__ContactsContactEmail> query;
795 query.SetProjection(projectionIds, 1);
796 query.SetFilter(filter);
797 query.SetDistinct(true);
798 query.SetSort(_contacts_contact_email.display_name, true);
800 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactEmail, Contact>(query);
801 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
807 _AddressbookImpl::SearchContactsByNameN(const String& name) const
809 SysTryReturn(NID_SCL, !name.IsEmpty(), null, E_INVALID_ARG, "[%s] Invalid argument is used. The specified email is an empty string.", GetErrorMessage(E_INVALID_ARG));
810 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
812 if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
814 SysTryReturn(NID_SCL, name.GetLength() <= MAX_CONTACT_NAME_LENGTH, null, E_INVALID_ARG, "[%s] Invalid argument is used. The length of name is greater than MAX_CONTACT_NAME_LENGTH.", GetErrorMessage(E_INVALID_ARG));
820 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(name));
821 SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
823 __Filter<__ContactsContact> filter;
825 filter.AddInt(_contacts_contact.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
826 filter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
827 filter.AddString(_contacts_contact.display_name, CONTACTS_MATCH_CONTAINS, pCharArray.get());
829 __Query<__ContactsContact> query;
831 query.SetFilter(filter);
832 query.SetSort(_contacts_contact.display_name, true);
833 query.SetDistinct(true);
835 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContact, Contact>(query);
836 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
843 _AddressbookImpl::SearchContactsByPhoneNumberN(const String& phoneNumber) const
845 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));
846 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
849 if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
851 int count = phoneNumber.GetLength();
852 SysTryReturn(NID_SCL, count >= MIN_PHONENUMBER_QUERY_LENGTH && count <= MAX_PHONE_NUMBER_LENGTH, null, E_INVALID_ARG, "[%s] Invalid argument is used. The length of phoneNumber is shorter than MIN_PHONENUMBER_QUERY_LENGTH or greater than MAX_PHONE_NUMBER_LENGTH.", GetErrorMessage(E_INVALID_ARG));
857 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(phoneNumber));
858 SysTryReturn(NID_SCL, pCharArray != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
860 __Filter<__ContactsContactNumber> filter;
862 filter.AddInt(_contacts_contact_number.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
863 filter.AddOperator(CONTACTS_FILTER_OPERATOR_AND);
864 filter.AddString(_contacts_contact_number.number, CONTACTS_MATCH_CONTAINS, pCharArray.get());
866 unsigned int projectionIds[1];
867 projectionIds[0] = _contacts_contact_number.contact_id;
869 __Query<__ContactsContactNumber> query;
871 query.SetProjection(projectionIds, 1);
872 query.SetFilter(filter);
873 query.SetDistinct(true);
874 query.SetSort(_contacts_contact_number.display_name, true);
876 IList* pContacts = _AddressbookUtil::SearchWithQueryN<__ContactsContactNumber, Contact>(query);
877 SysTryReturn(NID_SCL, pContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
883 _AddressbookImpl::GetCategoryCount(void) const
885 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
891 __Filter<__ContactsGroup> filter;
893 filter.AddInt(_contacts_group.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
895 __Query<__ContactsGroup> query;
897 query.SetFilter(filter);
899 count = _AddressbookUtil::GetCountWithQuery(query);
900 SysTryReturn(NID_SCL, count >= 0, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
906 _AddressbookImpl::GetContactCount(void) const
908 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
914 __Filter<__ContactsContact> filter;
916 filter.AddInt(_contacts_contact.address_book_id, CONTACTS_MATCH_EQUAL, __addressbookId);
918 __Query<__ContactsContact> query;
920 query.SetFilter(filter);
922 count = _AddressbookUtil::GetCountWithQuery(query);
923 SysTryReturn(NID_SCL, count >= 0, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
929 _AddressbookImpl::GetContactN(RecordId contactId) const
931 SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. contactId = %d.", GetErrorMessage(E_INVALID_ARG), contactId);
932 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
935 contacts_record_h contactHandle = null;
939 std::unique_ptr<Contact> pContact(new (std::nothrow) Contact());
940 SysTryReturn(NID_SCL, pContact, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
942 int ret = contacts_db_get_record(_contacts_contact._uri, contactId, &contactHandle);
943 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
944 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
945 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
947 _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(contactHandle);
949 contacts_record_get_int(contactHandle, _contacts_contact.id, &intValue);
950 SysTryReturn(NID_SCL, intValue == contactId, null, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
952 _RecordImpl::GetInstance(*pContact)->SetRecordId(intValue);
954 return pContact.release();
958 _AddressbookImpl::GetCategoryN(RecordId categoryId) const
960 SysTryReturn(NID_SCL, categoryId != INVALID_RECORD_ID, null, E_INVALID_ARG, "[%s] Invalid argument is used. categoryId = %d.", GetErrorMessage(E_INVALID_ARG), categoryId);
961 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
963 contacts_record_h recordHandle = null;
968 int ret = contacts_db_get_record(_contacts_group._uri, categoryId, &recordHandle);
969 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
970 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
971 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
973 std::unique_ptr<Category> pCategory(new (std::nothrow) Category());
974 SysTryReturn(NID_SCL, pCategory != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
976 contacts_record_get_int(recordHandle, _contacts_group.id, &intValue);
977 SysTryReturn(NID_SCL, intValue == categoryId, null, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
979 __Filter<__ContactsGroupRelation> filter;
981 filter.AddInt(_contacts_group_relation.group_id, CONTACTS_MATCH_EQUAL, categoryId);
983 __Query<__ContactsGroupRelation> query;
985 query.SetFilter(filter);
987 int count = _AddressbookUtil::GetCountWithQuery(query);
988 SysTryReturn(NID_SCL, count >= 0, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
990 _CategoryImpl::GetInstance(*pCategory)->SetRecordHandle(recordHandle);
991 _CategoryImpl::GetInstance(*pCategory)->SetMemberCount(count);
992 _RecordImpl::GetInstance(*pCategory)->SetRecordId(categoryId);
994 return pCategory.release();
998 _AddressbookImpl::GetLatestVersion(void) const
1000 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1002 int latestVersion = -1;
1006 int ret = contacts_db_get_current_version(&latestVersion);
1007 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, -1, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1008 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
1010 return latestVersion;
1014 _AddressbookImpl::GetChangedContactsAfterN(int version, int& latestVersion) const
1016 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);
1017 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1019 IList* pChangedContacts = _AddressbookUtil::SearchWithVersionN<__ContactsContactUpdatedInfo, ContactChangeInfo>(__addressbookId, version, latestVersion);
1020 SysTryReturn(NID_SCL, pChangedContacts != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1022 return pChangedContacts;
1026 _AddressbookImpl::GetChangedCategoriesAfterN(int version, int& latestVersion) const
1028 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);
1029 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1031 int latestVersion1 = 0;
1032 int latestVersion2 = 0;
1034 std::unique_ptr<IList, AllElementsDeleter> pChangedGroups(_AddressbookUtil::SearchWithVersionN<__ContactsGroupUpdatedInfo, CategoryChangeInfo>(__addressbookId, version, latestVersion1));
1035 SysTryReturn(NID_SCL, pChangedGroups != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1037 std::unique_ptr<IList, AllElementsDeleter> pChangedRelations(_AddressbookUtil::SearchWithVersionN<__ContactsGroupRelUpdatedInfo, CategoryChangeInfo>(__addressbookId, version, latestVersion2));
1038 SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1040 std::unique_ptr<ArrayList, AllElementsDeleter> pChangeList(new (std::nothrow) Tizen::Base::Collection::ArrayList());
1041 SysTryReturn(NID_SCL, pChangeList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1043 result r = pChangeList->AddItems(*pChangedGroups);
1044 SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1046 r = pChangeList->AddItems(*pChangedRelations);
1047 SysTryReturn(NID_SCL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
1049 pChangedGroups->RemoveAll(false);
1050 pChangedRelations->RemoveAll(false);
1052 latestVersion = latestVersion2 > latestVersion1 ? latestVersion2 : latestVersion1;
1054 return pChangeList.release();
1058 _AddressbookImpl::GetChangedGroupsAfterN(int version, int& latestVersion) const
1060 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);
1061 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1063 IList* pChangedRelations = _AddressbookUtil::SearchWithVersionN<__ContactsGroupUpdatedInfo, CategoryChangeInfo>(__addressbookId, version, latestVersion);
1064 SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1066 return pChangedRelations;
1070 _AddressbookImpl::GetChangedRelationsAfterN(int version, int& latestVersion) const
1072 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);
1073 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1077 IList* pChangedRelations = _AddressbookUtil::SearchWithVersionN<__ContactsGroupRelUpdatedInfo, CategoryChangeInfo>(__addressbookId, version, latestVersion);
1078 SysTryReturn(NID_SCL, pChangedRelations != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1080 return pChangedRelations;
1084 _AddressbookImpl::OnContactChanged(void)
1086 if (__pIAddressbookEventListener == null && __pIRecordEventListener == null)
1091 std::unique_ptr<IList, AllElementsDeleter> pChangedContactList(GetChangedContactsAfterN(__dbVersionForContact, __dbVersionForContact));
1092 SysTryReturnVoidResult(NID_SCL, pChangedContactList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1094 if (pChangedContactList->GetCount() > 0)
1096 if (__pIAddressbookEventListener != null)
1098 __pIAddressbookEventListener->OnContactsChanged(*pChangedContactList);
1102 RecordEventType recordEventType = RECORD_ADDED;
1103 std::unique_ptr<IEnumerator> pEnum(pChangedContactList->GetEnumeratorN());
1104 std::unique_ptr<Contact> pContact(null);
1106 while (pEnum->MoveNext() == E_SUCCESS)
1108 ContactChangeInfo* pContactChagneInfo = static_cast<ContactChangeInfo*>(pEnum->GetCurrent());
1110 if (pContactChagneInfo->GetChangeType() == RECORD_CHANGE_TYPE_ADDED)
1112 recordEventType = RECORD_ADDED;
1113 pContact.reset(GetContactN(pContactChagneInfo->GetContactId()));
1114 if (pContact == null)
1116 if (GetLastResult() == E_OBJ_NOT_FOUND)
1126 else if (pContactChagneInfo->GetChangeType() == RECORD_CHANGE_TYPE_UPDATED)
1128 recordEventType = RECORD_UPDATED;
1129 pContact.reset(GetContactN(pContactChagneInfo->GetContactId()));
1130 if (pContact == null)
1132 if (GetLastResult() == E_OBJ_NOT_FOUND)
1144 recordEventType = RECORD_REMOVED;
1145 pContact.reset(new (std::nothrow) Contact());
1146 if (pContact == null)
1148 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1152 _RecordImpl::GetInstance(*pContact)->SetRecordId(pContactChagneInfo->GetContactId());
1155 __pIRecordEventListener->OnRecordChangedN(recordEventType, RECORD_TYPE_CONTACT, *pContact, null, null);
1163 _AddressbookImpl::OnCategoryChanged(void)
1165 if (__pIAddressbookEventListener == null && __pIRecordEventListener == null)
1170 std::unique_ptr<IList, AllElementsDeleter> pChangedCategoryList(GetChangedGroupsAfterN(__dbVersionForGroup, __dbVersionForGroup));
1171 SysTryReturnVoidResult(NID_SCL, pChangedCategoryList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1173 if (pChangedCategoryList->GetCount() > 0)
1175 if (__pIAddressbookEventListener != null)
1177 __pIAddressbookEventListener->OnCategoriesChanged(*pChangedCategoryList);
1181 RecordEventType recordEventType = RECORD_ADDED;
1182 std::unique_ptr<IEnumerator> pEnum(pChangedCategoryList->GetEnumeratorN());
1183 std::unique_ptr<Category> pCategory(null);
1185 while (pEnum->MoveNext() == E_SUCCESS)
1187 CategoryChangeInfo* pCategoryChagneInfo = static_cast<CategoryChangeInfo*>(pEnum->GetCurrent());
1189 if (pCategoryChagneInfo->GetChangeType() == RECORD_CHANGE_TYPE_ADDED)
1191 recordEventType = RECORD_ADDED;
1192 pCategory.reset(GetCategoryN(pCategoryChagneInfo->GetCategoryId()));
1193 if (pCategory == null)
1195 if (GetLastResult() == E_OBJ_NOT_FOUND)
1205 else if (pCategoryChagneInfo->GetChangeType() == RECORD_CHANGE_TYPE_UPDATED)
1207 recordEventType = RECORD_UPDATED;
1208 pCategory.reset(GetCategoryN(pCategoryChagneInfo->GetCategoryId()));
1209 if (pCategory == null)
1211 if (GetLastResult() == E_OBJ_NOT_FOUND)
1223 recordEventType = RECORD_REMOVED;
1224 pCategory.reset(new (std::nothrow) Category());
1225 if (pCategory == null)
1227 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1231 _RecordImpl::GetInstance(*pCategory)->SetRecordId(pCategoryChagneInfo->GetCategoryId());
1234 __pIRecordEventListener->OnRecordChangedN(recordEventType, RECORD_TYPE_CATEGORY, *pCategory, null, null);
1242 _AddressbookImpl::OnRelationChanged(void)
1244 if (__pIAddressbookEventListener == null && __pIRecordEventListener == null)
1249 std::unique_ptr<IList, AllElementsDeleter> pChangedCategoryList(GetChangedRelationsAfterN(__dbVersionForRelation, __dbVersionForRelation));
1250 SysTryReturnVoidResult(NID_SCL, pChangedCategoryList != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1252 if (pChangedCategoryList->GetCount() > 0)
1254 if (__pIAddressbookEventListener != null)
1256 __pIAddressbookEventListener->OnCategoriesChanged(*pChangedCategoryList);
1260 RecordEventType recordEventType = RECORD_ADDED;
1261 std::unique_ptr<IEnumerator> pEnum(pChangedCategoryList->GetEnumeratorN());
1262 std::unique_ptr<Category> pCategory(null);
1264 while (pEnum->MoveNext() == E_SUCCESS)
1266 CategoryChangeInfo* pCategoryChagneInfo = static_cast<CategoryChangeInfo*>(pEnum->GetCurrent());
1268 recordEventType = RECORD_UPDATED;
1269 pCategory.reset(GetCategoryN(pCategoryChagneInfo->GetCategoryId()));
1270 if (pCategory == null)
1272 if (GetLastResult() == E_OBJ_NOT_FOUND)
1282 __pIRecordEventListener->OnRecordChangedN(recordEventType, RECORD_TYPE_CATEGORY, *pCategory, null, null);
1289 _AddressbookImpl::GetInstance(Addressbook& addressbook)
1291 return addressbook.__pAddressbookImpl;
1294 const _AddressbookImpl*
1295 _AddressbookImpl::GetInstance(const Addressbook& addressbook)
1297 return addressbook.__pAddressbookImpl;