Move notification_get_text_input_max_length to internal
[platform/core/api/notification.git] / src / notification_db.c
1 /*
2  * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <errno.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <string.h>
21
22 #include <sqlite3.h>
23 #include <db-util.h>
24 #include <tizen.h>
25 #include <tzplatform_config.h>
26
27 #include <notification_error.h>
28 #include <notification_debug.h>
29 #include <notification_db.h>
30
31 #define NOTIFICATION_DB_NAME ".notification.db"
32 #define CREATE_NOTIFICATION_TABLE " \
33 PRAGMA journal_mode = PERSIST; \
34 PRAGMA synchronous = FULL; \
35 create  table if not exists noti_list ( \
36                         type INTEGER NOT NULL, \
37                         layout INTEGER NOT NULL default 0, \
38                         caller_pkgname TEXT NOT NULL, \
39                         launch_pkgname TEXT, \
40                         image_path TEXT, \
41                         group_id INTEGER default 0,  \
42                         internal_group_id INTEGER default 0,  \
43                         priv_id INTEGER PRIMARY KEY AUTOINCREMENT,  \
44                         title_key TEXT, \
45                         b_text TEXT, \
46                         b_key TEXT, \
47                         tag TEXT, \
48                         b_format_args TEXT, \
49                         num_format_args INTEGER default 0, \
50                         text_domain TEXT, \
51                         text_dir TEXT, \
52                         time INTEGER default 0, \
53                         insert_time INTEGER default 0, \
54                         args TEXT, \
55                         group_args TEXT, \
56                         b_execute_option TEXT, \
57                         b_service_responding TEXT, \
58                         b_service_single_launch TEXT, \
59                         b_service_multi_launch TEXT, \
60                         b_event_handler_click_on_button_1 TEXT, \
61                         b_event_handler_click_on_button_2 TEXT, \
62                         b_event_handler_click_on_button_3 TEXT, \
63                         b_event_handler_click_on_button_4 TEXT, \
64                         b_event_handler_click_on_button_5 TEXT, \
65                         b_event_handler_click_on_button_6 TEXT, \
66                         b_event_handler_click_on_icon TEXT, \
67                         b_event_handler_click_on_thumbnail TEXT, \
68                         sound_type INTEGER default 0, \
69                         sound_path TEXT, \
70                         vibration_type INTEGER default 0, \
71                         vibration_path TEXT, \
72                         led_operation INTEGER default 0, \
73                         led_argb INTEGER default 0, \
74                         led_on_ms INTEGER default -1, \
75                         led_off_ms INTEGER default -1, \
76                         flags_for_property INTEGER default 0, \
77                         flag_simmode INTEGER default 0, \
78                         display_applist INTEGER, \
79                         progress_size DOUBLE default 0, \
80                         progress_percentage DOUBLE default 0, \
81                         ongoing_flag INTEGER default 0, \
82                         ongoing_value_type INTEGER default 0, \
83                         ongoing_current INTEGER default 0, \
84                         ongoing_duration INTEGER default 0, \
85                         auto_remove INTEGER default 1, \
86                         default_button_index INTEGER default 0, \
87                         timeout INTEGER default 0, \
88                         text_input_max_length INTEGER default 0, \
89                         uid INTEGER \
90                 ); \
91                 create table if not exists noti_group_data ( \
92                         caller_pkgname TEXT NOT NULL, \
93                         group_id INTEGER default 0, \
94                         badge INTEGER default 0, \
95                         title TEXT, \
96                         content TEXT, \
97                         loc_title TEXT, \
98                         loc_content TEXT, \
99                         count_display_title INTEGER, \
100                         count_display_content INTEGER, \
101                         rowid INTEGER PRIMARY KEY AUTOINCREMENT, \
102                         UNIQUE (caller_pkgname, group_id) \
103                 ); \
104                 create table if not exists ongoing_list ( \
105                         caller_pkgname TEXT NOT NULL, \
106                         launch_pkgname TEXT, \
107                         icon_path TEXT, \
108                         group_id INTEGER default 0, \
109                         internal_group_id INTEGER default 0, \
110                         priv_id INTERGER NOT NULL, \
111                         title TEXT, \
112                         content TEXT, \
113                         default_content TEXT, \
114                         loc_title TEXT, \
115                         loc_content TEXT, \
116                         loc_default_content TEXT, \
117                         text_domain TEXT, \
118                         text_dir TEXT, \
119                         args TEXT, \
120                         group_args TEXT, \
121                         flag INTEGER default 0, \
122                         progress_size DOUBLE default 0, \
123                         progress_percentage DOUBLE default 0, \
124                         rowid INTEGER PRIMARY KEY AUTOINCREMENT, \
125                         UNIQUE (caller_pkgname, priv_id) \
126                 ); \
127                 CREATE TABLE IF NOT EXISTS notification_setting ( \
128                         uid INTEGER, \
129                         package_name TEXT NOT NULL, \
130                         allow_to_notify INTEGER DEFAULT 1, \
131                         do_not_disturb_except INTEGER DEFAULT 0, \
132                         visibility_class INTEGER DEFAULT 0, \
133                         pop_up_notification INTEGER DEFAULT 1, \
134                         lock_screen_content_level INTEGER DEFAULT 0, \
135                         UNIQUE (uid, package_name) \
136                 ); \
137                 CREATE TABLE IF NOT EXISTS notification_system_setting ( \
138                         uid INTEGER, \
139                         do_not_disturb INTEGER DEFAULT 0, \
140                         visibility_class INTEGER DEFAULT 0, \
141                         dnd_schedule_enabled INTEGER DEFAULT 0, \
142                         dnd_schedule_day INTEGER DEFAULT 62, \
143                         dnd_start_hour INTEGER DEFAULT 22, \
144                         dnd_start_min INTEGER DEFAULT 0, \
145                         dnd_end_hour INTEGER DEFAULT 8, \
146                         dnd_end_min INTEGER DEFAULT 0, \
147                         lock_screen_content_level INTEGER DEFAULT 0, \
148                         UNIQUE (uid) \
149                 ); \
150                 CREATE TABLE IF NOT EXISTS dnd_allow_exception ( \
151                         uid INTEGER, \
152                         type INTEGER DEFAULT 0, \
153                         value INTEGER DEFAULT 0, \
154                         UNIQUE (uid, type) \
155                 ); \
156                 CREATE TABLE IF NOT EXISTS noti_template ( \
157                         type INTEGER NOT NULL, \
158                         layout INTEGER NOT NULL default 0, \
159                         caller_pkgname TEXT NOT NULL, \
160                         launch_pkgname TEXT, \
161                         image_path TEXT, \
162                         group_id INTEGER default 0,  \
163                         internal_group_id INTEGER default 0,  \
164                         priv_id INTEGER PRIMARY KEY AUTOINCREMENT,  \
165                         title_key TEXT, \
166                         b_text TEXT, \
167                         b_key TEXT, \
168                         tag TEXT, \
169                         b_format_args TEXT, \
170                         num_format_args INTEGER default 0, \
171                         text_domain TEXT, \
172                         text_dir TEXT, \
173                         time INTEGER default 0, \
174                         insert_time INTEGER default 0, \
175                         args TEXT, \
176                         group_args TEXT, \
177                         b_execute_option TEXT, \
178                         b_service_responding TEXT, \
179                         b_service_single_launch TEXT, \
180                         b_service_multi_launch TEXT, \
181                         b_event_handler_click_on_button_1 TEXT, \
182                         b_event_handler_click_on_button_2 TEXT, \
183                         b_event_handler_click_on_button_3 TEXT, \
184                         b_event_handler_click_on_button_4 TEXT, \
185                         b_event_handler_click_on_button_5 TEXT, \
186                         b_event_handler_click_on_button_6 TEXT, \
187                         b_event_handler_click_on_icon TEXT, \
188                         b_event_handler_click_on_thumbnail TEXT, \
189                         sound_type INTEGER default 0, \
190                         sound_path TEXT, \
191                         vibration_type INTEGER default 0, \
192                         vibration_path TEXT, \
193                         led_operation INTEGER default 0, \
194                         led_argb INTEGER default 0, \
195                         led_on_ms INTEGER default -1, \
196                         led_off_ms INTEGER default -1, \
197                         flags_for_property INTEGER default 0, \
198                         flag_simmode INTEGER default 0, \
199                         display_applist INTEGER, \
200                         progress_size DOUBLE default 0, \
201                         progress_percentage DOUBLE default 0, \
202                         ongoing_flag INTEGER default 0, \
203                         ongoing_value_type INTEGER default 0, \
204                         ongoing_current INTEGER default 0, \
205                         ongoing_duration INTEGER default 0, \
206                         auto_remove INTEGER default 1, \
207                         default_button_index INTEGER default 0, \
208                         timeout INTEGER default 0, \
209                         text_input_max_length INTEGER default 0, \
210                         uid INTEGER, \
211                         template_name TEXT, \
212                         UNIQUE (caller_pkgname, template_name) \
213                 );"
214
215
216 EXPORT_API int notification_db_init()
217 {
218         int r;
219         sqlite3 *db = NULL;
220         char *errmsg = NULL;
221         char defname[FILENAME_MAX];
222         char *query = NULL;
223         const char *db_path = tzplatform_getenv(TZ_SYS_DB);
224         if (db_path == NULL) {
225                 NOTIFICATION_ERR("fail to get db_path"); /* LCOV_EXCL_LINE */
226                 return NOTIFICATION_ERROR_OUT_OF_MEMORY;
227         }
228         snprintf(defname, sizeof(defname), "%s/%s", db_path, NOTIFICATION_DB_NAME);
229
230         NOTIFICATION_DBG("db path : %s", defname);
231         r = sqlite3_open_v2(defname, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
232         if (r) {
233                 /* LCOV_EXCL_START */
234                 db_util_close(db);
235                 NOTIFICATION_ERR("fail to open notification db %d", r);
236                 return NOTIFICATION_ERROR_IO_ERROR;
237                 /* LCOV_EXCL_STOP */
238         }
239         query = sqlite3_mprintf(CREATE_NOTIFICATION_TABLE, tzplatform_getuid(TZ_SYS_DEFAULT_USER));
240         NOTIFICATION_DBG("@@@ query : %s", query);
241
242         r = sqlite3_exec(db, CREATE_NOTIFICATION_TABLE, NULL, NULL, &errmsg);
243         if (query)
244                 sqlite3_free(query);
245         if (r != SQLITE_OK) {
246                 /* LCOV_EXCL_START */
247                 NOTIFICATION_ERR("query error(%d)(%s)", r, errmsg);
248                 sqlite3_free(errmsg);
249                 db_util_close(db);
250                 return NOTIFICATION_ERROR_IO_ERROR;
251                 /* LCOV_EXCL_STOP */
252         }
253
254         db_util_close(db);
255         return NOTIFICATION_ERROR_NONE;
256 }
257
258 sqlite3 *notification_db_open(const char *dbfile)
259 {
260         int ret = 0;
261         sqlite3 *db = 0;
262
263         ret = db_util_open(dbfile, &db, 0);
264         if (ret != SQLITE_OK) {
265                 /* LCOV_EXCL_START */
266                 if (ret == SQLITE_PERM)
267                         set_last_result(NOTIFICATION_ERROR_PERMISSION_DENIED);
268                 else
269                         set_last_result(NOTIFICATION_ERROR_FROM_DB);
270
271                 return NULL;
272                 /* LCOV_EXCL_STOP */
273         }
274
275         return db;
276 }
277
278 int notification_db_close(sqlite3 **db)
279 {
280         int ret = 0;
281
282         if (db == NULL || *db == NULL)
283                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
284
285         ret = db_util_close(*db);
286         if (ret != SQLITE_OK) {
287                 /* LCOV_EXCL_START */
288                 NOTIFICATION_ERR("DB close error(%d)", ret);
289                 return NOTIFICATION_ERROR_FROM_DB;
290                 /* LCOV_EXCL_STOP */
291         }
292
293         *db = NULL;
294
295         return NOTIFICATION_ERROR_NONE;
296 }
297
298 int notification_db_exec(sqlite3 *db, const char *query, int *num_changes)
299 {
300         int ret = 0;
301         sqlite3_stmt *stmt = NULL;
302
303         if (db == NULL)
304                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
305
306         if (query == NULL)
307                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
308
309         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
310         if (ret != SQLITE_OK) {
311                 /* LCOV_EXCL_START */
312                 NOTIFICATION_ERR("DB err(%d) : %s", ret,
313                                  sqlite3_errmsg(db));
314                 return NOTIFICATION_ERROR_FROM_DB;
315                 /* LCOV_EXCL_STOP */
316         }
317
318         if (stmt != NULL) {
319                 ret = sqlite3_step(stmt);
320                 if (ret == SQLITE_OK || ret == SQLITE_DONE) {
321                         if (num_changes != NULL)
322                                 *num_changes = sqlite3_changes(db);
323
324                         sqlite3_finalize(stmt);
325                 } else {
326                         /* LCOV_EXCL_START */
327                         NOTIFICATION_ERR("DB err(%d) : %s", ret,
328                                          sqlite3_errmsg(db));
329                         sqlite3_finalize(stmt);
330                         return NOTIFICATION_ERROR_FROM_DB;
331                         /* LCOV_EXCL_STOP */
332                 }
333         } else {
334                         return NOTIFICATION_ERROR_FROM_DB;
335         }
336
337         return NOTIFICATION_ERROR_NONE;
338 }
339
340 char *notification_db_column_text(sqlite3_stmt * stmt, int col)
341 {
342         const unsigned char *col_text = NULL;
343
344         col_text = sqlite3_column_text(stmt, col);
345         if (col_text == NULL || col_text[0] == '\0')
346                 return NULL;
347
348         return strdup((char *)col_text);
349 }
350
351 bundle *notification_db_column_bundle(sqlite3_stmt * stmt, int col)
352 {
353         const unsigned char *col_bundle = NULL;
354
355         col_bundle = sqlite3_column_text(stmt, col);
356         if (col_bundle == NULL || col_bundle[0] == '\0')
357                 return NULL;
358
359         return bundle_decode(col_bundle, strlen((char *)col_bundle));
360 }
361