def52a5447007aebf9edb466600711f236d6baaa
[platform/core/appfw/badge.git] / src / badge_setting.c
1 /*
2  *  libbadge
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Youngjoo Park <yjoo93.park@samsung.com>,
7  *      Seungtaek Chung <seungtaek.chung@samsung.com>, Youngsub Ko <ys4610.ko@samsung.com>
8  *
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
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
21  */
22
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <sqlite3.h>
26 #include <db-util.h>
27
28 #include "badge.h"
29 #include "badge_log.h"
30 #include "badge_error.h"
31 #include "badge_internal.h"
32 #include "badge_ipc.h"
33 #include "badge_db.h"
34
35 #define SETTING_DB_TABLE "notification_setting"
36 #define SETTING_DB_FILE "/opt/usr/dbspace/.notification_parser.db"
37
38 struct prop_table {
39         const char *property;
40         const char *column;
41         const char *default_value;
42 };
43
44 static struct prop_table g_prop_table[] = {
45                 {
46                         .property = "OPT_BADGE",
47                         .column = "badge",
48                         .default_value = "ON",
49                 },
50                 {
51                         .property = NULL,
52                         .column = NULL,
53                         .default_value = NULL,
54                 }
55 };
56
57 static const char *_get_prop_column(const char *property)
58 {
59         int i;
60
61         for (i = 0; g_prop_table[i].property; i++) {
62                 if (strcmp(g_prop_table[i].property, property))
63                         continue;
64
65                 return g_prop_table[i].column;
66         }
67
68         return NULL;
69 }
70
71 #ifdef TBD
72 static const char *_get_prop_default_value(const char *property)
73 {
74         int i;
75
76         for (i = 0; g_prop_table[i].property; i++) {
77                 if (strcmp(g_prop_table[i].property, property))
78                         continue;
79
80                 return g_prop_table[i].default_value;
81         }
82
83         return NULL;
84 }
85 #endif
86
87 static int _is_record_exist(const char *pkgname, sqlite3 *db)
88 {
89         sqlite3_stmt *stmt = NULL;
90         int count = 0;
91         int result = BADGE_ERROR_NONE;
92         char *sqlbuf = NULL;
93         int sqlret;
94
95         if (!pkgname)
96                 return BADGE_ERROR_INVALID_PARAMETER;
97
98         if (!db)
99                 return BADGE_ERROR_INVALID_PARAMETER;
100
101         sqlbuf = sqlite3_mprintf("SELECT count(*) FROM %s WHERE " \
102                          "appid = %Q",
103                          SETTING_DB_TABLE, pkgname);
104
105         if (!sqlbuf) {
106                 ERR("fail to alloc sql query");
107                 return BADGE_ERROR_OUT_OF_MEMORY;
108         }
109
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;
116         }
117
118         sqlret = sqlite3_step(stmt);
119         if (sqlret == SQLITE_ROW)
120                 count = sqlite3_column_int(stmt, 0);
121         else
122                 count = 0;
123
124         if (count > 0)
125                 result = BADGE_ERROR_ALREADY_EXIST;
126         else
127                 result = BADGE_ERROR_NOT_EXIST;
128
129 free_and_return:
130         if (sqlbuf)
131                 sqlite3_free(sqlbuf);
132
133         if (stmt)
134                 sqlite3_finalize(stmt);
135
136         return result;
137 }
138
139 EXPORT_API int badge_setting_db_set(const char *pkgname, const char *property, const char *value)
140 {
141         int ret = BADGE_ERROR_NONE;
142         int result = BADGE_ERROR_NONE;
143         sqlite3 *db = NULL;
144         char *sqlbuf = NULL;
145         int sqlret;
146         const char *column = NULL;
147
148         if (!pkgname)
149                 return BADGE_ERROR_INVALID_PARAMETER;
150
151         if (!property)
152                 return BADGE_ERROR_INVALID_PARAMETER;
153
154         if (!value)
155                 return BADGE_ERROR_INVALID_PARAMETER;
156
157         column = _get_prop_column(property);
158         if (!column)
159                 return BADGE_ERROR_INVALID_PARAMETER;
160
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;
165         }
166
167         ret = _is_record_exist(pkgname, db);
168         if (ret != BADGE_ERROR_ALREADY_EXIST) {
169                 result = ret;
170                 goto return_close_db;
171         }
172
173         sqlbuf = sqlite3_mprintf("UPDATE %s SET %s = %Q " \
174                         "WHERE appid = %Q",
175                         SETTING_DB_TABLE, column, value, pkgname);
176         if (!sqlbuf) {
177                 ERR("fail to alloc query");
178                 result = BADGE_ERROR_OUT_OF_MEMORY;
179                 goto return_close_db;
180         }
181
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);
186                 result = ret;
187                 goto return_close_db;
188         }
189
190 return_close_db:
191         if (sqlbuf)
192                 sqlite3_free(sqlbuf);
193
194         sqlret = db_util_close(db);
195         if (sqlret != SQLITE_OK)
196                 WARN("fail to db_util_close - [%d]", sqlret);
197
198         return result;
199 }
200
201 EXPORT_API int badge_setting_db_get(const char *pkgname, const char *property, char **value)
202 {
203         int ret = BADGE_ERROR_NONE;
204         int result = BADGE_ERROR_NONE;
205         sqlite3 *db = NULL;
206         char *sqlbuf = NULL;
207         sqlite3_stmt *stmt = NULL;
208         int sqlret;
209         const char *column = NULL;
210
211         if (!pkgname)
212                 return BADGE_ERROR_INVALID_PARAMETER;
213
214         if (!property)
215                 return BADGE_ERROR_INVALID_PARAMETER;
216
217         if (!value)
218                 return BADGE_ERROR_INVALID_PARAMETER;
219
220         column = _get_prop_column(property);
221         if (!column)
222                 return BADGE_ERROR_INVALID_PARAMETER;
223
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;
228         }
229
230         ret = _is_record_exist(pkgname, db);
231         if (ret != BADGE_ERROR_ALREADY_EXIST) {
232                 result = ret;
233                 goto return_close_db;
234         }
235
236         sqlbuf = sqlite3_mprintf("SELECT %s FROM %s " \
237                         "WHERE appid = %Q",
238                         column, SETTING_DB_TABLE, pkgname);
239         if (!sqlbuf) {
240                 ERR("fail to alloc query");
241                 result = BADGE_ERROR_OUT_OF_MEMORY;
242                 goto return_close_db;
243         }
244
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;
251         }
252
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';
261                         *value = get_data;
262                 } else {
263                         ERR("fail to alloc query");
264                         result = BADGE_ERROR_OUT_OF_MEMORY;
265                         goto return_close_db;
266                 }
267         }
268
269 return_close_db:
270         if (sqlbuf)
271                 sqlite3_free(sqlbuf);
272
273         if (stmt)
274                 sqlite3_finalize(stmt);
275
276         sqlret = db_util_close(db);
277         if (sqlret != SQLITE_OK)
278                 WARN("fail to db_util_close - [%d]", sqlret);
279
280         return result;
281 }
282
283 EXPORT_API int badge_setting_property_set(const char *pkgname, const char *property, const char *value)
284 {
285         int ret = 0;
286
287         if (!pkgname)
288                 return BADGE_ERROR_INVALID_PARAMETER;
289
290         if (!property)
291                 return BADGE_ERROR_INVALID_PARAMETER;
292
293         if (!value)
294                 return BADGE_ERROR_INVALID_PARAMETER;
295
296         ret = badge_ipc_setting_property_set(pkgname, property, value);
297         if (ret != BADGE_ERROR_NONE) {
298                 return ret;
299         }
300
301         return BADGE_ERROR_NONE;
302 }
303
304 EXPORT_API int badge_setting_property_get(const char *pkgname, const char *property, char **value)
305 {
306         int ret = 0;
307
308         if (!pkgname)
309                 return BADGE_ERROR_INVALID_PARAMETER;
310
311         if (!property)
312                 return BADGE_ERROR_INVALID_PARAMETER;
313
314         if (!value)
315                 return BADGE_ERROR_INVALID_PARAMETER;
316
317         ret = badge_ipc_setting_property_get(pkgname, property, value);
318         if (ret != BADGE_ERROR_NONE) {
319                 return ret;
320         }
321
322         return BADGE_ERROR_NONE;
323 }