3 * Copyright 2012 Samsung Electronics Co., Ltd
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
9 * http://www.tizenopensource.org/license
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.
25 static Calendar_color def_color[] = {
34 int calendar_svc_get_month_event_check(int account_id, time_t startdate, time_t enddate, int *day_flag);
35 API int calendar_svc_get_month_event_list_by_period (int account_id, time_t startdate, time_t enddate, cal_iter **iter);
37 static void __cal_calendar_svc_free_cslist(Eina_List **h)
45 EINA_LIST_FOREACH(*h, l, cs) {
46 CALENDAR_SVC_STRUCT_FREE(&cs);
48 *h = eina_list_free(*h);
51 static void __cal_calendar_svc_free_tmlist(Eina_List **h)
59 EINA_LIST_FOREACH(*h, l, tm)
63 *h = eina_list_free(*h);
66 static inline struct tmnode* __cal_calendar_svc_get_tmnode(time_t s, time_t e, int rp, cal_struct *cs)
70 tm = malloc(sizeof(struct tmnode));
82 static int __cal_calendar_svc_get_event_list(int acct_id, time_t st, time_t et, Eina_List **csl, Eina_List **tml)
88 time_t lst, let; // not real time_t
98 r = CALENDAR_SVC_GET_EVENT_LIST_BY_PERIOD(acct_id, st, et, &it);
103 while (CALENDAR_SVC_ITER_NEXT(it) == CAL_SUCCESS) {
105 r = CALENDAR_SVC_ITER_GET_INFO(it, &cs);
106 if (r != CAL_SUCCESS)
109 rp = CALENDAR_SVC_STRUCT_GET_INT(cs, CAL_VALUE_INT_REPEAT_TERM);
111 localtime_r(&st,&stm);
112 localtime_r(&et,&etm);
113 memset(&lstm,0x00,sizeof(struct tm));
114 memset(&letm,0x00,sizeof(struct tm));
116 while (CALENDAR_SVC_UTIL_NEXT_VAILD_EVENT_TM(cs,
117 &stm, &etm, &lstm, &letm) == CAL_SUCCESS) {
126 if( (s <= et) && (tmp >= st)){
129 *csl = eina_list_append(*csl, cs);
132 tm = __cal_calendar_svc_get_tmnode(s, e, rp, cur);
134 *tml = eina_list_append(*tml, tm);
138 CALENDAR_SVC_ITER_REMOVE(&it);
143 struct calsvc* _calendar_svc_get_event(int acct_id, time_t st, time_t et)
150 CAL_CALLOC(c, 1, struct calsvc);
155 r = __cal_calendar_svc_get_event_list(acct_id, st, et, &c->cslist, &c->tmlist);
158 CALENDAR_SVC_FREE_CS(&c);
167 int* _calendar_svc_get_simple_cs(int acct_id, time_t st, time_t et)
174 CAL_CALLOC(month, 31, int);
176 r = calendar_svc_get_month_event_check ( acct_id, st, et, month);
183 void _calendar_svc_free_cs(struct calsvc **csv)
192 __cal_calendar_svc_free_cslist(&c->cslist);
193 __cal_calendar_svc_free_tmlist(&c->tmlist);
199 void _calendar_svc_free_cslist(Eina_List **h)
201 return __cal_calendar_svc_free_cslist(h);
204 int _calendar_svc_get_account_visibility(int account_id)
206 cal_struct* cs = NULL;
207 Eina_List* list = NULL;
211 list = CALENDAR_SVC_GET_CALENDARS(account_id);
212 EINA_LIST_FOREACH(list,l,cs)
214 r = CALENDAR_SVC_STRUCT_GET_INT(cs, CAL_TABLE_INT_VISIBILITY);
225 void _calendar_svc_set_account_visibility(int account_id, int value)
227 cal_struct* cs = NULL;
228 Eina_List* list = NULL;
232 list = CALENDAR_SVC_GET_CALENDARS(account_id);
233 EINA_LIST_FOREACH(list,l,cs)
235 r = CALENDAR_SVC_STRUCT_SET_INT(cs, CAL_TABLE_INT_VISIBILITY, value);
241 r = CALENDAR_SVC_UPDATE(cs);
249 void _calendar_svc_set_calendar_visibility(cal_struct* cs, int value)
255 r = CALENDAR_SVC_STRUCT_SET_INT(cs, CAL_TABLE_INT_VISIBILITY, value);
258 DBG("\n calendar_svc_struct_set_int fail : %d \n", r);
261 account_id = CALENDAR_SVC_STRUCT_GET_INT(cs,CAL_TABLE_INT_ACCOUNT_ID);
262 cal_id = CALENDAR_SVC_STRUCT_GET_INT(cs,CAL_TABLE_INT_INDEX);
263 r = CALENDAR_SVC_UPDATE(cs);
266 DBG("\n calendar_svc_update fail : %d \n", r);
271 int _calendar_svc_get_calendar_visibility(cal_struct* cs)
277 r = CALENDAR_SVC_STRUCT_GET_INT(cs, CAL_TABLE_INT_VISIBILITY);
278 account_id = CALENDAR_SVC_STRUCT_GET_INT(cs,CAL_TABLE_INT_ACCOUNT_ID);
279 cal_id = CALENDAR_SVC_STRUCT_GET_INT(cs,CAL_TABLE_INT_INDEX);
283 //cal_calendar_svc_find_event is used only in alarm call back
284 Eina_List* _calendar_svc_find_event(int acct_id, const char *field, const void *val)
290 time_t t = time(NULL);
295 tmp = localtime_r((const time_t*)&t, &tm);
298 ERR("localtime_r return null");
303 r = CALENDAR_SVC_FIND_EVENT_LIST(acct_id, field, val, &it);
304 if (r != CAL_SUCCESS)
308 while (CALENDAR_SVC_ITER_NEXT(it) == CAL_SUCCESS) {
310 r = CALENDAR_SVC_ITER_GET_INFO(it, &cs);
311 if (r != CAL_SUCCESS)
314 csl = eina_list_append(csl, cs);
317 CALENDAR_SVC_ITER_REMOVE(&it);
322 Eina_List* _calendar_svc_get_calendars(int acct_id)
330 r = CALENDAR_SVC_GET_ALL(acct_id, 0, CAL_STRUCT_CALENDAR, &it);
331 if (r != CAL_SUCCESS)
333 DBG("calendar_svc_get_all fail : %d \n",r);
338 while (CALENDAR_SVC_ITER_NEXT(it) == CAL_SUCCESS) {
340 r = CALENDAR_SVC_ITER_GET_INFO(it, &cs);
341 if (r != CAL_SUCCESS)
343 DBG("calendar_svc_iter_get_info fail : %d \n",r);
347 csl = eina_list_append(csl, cs);
350 CALENDAR_SVC_ITER_REMOVE(&it);
355 Eina_List* _calendar_svc_get_calendars_with_account(int account_id, void* at)
360 cal_struct *cal=NULL;
361 struct _cs* cs = NULL;
364 r = CALENDAR_SVC_GET_ALL(account_id, 0, CAL_STRUCT_CALENDAR, &it);
365 if (r != CAL_SUCCESS)
369 while (CALENDAR_SVC_ITER_NEXT(it) == CAL_SUCCESS) {
372 r = CALENDAR_SVC_ITER_GET_INFO(it, &cal);
373 if (r != CAL_SUCCESS)
375 CAL_CALLOC(cs, 1, struct _cs);
378 csl = eina_list_append(csl, cs);
381 CALENDAR_SVC_ITER_REMOVE(&it);
385 void _calendar_svc_set_calendar_color(cal_struct* cal, Calendar_color* color )
389 snprintf(buf, sizeof(buf), "%d.%d.%d.%d", color->red, color->green, color->blue, color->alpha);
391 r = CALENDAR_SVC_STRUCT_SET_STR(cal, CAL_TABLE_TXT_COLOR, buf);
392 if (r != CAL_SUCCESS)
394 ERR("CALENDAR_SVC_STRUCT_SET_STR return %d", r);
398 r = CALENDAR_SVC_UPDATE(cal);
399 if (r != CAL_SUCCESS)
401 ERR("calendar_svc_update return %d", r);
406 void _calendar_svc_get_calendar_color(cal_struct* cal, Calendar_color* calendar_color )
408 if(!cal || !calendar_color)
410 ERR("parameter is NULL");
414 char* calendar = NULL;
420 calendar = CALENDAR_SVC_STRUCT_GET_STR(cal, CAL_TABLE_TXT_COLOR);
422 color = strdup(calendar);
425 ERR("CALENDAR_SVC_STRUCT_GET_STR return NULL");
428 if(CAL_STRLEN(color) > 8)
430 token = strtok_r(color, ".", &temp);
432 calendar_color->red = atoi(token);
435 calendar_color->red = 0;
436 ERR("color : %s, token is NULL", calendar);
439 token = strtok_r(NULL, ".", &temp);
441 calendar_color->green = atoi(token);
444 calendar_color->green = 0;
445 ERR("color : %s, token is NULL", calendar);
448 token = strtok_r(NULL, ".", &temp);
450 calendar_color->blue = atoi(token);
453 calendar_color->blue = 0;
454 ERR("color : %s, token is NULL", calendar);
457 token = strtok_r(NULL, ".", &temp);
459 calendar_color->alpha = atoi(token);
462 calendar_color->alpha = 255;
463 ERR("color : %s, token is NULL", calendar);
468 index = CALENDAR_SVC_STRUCT_GET_INT(cal, CAL_TABLE_INT_INDEX)%6;
470 *calendar_color = def_color[index];
472 CALENDAR_SVC_SET_CALENDAR_COLOR(cal, def_color+index);
478 void _calendar_svc_add_event_changed_callback(void(*callback)(void *), void *user_data)
480 c_retm_if(!callback, "callback is null");
481 c_retm_if(!user_data, "user_data is null");
483 int ret = calendar_svc_subscribe_change(callback, user_data);
484 c_retm_if(ret!= CAL_SUCCESS, "calendar_svc_subscribe_change() is failed");
487 void _calendar_svc_del_event_changed_callback(void(*callback)(void *))
489 c_retm_if(!callback, "callback is null");
491 int ret = calendar_svc_unsubscribe_change(callback);
492 c_retm_if(ret!= CAL_SUCCESS, "calendar_svc_unsubscribe_change() is failed");
495 void _calendar_svc_add_calendar_changed_callback(const char *type, void(*callback)(void *), void *user_data)
497 c_retm_if(!callback, "callback is null");
498 c_retm_if(!user_data, "user_data is null");
500 int ret = calendar_svc_subscribe_db_change(type, callback, user_data);
501 c_retm_if(ret!= CAL_SUCCESS, "calendar_svc_subscribe_db_change() is failed");
504 void _calendar_svc_del_calendar_changed_callback(const char *type, void(*callback)(void *))
506 c_retm_if(!callback, "callback is null");
508 int ret = calendar_svc_unsubscribe_db_change(type, callback);
509 c_retm_if(ret!= CAL_SUCCESS, "calendar_svc_unsubscribe_db_change() is failed");