Add new api to get notification block state 06/84006/22
authorseungha.son <seungha.son@samsung.com>
Tue, 16 Aug 2016 08:11:25 +0000 (17:11 +0900)
committerseungha.son <seungha.son@samsung.com>
Fri, 26 Aug 2016 02:27:52 +0000 (11:27 +0900)
 - The user can set notification block state in Setting application.
   The user can get notification block state that allow or block
   notifications by using this function.
   The "Block" means wether or not the ability to post notifications is
   blocked. But 'Do not disturb mode' means that Only notification on
   notification panel is allowed.

Signed-off-by: seungha.son <seungha.son@samsung.com>
Change-Id: Id19d9b9d4a89d4693b2ae37a74c15b2543b0758a

include/notification.h
include/notification_ipc.h
include/notification_setting_service.h
include/notification_type.h
src/notification.c
src/notification_ipc.c
src/notification_setting_service.c

index 52593eb..2a48877 100755 (executable)
@@ -1519,6 +1519,44 @@ int notification_save_as_template(notification_h noti, const char *template_name
 notification_h notification_create_from_template(const char *template_name);
 
 /**
+ * @brief Gets notification block state.
+ * @details    The user can set the notification block state in settings.
+ *             The block state indicates whether or not notifications can be posted.
+ *             Additionally only notifications to the notification panel are
+ *             allowed in "Do not disturb mode". Sound, Vibrate and
+ *             Active/Instant notifications are blocked.
+ * @since_tizen 3.0
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/notification
+ * @param[out] state Notification block state
+ * @return #NOTIFICATION_ERROR_NONE On success, other value on failure
+ * @retval #NOTIFICATION_ERROR_NONE Success
+ * @retval #NOTIFICATION_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #NOTIFICATION_ERROR_OUT_OF_MEMORY out of memory
+ * @retval #NOTIFICATION_ERROR_IO_ERROR I/O Error
+ * @retval #NOTIFICATION_ERROR_PERMISSION_DENIED Permission denied
+ * @retval #NOTIFICATION_ERROR_SERVICE_NOT_READY No response from notification service
+ * @see #notification_block_state_e
+ * @par Sample code:
+ * @code
+#include <notification.h>
+...
+{
+       int noti_err = NOTIFICATION_ERROR_NONE;
+       notification_block_state_e state;
+
+       ...
+
+       noti_err = notification_get_noti_block_state(&state);
+       if(noti_err != NOTIFICATION_ERROR_NONE) {
+               return;
+       }
+}
+ * @endcode
+ */
+int notification_get_noti_block_state(notification_block_state_e *state);
+
+/**
  * @}
  */
 
index fda5f95..db14a28 100755 (executable)
@@ -87,8 +87,8 @@ int notification_ipc_request_save_as_template(notification_h noti, const char *t
 int notification_ipc_request_create_from_template(notification_h noti, const char *template_name);
 int notification_ipc_request_create_from_package_template(notification_h noti,
                const char *pkgname, const char *template_name);
-
-
+int notification_ipc_get_noti_block_state(const char *pkgname, int *do_not_disturb, int *do_not_disturb_except,
+                                         int *allow_to_notify, uid_t uid);
 
 #ifdef __cplusplus
 }
index 6de7bab..1d810b2 100644 (file)
@@ -39,6 +39,7 @@ int noti_setting_service_get_setting_by_package_name(const char *package_name, n
 int noti_setting_get_setting_array(notification_setting_h *setting_array, int *count, uid_t uid);
 int noti_system_setting_load_system_setting(notification_system_setting_h *system_setting, uid_t uid);
 int notification_system_setting_get_dnd_schedule_enabled_uid(uid_t **uids, int *count);
+int notification_get_dnd_and_allow_to_notify(const char *pkgname, int *do_not_disturb, int *do_not_disturb_except, int *allow_to_notify, uid_t uid);
 #ifdef __cplusplus
 }
 #endif
index 67db1f2..43e0252 100644 (file)
@@ -423,6 +423,16 @@ typedef enum notification_permission_type {
 } notification_permission_type_e;
 
 /**
+ * @brief Enumeration for notification block state.
+ * @since_tizen 3.0
+ */
+typedef enum notification_block_state {
+       NOTIFICATION_BLOCK_STATE_ALLOWED = 0,   /**< The app is allowed to post notifications */
+       NOTIFICATION_BLOCK_STATE_BLOCKED,       /**< The app is not allowed to post any notifications */
+       NOTIFICATION_BLOCK_STATE_DO_NOT_DISTURB /**< User set do not disturb mode */
+} notification_block_state_e;
+
+/**
  * @}
  */
 
index 1cc9f04..480fc19 100755 (executable)
@@ -1231,10 +1231,6 @@ out:
        return err;
 }
 
-
-
-
-
 EXPORT_API int notification_set_property(notification_h noti,
                                                          int flags)
 {
@@ -1867,3 +1863,38 @@ EXPORT_API notification_h notification_create_from_template(const char *template
 
        return noti;
 }
+
+EXPORT_API int notification_get_noti_block_state(notification_block_state_e *state)
+{
+       int ret;
+       char *pkgname;
+       int do_not_disturb;
+       int do_not_disturb_except;
+       int allow_to_notify;
+
+       if (state == NULL)
+               return NOTIFICATION_ERROR_INVALID_PARAMETER;
+
+       pkgname = notification_get_pkgname_by_pid();
+
+       ret = notification_ipc_get_noti_block_state(pkgname, &do_not_disturb, &do_not_disturb_except, &allow_to_notify, getuid());
+
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               if (pkgname)
+                       free(pkgname);
+               return ret;
+       }
+
+       if (allow_to_notify) {
+               *state = NOTIFICATION_BLOCK_STATE_ALLOWED;
+               if (do_not_disturb && !do_not_disturb_except)
+                       *state = NOTIFICATION_BLOCK_STATE_DO_NOT_DISTURB;
+       } else {
+               *state = NOTIFICATION_BLOCK_STATE_BLOCKED;
+       }
+
+       if (pkgname)
+               free(pkgname);
+
+       return ret;
+}
index 1dddb16..3fbb1d3 100755 (executable)
@@ -1423,6 +1423,50 @@ int notification_ipc_request_create_from_package_template(notification_h noti, c
        return result;
 }
 
