Fetching package names from package manager.
authorKyuho Jo <kyuho.jo@samsung.com>
Tue, 19 May 2015 11:13:12 +0000 (20:13 +0900)
committerKyuho Jo <kyuho.jo@samsung.com>
Tue, 19 May 2015 11:13:12 +0000 (20:13 +0900)
Change-Id: Ida53aa317073496b157aa097d6650df96886159a
Signed-off-by: Kyuho Jo <kyuho.jo@samsung.com>
include/notification_setting.h
packaging/notification.spec
src/notification_setting.c
test-app/main.c

index a06fef6..39a5ec5 100644 (file)
@@ -40,6 +40,8 @@ int notification_setting_update_setting(notification_setting_h setting);
 
 int notification_setting_free_notification(notification_setting_h setting);
 
+int notification_setting_refresh_setting_table();
+
 #ifdef __cplusplus
 }
 #endif
index 327579f..6477561 100644 (file)
@@ -1,6 +1,6 @@
 Name:       notification
 Summary:    notification library
-Version:    0.2.33
+Version:    0.2.34
 Release:    1
 Group:      TBD
 License:    Apache
@@ -172,7 +172,7 @@ then
                        UNIQUE (caller_pkgname, priv_id)
                ); 
                CREATE TABLE IF NOT EXISTS notification_setting ( 
-                       priv_id INTERGER PRIMARY KEY,
+                       priv_id INTEGER PRIMARY KEY AUTOINCREMENT,
                        package_name TEXT NOT NULL,
                        allow_to_notify INTEGER DEFAULT 1,
                        do_not_disturb_except INTEGER DEFAULT 0,
@@ -186,11 +186,6 @@ then
                        UNIQUE (priv_id)
                ); 
 
-               INSERT INTO notification_setting (priv_id, package_name, allow_to_notify, do_not_disturb_except, visibility_class) VALUES (1, "org.tizen.photos", 1, 0, 0);
-               INSERT INTO notification_setting (priv_id, package_name, allow_to_notify, do_not_disturb_except, visibility_class) VALUES (2, "org.tizen.email", 1, 0, 0);
-               INSERT INTO notification_setting (priv_id, package_name, allow_to_notify, do_not_disturb_except, visibility_class) VALUES (3, "org.tizen.message", 1, 0, 0);
-               INSERT INTO notification_setting (priv_id, package_name, allow_to_notify, do_not_disturb_except, visibility_class) VALUES (4, "org.tizen.internet", 1, 0, 0);
-               INSERT INTO notification_setting (priv_id, package_name, allow_to_notify, do_not_disturb_except, visibility_class) VALUES (5, "org.tizen.games", 1, 0, 0);
                INSERT INTO notification_system_setting (priv_id, do_not_disturb, visibility_class) VALUES (0, 0, 0);
 
                CREATE UNIQUE INDEX package_name_idx1 ON notification_setting (package_name);
index 10cea24..44526f5 100644 (file)
@@ -23,6 +23,8 @@
 #include <string.h>
 #include <stdlib.h>
 #include <db-util.h>
+#include <package_manager.h>
+#include <tizen_type.h>
 
 #include <notification.h>
 #include <notification_db.h>
