app: Call FormatwithType asynchronously 57/150757/2
authorpr.jung <pr.jung@samsung.com>
Mon, 18 Sep 2017 11:22:34 +0000 (20:22 +0900)
committerJung <pr.jung@samsung.com>
Tue, 19 Sep 2017 01:11:56 +0000 (01:11 +0000)
Change-Id: I10d779bbd64d1dd6f856d650df2a42ffea9fc33e
Signed-off-by: pr.jung <pr.jung@samsung.com>
apps/extended-sd/include/dbus-call.h
apps/extended-sd/src/dbus-call.c
apps/extended-sd/src/es-internal-storage-page.c
apps/extended-sd/src/es-portable-storage-page.c

index 2cd3a16..7250ae0 100644 (file)
@@ -39,9 +39,6 @@ struct dbus_int {
        int size;
 };
 
-int register_signal_handler(appdata_s *);
-int unregister_signal_handler(void);
-
 int dbus_method_sync(const char *dest, const char *path,
                const char *interface, const char *method,
                const char *sig, char *param[]);
@@ -50,6 +47,15 @@ int dbus_method_sync_with_reply(const char *dest,
                const char *method, const char *sig,
                char *param[], GVariant **info);
 
+typedef void (*dbus_pending_cb)(void *data, GVariant *result, GError *err);
+
+int dbus_method_async_with_reply(const char *dest, const char *path,
+               const char *interface, const char *method,
+               const char *sig, char *param[], dbus_pending_cb cb, int timeout, void *data);
+
+int register_signal_handler(appdata_s *);
+int unregister_signal_handler(void);
+
 /**
  * If result is NULL, err is set.
  * Do not invoke g_variant_unref() with result.
index 1c4123c..e1ddfff 100644 (file)
 #define DBUS_REPLY_TIMEOUT     (-1)
 #define DBUS_MAXIMUM_NAME_LENGTH 255
 
+struct pending_call_data {
+       dbus_pending_cb func;
+       void *data;
+};
+
 struct proxy_node {
        GDBusProxy *proxy;
        char *dest;
@@ -332,6 +337,80 @@ int dbus_method_sync_with_reply(const char *dest,
        return 0;
 }
 
+static void cb_pending(GDBusProxy *proxy,
+               GAsyncResult *res,
+               gpointer user_data)
+{
+       struct pending_call_data *data = user_data;
+       GError *err = NULL;
+       GVariant *output;
+
+       output = g_dbus_proxy_call_finish(proxy,
+                       res, /* GAsyncResult */
+                       &err);
+       if (!output)
+               DMSG_ERR("g_dbus_proxy_call_finish error : %d-%s",
+                               err->code, err->message);
+
+       if (data && data->func)
+               data->func(data->data, output, err);
+       free(data);
+
+       if (err)
+               g_clear_error(&err);
+       if (output)
+               g_variant_unref(output);
+}
+
+int dbus_method_async_with_reply(const char *dest, const char *path,
+               const char *interface, const char *method,
+               const char *sig, char *param[],
+               dbus_pending_cb cb, int timeout, void *data)
+{
+       GDBusProxy *proxy;
+       GError *err = NULL;
+       struct pending_call_data *pdata;
+       int result;
+
+#if !GLIB_CHECK_VERSION(2, 35, 0)
+       g_type_init();
+#endif
+
+       pthread_mutex_lock(&dmutex);
+       proxy = get_proxy_from_proxy_pool(dest, path, interface, &err);
+       if (!proxy) {
+               pthread_mutex_unlock(&dmutex);
+               DMSG_ERR("fail to get proxy from proxy pool : %s-%s (%d-%s)",
+                               interface, method, err->code, err->message);
+               result = g_dbus_error_to_errno(err->code);
+               g_clear_error(&err);
+               return result;
+       }
+
+       pdata = malloc(sizeof(struct pending_call_data));
+       if (!pdata) {
+               pthread_mutex_unlock(&dmutex);
+               DMSG_ERR("malloc error : %s-%s",
+                               interface, method);
+               return -ENOMEM;
+       }
+
+       pdata->func = cb;
+       pdata->data = data;
+
+       g_dbus_proxy_call(proxy,
+                       method,                          /* method name */
+                       append_g_variant(sig, param),    /* parameters */
+                       G_DBUS_CALL_FLAGS_NONE,
+                       DBUS_REPLY_TIMEOUT,              /* timeout */
+                       NULL,                            /* GCancellable */
+                       (GAsyncReadyCallback)cb_pending, /* GAsyncReadyCallback */
+                       pdata);                          /* user data */
+       pthread_mutex_unlock(&dmutex);
+
+       return 0;
+}
+
 static GDBusConnection *get_dbus_connection(void)
 {
        GError *err = NULL;
index ddf4867..00834c5 100644 (file)
@@ -58,11 +58,55 @@ _format_click_cb(void *data, Evas_Object* obj, void *event_info)
 }
 
 static void
