4 * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
39 static int _get_table_field_data_int(char **table, int *buf, int index)
41 if ((table == NULL) || (buf == NULL) || (index < 0)) {
42 NOTIFICATION_ERR("table[%p], buf[%p], index[%d]", table, buf, index);
46 if (table[index] != NULL) {
47 *buf = atoi(table[index]);
55 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
59 if ((table == NULL) || (buf == NULL) || (index < 0)) {
60 NOTIFICATION_ERR("table[%p], buf[%p], index[%d]", table, buf, index);
64 char *pTemp = table[index];
71 *buf = (char *) malloc(sLen + 1);
73 NOTIFICATION_ERR("malloc is failed");
76 memset(*buf, 0, sLen + 1);
77 strncpy(*buf, pTemp, sLen);
89 EXPORT_API int noti_setting_service_get_setting_by_package_name(const char *package_name, notification_setting_h *setting)
91 int err = NOTIFICATION_ERROR_NONE;
92 sqlite3 *local_db_handle = NULL;
93 char *sql_query = NULL;
94 char **query_result = NULL;
100 notification_setting_h result_setting_array = NULL;
102 if (package_name == NULL || setting == NULL) {
103 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
104 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
108 sql_return = db_util_open(DBPATH, &local_db_handle, 0);
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;
116 sql_query = sqlite3_mprintf("SELECT package_name, allow_to_notify, do_not_disturb_except, visibility_class "
118 "WHERE package_name = %Q ", NOTIFICATION_SETTING_DB_TABLE, package_name);
121 NOTIFICATION_ERR("fail to alloc query");
122 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
126 sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
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;
135 NOTIFICATION_DBG("No setting found for [%s]", package_name);
136 err = NOTIFICATION_ERROR_NOT_EXIST_ID;
140 NOTIFICATION_DBG("row_count [%d] column_count [%d]", row_count, column_count);
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;
150 col_index = column_count;
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++);
157 *setting = result_setting_array;
161 sqlite3_free_table(query_result);
164 sqlite3_free(sql_query);
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);
177 EXPORT_API int noti_setting_get_setting_array(notification_setting_h *setting_array, int *count)
179 int err = NOTIFICATION_ERROR_NONE;
180 sqlite3 *local_db_handle = NULL;
181 char *sql_query = NULL;
182 char **query_result = NULL;
185 int column_count = 0;
188 notification_setting_h result_setting_array = NULL;
190 if (setting_array == NULL || count == NULL) {
191 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
192 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
196 sql_return = db_util_open(DBPATH, &local_db_handle, 0);
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;
204 sql_query = sqlite3_mprintf("SELECT package_name, allow_to_notify, do_not_disturb_except, visibility_class "
206 "ORDER BY package_name", NOTIFICATION_SETTING_DB_TABLE);
209 NOTIFICATION_ERR("fail to alloc query");
210 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
214 sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
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;
223 NOTIFICATION_DBG("No setting found...");
224 err = NOTIFICATION_ERROR_NOT_EXIST_ID;
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;
235 col_index = column_count;
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++);
244 *setting_array = result_setting_array;
249 sqlite3_free_table(query_result);
252 sqlite3_free(sql_query);
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);
264 EXPORT_API int noti_system_setting_load_system_setting(notification_system_setting_h *system_setting)
266 int err = NOTIFICATION_ERROR_NONE;
267 sqlite3 *local_db_handle = NULL;
268 char *sql_query = NULL;
269 char **query_result = NULL;
272 int column_count = 0;
274 notification_system_setting_h result_system_setting = NULL;
276 if (system_setting == NULL) {
277 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
278 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
282 sql_return = db_util_open(DBPATH, &local_db_handle, 0);
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;
290 sql_query = sqlite3_mprintf("SELECT do_not_disturb, visibility_class "
291 "FROM %s ", NOTIFICATION_SYSTEM_SETTING_DB_TABLE);
294 NOTIFICATION_ERR("fail to alloc query");
295 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
299 sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
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;
308 NOTIFICATION_DBG("No setting found...");
309 err = NOTIFICATION_ERROR_NOT_EXIST_ID;
313 NOTIFICATION_DBG("row_count [%d] column_count [%d]", row_count, column_count);
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;
323 col_index = column_count;
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++);
328 *system_setting = result_system_setting;
332 sqlite3_free_table(query_result);
335 sqlite3_free(sql_query);
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);