1b7ca33ae5a9f72ad5323356d5cef947708aecf1
[apps/core/preloaded/calendar.git] / common / cal-svc.c
1 /*
2   *
3   *  Copyright 2012  Samsung Electronics Co., Ltd
4   *
5   *  Licensed under the Flora License, Version 1.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
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 <unicode/ucal.h>
19 #include <unicode/ustring.h>
20
21 #include "cld.h"
22 #include "query.h"
23
24 static GHashTable *__calendar_record_uri_hash_table = NULL;
25
26 static _calendar_book_color __calendar_default_color[] = {
27         {90, 167, 48, 255,},
28         {221, 129, 0, 255,},
29         {0, 182, 252, 255,},
30         {122, 52, 122, 255,},
31         {22, 139, 121, 255,},
32         {40, 81, 163, 255,},
33 };
34
35 void _calendar_init_hash()
36 {
37         CAL_FN_START;
38
39         if (__calendar_record_uri_hash_table)
40                 return;
41
42         __calendar_record_uri_hash_table = g_hash_table_new(g_str_hash, g_str_equal);
43         c_ret_if(!__calendar_record_uri_hash_table);
44
45         g_hash_table_insert(__calendar_record_uri_hash_table, (gpointer)_calendar_book._uri, (gpointer)_CALENDAR_RECORD_TYPE_CALENDARBOOK);
46         g_hash_table_insert(__calendar_record_uri_hash_table, (gpointer)_calendar_event._uri, (gpointer)_CALENDAR_RECORD_TYPE_EVENT);
47         g_hash_table_insert(__calendar_record_uri_hash_table, (gpointer)_calendar_todo._uri, (gpointer)_CALENDAR_RECORD_TYPE_TODO);
48         g_hash_table_insert(__calendar_record_uri_hash_table, (gpointer)_calendar_timezone._uri, (gpointer)_CALENDAR_RECORD_TYPE_TIMEZONE);
49         g_hash_table_insert(__calendar_record_uri_hash_table, (gpointer)_calendar_attendee._uri, (gpointer)_CALENDAR_RECORD_TYPE_ATTENDEE);
50         g_hash_table_insert(__calendar_record_uri_hash_table, (gpointer)_calendar_alarm._uri, (gpointer)_CALENDAR_RECORD_TYPE_ALARM);
51         g_hash_table_insert(__calendar_record_uri_hash_table, (gpointer)_calendar_updated_info._uri, (gpointer)_CALENDAR_RECORD_TYPE_UPDATED_INFO);
52         g_hash_table_insert(__calendar_record_uri_hash_table, (gpointer)_calendar_event_calendar_book._uri, (gpointer)_CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR);
53         g_hash_table_insert(__calendar_record_uri_hash_table, (gpointer)_calendar_todo_calendar_book._uri, (gpointer)_CALENDAR_RECORD_TYPE_SEARCH_TODO_CALENDAR);
54         g_hash_table_insert(__calendar_record_uri_hash_table, (gpointer)_calendar_event_calendar_book_attendee._uri, (gpointer)_CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR_ATTENDEE);
55         g_hash_table_insert(__calendar_record_uri_hash_table, (gpointer)_calendar_instance_normal_calendar_book._uri, (gpointer)_CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_NORMAL_CALENDAR);
56         g_hash_table_insert(__calendar_record_uri_hash_table, (gpointer)_calendar_instance_allday_calendar_book._uri, (gpointer)_CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_ALLDAY_CALENDAR);
57         g_hash_table_insert(__calendar_record_uri_hash_table, (gpointer)_calendar_extended_property._uri, (gpointer)_CALENDAR_RECORD_TYPE_EXTENDED_PROPERTY);
58
59         CAL_FN_END;
60 }
61
62 _calendar_record_type _calendar_get_record_type(calendar_record_h record)
63 {
64         c_retv_if(!record, _CALENDAR_RECORD_TYPE_MAX);
65
66         char *view_uri = NULL;
67
68         calendar_error_e error = CALENDAR_ERROR_NONE;
69
70         error =  calendar_record_get_uri_p(record, &view_uri);
71         c_retvm_if(error != CALENDAR_ERROR_NONE, _CALENDAR_RECORD_TYPE_MAX, "calendar_record_get_uri_p() is failed(%x)", error);
72         c_retv_if(!CAL_STRLEN(view_uri), _CALENDAR_RECORD_TYPE_MAX);
73
74         return (_calendar_record_type)g_hash_table_lookup(__calendar_record_uri_hash_table, view_uri);
75 }
76
77 Eina_List * _calendar_get_normal_instance_list(struct tm *start, struct tm *end)
78 {
79         c_retv_if(!start, NULL);
80         c_retv_if(!end, NULL);
81
82         Eina_List *normal_instance_list = NULL;
83
84         calendar_query_h query = NULL;
85
86         calendar_error_e error = CALENDAR_ERROR_NONE;
87
88         error = calendar_query_create(_calendar_instance_normal_calendar_book._uri, &query);
89         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_create() is failed(%x)", error);
90
91         calendar_time_s caltime_start = {0};
92         calendar_time_s caltime_end = {0};
93
94         long long int lli_start = 0;
95         long long int lli_end = 0;
96
97         cal_util_convert_tm_to_lli(NULL, start, &lli_start);
98         cal_util_convert_tm_to_lli(NULL, end, &lli_end);
99
100         caltime_start.type = CALENDAR_TIME_UTIME;
101         caltime_start.time.utime = lli_start;
102
103         caltime_end.type = CALENDAR_TIME_UTIME;
104         caltime_end.time.utime = lli_end;
105
106         calendar_filter_h filter = NULL;
107
108         error = calendar_filter_create(_calendar_instance_normal_calendar_book._uri, &filter);
109         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
110
111         error = calendar_filter_add_caltime(filter, _calendar_instance_normal_calendar_book.end_time, CALENDAR_MATCH_GREATER_THAN, caltime_start);
112         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
113
114         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
115         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
116
117         error = calendar_filter_add_caltime(filter, _calendar_instance_normal_calendar_book.start_time,CALENDAR_MATCH_LESS_THAN_OR_EQUAL, caltime_end);
118         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
119
120         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
121         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
122
123         error = calendar_filter_add_int(filter, _calendar_instance_normal_calendar_book.calendar_book_visibility, CALENDAR_MATCH_EQUAL, 1);
124         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
125
126         error = calendar_query_set_filter(query, filter);
127         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_set_filter() is failed(%x)", error);
128
129         calendar_list_h list = NULL;
130
131         error = calendar_db_get_records_with_query(query, 0, 0, &list);
132         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_records_with_query() is failed(%x)", error);
133
134         int count = 0;
135
136         error = calendar_list_get_count(list, &count);
137         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_count() is failed(%x)", error);
138
139         error = calendar_list_first(list);
140         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_first() is failed(%x)", error);
141
142         int i = 0;
143
144         for (i = 0; i < count; i++) {
145
146                 calendar_record_h record = NULL;
147
148                 error = calendar_list_get_current_record_p(list, &record);
149                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_current_record_p() is failed(%x)", error);
150
151                 calendar_record_h new_record = NULL;
152
153                 error = calendar_record_clone(record, &new_record);
154                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_clone() is failed(%x)", error);
155
156                 normal_instance_list = eina_list_append(normal_instance_list, new_record);
157                 c_warn_if(!normal_instance_list, "eina_list_append() is failed.");
158
159                 calendar_list_next(list);
160         }
161
162         error = calendar_list_destroy(list, false);
163         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_destroy() is failed(%x)", error);
164
165         error = calendar_filter_destroy(filter);
166         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
167
168         error = calendar_query_destroy(query);
169         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_destroy() is failed(%x)", error);
170
171         return normal_instance_list;
172 }
173
174 Eina_List * _calendar_get_allday_instance_list(struct tm *start, struct tm *end)
175 {
176         c_retv_if(!start, NULL);
177         c_retv_if(!end, NULL);
178
179         Eina_List *allday_instance_list = NULL;
180
181         calendar_query_h query = NULL;
182
183         calendar_error_e error = CALENDAR_ERROR_NONE;
184
185         error = calendar_query_create(_calendar_instance_allday_calendar_book._uri, &query);
186         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_create() is failed(%x)", error);
187
188         calendar_time_s caltime_start = {0};
189         calendar_time_s caltime_end = {0};
190
191         caltime_start.type = CALENDAR_TIME_LOCALTIME;
192         caltime_start.time.date.year = start->tm_year + 1900;
193         caltime_start.time.date.month = start->tm_mon + 1;
194         caltime_start.time.date.mday = start->tm_mday;
195
196         caltime_end.type = CALENDAR_TIME_LOCALTIME;
197         caltime_end.time.date.year = end->tm_year + 1900;
198         caltime_end.time.date.month = end->tm_mon + 1;
199         caltime_end.time.date.mday = end->tm_mday;
200
201         calendar_filter_h filter = NULL;
202
203         error = calendar_filter_create(_calendar_instance_allday_calendar_book._uri, &filter);
204         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
205
206         error = calendar_filter_add_caltime(filter, _calendar_instance_allday_calendar_book.end_time, CALENDAR_MATCH_GREATER_THAN_OR_EQUAL, caltime_start);
207         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
208
209         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
210         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
211
212         error = calendar_filter_add_caltime(filter, _calendar_instance_allday_calendar_book.start_time,CALENDAR_MATCH_LESS_THAN_OR_EQUAL, caltime_end);
213         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
214
215         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
216         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
217
218         error = calendar_filter_add_int(filter, _calendar_instance_allday_calendar_book.calendar_book_visibility, CALENDAR_MATCH_EQUAL, 1);
219         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
220
221         error = calendar_query_set_filter(query, filter);
222         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_set_filter() is failed(%x)", error);
223
224         calendar_list_h list = NULL;
225
226         error = calendar_db_get_records_with_query(query, 0, 0, &list);
227         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_records_with_query() is failed(%x)", error);
228
229         int count = 0;
230
231         error = calendar_list_get_count(list,&count);
232         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_count() is failed(%x)", error);
233
234         error = calendar_list_first(list);
235         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_first() is failed(%x)", error);
236
237         int i = 0;
238
239         for (i = 0; i < count; i++) {
240
241                 calendar_record_h record = NULL;
242
243                 error = calendar_list_get_current_record_p(list, &record);
244                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_current_record_p() is failed(%x)", error);
245
246                 calendar_record_h new_record = NULL;
247
248                 error = calendar_record_clone(record, &new_record);
249                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_clone() is failed(%x)", error);
250
251                 allday_instance_list = eina_list_append(allday_instance_list, new_record);
252                 c_warn_if(!allday_instance_list, "eina_list_append() is failed.");
253
254                 calendar_list_next(list);
255         }
256
257         error = calendar_list_destroy(list, false);
258         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_destroy() is failed(%x)", error);
259
260         error = calendar_filter_destroy(filter);
261         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
262
263         error = calendar_query_destroy(query);
264         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_destroy() is failed(%x)", error);
265
266         return allday_instance_list;
267 }
268
269 static int __calendar_compare_list_item(const void *data1, const void *data2)
270 {
271         c_retv_if(!data1, 1);
272         c_retv_if(!data2, -1);
273
274         calendar_record_h normal_instance = (calendar_record_h)data1;
275         calendar_record_h allday_instance = (calendar_record_h)data2;
276
277         calendar_error_e error = CALENDAR_ERROR_NONE;
278
279         calendar_time_s normal_instance_start_time = {0};
280
281         error = calendar_record_get_caltime(normal_instance, _calendar_instance_normal_calendar_book.start_time, &normal_instance_start_time);
282         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_caltime() is failed(%x)", error);
283
284         long long int lli_normal = normal_instance_start_time.time.utime;
285
286         calendar_time_s allday_instance_start_time = {0};
287
288         error = calendar_record_get_caltime(allday_instance, _calendar_instance_allday_calendar_book.start_time, &allday_instance_start_time);
289         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_caltime() is failed(%x)", error);
290
291         struct tm tm_allday = {0};
292         tm_allday.tm_year = allday_instance_start_time.time.date.year - 1900;
293         tm_allday.tm_mon = allday_instance_start_time.time.date.month - 1;
294         tm_allday.tm_mday = allday_instance_start_time.time.date.mday;
295
296         long long int lli_allday;
297         cal_util_convert_tm_to_lli(NULL, &tm_allday, &lli_allday);
298
299         return (lli_allday < lli_normal) ? 1 : -1;
300 }
301
302 static int __calendar_compare_event_record_list_item(const void *data1, const void *data2)
303 {
304         c_retv_if(!data1, 1);
305         c_retv_if(!data2, -1);
306
307         calendar_record_h record1 = data1;
308         calendar_record_h record2 = data2;
309
310         calendar_time_s record1_start_time = {0};
311         calendar_time_s record2_start_time = {0};
312
313         calendar_error_e error = CALENDAR_ERROR_NONE;
314         error = calendar_record_get_caltime(record1, _calendar_instance_normal_calendar_book.start_time, &record1_start_time);
315         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_caltime() is failed(%x)", error);
316
317         error = calendar_record_get_caltime(record2, _calendar_instance_normal_calendar_book.start_time, &record2_start_time);
318         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_caltime() is failed(%x)", error);
319
320         long long int lli_normal1 = record1_start_time.time.utime;
321         long long int lli_normal2 = record2_start_time.time.utime;
322
323         return (lli_normal1 > lli_normal2) ? 1 : -1;
324 }
325
326
327 Eina_List * _calendar_get_all_instance_list(struct tm *start, struct tm *end)
328 {
329         c_retv_if(start < 0, NULL);
330         c_retv_if(end < 0, NULL);
331
332         Eina_List *allday_instance_list = _calendar_get_allday_instance_list(start, end);
333
334         Eina_List *normal_instance_list = _calendar_get_normal_instance_list(start, end);
335         normal_instance_list = eina_list_sort(normal_instance_list, 0, __calendar_compare_event_record_list_item);
336
337         allday_instance_list = eina_list_merge(allday_instance_list, normal_instance_list);
338
339         return allday_instance_list;
340 }
341
342 Eina_List * _calendar_get_all_record_list(void)
343 {
344         calendar_query_h query =
345                         cal_query_create_all_event_list_query();
346         c_retv_if(!query, NULL);
347
348         calendar_list_h list = NULL;
349         calendar_error_e error = CALENDAR_ERROR_NONE;
350
351         error = calendar_db_get_records_with_query(query, 0, 0, &list);
352         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_records_with_query() is failed(%x)", error);
353
354         int count = 0;
355
356         error = calendar_list_get_count(list, &count);
357         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_count() is failed(%x)", error);
358
359         Eina_List *event_list = NULL;
360
361         error = calendar_list_first(list);
362         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_first() is failed(%x)", error);
363
364         int i = 0;
365         for (i = 0; i < count; i++) {
366
367                 calendar_record_h record = NULL;
368
369                 error = calendar_list_get_current_record_p(list, &record);
370                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_current_record_p() is failed(%x)", error);
371
372                 calendar_record_h new_record = NULL;
373
374                 error = calendar_record_clone(record, &new_record);
375                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_clone() is failed(%x)", error);
376
377                 event_list = eina_list_append(event_list, new_record);
378                 c_warn_if(!event_list, "eina_list_append() is failed.");
379
380                 calendar_list_next(list);
381         }
382
383         error = calendar_list_destroy(list, false);
384         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_destroy() is failed(%x)", error);
385
386         error = calendar_query_destroy(query);
387         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_destroy() is failed(%x)", error);
388
389         return event_list;
390 }
391
392 Eina_Bool _calendar_is_task_record(calendar_record_h record)
393 {
394         c_retv_if(!record, EINA_FALSE);
395
396         _calendar_record_type record_type = _calendar_get_record_type(record);
397
398         if (record_type == _CALENDAR_RECORD_TYPE_TODO)
399                 return EINA_TRUE;
400         else if (record_type == _CALENDAR_RECORD_TYPE_SEARCH_TODO_CALENDAR)
401                 return EINA_TRUE;
402         else
403                 return EINA_FALSE;
404 }
405
406 Eina_Bool _calendar_is_allday_record(calendar_record_h record)
407 {
408         c_retv_if(!record, EINA_FALSE);
409
410         calendar_error_e error = CALENDAR_ERROR_NONE;
411
412         calendar_time_s start_time = {0};
413
414         _calendar_record_type record_type = _calendar_get_record_type(record);
415
416         switch (record_type) {
417                 case _CALENDAR_RECORD_TYPE_EVENT:
418                         error = calendar_record_get_caltime(record, _calendar_event.start_time, &start_time);
419                         break;
420                 case _CALENDAR_RECORD_TYPE_TODO:
421                         error = calendar_record_get_caltime(record, _calendar_todo.start_time, &start_time);
422                         break;
423                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR:
424                         error = calendar_record_get_caltime(record, _calendar_event_calendar_book.start_time, &start_time);
425                         break;
426                 case _CALENDAR_RECORD_TYPE_SEARCH_TODO_CALENDAR:
427                         error = calendar_record_get_caltime(record, _calendar_todo_calendar_book.start_time, &start_time);
428                         break;
429                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR_ATTENDEE:
430                         error = calendar_record_get_caltime(record, _calendar_event_calendar_book_attendee.start_time, &start_time);
431                         break;
432                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_NORMAL_CALENDAR:
433                         error = calendar_record_get_caltime(record, _calendar_instance_normal_calendar_book.start_time, &start_time);
434                         break;
435                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_ALLDAY_CALENDAR:
436                         error = calendar_record_get_caltime(record, _calendar_instance_allday_calendar_book.start_time, &start_time);
437                         break;
438                 default:
439                         ERR("Invaild type : %d", record_type);
440                         break;
441         }
442
443         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_caltime() is failed(%x)", error);
444
445         if (start_time.type == CALENDAR_TIME_LOCALTIME)
446                 return EINA_TRUE;
447         else
448                 return EINA_FALSE;
449 }
450
451 int _calendar_get_account_id(calendar_record_h record)
452 {
453         c_retv_if(!record, -1);
454
455         calendar_error_e error = CALENDAR_ERROR_NONE;
456
457         int calendar_book_id = 0;
458         int account_id = 0;
459
460         _calendar_record_type record_type = _calendar_get_record_type(record);
461
462         switch (record_type) {
463                 case _CALENDAR_RECORD_TYPE_EVENT:
464                         error = calendar_record_get_int(record, _calendar_event.calendar_book_id, &calendar_book_id);
465                         break;
466                 case _CALENDAR_RECORD_TYPE_TODO:
467                         error = calendar_record_get_int(record, _calendar_todo.calendar_book_id, &calendar_book_id);
468                         break;
469                 case _CALENDAR_RECORD_TYPE_CALENDARBOOK:
470                         error = calendar_record_get_int(record, _calendar_book.account_id, &account_id);
471                         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
472
473                         return account_id;
474
475                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR:
476                         error = calendar_record_get_int(record, _calendar_event_calendar_book.calendar_book_id, &calendar_book_id);
477                         break;
478                 case _CALENDAR_RECORD_TYPE_SEARCH_TODO_CALENDAR:
479                         error = calendar_record_get_int(record, _calendar_todo_calendar_book.calendar_book_id, &calendar_book_id);
480                         break;
481                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR_ATTENDEE:
482                         error = calendar_record_get_int(record, _calendar_event_calendar_book_attendee.calendar_book_id, &calendar_book_id);
483                         break;
484                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_NORMAL_CALENDAR:
485                         error = calendar_record_get_int(record, _calendar_instance_normal_calendar_book.calendar_book_id, &calendar_book_id);
486                         break;
487                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_ALLDAY_CALENDAR:
488                         error = calendar_record_get_int(record, _calendar_instance_allday_calendar_book.calendar_book_id, &calendar_book_id);
489                         break;
490                 default:
491                         ERR("Invaild type : %d", record_type);
492                         break;
493         }
494
495         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
496
497         calendar_record_h calendar_book = NULL;
498
499         error = calendar_db_get_record(_calendar_book._uri, calendar_book_id, &calendar_book);
500         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_record() is failed(%x)", error);
501
502
503
504         error = calendar_record_get_int(calendar_book, _calendar_book.account_id, &account_id);
505         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
506
507         if (calendar_book) {
508                 error = calendar_record_destroy(calendar_book, true);
509                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_destroy() is failed(%x)", error);
510         }
511
512         return account_id;
513 }
514
515 char * _calendar_get_calendar_name(calendar_record_h record)
516 {
517         c_retv_if(!record, NULL);
518
519         calendar_error_e error = CALENDAR_ERROR_NONE;
520
521         int calendar_book_id = 0;
522
523         calendar_record_h calendar_book = NULL;
524
525         _calendar_record_type record_type = _calendar_get_record_type(record);
526
527         switch (record_type) {
528                 case _CALENDAR_RECORD_TYPE_CALENDARBOOK:
529                         calendar_book = record;
530                         break;
531                 case _CALENDAR_RECORD_TYPE_EVENT:
532                         error = calendar_record_get_int(record, _calendar_event.calendar_book_id, &calendar_book_id);
533                         break;
534                 case _CALENDAR_RECORD_TYPE_TODO:
535                         error = calendar_record_get_int(record, _calendar_todo.calendar_book_id, &calendar_book_id);
536                         break;
537                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR:
538                         error = calendar_record_get_int(record, _calendar_event_calendar_book.calendar_book_id, &calendar_book_id);
539                         break;
540                 case _CALENDAR_RECORD_TYPE_SEARCH_TODO_CALENDAR:
541                         error = calendar_record_get_int(record, _calendar_todo_calendar_book.calendar_book_id, &calendar_book_id);
542                         break;
543                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR_ATTENDEE:
544                         error = calendar_record_get_int(record, _calendar_event_calendar_book_attendee.calendar_book_id, &calendar_book_id);
545                         break;
546                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_NORMAL_CALENDAR:
547                         error = calendar_record_get_int(record, _calendar_instance_normal_calendar_book.calendar_book_id, &calendar_book_id);
548                         break;
549                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_ALLDAY_CALENDAR:
550                         error = calendar_record_get_int(record, _calendar_instance_allday_calendar_book.calendar_book_id, &calendar_book_id);
551                         break;
552                 default:
553                         ERR("Invaild type : %d", record_type);
554                         break;
555         }
556
557         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
558
559         if (!calendar_book) {
560
561                 error = calendar_db_get_record(_calendar_book._uri, calendar_book_id, &calendar_book);
562                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_record() is failed(%x)", error);
563         }
564
565         char *calendar_name = NULL;
566
567         error = calendar_record_get_str(calendar_book, _calendar_book.name, &calendar_name);
568         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
569
570         if (calendar_book != record) {
571                 error = calendar_record_destroy(calendar_book, true);
572                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_destroy() is failed(%x)", error);
573         }
574
575         return CAL_STRDUP(calendar_name);
576 }
577
578 void _calendar_get_calendar_color(calendar_record_h record, _calendar_book_color* calendar_color )
579 {
580         c_ret_if(!record);
581         c_ret_if(!calendar_color);
582
583         calendar_error_e error = CALENDAR_ERROR_NONE;
584
585         calendar_record_h calendar_book = NULL;
586
587         _calendar_record_type record_type = _calendar_get_record_type(record);
588
589         if (record_type == _CALENDAR_RECORD_TYPE_CALENDARBOOK)
590                 calendar_book = record;
591         else {
592                 int calendar_book_id = _calendar_get_calendar_index(record);
593
594                 error = calendar_db_get_record(_calendar_book._uri, calendar_book_id, &calendar_book);
595                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_record(%d) is failed(%x)", calendar_book_id, error);
596         }
597
598         char *color = NULL;
599
600         error = calendar_record_get_str(calendar_book, _calendar_book.color, &color);
601         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_str() is failed(%x)", error);
602
603         char* token = NULL;
604         char* temp = NULL;
605
606         if (8 < CAL_STRLEN(color)) {
607
608                 token = strtok_r(color, ".", &temp);
609                 if (token)
610                         calendar_color->red = atoi(token);
611                 else
612                         calendar_color->red = 0;
613
614                 token = strtok_r(NULL, ".", &temp);
615                 if (token)
616                         calendar_color->green = atoi(token);
617                 else
618                         calendar_color->green = 0;
619
620                 token = strtok_r(NULL, ".", &temp);
621                 if (token)
622                         calendar_color->blue = atoi(token);
623                 else
624                         calendar_color->blue = 0;
625
626                 token = strtok_r(NULL, ".", &temp);
627                 if (token)
628                         calendar_color->alpha = atoi(token);
629                 else
630                         calendar_color->alpha = 255;
631         } else
632                 *calendar_color = __calendar_default_color[_calendar_get_record_index(calendar_book)%6];
633
634         if (color)
635                 free(color);
636
637         if (calendar_book != record) {
638                 error = calendar_record_destroy(calendar_book, true);
639                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_destroy() is failed(%x)", error);
640         }
641 }
642
643 int _calendar_get_calendar_index(calendar_record_h record)
644 {
645         c_retv_if(!record, -1);
646
647         calendar_error_e error = CALENDAR_ERROR_NONE;
648
649         _calendar_record_type record_type = _calendar_get_record_type(record);
650
651         int calendar_book_id = 0;
652
653         switch (record_type) {
654                 case _CALENDAR_RECORD_TYPE_CALENDARBOOK:
655                         error = calendar_record_get_int(record, _calendar_book.id, &calendar_book_id);
656                         break;
657                 case _CALENDAR_RECORD_TYPE_EVENT:
658                         error = calendar_record_get_int(record, _calendar_event.calendar_book_id, &calendar_book_id);
659                         break;
660                 case _CALENDAR_RECORD_TYPE_TODO:
661                         error = calendar_record_get_int(record, _calendar_todo.calendar_book_id, &calendar_book_id);
662                         break;
663                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR:
664                         error = calendar_record_get_int(record, _calendar_event_calendar_book.calendar_book_id, &calendar_book_id);
665                         break;
666                 case _CALENDAR_RECORD_TYPE_SEARCH_TODO_CALENDAR:
667                         error = calendar_record_get_int(record, _calendar_todo_calendar_book.calendar_book_id, &calendar_book_id);
668                         break;
669                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR_ATTENDEE:
670                         error = calendar_record_get_int(record, _calendar_event_calendar_book_attendee.calendar_book_id, &calendar_book_id);
671                         break;
672                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_NORMAL_CALENDAR:
673                         error = calendar_record_get_int(record, _calendar_instance_normal_calendar_book.calendar_book_id, &calendar_book_id);
674                         break;
675                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_ALLDAY_CALENDAR:
676                         error = calendar_record_get_int(record, _calendar_instance_allday_calendar_book.calendar_book_id, &calendar_book_id);
677                         break;
678                 default:
679                         ERR("Invaild type : %d", record_type);
680                         break;
681         }
682
683         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
684
685         return calendar_book_id;
686 }
687
688 static char * __calendar_get_birthday_contact_title(calendar_record_h record)
689 {
690         c_retv_if(!record, NULL);
691
692         calendar_record_h origin_calendar_record = NULL;
693         calendar_error_e error = CALENDAR_ERROR_NONE;
694
695         if (_calendar_get_record_type(record) == _CALENDAR_RECORD_TYPE_EVENT)
696                 origin_calendar_record = record;
697         else
698         {
699                 int id = _calendar_get_record_index(record);
700
701                 error = calendar_db_get_record(_calendar_event._uri, id, &origin_calendar_record);
702                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_record(%d) is failed(%x)", id, error);
703         }
704
705         char *sync_data1 = NULL;
706         char *summary = NULL;
707
708         error = calendar_record_get_str_p(origin_calendar_record, _calendar_event.sync_data1, &sync_data1);
709         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_str_p() is failed(%x)", error);
710
711         error = calendar_record_get_str_p(origin_calendar_record, _calendar_event.summary, &summary);
712         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_str_p() is failed(%x)", error);
713
714         int title_len = strlen(summary)+50;
715         char *contact_title = calloc(1, title_len);
716         c_retv_if(!contact_title, NULL);
717         char *format = NULL;
718
719         if (!CAL_STRCMP(sync_data1, "anniversary"))
720         {
721                 format = C_("IDS_CLD_BODY_PSS_ANNIVERSARY");
722                 snprintf(contact_title, title_len-1, format, summary);
723         }
724         else if (!CAL_STRCMP(sync_data1, "birthday"))
725         {
726                 format = C_("IDS_CLD_POP_NAMES_BIRTHDAY");
727                 snprintf(contact_title, title_len-1, format, summary);
728         }
729         else
730                 snprintf(contact_title, title_len-1, "%s.%s", summary, sync_data1);
731
732         if (origin_calendar_record != record) {
733                 error = calendar_record_destroy(origin_calendar_record, true);
734                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_destroy() is failed(%x)", error);
735         }
736
737         return contact_title;
738 }
739
740 static Eina_Bool __calendar_is_birthday_contact_record(calendar_record_h record)
741 {
742         c_retv_if(!record, EINA_FALSE);
743
744         int calendar_id = _calendar_get_calendar_index(record);
745         if(calendar_id == DEFAULT_BIRTHDAY_CALENDAR_BOOK_ID )
746                 return EINA_TRUE;
747
748         return EINA_FALSE;
749 }
750
751 char * _calendar_get_summary(calendar_record_h record)
752 {
753         c_retv_if(!record, NULL);
754
755         calendar_error_e error = CALENDAR_ERROR_NONE;
756
757         _calendar_record_type record_type = _calendar_get_record_type(record);
758
759         char *summary = NULL;
760
761         if(__calendar_is_birthday_contact_record(record))
762         {
763                 summary = __calendar_get_birthday_contact_title(record);
764                 return summary;
765         }
766
767         switch (record_type) {
768                 case _CALENDAR_RECORD_TYPE_EVENT:
769                         error = calendar_record_get_str(record, _calendar_event.summary, &summary);
770                         break;
771                 case _CALENDAR_RECORD_TYPE_TODO:
772                         error = calendar_record_get_str(record, _calendar_todo.summary, &summary);
773                         break;
774                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR:
775                         error = calendar_record_get_str(record, _calendar_event_calendar_book.summary, &summary);
776                         break;
777                 case _CALENDAR_RECORD_TYPE_SEARCH_TODO_CALENDAR:
778                         error = calendar_record_get_str(record, _calendar_todo_calendar_book.summary, &summary);
779                         break;
780                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR_ATTENDEE:
781                         error = calendar_record_get_str(record, _calendar_event_calendar_book_attendee.summary, &summary);
782                         break;
783                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_NORMAL_CALENDAR:
784                         error = calendar_record_get_str(record, _calendar_instance_normal_calendar_book.summary, &summary);
785                         break;
786                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_ALLDAY_CALENDAR:
787                         error = calendar_record_get_str(record, _calendar_instance_allday_calendar_book.summary, &summary);
788                         break;
789                 default:
790                         ERR("Invaild type : %d", record_type);
791                         break;
792         }
793
794         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_str() is failed(%x)", error);
795
796         if (!CAL_STRLEN(summary)) {
797
798                 CAL_FREE(summary);
799
800                 return strdup(C_("IDS_CLD_BODY_NO_TITLE"));
801         }
802
803         return summary;
804 }
805
806 char * _calendar_get_location(calendar_record_h record)
807 {
808         c_retv_if(!record, NULL);
809
810         calendar_error_e error = CALENDAR_ERROR_NONE;
811
812         _calendar_record_type record_type = _calendar_get_record_type(record);
813
814         char *location = NULL;
815
816         switch (record_type) {
817                 case _CALENDAR_RECORD_TYPE_EVENT:
818                         error = calendar_record_get_str(record, _calendar_event.location, &location);
819                         break;
820                 case _CALENDAR_RECORD_TYPE_TODO:
821                         error = calendar_record_get_str(record, _calendar_todo.location, &location);
822                         break;
823                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR:
824                         error = calendar_record_get_str(record, _calendar_event_calendar_book.location, &location);
825                         break;
826                 case _CALENDAR_RECORD_TYPE_SEARCH_TODO_CALENDAR:
827                         error = calendar_record_get_str(record, _calendar_todo_calendar_book.location, &location);
828                         break;
829                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR_ATTENDEE:
830                         error = calendar_record_get_str(record, _calendar_event_calendar_book_attendee.location, &location);
831                         break;
832                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_NORMAL_CALENDAR:
833                         error = calendar_record_get_str(record, _calendar_instance_normal_calendar_book.location, &location);
834                         break;
835                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_ALLDAY_CALENDAR:
836                         error = calendar_record_get_str(record, _calendar_instance_allday_calendar_book.location, &location);
837                         break;
838                 default:
839                         ERR("Invaild type : %d", record_type);
840                         break;
841         }
842
843         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_str() is failed(%x)", error);
844
845         return location;
846 }
847
848
849 void _calendar_free_record_list(Eina_List **record_list)
850 {
851         c_ret_if(!record_list);
852
853         if (!*record_list)
854                 return;
855
856         calendar_record_h record = NULL;
857         Eina_List *list = NULL;
858
859         EINA_LIST_FOREACH(*record_list, list, record) {
860                 if (!record)
861                         continue;
862
863                 calendar_error_e error = CALENDAR_ERROR_NONE;
864
865                 error = calendar_record_destroy(record, true);
866                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_destroy() is failed(%x)", error);
867         }
868
869         *record_list = eina_list_free(*record_list);
870         c_warn_if(*record_list, "eina_list_free() is failed()");
871 }
872
873 void _calendar_get_start_time(calendar_record_h record, calendar_time_s *start_time)
874 {
875         c_ret_if(!record);
876         c_ret_if(!start_time);
877
878         calendar_error_e error = CALENDAR_ERROR_NONE;
879
880         _calendar_record_type record_type = _calendar_get_record_type(record);
881
882         switch (record_type) {
883                 case _CALENDAR_RECORD_TYPE_EVENT:
884                         error = calendar_record_get_caltime(record, _calendar_event.start_time, start_time);
885                         break;
886                 case _CALENDAR_RECORD_TYPE_TODO:
887                         error = calendar_record_get_caltime(record, _calendar_todo.due_time, start_time);       // !!!
888                         break;
889                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR:
890                         error = calendar_record_get_caltime(record, _calendar_event_calendar_book.start_time, start_time);
891                         break;
892                 case _CALENDAR_RECORD_TYPE_SEARCH_TODO_CALENDAR:
893                         error = calendar_record_get_caltime(record, _calendar_todo_calendar_book.due_time, start_time);         // !!!
894                         break;
895                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR_ATTENDEE:
896                         error = calendar_record_get_caltime(record, _calendar_event_calendar_book_attendee.start_time, start_time);
897                         break;
898                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_NORMAL_CALENDAR:
899                         error = calendar_record_get_caltime(record, _calendar_instance_normal_calendar_book.start_time, start_time);
900                         break;
901                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_ALLDAY_CALENDAR:
902                         error = calendar_record_get_caltime(record, _calendar_instance_allday_calendar_book.start_time, start_time);
903                         break;
904                 default:
905                         ERR("Invaild type : %d", record_type);
906                         break;
907         }
908
909         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_caltime() is failed(%x)", error);
910 }
911
912 void _calendar_get_end_time(calendar_record_h record, calendar_time_s *end_time)
913 {
914         c_ret_if(!record);
915         c_ret_if(!end_time);
916
917         calendar_error_e error = CALENDAR_ERROR_NONE;
918
919         _calendar_record_type record_type = _calendar_get_record_type(record);
920
921         switch (record_type) {
922                 case _CALENDAR_RECORD_TYPE_EVENT:
923                         error = calendar_record_get_caltime(record, _calendar_event.end_time, end_time);
924                         break;
925                 case _CALENDAR_RECORD_TYPE_TODO:
926                         error = calendar_record_get_caltime(record, _calendar_todo.due_time, end_time);
927                         break;
928                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR:
929                         error = calendar_record_get_caltime(record, _calendar_event_calendar_book.end_time, end_time);
930                         break;
931                 case _CALENDAR_RECORD_TYPE_SEARCH_TODO_CALENDAR:
932                         error = calendar_record_get_caltime(record, _calendar_todo_calendar_book.due_time, end_time);
933                         break;
934                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR_ATTENDEE:
935                         error = calendar_record_get_caltime(record, _calendar_event_calendar_book_attendee.end_time, end_time);
936                         break;
937                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_NORMAL_CALENDAR:
938                         error = calendar_record_get_caltime(record, _calendar_instance_normal_calendar_book.end_time, end_time);
939                         break;
940                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_ALLDAY_CALENDAR:
941                         error = calendar_record_get_caltime(record, _calendar_instance_allday_calendar_book.end_time, end_time);
942                         break;
943                 default:
944                         ERR("Invaild type : %d", record_type);
945                         break;
946         }
947
948         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_caltime() is failed(%x)", error);
949 }
950
951 void _calendar_set_start_time(calendar_record_h record, calendar_time_s *start_time)
952 {
953         c_ret_if(!record);
954         c_ret_if(!start_time);
955
956         calendar_error_e error = CALENDAR_ERROR_NONE;
957
958         _calendar_record_type record_type = _calendar_get_record_type(record);
959
960         switch (record_type) {
961                 case _CALENDAR_RECORD_TYPE_EVENT:
962                         error = calendar_record_set_caltime(record, _calendar_event.start_time, *start_time);
963                         break;
964                 case _CALENDAR_RECORD_TYPE_TODO:
965                         error = calendar_record_set_caltime(record, _calendar_todo.start_time, *start_time);
966                         break;
967                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR:
968                         error = calendar_record_set_caltime(record, _calendar_event_calendar_book.start_time, *start_time);
969                         break;
970                 case _CALENDAR_RECORD_TYPE_SEARCH_TODO_CALENDAR:
971                         error = calendar_record_set_caltime(record, _calendar_todo_calendar_book.start_time, *start_time);
972                         break;
973                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR_ATTENDEE:
974                         error = calendar_record_set_caltime(record, _calendar_event_calendar_book_attendee.start_time, *start_time);
975                         break;
976                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_NORMAL_CALENDAR:
977                         error = calendar_record_set_caltime(record, _calendar_instance_normal_calendar_book.start_time, *start_time);
978                         break;
979                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_ALLDAY_CALENDAR:
980                         error = calendar_record_set_caltime(record, _calendar_instance_allday_calendar_book.start_time, *start_time);
981                         break;
982                 default:
983                         ERR("Invaild type : %d", record_type);
984                         break;
985         }
986
987         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_caltime() is failed(%x)", error);
988 }
989
990 void _calendar_set_end_time(calendar_record_h record, calendar_time_s *end_time)
991 {
992         c_ret_if(!record);
993         c_ret_if(!end_time);
994
995         calendar_error_e error = CALENDAR_ERROR_NONE;
996
997         _calendar_record_type record_type = _calendar_get_record_type(record);
998
999         switch (record_type) {
1000                 case _CALENDAR_RECORD_TYPE_EVENT:
1001                         error = calendar_record_set_caltime(record, _calendar_event.end_time, *end_time);
1002                         break;
1003                 case _CALENDAR_RECORD_TYPE_TODO:
1004                         error = calendar_record_set_caltime(record, _calendar_todo.due_time, *end_time);
1005                         break;
1006                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR:
1007                         error = calendar_record_set_caltime(record, _calendar_event_calendar_book.end_time, *end_time);
1008                         break;
1009                 case _CALENDAR_RECORD_TYPE_SEARCH_TODO_CALENDAR:
1010                         error = calendar_record_set_caltime(record, _calendar_todo_calendar_book.due_time, *end_time);
1011                         break;
1012                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR_ATTENDEE:
1013                         error = calendar_record_set_caltime(record, _calendar_event_calendar_book_attendee.end_time, *end_time);
1014                         break;
1015                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_NORMAL_CALENDAR:
1016                         error = calendar_record_set_caltime(record, _calendar_instance_normal_calendar_book.end_time, *end_time);
1017                         break;
1018                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_ALLDAY_CALENDAR:
1019                         error = calendar_record_set_caltime(record, _calendar_instance_allday_calendar_book.end_time, *end_time);
1020                         break;
1021                 default:
1022                         ERR("Invaild type : %d", record_type);
1023                         break;
1024         }
1025
1026         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_caltime() is failed(%x)", error);
1027 }
1028
1029
1030 void _calendar_delete_record_with_index(int index)
1031 {
1032         c_ret_if(index < 0);
1033
1034         calendar_error_e error = CALENDAR_ERROR_NONE;
1035
1036         error = calendar_db_delete_record(_calendar_event._uri, index);
1037
1038         if (error != CALENDAR_ERROR_NONE)
1039                 error = calendar_db_delete_record(_calendar_todo._uri, index);
1040
1041         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_delete_record() is failed(%x)", error);
1042 }
1043
1044 void _calendar_delete_record(calendar_record_h record)
1045 {
1046         c_ret_if(!record);
1047
1048         calendar_error_e error = CALENDAR_ERROR_NONE;
1049
1050         int index = _calendar_get_record_index(record);
1051
1052         _calendar_record_type record_type = _calendar_get_record_type(record);
1053
1054         switch (record_type) {
1055                 case _CALENDAR_RECORD_TYPE_TODO:
1056                         error = calendar_db_delete_record(_calendar_todo._uri, index);
1057                         break;
1058                 default:
1059                         error = calendar_db_delete_record(_calendar_event._uri, index);
1060                         break;
1061         }
1062
1063         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_delete_record() is failed(%x)", error);
1064 }
1065
1066 Eina_Bool _calendar_is_recurrent_record(calendar_record_h record)
1067 {
1068         c_retv_if(!record, EINA_FALSE);
1069
1070         calendar_error_e error = CALENDAR_ERROR_NONE;
1071
1072         _calendar_record_type record_type = _calendar_get_record_type(record);
1073
1074         calendar_recurrence_frequency_e frequency = CALENDAR_RECURRENCE_NONE;
1075
1076         switch (record_type) {
1077                 case _CALENDAR_RECORD_TYPE_EVENT:
1078                         error = calendar_record_get_int(record, _calendar_event.freq, (int *)&frequency);
1079                         break;
1080                 case _CALENDAR_RECORD_TYPE_TODO:
1081                         error = calendar_record_get_int(record, _calendar_todo.freq, (int *)&frequency);
1082                         break;
1083                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR:
1084                         error = calendar_record_get_int(record, _calendar_event_calendar_book.freq, (int *)&frequency);
1085                         break;
1086                 case _CALENDAR_RECORD_TYPE_SEARCH_TODO_CALENDAR:
1087                         error = calendar_record_get_int(record, _calendar_todo_calendar_book.freq, (int *)&frequency);
1088                         break;
1089                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR_ATTENDEE:
1090                         error = calendar_record_get_int(record, _calendar_event_calendar_book_attendee.freq, (int *)&frequency);
1091                         break;
1092                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_NORMAL_CALENDAR:
1093                         error = calendar_record_get_int(record, _calendar_instance_normal_calendar_book.has_rrule, (int *)&frequency);
1094                         break;
1095                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_ALLDAY_CALENDAR:
1096                         error = calendar_record_get_int(record, _calendar_instance_allday_calendar_book.has_rrule, (int *)&frequency);
1097                         break;
1098                 default:
1099                         ERR("Invaild type : %d", record_type);
1100                         break;
1101         }
1102
1103         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
1104
1105         if (frequency == CALENDAR_RECURRENCE_NONE)
1106                 return EINA_FALSE;
1107         else
1108                 return EINA_TRUE;
1109 }
1110
1111 Eina_Bool _calendar_has_reminder(calendar_record_h record)
1112 {
1113         c_retv_if(!record, EINA_FALSE);
1114
1115         calendar_error_e error = CALENDAR_ERROR_NONE;
1116
1117         _calendar_record_type record_type = _calendar_get_record_type(record);
1118
1119         int has_reminder = 0;
1120
1121         switch (record_type) {
1122                 case _CALENDAR_RECORD_TYPE_EVENT:
1123                         error = calendar_record_get_int(record, _calendar_event.has_alarm, &has_reminder);
1124                         break;
1125                 case _CALENDAR_RECORD_TYPE_TODO:
1126                         error = calendar_record_get_int(record, _calendar_todo.has_alarm, &has_reminder);
1127                         break;
1128                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR:
1129                         error = calendar_record_get_int(record, _calendar_event_calendar_book.has_alarm, &has_reminder);
1130                         break;
1131                 case _CALENDAR_RECORD_TYPE_SEARCH_TODO_CALENDAR:
1132                         error = calendar_record_get_int(record, _calendar_todo_calendar_book.has_alarm, &has_reminder);
1133                         break;
1134                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR_ATTENDEE:
1135                         error = calendar_record_get_int(record, _calendar_event_calendar_book_attendee.has_alarm, &has_reminder);
1136                         break;
1137                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_NORMAL_CALENDAR:
1138                         error = calendar_record_get_int(record, _calendar_instance_normal_calendar_book.has_alarm, &has_reminder);
1139                         break;
1140                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_ALLDAY_CALENDAR:
1141                         error = calendar_record_get_int(record, _calendar_instance_allday_calendar_book.has_alarm, &has_reminder);
1142                         break;
1143                 default:
1144                         ERR("Invaild type : %d", record_type);
1145                         break;
1146         }
1147
1148         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
1149
1150         _calendar_show_error(error);
1151
1152         if (has_reminder)
1153                 return EINA_TRUE;
1154         else
1155                 return EINA_FALSE;
1156 }
1157
1158
1159 calendar_record_h _calendar_get_record_with_index(int index)
1160 {
1161         c_retv_if(index < 0, NULL);
1162
1163         calendar_record_h record = NULL;
1164
1165         calendar_error_e error = CALENDAR_ERROR_NONE;
1166
1167         error = calendar_db_get_record(_calendar_event._uri, index, &record);
1168
1169         if (error != CALENDAR_ERROR_NONE)
1170                 error = calendar_db_get_record(_calendar_todo._uri, index, &record);
1171
1172         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_delete_record() is failed(%x)", error);
1173
1174         return record;
1175 }
1176
1177 int _calendar_get_record_index(calendar_record_h record)
1178 {
1179         c_retv_if(!record, -1);
1180
1181         calendar_error_e error = CALENDAR_ERROR_NONE;
1182
1183         int index = -1;
1184
1185         _calendar_record_type record_type = _calendar_get_record_type(record);
1186
1187         switch (record_type) {
1188                 case _CALENDAR_RECORD_TYPE_EVENT:
1189                         error = calendar_record_get_int(record, _calendar_event.id, &index);
1190                         break;
1191                 case _CALENDAR_RECORD_TYPE_TODO:
1192                         error = calendar_record_get_int(record, _calendar_todo.id, &index);
1193                         break;
1194                 case _CALENDAR_RECORD_TYPE_CALENDARBOOK:
1195                         error = calendar_record_get_int(record, _calendar_book.id, &index);
1196                         break;
1197                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR:
1198                         error = calendar_record_get_int(record, _calendar_event_calendar_book.event_id, &index);
1199                         break;
1200                 case _CALENDAR_RECORD_TYPE_SEARCH_TODO_CALENDAR:
1201                         error = calendar_record_get_int(record, _calendar_todo_calendar_book.todo_id, &index);
1202                         break;
1203                 case _CALENDAR_RECORD_TYPE_SEARCH_EVENT_CALENDAR_ATTENDEE:
1204                         error = calendar_record_get_int(record, _calendar_event_calendar_book_attendee.event_id, &index);
1205                         break;
1206                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_NORMAL_CALENDAR:
1207                         error = calendar_record_get_int(record, _calendar_instance_normal_calendar_book.event_id, &index);
1208                         break;
1209                 case _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_ALLDAY_CALENDAR:
1210                         error = calendar_record_get_int(record, _calendar_instance_allday_calendar_book.event_id, &index);
1211                         break;
1212                 case _CALENDAR_RECORD_TYPE_EXTENDED_PROPERTY:
1213                         error = calendar_record_get_int(record, _calendar_extended_property.id, &index);
1214                         break;
1215                 default:
1216                         ERR("Invaild type : %d", record_type);
1217                         break;
1218         }
1219
1220         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
1221
1222         return index;
1223 }
1224
1225 void _calendar_export_record_to_vcs(calendar_record_h record, const char *file_path)
1226 {
1227         c_ret_if(!record);
1228         c_ret_if(!file_path);
1229
1230         calendar_list_h list = NULL;
1231
1232         calendar_error_e error = CALENDAR_ERROR_NONE;
1233
1234         error = calendar_list_create(&list);
1235         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_create() is failed(%x)");
1236
1237         error = calendar_list_add(list, record);
1238         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_add() is failed(%x)");
1239
1240         char *vcalendar_strem = NULL;
1241
1242         error = calendar_vcalendar_make_from_records(list, &vcalendar_strem);
1243         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_vcalendar_make_from_records() is failed(%x)");
1244
1245         int fd = open(file_path, O_WRONLY|O_CREAT|O_TRUNC, 0644);
1246         if(fd < 0){
1247                 ERR("open(%s) is failed.", file_path);
1248                 free(vcalendar_strem);
1249                 error = calendar_list_destroy(list, false);
1250                 return;
1251         }
1252
1253         int ret = 0;
1254
1255         if (0 <= fd) {
1256
1257                 if (vcalendar_strem) {
1258                         ret = write(fd, vcalendar_strem, strlen(vcalendar_strem));
1259                         if (ret < 0) {
1260                                 ERR("write() is failed");
1261
1262                         } else
1263                                 cal_util_update_media_db(file_path);
1264
1265                         free(vcalendar_strem);
1266                 }
1267
1268                 ret = fsync(fd);
1269                 c_warn_if(ret < 0, "fsync(%d) is failed.", fd);
1270
1271                 close(fd);
1272         }
1273
1274         error = calendar_list_destroy(list, false);
1275 }
1276
1277 int * _calendar_get_month(struct tm *start, struct tm *end, int is_display_completed_todo)
1278 {
1279         CAL_FN_START;
1280
1281         c_retv_if(!start, NULL);
1282         c_retv_if(!end, NULL);
1283
1284         int *month = calloc(31, sizeof(int));
1285         c_retv_if(!month, NULL);
1286
1287         calendar_query_h query = NULL;
1288
1289         calendar_error_e error = CALENDAR_ERROR_NONE;
1290
1291         calendar_time_s caltime_start = {0};
1292         calendar_time_s caltime_end = {0};
1293
1294         long long int lli_start = 0;
1295         long long int lli_end = 0;
1296
1297         struct tm tm_start = {0};
1298         struct tm tm_end = {0};
1299
1300         cal_util_convert_tm_to_lli(NULL, start, &lli_start);
1301         cal_util_convert_tm_to_lli(NULL, end, &lli_end);
1302
1303         caltime_start.type = CALENDAR_TIME_UTIME;
1304         caltime_start.time.utime = lli_start;
1305
1306         caltime_end.type = CALENDAR_TIME_UTIME;
1307         caltime_end.time.utime = lli_end;
1308
1309         calendar_filter_h filter = NULL;
1310
1311         error = calendar_query_create(_calendar_instance_normal_calendar_book._uri, &query);
1312         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_create() is failed(%x)", error);
1313
1314         error = calendar_filter_create(_calendar_instance_normal_calendar_book._uri, &filter);
1315         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
1316
1317         error = calendar_filter_add_caltime(filter, _calendar_instance_normal_calendar_book.end_time, CALENDAR_MATCH_GREATER_THAN, caltime_start);
1318         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
1319
1320         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
1321         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
1322
1323         error = calendar_filter_add_caltime(filter, _calendar_instance_normal_calendar_book.start_time,CALENDAR_MATCH_LESS_THAN_OR_EQUAL, caltime_end);
1324         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
1325
1326         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
1327         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
1328
1329         error = calendar_filter_add_int(filter, _calendar_instance_normal_calendar_book.calendar_book_visibility, CALENDAR_MATCH_EQUAL, 1);
1330         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
1331
1332         error = calendar_query_set_filter(query, filter);
1333         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_set_filter() is failed(%x)", error);
1334
1335         calendar_list_h list = NULL;
1336
1337         error = calendar_db_get_records_with_query(query, 0, 0, &list);
1338         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_records_with_query() is failed(%x)", error);
1339
1340         int count = 0;
1341
1342         error = calendar_list_get_count(list,&count);
1343         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_count() is failed(%x)", error);
1344
1345         error = calendar_list_first(list);
1346         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_first() is failed(%x)", error);
1347
1348         int i = 0;
1349         int index = 0;
1350
1351         for (index = 0; index < count; index++) {
1352
1353                 calendar_record_h record = NULL;
1354
1355                 error = calendar_list_get_current_record_p(list, &record);
1356                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_current_record_p() is failed(%x)", error);
1357
1358                 _calendar_get_start_time(record, &caltime_start);
1359                 _calendar_get_end_time(record, &caltime_end);
1360
1361                 cal_util_convert_lli_to_tm(NULL, caltime_start.time.utime, &tm_start);
1362                 cal_util_convert_lli_to_tm(NULL, caltime_end.time.utime, &tm_end);
1363
1364                 for (i = tm_start.tm_mday; i <= tm_end.tm_mday; i++)
1365                         month[i - 1] += 1;
1366
1367                 calendar_list_next(list);
1368         }
1369
1370         error = calendar_list_destroy(list, true);
1371         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_destroy() is failed(%x)", error);
1372         list = NULL;
1373
1374         error = calendar_filter_destroy(filter);
1375         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
1376         filter = NULL;
1377
1378         error = calendar_query_destroy(query);
1379         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_destroy() is failed(%x)", error);
1380         query = NULL;
1381
1382         error = calendar_query_create(_calendar_todo_calendar_book._uri, &query);
1383         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_create() is failed(%x)", error);
1384
1385         error = calendar_filter_create(_calendar_todo_calendar_book._uri, &filter);
1386         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
1387
1388         caltime_start.time.utime = lli_start;
1389         caltime_end.time.utime = lli_end;
1390
1391         error = calendar_filter_add_caltime(filter, _calendar_todo_calendar_book.due_time, CALENDAR_MATCH_GREATER_THAN, caltime_start);
1392         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
1393
1394         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
1395         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
1396
1397         error = calendar_filter_add_caltime(filter, _calendar_todo_calendar_book.due_time,CALENDAR_MATCH_LESS_THAN_OR_EQUAL, caltime_end);
1398         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
1399
1400         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
1401         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
1402
1403         error = calendar_filter_add_int(filter, _calendar_todo_calendar_book.is_deleted, CALENDAR_MATCH_EQUAL, 0);
1404         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
1405
1406         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
1407         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
1408
1409         error = calendar_filter_add_int(filter, _calendar_todo_calendar_book.calendar_book_visibility, CALENDAR_MATCH_EQUAL, 1);
1410         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
1411
1412         error = calendar_query_set_filter(query, filter);
1413         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_set_filter() is failed(%x)", error);
1414
1415         error = calendar_db_get_records_with_query(query, 0, 0, &list);
1416         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_records_with_query() is failed(%x)", error);
1417
1418         error = calendar_list_get_count(list,&count);
1419         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_count() is failed(%x)", error);
1420
1421         error = calendar_list_first(list);
1422         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_first() is failed(%x)", error);
1423
1424         for (index = 0; index < count; index++) {
1425
1426                 calendar_record_h record = NULL;
1427
1428                 error = calendar_list_get_current_record_p(list, &record);
1429                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_current_record_p() is failed(%x)", error);
1430
1431                 _calendar_get_end_time(record, &caltime_end);
1432
1433                 cal_util_convert_lli_to_tm(NULL, caltime_end.time.utime, &tm_end);
1434
1435                 if (!is_display_completed_todo) {
1436                         int todo_status;
1437                         calendar_record_get_int(record, _calendar_todo_calendar_book.todo_status, &todo_status);
1438                         if (CALENDAR_TODO_STATUS_COMPLETED != todo_status)
1439                                 month[tm_end.tm_mday - 1] += 1;
1440                 } else {
1441                         month[tm_end.tm_mday - 1] += 1;
1442                 }
1443
1444                 calendar_list_next(list);
1445         }
1446
1447         error = calendar_list_destroy(list, true);
1448         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_destroy() is failed(%x)", error);
1449         list = NULL;
1450
1451         error = calendar_filter_destroy(filter);
1452         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
1453         filter = NULL;
1454
1455         error = calendar_query_destroy(query);
1456         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_destroy() is failed(%x)", error);
1457         query = NULL;
1458
1459         caltime_start.type = CALENDAR_TIME_LOCALTIME;
1460         caltime_start.time.date.year = start->tm_year + 1900;
1461         caltime_start.time.date.month = start->tm_mon + 1;
1462         caltime_start.time.date.mday = start->tm_mday;
1463
1464         caltime_end.type = CALENDAR_TIME_LOCALTIME;
1465         caltime_end.time.date.year = end->tm_year + 1900;
1466         caltime_end.time.date.month = end->tm_mon + 1;
1467         caltime_end.time.date.mday = end->tm_mday;
1468
1469         error = calendar_query_create(_calendar_instance_allday_calendar_book._uri, &query);
1470         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_create() is failed(%x)", error);
1471
1472         error = calendar_filter_create(_calendar_instance_allday_calendar_book._uri, &filter);
1473         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
1474
1475         error = calendar_filter_add_caltime(filter, _calendar_instance_allday_calendar_book.end_time, CALENDAR_MATCH_GREATER_THAN_OR_EQUAL, caltime_start);
1476         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
1477
1478         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
1479         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
1480
1481         error = calendar_filter_add_caltime(filter, _calendar_instance_allday_calendar_book.start_time,CALENDAR_MATCH_LESS_THAN_OR_EQUAL, caltime_end);
1482         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
1483
1484         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
1485         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
1486
1487         error = calendar_filter_add_int(filter, _calendar_instance_allday_calendar_book.calendar_book_visibility, CALENDAR_MATCH_EQUAL, 1);
1488         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
1489
1490         error = calendar_query_set_filter(query, filter);
1491         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_set_filter() is failed(%x)", error);
1492
1493         error = calendar_db_get_records_with_query(query, 0, 0, &list);
1494         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_records_with_query() is failed(%x)", error);
1495
1496         error = calendar_list_get_count(list,&count);
1497         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_count() is failed(%x)", error);
1498
1499         error = calendar_list_first(list);
1500         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_first() is failed(%x)", error);
1501
1502         i = 0;
1503         for (index = 0; index < count; index++) {
1504
1505                 calendar_record_h record = NULL;
1506
1507                 error = calendar_list_get_current_record_p(list, &record);
1508                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_current_record_p() is failed(%x)", error);
1509
1510                 _calendar_get_start_time(record, &caltime_start);
1511                 _calendar_get_end_time(record, &caltime_end);
1512
1513                 for (i = caltime_start.time.date.mday; i <= caltime_end.time.date.mday; i++)
1514                         month[i - 1] += 1;
1515
1516                 calendar_list_next(list);
1517         }
1518
1519         error = calendar_list_destroy(list, true);
1520         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_destroy() is failed(%x)", error);
1521         list = NULL;
1522
1523         error = calendar_filter_destroy(filter);
1524         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
1525         filter = NULL;
1526
1527         error = calendar_query_destroy(query);
1528         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_destroy() is failed(%x)", error);
1529         query = NULL;
1530
1531         CAL_FN_END;
1532
1533         return month;
1534 }
1535
1536 calendar_list_h _calendar_get_normal_instance_list_handler(struct tm *start, struct tm *end)
1537 {
1538         c_retv_if(!start, NULL);
1539         c_retv_if(!end, NULL);
1540
1541         calendar_query_h query = NULL;
1542
1543         calendar_error_e error = CALENDAR_ERROR_NONE;
1544
1545         error = calendar_query_create(_calendar_instance_normal_calendar_book._uri, &query);
1546         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_create() is failed(%x)", error);
1547
1548         calendar_time_s caltime_start = {0};
1549         calendar_time_s caltime_end = {0};
1550
1551         long long int lli_start = 0;
1552         long long int lli_end = 0;
1553
1554         cal_util_convert_tm_to_lli(NULL, start, &lli_start);
1555         cal_util_convert_tm_to_lli(NULL, end, &lli_end);
1556
1557         caltime_start.type = CALENDAR_TIME_UTIME;
1558         caltime_start.time.utime = lli_start;
1559
1560         caltime_end.type = CALENDAR_TIME_UTIME;
1561         caltime_end.time.utime = lli_end;
1562
1563         calendar_filter_h filter = NULL;
1564
1565         error = calendar_filter_create(_calendar_instance_normal_calendar_book._uri, &filter);
1566         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
1567
1568         error = calendar_filter_add_caltime(filter, _calendar_instance_normal_calendar_book.end_time, CALENDAR_MATCH_GREATER_THAN, caltime_start);
1569         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
1570
1571         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
1572         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
1573
1574         error = calendar_filter_add_caltime(filter, _calendar_instance_normal_calendar_book.start_time,CALENDAR_MATCH_LESS_THAN_OR_EQUAL, caltime_end);
1575         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
1576
1577         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
1578         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
1579
1580         error = calendar_filter_add_int(filter, _calendar_instance_normal_calendar_book.calendar_book_visibility, CALENDAR_MATCH_EQUAL, 1);
1581         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
1582
1583         error = calendar_query_set_filter(query, filter);
1584         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_set_filter() is failed(%x)", error);
1585
1586         calendar_list_h list = NULL;
1587
1588         error = calendar_db_get_records_with_query(query, 0, 0, &list);
1589         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_records_with_query() is failed(%x)", error);
1590
1591         error = calendar_filter_destroy(filter);
1592         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
1593
1594         error = calendar_query_destroy(query);
1595         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_destroy() is failed(%x)", error);
1596
1597         error = calendar_list_first(list);
1598         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_first() is failed(%x)", error);
1599
1600         return list;
1601 }
1602
1603 calendar_list_h _calendar_get_allday_instance_list_handler(struct tm *start, struct tm *end)
1604 {
1605         c_retv_if(!start, NULL);
1606         c_retv_if(!end, NULL);
1607
1608         calendar_query_h query = NULL;
1609
1610         calendar_error_e error = CALENDAR_ERROR_NONE;
1611
1612         error = calendar_query_create(_calendar_instance_allday_calendar_book._uri, &query);
1613         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_create() is failed(%x)", error);
1614
1615         calendar_time_s caltime_start = {0};
1616         calendar_time_s caltime_end = {0};
1617
1618         caltime_start.type = CALENDAR_TIME_LOCALTIME;
1619         caltime_start.time.date.year = start->tm_year + 1900;
1620         caltime_start.time.date.month = start->tm_mon + 1;
1621         caltime_start.time.date.mday = start->tm_mday;
1622
1623         caltime_end.type = CALENDAR_TIME_LOCALTIME;
1624         caltime_end.time.date.year = end->tm_year + 1900;
1625         caltime_end.time.date.month = end->tm_mon + 1;
1626         caltime_end.time.date.mday = end->tm_mday;
1627
1628         calendar_filter_h filter = NULL;
1629
1630         error = calendar_filter_create(_calendar_instance_allday_calendar_book._uri, &filter);
1631         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
1632
1633         error = calendar_filter_add_caltime(filter, _calendar_instance_allday_calendar_book.end_time, CALENDAR_MATCH_GREATER_THAN_OR_EQUAL, caltime_start);
1634         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
1635
1636         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
1637         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
1638
1639         error = calendar_filter_add_caltime(filter, _calendar_instance_allday_calendar_book.start_time,CALENDAR_MATCH_LESS_THAN_OR_EQUAL, caltime_end);
1640         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
1641
1642         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
1643         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
1644
1645         error = calendar_filter_add_int(filter, _calendar_instance_allday_calendar_book.calendar_book_visibility, CALENDAR_MATCH_EQUAL, 1);
1646         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
1647
1648         error = calendar_query_set_filter(query, filter);
1649         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_set_filter() is failed(%x)", error);
1650
1651         calendar_list_h list = NULL;
1652
1653         error = calendar_db_get_records_with_query(query, 0, 0, &list);
1654         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_records_with_query() is failed(%x)", error);
1655
1656         error = calendar_filter_destroy(filter);
1657         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
1658
1659         error = calendar_query_destroy(query);
1660         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_destroy() is failed(%x)", error);
1661
1662         error = calendar_list_first(list);
1663         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_first() is failed(%x)", error);
1664
1665         return list;
1666 }
1667
1668 calendar_list_h _calendar_get_task_list_handler(struct tm *start, struct tm *end, Eina_Bool is_show_completed_task, _calendar_task_sort_type sort_type)
1669 {
1670         c_retv_if(!start, NULL);
1671         c_retv_if(!end, NULL);
1672
1673         calendar_query_h query = NULL;
1674
1675         calendar_error_e error = CALENDAR_ERROR_NONE;
1676
1677         error = calendar_query_create(_calendar_todo_calendar_book._uri, &query);
1678         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_create() is failed(%x)", error);
1679
1680         calendar_time_s caltime_start = {0};
1681         calendar_time_s caltime_end = {0};
1682
1683         long long int lli_start = 0;
1684         long long int lli_end = 0;
1685
1686         cal_util_convert_tm_to_lli(NULL, start, &lli_start);
1687         cal_util_convert_tm_to_lli(NULL, end, &lli_end);
1688
1689         caltime_start.type = CALENDAR_TIME_UTIME;
1690         caltime_start.time.utime = lli_start;
1691
1692         caltime_end.type = CALENDAR_TIME_UTIME;
1693         caltime_end.time.utime = lli_end;
1694
1695         calendar_filter_h filter = NULL;
1696         calendar_filter_h status_filter = NULL;
1697
1698         error = calendar_filter_create(_calendar_todo_calendar_book._uri, &filter);
1699         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
1700
1701         error = calendar_filter_add_caltime(filter, _calendar_todo_calendar_book.due_time, CALENDAR_MATCH_GREATER_THAN, caltime_start);
1702         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
1703
1704         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
1705         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
1706
1707         error = calendar_filter_add_caltime(filter, _calendar_todo_calendar_book.due_time,CALENDAR_MATCH_LESS_THAN_OR_EQUAL, caltime_end);
1708         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
1709
1710         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
1711         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
1712
1713         error = calendar_filter_add_int(filter, _calendar_todo_calendar_book.is_deleted, CALENDAR_MATCH_EQUAL, 0);
1714         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
1715
1716         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
1717         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
1718
1719         error = calendar_filter_add_int(filter, _calendar_todo_calendar_book.calendar_book_visibility, CALENDAR_MATCH_EQUAL, 1);
1720         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
1721
1722         if (!is_show_completed_task) {
1723
1724                 error = calendar_filter_create(_calendar_todo_calendar_book._uri, &status_filter);
1725                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
1726
1727                 error = calendar_filter_add_int(status_filter, _calendar_todo_calendar_book.todo_status, CALENDAR_MATCH_EQUAL, CALENDAR_TODO_STATUS_NONE);
1728                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
1729
1730                 error = calendar_filter_add_operator(status_filter, CALENDAR_FILTER_OPERATOR_OR);
1731                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
1732
1733                 error = calendar_filter_add_int(status_filter, _calendar_todo_calendar_book.todo_status, CALENDAR_MATCH_EQUAL, CALENDAR_TODO_STATUS_NEEDS_ACTION);
1734                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
1735
1736                 error = calendar_filter_add_operator(status_filter, CALENDAR_FILTER_OPERATOR_OR);
1737                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
1738
1739                 error = calendar_filter_add_int(status_filter, _calendar_todo_calendar_book.todo_status, CALENDAR_MATCH_EQUAL, CALENDAR_TODO_STATUS_IN_PROCESS);
1740                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
1741
1742                 error = calendar_filter_add_operator(status_filter, CALENDAR_FILTER_OPERATOR_OR);
1743                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
1744
1745                 error = calendar_filter_add_int(status_filter, _calendar_todo_calendar_book.todo_status, CALENDAR_MATCH_EQUAL, CALENDAR_TODO_STATUS_CANCELED);
1746                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
1747
1748                 error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
1749                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
1750
1751                 error = calendar_filter_add_filter(filter, status_filter);
1752                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_filter() is failed(%x)", error);
1753         }
1754
1755         error = calendar_query_set_filter(query, filter);
1756         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_set_filter() is failed(%x)", error);
1757
1758         switch (sort_type) {
1759                 case _CALENDAR_TASK_SORT_TYPE_DUEDATE_ASC:
1760                         error = calendar_query_set_sort(query, _calendar_todo_calendar_book.due_time, true);
1761                         break;
1762                 case _CALENDAR_TASK_SORT_TYPE_DUEDATE_DES:
1763                         error = calendar_query_set_sort(query, _calendar_todo_calendar_book.due_time, false);
1764                         break;
1765                 case _CALENDAR_TASK_SORT_TYPE_PRIORITY_ASC:
1766                         error = calendar_query_set_sort(query, _calendar_todo_calendar_book.priority, true);
1767                         break;
1768                 case _CALENDAR_TASK_SORT_TYPE_PRIORITY_DES:
1769                         error = calendar_query_set_sort(query, _calendar_todo_calendar_book.priority, false);
1770                         break;
1771                 case _CALENDAR_TASK_SORT_TYPE_STATUS_ASC:
1772                         error = calendar_query_set_sort(query, _calendar_todo_calendar_book.todo_status, true);
1773                         break;
1774                 case _CALENDAR_TASK_SORT_TYPE_STATUS_DES:
1775                         error = calendar_query_set_sort(query, _calendar_todo_calendar_book.todo_status, false);
1776                         break;
1777                 default:
1778                         break;
1779         }
1780
1781         calendar_list_h list = NULL;
1782
1783         error = calendar_db_get_records_with_query(query, 0, 0, &list);
1784         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_records_with_query() is failed(%x)", error);
1785
1786         error = calendar_filter_destroy(filter);
1787         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
1788
1789         error = calendar_filter_destroy(status_filter);
1790         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
1791
1792         error = calendar_query_destroy(query);
1793         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_destroy() is failed(%x)", error);
1794
1795         error = calendar_list_first(list);
1796         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_first() is failed(%x)", error);
1797
1798         return list;
1799 }
1800
1801 calendar_list_h _calendar_search_event(const char *keyword)
1802 {
1803         c_retv_if(!CAL_STRLEN(keyword), NULL);
1804
1805         calendar_query_h query = NULL;
1806
1807         calendar_error_e error = CALENDAR_ERROR_NONE;
1808
1809         error = calendar_query_create(_calendar_event_calendar_book_attendee._uri, &query);
1810         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_create() is failed(%x)", error);
1811
1812         calendar_filter_h visibility_filter = NULL;
1813
1814         error = calendar_filter_create(_calendar_event_calendar_book_attendee._uri, &visibility_filter);
1815         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
1816
1817         error = calendar_filter_add_int(visibility_filter, _calendar_event_calendar_book_attendee.calendar_book_visibility, CALENDAR_MATCH_EQUAL, 1);
1818         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
1819
1820         calendar_filter_h filter = NULL;
1821
1822         error = calendar_filter_create(_calendar_event_calendar_book_attendee._uri, &filter);
1823         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
1824
1825         error = calendar_filter_add_str(filter, _calendar_event_calendar_book_attendee.summary, CALENDAR_MATCH_CONTAINS, keyword);
1826         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_str() is failed(%x)", error);
1827
1828         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_OR);
1829         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
1830
1831         error = calendar_filter_add_str(filter, _calendar_event_calendar_book_attendee.location, CALENDAR_MATCH_CONTAINS, keyword);
1832         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_str() is failed(%x)", error);
1833
1834         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_OR);
1835         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
1836
1837         error = calendar_filter_add_str(filter, _calendar_event_calendar_book_attendee.description, CALENDAR_MATCH_CONTAINS, keyword);
1838         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_str() is failed(%x)", error);
1839
1840         error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_OR);
1841         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
1842
1843         error = calendar_filter_add_str(filter, _calendar_event_calendar_book_attendee.attendee_name, CALENDAR_MATCH_CONTAINS, keyword);
1844         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_str() is failed(%x)", error);
1845
1846         error = calendar_filter_add_operator(visibility_filter, CALENDAR_FILTER_OPERATOR_AND);
1847         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
1848
1849         error = calendar_filter_add_filter(visibility_filter, filter);
1850         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_filter() is failed(%x)", error);
1851
1852         error = calendar_query_set_filter(query, visibility_filter);
1853         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_set_filter() is failed(%x)", error);
1854
1855         calendar_list_h list = NULL;
1856
1857         error = calendar_db_get_records_with_query(query, 0, 0, &list);
1858         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_records_with_query() is failed(%x)", error);
1859
1860         error = calendar_list_first(list);
1861         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_first() is failed(%x)", error);
1862
1863         error = calendar_filter_destroy(visibility_filter);
1864         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
1865
1866         error = calendar_filter_destroy(filter);
1867         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
1868
1869         error = calendar_query_destroy(query);
1870         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_destroy() is failed(%x)", error);
1871
1872         return list;
1873 }
1874
1875 calendar_list_h _calendar_search_task(const char* keyword)
1876 {
1877         calendar_query_h query = cal_query_create_task_search_query(keyword);
1878         c_retv_if(!query, NULL);
1879
1880         calendar_list_h list = NULL;
1881         calendar_error_e error = CALENDAR_ERROR_NONE;
1882
1883         error = calendar_db_get_records_with_query(query, 0, 0, &list);
1884         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_records_with_query() is failed(%x)", error);
1885
1886         error = calendar_list_first(list);
1887         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_first() is failed(%x)", error);
1888
1889         error = calendar_query_destroy(query);
1890         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_destroy() is failed(%x)", error);
1891
1892         return list;
1893 }
1894
1895 calendar_list_h _calendar_get_all_task_list(struct tm *start, struct tm *end, Eina_Bool is_show_completed_task, _calendar_task_sort_type sort_type)
1896 {
1897         calendar_query_h query =
1898                         cal_query_create_all_task_list_query(start, end, is_show_completed_task, sort_type);
1899         c_retv_if(!query, NULL);
1900
1901         calendar_list_h list = NULL;
1902         calendar_error_e error = CALENDAR_ERROR_NONE;
1903
1904         error = calendar_db_get_records_with_query(query, 0, 0, &list);
1905         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_records_with_query() is failed(%x)", error);
1906
1907         error = calendar_query_destroy(query);
1908         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_destroy() is failed(%x)", error);
1909
1910         error = calendar_list_first(list);
1911         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_first() is failed(%x)", error);
1912
1913         return list;
1914 }
1915
1916 Eina_List* _calendar_get_all_task_list2(struct tm *start, struct tm *end, Eina_Bool is_show_completed_task, _calendar_task_sort_type sort_type)
1917 {
1918         calendar_query_h query =
1919                         cal_query_create_all_task_list_query(start, end, is_show_completed_task, sort_type);
1920         c_retv_if(!query, NULL);
1921
1922         calendar_list_h list = NULL;
1923         calendar_error_e error = CALENDAR_ERROR_NONE;
1924
1925         error = calendar_db_get_records_with_query(query, 0, 0, &list);
1926         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_records_with_query() is failed(%x)", error);
1927
1928         int count = 0;
1929
1930         error = calendar_list_get_count(list, &count);
1931         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_count() is failed(%x)", error);
1932
1933         Eina_List *todo_list = NULL;
1934
1935         error = calendar_list_first(list);
1936         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_first() is failed(%x)", error);
1937
1938         int i = 0;
1939         for (i = 0; i < count; i++) {
1940
1941                 calendar_record_h record = NULL;
1942
1943                 error = calendar_list_get_current_record_p(list, &record);
1944                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_current_record_p() is failed(%x)", error);
1945
1946                 calendar_record_h new_record = NULL;
1947
1948                 error = calendar_record_clone(record, &new_record);
1949                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_clone() is failed(%x)", error);
1950
1951                 todo_list = eina_list_append(todo_list, new_record);
1952                 c_warn_if(!todo_list, "eina_list_append() is failed.");
1953
1954                 calendar_list_next(list);
1955         }
1956
1957         error = calendar_list_destroy(list, false);
1958         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_destroy() is failed(%x)", error);
1959
1960         error = calendar_query_destroy(query);
1961         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_destroy() is failed(%x)", error);
1962
1963         return todo_list;
1964 }
1965
1966 static int __calendar_compare_todo_record_list_item(const void *data1, const void *data2)
1967 {
1968         c_retv_if(!data1, 1);
1969         c_retv_if(!data2, -1);
1970
1971         calendar_record_h record1 = data1;
1972         calendar_record_h record2 = data2;
1973
1974         calendar_time_s record1_due_time = {0};
1975         calendar_time_s record2_due_time = {0};
1976
1977         calendar_error_e error = CALENDAR_ERROR_NONE;
1978         error = calendar_record_get_caltime(record1, _calendar_todo_calendar_book.due_time, &record1_due_time);
1979         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_caltime() is failed(%x)", error);
1980
1981         error = calendar_record_get_caltime(record2, _calendar_todo_calendar_book.due_time, &record2_due_time);
1982         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_caltime() is failed(%x)", error);
1983
1984         long long int lli_due1 = record1_due_time.time.utime;
1985         long long int lli_due2 = record2_due_time.time.utime;
1986
1987         return (lli_due1 > lli_due2) ? 1 : -1;
1988 }
1989
1990
1991 Eina_List* _calendar_get_due_date_task_list(struct tm *start, struct tm *end, Eina_Bool is_show_completed_task, _calendar_task_sort_type sort_type)
1992 {
1993         calendar_query_h query =
1994                         cal_query_create_due_date_task_list_query(start, end, is_show_completed_task, sort_type);
1995         c_retv_if(!query, NULL);
1996
1997         calendar_list_h list = NULL;
1998         calendar_error_e error = CALENDAR_ERROR_NONE;
1999
2000         error = calendar_db_get_records_with_query(query, 0, 0, &list);
2001         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_records_with_query() is failed(%x)", error);
2002
2003         int count = 0;
2004
2005         error = calendar_list_get_count(list, &count);
2006         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_count() is failed(%x)", error);
2007
2008         Eina_List *todo_list = NULL;
2009
2010         error = calendar_list_first(list);
2011         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_first() is failed(%x)", error);
2012
2013         int i = 0;
2014         for (i = 0; i < count; i++) {
2015
2016                 calendar_record_h record = NULL;
2017
2018                 error = calendar_list_get_current_record_p(list, &record);
2019                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_current_record_p() is failed(%x)", error);
2020
2021                 calendar_record_h new_record = NULL;
2022
2023                 error = calendar_record_clone(record, &new_record);
2024                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_clone() is failed(%x)", error);
2025
2026                 todo_list = eina_list_append(todo_list, new_record);
2027                 c_warn_if(!todo_list, "eina_list_append() is failed.");
2028
2029                 calendar_list_next(list);
2030         }
2031
2032         error = calendar_list_destroy(list, false);
2033         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_destroy() is failed(%x)", error);
2034
2035         error = calendar_query_destroy(query);
2036         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_destroy() is failed(%x)", error);
2037
2038         eina_list_sort(todo_list, 0, __calendar_compare_todo_record_list_item);
2039         return todo_list;
2040 }
2041
2042 Eina_List * _calendar_get_calendar_book_list_with_account_id(int account_id)
2043 {
2044         c_retv_if(account_id < -1, NULL);
2045
2046         Eina_List *calendar_book_list = NULL;
2047
2048         calendar_error_e error = CALENDAR_ERROR_NONE;
2049
2050         calendar_query_h query = NULL;
2051
2052         error = calendar_query_create( _calendar_book._uri, &query);
2053         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_create() is failed(%x)", error);
2054
2055         calendar_filter_h filter = NULL;
2056
2057         if (account_id != _CALENDAR_ALL_ACCOUNT_ID) {
2058
2059                 error = calendar_filter_create(_calendar_book._uri, &filter);
2060                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
2061
2062                 error = calendar_filter_add_int(filter, _calendar_book.is_deleted, CALENDAR_MATCH_EQUAL, 0);
2063                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
2064
2065                 error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
2066                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
2067
2068                 error = calendar_filter_add_int(filter, _calendar_book.account_id, CALENDAR_MATCH_EQUAL, account_id);
2069                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
2070
2071                 error = calendar_query_set_filter(query, filter);
2072                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_set_filter() is failed(%x)", error);
2073         }
2074
2075         calendar_list_h list = NULL;
2076
2077         error = calendar_db_get_records_with_query(query, 0, 0, &list);
2078         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_records_with_query() is failed(%x)", error);
2079
2080         int count = 0;
2081
2082         error = calendar_list_get_count(list, &count);
2083         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_count() is failed(%x)", error);
2084
2085         error = calendar_list_first(list);
2086         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_first() is failed(%x)", error);
2087
2088         int i = 0;
2089
2090         for (i = 0; i < count; i++) {
2091
2092                 calendar_record_h record = NULL;
2093
2094                 error = calendar_list_get_current_record_p(list, &record);
2095                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_current_record_p() is failed(%x)", error);
2096
2097                 calendar_book_list = eina_list_append(calendar_book_list, record);
2098                 c_warn_if(!calendar_book_list, "eina_list_append() is failed.");
2099
2100                 calendar_list_next(list);
2101         }
2102
2103         error = calendar_list_destroy(list, false);
2104         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_destroy() is failed(%x)", error);
2105
2106         if (account_id != _CALENDAR_ALL_ACCOUNT_ID) {
2107                 error = calendar_filter_destroy(filter);
2108                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
2109         }
2110
2111         error = calendar_query_destroy(query);
2112         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_destroy() is failed(%x)", error);
2113
2114         return calendar_book_list;
2115 }
2116
2117 static char * __calendar_get_time_str_from_lli(long long int lli)
2118 {
2119         UErrorCode status = U_ZERO_ERROR;
2120
2121         UChar *tz_id = (UChar*)calloc(strlen("Etc/Unknown") + 1, sizeof(UChar));
2122         c_retv_if(!tz_id, NULL);
2123
2124         u_uastrcpy(tz_id, "Etc/Unknown");
2125
2126         UCalendar *cal = ucal_open(tz_id, u_strlen(tz_id), "en_US", UCAL_TRADITIONAL, &status);
2127         c_retv_if(U_FAILURE(status), NULL);
2128
2129         free(tz_id);
2130
2131         int year = 0;
2132         int month = 0;
2133         int day = 0;
2134         int hour = 0;
2135         int minute = 0;
2136         int second = 0;
2137
2138         ucal_setMillis(cal, lli * 1000, &status);
2139
2140         year = ucal_get(cal, UCAL_YEAR, &status);
2141         month = ucal_get(cal, UCAL_MONTH, &status) + 1;
2142         day = ucal_get(cal, UCAL_DATE, &status);
2143         hour = ucal_get(cal, UCAL_HOUR_OF_DAY, &status);
2144         minute = ucal_get(cal, UCAL_MINUTE, &status);
2145         second = ucal_get(cal, UCAL_SECOND, &status);
2146
2147         return g_strdup_printf("%04d%02d%02dT%02d%02d%02dZ", year, month, day, hour, minute, second);
2148 }
2149
2150 void _calendar_delete_recurrence_instance(calendar_record_h instance)
2151 {
2152         c_ret_if(!instance);
2153
2154         _calendar_record_type record_type = _calendar_get_record_type(instance);
2155
2156         c_ret_if(record_type != _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_ALLDAY_CALENDAR
2157                 && record_type != _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_NORMAL_CALENDAR);
2158
2159         calendar_record_h original_record = NULL;
2160
2161         calendar_error_e error = CALENDAR_ERROR_NONE;
2162
2163         int event_id = 0;
2164
2165         if (record_type == _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_ALLDAY_CALENDAR) {
2166                 error = calendar_record_get_int(instance, _calendar_instance_allday_calendar_book.event_id, &event_id);
2167         } else {
2168                 error = calendar_record_get_int(instance, _calendar_instance_normal_calendar_book.event_id, &event_id);
2169         }
2170
2171         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
2172
2173         error = calendar_db_get_record(_calendar_event._uri, event_id, &original_record);
2174         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_record() is failed(%x)", error);
2175
2176         char *exdate = NULL;
2177
2178         error = calendar_record_get_str(original_record, _calendar_event.exdate, &exdate);
2179         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_str() is failed(%x)", error);
2180
2181         calendar_time_s start_time = {0};
2182
2183         _calendar_get_start_time(instance, &start_time);
2184
2185         char *new_exdate = NULL;
2186
2187         if (record_type == _CALENDAR_RECORD_TYPE_SEARCH_INSTANCE_ALLDAY_CALENDAR) {
2188
2189                 if (CAL_STRLEN(exdate))
2190                         new_exdate = g_strdup_printf("%s,%04d%02d%02d", exdate, start_time.time.date.year, start_time.time.date.month, start_time.time.date.mday);
2191                 else
2192                         new_exdate = g_strdup_printf("%04d%02d%02d", start_time.time.date.year, start_time.time.date.month, start_time.time.date.mday);
2193
2194         } else {
2195                 char *lli = __calendar_get_time_str_from_lli(start_time.time.utime);
2196                 c_ret_if(!lli);
2197
2198                 if (CAL_STRLEN(exdate))
2199                         new_exdate = g_strdup_printf("%s,%s", exdate, lli);
2200                 else
2201                         new_exdate = g_strdup_printf("%s", lli);
2202
2203                 free(lli);
2204         }
2205
2206         error = calendar_record_set_str(original_record, _calendar_event.exdate, new_exdate);
2207         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_str() is failed(%x)", error);
2208
2209         CAL_FREE(new_exdate);
2210         CAL_FREE(exdate);
2211
2212         error = calendar_db_update_record(original_record);
2213         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_update_record() is failed(%x)", error);
2214 }
2215
2216 void _calendar_edit_recurrence_instance(calendar_record_h exception_record, int original_event_id)
2217 {
2218         c_ret_if(!exception_record);
2219         c_ret_if(original_event_id <= 0);
2220
2221         calendar_record_h original_record = NULL;
2222
2223         calendar_error_e error = CALENDAR_ERROR_NONE;
2224
2225         error = calendar_record_set_int(exception_record, _calendar_event.original_event_id, original_event_id);
2226         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_int() is failed(%x)", error);
2227
2228         error = calendar_db_get_record(_calendar_event._uri, original_event_id, &original_record);
2229         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_record() is failed(%x)", error);
2230
2231         char *recurrence_id = NULL;
2232
2233         error = calendar_record_get_str(original_record, _calendar_event.recurrence_id, &recurrence_id);
2234         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_str() is failed(%x)", error);
2235
2236         calendar_time_s time = {0};
2237
2238         _calendar_get_start_time(exception_record, &time);
2239
2240         char *new_recurrence_id = NULL;
2241
2242         if (time.type == CALENDAR_TIME_LOCALTIME) {
2243
2244                 if (CAL_STRLEN(recurrence_id))
2245                         new_recurrence_id = g_strdup_printf("%s,%04d%02d%02d", recurrence_id, time.time.date.year, time.time.date.month, time.time.date.mday);
2246                 else
2247                         new_recurrence_id = g_strdup_printf("%04d%02d%02d", time.time.date.year, time.time.date.month, time.time.date.mday);
2248
2249         } else {
2250                 char *lli = __calendar_get_time_str_from_lli(time.time.utime);
2251                 c_ret_if(!lli);
2252
2253                 if (CAL_STRLEN(recurrence_id))
2254                         new_recurrence_id = g_strdup_printf("%s,%s", recurrence_id, lli);
2255                 else
2256                         new_recurrence_id = g_strdup_printf("%s", lli);
2257
2258                 free(lli);
2259         }
2260
2261         error = calendar_record_set_str(exception_record, _calendar_event.recurrence_id, new_recurrence_id);
2262         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_str() is failed(%x)", error);
2263
2264         CAL_FREE(new_recurrence_id);
2265         CAL_FREE(recurrence_id);
2266 }
2267
2268 void _calendar_clear_child_record(calendar_record_h record, unsigned int property_id)
2269 {
2270         c_ret_if(!record);
2271
2272         calendar_error_e error = CALENDAR_ERROR_NONE;
2273
2274         unsigned int child_record_count = 0;
2275
2276         error = calendar_record_get_child_record_count(record, property_id, &child_record_count);
2277         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_child_record_count() is failed(%x)", error);
2278
2279         while (child_record_count--) {
2280                 calendar_record_h child_record = NULL;
2281
2282                 error = calendar_record_get_child_record_at_p(record, property_id, 0, &child_record);
2283                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_child_record_at_p() is failed(%x)", error);
2284
2285                 error = calendar_record_remove_child_record(record, property_id, child_record);
2286                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_child_record_at_p() is failed(%x)", error);
2287         }
2288 }
2289
2290 void _calendar_clear_child_record_by_key(calendar_record_h record, unsigned int property_id, const char *key)
2291 {
2292         c_ret_if(!record);
2293
2294         calendar_error_e error = CALENDAR_ERROR_NONE;
2295
2296         unsigned int child_record_count = 0;
2297         char *item_key = NULL;
2298
2299         error = calendar_record_get_child_record_count(record, property_id, &child_record_count);
2300         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_child_record_count() is failed(%x)", error);
2301
2302         while (child_record_count--) {
2303                 calendar_record_h child_record = NULL;
2304
2305                 error = calendar_record_get_child_record_at_p(record, property_id, 0, &child_record);
2306                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_child_record_at_p() is failed(%x)", error);
2307
2308                 error = calendar_record_get_str_p(child_record, _calendar_extended_property.key, &item_key);
2309                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_str_p() is failed(%x)", error);
2310
2311                 if (!CAL_STRCMP(item_key, key)) {
2312                 error = calendar_record_remove_child_record(record, property_id, child_record);
2313                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_child_record_at_p() is failed(%x)", error);
2314                 }
2315         }
2316 }
2317
2318 calendar_record_h _calendar_get_instance_from_record(calendar_record_h record, struct tm *start, struct tm *end)
2319 {
2320         CAL_FN_START;
2321
2322         c_retv_if(!record, NULL);
2323         c_retv_if(!start, NULL);
2324         c_retv_if(!end, NULL);
2325
2326         _calendar_record_type record_type = _calendar_get_record_type(record);
2327         c_retv_if(record_type != _CALENDAR_RECORD_TYPE_EVENT, NULL);
2328
2329         calendar_error_e error = CALENDAR_ERROR_NONE;
2330
2331         Eina_Bool is_allday_record = _calendar_is_allday_record(record);
2332
2333         int record_index = 0;
2334
2335         error = calendar_record_get_int(record, _calendar_event.id, &record_index);
2336         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_int() is failed(%x)", error);
2337
2338         calendar_time_s caltime_start = {0};
2339         calendar_time_s caltime_end = {0};
2340
2341         calendar_query_h query = NULL;
2342         calendar_filter_h filter = NULL;
2343
2344         if (is_allday_record) {
2345                 error = calendar_query_create(_calendar_instance_allday_calendar_book._uri, &query);
2346                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_create() is failed(%x)", error);
2347
2348                 error = calendar_filter_create(_calendar_instance_allday_calendar_book._uri, &filter);
2349                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
2350
2351                 caltime_start.type = CALENDAR_TIME_LOCALTIME;
2352                 caltime_end.type = CALENDAR_TIME_LOCALTIME;
2353
2354                 caltime_start.time.date.year = start->tm_year + 1900;
2355                 caltime_start.time.date.month = start->tm_mon + 1;
2356                 caltime_start.time.date.mday = start->tm_mday;
2357
2358                 caltime_end.time.date.year = end->tm_year + 1900;
2359                 caltime_end.time.date.month = end->tm_mon + 1;
2360                 caltime_end.time.date.mday = end->tm_mday;
2361
2362                 error = calendar_filter_add_caltime(filter, _calendar_instance_allday_calendar_book.end_time, CALENDAR_MATCH_GREATER_THAN_OR_EQUAL, caltime_start);
2363                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
2364
2365                 error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
2366                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
2367
2368                 error = calendar_filter_add_caltime(filter, _calendar_instance_allday_calendar_book.start_time, CALENDAR_MATCH_LESS_THAN_OR_EQUAL, caltime_end);
2369                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
2370
2371                 error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
2372                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
2373
2374                 error = calendar_filter_add_int(filter, _calendar_instance_allday_calendar_book.event_id, CALENDAR_MATCH_EQUAL, record_index);
2375                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
2376
2377         } else {
2378
2379                 error = calendar_query_create(_calendar_instance_normal_calendar_book._uri, &query);
2380                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_create() is failed(%x)", error);
2381
2382                 error = calendar_filter_create(_calendar_instance_normal_calendar_book._uri, &filter);
2383                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_create() is failed(%x)", error);
2384
2385                 caltime_start.type = CALENDAR_TIME_UTIME;
2386                 caltime_end.type = CALENDAR_TIME_UTIME;
2387
2388                 cal_util_convert_tm_to_lli(NULL, start, &caltime_start.time.utime);
2389                 cal_util_convert_tm_to_lli(NULL, end, &caltime_end.time.utime);
2390
2391                 error = calendar_filter_add_caltime(filter, _calendar_instance_normal_calendar_book.end_time, CALENDAR_MATCH_GREATER_THAN, caltime_start);
2392                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
2393
2394                 error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
2395                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
2396
2397                 error = calendar_filter_add_caltime(filter, _calendar_instance_normal_calendar_book.start_time, CALENDAR_MATCH_LESS_THAN_OR_EQUAL, caltime_end);
2398                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_caltime() is failed(%x)", error);
2399
2400                 error = calendar_filter_add_operator(filter, CALENDAR_FILTER_OPERATOR_AND);
2401                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_operator() is failed(%x)", error);
2402
2403                 error = calendar_filter_add_int(filter, _calendar_instance_normal_calendar_book.event_id, CALENDAR_MATCH_EQUAL, record_index);
2404                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_add_int() is failed(%x)", error);
2405         }
2406
2407         error = calendar_query_set_filter(query, filter);
2408         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_set_filter() is failed(%x)", error);
2409
2410         calendar_list_h list = NULL;
2411
2412         error = calendar_db_get_records_with_query(query, 0, 0, &list);
2413         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_records_with_query() is failed(%x)", error);
2414
2415         int count = 0;
2416
2417         error = calendar_list_get_count(list, &count);
2418         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_count() is failed(%x)", error);
2419         c_warn_if(count != 1, "count is not 1(%d)", count);
2420
2421         error = calendar_list_first(list);
2422         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_first() is failed(%x)", error);
2423
2424         calendar_record_h instance = NULL;
2425
2426         error = calendar_list_get_current_record_p(list, &instance);
2427         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_get_current_record_p() is failed(%x)", error);
2428
2429         error = calendar_list_destroy(list, false);
2430         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_list_destroy() is failed(%x)", error);
2431
2432         error = calendar_filter_destroy(filter);
2433         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
2434
2435         error = calendar_query_destroy(query);
2436         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_query_destroy() is failed(%x)", error);
2437
2438         return instance;
2439 }
2440
2441 void _calendar_convert_calendar_time_to_tm(calendar_time_s *time, struct tm *tm)
2442 {
2443         c_ret_if(!time);
2444         c_ret_if(!tm);
2445
2446         if (time->type == CALENDAR_TIME_UTIME) {
2447                 cal_util_convert_lli_to_tm(NULL, time->time.utime, tm);
2448         } else {
2449                 tm->tm_year = time->time.date.year - 1900;
2450                 tm->tm_mon = time->time.date.month - 1;
2451                 tm->tm_mday = time->time.date.mday;
2452                 tm->tm_hour = 0;
2453                 tm->tm_min = 0;
2454                 tm->tm_sec = 0;
2455         }
2456 }
2457
2458 char * _calendar_get_time_str(calendar_time_s *time, const char *date_format, const char *time_format)
2459 {
2460         c_retv_if(!time, NULL);
2461
2462         struct tm tm = {0};
2463
2464         _calendar_convert_calendar_time_to_tm(time, &tm);
2465
2466         char buffer[128] = {0};
2467
2468         cal_util_get_time_text(buffer, sizeof(buffer), date_format, time_format, &tm);
2469
2470         return strdup(buffer);
2471 }
2472
2473 const char* __calendar_get_time_format()
2474 {
2475         if (is_hour24)
2476                 return CAL_UTIL_TIME_FORMAT_6;
2477         else
2478                 return CAL_UTIL_TIME_FORMAT_1;
2479 }
2480
2481 char * _calendar_get_time_str_for_genlist(calendar_record_h record)
2482 {
2483         c_retv_if(!record, NULL);
2484
2485         if (_calendar_is_allday_record(record))
2486                 return strdup(C_("IDS_COM_BODY_ALL_DAY"));
2487
2488         calendar_time_s stat_time = {0};
2489         calendar_time_s end_time = {0};
2490         char buff[256] = {0};
2491
2492         if (_calendar_is_task_record(record)) {
2493                 calendar_time_s due_time;
2494                 calendar_record_get_caltime(record, _calendar_todo_calendar_book.due_time, &due_time);
2495                 if (due_time.time.utime == CALENDAR_TODO_NO_DUE_DATE) {
2496                         return strdup(C_("IDS_CLD_BODY_NO_DUE_DATE_M_NOUN"));
2497                 }
2498
2499                 _calendar_get_end_time(record, &end_time);
2500
2501                 struct tm today_tm;
2502                 struct tm record_tm;
2503
2504                 time_t now = time(NULL);
2505                 localtime_r(&now, &today_tm);
2506
2507                 _calendar_convert_calendar_time_to_tm(&end_time, &record_tm);
2508
2509                 if ((today_tm.tm_year == record_tm.tm_year) && (today_tm.tm_mon == record_tm.tm_mon) && (today_tm.tm_mday == record_tm.tm_mday)) {
2510                         return _calendar_get_time_str(&end_time, NULL, __calendar_get_time_format());
2511                 } else {
2512                         char *time = _calendar_get_time_str(&end_time, NULL, __calendar_get_time_format());
2513                         char *date = _calendar_get_time_str(&end_time, NULL, CAL_UTIL_DATE_FORMAT_12);
2514                         sprintf(buff, "%s %s", time, date);
2515                         free(time);
2516                         free(date);
2517                         return strdup(buff);
2518                 }
2519         } else {
2520                 _calendar_get_start_time(record, &stat_time);
2521                 _calendar_get_end_time(record, &end_time);
2522                 char *start_str = _calendar_get_time_str(&stat_time, NULL, __calendar_get_time_format());
2523                 char *end_str = _calendar_get_time_str(&end_time, NULL, __calendar_get_time_format());
2524                 sprintf(buff, "%s-%s", start_str, end_str);
2525                 free(start_str);
2526                 free(end_str);
2527                 return strdup(buff);
2528         }
2529 }
2530
2531 static Eina_Bool __calendar_check_record_sync_data4(calendar_record_h record, const char *category)
2532 {
2533         c_retv_if(!record, EINA_FALSE);
2534         c_retv_if(!CAL_STRLEN(category), EINA_FALSE);
2535
2536         int calendar_book_id = _calendar_get_calendar_index(record);
2537
2538         calendar_error_e error = CALENDAR_ERROR_NONE;
2539
2540         calendar_record_h calendar_book = NULL;
2541
2542         error = calendar_db_get_record(_calendar_book._uri, calendar_book_id, &calendar_book);
2543         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_db_get_record(%d) is failed(%x)", calendar_book_id, error);
2544
2545         char *sync_data4 = NULL;
2546
2547         error = calendar_record_get_str_p(calendar_book, _calendar_book.sync_data4, &sync_data4);
2548         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_str_p() is failed(%x)", error);
2549
2550         Eina_Bool result = EINA_FALSE;
2551
2552         if (!CAL_STRCMP(sync_data4, category))
2553                 result = EINA_TRUE;
2554
2555         error = calendar_record_destroy(calendar_book, true);
2556         c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_destroy() is failed(%x)", error);
2557
2558         return result;
2559 }
2560
2561 Eina_Bool _calendar_is_eas_record(calendar_record_h record)
2562 {
2563         return __calendar_check_record_sync_data4(record, "exchange");
2564 }
2565
2566 Eina_Bool _calendar_is_facebook_record(calendar_record_h record)
2567 {
2568         return __calendar_check_record_sync_data4(record, "facebook");
2569 }
2570
2571 void _calendar_get_recurrence_frequency_str(int freq, char *buf, int size)
2572 {
2573         c_ret_if(!buf);
2574
2575         char *format = NULL;
2576
2577         switch (freq) {
2578
2579                 case CALENDAR_RECURRENCE_DAILY:
2580
2581                         snprintf(buf, size, "%s", C_("IDS_KC_BODY_EVERY_DAY"));
2582                         break;
2583
2584                 case CAL_REPEAT_EVERY_3_DAY:
2585
2586                         format = C_("IDS_CLD_BODY_EVERY_PD_DAYS");
2587                         snprintf(buf, size, format, 3);
2588                         break;
2589
2590                 case CALENDAR_RECURRENCE_WEEKLY:
2591
2592                         snprintf(buf, size, "%s", C_("IDS_CLD_BODY_EVERY_WEEK"));
2593                         break;
2594
2595                 case CAL_REPEAT_EVERY_2_WEEK:
2596
2597                         format = C_("IDS_CLD_BODY_EVERY_PD_WEEKS");
2598                         snprintf(buf, size, format, 2);
2599                         break;
2600
2601                 case CALENDAR_RECURRENCE_MONTHLY:
2602
2603                         snprintf(buf, size, "%s", C_("IDS_CLD_BODY_EVERY_MONTH"));
2604                         break;
2605
2606                 case CALENDAR_RECURRENCE_YEARLY:
2607
2608                         snprintf(buf, size, "%s", C_("IDS_CLD_BODY_EVERY_YEAR"));
2609                         break;
2610
2611                 default:
2612
2613                         snprintf(buf, size, "%s", S_("IDS_COM_BODY_OFF"));
2614                         break;
2615
2616         }
2617
2618         buf[size-1] = '\0';
2619 }
2620
2621 void _calendar_set_coordinates(calendar_record_h record, double latitude, double longitude)
2622 {
2623         calendar_error_e error = CALENDAR_ERROR_NONE;
2624
2625         if (_calendar_is_task_record(record)) {
2626
2627                 error = calendar_record_set_double(record, _calendar_todo.latitude, latitude);
2628                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_double() is failed(%x)", error);
2629
2630                 error = calendar_record_set_double(record, _calendar_todo.longitude, longitude);
2631                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_double() is failed(%x)", error);
2632
2633         } else {
2634                 error = calendar_record_set_double(record, _calendar_event.latitude, latitude);
2635                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_double() is failed(%x)", error);
2636
2637                 error = calendar_record_set_double(record, _calendar_event.longitude, longitude);
2638                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_set_double() is failed(%x)", error);
2639
2640         }
2641 }
2642
2643 void _calendar_get_coordinates(calendar_record_h record, double *latitude, double *longitude)
2644 {
2645         calendar_error_e error = CALENDAR_ERROR_NONE;
2646
2647         if (_calendar_is_task_record(record)) {
2648
2649                 error = calendar_record_get_double(record, _calendar_todo.latitude, latitude);
2650                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_double() is failed(%x)", error);
2651
2652                 error = calendar_record_get_double(record, _calendar_todo.longitude, longitude);
2653                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_double() is failed(%x)", error);
2654
2655         } else {
2656                 error = calendar_record_get_double(record, _calendar_event.latitude, latitude);
2657                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_double() is failed(%x)", error);
2658
2659                 error = calendar_record_get_double(record, _calendar_event.longitude, longitude);
2660                 c_warn_if(error != CALENDAR_ERROR_NONE, "calendar_record_get_double() is failed(%x)", error);
2661         }
2662 }