[virt-tools-list] [virt-viewer v2] Use GResource for loading ui files

Fabiano Fidêncio fidencio at redhat.com
Wed Mar 2 15:23:17 UTC 2016


Let's take advantage of GResource for loading ui files in a better and
cleaner way than virt_viewer_util_load_ui() was doing.
It also brings the benefit, at least for developers, of being able to
test ui changes without having to "make install" virt-viewer.

Signed-off-by: Fabiano Fidêncio <fidencio at redhat.com>
---
Changes since v1:
 - Adressed all comments from Eduardo and Jonathon.
---
 mingw-virt-viewer.spec.in     | 18 ++--------------
 src/Makefile.am               | 21 +++++++++++++------
 src/virt-viewer-about.xml     |  1 -
 src/virt-viewer-app.c         |  5 +++++
 src/virt-viewer-util.c        | 48 +++++--------------------------------------
 src/virt-viewer-util.h        |  1 +
 src/virt-viewer-window.c      | 20 ++++++++++++++----
 src/virt-viewer.gresource.xml | 19 +++++++++++++++++
 virt-viewer.spec.in           |  9 --------
 9 files changed, 63 insertions(+), 79 deletions(-)
 create mode 100644 src/virt-viewer.gresource.xml

diff --git a/mingw-virt-viewer.spec.in b/mingw-virt-viewer.spec.in
index b200db7..ddea296 100644
--- a/mingw-virt-viewer.spec.in
+++ b/mingw-virt-viewer.spec.in
@@ -114,10 +114,12 @@ MinGW Windows virt-viewer MSI
 %mingw_make_install DESTDIR=$RPM_BUILD_ROOT
 
 %if 0%{?mingw_build_win32} == 1
+mkdir $RPM_BUILD_ROOT/%{mingw32_datadir}/virt-viewer
 cp build_win32$MINGW_BUILDDIR_SUFFIX/data/virt-viewer-x86- at VERSION@.msi $RPM_BUILD_ROOT/%{mingw32_datadir}/virt-viewer
 %endif
 
 %if 0%{?mingw_build_win64} == 1
+mkdir $RPM_BUILD_ROOT/%{mingw64_datadir}/virt-viewer
 cp build_win64$MINGW_BUILDDIR_SUFFIX/data/virt-viewer-x64- at VERSION@.msi $RPM_BUILD_ROOT/%{mingw64_datadir}/virt-viewer
 %endif
 
@@ -138,14 +140,6 @@ rm -rf $RPM_BUILD_ROOT
 %{mingw32_bindir}/debug-helper.exe
 
 %dir %{mingw32_datadir}/virt-viewer/
