Adjust coding rules
[platform/core/api/notification.git] / src / notification_ipc.c
index d04d647..4ce6465 100755 (executable)
@@ -1,9 +1,5 @@
 /*
- *  libnotification
- *
- * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Contact: Seungtaek Chung <seungtaek.chung@samsung.com>, Mi-Ju Lee <miju52.lee@samsung.com>, Xi Zhichan <zhichan.xi@samsung.com>
+ * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,7 +12,6 @@
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- *
  */
 
 #include <stdio.h>
@@ -44,6 +39,7 @@
 #define DBUS_SERVICE_DBUS "org.freedesktop.DBus"
 #define DBUS_PATH_DBUS "/org/freedesktop/DBus"
 #define DBUS_INTERFACE_DBUS "org.freedesktop.DBus"
+#define ERR_BUFFER_SIZE                1024
 
 static const gchar *_bus_name = NULL;
 static GDBusConnection *_gdbus_conn = NULL;
@@ -51,59 +47,6 @@ static int monitor_id = 0;
 static int provider_monitor_id = 0;
 static int is_master_started = 0;
 
-static const char *NOTI_DATA_STRING[] = {
-    "NOTIFICATION_DATA_TYPE_NOTI_TYPE",
-    "NOTIFICATION_DATA_TYPE_LAYOUT",
-    "NOTIFICATION_DATA_TYPE_GROUP_ID",
-    "NOTIFICATION_DATA_TYPE_INTERNAL_GROUP_ID",
-    "NOTIFICATION_DATA_TYPE_PRIV_ID",
-    "NOTIFICATION_DATA_TYPE_CALLER_PKGNAME",
-    "NOTIFICATION_DATA_TYPE_LAUNCH_PKGNAME",
-    "NOTIFICATION_DATA_TYPE_ARGS",
-    "NOTIFICATION_DATA_TYPE_GROUP_ARGS",
-    "NOTIFICATION_DATA_TYPE_EXECUTE_OPTION",
-    "NOTIFICATION_DATA_TYPE_SERVICE_RESPONDING",
-    "NOTIFICATION_DATA_TYPE_SERVICE_SINGLE_LAUNCH",
-    "NOTIFICATION_DATA_TYPE_SERVICE_MULTI_LAUNCH",
-    "NOTIFICATION_DATA_TYPE_BUTTON1_EVENT",
-    "NOTIFICATION_DATA_TYPE_BUTTON2_EVENT",
-    "NOTIFICATION_DATA_TYPE_BUTTON3_EVENT",
-    "NOTIFICATION_DATA_TYPE_BUTTON4_EVENT",
-    "NOTIFICATION_DATA_TYPE_BUTTON5_EVENT",
-    "NOTIFICATION_DATA_TYPE_BUTTON6_EVENT",
-    "NOTIFICATION_DATA_TYPE_ICON_EVENT",
-    "NOTIFICATION_DATA_TYPE_THUMBNAIL_EVENT",
-    "NOTIFICATION_DATA_TYPE_DOMAIN",
-    "NOTIFICATION_DATA_TYPE_DIR",
-    "NOTIFICATION_DATA_TYPE_TEXT",
-    "NOTIFICATION_DATA_TYPE_KEY",
-    "NOTIFICATION_DATA_TYPE_FORMAT_ARGS",
-    "NOTIFICATION_DATA_TYPE_NUM_FORMAT_ARGS",
-    "NOTIFICATION_DATA_TYPE_IMAGE_PATH",
-    "NOTIFICATION_DATA_TYPE_SOUND_TYPE",
-    "NOTIFICATION_DATA_TYPE_SOUND_PATH",
-    "NOTIFICATION_DATA_TYPE_VIBRATION_TYPE",
-    "NOTIFICATION_DATA_TYPE_VIBRATION_PATH",
-    "NOTIFICATION_DATA_TYPE_LED_OPERATION",
-    "NOTIFICATION_DATA_TYPE_LED_ARGB",
-    "NOTIFICATION_DATA_TYPE_LED_ON_MS",
-    "NOTIFICATION_DATA_TYPE_LED_OFF_MS",
-    "NOTIFICATION_DATA_TYPE_TIME",
-    "NOTIFICATION_DATA_TYPE_INSERT_TIME",
-    "NOTIFICATION_DATA_TYPE_FLAGS_FOR_PROPERTY",
-    "NOTIFICATION_DATA_TYPE_DISPLAY_APPLIST",
-    "NOTIFICATION_DATA_TYPE_PROGRESS_SIZE",
-    "NOTIFICATION_DATA_TYPE_PROGRESS_PERCENTAGE",
-    "NOTIFICATION_DATA_TYPE_APP_ICON_PATH",
-    "NOTIFICATION_DATA_TYPE_APP_NAME",
-    "NOTIFICATION_DATA_TYPE_TEMP_TITLE",
-    "NOTIFICATION_DATA_TYPE_TEMP_CONTENT",
-    "NOTIFICATION_DATA_TYPE_TAG",
-    "NOTIFICATION_DATA_TYPE_ONGOING_FLAG",
-    "NOTIFICATION_DATA_TYPE_AUTO_REMOVE",
-};
-
-
 typedef struct _result_cb_item {
        void (*result_cb)(int priv_id, int result, void *data);
        void *data;
@@ -124,23 +67,27 @@ static int _ipc_monitor_register(void);
 static int _ipc_monitor_deregister(void);
 static void _do_deffered_task(void);
 
-static void _print_noti(notification_h noti) {
+static void _print_noti(notification_h noti)
+{
        char *pkgname = NULL;
        char *text = NULL;
        char *content = NULL;
        const char *tag = NULL;
+       notification_vibration_type_e vib_type = NOTIFICATION_VIBRATION_TYPE_NONE;
 
        notification_get_pkgname(noti, &pkgname);
        notification_get_text(noti, NOTIFICATION_TEXT_TYPE_TITLE, &text);
        notification_get_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT, &content);
        notification_get_tag(noti, &tag);
+       notification_get_vibration(noti, &vib_type, NULL);
 
-       NOTIFICATION_DBG("client print_noti  pkgname  = %s ", pkgname );
-       NOTIFICATION_DBG("client print_noti  title  = %s ", text );
-       NOTIFICATION_DBG("client print_noti  content  = %s ", content );
-       NOTIFICATION_DBG("client print_noti  tag  = %s ", tag );
+       NOTIFICATION_DBG("client print_noti  pkgname  = %s ", pkgname);
+       NOTIFICATION_DBG("client print_noti  title  = %s ", text);
+       NOTIFICATION_DBG("client print_noti  content  = %s ", content);
+       NOTIFICATION_DBG("client print_noti  tag  = %s ", tag);
        NOTIFICATION_DBG("client print_noti  priv_id  = %d ", noti->priv_id);
        NOTIFICATION_DBG("client print_noti  vibration_path  = %s ", noti->vibration_path);
+       NOTIFICATION_DBG("client print_noti  vibration_type  = %d ", vib_type);
 }
 
 static inline char *_string_get(char *string)
@@ -193,7 +140,7 @@ int notification_ipc_is_master_ready(void)
                }
        }
 
