[virt-tools-list] [PATCH] remote-viewer: Allow toggling shared clipboard

Marc-André Lureau marcandre.lureau at redhat.com
Tue Feb 26 12:42:06 UTC 2019


Hi

On Tue, Feb 26, 2019 at 1:19 PM Aleksei Nikiforov
<darktemplar at basealt.ru> wrote:
>
> Signed-off-by: Aleksei Nikiforov <darktemplar at basealt.ru>

looks good overall,

please update the man page for the new key

> ---
>  src/resources/ui/virt-viewer.ui | 11 +++++++++++
>  src/virt-viewer-app.c           | 25 +++++++++++++++++++++++++
>  src/virt-viewer-app.h           |  3 +++
>  src/virt-viewer-session-spice.c | 32 ++++++++++++++++++++++++++++++--
>  src/virt-viewer-session.h       |  2 ++
>  src/virt-viewer-window.c        | 28 ++++++++++++++++++++++++++++
>  6 files changed, 99 insertions(+), 2 deletions(-)
>
> diff --git a/src/resources/ui/virt-viewer.ui b/src/resources/ui/virt-viewer.ui
> index e9609ec..1eccd35 100644
> --- a/src/resources/ui/virt-viewer.ui
> +++ b/src/resources/ui/virt-viewer.ui
> @@ -42,6 +42,17 @@
>                              <signal name="activate" handler="virt_viewer_window_menu_file_screenshot" swapped="no"/>
>                            </object>
>                          </child>
> +                        <child>
> +                          <object class="GtkCheckMenuItem" id="menu-file-share-clipboard">
> +                            <property name="visible">True</property>
> +                            <property name="can_focus">False</property>
> +                            <property name="use_action_appearance">False</property>
> +                            <property name="accel_path"><virt-viewer>/file/share-clipboard</property>
> +                            <property name="label" translatable="yes">Share clipboard</property>
> +                            <property name="use_underline">True</property>
> +                            <signal name="toggled" handler="virt_viewer_window_menu_file_share_clipboard" swapped="no"/>
> +                          </object>
> +                        </child>

I'd rather have it under "Preferences" dialog (it's not an "action").

>                          <child>
>                            <object class="GtkMenuItem" id="menu-file-usb-device-selection">
>                              <property name="visible">True</property>
> diff --git a/src/virt-viewer-app.c b/src/virt-viewer-app.c
> index e0e6e63..6b50e5f 100644
> --- a/src/virt-viewer-app.c
> +++ b/src/virt-viewer-app.c
> @@ -2571,6 +2571,31 @@ gboolean virt_viewer_app_get_session_cancelled(VirtViewerApp *self)
>      return self->priv->cancelled;
>  }
>
> +gboolean virt_viewer_app_get_config_share_clipboard(VirtViewerApp *self)
> +{
> +    VirtViewerAppPrivate *priv = self->priv;
> +
> +    GError *error = NULL;
> +
> +    gboolean share_clipboard = g_key_file_get_boolean(priv->config,
> +                                                      "virt-viewer", "share-clipboard", &error);
> +
> +    if (error) {
> +        share_clipboard = TRUE; /* backwards-compatible default value */
> +        g_clear_error(&error);
> +    }
> +
> +    return share_clipboard;
> +}
> +
> +void virt_viewer_app_set_config_share_clipboard(VirtViewerApp *self, gboolean enable)
> +{
> +    VirtViewerAppPrivate *priv = self->priv;
> +
> +    g_key_file_set_boolean(priv->config,
> +                           "virt-viewer", "share-clipboard", enable);
> +}
> +
>  /*
>   * Local variables:
>   *  c-indent-level: 4
> diff --git a/src/virt-viewer-app.h b/src/virt-viewer-app.h
> index 16b1c8c..962226b 100644
> --- a/src/virt-viewer-app.h
> +++ b/src/virt-viewer-app.h
> @@ -99,6 +99,9 @@ void virt_viewer_app_show_preferences(VirtViewerApp *app, GtkWidget *parent);
>  void virt_viewer_app_set_menus_sensitive(VirtViewerApp *self, gboolean sensitive);
>  gboolean virt_viewer_app_get_session_cancelled(VirtViewerApp *self);
>
> +gboolean virt_viewer_app_get_config_share_clipboard(VirtViewerApp *self);
> +void virt_viewer_app_set_config_share_clipboard(VirtViewerApp *self, gboolean enable);
> +
>  G_END_DECLS
>
>  #endif /* VIRT_VIEWER_APP_H */
> diff --git a/src/virt-viewer-session-spice.c b/src/virt-viewer-session-spice.c
> index fdc7004..2e8b6a8 100644
> --- a/src/virt-viewer-session-spice.c
> +++ b/src/virt-viewer-session-spice.c
> @@ -62,7 +62,8 @@ enum {
>      PROP_MAIN_WINDOW
>  };
>
> -
> +static gboolean virt_viewer_session_spice_get_share_clipboard(VirtViewerSession* session);
> +static void virt_viewer_session_spice_set_share_clipboard(VirtViewerSession* session, gboolean value);
>  static void virt_viewer_session_spice_close(VirtViewerSession *session);
>  static gboolean virt_viewer_session_spice_open_fd(VirtViewerSession *session, int fd);
>  static gboolean virt_viewer_session_spice_open_host(VirtViewerSession *session, const gchar *host, const gchar *port, const gchar *tlsport);
> @@ -247,6 +248,8 @@ virt_viewer_session_spice_class_init(VirtViewerSessionSpiceClass *klass)
>      oclass->constructed = virt_viewer_session_spice_constructed;
>
>      dclass->close = virt_viewer_session_spice_close;
> +    dclass->get_share_clipboard = virt_viewer_session_spice_get_share_clipboard;
> +    dclass->set_share_clipboard = virt_viewer_session_spice_set_share_clipboard;
>      dclass->open_fd = virt_viewer_session_spice_open_fd;
>      dclass->open_host = virt_viewer_session_spice_open_host;
>      dclass->open_uri = virt_viewer_session_spice_open_uri;
> @@ -392,7 +395,9 @@ create_spice_session(VirtViewerSessionSpice *self)
>      spice_set_session_option(self->priv->session);
>
>      self->priv->gtk_session = spice_gtk_session_get(self->priv->session);
> -    g_object_set(self->priv->gtk_session, "auto-clipboard", TRUE, NULL);
> +
> +    gboolean share_clipboard = virt_viewer_app_get_config_share_clipboard(virt_viewer_session_get_app(VIRT_VIEWER_SESSION(self)));