+format_done(void *data, GVariant *result, GError *err)
+{
+       appdata_s* ad = (appdata_s*)data;
+       char str_id[32];
+       char *arr[2];
+       int ret;
+
+       FUNC_BEGIN();
+       ret_if(ad == NULL);
+       ret_if(result == NULL);
+
+       g_variant_get(result, "(i)", &ret);
+       if (ret < 0) {
+               DMSG("Failed to format sd card as portable storage: %d", ret);
+               return;
+       }
+
+       snprintf(str_id, sizeof(str_id), "%d", ad->storage_id);
+       arr[0] = str_id;
+       arr[1] = "ExtendedInternalSD";
+
+       ret = dbus_method_sync(STORAGED_BUS_NAME,
+                       STORAGED_PATH_BLOCK_MANAGER, STORAGED_INTERFACE_BLOCK_MANAGER, "Mount", "is", arr);
+       if (ret < 0) {
+               DMSG("Failed to mount sd card as extended internal storage: %d", ret);
+               return;
+       }
+
+       Evas_Object* page_content = elm_object_part_content_unset(ad->internal_storage_page_data->internal_storage_page_base_layout, "elm.swallow.content");
+       EVAS_OBJECT_DEL(page_content);
+       page_content = create_page_5(ad);
+       elm_object_part_content_set(ad->internal_storage_page_data->internal_storage_page_base_layout, "elm.swallow.content", page_content);
+
+
+       g_variant_unref(result);
+       FUNC_END();
+}
+
+static void
 _continue_click_cb(void *data, Evas_Object* obj, void *event_info)
 {
        FUNC_BEGIN();
 
        appdata_s* ad = (appdata_s*)data;
+       char str_id[32];
+       char str_option[32];
+       char *arr[3];
+       int ret;
+
        ret_if(ad == NULL);
 
        Evas_Object* page_content = elm_object_part_content_unset(ad->internal_storage_page_data->internal_storage_page_base_layout, "elm.swallow.content");
@@ -70,6 +114,23 @@ _continue_click_cb(void *data, Evas_Object* obj, void *event_info)
        page_content = create_page_4(ad);
        elm_object_part_content_set(ad->internal_storage_page_data->internal_storage_page_base_layout, "elm.swallow.content", page_content);
 
+       snprintf(str_id, sizeof(str_id), "%d", ad->storage_id);
+       arr[0] = str_id;
+       snprintf(str_option, sizeof(str_option), "%d", UNMOUNT_FORCE);
+       arr[1] = str_option;
+       arr[2] = "ext4";
+       ret = dbus_method_async_with_reply(STORAGED_BUS_NAME,
+                       STORAGED_PATH_BLOCK_MANAGER,
+                       STORAGED_INTERFACE_BLOCK_MANAGER,
+                       "FormatwithType",
+                       "iis",
+                       arr,
+                       format_done,
+                       -1,
+                       ad);
+       if (ret < 0)
+               DMSG("Failed to format", ret);
+
        FUNC_END();
 }
 
