Add sync apis for shortcut add/delete 85/177685/7
authormk5004.lee <mk5004.lee@samsung.com>
Thu, 3 May 2018 02:09:29 +0000 (11:09 +0900)
committermk5004.lee <mk5004.lee@samsung.com>
Tue, 8 May 2018 11:44:52 +0000 (20:44 +0900)
Change-Id: I997ba95b080004aaf892d5a729db0d5fddca7ca2
Signed-off-by: mk5004.lee <mk5004.lee@samsung.com>
lib/include/shortcut_internal.h
lib/src/shortcut_internal.c
lib/src/shortcut_manager.c

index f40263c..369296a 100755 (executable)
@@ -37,6 +37,8 @@ extern "C" {
  * @{
  */
 
+#define SHORTCUT_IS_WIDGET_SIZE(size) (!!((size) & WIDGET_SIZE_DEFAULT))
+
 struct result_cb_item {
        result_internal_cb result_internal_cb;
        result_cb result_cb;
@@ -55,6 +57,14 @@ void _set_remove_cb(shortcut_remove_cb remove_cb, void *data);
 void _unset_request_cb(void);
 void _unset_remove_cb(void);
 int _dbus_set_watch_name();
+char *_make_request_id(void);
+int _ready_to_send(char **appid, char **request_id);
+int shortcut_add_to_home_sync(const char *name, shortcut_type type,
+               const char *uri, const char *icon, int allow_duplicate);
+int shortcut_add_to_home_widget_sync(const char *name,
+               shortcut_widget_size_e size, const char *widget_id,
+               const char *icon, double period, int allow_duplicate);
+int shortcut_remove_from_home_sync(const char *name);
 
 /**
  * @}
index eed9723..1ea84e4 100755 (executable)
@@ -31,6 +31,8 @@
 #include "shortcut_internal.h"
 
 #define SHORTCUT_PKGNAME_LEN 512
+#define REQUEST_ID_LEN 40
+#define TIMEOUT 4000
 
 #define PROVIDER_BUS_NAME "org.tizen.data_provider_service"
 #define PROVIDER_OBJECT_PATH "/org/tizen/data_provider_service"
@@ -69,8 +71,7 @@ static void _shortcut_send_return(int ret_val, const char *request_id)
        }
 
        _send_sync_shortcut(g_variant_new("(is)", ret_val, request_id),
-                           &reply,
-                           "send_return_value");
+                           &reply, "send_return_value");
        if (reply)
                g_object_unref(reply);
 }
@@ -95,8 +96,8 @@ static void _add_shortcut_notify(GVariant *parameters)
                ret = _request_callback_info.request_cb(appid, name, type, content, icon, sender_pid, -1.0f, allow_duplicate, _request_callback_info.data);
        else
                SHORTCUT_DBG("request_cb is null.");
-       _shortcut_send_return(ret, request_id);
 
+       _shortcut_send_return(ret, request_id);
 }
 /* LCOV_EXCL_STOP */
 
@@ -121,6 +122,7 @@ static void _add_shortcut_widget_notify(GVariant *parameters)
                ret = _request_callback_info.request_cb(appid, name, type, content, icon, sender_pid, period, allow_duplicate, _request_callback_info.data);
        else
                SHORTCUT_DBG("request_cb is null.");
+
        _shortcut_send_return(ret, request_id);
 }
 /* LCOV_EXCL_STOP */
@@ -140,6 +142,7 @@ static void _remove_shortcut_notify(GVariant *parameters)
                ret = _remove_callback_info.remove_cb(appid, name, sender_pid, _remove_callback_info.data);
        else
                SHORTCUT_DBG("remove_cb is null.");
+
        _shortcut_send_return(ret, request_id);
 }
 
@@ -195,10 +198,10 @@ int _dbus_signal_init()
                id = g_dbus_connection_signal_subscribe(
                                _gdbus_conn,
                                PROVIDER_BUS_NAME,
-                               PROVIDER_SHORTCUT_INTERFACE_NAME,       /*    interface */
-                               NULL,                                   /*    member */
-                               PROVIDER_OBJECT_PATH,                   /*    path */
-                               NULL,                                   /*    arg0 */
+                               PROVIDER_SHORTCUT_INTERFACE_NAME,       /* interface */
+                               NULL,                                   /* member */
+                               PROVIDER_OBJECT_PATH,                   /* path */
+                               NULL,                                   /* arg0 */
                                G_DBUS_SIGNAL_FLAGS_NONE,
                                _handle_shortcut_notify,
                                NULL,
@@ -292,7 +295,7 @@ int _send_sync_shortcut(GVariant *body, GDBusMessage **reply, char *cmd)
                        _gdbus_conn,
                        msg,
                        G_DBUS_SEND_MESSAGE_FLAGS_NONE,
-                       -1,
+                       TIMEOUT,
                        NULL,
                        NULL,
                        &err);
@@ -542,3 +545,211 @@ int _dbus_set_watch_name()
 
        return SHORTCUT_ERROR_NONE;
 }
