[virt-tools-list] [PATCH virt-viewer 11/11] iso-dialog: Implement functionality provided by oVirt foreign menu

Eduardo Lima (Etrunko) etrunko at redhat.com
Mon Jul 18 02:13:11 UTC 2016


Signed-off-by: Eduardo Lima (Etrunko) <etrunko at redhat.com>
---
 src/remote-viewer-iso-list-dialog.c        | 149 +++++++++++++++++++++++++++++
 src/resources/ui/remote-viewer-iso-list.ui |   4 +-
 2 files changed, 152 insertions(+), 1 deletion(-)

diff --git a/src/remote-viewer-iso-list-dialog.c b/src/remote-viewer-iso-list-dialog.c
index 996c1f2..3b2e187 100644
--- a/src/remote-viewer-iso-list-dialog.c
+++ b/src/remote-viewer-iso-list-dialog.c
@@ -20,6 +20,7 @@
 
 #include <config.h>
 
+#include <stdlib.h>
 #include <glib/gi18n.h>
 
 #include "remote-viewer-iso-list-dialog.h"
@@ -34,7 +35,9 @@ struct _RemoteViewerISOListDialogPrivate
 {
     GtkBuilder *builder;
     GtkWidget *notebook;
+    GtkWidget *treeview;
     OvirtForeignMenu *foreign_menu;
+    GtkListStore *list_store;
 };
 
 enum RemoteViewerIsoListDialogPages
@@ -43,6 +46,15 @@ enum RemoteViewerIsoListDialogPages
     ISO_LIST_PAGE,
 };
 
+enum RemoteViewerIsoListDialogModel
+{
+    ISO_IS_ACTIVE = 0,
+    ISO_NAME,
+    FONT_WEIGHT,
+};
+
+void remote_viewer_iso_list_dialog_toggled(GtkCellRendererToggle *cell_renderer, gchar *path, gpointer user_data);
+
 static void
 remote_viewer_iso_list_dialog_dispose(GObject *object)
 {
@@ -65,6 +77,52 @@ remote_viewer_iso_list_dialog_class_init(RemoteViewerISOListDialogClass *klass)
 }
 
 static void
+remote_viewer_iso_list_dialog_show_files(RemoteViewerISOListDialog *self)
+{
+    RemoteViewerISOListDialogPrivate *priv = self->priv = DIALOG_PRIVATE(self);
+    gtk_notebook_set_current_page(GTK_NOTEBOOK(priv->notebook), ISO_LIST_PAGE);
+    gtk_dialog_set_response_sensitive(GTK_DIALOG(self), GTK_RESPONSE_NONE, TRUE);
+    gtk_dialog_set_response_sensitive(GTK_DIALOG(self), GTK_RESPONSE_CLOSE, TRUE);
+}
+
+static void
+remote_viewer_iso_list_dialog_foreach(char *name, RemoteViewerISOListDialog *self)
+{
+    RemoteViewerISOListDialogPrivate *priv = self->priv;
+    char * current_iso = ovirt_foreign_menu_get_current_iso_name(self->priv->foreign_menu);
+    gboolean active = g_strcmp0(current_iso, name) == 0;
+    gint weight = active ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL;
+    GtkTreeIter iter;
+
+    gtk_list_store_append(priv->list_store, &iter);
+    gtk_list_store_set(priv->list_store, &iter,
+                       ISO_IS_ACTIVE, active,
+                       ISO_NAME, name,
+                       FONT_WEIGHT, weight, -1);
+
+    free(current_iso);
+}
+
+static void
+remote_viewer_iso_list_dialog_refresh_iso_list(RemoteViewerISOListDialog *self,
+                                               gboolean refreshing)
+{
+    RemoteViewerISOListDialogPrivate *priv = self->priv;
+    GList *iso_list;
+
+    if (refreshing) {
+        ovirt_foreign_menu_start(priv->foreign_menu);
+        return;
+    }
+
+    iso_list = ovirt_foreign_menu_get_iso_names(priv->foreign_menu);
+
+    gtk_list_store_clear(priv->list_store);
+    g_list_foreach(iso_list, (GFunc) remote_viewer_iso_list_dialog_foreach, self);
+    remote_viewer_iso_list_dialog_show_files(self);
+}
+
+static void
 remote_viewer_iso_list_dialog_response(GtkDialog *dialog,
                                        gint response_id,
                                        gpointer user_data G_GNUC_UNUSED)