@@ -513,13 +574,7 @@ static Evas_Object* create_page_4(appdata_s* ad)
        FUNC_BEGIN();
 
        Elm_Genlist_Item_Class *extended_multiline_genlist_itc;
-       Evas_Object *page_content;
        genlist_item_s *item;
-       char str_id[32];
-       char str_option[32];
-       char *arr[3];
-       char *arr2[2];
-       int ret;
 
        extended_multiline_genlist_itc = elm_genlist_item_class_new();
        extended_multiline_genlist_itc->item_style = "extended-multiline";
@@ -563,31 +618,6 @@ static Evas_Object* create_page_4(appdata_s* ad)
 
        elm_genlist_item_class_free(extended_multiline_genlist_itc);
 
-       snprintf(str_id, sizeof(str_id), "%d", ad->storage_id);
-       arr[0] = str_id;
-       arr2[0] = str_id;
-       snprintf(str_option, sizeof(str_option), "%d", UNMOUNT_FORCE);
-       arr[1] = str_option;
-       arr[2] = "ext4";
-       ret = dbus_method_sync(STORAGED_BUS_NAME,
-                       STORAGED_PATH_BLOCK_MANAGER, STORAGED_INTERFACE_BLOCK_MANAGER, "FormatwithType", "iis", arr);
-       if (ret < 0)
-               DMSG("Failed to format sd card as extended internal storage: %d", ret);
-
-       arr2[1] = "ExtendedInternalSD";
-       ret = dbus_method_sync(STORAGED_BUS_NAME,
-                       STORAGED_PATH_BLOCK_MANAGER, STORAGED_INTERFACE_BLOCK_MANAGER, "Mount", "is", arr2);
-       if (ret < 0)
-               DMSG("Failed to mount sd card as extended internal storage: %d", ret);
-
-       page_content = elm_object_part_content_unset(ad->internal_storage_page_data->internal_storage_page_base_layout, "elm.swallow.content");
-       EVAS_OBJECT_DEL(page_content);
-       page_content = create_page_5(ad);
-       elm_object_part_content_set(ad->internal_storage_page_data->internal_storage_page_base_layout, "elm.swallow.content", page_content);
-
-       FUNC_END();
-       return page_content;
-
 out:
        FUNC_END();
        return genlist;
index 5699031..01077f6 100644 (file)
@@ -30,9 +30,52 @@ static Evas_Object* create_page_2(appdata_s* ad);
 static Evas_Object* create_page_3(appdata_s* ad);
 
 static void
