2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 * @file FScl_AddressbookUtil.h
18 * @brief This is the header file for the _AddressbookUtil class.
20 * This header file contains the declarations of the _AddressbookUtil class.
22 #ifndef _FSCL_INTERNAL_ADDRESSBOOK_UTIL_H_
23 #define _FSCL_INTERNAL_ADDRESSBOOK_UTIL_H_
25 #include <unique_ptr.h>
27 #include <FBaseColArrayList.h>
28 #include <FBaseColAllElementsDeleter.h>
29 #include "FScl_ContactDbConnector.h"
31 namespace Tizen { namespace Social
38 typedef __contacts_record_h ContactRecord;
40 struct ContactRecordDeleter
42 void operator()(ContactRecord* pRecord)
44 contacts_record_destroy(pRecord, true);
49 class __ContactsRecordHandle
52 __ContactsRecordHandle(contacts_record_h handle)
57 ~__ContactsRecordHandle(void)
61 contacts_record_destroy(__handle, true);
65 void Reset(contacts_record_h handle)
69 contacts_record_destroy(__handle, true);
75 contacts_record_h Release(void)
77 contacts_record_h handle = __handle;
84 contacts_record_h __handle;
87 template<typename __View>
91 __SearchResult(contacts_list_h list)
92 :__list(list), __isFirst(true)
97 int GetCount(void) const
101 unsigned int count = 0;
102 contacts_list_get_count(__list, &count);
110 contacts_record_h GetCurrentRecord(void) const
114 contacts_record_h record = null;
115 int ret = contacts_list_get_current_record_p(__list, &record);
116 if (ret != CONTACTS_ERROR_NONE)
127 result MoveNext(void)
131 int ret = CONTACTS_ERROR_NONE;
135 ret = contacts_list_next(__list);
136 if (ret != CONTACTS_ERROR_NONE)
138 return E_INVALID_OPERATION;
152 return E_INVALID_OPERATION;
156 return E_INVALID_OPERATION;
160 ~__SearchResult(void)
164 contacts_list_destroy(__list, true);
169 contacts_list_h __list;
173 template<typename __View>
178 :__filterHandle(null)
179 ,__destroyFilterHandle(false)
183 result Construct(void)
185 contacts_filter_h filterHandle = null;
186 int ret = contacts_filter_create(__View::GetUri(), &filterHandle);
187 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
189 __filterHandle = filterHandle;
190 __destroyFilterHandle = true;
195 result Construct(contacts_filter_h filterHandle)
197 __filterHandle = filterHandle;
198 __destroyFilterHandle = false;
203 contacts_filter_h Get(void) const
205 return __filterHandle;
208 result AddString(unsigned int propertyId, contacts_match_str_flag_e match, const char* value)
210 int ret = contacts_filter_add_str(__filterHandle, propertyId, match, value);
211 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
216 result AddInt(unsigned int propertyId, contacts_match_int_flag_e match, int value)
218 int ret = contacts_filter_add_int(__filterHandle, propertyId, match, value);
219 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
224 result AddLongLong(unsigned int propertyId, contacts_match_int_flag_e match, long long int value)
226 int ret = contacts_filter_add_lli(__filterHandle, propertyId, match, value);
227 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
232 result AddDouble(unsigned int propertyId, contacts_match_int_flag_e match, double value)
234 int ret = contacts_filter_add_double(__filterHandle, propertyId, match, value);
235 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
240 result AddBool(unsigned int propertyId, bool value)
242 int ret = contacts_filter_add_bool(__filterHandle, propertyId, value);
243 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
248 result AddOperator(contacts_filter_operator_e type)
250 int ret = contacts_filter_add_operator(__filterHandle, type);
251 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
256 result AddFilter(__Filter<__View>& filter)
258 contacts_filter_h handle = filter.Get();
259 int ret = contacts_filter_add_filter(__filterHandle, handle);
260 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
267 if (__filterHandle != null && __destroyFilterHandle)
269 contacts_filter_destroy(__filterHandle);
274 contacts_filter_h __filterHandle;
275 bool __destroyFilterHandle;
278 template<typename __View>
287 result Construct(void)
289 contacts_query_h queryHandle = null;
290 int ret = contacts_query_create(__View::GetUri(), &queryHandle);
291 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
293 __queryHandle = queryHandle;
298 contacts_query_h Get(void) const
300 return __queryHandle;
304 result SetProjection(unsigned int propertyIds[], int count)
306 int ret = contacts_query_set_projection(__queryHandle, propertyIds, count);
307 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
312 result SetDistinct(bool set)
314 int ret = contacts_query_set_distinct(__queryHandle, set);
315 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
320 result SetFilter(__Filter<__View>& filter)
322 contacts_filter_h filterHandle = filter.Get();
324 if (filterHandle != null)
326 int ret = contacts_query_set_filter(__queryHandle, filterHandle);
327 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
333 result SetSort(unsigned int propertyId, bool ascending)
335 int ret = contacts_query_set_sort(__queryHandle, propertyId, ascending);
336 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
343 if (__queryHandle != null)
345 contacts_query_destroy(__queryHandle);
350 contacts_query_h __queryHandle;
354 class __ContactsAddressbook
357 static const char* GetUri(void)
359 return _contacts_address_book._uri;
362 template<typename Type>
363 static Type* ConvertResultTo(__SearchResult<__ContactsAddressbook>& searchResult);
365 static int GetAbPropertyId(void)
367 return _contacts_address_book.id;
372 class __ContactsPerson
375 static const char* GetUri(void)
377 return _contacts_person._uri;
380 template<typename Type>
381 static Type* ConvertResultTo(__SearchResult<__ContactsPerson>& searchResult);
383 template<typename Type>
384 static Type* ConvertHandleTo(contacts_record_h recordHandle);
388 class __ContactsPersonEmail
391 static const char* GetUri(void)
393 return _contacts_person_email._uri;
397 class __ContactsPersonGroupRel
400 static const char* GetUri(void)
402 return _contacts_person_grouprel._uri;
405 template<typename Type>
406 static Type* ConvertHandleTo(contacts_record_h recordHandle);
408 template<typename Type>
409 static Type* ConvertResultTo(__SearchResult<__ContactsPersonGroupRel>& searchResult);
411 static int GetAbPropertyId(void)
413 return _contacts_person_grouprel.address_book_id;
417 class __ContactsPersonNumber
420 static const char* GetUri(void)
422 return _contacts_person_number._uri;
427 class __ContactsGroup
430 static const char* GetUri(void)
432 return _contacts_group._uri;
435 template<typename Type>
436 static Type* ConvertResultTo(__SearchResult<__ContactsGroup>& searchResult);
438 static int GetAbPropertyId(void)
440 return _contacts_group.address_book_id;
444 class __ContactsGroupRelation
447 static const char* GetUri(void)
449 return _contacts_group_relation._uri;
452 template<typename Type>
453 static Type* ConvertResultTo(__SearchResult<__ContactsGroupRelation>& searchResult);
456 class __ContactsContact
459 static const char* GetUri(void)
461 return _contacts_contact._uri;
464 template<typename Type>
465 static Type* ConvertResultTo(__SearchResult<__ContactsContact>& searchResult);
467 static int GetAbPropertyId(void)
469 return _contacts_contact.address_book_id;
473 class __ContactsContactGroupRel
476 static const char* GetUri(void)
478 return _contacts_contact_grouprel._uri;
481 template<typename Type>
482 static Type* ConvertResultTo(__SearchResult<__ContactsContactGroupRel>& searchResult);
484 static int GetAbPropertyId(void)
486 return _contacts_contact_grouprel.address_book_id;
490 class __ContactsContactEmail
493 static const char* GetUri(void)
495 return _contacts_contact_email._uri;
498 template<typename Type>
499 static Type* ConvertResultTo(__SearchResult<__ContactsContactEmail>& searchResult);
501 static int GetAbPropertyId(void)
503 return _contacts_contact_email.address_book_id;
507 class __ContactsContactNumber
510 static const char* GetUri(void)
512 return _contacts_contact_number._uri;
515 template<typename Type>
516 static Type* ConvertResultTo(__SearchResult<__ContactsContactNumber>& searchResult);
518 static int GetAbPropertyId(void)
520 return _contacts_contact_number.address_book_id;
524 class __ContactsContactUpdatedInfo
527 static const char* GetUri(void)
529 return _contacts_contact_updated_info._uri;
532 template<typename Type>
533 static Type* ConvertResultTo(__SearchResult<__ContactsContactUpdatedInfo>& searchResult);
536 class __ContactsMyProfileUpdatedInfo
539 static const char* GetUri(void)
541 return _contacts_my_profile_updated_info._uri;
545 class __ContactsGroupUpdatedInfo
548 static const char* GetUri(void)
550 return _contacts_group_updated_info._uri;
553 template<typename Type>
554 static Type* ConvertResultTo(__SearchResult<__ContactsGroupUpdatedInfo>& searchResult);
557 class __ContactsGroupRelUpdatedInfo
560 static const char* GetUri(void)
562 return _contacts_grouprel_updated_info._uri;
565 template<typename Type>
566 static Type* ConvertResultTo(__SearchResult<__ContactsGroupRelUpdatedInfo>& searchResult);
569 class __ContactsUserProfile
572 static const char* GetUri(void)
574 return _contacts_my_profile._uri;
577 template<typename Type>
578 static Type* ConvertResultTo(__SearchResult<__ContactsUserProfile>& searchResult);
580 static int GetAbPropertyId(void)
582 return _contacts_my_profile.address_book_id;
587 class _AddressbookUtil
590 template<typename __View>
591 static __SearchResult<__View>* ExecuteQuery(const __Query<__View>& query, int offset = 0, int limit = 0)
593 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
594 __SearchResult<__View>* pSearchResult = null;
596 contacts_list_h list = null;
597 int ret = contacts_db_get_records_with_query(query.Get(), offset, limit, &list);
598 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
599 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] No matched item found. (%d)", GetErrorMessage(E_OBJ_NOT_FOUND), ret);
600 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred. (%d)", GetErrorMessage(E_SYSTEM), ret);
602 pSearchResult = new (std::nothrow) __SearchResult<__View>(list);
603 SysTryReturn(NID_SCL, pSearchResult != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
605 return pSearchResult;
608 template<typename __View>
609 static int GetCountWithQuery(const __Query<__View>& query)
611 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
614 int ret = contacts_db_get_count_with_query(query.Get(), &count);
615 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, -1, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
616 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred. (%d)", GetErrorMessage(E_SYSTEM), ret);
621 template<typename __View>
622 static int GetCount(void)
624 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
627 int ret = contacts_db_get_count(__View::GetUri(), &count);
628 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, -1, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
629 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred. (%d)", GetErrorMessage(E_SYSTEM), ret);
634 template<typename __View>
635 static __SearchResult<__View>* GetAllRecords(int offset = 0, int limit = 0)
637 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
639 __SearchResult<__View>* pSearchResult = null;
641 contacts_list_h list = null;
642 int ret = contacts_db_get_all_records(__View::GetUri(), offset, limit, &list);
643 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
644 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred. (%d)", GetErrorMessage(E_SYSTEM), ret);
646 pSearchResult = new (std::nothrow) __SearchResult<__View>(list);
647 SysTryReturn(NID_SCL, pSearchResult, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
649 return pSearchResult;
652 template<typename __View>
653 static __SearchResult<__View>* GetChangesByVersion(int id, int current, int& latest)
655 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
657 __SearchResult<__View>* pSearchResult = null;
659 contacts_list_h list = null;
660 int ret = contacts_db_get_changes_by_version(__View::GetUri(), id, current, &list, &latest);
661 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
662 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred. (%d)", GetErrorMessage(E_SYSTEM), ret);
664 pSearchResult = new (std::nothrow) __SearchResult<__View>(list);
665 SysTryReturn(NID_SCL, pSearchResult, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
667 return pSearchResult;
670 template<typename __View>
671 static int GetChangedItemCountByVersion(int id, int current, int& latest)
673 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
675 unsigned int count = 0;
676 contacts_list_h list = null;
677 int ret = contacts_db_get_changes_by_version(__View::GetUri(), id, current, &list, &latest);
678 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, -1, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
679 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred. (%d)", GetErrorMessage(E_SYSTEM), ret);
681 contacts_list_get_count(list, &count);
682 contacts_list_destroy(list, true);
687 template<typename __View>
688 static __SearchResult<__View>* Search(const Tizen::Base::String& keyword);
690 static Person* CreatePersonN(void);
692 template<typename __View, typename ChangeInfo>
693 static Tizen::Base::Collection::IList* SearchWithVersionN(int addressbookId, int currentVersion, int& latestVersion, Tizen::Base::Collection::IListT<AddressbookId>* pRwAbIdList = null)
695 SysTryReturn(NID_SCL, currentVersion >= 0, null, E_INVALID_ARG, "[%s] currentVersion %d must be greater that or equal 0.", GetErrorMessage(GetLastResult()), currentVersion);
697 std::unique_ptr<ChangeInfo> pChangeInfo(null);
701 std::unique_ptr<Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter> pList(new (std::nothrow) Tizen::Base::Collection::ArrayList());
702 SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
704 result r = pList->Construct();
705 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
707 std::unique_ptr<__SearchResult<__View> > pSearchResult(_AddressbookUtil::template GetChangesByVersion<__View>(addressbookId, currentVersion, latestVersion));
708 SysTryReturn(NID_SCL, pSearchResult != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
710 while (pSearchResult->MoveNext() == E_SUCCESS)
712 pChangeInfo.reset(__View::template ConvertResultTo<ChangeInfo>(*pSearchResult));
713 if (pRwAbIdList != null && (pRwAbIdList->Contains(pChangeInfo->GetAddressbookId())))
718 if (pChangeInfo == null)
720 SysTryReturn(NID_SCL, GetLastResult() != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
721 SysTryReturn(NID_SCL, GetLastResult() != E_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
727 r = pList->Add(*pChangeInfo);
728 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
730 pChangeInfo.release();
733 return pList.release();
737 template<typename __View, typename Data>
738 static Tizen::Base::Collection::IList* SearchWithQueryN(__Query<__View>& query, int offset = 0, int limit = 0)
740 std::unique_ptr<Data> pData(null);
742 std::unique_ptr<Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter> pDataList(new (std::nothrow) Tizen::Base::Collection::ArrayList());
743 SysTryReturn(NID_SCL, pDataList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
745 result r = pDataList->Construct();
746 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
748 std::unique_ptr<__SearchResult<__View> > pSearchResult(_AddressbookUtil::ExecuteQuery(query, offset, limit));
749 if (pSearchResult == null)
751 SysTryReturn(NID_SCL, GetLastResult() == E_OBJ_NOT_FOUND, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
755 return pDataList.release();
758 while (pSearchResult->MoveNext() == E_SUCCESS)
760 pData.reset(__View::template ConvertResultTo<Data>(*pSearchResult));
763 SysTryReturn(NID_SCL, GetLastResult() != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
764 SysTryReturn(NID_SCL, GetLastResult() != E_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
770 r = pDataList->Add(*pData);
771 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
776 return pDataList.release();
779 template<typename __View>
780 static int GetMatchedItemCountWithFilter(contacts_filter_h filterHandle)
784 if (filterHandle != null)
786 __Filter<__View> filter;
787 filter.Construct(filterHandle);
789 __Query<__View> query;
791 query.SetFilter(filter);
793 count = _AddressbookUtil::GetCountWithQuery(query);
797 __Query<__View> query;
800 count = _AddressbookUtil::GetCountWithQuery(query);
806 static ContactRecord* CreateContactRecordN(const char* pUri)
810 contacts_record_h recordHandle = null;
812 int ret = contacts_record_create(pUri, &recordHandle);
813 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
818 static result InsertContactRecordN(contacts_record_h recordHandle, int& recordId)
820 int ret = contacts_db_insert_record(recordHandle, &recordId);
821 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
822 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is insufficient. (%d)", GetErrorMessage(E_STORAGE_FULL), ret);
823 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred. (%d)", GetErrorMessage(E_SYSTEM), ret);
828 static result DeleteContactRecord(const char* pUri, int recordId)
830 int ret = contacts_db_delete_record(pUri, recordId);
831 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
832 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The record is not found. (%d)", GetErrorMessage(E_OBJ_NOT_FOUND), ret);
833 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred. (%d)", GetErrorMessage(E_SYSTEM), ret);
838 static result UpdateContactRecord(contacts_record_h recordHandle)
840 int ret = contacts_db_update_record(recordHandle);
841 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
842 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The Record is not found. (%d)", GetErrorMessage(E_OBJ_NOT_FOUND), ret);
843 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_FILE_NO_SPACE, E_STORAGE_FULL, E_STORAGE_FULL, "[%s] The storage is insufficient. (%d)", GetErrorMessage(E_STORAGE_FULL), ret);
844 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));
849 static result UpdateContactRecords(contacts_list_h listHandle)
851 int ret = contacts_db_update_records(listHandle);
852 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));
853 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_INVALID_PARAMETER, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used.", GetErrorMessage(E_INVALID_ARG));
854 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));
855 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
860 static ContactRecord* GetContactRecordN(const char* pUri, RecordId recordId)
864 contacts_record_h recordHandle = null;
866 int ret = contacts_db_get_record(pUri, recordId, &recordHandle);
867 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed. (%d)", GetErrorMessage(E_OUT_OF_MEMORY), ret);
868 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The record is not found. (%d)", GetErrorMessage(E_OBJ_NOT_FOUND), ret);
869 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred. (%d)", GetErrorMessage(E_SYSTEM), ret);
874 static Tizen::Base::Collection::IListT<AddressbookId>* GetRwAbIdListN(void);
875 static Tizen::Base::Collection::IListT<AddressbookId>* GetRoAbIdListN(void);
877 static result GetAbListN(Tizen::Base::Collection::IListT<AddressbookId>& rwAbIdList, Tizen::Base::Collection::IListT<AddressbookId>& roAbIdList);
879 template<typename __View>
880 static __Filter<__View>* GetRwAbFilterN(void)
882 std::unique_ptr< __Filter<__View> > pFilter(new (std::nothrow) __Filter<__View>);
883 SysTryReturn(NID_SCL, pFilter != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
885 Tizen::Base::Collection::ArrayListT<AddressbookId> rwAbIdList;
886 rwAbIdList.Construct();
888 Tizen::Base::Collection::ArrayListT<AddressbookId> roAbIdList;
889 roAbIdList.Construct();
891 GetAbListN(rwAbIdList, roAbIdList);
893 if (roAbIdList.GetCount() == 0)
895 // return null filter
896 return pFilter.release();
899 pFilter->Construct();
902 AddressbookId addressbookId = -1;
904 if (rwAbIdList.GetCount() < 5)
906 std::unique_ptr< Tizen::Base::Collection::IEnumeratorT<AddressbookId> > pEnum(rwAbIdList.GetEnumeratorN());
907 while (pEnum->MoveNext() == E_SUCCESS)
909 pEnum->GetCurrent(addressbookId);
912 pFilter->AddOperator(CONTACTS_FILTER_OPERATOR_OR);
919 pFilter->AddInt(__View::GetAbPropertyId(), CONTACTS_MATCH_EQUAL, addressbookId);
924 std::unique_ptr< Tizen::Base::Collection::IEnumeratorT<AddressbookId> > pEnum(roAbIdList.GetEnumeratorN());
925 while (pEnum->MoveNext() == E_SUCCESS)
927 pEnum->GetCurrent(addressbookId);
930 pFilter->AddOperator(CONTACTS_FILTER_OPERATOR_AND);
937 pFilter->AddInt(__View::GetAbPropertyId(), CONTACTS_MATCH_NOT_EQUAL, addressbookId);
941 return pFilter.release();
947 #endif //_FSCL_INTERNAL_ADDRESSBOOK_UTIL_H_