+
+char *_make_request_id(void)
+{
+       static int id = 0;
+       char request_id[REQUEST_ID_LEN];
+
+       g_atomic_int_inc(&id);
+       snprintf(request_id, sizeof(request_id), "%d@%d", getpid(), id);
+
+       SHORTCUT_DBG("The request_id of shortcut is [%s]", request_id);
+
+       return strdup(request_id);
+}
+
+int _ready_to_send(char **appid, char **request_id)
+{
+       int ret;
+
+       ret = _dbus_init();
+       if (ret != SHORTCUT_ERROR_NONE) {
+               /* LCOV_EXCL_START */
+               SHORTCUT_ERR("Can't init dbus %d", ret);
+               return ret;
+               /* LCOV_EXCL_STOP */
+       }
+
+       ret = _check_privilege();
+       if (ret != SHORTCUT_ERROR_NONE)
+               return ret;
+
+       *appid = _shortcut_get_pkgname_by_pid();
+       if (*appid == NULL) {
+               /* LCOV_EXCL_START */
+               SHORTCUT_ERR("Can't get appid");
+               return SHORTCUT_ERROR_IO_ERROR;
+               /* LCOV_EXCL_STOP */
+       }
+
+       *request_id = _make_request_id();
+       if (*request_id == NULL) {
+               SHORTCUT_ERR("Can't get request_id");
+               free(*appid);
+               return SHORTCUT_ERROR_OUT_OF_MEMORY;
+       }
+
+       return SHORTCUT_ERROR_NONE;
+}
+
+EAPI int shortcut_add_to_home_sync(const char *name, shortcut_type type,
+               const char *uri, const char *icon, int allow_duplicate)
+{
+       int ret;
+       char *appid;
+       char *request_id = NULL;
+       GVariant *body;
+       GVariant *reply_body;
+       GDBusMessage *reply = NULL;
+
+       CHECK_SHORTCUT_FEATURE();
+
+       if (ADD_TO_HOME_IS_DYNAMICBOX(type)) {
+               /* LCOV_EXCL_START */
+               SHORTCUT_ERR("Invalid type used for adding a shortcut\n");
+               return SHORTCUT_ERROR_INVALID_PARAMETER;
+               /* LCOV_EXCL_STOP */
+       }
+
+       ret = _ready_to_send(&appid, &request_id);
+       if (ret != SHORTCUT_ERROR_NONE) {
+               /* LCOV_EXCL_START */
+               SHORTCUT_ERR("ready to send error [%d]", ret);
+               return ret;
+               /* LCOV_EXCL_STOP */
+       }
+
+       if (!name)
+               name = "";
+
+       if (!uri)
+               uri = "";
+
+       if (!icon)
+               icon = "";
+
+       body = g_variant_new("(sississi)", request_id, getpid(), appid, name,
+                       type, uri, icon, allow_duplicate);
+
+       ret = _send_sync_shortcut(body, &reply, "add_shortcut");
+       if (ret == SHORTCUT_ERROR_NONE) {
+               reply_body = g_dbus_message_get_body(reply);
+               g_variant_get(reply_body, "(i)", &ret);
+       }
+
+       if (appid)
+               free(appid);
+       if (body)
+               g_variant_unref(body);
+       if (request_id)
+               free(request_id);
+       if (reply)
+               g_object_unref(reply);
+
+       SHORTCUT_DBG("result[%d]", ret);
+
+       return ret;
+}
+
+EAPI int shortcut_add_to_home_widget_sync(const char *name,
+               shortcut_widget_size_e size, const char *widget_id,
+               const char *icon, double period, int allow_duplicate)
+{
+       int ret;
+       char *appid;
+       char *request_id = NULL;
+       GVariant *body;
+       GVariant *reply_body;
+       GDBusMessage *reply = NULL;
+
+       CHECK_SHORTCUT_FEATURE();
+
+       if (name == NULL) {
+               SHORTCUT_ERR("AppID is null\n");
+               return SHORTCUT_ERROR_INVALID_PARAMETER;
+       }
+
+       if (!SHORTCUT_IS_WIDGET_SIZE(size)) {
+               /* LCOV_EXCL_START */
+               SHORTCUT_ERR("Invalid type used for adding a widget\n");
+               return SHORTCUT_ERROR_INVALID_PARAMETER;
+               /* LCOV_EXCL_STOP */
+       }
+
+       ret = _ready_to_send(&appid, &request_id);
+       if (ret != SHORTCUT_ERROR_NONE) {
+               /* LCOV_EXCL_START */
+               SHORTCUT_ERR("ready to send error [%d]", ret);
+               return ret;
+               /* LCOV_EXCL_STOP */
+       }
+
+       body = g_variant_new("(sississdi)", request_id, getpid(), widget_id,
+                       name, size, NULL, icon, period, allow_duplicate);
+
+       ret = _send_sync_shortcut(body, &reply, "add_shortcut_widget");
+       if (ret == SHORTCUT_ERROR_NONE) {
+               reply_body = g_dbus_message_get_body(reply);
+               g_variant_get(reply_body, "(i)", &ret);
+       }
+
+       if (appid)
+               free(appid);
+       if (body)
+               g_variant_unref(body);
+       if (request_id)
+               free(request_id);
+       if (reply)
+               g_object_unref(reply);
+
+       SHORTCUT_DBG("result[%d]", ret);
+
+       return ret;
+}
+
+EAPI int shortcut_remove_from_home_sync(const char *name)
+{
+       int ret;
+       char *appid;
+       char *request_id = NULL;
+       GVariant *body;
+       GVariant *reply_body;
+       GDBusMessage *reply = NULL;
+
+       CHECK_SHORTCUT_FEATURE();
+
+       if (name == NULL) {
+               SHORTCUT_ERR("name is NULL.");
+               return SHORTCUT_ERROR_INVALID_PARAMETER;
+       }
+
+       ret = _ready_to_send(&appid, &request_id);
+       if (ret != SHORTCUT_ERROR_NONE) {
+               /* LCOV_EXCL_START */
+               SHORTCUT_ERR("ready to send error [%d]", ret);
+               return ret;
+               /* LCOV_EXCL_STOP */
+       }
+
+       body = g_variant_new("(siss)", request_id, getpid(), appid, name);
+
+       ret = _send_sync_shortcut(body, &reply, "remove_shortcut");
+       if (ret == SHORTCUT_ERROR_NONE) {
+               reply_body = g_dbus_message_get_body(reply);
+               g_variant_get(reply_body, "(i)", &ret);
+       }
+
+       if (appid)
+               free(appid);
+       if (body)
+               g_variant_unref(body);
+       if (request_id)
+               free(request_id);
+       if (reply)
+               g_object_unref(reply);
+
+       SHORTCUT_DBG("result[%d]", ret);
+
+       return ret;
+}
index e069d6a..6499489 100755 (executable)
@@ -33,9 +33,6 @@
 #include "shortcut_manager.h"
 #include "shortcut_internal.h"
 
