[virt-tools-list] [PATCH virt-viewer v4 11/12] ovirt-foreign-menu: Set new ISO name using GTask API

Eduardo Lima (Etrunko) etrunko at redhat.com
Tue Nov 1 20:33:41 UTC 2016


From: Christophe Fergeau <cfergeau at redhat.com>

This is done with the new ovirt_foreign_menu_set_current_iso_name_async
function. Also, an error dialog will be shown in case anything goes
wrong with the async method.

Signed-off-by: Eduardo Lima (Etrunko) <etrunko at redhat.com>
---
 src/ovirt-foreign-menu.c            | 40 +++++++++++++++++++++++++++----------
 src/ovirt-foreign-menu.h            |  9 ++++++++-
 src/remote-viewer-iso-list-dialog.c | 40 +++++++++++++++++++++++++++++--------
 3 files changed, 70 insertions(+), 19 deletions(-)

diff --git a/src/ovirt-foreign-menu.c b/src/ovirt-foreign-menu.c
index 8320552..288a812 100644
--- a/src/ovirt-foreign-menu.c
+++ b/src/ovirt-foreign-menu.c
@@ -47,7 +47,7 @@ static void ovirt_foreign_menu_fetch_storage_domain_async(OvirtForeignMenu *menu
 static void ovirt_foreign_menu_fetch_vm_cdrom_async(OvirtForeignMenu *menu);
 static void ovirt_foreign_menu_refresh_cdrom_file_async(OvirtForeignMenu *menu);
 static void ovirt_foreign_menu_fetch_iso_list_async(OvirtForeignMenu *menu);
-static void updated_cdrom_cb(GObject *source_object, GAsyncResult *result, gpointer user_data);
+static void iso_name_set_cb(GObject *source_object, GAsyncResult *result, gpointer user_data);
 
 G_DEFINE_TYPE (OvirtForeignMenu, ovirt_foreign_menu, G_TYPE_OBJECT)
 
@@ -102,8 +102,14 @@ ovirt_foreign_menu_get_current_iso_name(OvirtForeignMenu *foreign_menu)
 
 
 void
-ovirt_foreign_menu_set_current_iso_name(OvirtForeignMenu *foreign_menu, char *name)
+ovirt_foreign_menu_set_current_iso_name_async(OvirtForeignMenu *foreign_menu,
+                                              char *name,
+                                              GCancellable *cancellable,
+                                              GAsyncReadyCallback callback,
+                                              gpointer user_data)
 {
+    GTask *task;
+
     g_return_if_fail(foreign_menu->priv->cdrom != NULL);
     g_return_if_fail(foreign_menu->priv->next_iso_name == NULL);
 
@@ -118,11 +124,20 @@ ovirt_foreign_menu_set_current_iso_name(OvirtForeignMenu *foreign_menu, char *na
     g_object_set(foreign_menu->priv->cdrom,
                  "file", name,
                  NULL);
+
+    task = g_task_new(foreign_menu, cancellable, callback, user_data);
     ovirt_cdrom_update_async(foreign_menu->priv->cdrom, TRUE,
                              foreign_menu->priv->proxy, NULL,
-                             updated_cdrom_cb, foreign_menu);
+                             iso_name_set_cb, task);
 }
 
+gboolean ovirt_foreign_menu_set_current_iso_name_finish(OvirtForeignMenu *foreign_menu,
+                                                        GAsyncResult *result,
+                                                        GError **error)
+{
+    g_return_val_if_fail(OVIRT_IS_FOREIGN_MENU(foreign_menu), FALSE);
+    return g_task_propagate_boolean(G_TASK(result), error);
+}
 
 GList*
 ovirt_foreign_menu_get_iso_names(OvirtForeignMenu *foreign_menu)
@@ -359,15 +374,16 @@ ovirt_foreign_menu_start(OvirtForeignMenu *menu)
 }
 
 
