69aca512275d9f04420adb15244100a54a89e7f5
[platform/core/api/notification.git] / src / notification_setting.c
1 /*
2  *  libnotification
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <db-util.h>
26
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>
35
36 #define NOTIFICATION_SETTING_DB "notification_setting"
37 #define NOTIFICATION_SETTING_DB_PATH tzplatform_mkpath(TZ_USER_DB,".notification_parser.db")
38
39 struct _notification_setting_h {
40         char *appid;
41         char *notification;
42         char *sounds;
43         char *contents;
44         char *badge;
45         char *pkgid;
46 };
47
48 struct prop_table {
49         const char *property;
50         const char *column;
51         const char *default_value;
52 };
53
54 static struct prop_table g_prop_table[] = {
55                 {
56                         .property = "OPT_NOTIFICATION",
57                         .column = "notification",
58                         .default_value = "ON",
59                 },
60                 {
61                         .property = "OPT_SOUNDS",
62                         .column = "sounds",
63                         .default_value = "ON",
64                 },
65                 {
66                         .property = "OPT_CONTENTS",
67                         .column = "contents",
68                         .default_value = "ON",
69                 },
70                 {
71                         .property = "OPT_BADGE",
72                         .column = "badge",
73                         .default_value = "ON",
74                 },
75                 {
76                         .property = NULL,
77                         .column = NULL,
78                         .default_value = NULL,
79                 }
80 };
81
82 static const char *_get_prop_column(const char *property)
83 {
84         int i;
85
86         for (i = 0; g_prop_table[i].property; i++) {
87                 if (strcmp(g_prop_table[i].property, property))
88                         continue;
89
90                 return g_prop_table[i].column;
91         }
92
93         return NULL;
94 }
95
96 #ifdef TBD
97 static const char *_get_prop_default_value(const char *property)
98 {
99         int i;
100
101         for (i = 0; g_prop_table[i].property; i++) {
102                 if (strcmp(g_prop_table[i].property, property))
103                         continue;
104
105                 return g_prop_table[i].default_value;
106         }
107
108         return NULL;
109 }
110 #endif
111
112 static notification_error_e _is_record_exist(const char *pkgname, sqlite3 *db)
113 {
114         sqlite3_stmt *stmt = NULL;
115         int count = 0;
116         int result = NOTIFICATION_ERROR_NONE;
117         char *sqlbuf = NULL;
118         int sqlret;
119
120         if (!pkgname)
121                 return NOTIFICATION_ERROR_INVALID_DATA;
122
123         if (!db)
124                 return NOTIFICATION_ERROR_INVALID_DATA;
125
126         sqlbuf = sqlite3_mprintf("SELECT count(*) FROM %s WHERE " \
127                          "appid = %Q",
128                          NOTIFICATION_SETTING_DB, pkgname);
129
130         if (!sqlbuf) {
131                 NOTIFICATION_ERR("fail to alloc sql query");
132                 return NOTIFICATION_ERROR_NO_MEMORY;
133         }
134
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;
141         }
142
143         sqlret = sqlite3_step(stmt);
144         if (sqlret == SQLITE_ROW)
145                 count = sqlite3_column_int(stmt, 0);
146         else
147                 count = 0;
148
149         if (count > 0)
150                 result = NOTIFICATION_ERROR_ALREADY_EXIST_ID;
151         else
152                 result = NOTIFICATION_ERROR_NOT_EXIST_ID;
153
154 free_and_return:
155         if (sqlbuf)
156                 sqlite3_free(sqlbuf);
157
158         if (stmt)
159                 sqlite3_finalize(stmt);
160
161         return result;
162 }
163
164 EXPORT_API notification_error_e notification_setting_db_set(const char *pkgname, const char *property, const char *value)
165 {
166         notification_error_e ret = NOTIFICATION_ERROR_NONE;
167         notification_error_e result = NOTIFICATION_ERROR_NONE;
168         sqlite3 *db = NULL;
169         char *sqlbuf = NULL;
170         int sqlret;
171         const char *column = NULL;
172
173         if (!pkgname)
174                 return NOTIFICATION_ERROR_INVALID_DATA;
175
176         if (!property)
177                 return NOTIFICATION_ERROR_INVALID_DATA;
178
179         if (!value)
180                 return NOTIFICATION_ERROR_INVALID_DATA;
181
182         column = _get_prop_column(property);
183         if (!column)
184                 return NOTIFICATION_ERROR_INVALID_DATA;
185
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;
190         }
191
192         ret = _is_record_exist(pkgname, db);
193         if (ret != NOTIFICATION_ERROR_ALREADY_EXIST_ID) {
194                 result = ret;
195                 goto return_close_db;
196         }
197
198         sqlbuf = sqlite3_mprintf("UPDATE %s SET %s = %Q " \
199                         "WHERE appid = %Q",
200                         NOTIFICATION_SETTING_DB, column, value, pkgname);
201         if (!sqlbuf) {
202                 NOTIFICATION_ERR("fail to alloc query");
203                 result = NOTIFICATION_ERROR_NO_MEMORY;
204                 goto return_close_db;
205         }
206
207         result = notification_db_exec(db, sqlbuf, NULL);
208
209 return_close_db:
210         if (sqlbuf)
211                 sqlite3_free(sqlbuf);
212
213         sqlret = db_util_close(db);
214         if (sqlret != SQLITE_OK) {
215                 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
216         }
217
218         return result;
219 }
220
221 EXPORT_API notification_error_e notification_setting_db_get(const char *pkgname, const char *property, char **value)
222 {
223         notification_error_e ret = NOTIFICATION_ERROR_NONE;
224         notification_error_e result = NOTIFICATION_ERROR_NONE;
225         sqlite3 *db = NULL;
226         char *sqlbuf = NULL;
227         sqlite3_stmt *stmt = NULL;
228         int sqlret;
229         const char *column = NULL;
230
231         if (!pkgname)
232                 return NOTIFICATION_ERROR_INVALID_DATA;
233
234         if (!property)
235                 return NOTIFICATION_ERROR_INVALID_DATA;
236
237         if (!value)
238                 return NOTIFICATION_ERROR_INVALID_DATA;
239
240         column = _get_prop_column(property);
241         if (!column)
242                 return NOTIFICATION_ERROR_INVALID_DATA;
243
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;
248         }
249
250         ret = _is_record_exist(pkgname, db);
251         if (ret != NOTIFICATION_ERROR_ALREADY_EXIST_ID) {
252                 result = ret;
253                 goto return_close_db;
254         }
255
256         sqlbuf = sqlite3_mprintf("SELECT %s FROM %s " \
257                         "WHERE appid = %Q",
258                         column, NOTIFICATION_SETTING_DB, pkgname);
259         if (!sqlbuf) {
260                 NOTIFICATION_ERR("fail to alloc query");
261                 result = NOTIFICATION_ERROR_NO_MEMORY;
262                 goto return_close_db;
263         }
264
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;
271         }
272
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';
281                         *value = get_data;
282                 } else {
283                         NOTIFICATION_ERR("fail to alloc query");
284                         result = NOTIFICATION_ERROR_NO_MEMORY;
285                         goto return_close_db;
286                 }
287         }
288
289 return_close_db:
290         if (sqlbuf)
291                 sqlite3_free(sqlbuf);
292
293         if (stmt)
294                 sqlite3_finalize(stmt);
295
296         sqlret = db_util_close(db);
297         if (sqlret != SQLITE_OK)
298                 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
299
300         return result;
301 }
302
303 EXPORT_API notification_error_e notification_setting_property_set(const char *pkgname, const char *property, const char *value)
304 {
305         int ret = 0;
306
307         if (!pkgname)
308                 return NOTIFICATION_ERROR_INVALID_DATA;
309
310         if (!property)
311                 return NOTIFICATION_ERROR_INVALID_DATA;
312
313         if (!value)
314                 return NOTIFICATION_ERROR_INVALID_DATA;
315
316         ret = notification_ipc_noti_setting_property_set(pkgname, property, value);
317         if (ret != NOTIFICATION_ERROR_NONE) {
318                 return ret;
319         }
320
321         return NOTIFICATION_ERROR_NONE;
322 }
323
324 EXPORT_API notification_error_e notification_setting_property_get(const char *pkgname, const char *property, char **value)
325 {
326         int ret = 0;
327
328         if (!pkgname)
329                 return NOTIFICATION_ERROR_INVALID_DATA;
330
331         if (!property)
332                 return NOTIFICATION_ERROR_INVALID_DATA;
333
334         if (!value)
335                 return NOTIFICATION_ERROR_INVALID_DATA;
336
337         ret = notification_ipc_noti_setting_property_get(pkgname, property, value);
338         if (ret != NOTIFICATION_ERROR_NONE) {
339                 return ret;
340         }
341
342         return NOTIFICATION_ERROR_NONE;
343 }