Added support for modules and l16n.
authorAndrea Zagli <azagli@libero.it>
Wed, 4 Nov 2015 19:56:33 +0000 (20:56 +0100)
committerAndrea Zagli <azagli@libero.it>
Wed, 4 Nov 2015 19:56:33 +0000 (20:56 +0100)
configure.ac
docs/notes.txt
libzakform.pc.in
src/Makefile.am
src/form.c

index 68a06d43e090fd488aaf2aaf555cba68ce6246d8..b05d4461fa2f28fccb14798258c601a1f6d11dae 100644 (file)
@@ -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)
index 1703604129fc9be0fbe2c0df3154fda4c9826772..f111d71dddc399a6d395141ba91962d135c2e4fc 100644 (file)
@@ -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 ???
-
-
index 18a2325f3ca3dac5fedd58e3df8cffb69cb22622..0c0cf06b44dddc5da91d2f7fab635ebeb6c69a9a 100644 (file)
@@ -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.
index d300a591d8f6a716b0ab73685cd2c67a2172d9c7..26b354fcaa104d5c3b1ec68731404f319d2ad3ab 100644 (file)
@@ -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)"
index c66d673b498ecc93d24cccdf1eaeb33ad31fe21c..166262b44417c5473f6f003e3ace434f2bf89ea1 100644 (file)
        #include <config.h>
 #endif
 
+#include <glib/gi18n-lib.h>
+#include <gmodule.h>
+
 #include "form.h"
 
+#ifdef G_OS_WIN32
+       #include <windows.h>
+#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)
 {