+int notification_ipc_get_noti_block_state(const char *pkgname, int *do_not_disturb,
+                                         int *do_not_disturb_except, int *allow_to_notify,
+                                         uid_t uid)
+{
+       int ret;
+       int dnd;
+       int dnd_except;
+       int allow;
+       GDBusMessage *reply = NULL;
+       GVariant *body;
+       GVariant *reply_body;
+       GVariant *result_body;
+
+       if (pkgname == NULL)
+               return NOTIFICATION_ERROR_INVALID_PARAMETER;
+
+       ret = _dbus_init();
+       if (ret != NOTIFICATION_ERROR_NONE) {
+               NOTIFICATION_ERR("Can't init dbus %d", ret);
+               return ret;
+       }
+
+       body = g_variant_new("(si)", pkgname, uid);
+
+       ret = _send_sync_noti(body, &reply, "get_noti_block_state");
+
+       if (ret == NOTIFICATION_ERROR_NONE) {
+               reply_body = g_dbus_message_get_body(reply);
+               g_variant_get(reply_body, "(v)", &result_body);
+               g_variant_get(result_body, "(iii)", &dnd, &dnd_except, &allow);
+               *do_not_disturb = dnd;
+               *do_not_disturb_except = dnd_except;
+               *allow_to_notify = allow;
+               g_variant_unref(result_body);
+       }
+
+       if (reply)
+               g_object_unref(reply);
+
+       NOTIFICATION_DBG("notification_ipc_get_noti_block_state done");
+
+       return ret;
+}
+
 EXPORT_API GVariant *notification_ipc_make_gvariant_from_noti(notification_h noti, bool translate)
 {
        NOTIFICATION_DBG("make gvariant from noti");
index 61c3a0a..9202ce5 100644 (file)
@@ -568,3 +568,81 @@ err:
 
        return ret;
 }
+
+EXPORT_API int notification_get_dnd_and_allow_to_notify(const char *pkgname,
+                                                       int *do_not_disturb,
+                                                       int *do_not_disturb_except,
+                                                       int *allow_to_notify,
+                                                       uid_t uid)
+{
+       int ret = NOTIFICATION_ERROR_NONE;
+       int sql_ret;
+       int row_count;
+       int col_count;
+       int col_index;
+       sqlite3 *db;
+       char *query_setting = NULL;
+       char **query_setting_result = NULL;
+       char *query_system_setting = NULL;
+       char **query_system_setting_result = NULL;
+
+       if (pkgname == NULL)
+               return NOTIFICATION_ERROR_INVALID_PARAMETER;
+
+       db = notification_db_open(DBPATH);
+       if (db == NULL)
+               return get_last_result();
+
+       query_setting = sqlite3_mprintf("SELECT allow_to_notify, do_not_disturb_except "
+                                       "FROM %s WHERE package_name = %Q AND uid = %d",
+                                       NOTIFICATION_SETTING_DB_TABLE, pkgname, uid);
+       if (query_setting == NULL) {
+               NOTIFICATION_ERR("fail to alloc query");
+               ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
+               goto out;
+       }
+
+       query_system_setting = sqlite3_mprintf("SELECT do_not_disturb FROM %s WHERE uid = %d",
+                                              NOTIFICATION_SYSTEM_SETTING_DB_TABLE, uid);
+
+       if (query_system_setting == NULL) {
+               NOTIFICATION_ERR("fail to alloc query");
+               ret = NOTIFICATION_ERROR_OUT_OF_MEMORY;
+               goto out;
+       }
+
+       sql_ret = sqlite3_get_table(db, query_setting, &query_setting_result, &row_count, &col_count, NULL);
+       if (sql_ret != SQLITE_OK && sql_ret != -1) {
+               NOTIFICATION_ERR("NOTIFICATION_ERROR_FROM_DB failed [%d][%s]", sql_ret, query_setting);
+               ret = NOTIFICATION_ERROR_FROM_DB;
+               goto out;
+       }
+
+       col_index = col_count;
+       _get_table_field_data_int(query_setting_result, (int *)allow_to_notify, col_index++);
+       _get_table_field_data_int(query_setting_result, (int *)do_not_disturb_except, col_index++);
+
+       sql_ret = sqlite3_get_table(db, query_system_setting, &query_system_setting_result, &row_count, &col_count, NULL);
+       if (sql_ret != SQLITE_OK && sql_ret != -1) {
+               NOTIFICATION_ERR("NOTIFICATION_ERROR_FROM_DB failed [%d][%s]", sql_ret, query_setting);
+               ret = NOTIFICATION_ERROR_FROM_DB;
+               goto out;
+       }
+
+       col_index = col_count;
+       _get_table_field_data_int(query_system_setting_result, (int *)do_not_disturb, col_index++);
+
+out:
+       if (query_setting_result)
+               sqlite3_free_table(query_setting_result);
+       if (query_system_setting_result)
+               sqlite3_free_table(query_system_setting_result);
+       if (query_setting)
+               sqlite3_free(query_setting);
+       if (query_system_setting)
+               sqlite3_free(query_system_setting);
+       if (db)
+               notification_db_close(&db);
+
+       return ret;
+}