4 * Copyright (c) 2012 - 2015 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.
22 #include "calendar_db.h"
24 #include "cal_internal.h"
25 #include "cal_typedef.h"
28 #include "cal_record.h"
31 #include "cal_db_util.h"
33 #include "cal_db_query.h"
34 #include "cal_db_instance.h"
35 #include "cal_db_plugin_alarm_helper.h"
36 #include "cal_db_util.h"
37 #include "cal_utils.h"
39 static int _cal_db_alarm_insert_record(calendar_record_h record, int parent_id)
41 cal_alarm_s *alarm = NULL;
43 alarm = (cal_alarm_s *)(record);
44 RETVM_IF(NULL == alarm, CALENDAR_ERROR_INVALID_PARAMETER,
45 "Invalid argument: cal_alarm_s is NULL");
47 if (alarm->remind_tick_unit == CALENDAR_ALARM_NONE) {
48 DBG("No alarm unit tick");
49 return CALENDAR_ERROR_NONE;
53 char query[CAL_DB_SQL_MAX_LEN] = {0};
54 sqlite3_stmt *stmt = NULL;
56 snprintf(query, sizeof(query),
59 "remind_tick, remind_tick_unit, "
62 "alarm_summary, alarm_action, alarm_attach, "
63 "alarm_utime, alarm_datetime "
73 alarm->remind_tick, alarm->remind_tick_unit,
76 alarm->alarm.time.utime);
78 ret = cal_db_util_query_prepare(query, &stmt);
79 if (CALENDAR_ERROR_NONE != ret) {
80 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
81 SECURE("query[%s]", query);
87 if (alarm->alarm_description) {
88 cal_db_util_stmt_bind_text(stmt, index, alarm->alarm_description);
89 DBG("description[%s]", alarm->alarm_description);
93 if (alarm->alarm_summary) {
94 cal_db_util_stmt_bind_text(stmt, index, alarm->alarm_summary);
95 DBG("summary [%s]", alarm->alarm_summary);
99 if (alarm->alarm_attach)
100 cal_db_util_stmt_bind_text(stmt, index, alarm->alarm_attach);
103 if (CALENDAR_TIME_LOCALTIME == alarm->alarm.type) {
104 char alarm_datetime[CAL_STR_SHORT_LEN32] = {0};
105 snprintf(alarm_datetime, sizeof(alarm_datetime), CAL_FORMAT_LOCAL_DATETIME,
106 alarm->alarm.time.date.year,
107 alarm->alarm.time.date.month,
108 alarm->alarm.time.date.mday,
109 alarm->alarm.time.date.hour,
110 alarm->alarm.time.date.minute,
111 alarm->alarm.time.date.second);
112 cal_db_util_stmt_bind_text(stmt, index, alarm_datetime);
113 DBG("datetime [%s]", alarm_datetime);
117 ret = cal_db_util_stmt_step(stmt);
118 sqlite3_finalize(stmt);
119 if (CALENDAR_ERROR_NONE != ret) {
120 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
124 return CALENDAR_ERROR_NONE;
127 int cal_db_alarm_insert_records(cal_list_s *list_s, int event_id)
131 calendar_record_h record = NULL;
132 calendar_list_h list = (calendar_list_h)list_s;
133 RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
135 calendar_list_get_count(list, &count);
137 return CALENDAR_ERROR_NONE;
139 calendar_list_first(list);
140 while (CALENDAR_ERROR_NONE == calendar_list_get_current_record_p(list, &record)) {
141 ret = _cal_db_alarm_insert_record(record, event_id);
142 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "cal_db_extended_insert_record() Fail(%d)", ret);
143 calendar_list_next(list);
145 return CALENDAR_ERROR_NONE;
148 int cal_db_alarm_get_records(int parent, cal_list_s *list)
151 char query[CAL_DB_SQL_MAX_LEN] = {0};
152 sqlite3_stmt *stmt = NULL;
154 RETVM_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER, "Invalid parameter: list is NULL");
156 snprintf(query, sizeof(query), "SELECT rowid,remind_tick,remind_tick_unit,"
157 "alarm_description,alarm_type,alarm_summary,alarm_action,alarm_attach,"
158 "alarm_utime,alarm_datetime FROM %s WHERE event_id=%d ",
159 CAL_TABLE_ALARM, parent);
161 ret = cal_db_util_query_prepare(query, &stmt);
162 if (CALENDAR_ERROR_NONE != ret) {
163 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
164 SECURE("query[%s]", query);
169 const unsigned char *temp;
170 calendar_record_h record = NULL;
171 cal_alarm_s *alarm = NULL;
173 while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
174 ret = calendar_record_create(_calendar_alarm._uri, &record);
175 if (CALENDAR_ERROR_NONE != ret) {
176 sqlite3_finalize(stmt);
177 cal_list_clear(list);
182 alarm = (cal_alarm_s *)(record);
184 alarm->parent_id = parent;
185 alarm->id = sqlite3_column_int(stmt, index++);
186 alarm->remind_tick = sqlite3_column_int(stmt, index++);
187 alarm->remind_tick_unit = sqlite3_column_int(stmt, index++);
189 temp = sqlite3_column_text(stmt, index++);
190 alarm->alarm_description = cal_strdup((const char*)temp);
192 alarm->alarm.type = sqlite3_column_int(stmt, index++);
194 temp = sqlite3_column_text(stmt, index++);
195 alarm->alarm_summary = cal_strdup((const char*)temp);
197 alarm->alarm_action = sqlite3_column_int(stmt, index++);
199 temp = sqlite3_column_text(stmt, index++);
200 alarm->alarm_attach = cal_strdup((const char*)temp);
202 if (alarm->alarm.type == CALENDAR_TIME_UTIME) {
203 alarm->alarm.time.utime = sqlite3_column_int64(stmt, index++);
204 index++; /* datetime */
207 temp = sqlite3_column_text(stmt, index++);
209 int y = 0, m = 0, d = 0;
210 int h = 0, n = 0, s = 0;
211 switch (strlen((const char*)temp)) {
213 sscanf((const char *)temp, CAL_DATETIME_FORMAT_YYYYMMDD, &y, &m, &d);
216 sscanf((const char *)temp, CAL_DATETIME_FORMAT_YYYYMMDDTHHMMSS, &y, &m, &d, &h, &n, &s);
219 sscanf((const char *)temp, CAL_FORMAT_LOCAL_DATETIME, &y, &m, &d, &h, &n, &s);
222 alarm->alarm.time.date.year = y;
223 alarm->alarm.time.date.month = m;
224 alarm->alarm.time.date.mday = d;
225 alarm->alarm.time.date.hour = h;
226 alarm->alarm.time.date.minute = n;
227 alarm->alarm.time.date.second = s;
230 calendar_list_add((calendar_list_h)list, record);
232 sqlite3_finalize(stmt);
233 return CALENDAR_ERROR_NONE;
236 int cal_db_alarm_delete_with_id(int parent_id)
238 char query[CAL_DB_SQL_MAX_LEN] = {0};
241 snprintf(query, sizeof(query), "DELETE FROM %s WHERE event_id=%d ",
242 CAL_TABLE_ALARM, parent_id);
244 ret = cal_db_util_query_exec(query);
245 if (CALENDAR_ERROR_NONE != ret) {
246 ERR("cal_db_util_query_exec() Fail(%d)", ret);
247 SECURE("[%s]", query);
251 return CALENDAR_ERROR_NONE;
254 int cal_db_alarm_has_alarm(cal_list_s *list_s)
256 calendar_record_h alarm = NULL;
258 calendar_list_h list = (calendar_list_h)list_s;
260 calendar_list_first(list);
261 while (CALENDAR_ERROR_NONE == calendar_list_get_current_record_p(list, &alarm)) {
263 calendar_record_get_int(alarm, _calendar_alarm.tick_unit, &unit);
264 if (CALENDAR_ALARM_NONE != unit) {
268 calendar_list_next(list);