[virt-tools-list] [PATCH] virt-viewer: Connecting without specifying VM domain name
Pavel Grunt
pgrunt at redhat.com
Fri Sep 19 12:15:57 UTC 2014
When user starts virt-viewer without specifying VM domain name
a list of running machines is shown and user may choose one of them.
---
depends on ui file from http://www.redhat.com/archives/virt-tools-list/2014-September/msg00171.html
src/virt-viewer-main.c | 4 ++--
src/virt-viewer.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 67 insertions(+), 2 deletions(-)
diff --git a/src/virt-viewer-main.c b/src/virt-viewer-main.c
index 44d1182..3fae955 100644
--- a/src/virt-viewer-main.c
+++ b/src/virt-viewer-main.c
@@ -104,12 +104,12 @@ int main(int argc, char **argv)
g_option_context_free(context);
- if (!args || (g_strv_length(args) != 1)) {
+ if (args && (g_strv_length(args) != 1)) {
g_printerr(_("\nUsage: %s [OPTIONS] DOMAIN-NAME|ID|UUID\n\n%s\n\n"), argv[0], help_msg);
goto cleanup;
}
- viewer = virt_viewer_new(uri, args[0], direct, attach, waitvm, reconnect);
+ viewer = virt_viewer_new(uri, (args) ? args[0] : NULL, direct, attach, waitvm, reconnect);
if (viewer == NULL)
goto cleanup;
diff --git a/src/virt-viewer.c b/src/virt-viewer.c
index f8a9ab5..3c9701a 100644
--- a/src/virt-viewer.c
+++ b/src/virt-viewer.c
@@ -600,6 +600,66 @@ virt_viewer_initial_connect(VirtViewerApp *app, GError **error)
}
static void
+vm_list_selection_changed_dialog_cb(GtkTreeSelection *selection, gpointer data)
+{
+ GtkTreeIter iter;
+ GtkTreeModel *model;
+ const gchar *vm_name;
+ GtkWidget *entry = data;
+
+ if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
+ gtk_tree_model_get(model, &iter, 0, &vm_name, -1);
+ gtk_entry_set_text(GTK_ENTRY(entry), vm_name);
+ }
+}
+
+static char *choose_vm_dialog(virConnectPtr conn)
+{
+ GtkBuilder *vm_connection;
+ GtkWidget *dialog, *entry;
+ GtkTreeModel *store;
+ GtkTreeSelection *select;
+ GtkTreeIter iter;
+ char *vm_name;
+ virDomainPtr *domains;
+ int i, vms_running;
+ unsigned int flags = VIR_CONNECT_LIST_DOMAINS_RUNNING;
+
+ vm_connection = virt_viewer_util_load_ui("virt-viewer-vm-connection.xml");
+ dialog = GTK_WIDGET(gtk_builder_get_object(vm_connection, "vm-connection-dialog"));
+ entry = GTK_WIDGET(gtk_builder_get_object(vm_connection, "entry"));
+ select = GTK_TREE_SELECTION(gtk_builder_get_object(vm_connection, "treeview-selection"));
+ store = GTK_TREE_MODEL(gtk_builder_get_object(vm_connection, "store"));
+
+ vms_running = virConnectListAllDomains(conn, &domains, flags);
+ for (i = 0; i < vms_running; i++) {
+ gtk_list_store_append(GTK_LIST_STORE(store), &iter);
+ gtk_list_store_set(GTK_LIST_STORE(store), &iter, 0, virDomainGetName(domains[i]), -1);
+ virDomainFree(domains[i]);
+ }
+ free(domains);
+
+ g_signal_connect(select, "changed",
+ G_CALLBACK(vm_list_selection_changed_dialog_cb), entry);
+ if (gtk_tree_model_get_iter_first(store, &iter)) {
+ gtk_tree_selection_select_iter(select, &iter);
+ }
+ /* show and wait for response */
+ vm_name = NULL;
+ if (vms_running > 0) {
+ gtk_widget_show_all(dialog);
+ if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
+ vm_name = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
+ }
+ } else {
+ g_warning("No domain running");
+ }
+ gtk_widget_destroy(dialog);
+
+ return vm_name;
+}
+
+static void
virt_viewer_error_func (void *data G_GNUC_UNUSED,
virErrorPtr error G_GNUC_UNUSED)
{
@@ -700,6 +760,11 @@ virt_viewer_connect(VirtViewerApp *app)
return -1;
}
+ if (priv->domkey == NULL && (priv->domkey = choose_vm_dialog(priv->conn)) == NULL) {
+ g_debug("No domain chosen");
+ return -1;
+ }
+
if (!virt_viewer_app_initial_connect(app, &error)) {
if (error)
g_warning("%s", error->message);
--
1.9.3
More information about the virt-tools-list
mailing list