+format_done(void *data, GVariant *result, GError *err)
+{
+       appdata_s* ad = (appdata_s*)data;
+       char str_id[32];
+       char *arr[2];
+       int ret;
+
+       FUNC_BEGIN();
+       ret_if(ad == NULL);
+       ret_if(result == NULL);
+
+       g_variant_get(result, "(i)", &ret);
+       if (ret < 0) {
+               DMSG("Failed to format sd card as portable storage: %d", ret);
+               return;
+       }
+
+       snprintf(str_id, sizeof(str_id), "%d", ad->storage_id);
+       arr[0] = str_id;
+       arr[1] = "";
+
+       ret = dbus_method_sync(STORAGED_BUS_NAME,
+                       STORAGED_PATH_BLOCK_MANAGER, STORAGED_INTERFACE_BLOCK_MANAGER, "Mount", "is", arr);
+       if (ret < 0) {
+               DMSG("Failed to mount sd card as portable storage: %d", ret);
+               return;
+       }
+
+       Evas_Object* page_content = elm_object_part_content_unset(ad->portable_storage_page_data->portable_storage_page_base_layout, "elm.swallow.content");
+       EVAS_OBJECT_DEL(page_content);
+       page_content = create_page_3(ad);
+       elm_object_part_content_set(ad->portable_storage_page_data->portable_storage_page_base_layout, "elm.swallow.content", page_content);
+
+       g_variant_unref(result);
+       FUNC_END();
+}
+
+static void
 _format_click_cb(void *data, Evas_Object* obj, void *event_info)
 {
        FUNC_BEGIN();
+       char str_id[32];
+       char str_option[32];
+       char *arr[3];
+       int ret;
+
 
        appdata_s* ad = (appdata_s*)data;
        ret_if(ad == NULL);
@@ -42,7 +85,26 @@ _format_click_cb(void *data, Evas_Object* obj, void *event_info)
        page_content = create_page_2(ad);
        elm_object_part_content_set(ad->portable_storage_page_data->portable_storage_page_base_layout, "elm.swallow.content", page_content);
 
+
+       snprintf(str_id, sizeof(str_id), "%d", ad->storage_id);
+       arr[0] = str_id;
+       snprintf(str_option, sizeof(str_option), "%d", UNMOUNT_FORCE);
+       arr[1] = str_option;
+       arr[2] = "vfat";
+       ret = dbus_method_async_with_reply(STORAGED_BUS_NAME,
+                       STORAGED_PATH_BLOCK_MANAGER,
+                       STORAGED_INTERFACE_BLOCK_MANAGER,
+                       "FormatwithType",
+                       "iis",
+                       arr,
+                       format_done,
+                       -1,
+                       ad);
+       if (ret < 0)
+               DMSG("Failed to format", ret);
+
        FUNC_END();
+
 }
 
 static void
@@ -367,13 +429,7 @@ static Evas_Object* create_page_2(appdata_s* ad)
        FUNC_BEGIN();
 
        Elm_Genlist_Item_Class *extended_multiline_genlist_itc;
-       Evas_Object *page_content;
        genlist_item_s *item;
-       char str_id[32];
-       char str_option[32];
-       char *arr[3];
-       char *arr2[2];
-       int ret;
 
        extended_multiline_genlist_itc = elm_genlist_item_class_new();
        extended_multiline_genlist_itc->item_style = "extended-multiline";
@@ -417,30 +473,6 @@ static Evas_Object* create_page_2(appdata_s* ad)
 
        elm_genlist_item_class_free(extended_multiline_genlist_itc);
 
-       snprintf(str_id, sizeof(str_id), "%d", ad->storage_id);
-       arr[0] = str_id;
-       arr2[0] = str_id;
-       snprintf(str_option, sizeof(str_option), "%d", UNMOUNT_FORCE);
-       arr[1] = str_option;
-       arr[2] = "vfat";
-       ret = dbus_method_sync(STORAGED_BUS_NAME,
-                       STORAGED_PATH_BLOCK_MANAGER, STORAGED_INTERFACE_BLOCK_MANAGER, "FormatwithType", "iis", arr);
-       if (ret < 0)
-               _D("Failed to format sd card as portable storage: %d", ret);
-
-       arr2[1] = "";
-       ret = dbus_method_sync(STORAGED_BUS_NAME,
-                       STORAGED_PATH_BLOCK_MANAGER, STORAGED_INTERFACE_BLOCK_MANAGER, "Mount", "is", arr2);
-       if (ret < 0)
-               _D("Failed to mount sd card as portable storage: %d", ret);
-
-       page_content = elm_object_part_content_unset(ad->portable_storage_page_data->portable_storage_page_base_layout, "elm.swallow.content");
-       EVAS_OBJECT_DEL(page_content);
-       page_content = create_page_3(ad);
-       elm_object_part_content_set(ad->portable_storage_page_data->portable_storage_page_base_layout, "elm.swallow.content", page_content);
-
-       FUNC_END();
-       return page_content;
 out:
        FUNC_END();
        return genlist;