4f34df966564a4ae409fba3b594013ac67a725a0
[platform/core/pim/calendar-service.git] / server / db / cal_db_plugin_alarm_helper.c
1 /*
2  * Calendar Service
3  *
4  * Copyright (c) 2012 - 2015 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 <stdlib.h>
21
22 #include "calendar_db.h"
23
24 #include "cal_internal.h"
25 #include "cal_typedef.h"
26 #include "cal_view.h"
27 #include "cal_time.h"
28 #include "cal_record.h"
29 #include "cal_list.h"
30
31 #include "cal_db_util.h"
32 #include "cal_db.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"
38
39 static int _cal_db_alarm_insert_record(calendar_record_h record, int parent_id)
40 {
41         cal_alarm_s *alarm = NULL;
42
43         alarm = (cal_alarm_s *)(record);
44         RETVM_IF(NULL == alarm, CALENDAR_ERROR_INVALID_PARAMETER,
45                         "Invalid argument: cal_alarm_s is NULL");
46
47         if (alarm->remind_tick_unit == CALENDAR_ALARM_NONE) {
48                 DBG("No alarm unit tick");
49                 return CALENDAR_ERROR_NONE;
50         }
51
52         int ret = 0;
53         char query[CAL_DB_SQL_MAX_LEN] = {0};
54         sqlite3_stmt *stmt = NULL;
55
56         snprintf(query, sizeof(query),
57                         "INSERT INTO %s ("
58                         "event_id, "
59                         "remind_tick, remind_tick_unit, "
60                         "alarm_description, "
61                         "alarm_type,  "
62                         "alarm_summary, alarm_action, alarm_attach, "
63                         "alarm_utime, alarm_datetime "
64                         ") VALUES ("
65                         "%d, "
66                         "%d, %d, "
67                         "?, "
68                         "%d, "
69                         "?, %d, ?, "
70                         "%lld, ?)",
71                         CAL_TABLE_ALARM,
72                         parent_id,
73                         alarm->remind_tick, alarm->remind_tick_unit,
74                         alarm->alarm.type,
75                         alarm->alarm_action,
76                         alarm->alarm.time.utime);
77
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);
82                 return ret;
83         }
84
85         int index = 1;
86
87         if (alarm->alarm_description) {
88                 cal_db_util_stmt_bind_text(stmt, index, alarm->alarm_description);
89                 DBG("description[%s]", alarm->alarm_description);
90         }
91         index++;
92
93         if (alarm->alarm_summary) {
94                 cal_db_util_stmt_bind_text(stmt, index, alarm->alarm_summary);
95                 DBG("summary [%s]", alarm->alarm_summary);
96         }
97         index++;
98
99         if (alarm->alarm_attach)
100                 cal_db_util_stmt_bind_text(stmt, index, alarm->alarm_attach);
101         index++;
102
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);
114         }
115         index++;
116
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);
121                 return ret;
122         }
123
124         return CALENDAR_ERROR_NONE;
125 }
126
127 int cal_db_alarm_insert_records(cal_list_s *list_s, int event_id)
128 {
129         int ret;
130         int count = 0;
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);
134
135         calendar_list_get_count(list, &count);
136         if (0 == count)
137                 return CALENDAR_ERROR_NONE;
138
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);
144         }
145         return CALENDAR_ERROR_NONE;
146 }
147
148 int cal_db_alarm_get_records(int parent, cal_list_s *list)
149 {
150         int ret;
151         char query[CAL_DB_SQL_MAX_LEN] = {0};
152         sqlite3_stmt *stmt = NULL;
153
154         RETVM_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER, "Invalid parameter: list is NULL");
155
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);
160
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);
165                 return ret;
166         }
167
168         int index = 0;
169         const unsigned char *temp;
170         calendar_record_h record = NULL;
171         cal_alarm_s *alarm = NULL;
172
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);
178                         return ret;
179                 }
180
181                 index = 0;
182                 alarm = (cal_alarm_s *)(record);
183
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++);
188
189                 temp = sqlite3_column_text(stmt, index++);
190                 alarm->alarm_description = cal_strdup((const char*)temp);
191
192                 alarm->alarm.type = sqlite3_column_int(stmt, index++);
193
194                 temp = sqlite3_column_text(stmt, index++);
195                 alarm->alarm_summary = cal_strdup((const char*)temp);
196
197                 alarm->alarm_action = sqlite3_column_int(stmt, index++);
198
199                 temp = sqlite3_column_text(stmt, index++);
200                 alarm->alarm_attach = cal_strdup((const char*)temp);
201
202                 if (alarm->alarm.type == CALENDAR_TIME_UTIME) {
203                         alarm->alarm.time.utime = sqlite3_column_int64(stmt, index++);
204                         index++; /* datetime */
205                 } else {
206                         index++; /* utime */
207                         temp = sqlite3_column_text(stmt, index++);
208                         if (temp) {
209                                 int y = 0, m = 0, d = 0;
210                                 int h = 0, n = 0, s = 0;
211                                 switch (strlen((const char*)temp)) {
212                                 case 8:
213                                         sscanf((const char *)temp, CAL_DATETIME_FORMAT_YYYYMMDD, &y, &m, &d);
214                                         break;
215                                 case 15:
216                                         sscanf((const char *)temp, CAL_DATETIME_FORMAT_YYYYMMDDTHHMMSS, &y, &m, &d, &h, &n, &s);
217                                         break;
218                                 case 19:
219                                         sscanf((const char *)temp, CAL_FORMAT_LOCAL_DATETIME, &y, &m, &d, &h, &n, &s);
220                                         break;
221                                 }
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;
228                         }
229                 }
230                 calendar_list_add((calendar_list_h)list, record);
231         }
232         sqlite3_finalize(stmt);
233         return CALENDAR_ERROR_NONE;
234 }
235
236 int cal_db_alarm_delete_with_id(int parent_id)
237 {
238         char query[CAL_DB_SQL_MAX_LEN] = {0};
239         int ret = 0;
240
241         snprintf(query, sizeof(query), "DELETE FROM %s WHERE event_id=%d ",
242                         CAL_TABLE_ALARM, parent_id);
243
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);
248                 return ret;
249         }
250
251         return CALENDAR_ERROR_NONE;
252 }
253
254 int cal_db_alarm_has_alarm(cal_list_s *list_s)
255 {
256         calendar_record_h alarm = NULL;
257         int has_alarm = 0;
258         calendar_list_h list = (calendar_list_h)list_s;
259
260         calendar_list_first(list);
261         while (CALENDAR_ERROR_NONE == calendar_list_get_current_record_p(list, &alarm)) {
262                 int unit;
263                 calendar_record_get_int(alarm, _calendar_alarm.tick_unit, &unit);
264                 if (CALENDAR_ALARM_NONE != unit) {
265                         has_alarm = 1;
266                         break;
267                 }
268                 calendar_list_next(list);
269         }
270         return has_alarm;
271 }