Skip to content

Commit

Permalink
Use GResource to load GtkBuilder XML UI file
Browse files Browse the repository at this point in the history
This prevents Geany from failing to run when it can't find the
Glade/GtkBuilder UI for whatever reason.
  • Loading branch information
codebrainz committed Dec 2, 2017
1 parent ca16aff commit e64ef78
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Makefile.in
#-----------------------------------------------------------------------
/src/geany
/src/geany_private.res
/src/resources.[ch]
/src/signallist.i

#-----------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ GEANY_CHECK_PLUGINS
# check for mingw specific settings
GEANY_CHECK_MINGW

GEANY_CHECK_GRESOURCE
GEANY_CHECK_SOCKET
GEANY_CHECK_VTE
GEANY_CHECK_MAC_INTEGRATION
Expand Down
5 changes: 3 additions & 2 deletions data/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ nobase_dist_pkgdata_DATA = \
$(templates) \
filetype_extensions.conf \
snippets.conf \
ui_toolbar.xml \
geany.glade
ui_toolbar.xml

EXTRA_DIST = geany.glade

if GTK3
nobase_dist_pkgdata_DATA += \
Expand Down
7 changes: 7 additions & 0 deletions m4/geany-gresource.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
AC_DEFUN([GEANY_CHECK_GRESOURCE],
[
AC_PATH_PROG([GLIB_COMPILE_RESOURCES], [glib-compile-resources], [no])
AS_IF([test "x$GLIB_COMPILE_RESOURCES" = "xno"], [
AC_MSG_ERROR([Unable to find glib-compile-resources, is it installed?])
])
])
27 changes: 26 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ EXTRA_DIST = \
filetypesprivate.h \
keybindingsprivate.h \
pluginprivate.h \
projectprivate.h
projectprivate.h \
geany.gresource.xml

AM_CPPFLAGS = \
-I$(top_srcdir) \
Expand Down Expand Up @@ -93,6 +94,7 @@ libgeany_la_SOURCES = \
prefs.c prefs.h \
printing.c printing.h \
project.c project.h \
resources.c resources.h \
sciwrappers.c sciwrappers.h \
search.c search.h \
socket.c socket.h \
Expand Down Expand Up @@ -184,6 +186,29 @@ signallist.i: $(glade_file) Makefile

CLEANFILES += signallist.i

# embed files using gresource
resource_dependencies = geany.gresource.xml $(top_srcdir)/data/geany.glade
resources.c: $(resource_dependencies)
$(AM_V_GEN)$(GLIB_COMPILE_RESOURCES) \
--target=$@ \
--sourcedir=$(srcdir) \
--generate-source \
--c-name="geany" \
--manual-register \
--internal \
$(srcdir)/geany.gresource.xml
resources.h: $(resource_dependencies)
$(AM_V_GEN)$(GLIB_COMPILE_RESOURCES) \
--target=$@ \
--sourcedir=$(srcdir) \
--generate-header \
--c-name="geany" \
--manual-register \
--internal \
$(srcdir)/geany.gresource.xml
CLEANFILES += resources.c resources.h
BUILT_SOURCES = resources.c resources.h

# install the run script
if MINGW
dist_pkglibexec_SCRIPTS = geany-run-helper.bat
Expand Down
6 changes: 6 additions & 0 deletions src/geany.gresource.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/org/geany/Geany">
<file alias="geany.glade" compressed="true">../data/geany.glade</file>
</gresource>
</gresources>
3 changes: 3 additions & 0 deletions src/libmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "plugins.h"
#include "prefs.h"
#include "printing.h"
#include "resources.h"
#include "sidebar.h"
#ifdef HAVE_SOCKET
# include "socket.h"
Expand Down Expand Up @@ -1046,6 +1047,7 @@ gint main_lib(gint argc, gchar **argv)
g_type_init();
#endif

geany_register_resource();
log_handlers_init();

app = g_new0(GeanyApp, 1);
Expand Down Expand Up @@ -1362,6 +1364,7 @@ static void do_main_quit(void)
g_free(app);

ui_finalize_builder();
geany_unregister_resource();

gtk_main_quit();
}
Expand Down
27 changes: 20 additions & 7 deletions src/ui_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "msgwindow.h"
#include "prefs.h"
#include "project.h"
#include "resources.h"
#include "sciwrappers.h"
#include "sidebar.h"
#include "stash.h"
Expand Down Expand Up @@ -2425,8 +2426,8 @@ static GtkWidget *ui_get_top_parent(GtkWidget *widget)

void ui_init_builder(void)
{
gchar *interface_file;
const gchar *name;
GBytes *ui_data;
GError *error;
GSList *iter, *all_objects;
GtkWidget *widget, *toplevel;
Expand All @@ -2440,20 +2441,32 @@ void ui_init_builder(void)
gtk_builder_set_translation_domain(builder, GETTEXT_PACKAGE);

error = NULL;
interface_file = g_build_filename(app->datadir, "geany.glade", NULL);
if (! gtk_builder_add_from_file(builder, interface_file, &error))
ui_data = g_resource_lookup_data(geany_get_resource(),
"/org/geany/Geany/geany.glade", G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
if (ui_data == NULL)
{
dialogs_show_msgbox_with_secondary(GTK_MESSAGE_ERROR,
_("Geany cannot start!"), error->message);
g_error("Cannot load user-interface: %s", error->message);
g_error_free(error);
g_object_unref(builder);
return;
}

error = NULL;
if (gtk_builder_add_from_string(builder, g_bytes_get_data(ui_data, NULL),
g_bytes_get_size(ui_data), &error) == 0)
{
/* Show the user this message so they know WTF happened */
dialogs_show_msgbox_with_secondary(GTK_MESSAGE_ERROR,
_("Geany cannot start!"), error->message);
/* Aborts */
g_error("Cannot create user-interface: %s", error->message);
g_error_free(error);
g_free(interface_file);
g_bytes_unref(ui_data);
g_object_unref(builder);
return;
}
g_free(interface_file);

g_bytes_unref(ui_data);

callbacks_connect(builder);

Expand Down

0 comments on commit e64ef78

Please sign in to comment.