4 * Copyright (c) 2012 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 #include "cal_internal.h"
23 #include "cal_typedef.h"
25 #include "cal_filter.h"
26 #include "cal_utils.h"
28 static int _cal_filter_create_attribute(cal_composite_filter_s *com_filter, unsigned int property_id,
29 int match, int filter_type, cal_attribute_filter_s **out_filter);
31 static int _cal_filter_destroy_composite(cal_composite_filter_s* filter);
32 static int _cal_filter_destroy_attribute(cal_attribute_filter_s* filter);
34 static int _cal_filter_clone_composite(cal_composite_filter_s* filter,
35 cal_composite_filter_s **out_filter);
36 static int _cal_filter_clone_attribute(cal_attribute_filter_s* filter,
37 cal_attribute_filter_s **out_filter);
39 EXPORT_API int calendar_filter_create(const char* view_uri, calendar_filter_h* out_filter)
41 CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
43 cal_composite_filter_s *com_filter;
45 RETV_IF(NULL == view_uri, CALENDAR_ERROR_INVALID_PARAMETER);
46 RETV_IF(NULL == out_filter, CALENDAR_ERROR_INVALID_PARAMETER);
48 com_filter = calloc(1, sizeof(cal_composite_filter_s));
49 RETV_IF(NULL == com_filter, CALENDAR_ERROR_OUT_OF_MEMORY);
52 cal_view_initialize();
54 com_filter->filter_type = CAL_FILTER_COMPOSITE;
55 com_filter->view_uri = cal_strdup(view_uri);
56 com_filter->properties = (cal_property_info_s *)cal_view_get_property_info(view_uri, &com_filter->property_count);
57 *out_filter = (calendar_filter_h)com_filter;
58 return CALENDAR_ERROR_NONE;
61 EXPORT_API int calendar_filter_add_operator(calendar_filter_h filter, calendar_filter_operator_e op)
63 CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
65 cal_composite_filter_s *com_filter;
67 RETV_IF(NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
68 RETVM_IF(CALENDAR_FILTER_OPERATOR_MAX <= op, CALENDAR_ERROR_INVALID_PARAMETER, "max(%d) <= operator(%d)", CALENDAR_FILTER_OPERATOR_MAX, op);
70 com_filter = (cal_composite_filter_s*)filter;
72 RETVM_IF(g_slist_length(com_filter->filter_ops) != (g_slist_length(com_filter->filters)-1),
73 CALENDAR_ERROR_INVALID_PARAMETER, "Invalid parameter : Please check the operator of filter");
74 com_filter->filter_ops = g_slist_append(com_filter->filter_ops, (void*)op);
75 return CALENDAR_ERROR_NONE;
78 EXPORT_API int calendar_filter_add_filter(calendar_filter_h filter, calendar_filter_h add_filter)
80 CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
82 cal_composite_filter_s *com_filter;
83 cal_composite_filter_s *com_filter2;
84 calendar_filter_h f = NULL;
85 int ret = CALENDAR_ERROR_NONE;
87 RETV_IF(NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
88 RETV_IF(NULL == add_filter, CALENDAR_ERROR_INVALID_PARAMETER);
90 com_filter = (cal_composite_filter_s*)filter;
91 com_filter2 = (cal_composite_filter_s*)add_filter;
93 RETVM_IF(g_slist_length(com_filter->filter_ops) != g_slist_length(com_filter->filters),
94 CALENDAR_ERROR_INVALID_PARAMETER, "Invalid parameter :Please check the operator of filter");
95 RETVM_IF(0 != strcmp(com_filter->view_uri, com_filter2->view_uri), CALENDAR_ERROR_INVALID_PARAMETER,
96 "The filter view_uri is different (filter1:%s, filter2:%s)", com_filter->view_uri, com_filter2->view_uri);
98 ret = cal_filter_clone(add_filter, &f);
99 RETV_IF(CALENDAR_ERROR_NONE != ret, ret);
101 com_filter->filters = g_slist_append(com_filter->filters, f);
103 return CALENDAR_ERROR_NONE;
106 static int _cal_filter_create_attribute(cal_composite_filter_s *com_filter,
107 unsigned int property_id, int match, int filter_type, cal_attribute_filter_s **out_filter)
109 cal_attribute_filter_s *filter;
111 RETVM_IF(g_slist_length(com_filter->filter_ops) != g_slist_length(com_filter->filters),
112 CALENDAR_ERROR_INVALID_PARAMETER, "Invalid parameter :Please check the operator of filter");
114 filter = calloc(1, sizeof(cal_attribute_filter_s));
115 RETVM_IF(NULL == filter, CALENDAR_ERROR_OUT_OF_MEMORY, "calloc() Fail");
116 filter->filter_type = filter_type;
117 filter->property_id = property_id;
118 filter->match = match;
120 com_filter->filters = g_slist_append(com_filter->filters, filter);
121 *out_filter = filter;
122 return CALENDAR_ERROR_NONE;
125 EXPORT_API int calendar_filter_add_str(calendar_filter_h filter, unsigned int property_id,
126 calendar_match_str_flag_e match, const char* match_value)
128 CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
130 cal_composite_filter_s *com_filter;
131 cal_attribute_filter_s *str_filter;
135 RETV_IF(NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
136 RETV_IF(NULL == match_value, CALENDAR_ERROR_INVALID_PARAMETER);
137 RETVM_IF(CALENDAR_MATCH_STR_MAX <= match, CALENDAR_ERROR_INVALID_PARAMETER, "max(%d) <= match(%d)", CALENDAR_MATCH_STR_MAX, match);
139 bcheck = CAL_PROPERTY_CHECK_DATA_TYPE(property_id, CAL_PROPERTY_DATA_TYPE_STR);
140 RETVM_IF(false == bcheck, CALENDAR_ERROR_INVALID_PARAMETER, "property_id(%d)", property_id);
142 bcheck = CAL_PROPERTY_CHECK_FLAGS(property_id, CAL_PROPERTY_FLAGS_PROJECTION);
143 RETVM_IF(true == bcheck, CALENDAR_ERROR_INVALID_PARAMETER, "property_id(%d)", property_id);
145 com_filter = (cal_composite_filter_s*)filter;
146 ret = _cal_filter_create_attribute(com_filter, property_id, match, CAL_FILTER_STR, &str_filter);
147 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "property_id(%d), match(%d), match_value(%s)", property_id, match, match_value);
149 str_filter->value.s = cal_strdup(match_value);
150 return CALENDAR_ERROR_NONE;
153 EXPORT_API int calendar_filter_add_int(calendar_filter_h filter, unsigned int property_id,
154 calendar_match_int_flag_e match, int match_value)
156 CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
158 cal_composite_filter_s *com_filter;
159 cal_attribute_filter_s *int_filter;
163 RETV_IF(NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
164 RETVM_IF(CALENDAR_MATCH_INT_MAX <= match, CALENDAR_ERROR_INVALID_PARAMETER, "max(%d) <= match(%d)", CALENDAR_MATCH_INT_MAX, match);
166 bcheck = CAL_PROPERTY_CHECK_DATA_TYPE(property_id, CAL_PROPERTY_DATA_TYPE_INT);
167 RETVM_IF(false == bcheck, CALENDAR_ERROR_INVALID_PARAMETER, "property_id(%d)", property_id);
169 bcheck = CAL_PROPERTY_CHECK_FLAGS(property_id, CAL_PROPERTY_FLAGS_PROJECTION);
170 RETVM_IF(true == bcheck, CALENDAR_ERROR_INVALID_PARAMETER, "property_id(%d)", property_id);
172 com_filter = (cal_composite_filter_s*)filter;
173 ret = _cal_filter_create_attribute(com_filter, property_id, match, CAL_FILTER_INT, &int_filter);
174 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "property_id(%d), match(%d), match_value(%d)", property_id, match, match_value);
176 int_filter->value.i = match_value;
178 return CALENDAR_ERROR_NONE;
181 EXPORT_API int calendar_filter_add_double(calendar_filter_h filter, unsigned int property_id,
182 calendar_match_int_flag_e match, double match_value)
184 CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
186 cal_composite_filter_s *com_filter;
187 cal_attribute_filter_s *int_filter;
191 RETV_IF(NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
192 RETVM_IF(CALENDAR_MATCH_INT_MAX <= match, CALENDAR_ERROR_INVALID_PARAMETER, "max(%d) <= match(%d)", CALENDAR_MATCH_INT_MAX, match);
194 bcheck = CAL_PROPERTY_CHECK_DATA_TYPE(property_id, CAL_PROPERTY_DATA_TYPE_DOUBLE);
195 RETVM_IF(false == bcheck, CALENDAR_ERROR_INVALID_PARAMETER, "property_id(%d)", property_id);
197 bcheck = CAL_PROPERTY_CHECK_FLAGS(property_id, CAL_PROPERTY_FLAGS_PROJECTION);
198 RETVM_IF(true == bcheck, CALENDAR_ERROR_INVALID_PARAMETER, "property_id(%d)", property_id);
200 com_filter = (cal_composite_filter_s*)filter;
201 ret = _cal_filter_create_attribute(com_filter, property_id, match, CAL_FILTER_DOUBLE, &int_filter);
202 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "property_id(%d), match(%d), match_value(%lf)", property_id, match, match_value);
204 int_filter->value.d = match_value;
206 return CALENDAR_ERROR_NONE;
209 EXPORT_API int calendar_filter_add_lli(calendar_filter_h filter, unsigned int property_id,
210 calendar_match_int_flag_e match, long long int match_value)
212 CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
214 cal_composite_filter_s *com_filter;
215 cal_attribute_filter_s *int_filter;
219 RETV_IF(NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
220 RETVM_IF(CALENDAR_MATCH_INT_MAX <= match, CALENDAR_ERROR_INVALID_PARAMETER, "max(%d) <= match(%d)", CALENDAR_MATCH_INT_MAX, match);
222 bcheck = CAL_PROPERTY_CHECK_DATA_TYPE(property_id, CAL_PROPERTY_DATA_TYPE_LLI);
223 RETVM_IF(false == bcheck, CALENDAR_ERROR_INVALID_PARAMETER, "property_id(%d)", property_id);
225 bcheck = CAL_PROPERTY_CHECK_FLAGS(property_id, CAL_PROPERTY_FLAGS_PROJECTION);
226 RETVM_IF(true == bcheck, CALENDAR_ERROR_INVALID_PARAMETER, "property_id(%d)", property_id);
228 com_filter = (cal_composite_filter_s*)filter;
229 ret = _cal_filter_create_attribute(com_filter, property_id, match, CAL_FILTER_LLI, &int_filter);
230 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "property_id(%d), match(%d), match_value(%lld)", property_id, match, match_value);
232 int_filter->value.lli = match_value;
234 return CALENDAR_ERROR_NONE;
237 EXPORT_API int calendar_filter_add_caltime(calendar_filter_h filter, unsigned int property_id, calendar_match_int_flag_e match, calendar_time_s match_value)
239 CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
241 cal_composite_filter_s *com_filter;
242 cal_attribute_filter_s *int_filter;
246 RETV_IF(NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
247 RETVM_IF(CALENDAR_MATCH_INT_MAX <= match, CALENDAR_ERROR_INVALID_PARAMETER, "max(%d) <= match(%d)", CALENDAR_MATCH_INT_MAX, match);
249 bcheck = CAL_PROPERTY_CHECK_DATA_TYPE(property_id, CAL_PROPERTY_DATA_TYPE_CALTIME);
250 RETVM_IF(false == bcheck, CALENDAR_ERROR_INVALID_PARAMETER, "property_id(%d)", property_id);
252 bcheck = CAL_PROPERTY_CHECK_FLAGS(property_id, CAL_PROPERTY_FLAGS_PROJECTION);
253 RETVM_IF(true == bcheck, CALENDAR_ERROR_INVALID_PARAMETER, "property_id(%d)", property_id);
255 com_filter = (cal_composite_filter_s*)filter;
256 ret = _cal_filter_create_attribute(com_filter, property_id, match, CAL_FILTER_CALTIME, &int_filter);
257 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "property_id(%d), match(%d)", property_id, match);
259 int_filter->value.caltime = match_value;
261 return CALENDAR_ERROR_NONE;
264 EXPORT_API int calendar_filter_destroy(calendar_filter_h filter)
266 CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
268 RETV_IF(NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
270 #ifdef CAL_IPC_CLIENT
274 return _cal_filter_destroy_composite((cal_composite_filter_s*)filter);
277 int cal_filter_clone(calendar_filter_h filter, calendar_filter_h* out_filter)
279 RETV_IF(NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
280 RETV_IF(NULL == out_filter, CALENDAR_ERROR_INVALID_PARAMETER);
282 return _cal_filter_clone_composite((cal_composite_filter_s*)filter, (cal_composite_filter_s**)out_filter);
285 static int _cal_filter_destroy_composite(cal_composite_filter_s* filter)
287 GSList *cursor = NULL;
289 RETV_IF(NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
290 for (cursor = filter->filters; cursor; cursor = cursor->next) {
291 cal_filter_s *src = (cal_filter_s *)cursor->data;
294 if (src->filter_type == CAL_FILTER_COMPOSITE)
295 _cal_filter_destroy_composite((cal_composite_filter_s*)src);
297 _cal_filter_destroy_attribute((cal_attribute_filter_s*)src);
299 CAL_FREE(filter->view_uri);
300 g_slist_free(filter->filters);
301 g_slist_free(filter->filter_ops);
304 return CALENDAR_ERROR_NONE;
307 static int _cal_filter_destroy_attribute(cal_attribute_filter_s* filter)
309 RETV_IF(NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
311 if (filter->filter_type == CAL_FILTER_STR)
312 CAL_FREE(filter->value.s);
315 return CALENDAR_ERROR_NONE;
318 static int _cal_filter_clone_composite(cal_composite_filter_s* filter,
319 cal_composite_filter_s **out_filter)
322 cal_composite_filter_s *out;
323 int ret = CALENDAR_ERROR_NONE;
325 RETV_IF(NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
327 ret = calendar_filter_create(filter->view_uri, (calendar_filter_h *)&out);
328 RETV_IF(CALENDAR_ERROR_NONE != ret, CALENDAR_ERROR_OUT_OF_MEMORY);
330 for (cursor = filter->filters; cursor ; cursor = cursor->next) {
331 cal_filter_s *src = (cal_filter_s *)cursor->data;
332 cal_filter_s *dest = NULL;
337 if (src->filter_type == CAL_FILTER_COMPOSITE) {
338 ret = _cal_filter_clone_composite((cal_composite_filter_s *)src,
339 (cal_composite_filter_s **)&dest);
341 ret = _cal_filter_clone_attribute((cal_attribute_filter_s *)src,
342 (cal_attribute_filter_s **)&dest);
344 if (CALENDAR_ERROR_NONE == ret) {
345 out->filters = g_slist_append(out->filters, dest);
347 calendar_filter_destroy((calendar_filter_h)out);
352 out->filter_ops = g_slist_copy(filter->filter_ops);
355 return CALENDAR_ERROR_NONE;
358 static int _cal_filter_clone_attribute(cal_attribute_filter_s* filter,
359 cal_attribute_filter_s **out_filter)
361 cal_attribute_filter_s *out;
362 out = calloc(1, sizeof(cal_attribute_filter_s));
363 RETV_IF(NULL == out, CALENDAR_ERROR_OUT_OF_MEMORY);
365 out->filter_type = filter->filter_type;
366 out->match = filter->match;
367 out->property_id = filter->property_id;
368 switch (filter->filter_type) {
370 out->value.s = cal_strdup(filter->value.s);
373 out->value.i = filter->value.i;
375 case CAL_FILTER_DOUBLE:
376 out->value.d = filter->value.d;
379 out->value.lli = filter->value.lli;
381 case CAL_FILTER_CALTIME:
382 out->value.caltime = filter->value.caltime;
389 return CALENDAR_ERROR_NONE;