-%dir %{mingw32_datadir}/virt-viewer/ui/
-%{mingw32_datadir}/virt-viewer/ui/virt-viewer.xml
-%{mingw32_datadir}/virt-viewer/ui/virt-viewer-about.xml
-%{mingw32_datadir}/virt-viewer/ui/virt-viewer-auth.xml
-%{mingw32_datadir}/virt-viewer/ui/virt-viewer-guest-details.xml
-%{mingw32_datadir}/virt-viewer/ui/virt-viewer-vm-connection.xml
-%{mingw32_datadir}/virt-viewer/ui/virt-viewer-preferences.xml
-%{mingw32_datadir}/virt-viewer/ui/remote-viewer-connect.xml
 %{mingw32_datadir}/icons/hicolor/*/apps/*
 %{mingw32_datadir}/icons/hicolor/*/devices/*
 
@@ -163,14 +157,6 @@ rm -rf $RPM_BUILD_ROOT
 %{mingw64_bindir}/debug-helper.exe
 
 %dir %{mingw64_datadir}/virt-viewer/
-%dir %{mingw64_datadir}/virt-viewer/ui/
-%{mingw64_datadir}/virt-viewer/ui/virt-viewer.xml
-%{mingw64_datadir}/virt-viewer/ui/virt-viewer-about.xml
-%{mingw64_datadir}/virt-viewer/ui/virt-viewer-auth.xml
-%{mingw64_datadir}/virt-viewer/ui/virt-viewer-guest-details.xml
-%{mingw64_datadir}/virt-viewer/ui/virt-viewer-vm-connection.xml
-%{mingw64_datadir}/virt-viewer/ui/virt-viewer-preferences.xml
-%{mingw64_datadir}/virt-viewer/ui/remote-viewer-connect.xml
 %{mingw64_datadir}/icons/hicolor/*/apps/*
 %{mingw64_datadir}/icons/hicolor/*/devices/*
 
diff --git a/src/Makefile.am b/src/Makefile.am
index f42a7bf..a42c01e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -5,8 +5,7 @@ bin_PROGRAMS =
 
 noinst_LTLIBRARIES = libvirt-viewer.la
 
-builderxmldir = $(pkgdatadir)/ui
-builderxml_DATA =				\
+noinst_DATA =						\
 	virt-viewer.xml				\
 	virt-viewer-about.xml			\
 	virt-viewer-auth.xml			\
@@ -17,9 +16,12 @@ builderxml_DATA =				\
 	$(NULL)
 
 EXTRA_DIST =					\
-	$(builderxml_DATA)			\
+	$(noinst_DATA)					\
+	virt-viewer-resources.c			\
+	virt-viewer-resources.h			\
 	virt-viewer-enums.c.etemplate		\
 	virt-viewer-enums.h.etemplate		\
+	virt-viewer.gresource.xml		\
 	$(NULL)
 
 ENUMS_FILES =					\
@@ -27,15 +29,24 @@ ENUMS_FILES =					\
 	$(NULL)
 
 BUILT_SOURCES =					\
+	virt-viewer-resources.h			\
+	virt-viewer-resources.c			\
 	virt-viewer-enums.h			\
 	virt-viewer-enums.c			\
 	$(NULL)
 
-$(BUILT_SOURCES): %: %.etemplate $(ENUMS_FILES)
+virt-viewer-resources.c virt-viewer-resources.h: virt-viewer.gresource.xml Makefile $(shell glib-compile-resources --generate-dependencies --sourcedir $(srcdir) $(srcdir)/virt-viewer.gresource.xml)
+	$(AM_V_GEN) glib-compile-resources --target=$@ --sourcedir=$(srcdir) --generate --c-name virt_viewer $<
+
+virt-viewer-enums.c virt-viewer-enums.h: %: %.etemplate $(ENUMS_FILES)
 	$(AM_V_GEN)$(GLIB_MKENUMS) --template $^ | \
             sed -e 's/VIRT_TYPE_VIEWER/VIRT_VIEWER_TYPE/' \
                 -e 's,#include "$(srcdir)/,#include ",' > $@
 
+CLEANFILES = \
+	$(BUILT_SOURCES)				\
+	$(NULL)
+
 libvirt_viewer_la_SOURCES =					\
 	$(BUILT_SOURCES)				\
 	virt-viewer-util.h				\
@@ -185,8 +196,6 @@ if OS_WIN32
 remote_viewer_LDFLAGS += -Wl,--subsystem,windows
 endif
 
-AM_CPPFLAGS = -DPACKAGE_DATADIR=\""$(pkgdatadir)"\"
-
 VIRT_VIEWER_RES = virt-viewer.rc virt-viewer.manifest
 ICONDIR = $(top_builddir)/icons
 MANIFESTDIR = $(srcdir)
diff --git a/src/virt-viewer-about.xml b/src/virt-viewer-about.xml
index 65e2da6..28e38c8 100644
--- a/src/virt-viewer-about.xml
+++ b/src/virt-viewer-about.xml
@@ -36,7 +36,6 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 Marc-André Lureau
 </property>
     <property name="translator_credits" translatable="yes">The Fedora Translation Team</property>
-    <property name="logo_icon_name">virt-viewer</property>
     <signal name="delete-event" handler="virt_viewer_app_about_delete" swapped="no"/>
     <signal name="response" handler="virt_viewer_app_about_close" swapped="no"/>
     <child internal-child="vbox">
diff --git a/src/virt-viewer-app.c b/src/virt-viewer-app.c
index b51bf4f..660acef 100644
--- a/src/virt-viewer-app.c
+++ b/src/virt-viewer-app.c
@@ -49,6 +49,7 @@
 #endif
 
 #include "virt-viewer-app.h"
+#include "virt-viewer-resources.h"
 #include "virt-viewer-auth.h"
 #include "virt-viewer-window.h"
 #include "virt-viewer-session.h"
@@ -112,6 +113,7 @@ struct _VirtViewerAppPrivate {
     gchar *clipboard;
     GtkWidget *preferences;
     GtkFileChooser *preferences_shared_folder;
+    GResource *resource;
     gboolean direct;
     gboolean verbose;
     gboolean enable_accel;
@@ -1714,6 +1716,7 @@ virt_viewer_app_dispose (GObject *object)
         g_hash_table_unref(tmp);
     }
 
+    priv->resource = NULL;
     g_clear_object(&priv->session);
     g_free(priv->title);
     priv->title = NULL;
@@ -1860,6 +1863,8 @@ virt_viewer_app_on_application_startup(GApplication *app)
 
     G_APPLICATION_CLASS(virt_viewer_app_parent_class)->startup(app);
 
+    self->priv->resource = virt_viewer_get_resource();
+
     virt_viewer_app_set_debug(opt_debug);
     virt_viewer_app_set_fullscreen(self, opt_fullscreen);
 
diff --git a/src/virt-viewer-util.c b/src/virt-viewer-util.c
index 76b61a1..9a4b7c9 100644
--- a/src/virt-viewer-util.c
+++ b/src/virt-viewer-util.c
@@ -49,53 +49,15 @@ virt_viewer_error_quark(void)
 
 GtkBuilder *virt_viewer_util_load_ui(const char *name)
 {
-    struct stat sb;
     GtkBuilder *builder;
-    GError *error = NULL;
-
-    builder = gtk_builder_new();
-    if (stat(name, &sb) >= 0) {
-        gtk_builder_add_from_file(builder, name, &error);
-    } else {
-        gchar *path = g_build_filename(PACKAGE_DATADIR, "ui", name, NULL);
-        gboolean success = (gtk_builder_add_from_file(builder, path, &error) != 0);
-        if (error) {
-            if (!(error->domain == G_FILE_ERROR && error->code == G_FILE_ERROR_NOENT))
-                g_warning("Failed to add ui file '%s': %s", path, error->message);
-            g_clear_error(&error);
-        }
-        g_free(path);
-
-        if (!success) {
-            const gchar * const * dirs = g_get_system_data_dirs();
-            g_return_val_if_fail(dirs != NULL, NULL);
-
-            while (dirs[0] != NULL) {
-                path = g_build_filename(dirs[0], PACKAGE, "ui", name, NULL);
-                if (gtk_builder_add_from_file(builder, path, NULL) != 0) {
-                    g_free(path);
-                    break;
-                }
-                g_free(path);
-                dirs++;
-            }
-            if (dirs[0] == NULL)
-                goto failed;
-        }
-    }
+    gchar *resource = g_strdup_printf("%s/%s",
+                                      VIRT_VIEWER_RESOURCE_PREFIX,
+                                      name);
 
-    if (error) {
-        g_error("Cannot load UI description %s: %s", name,
-                error->message);
-        g_clear_error(&error);
-        goto failed;
-    }
+    builder = gtk_builder_new_from_resource(resource);
 
+    g_free(resource);
     return builder;
- failed:
-    g_error("failed to find UI description file");
-    g_object_unref(builder);
-    return NULL;
 }
 
 int
diff --git a/src/virt-viewer-util.h b/src/virt-viewer-util.h
index 0a7dd97..c453804 100644
--- a/src/virt-viewer-util.h
+++ b/src/virt-viewer-util.h
@@ -34,6 +34,7 @@ enum {
 };
 
 #define VIRT_VIEWER_ERROR virt_viewer_error_quark ()
+#define VIRT_VIEWER_RESOURCE_PREFIX "/org/virt-manager/virt-viewer"
 
 GQuark virt_viewer_error_quark(void);
 
diff --git a/src/virt-viewer-window.c b/src/virt-viewer-window.c
index 8ce34ca..ef62d9a 100644
--- a/src/virt-viewer-window.c
+++ b/src/virt-viewer-window.c
@@ -1030,11 +1030,24 @@ G_MODULE_EXPORT void
 virt_viewer_window_menu_help_about(GtkWidget *menu G_GNUC_UNUSED,
                                    VirtViewerWindow *self)
 {
-    GtkBuilder *about = virt_viewer_util_load_ui("virt-viewer-about.xml");
+    GtkBuilder *about;
+    GtkWidget *dialog;
+    GdkPixbuf *icon;
+
+    about = virt_viewer_util_load_ui("virt-viewer-about.xml");
+
+    dialog = GTK_WIDGET(gtk_builder_get_object(about, "about"));
 
-    GtkWidget *dialog = GTK_WIDGET(gtk_builder_get_object(about, "about"));
     gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(dialog), VERSION BUILDID);
 
+    icon = gdk_pixbuf_new_from_resource(VIRT_VIEWER_RESOURCE_PREFIX"/icons/48x48/virt-viewer.png", NULL);
+    if (icon != NULL) {
+        gtk_about_dialog_set_logo(GTK_ABOUT_DIALOG(dialog), icon);
+        g_object_unref(icon);
+    } else {
+        gtk_about_dialog_set_logo_icon_name(GTK_ABOUT_DIALOG(dialog), "virt-viewer");
+    }
+
     gtk_window_set_transient_for(GTK_WINDOW(dialog),
                                  GTK_WINDOW(self->priv->window));
 
@@ -1066,8 +1079,7 @@ virt_viewer_window_toolbar_setup(VirtViewerWindow *self)
     g_signal_connect(button, "clicked", G_CALLBACK(virt_viewer_window_menu_file_quit), self);
 
     /* USB Device selection */
