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_AddressbookFilterImpl.cpp
18 * @brief This is the implementation for _AddressbookFilterImpl class.
20 * This file contains definitions of @e _AddressbookFilterImpl class.
23 #include <unique_ptr.h>
24 #include <FBaseSysLog.h>
25 #include <FSclAddressbookFilter.h>
26 #include <FBase_StringConverter.h>
27 #include "FScl_ContactDbConnector.h"
28 #include "FScl_AddressbookFilterImpl.h"
30 using namespace Tizen::App;
31 using namespace Tizen::Base;
33 namespace Tizen { namespace Social
36 // AB_FI_TYPE_ADDRESSBOOK
37 // The entries must be ordered same as AddressbookFilterProperty
38 const _AddressbookFilterImpl::__FilterPropertyInfoEntry _AddressbookFilterImpl::__addressbookFilterPropertyInfos[] =
40 {AB_FI_PR_ADDRESSBOOK_ID, _contacts_address_book.id, DATA_TYPE_INT},
41 {AB_FI_PR_ACCOUNT_ID, _contacts_address_book.account_id, DATA_TYPE_INT},
42 {AB_FI_PR_NAME, _contacts_address_book.name, DATA_TYPE_STRING},
46 // The entries must be ordered same as PersonFilterProperty
47 const _AddressbookFilterImpl::__FilterPropertyInfoEntry _AddressbookFilterImpl::__personFilterPropertyInfos[] =
49 {PERSON_FI_PR_PERSON_ID, _contacts_person_grouprel.person_id, DATA_TYPE_INT},
50 {PERSON_FI_PR_DISPLAY_NAME, _contacts_person_grouprel.display_name, DATA_TYPE_STRING},
51 {PERSON_FI_PR_HAS_PHONE, _contacts_person_grouprel.has_phonenumber, DATA_TYPE_BOOL},
52 {PERSON_FI_PR_HAS_EMAIL, _contacts_person_grouprel.has_email, DATA_TYPE_BOOL},
53 {PERSON_FI_PR_IS_FAVORITE, _contacts_person_grouprel.is_favorite, DATA_TYPE_BOOL},
54 {PERSON_FI_PR_CATEGORY_ID, _contacts_person_grouprel.group_id, DATA_TYPE_INT},
55 {PERSON_FI_PR_ADDRESSBOOK_ID, _contacts_person_grouprel.address_book_id, DATA_TYPE_INT},
59 // The entries must be ordered same as ContactFilterProperty
60 const _AddressbookFilterImpl::__FilterPropertyInfoEntry _AddressbookFilterImpl::__contactFilterPropertyInfos[] =
62 {CONTACT_FI_PR_CONTACT_ID, _contacts_contact.id, DATA_TYPE_INT},
63 {CONTACT_FI_PR_ADDRESSBOOK_ID, _contacts_contact.address_book_id, DATA_TYPE_INT},
64 {CONTACT_FI_PR_PERSON_ID, _contacts_contact.person_id, DATA_TYPE_INT},
65 {CONTACT_FI_PR_DISPLAY_NAME, _contacts_contact.display_name, DATA_TYPE_STRING},
66 {CONTACT_FI_PR_HAS_PHONE, _contacts_contact.has_phonenumber, DATA_TYPE_BOOL},
67 {CONTACT_FI_PR_HAS_EMAIL, _contacts_contact.has_email, DATA_TYPE_BOOL},
70 // AB_FI_TYPE_CATEGORY
71 // The entries must be ordered same as CategoryFilterProperty
72 const _AddressbookFilterImpl::__FilterPropertyInfoEntry _AddressbookFilterImpl::__categoryFilterPropertyInfos[] =
74 {CATEGORY_FI_PR_CATEGORY_ID, _contacts_group.id, DATA_TYPE_INT},
75 {CATEGORY_FI_PR_ADDRESSBOOK_ID, _contacts_group.address_book_id, DATA_TYPE_INT},
76 {CATEGORY_FI_PR_NAME, _contacts_group.name, DATA_TYPE_STRING},
79 // AB_FI_TYPE_PHONE_CONTACT
80 // The entries must be ordered same as PhoneContactFilterProperty
81 const _AddressbookFilterImpl::__FilterPropertyInfoEntry _AddressbookFilterImpl::__phoneContactFilterPropertyInfos[] =
83 {PHONE_CONTACT_FI_PR_CONTACT_ID, _contacts_contact_number.contact_id, DATA_TYPE_INT},
84 {PHONE_CONTACT_FI_PR_ADDRESSBOOK_ID, _contacts_contact_number.address_book_id, DATA_TYPE_INT},
85 {PHONE_CONTACT_FI_PR_PERSON_ID, _contacts_contact_number.person_id, DATA_TYPE_INT},
86 {PHONE_CONTACT_FI_PR_DISPLAY_NAME, _contacts_contact_number.display_name, DATA_TYPE_STRING},
87 {PHONE_CONTACT_FI_PR_PHONE, _contacts_contact_number.number, DATA_TYPE_STRING},
90 // AB_FI_TYPE_EMAIL_CONTACT
91 // The entries must be ordered same as EmailContactFilterProperty
92 const _AddressbookFilterImpl::__FilterPropertyInfoEntry _AddressbookFilterImpl::__emailContactFilterPropertyInfos[] =
94 {EMAIL_CONTACT_FI_PR_CONTACT_ID, _contacts_contact_email.contact_id, DATA_TYPE_INT},
95 {EMAIL_CONTACT_FI_PR_ADDRESSBOOK_ID, _contacts_contact_email.address_book_id, DATA_TYPE_INT},
96 {EMAIL_CONTACT_FI_PR_PERSON_ID, _contacts_contact_email.person_id, DATA_TYPE_INT},
97 {EMAIL_CONTACT_FI_PR_DISPLAY_NAME, _contacts_contact_email.display_name, DATA_TYPE_STRING},
98 {EMAIL_CONTACT_FI_PR_EMAIL, _contacts_contact_email.email, DATA_TYPE_STRING},
101 const _AddressbookFilterImpl::__FilterInfoEntry _AddressbookFilterImpl::__filterInfos[] =
103 {AB_FI_TYPE_ADDRESSBOOK, _contacts_address_book._uri, AB_FI_PR_ADDRESSBOOK_ID, sizeof(__addressbookFilterPropertyInfos)/sizeof(_AddressbookFilterImpl::__FilterPropertyInfoEntry), __addressbookFilterPropertyInfos},
104 {AB_FI_TYPE_PERSON, _contacts_person_grouprel._uri, PERSON_FI_PR_PERSON_ID, sizeof(__personFilterPropertyInfos)/sizeof(_AddressbookFilterImpl::__FilterPropertyInfoEntry), __personFilterPropertyInfos},
105 {AB_FI_TYPE_CONTACT, _contacts_contact._uri, CONTACT_FI_PR_CONTACT_ID, sizeof(__contactFilterPropertyInfos)/sizeof(_AddressbookFilterImpl::__FilterPropertyInfoEntry), __contactFilterPropertyInfos},
106 {AB_FI_TYPE_CATEGORY, _contacts_group._uri, CATEGORY_FI_PR_CATEGORY_ID, sizeof(__categoryFilterPropertyInfos)/sizeof(_AddressbookFilterImpl::__FilterPropertyInfoEntry), __categoryFilterPropertyInfos},
107 {AB_FI_TYPE_PHONE_CONTACT, _contacts_contact_number._uri, PHONE_CONTACT_FI_PR_CONTACT_ID, sizeof(__phoneContactFilterPropertyInfos)/sizeof(_AddressbookFilterImpl::__FilterPropertyInfoEntry), __phoneContactFilterPropertyInfos},
108 {AB_FI_TYPE_EMAIL_CONTACT, _contacts_contact_email._uri, EMAIL_CONTACT_FI_PR_CONTACT_ID, sizeof(__emailContactFilterPropertyInfos)/sizeof(_AddressbookFilterImpl::__FilterPropertyInfoEntry), __emailContactFilterPropertyInfos},
112 _AddressbookFilterImpl::IsValidProperty(AddressbookFilterType filterType, unsigned long property)
114 int filterInfoCount = sizeof(__filterInfos)/sizeof(__FilterInfoEntry);
116 if (filterType < 0 || filterType > (filterInfoCount - 1))
121 unsigned long baseIndex = __filterInfos[filterType].baseIndex;
122 int propertyInfoCount = __filterInfos[filterType].propertyInfoCount;
124 if (property < baseIndex || property >= (baseIndex + propertyInfoCount))
132 // IsValidProperty must be called before calling this. If IsValidProperty returns false
133 // this should not be called.
135 _AddressbookFilterImpl::GetUriFromType(AddressbookFilterType filterType)
137 return __filterInfos[filterType].pViewUri;
140 // IsValidProperty must be called before calling this. If IsValidProperty returns false
141 // this should not be called.
143 _AddressbookFilterImpl::GetViewPropertyId(AddressbookFilterType filterType, unsigned long property)
145 const __FilterPropertyInfoEntry* propertyInfos = __filterInfos[filterType].propertyInfos;
146 unsigned long baseIndex = __filterInfos[filterType].baseIndex;
148 return propertyInfos[property - baseIndex].viewPropertyId;
151 // IsValidProperty must be called before calling this. If IsValidProperty returns false
152 // this should not be called.
154 _AddressbookFilterImpl::GetPropertyType(AddressbookFilterType filterType, unsigned long property)
156 const __FilterPropertyInfoEntry* propertyInfos = __filterInfos[filterType].propertyInfos;
157 unsigned long baseIndex = __filterInfos[filterType].baseIndex;
159 return propertyInfos[property - baseIndex].dataType;
162 _AddressbookFilterImpl::_AddressbookFilterImpl(AddressbookFilterType type)
163 : __filterHandle(null)
167 contacts_filter_h filterHandle = null;
168 const char* pUri = GetUriFromType(type);
170 SysTryReturnVoidResult(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
172 int ret = contacts_filter_create(pUri, &filterHandle);
173 SysTryReturnVoidResult(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, "[%s ] Memory allocation failed.", GetErrorMessage(GetLastResult()));
175 __filterHandle = filterHandle;
178 _AddressbookFilterImpl::~_AddressbookFilterImpl(void)
180 if (__filterHandle != null)
182 contacts_filter_destroy(__filterHandle);
187 _AddressbookFilterImpl::Equals(const Tizen::Base::Object& rhs) const
189 const _AddressbookFilterImpl* pAddressbookFilter = dynamic_cast<const _AddressbookFilterImpl*>(&rhs);
190 if (this == pAddressbookFilter)
199 _AddressbookFilterImpl::GetHashCode(void) const
201 return (int)__filterHandle;
204 contacts_match_int_flag_e
205 _AddressbookFilterImpl::GetMatchIntFlag(FilterComparisonOperator comparisonOperator)
207 contacts_match_int_flag_e match = CONTACTS_MATCH_NONE;
208 switch(comparisonOperator)
210 case FI_CMP_OP_EQUAL:
211 match = CONTACTS_MATCH_EQUAL;
213 case FI_CMP_OP_LESS_THAN:
214 match = CONTACTS_MATCH_GREATER_THAN;
216 case FI_CMP_OP_LESS_THAN_OR_EQUAL:
217 match = CONTACTS_MATCH_GREATER_THAN_OR_EQUAL;
219 case FI_CMP_OP_GREATER_THAN:
220 match = CONTACTS_MATCH_LESS_THAN;
222 case FI_CMP_OP_GREATER_THAN_OR_EQUAL:
223 match = CONTACTS_MATCH_LESS_THAN_OR_EQUAL;
225 case FI_CMP_OP_IS_NULL:
226 match = CONTACTS_MATCH_NONE;
229 match = CONTACTS_MATCH_NONE;
235 contacts_match_str_flag_e
236 _AddressbookFilterImpl::GetMatchStrFlag(FilterStringOperator stringOperstor)
238 contacts_match_str_flag_e match = CONTACTS_MATCH_EXISTS;
239 switch(stringOperstor)
241 case FI_STR_OP_EQUAL:
242 match = CONTACTS_MATCH_EXACTLY;
244 case FI_STR_OP_FULL_STRING:
245 match = CONTACTS_MATCH_FULLSTRING;
247 case FI_STR_OP_START_WITH:
248 match = CONTACTS_MATCH_STARTSWITH;
250 case FI_STR_OP_END_WITH:
251 match = CONTACTS_MATCH_ENDSWITH;
253 case FI_STR_OP_CONTAIN:
254 match = CONTACTS_MATCH_CONTAINS;
256 case FI_STR_OP_IS_NOT_NULL:
257 match = CONTACTS_MATCH_EXISTS;
260 match = CONTACTS_MATCH_EXISTS;
268 _AddressbookFilterImpl::AppendInt(FilterConjunctiveOperator conjunctiveOperator, unsigned long filterProperty, FilterComparisonOperator comparisonOperator, int value)
270 SysTryReturn(NID_SCL, IsValidProperty(__filterType, filterProperty), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The specified filter property is invalid.", GetErrorMessage(E_INVALID_ARG));
271 SysTryReturn(NID_SCL, GetPropertyType(__filterType, filterProperty) == DATA_TYPE_INT, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the specified filter property %d must be int.", GetErrorMessage(E_INVALID_ARG), filterProperty);
272 SysTryReturn(NID_SCL, !(__isEmpty && (conjunctiveOperator == FI_CONJ_OP_AND || conjunctiveOperator == FI_CONJ_OP_OR)),\
273 E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be FI_CONJ_OP_NONE if there is no filtering expression or filter that has been appended before",\
274 GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
275 SysTryReturn(NID_SCL, !(!__isEmpty && (conjunctiveOperator == FI_CONJ_OP_NONE)),\
276 E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be either of FI_CONJ_OP_AND or FI_CONJ_OP_OR, if there is a filtering expression or filter that has been appended before.",\
277 GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
280 unsigned int id = _AddressbookFilterImpl::GetViewPropertyId(__filterType, filterProperty);
281 contacts_match_int_flag_e match = GetMatchIntFlag(comparisonOperator);
285 contacts_filter_operator_e filterOperator = conjunctiveOperator == FI_CONJ_OP_AND ? CONTACTS_FILTER_OPERATOR_AND : CONTACTS_FILTER_OPERATOR_OR;
287 contacts_filter_add_operator(__filterHandle, filterOperator);
290 int ret = contacts_filter_add_int(__filterHandle, id, match, value);
291 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
299 _AddressbookFilterImpl::AppendBool(FilterConjunctiveOperator conjunctiveOperator, unsigned long filterProperty, FilterComparisonOperator comparisonOperator, bool value)
301 SysTryReturn(NID_SCL, IsValidProperty(__filterType, filterProperty), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The specified filter property is invalid.", GetErrorMessage(E_INVALID_ARG));
302 SysTryReturn(NID_SCL, GetPropertyType(__filterType, filterProperty) == DATA_TYPE_BOOL, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the specified filter property %d must be bool.", GetErrorMessage(E_INVALID_ARG), filterProperty);
303 SysTryReturn(NID_SCL, !(__isEmpty && (conjunctiveOperator == FI_CONJ_OP_AND || conjunctiveOperator == FI_CONJ_OP_OR)),\
304 E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be FI_CONJ_OP_NONE if there is no filtering expression or filter that has been appended before",\
305 GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
306 SysTryReturn(NID_SCL, !(!__isEmpty && conjunctiveOperator == FI_CONJ_OP_NONE),\
307 E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be either of FI_CONJ_OP_AND or FI_CONJ_OP_OR, if there is a filtering expression or filter that has been appended before.",\
308 GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
309 SysTryReturn(NID_SCL, comparisonOperator == FI_CMP_OP_EQUAL, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. comparisonOperator %d must be FI_CMP_OP_EQUAL.", GetErrorMessage(E_INVALID_ARG), comparisonOperator);
311 unsigned int viewPropertyId = _AddressbookFilterImpl::GetViewPropertyId(__filterType, filterProperty);
315 contacts_filter_operator_e filterOperator = conjunctiveOperator == FI_CONJ_OP_AND ? CONTACTS_FILTER_OPERATOR_AND : CONTACTS_FILTER_OPERATOR_OR;
317 contacts_filter_add_operator(__filterHandle, filterOperator);
321 int ret = contacts_filter_add_bool(__filterHandle, viewPropertyId, value);
322 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
330 _AddressbookFilterImpl::AppendString(FilterConjunctiveOperator conjunctiveOperator, unsigned long filterProperty, FilterStringOperator comparisonOperator, const Tizen::Base::String& value)
332 SysTryReturn(NID_SCL, IsValidProperty(__filterType, filterProperty), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The specified filter property is invalid.", GetErrorMessage(E_INVALID_ARG));
333 SysTryReturn(NID_SCL, GetPropertyType(__filterType, filterProperty) == DATA_TYPE_STRING, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the specified filter property %d must be String.", GetErrorMessage(E_INVALID_ARG), filterProperty);
334 SysTryReturn(NID_SCL, !(__isEmpty && (conjunctiveOperator == FI_CONJ_OP_AND || conjunctiveOperator == FI_CONJ_OP_OR)),\
335 E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be FI_CONJ_OP_NONE if there is no filtering expression or filter that has been appended before",\
336 GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
337 SysTryReturn(NID_SCL, !(!__isEmpty && conjunctiveOperator == FI_CONJ_OP_NONE),\
338 E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be either of FI_CONJ_OP_AND or FI_CONJ_OP_OR, if there is a filtering expression or filter that has been appended before.",\
339 GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
342 unsigned int viewPropertyId = _AddressbookFilterImpl::GetViewPropertyId(__filterType, filterProperty);
343 contacts_match_str_flag_e match = GetMatchStrFlag(comparisonOperator);
344 std::unique_ptr<char[]> pCharArray( _StringConverter::CopyToCharArrayN(value));
345 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
349 contacts_filter_operator_e filterOperator = conjunctiveOperator == FI_CONJ_OP_AND ? CONTACTS_FILTER_OPERATOR_AND : CONTACTS_FILTER_OPERATOR_OR;
351 contacts_filter_add_operator(__filterHandle, filterOperator);
354 int ret = contacts_filter_add_str(__filterHandle, viewPropertyId, match, pCharArray.get());
355 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
364 _AddressbookFilterImpl::AppendFilter(FilterConjunctiveOperator conjunctiveOperator, const _AddressbookFilterImpl& filter)
366 SysTryReturn(NID_SCL, !filter.IsEmpty(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The specified filter should not be empty.", GetErrorMessage(E_INVALID_ARG));
367 SysTryReturn(NID_SCL, __filterType == filter.__filterType, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The type of the filter must be same with the type of this filter.", GetErrorMessage(E_INVALID_ARG));
368 SysTryReturn(NID_SCL, !(__isEmpty && (conjunctiveOperator == FI_CONJ_OP_AND || conjunctiveOperator == FI_CONJ_OP_OR)),\
369 E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be FI_CONJ_OP_NONE if there is no filtering expression or filter that has been appended before",\
370 GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
371 SysTryReturn(NID_SCL, !(!__isEmpty && conjunctiveOperator == FI_CONJ_OP_NONE),\
372 E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. conjunctiveOperator %d must be either of FI_CONJ_OP_AND or FI_CONJ_OP_OR, if there is a filtering expression or filter that has been appended before.",\
373 GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
377 contacts_filter_operator_e filterOperator = conjunctiveOperator == FI_CONJ_OP_AND ? CONTACTS_FILTER_OPERATOR_AND : CONTACTS_FILTER_OPERATOR_OR;
378 contacts_filter_add_operator(__filterHandle, filterOperator);
381 int ret = contacts_filter_add_filter(__filterHandle, filter.__filterHandle);
382 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
390 _AddressbookFilterImpl::GetFilterHandle(void) const
394 return __filterHandle;
400 AddressbookFilterType
401 _AddressbookFilterImpl::GetType(void) const
407 _AddressbookFilterImpl::IsEmpty(void) const
412 const _AddressbookFilterImpl*
413 _AddressbookFilterImpl::GetInstance(const AddressbookFilter& filter)
415 return filter.__pAddressbookFilterImpl;
418 _AddressbookFilterImpl*
419 _AddressbookFilterImpl::GetInstance(AddressbookFilter& filter)
421 return filter.__pAddressbookFilterImpl;