-#define SHORTCUT_IS_WIDGET_SIZE(size)           (!!((size) & WIDGET_SIZE_DEFAULT))
-#define REQUEST_ID_LEN 40
-
 static int __shortcut_init_ipc_process()
 {
        int ret;
@@ -141,21 +138,9 @@ EAPI void shortcut_unset_remove_cb(void)
        set_last_result(SHORTCUT_ERROR_NONE);
 }
 
-static char *_make_request_id()
-{
-       static int id = 0;
-       char request_id[REQUEST_ID_LEN] = { 0, };
-
-       g_atomic_int_inc(&id);
-       snprintf(request_id, sizeof(request_id), "%d@%d", getpid(), id);
-
-       SHORTCUT_DBG("The request_id of shortcut is [%s]", request_id);
-
-       return strdup(request_id);
-}
-
-EAPI int shortcut_add_to_home(const char *name, shortcut_type type, const char *uri,
-               const char *icon, int allow_duplicate, result_cb cb, void *data)
+EAPI int shortcut_add_to_home(const char *name, shortcut_type type,
+               const char *uri, const char *icon, int allow_duplicate,
+               result_cb cb, void *data)
 {
        struct result_cb_item *item;
        char *appid;
@@ -172,26 +157,14 @@ EAPI int shortcut_add_to_home(const char *name, shortcut_type type, const char *
                /* LCOV_EXCL_STOP */
        }
 
-       ret = _dbus_init();
+       ret = _ready_to_send(&appid, &request_id);
        if (ret != SHORTCUT_ERROR_NONE) {
                /* LCOV_EXCL_START */
-               SHORTCUT_ERR("Can't init dbus %d", ret);
+               SHORTCUT_ERR("ready to send error [%d]", ret);
                return ret;
                /* LCOV_EXCL_STOP */
        }
 
-       ret = _check_privilege();
-       if (ret != SHORTCUT_ERROR_NONE)
-               return ret;
-
-       appid = _shortcut_get_pkgname_by_pid();
-       if (appid == NULL) {
-               /* LCOV_EXCL_START */
-               SHORTCUT_ERR("Can't get appid");
-               return SHORTCUT_ERROR_IO_ERROR;
-               /* LCOV_EXCL_STOP */
-       }
-
        item = malloc(sizeof(struct result_cb_item));
        if (!item) {
                /* LCOV_EXCL_START */
@@ -216,8 +189,8 @@ EAPI int shortcut_add_to_home(const char *name, shortcut_type type, const char *
        if (!icon)
                icon = "";
 
-       request_id = _make_request_id();
-       body = g_variant_new("(sississi)", request_id, getpid(), appid, name, type, uri, icon, allow_duplicate);
+       body = g_variant_new("(sississi)", request_id, getpid(), appid, name,
+                       type, uri, icon, allow_duplicate);
 
        ret = _send_async_shortcut(body, item, "add_shortcut");
        if (ret != SHORTCUT_ERROR_NONE) {
@@ -237,8 +210,9 @@ EAPI int shortcut_add_to_home(const char *name, shortcut_type type, const char *
        return ret;
 }
 
-EAPI int shortcut_add_to_home_widget(const char *name, shortcut_widget_size_e size, const char *widget_id,
-               const char *icon, double period, int allow_duplicate, result_cb cb, void *data)
+EAPI int shortcut_add_to_home_widget(const char *name, shortcut_widget_size_e size,
+               const char *widget_id, const char *icon, double period,
+               int allow_duplicate, result_cb cb, void *data)
 {
        struct result_cb_item *item;
        char *appid;
@@ -260,26 +234,14 @@ EAPI int shortcut_add_to_home_widget(const char *name, shortcut_widget_size_e si
                /* LCOV_EXCL_STOP */
        }
 
-       ret = _dbus_init();
+       ret = _ready_to_send(&appid, &request_id);
        if (ret != SHORTCUT_ERROR_NONE) {
                /* LCOV_EXCL_START */
-               SHORTCUT_ERR("Can't init dbus %d", ret);
+               SHORTCUT_ERR("ready to send error [%d]", ret);
                return ret;
                /* LCOV_EXCL_STOP */
        }
 
-       ret = _check_privilege();
-       if (ret != SHORTCUT_ERROR_NONE)
-               return ret;
-
-       appid = _shortcut_get_pkgname_by_pid();
-       if (appid == NULL) {
-               /* LCOV_EXCL_START */
-               SHORTCUT_ERR("Can't get appid");
-               return SHORTCUT_ERROR_IO_ERROR;
-               /* LCOV_EXCL_STOP */
-       }
-
        item = malloc(sizeof(struct result_cb_item));
        if (!item) {
                /* LCOV_EXCL_START */
@@ -295,8 +257,9 @@ EAPI int shortcut_add_to_home_widget(const char *name, shortcut_widget_size_e si
        item->result_internal_cb = NULL;
        item->data = data;
 
-       request_id = _make_request_id();
-       body = g_variant_new("(sississdi)", request_id, getpid(), widget_id, name, size, NULL, icon, period, allow_duplicate);
+       body = g_variant_new("(sississdi)", request_id, getpid(), widget_id,
+                       name, size, NULL, icon, period, allow_duplicate);
+
        ret = _send_async_shortcut(body, item, "add_shortcut_widget");
 
        if (ret != SHORTCUT_ERROR_NONE) {
@@ -331,26 +294,14 @@ EAPI int shortcut_remove_from_home(const char *name, result_cb cb, void *user_da
                return SHORTCUT_ERROR_INVALID_PARAMETER;
        }
 
-       ret = _dbus_init();
+       ret = _ready_to_send(&appid, &request_id);
        if (ret != SHORTCUT_ERROR_NONE) {
                /* LCOV_EXCL_START */
-               SHORTCUT_ERR("Can't init dbus %d", ret);
+               SHORTCUT_ERR("ready to send error [%d]", ret);
                return ret;
                /* LCOV_EXCL_STOP */
        }
 
-       ret = _check_privilege();
-       if (ret != SHORTCUT_ERROR_NONE)
-               return ret;
-
-       appid = _shortcut_get_pkgname_by_pid();
-       if (appid == NULL) {
-               /* LCOV_EXCL_START */
-               SHORTCUT_ERR("Can't get appid");
-               return SHORTCUT_ERROR_IO_ERROR;
-               /* LCOV_EXCL_STOP */
-       }
-
        item = malloc(sizeof(struct result_cb_item));
        if (!item) {
                /* LCOV_EXCL_START */
@@ -366,7 +317,6 @@ EAPI int shortcut_remove_from_home(const char *name, result_cb cb, void *user_da
        item->result_internal_cb = NULL;
        item->data = user_data;
 
-       request_id = _make_request_id();
        body = g_variant_new("(siss)", request_id, getpid(), appid, name);
 
        ret = _send_async_shortcut(body, item, "remove_shortcut");