-    button = gtk_image_new_from_icon_name("virt-viewer-usb",
-                                          GTK_ICON_SIZE_INVALID);
+    button = gtk_image_new_from_resource(VIRT_VIEWER_RESOURCE_PREFIX"/icons/24x24/virt-viewer-usb.png");
     button = GTK_WIDGET(gtk_tool_button_new(button, NULL));
     gtk_tool_button_set_label(GTK_TOOL_BUTTON(button), _("USB device selection"));
     gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(button), _("USB device selection"));
diff --git a/src/virt-viewer.gresource.xml b/src/virt-viewer.gresource.xml
new file mode 100644
index 0000000..596889a
--- /dev/null
+++ b/src/virt-viewer.gresource.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/org/virt-manager/virt-viewer">
+    <file>remote-viewer-connect.xml</file>
+    <file>virt-viewer-about.xml</file>
+    <file>virt-viewer-auth.xml</file>
+    <file>virt-viewer-guest-details.xml</file>
+    <file>virt-viewer-preferences.xml</file>
+    <file>virt-viewer-vm-connection.xml</file>
+    <file>virt-viewer.xml</file>
+    <file alias="icons/16x16/virt-viewer.png">../icons/16x16/virt-viewer.png</file>
+    <file alias="icons/22x22/virt-viewer.png">../icons/22x22/virt-viewer.png</file>
+    <file alias="icons/24x24/virt-viewer.png">../icons/24x24/virt-viewer.png</file>
+    <file alias="icons/24x24/virt-viewer-usb.png">../icons/24x24/virt-viewer-usb.png</file>
+    <file alias="icons/32x32/virt-viewer.png">../icons/32x32/virt-viewer.png</file>
+    <file alias="icons/48x48/virt-viewer.png">../icons/48x48/virt-viewer.png</file>
+    <file alias="icons/256x256/virt-viewer.png">../icons/256x256/virt-viewer.png</file>
+  </gresource>
+</gresources>
diff --git a/virt-viewer.spec.in b/virt-viewer.spec.in
index 274e7e7..3c483ea 100644
--- a/virt-viewer.spec.in
+++ b/virt-viewer.spec.in
@@ -122,15 +122,6 @@ fi
 %doc README COPYING AUTHORS ChangeLog NEWS
 %{_bindir}/%{name}
 %{_bindir}/remote-viewer
-%dir %{_datadir}/%{name}
-%dir %{_datadir}/%{name}/ui/
-%{_datadir}/%{name}/ui/virt-viewer.xml
-%{_datadir}/%{name}/ui/virt-viewer-auth.xml
-%{_datadir}/%{name}/ui/virt-viewer-about.xml
-%{_datadir}/%{name}/ui/virt-viewer-guest-details.xml
-%{_datadir}/%{name}/ui/virt-viewer-vm-connection.xml
-%{_datadir}/%{name}/ui/virt-viewer-preferences.xml
-%{_datadir}/%{name}/ui/remote-viewer-connect.xml
 %{_datadir}/icons/hicolor/*/apps/*
 %{_datadir}/icons/hicolor/*/devices/*
 %{_datadir}/applications/remote-viewer.desktop
-- 
2.5.0




More information about the virt-tools-list mailing list