In general we don't like mixing declarations and code, can you move it
before code?

> +    g_object_set(self->priv->gtk_session, "auto-clipboard", share_clipboard, NULL);
>
>      virt_viewer_signal_connect_object(self->priv->session, "channel-new",
>                                        G_CALLBACK(virt_viewer_session_spice_channel_new), self, 0);
> @@ -629,6 +634,29 @@ virt_viewer_session_spice_open_uri(VirtViewerSession *session,
>      return spice_session_connect(self->priv->session);
>  }
>
> +static gboolean
> +virt_viewer_session_spice_get_share_clipboard(VirtViewerSession* session)
> +{
> +    VirtViewerSessionSpice *self = VIRT_VIEWER_SESSION_SPICE(session);
> +
> +    g_return_val_if_fail(self != NULL, TRUE); /* backwards-compatible default value */
> +
> +    gboolean value;

same

> +    g_object_get(self->priv->gtk_session, "auto-clipboard", &value, NULL);
> +    return value;
> +}
> +
> +static void
> +virt_viewer_session_spice_set_share_clipboard(VirtViewerSession* session,
> +                                              gboolean value)
> +{
> +    VirtViewerSessionSpice *self = VIRT_VIEWER_SESSION_SPICE(session);
> +
> +    g_return_if_fail(self != NULL);
> +
> +    g_object_set(self->priv->gtk_session, "auto-clipboard", value, NULL);
> +}
> +
>  static gboolean
>  virt_viewer_session_spice_open_fd(VirtViewerSession *session,
>                                    int fd)
> diff --git a/src/virt-viewer-session.h b/src/virt-viewer-session.h
> index 8ee4d8a..4f2914d 100644
> --- a/src/virt-viewer-session.h
> +++ b/src/virt-viewer-session.h
> @@ -74,6 +74,8 @@ struct _VirtViewerSessionClass {
>      void (* smartcard_insert) (VirtViewerSession* session);
>      void (* smartcard_remove) (VirtViewerSession* session);
>      const gchar* (* mime_type) (VirtViewerSession* session);
> +    gboolean (* get_share_clipboard) (VirtViewerSession* session);
> +    void (* set_share_clipboard) (VirtViewerSession* session, gboolean value);
>
>      /* signals */
>      void (*session_connected)(VirtViewerSession *session);
> diff --git a/src/virt-viewer-window.c b/src/virt-viewer-window.c
> index af3441f..4b77e2c 100644
> --- a/src/virt-viewer-window.c
> +++ b/src/virt-viewer-window.c
> @@ -59,6 +59,7 @@ void virt_viewer_window_menu_help_guest_details(GtkWidget *menu, VirtViewerWindo
>  void virt_viewer_window_menu_view_fullscreen(GtkWidget *menu, VirtViewerWindow *self);
>  void virt_viewer_window_menu_send(GtkWidget *menu, VirtViewerWindow *self);
>  void virt_viewer_window_menu_file_screenshot(GtkWidget *menu, VirtViewerWindow *self);
> +void virt_viewer_window_menu_file_share_clipboard(GtkWidget *menu, VirtViewerWindow *self);
>  void virt_viewer_window_menu_file_usb_device_selection(GtkWidget *menu, VirtViewerWindow *self);
>  void virt_viewer_window_menu_file_smartcard_insert(GtkWidget *menu, VirtViewerWindow *self);
>  void virt_viewer_window_menu_file_smartcard_remove(GtkWidget *menu, VirtViewerWindow *self);
> @@ -233,6 +234,9 @@ virt_viewer_window_constructed(GObject *object)
>      g_signal_connect(priv->app, "notify::enable-accel",
>                       G_CALLBACK(rebuild_combo_menu), object);
>      rebuild_combo_menu(NULL, NULL, object);
> +
> +    gboolean share_clipboard = virt_viewer_app_get_config_share_clipboard(priv->app);
> +    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(gtk_builder_get_object(priv->builder, "menu-file-share-clipboard")), share_clipboard);
>  }
>
>  static void
> @@ -325,6 +329,8 @@ virt_viewer_window_init (VirtViewerWindow *self)
>      /* make sure they can be activated even if the menu item is not visible */
>      g_signal_connect(gtk_builder_get_object(priv->builder, "menu-view-fullscreen"),
>                       "can-activate-accel", G_CALLBACK(can_activate_cb), self);
> +    g_signal_connect(gtk_builder_get_object(priv->builder, "menu-file-share-clipboard"),
> +                     "can-activate-accel", G_CALLBACK(can_activate_cb), self);
>      g_signal_connect(gtk_builder_get_object(priv->builder, "menu-file-smartcard-insert"),
>                       "can-activate-accel", G_CALLBACK(can_activate_cb), self);
>      g_signal_connect(gtk_builder_get_object(priv->builder, "menu-file-smartcard-remove"),
> @@ -1003,6 +1009,25 @@ virt_viewer_window_menu_file_screenshot(GtkWidget *menu G_GNUC_UNUSED,
>      gtk_widget_destroy(dialog);
>  }
>
> +G_MODULE_EXPORT void
> +virt_viewer_window_menu_file_share_clipboard(GtkWidget *menu G_GNUC_UNUSED,
> +                                             VirtViewerWindow *self)
> +{
> +    VirtViewerSession *session = virt_viewer_app_get_session(self->priv->app);
> +    VirtViewerSessionClass *klass;
> +
> +    g_return_if_fail(session != NULL);
> +
> +    gboolean share_buffer = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menu));
> +
> +    klass = VIRT_VIEWER_SESSION_GET_CLASS(session);
> +    if (klass->set_share_clipboard) {
> +        klass->set_share_clipboard(session, share_buffer);
> +    }
> +
> +    virt_viewer_app_set_config_share_clipboard(self->priv->app, share_buffer);

