[virt-tools-list] [PATCH 1/2] Code related to vm_choose_dialog moved into separate files

Pavel Grunt pgrunt at redhat.com
Fri Oct 3 15:02:01 UTC 2014


---
 src/Makefile.am                   |   1 +
 src/remote-viewer.c               |  85 ++++++------------------------
 src/virt-viewer-vm-connection.c   | 105 ++++++++++++++++++++++++++++++++++++++
 src/virt-viewer-vm-connection.h   |  36 +++++++++++++
 src/virt-viewer-vm-connection.xml |  20 --------
 5 files changed, 159 insertions(+), 88 deletions(-)
 create mode 100644 src/virt-viewer-vm-connection.c
 create mode 100644 src/virt-viewer-vm-connection.h

diff --git a/src/Makefile.am b/src/Makefile.am
index a33417c..e76da2c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -47,6 +47,7 @@ COMMON_SOURCES =					\
 	virt-viewer-display.h virt-viewer-display.c	\
 	virt-viewer-notebook.h virt-viewer-notebook.c	\
 	virt-viewer-window.h virt-viewer-window.c	\
+	virt-viewer-vm-connection.h virt-viewer-vm-connection.c	\
 	view/autoDrawer.c				\
 	view/autoDrawer.h				\
 	view/drawer.c					\
diff --git a/src/remote-viewer.c b/src/remote-viewer.c
index 3b44269..485baf2 100644
--- a/src/remote-viewer.c
+++ b/src/remote-viewer.c
@@ -31,6 +31,7 @@
 #ifdef HAVE_OVIRT
 #include <govirt/govirt.h>
 #include "ovirt-foreign-menu.h"
+#include "virt-viewer-vm-connection.h"
 #endif
 
 #ifdef HAVE_SPICE_GTK
@@ -75,7 +76,7 @@ enum {
 };
 
 #ifdef HAVE_OVIRT
-static OvirtVm * choose_vm_dialog(char **vm_name, OvirtCollection *vms, GError **error);
+static OvirtVm * choose_vm(char **vm_name, OvirtCollection *vms, GError **error);
 #endif
 
 static gboolean remote_viewer_start(VirtViewerApp *self);
