2 * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
26 #include <notification.h>
27 #include <notification_db.h>
28 #include <notification_error.h>
29 #include <notification_debug.h>
30 #include <notification_private.h>
31 #include <notification_setting.h>
32 #include <notification_setting_internal.h>
33 #include <notification_setting_service.h>
36 static int _get_table_field_data_int(char **table, int *buf, int index)
38 if ((table == NULL) || (buf == NULL) || (index < 0)) {
39 NOTIFICATION_ERR("table[%p], buf[%p], index[%d]", table, buf, index);
43 if (table[index] != NULL) {
44 *buf = atoi(table[index]);
52 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
56 if ((table == NULL) || (buf == NULL) || (index < 0)) {
57 NOTIFICATION_ERR("table[%p], buf[%p], index[%d]", table, buf, index);
61 char *pTemp = table[index];
68 *buf = (char *) malloc(sLen + 1);
70 NOTIFICATION_ERR("malloc is failed");
73 memset(*buf, 0, sLen + 1);
74 strncpy(*buf, pTemp, sLen);
86 EXPORT_API int noti_setting_service_get_setting_by_package_name(const char *package_name, notification_setting_h *setting)
88 int err = NOTIFICATION_ERROR_NONE;
89 sqlite3 *local_db_handle = NULL;
90 char *sql_query = NULL;
91 char **query_result = NULL;
97 notification_setting_h result_setting_array = NULL;
99 if (package_name == NULL || setting == NULL) {
100 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
101 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
105 sql_return = db_util_open(DBPATH, &local_db_handle, 0);
107 if (sql_return != SQLITE_OK || local_db_handle == NULL) {
108 NOTIFICATION_ERR("db_util_open failed [%d]", sql_return);
109 err = NOTIFICATION_ERROR_FROM_DB;
113 sql_query = sqlite3_mprintf("SELECT package_name, allow_to_notify, do_not_disturb_except, visibility_class "
115 "WHERE package_name = %Q ", NOTIFICATION_SETTING_DB_TABLE, package_name);
118 NOTIFICATION_ERR("fail to alloc query");
119 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
123 sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
125 if (sql_return != SQLITE_OK && sql_return != -1) {
126 NOTIFICATION_ERR("sqlite3_get_table failed [%d][%s]", sql_return, sql_query);
127 err = NOTIFICATION_ERROR_FROM_DB;
132 NOTIFICATION_DBG("No setting found for [%s]", package_name);
133 err = NOTIFICATION_ERROR_NOT_EXIST_ID;
137 NOTIFICATION_DBG("row_count [%d] column_count [%d]", row_count, column_count);
141 if (!(result_setting_array = (struct notification_setting *)malloc(sizeof(struct notification_setting) * row_count))) {
142 NOTIFICATION_ERR("malloc failed...");
143 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
147 col_index = column_count;
149 _get_table_field_data_string(query_result, &(result_setting_array[i].package_name), 1, col_index++);
150 _get_table_field_data_int(query_result, (int *)&(result_setting_array[i].allow_to_notify), col_index++);
151 _get_table_field_data_int(query_result, (int *)&(result_setting_array[i].do_not_disturb_except), col_index++);
152 _get_table_field_data_int(query_result, &(result_setting_array[i].visibility_class), col_index++);
154 *setting = result_setting_array;
158 sqlite3_free_table(query_result);
161 sqlite3_free(sql_query);
163 if (local_db_handle) {
164 sql_return = db_util_close(local_db_handle);
165 if (sql_return != SQLITE_OK)
166 NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return);
174 EXPORT_API int noti_setting_get_setting_array(notification_setting_h *setting_array, int *count)
176 int err = NOTIFICATION_ERROR_NONE;
177 sqlite3 *local_db_handle = NULL;
178 char *sql_query = NULL;
179 char **query_result = NULL;
182 int column_count = 0;
185 notification_setting_h result_setting_array = NULL;
187 if (setting_array == NULL || count == NULL) {
188 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
189 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
193 sql_return = db_util_open(DBPATH, &local_db_handle, 0);
195 if (sql_return != SQLITE_OK || local_db_handle == NULL) {
196 NOTIFICATION_ERR("db_util_open failed [%d]", sql_return);
197 err = NOTIFICATION_ERROR_FROM_DB;
201 sql_query = sqlite3_mprintf("SELECT package_name, allow_to_notify, do_not_disturb_except, visibility_class "
203 "ORDER BY package_name", NOTIFICATION_SETTING_DB_TABLE);
206 NOTIFICATION_ERR("fail to alloc query");
207 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
211 sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
213 if (sql_return != SQLITE_OK && sql_return != -1) {
214 NOTIFICATION_ERR("NOTIFICATION_ERROR_FROM_DB failed [%d][%s]", sql_return, sql_query);
215 err = NOTIFICATION_ERROR_FROM_DB;
220 NOTIFICATION_DBG("No setting found...");
221 err = NOTIFICATION_ERROR_NOT_EXIST_ID;
225 NOTIFICATION_DBG("row_count [%d] column_count [%d]", row_count, column_count);
226 if (!(result_setting_array = (struct notification_setting *)malloc(sizeof(struct notification_setting) * row_count))) {
227 NOTIFICATION_ERR("malloc failed...");
228 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
232 col_index = column_count;
234 for (i = 0; i < row_count; i++) {
235 _get_table_field_data_string(query_result, &(result_setting_array[i].package_name), 1, col_index++);
236 _get_table_field_data_int(query_result, (int *)&(result_setting_array[i].allow_to_notify), col_index++);
237 _get_table_field_data_int(query_result, (int *)&(result_setting_array[i].do_not_disturb_except), col_index++);
238 _get_table_field_data_int(query_result, &(result_setting_array[i].visibility_class), col_index++);
241 *setting_array = result_setting_array;
246 sqlite3_free_table(query_result);
249 sqlite3_free(sql_query);
251 if (local_db_handle) {
252 sql_return = db_util_close(local_db_handle);
253 if (sql_return != SQLITE_OK)
254 NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return);
261 EXPORT_API int noti_system_setting_load_system_setting(notification_system_setting_h *system_setting)
263 int err = NOTIFICATION_ERROR_NONE;
264 sqlite3 *local_db_handle = NULL;
265 char *sql_query = NULL;
266 char **query_result = NULL;
269 int column_count = 0;
271 notification_system_setting_h result_system_setting = NULL;
273 if (system_setting == NULL) {
274 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
275 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
279 sql_return = db_util_open(DBPATH, &local_db_handle, 0);
281 if (sql_return != SQLITE_OK || local_db_handle == NULL) {
282 NOTIFICATION_ERR("db_util_open failed [%d]", sql_return);
283 err = NOTIFICATION_ERROR_FROM_DB;
287 sql_query = sqlite3_mprintf("SELECT do_not_disturb, visibility_class "
288 "FROM %s ", NOTIFICATION_SYSTEM_SETTING_DB_TABLE);
291 NOTIFICATION_ERR("fail to alloc query");
292 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
296 sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
298 if (sql_return != SQLITE_OK && sql_return != -1) {
299 NOTIFICATION_ERR("sqlite3_get_table failed [%d][%s]", sql_return, sql_query);
300 err = NOTIFICATION_ERROR_FROM_DB;
305 NOTIFICATION_DBG("No setting found...");
306 err = NOTIFICATION_ERROR_NOT_EXIST_ID;
310 NOTIFICATION_DBG("row_count [%d] column_count [%d]", row_count, column_count);
314 if (!(result_system_setting = (struct notification_system_setting *)malloc(sizeof(struct notification_system_setting)))) {
315 NOTIFICATION_ERR("malloc failed...");
316 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
320 col_index = column_count;
322 _get_table_field_data_int(query_result, (int *)&(result_system_setting->do_not_disturb), col_index++);
323 _get_table_field_data_int(query_result, &(result_system_setting->visibility_class), col_index++);
325 *system_setting = result_system_setting;
329 sqlite3_free_table(query_result);
332 sqlite3_free(sql_query);
334 if (local_db_handle) {
335 sql_return = db_util_close(local_db_handle);
336 if (sql_return != SQLITE_OK)
337 NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return);