[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[virt-tools-list] [PATCH 2/2] Only make the USB device selection sensitive when the vm is USB capable



Signed-off-by: Hans de Goede <hdegoede redhat com>
---
 src/virt-viewer-app.c           |   19 +++++++++++++++++++
 src/virt-viewer-session-spice.c |   13 +++++++++++++
 src/virt-viewer-session.c       |   13 +++++++++++++
 src/virt-viewer-session.h       |    2 ++
 src/virt-viewer-window.c        |   13 +++++++++++++
 src/virt-viewer-window.h        |    1 +
 src/virt-viewer.xml             |    1 +
 7 files changed, 62 insertions(+), 0 deletions(-)

diff --git a/src/virt-viewer-app.c b/src/virt-viewer-app.c
index d97ffa2..f14e46b 100644
--- a/src/virt-viewer-app.c
+++ b/src/virt-viewer-app.c
@@ -449,6 +449,21 @@ virt_viewer_app_update_title(VirtViewerApp *self)
 	g_hash_table_foreach(self->priv->windows, update_title, NULL);
 }
 
+static void set_usb_options_sensitive(gpointer key G_GNUC_UNUSED,
+					 gpointer value,
+					 gpointer user_data)
+{
+	virt_viewer_window_set_usb_options_sensitive(
+		VIRT_VIEWER_WINDOW(value), GPOINTER_TO_INT(user_data));
+}
+
+static void
+virt_viewer_app_set_usb_options_sensitive(VirtViewerApp *self, gboolean sensitive)
+{
+	g_hash_table_foreach(self->priv->windows, set_usb_options_sensitive,
+			     GINT_TO_POINTER(sensitive));
+}
+
 static VirtViewerWindow *
 virt_viewer_app_get_nth_window(VirtViewerApp *self, gint nth)
 {
@@ -981,7 +996,10 @@ static void
 virt_viewer_app_initialized(VirtViewerSession *session G_GNUC_UNUSED,
 			    VirtViewerApp *self)
 {
+	gboolean has_usb = virt_viewer_session_has_usb(self->priv->session);
+
 	virt_viewer_app_update_title(self);
+	virt_viewer_app_set_usb_options_sensitive(self, has_usb);
 }
 
 static void
@@ -995,6 +1013,7 @@ virt_viewer_app_disconnected(VirtViewerSession *session G_GNUC_UNUSED,
 						      _("Unable to connect to the graphic server %s"),
 						      priv->pretty_address);
 	}
+	virt_viewer_app_set_usb_options_sensitive(self, FALSE);
 	virt_viewer_app_deactivate(self);
 }
 
