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.
21 #include "cal_internal.h"
22 #include "cal_typedef.h"
23 #include "cal_db_util.h"
28 void _cal_db_rrule_set_default(calendar_record_h record)
30 cal_event_s *event = NULL;
31 retm_if(record == NULL, "Invalid argument: rrule is NULL");
33 event = (cal_event_s *)record;
37 case CALENDAR_RECURRENCE_NONE:
39 case CALENDAR_RECURRENCE_DAILY:
41 case CALENDAR_RECURRENCE_WEEKLY:
42 if (event->bymonthday || event->byday)
45 event->byday = _cal_time_extract_by(event->start_tzid, event->wkst,
46 &event->start, CAL_DAY_OF_WEEK);
47 DBG("Not enough field in weekly, so set byda[%s]", event->byday);
50 case CALENDAR_RECURRENCE_MONTHLY:
51 if (event->bymonthday || event->byday)
54 event->bymonthday = _cal_time_extract_by(event->start_tzid, event->wkst,
55 &event->start, CAL_DATE);
56 DBG("Not enough field in monthly, so set bymonthday[%s]", event->bymonthday);
59 case CALENDAR_RECURRENCE_YEARLY:
60 if (event->bymonth && (event->bymonthday || event->byday))
62 else if (event->byyearday || event->byweekno)
65 event->bymonth = _cal_time_extract_by(event->start_tzid, event->wkst,
66 &event->start, CAL_MONTH);
67 event->bymonthday = _cal_time_extract_by(event->start_tzid, event->wkst,
68 &event->start, CAL_DATE);
69 DBG("Not enough field in yearly, so set bymonth[%s] bymonthday[%s]",
70 event->bymonth, event->bymonthday);
78 void _cal_db_rrule_get_rrule_from_event(calendar_record_h event, cal_rrule_s **rrule)
83 retm_if(event == NULL, "Invalid argument: rrule is NULL");
84 _event = (cal_event_s *)event;
85 if (_event->freq == CALENDAR_RECURRENCE_NONE)
90 _rrule = calloc(1, sizeof(cal_rrule_s));
91 retm_if(_rrule == NULL, "Failed to calloc");
93 _rrule->freq = _event->freq;
95 _rrule->range_type = _event->range_type;
96 switch (_rrule->range_type)
98 case CALENDAR_RANGE_UNTIL:
99 _rrule->until_type = _event->until_type;
100 switch (_rrule->until_type)
102 case CALENDAR_TIME_UTIME:
103 _rrule->until_utime = _event->until_utime;
106 case CALENDAR_TIME_LOCALTIME:
107 _rrule->until_year = _event->until_year;
108 _rrule->until_month = _event->until_month;
109 _rrule->until_mday = _event->until_mday;
114 case CALENDAR_RANGE_COUNT:
116 case CALENDAR_RANGE_NONE:
120 _rrule->count = _event->count;
121 _rrule->interval = _event->interval;
122 _rrule->bysecond = _event->bysecond;
123 _rrule->byminute = _event->byminute;
124 _rrule->byhour = _event->byhour;
125 _rrule->byday = _event->byday;
126 _rrule->bymonthday = _event->bymonthday;
127 _rrule->byyearday = _event->byyearday;
128 _rrule->byweekno = _event->byweekno;
129 _rrule->bymonth = _event->bymonth;
130 _rrule->bysetpos = _event->bysetpos;
131 _rrule->wkst = _event->wkst;
136 void _cal_db_rrule_set_rrule_to_event(cal_rrule_s *rrule, calendar_record_h event)
140 retm_if(rrule == NULL, "Invalid argument: rrule is NULL");
141 retm_if(event == NULL, "Invalid argument: rrule is NULL");
143 _event = (cal_event_s *)event;
145 _event->freq = rrule->freq;
146 _event->range_type = rrule->range_type;
147 _event->until_type = rrule->until_type;
148 _event->until_utime = rrule->until_utime;
149 _event->until_year = rrule->until_year;
150 _event->until_month = rrule->until_month;
151 _event->until_mday = rrule->until_mday;
152 _event->count = rrule->count;
153 _event->interval = rrule->interval;
154 _event->bysecond = rrule->bysecond;
155 _event->byminute = rrule->byminute;
156 _event->byhour = rrule->byhour;
157 _event->byday = rrule->byday;
158 _event->bymonthday = rrule->bymonthday;
159 _event->byyearday = rrule->byyearday;
160 _event->byweekno = rrule->byweekno;
161 _event->bymonth = rrule->bymonth;
162 _event->bysetpos = rrule->bysetpos;
163 _event->wkst = rrule->wkst;
166 void _cal_db_rrule_set_rrule_to_todo(cal_rrule_s *rrule, calendar_record_h todo)
170 retm_if(rrule == NULL, "Invalid argument: rrule is NULL");
171 retm_if(todo == NULL, "Invalid argument: todo is NULL");
173 _todo = (cal_todo_s *)todo;
175 _todo->freq = rrule->freq;
176 _todo->range_type = rrule->range_type;
177 _todo->until_type = rrule->until_type;
178 _todo->until_utime = rrule->until_utime;
179 _todo->until_year = rrule->until_year;
180 _todo->until_month = rrule->until_month;
181 _todo->until_mday = rrule->until_mday;
182 _todo->count = rrule->count;
183 _todo->interval = rrule->interval;
184 _todo->bysecond = rrule->bysecond;
185 _todo->byminute = rrule->byminute;
186 _todo->byhour = rrule->byhour;
187 _todo->byday = rrule->byday;
188 _todo->bymonthday = rrule->bymonthday;
189 _todo->byyearday = rrule->byyearday;
190 _todo->byweekno = rrule->byweekno;
191 _todo->bymonth = rrule->bymonth;
192 _todo->bysetpos = rrule->bysetpos;
193 _todo->wkst = rrule->wkst;
196 void _cal_db_rrule_get_rrule_from_todo(calendar_record_h todo, cal_rrule_s **rrule)
201 retm_if(todo == NULL, "Invalid argument: rrule is NULL");
203 _todo = (cal_todo_s *)todo;
205 _rrule = calloc(1, sizeof(cal_rrule_s));
206 retm_if(_rrule == NULL, "Failed to calloc");
208 _rrule->freq = _todo->freq;
209 _rrule->range_type = _todo->range_type;
210 _rrule->until_type = _todo->until_type;
211 _rrule->until_utime = _todo->until_utime;
212 _rrule->until_year = _todo->until_year;
213 _rrule->until_month = _todo->until_month;
214 _rrule->until_mday = _todo->until_mday;
215 _rrule->count = _todo->count;
216 _rrule->interval = _todo->interval;
217 _rrule->bysecond = _todo->bysecond;
218 _rrule->byminute = _todo->byminute;
219 _rrule->byhour = _todo->byhour;
220 _rrule->byday = _todo->byday;
221 _rrule->bymonthday = _todo->bymonthday;
222 _rrule->byyearday = _todo->byyearday;
223 _rrule->byweekno = _todo->byweekno;
224 _rrule->bymonth = _todo->bymonth;
225 _rrule->bysetpos = _todo->bysetpos;
226 _rrule->wkst = _todo->wkst;
231 int __cal_db_rrule_insert_record(int id, cal_rrule_s *rrule)
235 char query[CAL_DB_SQL_MAX_LEN] = {0};
236 char until_datetime[32] = {0};
237 sqlite3_stmt *stmt = NULL;
238 cal_db_util_error_e dbret = CAL_DB_OK;
240 retvm_if(rrule == NULL, CALENDAR_ERROR_INVALID_PARAMETER,
241 "Invalid argument: rrule is NULL");
243 snprintf(query, sizeof(query),
245 "event_id, freq, range_type, "
246 "until_type, until_utime, until_datetime, "
248 "bysecond, byminute, byhour, byday, "
249 "bymonthday, byyearday, byweekno, bymonth, "
260 id, rrule->freq, rrule->range_type,
261 rrule->until_type, rrule->until_type == CALENDAR_TIME_UTIME ? rrule->until_utime : 0,
262 rrule->count, rrule->interval,
266 stmt = _cal_db_util_query_prepare(query);
269 DBG("query[%s]", query);
270 ERR("_cal_db_util_query_prepare() Failed");
271 return CALENDAR_ERROR_DB_FAILED;
276 if (CALENDAR_TIME_LOCALTIME == rrule->until_type)
278 snprintf(until_datetime, sizeof(until_datetime), "%04d%02d%02d",
279 rrule->until_year, rrule->until_month, rrule->until_mday);
280 _cal_db_util_stmt_bind_text(stmt, index, until_datetime);
285 _cal_db_util_stmt_bind_text(stmt, index, rrule->bysecond);
289 _cal_db_util_stmt_bind_text(stmt, index, rrule->byminute);
293 _cal_db_util_stmt_bind_text(stmt, index, rrule->byhour);
297 _cal_db_util_stmt_bind_text(stmt, index, rrule->byday);
300 if (rrule->bymonthday)
301 _cal_db_util_stmt_bind_text(stmt, index, rrule->bymonthday);
304 if (rrule->byyearday)
305 _cal_db_util_stmt_bind_text(stmt, index, rrule->byyearday);
309 _cal_db_util_stmt_bind_text(stmt, index, rrule->byweekno);
313 _cal_db_util_stmt_bind_text(stmt, index, rrule->bymonth);
317 _cal_db_util_stmt_bind_text(stmt, index, rrule->bysetpos);
320 dbret = _cal_db_util_stmt_step(stmt);
321 sqlite3_finalize(stmt);
322 if (CAL_DB_OK != dbret)
324 ERR("_cal_db_util_stmt_step() Failed(%d)", dbret);
327 case CAL_DB_ERROR_NO_SPACE:
328 return CALENDAR_ERROR_FILE_NO_SPACE;
330 return CALENDAR_ERROR_DB_FAILED;
334 rrule_id = _cal_db_util_last_insert_id();
335 DBG("rrule_id(%d)", rrule_id);
336 return CALENDAR_ERROR_NONE;
339 int _cal_db_rrule_get_rrule(int id, cal_rrule_s **rrule)
341 char query[CAL_DB_SQL_MAX_LEN];
343 sqlite3_stmt *stmt = NULL;
344 cal_rrule_s *_rrule = NULL;
345 const unsigned char *temp;
346 cal_db_util_error_e dbret = CAL_DB_OK;
348 _rrule = calloc(1, sizeof(cal_rrule_s));
349 retvm_if(_rrule == NULL, CALENDAR_ERROR_OUT_OF_MEMORY,
352 snprintf(query, sizeof(query),
354 "WHERE event_id = %d ",
358 stmt = _cal_db_util_query_prepare(query);
361 ERR("_cal_db_util_query_prepare() Failed");
363 return CALENDAR_ERROR_DB_FAILED;
366 dbret = _cal_db_util_stmt_step(stmt);
367 if (dbret != CAL_DB_ROW) {
368 ERR("Failed to step stmt(%d)", dbret);
369 sqlite3_finalize(stmt);
373 case CAL_DB_ERROR_NO_SPACE:
374 return CALENDAR_ERROR_FILE_NO_SPACE;
376 return CALENDAR_ERROR_DB_FAILED;
381 sqlite3_column_int(stmt, index++); // id
382 sqlite3_column_int(stmt, index++); // event_id
383 _rrule->freq = sqlite3_column_int(stmt, index++);
385 //rrule->_rrule_id = 1;
386 _rrule->range_type = sqlite3_column_int(stmt, index++);
387 _rrule->until_type = sqlite3_column_int(stmt, index++);
388 _rrule->until_utime = sqlite3_column_int64(stmt, index++);
390 temp = sqlite3_column_text(stmt, index++);
391 if (CALENDAR_TIME_LOCALTIME == _rrule->until_type)
393 sscanf((const char *)temp, "%04d%02d%02d",
394 &_rrule->until_year, &_rrule->until_month, &_rrule->until_mday);
397 _rrule->count = sqlite3_column_int(stmt, index++);
398 _rrule->interval = sqlite3_column_int(stmt, index++);
400 temp = sqlite3_column_text(stmt, index++);
401 _rrule->bysecond = SAFE_STRDUP(temp);
403 temp = sqlite3_column_text(stmt, index++);
404 _rrule->byminute = SAFE_STRDUP(temp);
406 temp = sqlite3_column_text(stmt, index++);
407 _rrule->byhour = SAFE_STRDUP(temp);
409 temp = sqlite3_column_text(stmt, index++);
410 _rrule->byday= SAFE_STRDUP(temp);
412 temp = sqlite3_column_text(stmt, index++);
413 _rrule->bymonthday= SAFE_STRDUP(temp);
415 temp = sqlite3_column_text(stmt, index++);
416 _rrule->byyearday= SAFE_STRDUP(temp);
418 temp = sqlite3_column_text(stmt, index++);
419 _rrule->byweekno= SAFE_STRDUP(temp);
421 temp = sqlite3_column_text(stmt, index++);
422 _rrule->bymonth= SAFE_STRDUP(temp);
424 temp = sqlite3_column_text(stmt, index++);
425 _rrule->bysetpos = SAFE_STRDUP(temp);
427 _rrule->wkst = sqlite3_column_int(stmt, index++);
429 sqlite3_finalize(stmt);
432 return CALENDAR_ERROR_NONE;
435 static int __cal_db_rrule_delete_record(int id)
437 char query[CAL_DB_SQL_MAX_LEN] = {0};
438 cal_db_util_error_e dbret = CAL_DB_OK;
440 snprintf(query, sizeof(query),
441 "DELETE FROM %s WHERE event_id = %d ",
442 CAL_TABLE_RRULE, id);
444 dbret = _cal_db_util_query_exec(query);
445 if(CAL_DB_DONE != dbret)
447 ERR("_cal_db_util_query_exec() Failed");
450 case CAL_DB_ERROR_NO_SPACE:
451 return CALENDAR_ERROR_FILE_NO_SPACE;
453 return CALENDAR_ERROR_DB_FAILED;
456 return CALENDAR_ERROR_NONE;
459 static int __cal_db_rrule_has_record(int id, int *has_record)
463 char query[CAL_DB_SQL_MAX_LEN] = {0};
465 snprintf(query, sizeof(query),
466 "SELECT count(*) FROM %s WHERE event_id = %d ",
467 CAL_TABLE_RRULE, id);
469 ret = _cal_db_util_query_get_first_int_result(query, NULL, &count);
470 if (CALENDAR_ERROR_NONE != ret)
472 ERR("_cal_db_util_query_get_first_int_result() failed");
476 *has_record = count > 0 ? 1 : 0;
477 return CALENDAR_ERROR_NONE;
480 static int __cal_db_rrule_update_record(int id, cal_rrule_s *rrule)
482 char query[CAL_DB_SQL_MAX_LEN] = {0};
483 char until_datetime[32] = {0};
484 cal_db_util_error_e dbret = CAL_DB_OK;
485 sqlite3_stmt *stmt = NULL;
487 snprintf(query, sizeof(query),
492 "until_utime = %lld, "
493 "until_datetime = ?, "
506 "WHERE event_id = %d ",
511 rrule->until_type == CALENDAR_TIME_UTIME ? rrule->until_utime : 0,
517 stmt = _cal_db_util_query_prepare(query);
520 DBG("query[%s]", query);
521 ERR("_cal_db_util_query_prepare() Failed");
522 return CALENDAR_ERROR_DB_FAILED;
526 if (CALENDAR_TIME_LOCALTIME == rrule->until_type)
528 snprintf(until_datetime, sizeof(until_datetime), "%04d%02d%02d",
529 rrule->until_year, rrule->until_month, rrule->until_mday);
530 _cal_db_util_stmt_bind_text(stmt, index, until_datetime);
535 _cal_db_util_stmt_bind_text(stmt, index, rrule->bysecond);
539 _cal_db_util_stmt_bind_text(stmt, index, rrule->byminute);
543 _cal_db_util_stmt_bind_text(stmt, index, rrule->byhour);
547 _cal_db_util_stmt_bind_text(stmt, index, rrule->byday);
550 if (rrule->bymonthday)
551 _cal_db_util_stmt_bind_text(stmt, index, rrule->bymonthday);
554 if (rrule->byyearday)
555 _cal_db_util_stmt_bind_text(stmt, index, rrule->byyearday);
559 _cal_db_util_stmt_bind_text(stmt, index, rrule->byweekno);
563 _cal_db_util_stmt_bind_text(stmt, index, rrule->bymonth);
567 _cal_db_util_stmt_bind_text(stmt, index, rrule->bysetpos);
570 dbret = _cal_db_util_stmt_step(stmt);
571 sqlite3_finalize(stmt);
572 if (CAL_DB_DONE != dbret) {
573 ERR("sqlite3_step() Failed(%d)", dbret);
576 case CAL_DB_ERROR_NO_SPACE:
577 return CALENDAR_ERROR_FILE_NO_SPACE;
579 return CALENDAR_ERROR_DB_FAILED;
582 return CALENDAR_ERROR_NONE;
585 int _cal_db_rrule_insert_record(int id, cal_rrule_s *rrule)
587 retvm_if(rrule == NULL, CALENDAR_ERROR_INVALID_PARAMETER,
588 "Invalid argument: rrule is NULL");
590 if (rrule->freq == CALENDAR_RECURRENCE_NONE)
595 __cal_db_rrule_insert_record(id, rrule);
597 return CALENDAR_ERROR_NONE;
600 int _cal_db_rrule_update_record(int id, cal_rrule_s *rrule)
604 if (NULL == rrule || rrule->freq == CALENDAR_RECURRENCE_NONE)
607 __cal_db_rrule_delete_record(id);
608 return CALENDAR_ERROR_NONE;
612 __cal_db_rrule_has_record(id, &has_record);
615 __cal_db_rrule_update_record(id, rrule);
619 __cal_db_rrule_insert_record(id, rrule);
622 return CALENDAR_ERROR_NONE;