apply FSL license
[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://www.tizenopensource.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
19
20
21
22
23 #include "cld.h"
24
25 static Calendar_color def_color[] = {
26         {90, 167, 48, 255,},
27         {221, 129, 0, 255,},
28         {0, 182, 252, 255,},
29         {122, 52, 122, 255,},
30         {22, 139, 121, 255,},
31         {40, 81, 163, 255,},
32 };
33
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);
36
37 static void __cal_calendar_svc_free_cslist(Eina_List **h)
38 {
39         cal_struct *cs;
40         Eina_List *l;
41
42         if (!h || !*h)
43                 return;
44
45         EINA_LIST_FOREACH(*h, l, cs) {
46                 CALENDAR_SVC_STRUCT_FREE(&cs);
47         }
48         *h = eina_list_free(*h);
49 }
50
51 static void __cal_calendar_svc_free_tmlist(Eina_List **h)
52 {
53         struct tmnode *tm;
54         Eina_List *l;
55
56         if (!h || !*h)
57                 return;
58
59         EINA_LIST_FOREACH(*h, l, tm)
60         {
61                 free(tm);
62         }
63         *h = eina_list_free(*h);
64 }
65
66 static inline struct tmnode* __cal_calendar_svc_get_tmnode(time_t s, time_t e, int rp, cal_struct *cs)
67 {
68         struct tmnode *tm;
69
70         tm = malloc(sizeof(struct tmnode));
71         if (!tm)
72                 return NULL;
73
74         tm->st = s;
75         tm->et = e;
76         tm->repeat = rp;
77         tm->cs = cs;
78
79         return tm;
80 }
81
82 static int __cal_calendar_svc_get_event_list(int acct_id, time_t st, time_t et, Eina_List **csl, Eina_List **tml)
83 {
84         int r;
85         cal_iter *it;
86         cal_struct *cs;
87         cal_struct *cur;
88         time_t lst, let; // not real time_t
89         struct tmnode *tm;
90         time_t s, e, tmp;
91         int rp;
92         struct tm lstm={0,};
93         struct tm letm={0,};
94         struct tm stm = {0,};
95         struct tm etm = {0,};
96
97         it = NULL;
98         r = CALENDAR_SVC_GET_EVENT_LIST_BY_PERIOD(acct_id, st, et, &it);
99         if (r != CAL_SUCCESS)
100                 return -1;
101
102         cur = NULL;
103         while (CALENDAR_SVC_ITER_NEXT(it) == CAL_SUCCESS) {
104                 cs = NULL;
105                 r = CALENDAR_SVC_ITER_GET_INFO(it, &cs);
106                 if (r != CAL_SUCCESS)
107                         break;
108
109                 rp = CALENDAR_SVC_STRUCT_GET_INT(cs, CAL_VALUE_INT_REPEAT_TERM);
110                 lst = let = 0;
111                 localtime_r(&st,&stm);
112                 localtime_r(&et,&etm);
113                 memset(&lstm,0x00,sizeof(struct tm));
114                 memset(&letm,0x00,sizeof(struct tm));
115
116                 while (CALENDAR_SVC_UTIL_NEXT_VAILD_EVENT_TM(cs,
117                                         &stm, &etm, &lstm, &letm) == CAL_SUCCESS) {
118                         s = mktime(&lstm);
119                         e = mktime(&letm);
120
121                         if (s == e)
122                                 tmp = e + 1;
123                         else
124                                 tmp = e;
125
126                         if( (s <= et) && (tmp >= st)){
127                                 if (cs != cur) {
128                                         cur = cs;
129                                         *csl = eina_list_append(*csl, cs);
130                                 }
131
132                                 tm = __cal_calendar_svc_get_tmnode(s, e, rp, cur);
133                                 if (tm)
134                                         *tml = eina_list_append(*tml, tm);
135                         }
136                 }
137         }
138         CALENDAR_SVC_ITER_REMOVE(&it);
139
140         return 0;
141 }
142
143 struct calsvc* _calendar_svc_get_event(int acct_id, time_t st, time_t et)
144 {
145         CAL_FN_START;
146
147         int r;
148         struct calsvc *c;
149
150         CAL_CALLOC(c, 1, struct calsvc);
151
152         if (st < 0)
153                 st = 0;
154
155         r = __cal_calendar_svc_get_event_list(acct_id, st, et, &c->cslist, &c->tmlist);
156         if (r)
157         {
158                 CALENDAR_SVC_FREE_CS(&c);
159                 return NULL;
160         }
161
162         CAL_FN_END;
163
164         return c;
165 }
166
167 int* _calendar_svc_get_simple_cs(int acct_id, time_t st, time_t et)
168 {
169         CAL_FN_START;
170
171         int r;
172         int *month;
173
174         CAL_CALLOC(month, 31, int);
175
176         r = calendar_svc_get_month_event_check ( acct_id, st, et, month);
177
178         CAL_FN_END;
179
180         return month;
181 }
182
183 void _calendar_svc_free_cs(struct calsvc **csv)
184 {
185         struct calsvc *c;
186
187         if (!csv || !*csv)
188                 return;
189
190         c = *csv;
191
192         __cal_calendar_svc_free_cslist(&c->cslist);
193         __cal_calendar_svc_free_tmlist(&c->tmlist);
194         free(c);
195
196         *csv = NULL;
197 }
198
199 void _calendar_svc_free_cslist(Eina_List **h)
200 {
201         return __cal_calendar_svc_free_cslist(h);
202 }
203
204 int _calendar_svc_get_account_visibility(int account_id)
205 {
206         cal_struct* cs = NULL;
207         Eina_List* list = NULL;
208         Eina_List* l = NULL;
209         int r = 0;
210
211         list = CALENDAR_SVC_GET_CALENDARS(account_id);
212         EINA_LIST_FOREACH(list,l,cs)
213         {
214                 r = CALENDAR_SVC_STRUCT_GET_INT(cs, CAL_TABLE_INT_VISIBILITY);
215                 if(r == 1)
216                         break;
217         }
218         if(r != 0 && r!=1)
219         {
220                 return -1;
221         }
222         return r;
223 }
224
225 void _calendar_svc_set_account_visibility(int account_id, int value)
226 {
227         cal_struct* cs = NULL;
228         Eina_List* list = NULL;
229         Eina_List* l = NULL;
230         int r = 0;
231
232         list = CALENDAR_SVC_GET_CALENDARS(account_id);
233         EINA_LIST_FOREACH(list,l,cs)
234         {
235                 r = CALENDAR_SVC_STRUCT_SET_INT(cs, CAL_TABLE_INT_VISIBILITY, value);
236                 if(r != 0)
237                 {
238                         return;
239                 }
240
241                 r = CALENDAR_SVC_UPDATE(cs);
242                 if(r != 0)
243                 {
244                         return;
245                 }
246         }
247 }
248
249 void _calendar_svc_set_calendar_visibility(cal_struct* cs, int value)
250 {
251         int r = 0;
252         int account_id = 0;
253         int cal_id = 0;
254
255         r = CALENDAR_SVC_STRUCT_SET_INT(cs, CAL_TABLE_INT_VISIBILITY, value);
256         if(r)
257         {
258                 DBG("\n calendar_svc_struct_set_int fail : %d \n", r);
259                 return;
260         }
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);
264         if(r != 0)
265         {
266                 DBG("\n calendar_svc_update fail : %d \n", r);
267                 return;
268         }
269 }
270
271 int _calendar_svc_get_calendar_visibility(cal_struct* cs)
272 {
273         int r = 0;
274         int account_id = 0;
275         int cal_id = 0;
276
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);
280         return r;
281 }
282
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)
285 {
286         Eina_List *csl;
287         int r;
288         cal_struct *cs;
289         cal_iter *it;
290         time_t t = time(NULL);
291
292         struct tm tm;
293         struct tm* tmp;
294
295         tmp = localtime_r((const time_t*)&t, &tm);
296         if(!tmp)
297         {
298                 ERR("localtime_r return null");
299                 return NULL;
300         }
301
302         it = NULL;
303         r = CALENDAR_SVC_FIND_EVENT_LIST(acct_id, field, val, &it);
304         if (r != CAL_SUCCESS)
305                 return NULL;
306
307         csl = NULL;
308         while (CALENDAR_SVC_ITER_NEXT(it) == CAL_SUCCESS) {
309                 cs = NULL;
310                 r = CALENDAR_SVC_ITER_GET_INFO(it, &cs);
311                 if (r != CAL_SUCCESS)
312                         break;
313
314                 csl = eina_list_append(csl, cs);
315         }
316
317         CALENDAR_SVC_ITER_REMOVE(&it);
318
319         return csl;
320 }
321
322 Eina_List* _calendar_svc_get_calendars(int acct_id)
323 {
324         Eina_List *csl;
325         int r;
326         cal_iter *it=NULL;
327         cal_struct *cs=NULL;
328
329         it = NULL;
330         r = CALENDAR_SVC_GET_ALL(acct_id, 0, CAL_STRUCT_CALENDAR, &it);
331         if (r != CAL_SUCCESS)
332         {
333                 DBG("calendar_svc_get_all fail : %d \n",r);
334                 return NULL;
335         }
336
337         csl = NULL;
338         while (CALENDAR_SVC_ITER_NEXT(it) == CAL_SUCCESS) {
339                 cs = NULL;
340                 r = CALENDAR_SVC_ITER_GET_INFO(it, &cs);
341                 if (r != CAL_SUCCESS)
342                 {
343                         DBG("calendar_svc_iter_get_info fail : %d \n",r);
344                         break;
345                 }
346
347                 csl = eina_list_append(csl, cs);
348         }
349
350         CALENDAR_SVC_ITER_REMOVE(&it);
351
352         return csl;
353 }
354
355 Eina_List* _calendar_svc_get_calendars_with_account(int account_id, void* at)
356 {
357         Eina_List *csl;
358         int r;
359         cal_iter *it=NULL;
360         cal_struct *cal=NULL;
361         struct _cs* cs = NULL;
362
363         it = NULL;
364         r = CALENDAR_SVC_GET_ALL(account_id, 0, CAL_STRUCT_CALENDAR, &it);
365         if (r != CAL_SUCCESS)
366                 return NULL;
367
368         csl = NULL;
369         while (CALENDAR_SVC_ITER_NEXT(it) == CAL_SUCCESS) {
370                 cal = NULL;
371                 cs = NULL;
372                 r = CALENDAR_SVC_ITER_GET_INFO(it, &cal);
373                 if (r != CAL_SUCCESS)
374                         break;
375                 CAL_CALLOC(cs, 1, struct _cs);
376                 cs->cs = cal;
377                 cs->at = at;
378                 csl = eina_list_append(csl, cs);
379         }
380
381         CALENDAR_SVC_ITER_REMOVE(&it);
382         return csl;
383 }
384
385 void _calendar_svc_set_calendar_color(cal_struct* cal, Calendar_color* color )
386 {
387         char buf[16] = {0,};
388         int r;
389         snprintf(buf, sizeof(buf), "%d.%d.%d.%d", color->red, color->green, color->blue, color->alpha);
390
391         r = CALENDAR_SVC_STRUCT_SET_STR(cal, CAL_TABLE_TXT_COLOR, buf);
392         if (r != CAL_SUCCESS)
393         {
394                 ERR("CALENDAR_SVC_STRUCT_SET_STR return %d", r);
395                 return;
396         }
397
398         r = CALENDAR_SVC_UPDATE(cal);
399         if (r != CAL_SUCCESS)
400         {
401                 ERR("calendar_svc_update return %d", r);
402                 return;
403         }
404 }
405
406 void _calendar_svc_get_calendar_color(cal_struct* cal, Calendar_color* calendar_color )
407 {
408         if(!cal || !calendar_color)
409         {
410                 ERR("parameter is NULL");
411                 return;
412         }
413
414         char* calendar = NULL;
415         char* color = NULL;
416         char* token = NULL;
417         char* temp = NULL;
418         int index = 0;
419
420         calendar = CALENDAR_SVC_STRUCT_GET_STR(cal, CAL_TABLE_TXT_COLOR);
421         if(calendar)
422                 color = strdup(calendar);
423         else
424         {
425                 ERR("CALENDAR_SVC_STRUCT_GET_STR return NULL");
426         }
427
428         if(CAL_STRLEN(color) > 8)
429         {
430                 token = strtok_r(color, ".", &temp);
431                 if(token)
432                         calendar_color->red = atoi(token);
433                 else
434                 {
435                         calendar_color->red = 0;
436                         ERR("color : %s, token is NULL", calendar);
437                 }
438
439                 token = strtok_r(NULL, ".", &temp);
440                 if(token)
441                         calendar_color->green = atoi(token);
442                 else
443                 {
444                         calendar_color->green = 0;
445                         ERR("color : %s, token is NULL", calendar);
446                 }
447
448                 token = strtok_r(NULL, ".", &temp);
449                 if(token)
450                         calendar_color->blue = atoi(token);
451                 else
452                 {
453                         calendar_color->blue = 0;
454                         ERR("color : %s, token is NULL", calendar);
455                 }
456
457                 token = strtok_r(NULL, ".", &temp);
458                 if(token)
459                         calendar_color->alpha = atoi(token);
460                 else
461                 {
462                         calendar_color->alpha = 255;
463                         ERR("color : %s, token is NULL", calendar);
464                 }
465         }
466         else
467         {
468                 index = CALENDAR_SVC_STRUCT_GET_INT(cal, CAL_TABLE_INT_INDEX)%6;
469
470                 *calendar_color = def_color[index];
471
472                 CALENDAR_SVC_SET_CALENDAR_COLOR(cal, def_color+index);
473         }
474
475         free(color);
476 }
477
478 void _calendar_svc_add_event_changed_callback(void(*callback)(void *), void *user_data)
479 {
480         c_retm_if(!callback, "callback is null");
481         c_retm_if(!user_data, "user_data is null");
482
483         int ret = calendar_svc_subscribe_change(callback, user_data);
484         c_retm_if(ret!= CAL_SUCCESS, "calendar_svc_subscribe_change() is failed");
485 }
486
487 void _calendar_svc_del_event_changed_callback(void(*callback)(void *))
488 {
489         c_retm_if(!callback, "callback is null");
490
491         int ret = calendar_svc_unsubscribe_change(callback);
492         c_retm_if(ret!= CAL_SUCCESS, "calendar_svc_unsubscribe_change() is failed");
493 }
494
495 void _calendar_svc_add_calendar_changed_callback(const char *type, void(*callback)(void *), void *user_data)
496 {
497         c_retm_if(!callback, "callback is null");
498         c_retm_if(!user_data, "user_data is null");
499
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");
502 }
503
504 void _calendar_svc_del_calendar_changed_callback(const char *type, void(*callback)(void *))
505 {
506         c_retm_if(!callback, "callback is null");
507
508         int ret = calendar_svc_unsubscribe_db_change(type, callback);
509         c_retm_if(ret!= CAL_SUCCESS, "calendar_svc_unsubscribe_db_change() is failed");
510 }