-static void updated_cdrom_cb(GObject *source_object,
-                             GAsyncResult *result,
-                             gpointer user_data)
+static void iso_name_set_cb(GObject *source_object,
+                            GAsyncResult *result,
+                            gpointer user_data)
 {
     GError *error = NULL;
     OvirtForeignMenu *foreign_menu;
     gboolean updated;
+    GTask *task = G_TASK(user_data);
 
-    foreign_menu = OVIRT_FOREIGN_MENU(user_data);
+    foreign_menu = OVIRT_FOREIGN_MENU(g_task_get_source_object(task));
     updated = ovirt_cdrom_update_finish(OVIRT_CDROM(source_object),
                                         result, &error);
     g_debug("Finished updating cdrom content");
@@ -375,6 +391,7 @@ static void updated_cdrom_cb(GObject *source_object,
         g_free(foreign_menu->priv->current_iso_name);
         foreign_menu->priv->current_iso_name = foreign_menu->priv->next_iso_name;
         foreign_menu->priv->next_iso_name = NULL;
+        g_task_return_boolean(task, TRUE);
         goto end;
     }
 
@@ -382,7 +399,11 @@ static void updated_cdrom_cb(GObject *source_object,
      * the new ISO */
     if (error != NULL) {
         g_warning("failed to update cdrom resource: %s", error->message);
-        g_clear_error(&error);
+        g_task_return_error(task, error);
+    } else {
+        g_warn_if_reached();
+        g_task_return_new_error(task, OVIRT_ERROR, OVIRT_ERROR_FAILED,
+                                "failed to update cdrom resource");
     }
     g_debug("setting OvirtCdrom:file back to '%s'",
             foreign_menu->priv->current_iso_name);
@@ -392,10 +413,9 @@ static void updated_cdrom_cb(GObject *source_object,
     g_clear_pointer(&foreign_menu->priv->next_iso_name, g_free);
 
 end:
-    g_object_notify(G_OBJECT(foreign_menu), "file");
+    g_object_unref(task);
 }
 
-
 static void ovirt_foreign_menu_set_files(OvirtForeignMenu *menu,
                                          const GList *files)
 {
diff --git a/src/ovirt-foreign-menu.h b/src/ovirt-foreign-menu.h
index f1a1ddb..df4a50e 100644
--- a/src/ovirt-foreign-menu.h
+++ b/src/ovirt-foreign-menu.h
@@ -71,7 +71,14 @@ OvirtForeignMenu *ovirt_foreign_menu_new_from_file(VirtViewerFile *self);
 void ovirt_foreign_menu_start(OvirtForeignMenu *menu);
 
 char *ovirt_foreign_menu_get_current_iso_name(OvirtForeignMenu *menu);
-void ovirt_foreign_menu_set_current_iso_name(OvirtForeignMenu *menu, char *name);
+void ovirt_foreign_menu_set_current_iso_name_async(OvirtForeignMenu *foreign_menu,
+                                                   char *name,
+                                                   GCancellable *cancellable,
+                                                   GAsyncReadyCallback callback,
+                                                   gpointer user_data);
+gboolean ovirt_foreign_menu_set_current_iso_name_finish(OvirtForeignMenu *foreign_menu,
+                                                        GAsyncResult *result,
+                                                        GError **error);
 
 GList *ovirt_foreign_menu_get_iso_names(OvirtForeignMenu *menu);
 
diff --git a/src/remote-viewer-iso-list-dialog.c b/src/remote-viewer-iso-list-dialog.c
index dd652d7..b374b89 100644
--- a/src/remote-viewer-iso-list-dialog.c
+++ b/src/remote-viewer-iso-list-dialog.c
@@ -27,6 +27,9 @@
 #include "virt-viewer-util.h"
 #include "ovirt-foreign-menu.h"
 
+static void ovirt_foreign_menu_iso_name_changed(OvirtForeignMenu *foreign_menu, GAsyncResult *result, RemoteViewerISOListDialog *self);
+static void remote_viewer_iso_list_dialog_show_error(RemoteViewerISOListDialog *self, gchar *message);
+
 G_DEFINE_TYPE(RemoteViewerISOListDialog, remote_viewer_iso_list_dialog, GTK_TYPE_DIALOG)
 
 #define DIALOG_PRIVATE(o) \
@@ -180,7 +183,9 @@ remote_viewer_iso_list_dialog_toggled(GtkCellRendererToggle *cell_renderer G_GNU
     gtk_dialog_set_response_sensitive(GTK_DIALOG(self), GTK_RESPONSE_NONE, FALSE);
     gtk_widget_set_sensitive(priv->tree_view, FALSE);
 
-    ovirt_foreign_menu_set_current_iso_name(priv->foreign_menu, active ? NULL : name);
+    ovirt_foreign_menu_set_current_iso_name_async(priv->foreign_menu, active ? NULL : name, NULL,
+                                                  (GAsyncReadyCallback)ovirt_foreign_menu_iso_name_changed,
+                                                  self);
     gtk_tree_path_free(tree_path);
     g_free(name);
 }
@@ -231,9 +236,26 @@ remote_viewer_iso_list_dialog_init(RemoteViewerISOListDialog *self)
 }
 
 static void
-ovirt_foreign_menu_notify_file(OvirtForeignMenu *foreign_menu,
-                               GParamSpec *pspec G_GNUC_UNUSED,
-                               RemoteViewerISOListDialog *self)
+remote_viewer_iso_list_dialog_show_error(RemoteViewerISOListDialog *self,
+                                         gchar *message)
+{
+    GtkWidget *dialog;
+
+    g_warn_if_fail(message != NULL);
+
+    dialog = gtk_message_dialog_new(GTK_WINDOW(self),
+                                    GTK_DIALOG_DESTROY_WITH_PARENT,
+                                    GTK_MESSAGE_ERROR,
+                                    GTK_BUTTONS_CLOSE,
+                                    message ? message : _("Unspecified error"));
+    gtk_dialog_run(GTK_DIALOG(dialog));
+    gtk_widget_destroy(dialog);
+}
+
+static void
+ovirt_foreign_menu_iso_name_changed(OvirtForeignMenu *foreign_menu,
+                                    GAsyncResult *result,
+                                    RemoteViewerISOListDialog *self)
 {
     RemoteViewerISOListDialogPrivate *priv = self->priv;
     GtkTreeModel *model = GTK_TREE_MODEL(priv->list_store);
@@ -241,6 +263,12 @@ ovirt_foreign_menu_notify_file(OvirtForeignMenu *foreign_menu,
     GtkTreeIter iter;
     gchar *name;
     gboolean active, match = FALSE;
+    GError *error = NULL;
+
+    if (!ovirt_foreign_menu_set_current_iso_name_finish(foreign_menu, result, &error)) {
+        remote_viewer_iso_list_dialog_show_error(self, error ? error->message : _("Failed to change CD"));
+        g_clear_error(&error);
+    }
 
     if (!gtk_tree_model_get_iter_first(model, &iter))
         goto end;
@@ -303,10 +331,6 @@ remote_viewer_iso_list_dialog_new(GtkWindow *parent, GObject *foreign_menu)
     self->priv->foreign_menu = OVIRT_FOREIGN_MENU(g_object_ref(foreign_menu));
 
     g_signal_connect(foreign_menu,
-                     "notify::file",
-                     (GCallback)ovirt_foreign_menu_notify_file,
-                     dialog);
-    g_signal_connect(foreign_menu,
                      "notify::files",
                      (GCallback)ovirt_foreign_menu_notify_files,
                      dialog);
-- 
2.7.4




More information about the virt-tools-list mailing list