[virt-tools-list] [PATCH virt-viewer 2/2] vnc: add connection by file

Marc-André Lureau marcandre.lureau at gmail.com
Thu Jan 17 11:53:34 UTC 2013


Learn to connect to a VNC server with the connection details file, ex:

[virt-viewer]
type=vnc
host=localhost
port=2356
password=foobar

v2:
- add username/password support

https://bugzilla.redhat.com/show_bug.cgi?id=843410
---
 src/virt-viewer-app.c         |  2 +-
 src/virt-viewer-auth.c        | 16 +++++++++++++-
 src/virt-viewer-auth.h        |  4 +++-
 src/virt-viewer-session-vnc.c | 50 +++++++++++++++++++++++++++----------------
 src/virt-viewer-session-vnc.h |  2 +-
 5 files changed, 52 insertions(+), 22 deletions(-)

diff --git a/src/virt-viewer-app.c b/src/virt-viewer-app.c
index 6b3ad6d..760ed77 100644
--- a/src/virt-viewer-app.c
+++ b/src/virt-viewer-app.c
@@ -706,7 +706,7 @@ virt_viewer_app_create_session(VirtViewerApp *self, const gchar *type)
         GtkWindow *window = virt_viewer_window_get_window(priv->main_window);
         virt_viewer_app_trace(self, "Guest %s has a %s display",
                               priv->guest_name, type);
-        priv->session = virt_viewer_session_vnc_new(window);
+        priv->session = virt_viewer_session_vnc_new(self, window);
     } else
 #endif
 #ifdef HAVE_SPICE_GTK