diff --git a/src/virt-viewer-session-spice.c b/src/virt-viewer-session-spice.c
index 9832a4b..a39ea07 100644
--- a/src/virt-viewer-session-spice.c
+++ b/src/virt-viewer-session-spice.c
@@ -54,6 +54,7 @@ static gboolean virt_viewer_session_spice_open_fd(VirtViewerSession *session, in
 static gboolean virt_viewer_session_spice_open_host(VirtViewerSession *session, char *host, char *port);
 static gboolean virt_viewer_session_spice_open_uri(VirtViewerSession *session, char *uri);
 static gboolean virt_viewer_session_spice_channel_open_fd(VirtViewerSession *session, VirtViewerSessionChannel *channel, int fd);
+static gboolean virt_viewer_session_spice_has_usb(VirtViewerSession *session);
 static void virt_viewer_session_spice_usb_device_selection(VirtViewerSession *session, GtkWindow *parent);
 static void virt_viewer_session_spice_channel_new(SpiceSession *s,
 						  SpiceChannel *channel,
@@ -120,6 +121,7 @@ virt_viewer_session_spice_class_init(VirtViewerSessionSpiceClass *klass)
 	dclass->open_host = virt_viewer_session_spice_open_host;
 	dclass->open_uri = virt_viewer_session_spice_open_uri;
 	dclass->channel_open_fd = virt_viewer_session_spice_channel_open_fd;
+	dclass->has_usb = virt_viewer_session_spice_has_usb;
 	dclass->usb_device_selection = virt_viewer_session_spice_usb_device_selection;
 
 	g_type_class_add_private(klass, sizeof(VirtViewerSessionSpicePrivate));
@@ -310,6 +312,17 @@ virt_viewer_session_spice_main_channel_event(SpiceChannel *channel G_GNUC_UNUSED
 	g_free(password);
 }
 
+static gboolean
+virt_viewer_session_spice_has_usb(VirtViewerSession *session)
+{
+	VirtViewerSessionSpice *self = VIRT_VIEWER_SESSION_SPICE(session);
+	VirtViewerSessionSpicePrivate *priv = self->priv;
+
+	return spice_usb_device_manager_get(priv->session, NULL) &&
+	       spice_session_has_channel_type(priv->session,
+					      SPICE_CHANNEL_USBREDIR);
+}
+
 static void
 virt_viewer_session_spice_usb_device_selection(VirtViewerSession *session,
 					       GtkWindow *parent)
diff --git a/src/virt-viewer-session.c b/src/virt-viewer-session.c
index 8fb4646..7ed6333 100644
--- a/src/virt-viewer-session.c
+++ b/src/virt-viewer-session.c
@@ -352,6 +352,19 @@ gboolean virt_viewer_session_get_auto_usbredir(VirtViewerSession *self)
 	return self->priv->auto_usbredir;
 }
 
+gboolean virt_viewer_session_has_usb(VirtViewerSession *self)
+{
+	VirtViewerSessionClass *klass;
+
+	g_return_val_if_fail(VIRT_VIEWER_IS_SESSION(self), FALSE);
+
+	klass = VIRT_VIEWER_SESSION_GET_CLASS(self);
+	if (klass->has_usb == NULL)
+		return FALSE;
+
+	return klass->has_usb(self);
+}
+
 void virt_viewer_session_usb_device_selection(VirtViewerSession   *self,
 					      GtkWindow           *parent)
 {
diff --git a/src/virt-viewer-session.h b/src/virt-viewer-session.h
index 565ff31..f02c1ee 100644
--- a/src/virt-viewer-session.h
+++ b/src/virt-viewer-session.h
@@ -70,6 +70,7 @@ struct _VirtViewerSessionClass {
 	gboolean (* open_host) (VirtViewerSession* session, char *host, char *port);
 	gboolean (* open_uri) (VirtViewerSession* session, char *uri);
 	gboolean (* channel_open_fd) (VirtViewerSession* session, VirtViewerSessionChannel *channel, int fd);
+	gboolean (* has_usb) (VirtViewerSession* session);
 	void (* usb_device_selection) (VirtViewerSession* session, GtkWindow *parent);
 
 	/* signals */
@@ -113,6 +114,7 @@ gboolean virt_viewer_session_open_uri(VirtViewerSession *session, gchar *uri);
 void virt_viewer_session_set_auto_usbredir(VirtViewerSession* session, gboolean auto_usbredir);
 gboolean virt_viewer_session_get_auto_usbredir(VirtViewerSession* session);
 
+gboolean virt_viewer_session_has_usb(VirtViewerSession *self);
 void virt_viewer_session_usb_device_selection(VirtViewerSession   *self,
 					      GtkWindow           *parent);
 
diff --git a/src/virt-viewer-window.c b/src/virt-viewer-window.c
index 555bdae..1a13e16 100644
--- a/src/virt-viewer-window.c
+++ b/src/virt-viewer-window.c
@@ -867,6 +867,19 @@ virt_viewer_window_update_title(VirtViewerWindow *self)
 }
 
 void
+virt_viewer_window_set_usb_options_sensitive(VirtViewerWindow *self, gboolean sensitive)
+{
+	VirtViewerWindowPrivate *priv;
+	GtkWidget *menu;
+
+	g_return_if_fail(VIRT_VIEWER_IS_WINDOW(self));
+
+	priv = self->priv;
+	menu = GTK_WIDGET(gtk_builder_get_object(priv->builder, "menu-file-usb-device-selection"));
+	gtk_widget_set_sensitive(menu, sensitive);
+}
+
+void
 virt_viewer_window_set_display(VirtViewerWindow *self, VirtViewerDisplay *display)
 {
 	VirtViewerWindowPrivate *priv;
diff --git a/src/virt-viewer-window.h b/src/virt-viewer-window.h
index cf66f5e..e5c184f 100644
--- a/src/virt-viewer-window.h
+++ b/src/virt-viewer-window.h
@@ -63,6 +63,7 @@ GType virt_viewer_window_get_type (void);
 GtkWindow* virt_viewer_window_get_window (VirtViewerWindow* window);
 VirtViewerNotebook* virt_viewer_window_get_notebook (VirtViewerWindow* window);
 void virt_viewer_window_set_display(VirtViewerWindow *self, VirtViewerDisplay *display);
+void virt_viewer_window_set_usb_options_sensitive(VirtViewerWindow *self, gboolean sensitive);
 void virt_viewer_window_update_title(VirtViewerWindow *self);
 void virt_viewer_window_set_zoom_level(VirtViewerWindow *self, gint zoom_level);
 gint virt_viewer_window_get_zoom_level(VirtViewerWindow *self);
diff --git a/src/virt-viewer.xml b/src/virt-viewer.xml
index d9c1334..9075f26 100644
--- a/src/virt-viewer.xml
+++ b/src/virt-viewer.xml
@@ -38,6 +38,7 @@
                     <child>
                       <object class="GtkMenuItem" id="menu-file-usb-device-selection">
                         <property name="visible">True</property>
+                        <property name="sensitive">False</property>
                         <property name="can_focus">False</property>
                         <property name="use_action_appearance">False</property>
                         <property name="label" translatable="yes">USB device selection</property>
-- 
1.7.7.6


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]