-       if(result)
+       if (result)
                g_variant_unref(result);
 
        return is_master_started;
@@ -340,6 +287,7 @@ static notification_op *_ipc_create_op(notification_op_type_e type,
 static inline char *_dup_string(const char *string)
 {
        char *ret;
+       char err_buf[ERR_BUFFER_SIZE];
 
        if (string == NULL)
                return NULL;
@@ -348,7 +296,7 @@ static inline char *_dup_string(const char *string)
 
        ret = strdup(string);
        if (!ret)
-               NOTIFICATION_ERR("Error: %s\n", strerror(errno));
+               NOTIFICATION_ERR("Error: %s\n", strerror_r(errno, err_buf, sizeof(err_buf)));
 
        return ret;
 }
@@ -579,16 +527,23 @@ static int _send_sync_noti(GVariant *body, GDBusMessage **reply, char *cmd)
        g_object_unref(msg);
 
        if (!*reply) {
+               ret = NOTIFICATION_ERROR_SERVICE_NOT_READY;
                if (err != NULL) {
                        NOTIFICATION_ERR("No reply. cmd = %s,  error = %s", cmd, err->message);
+                       if (err->code == G_DBUS_ERROR_ACCESS_DENIED)
+                               ret = NOTIFICATION_ERROR_PERMISSION_DENIED;
                        g_error_free(err);
                }
-               return NOTIFICATION_ERROR_SERVICE_NOT_READY;
+               return ret;
        }
 
        if (g_dbus_message_to_gerror(*reply, &err)) {
-               ret = err->code;
-               NOTIFICATION_ERR("_send_sync_noti cmd = %s, error %s", cmd, err->message);
+               if (err->code == G_DBUS_ERROR_ACCESS_DENIED)
+                       ret = NOTIFICATION_ERROR_PERMISSION_DENIED;
+               else
+                       ret = err->code;
+
+               NOTIFICATION_ERR("_send_sync_noti cmd = %s, error %s, err code %d", cmd, err->message, ret);
                g_error_free(err);
                return ret;
        }
@@ -626,9 +581,13 @@ static void _send_message_with_reply_async_cb(GDBusConnection *connection,
                result = NOTIFICATION_ERROR_SERVICE_NOT_READY;
 
        } else if (g_dbus_message_to_gerror(reply, &err)) {
-               result = err->code;
-               g_error_free(err);
+               if (err->code == G_DBUS_ERROR_ACCESS_DENIED)
+                       result = NOTIFICATION_ERROR_PERMISSION_DENIED;
+               else
+                       result = err->code;
+
                NOTIFICATION_ERR("_send_async_noti error %s", err->message);
+               g_error_free(err);
        }
 
        NOTIFICATION_DBG("_send_async_noti done !![%d]", result);
@@ -686,7 +645,7 @@ static int _send_async_noti(GVariant *body, result_cb_item *cb_item, char *cmd)
 int notification_ipc_request_insert(notification_h noti, int *priv_id)
 {
        int result;
-       int id;
+       int id = NOTIFICATION_PRIV_ID_NONE;
        GDBusMessage *reply = NULL;
        GVariant *body;
        GVariant *reply_body;
@@ -720,17 +679,17 @@ int notification_ipc_request_insert(notification_h noti, int *priv_id)
                        *priv_id = id;
        }
 
-       if(reply)
+       if (reply)
                g_object_unref(reply);
 
-       NOTIFICATION_DBG("notification_ipc_request_insert done [priv_id : %d, result: %d]", *priv_id, result);
+       NOTIFICATION_DBG("notification_ipc_request_insert done [priv_id : %d, result: %d]", id, result);
        return result;
 }
 
 int notification_ipc_request_update(notification_h noti)
 {
        int result;
-       int priv_id;
+       int priv_id = NOTIFICATION_PRIV_ID_NONE;
 
        GDBusMessage *reply = NULL;
        GVariant *body;
@@ -754,7 +713,7 @@ int notification_ipc_request_update(notification_h noti)
                g_variant_get(reply_body, "(i)", &priv_id);
        }
 