By using app properties, you should be able to use
g_object_bind_property() which would eventually simplify the code. See
VirtViewerSession:share-folder prop & usage for an example of how
properties are bound from UI to spice-gtk.

> +}
> +
>  G_MODULE_EXPORT void
>  virt_viewer_window_menu_file_usb_device_selection(GtkWidget *menu G_GNUC_UNUSED,
>                                                    VirtViewerWindow *self)
> @@ -1347,6 +1372,9 @@ virt_viewer_window_set_menus_sensitive(VirtViewerWindow *self, gboolean sensitiv
>      menu = GTK_WIDGET(gtk_builder_get_object(priv->builder, "menu-file-screenshot"));
>      gtk_widget_set_sensitive(menu, sensitive);
>
> +    menu = GTK_WIDGET(gtk_builder_get_object(priv->builder, "menu-file-share-clipboard"));
> +    gtk_widget_set_sensitive(menu, sensitive);
> +
>      menu = GTK_WIDGET(gtk_builder_get_object(priv->builder, "menu-view-zoom"));
>      gtk_widget_set_sensitive(menu, sensitive);
>
> --
> 2.19.2
>
> _______________________________________________
> virt-tools-list mailing list
> virt-tools-list at redhat.com
> https://www.redhat.com/mailman/listinfo/virt-tools-list




More information about the virt-tools-list mailing list