Add new apis to set/get noti_template
[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                         auto_remove INTEGER default 1, \
83                         uid INTEGER \
84                 ); \
85                 create table if not exists noti_group_data ( \
86                         caller_pkgname TEXT NOT NULL, \
87                         group_id INTEGER default 0, \
88                         badge INTEGER default 0, \
89                         title TEXT, \
90                         content TEXT, \
91                         loc_title TEXT, \
92                         loc_content TEXT, \
93                         count_display_title INTEGER, \
94                         count_display_content INTEGER, \
95                         rowid INTEGER PRIMARY KEY AUTOINCREMENT, \
96                         UNIQUE (caller_pkgname, group_id) \
97                 ); \
98                 create table if not exists ongoing_list ( \
99                         caller_pkgname TEXT NOT NULL, \
100                         launch_pkgname TEXT, \
101                         icon_path TEXT, \
102                         group_id INTEGER default 0, \
103                         internal_group_id INTEGER default 0, \
104                         priv_id INTERGER NOT NULL, \
105                         title TEXT, \
106                         content TEXT, \
107                         default_content TEXT, \
108                         loc_title TEXT, \
109                         loc_content TEXT, \
110                         loc_default_content TEXT, \
111                         text_domain TEXT, \
112                         text_dir TEXT, \
113                         args TEXT, \
114                         group_args TEXT, \
115                         flag INTEGER default 0, \
116                         progress_size DOUBLE default 0, \
117                         progress_percentage DOUBLE default 0, \
118                         rowid INTEGER PRIMARY KEY AUTOINCREMENT, \
119                         UNIQUE (caller_pkgname, priv_id) \
120                 ); \
121                 CREATE TABLE IF NOT EXISTS notification_setting ( \
122                         uid INTEGER, \
123                         package_name TEXT NOT NULL, \
124                         allow_to_notify INTEGER DEFAULT 1, \
125                         do_not_disturb_except INTEGER DEFAULT 0, \
126                         visibility_class INTEGER DEFAULT 0, \
127                         UNIQUE (uid, package_name) \
128                 ); \
129                 CREATE TABLE IF NOT EXISTS notification_system_setting ( \
130                         uid INTEGER, \
131                         do_not_disturb INTEGER DEFAULT 0, \
132                         visibility_class INTEGER DEFAULT 0, \
133                         dnd_schedule_enabled INTEGER DEFAULT 0, \
134                         dnd_schedule_day INTEGER DEFAULT 0, \
135                         dnd_start_hour INTEGER DEFAULT 0, \
136                         dnd_start_min INTEGER DEFAULT 0, \
137                         dnd_end_hour INTEGER DEFAULT 0, \
138                         dnd_end_min INTEGER DEFAULT 0, \
139                         lock_screen_content_level INTEGER DEFAULT 0, \
140                         UNIQUE (uid) \
141                 );\
142                 CREATE TABLE IF NOT EXISTS noti_template ( \
143                         type INTEGER NOT NULL, \
144                         layout INTEGER NOT NULL default 0, \
145                         caller_pkgname TEXT NOT NULL, \
146                         launch_pkgname TEXT, \
147                         image_path TEXT, \
148                         group_id INTEGER default 0,  \
149                         internal_group_id INTEGER default 0,  \
150                         priv_id INTEGER PRIMARY KEY AUTOINCREMENT,  \
151                         title_key TEXT, \
152                         b_text TEXT, \
153                         b_key TEXT, \
154                         tag TEXT, \
155                         b_format_args TEXT, \
156                         num_format_args INTEGER default 0, \
157                         text_domain TEXT, \
158                         text_dir TEXT, \
159                         time INTEGER default 0, \
160                         insert_time INTEGER default 0, \
161                         args TEXT, \
162                         group_args TEXT, \
163                         b_execute_option TEXT, \
164                         b_service_responding TEXT, \
165                         b_service_single_launch TEXT, \
166                         b_service_multi_launch TEXT, \
167                         b_event_handler_click_on_button_1 TEXT, \
168                         b_event_handler_click_on_button_2 TEXT, \
169                         b_event_handler_click_on_button_3 TEXT, \
170                         b_event_handler_click_on_button_4 TEXT, \
171                         b_event_handler_click_on_button_5 TEXT, \
172                         b_event_handler_click_on_button_6 TEXT, \
173                         b_event_handler_click_on_icon TEXT, \
174                         b_event_handler_click_on_thumbnail TEXT, \
175                         sound_type INTEGER default 0, \
176                         sound_path TEXT, \
177                         vibration_type INTEGER default 0, \
178                         vibration_path TEXT, \
179                         led_operation INTEGER default 0, \
180                         led_argb INTEGER default 0, \
181                         led_on_ms INTEGER default -1, \
182                         led_off_ms INTEGER default -1, \
183                         flags_for_property INTEGER default 0, \
184                         flag_simmode INTEGER default 0, \
185                         display_applist INTEGER, \
186                         progress_size DOUBLE default 0, \
187                         progress_percentage DOUBLE default 0, \
188                         ongoing_flag INTEGER default 0, \
189                         auto_remove INTEGER default 1, \
190                         uid INTEGER, \
191                         template_name TEXT, \
192                         UNIQUE (caller_pkgname, template_name) \
193                 );"
194
195
196 EXPORT_API int notification_db_init()
197 {
198         int r;
199         sqlite3 *db = NULL;
200         char *errmsg = NULL;
201         char defname[FILENAME_MAX];
202         char *query = NULL;
203         const char *db_path = tzplatform_getenv(TZ_SYS_DB);
204         if (db_path == NULL) {
205                 NOTIFICATION_ERR("fail to get db_path"); /* LCOV_EXCL_LINE */
206                 return NOTIFICATION_ERROR_OUT_OF_MEMORY;
207         }
208         snprintf(defname, sizeof(defname), "%s/%s", db_path, NOTIFICATION_DB_NAME);
209
210         NOTIFICATION_DBG("db path : %s", defname);
211         r = sqlite3_open_v2(defname, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
212         if (r) {
213                 /* LCOV_EXCL_START */
214                 db_util_close(db);
215                 NOTIFICATION_ERR("fail to open notification db %d", r);
216                 return NOTIFICATION_ERROR_IO_ERROR;
217                 /* LCOV_EXCL_STOP */
218         }
219         query = sqlite3_mprintf(CREATE_NOTIFICATION_TABLE, tzplatform_getuid(TZ_SYS_DEFAULT_USER));
220         NOTIFICATION_DBG("@@@ query : %s", query);
221
222         r = sqlite3_exec(db, CREATE_NOTIFICATION_TABLE, NULL, NULL, &errmsg);
223         if (query)
224                 sqlite3_free(query);
225         if (r != SQLITE_OK) {
226                 /* LCOV_EXCL_START */
227                 NOTIFICATION_ERR("query error(%d)(%s)", r, errmsg);
228                 sqlite3_free(errmsg);
229                 db_util_close(db);
230                 return NOTIFICATION_ERROR_IO_ERROR;
231                 /* LCOV_EXCL_STOP */
232         }
233
234         db_util_close(db);
235         return NOTIFICATION_ERROR_NONE;
236 }
237
238 sqlite3 *notification_db_open(const char *dbfile)
239 {
240         int ret = 0;
241         sqlite3 *db = 0;
242
243         ret = db_util_open(dbfile, &db, 0);
244         if (ret != SQLITE_OK) {
245                 /* LCOV_EXCL_START */
246                 if (ret == SQLITE_PERM)
247                         set_last_result(NOTIFICATION_ERROR_PERMISSION_DENIED);
248                 else
249                         set_last_result(NOTIFICATION_ERROR_FROM_DB);
250
251                 return NULL;
252                 /* LCOV_EXCL_STOP */
253         }
254
255         return db;
256 }
257
258 int notification_db_close(sqlite3 **db)
259 {
260         int ret = 0;
261
262         if (db == NULL || *db == NULL)
263                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
264
265         ret = db_util_close(*db);
266         if (ret != SQLITE_OK) {
267                 /* LCOV_EXCL_START */
268                 NOTIFICATION_ERR("DB close error(%d)", ret);
269                 return NOTIFICATION_ERROR_FROM_DB;
270                 /* LCOV_EXCL_STOP */
271         }
272
273         *db = NULL;
274
275         return NOTIFICATION_ERROR_NONE;
276 }
277
278 int notification_db_exec(sqlite3 *db, const char *query, int *num_changes)
279 {
280         int ret = 0;
281         sqlite3_stmt *stmt = NULL;
282
283         if (db == NULL)
284                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
285
286         if (query == NULL)
287                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
288
289         ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
290         if (ret != SQLITE_OK) {
291                 /* LCOV_EXCL_START */
292                 NOTIFICATION_ERR("DB err(%d) : %s", ret,
293                                  sqlite3_errmsg(db));
294                 return NOTIFICATION_ERROR_FROM_DB;
295                 /* LCOV_EXCL_STOP */
296         }
297
298         if (stmt != NULL) {
299                 ret = sqlite3_step(stmt);
300                 if (ret == SQLITE_OK || ret == SQLITE_DONE) {
301                         if (num_changes != NULL)
302                                 *num_changes = sqlite3_changes(db);
303
304                         sqlite3_finalize(stmt);
305                 } else {
306                         /* LCOV_EXCL_START */
307                         NOTIFICATION_ERR("DB err(%d) : %s", ret,
308                                          sqlite3_errmsg(db));
309                         sqlite3_finalize(stmt);
310                         return NOTIFICATION_ERROR_FROM_DB;
311                         /* LCOV_EXCL_STOP */
312                 }
313         } else {
314                         return NOTIFICATION_ERROR_FROM_DB;
315         }
316
317         return NOTIFICATION_ERROR_NONE;
318 }
319
320 char *notification_db_column_text(sqlite3_stmt * stmt, int col)
321 {
322         const unsigned char *col_text = NULL;
323
324         col_text = sqlite3_column_text(stmt, col);
325         if (col_text == NULL || col_text[0] == '\0')
326                 return NULL;
327
328         return strdup((char *)col_text);
329 }
330
331 bundle *notification_db_column_bundle(sqlite3_stmt * stmt, int col)
332 {
333         const unsigned char *col_bundle = NULL;
334
335         col_bundle = sqlite3_column_text(stmt, col);
336         if (col_bundle == NULL || col_bundle[0] == '\0')
337                 return NULL;
338
339         return bundle_decode(col_bundle, strlen((char *)col_bundle));
340 }
341