-       if(reply)
+       if (reply)
                g_object_unref(reply);
 
        NOTIFICATION_DBG("notification_ipc_request_update done [result: %d, priv_id :%d]", result, priv_id);
@@ -816,7 +775,7 @@ int notification_ipc_request_refresh(void)
        body = g_variant_new("(i)", NOTIFICATION_OP_REFRESH);
        result = _send_sync_noti(body, &reply, "refresh_noti");
 
-       if(reply)
+       if (reply)
                g_object_unref(reply);
 
        NOTIFICATION_ERR("notification_ipc_request_refresh done [result: %d]", result);
@@ -845,7 +804,7 @@ int notification_ipc_request_delete_single(notification_type_e type, char *pkgna
                g_variant_get(reply_body, "(i)", &id);
        }
 
-       if(reply)
+       if (reply)
                g_object_unref(reply);
 
        NOTIFICATION_ERR("notification_ipc_request_delete_single done [result: %d]", result);
@@ -866,6 +825,9 @@ int notification_ipc_request_delete_multiple(notification_type_e type, char *pkg
                return result;
        }
 
+       if (!pkgname)
+               pkgname = "";
+
        body = g_variant_new("(si)", pkgname, type);
        result = _send_sync_noti(body, &reply, "del_noti_multiple");
 
