merge with master
[framework/pim/calendar-service.git] / native / cal_db_instance.c
1 /*
2  * Calendar Service
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <string.h>
21 #include <stdlib.h>
22 #include <glib.h>
23 #include <unicode/ucal.h>
24 #include <unicode/ustring.h>
25 #include <unicode/ustdio.h>
26 #include <unicode/udat.h>
27 #include <sys/types.h>
28
29 #include "calendar_db.h"
30 #include "calendar_types2.h"
31
32 #include "cal_internal.h"
33 #include "cal_typedef.h"
34 #include "cal_view.h"
35 #include "cal_time.h"
36 #include "cal_record.h"
37
38 #include "cal_db_util.h"
39 #include "cal_db.h"
40
41 #define ms2sec(ms) (long long int)(ms / 1000.0)
42 #define sec2ms(s) (s * 1000.0)
43
44 /* input order
45    UCAL_MONTH + UCAL_DAY_OF_MONTH
46    UCAL_MONTH + UCAL_WEEK_OF_MONTH + UCAL_DAY_OF_WEEK
47    UCAL_MONTH + UCAL_DAY_OF_WEEK_IN_MONTH + UCAL_DAY_OF_WEEK
48    UCAL_DAY_OF_YEAR
49    UCAL_DAY_OF_WEEK + UCAL_WEEK_OF_YEAR
50 */
51
52 struct day {
53         int uday;
54         const char *str;
55 };
56
57 #define CAL_ENDLESS_LIMIT_YEAR 2038
58 #define CAL_ENDLESS_LIMIT_MONTH 12
59 #define CAL_ENDLESS_LIMIT_MDAY 31
60
61 static struct day wdays[] = {
62         [CALENDAR_SUNDAY] = {UCAL_SUNDAY, "SU"},
63         [CALENDAR_MONDAY] = {UCAL_MONDAY, "MO"},
64         [CALENDAR_TUESDAY] = {UCAL_TUESDAY, "TU"},
65         [CALENDAR_WEDNESDAY] = {UCAL_WEDNESDAY, "WE"},
66         [CALENDAR_THURSDAY] = {UCAL_THURSDAY, "TH"},
67         [CALENDAR_FRIDAY] = {UCAL_FRIDAY, "FR"},
68         [CALENDAR_SATURDAY] = {UCAL_SATURDAY, "SA"},
69 };
70
71 static void __cal_db_instance_print_ucal(UCalendar *ucal)
72 {
73         long long int lli;
74         UErrorCode ec = U_ZERO_ERROR;
75
76         if (ucal)
77         {
78                 lli = ms2sec(ucal_getMillis(ucal, &ec));
79                 DBG("(%d)", lli);
80         }
81 }
82
83 static int __cal_db_instance_work_normal_setpos(int event_id, long long int lli_s, long long int lli_e, char **t)
84 {
85         int i, j;
86         int ret;
87         int count = 0;
88         int ids[366] = {0};
89         char str_option[CAL_DB_SQL_MAX_LEN] = {0};
90         char query[CAL_DB_SQL_MAX_LEN] = {0};
91         char str_fraction[64] = {0};
92     cal_db_util_error_e dbret = CAL_DB_OK;
93
94         j = -1;
95         for (i = 0; t[i];i++)
96         {
97                 snprintf(query, sizeof(query), "SELECT rowid FROM %s "
98                                 "WHERE event_id = %d "
99                                 "AND dtstart_utime >= %lld AND dtend_utime < %lld "
100                                 "ORDER BY dtstart_utime "
101                                 "LIMIT %d, 1 ",
102                                 CAL_TABLE_NORMAL_INSTANCE,
103                                 event_id,
104                                 lli_s, lli_e,
105                                 atoi(t[i]) - 1);
106                 ret = _cal_db_util_query_get_first_int_result(query, NULL, &count);
107                 if (CALENDAR_ERROR_NONE != ret)
108                 {
109                         ERR("_cal_db_util_query_get_first_int_result() failed");
110                         return ret;
111                 }
112                 if (count <= 0)
113                 {
114                         DBG("%s", query);
115                         DBG("no result");
116                         continue;
117                 }
118                 else
119                 {
120                         j++;
121                         ids[j] = count;
122                         DBG("append result");
123                 }
124                 DBG("[%lld] ~ [%lld] setpos id(%d)", lli_s, lli_e, ids[j]);
125         }
126
127         while (j >= 0)
128         {
129                 snprintf(str_fraction, sizeof(str_fraction), "AND rowid != %d ", ids[j]);
130                 DBG("fra:%s", str_fraction);
131                 strcat(str_option, str_fraction);
132                 DBG("opt:%s", str_option);
133                 j--;
134         }
135         DBG("str_option[%s]", str_option);
136         snprintf(query, sizeof(query), "DELETE FROM %s "
137                         "WHERE event_id = %d "
138                         "AND dtstart_utime>= %lld AND dtend_utime < %lld "
139                         "%s",
140                         CAL_TABLE_NORMAL_INSTANCE,
141                         event_id,
142                         lli_s, lli_e,
143                         str_option);
144         dbret = _cal_db_util_query_exec(query);
145         if (CAL_DB_OK != dbret)
146         {
147                 DBG("query[%s]", query);
148                 ERR("_cal_db_util_query_exec() Failed(%d)", dbret);
149                 switch (dbret)
150                 {
151                 case CAL_DB_ERROR_NO_SPACE:
152                         return CALENDAR_ERROR_FILE_NO_SPACE;
153                 default:
154                         return CALENDAR_ERROR_DB_FAILED;
155                 }
156         }
157
158         return CALENDAR_ERROR_NONE;
159 }
160
161 static int __cal_db_instance_work_allday_setpos(int event_id, char *str_s, char *str_e, char **t)
162 {
163         int i, j;
164         int ret;
165     cal_db_util_error_e dbret = CAL_DB_OK;
166         int count = 0;
167         int ids[366] = {0};
168         char str_option[CAL_DB_SQL_MAX_LEN] = {0};
169         char query[CAL_DB_SQL_MAX_LEN] = {0};
170         char str_fraction[64] = {0};
171
172         j = -1;
173         for (i = 0; t[i];i++)
174         {
175                 snprintf(query, sizeof(query), "SELECT rowid FROM %s "
176                                 "WHERE event_id = %d "
177                                 "AND dtstart_datetime >= %s AND dtend_datetime < %s "
178                                 "ORDER BY dtstart_datetime "
179                                 "LIMIT %d, 1 ",
180                                 CAL_TABLE_ALLDAY_INSTANCE,
181                                 event_id,
182                                 str_s, str_e,
183                                 atoi(t[i]) - 1);
184                 ret = _cal_db_util_query_get_first_int_result(query, NULL, &count);
185                 if (CALENDAR_ERROR_NONE != ret)
186                 {
187                         ERR("_cal_db_util_query_get_first_int_result() failed");
188                         return ret;
189                 }
190                 if (count <= 0)
191                 {
192                         DBG("%s", query);
193                         DBG("no result");
194                         continue;
195                 }
196                 else
197                 {
198                         j++;
199                         ids[j] = count;
200                         DBG("append result");
201                 }
202                 DBG("[%s] ~ [%s] setpos id(%d)", str_s, str_e, ids[j]);
203         }
204
205         while (j >= 0)
206         {
207                 snprintf(str_fraction, sizeof(str_fraction), "AND rowid != %d ", ids[j]);
208                 DBG("fra:%s", str_fraction);
209                 strcat(str_option, str_fraction);
210                 DBG("opt:%s", str_option);
211                 j--;
212         }
213         DBG("str_option[%s]", str_option);
214         snprintf(query, sizeof(query), "DELETE FROM %s "
215                         "WHERE event_id = %d "
216                         "AND dtstart_datetime >= %s AND dtend_datetime < %s "
217                         "%s",
218                         CAL_TABLE_ALLDAY_INSTANCE,
219                         event_id,
220                         str_s, str_e,
221                         str_option);
222         dbret = _cal_db_util_query_exec(query);
223         if (CAL_DB_OK != dbret)
224         {
225                 DBG("query[%s]", query);
226                 ERR("_cal_db_util_query_exec() Failed(%d)", dbret);
227                 switch (dbret)
228                 {
229                 case CAL_DB_ERROR_NO_SPACE:
230                         return CALENDAR_ERROR_FILE_NO_SPACE;
231                 default:
232                         return CALENDAR_ERROR_DB_FAILED;
233                 }
234         }
235
236         return CALENDAR_ERROR_NONE;
237 }
238
239 static int __cal_db_instance_apply_setpos(int event_id, calendar_time_s *st, cal_event_s *event, int freq)
240 {
241         int ret;
242         int count;
243         int index;
244         int y, m, d;
245         int y1, m1, d1;
246         long long int lli_s = 0, lli_e = 0;
247         char **t;
248         const char *dl = ",";
249         char str_t[32] = {0};
250         char str_s[32] = {0};
251         char str_e[32] = {0};
252         char query[CAL_DB_SQL_MAX_LEN] = {0};
253         UCalendar *ucal;
254         UErrorCode ec = U_ZERO_ERROR;
255
256         t = g_strsplit(event->bysetpos, dl, -1);
257         if (!t)
258         {
259                 ERR("g_strsplit failed");
260                 return CALENDAR_ERROR_OUT_OF_MEMORY;
261         }
262
263         switch (st->type)
264         {
265         case CALENDAR_TIME_UTIME:
266                 DBG("(%lld)", st->time.utime);
267                 snprintf(query, sizeof(query), "SELECT count(*) FROM %s "
268                                 "WHERE event_id = %d "
269                                 "AND dtstart_utime >= %lld",
270                                 CAL_TABLE_NORMAL_INSTANCE,
271                                 event_id,
272                                 st->time.utime);
273                 ret = _cal_db_util_query_get_first_int_result(query, NULL, &count);
274                 if (CALENDAR_ERROR_NONE != ret)
275                 {
276                         ERR("_cal_db_util_query_get_first_int_result() failed");
277                         g_strfreev(t);
278                         return ret;
279                 }
280                 DBG("count(%d)", count);
281                 index = 0;
282
283                 while (count > 0)
284                 {
285                         ucal = _cal_time_get_ucal(event->start_tzid, event->wkst);
286                         ucal_setMillis(ucal, sec2ms(st->time.utime), &ec);
287                         switch (freq)
288                         {
289                         case CALENDAR_RECURRENCE_YEARLY:
290                                 ucal_add(ucal, UCAL_YEAR, index, &ec);
291                                 ucal_set(ucal, UCAL_MONTH, 1);
292                                 ucal_set(ucal, UCAL_DATE, 1);
293                                 lli_s = ms2sec(ucal_getMillis(ucal, &ec));
294
295                                 ucal_add(ucal, UCAL_YEAR, 1, &ec);
296                                 lli_e = ms2sec(ucal_getMillis(ucal, &ec));
297                                 break;
298
299                         case CALENDAR_RECURRENCE_MONTHLY:
300                                 ucal_add(ucal, UCAL_MONTH, index, &ec);
301                                 ucal_set(ucal, UCAL_DATE, 1);
302                                 lli_s = ms2sec(ucal_getMillis(ucal, &ec));
303
304                                 ucal_add(ucal, UCAL_MONTH, 1, &ec);
305                                 lli_e = ms2sec(ucal_getMillis(ucal, &ec));
306                                 break;
307
308                         case CALENDAR_RECURRENCE_WEEKLY:
309                                 ucal_add(ucal, UCAL_DATE, (7 * index), &ec);
310                                 ucal_set(ucal, UCAL_DAY_OF_WEEK, 1);
311                                 lli_s = ms2sec(ucal_getMillis(ucal, &ec));
312
313                                 ucal_add(ucal, UCAL_DATE, 7, &ec);
314                                 lli_e = ms2sec(ucal_getMillis(ucal, &ec));
315                                 break;
316
317                         default:
318                                 break;
319                         }
320                         ucal_close(ucal);
321
322                         DBG("%lld %lld", lli_s, lli_e);
323                         __cal_db_instance_work_normal_setpos(event_id, lli_s, lli_e, t);
324                         snprintf(query, sizeof(query), "SELECT count(*) FROM %s "
325                                         "WHERE event_id = %d "
326                                         "AND dtstart_utime >= %lld",
327                                         CAL_TABLE_NORMAL_INSTANCE,
328                                         event_id,
329                                         lli_e);
330                         ret = _cal_db_util_query_get_first_int_result(query, NULL, &count);
331                         if (CALENDAR_ERROR_NONE != ret)
332                         {
333                                 ERR("_cal_db_util_query_get_first_int_result() failed");
334                                 g_strfreev(t);
335                                 return ret;
336                         }
337                         DBG("setpos left count(%d) from [%lld]", count, lli_e);
338                         index++;
339                 }
340                 break;
341
342         case CALENDAR_TIME_LOCALTIME:
343                 y = st->time.date.year;
344                 m = st->time.date.month;
345                 d = st->time.date.mday;
346                 DBG("%d/%d/%d", y, m, d);
347                 snprintf(str_t, sizeof(str_t), "%04d%02d%02d", y, m, d);
348                 snprintf(query, sizeof(query), "SELECT count(*) FROM %s "
349                                 "WHERE event_id = %d "
350                                 "AND dtstart_datetime >= %s",
351                                 CAL_TABLE_ALLDAY_INSTANCE,
352                                 event_id,
353                                 str_t);
354                 ret = _cal_db_util_query_get_first_int_result(query, NULL, &count);
355                 if (CALENDAR_ERROR_NONE != ret)
356                 {
357                         ERR("_cal_db_util_query_get_first_int_result() failed");
358                         g_strfreev(t);
359                         return ret;
360                 }
361                 DBG("count(%d)", count);
362                 index = 0;
363
364                 while (count > 0)
365                 {
366                         // _cal_db_instance_get_next_period(event, str_s, str_e, index);
367                         switch (freq)
368                         {
369                         case CALENDAR_RECURRENCE_YEARLY:
370                                 snprintf(str_s, sizeof(str_s), "%04d%02d%02d", y + index, 1, 1);
371                                 snprintf(str_e, sizeof(str_e), "%04d%02d%02d", y + index + 1, 1, 1);
372                                 break;
373
374                         case CALENDAR_RECURRENCE_MONTHLY:
375                                 snprintf(str_s, sizeof(str_s), "%04d%02d%02d",
376                                                 y + ((m + index) / 13),
377                                                 (m + index) % 13 == 0 ? 1 : (m + index) % 13,
378                                                 1);
379                                 snprintf(str_e, sizeof(str_e), "%04d%02d%02d",
380                                                 y + ((m + index + 1) / 13),
381                                                 (m + index + 1) % 13 == 0 ? 1 : (m + index + 1) % 13,
382                                                 1);
383                                 break;
384
385                         case CALENDAR_RECURRENCE_WEEKLY:
386                                 ucal = _cal_time_get_ucal(event->start_tzid, event->wkst);
387                                 ucal_setDate(ucal, y, m - 1, d + (7 * index), &ec);
388                                 y1 = ucal_get(ucal, UCAL_YEAR, &ec);
389                                 m1 = ucal_get(ucal, UCAL_MONTH, &ec) + 1;
390                                 d1 = ucal_get(ucal, UCAL_DATE, &ec);
391                                 DBG("start(%d/%d/%d", y1, m1, d1);
392                                 ucal_set(ucal, UCAL_DAY_OF_WEEK, 1);
393                                 y1 = ucal_get(ucal, UCAL_YEAR, &ec);
394                                 m1 = ucal_get(ucal, UCAL_MONTH, &ec) + 1;
395                                 d1 = ucal_get(ucal, UCAL_DATE, &ec);
396                                 snprintf(str_s, sizeof(str_s), "%04d%02d%02d",
397                                                 y1, m1, d1);
398                                 DBG("start(%d/%d/%d", y1, m1, d1);
399
400                                 ucal_add(ucal, UCAL_DATE, 7, &ec);
401                                 y1 = ucal_get(ucal, UCAL_YEAR, &ec);
402                                 m1 = ucal_get(ucal, UCAL_MONTH, &ec) + 1;
403                                 d1 = ucal_get(ucal, UCAL_DATE, &ec);
404                                 snprintf(str_e, sizeof(str_e), "%04d%02d%02d",
405                                                 y1, m1, d1);
406                                 DBG("end(%d/%d/%d", y1, m1, d1);
407                                 ucal_close(ucal);
408                                 break;
409
410                         case CALENDAR_RECURRENCE_DAILY:
411                                 DBG("Not support setpos in daily");
412                                 break;
413
414                         default:
415                                 break;
416                         }
417
418                         __cal_db_instance_work_allday_setpos(event_id, str_s, str_e, t);
419
420                         snprintf(query, sizeof(query), "SELECT count(*) FROM %s "
421                                         "WHERE event_id = %d "
422                                         "AND dtstart_datetime >= %s",
423                                         CAL_TABLE_ALLDAY_INSTANCE,
424                                         event_id,
425                                         str_e);
426 //                      DBG("%s", query);
427                         ret = _cal_db_util_query_get_first_int_result(query, NULL, &count);
428                         if (CALENDAR_ERROR_NONE != ret)
429                         {
430                                 ERR("_cal_db_util_query_get_first_int_result() failed");
431                                 g_strfreev(t);
432                                 return ret;
433                         }
434                         DBG("setpos left count(%d) from [%s]", count, str_e);
435                         index++;
436                 }
437                 break;
438
439         default:
440                 break;
441         }
442         g_strfreev(t);
443         return CALENDAR_ERROR_NONE;
444 }
445
446 static int __cal_db_instance_update_exdate_del(int id, char *exdate)
447 {
448         int dbret;
449         int i, j;
450         int y, mon, d, h, min, s;
451         char query[CAL_DB_SQL_MAX_LEN] = {0};
452         const char *dl = ",";
453         char **t = NULL;
454         char *p = NULL;
455
456         if (exdate == NULL)
457         {
458                 DBG("Nothing to update exdate del");
459                 return CALENDAR_ERROR_NONE;
460         }
461
462         DBG("exdate [%s]", exdate);
463         t = g_strsplit(exdate, dl, -1);
464         if (!t)
465         {
466                 ERR("g_strsplit failed");
467                 return CALENDAR_ERROR_OUT_OF_MEMORY;
468         }
469
470         for (i = 0; t[i]; i++)
471         {
472                 p = t[i];
473                 // remove space
474                 j = 0;
475                 while (p[j] == ' ')
476                 {
477                         j++;
478                 }
479                 p = t[i] + j;
480                 DBG("exdate[%s]", p);
481
482                 if (strlen(p) > strlen("YYYYMMDD"))
483                 {
484                         DBG("NORMAL instance");
485                         sscanf(p, "%04d%02d%02dT%02d%02d%02dZ",
486                                         &y, &mon, &d, &h, &min, &s);
487                         snprintf(query, sizeof(query),
488                                         "DELETE FROM %s "
489                                         "WHERE event_id = %d AND dtstart_utime = %lld ",
490                                         CAL_TABLE_NORMAL_INSTANCE,
491                                         id, _cal_time_convert_itol(NULL, y, mon, d, h, min, s));
492                         DBG("(%lld)", _cal_time_convert_itol(NULL, y, mon, d, h, min, s));
493                 }
494                 else
495                 {
496                         DBG("ALLDAY instance");
497                         snprintf(query, sizeof(query),
498                                         "DELETE FROM %s "
499                                         "WHERE event_id = %d AND dtstart_datetime = %s ",
500                                         CAL_TABLE_ALLDAY_INSTANCE,
501                                         id, p);
502
503                 }
504                 dbret = _cal_db_util_query_exec(query);
505                 if (dbret != CAL_DB_OK) {
506                         ERR("_cal_db_util_query_exec() failed (%d)", dbret);
507                         g_strfreev(t);
508                         switch (dbret)
509                         {
510                         case CAL_DB_ERROR_NO_SPACE:
511                                 return CALENDAR_ERROR_FILE_NO_SPACE;
512                         default:
513                                 return CALENDAR_ERROR_DB_FAILED;
514                         }
515                 }
516         }
517         g_strfreev(t);
518         return CALENDAR_ERROR_NONE;
519 }
520
521 static int __cal_db_instance_update_exdate_mod(int original_event_id, char *recurrence_id)
522 {
523         int dbret;
524         int i, j;
525         int y, mon, d, h, min, s;
526         const char *dl = ",";
527         char **t = NULL;
528         char *p = NULL;
529         char query[CAL_DB_SQL_MAX_LEN] = {0};
530
531         if (original_event_id < 1 || recurrence_id == NULL)
532         {
533                 DBG("Nothing to update exdate mod");
534                 return CALENDAR_ERROR_NONE;
535         }
536
537         DBG("recurrence_id[%s]", recurrence_id);
538         t = g_strsplit(recurrence_id, dl, -1);
539         if (!t)
540         {
541                 ERR("g_strsplit failed");
542                 return CALENDAR_ERROR_OUT_OF_MEMORY;
543         }
544
545         for (i = 0; t[i]; i++)
546         {
547                 p = t[i];
548                 // remove space
549                 j = 0;
550                 while (p[j] == ' ')
551                 {
552                         j++;
553                 }
554                 p = t[i] + j;
555                 DBG("%d[%s]", i + 1, p);
556
557                 if (strlen(p) > strlen("YYYYMMDD"))
558                 {
559                         DBG("NORMAL instance");
560                         sscanf(p, "%04d%02d%02dT%02d%02d%02dZ",
561                                         &y, &mon, &d, &h, &min, &s);
562                         snprintf(query, sizeof(query),
563                                         "DELETE FROM %s "
564                                         "WHERE event_id = %d AND dtstart_utime = %lld ",
565                                         CAL_TABLE_NORMAL_INSTANCE,
566                                         original_event_id, _cal_time_convert_itol(NULL, y, mon, d, h, min, s));
567                         DBG("(%lld)", _cal_time_convert_itol(NULL, y, mon, d, h, min, s));
568                 }
569                 else
570                 {
571                         DBG("ALLDAY instance");
572                         snprintf(query, sizeof(query),
573                                         "DELETE FROM %s "
574                                         "WHERE event_id = %d AND dtstart_datetime = %s ",
575                                         CAL_TABLE_ALLDAY_INSTANCE,
576                                         original_event_id, p);
577
578                 }
579                 dbret = _cal_db_util_query_exec(query);
580                 if (dbret != CAL_DB_OK) {
581                         ERR("_cal_db_util_query_exec() failed (%d)", dbret);
582                         g_strfreev(t);
583                         switch (dbret)
584                         {
585                         case CAL_DB_ERROR_NO_SPACE:
586                                 return CALENDAR_ERROR_FILE_NO_SPACE;
587                         default:
588                                 return CALENDAR_ERROR_DB_FAILED;
589                         }
590                 }
591         }
592         g_strfreev(t);
593         return CALENDAR_ERROR_NONE;
594 }
595
596 static inline int ___cal_db_instance_has_after(calendar_time_s *t1, calendar_time_s *t2)
597 {
598         if (t1->type == CALENDAR_TIME_UTIME) {
599                 if (t1->time.utime > t2->time.utime)
600                         return 1;
601                 else
602                         return 0;
603         }
604
605         DBG("%d %d %d /%d %d %d", t1->time.date.year, t1->time.date.month, t1->time.date.mday,
606                         t2->time.date.year, t2->time.date.month, t2->time.date.mday);
607         if (t1->time.date.year > t2->time.date.year) {
608                 DBG("exit year");
609                 return 1;
610         } else if (t1->time.date.year < t2->time.date.year) {
611                 return 0;
612         } else {
613                 if (t1->time.date.month > t2->time.date.month) {
614                         return 1;
615                 } else if (t1->time.date.month < t2->time.date.month) {
616                         return 0;
617                 } else {
618                         if (t1->time.date.mday > t2->time.date.mday) {
619                                 return 1;
620                         } else {
621                                 return 0;
622                         }
623                 }
624         }
625 }
626
627 static inline int __cal_db_instance_convert_mday(const char *str, int *mday)
628 {
629         int d;
630
631         if (!str || !*str) {
632                 ERR("Invalid argument: check mday[%s]", str);
633                 return CALENDAR_ERROR_INVALID_PARAMETER;
634         }
635
636         d = atoi(str);
637         if (d < 1 || d > 31) {
638                 ERR("Invalid argument: check day(%d)", d);
639                 return CALENDAR_ERROR_INVALID_PARAMETER;
640         }
641
642         DBG("get mday[%s] and convert to int(%d)", str, d);
643         *mday = d;
644         return CALENDAR_ERROR_NONE;
645 }
646
647 static int __cal_db_instance_del_inundant(int event_id, calendar_time_s *st, cal_event_s *event)
648 {
649     cal_db_util_error_e dbret = CAL_DB_OK;
650         int cnt;
651         char query[CAL_DB_SQL_MAX_LEN];
652
653         if (event->range_type != CALENDAR_RANGE_COUNT) {
654                 return CALENDAR_ERROR_NONE;
655         }
656
657         cnt = event->count;
658         DBG("get count(%d) and del after this", cnt);
659
660         if (st->type == CALENDAR_TIME_UTIME) {
661                 snprintf(query, sizeof(query), "DELETE FROM %s "
662                                 "WHERE event_id = %d "
663                                 "AND dtstart_utime > (SELECT dtstart_utime FROM %s "
664                                 "WHERE event_id = %d ORDER BY dtstart_utime LIMIT %d, 1) ",
665                                 CAL_TABLE_NORMAL_INSTANCE,
666                                 event_id,
667                                 CAL_TABLE_NORMAL_INSTANCE,
668                                 event_id, cnt -1);
669
670         } else if (st->type == CALENDAR_TIME_LOCALTIME) {
671                 snprintf(query, sizeof(query), "DELETE FROM %s "
672                                 "WHERE event_id = %d "
673                                 "AND dtstart_datetime > (SELECT dtstart_datetime FROM %s "
674                                 "WHERE event_id = %d ORDER BY dtstart_datetime LIMIT %d, 1) ",
675                                 CAL_TABLE_ALLDAY_INSTANCE,
676                                 event_id,
677                                 CAL_TABLE_ALLDAY_INSTANCE,
678                                 event_id, cnt -1);
679         }
680
681         dbret = _cal_db_util_query_exec(query);
682         if (dbret != CAL_DB_OK)
683         {
684                 DBG("query(%s)", query);
685                 switch (dbret)
686                 {
687                 case CAL_DB_ERROR_NO_SPACE:
688                         return CALENDAR_ERROR_FILE_NO_SPACE;
689                 default:
690                         return CALENDAR_ERROR_DB_FAILED;
691                 }
692         }
693         return CALENDAR_ERROR_NONE;
694 }
695
696 static void __cal_db_instance_print_caltime(calendar_time_s *caltime)
697 {
698         if (caltime) {
699                 switch (caltime->type) {
700                 case CALENDAR_TIME_UTIME:
701                         DBG("utime(%lld)", caltime->time.utime);
702                         break;
703
704                 case CALENDAR_TIME_LOCALTIME:
705                         DBG("datetime(%04d/%02d/%02d)",
706                                         caltime->time.date.year, caltime->time.date.month, caltime->time.date.mday);
707                         break;
708                 }
709         }
710 }
711
712 static int __cal_db_instance_get_duration(UCalendar *ucal, calendar_time_s *st, calendar_time_s *et, int *duration)
713 {
714         int _duration = -1;
715         UErrorCode ec = U_ZERO_ERROR;
716         UDate ud;
717
718         if (NULL == ucal)
719         {
720                 ERR("Invalid parameter: ucal is NULL");
721                 return CALENDAR_ERROR_INVALID_PARAMETER;
722         }
723
724         if (NULL == st || NULL == et)
725         {
726                 ERR("Invalid parameter: calendar_time_s is NULL");
727                 return CALENDAR_ERROR_INVALID_PARAMETER;
728         }
729
730         switch (st->type) {
731         case CALENDAR_TIME_UTIME:
732                 if (st->time.utime > et->time.utime)
733                 {
734                         ERR("check time: start(%lld) > end(%lld)", st->time.utime, et->time.utime);
735                         return CALENDAR_ERROR_INVALID_PARAMETER;
736                 }
737                 _duration = (int)(et->time.utime - st->time.utime);
738                 break;
739
740         case CALENDAR_TIME_LOCALTIME:
741                 ucal_setDateTime(ucal, et->time.date.year, et->time.date.month -1, et->time.date.mday, 0, 0, 0, &ec);
742                 ud = ucal_getMillis(ucal, &ec);
743
744                 ucal_setDateTime(ucal, st->time.date.year, st->time.date.month -1, st->time.date.mday, 0, 0, 0, &ec);
745
746                 _duration = ucal_getFieldDifference(ucal, ud, UCAL_DATE, &ec);
747                 if (U_FAILURE(ec))
748                 {
749                         ERR("ucal_getFieldDifference failed (%s)", u_errorName(ec));
750                         return ec;
751                 }
752                 break;
753         }
754
755         if (duration) *duration = _duration;
756         return CALENDAR_ERROR_NONE;
757 }
758
759 static int __cal_db_instance_parse_byday(const char *byday, int *nth, int *wday)
760 {
761         int _nth = 0;
762         int _wday = 0;
763         char buf[3] = {0};
764
765         if (NULL == byday || strlen(byday) == 0)
766         {
767                 ERR("Invalid parameter: byday is NULL");
768                 return CALENDAR_ERROR_INVALID_PARAMETER;
769         }
770
771         if (!sscanf(byday, "%d", &_nth)) {
772                 DBG("no digit in front of byday, so set 1 as default");
773                 if (sscanf(byday, "%s", buf) != 1) {
774                         ERR("Failed to get byday[%s]", byday);
775                         return -1;
776                 }
777                 _nth = 1;
778         } else {
779                 if (sscanf(byday, "%d%s", &_nth, buf) != 2) {
780                         ERR("Failed to get byday[%s]", byday);
781                         return -1;
782                 }
783         }
784         buf[2] = '\0';
785
786         // CALENDAR_SUNDAY
787         for (_wday = CALENDAR_SUNDAY; _wday <= CALENDAR_SATURDAY; _wday++) {
788                 if (!strncmp(wdays[_wday].str, buf, 2)) {
789                         DBG("inserted nth(%d) wday[%s]", _nth, wdays[_wday].str);
790                         break;
791                 }
792         }
793
794         if (nth) *nth = _nth > 4 ? -1 : _nth;
795         if (wday) *wday = _wday;
796
797         return CALENDAR_ERROR_NONE;
798 }
799
800 static void __cal_db_instance_set_ucal_start(UCalendar *ucal, calendar_time_s *st)
801 {
802         UErrorCode ec = U_ZERO_ERROR;
803
804         if (ucal)
805         {
806                 switch (st->type)
807                 {
808                 case CALENDAR_TIME_UTIME:
809                         ucal_setMillis(ucal, sec2ms(st->time.utime), &ec);
810                         break;
811
812                 case CALENDAR_TIME_LOCALTIME:
813                         ucal_setDate(ucal, st->time.date.year,
814                                         st->time.date.month -1, st->time.date.mday, &ec);
815                         break;
816                 }
817         }
818 }
819
820 static void __cal_db_instance_set_wday(UCalendar *ucal, int nth, int wday)
821 {
822         if (ucal)
823         {
824                 if (nth > 0) ucal_set(ucal, UCAL_DAY_OF_WEEK, wday);
825                 if (wday > 0) ucal_set(ucal, UCAL_DAY_OF_WEEK_IN_MONTH, nth);
826         }
827 }
828
829 static int __cal_db_instance_insert_record(UCalendar *ucal, int duration, int type, int event_id)
830 {
831         int ret;
832         long long int lli;
833         UErrorCode ec = U_ZERO_ERROR;
834         calendar_record_h record = NULL;
835         calendar_time_s st = {0};
836         calendar_time_s et = {0};
837         UCalendar *ucal2 = NULL;
838
839         if (NULL == ucal)
840         {
841                 ERR("Invalid parameter: ucal is NULL");
842                 return CALENDAR_ERROR_INVALID_PARAMETER;
843         }
844
845         switch (type)
846         {
847         case CALENDAR_TIME_UTIME:
848                 lli =  ms2sec(ucal_getMillis(ucal, &ec));
849
850                 st.type = type;
851                 st.time.utime = lli;
852                 DBG("insert normal (%lld)%04d-%02d-%02d %02d:%02d:00",
853                                 st.time.utime,
854                                 ucal_get(ucal, UCAL_YEAR, &ec),
855                                 ucal_get(ucal, UCAL_MONTH, &ec) +1,
856                                 ucal_get(ucal, UCAL_DATE, &ec),
857                                 ucal_get(ucal, UCAL_HOUR_OF_DAY, &ec),
858                                 ucal_get(ucal, UCAL_MINUTE, &ec));
859                 et.type = type;
860                 et.time.utime = lli + (long long int)duration;
861
862                 ret = calendar_record_create(_calendar_instance_normal._uri, &record);
863                 if (CALENDAR_ERROR_NONE != ret)
864                 {
865                         ERR("calendar_record_create() failed");
866                         return CALENDAR_ERROR_DB_FAILED;
867                 }
868                 _cal_record_set_int(record, _calendar_instance_normal.event_id, event_id);
869                 ret = _cal_record_set_caltime(record, _calendar_instance_normal.start_time, st);
870                 if (CALENDAR_ERROR_NONE != ret)
871                 {
872                         ERR("_cal_record_set_caltime() failed");
873                         return ret;
874                 }
875                 ret = _cal_record_set_caltime(record, _calendar_instance_normal.end_time, et);
876                 if (CALENDAR_ERROR_NONE != ret)
877                 {
878                         ERR("_cal_record_set_caltime() failed");
879                         return ret;
880                 }
881                 break;
882
883         case CALENDAR_TIME_LOCALTIME:
884                 st.type = type;
885                 st.time.date.year = ucal_get(ucal, UCAL_YEAR, &ec);
886                 st.time.date.month = ucal_get(ucal, UCAL_MONTH, &ec) +1;
887                 st.time.date.mday = ucal_get(ucal, UCAL_DATE, &ec);
888                 DBG("insert allday %04d-%02d-%02d",
889                                 st.time.date.year, st.time.date.month, st.time.date.mday);
890                 et.type = type;
891                 if (duration > 0)
892                 {
893                         ucal2 = ucal_clone(ucal, &ec);
894                         ucal_add(ucal2, UCAL_DATE, duration, &ec);
895                         et.time.date.year = ucal_get(ucal2, UCAL_YEAR, &ec);
896                         et.time.date.month = ucal_get(ucal2, UCAL_MONTH, &ec) +1;
897                         et.time.date.mday = ucal_get(ucal2, UCAL_DATE, &ec);
898                         ucal_close(ucal2);
899                 }
900                 else
901                 {
902                         et.time.date.year = st.time.date.year;
903                         et.time.date.month = st.time.date.month;
904                         et.time.date.mday = st.time.date.mday;
905                 }
906
907                 ret = calendar_record_create(_calendar_instance_allday._uri, &record);
908                 if (CALENDAR_ERROR_NONE != ret)
909                 {
910                         ERR("calendar_record_create() failed");
911                         return ret;
912                 }
913                 ret = _cal_record_set_int(record, _calendar_instance_allday.event_id, event_id);
914                 if (CALENDAR_ERROR_NONE != ret)
915                 {
916                         ERR("_cal_record_set_int() failed");
917                         return ret;
918                 }
919                 ret = _cal_record_set_caltime(record, _calendar_instance_allday.start_time, st);
920                 if (CALENDAR_ERROR_NONE != ret)
921                 {
922                         ERR("_cal_record_set_caltime() failed");
923                         return ret;
924                 }
925                 ret = _cal_record_set_caltime(record, _calendar_instance_allday.end_time, et);
926                 if (CALENDAR_ERROR_NONE != ret)
927                 {
928                         ERR("_cal_record_set_caltime() failed");
929                         return ret;
930                 }
931                 break;
932         }
933         ret = calendar_db_insert_record(record, NULL);
934         if (CALENDAR_ERROR_NONE != ret)
935         {
936                 ERR("calendar_db_insert_record() failed");
937                 return ret;
938         }
939         calendar_record_destroy(record, true);
940
941         return CALENDAR_ERROR_NONE;
942 }
943
944 static int __cal_db_instance_has_next(UCalendar *ucal, calendar_time_s *until)
945 {
946         int has_next = 0;
947         long long int lli = 0;
948         long long int lli2 = 0;
949         UErrorCode ec = U_ZERO_ERROR;
950         UCalendar *ucal2 = NULL;
951
952         if (NULL == ucal)
953         {
954                 ERR("Invalid parameter: ucal is NULL");
955                 return CALENDAR_ERROR_INVALID_PARAMETER;
956         }
957         if (NULL == until)
958         {
959                 ERR("Invalid parameter: until is NULL");
960                 return CALENDAR_ERROR_INVALID_PARAMETER;
961         }
962
963         switch (until->type)
964         {
965         case CALENDAR_TIME_UTIME:
966                 lli = ms2sec(ucal_getMillis(ucal, &ec));
967                 has_next = (lli > until->time.utime) ? 0 : 1;
968                 if (has_next == 0)
969                 {
970                         DBG("No next:get(%lld) until(%lld)", lli, until->time.utime);
971                 }
972                 break;
973
974         case CALENDAR_TIME_LOCALTIME:
975                 lli = ms2sec(ucal_getMillis(ucal, &ec));
976
977                 ucal2 = ucal_clone(ucal, &ec);
978                 ucal_setDateTime(ucal2, until->time.date.year, until->time.date.month -1,
979                                 until->time.date.mday, 0, 0, 0, &ec);
980                 lli2 = ms2sec(ucal_getMillis(ucal2, &ec));
981                 ucal_close(ucal2);
982
983                 has_next = (lli > lli2) ? 0 : 1;
984                 if (has_next == 0)
985                 {
986                         DBG("No next:get(%lld) until(%lld)", lli, lli2);
987                 }
988                 break;
989         }
990
991
992         return has_next;
993 }
994
995 static int __cal_db_instance_publish_with_wday(UCalendar *ucal, cal_event_s *event, int duration, int field, calendar_time_s *until)
996 {
997         int ret = CALENDAR_ERROR_NONE;
998         int i, j;
999         int count = 0;
1000         int length = 0;
1001         char **t = NULL;
1002         const char *d = ",";
1003         UDate ud1, ud2;
1004         UErrorCode ec = U_ZERO_ERROR;
1005
1006         if (event->byday && strlen(event->byday) > 0)
1007         {
1008                 // range: MO, TU, WE, TH, FR, SA, SU ex> 3TH, -1MO
1009                 t = g_strsplit(event->byday, d, -1);
1010                 if (!t) {
1011                         ERR("g_strsplit failed");
1012                         g_strfreev(t);
1013                         return CALENDAR_ERROR_OUT_OF_MEMORY;
1014                 }
1015                 length = g_strv_length(t);
1016         }
1017         else
1018         {
1019                 int nth = 0, wday = 0;
1020                 int nth2;
1021                 char buf[8] = {0};
1022                 UCalendar *ucal2 = NULL;
1023
1024                 ucal2 = ucal_clone(ucal, &ec);
1025
1026                 switch (event->start.type)
1027                 {
1028                 case CALENDAR_TIME_UTIME:
1029                         ucal_setMillis(ucal, sec2ms(event->start.time.utime), &ec);
1030                         break;
1031
1032                 case CALENDAR_TIME_LOCALTIME:
1033                         ucal_setDate(ucal2, event->start.time.date.year,
1034                                         event->start.time.date.month -1, event->start.time.date.mday, &ec);
1035                         break;
1036                 }
1037
1038                 wday = ucal_get(ucal, UCAL_DAY_OF_WEEK, &ec);
1039                 wday = wday < UCAL_SUNDAY ? UCAL_SUNDAY : wday;
1040
1041                 nth = ucal_get(ucal2, UCAL_DAY_OF_WEEK_IN_MONTH, &ec);
1042                 ucal_add(ucal, UCAL_DATE, 7, &ec);
1043                 nth2 = ucal_get(ucal2, UCAL_DAY_OF_WEEK_IN_MONTH, &ec);
1044                 nth = (nth2 == 1) ? -1 : nth;
1045
1046                 ucal_close(ucal2);
1047
1048                 snprintf(buf, sizeof(buf), "%d%s", nth, wdays[wday].str);
1049                 t = g_strsplit(buf, d, -1);
1050                 if (!t) {
1051                         ERR("g_strsplit failed");
1052                         g_strfreev(t);
1053                         return CALENDAR_ERROR_OUT_OF_MEMORY;
1054                 }
1055                 DBG("No byday, so set wday(%s)", t[0]);
1056
1057                 length = 1;
1058         }
1059         DBG("length(%d)", length);
1060
1061         if (event->count)
1062         {
1063                 count = event->count;
1064
1065                 if (event->bysetpos)
1066                 {
1067                         int share, remind;
1068                         int length2;
1069                         char **t2 = NULL;
1070
1071                         t2 = g_strsplit(event->bysetpos, ",", -1);
1072                         length2 = g_strv_length(t2);
1073                         g_strfreev(t2);
1074
1075                         share = length / length2;
1076                         remind = length % length2;
1077                         count = count * share + (remind ? length : 0);
1078                 }
1079                 else
1080                 {
1081                         count = count / length + ((count % length == 0) ? 0 : 1);
1082                 }
1083                 DBG("bycount(%d) count(%d)", event->count, count);
1084         }
1085
1086         ud1 = ucal_getMillis(ucal, &ec);
1087         int week = ucal_get(ucal, field, &ec);
1088
1089         for (i = 0; i < length; i++)
1090         {
1091                 // set nth,  wday
1092                 int nth = 0, wday = 0;
1093                 ret = __cal_db_instance_parse_byday(t[i], &nth, &wday);
1094                 if (CALENDAR_ERROR_NONE != ret)
1095                 {
1096                         ERR("__cal_db_instance_parse_byday() failed");
1097                         g_strfreev(t);
1098                         return ret;
1099                 }
1100                 ucal_setMillis(ucal, ud1, &ec);
1101                 switch (field)
1102                 {
1103                 case UCAL_WEEK_OF_YEAR:
1104                         ucal_set(ucal, field, week);
1105                         ucal_set(ucal, UCAL_DAY_OF_WEEK, wday);
1106                         break;
1107                 default:
1108                         __cal_db_instance_set_wday(ucal, nth, wday);
1109                         break;
1110                 }
1111                 DBG("set wday nth(%d) wday(%d)", nth, wday);
1112
1113                 // check whether set ucal goes past or not
1114                 ud1 = ucal_getMillis(ucal, &ec);
1115
1116                 UCalendar *ucal2 = NULL;
1117                 ucal2 = ucal_clone(ucal, &ec);
1118                 __cal_db_instance_set_ucal_start(ucal2, &event->start);
1119                 ud2 = ucal_getMillis(ucal2, &ec);
1120                 ucal_close(ucal2);
1121
1122                 if (ms2sec(ud2) > ms2sec(ud1))
1123                 {
1124                         DBG("Invalid first time: start(%lld) get(%lld)", ms2sec(ud2), ms2sec(ud1));
1125                         switch (field)
1126                         {
1127                         case UCAL_WEEK_OF_YEAR:
1128                                 ucal_add(ucal, field, event->interval, &ec);
1129                                 break;
1130
1131                         default:
1132                                 ucal_add(ucal, field, event->interval, &ec);
1133                                 __cal_db_instance_set_wday(ucal, nth, wday);
1134                                 break;
1135                         }
1136                 }
1137
1138                 DBG("range type(%d) interval(%d)", event->range_type, event->interval);
1139                 switch (event->range_type)
1140                 {
1141                 case CALENDAR_RANGE_COUNT:
1142                         for (j = 0; j < count; j++)
1143                         {
1144                                 __cal_db_instance_insert_record(ucal, duration, event->start.type, event->index);
1145                                 ucal_add(ucal, field, event->interval, &ec);
1146                                 switch (field)
1147                                 {
1148                                 case UCAL_WEEK_OF_YEAR:
1149                                         break;
1150                                 default:
1151                                         __cal_db_instance_set_wday(ucal, nth, wday);
1152                                         break;
1153                                 }
1154                         }
1155                         break;
1156
1157                 case CALENDAR_RANGE_UNTIL:
1158                 case CALENDAR_RANGE_NONE:
1159                         while (__cal_db_instance_has_next(ucal, until))
1160                         {
1161                                 __cal_db_instance_insert_record(ucal, duration, event->start.type, event->index);
1162                                 ucal_add(ucal, field, event->interval, &ec);
1163                                 switch (field)
1164                                 {
1165                                 case UCAL_WEEK_OF_YEAR:
1166                                         break;
1167                                 default:
1168                                         __cal_db_instance_set_wday(ucal, nth, wday);
1169                                         break;
1170                                 }
1171                         }
1172                         break;
1173                 }
1174         }
1175         g_strfreev(t);
1176
1177         return CALENDAR_ERROR_NONE;
1178 }
1179
1180 static int __cal_db_instance_publish_with_mday(UCalendar *ucal, cal_event_s *event, int duration, int field, calendar_time_s *until)
1181 {
1182         int i;
1183         int count = 0;
1184         int length = 0;
1185         int get_mday;
1186         char **t = NULL;
1187         const char *d = ",";
1188         UDate ud1, ud2;
1189         UErrorCode ec = U_ZERO_ERROR;
1190
1191         if (event->bymonthday && strlen(event->bymonthday) > 0)
1192         {
1193                 // range: 1, 2, ... , 31 ex> 1, 5, 7
1194                 t = g_strsplit(event->bymonthday, d, -1);
1195                 if (!t) {
1196                         ERR("g_strsplit failed");
1197                         g_strfreev(t);
1198                         return CALENDAR_ERROR_OUT_OF_MEMORY;
1199                 }
1200                 length = g_strv_length(t);
1201                 DBG("has bymonthday");
1202
1203         }
1204         else
1205         {
1206                 int mday = 0;
1207                 char buf[8] = {0};
1208                 switch (event->start.type)
1209                 {
1210                 case CALENDAR_TIME_UTIME:
1211                         _cal_time_ltoi(event->start_tzid, event->start.time.utime, NULL, NULL, &mday);
1212                         snprintf(buf, sizeof(buf), "%d", mday);
1213                         break;
1214
1215                 case CALENDAR_TIME_LOCALTIME:
1216                         snprintf(buf, sizeof(buf), "%d", event->start.time.date.mday);
1217                         break;
1218                 }
1219                 t = g_strsplit(buf, d, -1);
1220                 if (!t) {
1221                         ERR("g_strsplit failed");
1222                         g_strfreev(t);
1223                         return CALENDAR_ERROR_OUT_OF_MEMORY;
1224                 }
1225                 DBG("No bymonthday, so set mday(%s)", t[0]);
1226
1227                 length = 1;
1228         }
1229
1230         if (event->count)
1231         {
1232                 count = event->count;
1233
1234                 if (event->bysetpos)
1235                 {
1236                         int share, remind;
1237                         int length2;
1238                         char **t2 = NULL;
1239
1240                         t2 = g_strsplit(event->bysetpos, ",", -1);
1241                         length2 = g_strv_length(t2);
1242                         g_strfreev(t2);
1243
1244                         share = length / length2;
1245                         remind = length % length2;
1246                         count = count * share + (remind ? length : 0);
1247                         DBG("share(%d) remind(%d)", share, remind);
1248                 }
1249                 else
1250                 {
1251                         count = count / length + ((count % length == 0) ? 0 : 1);
1252                 }
1253                 DBG("count(%d)", count);
1254         }
1255
1256         ud1 = ucal_getMillis(ucal, &ec);
1257
1258         for (i = 0; i < length; i++)
1259         {
1260                 int mday = atoi(t[i]);
1261                 DBG("mday is set(%d)", mday);
1262                 mday = mday < 1 ? mday +1 : mday; // In ICU, last day is 0 not -1.
1263
1264                 ucal_setMillis(ucal, ud1, &ec);
1265                 ucal_set(ucal, UCAL_DATE, mday);
1266
1267                 // check whether set ucal goes past or not
1268                 ud1 = ucal_getMillis(ucal, &ec);
1269
1270                 UCalendar *ucal2 = NULL;
1271                 ucal2 = ucal_clone(ucal, &ec);
1272                 __cal_db_instance_set_ucal_start(ucal2, &event->start);
1273                 ud2 = ucal_getMillis(ucal2, &ec);
1274                 ucal_close(ucal2);
1275
1276                 if (ms2sec(ud2) > ms2sec(ud1))
1277                 {
1278                         DBG("Invalid first time(%lld) > (%lld), so added interval",
1279                                         ms2sec(ud2), ms2sec(ud1));
1280                         ucal_add(ucal, field, event->interval, &ec);
1281                 }
1282
1283                 int j = 0, k = 0;
1284                 DBG("range type(%d)", event->range_type);
1285                 switch (event->range_type)
1286                 {
1287                 case CALENDAR_RANGE_COUNT:
1288                         if (count == 1)
1289                         {
1290                                 __cal_db_instance_insert_record(ucal, duration, event->start.type, event->index);
1291                                 break;
1292                         }
1293
1294                         for (j = 0; j < count; j++)
1295                         {
1296                                 __cal_db_instance_insert_record(ucal, duration, event->start.type, event->index);
1297                                 ucal_add(ucal, field, event->interval, &ec);
1298                                 if (event->freq != CALENDAR_RECURRENCE_DAILY)
1299                                 {
1300                                         if (mday > 0)
1301                                         {
1302                                                 while ((get_mday = ucal_get(ucal, UCAL_DATE, &ec)) != mday)
1303                                                 {
1304                                                         k++;
1305                                                         DBG("Skip this date, we got mday(%d) no match with mday(%d)", get_mday, mday);
1306                                                         ucal_setMillis(ucal, ud1, &ec);
1307                                                         ucal_set(ucal, UCAL_DATE, mday);
1308                                                         ucal_add(ucal, field, event->interval * (j + k), &ec);
1309                                                         if ((ucal_get(ucal, UCAL_YEAR, &ec) > CAL_ENDLESS_LIMIT_YEAR) || (k > 50))
1310                                                         {
1311                                                                 ERR("Out of range");
1312                                                                 j = count;
1313                                                                 break;
1314                                                         }
1315                                                 }
1316                                         }
1317                                         else
1318                                         {
1319                                                 ucal_add(ucal, field, event->interval, &ec);
1320                                                 ucal_set(ucal, UCAL_DATE, mday);
1321                                         }
1322                                 }
1323                         }
1324                         break;
1325
1326                 case CALENDAR_RANGE_UNTIL:
1327                 case CALENDAR_RANGE_NONE:
1328                         j = 0;
1329                         while (__cal_db_instance_has_next(ucal, until))
1330                         {
1331                                 __cal_db_instance_insert_record(ucal, duration, event->start.type, event->index);
1332                                 ucal_add(ucal, field, event->interval, &ec);
1333                                 if (event->freq != CALENDAR_RECURRENCE_DAILY)
1334                                 {
1335                                         if (mday > 0)
1336                                         {
1337                                                 while ((get_mday = ucal_get(ucal, UCAL_DATE, &ec)) != mday)
1338                                                 {
1339                                                         k++;
1340                                                         DBG("Skip this date, we got mday(%d) no match with mday(%d)", get_mday, mday);
1341                                                         ucal_setMillis(ucal, ud1, &ec);
1342                                                         ucal_set(ucal, UCAL_DATE, mday);
1343                                                         ucal_add(ucal, field, event->interval * (j + k), &ec);
1344                                                         if ((ucal_get(ucal, UCAL_YEAR, &ec) > CAL_ENDLESS_LIMIT_YEAR) || (k > 50))
1345                                                         {
1346                                                                 ERR("Out of range");
1347                                                                 j = count;
1348                                                                 break;
1349                                                         }
1350                                                 }
1351                                         }
1352                                         else
1353                                         {
1354                                                 ucal_add(ucal, field, event->interval, &ec);
1355                                                 ucal_set(ucal, UCAL_DATE, mday);
1356                                         }
1357                                 }
1358                                 j++;
1359                         }
1360                         break;
1361                 }
1362         }
1363         g_strfreev(t);
1364
1365         return CALENDAR_ERROR_NONE;
1366 }
1367
1368 static int __cal_db_instance_publish_with_weekno(UCalendar *ucal, cal_event_s *event, int duration, int field, calendar_time_s *until)
1369 {
1370         int i, j;
1371         int count = 0;
1372         int length = 0;
1373         char **t = NULL;
1374         const char *d = ",";
1375         UDate ud1, ud2;
1376         UErrorCode ec = U_ZERO_ERROR;
1377
1378         if (event->byweekno && strlen(event->byweekno) > 0)
1379         {
1380                 // range: -366, -1, 1, 366 ex> 3,200
1381                 t = g_strsplit(event->byweekno, d, -1);
1382                 if (!t) {
1383                         ERR("g_strsplit failed");
1384                         g_strfreev(t);
1385                         return CALENDAR_ERROR_OUT_OF_MEMORY;
1386                 }
1387                 length = g_strv_length(t);
1388                 DBG("has bymonthday");
1389
1390         }
1391
1392         if (event->count)
1393         {
1394                 count = event->count;
1395
1396                 if (event->bysetpos)
1397                 {
1398                         int share, remind;
1399                         int length2;
1400                         char **t2 = NULL;
1401
1402                         t2 = g_strsplit(event->bysetpos, ",", -1);
1403                         length2 = g_strv_length(t2);
1404                         g_strfreev(t2);
1405
1406                         share = length / length2;
1407                         remind = length % length2;
1408                         count = count * share + (remind ? length : 0);
1409                         DBG("share(%d) remind(%d)", share, remind);
1410                 }
1411                 else
1412                 {
1413                         count = count / length + ((count % length == 0) ? 0 : 1);
1414                         DBG("length(%d)", length);
1415                 }
1416                 DBG("bycount(%d) count(%d)", event->count, count);
1417         }
1418
1419         int wday;
1420         wday = ucal_get(ucal, UCAL_DAY_OF_WEEK, &ec);
1421         ud1 = ucal_getMillis(ucal, &ec);
1422
1423         for (i = 0; i < length; i++)
1424         {
1425                 int weekno = atoi(t[i]);
1426
1427                 ucal_setMillis(ucal, ud1, &ec);
1428                 ucal_set(ucal, UCAL_WEEK_OF_YEAR, weekno);
1429                 DBG("weekno is set(%d)", weekno);
1430
1431                 // check whether set ucal goes past or not
1432                 ud1 = ucal_getMillis(ucal, &ec);
1433
1434                 UCalendar *ucal2 = NULL;
1435                 ucal2 = ucal_clone(ucal, &ec);
1436                 __cal_db_instance_set_ucal_start(ucal2, &event->start);
1437                 ud2 = ucal_getMillis(ucal2, &ec);
1438                 ucal_close(ucal2);
1439
1440                 if (ms2sec(ud2) > ms2sec(ud1))
1441                 {
1442                         DBG("Invalid first time: start(%lld) get(%lld)", ms2sec(ud2), ms2sec(ud1));
1443                         ucal_add(ucal, field, 1, &ec);
1444                 }
1445
1446                 DBG("range type(%d) interval(%d)", event->range_type, event->interval);
1447                 switch (event->range_type)
1448                 {
1449                 case CALENDAR_RANGE_COUNT:
1450                         for (j = 0; j < count; j++)
1451                         {
1452                                 __cal_db_instance_insert_record(ucal, duration, event->start.type, event->index);
1453                                 ucal_add(ucal, field, event->interval, &ec);
1454                         }
1455                         break;
1456
1457                 case CALENDAR_RANGE_UNTIL:
1458                 case CALENDAR_RANGE_NONE:
1459                         while (__cal_db_instance_has_next(ucal, until))
1460                         {
1461                                 __cal_db_instance_insert_record(ucal, duration, event->start.type, event->index);
1462                                 ucal_add(ucal, field, event->interval, &ec);
1463                         }
1464                         break;
1465                 }
1466         }
1467         g_strfreev(t);
1468
1469         return CALENDAR_ERROR_NONE;
1470 }
1471
1472
1473 static int __cal_db_instance_publish_with_yday(UCalendar *ucal, cal_event_s *event, int duration, int field, calendar_time_s *until)
1474 {
1475         int i, j;
1476         int count = 0;
1477         int length = 0;
1478         char **t = NULL;
1479         const char *d = ",";
1480         UDate ud1, ud2;
1481         UErrorCode ec = U_ZERO_ERROR;
1482
1483         if (event->byyearday && strlen(event->byyearday) > 0)
1484         {
1485                 // range: -366, -1, 1, 366 ex> 3,200
1486                 t = g_strsplit(event->byyearday, d, -1);
1487                 if (!t) {
1488                         ERR("g_strsplit failed");
1489                         g_strfreev(t);
1490                         return CALENDAR_ERROR_OUT_OF_MEMORY;
1491                 }
1492                 length = g_strv_length(t);
1493                 DBG("has bymonthday");
1494
1495         }
1496
1497         if (event->count)
1498         {
1499                 count = event->count;
1500
1501                 if (event->bysetpos)
1502                 {
1503                         int share, remind;
1504                         int length2;
1505                         char **t2 = NULL;
1506
1507                         t2 = g_strsplit(event->bysetpos, ",", -1);
1508                         length2 = g_strv_length(t2);
1509                         g_strfreev(t2);
1510
1511                         share = length / length2;
1512                         remind = length % length2;
1513                         count = count * share + (remind ? length : 0);
1514                         DBG("share(%d) remind(%d)", share, remind);
1515                 }
1516                 else
1517                 {
1518                         count = count / length + ((count % length == 0) ? 0 : 1);
1519                         DBG("length(%d)", length);
1520                 }
1521                 DBG("bycount(%d) count(%d)", event->count, count);
1522         }
1523
1524         ud1 = ucal_getMillis(ucal, &ec);
1525
1526         for (i = 0; i < length; i++)
1527         {
1528                 int yday = atoi(t[i]);
1529
1530                 ucal_setMillis(ucal, ud1, &ec);
1531                 ucal_set(ucal, UCAL_DAY_OF_YEAR, yday);
1532                 DBG("yday is set(%d)", yday);
1533
1534                 // check whether set ucal goes past or not
1535                 ud1 = ucal_getMillis(ucal, &ec);
1536
1537                 UCalendar *ucal2 = NULL;
1538                 ucal2 = ucal_clone(ucal, &ec);
1539                 __cal_db_instance_set_ucal_start(ucal2, &event->start);
1540                 ud2 = ucal_getMillis(ucal2, &ec);
1541                 ucal_close(ucal2);
1542
1543                 if (ms2sec(ud2) > ms2sec(ud1))
1544                 {
1545                         DBG("Invalid first time: start(%lld) get(%lld)", ms2sec(ud2), ms2sec(ud1));
1546                         ucal_add(ucal, field, 1, &ec);
1547                 }
1548
1549                 DBG("range type(%d) interval(%d)", event->range_type, event->interval);
1550                 switch (event->range_type)
1551                 {
1552                 case CALENDAR_RANGE_COUNT:
1553                         for (j = 0; j < count; j++)
1554                         {
1555                                 __cal_db_instance_insert_record(ucal, duration, event->start.type, event->index);
1556                                 ucal_add(ucal, field, event->interval, &ec);
1557                         }
1558                         break;
1559
1560                 case CALENDAR_RANGE_UNTIL:
1561                 case CALENDAR_RANGE_NONE:
1562                         while (__cal_db_instance_has_next(ucal, until))
1563                         {
1564                                 __cal_db_instance_insert_record(ucal, duration, event->start.type, event->index);
1565                                 ucal_add(ucal, field, event->interval, &ec);
1566                         }
1567                         break;
1568                 }
1569         }
1570         g_strfreev(t);
1571
1572         return CALENDAR_ERROR_NONE;
1573 }
1574
1575 static int __cal_db_instance_publish_record_yearly(UCalendar *ucal, cal_event_s *event, int duration, calendar_time_s *until)
1576 {
1577         int i;
1578         char **t = NULL;
1579         const char *d = ",";
1580
1581         if (NULL == ucal)
1582         {
1583                 ERR("Invalid parameter: ucal is NULL");
1584                 return CALENDAR_ERROR_INVALID_PARAMETER;
1585         }
1586         if (NULL == event)
1587         {
1588                 ERR("Invalid parameter: event is NULL");
1589                 return CALENDAR_ERROR_INVALID_PARAMETER;
1590         }
1591
1592         __cal_db_instance_set_ucal_start(ucal, &event->start);
1593         __cal_db_instance_print_ucal(ucal);
1594
1595         if (event->bymonth && strlen(event->bymonth) > 0)
1596         {
1597                 // range: 1 ~ 12 ex> 1, 2, 4
1598                 t = g_strsplit(event->bymonth, d, -1);
1599                 if (!t) {
1600                         ERR("g_strsplit failed");
1601                         return CALENDAR_ERROR_OUT_OF_MEMORY;
1602                 }
1603
1604                 for (i = 0; t[i]; i++)
1605                 {
1606                         int month = atoi(t[i]);
1607                         ucal_set(ucal, UCAL_MONTH, month -1);
1608
1609                         if (event->byday && strlen(event->byday) > 0)
1610                         {
1611                                 __cal_db_instance_publish_with_wday(ucal, event, duration, UCAL_YEAR, until);
1612                         }
1613                         else if (event->bymonthday && strlen(event->bymonthday) > 0)
1614                         {
1615                                 __cal_db_instance_publish_with_mday(ucal, event, duration, UCAL_YEAR, until);
1616                         }
1617                         else
1618                         {
1619                                 ERR("Not completed");
1620                         }
1621                 }
1622                 g_strfreev(t);
1623         }
1624         else if (event->byyearday && strlen(event->byyearday) > 0)
1625         {
1626                 __cal_db_instance_publish_with_yday(ucal, event, duration, UCAL_YEAR, until);
1627
1628         }
1629         else if (event->byweekno && strlen(event->byweekno) > 0)
1630         {
1631                 __cal_db_instance_publish_with_weekno(ucal, event, duration, UCAL_YEAR, until);
1632
1633                 // range: 1 ~ 53 or -53 ~ -1 ex> 1, 4
1634                 t = g_strsplit(event->byweekno, d, -1);
1635                 if (!t) {
1636                         ERR("g_strsplit failed");
1637                         return CALENDAR_ERROR_OUT_OF_MEMORY;
1638                 }
1639
1640                 for (i = 0; t[i]; i++)
1641                 {
1642
1643                 }
1644
1645                 g_strfreev(t);
1646         }
1647         else
1648         {
1649                 UErrorCode ec = U_ZERO_ERROR;
1650                 int month = ucal_get(ucal, UCAL_MONTH, &ec) + 1;
1651                 DBG("No bymonth, so set start time month(%d)", month);
1652
1653                 if (event->byday && strlen(event->byday) > 0)
1654                 {
1655                         __cal_db_instance_publish_with_wday(ucal, event, duration, UCAL_YEAR, until);
1656                 }
1657                 else if (event->bymonthday && strlen(event->bymonthday) > 0)
1658                 {
1659                         __cal_db_instance_publish_with_mday(ucal, event, duration, UCAL_YEAR, until);
1660                 }
1661                 else
1662                 {
1663                         ERR("Not completed");
1664                 }
1665         }
1666
1667         return CALENDAR_ERROR_NONE;
1668 }
1669
1670 static int __cal_db_instance_publish_record_monthly(UCalendar *ucal, cal_event_s *event, int duration, calendar_time_s *until)
1671 {
1672         if (NULL == ucal)
1673         {
1674                 ERR("Invalid parameter: ucal is NULL");
1675                 return CALENDAR_ERROR_INVALID_PARAMETER;
1676         }
1677         if (NULL == event)
1678         {
1679                 ERR("Invalid parameter: event is NULL");
1680                 return CALENDAR_ERROR_INVALID_PARAMETER;
1681         }
1682
1683         __cal_db_instance_set_ucal_start(ucal, &event->start);
1684         __cal_db_instance_print_ucal(ucal);
1685
1686         if (event->byday && strlen(event->byday) > 0)
1687         {
1688                 __cal_db_instance_publish_with_wday(ucal, event, duration, UCAL_MONTH, until);
1689         }
1690         else
1691         {
1692                 // bymonthday or none
1693                 __cal_db_instance_publish_with_mday(ucal, event, duration, UCAL_MONTH, until);
1694         }
1695
1696         return CALENDAR_ERROR_NONE;
1697 }
1698
1699 static int __cal_db_instance_publish_record_weekly(UCalendar *ucal, cal_event_s *event, int duration, calendar_time_s *until)
1700 {
1701         if (NULL == ucal)
1702         {
1703                 ERR("Invalid parameter: ucal is NULL");
1704                 return CALENDAR_ERROR_INVALID_PARAMETER;
1705         }
1706         if (NULL == event)
1707         {
1708                 ERR("Invalid parameter: event is NULL");
1709                 return CALENDAR_ERROR_INVALID_PARAMETER;
1710         }
1711
1712         __cal_db_instance_set_ucal_start(ucal, &event->start);
1713         __cal_db_instance_publish_with_wday(ucal, event, duration, UCAL_WEEK_OF_YEAR, until);
1714
1715         return CALENDAR_ERROR_NONE;
1716 }
1717
1718 static int __cal_db_instance_publish_record_daily(UCalendar *ucal, cal_event_s *event, int duration, calendar_time_s *until)
1719 {
1720         if (NULL == ucal)
1721         {
1722                 ERR("Invalid parameter: ucal is NULL");
1723                 return CALENDAR_ERROR_INVALID_PARAMETER;
1724         }
1725         if (NULL == event)
1726         {
1727                 ERR("Invalid parameter: event is NULL");
1728                 return CALENDAR_ERROR_INVALID_PARAMETER;
1729         }
1730
1731         __cal_db_instance_set_ucal_start(ucal, &event->start);
1732         __cal_db_instance_print_ucal(ucal);
1733
1734         __cal_db_instance_publish_with_mday(ucal, event, duration, UCAL_DATE, until);
1735
1736         return CALENDAR_ERROR_NONE;
1737 }
1738
1739 static int __cal_db_instance_publish_record_once(UCalendar *ucal, cal_event_s *event, int duration, calendar_time_s *until)
1740 {
1741         if (NULL == ucal)
1742         {
1743                 ERR("Invalid parameter: ucal is NULL");
1744                 return CALENDAR_ERROR_INVALID_PARAMETER;
1745         }
1746         if (NULL == event)
1747         {
1748                 ERR("Invalid parameter: event is NULL");
1749                 return CALENDAR_ERROR_INVALID_PARAMETER;
1750         }
1751
1752         __cal_db_instance_set_ucal_start(ucal, &event->start);
1753         __cal_db_instance_print_ucal(ucal);
1754
1755         event->interval = 1;
1756         event->count = 1;
1757         event->range_type = CALENDAR_RANGE_COUNT;
1758
1759         // start:for exception record which has original_event_id, mod freq 0
1760
1761         __cal_db_instance_publish_with_mday(ucal, event, duration, UCAL_DATE, until);
1762         return CALENDAR_ERROR_NONE;
1763 }
1764
1765 static int __cal_db_instance_publish_record_details(UCalendar *ucal, cal_event_s *event)
1766 {
1767         if (NULL == ucal)
1768         {
1769                 ERR("Invalid parameter: ucal is NULL");
1770                 return CALENDAR_ERROR_INVALID_PARAMETER;
1771         }
1772         if (NULL == event)
1773         {
1774                 ERR("Invalid parameter: event is NULL");
1775                 return CALENDAR_ERROR_INVALID_PARAMETER;
1776         }
1777
1778         int duration = -1;
1779         int exception_freq; // for exception
1780         long long int range = 0;
1781         calendar_time_s until = {0};
1782
1783         __cal_db_instance_get_duration(ucal, &event->start, &event->end, &duration);
1784
1785         if (event->original_event_id > 0)
1786         {
1787                 DBG("this is exception event so publish only one instance");
1788                 exception_freq = event->freq;
1789                 event->freq = CALENDAR_RECURRENCE_NONE;
1790         }
1791
1792
1793         switch (event->range_type)
1794         {
1795         case CALENDAR_RANGE_COUNT:
1796                 DBG("range count");
1797                 break;
1798
1799         case CALENDAR_RANGE_UNTIL:
1800                 DBG("range until");
1801                 until.type = event->until_type;
1802                 switch (until.type)
1803                 {
1804                 case CALENDAR_TIME_UTIME:
1805                         range = _cal_time_convert_itol(NULL, CAL_ENDLESS_LIMIT_YEAR,
1806                                         CAL_ENDLESS_LIMIT_MONTH, CAL_ENDLESS_LIMIT_MDAY,
1807                                         0, 0, 0);
1808                         if (event->until_utime > range)
1809                         {
1810                                 DBG("until time(%lld) > max, so set max(%lld)", event->until_utime, range);
1811                                 until.time.utime = range;
1812                         }
1813                         else
1814                         {
1815                                 until.time.utime = event->until_utime;
1816                         }
1817                         break;
1818
1819                 case CALENDAR_TIME_LOCALTIME:
1820                         until.time.date.year = event->until_year;
1821                         until.time.date.month = event->until_month;
1822                         until.time.date.mday = event->until_mday;
1823                         break;
1824                 }
1825                 break;
1826
1827         case CALENDAR_RANGE_NONE:
1828                 DBG("range none");
1829                 until.type = event->until_type;
1830                 switch (until.type)
1831                 {
1832                 case CALENDAR_TIME_UTIME:
1833                         until.time.utime = _cal_time_convert_itol(event->start_tzid,
1834                                         CAL_ENDLESS_LIMIT_YEAR,
1835                                         CAL_ENDLESS_LIMIT_MONTH,
1836                                         CAL_ENDLESS_LIMIT_MDAY,
1837                                         0, 0, 0);
1838                         break;
1839                 case CALENDAR_TIME_LOCALTIME:
1840                         until.time.date.year = CAL_ENDLESS_LIMIT_YEAR;
1841                         until.time.date.month = CAL_ENDLESS_LIMIT_MONTH;
1842                         until.time.date.mday = CAL_ENDLESS_LIMIT_MDAY;
1843                         break;
1844                 }
1845                 break;
1846         }
1847
1848         switch (event->freq)
1849         {
1850         case CALENDAR_RECURRENCE_YEARLY:
1851                 __cal_db_instance_publish_record_yearly(ucal, event, duration, &until);
1852                 break;
1853
1854         case CALENDAR_RECURRENCE_MONTHLY:
1855                 __cal_db_instance_publish_record_monthly(ucal, event, duration, &until);
1856                 break;
1857
1858         case CALENDAR_RECURRENCE_WEEKLY:
1859                 __cal_db_instance_publish_record_weekly(ucal, event, duration, &until);
1860                 break;
1861
1862         case CALENDAR_RECURRENCE_DAILY:
1863                 __cal_db_instance_publish_record_daily(ucal, event, duration, &until);
1864                 break;
1865
1866         case CALENDAR_RECURRENCE_NONE:
1867         default:
1868                 __cal_db_instance_publish_record_once(ucal, event, duration, &until);
1869                 break;
1870
1871         }
1872
1873         if (event->bysetpos)
1874         {
1875                 __cal_db_instance_apply_setpos(event->index, &event->start, event, event->freq);
1876         }
1877
1878         if (event->original_event_id > 0)
1879         {
1880                 DBG("return freq for exception event");
1881                 event->freq = exception_freq;
1882         }
1883
1884         return CALENDAR_ERROR_NONE;
1885 }
1886
1887 int _cal_db_instance_publish_record(calendar_record_h record)
1888 {
1889         cal_event_s *event;
1890
1891         if (NULL == record)
1892         {
1893                 ERR("Invalid argument: record is NULL");
1894                 return CALENDAR_ERROR_INVALID_PARAMETER;
1895         }
1896
1897         event = (cal_event_s *)(record);
1898
1899         DBG("Start");
1900         __cal_db_instance_print_caltime(&event->start);
1901         DBG("End");
1902         __cal_db_instance_print_caltime(&event->end);
1903
1904
1905         UCalendar *ucal = NULL;
1906         switch (event->start.type)
1907         {
1908         case CALENDAR_TIME_UTIME:
1909                 ucal = _cal_time_get_ucal(event->start_tzid, event->wkst);
1910                 break;
1911
1912         case CALENDAR_TIME_LOCALTIME:
1913                 ucal = _cal_time_get_ucal(NULL, event->wkst);
1914                 break;
1915         }
1916
1917         __cal_db_instance_publish_record_details(ucal, event);
1918         __cal_db_instance_del_inundant(event->index, &event->start, event);
1919         __cal_db_instance_update_exdate_del(event->index, event->exdate);
1920         __cal_db_instance_update_exdate_mod(event->original_event_id, event->recurrence_id);
1921
1922         ucal_close(ucal);
1923
1924         return CALENDAR_ERROR_NONE;
1925 }
1926
1927 int _cal_db_instance_get_now(long long int *current)
1928 {
1929         *current = ms2sec(ucal_getNow());
1930         return CALENDAR_ERROR_NONE;
1931 }
1932
1933 int _cal_db_instance_discard_record(calendar_record_h record)
1934 {
1935         char query[CAL_DB_SQL_MAX_LEN] = {0};
1936     cal_db_util_error_e dbret = CAL_DB_OK;
1937         cal_event_s *event;
1938
1939         event = (cal_event_s *)(record);
1940         if (event == NULL) {
1941                 ERR("Invalid argument: cal_event_s is NULL");
1942                 return CALENDAR_ERROR_INVALID_PARAMETER;
1943         }
1944
1945         DBG("delete normal");
1946         snprintf(query, sizeof(query), "DELETE FROM %s WHERE event_id = %d ",
1947                         CAL_TABLE_NORMAL_INSTANCE, event->index);
1948
1949         dbret = _cal_db_util_query_exec(query);
1950         if (CAL_DB_OK != dbret)
1951         {
1952                 DBG("query[%s]", query);
1953                 ERR("_cal_db_util_query_exec() failed (%d)", dbret);
1954                 switch (dbret)
1955                 {
1956                 case CAL_DB_ERROR_NO_SPACE:
1957                         return CALENDAR_ERROR_FILE_NO_SPACE;
1958                 default:
1959                         return CALENDAR_ERROR_DB_FAILED;
1960                 }
1961         }
1962
1963         DBG("delete allday");
1964         snprintf(query, sizeof(query), "DELETE FROM %s WHERE event_id = %d ",
1965                         CAL_TABLE_ALLDAY_INSTANCE, event->index);
1966
1967         dbret = _cal_db_util_query_exec(query);
1968         if (CAL_DB_OK != dbret)
1969         {
1970                 ERR("_cal_db_util_query_exec() failed (%d)", dbret);
1971                 switch (dbret)
1972                 {
1973                 case CAL_DB_ERROR_NO_SPACE:
1974                         return CALENDAR_ERROR_FILE_NO_SPACE;
1975                 default:
1976                         return CALENDAR_ERROR_DB_FAILED;
1977                 }
1978         }
1979
1980         return CALENDAR_ERROR_NONE;
1981 }
1982