[misc] Sycn with master branch.
[apps/core/preloaded/calendar.git] / common / query.c
1 /*
2   *
3   *  Copyright 2012  Samsung Electronics Co., Ltd
4   *
5   *  Licensed under the Flora License, Version 1.1 (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
8   *
9   *       http://floralicense.org/license/
10   *
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.
16   */
17
18 #include "query.h"
19
20 void __cal_query_get_caltime_day(const struct tm* date, bool all_day, calendar_time_s* caltime)
21 {
22         if (all_day) {
23                 caltime->type = CALENDAR_TIME_LOCALTIME;
24                 caltime->time.date.year = date->tm_year + 1900;
25                 caltime->time.date.month = date->tm_mon + 1;
26                 caltime->time.date.mday = date->tm_mday;
27         } else {
28                 long long int lli_start = 0;
29                 cal_util_convert_tm_to_lli(NULL, date, &lli_start);
30                 caltime->type = CALENDAR_TIME_UTIME;
31                 caltime->time.utime = lli_start;
32         }
33 }
34
35 void __cal_query_add_int_equal_condition(calendar_filter_h filter, unsigned int property_id, int value)
36 {
37         calendar_error_e error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
38         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
39
40         error = calendar_filter_add_int(filter, property_id, CALENDAR_MATCH_EQUAL, value);
41         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
42 }
43
44 void __cal_query_set_date_range_condition(calendar_filter_h filter,
45                                           unsigned int time_property_id, const struct tm* date,
46                                           int direction, bool all_day)
47 {
48         calendar_time_s caltime_start = {0};
49         __cal_query_get_caltime_day(date, all_day, &caltime_start);
50
51         calendar_error_e error = CALENDAR_ERROR_NONE;
52
53         if (direction > 0)
54                 error = calendar_filter_add_caltime(
55                                         filter, time_property_id, CALENDAR_MATCH_GREATER_THAN_OR_EQUAL, caltime_start);
56         else
57                 error = calendar_filter_add_caltime(
58                                         filter, time_property_id, CALENDAR_MATCH_LESS_THAN, caltime_start);
59
60         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
61 }
62
63 calendar_query_h cal_query_create_list_more_normal_query(
64                 unsigned int time_property_id, const struct tm* start, int direction)
65 {
66         c_retv_if(start == NULL, NULL);
67
68         calendar_query_h query = NULL;
69
70         calendar_error_e error =
71                         calendar_query_create(_calendar_instance_normal_calendar_book._uri, &query);
72         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_create() is failed(%x)", error);
73
74         calendar_filter_h filter;
75
76         error = calendar_filter_create(_calendar_instance_normal_calendar_book._uri, &filter);
77         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
78
79         __cal_query_set_date_range_condition(filter, time_property_id, start, direction, false);
80
81         __cal_query_add_int_equal_condition(filter,
82                         _calendar_instance_normal_calendar_book.calendar_book_visibility, 1);
83
84         error = calendar_query_set_filter(query, filter);
85         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_add_filter() is failed(%x)", error);
86
87         error = calendar_query_set_sort(query, time_property_id, direction > 0);
88         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_set_sort() is failed(%x)", error);
89
90         error = calendar_filter_destroy(filter);
91         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
92
93         return query;
94 }
95
96 calendar_query_h cal_query_create_list_more_allday_query(
97                 unsigned int time_property_id, const struct tm* start, int direction)
98 {
99         c_retv_if(start == NULL, NULL);
100
101         calendar_query_h query = NULL;
102
103         calendar_error_e error =
104                         calendar_query_create(_calendar_instance_allday_calendar_book._uri, &query);
105         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_create() is failed(%x)", error);
106
107         calendar_filter_h filter;
108
109         error = calendar_filter_create(_calendar_instance_allday_calendar_book._uri, &filter);
110         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
111
112         __cal_query_set_date_range_condition(filter, time_property_id, start, direction, true);
113
114         __cal_query_add_int_equal_condition(filter,
115                         _calendar_instance_allday_calendar_book.calendar_book_visibility, 1);
116
117         error = calendar_query_set_filter(query, filter);
118         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_add_filter() is failed(%x)", error);
119
120         error = calendar_query_set_sort(query, time_property_id, direction > 0);
121         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_set_sort() is failed(%x)", error);
122
123         error = calendar_filter_destroy(filter);
124         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
125
126         return query;
127 }
128
129 calendar_filter_h cal_query_create_not_completed_task_filter()
130 {
131         calendar_filter_h filter;
132
133         calendar_error_e error = calendar_filter_create(_calendar_todo_calendar_book._uri, &filter);
134         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
135
136         error = calendar_filter_add_int(filter, _calendar_todo_calendar_book.todo_status,
137                                                                         CALENDAR_MATCH_EQUAL, CALENDAR_TODO_STATUS_NONE);
138         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
139
140         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_OR);
141         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
142
143         error = calendar_filter_add_int(filter, _calendar_todo_calendar_book.todo_status,
144                                                                         CALENDAR_MATCH_EQUAL, CALENDAR_TODO_STATUS_NEEDS_ACTION);
145         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
146
147         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_OR);
148         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
149
150         error = calendar_filter_add_int(filter, _calendar_todo_calendar_book.todo_status,
151                                                                         CALENDAR_MATCH_EQUAL, CALENDAR_TODO_STATUS_IN_PROCESS);
152         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
153
154         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
155         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
156
157         return filter;
158 }
159
160 calendar_query_h cal_query_create_list_more_task_query(
161                 const struct tm* start, int direction, bool include_completed)
162 {
163         c_retv_if(start == NULL, NULL);
164
165         calendar_query_h query = NULL;
166
167         calendar_error_e error = calendar_query_create(_calendar_todo_calendar_book._uri, &query);
168         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_create() is failed(%x)", error);
169
170         calendar_filter_h filter = NULL;
171
172         calendar_filter_h not_competed_filter = NULL;
173
174         error = calendar_filter_create(_calendar_todo_calendar_book._uri, &filter);
175         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
176
177         __cal_query_set_date_range_condition(filter,
178                         _calendar_todo_calendar_book.due_time, start, direction, false);
179
180         __cal_query_add_int_equal_condition(filter,
181                                                                         _calendar_todo_calendar_book.calendar_book_visibility, 1);
182
183         if (!include_completed) {
184                 not_competed_filter = cal_query_create_not_completed_task_filter();
185
186                 error = calendar_filter_add_filter(filter, not_competed_filter);
187                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_filter() is failed(%x)", error);
188
189                 // todo: filter handle destroy!
190         }
191
192         error = calendar_query_set_filter(query, filter);
193         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_add_filter() is failed(%x)", error);
194
195         error = calendar_query_set_sort(query, _calendar_todo_calendar_book.due_time, direction > 0);
196         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_set_sort() is failed(%x)", error);
197
198         if (not_competed_filter) {
199                 error = calendar_filter_destroy(not_competed_filter);
200                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
201         }
202
203         error = calendar_filter_destroy(filter);
204         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
205
206         return query;
207 }
208
209 calendar_query_h cal_query_create_list_range_query(const char* view_uri,
210                 unsigned int start_time_property_id, unsigned int end_time_property_id,
211                 const struct tm* start, const struct tm* end, bool all_day)
212 {
213         c_retv_if(start == NULL, NULL);
214         c_retv_if(end == NULL, NULL);
215
216         calendar_query_h query = NULL;
217
218         calendar_error_e error = calendar_query_create(view_uri, &query);
219         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_create() is failed(%x)", error);
220
221         calendar_time_s caltime_start = {0};
222         __cal_query_get_caltime_day(start, all_day, &caltime_start);
223
224         calendar_time_s caltime_end = {0};
225         __cal_query_get_caltime_day(end, all_day, &caltime_end);
226
227         calendar_filter_h filter;
228
229         error = calendar_filter_create(view_uri, &filter);
230         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
231
232         error = calendar_filter_add_caltime(
233                                 filter, end_time_property_id, CALENDAR_MATCH_GREATER_THAN_OR_EQUAL, caltime_start);
234         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
235
236         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
237         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
238
239         error = calendar_filter_add_caltime(
240                                 filter, start_time_property_id, CALENDAR_MATCH_LESS_THAN_OR_EQUAL, caltime_end);
241         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
242
243         error = calendar_query_set_filter(query, filter);
244         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_add_filter() is failed(%x)", error);
245
246         error = calendar_query_set_sort(query, start_time_property_id, true);
247         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_set_sort() is failed(%x)", error);
248
249         error = calendar_filter_destroy(filter);
250         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
251
252         return query;
253 }
254
255 calendar_query_h cal_query_create_due_date_task_list_query(struct tm *start, struct tm *end, Eina_Bool is_show_completed_task, _calendar_task_sort_type sort_type)
256 {
257         c_retv_if(!start, NULL);
258         c_retv_if(!end, NULL);
259         c_retv_if(sort_type <= _CALENDAR_TASK_SORT_TYPE_NONE, NULL);
260         c_retv_if(_CALENDAR_TASK_SORT_TYPE_MAX <= sort_type, NULL);
261
262         calendar_query_h query = NULL;
263
264         calendar_error_e error = CALENDAR_ERROR_NONE;
265
266         error = calendar_query_create(_calendar_todo_calendar_book._uri, &query);
267         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_create() is failed(%x)", error);
268
269         calendar_time_s caltime_start = {0};
270         calendar_time_s caltime_end = {0};
271
272         long long int lli_start = 0;
273         long long int lli_end = 0;
274
275         cal_util_convert_tm_to_lli(NULL, start, &lli_start);
276         cal_util_convert_tm_to_lli(NULL, end, &lli_end);
277
278         caltime_start.type = CALENDAR_TIME_UTIME;
279         caltime_start.time.utime = lli_start;
280
281         caltime_end.type = CALENDAR_TIME_UTIME;
282         caltime_end.time.utime = lli_end;
283
284         calendar_filter_h filter = NULL;
285         calendar_filter_h status_filter = NULL;
286
287         error = calendar_filter_create(_calendar_todo_calendar_book._uri, &filter);
288         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
289
290         error = calendar_filter_add_caltime(filter, _calendar_todo_calendar_book.due_time, CALENDAR_MATCH_GREATER_THAN_OR_EQUAL, caltime_start);
291         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
292
293         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
294         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
295
296         error = calendar_filter_add_caltime(filter, _calendar_todo_calendar_book.due_time,CALENDAR_MATCH_LESS_THAN_OR_EQUAL, caltime_end);
297         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
298
299         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
300         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
301
302         error = calendar_filter_add_int(filter, _calendar_todo_calendar_book.is_deleted, CALENDAR_MATCH_EQUAL, 0);
303         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
304
305         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
306         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
307
308         error = calendar_filter_add_int(filter, _calendar_todo_calendar_book.calendar_book_visibility, CALENDAR_MATCH_EQUAL, 1);
309         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
310
311         if (!is_show_completed_task) {
312
313                 error = calendar_filter_create(_calendar_todo_calendar_book._uri, &status_filter);
314                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
315
316                 error = calendar_filter_add_int(status_filter, _calendar_todo_calendar_book.todo_status, CALENDAR_MATCH_EQUAL, CALENDAR_TODO_STATUS_NONE);
317                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
318
319                 error = calendar_filter_add_operator(status_filter, CALENDAR_FILTER_OPERATOR_OR);
320                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
321
322                 error = calendar_filter_add_int(status_filter, _calendar_todo_calendar_book.todo_status, CALENDAR_MATCH_EQUAL, CALENDAR_TODO_STATUS_NEEDS_ACTION);
323                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
324
325                 error = calendar_filter_add_operator(status_filter, CALENDAR_FILTER_OPERATOR_OR);
326                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
327
328                 error = calendar_filter_add_int(status_filter, _calendar_todo_calendar_book.todo_status, CALENDAR_MATCH_EQUAL, CALENDAR_TODO_STATUS_IN_PROCESS);
329                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
330
331                 error = calendar_filter_add_operator(status_filter, CALENDAR_FILTER_OPERATOR_OR);
332                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
333
334                 error = calendar_filter_add_int(status_filter, _calendar_todo_calendar_book.todo_status, CALENDAR_MATCH_EQUAL, CALENDAR_TODO_STATUS_CANCELED);
335                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
336
337                 error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
338                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
339
340                 error = calendar_filter_add_filter(filter, status_filter);
341                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_filter() is failed(%x)", error);
342         }
343
344         error = calendar_query_set_filter(query, filter);
345         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_set_filter() is failed(%x)", error);
346
347         switch (sort_type) {
348                 case _CALENDAR_TASK_SORT_TYPE_DUEDATE_ASC:
349                         error = calendar_query_set_sort(query, _calendar_todo_calendar_book.due_time, true);
350                         break;
351                 case _CALENDAR_TASK_SORT_TYPE_DUEDATE_DES:
352                         error = calendar_query_set_sort(query, _calendar_todo_calendar_book.due_time, false);
353                         break;
354                 case _CALENDAR_TASK_SORT_TYPE_PRIORITY_ASC:
355                         error = calendar_query_set_sort(query, _calendar_todo_calendar_book.priority, true);
356                         break;
357                 case _CALENDAR_TASK_SORT_TYPE_PRIORITY_DES:
358                         error = calendar_query_set_sort(query, _calendar_todo_calendar_book.priority, false);
359                         break;
360                 case _CALENDAR_TASK_SORT_TYPE_STATUS_ASC:
361                         error = calendar_query_set_sort(query, _calendar_todo_calendar_book.todo_status, true);
362                         break;
363                 case _CALENDAR_TASK_SORT_TYPE_STATUS_DES:
364                         error = calendar_query_set_sort(query, _calendar_todo_calendar_book.todo_status, false);
365                         break;
366         }
367
368         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_set_sort() is failed(%x)", error);
369
370         if (status_filter) {
371                 error = calendar_filter_destroy(status_filter);
372                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
373         }
374
375         error = calendar_filter_destroy(filter);
376         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
377
378         return query;
379 }
380
381 calendar_query_h cal_query_create_all_task_list_query(struct tm *start, struct tm *end, Eina_Bool is_show_completed_task, _calendar_task_sort_type sort_type)
382 {
383         c_retv_if(!start, NULL);
384         c_retv_if(!end, NULL);
385         c_retv_if(sort_type <= _CALENDAR_TASK_SORT_TYPE_NONE, NULL);
386         c_retv_if(_CALENDAR_TASK_SORT_TYPE_MAX <= sort_type, NULL);
387
388         calendar_query_h query = NULL;
389
390         calendar_error_e error = CALENDAR_ERROR_NONE;
391
392         error = calendar_query_create(_calendar_todo_calendar_book._uri, &query);
393         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_create() is failed(%x)", error);
394
395         calendar_time_s caltime_start = {0};
396         calendar_time_s caltime_end = {0};
397
398         long long int lli_start = 0;
399         long long int lli_end = 0;
400
401         cal_util_convert_tm_to_lli(NULL, start, &lli_start);
402         cal_util_convert_tm_to_lli(NULL, end, &lli_end);
403
404         caltime_start.type = CALENDAR_TIME_UTIME;
405         caltime_start.time.utime = lli_start;
406
407         caltime_end.type = CALENDAR_TIME_UTIME;
408         caltime_end.time.utime = lli_end;
409
410         calendar_filter_h filter = NULL;
411         calendar_filter_h status_filter = NULL;
412
413         error = calendar_filter_create(_calendar_todo_calendar_book._uri, &filter);
414         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
415
416         error = calendar_filter_add_int(filter, _calendar_todo_calendar_book.is_deleted, CALENDAR_MATCH_EQUAL, 0);
417         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
418
419         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
420         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
421
422         error = calendar_filter_add_int(filter, _calendar_todo_calendar_book.calendar_book_visibility, CALENDAR_MATCH_EQUAL, 1);
423         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
424
425         if (!is_show_completed_task) {
426
427                 error = calendar_filter_create(_calendar_todo_calendar_book._uri, &status_filter);
428                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
429
430                 error = calendar_filter_add_int(status_filter, _calendar_todo_calendar_book.todo_status, CALENDAR_MATCH_EQUAL, CALENDAR_TODO_STATUS_NONE);
431                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
432
433                 error = calendar_filter_add_operator(status_filter, CALENDAR_FILTER_OPERATOR_OR);
434                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
435
436                 error = calendar_filter_add_int(status_filter, _calendar_todo_calendar_book.todo_status, CALENDAR_MATCH_EQUAL, CALENDAR_TODO_STATUS_NEEDS_ACTION);
437                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
438
439                 error = calendar_filter_add_operator(status_filter, CALENDAR_FILTER_OPERATOR_OR);
440                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
441
442                 error = calendar_filter_add_int(status_filter, _calendar_todo_calendar_book.todo_status, CALENDAR_MATCH_EQUAL, CALENDAR_TODO_STATUS_IN_PROCESS);
443                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
444
445                 error = calendar_filter_add_operator(status_filter, CALENDAR_FILTER_OPERATOR_OR);
446                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
447
448                 error = calendar_filter_add_int(status_filter, _calendar_todo_calendar_book.todo_status, CALENDAR_MATCH_EQUAL, CALENDAR_TODO_STATUS_CANCELED);
449                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
450
451                 error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
452                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
453
454                 error = calendar_filter_add_filter(filter, status_filter);
455                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_filter() is failed(%x)", error);
456         }
457
458         error = calendar_query_set_filter(query, filter);
459         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_set_filter() is failed(%x)", error);
460
461         switch (sort_type) {
462                 case _CALENDAR_TASK_SORT_TYPE_DUEDATE_ASC:
463                         error = calendar_query_set_sort(query, _calendar_todo_calendar_book.due_time, true);
464                         break;
465                 case _CALENDAR_TASK_SORT_TYPE_DUEDATE_DES:
466                         error = calendar_query_set_sort(query, _calendar_todo_calendar_book.due_time, false);
467                         break;
468                 case _CALENDAR_TASK_SORT_TYPE_PRIORITY_ASC:
469                         error = calendar_query_set_sort(query, _calendar_todo_calendar_book.priority, true);
470                         break;
471                 case _CALENDAR_TASK_SORT_TYPE_PRIORITY_DES:
472                         error = calendar_query_set_sort(query, _calendar_todo_calendar_book.priority, false);
473                         break;
474                 case _CALENDAR_TASK_SORT_TYPE_STATUS_ASC:
475                         error = calendar_query_set_sort(query, _calendar_todo_calendar_book.todo_status, true);
476                         break;
477                 case _CALENDAR_TASK_SORT_TYPE_STATUS_DES:
478                         error = calendar_query_set_sort(query, _calendar_todo_calendar_book.todo_status, false);
479                         break;
480         }
481
482         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_set_sort() is failed(%x)", error);
483
484         if (status_filter) {
485                 error = calendar_filter_destroy(status_filter);
486                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
487         }
488
489         error = calendar_filter_destroy(filter);
490         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
491
492         return query;
493 }
494
495
496
497 calendar_query_h cal_query_create_task_search_query(const char* keyword)
498 {
499         c_retv_if(!CAL_STRLEN(keyword), NULL);
500
501         calendar_query_h query = NULL;
502
503         calendar_error_e error = CALENDAR_ERROR_NONE;
504
505         calendar_filter_h visibility_filter = NULL;
506
507         error = calendar_filter_create(_calendar_todo_calendar_book._uri, &visibility_filter);
508         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
509
510         error = calendar_filter_add_int(visibility_filter, _calendar_todo_calendar_book.calendar_book_visibility, CALENDAR_MATCH_EQUAL, 1);
511         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
512
513         calendar_filter_h filter = NULL;
514
515         error = calendar_query_create(_calendar_todo_calendar_book._uri, &query);
516         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_create() is failed(%x)", error);
517
518         error = calendar_filter_create(_calendar_todo_calendar_book._uri, &filter);
519         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
520
521         error = calendar_filter_add_str(filter, _calendar_todo_calendar_book.summary, CALENDAR_MATCH_CONTAINS, keyword);
522         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_str() is failed(%x)", error);
523
524         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_OR);
525         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
526
527         error = calendar_filter_add_str(filter, _calendar_todo_calendar_book.location, CALENDAR_MATCH_CONTAINS, keyword);
528         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_str() is failed(%x)", error);
529
530         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_OR);
531         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
532
533         error = calendar_filter_add_str(filter, _calendar_todo_calendar_book.description, CALENDAR_MATCH_CONTAINS, keyword);
534         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_str() is failed(%x)", error);
535
536         error = calendar_filter_add_operator(visibility_filter, CALENDAR_FILTER_OPERATOR_AND);
537         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
538
539         error = calendar_filter_add_filter(visibility_filter, filter);
540         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_filter() is failed(%x)", error);
541
542         error = calendar_query_set_filter(query, filter);
543         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_set_filter() is failed(%x)", error);
544
545         error = calendar_filter_destroy(visibility_filter);
546         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
547
548         error = calendar_filter_destroy(filter);
549         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
550
551         return query;
552 }
553
554 void cal_do_for_each_record_in_list(calendar_list_h list, cal_for_each_record_callback cb, void* data)
555 {
556         calendar_record_h record = NULL;
557         calendar_error_e error = calendar_list_get_current_record_p(list, &record);
558         while (error == CALENDAR_ERROR_NONE) {
559                 cb(record, data);
560
561                 error = calendar_list_next(list);
562                 c_ret_if(error != CALENDAR_ERROR_NONE);
563
564                 error = calendar_list_get_current_record_p(list, &record);
565         }
566 }
567
568 calendar_query_h cal_query_create_all_event_list_query(void)
569 {
570         calendar_query_h query = NULL;
571
572         calendar_error_e error = CALENDAR_ERROR_NONE;
573
574         error = calendar_query_create(_calendar_event_calendar_book._uri, &query);
575         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_create() is failed(%x)", error);
576
577         calendar_filter_h filter = NULL;
578
579         error = calendar_filter_create(_calendar_event_calendar_book._uri, &filter);
580         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
581
582         error = calendar_filter_add_int(filter, _calendar_event_calendar_book.is_deleted, CALENDAR_MATCH_EQUAL, 0);
583         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
584
585         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
586         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
587
588         error = calendar_filter_add_int(filter, _calendar_event_calendar_book.calendar_book_visibility, CALENDAR_MATCH_EQUAL, 1);
589         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
590
591         error = calendar_query_set_filter(query, filter);
592         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_set_filter() is failed(%x)", error);
593
594         error = calendar_filter_destroy(filter);
595         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
596
597         return query;
598 }
599