@@ -861,7 +862,7 @@ create_ovirt_session(VirtViewerApp *app, const char *uri, GError **err)
     }
     if (vm_name == NULL ||
         (vm = OVIRT_VM(ovirt_collection_lookup_resource(vms, vm_name))) == NULL) {
-        vm = choose_vm_dialog(&vm_name, vms, &error);
+        vm = choose_vm(&vm_name, vms, &error);
         if (vm == NULL) {
             goto error;
         }
@@ -1133,91 +1134,39 @@ connect_dialog(gchar **uri)
 
 
 #ifdef HAVE_OVIRT
-static void
-treeview_row_activated_cb(GtkTreeView *treeview G_GNUC_UNUSED,
-                          GtkTreePath *path G_GNUC_UNUSED,
-                          GtkTreeViewColumn *col G_GNUC_UNUSED,
-                          gpointer userdata)
-{
-    gtk_widget_activate(GTK_WIDGET(userdata));
-}
-
-static void
-treeselection_changed_cb(GtkTreeSelection *selection, gpointer userdata)
-{
-    gtk_widget_set_sensitive(GTK_WIDGET(userdata),
-                             gtk_tree_selection_count_selected_rows(selection) == 1);
-}
-
-
 static OvirtVm *
-choose_vm_dialog(char **vm_name, OvirtCollection *vms_collection, GError **error)
+choose_vm(char **vm_name, OvirtCollection *vms_collection, GError **error)
 {
-    GtkBuilder *vm_connection;
-    GtkWidget *dialog;
-    GtkButton *button_connect;
-    GtkTreeView *treeview;
-    GtkTreeModel *store;
-    GtkTreeSelection *select;
+    GtkListStore *model;
     GtkTreeIter iter;
     GHashTable *vms;
     GHashTableIter vms_iter;
-    OvirtVm *vm;
     OvirtVmState state;
-    int response;
-    gboolean any_vm_running = FALSE;
+    OvirtVm *vm;
 
     g_return_val_if_fail(vm_name != NULL, NULL);
     if (*vm_name != NULL) {
         free(*vm_name);
     }
 
-    vms = ovirt_collection_get_resources(vms_collection);
-
-    vm_connection = virt_viewer_util_load_ui("virt-viewer-vm-connection.xml");
-    dialog = GTK_WIDGET(gtk_builder_get_object(vm_connection, "vm-connection-dialog"));
-    button_connect = GTK_BUTTON(gtk_builder_get_object(vm_connection, "button-connect"));
-    treeview = GTK_TREE_VIEW(gtk_builder_get_object(vm_connection, "treeview"));
-    select = GTK_TREE_SELECTION(gtk_builder_get_object(vm_connection, "treeview-selection"));
-    store = GTK_TREE_MODEL(gtk_builder_get_object(vm_connection, "store"));
-
-    g_signal_connect(treeview, "row-activated",
-        G_CALLBACK(treeview_row_activated_cb), button_connect);
-    g_signal_connect(select, "changed",
-        G_CALLBACK(treeselection_changed_cb), button_connect);
+    model = gtk_list_store_new(1, G_TYPE_STRING);
 
+    vms = ovirt_collection_get_resources(vms_collection);
     g_hash_table_iter_init(&vms_iter, vms);
     while (g_hash_table_iter_next(&vms_iter, (gpointer *) vm_name, (gpointer *) &vm)) {
         g_object_get(G_OBJECT(vm), "state", &state, NULL);
         if (state == OVIRT_VM_STATE_UP) {
-            gtk_list_store_append(GTK_LIST_STORE(store), &iter);
-            gtk_list_store_set(GTK_LIST_STORE(store), &iter, 0, *vm_name, -1);
-            any_vm_running = TRUE;
+            gtk_list_store_append(model, &iter);
+            gtk_list_store_set(model, &iter, 0, *vm_name, -1);
        }
     }
-    *vm_name = NULL;
-    if (any_vm_running) {
-        gtk_widget_show_all(dialog);
-        response = gtk_dialog_run(GTK_DIALOG(dialog));
-        gtk_widget_hide(dialog);
-        if (response == GTK_RESPONSE_ACCEPT &&
-            gtk_tree_selection_get_selected(select, &store, &iter)) {
-            gtk_tree_model_get(store, &iter, 0, vm_name, -1);
-            vm = OVIRT_VM(ovirt_collection_lookup_resource(vms_collection, *vm_name));
-        } else {
-            g_set_error_literal(error,
-                                VIRT_VIEWER_ERROR, VIRT_VIEWER_VM_CHOOSE_DIALOG_CANCELLED,
-                                _("No virtual machine was chosen"));
-            vm = NULL;
-        }
-    } else {
-        g_set_error_literal(error,
-                            VIRT_VIEWER_ERROR, VIRT_VIEWER_ERROR_FAILED,
-                            _("No running virtual machines found"));
-        vm = NULL;
-    }
-    gtk_widget_destroy(dialog);
-    g_object_unref(G_OBJECT(vm_connection));
+
+    *vm_name = virt_viewer_vm_connection_choose_name_dialog(GTK_TREE_MODEL(model), error);
+    g_object_unref(model);
+    if (*vm_name == NULL)
+        return NULL;
+
+    vm = OVIRT_VM(ovirt_collection_lookup_resource(vms_collection, *vm_name));
 
     return vm;
 }
diff --git a/src/virt-viewer-vm-connection.c b/src/virt-viewer-vm-connection.c
new file mode 100644
index 0000000..fb0c2b0
--- /dev/null
+++ b/src/virt-viewer-vm-connection.c
@@ -0,0 +1,105 @@
+/*
+ * Virt Viewer: A virtual machine console viewer
+ *
+ * Copyright (C) 2014 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <config.h>
+
+#include <glib.h>
+#include <glib/gi18n.h>
+
+#include "virt-viewer-vm-connection.h"
+#include "virt-viewer-util.h"
+
+static void
+treeview_row_activated_cb(GtkTreeView *treeview G_GNUC_UNUSED,
+                          GtkTreePath *path G_GNUC_UNUSED,
+                          GtkTreeViewColumn *col G_GNUC_UNUSED,
+                          gpointer userdata)
+{
+    gtk_widget_activate(GTK_WIDGET(userdata));
+}
+
+static void
+treeselection_changed_cb(GtkTreeSelection *selection, gpointer userdata)
+{
+    gtk_widget_set_sensitive(GTK_WIDGET(userdata),
+                             gtk_tree_selection_count_selected_rows(selection) == 1);
+}
+
+gchar*
+virt_viewer_vm_connection_choose_name_dialog(GtkTreeModel *model, GError **error)
+{
+    GtkBuilder *vm_connection;
+    GtkWidget *dialog;
+    GtkButton *button_connect;
+    GtkTreeView *treeview;
+    GtkTreeSelection *select;
+    GtkTreeIter iter;
+    int dialog_response;
+	gchar *vm_name = NULL;
+
+	g_return_val_if_fail(model != NULL, NULL);
+
+	if (!gtk_tree_model_get_iter_first(model, &iter)) {
+		g_set_error_literal(error,
+                            VIRT_VIEWER_ERROR, VIRT_VIEWER_ERROR_FAILED,
+                            _("No virtual machine found"));
+		return NULL;
+	}
+
+    vm_connection = virt_viewer_util_load_ui("virt-viewer-vm-connection.xml");
+    g_return_val_if_fail(vm_connection != NULL, NULL);
+
+    dialog = GTK_WIDGET(gtk_builder_get_object(vm_connection, "vm-connection-dialog"));
+    button_connect = GTK_BUTTON(gtk_builder_get_object(vm_connection, "button-connect"));
+    treeview = GTK_TREE_VIEW(gtk_builder_get_object(vm_connection, "treeview"));
+    select = GTK_TREE_SELECTION(gtk_builder_get_object(vm_connection, "treeview-selection"));
+    gtk_tree_view_set_model(treeview, model);
+
+    g_signal_connect(treeview, "row-activated",
+        G_CALLBACK(treeview_row_activated_cb), button_connect);
+    g_signal_connect(select, "changed",
+        G_CALLBACK(treeselection_changed_cb), button_connect);
+
+    gtk_widget_show_all(dialog);
+    dialog_response = gtk_dialog_run(GTK_DIALOG(dialog));
+    gtk_widget_hide(dialog);
+
+	if (dialog_response == GTK_RESPONSE_ACCEPT &&
+        gtk_tree_selection_get_selected(select, &model, &iter)) {
+        gtk_tree_model_get(model, &iter, 0, &vm_name, -1);
+    } else {
+        g_set_error_literal(error,
+                            VIRT_VIEWER_ERROR, VIRT_VIEWER_VM_CHOOSE_DIALOG_CANCELLED,
+                            _("No virtual machine was chosen"));
+    }
+
+    gtk_widget_destroy(dialog);
+    g_object_unref(G_OBJECT(vm_connection));
+
+	return vm_name;
+}
+
+/*
+ * Local variables:
+ *  c-indent-level: 4
+ *  c-basic-offset: 4
+ *  indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/virt-viewer-vm-connection.h b/src/virt-viewer-vm-connection.h
new file mode 100644
index 0000000..d198c89
--- /dev/null
+++ b/src/virt-viewer-vm-connection.h
@@ -0,0 +1,36 @@
+/*
+ * Virt Viewer: A virtual machine console viewer
+ *
+ * Copyright (C) 2014 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef VIRT_VIEWER_VM_CONNECTION_H
+#define VIRT_VIEWER_VM_CONNECTION_H
+
+#include <glib.h>
+#include <gtk/gtk.h>
+
+gchar* virt_viewer_vm_connection_choose_name_dialog(GtkTreeModel *model, GError **error);
+
+#endif
+/*
+ * Local variables:
+ *  c-indent-level: 4
+ *  c-basic-offset: 4
+ *  indent-tabs-mode: nil
+ * End:
+ */
diff --git a/src/virt-viewer-vm-connection.xml b/src/virt-viewer-vm-connection.xml
index 147dc92..04797fa 100644
--- a/src/virt-viewer-vm-connection.xml
+++ b/src/virt-viewer-vm-connection.xml
@@ -1,14 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!-- Generated with glade 3.16.1 -->
 <interface>
-  <object class="GtkListStore" id="store">
-    <columns>
-      <!-- column-name name -->
-      <column type="gchararray"/>
-      <!-- column-name state -->
-      <column type="gchararray"/>
-    </columns>
-  </object>
   <object class="GtkDialog" id="vm-connection-dialog">
     <property name="can_focus">False</property>
     <property name="border_width">5</property>
@@ -71,7 +63,6 @@
           <object class="GtkTreeView" id="treeview">
             <property name="visible">True</property>
             <property name="can_focus">True</property>
-            <property name="model">store</property>
             <property name="headers_visible">False</property>
             <property name="search_column">0</property>
             <property name="enable_grid_lines">horizontal</property>
@@ -89,17 +80,6 @@
                 </child>
               </object>
             </child>
-            <child>
-              <object class="GtkTreeViewColumn" id="treeviewcolumn2">
-                <property name="title" translatable="yes">State</property>
-                <child>
-                  <object class="GtkCellRendererText" id="cellrenderertext2"/>
-                  <attributes>
-                    <attribute name="text">1</attribute>
-                  </attributes>
-                </child>
-              </object>
-            </child>
           </object>
           <packing>
             <property name="expand">True</property>
-- 
1.9.3




More information about the virt-tools-list mailing list