summaryrefslogtreecommitdiff
path: root/src/notification_db.c
blob: 3b5c2c99b9057fe1dfaf01ab6c0ed9023112d8d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

#include <sqlite3.h>
#include <db-util.h>
#include <tizen.h>
#include <tzplatform_config.h>

#include <notification_error.h>
#include <notification_debug.h>
#include <notification_db.h>

#define NOTIFICATION_DB_NAME ".notification.db"
#define CREATE_NOTIFICATION_TABLE " \
PRAGMA journal_mode = PERSIST; \
PRAGMA synchronous = FULL; \
create	table if not exists noti_list ( \
			type INTEGER NOT NULL, \
			layout INTEGER NOT NULL default 0, \
			caller_pkgname TEXT NOT NULL, \
			launch_pkgname TEXT, \
			image_path TEXT, \
			group_id INTEGER default 0,  \
			internal_group_id INTEGER default 0,  \
			priv_id INTEGER PRIMARY KEY AUTOINCREMENT,  \
			title_key TEXT, \
			b_text TEXT, \
			b_key TEXT, \
			tag TEXT, \
			b_format_args TEXT, \
			num_format_args INTEGER default 0, \
			text_domain TEXT, \
			text_dir TEXT, \
			time INTEGER default 0, \
			insert_time INTEGER default 0, \
			args TEXT, \
			group_args TEXT, \
			b_execute_option TEXT, \
			b_service_responding TEXT, \
			b_service_single_launch TEXT, \
			b_service_multi_launch TEXT, \
			b_event_handler_click_on_button_1 TEXT, \
			b_event_handler_click_on_button_2 TEXT, \
			b_event_handler_click_on_button_3 TEXT, \
			b_event_handler_click_on_button_4 TEXT, \
			b_event_handler_click_on_button_5 TEXT, \
			b_event_handler_click_on_button_6 TEXT, \
			b_event_handler_click_on_icon TEXT, \
			b_event_handler_click_on_thumbnail TEXT, \
			sound_type INTEGER default 0, \
			sound_path TEXT, \
			vibration_type INTEGER default 0, \
			vibration_path TEXT, \
			led_operation INTEGER default 0, \
			led_argb INTEGER default 0, \
			led_on_ms INTEGER default -1, \
			led_off_ms INTEGER default -1, \
			flags_for_property INTEGER default 0, \
			flag_simmode INTEGER default 0, \
			display_applist INTEGER, \
			progress_size DOUBLE default 0, \
			progress_percentage DOUBLE default 0, \
			ongoing_flag INTEGER default 0, \
			ongoing_value_type INTEGER default 0, \
			ongoing_current INTEGER default 0, \
			ongoing_duration INTEGER default 0, \
			auto_remove INTEGER default 1, \
			default_button_index INTEGER default 0, \
			timeout INTEGER default 0, \
			text_input_max_length INTEGER default 0, \
			event_flag INTEGER default 0, \
			uid INTEGER \
		); \
		create table if not exists noti_group_data ( \
			caller_pkgname TEXT NOT NULL, \
			group_id INTEGER default 0, \
			badge INTEGER default 0, \
			title TEXT, \
			content TEXT, \
			loc_title TEXT, \
			loc_content TEXT, \
			count_display_title INTEGER, \
			count_display_content INTEGER, \
			rowid INTEGER PRIMARY KEY AUTOINCREMENT, \
			UNIQUE (caller_pkgname, group_id) \
		); \
		create table if not exists ongoing_list ( \
			caller_pkgname TEXT NOT NULL, \
			launch_pkgname TEXT, \
			icon_path TEXT, \
			group_id INTEGER default 0, \
			internal_group_id INTEGER default 0, \
			priv_id INTERGER NOT NULL, \
			title TEXT, \
			content TEXT, \
			default_content TEXT, \
			loc_title TEXT, \
			loc_content TEXT, \
			loc_default_content TEXT, \
			text_domain TEXT, \
			text_dir TEXT, \
			args TEXT, \
			group_args TEXT, \
			flag INTEGER default 0, \
			progress_size DOUBLE default 0, \
			progress_percentage DOUBLE default 0, \
			rowid INTEGER PRIMARY KEY AUTOINCREMENT, \
			UNIQUE (caller_pkgname, priv_id) \
		); \
		CREATE TABLE IF NOT EXISTS notification_setting ( \
			uid INTEGER, \
			package_name TEXT NOT NULL, \
			allow_to_notify INTEGER DEFAULT 1, \
			do_not_disturb_except INTEGER DEFAULT 0, \
			visibility_class INTEGER DEFAULT 0, \
			pop_up_notification INTEGER DEFAULT 1, \
			lock_screen_content_level INTEGER DEFAULT 0, \
			UNIQUE (uid, package_name) \
		); \
		CREATE TABLE IF NOT EXISTS notification_system_setting ( \
			uid INTEGER, \
			do_not_disturb INTEGER DEFAULT 0, \
			visibility_class INTEGER DEFAULT 0, \
			dnd_schedule_enabled INTEGER DEFAULT 0, \
			dnd_schedule_day INTEGER DEFAULT 62, \
			dnd_start_hour INTEGER DEFAULT 22, \
			dnd_start_min INTEGER DEFAULT 0, \
			dnd_end_hour INTEGER DEFAULT 8, \
			dnd_end_min INTEGER DEFAULT 0, \
			lock_screen_content_level INTEGER DEFAULT 0, \
			UNIQUE (uid) \
		); \
		CREATE TABLE IF NOT EXISTS dnd_allow_exception ( \
			uid INTEGER, \
			type INTEGER DEFAULT 0, \
			value INTEGER DEFAULT 0, \
			UNIQUE (uid, type) \
		); \
		CREATE TABLE IF NOT EXISTS noti_template ( \
			type INTEGER NOT NULL, \
			layout INTEGER NOT NULL default 0, \
			caller_pkgname TEXT NOT NULL, \
			launch_pkgname TEXT, \
			image_path TEXT, \
			group_id INTEGER default 0,  \
			internal_group_id INTEGER default 0,  \
			priv_id INTEGER PRIMARY KEY AUTOINCREMENT,  \
			title_key TEXT, \
			b_text TEXT, \
			b_key TEXT, \
			tag TEXT, \
			b_format_args TEXT, \
			num_format_args INTEGER default 0, \
			text_domain TEXT, \
			text_dir TEXT, \
			time INTEGER default 0, \
			insert_time INTEGER default 0, \
			args TEXT, \
			group_args TEXT, \
			b_execute_option TEXT, \
			b_service_responding TEXT, \
			b_service_single_launch TEXT, \
			b_service_multi_launch TEXT, \
			b_event_handler_click_on_button_1 TEXT, \
			b_event_handler_click_on_button_2 TEXT, \
			b_event_handler_click_on_button_3 TEXT, \
			b_event_handler_click_on_button_4 TEXT, \
			b_event_handler_click_on_button_5 TEXT, \
			b_event_handler_click_on_button_6 TEXT, \
			b_event_handler_click_on_icon TEXT, \
			b_event_handler_click_on_thumbnail TEXT, \
			sound_type INTEGER default 0, \
			sound_path TEXT, \
			vibration_type INTEGER default 0, \
			vibration_path TEXT, \
			led_operation INTEGER default 0, \
			led_argb INTEGER default 0, \
			led_on_ms INTEGER default -1, \
			led_off_ms INTEGER default -1, \
			flags_for_property INTEGER default 0, \
			flag_simmode INTEGER default 0, \
			display_applist INTEGER, \
			progress_size DOUBLE default 0, \
			progress_percentage DOUBLE default 0, \
			ongoing_flag INTEGER default 0, \
			ongoing_value_type INTEGER default 0, \
			ongoing_current INTEGER default 0, \
			ongoing_duration INTEGER default 0, \
			auto_remove INTEGER default 1, \
			default_button_index INTEGER default 0, \
			timeout INTEGER default 0, \
			text_input_max_length INTEGER default 0, \
			event_flag INTEGER default 0, \
			uid INTEGER, \
			template_name TEXT, \
			UNIQUE (caller_pkgname, template_name) \
		);"