@@ -875,7 +837,7 @@ int notification_ipc_request_delete_multiple(notification_type_e type, char *pkg
                NOTIFICATION_ERR("num deleted:%d", num_deleted);
        }
 
-       if(reply)
+       if (reply)
                g_object_unref(reply);
 
        return result;
@@ -895,6 +857,9 @@ int notification_ipc_request_load_noti_by_tag(notification_h noti, const char *p
                return result;
        }
 
+       if (!pkgname)
+               pkgname = "";
+
        body = g_variant_new("(ss)", pkgname, tag);
        result = _send_sync_noti(body, &reply, "load_noti_by_tag");
 
@@ -908,7 +873,7 @@ int notification_ipc_request_load_noti_by_tag(notification_h noti, const char *p
 
        }
 
-       if(reply)
+       if (reply)
                g_object_unref(reply);
 
        NOTIFICATION_DBG("notification_ipc_request_load_noti_by_tag done [result: %d]", result);
@@ -929,6 +894,9 @@ int notification_ipc_request_load_noti_by_priv_id(notification_h noti, const cha
                return result;
        }
 
+       if (!pkgname)
+               pkgname = "";
+
        body = g_variant_new("(si)", pkgname, priv_id);
        result = _send_sync_noti(body, &reply, "load_noti_by_priv_id");
 
@@ -941,7 +909,7 @@ int notification_ipc_request_load_noti_by_priv_id(notification_h noti, const cha
                _print_noti(noti);
        }
 
-       if(reply)
+       if (reply)
                g_object_unref(reply);
 
        NOTIFICATION_DBG("notification_ipc_request_load_noti_by_priv_id done [result: %d]", result);
@@ -963,6 +931,9 @@ int notification_ipc_request_get_count(notification_type_e type,
                return result;
        }
 
+       if (!pkgname)
+               pkgname = "";
+
        body = g_variant_new("(isii)", type, pkgname, group_id, priv_id);
        result = _send_sync_noti(body, &reply, "get_noti_count");
 
@@ -974,7 +945,7 @@ int notification_ipc_request_get_count(notification_type_e type,
                NOTIFICATION_DBG("noti count [%d]", re_count);
        }
 
-       if(reply)
+       if (reply)
                g_object_unref(reply);
 
        NOTIFICATION_DBG("notification_ipc_request_get_count done [result: %d]", result);
