4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
29 #include "calendar_db.h"
30 #include "calendar_types2.h"
32 #include "cal_internal.h"
33 #include "cal_typedef.h"
36 #include "cal_record.h"
38 #include "cal_db_util.h"
41 #define ms2sec(ms) (long long int)(ms / 1000.0)
42 #define sec2ms(s) (s * 1000.0)
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
49 UCAL_DAY_OF_WEEK + UCAL_WEEK_OF_YEAR
57 #define CAL_ENDLESS_LIMIT_YEAR 2038
58 #define CAL_ENDLESS_LIMIT_MONTH 12
59 #define CAL_ENDLESS_LIMIT_MDAY 31
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"},
71 static void __cal_db_instance_print_ucal(UCalendar *ucal)
74 UErrorCode ec = U_ZERO_ERROR;
78 lli = ms2sec(ucal_getMillis(ucal, &ec));
83 static int __cal_db_instance_work_normal_setpos(int event_id, long long int lli_s, long long int lli_e, char **t)
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;
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 "
102 CAL_TABLE_NORMAL_INSTANCE,
106 ret = _cal_db_util_query_get_first_int_result(query, NULL, &count);
107 if (CALENDAR_ERROR_NONE != ret)
109 ERR("_cal_db_util_query_get_first_int_result() failed");
122 DBG("append result");
124 DBG("[%lld] ~ [%lld] setpos id(%d)", lli_s, lli_e, ids[j]);
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);
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 "
140 CAL_TABLE_NORMAL_INSTANCE,
144 dbret = _cal_db_util_query_exec(query);
145 if (CAL_DB_OK != dbret)
147 DBG("query[%s]", query);
148 ERR("_cal_db_util_query_exec() Failed(%d)", dbret);
151 case CAL_DB_ERROR_NO_SPACE:
152 return CALENDAR_ERROR_FILE_NO_SPACE;
154 return CALENDAR_ERROR_DB_FAILED;
158 return CALENDAR_ERROR_NONE;
161 static int __cal_db_instance_work_allday_setpos(int event_id, char *str_s, char *str_e, char **t)
165 cal_db_util_error_e dbret = CAL_DB_OK;
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};
173 for (i = 0; t[i];i++)
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 "
180 CAL_TABLE_ALLDAY_INSTANCE,
184 ret = _cal_db_util_query_get_first_int_result(query, NULL, &count);
185 if (CALENDAR_ERROR_NONE != ret)
187 ERR("_cal_db_util_query_get_first_int_result() failed");
200 DBG("append result");
202 DBG("[%s] ~ [%s] setpos id(%d)", str_s, str_e, ids[j]);
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);
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 "
218 CAL_TABLE_ALLDAY_INSTANCE,
222 dbret = _cal_db_util_query_exec(query);
223 if (CAL_DB_OK != dbret)
225 DBG("query[%s]", query);
226 ERR("_cal_db_util_query_exec() Failed(%d)", dbret);
229 case CAL_DB_ERROR_NO_SPACE:
230 return CALENDAR_ERROR_FILE_NO_SPACE;
232 return CALENDAR_ERROR_DB_FAILED;
236 return CALENDAR_ERROR_NONE;
239 static int __cal_db_instance_apply_setpos(int event_id, calendar_time_s *st, cal_event_s *event, int freq)
246 long long int lli_s = 0, lli_e = 0;
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};
254 UErrorCode ec = U_ZERO_ERROR;
256 t = g_strsplit(event->bysetpos, dl, -1);
259 ERR("g_strsplit failed");
260 return CALENDAR_ERROR_OUT_OF_MEMORY;
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,
273 ret = _cal_db_util_query_get_first_int_result(query, NULL, &count);
274 if (CALENDAR_ERROR_NONE != ret)
276 ERR("_cal_db_util_query_get_first_int_result() failed");
280 DBG("count(%d)", count);
285 ucal = _cal_time_get_ucal(event->start_tzid, event->wkst);
286 ucal_setMillis(ucal, sec2ms(st->time.utime), &ec);
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));
295 ucal_add(ucal, UCAL_YEAR, 1, &ec);
296 lli_e = ms2sec(ucal_getMillis(ucal, &ec));
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));
304 ucal_add(ucal, UCAL_MONTH, 1, &ec);
305 lli_e = ms2sec(ucal_getMillis(ucal, &ec));
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));
313 ucal_add(ucal, UCAL_DATE, 7, &ec);
314 lli_e = ms2sec(ucal_getMillis(ucal, &ec));
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,
330 ret = _cal_db_util_query_get_first_int_result(query, NULL, &count);
331 if (CALENDAR_ERROR_NONE != ret)
333 ERR("_cal_db_util_query_get_first_int_result() failed");
337 DBG("setpos left count(%d) from [%lld]", count, lli_e);
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,
354 ret = _cal_db_util_query_get_first_int_result(query, NULL, &count);
355 if (CALENDAR_ERROR_NONE != ret)
357 ERR("_cal_db_util_query_get_first_int_result() failed");
361 DBG("count(%d)", count);
366 // _cal_db_instance_get_next_period(event, str_s, str_e, index);
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);
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,
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,
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",
398 DBG("start(%d/%d/%d", y1, m1, d1);
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",
406 DBG("end(%d/%d/%d", y1, m1, d1);
410 case CALENDAR_RECURRENCE_DAILY:
411 DBG("Not support setpos in daily");
418 __cal_db_instance_work_allday_setpos(event_id, str_s, str_e, t);
420 snprintf(query, sizeof(query), "SELECT count(*) FROM %s "
421 "WHERE event_id = %d "
422 "AND dtstart_datetime >= %s",
423 CAL_TABLE_ALLDAY_INSTANCE,
427 ret = _cal_db_util_query_get_first_int_result(query, NULL, &count);
428 if (CALENDAR_ERROR_NONE != ret)
430 ERR("_cal_db_util_query_get_first_int_result() failed");
434 DBG("setpos left count(%d) from [%s]", count, str_e);
443 return CALENDAR_ERROR_NONE;
446 static int __cal_db_instance_update_exdate_del(int id, char *exdate)
450 int y, mon, d, h, min, s;
451 char query[CAL_DB_SQL_MAX_LEN] = {0};
452 const char *dl = ",";
458 DBG("Nothing to update exdate del");
459 return CALENDAR_ERROR_NONE;
462 DBG("exdate [%s]", exdate);
463 t = g_strsplit(exdate, dl, -1);
466 ERR("g_strsplit failed");
467 return CALENDAR_ERROR_OUT_OF_MEMORY;
470 for (i = 0; t[i]; i++)
480 DBG("exdate[%s]", p);
482 if (strlen(p) > strlen("YYYYMMDD"))
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),
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));
496 DBG("ALLDAY instance");
497 snprintf(query, sizeof(query),
499 "WHERE event_id = %d AND dtstart_datetime = %s ",
500 CAL_TABLE_ALLDAY_INSTANCE,
504 dbret = _cal_db_util_query_exec(query);
505 if (dbret != CAL_DB_OK) {
506 ERR("_cal_db_util_query_exec() failed (%d)", dbret);
510 case CAL_DB_ERROR_NO_SPACE:
511 return CALENDAR_ERROR_FILE_NO_SPACE;
513 return CALENDAR_ERROR_DB_FAILED;
518 return CALENDAR_ERROR_NONE;
521 static int __cal_db_instance_update_exdate_mod(int original_event_id, char *recurrence_id)
525 int y, mon, d, h, min, s;
526 const char *dl = ",";
529 char query[CAL_DB_SQL_MAX_LEN] = {0};
531 if (original_event_id < 1 || recurrence_id == NULL)
533 DBG("Nothing to update exdate mod");
534 return CALENDAR_ERROR_NONE;
537 DBG("recurrence_id[%s]", recurrence_id);
538 t = g_strsplit(recurrence_id, dl, -1);
541 ERR("g_strsplit failed");
542 return CALENDAR_ERROR_OUT_OF_MEMORY;
545 for (i = 0; t[i]; i++)
555 DBG("%d[%s]", i + 1, p);
557 if (strlen(p) > strlen("YYYYMMDD"))
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),
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));
571 DBG("ALLDAY instance");
572 snprintf(query, sizeof(query),
574 "WHERE event_id = %d AND dtstart_datetime = %s ",
575 CAL_TABLE_ALLDAY_INSTANCE,
576 original_event_id, p);
579 dbret = _cal_db_util_query_exec(query);
580 if (dbret != CAL_DB_OK) {
581 ERR("_cal_db_util_query_exec() failed (%d)", dbret);
585 case CAL_DB_ERROR_NO_SPACE:
586 return CALENDAR_ERROR_FILE_NO_SPACE;
588 return CALENDAR_ERROR_DB_FAILED;
593 return CALENDAR_ERROR_NONE;
596 static inline int ___cal_db_instance_has_after(calendar_time_s *t1, calendar_time_s *t2)
598 if (t1->type == CALENDAR_TIME_UTIME) {
599 if (t1->time.utime > t2->time.utime)
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) {
610 } else if (t1->time.date.year < t2->time.date.year) {
613 if (t1->time.date.month > t2->time.date.month) {
615 } else if (t1->time.date.month < t2->time.date.month) {
618 if (t1->time.date.mday > t2->time.date.mday) {
627 static inline int __cal_db_instance_convert_mday(const char *str, int *mday)
632 ERR("Invalid argument: check mday[%s]", str);
633 return CALENDAR_ERROR_INVALID_PARAMETER;
637 if (d < 1 || d > 31) {
638 ERR("Invalid argument: check day(%d)", d);
639 return CALENDAR_ERROR_INVALID_PARAMETER;
642 DBG("get mday[%s] and convert to int(%d)", str, d);
644 return CALENDAR_ERROR_NONE;
647 static int __cal_db_instance_del_inundant(int event_id, calendar_time_s *st, cal_event_s *event)
649 cal_db_util_error_e dbret = CAL_DB_OK;
651 char query[CAL_DB_SQL_MAX_LEN];
653 if (event->range_type != CALENDAR_RANGE_COUNT) {
654 return CALENDAR_ERROR_NONE;
658 DBG("get count(%d) and del after this", cnt);
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,
667 CAL_TABLE_NORMAL_INSTANCE,
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,
677 CAL_TABLE_ALLDAY_INSTANCE,
681 dbret = _cal_db_util_query_exec(query);
682 if (dbret != CAL_DB_OK)
684 DBG("query(%s)", query);
687 case CAL_DB_ERROR_NO_SPACE:
688 return CALENDAR_ERROR_FILE_NO_SPACE;
690 return CALENDAR_ERROR_DB_FAILED;
693 return CALENDAR_ERROR_NONE;
696 static void __cal_db_instance_print_caltime(calendar_time_s *caltime)
699 switch (caltime->type) {
700 case CALENDAR_TIME_UTIME:
701 DBG("utime(%lld)", caltime->time.utime);
704 case CALENDAR_TIME_LOCALTIME:
705 DBG("datetime(%04d/%02d/%02d)",
706 caltime->time.date.year, caltime->time.date.month, caltime->time.date.mday);
712 static int __cal_db_instance_get_duration(UCalendar *ucal, calendar_time_s *st, calendar_time_s *et, int *duration)
715 UErrorCode ec = U_ZERO_ERROR;
720 ERR("Invalid parameter: ucal is NULL");
721 return CALENDAR_ERROR_INVALID_PARAMETER;
724 if (NULL == st || NULL == et)
726 ERR("Invalid parameter: calendar_time_s is NULL");
727 return CALENDAR_ERROR_INVALID_PARAMETER;
731 case CALENDAR_TIME_UTIME:
732 if (st->time.utime > et->time.utime)
734 ERR("check time: start(%lld) > end(%lld)", st->time.utime, et->time.utime);
735 return CALENDAR_ERROR_INVALID_PARAMETER;
737 _duration = (int)(et->time.utime - st->time.utime);
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);
744 ucal_setDateTime(ucal, st->time.date.year, st->time.date.month -1, st->time.date.mday, 0, 0, 0, &ec);
746 _duration = ucal_getFieldDifference(ucal, ud, UCAL_DATE, &ec);
749 ERR("ucal_getFieldDifference failed (%s)", u_errorName(ec));
755 if (duration) *duration = _duration;
756 return CALENDAR_ERROR_NONE;
759 static int __cal_db_instance_parse_byday(const char *byday, int *nth, int *wday)
765 if (NULL == byday || strlen(byday) == 0)
767 ERR("Invalid parameter: byday is NULL");
768 return CALENDAR_ERROR_INVALID_PARAMETER;
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);
779 if (sscanf(byday, "%d%s", &_nth, buf) != 2) {
780 ERR("Failed to get byday[%s]", byday);
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);
794 if (nth) *nth = _nth > 4 ? -1 : _nth;
795 if (wday) *wday = _wday;
797 return CALENDAR_ERROR_NONE;
800 static void __cal_db_instance_set_ucal_start(UCalendar *ucal, calendar_time_s *st)
802 UErrorCode ec = U_ZERO_ERROR;
808 case CALENDAR_TIME_UTIME:
809 ucal_setMillis(ucal, sec2ms(st->time.utime), &ec);
812 case CALENDAR_TIME_LOCALTIME:
813 ucal_setDate(ucal, st->time.date.year,
814 st->time.date.month -1, st->time.date.mday, &ec);
820 static void __cal_db_instance_set_wday(UCalendar *ucal, int nth, int wday)
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);
829 static int __cal_db_instance_insert_record(UCalendar *ucal, int duration, int type, int event_id)
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;
841 ERR("Invalid parameter: ucal is NULL");
842 return CALENDAR_ERROR_INVALID_PARAMETER;
847 case CALENDAR_TIME_UTIME:
848 lli = ms2sec(ucal_getMillis(ucal, &ec));
852 DBG("insert normal (%lld)%04d-%02d-%02d %02d:%02d:00",
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));
860 et.time.utime = lli + (long long int)duration;
862 ret = calendar_record_create(_calendar_instance_normal._uri, &record);
863 if (CALENDAR_ERROR_NONE != ret)
865 ERR("calendar_record_create() failed");
866 return CALENDAR_ERROR_DB_FAILED;
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)
872 ERR("_cal_record_set_caltime() failed");
875 ret = _cal_record_set_caltime(record, _calendar_instance_normal.end_time, et);
876 if (CALENDAR_ERROR_NONE != ret)
878 ERR("_cal_record_set_caltime() failed");
883 case CALENDAR_TIME_LOCALTIME:
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);
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);
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;
907 ret = calendar_record_create(_calendar_instance_allday._uri, &record);
908 if (CALENDAR_ERROR_NONE != ret)
910 ERR("calendar_record_create() failed");
913 ret = _cal_record_set_int(record, _calendar_instance_allday.event_id, event_id);
914 if (CALENDAR_ERROR_NONE != ret)
916 ERR("_cal_record_set_int() failed");
919 ret = _cal_record_set_caltime(record, _calendar_instance_allday.start_time, st);
920 if (CALENDAR_ERROR_NONE != ret)
922 ERR("_cal_record_set_caltime() failed");
925 ret = _cal_record_set_caltime(record, _calendar_instance_allday.end_time, et);
926 if (CALENDAR_ERROR_NONE != ret)
928 ERR("_cal_record_set_caltime() failed");
933 ret = calendar_db_insert_record(record, NULL);
934 if (CALENDAR_ERROR_NONE != ret)
936 ERR("calendar_db_insert_record() failed");
939 calendar_record_destroy(record, true);
941 return CALENDAR_ERROR_NONE;
944 static int __cal_db_instance_has_next(UCalendar *ucal, calendar_time_s *until)
947 long long int lli = 0;
948 long long int lli2 = 0;
949 UErrorCode ec = U_ZERO_ERROR;
950 UCalendar *ucal2 = NULL;
954 ERR("Invalid parameter: ucal is NULL");
955 return CALENDAR_ERROR_INVALID_PARAMETER;
959 ERR("Invalid parameter: until is NULL");
960 return CALENDAR_ERROR_INVALID_PARAMETER;
965 case CALENDAR_TIME_UTIME:
966 lli = ms2sec(ucal_getMillis(ucal, &ec));
967 has_next = (lli > until->time.utime) ? 0 : 1;
970 DBG("No next:get(%lld) until(%lld)", lli, until->time.utime);
974 case CALENDAR_TIME_LOCALTIME:
975 lli = ms2sec(ucal_getMillis(ucal, &ec));
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));
983 has_next = (lli > lli2) ? 0 : 1;
986 DBG("No next:get(%lld) until(%lld)", lli, lli2);
995 static int __cal_db_instance_publish_with_wday(UCalendar *ucal, cal_event_s *event, int duration, int field, calendar_time_s *until)
997 int ret = CALENDAR_ERROR_NONE;
1002 const char *d = ",";
1004 UErrorCode ec = U_ZERO_ERROR;
1006 if (event->byday && strlen(event->byday) > 0)
1008 // range: MO, TU, WE, TH, FR, SA, SU ex> 3TH, -1MO
1009 t = g_strsplit(event->byday, d, -1);
1011 ERR("g_strsplit failed");
1013 return CALENDAR_ERROR_OUT_OF_MEMORY;
1015 length = g_strv_length(t);
1019 int nth = 0, wday = 0;
1022 UCalendar *ucal2 = NULL;
1024 ucal2 = ucal_clone(ucal, &ec);
1026 switch (event->start.type)
1028 case CALENDAR_TIME_UTIME:
1029 ucal_setMillis(ucal, sec2ms(event->start.time.utime), &ec);
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);
1038 wday = ucal_get(ucal, UCAL_DAY_OF_WEEK, &ec);
1039 wday = wday < UCAL_SUNDAY ? UCAL_SUNDAY : wday;
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;
1048 snprintf(buf, sizeof(buf), "%d%s", nth, wdays[wday].str);
1049 t = g_strsplit(buf, d, -1);
1051 ERR("g_strsplit failed");
1053 return CALENDAR_ERROR_OUT_OF_MEMORY;
1055 DBG("No byday, so set wday(%s)", t[0]);
1059 DBG("length(%d)", length);
1063 count = event->count;
1065 if (event->bysetpos)
1071 t2 = g_strsplit(event->bysetpos, ",", -1);
1072 length2 = g_strv_length(t2);
1075 share = length / length2;
1076 remind = length % length2;
1077 count = count * share + (remind ? length : 0);
1081 count = count / length + ((count % length == 0) ? 0 : 1);
1083 DBG("bycount(%d) count(%d)", event->count, count);
1086 ud1 = ucal_getMillis(ucal, &ec);
1087 int week = ucal_get(ucal, field, &ec);
1089 for (i = 0; i < length; i++)
1092 int nth = 0, wday = 0;
1093 ret = __cal_db_instance_parse_byday(t[i], &nth, &wday);
1094 if (CALENDAR_ERROR_NONE != ret)
1096 ERR("__cal_db_instance_parse_byday() failed");
1100 ucal_setMillis(ucal, ud1, &ec);
1103 case UCAL_WEEK_OF_YEAR:
1104 ucal_set(ucal, field, week);
1105 ucal_set(ucal, UCAL_DAY_OF_WEEK, wday);
1108 __cal_db_instance_set_wday(ucal, nth, wday);
1111 DBG("set wday nth(%d) wday(%d)", nth, wday);
1113 // check whether set ucal goes past or not
1114 ud1 = ucal_getMillis(ucal, &ec);
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);
1122 if (ms2sec(ud2) > ms2sec(ud1))
1124 DBG("Invalid first time: start(%lld) get(%lld)", ms2sec(ud2), ms2sec(ud1));
1127 case UCAL_WEEK_OF_YEAR:
1128 ucal_add(ucal, field, event->interval, &ec);
1132 ucal_add(ucal, field, event->interval, &ec);
1133 __cal_db_instance_set_wday(ucal, nth, wday);
1138 DBG("range type(%d) interval(%d)", event->range_type, event->interval);
1139 switch (event->range_type)
1141 case CALENDAR_RANGE_COUNT:
1142 for (j = 0; j < count; j++)
1144 __cal_db_instance_insert_record(ucal, duration, event->start.type, event->index);
1145 ucal_add(ucal, field, event->interval, &ec);
1148 case UCAL_WEEK_OF_YEAR:
1151 __cal_db_instance_set_wday(ucal, nth, wday);
1157 case CALENDAR_RANGE_UNTIL:
1158 case CALENDAR_RANGE_NONE:
1159 while (__cal_db_instance_has_next(ucal, until))
1161 __cal_db_instance_insert_record(ucal, duration, event->start.type, event->index);
1162 ucal_add(ucal, field, event->interval, &ec);
1165 case UCAL_WEEK_OF_YEAR:
1168 __cal_db_instance_set_wday(ucal, nth, wday);
1177 return CALENDAR_ERROR_NONE;
1180 static int __cal_db_instance_publish_with_mday(UCalendar *ucal, cal_event_s *event, int duration, int field, calendar_time_s *until)
1187 const char *d = ",";
1189 UErrorCode ec = U_ZERO_ERROR;
1191 if (event->bymonthday && strlen(event->bymonthday) > 0)
1193 // range: 1, 2, ... , 31 ex> 1, 5, 7
1194 t = g_strsplit(event->bymonthday, d, -1);
1196 ERR("g_strsplit failed");
1198 return CALENDAR_ERROR_OUT_OF_MEMORY;
1200 length = g_strv_length(t);
1201 DBG("has bymonthday");
1208 switch (event->start.type)
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);
1215 case CALENDAR_TIME_LOCALTIME:
1216 snprintf(buf, sizeof(buf), "%d", event->start.time.date.mday);
1219 t = g_strsplit(buf, d, -1);
1221 ERR("g_strsplit failed");
1223 return CALENDAR_ERROR_OUT_OF_MEMORY;
1225 DBG("No bymonthday, so set mday(%s)", t[0]);
1232 count = event->count;
1234 if (event->bysetpos)
1240 t2 = g_strsplit(event->bysetpos, ",", -1);
1241 length2 = g_strv_length(t2);
1244 share = length / length2;
1245 remind = length % length2;
1246 count = count * share + (remind ? length : 0);
1247 DBG("share(%d) remind(%d)", share, remind);
1251 count = count / length + ((count % length == 0) ? 0 : 1);
1253 DBG("count(%d)", count);
1256 ud1 = ucal_getMillis(ucal, &ec);
1258 for (i = 0; i < length; i++)
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.
1264 ucal_setMillis(ucal, ud1, &ec);
1265 ucal_set(ucal, UCAL_DATE, mday);
1267 // check whether set ucal goes past or not
1268 ud1 = ucal_getMillis(ucal, &ec);
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);
1276 if (ms2sec(ud2) > ms2sec(ud1))
1278 DBG("Invalid first time(%lld) > (%lld), so added interval",
1279 ms2sec(ud2), ms2sec(ud1));
1280 ucal_add(ucal, field, event->interval, &ec);
1284 DBG("range type(%d)", event->range_type);
1285 switch (event->range_type)
1287 case CALENDAR_RANGE_COUNT:
1290 __cal_db_instance_insert_record(ucal, duration, event->start.type, event->index);
1294 for (j = 0; j < count; j++)
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)
1302 while ((get_mday = ucal_get(ucal, UCAL_DATE, &ec)) != mday)
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))
1311 ERR("Out of range");
1319 ucal_add(ucal, field, event->interval, &ec);
1320 ucal_set(ucal, UCAL_DATE, mday);
1326 case CALENDAR_RANGE_UNTIL:
1327 case CALENDAR_RANGE_NONE:
1329 while (__cal_db_instance_has_next(ucal, until))
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)
1337 while ((get_mday = ucal_get(ucal, UCAL_DATE, &ec)) != mday)
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))
1346 ERR("Out of range");
1354 ucal_add(ucal, field, event->interval, &ec);
1355 ucal_set(ucal, UCAL_DATE, mday);
1365 return CALENDAR_ERROR_NONE;
1368 static int __cal_db_instance_publish_with_weekno(UCalendar *ucal, cal_event_s *event, int duration, int field, calendar_time_s *until)
1374 const char *d = ",";
1376 UErrorCode ec = U_ZERO_ERROR;
1378 if (event->byweekno && strlen(event->byweekno) > 0)
1380 // range: -366, -1, 1, 366 ex> 3,200
1381 t = g_strsplit(event->byweekno, d, -1);
1383 ERR("g_strsplit failed");
1385 return CALENDAR_ERROR_OUT_OF_MEMORY;
1387 length = g_strv_length(t);
1388 DBG("has bymonthday");
1394 count = event->count;
1396 if (event->bysetpos)
1402 t2 = g_strsplit(event->bysetpos, ",", -1);
1403 length2 = g_strv_length(t2);
1406 share = length / length2;
1407 remind = length % length2;
1408 count = count * share + (remind ? length : 0);
1409 DBG("share(%d) remind(%d)", share, remind);
1413 count = count / length + ((count % length == 0) ? 0 : 1);
1414 DBG("length(%d)", length);
1416 DBG("bycount(%d) count(%d)", event->count, count);
1420 wday = ucal_get(ucal, UCAL_DAY_OF_WEEK, &ec);
1421 ud1 = ucal_getMillis(ucal, &ec);
1423 for (i = 0; i < length; i++)
1425 int weekno = atoi(t[i]);
1427 ucal_setMillis(ucal, ud1, &ec);
1428 ucal_set(ucal, UCAL_WEEK_OF_YEAR, weekno);
1429 DBG("weekno is set(%d)", weekno);
1431 // check whether set ucal goes past or not
1432 ud1 = ucal_getMillis(ucal, &ec);
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);
1440 if (ms2sec(ud2) > ms2sec(ud1))
1442 DBG("Invalid first time: start(%lld) get(%lld)", ms2sec(ud2), ms2sec(ud1));
1443 ucal_add(ucal, field, 1, &ec);
1446 DBG("range type(%d) interval(%d)", event->range_type, event->interval);
1447 switch (event->range_type)
1449 case CALENDAR_RANGE_COUNT:
1450 for (j = 0; j < count; j++)
1452 __cal_db_instance_insert_record(ucal, duration, event->start.type, event->index);
1453 ucal_add(ucal, field, event->interval, &ec);
1457 case CALENDAR_RANGE_UNTIL:
1458 case CALENDAR_RANGE_NONE:
1459 while (__cal_db_instance_has_next(ucal, until))
1461 __cal_db_instance_insert_record(ucal, duration, event->start.type, event->index);
1462 ucal_add(ucal, field, event->interval, &ec);
1469 return CALENDAR_ERROR_NONE;
1473 static int __cal_db_instance_publish_with_yday(UCalendar *ucal, cal_event_s *event, int duration, int field, calendar_time_s *until)
1479 const char *d = ",";
1481 UErrorCode ec = U_ZERO_ERROR;
1483 if (event->byyearday && strlen(event->byyearday) > 0)
1485 // range: -366, -1, 1, 366 ex> 3,200
1486 t = g_strsplit(event->byyearday, d, -1);
1488 ERR("g_strsplit failed");
1490 return CALENDAR_ERROR_OUT_OF_MEMORY;
1492 length = g_strv_length(t);
1493 DBG("has bymonthday");
1499 count = event->count;
1501 if (event->bysetpos)
1507 t2 = g_strsplit(event->bysetpos, ",", -1);
1508 length2 = g_strv_length(t2);
1511 share = length / length2;
1512 remind = length % length2;
1513 count = count * share + (remind ? length : 0);
1514 DBG("share(%d) remind(%d)", share, remind);
1518 count = count / length + ((count % length == 0) ? 0 : 1);
1519 DBG("length(%d)", length);
1521 DBG("bycount(%d) count(%d)", event->count, count);
1524 ud1 = ucal_getMillis(ucal, &ec);
1526 for (i = 0; i < length; i++)
1528 int yday = atoi(t[i]);
1530 ucal_setMillis(ucal, ud1, &ec);
1531 ucal_set(ucal, UCAL_DAY_OF_YEAR, yday);
1532 DBG("yday is set(%d)", yday);
1534 // check whether set ucal goes past or not
1535 ud1 = ucal_getMillis(ucal, &ec);
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);
1543 if (ms2sec(ud2) > ms2sec(ud1))
1545 DBG("Invalid first time: start(%lld) get(%lld)", ms2sec(ud2), ms2sec(ud1));
1546 ucal_add(ucal, field, 1, &ec);
1549 DBG("range type(%d) interval(%d)", event->range_type, event->interval);
1550 switch (event->range_type)
1552 case CALENDAR_RANGE_COUNT:
1553 for (j = 0; j < count; j++)
1555 __cal_db_instance_insert_record(ucal, duration, event->start.type, event->index);
1556 ucal_add(ucal, field, event->interval, &ec);
1560 case CALENDAR_RANGE_UNTIL:
1561 case CALENDAR_RANGE_NONE:
1562 while (__cal_db_instance_has_next(ucal, until))
1564 __cal_db_instance_insert_record(ucal, duration, event->start.type, event->index);
1565 ucal_add(ucal, field, event->interval, &ec);
1572 return CALENDAR_ERROR_NONE;
1575 static int __cal_db_instance_publish_record_yearly(UCalendar *ucal, cal_event_s *event, int duration, calendar_time_s *until)
1579 const char *d = ",";
1583 ERR("Invalid parameter: ucal is NULL");
1584 return CALENDAR_ERROR_INVALID_PARAMETER;
1588 ERR("Invalid parameter: event is NULL");
1589 return CALENDAR_ERROR_INVALID_PARAMETER;
1592 __cal_db_instance_set_ucal_start(ucal, &event->start);
1593 __cal_db_instance_print_ucal(ucal);
1595 if (event->bymonth && strlen(event->bymonth) > 0)
1597 // range: 1 ~ 12 ex> 1, 2, 4
1598 t = g_strsplit(event->bymonth, d, -1);
1600 ERR("g_strsplit failed");
1601 return CALENDAR_ERROR_OUT_OF_MEMORY;
1604 for (i = 0; t[i]; i++)
1606 int month = atoi(t[i]);
1607 ucal_set(ucal, UCAL_MONTH, month -1);
1609 if (event->byday && strlen(event->byday) > 0)
1611 __cal_db_instance_publish_with_wday(ucal, event, duration, UCAL_YEAR, until);
1613 else if (event->bymonthday && strlen(event->bymonthday) > 0)
1615 __cal_db_instance_publish_with_mday(ucal, event, duration, UCAL_YEAR, until);
1619 ERR("Not completed");
1624 else if (event->byyearday && strlen(event->byyearday) > 0)
1626 __cal_db_instance_publish_with_yday(ucal, event, duration, UCAL_YEAR, until);
1629 else if (event->byweekno && strlen(event->byweekno) > 0)
1631 __cal_db_instance_publish_with_weekno(ucal, event, duration, UCAL_YEAR, until);
1633 // range: 1 ~ 53 or -53 ~ -1 ex> 1, 4
1634 t = g_strsplit(event->byweekno, d, -1);
1636 ERR("g_strsplit failed");
1637 return CALENDAR_ERROR_OUT_OF_MEMORY;
1640 for (i = 0; t[i]; i++)
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);
1653 if (event->byday && strlen(event->byday) > 0)
1655 __cal_db_instance_publish_with_wday(ucal, event, duration, UCAL_YEAR, until);
1657 else if (event->bymonthday && strlen(event->bymonthday) > 0)
1659 __cal_db_instance_publish_with_mday(ucal, event, duration, UCAL_YEAR, until);
1663 ERR("Not completed");
1667 return CALENDAR_ERROR_NONE;
1670 static int __cal_db_instance_publish_record_monthly(UCalendar *ucal, cal_event_s *event, int duration, calendar_time_s *until)
1674 ERR("Invalid parameter: ucal is NULL");
1675 return CALENDAR_ERROR_INVALID_PARAMETER;
1679 ERR("Invalid parameter: event is NULL");
1680 return CALENDAR_ERROR_INVALID_PARAMETER;
1683 __cal_db_instance_set_ucal_start(ucal, &event->start);
1684 __cal_db_instance_print_ucal(ucal);
1686 if (event->byday && strlen(event->byday) > 0)
1688 __cal_db_instance_publish_with_wday(ucal, event, duration, UCAL_MONTH, until);
1692 // bymonthday or none
1693 __cal_db_instance_publish_with_mday(ucal, event, duration, UCAL_MONTH, until);
1696 return CALENDAR_ERROR_NONE;
1699 static int __cal_db_instance_publish_record_weekly(UCalendar *ucal, cal_event_s *event, int duration, calendar_time_s *until)
1703 ERR("Invalid parameter: ucal is NULL");
1704 return CALENDAR_ERROR_INVALID_PARAMETER;
1708 ERR("Invalid parameter: event is NULL");
1709 return CALENDAR_ERROR_INVALID_PARAMETER;
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);
1715 return CALENDAR_ERROR_NONE;
1718 static int __cal_db_instance_publish_record_daily(UCalendar *ucal, cal_event_s *event, int duration, calendar_time_s *until)
1722 ERR("Invalid parameter: ucal is NULL");
1723 return CALENDAR_ERROR_INVALID_PARAMETER;
1727 ERR("Invalid parameter: event is NULL");
1728 return CALENDAR_ERROR_INVALID_PARAMETER;
1731 __cal_db_instance_set_ucal_start(ucal, &event->start);
1732 __cal_db_instance_print_ucal(ucal);
1734 __cal_db_instance_publish_with_mday(ucal, event, duration, UCAL_DATE, until);
1736 return CALENDAR_ERROR_NONE;
1739 static int __cal_db_instance_publish_record_once(UCalendar *ucal, cal_event_s *event, int duration, calendar_time_s *until)
1743 ERR("Invalid parameter: ucal is NULL");
1744 return CALENDAR_ERROR_INVALID_PARAMETER;
1748 ERR("Invalid parameter: event is NULL");
1749 return CALENDAR_ERROR_INVALID_PARAMETER;
1752 __cal_db_instance_set_ucal_start(ucal, &event->start);
1753 __cal_db_instance_print_ucal(ucal);
1755 event->interval = 1;
1757 event->range_type = CALENDAR_RANGE_COUNT;
1759 // start:for exception record which has original_event_id, mod freq 0
1761 __cal_db_instance_publish_with_mday(ucal, event, duration, UCAL_DATE, until);
1762 return CALENDAR_ERROR_NONE;
1765 static int __cal_db_instance_publish_record_details(UCalendar *ucal, cal_event_s *event)
1769 ERR("Invalid parameter: ucal is NULL");
1770 return CALENDAR_ERROR_INVALID_PARAMETER;
1774 ERR("Invalid parameter: event is NULL");
1775 return CALENDAR_ERROR_INVALID_PARAMETER;
1779 int exception_freq; // for exception
1780 long long int range = 0;
1781 calendar_time_s until = {0};
1783 __cal_db_instance_get_duration(ucal, &event->start, &event->end, &duration);
1785 if (event->original_event_id > 0)
1787 DBG("this is exception event so publish only one instance");
1788 exception_freq = event->freq;
1789 event->freq = CALENDAR_RECURRENCE_NONE;
1793 switch (event->range_type)
1795 case CALENDAR_RANGE_COUNT:
1799 case CALENDAR_RANGE_UNTIL:
1801 until.type = event->until_type;
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,
1808 if (event->until_utime > range)
1810 DBG("until time(%lld) > max, so set max(%lld)", event->until_utime, range);
1811 until.time.utime = range;
1815 until.time.utime = event->until_utime;
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;
1827 case CALENDAR_RANGE_NONE:
1829 until.type = event->until_type;
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,
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;
1848 switch (event->freq)
1850 case CALENDAR_RECURRENCE_YEARLY:
1851 __cal_db_instance_publish_record_yearly(ucal, event, duration, &until);
1854 case CALENDAR_RECURRENCE_MONTHLY:
1855 __cal_db_instance_publish_record_monthly(ucal, event, duration, &until);
1858 case CALENDAR_RECURRENCE_WEEKLY:
1859 __cal_db_instance_publish_record_weekly(ucal, event, duration, &until);
1862 case CALENDAR_RECURRENCE_DAILY:
1863 __cal_db_instance_publish_record_daily(ucal, event, duration, &until);
1866 case CALENDAR_RECURRENCE_NONE:
1868 __cal_db_instance_publish_record_once(ucal, event, duration, &until);
1873 if (event->bysetpos)
1875 __cal_db_instance_apply_setpos(event->index, &event->start, event, event->freq);
1878 if (event->original_event_id > 0)
1880 DBG("return freq for exception event");
1881 event->freq = exception_freq;
1884 return CALENDAR_ERROR_NONE;
1887 int _cal_db_instance_publish_record(calendar_record_h record)
1893 ERR("Invalid argument: record is NULL");
1894 return CALENDAR_ERROR_INVALID_PARAMETER;
1897 event = (cal_event_s *)(record);
1900 __cal_db_instance_print_caltime(&event->start);
1902 __cal_db_instance_print_caltime(&event->end);
1905 UCalendar *ucal = NULL;
1906 switch (event->start.type)
1908 case CALENDAR_TIME_UTIME:
1909 ucal = _cal_time_get_ucal(event->start_tzid, event->wkst);
1912 case CALENDAR_TIME_LOCALTIME:
1913 ucal = _cal_time_get_ucal(NULL, event->wkst);
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);
1924 return CALENDAR_ERROR_NONE;
1927 int _cal_db_instance_get_now(long long int *current)
1929 *current = ms2sec(ucal_getNow());
1930 return CALENDAR_ERROR_NONE;
1933 int _cal_db_instance_discard_record(calendar_record_h record)
1935 char query[CAL_DB_SQL_MAX_LEN] = {0};
1936 cal_db_util_error_e dbret = CAL_DB_OK;
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;
1945 DBG("delete normal");
1946 snprintf(query, sizeof(query), "DELETE FROM %s WHERE event_id = %d ",
1947 CAL_TABLE_NORMAL_INSTANCE, event->index);
1949 dbret = _cal_db_util_query_exec(query);
1950 if (CAL_DB_OK != dbret)
1952 DBG("query[%s]", query);
1953 ERR("_cal_db_util_query_exec() failed (%d)", dbret);
1956 case CAL_DB_ERROR_NO_SPACE:
1957 return CALENDAR_ERROR_FILE_NO_SPACE;
1959 return CALENDAR_ERROR_DB_FAILED;
1963 DBG("delete allday");
1964 snprintf(query, sizeof(query), "DELETE FROM %s WHERE event_id = %d ",
1965 CAL_TABLE_ALLDAY_INSTANCE, event->index);
1967 dbret = _cal_db_util_query_exec(query);
1968 if (CAL_DB_OK != dbret)
1970 ERR("_cal_db_util_query_exec() failed (%d)", dbret);
1973 case CAL_DB_ERROR_NO_SPACE:
1974 return CALENDAR_ERROR_FILE_NO_SPACE;
1976 return CALENDAR_ERROR_DB_FAILED;
1980 return CALENDAR_ERROR_NONE;