[balsa] Use GResource instead of installed files



commit 3daecc95d18466b6fcc7b684296446823d2a0685
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Thu May 10 20:20:40 2018 -0400

    Use GResource instead of installed files
    
    Current Balsa uses installed ui/*.ui files to define the user interfaces
    for the main window, the message window, the compose window, and the
    view-source window. Those files can be replaced by GResource objects,
    which are part of the binary file, so that accessing them requires no
    additional file system access.
    
    This commit:
    • changes the C code to use GResources instead of installed files;
    • changes various meson.build files to generate the necessary C files
      instead of installing XML ui files (not difficult);
    • changes configure.ac and various Makefile.am files for the same
      purpose (far more difficult, for me!).

 ChangeLog                      |    7 ++++++
 configure.ac                   |    4 +++
 libbalsa/application-helpers.c |   20 +++++++++---------
 libbalsa/application-helpers.h |    2 +-
 libbalsa/source-viewer.c       |   10 ++------
 meson.build                    |   11 +++++++++-
 src/Makefile.am                |   41 +++++++++++++++++++++++++++++++--------
 src/ab-main.c                  |    7 +----
 src/main-window.c              |    9 ++-----
 src/meson.build                |    4 +-
 src/message-window.c           |   10 ++------
 src/sendmsg-window.c           |    7 +----
 ui/Makefile.am                 |    3 --
 ui/balsa-ab.gresource.xml      |    6 +++++
 ui/balsa.gresource.xml         |    9 ++++++++
 ui/meson.build                 |   12 -----------
 16 files changed, 94 insertions(+), 68 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 11da7d7..63847e1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2018-05-10  Peter Bloomfield  <pbloomfield bellsouth net>
+
+       New files for migrating to GResource
+
+       * ui/balsa-ab.gresource.xml:
+       * ui/balsa.gresource.xml:
+
 2018-05-01  Albrecht Dreß <albrecht dress arcor de>
 
        Display time stamps of date-only RFC 5545 iCalendar events
diff --git a/configure.ac b/configure.ac
index b036898..83adcce 100644
--- a/configure.ac
+++ b/configure.ac
@@ -167,6 +167,10 @@ AC_CHECK_MEMBERS([struct utsname.domainname],[],[],[[#include<sys/utsname.h>]])
 #
 AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
 
+# glib-compile-resources
+#
+AC_PATH_PROG([GLIB_COMPILE_RESOURCES], [glib-compile-resources])
+
 # yelp
 #
 AC_MSG_CHECKING([for Yelp tool])
diff --git a/libbalsa/application-helpers.c b/libbalsa/application-helpers.c
index 619ed35..8c65007 100644
--- a/libbalsa/application-helpers.c
+++ b/libbalsa/application-helpers.c
@@ -145,22 +145,22 @@ get_accel_group(GMenuModel * model,
  * Construct a menu-bar for a GtkApplicationWindow that does not use the
  * GApplication's menubar
  *
- * window       the GtkApplicationWindow
- * entries      array of GActionEntry structures
- * n_entries    length of the array
- * ui_file      filename for GtkBuilder input defining a menu named
- *              "menubar"
- * error        GError for returning error information
- * cb_data      user data for GAction callbacks
+ * window        the GtkApplicationWindow
+ * entries       array of GActionEntry structures
+ * n_entries     length of the array
+ * resource_path resource path for GtkBuilder input defining a menu named
+ *               "menubar"
+ * error         GError for returning error information
+ * cb_data       user data for GAction callbacks
  *
- * returns:     the GtkMenuBar
+ * returns:      the GtkMenuBar
  */
 
 GtkWidget *
 libbalsa_window_get_menu_bar(GtkApplicationWindow * window,
                              const GActionEntry   * entries,
                              gint                   n_entries,
-                             const gchar          * ui_file,
+                             const gchar          * resource_path,
                              GError              ** error,
                              gpointer               cb_data)
 {
@@ -171,7 +171,7 @@ libbalsa_window_get_menu_bar(GtkApplicationWindow * window,
     g_action_map_add_action_entries(map, entries, n_entries, cb_data);
 
     builder = gtk_builder_new();
-    if (gtk_builder_add_from_file(builder, ui_file, error)) {
+    if (gtk_builder_add_from_resource(builder, resource_path, error)) {
         GMenuModel *menu_model;
 
         menu_model =
diff --git a/libbalsa/application-helpers.h b/libbalsa/application-helpers.h
index a460a0a..642213d 100644
--- a/libbalsa/application-helpers.h
+++ b/libbalsa/application-helpers.h
@@ -34,7 +34,7 @@
 GtkWidget *libbalsa_window_get_menu_bar(GtkApplicationWindow * window,
                                         const GActionEntry   * entries,
                                         gint                   n_entries,
-                                        const gchar          * ui_file,
+                                        const gchar          * resource_path,
                                         GError              ** error,
                                         gpointer               cb_data);
 
diff --git a/libbalsa/source-viewer.c b/libbalsa/source-viewer.c
index 208005f..77dd848 100644
--- a/libbalsa/source-viewer.c
+++ b/libbalsa/source-viewer.c
@@ -202,7 +202,7 @@ libbalsa_show_message_source(GtkApplication  * application,
     GtkCssProvider *css_provider;
     GtkWidget *vbox, *interior;
     GtkWidget *window;
-    gchar *ui_file;
+    const gchar resource_path[] = "/org/desktop/Balsa/source-viewer.ui";
     GtkWidget *menu_bar;
     GError *err = NULL;
     LibBalsaSourceViewerInfo *lsvi;
@@ -239,21 +239,17 @@ libbalsa_show_message_source(GtkApplication  * application,
     gtk_window_set_role(GTK_WINDOW(window), "message-source");
     gtk_window_set_default_size(GTK_WINDOW(window), *width, *height);
 
-    ui_file = g_build_filename(BALSA_DATA_PREFIX, "ui", "source-viewer.ui",
-                               NULL);
     menu_bar = libbalsa_window_get_menu_bar(GTK_APPLICATION_WINDOW(window),
                                             win_entries,
                                             G_N_ELEMENTS(win_entries),
-                                            ui_file, &err, window);
+                                            resource_path, &err, window);
     if (!menu_bar) {
         libbalsa_information(LIBBALSA_INFORMATION_WARNING,
-                             _("Error adding from %s: %s\n"), ui_file,
+                             _("Error adding from %s: %s\n"), resource_path,
                              err->message);
-        g_free(ui_file);
         g_error_free(err);
         return;
     }
-    g_free(ui_file);
 
     vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 1);
 #if HAVE_MACOSX_DESKTOP
diff --git a/meson.build b/meson.build
index bba96e5..33998aa 100644
--- a/meson.build
+++ b/meson.build
@@ -753,13 +753,22 @@ src_include           = include_directories('src')
 
 gnome = import('gnome')
 
+balsa_resources = gnome.compile_resources('balsa-resources',
+                                          'ui/balsa.gresource.xml',
+                                          source_dir : 'ui',
+                                          c_name     : 'balsa')
+
+balsa_ab_resources = gnome.compile_resources('balsa-ab-resources',
+                                             'ui/balsa-ab.gresource.xml',
+                                             source_dir : 'ui',
+                                             c_name     : 'balsa_ab')
+
 subdir('sounds')
 subdir('images')
 subdir('libnetclient')
 subdir('libbalsa')
 subdir('libinit_balsa')
 subdir('src')
-subdir('ui')
 if help_files
   subdir('doc')
 endif
diff --git a/src/Makefile.am b/src/Makefile.am
index d1ebb77..3875baa 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -99,13 +99,26 @@ balsa_spell_extra =         \
                spell-check.h
 endif
 
+resource_xml = $(top_srcdir)/ui/balsa.gresource.xml
+
+balsa-resources.c: $(resource_xml) $(shell $(GLIB_COMPILE_RESOURCES) --sourcedir=$(top_srcdir)/ui 
--generate-dependencies $(resource_xml))
+       $(AM_V_GEN)                             \
+       $(GLIB_COMPILE_RESOURCES)               \
+               --sourcedir=$(top_srcdir)/ui    \
+               --target=$@                     \
+               --generate-source               \
+               --c-name=balsa                  \
+               $(resource_xml)
+
+balsa_built_sources =          \
+       balsa-resources.c
+
 balsa_SOURCES =                        \
        $(balsa_BASE_SRCLIST)   \
        $(balsa_spell_extra) \
-       $(balsa_print_source)
-
+       $(balsa_print_source)   \
+       $(balsa_built_sources)
 
-DISTCLEANFILES = $(balsa_IDL_SRCLIST)
 
 EXTRA_DIST = $(balsa_spell_extra_dist)
 
@@ -123,15 +136,29 @@ balsa_LDADD = \
        $(top_builddir)/libbalsa/libbalsa.a     \
        $(top_builddir)/libbalsa/imap/libimap.a \
        $(top_builddir)/libnetclient/libnetclient.a \
-       $(balsa_IDL_OBJS) \
        $(INTLLIBS) \
         $(BALSA_LIBS)
 
 
+ab_resource_xml = $(top_srcdir)/ui/balsa-ab.gresource.xml
+
+balsa-ab-resources.c: $(ab_resource_xml) $(shell $(GLIB_COMPILE_RESOURCES) --sourcedir=$(top_srcdir)/ui 
--generate-dependencies $(ab_resource_xml))
+       $(AM_V_GEN)                             \
+       $(GLIB_COMPILE_RESOURCES)               \
+               --sourcedir=$(top_srcdir)/ui    \
+               --target=$@                     \
+               --generate-source               \
+               --c-name=balsa_ab               \
+               $(ab_resource_xml)
+
+balsa_ab_built_sources =               \
+       balsa-ab-resources.c
+
 
 balsa_ab_SOURCES =     ab-main.c               \
                        address-book-config.c   \
-                       address-book-config.h   
+                       address-book-config.h   \
+                       balsa-ab-resources.c
 
 balsa_ab_LDADD = \
        $(top_builddir)/libbalsa/libbalsa.a     \
@@ -140,7 +167,3 @@ balsa_ab_LDADD = \
 dist-hook:
        mkdir $(distdir)/pixmaps
        cp $(srcdir)/pixmaps/*.{xpm,png} $(distdir)/pixmaps
-
-$(balsa_IDL_SRCLIST): $(srcdir)/$(idl_DATA) 
-       $(ORBIT_IDL) -I $(LIBBONOBO_IDL) -I $(BONOBO_ACTIVATION_IDL) \
-       $(srcdir)/$(idl_DATA)
diff --git a/src/ab-main.c b/src/ab-main.c
index a7814d6..5401e45 100644
--- a/src/ab-main.c
+++ b/src/ab-main.c
@@ -580,13 +580,11 @@ get_main_menu(GtkApplication * application)
             address_book_change_state},
     };
     GtkBuilder *builder;
-    gchar *ui_file;
+    const gchar resource_path[] = "/org/desktop/BalsaAb/ab-main.ui";
     GError *err = NULL;
 
     builder = gtk_builder_new();
-    ui_file = g_build_filename(BALSA_DATA_PREFIX, "ui", "ab-main.ui",
-                               NULL);
-    if (gtk_builder_add_from_file(builder, ui_file, &err)) {
+    if (gtk_builder_add_from_resource(builder, resource_path, &err)) {
         gtk_application_set_menubar(application,
                                     G_MENU_MODEL(gtk_builder_get_object
                                                  (builder, "menubar")));
@@ -596,7 +594,6 @@ get_main_menu(GtkApplication * application)
         g_print("%s error: %s\n", __func__, err->message);
         g_error_free(err);
     }
-    g_free(ui_file);
     g_object_unref(builder);
 
     g_action_map_add_action_entries(G_ACTION_MAP(contacts_app.window),
diff --git a/src/main-window.c b/src/main-window.c
index 19e0ce7..272be3b 100644
--- a/src/main-window.c
+++ b/src/main-window.c
@@ -2049,16 +2049,14 @@ static void
 bw_set_menus(BalsaWindow * window)
 {
     GtkBuilder *builder;
-    gchar *ui_file;
+    const gchar resource_path[] = "/org/desktop/Balsa/main-window.ui";
     GError *err = NULL;
 
     bw_add_app_action_entries(G_ACTION_MAP(balsa_app.application), window);
     bw_add_win_action_entries(G_ACTION_MAP(window));
 
     builder = gtk_builder_new();
-    ui_file = g_build_filename(BALSA_DATA_PREFIX, "ui", "main-window.ui",
-                               NULL);
-    if (gtk_builder_add_from_file(builder, ui_file, &err)) {
+    if (gtk_builder_add_from_resource(builder, resource_path, &err)) {
         gtk_application_set_app_menu(balsa_app.application,
                                      G_MENU_MODEL(gtk_builder_get_object
                                                   (builder, "app-menu")));
@@ -2068,11 +2066,10 @@ bw_set_menus(BalsaWindow * window)
     } else {
         g_print("%s error: %s\n", __func__, err->message);
         balsa_information(LIBBALSA_INFORMATION_WARNING,
-                          _("Error adding from %s: %s\n"), ui_file,
+                          _("Error adding from %s: %s\n"), resource_path,
                           err->message);
         g_error_free(err);
     }
-    g_free(ui_file);
     g_object_unref(builder);
 }
 
diff --git a/src/meson.build b/src/meson.build
index b9f72bd..5b2dba9 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -103,7 +103,7 @@ balsa_compile_arg = '-DGNOMELOCALEDIR="' + locale_dir + '"'
 balsa_compile_dep = declare_dependency(compile_args : balsa_compile_arg)
 balsa_libs = [libinit_balsa_a, libbalsa_a, libimap_a, libnetclient_a]
 
-executable('balsa', balsa_sources,
+executable('balsa', balsa_sources, balsa_resources,
            dependencies        : balsa_deps + [balsa_compile_dep],
            include_directories : [top_include, libbalsa_include, libnetclient_include],
            link_with           : balsa_libs,
@@ -114,7 +114,7 @@ balsa_ab_sources = [
   'address-book-config.c'
   ]
 
-executable('balsa_ab', balsa_ab_sources,
+executable('balsa_ab', balsa_ab_sources, balsa_ab_resources,
            dependencies        : balsa_ab_deps,
            include_directories : [top_include, libbalsa_include],
            link_with           : libbalsa_a,
diff --git a/src/message-window.c b/src/message-window.c
index 5dba3dd..d236237 100644
--- a/src/message-window.c
+++ b/src/message-window.c
@@ -809,7 +809,7 @@ message_window_new(LibBalsaMailbox * mailbox, guint msgno)
     GtkWidget *vbox;
     static const gchar *const header_options[] =
         { "none", "selected", "all" };
-    gchar *ui_file;
+    const gchar resource_path[] = "/org/desktop/Balsa/message-window.ui";
     GAction *action;
 
     if (!mailbox || !msgno)
@@ -833,21 +833,17 @@ message_window_new(LibBalsaMailbox * mailbox, guint msgno)
     gtk_container_add(GTK_CONTAINER(window), vbox);
 
     /* Set up the GMenu structures */
-    ui_file = g_build_filename(BALSA_DATA_PREFIX, "ui",
-                               "message-window.ui", NULL);
     menubar = libbalsa_window_get_menu_bar(GTK_APPLICATION_WINDOW(window),
                                            win_entries,
                                            G_N_ELEMENTS(win_entries),
-                                           ui_file, &error, mw);
+                                           resource_path, &error, mw);
     if (!menubar) {
         libbalsa_information(LIBBALSA_INFORMATION_WARNING,
-                             _("Error adding from %s: %s\n"), ui_file,
+                             _("Error adding from %s: %s\n"), resource_path,
                              error->message);
-        g_free(ui_file);
         g_error_free(error);
         return;
     }
-    g_free(ui_file);
     gtk_widget_show(menubar);
 #if HAVE_MACOSX_DESKTOP
     libbalsa_macosx_menu(window, GTK_MENU_SHELL(menubar));
diff --git a/src/sendmsg-window.c b/src/sendmsg-window.c
index 30eef2b..ff62d86 100644
--- a/src/sendmsg-window.c
+++ b/src/sendmsg-window.c
@@ -6577,7 +6577,7 @@ sendmsg_window_new()
     GError *error = NULL;
     GtkWidget *menubar;
     GtkWidget *paned;
-    gchar *ui_file;
+    const gchar resource_path[] = "/org/desktop/Balsa/sendmsg-window.ui";
     const gchar *current_locale;
 
     bsmsg = g_malloc(sizeof(BalsaSendmsg));
@@ -6634,13 +6634,10 @@ sendmsg_window_new()
                       (GWeakNotify) gtk_widget_destroy, window);
 
     /* Set up the GMenu structures */
-    ui_file = g_build_filename(BALSA_DATA_PREFIX, "ui",
-                               "sendmsg-window.ui", NULL);
     menubar = libbalsa_window_get_menu_bar(GTK_APPLICATION_WINDOW(window),
                                            win_entries,
                                            G_N_ELEMENTS(win_entries),
-                                           ui_file, &error, bsmsg);
-    g_free(ui_file);
+                                           resource_path, &error, bsmsg);
     if (error) {
         g_print("%s %s\n", __func__, error->message);
         g_error_free(error);
diff --git a/ui/Makefile.am b/ui/Makefile.am
index 07803cf..09b1d60 100644
--- a/ui/Makefile.am
+++ b/ui/Makefile.am
@@ -6,6 +6,3 @@ balsa_ui_FILES =                   \
                 ab-main.ui
 
 EXTRA_DIST = $(balsa_ui_FILES)
-
-balsa_uidir = $(BALSA_DATA_PREFIX)/ui
-balsa_ui_DATA = $(balsa_ui_FILES)
diff --git a/ui/balsa-ab.gresource.xml b/ui/balsa-ab.gresource.xml
new file mode 100644
index 0000000..1b32287
--- /dev/null
+++ b/ui/balsa-ab.gresource.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/org/desktop/Balsa">
+    <file compressed="true">ab-main.ui</file>
+  </gresource>
+</gresources>
diff --git a/ui/balsa.gresource.xml b/ui/balsa.gresource.xml
new file mode 100644
index 0000000..fa53aed
--- /dev/null
+++ b/ui/balsa.gresource.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/org/desktop/Balsa">
+    <file compressed="true">main-window.ui</file>
+    <file compressed="true">message-window.ui</file>
+    <file compressed="true">sendmsg-window.ui</file>
+    <file compressed="true">source-viewer.ui</file>
+  </gresource>
+</gresources>


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