@@ -78,6 +136,32 @@ remote_viewer_iso_list_dialog_response(GtkDialog *dialog,
     gtk_notebook_set_current_page(GTK_NOTEBOOK(priv->notebook), STATUS_PAGE);
     gtk_dialog_set_response_sensitive(GTK_DIALOG(self), GTK_RESPONSE_NONE, FALSE);
     gtk_dialog_set_response_sensitive(GTK_DIALOG(self), GTK_RESPONSE_CLOSE, FALSE);
+
+    remote_viewer_iso_list_dialog_refresh_iso_list(self, TRUE);
+}
+
+void
+remote_viewer_iso_list_dialog_toggled(GtkCellRendererToggle *cell_renderer G_GNUC_UNUSED,
+                                      gchar *path G_GNUC_UNUSED,
+                                      gpointer user_data)
+{
+    RemoteViewerISOListDialog *self = REMOTE_VIEWER_ISO_LIST_DIALOG(user_data);
+    RemoteViewerISOListDialogPrivate *priv = self->priv;
+    GtkTreeModel *model = GTK_TREE_MODEL(priv->list_store);
+    GtkTreeIter iter;
+    gboolean active;
+    gchar *name;
+
+    gtk_tree_model_get(model, &iter,
+                       ISO_IS_ACTIVE, &active,
+                       ISO_NAME, &name, -1);
+
+    gtk_dialog_set_response_sensitive(GTK_DIALOG(self), GTK_RESPONSE_NONE, FALSE);
+    gtk_dialog_set_response_sensitive(GTK_DIALOG(self), GTK_RESPONSE_CLOSE, FALSE);
+    gtk_widget_set_sensitive(priv->treeview, FALSE);
+
+    ovirt_foreign_menu_set_current_iso_name(priv->foreign_menu, active ? NULL : name);
+    g_free(name);
 }
 
 static void