diff --git a/src/virt-viewer-auth.c b/src/virt-viewer-auth.c
index 487e67c..9751ac8 100644
--- a/src/virt-viewer-auth.c
+++ b/src/virt-viewer-auth.c
@@ -96,7 +96,8 @@ virt_viewer_auth_collect_credentials(GtkWindow *window,
 
 #ifdef HAVE_GTK_VNC
 void
-virt_viewer_auth_vnc_credentials(GtkWindow *window,
+virt_viewer_auth_vnc_credentials(VirtViewerSession *session,
+                                 GtkWindow *window,
                                  GtkWidget *vnc,
                                  GValueArray *credList,
                                  char *vncAddress)
@@ -125,6 +126,19 @@ virt_viewer_auth_vnc_credentials(GtkWindow *window,
         }
     }
 
+    VirtViewerFile *file = virt_viewer_session_get_file(session);
+    if (file != NULL) {
+        if (wantUsername && virt_viewer_file_is_set(file, "username")) {
+            username = virt_viewer_file_get_username(file);
+            wantUsername = FALSE;
+        }
+        if (wantPassword && virt_viewer_file_is_set(file, "password")) {
+            password = virt_viewer_file_get_password(file);
+            g_message (password);
+            wantPassword = FALSE;
+        }
+    }
+
     if (wantUsername || wantPassword) {
         int ret = virt_viewer_auth_collect_credentials(window,
                                                        "VNC", vncAddress,
diff --git a/src/virt-viewer-auth.h b/src/virt-viewer-auth.h
index 5b4c546..1f102e0 100644
--- a/src/virt-viewer-auth.h
+++ b/src/virt-viewer-auth.h
@@ -29,9 +29,11 @@
 #include <libvirt/libvirt.h>
 #endif
 
+#include "virt-viewer-session.h"
 #include "virt-viewer-util.h"
 
-void virt_viewer_auth_vnc_credentials(GtkWindow *window,
+void virt_viewer_auth_vnc_credentials(VirtViewerSession *session,
+                                      GtkWindow *window,
                                       GtkWidget *vnc,
                                       GValueArray *credList,
                                       char *vncAddress);
diff --git a/src/virt-viewer-session-vnc.c b/src/virt-viewer-session-vnc.c
index 3245de3..3abd4e4 100644
--- a/src/virt-viewer-session-vnc.c
+++ b/src/virt-viewer-session-vnc.c
@@ -201,7 +201,8 @@ virt_viewer_session_vnc_open_uri(VirtViewerSession* session,
                                  const gchar *uristr)
 {
     VirtViewerSessionVnc *self = VIRT_VIEWER_SESSION_VNC(session);
-    xmlURIPtr uri = NULL;
+    VirtViewerFile *file = virt_viewer_session_get_file(session);
+    VirtViewerApp *app = virt_viewer_session_get_app(session);
     gchar *portstr;
     gchar *hoststr = NULL;
     gboolean ret;
@@ -209,20 +210,33 @@ virt_viewer_session_vnc_open_uri(VirtViewerSession* session,
     g_return_val_if_fail(self != NULL, FALSE);
     g_return_val_if_fail(self->priv->vnc != NULL, FALSE);
 
-    if (!(uri = xmlParseURI(uristr)))
-        return FALSE;
-
-    portstr = g_strdup_printf("%d", uri->port);
-
-    if (uri->server) {
-        if (uri->server[0] == '[') {
-            gchar *tmp;
-            hoststr = g_strdup(uri->server + 1);
-            if ((tmp = strchr(hoststr, ']')))
-                *tmp = '\0';
-        } else {
-            hoststr = g_strdup(uri->server);
+    if (file) {
+        g_return_val_if_fail(virt_viewer_file_is_set(file, "port"), FALSE);
+        g_return_val_if_fail(virt_viewer_file_is_set(file, "host"), FALSE);
+
+        portstr = g_strdup_printf("%d", virt_viewer_file_get_port(file));
+        hoststr = g_strdup(virt_viewer_file_get_host(file));
+
+        virt_viewer_file_fill_app(file, app);
+    } else {
+        xmlURIPtr uri = NULL;
+        if (!(uri = xmlParseURI(uristr)))
+            return FALSE;
+
+        portstr = g_strdup_printf("%d", uri->port);
+
+        if (uri->server) {
+            if (uri->server[0] == '[') {
+                gchar *tmp;
+                hoststr = g_strdup(uri->server + 1);
+                if ((tmp = strchr(hoststr, ']')))
+                    *tmp = '\0';
+            } else {
+                hoststr = g_strdup(uri->server);
+            }
         }
+
+        xmlFreeURI(uri);
     }
 
     ret = vnc_display_open_host(self->priv->vnc,
@@ -230,7 +244,6 @@ virt_viewer_session_vnc_open_uri(VirtViewerSession* session,
                                 portstr);
     g_free(portstr);
     g_free(hoststr);
-    xmlFreeURI(uri);
     return ret;
 }
 
@@ -242,7 +255,8 @@ virt_viewer_session_vnc_auth_credential(GtkWidget *src,
 {
     VirtViewerSessionVnc *self = VIRT_VIEWER_SESSION_VNC(session);
 
-    virt_viewer_auth_vnc_credentials(self->priv->main_window,
+    virt_viewer_auth_vnc_credentials(session,
+                                     self->priv->main_window,
                                      src,
                                      credList,
                                      NULL);
@@ -288,11 +302,11 @@ virt_viewer_session_vnc_close(VirtViewerSession* session)
 }
 
 VirtViewerSession *
-virt_viewer_session_vnc_new(GtkWindow *main_window)
+virt_viewer_session_vnc_new(VirtViewerApp *app, GtkWindow *main_window)
 {
     VirtViewerSessionVnc *session;
 
-    session = g_object_new(VIRT_VIEWER_TYPE_SESSION_VNC, NULL);
+    session = g_object_new(VIRT_VIEWER_TYPE_SESSION_VNC, "app", app, NULL);
 
     session->priv->vnc = VNC_DISPLAY(vnc_display_new());
     g_object_ref_sink(session->priv->vnc);
diff --git a/src/virt-viewer-session-vnc.h b/src/virt-viewer-session-vnc.h
index 2b95dde..ab7b595 100644
--- a/src/virt-viewer-session-vnc.h
+++ b/src/virt-viewer-session-vnc.h
@@ -64,7 +64,7 @@ struct _VirtViewerSessionVncClass {
 
 GType virt_viewer_session_vnc_get_type(void);
 
-VirtViewerSession *virt_viewer_session_vnc_new(GtkWindow *main_window);
+VirtViewerSession *virt_viewer_session_vnc_new(VirtViewerApp *app, GtkWindow *main_window);
 
 G_END_DECLS
 
-- 
1.8.1.rc1.17.g75ed918




More information about the virt-tools-list mailing list