@@ -991,6 +962,7 @@ int notification_ipc_request_load_noti_grouping_list(notification_type_e type, i
        GVariant *iter_body;
        GVariantIter *iter;
        notification_h noti;
+       GVariant *noti_body;
 
        result = _dbus_init();
        if (result != NOTIFICATION_ERROR_NONE) {
@@ -1007,14 +979,15 @@ int notification_ipc_request_load_noti_grouping_list(notification_type_e type, i
 
                while (g_variant_iter_loop(iter, "(v)", &iter_body)) {
                        noti = notification_create(NOTIFICATION_TYPE_NOTI);
-                       notification_ipc_make_noti_from_gvariant(noti, iter_body);
+                       g_variant_get(iter_body, "(v)", &noti_body);
+                       notification_ipc_make_noti_from_gvariant(noti, noti_body);
                        _print_noti(noti);
                        *list = notification_list_append(*list, noti);
                }
                g_variant_iter_free(iter);
        }
 
-       if(reply)
+       if (reply)
                g_object_unref(reply);
 
        NOTIFICATION_DBG("notification_ipc_request_load_noti_grouping_list done [result: %d]", result);
@@ -1034,6 +1007,7 @@ int notification_ipc_request_load_noti_detail_list(const char *pkgname,
        GVariant *iter_body;
        GVariantIter *iter;
        notification_h noti;
+       GVariant *noti_body;
 
        result = _dbus_init();
        if (result != NOTIFICATION_ERROR_NONE) {
@@ -1050,14 +1024,15 @@ int notification_ipc_request_load_noti_detail_list(const char *pkgname,
 
                while (g_variant_iter_loop(iter, "(v)", &iter_body)) {
                        noti = notification_create(NOTIFICATION_TYPE_NOTI);
-                       notification_ipc_make_noti_from_gvariant(noti, iter_body);
+                       g_variant_get(iter_body, "(v)", &noti_body);
+                       notification_ipc_make_noti_from_gvariant(noti, noti_body);
                        _print_noti(noti);
                        *list = notification_list_append(*list, noti);
                }
                g_variant_iter_free(iter);
        }
 
-       if(reply)
+       if (reply)
                g_object_unref(reply);
 
        NOTIFICATION_DBG("notification_ipc_request_load_noti_detail_list done [result: %d]", result);
@@ -1111,7 +1086,7 @@ int notification_ipc_request_get_setting_array(
                g_variant_iter_free(iter);
        }
 
-       if(reply)
+       if (reply)
                g_object_unref(reply);
 
        NOTIFICATION_DBG("notification_ipc_request_get_setting_array done [result: %d]", result);
@@ -1154,7 +1129,7 @@ int notification_ipc_request_get_setting_by_package_name(
                g_variant_unref(setting_body);
        }
 
-       if(reply)
+       if (reply)
                g_object_unref(reply);
 
        NOTIFICATION_DBG("notification_ipc_request_get_setting_by_package_name done [result: %d]", result);
@@ -1193,7 +1168,7 @@ int notification_ipc_request_load_system_setting(notification_system_setting_h *
                g_variant_unref(setting_body);
        }
 
-       if(reply)
+       if (reply)
                g_object_unref(reply);
 
        NOTIFICATION_DBG("notification_ipc_request_load_system_setting done [result: %d]", result);
@@ -1220,7 +1195,7 @@ int notification_ipc_update_setting(notification_setting_h setting)
 
        result = _send_sync_noti(body, &reply, "update_noti_setting");
 
-       if(reply)
+       if (reply)
                g_object_unref(reply);
 
        NOTIFICATION_DBG("notification_ipc_update_setting done [result: %d]", result);
@@ -1252,63 +1227,6 @@ int notification_ipc_update_system_setting(notification_system_setting_h system_
        return result;
 }
 
-int notification_ipc_noti_setting_property_set(const char *pkgname, const char *property, const char *value)
-{
-       int result;
-       GDBusMessage *reply = NULL;
-       GVariant *body;
-
-       result = _dbus_init();
-       if (result != NOTIFICATION_ERROR_NONE) {
-               NOTIFICATION_ERR("Can't init dbus %d", result);
-               return result;
-       }
-       body = g_variant_new("(sss)", pkgname, property, value);
-
-       result = _send_sync_noti(body, &reply, "set_noti_property");
-
-       if (reply)
-               g_object_unref(reply);
-
-       NOTIFICATION_DBG("notification_ipc_noti_setting_property_set done [result: %d]", result);
-       return result;
-}
-
-int notification_ipc_noti_setting_property_get(const char *pkgname, const char *property, char **value)
-{
-       int result;
-       GDBusMessage *reply = NULL;
-       GVariant *body;
-       GVariant *reply_body = NULL;
-       gchar *ret_val;
-
-       result = _dbus_init();
-
-       if (result != NOTIFICATION_ERROR_NONE) {
-               NOTIFICATION_ERR("Can't init dbus %d", result);
-               return result;
-       }
-
-       body = g_variant_new("(ss)", pkgname, property);
-       result = _send_sync_noti(body, &reply, "get_noti_property");
-
-       if (result == NOTIFICATION_ERROR_NONE) {
-               reply_body = g_dbus_message_get_body(reply);
-               g_variant_get(body, "(s)", &ret_val);
-
-               if (ret_val != NULL) {
-                       *value = g_strdup(ret_val);
-                       g_free(ret_val);
-               }
-       }
-
-       if(reply)
-               g_object_unref(reply);
-
-       NOTIFICATION_DBG("notification_ipc_noti_setting_property_get done [result: %d]", result);
-       return result;
-}
-
 EXPORT_API GVariant *notification_ipc_make_gvariant_from_noti(notification_h noti)
 {
        NOTIFICATION_DBG("make gvariant from noti");
@@ -1454,8 +1372,7 @@ EXPORT_API GVariant *notification_ipc_make_gvariant_from_noti(notification_h not
        if (noti->sound_path)
                g_variant_builder_add(&builder, "{iv}", NOTIFICATION_DATA_TYPE_SOUND_PATH, g_variant_new_string((const gchar *)noti->sound_path));
 
-       if (noti->vibration_type != NOTIFICATION_VIBRATION_TYPE_NONE)
-               g_variant_builder_add(&builder, "{iv}", NOTIFICATION_DATA_TYPE_VIBRATION_TYPE, g_variant_new_int32(noti->vibration_type));
+       g_variant_builder_add(&builder, "{iv}", NOTIFICATION_DATA_TYPE_VIBRATION_TYPE, g_variant_new_int32(noti->vibration_type));
 
        if (noti->vibration_path)
                g_variant_builder_add(&builder, "{iv}", NOTIFICATION_DATA_TYPE_VIBRATION_PATH, g_variant_new_string((const gchar *)noti->vibration_path));
@@ -1509,8 +1426,8 @@ EXPORT_API GVariant *notification_ipc_make_gvariant_from_noti(notification_h not
        return body;
 }
 
-static gboolean _variant_to_int_dict(GHashTable **dict, GVariant *variant) {
-
+static gboolean _variant_to_int_dict(GHashTable **dict, GVariant *variant)
+{
        GVariantIter iter;
        int key;
        int *hash_key;
@@ -1524,7 +1441,7 @@ static gboolean _variant_to_int_dict(GHashTable **dict, GVariant *variant) {
        while (g_variant_iter_next(&iter, "{iv}", &key, &value)) {
                hash_key = (int *)calloc(sizeof(int), 1);
                if (hash_key == NULL) {
-                       g_hash_table_remove_all(*dict);
+                       g_hash_table_unref(*dict);
                        return FALSE;
                }
                *hash_key = key;
@@ -1678,7 +1595,7 @@ EXPORT_API int notification_ipc_make_noti_from_gvariant(notification_h noti,
        noti->temp_content = _dup_string(temp_content);
        noti->tag = _dup_string(tag);
 
-       g_hash_table_remove_all(dict);
+       g_hash_table_unref(dict);
 
        return NOTIFICATION_ERROR_NONE;
 }
@@ -1773,7 +1690,7 @@ static int _send_service_register()
 
        result = _send_sync_noti(NULL, &reply, "noti_service_register");
 
-       if(reply)
+       if (reply)
                g_object_unref(reply);
 
        NOTIFICATION_ERR("_send_service_register done = %s, result = %d", _bus_name, result);
@@ -1870,3 +1787,4 @@ int notification_ipc_monitor_fini(void)
 {
        return  _ipc_monitor_deregister();
 }
+