@@ -85,6 +169,7 @@ remote_viewer_iso_list_dialog_init(RemoteViewerISOListDialog *self)
 {
     GtkWidget *content = gtk_dialog_get_content_area(GTK_DIALOG(self));
     RemoteViewerISOListDialogPrivate *priv = self->priv = DIALOG_PRIVATE(self);
+    GtkCellRendererToggle *cell_renderer;
 
     gtk_widget_set_size_request(GTK_WIDGET(self), 400, 300);
     gtk_box_set_spacing(GTK_BOX(content), 6);
@@ -95,6 +180,11 @@ remote_viewer_iso_list_dialog_init(RemoteViewerISOListDialog *self)
     priv->notebook = GTK_WIDGET(gtk_builder_get_object(priv->builder, "notebook"));
     gtk_box_pack_start(GTK_BOX(content), priv->notebook, TRUE, TRUE, 0);
 
+    priv->list_store = GTK_LIST_STORE(gtk_builder_get_object(priv->builder, "liststore"));
+    priv->treeview = GTK_WIDGET(gtk_builder_get_object(priv->builder, "view"));
+    cell_renderer = GTK_CELL_RENDERER_TOGGLE(gtk_builder_get_object(priv->builder, "cellrenderertoggle"));
+    gtk_cell_renderer_toggle_set_radio(cell_renderer, TRUE);
+
     gtk_dialog_add_buttons(GTK_DIALOG(self),
                            _("Refresh"), GTK_RESPONSE_NONE,
                            _("Close"), GTK_RESPONSE_CLOSE,
@@ -106,6 +196,55 @@ remote_viewer_iso_list_dialog_init(RemoteViewerISOListDialog *self)
     g_signal_connect(self, "response", G_CALLBACK(remote_viewer_iso_list_dialog_response), NULL);
 }
 
+static void
+ovirt_foreign_menu_notify_file(OvirtForeignMenu *foreign_menu,
+                               GParamSpec *pspec G_GNUC_UNUSED,
+                               RemoteViewerISOListDialog *self)
+{
+    RemoteViewerISOListDialogPrivate *priv = self->priv;
+    GtkTreeModel *model = GTK_TREE_MODEL(priv->list_store);
+    char *current_iso = ovirt_foreign_menu_get_current_iso_name(foreign_menu);
+    GtkTreeIter iter;
+
+    if (gtk_tree_model_get_iter_first(model, &iter)) {
+        gchar *name;
+        gboolean active, match = FALSE;
+        do {
+            gtk_tree_model_get(model, &iter, 0, &active, 1, &name, -1);
+            match = g_strcmp0(current_iso, name) == 0;
+
+            /* iso is not active anymore */
+            if (active && !match)
+                gtk_list_store_set(priv->list_store, &iter,
+                                   ISO_IS_ACTIVE, FALSE,
+                                   FONT_WEIGHT, PANGO_WEIGHT_NORMAL, -1);
+
+            g_free(name);
+        } while (!match && gtk_tree_model_iter_next(model, &iter));
+
+        /* set new active iso */
+        if (match)
+            gtk_list_store_set(priv->list_store, &iter,
+                               ISO_IS_ACTIVE, TRUE,
+                               FONT_WEIGHT, PANGO_WEIGHT_BOLD, -1);
+    }
+
+    gtk_dialog_set_response_sensitive(GTK_DIALOG(self), GTK_RESPONSE_NONE, TRUE);
+    gtk_dialog_set_response_sensitive(GTK_DIALOG(self), GTK_RESPONSE_CLOSE, TRUE);
+    gtk_widget_set_sensitive(priv->treeview, TRUE);
+
+    free(current_iso);
+}
+
+static void
+ovirt_foreign_menu_notify_files(OvirtForeignMenu *foreign_menu G_GNUC_UNUSED,
+                                GParamSpec *pspec G_GNUC_UNUSED,
+                                RemoteViewerISOListDialog *self)
+{
+    remote_viewer_iso_list_dialog_refresh_iso_list(self, FALSE);
+}
+
+
 GtkWidget *
 remote_viewer_iso_list_dialog_new(GtkWindow *parent, OvirtForeignMenu *foreign_menu)
 {
@@ -121,5 +260,15 @@ remote_viewer_iso_list_dialog_new(GtkWindow *parent, OvirtForeignMenu *foreign_m
 
     self = REMOTE_VIEWER_ISO_LIST_DIALOG(dialog);
     self->priv->foreign_menu = foreign_menu;
+    g_signal_connect(G_OBJECT(foreign_menu),
+                     "notify::file",
+                     (GCallback)ovirt_foreign_menu_notify_file,
+                     dialog);
+    g_signal_connect(G_OBJECT(foreign_menu),
+                     "notify::files",
+                     (GCallback)ovirt_foreign_menu_notify_files,
+                     dialog);
+
+    remote_viewer_iso_list_dialog_refresh_iso_list(self, TRUE);
     return dialog;
 }
diff --git a/src/resources/ui/remote-viewer-iso-list.ui b/src/resources/ui/remote-viewer-iso-list.ui
index bb2f6c6..146584c 100644
--- a/src/resources/ui/remote-viewer-iso-list.ui
+++ b/src/resources/ui/remote-viewer-iso-list.ui
@@ -123,7 +123,9 @@
                         <property name="sizing">autosize</property>
                         <property name="title" translatable="yes">Selected</property>
                         <child>
-                          <object class="GtkCellRendererToggle" id="cellrenderertoggle"/>
+                          <object class="GtkCellRendererToggle" id="cellrenderertoggle">
+                            <signal name="toggled" handler="remote_viewer_iso_list_dialog_toggled" swapped="no"/>
+                          </object>
                           <attributes>
                             <attribute name="active">0</attribute>
                           </attributes>
-- 
2.7.4




More information about the virt-tools-list mailing list