4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Seungtaek Chung <seungtaek.chung@samsung.com>, Mi-Ju Lee <miju52.lee@samsung.com>, Xi Zhichan <zhichan.xi@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <package_manager.h>
27 #include <pkgmgr-info.h>
28 #include <tizen_type.h>
30 #include <notification.h>
31 #include <notification_db.h>
32 #include <notification_list.h>
33 #include <notification_noti.h>
34 #include <notification_debug.h>
35 #include <notification_ipc.h>
36 #include <notification_private.h>
37 #include <notification_setting.h>
38 #include <notification_setting_internal.h>
40 #define NOTIFICATION_PRIVILEGE "http://tizen.org/privilege/notification"
43 static int _get_table_field_data_int(char **table, int *buf, int index)
45 if ((table == NULL) || (buf == NULL) || (index < 0)) {
46 NOTIFICATION_ERR("table[%p], buf[%p], index[%d]", table, buf, index);
50 if (table[index] != NULL) {
51 *buf = atoi(table[index]);
59 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
63 if ((table == NULL) || (buf == NULL) || (index < 0)) {
64 NOTIFICATION_ERR("table[%p], buf[%p], index[%d]", table, buf, index);
68 char *pTemp = table[index];
75 *buf = (char *) malloc(sLen + 1);
77 NOTIFICATION_ERR("malloc is failed");
80 memset(*buf, 0, sLen + 1);
81 strncpy(*buf, pTemp, sLen);
95 EXPORT_API int notification_setting_get_setting_array(notification_setting_h *setting_array, int *count)
97 int err = NOTIFICATION_ERROR_NONE;
98 sqlite3 *local_db_handle = NULL;
99 char *sql_query = NULL;
100 char **query_result = NULL;
103 int column_count = 0;
106 notification_setting_h result_setting_array= NULL;
108 if (setting_array == NULL || count == NULL) {
109 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
110 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
114 sql_return = db_util_open(DBPATH, &local_db_handle, 0);
116 if (sql_return != SQLITE_OK || local_db_handle == NULL) {
117 NOTIFICATION_ERR("db_util_open failed [%d]", sql_return);
118 err = NOTIFICATION_ERROR_FROM_DB;
122 sql_query = sqlite3_mprintf("SELECT package_name, allow_to_notify, do_not_disturb_except, visibility_class "
124 "ORDER BY package_name", NOTIFICATION_SETTING_DB_TABLE);
127 NOTIFICATION_ERR("fail to alloc query");
128 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
132 sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
134 if (sql_return != SQLITE_OK && sql_return != -1) {
135 NOTIFICATION_ERR("NOTIFICATION_ERROR_FROM_DB failed [%d][%s]", sql_return, sql_query);
136 err = NOTIFICATION_ERROR_FROM_DB;
141 NOTIFICATION_DBG ("No setting found...");
142 err= NOTIFICATION_ERROR_NOT_EXIST_ID;
146 NOTIFICATION_DBG ("row_count [%d] column_count [%d]", row_count, column_count);
147 if (!(result_setting_array = (struct notification_setting*)malloc(sizeof(struct notification_setting) * row_count))) {
148 NOTIFICATION_ERR("malloc failed...");
149 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
153 col_index = column_count;
155 for (i = 0; i < row_count; i++) {
156 _get_table_field_data_string(query_result, &(result_setting_array[i].package_name), 1, col_index++);
157 _get_table_field_data_int(query_result, (int*)&(result_setting_array[i].allow_to_notify), col_index++);
158 _get_table_field_data_int(query_result, (int*)&(result_setting_array[i].do_not_disturb_except), col_index++);
159 _get_table_field_data_int(query_result, &(result_setting_array[i].visibility_class), col_index++);
162 *setting_array = result_setting_array;
167 sqlite3_free_table(query_result);
170 sqlite3_free(sql_query);
172 if (local_db_handle) {
173 sql_return = db_util_close(local_db_handle);
174 if (sql_return != SQLITE_OK) {
175 NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return);
182 EXPORT_API int notification_setting_get_setting_by_package_name(const char *package_name, notification_setting_h *setting)
184 int err = NOTIFICATION_ERROR_NONE;
185 sqlite3 *local_db_handle = NULL;
186 char *sql_query = NULL;
187 char **query_result = NULL;
190 int column_count = 0;
193 notification_setting_h result_setting_array= NULL;
195 if (package_name == NULL || setting == NULL) {
196 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
197 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
201 sql_return = db_util_open(DBPATH, &local_db_handle, 0);
203 if (sql_return != SQLITE_OK || local_db_handle == NULL) {
204 NOTIFICATION_ERR("db_util_open failed [%d]", sql_return);
205 err = NOTIFICATION_ERROR_FROM_DB;
209 sql_query = sqlite3_mprintf("SELECT package_name, allow_to_notify, do_not_disturb_except, visibility_class "
211 "WHERE package_name = %Q ", NOTIFICATION_SETTING_DB_TABLE, package_name);
214 NOTIFICATION_ERR("fail to alloc query");
215 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
219 sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
221 if (sql_return != SQLITE_OK && sql_return != -1) {
222 NOTIFICATION_ERR("sqlite3_get_table failed [%d][%s]", sql_return, sql_query);
223 err = NOTIFICATION_ERROR_FROM_DB;
228 NOTIFICATION_DBG ("No setting found for [%s]", package_name);
229 err= NOTIFICATION_ERROR_NOT_EXIST_ID;
233 NOTIFICATION_DBG ("row_count [%d] column_count [%d]", row_count, column_count);
237 if (!(result_setting_array = (struct notification_setting*)malloc(sizeof(struct notification_setting) * row_count))) {
238 NOTIFICATION_ERR("malloc failed...");
239 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
243 col_index = column_count;
245 _get_table_field_data_string(query_result, &(result_setting_array[i].package_name), 1, col_index++);
246 _get_table_field_data_int(query_result, (int*)&(result_setting_array[i].allow_to_notify), col_index++);
247 _get_table_field_data_int(query_result, (int*)&(result_setting_array[i].do_not_disturb_except), col_index++);
248 _get_table_field_data_int(query_result, &(result_setting_array[i].visibility_class), col_index++);
250 *setting = result_setting_array;
254 sqlite3_free_table(query_result);
257 sqlite3_free(sql_query);
259 if (local_db_handle) {
260 sql_return = db_util_close(local_db_handle);
261 if (sql_return != SQLITE_OK) {
262 NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return);
269 EXPORT_API int notification_setting_get_setting(notification_setting_h *setting)
272 char *package_name = NULL;
274 package_name = notification_get_pkgname_by_pid();
276 if (package_name == NULL)
277 return NOTIFICATION_ERROR_NOT_EXIST_ID;
279 ret = notification_setting_get_setting_by_package_name(package_name, setting);
286 EXPORT_API int notification_setting_get_package_name(notification_setting_h setting, char **value)
288 int err = NOTIFICATION_ERROR_NONE;
290 if (setting == NULL || value == NULL) {
291 NOTIFICATION_ERR("Invalid parameter\n");
292 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
296 if (setting->package_name == NULL) {
297 NOTIFICATION_ERR("setting->package_name is null\n");
298 err = NOTIFICATION_ERROR_NOT_EXIST_ID;
302 *value = SAFE_STRDUP(setting->package_name);
309 EXPORT_API int notification_setting_set_package_name(notification_setting_h setting, char *value)
311 int err = NOTIFICATION_ERROR_NONE;
313 if (setting == NULL || value == NULL) {
314 NOTIFICATION_ERR("Invalid parameter\n");
315 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
319 if (setting->package_name != NULL) {
320 free(setting->package_name);
323 setting->package_name = SAFE_STRDUP(value);
330 EXPORT_API int notification_setting_get_allow_to_notify(notification_setting_h setting, bool *value)
332 int err = NOTIFICATION_ERROR_NONE;
334 if (setting == NULL || value == NULL) {
335 NOTIFICATION_ERR("Invalid parameter\n");
336 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
340 *value = setting->allow_to_notify;
347 EXPORT_API int notification_setting_set_allow_to_notify(notification_setting_h setting, bool value)
349 int err = NOTIFICATION_ERROR_NONE;
351 if (setting == NULL) {
352 NOTIFICATION_ERR("Invalid parameter\n");
353 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
357 setting->allow_to_notify = value;
364 EXPORT_API int notification_setting_get_do_not_disturb_except(notification_setting_h setting, bool *value)
366 int err = NOTIFICATION_ERROR_NONE;
368 if (setting == NULL || value == NULL) {
369 NOTIFICATION_ERR("Invalid parameter\n");
370 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
374 *value = setting->do_not_disturb_except;
381 EXPORT_API int notification_setting_set_do_not_disturb_except(notification_setting_h setting, bool value)
383 int err = NOTIFICATION_ERROR_NONE;
385 if (setting == NULL) {
386 NOTIFICATION_ERR("Invalid parameter\n");
387 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
391 setting->do_not_disturb_except = value;
398 EXPORT_API int notification_setting_get_visibility_class(notification_setting_h setting, int *value)
400 int err = NOTIFICATION_ERROR_NONE;
402 if (setting == NULL || value == NULL) {
403 NOTIFICATION_ERR("Invalid parameter\n");
404 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
408 *value = setting->visibility_class;
415 EXPORT_API int notification_setting_set_visibility_class(notification_setting_h setting, int value)
417 int err = NOTIFICATION_ERROR_NONE;
419 if (setting == NULL) {
420 NOTIFICATION_ERR("Invalid parameter\n");
421 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
425 setting->visibility_class = value;
432 EXPORT_API int notification_setting_update_setting(notification_setting_h setting)
434 int err = NOTIFICATION_ERROR_NONE;
436 if (setting == NULL) {
437 NOTIFICATION_ERR("Invalid parameter\n");
438 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
442 err = notification_ipc_update_setting(setting);
443 if (err != NOTIFICATION_ERROR_NONE) {
444 NOTIFICATION_ERR("notification_setting_update_setting returns[%d]\n", err);
452 EXPORT_API int notification_setting_free_notification(notification_setting_h setting)
454 int err = NOTIFICATION_ERROR_NONE;
456 if (setting == NULL) {
457 NOTIFICATION_ERR("Invalid parameter\n");
458 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
462 SAFE_FREE(setting->package_name);
464 /* add codes to free all properties */
472 EXPORT_API int notification_setting_db_update(const char *package_name, int allow_to_notify, int do_not_disturb_except, int visibility_class)
474 int err = NOTIFICATION_ERROR_NONE;
479 if (package_name == NULL)
480 return NOTIFICATION_ERROR_INVALID_PARAMETER;
482 sqlret = db_util_open(DBPATH, &db, 0);
483 if (sqlret != SQLITE_OK || db == NULL) {
484 NOTIFICATION_ERR("db_util_open failed [%s][%d]", DBPATH, sqlret);
485 return NOTIFICATION_ERROR_FROM_DB;
488 sqlbuf = sqlite3_mprintf("UPDATE %s SET allow_to_notify = %d, do_not_disturb_except = %d, visibility_class = %d " \
489 "WHERE package_name = %Q",
490 NOTIFICATION_SETTING_DB_TABLE, allow_to_notify, do_not_disturb_except, visibility_class, package_name);
492 NOTIFICATION_ERR("fail to alloc query");
493 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
494 goto return_close_db;
497 err = notification_db_exec(db, sqlbuf, NULL);
501 sqlite3_free(sqlbuf);
503 sqlret = db_util_close(db);
504 if (sqlret != SQLITE_OK) {
505 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
511 static bool _is_package_in_setting_table(sqlite3 *db, const char *package_name)
513 sqlite3_stmt *db_statement = NULL;
514 int sqlite3_ret = SQLITE_OK;
518 sqlite3_ret = sqlite3_prepare_v2(db, "SELECT package_name FROM notification_setting WHERE package_name = ?", -1, &db_statement, NULL);
520 if (sqlite3_ret != SQLITE_OK) {
521 NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
526 sqlite3_bind_text(db_statement, field_index++, package_name, -1, SQLITE_TRANSIENT);
528 sqlite3_ret = sqlite3_step(db_statement);
530 if (sqlite3_ret == SQLITE_DONE) {
531 NOTIFICATION_INFO("no matched package_name found[%s][%d]", package_name, sqlite3_ret);
536 if (sqlite3_ret != SQLITE_OK) {
537 NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
543 sqlite3_finalize(db_statement);
549 static int foreach_package_info_callback(const pkgmgrinfo_pkginfo_h package_info, void *user_data)
551 sqlite3 *db = user_data;
552 sqlite3_stmt *db_statement = NULL;
553 char *package_name = NULL;
554 int pkgmgr_ret = PACKAGE_MANAGER_ERROR_NONE;
555 int sqlite3_ret = SQLITE_OK;
559 if ((pkgmgr_ret = pkgmgrinfo_pkginfo_get_pkgname(package_info, &package_name)) != PACKAGE_MANAGER_ERROR_NONE) {
560 NOTIFICATION_ERR("package_info_get_package failed [%d]", pkgmgr_ret);
565 if (_is_package_in_setting_table(db, package_name) == true) {
566 NOTIFICATION_INFO("[%s] is exist", package_name);
570 NOTIFICATION_INFO("[%s] will be inserted", package_name);
572 sqlite3_ret = sqlite3_prepare_v2(db, "INSERT INTO notification_setting (package_name) VALUES (?) ", -1, &db_statement, NULL);
574 if (sqlite3_ret != SQLITE_OK) {
575 NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
580 sqlite3_bind_text(db_statement, field_index++, package_name, -1, SQLITE_TRANSIENT);
582 sqlite3_ret = sqlite3_step(db_statement);
584 NOTIFICATION_INFO("sqlite3_step returns[%d]", sqlite3_ret);
586 if (sqlite3_ret != SQLITE_OK && sqlite3_ret != SQLITE_DONE) {
587 NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
593 sqlite3_finalize(db_statement);
596 NOTIFICATION_INFO("foreach_package_info_callback returns[%d]", err);
600 EXPORT_API int notification_setting_refresh_setting_table()
602 int err = NOTIFICATION_ERROR_NONE;
604 int sqlite3_ret = SQLITE_OK;
605 int pkgmgr_ret = PACKAGE_MANAGER_ERROR_NONE;
606 pkgmgrinfo_pkginfo_filter_h filter;
609 sqlite3_ret = db_util_open(DBPATH, &db, 0);
611 if (sqlite3_ret != SQLITE_OK || db == NULL) {
612 NOTIFICATION_ERR("db_util_open failed [%s][%d]", DBPATH, sqlite3_ret);
613 err = NOTIFICATION_ERROR_FROM_DB;
617 sqlite3_exec(db, "BEGIN immediate;", NULL, NULL, NULL);
619 pkgmgr_ret = pkgmgrinfo_pkginfo_filter_create(&filter);
620 if (pkgmgr_ret != PMINFO_R_OK) {
621 NOTIFICATION_ERR("pkgmgrinfo_pkginfo_filter_create failed [%d]", pkgmgr_ret);
622 err = NOTIFICATION_ERROR_FROM_DB;
626 pkgmgr_ret = pkgmgrinfo_pkginfo_filter_add_string(filter, PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE, NOTIFICATION_PRIVILEGE);
627 if (pkgmgr_ret != PMINFO_R_OK) {
628 NOTIFICATION_ERR("pkgmgrinfo_pkginfo_filter_add_string failed [%d]", pkgmgr_ret);
629 err = NOTIFICATION_ERROR_FROM_DB;
633 current_uid = getuid();
635 pkgmgr_ret = pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo(filter, foreach_package_info_callback, db, current_uid);
636 if (pkgmgr_ret != PMINFO_R_OK) {
637 NOTIFICATION_ERR("pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo failed [%d]", pkgmgr_ret);
638 err = NOTIFICATION_ERROR_FROM_DB;
642 pkgmgrinfo_pkginfo_filter_destroy(filter);
648 if (err == NOTIFICATION_ERROR_NONE) {
649 sqlite3_exec(db, "END;", NULL, NULL, NULL);
652 sqlite3_exec(db, "ROLLBACK;", NULL, NULL, NULL);
655 if ((sqlite3_ret = db_util_close(db)) != SQLITE_OK) {
656 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlite3_ret);
660 NOTIFICATION_INFO("notification_setting_refresh_setting_table returns [%08X]", err);
665 /* system setting --------------------------------*/
667 EXPORT_API int notification_system_setting_load_system_setting(notification_system_setting_h *system_setting)
669 int err = NOTIFICATION_ERROR_NONE;
670 sqlite3 *local_db_handle = NULL;
671 char *sql_query = NULL;
672 char **query_result = NULL;
675 int column_count = 0;
677 notification_system_setting_h result_system_setting= NULL;
679 if (system_setting == NULL) {
680 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
681 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
685 sql_return = db_util_open(DBPATH, &local_db_handle, 0);
687 if (sql_return != SQLITE_OK || local_db_handle == NULL) {
688 NOTIFICATION_ERR("db_util_open failed [%d]", sql_return);
689 err = NOTIFICATION_ERROR_FROM_DB;
693 sql_query = sqlite3_mprintf("SELECT do_not_disturb, visibility_class "
694 "FROM %s ", NOTIFICATION_SYSTEM_SETTING_DB_TABLE);
697 NOTIFICATION_ERR("fail to alloc query");
698 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
702 sql_return = sqlite3_get_table(local_db_handle, sql_query, &query_result, &row_count, &column_count, NULL);
704 if (sql_return != SQLITE_OK && sql_return != -1) {
705 NOTIFICATION_ERR("sqlite3_get_table failed [%d][%s]", sql_return, sql_query);
706 err = NOTIFICATION_ERROR_FROM_DB;
711 NOTIFICATION_DBG ("No setting found...");
712 err= NOTIFICATION_ERROR_NOT_EXIST_ID;
716 NOTIFICATION_DBG ("row_count [%d] column_count [%d]", row_count, column_count);
720 if (!(result_system_setting = (struct notification_system_setting*)malloc(sizeof(struct notification_system_setting)))) {
721 NOTIFICATION_ERR("malloc failed...");
722 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
726 col_index = column_count;
728 _get_table_field_data_int(query_result, (int*)&(result_system_setting->do_not_disturb), col_index++);
729 _get_table_field_data_int(query_result, &(result_system_setting->visibility_class), col_index++);
731 *system_setting = result_system_setting;
735 sqlite3_free_table(query_result);
738 sqlite3_free(sql_query);
740 if (local_db_handle) {
741 sql_return = db_util_close(local_db_handle);
742 if (sql_return != SQLITE_OK) {
743 NOTIFICATION_WARN("fail to db_util_close - [%d]", sql_return);
750 EXPORT_API int notification_system_setting_update_system_setting(notification_system_setting_h system_setting)
752 int err = NOTIFICATION_ERROR_NONE;
754 if (system_setting == NULL) {
755 NOTIFICATION_ERR("Invalid parameter\n");
756 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
760 err = notification_ipc_update_system_setting(system_setting);
761 if (err != NOTIFICATION_ERROR_NONE) {
762 NOTIFICATION_ERR("notification_ipc_update_system_setting returns[%d]\n", err);
770 EXPORT_API int notification_system_setting_free_system_setting(notification_system_setting_h system_setting)
772 int err = NOTIFICATION_ERROR_NONE;
774 if (system_setting == NULL) {
775 NOTIFICATION_ERR("Invalid parameter\n");
776 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
780 /* add codes to free all properties */
782 SAFE_FREE(system_setting);
789 EXPORT_API int notification_system_setting_get_do_not_disturb(notification_system_setting_h system_setting, bool *value)
791 int err = NOTIFICATION_ERROR_NONE;
793 if (system_setting == NULL || value == NULL) {
794 NOTIFICATION_ERR("Invalid parameter\n");
795 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
799 *value = system_setting->do_not_disturb;
806 EXPORT_API int notification_system_setting_set_do_not_disturb(notification_system_setting_h system_setting, bool value)
808 int err = NOTIFICATION_ERROR_NONE;
810 if (system_setting == NULL) {
811 NOTIFICATION_ERR("Invalid parameter\n");
812 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
816 system_setting->do_not_disturb = value;
823 EXPORT_API int notification_system_setting_get_visibility_class(notification_system_setting_h system_setting, int *value)
825 int err = NOTIFICATION_ERROR_NONE;
827 if (system_setting == NULL || value == NULL) {
828 NOTIFICATION_ERR("Invalid parameter\n");
829 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
833 *value = system_setting->visibility_class;
840 EXPORT_API int notification_system_setting_set_visibility_class(notification_system_setting_h system_setting, int value)
842 int err = NOTIFICATION_ERROR_NONE;
844 if (system_setting == NULL) {
845 NOTIFICATION_ERR("Invalid parameter\n");
846 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
850 system_setting->visibility_class = value;
858 EXPORT_API int notification_setting_db_update_system_setting(int do_not_disturb, int visibility_class)
860 int err = NOTIFICATION_ERROR_NONE;
864 sqlite3_stmt *db_statement = NULL;
866 sqlret = db_util_open(DBPATH, &db, 0);
868 if (sqlret != SQLITE_OK || db == NULL) {
869 NOTIFICATION_ERR("db_util_open failed [%s][%d][%s]", DBPATH, sqlret, sqlite3_errmsg(db));
870 err = NOTIFICATION_ERROR_FROM_DB;
871 goto return_close_db;
874 sqlite3_exec(db, "BEGIN immediate;", NULL, NULL, NULL);
876 sqlret = sqlite3_prepare_v2(db, "UPDATE notification_system_setting SET do_not_disturb = ?, visibility_class = ?;", -1, &db_statement, NULL);
878 if (sqlret != SQLITE_OK) {
879 NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlret, sqlite3_errmsg(db));
880 err = NOTIFICATION_ERROR_FROM_DB;
881 goto return_close_db;
884 sqlite3_bind_int(db_statement, field_index++, do_not_disturb);
885 sqlite3_bind_int(db_statement, field_index++, visibility_class);
887 sqlret = sqlite3_step(db_statement);
889 if (sqlret != SQLITE_OK && sqlret != SQLITE_DONE) {
890 NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlret, sqlite3_errmsg(db));
891 err = NOTIFICATION_ERROR_FROM_DB;
892 goto return_close_db;
895 sqlret = sqlite3_changes(db);
898 NOTIFICATION_WARN("No changes on DB");
903 sqlite3_finalize(db_statement);
907 if (err == NOTIFICATION_ERROR_NONE) {
908 sqlite3_exec(db, "END;", NULL, NULL, NULL);
911 sqlite3_exec(db, "ROLLBACK;", NULL, NULL, NULL);
913 sqlret = db_util_close(db);
916 if (sqlret != SQLITE_OK) {
917 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
923 /* OLD IMPLEMENTATION ----------------------------*/
924 #define NOTIFICATION_SETTING_DB "notification_setting"
925 #define NOTIFICATION_SETTING_DB_PATH "/opt/usr/dbspace/.notification_parser.db"
927 struct _notification_setting_h {
937 const char *property;
939 const char *default_value;
942 static struct prop_table g_prop_table[] = {
944 .property = "OPT_NOTIFICATION",
945 .column = "notification",
946 .default_value = "ON",
949 .property = "OPT_SOUNDS",
951 .default_value = "ON",
954 .property = "OPT_CONTENTS",
955 .column = "contents",
956 .default_value = "ON",
959 .property = "OPT_BADGE",
961 .default_value = "ON",
966 .default_value = NULL,
970 static const char *_get_prop_column(const char *property)
974 for (i = 0; g_prop_table[i].property; i++) {
975 if (strcmp(g_prop_table[i].property, property))
978 return g_prop_table[i].column;
985 static const char *_get_prop_default_value(const char *property)
989 for (i = 0; g_prop_table[i].property; i++) {
990 if (strcmp(g_prop_table[i].property, property))
993 return g_prop_table[i].default_value;
1000 static int _is_record_exist(const char *pkgname, sqlite3 *db)
1002 sqlite3_stmt *stmt = NULL;
1004 int result = NOTIFICATION_ERROR_NONE;
1005 char *sqlbuf = NULL;
1009 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1012 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1014 sqlbuf = sqlite3_mprintf("SELECT count(*) FROM %s WHERE " \
1016 NOTIFICATION_SETTING_DB, pkgname);
1019 NOTIFICATION_ERR("fail to alloc sql query");
1020 return NOTIFICATION_ERROR_OUT_OF_MEMORY;
1023 sqlret = sqlite3_prepare_v2(db, sqlbuf, -1, &stmt, NULL);
1024 if (sqlret != SQLITE_OK) {
1025 NOTIFICATION_ERR("DB err [%s]", sqlite3_errmsg(db));
1026 NOTIFICATION_ERR("query[%s]", sqlbuf);
1027 result = NOTIFICATION_ERROR_FROM_DB;
1028 goto free_and_return;
1031 sqlret = sqlite3_step(stmt);
1032 if (sqlret == SQLITE_ROW)
1033 count = sqlite3_column_int(stmt, 0);
1038 result = NOTIFICATION_ERROR_ALREADY_EXIST_ID;
1040 result = NOTIFICATION_ERROR_NOT_EXIST_ID;
1044 sqlite3_free(sqlbuf);
1047 sqlite3_finalize(stmt);
1052 EXPORT_API int notification_setting_db_set(const char *pkgname, const char *property, const char *value)
1054 int ret = NOTIFICATION_ERROR_NONE;
1055 int result = NOTIFICATION_ERROR_NONE;
1057 char *sqlbuf = NULL;
1059 const char *column = NULL;
1062 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1065 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1068 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1070 column = _get_prop_column(property);
1072 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1074 sqlret = db_util_open(NOTIFICATION_SETTING_DB_PATH, &db, 0);
1075 if (sqlret != SQLITE_OK || !db) {
1076 NOTIFICATION_ERR("fail to db_util_open - [%d]", sqlret);
1077 return NOTIFICATION_ERROR_FROM_DB;
1080 ret = _is_record_exist(pkgname, db);
1081 if (ret != NOTIFICATION_ERROR_ALREADY_EXIST_ID) {
1083 goto return_close_db;
1086 sqlbuf = sqlite3_mprintf("UPDATE %s SET %s = %Q " \
1088 NOTIFICATION_SETTING_DB, column, value, pkgname);
1090 NOTIFICATION_ERR("fail to alloc query");
1091 result = NOTIFICATION_ERROR_OUT_OF_MEMORY;
1092 goto return_close_db;
1095 result = notification_db_exec(db, sqlbuf, NULL);
1099 sqlite3_free(sqlbuf);
1101 sqlret = db_util_close(db);
1102 if (sqlret != SQLITE_OK) {
1103 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
1109 EXPORT_API int notification_setting_db_get(const char *pkgname, const char *property, char **value)
1111 int ret = NOTIFICATION_ERROR_NONE;
1112 int result = NOTIFICATION_ERROR_NONE;
1114 char *sqlbuf = NULL;
1115 sqlite3_stmt *stmt = NULL;
1117 const char *column = NULL;
1120 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1123 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1126 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1128 column = _get_prop_column(property);
1130 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1132 sqlret = db_util_open(NOTIFICATION_SETTING_DB_PATH, &db, 0);
1133 if (sqlret != SQLITE_OK || !db) {
1134 NOTIFICATION_ERR("fail to db_util_open - [%d]", sqlret);
1135 return NOTIFICATION_ERROR_FROM_DB;
1138 ret = _is_record_exist(pkgname, db);
1139 if (ret != NOTIFICATION_ERROR_ALREADY_EXIST_ID) {
1141 goto return_close_db;
1144 sqlbuf = sqlite3_mprintf("SELECT %s FROM %s " \
1146 column, NOTIFICATION_SETTING_DB, pkgname);
1148 NOTIFICATION_ERR("fail to alloc query");
1149 result = NOTIFICATION_ERROR_OUT_OF_MEMORY;
1150 goto return_close_db;
1153 sqlret = sqlite3_prepare_v2(db, sqlbuf, -1, &stmt, NULL);
1154 if (sqlret != SQLITE_OK) {
1155 NOTIFICATION_ERR("fail to prepare %s - [%s]",
1156 sqlbuf, sqlite3_errmsg(db));
1157 result = NOTIFICATION_ERROR_FROM_DB;
1158 goto return_close_db;
1161 sqlret = sqlite3_step(stmt);
1162 if (sqlret == SQLITE_ROW) {
1163 int get_bytes = sqlite3_column_bytes(stmt, 0);
1164 char *get_data = (char *)calloc(get_bytes + 1, sizeof(char));
1165 if (get_data != NULL) {
1166 memcpy(get_data, sqlite3_column_text(stmt, 0),
1167 get_bytes * sizeof(char));
1168 get_data[get_bytes] = '\0';
1171 NOTIFICATION_ERR("fail to alloc query");
1172 result = NOTIFICATION_ERROR_OUT_OF_MEMORY;
1173 goto return_close_db;
1179 sqlite3_free(sqlbuf);
1182 sqlite3_finalize(stmt);
1184 sqlret = db_util_close(db);
1185 if (sqlret != SQLITE_OK)
1186 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
1191 EXPORT_API int notification_setting_property_set(const char *pkgname, const char *property, const char *value)
1196 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1199 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1202 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1204 ret = notification_ipc_noti_setting_property_set(pkgname, property, value);
1205 if (ret != NOTIFICATION_ERROR_NONE) {
1209 return NOTIFICATION_ERROR_NONE;
1212 EXPORT_API int notification_setting_property_get(const char *pkgname, const char *property, char **value)
1217 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1220 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1223 return NOTIFICATION_ERROR_INVALID_PARAMETER;
1225 ret = notification_ipc_noti_setting_property_get(pkgname, property, value);
1226 if (ret != NOTIFICATION_ERROR_NONE) {
1230 return NOTIFICATION_ERROR_NONE;