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