Using gdbus for IPC instead of com-core package
[platform/core/api/notification.git] / src / notification_setting_service.c
1 /*
2  *  libnotification
3  *
4  * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <errno.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include <sqlite3.h>
26 #include <db-util.h>
27 #include <tizen.h>
28
29 #include <notification.h>
30 #include <notification_db.h>
31 #include <notification_error.h>
32 #include <notification_debug.h>
33 #include <notification_private.h>
34 #include <notification_setting.h>
35 #include <notification_setting_internal.h>
36 #include <notification_setting_service.h>
37
38
39 static int _get_table_field_data_int(char  **table, int *buf, int index)
40 {
41         if ((table == NULL) || (buf == NULL) || (index < 0))  {
42                 NOTIFICATION_ERR("table[%p], buf[%p], index[%d]", table, buf, index);
43                 return false;
44         }
45
46         if (table[index] != NULL) {
47                 *buf = atoi(table[index]);
48                 return true;
49         }
50
51         *buf = 0;
52         return false;
53 }
54
55 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
56 {
57         int ret = false;
58
59         if ((table == NULL) || (buf == NULL) || (index < 0))  {
60                 NOTIFICATION_ERR("table[%p], buf[%p], index[%d]", table, buf, index);
61                 return false;
62         }
63
64         char *pTemp = table[index];
65         int sLen = 0;
66         if (pTemp == NULL) {
67                 *buf = NULL;
68         } else {
69                 sLen = strlen(pTemp);
70                 if (sLen) {
71                         *buf = (char *) malloc(sLen + 1);
72                         if (*buf == NULL) {
73                                 NOTIFICATION_ERR("malloc is failed");
74                                 goto out;
75                         }
76                         memset(*buf, 0, sLen + 1);
77                         strncpy(*buf, pTemp, sLen);
78                 } else {
79                         *buf = NULL;
80                 }
81         }
82
83         ret = true;
84 out:
85
86         return ret;
87 }
88
89 EXPORT_API int noti_setting_service_get_setting_by_package_name(const char *package_name, notification_setting_h *setting)
90 {
91         int err = NOTIFICATION_ERROR_NONE;
92         sqlite3 *local_db_handle = NULL;
93         char *sql_query = NULL;
94         char **query_result = NULL;
95         int sql_return;
96         int row_count = 0;
97         int column_count = 0;
98         int i = 0;
99         int col_index = 0;
100         notification_setting_h result_setting_array = NULL;
101
102         if (package_name == NULL || setting == NULL) {
103                 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
104                 err =  NOTIFICATION_ERROR_INVALID_PARAMETER;
105                 goto out;
106         }
107
108         sql_return = db_util_open(DBPATH, &local_db_handle, 0);
109
110         if (sql_return != SQLITE_OK || local_db_handle == NULL) {
111                 NOTIFICATION_ERR("db_util_open failed [%d]", sql_return);
112                 err = NOTIFICATION_ERROR_FROM_DB;
113                 goto out;
114         }
115
116         sql_query = sqlite3_mprintf("SELECT package_name, allow_to_notify, do_not_disturb_except, visibility_class "
117                         "FROM %s "
118                         "WHERE package_name = %Q ", NOTIFICATION_SETTING_DB_TABLE, package_name);
119
120         if (!sql_query) {
121                 NOTIFICATION_ERR("fail to alloc query");
122                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
123                 goto out;
124         }
125
126         sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
127
128         if (sql_return != SQLITE_OK && sql_return != -1) {
129                 NOTIFICATION_ERR("sqlite3_get_table failed [%d][%s]", sql_return, sql_query);
130                 err = NOTIFICATION_ERROR_FROM_DB;
131                 goto out;
132         }
133
134         if (!row_count) {
135                 NOTIFICATION_DBG("No setting found for [%s]", package_name);
136                 err = NOTIFICATION_ERROR_NOT_EXIST_ID;
137                 goto out;
138         }
139
140         NOTIFICATION_DBG("row_count [%d] column_count [%d]", row_count, column_count);
141
142         row_count = 1;
143
144         if (!(result_setting_array = (struct notification_setting*)malloc(sizeof(struct notification_setting) * row_count))) {
145                 NOTIFICATION_ERR("malloc failed...");
146                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
147                 goto out;
148         }
149
150         col_index = column_count;
151
152         _get_table_field_data_string(query_result, &(result_setting_array[i].package_name), 1, col_index++);
153         _get_table_field_data_int(query_result, (int*)&(result_setting_array[i].allow_to_notify), col_index++);
154         _get_table_field_data_int(query_result, (int*)&(result_setting_array[i].do_not_disturb_except), col_index++);
155         _get_table_field_data_int(query_result, &(result_setting_array[i].visibility_class), col_index++);
156
157         *setting = result_setting_array;
158
159 out:
160         if (query_result)
161                 sqlite3_free_table(query_result);
162
163         if (sql_query)
164                 sqlite3_free(sql_query);
165
166         if (local_db_handle) {
167                 sql_return = db_util_close(local_db_handle);
168                 if (sql_return != SQLITE_OK)
169                         NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return);
170         }
171
172         return err;
173 }
174
175
176
177 EXPORT_API int noti_setting_get_setting_array(notification_setting_h *setting_array, int *count)
178 {
179         int err = NOTIFICATION_ERROR_NONE;
180         sqlite3 *local_db_handle = NULL;
181         char *sql_query = NULL;
182         char **query_result = NULL;
183         int sql_return;
184         int row_count = 0;
185         int column_count = 0;
186         int i = 0;
187         int col_index = 0;
188         notification_setting_h result_setting_array = NULL;
189
190         if (setting_array == NULL || count == NULL) {
191                 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
192                 err =  NOTIFICATION_ERROR_INVALID_PARAMETER;
193                 goto out;
194         }
195
196         sql_return = db_util_open(DBPATH, &local_db_handle, 0);
197
198         if (sql_return != SQLITE_OK || local_db_handle == NULL) {
199                 NOTIFICATION_ERR("db_util_open failed [%d]", sql_return);
200                 err = NOTIFICATION_ERROR_FROM_DB;
201                 goto out;
202         }
203
204         sql_query = sqlite3_mprintf("SELECT package_name, allow_to_notify, do_not_disturb_except, visibility_class "
205                         "FROM %s "
206                         "ORDER BY package_name", NOTIFICATION_SETTING_DB_TABLE);
207
208         if (!sql_query) {
209                 NOTIFICATION_ERR("fail to alloc query");
210                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
211                 goto out;
212         }
213
214         sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
215
216         if (sql_return != SQLITE_OK && sql_return != -1) {
217                 NOTIFICATION_ERR("NOTIFICATION_ERROR_FROM_DB failed [%d][%s]", sql_return, sql_query);
218                 err = NOTIFICATION_ERROR_FROM_DB;
219                 goto out;
220         }
221
222         if (!row_count) {
223                 NOTIFICATION_DBG("No setting found...");
224                 err = NOTIFICATION_ERROR_NOT_EXIST_ID;
225                 goto out;
226         }
227
228         NOTIFICATION_DBG("row_count [%d] column_count [%d]", row_count, column_count);
229         if (!(result_setting_array = (struct notification_setting*)malloc(sizeof(struct notification_setting) * row_count))) {
230                 NOTIFICATION_ERR("malloc failed...");
231                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
232                 goto out;
233         }
234
235         col_index = column_count;
236
237         for (i = 0; i < row_count; i++) {
238                 _get_table_field_data_string(query_result, &(result_setting_array[i].package_name), 1, col_index++);
239                 _get_table_field_data_int(query_result, (int*)&(result_setting_array[i].allow_to_notify), col_index++);
240                 _get_table_field_data_int(query_result, (int*)&(result_setting_array[i].do_not_disturb_except), col_index++);
241                 _get_table_field_data_int(query_result, &(result_setting_array[i].visibility_class), col_index++);
242         }
243
244         *setting_array = result_setting_array;
245         *count = row_count;
246
247 out:
248         if (query_result)
249                         sqlite3_free_table(query_result);
250
251         if (sql_query)
252                 sqlite3_free(sql_query);
253
254         if (local_db_handle) {
255                 sql_return = db_util_close(local_db_handle);
256                 if (sql_return != SQLITE_OK)
257                         NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return);
258         }
259
260         return err;
261 }
262
263
264 EXPORT_API int noti_system_setting_load_system_setting(notification_system_setting_h *system_setting)
265 {
266         int err = NOTIFICATION_ERROR_NONE;
267         sqlite3 *local_db_handle = NULL;
268         char *sql_query = NULL;
269         char **query_result = NULL;
270         int sql_return;
271         int row_count = 0;
272         int column_count = 0;
273         int col_index = 0;
274         notification_system_setting_h result_system_setting = NULL;
275
276         if (system_setting == NULL) {
277                 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
278                 err =  NOTIFICATION_ERROR_INVALID_PARAMETER;
279                 goto out;
280         }
281
282         sql_return = db_util_open(DBPATH, &local_db_handle, 0);
283
284         if (sql_return != SQLITE_OK || local_db_handle == NULL) {
285                 NOTIFICATION_ERR("db_util_open failed [%d]", sql_return);
286                 err = NOTIFICATION_ERROR_FROM_DB;
287                 goto out;
288         }
289
290         sql_query = sqlite3_mprintf("SELECT do_not_disturb, visibility_class "
291                         "FROM %s ", NOTIFICATION_SYSTEM_SETTING_DB_TABLE);
292
293         if (!sql_query) {
294                 NOTIFICATION_ERR("fail to alloc query");
295                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
296                 goto out;
297         }
298
299         sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
300
301         if (sql_return != SQLITE_OK && sql_return != -1) {
302                 NOTIFICATION_ERR("sqlite3_get_table failed [%d][%s]", sql_return, sql_query);
303                 err = NOTIFICATION_ERROR_FROM_DB;
304                 goto out;
305         }
306
307         if (!row_count) {
308                 NOTIFICATION_DBG("No setting found...");
309                 err = NOTIFICATION_ERROR_NOT_EXIST_ID;
310                 goto out;
311         }
312
313         NOTIFICATION_DBG("row_count [%d] column_count [%d]", row_count, column_count);
314
315         row_count = 1;
316
317         if (!(result_system_setting = (struct notification_system_setting*)malloc(sizeof(struct notification_system_setting)))) {
318                 NOTIFICATION_ERR("malloc failed...");
319                 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
320                 goto out;
321         }
322
323         col_index = column_count;
324
325         _get_table_field_data_int(query_result, (int*)&(result_system_setting->do_not_disturb), col_index++);
326         _get_table_field_data_int(query_result, &(result_system_setting->visibility_class), col_index++);
327
328         *system_setting = result_system_setting;
329
330 out:
331         if (query_result)
332                         sqlite3_free_table(query_result);
333
334         if (sql_query)
335                 sqlite3_free(sql_query);
336
337         if (local_db_handle) {
338                 sql_return = db_util_close(local_db_handle);
339                 if (sql_return != SQLITE_OK)
340                         NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return);
341         }
342
343         return err;
344 }
345
346
347
348