4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Seungtaek Chung <seungtaek.chung@samsung.com>, Mi-Ju Lee <miju52.lee@samsung.com>, Xi Zhichan <zhichan.xi@samsung.com>, Youngsub Ko <ys4610.ko@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
27 #include <notification.h>
28 #include <notification_db.h>
29 #include <notification_noti.h>
30 #include <notification_debug.h>
31 #include <notification_ipc.h>
32 #include <notification_setting.h>
33 #include <notification_internal.h>
34 #include <tzplatform_config.h>
36 #define NOTIFICATION_SETTING_DB "notification_setting"
37 #define NOTIFICATION_SETTING_DB_PATH tzplatform_mkpath(TZ_USER_DB,".notification_parser.db")
39 struct _notification_setting_h {
51 const char *default_value;
54 static struct prop_table g_prop_table[] = {
56 .property = "OPT_NOTIFICATION",
57 .column = "notification",
58 .default_value = "ON",
61 .property = "OPT_SOUNDS",
63 .default_value = "ON",
66 .property = "OPT_CONTENTS",
68 .default_value = "ON",
71 .property = "OPT_BADGE",
73 .default_value = "ON",
78 .default_value = NULL,
82 static const char *_get_prop_column(const char *property)
86 for (i = 0; g_prop_table[i].property; i++) {
87 if (strcmp(g_prop_table[i].property, property))
90 return g_prop_table[i].column;
97 static const char *_get_prop_default_value(const char *property)
101 for (i = 0; g_prop_table[i].property; i++) {
102 if (strcmp(g_prop_table[i].property, property))
105 return g_prop_table[i].default_value;
112 static notification_error_e _is_record_exist(const char *pkgname, sqlite3 *db)
114 sqlite3_stmt *stmt = NULL;
116 int result = NOTIFICATION_ERROR_NONE;
121 return NOTIFICATION_ERROR_INVALID_DATA;
124 return NOTIFICATION_ERROR_INVALID_DATA;
126 sqlbuf = sqlite3_mprintf("SELECT count(*) FROM %s WHERE " \
128 NOTIFICATION_SETTING_DB, pkgname);
131 NOTIFICATION_ERR("fail to alloc sql query");
132 return NOTIFICATION_ERROR_NO_MEMORY;
135 sqlret = sqlite3_prepare_v2(db, sqlbuf, -1, &stmt, NULL);
136 if (sqlret != SQLITE_OK) {
137 NOTIFICATION_ERR("DB err [%s]", sqlite3_errmsg(db));
138 NOTIFICATION_ERR("query[%s]", sqlbuf);
139 result = NOTIFICATION_ERROR_FROM_DB;
140 goto free_and_return;
143 sqlret = sqlite3_step(stmt);
144 if (sqlret == SQLITE_ROW)
145 count = sqlite3_column_int(stmt, 0);
150 result = NOTIFICATION_ERROR_ALREADY_EXIST_ID;
152 result = NOTIFICATION_ERROR_NOT_EXIST_ID;
156 sqlite3_free(sqlbuf);
159 sqlite3_finalize(stmt);
164 EXPORT_API notification_error_e notification_setting_db_set(const char *pkgname, const char *property, const char *value)
166 notification_error_e ret = NOTIFICATION_ERROR_NONE;
167 notification_error_e result = NOTIFICATION_ERROR_NONE;
171 const char *column = NULL;
174 return NOTIFICATION_ERROR_INVALID_DATA;
177 return NOTIFICATION_ERROR_INVALID_DATA;
180 return NOTIFICATION_ERROR_INVALID_DATA;
182 column = _get_prop_column(property);
184 return NOTIFICATION_ERROR_INVALID_DATA;
186 sqlret = db_util_open(NOTIFICATION_SETTING_DB_PATH, &db, 0);
187 if (sqlret != SQLITE_OK || !db) {
188 NOTIFICATION_ERR("fail to db_util_open - [%d]", sqlret);
189 return NOTIFICATION_ERROR_FROM_DB;
192 ret = _is_record_exist(pkgname, db);
193 if (ret != NOTIFICATION_ERROR_ALREADY_EXIST_ID) {
195 goto return_close_db;
198 sqlbuf = sqlite3_mprintf("UPDATE %s SET %s = %Q " \
200 NOTIFICATION_SETTING_DB, column, value, pkgname);
202 NOTIFICATION_ERR("fail to alloc query");
203 result = NOTIFICATION_ERROR_NO_MEMORY;
204 goto return_close_db;
207 result = notification_db_exec(db, sqlbuf, NULL);
211 sqlite3_free(sqlbuf);
213 sqlret = db_util_close(db);
214 if (sqlret != SQLITE_OK) {
215 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
221 EXPORT_API notification_error_e notification_setting_db_get(const char *pkgname, const char *property, char **value)
223 notification_error_e ret = NOTIFICATION_ERROR_NONE;
224 notification_error_e result = NOTIFICATION_ERROR_NONE;
227 sqlite3_stmt *stmt = NULL;
229 const char *column = NULL;
232 return NOTIFICATION_ERROR_INVALID_DATA;
235 return NOTIFICATION_ERROR_INVALID_DATA;
238 return NOTIFICATION_ERROR_INVALID_DATA;
240 column = _get_prop_column(property);
242 return NOTIFICATION_ERROR_INVALID_DATA;
244 sqlret = db_util_open(NOTIFICATION_SETTING_DB_PATH, &db, 0);
245 if (sqlret != SQLITE_OK || !db) {
246 NOTIFICATION_ERR("fail to db_util_open - [%d]", sqlret);
247 return NOTIFICATION_ERROR_FROM_DB;
250 ret = _is_record_exist(pkgname, db);
251 if (ret != NOTIFICATION_ERROR_ALREADY_EXIST_ID) {
253 goto return_close_db;
256 sqlbuf = sqlite3_mprintf("SELECT %s FROM %s " \
258 column, NOTIFICATION_SETTING_DB, pkgname);
260 NOTIFICATION_ERR("fail to alloc query");
261 result = NOTIFICATION_ERROR_NO_MEMORY;
262 goto return_close_db;
265 sqlret = sqlite3_prepare_v2(db, sqlbuf, -1, &stmt, NULL);
266 if (sqlret != SQLITE_OK) {
267 NOTIFICATION_ERR("fail to prepare %s - [%s]",
268 sqlbuf, sqlite3_errmsg(db));
269 result = NOTIFICATION_ERROR_FROM_DB;
270 goto return_close_db;
273 sqlret = sqlite3_step(stmt);
274 if (sqlret == SQLITE_ROW) {
275 int get_bytes = sqlite3_column_bytes(stmt, 0);
276 char *get_data = (char *)calloc(get_bytes + 1, sizeof(char));
277 if (get_data != NULL) {
278 memcpy(get_data, sqlite3_column_text(stmt, 0),
279 get_bytes * sizeof(char));
280 get_data[get_bytes] = '\0';
283 NOTIFICATION_ERR("fail to alloc query");
284 result = NOTIFICATION_ERROR_NO_MEMORY;
285 goto return_close_db;
291 sqlite3_free(sqlbuf);
294 sqlite3_finalize(stmt);
296 sqlret = db_util_close(db);
297 if (sqlret != SQLITE_OK)
298 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
303 EXPORT_API notification_error_e notification_setting_property_set(const char *pkgname, const char *property, const char *value)
308 return NOTIFICATION_ERROR_INVALID_DATA;
311 return NOTIFICATION_ERROR_INVALID_DATA;
314 return NOTIFICATION_ERROR_INVALID_DATA;
316 ret = notification_ipc_noti_setting_property_set(pkgname, property, value);
317 if (ret != NOTIFICATION_ERROR_NONE) {
321 return NOTIFICATION_ERROR_NONE;
324 EXPORT_API notification_error_e notification_setting_property_get(const char *pkgname, const char *property, char **value)
329 return NOTIFICATION_ERROR_INVALID_DATA;
332 return NOTIFICATION_ERROR_INVALID_DATA;
335 return NOTIFICATION_ERROR_INVALID_DATA;
337 ret = notification_ipc_noti_setting_property_get(pkgname, property, value);
338 if (ret != NOTIFICATION_ERROR_NONE) {
342 return NOTIFICATION_ERROR_NONE;