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_AddressbookFilterImpl.cpp
19 * @brief This is the implementation for _AddressbookFilterImpl class.
21 * This file contains definitions of @e _AddressbookFilterImpl class.
24 #include <unique_ptr.h>
25 #include <FBaseSysLog.h>
26 #include <FSclAddressbookFilter.h>
27 #include <FBase_StringConverter.h>
28 #include "FScl_ContactDbConnector.h"
29 #include "FScl_AddressbookFilterImpl.h"
31 using namespace Tizen::App;
32 using namespace Tizen::Base;
34 namespace Tizen { namespace Social
37 // AB_FI_TYPE_ADDRESSBOOK
38 // The entries must be ordered same as AddressbookFilterProperty
39 const _AddressbookFilterImpl::__FilterPropertyInfoEntry _AddressbookFilterImpl::__addressbookFilterPropertyInfos[] =
41 {AB_FI_PR_ADDRESSBOOK_ID, _contacts_address_book.id, DATA_TYPE_INT},
42 {AB_FI_PR_ACCOUNT_ID, _contacts_address_book.account_id, DATA_TYPE_INT},
43 {AB_FI_PR_NAME, _contacts_address_book.name, DATA_TYPE_STRING},
47 // The entries must be ordered same as PersonFilterProperty
48 const _AddressbookFilterImpl::__FilterPropertyInfoEntry _AddressbookFilterImpl::__personFilterPropertyInfos[] =
50 {PERSON_FI_PR_PERSON_ID, _contacts_person_grouprel.person_id, DATA_TYPE_INT},
51 {PERSON_FI_PR_DISPLAY_NAME, _contacts_person_grouprel.display_name, DATA_TYPE_STRING},
52 {PERSON_FI_PR_HAS_PHONE, _contacts_person_grouprel.has_phonenumber, DATA_TYPE_BOOL},
53 {PERSON_FI_PR_HAS_EMAIL, _contacts_person_grouprel.has_email, DATA_TYPE_BOOL},
54 {PERSON_FI_PR_IS_FAVORITE, _contacts_person_grouprel.is_favorite, DATA_TYPE_BOOL},
55 {PERSON_FI_PR_CATEGORY_ID, _contacts_person_grouprel.group_id, DATA_TYPE_INT},
56 {PERSON_FI_PR_ADDRESSBOOK_ID, _contacts_person_grouprel.address_book_id, DATA_TYPE_INT},
60 // The entries must be ordered same as ContactFilterProperty
61 const _AddressbookFilterImpl::__FilterPropertyInfoEntry _AddressbookFilterImpl::__contactFilterPropertyInfos[] =
63 {CONTACT_FI_PR_CONTACT_ID, _contacts_contact.id, DATA_TYPE_INT},
64 {CONTACT_FI_PR_ADDRESSBOOK_ID, _contacts_contact.address_book_id, DATA_TYPE_INT},
65 {CONTACT_FI_PR_PERSON_ID, _contacts_contact.person_id, DATA_TYPE_INT},
66 {CONTACT_FI_PR_DISPLAY_NAME, _contacts_contact.display_name, DATA_TYPE_STRING},
67 {CONTACT_FI_PR_HAS_PHONE, _contacts_contact.has_phonenumber, DATA_TYPE_BOOL},
68 {CONTACT_FI_PR_HAS_EMAIL, _contacts_contact.has_email, DATA_TYPE_BOOL},
71 // AB_FI_TYPE_CATEGORY
72 // The entries must be ordered same as CategoryFilterProperty
73 const _AddressbookFilterImpl::__FilterPropertyInfoEntry _AddressbookFilterImpl::__categoryFilterPropertyInfos[] =
75 {CATEGORY_FI_PR_CATEGORY_ID, _contacts_group.id, DATA_TYPE_INT},
76 {CATEGORY_FI_PR_ADDRESSBOOK_ID, _contacts_group.address_book_id, DATA_TYPE_INT},
77 {CATEGORY_FI_PR_NAME, _contacts_group.name, DATA_TYPE_STRING},
80 // AB_FI_TYPE_PHONE_CONTACT
81 // The entries must be ordered same as PhoneContactFilterProperty
82 const _AddressbookFilterImpl::__FilterPropertyInfoEntry _AddressbookFilterImpl::__phoneContactFilterPropertyInfos[] =
84 {PHONE_CONTACT_FI_PR_CONTACT_ID, _contacts_contact_number.contact_id, DATA_TYPE_INT},
85 {PHONE_CONTACT_FI_PR_ADDRESSBOOK_ID, _contacts_contact_number.address_book_id, DATA_TYPE_INT},
86 {PHONE_CONTACT_FI_PR_PERSON_ID, _contacts_contact_number.person_id, DATA_TYPE_INT},
87 {PHONE_CONTACT_FI_PR_DISPLAY_NAME, _contacts_contact_number.display_name, DATA_TYPE_STRING},
88 {PHONE_CONTACT_FI_PR_PHONE, _contacts_contact_number.number, DATA_TYPE_STRING},
91 // AB_FI_TYPE_EMAIL_CONTACT
92 // The entries must be ordered same as EmailContactFilterProperty
93 const _AddressbookFilterImpl::__FilterPropertyInfoEntry _AddressbookFilterImpl::__emailContactFilterPropertyInfos[] =
95 {EMAIL_CONTACT_FI_PR_CONTACT_ID, _contacts_contact_email.contact_id, DATA_TYPE_INT},
96 {EMAIL_CONTACT_FI_PR_ADDRESSBOOK_ID, _contacts_contact_email.address_book_id, DATA_TYPE_INT},
97 {EMAIL_CONTACT_FI_PR_PERSON_ID, _contacts_contact_email.person_id, DATA_TYPE_INT},
98 {EMAIL_CONTACT_FI_PR_DISPLAY_NAME, _contacts_contact_email.display_name, DATA_TYPE_STRING},
99 {EMAIL_CONTACT_FI_PR_EMAIL, _contacts_contact_email.email, DATA_TYPE_STRING},
102 const _AddressbookFilterImpl::__FilterInfoEntry _AddressbookFilterImpl::__filterInfos[] =
104 {AB_FI_TYPE_ADDRESSBOOK, _contacts_address_book._uri, AB_FI_PR_ADDRESSBOOK_ID, sizeof(__addressbookFilterPropertyInfos)/sizeof(_AddressbookFilterImpl::__FilterPropertyInfoEntry), __addressbookFilterPropertyInfos},
105 {AB_FI_TYPE_PERSON, _contacts_person_grouprel._uri, PERSON_FI_PR_PERSON_ID, sizeof(__personFilterPropertyInfos)/sizeof(_AddressbookFilterImpl::__FilterPropertyInfoEntry), __personFilterPropertyInfos},
106 {AB_FI_TYPE_CONTACT, _contacts_contact._uri, CONTACT_FI_PR_CONTACT_ID, sizeof(__contactFilterPropertyInfos)/sizeof(_AddressbookFilterImpl::__FilterPropertyInfoEntry), __contactFilterPropertyInfos},
107 {AB_FI_TYPE_CATEGORY, _contacts_group._uri, CATEGORY_FI_PR_CATEGORY_ID, sizeof(__categoryFilterPropertyInfos)/sizeof(_AddressbookFilterImpl::__FilterPropertyInfoEntry), __categoryFilterPropertyInfos},
108 {AB_FI_TYPE_PHONE_CONTACT, _contacts_contact_number._uri, PHONE_CONTACT_FI_PR_CONTACT_ID, sizeof(__phoneContactFilterPropertyInfos)/sizeof(_AddressbookFilterImpl::__FilterPropertyInfoEntry), __phoneContactFilterPropertyInfos},
109 {AB_FI_TYPE_EMAIL_CONTACT, _contacts_contact_email._uri, EMAIL_CONTACT_FI_PR_CONTACT_ID, sizeof(__emailContactFilterPropertyInfos)/sizeof(_AddressbookFilterImpl::__FilterPropertyInfoEntry), __emailContactFilterPropertyInfos},
113 _AddressbookFilterImpl::IsValidProperty(AddressbookFilterType filterType, unsigned long property)
115 int filterInfoCount = sizeof(__filterInfos)/sizeof(__FilterInfoEntry);
117 if (filterType < 0 || filterType > (filterInfoCount - 1))
122 unsigned long baseIndex = __filterInfos[filterType].baseIndex;
123 int propertyInfoCount = __filterInfos[filterType].propertyInfoCount;
125 if (property < baseIndex || property >= (baseIndex + propertyInfoCount))
133 // IsValidProperty must be called before calling this. If IsValidProperty returns false
134 // this should not be called.
136 _AddressbookFilterImpl::GetUriFromType(AddressbookFilterType filterType)
138 return __filterInfos[filterType].pViewUri;
141 // IsValidProperty must be called before calling this. If IsValidProperty returns false
142 // this should not be called.
144 _AddressbookFilterImpl::GetViewPropertyId(AddressbookFilterType filterType, unsigned long property)
146 const __FilterPropertyInfoEntry* propertyInfos = __filterInfos[filterType].propertyInfos;
147 unsigned long baseIndex = __filterInfos[filterType].baseIndex;
149 return propertyInfos[property - baseIndex].viewPropertyId;
152 // IsValidProperty must be called before calling this. If IsValidProperty returns false
153 // this should not be called.
155 _AddressbookFilterImpl::GetPropertyType(AddressbookFilterType filterType, unsigned long property)
157 const __FilterPropertyInfoEntry* propertyInfos = __filterInfos[filterType].propertyInfos;
158 unsigned long baseIndex = __filterInfos[filterType].baseIndex;
160 return propertyInfos[property - baseIndex].dataType;
163 _AddressbookFilterImpl::_AddressbookFilterImpl(AddressbookFilterType type)
164 : __filterHandle(null)
168 contacts_filter_h filterHandle = null;
169 const char* pUri = GetUriFromType(type);
171 SysTryReturnVoidResult(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
173 int ret = contacts_filter_create(pUri, &filterHandle);
174 SysTryReturnVoidResult(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, "[%s ] Memory allocation failed.", GetErrorMessage(GetLastResult()));
176 __filterHandle = filterHandle;
179 _AddressbookFilterImpl::~_AddressbookFilterImpl(void)
181 if (__filterHandle != null)
183 contacts_filter_destroy(__filterHandle);
188 _AddressbookFilterImpl::Equals(const Tizen::Base::Object& rhs) const
190 const _AddressbookFilterImpl* pAddressbookFilter = dynamic_cast<const _AddressbookFilterImpl*>(&rhs);
191 if (this == pAddressbookFilter)
200 _AddressbookFilterImpl::GetHashCode(void) const
202 return (int)__filterHandle;
205 contacts_match_int_flag_e
206 _AddressbookFilterImpl::GetMatchIntFlag(FilterComparisonOperator comparisonOperator)
208 contacts_match_int_flag_e match = CONTACTS_MATCH_NONE;
209 switch(comparisonOperator)
211 case FI_CMP_OP_EQUAL:
212 match = CONTACTS_MATCH_EQUAL;
214 case FI_CMP_OP_LESS_THAN:
215 match = CONTACTS_MATCH_GREATER_THAN;
217 case FI_CMP_OP_LESS_THAN_OR_EQUAL:
218 match = CONTACTS_MATCH_GREATER_THAN_OR_EQUAL;
220 case FI_CMP_OP_GREATER_THAN:
221 match = CONTACTS_MATCH_LESS_THAN;
223 case FI_CMP_OP_GREATER_THAN_OR_EQUAL:
224 match = CONTACTS_MATCH_LESS_THAN_OR_EQUAL;
226 case FI_CMP_OP_IS_NULL:
227 match = CONTACTS_MATCH_NONE;
230 match = CONTACTS_MATCH_NONE;
236 contacts_match_str_flag_e
237 _AddressbookFilterImpl::GetMatchStrFlag(FilterStringOperator stringOperstor)
239 contacts_match_str_flag_e match = CONTACTS_MATCH_EXISTS;
240 switch(stringOperstor)
242 case FI_STR_OP_EQUAL:
243 match = CONTACTS_MATCH_EXACTLY;
245 case FI_STR_OP_FULL_STRING:
246 match = CONTACTS_MATCH_FULLSTRING;
248 case FI_STR_OP_START_WITH:
249 match = CONTACTS_MATCH_STARTSWITH;
251 case FI_STR_OP_END_WITH:
252 match = CONTACTS_MATCH_ENDSWITH;
254 case FI_STR_OP_CONTAIN:
255 match = CONTACTS_MATCH_CONTAINS;
257 case FI_STR_OP_IS_NOT_NULL:
258 match = CONTACTS_MATCH_EXISTS;
261 match = CONTACTS_MATCH_EXISTS;
269 _AddressbookFilterImpl::AppendInt(FilterConjunctiveOperator conjunctiveOperator, unsigned long filterProperty, FilterComparisonOperator comparisonOperator, int value)
271 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));
272 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);
273 SysTryReturn(NID_SCL, !(__isEmpty && (conjunctiveOperator == FI_CONJ_OP_AND || conjunctiveOperator == FI_CONJ_OP_OR)),\
274 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",\
275 GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
276 SysTryReturn(NID_SCL, !(!__isEmpty && (conjunctiveOperator == FI_CONJ_OP_NONE)),\
277 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.",\
278 GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
281 unsigned int id = _AddressbookFilterImpl::GetViewPropertyId(__filterType, filterProperty);
282 contacts_match_int_flag_e match = GetMatchIntFlag(comparisonOperator);
286 contacts_filter_operator_e filterOperator = conjunctiveOperator == FI_CONJ_OP_AND ? CONTACTS_FILTER_OPERATOR_AND : CONTACTS_FILTER_OPERATOR_OR;
288 contacts_filter_add_operator(__filterHandle, filterOperator);
291 int ret = contacts_filter_add_int(__filterHandle, id, match, value);
292 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
300 _AddressbookFilterImpl::AppendBool(FilterConjunctiveOperator conjunctiveOperator, unsigned long filterProperty, FilterComparisonOperator comparisonOperator, bool value)
302 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));
303 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);
304 SysTryReturn(NID_SCL, !(__isEmpty && (conjunctiveOperator == FI_CONJ_OP_AND || conjunctiveOperator == FI_CONJ_OP_OR)),\
305 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",\
306 GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
307 SysTryReturn(NID_SCL, !(!__isEmpty && conjunctiveOperator == FI_CONJ_OP_NONE),\
308 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.",\
309 GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
310 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);
312 unsigned int viewPropertyId = _AddressbookFilterImpl::GetViewPropertyId(__filterType, filterProperty);
316 contacts_filter_operator_e filterOperator = conjunctiveOperator == FI_CONJ_OP_AND ? CONTACTS_FILTER_OPERATOR_AND : CONTACTS_FILTER_OPERATOR_OR;
318 contacts_filter_add_operator(__filterHandle, filterOperator);
322 int ret = contacts_filter_add_bool(__filterHandle, viewPropertyId, value);
323 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
331 _AddressbookFilterImpl::AppendString(FilterConjunctiveOperator conjunctiveOperator, unsigned long filterProperty, FilterStringOperator comparisonOperator, const Tizen::Base::String& value)
333 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));
334 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);
335 SysTryReturn(NID_SCL, !(__isEmpty && (conjunctiveOperator == FI_CONJ_OP_AND || conjunctiveOperator == FI_CONJ_OP_OR)),\
336 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",\
337 GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
338 SysTryReturn(NID_SCL, !(!__isEmpty && conjunctiveOperator == FI_CONJ_OP_NONE),\
339 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.",\
340 GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
343 unsigned int viewPropertyId = _AddressbookFilterImpl::GetViewPropertyId(__filterType, filterProperty);
344 contacts_match_str_flag_e match = GetMatchStrFlag(comparisonOperator);
345 std::unique_ptr<char[]> pCharArray( _StringConverter::CopyToCharArrayN(value));
346 SysTryReturn(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
350 contacts_filter_operator_e filterOperator = conjunctiveOperator == FI_CONJ_OP_AND ? CONTACTS_FILTER_OPERATOR_AND : CONTACTS_FILTER_OPERATOR_OR;
352 contacts_filter_add_operator(__filterHandle, filterOperator);
355 int ret = contacts_filter_add_str(__filterHandle, viewPropertyId, match, pCharArray.get());
356 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
365 _AddressbookFilterImpl::AppendFilter(FilterConjunctiveOperator conjunctiveOperator, const _AddressbookFilterImpl& filter)
367 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));
368 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));
369 SysTryReturn(NID_SCL, !(__isEmpty && (conjunctiveOperator == FI_CONJ_OP_AND || conjunctiveOperator == FI_CONJ_OP_OR)),\
370 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",\
371 GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
372 SysTryReturn(NID_SCL, !(!__isEmpty && conjunctiveOperator == FI_CONJ_OP_NONE),\
373 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.",\
374 GetErrorMessage(E_INVALID_ARG), conjunctiveOperator);
378 contacts_filter_operator_e filterOperator = conjunctiveOperator == FI_CONJ_OP_AND ? CONTACTS_FILTER_OPERATOR_AND : CONTACTS_FILTER_OPERATOR_OR;
379 contacts_filter_add_operator(__filterHandle, filterOperator);
382 int ret = contacts_filter_add_filter(__filterHandle, filter.__filterHandle);
383 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
391 _AddressbookFilterImpl::GetFilterHandle(void) const
395 return __filterHandle;
401 AddressbookFilterType
402 _AddressbookFilterImpl::GetType(void) const
408 _AddressbookFilterImpl::IsEmpty(void) const
413 const _AddressbookFilterImpl*
414 _AddressbookFilterImpl::GetInstance(const AddressbookFilter& filter)
416 return filter.__pAddressbookFilterImpl;
419 _AddressbookFilterImpl*
420 _AddressbookFilterImpl::GetInstance(AddressbookFilter& filter)
422 return filter.__pAddressbookFilterImpl;