Resolved Native API Reference issues for calendar-service
[platform/core/pim/calendar-service.git] / common / cal_filter.c
1 /*
2  * Calendar Service
3  *
4  * Copyright (c) 2012 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include <stdlib.h>
21
22 #include "cal_internal.h"
23 #include "cal_typedef.h"
24 #include "cal_view.h"
25 #include "cal_filter.h"
26 #include "cal_utils.h"
27
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);
30
31 static int _cal_filter_destroy_composite(cal_composite_filter_s* filter);
32 static int _cal_filter_destroy_attribute(cal_attribute_filter_s* filter);
33
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);
38
39 EXPORT_API int calendar_filter_create(const char* view_uri, calendar_filter_h* out_filter)
40 {
41         CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
42
43         cal_composite_filter_s *com_filter;
44
45         RETV_IF(NULL == view_uri, CALENDAR_ERROR_INVALID_PARAMETER);
46         RETV_IF(NULL == out_filter, CALENDAR_ERROR_INVALID_PARAMETER);
47
48         com_filter = calloc(1, sizeof(cal_composite_filter_s));
49         RETV_IF(NULL == com_filter, CALENDAR_ERROR_OUT_OF_MEMORY);
50
51 #ifdef CAL_IPC_CLIENT
52         cal_view_initialize();
53 #endif
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;
59 }
60
61 EXPORT_API int calendar_filter_add_operator(calendar_filter_h filter, calendar_filter_operator_e op)
62 {
63         CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
64
65         cal_composite_filter_s *com_filter;
66
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);
69
70         com_filter = (cal_composite_filter_s*)filter;
71
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;
76 }
77
78 EXPORT_API int calendar_filter_add_filter(calendar_filter_h filter, calendar_filter_h add_filter)
79 {
80         CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
81
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;
86
87         RETV_IF(NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
88         RETV_IF(NULL == add_filter, CALENDAR_ERROR_INVALID_PARAMETER);
89
90         com_filter = (cal_composite_filter_s*)filter;
91         com_filter2 = (cal_composite_filter_s*)add_filter;
92
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);
97
98         ret = cal_filter_clone(add_filter, &f);
99         RETV_IF(CALENDAR_ERROR_NONE != ret, ret);
100
101         com_filter->filters = g_slist_append(com_filter->filters, f);
102
103         return CALENDAR_ERROR_NONE;
104 }
105
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)
108 {
109         cal_attribute_filter_s *filter;
110
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");
113
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;
119
120         com_filter->filters = g_slist_append(com_filter->filters, filter);
121         *out_filter = filter;
122         return CALENDAR_ERROR_NONE;
123 }
124
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)
127 {
128         CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
129
130         cal_composite_filter_s *com_filter;
131         cal_attribute_filter_s *str_filter;
132         int ret;
133         bool bcheck;
134
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);
138
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);
141
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);
144
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);
148
149         str_filter->value.s = cal_strdup(match_value);
150         return CALENDAR_ERROR_NONE;
151 }
152
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)
155 {
156         CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
157
158         cal_composite_filter_s *com_filter;
159         cal_attribute_filter_s *int_filter;
160         int ret;
161         bool bcheck;
162
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);
165
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);
168
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);
171
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);
175
176         int_filter->value.i = match_value;
177
178         return CALENDAR_ERROR_NONE;
179 }
180
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)
183 {
184         CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
185
186         cal_composite_filter_s *com_filter;
187         cal_attribute_filter_s *int_filter;
188         int ret;
189         bool bcheck;
190
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);
193
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);
196
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);
199
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);
203
204         int_filter->value.d = match_value;
205
206         return CALENDAR_ERROR_NONE;
207 }
208
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)
211 {
212         CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
213
214         cal_composite_filter_s *com_filter;
215         cal_attribute_filter_s *int_filter;
216         int ret;
217         bool bcheck;
218
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);
221
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);
224
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);
227
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);
231
232         int_filter->value.lli = match_value;
233
234         return CALENDAR_ERROR_NONE;
235 }
236
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)
238 {
239         CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
240
241         cal_composite_filter_s *com_filter;
242         cal_attribute_filter_s *int_filter;
243         int ret;
244         bool bcheck;
245
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);
248
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);
251
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);
254
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);
258
259         int_filter->value.caltime = match_value;
260
261         return CALENDAR_ERROR_NONE;
262 }
263
264 EXPORT_API int calendar_filter_destroy(calendar_filter_h filter)
265 {
266         CHECK_CALENDAR_SUPPORTED(CALENDAR_FEATURE);
267
268         RETV_IF(NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
269
270 #ifdef CAL_IPC_CLIENT
271         cal_view_finalize();
272 #endif
273
274         return _cal_filter_destroy_composite((cal_composite_filter_s*)filter);
275 }
276
277 int cal_filter_clone(calendar_filter_h filter, calendar_filter_h* out_filter)
278 {
279         RETV_IF(NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
280         RETV_IF(NULL == out_filter, CALENDAR_ERROR_INVALID_PARAMETER);
281
282         return _cal_filter_clone_composite((cal_composite_filter_s*)filter, (cal_composite_filter_s**)out_filter);
283 }
284
285 static int _cal_filter_destroy_composite(cal_composite_filter_s* filter)
286 {
287         GSList *cursor = NULL;
288
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;
292                 if (NULL == src)
293                         continue;
294                 if (src->filter_type == CAL_FILTER_COMPOSITE)
295                         _cal_filter_destroy_composite((cal_composite_filter_s*)src);
296                 else
297                         _cal_filter_destroy_attribute((cal_attribute_filter_s*)src);
298         }
299         CAL_FREE(filter->view_uri);
300         g_slist_free(filter->filters);
301         g_slist_free(filter->filter_ops);
302         CAL_FREE(filter);
303
304         return CALENDAR_ERROR_NONE;
305 }
306
307 static int _cal_filter_destroy_attribute(cal_attribute_filter_s* filter)
308 {
309         RETV_IF(NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
310
311         if (filter->filter_type == CAL_FILTER_STR)
312                 CAL_FREE(filter->value.s);
313
314         CAL_FREE(filter);
315         return CALENDAR_ERROR_NONE;
316 }
317
318 static int _cal_filter_clone_composite(cal_composite_filter_s* filter,
319                 cal_composite_filter_s **out_filter)
320 {
321         GSList *cursor;
322         cal_composite_filter_s *out;
323         int ret = CALENDAR_ERROR_NONE;
324
325         RETV_IF(NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
326
327         ret = calendar_filter_create(filter->view_uri, (calendar_filter_h *)&out);
328         RETV_IF(CALENDAR_ERROR_NONE != ret, CALENDAR_ERROR_OUT_OF_MEMORY);
329
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;
333
334                 if (NULL == src)
335                         continue;
336
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);
340                 } else {
341                         ret = _cal_filter_clone_attribute((cal_attribute_filter_s *)src,
342                                         (cal_attribute_filter_s **)&dest);
343                 }
344                 if (CALENDAR_ERROR_NONE == ret) {
345                         out->filters = g_slist_append(out->filters, dest);
346                 } else {
347                         calendar_filter_destroy((calendar_filter_h)out);
348                         return ret;
349                 }
350         }
351
352         out->filter_ops = g_slist_copy(filter->filter_ops);
353         *out_filter = out;
354
355         return CALENDAR_ERROR_NONE;
356 }
357
358 static int _cal_filter_clone_attribute(cal_attribute_filter_s* filter,
359                 cal_attribute_filter_s **out_filter)
360 {
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);
364
365         out->filter_type = filter->filter_type;
366         out->match = filter->match;
367         out->property_id = filter->property_id;
368         switch (filter->filter_type) {
369         case CAL_FILTER_STR:
370                 out->value.s = cal_strdup(filter->value.s);
371                 break;
372         case CAL_FILTER_INT:
373                 out->value.i = filter->value.i;
374                 break;
375         case CAL_FILTER_DOUBLE:
376                 out->value.d = filter->value.d;
377                 break;
378         case CAL_FILTER_LLI:
379                 out->value.lli = filter->value.lli;
380                 break;
381         case CAL_FILTER_CALTIME:
382                 out->value.caltime = filter->value.caltime;
383                 break;
384         default:
385                 break;
386         }
387
388         *out_filter = out;
389         return CALENDAR_ERROR_NONE;
390 }