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_AddressbookUtil.h
19 * @brief This is the header file for the _AddressbookUtil class.
21 * This header file contains the declarations of the _AddressbookUtil class.
23 #ifndef _FSCL_INTERNAL_ADDRESSBOOK_UTIL_H_
24 #define _FSCL_INTERNAL_ADDRESSBOOK_UTIL_H_
26 #include <unique_ptr.h>
28 #include <FBaseColArrayList.h>
29 #include <FBaseColAllElementsDeleter.h>
30 #include "FScl_ContactDbConnector.h"
32 namespace Tizen { namespace Social
39 class __ContactsRecordHandle
42 __ContactsRecordHandle(contacts_record_h handle)
47 ~__ContactsRecordHandle(void)
51 contacts_record_destroy(__handle, true);
55 void Reset(contacts_record_h handle)
60 contacts_record_destroy(__handle, true);
66 contacts_record_h Release(void)
68 contacts_record_h handle = __handle;
75 contacts_record_h __handle;
78 template<typename __View>
82 __SearchResult(contacts_list_h list)
83 :__list(list), __isFirst(true)
88 int GetCount(void) const
92 unsigned int count = 0;
93 contacts_list_get_count(__list, &count);
101 contacts_record_h GetCurrentRecord(void) const
105 contacts_record_h record = null;
106 int ret = contacts_list_get_current_record_p(__list, &record);
107 if (ret != CONTACTS_ERROR_NONE)
118 result MoveNext(void)
122 int ret = CONTACTS_ERROR_NONE;
126 ret = contacts_list_next(__list);
127 if (ret != CONTACTS_ERROR_NONE)
129 return E_INVALID_OPERATION;
143 return E_INVALID_OPERATION;
147 return E_INVALID_OPERATION;
151 ~__SearchResult(void)
155 contacts_list_destroy(__list, true);
160 contacts_list_h __list;
164 template<typename __View>
169 :__filterHandle(null)
170 ,__destroyFilterHandle(false)
174 result Construct(void)
176 contacts_filter_h filterHandle = null;
177 int ret = contacts_filter_create(__View::GetUri(), &filterHandle);
178 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
180 __filterHandle = filterHandle;
181 __destroyFilterHandle = true;
186 result Construct(contacts_filter_h filterHandle)
188 __filterHandle = filterHandle;
189 __destroyFilterHandle = false;
194 contacts_filter_h Get(void) const
196 return __filterHandle;
199 result AddString(unsigned int propertyId, contacts_match_str_flag_e match, const char* value)
201 int ret = contacts_filter_add_str(__filterHandle, propertyId, match, value);
202 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
207 result AddInt(unsigned int propertyId, contacts_match_int_flag_e match, int value)
209 int ret = contacts_filter_add_int(__filterHandle, propertyId, match, value);
210 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
215 result AddLongLong(unsigned int propertyId, contacts_match_int_flag_e match, long long int value)
217 int ret = contacts_filter_add_lli(__filterHandle, propertyId, match, value);
218 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
223 result AddDouble(unsigned int propertyId, contacts_match_int_flag_e match, double value)
225 int ret = contacts_filter_add_double(__filterHandle, propertyId, match, value);
226 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
231 result AddBool(unsigned int propertyId, bool value)
233 int ret = contacts_filter_add_bool(__filterHandle, propertyId, value);
234 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
239 result AddOperator(contacts_filter_operator_e type)
241 int ret = contacts_filter_add_operator(__filterHandle, type);
242 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
247 result AddFilter(__Filter<__View>& filter)
249 contacts_filter_h handle = filter.Get();
250 int ret = contacts_filter_add_filter(__filterHandle, handle);
251 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
258 if (__filterHandle != null && __destroyFilterHandle)
260 contacts_filter_destroy(__filterHandle);
265 contacts_filter_h __filterHandle;
266 bool __destroyFilterHandle;
269 template<typename __View>
278 result Construct(void)
280 contacts_query_h queryHandle = null;
281 int ret = contacts_query_create(__View::GetUri(), &queryHandle);
282 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
284 __queryHandle = queryHandle;
289 contacts_query_h Get(void) const
291 return __queryHandle;
295 result SetProjection(unsigned int propertyIds[], int count)
297 int ret = contacts_query_set_projection(__queryHandle, propertyIds, count);
298 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
303 result SetDistinct(bool set)
305 int ret = contacts_query_set_distinct(__queryHandle, set);
306 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
311 result SetFilter(__Filter<__View>& filter)
313 contacts_filter_h filterHandle = filter.Get();
315 if (filterHandle != null)
317 int ret = contacts_query_set_filter(__queryHandle, filterHandle);
318 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
324 result SetSort(unsigned int propertyId, bool ascending)
326 int ret = contacts_query_set_sort(__queryHandle, propertyId, ascending);
327 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
334 if (__queryHandle != null)
336 contacts_query_destroy(__queryHandle);
341 contacts_query_h __queryHandle;
345 class __ContactsAddressbook
348 static const char* GetUri(void)
350 return _contacts_address_book._uri;
353 template<typename Type>
354 static Type* ConvertResultTo(__SearchResult<__ContactsAddressbook>& searchResult);
358 class __ContactsPerson
361 static const char* GetUri(void)
363 return _contacts_person._uri;
366 template<typename Type>
367 static Type* ConvertResultTo(__SearchResult<__ContactsPerson>& searchResult);
369 template<typename Type>
370 static Type* ConvertHandleTo(contacts_record_h recordHandle);
374 class __ContactsPersonEmail
377 static const char* GetUri(void)
379 return _contacts_person_email._uri;
383 class __ContactsPersonGroupRel
386 static const char* GetUri(void)
388 return _contacts_person_grouprel._uri;
391 template<typename Type>
392 static Type* ConvertHandleTo(contacts_record_h recordHandle);
394 template<typename Type>
395 static Type* ConvertResultTo(__SearchResult<__ContactsPersonGroupRel>& searchResult);
399 class __ContactsPersonNumber
402 static const char* GetUri(void)
404 return _contacts_person_number._uri;
409 class __ContactsGroup
412 static const char* GetUri(void)
414 return _contacts_group._uri;
417 template<typename Type>
418 static Type* ConvertResultTo(__SearchResult<__ContactsGroup>& searchResult);
421 class __ContactsGroupRelation
424 static const char* GetUri(void)
426 return _contacts_group_relation._uri;
429 template<typename Type>
430 static Type* ConvertResultTo(__SearchResult<__ContactsGroupRelation>& searchResult);
433 class __ContactsContact
436 static const char* GetUri(void)
438 return _contacts_contact._uri;
441 template<typename Type>
442 static Type* ConvertResultTo(__SearchResult<__ContactsContact>& searchResult);
445 class __ContactsContactGroupRel
448 static const char* GetUri(void)
450 return _contacts_contact_grouprel._uri;
453 template<typename Type>
454 static Type* ConvertResultTo(__SearchResult<__ContactsContactGroupRel>& searchResult);
457 class __ContactsContactEmail
460 static const char* GetUri(void)
462 return _contacts_contact_email._uri;
465 template<typename Type>
466 static Type* ConvertResultTo(__SearchResult<__ContactsContactEmail>& searchResult);
469 class __ContactsContactNumber
472 static const char* GetUri(void)
474 return _contacts_contact_number._uri;
477 template<typename Type>
478 static Type* ConvertResultTo(__SearchResult<__ContactsContactNumber>& searchResult);
481 class __ContactsContactUpdatedInfo
484 static const char* GetUri(void)
486 return _contacts_contact_updated_info._uri;
489 template<typename Type>
490 static Type* ConvertResultTo(__SearchResult<__ContactsContactUpdatedInfo>& searchResult);
493 class __ContactsMyProfileUpdatedInfo
496 static const char* GetUri(void)
498 return _contacts_my_profile_updated_info._uri;
502 class __ContactsGroupUpdatedInfo
505 static const char* GetUri(void)
507 return _contacts_group_updated_info._uri;
510 template<typename Type>
511 static Type* ConvertResultTo(__SearchResult<__ContactsGroupUpdatedInfo>& searchResult);
514 class __ContactsGroupRelUpdatedInfo
517 static const char* GetUri(void)
519 return _contacts_grouprel_updated_info._uri;
522 template<typename Type>
523 static Type* ConvertResultTo(__SearchResult<__ContactsGroupRelUpdatedInfo>& searchResult);
526 class __ContactsUserProfile
529 static const char* GetUri(void)
531 return _contacts_my_profile._uri;
534 template<typename Type>
535 static Type* ConvertResultTo(__SearchResult<__ContactsUserProfile>& searchResult);
539 class _AddressbookUtil
542 template<typename __View>
543 static __SearchResult<__View>* ExecuteQuery(const __Query<__View>& query, int offset = 0, int limit = 0)
545 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
546 __SearchResult<__View>* pSearchResult = null;
548 contacts_list_h list = null;
549 int ret = contacts_db_get_records_with_query(query.Get(), offset, limit, &list);
550 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
551 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] No matched item found.", GetErrorMessage(E_OBJ_NOT_FOUND));
552 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
554 pSearchResult = new (std::nothrow) __SearchResult<__View>(list);
555 SysTryReturn(NID_SCL, pSearchResult != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
557 return pSearchResult;
560 template<typename __View>
561 static int GetCountWithQuery(const __Query<__View>& query)
563 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
566 int ret = contacts_db_get_count_with_query(query.Get(), &count);
567 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, -1, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
568 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
573 template<typename __View>
574 static int GetCount(void)
576 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
579 int ret = contacts_db_get_count(__View::GetUri(), &count);
580 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, -1, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
581 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
586 template<typename __View>
587 static __SearchResult<__View>* GetAllRecords(int offset = 0, int limit = 0)
589 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
591 __SearchResult<__View>* pSearchResult = null;
593 contacts_list_h list = null;
594 int ret = contacts_db_get_all_records(__View::GetUri(), offset, limit, &list);
595 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
596 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
598 pSearchResult = new (std::nothrow) __SearchResult<__View>(list);
599 SysTryReturn(NID_SCL, pSearchResult, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
601 return pSearchResult;
604 template<typename __View>
605 static __SearchResult<__View>* GetChangesByVersion(int id, int current, int& latest)
607 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
609 __SearchResult<__View>* pSearchResult = null;
611 contacts_list_h list = null;
612 int ret = contacts_db_get_changes_by_version(__View::GetUri(), id, current, &list, &latest);
613 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
614 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
616 pSearchResult = new (std::nothrow) __SearchResult<__View>(list);
617 SysTryReturn(NID_SCL, pSearchResult, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
619 return pSearchResult;
622 template<typename __View>
623 static int GetChangedItemCountByVersion(int id, int current, int& latest)
625 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, -1, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
627 unsigned int count = 0;
628 contacts_list_h list = null;
629 int ret = contacts_db_get_changes_by_version(__View::GetUri(), id, current, &list, &latest);
630 SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, -1, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
631 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, -1, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
633 contacts_list_get_count(list, &count);
634 contacts_list_destroy(list, true);
639 template<typename __View>
640 static __SearchResult<__View>* Search(const Tizen::Base::String& keyword);
642 static Person* CreatePersonN(void);
644 template<typename __View, typename ChangeInfo>
645 static Tizen::Base::Collection::IList* SearchWithVersionN(int addressbookId, int currentVersion, int& latestVersion)
647 SysTryReturn(NID_SCL, currentVersion >= 0, null, E_INVALID_ARG, "[%s] currentVersion %d must be greater that or equal 0.", GetErrorMessage(GetLastResult()), currentVersion);
649 std::unique_ptr<ChangeInfo> pChangeInfo(null);
653 std::unique_ptr<Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter> pList(new (std::nothrow) Tizen::Base::Collection::ArrayList());
654 SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
656 result r = pList->Construct();
657 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
659 std::unique_ptr<__SearchResult<__View> > pSearchResult(_AddressbookUtil::template GetChangesByVersion<__View>(addressbookId, currentVersion, latestVersion));
660 SysTryReturn(NID_SCL, pSearchResult != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
662 while (pSearchResult->MoveNext() == E_SUCCESS)
664 pChangeInfo.reset(__View::template ConvertResultTo<ChangeInfo>(*pSearchResult));
665 if (pChangeInfo == null)
667 SysTryReturn(NID_SCL, GetLastResult() != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
668 SysTryReturn(NID_SCL, GetLastResult() != E_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
674 r = pList->Add(*pChangeInfo);
675 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
677 pChangeInfo.release();
680 return pList.release();
684 template<typename __View, typename Data>
685 static Tizen::Base::Collection::IList* SearchWithQueryN(__Query<__View>& query, int offset = 0, int limit = 0)
687 std::unique_ptr<Data> pData(null);
689 std::unique_ptr<Tizen::Base::Collection::ArrayList, Tizen::Base::Collection::AllElementsDeleter> pDataList(new (std::nothrow) Tizen::Base::Collection::ArrayList());
690 SysTryReturn(NID_SCL, pDataList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
692 result r = pDataList->Construct();
693 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
695 std::unique_ptr<__SearchResult<__View> > pSearchResult(_AddressbookUtil::ExecuteQuery(query, offset, limit));
696 if (pSearchResult == null)
698 SysTryReturn(NID_SCL, GetLastResult() == E_OBJ_NOT_FOUND, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
702 return pDataList.release();
705 while (pSearchResult->MoveNext() == E_SUCCESS)
707 pData.reset(__View::template ConvertResultTo<Data>(*pSearchResult));
710 SysTryReturn(NID_SCL, GetLastResult() != E_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
711 SysTryReturn(NID_SCL, GetLastResult() != E_SYSTEM, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
717 r = pDataList->Add(*pData);
718 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
723 return pDataList.release();
726 template<typename __View>
727 static int GetMatchedItemCountWithFilter(contacts_filter_h filterHandle)
729 __Filter<__View> filter;
730 filter.Construct(filterHandle);
732 __Query<__View> query;
734 query.SetFilter(filter);
736 int count = _AddressbookUtil::GetCountWithQuery(query);
744 #endif //_FSCL_INTERNAL_ADDRESSBOOK_UTIL_H_