4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Youngjoo Park <yjoo93.park@samsung.com>,
7 * Seungtaek Chung <seungtaek.chung@samsung.com>, Youngsub Ko <ys4610.ko@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the License);
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an AS IS BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
29 #include "badge_log.h"
30 #include "badge_error.h"
31 #include "badge_internal.h"
32 #include "badge_ipc.h"
35 #define SETTING_DB_TABLE "notification_setting"
36 #define SETTING_DB_FILE "/opt/usr/dbspace/.notification_parser.db"
41 const char *default_value;
44 static struct prop_table g_prop_table[] = {
46 .property = "OPT_BADGE",
48 .default_value = "ON",
53 .default_value = NULL,
57 static const char *_get_prop_column(const char *property)
61 for (i = 0; g_prop_table[i].property; i++) {
62 if (strcmp(g_prop_table[i].property, property))
65 return g_prop_table[i].column;
72 static const char *_get_prop_default_value(const char *property)
76 for (i = 0; g_prop_table[i].property; i++) {
77 if (strcmp(g_prop_table[i].property, property))
80 return g_prop_table[i].default_value;
87 static int _is_record_exist(const char *pkgname, sqlite3 *db)
89 sqlite3_stmt *stmt = NULL;
91 int result = BADGE_ERROR_NONE;
96 return BADGE_ERROR_INVALID_PARAMETER;
99 return BADGE_ERROR_INVALID_PARAMETER;
101 sqlbuf = sqlite3_mprintf("SELECT count(*) FROM %s WHERE " \
103 SETTING_DB_TABLE, pkgname);
106 ERR("fail to alloc sql query");
107 return BADGE_ERROR_OUT_OF_MEMORY;
110 sqlret = sqlite3_prepare_v2(db, sqlbuf, -1, &stmt, NULL);
111 if (sqlret != SQLITE_OK) {
112 ERR("DB err [%s]", sqlite3_errmsg(db));
113 ERR("query[%s]", sqlbuf);
114 result = BADGE_ERROR_FROM_DB;
115 goto free_and_return;
118 sqlret = sqlite3_step(stmt);
119 if (sqlret == SQLITE_ROW)
120 count = sqlite3_column_int(stmt, 0);
125 result = BADGE_ERROR_ALREADY_EXIST;
127 result = BADGE_ERROR_NOT_EXIST;
131 sqlite3_free(sqlbuf);
134 sqlite3_finalize(stmt);
139 EXPORT_API int badge_setting_db_set(const char *pkgname, const char *property, const char *value)
141 int ret = BADGE_ERROR_NONE;
142 int result = BADGE_ERROR_NONE;
146 const char *column = NULL;
149 return BADGE_ERROR_INVALID_PARAMETER;
152 return BADGE_ERROR_INVALID_PARAMETER;
155 return BADGE_ERROR_INVALID_PARAMETER;
157 column = _get_prop_column(property);
159 return BADGE_ERROR_INVALID_PARAMETER;
161 sqlret = db_util_open(SETTING_DB_FILE, &db, 0);
162 if (sqlret != SQLITE_OK || !db) {
163 ERR("fail to db_util_open - [%d]", sqlret);
164 return BADGE_ERROR_FROM_DB;
167 ret = _is_record_exist(pkgname, db);
168 if (ret != BADGE_ERROR_ALREADY_EXIST) {
170 goto return_close_db;
173 sqlbuf = sqlite3_mprintf("UPDATE %s SET %s = %Q " \
175 SETTING_DB_TABLE, column, value, pkgname);
177 ERR("fail to alloc query");
178 result = BADGE_ERROR_OUT_OF_MEMORY;
179 goto return_close_db;
182 ret = badge_db_exec(db, sqlbuf, NULL);
183 if (ret != BADGE_ERROR_NONE) {
184 ERR("failed to set pkgname[%s] option[%s], value[%s], err[%d]",
185 pkgname, column, value, ret);
187 goto return_close_db;
192 sqlite3_free(sqlbuf);
194 sqlret = db_util_close(db);
195 if (sqlret != SQLITE_OK)
196 WARN("fail to db_util_close - [%d]", sqlret);
201 EXPORT_API int badge_setting_db_get(const char *pkgname, const char *property, char **value)
203 int ret = BADGE_ERROR_NONE;
204 int result = BADGE_ERROR_NONE;
207 sqlite3_stmt *stmt = NULL;
209 const char *column = NULL;
212 return BADGE_ERROR_INVALID_PARAMETER;
215 return BADGE_ERROR_INVALID_PARAMETER;
218 return BADGE_ERROR_INVALID_PARAMETER;
220 column = _get_prop_column(property);
222 return BADGE_ERROR_INVALID_PARAMETER;
224 sqlret = db_util_open(SETTING_DB_FILE, &db, 0);
225 if (sqlret != SQLITE_OK || !db) {
226 ERR("fail to db_util_open - [%d]", sqlret);
227 return BADGE_ERROR_FROM_DB;
230 ret = _is_record_exist(pkgname, db);
231 if (ret != BADGE_ERROR_ALREADY_EXIST) {
233 goto return_close_db;
236 sqlbuf = sqlite3_mprintf("SELECT %s FROM %s " \
238 column, SETTING_DB_TABLE, pkgname);
240 ERR("fail to alloc query");
241 result = BADGE_ERROR_OUT_OF_MEMORY;
242 goto return_close_db;
245 sqlret = sqlite3_prepare_v2(db, sqlbuf, -1, &stmt, NULL);
246 if (sqlret != SQLITE_OK) {
247 ERR("fail to prepare %s - [%s]",
248 sqlbuf, sqlite3_errmsg(db));
249 result = BADGE_ERROR_FROM_DB;
250 goto return_close_db;
253 sqlret = sqlite3_step(stmt);
254 if (sqlret == SQLITE_ROW) {
255 int get_bytes = sqlite3_column_bytes(stmt, 0);
256 char *get_data = (char *)calloc(get_bytes + 1, sizeof(char));
257 if (get_data != NULL) {
258 memcpy(get_data, sqlite3_column_text(stmt, 0),
259 get_bytes * sizeof(char));
260 get_data[get_bytes] = '\0';
263 ERR("fail to alloc query");
264 result = BADGE_ERROR_OUT_OF_MEMORY;
265 goto return_close_db;
271 sqlite3_free(sqlbuf);
274 sqlite3_finalize(stmt);
276 sqlret = db_util_close(db);
277 if (sqlret != SQLITE_OK)
278 WARN("fail to db_util_close - [%d]", sqlret);
283 EXPORT_API int badge_setting_property_set(const char *pkgname, const char *property, const char *value)
288 return BADGE_ERROR_INVALID_PARAMETER;
291 return BADGE_ERROR_INVALID_PARAMETER;
294 return BADGE_ERROR_INVALID_PARAMETER;
296 ret = badge_ipc_setting_property_set(pkgname, property, value);
297 if (ret != BADGE_ERROR_NONE) {
301 return BADGE_ERROR_NONE;
304 EXPORT_API int badge_setting_property_get(const char *pkgname, const char *property, char **value)
309 return BADGE_ERROR_INVALID_PARAMETER;
312 return BADGE_ERROR_INVALID_PARAMETER;
315 return BADGE_ERROR_INVALID_PARAMETER;
317 ret = badge_ipc_setting_property_get(pkgname, property, value);
318 if (ret != BADGE_ERROR_NONE) {
322 return BADGE_ERROR_NONE;