Blame SOURCES/0006-ovirt-foreign-menu-Set-new-ISO-name-using-GTask-API.patch

1efd99
From e7be486b866af394ca63edf339ec0f465b644137 Mon Sep 17 00:00:00 2001
1efd99
From: "Eduardo Lima (Etrunko)" <etrunko@redhat.com>
1efd99
Date: Fri, 13 Jan 2017 18:02:25 -0200
1efd99
Subject: [PATCH 06/26] ovirt-foreign-menu: Set new ISO name using GTask API
1efd99
1efd99
This is done with the new ovirt_foreign_menu_set_current_iso_name_async
1efd99
function.
1efd99
1efd99
Signed-off-by: Eduardo Lima (Etrunko) <etrunko@redhat.com>
1efd99
---
1efd99
 src/ovirt-foreign-menu.c | 117 ++++++++++++++++++++++++++++++++++-------------
1efd99
 src/ovirt-foreign-menu.h |  10 ++++
1efd99
 2 files changed, 96 insertions(+), 31 deletions(-)
1efd99
1efd99
diff --git a/src/ovirt-foreign-menu.c b/src/ovirt-foreign-menu.c
1efd99
index a51f2c9..a422ebb 100644
1efd99
--- a/src/ovirt-foreign-menu.c
1efd99
+++ b/src/ovirt-foreign-menu.c
1efd99
@@ -344,38 +344,102 @@ menu_item_set_active_no_signal(GtkMenuItem *menuitem,
1efd99
 }
1efd99
 
1efd99
 
1efd99
-static void updated_cdrom_cb(GObject *source_object,
1efd99
-                             GAsyncResult *result,
1efd99
-                             gpointer user_data)
1efd99
+static void iso_name_set_cb(GObject *source_object,
1efd99
+                            GAsyncResult *result,
1efd99
+                            gpointer user_data)
1efd99
 {
1efd99
     GError *error = NULL;
1efd99
-    OvirtForeignMenu *foreign_menu;
1efd99
+    GTask *task = G_TASK(user_data);
1efd99
+    OvirtForeignMenu *foreign_menu = OVIRT_FOREIGN_MENU(g_task_get_source_object(task));
1efd99
     gboolean updated;
1efd99
 
1efd99
-    foreign_menu = OVIRT_FOREIGN_MENU(user_data);
1efd99
     updated = ovirt_cdrom_update_finish(OVIRT_CDROM(source_object),
1efd99
                                         result, &error);
1efd99
-    g_debug("Finished updating cdrom content");
1efd99
     if (updated) {
1efd99
+        g_debug("Finished updating cdrom content: %s", foreign_menu->priv->next_iso_name);
1efd99
         g_free(foreign_menu->priv->current_iso_name);
1efd99
         foreign_menu->priv->current_iso_name = foreign_menu->priv->next_iso_name;
1efd99
         foreign_menu->priv->next_iso_name = NULL;
1efd99
-        g_object_notify(G_OBJECT(foreign_menu), "file");
1efd99
+        g_task_return_boolean(task, TRUE);
1efd99
+        goto end;
1efd99
+    }
1efd99
+
1efd99
+    /* Reset old state back as we were not successful in switching to
1efd99
+     * the new ISO */
1efd99
+    g_debug("setting OvirtCdrom:file back to '%s'",
1efd99
+            foreign_menu->priv->current_iso_name);
1efd99
+    g_object_set(foreign_menu->priv->cdrom, "file",
1efd99
+                 foreign_menu->priv->current_iso_name, NULL);
1efd99
+    g_clear_pointer(&foreign_menu->priv->next_iso_name, g_free);
1efd99
+
1efd99
+    if (error != NULL) {
1efd99
+        g_warning("failed to update cdrom resource: %s", error->message);
1efd99
+        g_task_return_error(task, error);
1efd99
     } else {
1efd99
-        /* Reset old state back as we were not successful in switching to
1efd99
-         * the new ISO */
1efd99
-        const char *current_file = foreign_menu->priv->current_iso_name;
1efd99
+        g_warn_if_reached();
1efd99
+        g_task_return_new_error(task, OVIRT_ERROR, OVIRT_ERROR_FAILED,
1efd99
+                                "failed to update cdrom resource");
1efd99
+    }
1efd99
 
1efd99
-        if (error != NULL) {
1efd99
-            g_warning("failed to update cdrom resource: %s", error->message);
1efd99
-            g_clear_error(&error);
1efd99
-        }
1efd99
-        g_debug("setting OvirtCdrom:file back to '%s'",
1efd99
-                current_file?current_file:NULL);
1efd99
-        g_object_set(foreign_menu->priv->cdrom, "file", current_file, NULL);
1efd99
+end:
1efd99
+    g_object_unref(task);
1efd99
+}
1efd99
+
1efd99
+
1efd99
+void ovirt_foreign_menu_set_current_iso_name_async(OvirtForeignMenu *foreign_menu,
1efd99
+                                                   const char *name,
1efd99
+                                                   GCancellable *cancellable,
1efd99
+                                                   GAsyncReadyCallback callback,
1efd99
+                                                   gpointer user_data)
1efd99
+{
1efd99
+    GTask *task;
1efd99
+
1efd99
+    g_return_if_fail(foreign_menu->priv->cdrom != NULL);
1efd99
+    g_return_if_fail(foreign_menu->priv->next_iso_name == NULL);
1efd99
+
1efd99
+    if (name) {
1efd99
+        g_debug("Updating VM cdrom image to '%s'", name);
1efd99
+        foreign_menu->priv->next_iso_name = g_strdup(name);
1efd99
+    } else {
1efd99
+        g_debug("Removing current cdrom image");
1efd99
+        foreign_menu->priv->next_iso_name = NULL;
1efd99
     }
1efd99
 
1efd99
-    g_clear_pointer(&foreign_menu->priv->next_iso_name, g_free);
1efd99
+    g_object_set(foreign_menu->priv->cdrom,
1efd99
+                 "file", name,
1efd99
+                 NULL);
1efd99
+
1efd99
+    task = g_task_new(foreign_menu, cancellable, callback, user_data);
1efd99
+    ovirt_cdrom_update_async(foreign_menu->priv->cdrom, TRUE,
1efd99
+                             foreign_menu->priv->proxy, cancellable,
1efd99
+                             iso_name_set_cb, task);
1efd99
+}
1efd99
+
1efd99
+
1efd99
+gboolean ovirt_foreign_menu_set_current_iso_name_finish(OvirtForeignMenu *foreign_menu,
1efd99
+                                                        GAsyncResult *result,
1efd99
+                                                        GError **error)
1efd99
+{
1efd99
+    g_return_val_if_fail(OVIRT_IS_FOREIGN_MENU(foreign_menu), FALSE);
1efd99
+    return g_task_propagate_boolean(G_TASK(result), error);
1efd99
+}
1efd99
+
1efd99
+
1efd99
+static void
1efd99
+ovirt_foreign_menu_iso_name_changed(GObject *source_object,
1efd99
+                                    GAsyncResult *result,
1efd99
+                                    gpointer user_data G_GNUC_UNUSED)
1efd99
+{
1efd99
+    OvirtForeignMenu *foreign_menu = OVIRT_FOREIGN_MENU(source_object);
1efd99
+    GError *error = NULL;
1efd99
+
1efd99
+    if (!ovirt_foreign_menu_set_current_iso_name_finish(foreign_menu, result, &error)) {
1efd99
+        g_warning(error ? error->message : "Failed to change CD");
1efd99
+        g_clear_error(&error);
1efd99
+        return;
1efd99
+    }
1efd99
+
1efd99
+    g_object_notify(G_OBJECT(foreign_menu), "file");
1efd99
 }