@@ -450,7 +452,6 @@ EXPORT_API int notification_setting_free_notification(notification_setting_h set
 
        SAFE_FREE(setting->package_name);
 
-
        /* add codes to free all properties */
 
        SAFE_FREE(setting);
@@ -498,6 +499,143 @@ EXPORT_API int notification_setting_db_update(const char *package_name, int allo
        return err;
 }
 
+static bool _is_package_in_setting_table(sqlite3 *db, const char *package_name)
+{
+       sqlite3_stmt *db_statement = NULL;
+       int sqlite3_ret = SQLITE_OK;
+       int err = true;
+       int field_index = 1;
+
+       sqlite3_ret = sqlite3_prepare_v2(db, "SELECT package_name FROM notification_setting WHERE package_name = ?", -1, &db_statement, NULL);
+
+       if (sqlite3_ret != SQLITE_OK) {
+               NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
+               err = false;
+               goto out;
+       }
+
+       sqlite3_bind_text(db_statement, field_index++, package_name, -1, SQLITE_TRANSIENT);
+
+       sqlite3_ret = sqlite3_step(db_statement);
+
+       if (sqlite3_ret == SQLITE_DONE) {
+               NOTIFICATION_INFO("no matched package_name found[%s][%d]", package_name, sqlite3_ret);
+               err = false;
+               goto out;
+       }
+
+       if (sqlite3_ret != SQLITE_OK) {
+               NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
+               err = false;
+               goto out;
+       }
+out:
+       if (db_statement) {
+               sqlite3_finalize(db_statement);
+       }
+
+       return err;
+}
+
+static bool foreach_package_info_callback(package_info_h package_info, void *user_data)
+{
+       sqlite3 *db = user_data;
+       sqlite3_stmt *db_statement = NULL;
+       char *package_name = NULL;
+       int pkgmgr_ret = PACKAGE_MANAGER_ERROR_NONE;
+       int sqlite3_ret = SQLITE_OK;
+       int field_index = 1;
+       int err = true;
+
+       if ((pkgmgr_ret = package_info_get_package(package_info, &package_name)) != PACKAGE_MANAGER_ERROR_NONE) {
+               NOTIFICATION_ERR("package_info_get_package failed [%d]", pkgmgr_ret);
+               err = false;
+               goto out;
+       }
+
+       if (_is_package_in_setting_table(db, package_name) == true) {
+               NOTIFICATION_INFO("[%s] is exist", package_name);
+               goto out;
+       }
+
+       NOTIFICATION_INFO("[%s] will be inserted", package_name);
+
+       sqlite3_ret = sqlite3_prepare_v2(db, "INSERT INTO notification_setting (package_name) VALUES (?) ", -1, &db_statement, NULL);
+
+       if (sqlite3_ret != SQLITE_OK) {
+               NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
+               err = false;
+               goto out;
+       }
+
+       sqlite3_bind_text(db_statement, field_index++, package_name, -1, SQLITE_TRANSIENT);
+
+       sqlite3_ret = sqlite3_step(db_statement);
+
+       NOTIFICATION_INFO("sqlite3_step returns[%d]", sqlite3_ret);
+
+       if (sqlite3_ret != SQLITE_OK && sqlite3_ret != SQLITE_DONE) {
+               NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
+               err = false;
+       }
+
+out:
+       if (db_statement) {
+               sqlite3_finalize(db_statement);
+       }
+
+       if(package_name) {
+               free(package_name);
+       }
+
+       NOTIFICATION_INFO("foreach_package_info_callback returns[%d]", err);
+       return err;
+}
+
+EXPORT_API int notification_setting_refresh_setting_table()
+{
+       int err = NOTIFICATION_ERROR_NONE;
+       sqlite3 *db = NULL;
+       int sqlite3_ret = SQLITE_OK;
+       int pkgmgr_ret = PACKAGE_MANAGER_ERROR_NONE;
+
+       sqlite3_ret = db_util_open(DBPATH, &db, 0);
+
+       if (sqlite3_ret != SQLITE_OK || db == NULL) {
+               NOTIFICATION_ERR("db_util_open failed [%s][%d]", DBPATH, sqlite3_ret);
+               err = NOTIFICATION_ERROR_FROM_DB;
+               goto out;
+       }
+
+       sqlite3_exec(db, "BEGIN immediate;", NULL, NULL, NULL);
+
+       pkgmgr_ret = package_manager_foreach_package_info(foreach_package_info_callback, db);
+       if (pkgmgr_ret != PACKAGE_MANAGER_ERROR_NONE) {
+               NOTIFICATION_ERR("package_manager_filter_foreach_package_info failed [%d]", pkgmgr_ret);
+               err = NOTIFICATION_ERROR_FROM_DB;
+               goto out;
+       }
+
+out:
+
+       if (db) {
+               if (err == NOTIFICATION_ERROR_NONE) {
+                       sqlite3_exec(db, "END;", NULL, NULL, NULL);
+               }
+               else {
+                       sqlite3_exec(db, "ROLLBACK;", NULL, NULL, NULL);
+               }
+
+               if ((sqlite3_ret = db_util_close(db)) != SQLITE_OK) {
+                       NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlite3_ret);
+               }
+       }
+
+       NOTIFICATION_INFO("notification_setting_refresh_setting_table returns [%d]", err);
+
+       return err;
+}
+
 /* system setting --------------------------------*/
 
 EXPORT_API int notification_system_setting_load_system_setting(notification_system_setting_h *system_setting)
@@ -695,7 +833,7 @@ EXPORT_API int notification_setting_db_update_system_setting(int do_not_disturb,
 {
        int err = NOTIFICATION_ERROR_NONE;
        int sqlret;
-       int field_index = 0;
+       int field_index = 1;
        sqlite3 *db = NULL;
        sqlite3_stmt *db_statement = NULL;
 
index 4069e5e..95693ab 100755 (executable)
@@ -160,6 +160,7 @@ void testapp_show_menu (testapp_menu_type_e menu)
                testapp_print (" 1.  Get setting list\n");
                testapp_print (" 2.  Update setting\n");
                testapp_print (" 3.  Update system setting\n");
+               testapp_print (" 4.  Refresh setting table\n");
                testapp_print ("------------------------------------------\n");
                break;
        default:
@@ -595,7 +596,6 @@ static int testapp_test_update_system_setting()
        int visibility_class;
        notification_system_setting_h system_setting = NULL;
 
-
        err = notification_system_setting_load_system_setting(&system_setting);
 
        if (err != NOTIFICATION_ERROR_NONE || system_setting == NULL) {
@@ -627,6 +627,21 @@ out:
        return err;
 }
 
+static int testapp_test_refresh_setting_table()
+{
+       int err = NOTIFICATION_ERROR_NONE;
+       err = notification_setting_refresh_setting_table();
+
+       if (err != NOTIFICATION_ERROR_NONE) {
+               testapp_print("notification_setting_refresh_setting_table failed [%d]\n", err);
+               goto out;
+       }
+
+out:
+
+       return err;
+}
+
 static gboolean testapp_interpret_command_setting_test (int selected_number)
 {
        gboolean go_to_loop = TRUE;
@@ -644,6 +659,10 @@ static gboolean testapp_interpret_command_setting_test (int selected_number)
                testapp_test_update_system_setting();
                break;
 
+       case 4:
+               testapp_test_refresh_setting_table();
+               break;
+
        case 0:
                go_to_loop = FALSE;
                break;