From 72d92d2f7d1bcbf34440c0bc47fa9f8912b7d1cc Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Wed, 4 Nov 2015 20:56:33 +0100 Subject: [PATCH] Added support for modules and l16n. --- configure.ac | 12 +++- docs/notes.txt | 6 +- libzakform.pc.in | 1 + src/Makefile.am | 8 +++ src/form.c | 151 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 173 insertions(+), 5 deletions(-) diff --git a/configure.ac b/configure.ac index 68a06d4..b05d446 100644 --- a/configure.ac +++ b/configure.ac @@ -29,9 +29,19 @@ AC_PROG_MAKE_SET AC_PROG_LIBTOOL AC_PROG_RANLIB +dnl ****************************** +dnl Translations +dnl ****************************** +GETTEXT_PACKAGE=libzakform +AC_SUBST(GETTEXT_PACKAGE) +AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE, "$GETTEXT_PACKAGE", + [The prefix for our gettext translation domains.]) +AM_GLIB_GNU_GETTEXT + # Checks for libraries. PKG_CHECK_MODULES(ZAKFORM, [glib-2.0 >= 2.36 - gobject-2.0 >= 2.36]) + gobject-2.0 >= 2.36 + gmodule-2.0 >= 2.0.0]) AC_SUBST(ZAKFORM_CFLAGS) AC_SUBST(ZAKFORM_LIBS) diff --git a/docs/notes.txt b/docs/notes.txt index 1703604..f111d71 100644 --- a/docs/notes.txt +++ b/docs/notes.txt @@ -14,7 +14,7 @@ ELEMENT - defined as abstract - implement html and gtk - properties: - - default + - default (OK did it) - to-load - to-save - each element must parse own definition from xml @@ -22,7 +22,7 @@ ELEMENT - each element could have specifics properties/methods ELEMENT FILTER -- to be transformed from interface to abstract +- to be transformed from interface to abstract (OK did it) ELEMENT VALIDATOR @@ -34,5 +34,3 @@ FIELD/DB GROUP OF ELEMENTS ??? - - diff --git a/libzakform.pc.in b/libzakform.pc.in index 18a2325..0c0cf06 100644 --- a/libzakform.pc.in +++ b/libzakform.pc.in @@ -2,6 +2,7 @@ prefix=@prefix@ exec_prefix=@exec_prefix@ libdir=@libdir@ includedir=@includedir@ +modulesdir=@libdir@/@PACKAGE@/modules Name: @PACKAGE_NAME@ Description: Class for form. diff --git a/src/Makefile.am b/src/Makefile.am index d300a59..26b354f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,6 +1,8 @@ LIBS = $(ZAKFORM_LIBS) AM_CPPFLAGS = $(ZAKFORM_CFLAGS) \ + -DLOCALEDIR=\"$(localedir)\" \ + -DMODULESDIR=\""$(libdir)/$(PACKAGE)/modules"\" \ -DG_LOG_DOMAIN=\"ZakForm\" lib_LTLIBRARIES = libzakform.la @@ -27,3 +29,9 @@ libzakform_include_HEADERS = \ formelementvalidatorregex.h libzakform_includedir = $(includedir)/libzakform + +install-exec-hook: + mkdir -p "$(libdir)/$(PACKAGE)/modules" + +uninstall-hook: + rm -rf "$(libdir)/$(PACKAGE)" diff --git a/src/form.c b/src/form.c index c66d673..166262b 100644 --- a/src/form.c +++ b/src/form.c @@ -20,8 +20,15 @@ #include #endif +#include +#include + #include "form.h" +#ifdef G_OS_WIN32 + #include +#endif + static void zak_form_form_class_init (ZakFormFormClass *class); static void zak_form_form_init (ZakFormForm *zak_form_form); @@ -37,15 +44,41 @@ static void zak_form_form_get_property (GObject *object, static void zak_form_form_dispose (GObject *gobject); static void zak_form_form_finalize (GObject *gobject); +static void zak_form_form_load_modules (ZakFormForm *zakform); static GPtrArray *zak_form_form_get_elements (ZakFormForm *zakform); typedef struct { + GPtrArray *ar_modules; GPtrArray *ar_elements; } ZakFormFormPrivate; G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ZakFormForm, zak_form_form, G_TYPE_OBJECT) +#ifdef G_OS_WIN32 +static HMODULE backend_dll = NULL; + +BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved); + +BOOL WINAPI +DllMain (HINSTANCE hinstDLL, + DWORD fdwReason, + LPVOID lpReserved) +{ + switch (fdwReason) + { + case DLL_PROCESS_ATTACH: + backend_dll = (HMODULE) hinstDLL; + break; + case DLL_THREAD_ATTACH: + case DLL_THREAD_DETACH: + case DLL_PROCESS_DETACH: + break; + } + return TRUE; +} +#endif + static void zak_form_form_class_init (ZakFormFormClass *class) { @@ -62,9 +95,45 @@ zak_form_form_class_init (ZakFormFormClass *class) static void zak_form_form_init (ZakFormForm *zak_form_form) { + gchar *localedir; + ZakFormFormPrivate *priv = zak_form_form_get_instance_private (zak_form_form); + priv->ar_modules = NULL; priv->ar_elements = g_ptr_array_new (); + +#ifdef G_OS_WIN32 + + gchar *moddir; + gchar *p; + + moddir = g_win32_get_package_installation_directory_of_module (backend_dll); + + p = g_strrstr (moddir, g_strdup_printf ("%c", G_DIR_SEPARATOR)); + if (p != NULL + && (g_ascii_strcasecmp (p + 1, "src") == 0 + || g_ascii_strcasecmp (p + 1, ".libs") == 0)) + { + localedir = g_strdup (LOCALEDIR); + } + else + { + localedir = g_build_filename (moddir, "share", "locale", NULL); + } + + g_free (moddir); + +#else + + localedir = g_strdup (LOCALEDIR); + +#endif + + bindtextdomain (GETTEXT_PACKAGE, localedir); + textdomain (GETTEXT_PACKAGE); + bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); + + zak_form_form_load_modules (zak_form_form); } /** @@ -204,6 +273,88 @@ zak_form_form_finalize (GObject *gobject) parent_class->finalize (gobject); } +static void +zak_form_form_load_modules (ZakFormForm* zakform) +{ + ZakFormFormPrivate *priv; + + gchar *modulesdir; + GDir *dir; + GError *error; + + GModule *module; + const gchar *filename; + + if (g_module_supported ()) + { + priv = zak_form_form_get_instance_private (zakform); + + modulesdir = (gchar *)g_getenv ("LIBZAKFORM_MODULESDIR"); + if (modulesdir == NULL) + { +#ifdef G_OS_WIN32 + + gchar *moddir; + gchar *p; + + moddir = g_win32_get_package_installation_directory_of_module (backend_dll); + + p = g_strrstr (moddir, g_strdup_printf ("%c", G_DIR_SEPARATOR)); + if (p != NULL + && (g_ascii_strcasecmp (p + 1, "src") == 0 + || g_ascii_strcasecmp (p + 1, ".libs") == 0)) + { + modulesdir = g_strdup (MODULESDIR); + } + else + { + modulesdir = g_build_filename (moddir, "lib", PACKAGE, "modules", NULL); + } + +#else + + modulesdir = g_strdup (MODULESDIR); + +#endif + } + + /* for each file in MODULESDIR */ + error = NULL; + dir = g_dir_open (modulesdir, 0, &error); + if (dir != NULL && error == NULL) + { + while ((filename = g_dir_read_name (dir)) != NULL) + { + /* trying to open the module */ + module = g_module_open (filename, G_MODULE_BIND_LAZY); + if (module != NULL) + { + if (priv->ar_modules == NULL) + { + priv->ar_modules = g_ptr_array_new (); + } + g_ptr_array_add (priv->ar_modules, (gpointer)module); + } + else + { + g_warning (_("Unable to load %s: %s."), filename, g_module_error ()); + } + } + + g_dir_close (dir); + } + else + { + g_warning (_("Unable to open modules dir: %s."), + error != NULL && error->message != NULL ? error->message : _("no details")); + } + } + else + { + g_warning (_("Modules not supported by this operating system.")); + } +} + static GPtrArray *zak_form_form_get_elements (ZakFormForm *zakform) { -- 2.49.0