1efd99
 
1efd99
 
1efd99
@@ -383,7 +447,7 @@ static void
1efd99
 ovirt_foreign_menu_activate_item_cb(GtkMenuItem *menuitem, gpointer user_data)
1efd99
 {
1efd99
     OvirtForeignMenu *foreign_menu;
1efd99
-    const char *iso_name;
1efd99
+    const char *iso_name = NULL;
1efd99
     gboolean checked;
1efd99
 
1efd99
     checked = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem));
1efd99
@@ -403,19 +467,10 @@ ovirt_foreign_menu_activate_item_cb(GtkMenuItem *menuitem, gpointer user_data)
1efd99
 
1efd99
     if (checked) {
1efd99
         iso_name = gtk_menu_item_get_label(menuitem);
1efd99
-        g_debug("Updating VM cdrom image to '%s'", iso_name);
1efd99
-        foreign_menu->priv->next_iso_name = g_strdup(iso_name);
1efd99
-    } else {
1efd99
-        g_debug("Removing current cdrom image");
1efd99
-        iso_name = NULL;
1efd99
-        foreign_menu->priv->next_iso_name = NULL;
1efd99
     }
1efd99
-    g_object_set(foreign_menu->priv->cdrom,
1efd99
-                 "file", iso_name,
1efd99
-                 NULL);
1efd99
-    ovirt_cdrom_update_async(foreign_menu->priv->cdrom, TRUE,
1efd99
-                             foreign_menu->priv->proxy, NULL,
1efd99
-                             updated_cdrom_cb, foreign_menu);
1efd99
+    ovirt_foreign_menu_set_current_iso_name_async(foreign_menu, iso_name, NULL,
1efd99
+                                                  ovirt_foreign_menu_iso_name_changed,
1efd99
+                                                  menuitem);
1efd99
 }
1efd99
 
1efd99
 
1efd99
diff --git a/src/ovirt-foreign-menu.h b/src/ovirt-foreign-menu.h
1efd99
index cf18b52..791d0f3 100644
1efd99
--- a/src/ovirt-foreign-menu.h
1efd99
+++ b/src/ovirt-foreign-menu.h
1efd99
@@ -70,6 +70,16 @@ OvirtForeignMenu* ovirt_foreign_menu_new(OvirtProxy *proxy);
1efd99
 OvirtForeignMenu *ovirt_foreign_menu_new_from_file(VirtViewerFile *self);
1efd99
 void ovirt_foreign_menu_start(OvirtForeignMenu *menu);
1efd99
 
1efd99
+void ovirt_foreign_menu_set_current_iso_name_async(OvirtForeignMenu *foreign_menu,
1efd99
+                                                   const char *name,
1efd99
+                                                   GCancellable *cancellable,
1efd99
+                                                   GAsyncReadyCallback callback,
1efd99
+                                                   gpointer user_data);
1efd99
+gboolean ovirt_foreign_menu_set_current_iso_name_finish(OvirtForeignMenu *foreign_menu,
1efd99
+                                                        GAsyncResult *result,
1efd99
+                                                        GError **error);
1efd99
+
1efd99
+
1efd99
 GtkWidget *ovirt_foreign_menu_get_gtk_menu(OvirtForeignMenu *foreign_menu);
1efd99
 
1efd99
 G_END_DECLS
1efd99
-- 
1efd99
2.12.0
1efd99