EXPORT_API int notification_db_init()
{
	int r;
	sqlite3 *db = NULL;
	char *errmsg = NULL;
	char defname[FILENAME_MAX];
	char *query = NULL;
	const char *db_path = tzplatform_getenv(TZ_SYS_DB);
	if (db_path == NULL) {
		NOTIFICATION_ERR("fail to get db_path"); /* LCOV_EXCL_LINE */
		return NOTIFICATION_ERROR_OUT_OF_MEMORY;
	}
	snprintf(defname, sizeof(defname), "%s/%s", db_path, NOTIFICATION_DB_NAME);

	NOTIFICATION_DBG("db path : %s", defname);
	r = sqlite3_open_v2(defname, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
	if (r) {
		/* LCOV_EXCL_START */
		db_util_close(db);
		NOTIFICATION_ERR("fail to open notification db %d", r);
		return NOTIFICATION_ERROR_IO_ERROR;
		/* LCOV_EXCL_STOP */
	}
	query = sqlite3_mprintf(CREATE_NOTIFICATION_TABLE, tzplatform_getuid(TZ_SYS_DEFAULT_USER));
	NOTIFICATION_DBG("@@@ query : %s", query);

	r = sqlite3_exec(db, CREATE_NOTIFICATION_TABLE, NULL, NULL, &errmsg);
	if (query)
		sqlite3_free(query);
	if (r != SQLITE_OK) {
		/* LCOV_EXCL_START */
		NOTIFICATION_ERR("query error(%d)(%s)", r, errmsg);
		sqlite3_free(errmsg);
		db_util_close(db);
		return NOTIFICATION_ERROR_IO_ERROR;
		/* LCOV_EXCL_STOP */
	}

	db_util_close(db);
	return NOTIFICATION_ERROR_NONE;
}

sqlite3 *notification_db_open(const char *dbfile)
{
	int ret = 0;
	sqlite3 *db = 0;

	ret = db_util_open(dbfile, &db, 0);
	if (ret != SQLITE_OK) {
		/* LCOV_EXCL_START */
		if (ret == SQLITE_PERM)
			set_last_result(NOTIFICATION_ERROR_PERMISSION_DENIED);
		else
			set_last_result(NOTIFICATION_ERROR_FROM_DB);

		return NULL;
		/* LCOV_EXCL_STOP */
	}

	return db;
}

int notification_db_close(sqlite3 **db)
{
	int ret = 0;

	if (db == NULL || *db == NULL)
		return NOTIFICATION_ERROR_INVALID_PARAMETER;

	ret = db_util_close(*db);
	if (ret != SQLITE_OK) {
		/* LCOV_EXCL_START */
		NOTIFICATION_ERR("DB close error(%d)", ret);
		return NOTIFICATION_ERROR_FROM_DB;
		/* LCOV_EXCL_STOP */
	}

	*db = NULL;

	return NOTIFICATION_ERROR_NONE;
}

int notification_db_exec(sqlite3 *db, const char *query, int *num_changes)
{
	int ret = 0;
	sqlite3_stmt *stmt = NULL;

	if (db == NULL)
		return NOTIFICATION_ERROR_INVALID_PARAMETER;

	if (query == NULL)
		return NOTIFICATION_ERROR_INVALID_PARAMETER;

	ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
	if (ret != SQLITE_OK) {
		/* LCOV_EXCL_START */
		NOTIFICATION_ERR("DB err(%d) : %s", ret,
				 sqlite3_errmsg(db));
		return NOTIFICATION_ERROR_FROM_DB;
		/* LCOV_EXCL_STOP */
	}

	if (stmt != NULL) {
		ret = sqlite3_step(stmt);
		if (ret == SQLITE_OK || ret == SQLITE_DONE) {
			if (num_changes != NULL)
				*num_changes = sqlite3_changes(db);

			sqlite3_finalize(stmt);
		} else {
			/* LCOV_EXCL_START */
			NOTIFICATION_ERR("DB err(%d) : %s", ret,
					 sqlite3_errmsg(db));
			sqlite3_finalize(stmt);
			return NOTIFICATION_ERROR_FROM_DB;
			/* LCOV_EXCL_STOP */
		}
	} else {
			return NOTIFICATION_ERROR_FROM_DB;
	}

	return NOTIFICATION_ERROR_NONE;
}

char *notification_db_column_text(sqlite3_stmt * stmt, int col)
{
	const unsigned char *col_text = NULL;

	col_text = sqlite3_column_text(stmt, col);
	if (col_text == NULL || col_text[0] == '\0')
		return NULL;

	return strdup((char *)col_text);
}

bundle *notification_db_column_bundle(sqlite3_stmt * stmt, int col)
{
	const unsigned char *col_bundle = NULL;

	col_bundle = sqlite3_column_text(stmt, col);
	if (col_bundle == NULL || col_bundle[0] == '\0')
		return NULL;

	return bundle_decode(col_bundle, strlen((char *)col_bundle));
}