]> saetta.ns0.it Git - zakform/cgi/commitdiff
Added validator for size and mime type for ZakFormCgiFormElementFile. gvalue
authorAndrea Zagli <azagli@libero.it>
Sat, 13 May 2017 14:19:44 +0000 (16:19 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 13 May 2017 14:19:44 +0000 (16:19 +0200)
Makefile.am
configure.ac
po/LINGUAS [new file with mode: 0644]
po/POTFILES.in [new file with mode: 0644]
po/it.po [new file with mode: 0644]
src/Makefile.am
src/form.c
src/formelementfile.c
src/formelementfile.h

index 7f0923e1ef8a5a696a5569833c86bb33cc7ae2b8..0f07bdfa71aa6bfda1c748f6a8ea95fb2591e13f 100644 (file)
@@ -1,4 +1,4 @@
-SUBDIRS = src tests
+SUBDIRS = po src tests
 
 ACLOCAL_AMFLAGS = -I m4
 
index 80c06d2919ff9fc320a5f1e122629a84e66c4011..b83141028e686b800d44f76249ee5a53b76172d9 100644 (file)
@@ -29,14 +29,25 @@ AC_PROG_MAKE_SET
 AC_PROG_LIBTOOL
 AC_PROG_RANLIB
 
+IT_PROG_INTLTOOL
+
+dnl ******************************
+dnl Translations
+dnl ******************************
+GETTEXT_PACKAGE=libzakformcgi
+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(ZAKFORMCGI, [glib-2.0 >= 2.36
-                           gobject-2.0 >= 2.36
-                           gio-2.0 >= 2.36
-                           gio-unix-2.0 >= 2.36
-                           libzakcgi
-                           libzakform >= 0.0.1
-                           libzakformini])
+                               gobject-2.0 >= 2.36
+                               gio-2.0 >= 2.36
+                               gio-unix-2.0 >= 2.36
+                               libzakcgi
+                               libzakform >= 0.0.1
+                               libzakformini])
 
 AC_SUBST(ZAKFORMCGI_CFLAGS)
 AC_SUBST(ZAKFORMCGI_LIBS)
@@ -69,6 +80,7 @@ AM_CONDITIONAL(PLATFORM_WIN32, [test $platform_win32 = yes])
 AC_CONFIG_FILES([
        libzakformcgi.pc
        Makefile
+       po/Makefile.in
        src/Makefile
        tests/Makefile
 ])
diff --git a/po/LINGUAS b/po/LINGUAS
new file mode 100644 (file)
index 0000000..7d5856f
--- /dev/null
@@ -0,0 +1 @@
+it
diff --git a/po/POTFILES.in b/po/POTFILES.in
new file mode 100644 (file)
index 0000000..25aa9c4
--- /dev/null
@@ -0,0 +1,2 @@
+# List of source files which contain translatable strings.
+src/formelementfile.c
diff --git a/po/it.po b/po/it.po
new file mode 100644 (file)
index 0000000..70ede34
--- /dev/null
+++ b/po/it.po
@@ -0,0 +1,38 @@
+# libzakformcgi italian translation
+# Copyright (C) 2017 Andrea Zagli
+# This file is distributed under the same license as the libzakform package.
+# Andrea Zagli <azagli@libero.it>, 2017.
+#
+msgid ""
+msgstr ""
+"Project-Id-Version: libzakformcgi 0.0.1\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2017-05-13 15:50+0200\n"
+"PO-Revision-Date: 2017-05-16 12:44+0200\n"
+"Last-Translator: Andrea Zagli <azagli@libero.it>\n"
+"Language-Team: Italian <tp@lists.linux.it>\n"
+"Language: \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=2; plural=(n != 1);\n"
+
+#: ../src/formelementfile.c:411
+#, c-format
+msgid "The size of «%s» must be greater than %d."
+msgstr "La dimensione di «%s» deve essere superiore a %d."
+
+#: ../src/formelementfile.c:419
+#, c-format
+msgid "The size of «%s» cannot be greater than %d."
+msgstr "La dimensione di «%s» non può essere superiore a %d."
+
+#: ../src/formelementfile.c:447
+#, c-format
+msgid "The file for field «%s» must be of type «%s»."
+msgstr "Il file per «%s» deve essere del tipo «%s»."
+
+#: ../src/formelementfile.c:466
+#, c-format
+msgid "The field «%s» cannot be empty."
+msgstr "Il campo «%s» non può essere vuoto."
index 8e7ea917c2fcbeaeb10eee9b7f39874de42e5d6c..6b733e070588b7777c1e7bfb921be86138699a1b 100644 (file)
@@ -11,24 +11,25 @@ libzakformmodulesdir = `pkg-config --variable=modulesdir libzakform`
 LIBS = $(ZAKFORMCGI_LIBS)
 
 AM_CPPFLAGS = $(ZAKFORMCGI_CFLAGS) \
+              -DLOCALEDIR=\"$(localedir)\" \
               -DG_LOG_DOMAIN=\"ZakFormCGI\"
 
 lib_LTLIBRARIES = libzakformcgi.la
 
 libzakformcgi_la_SOURCES = commons.c \
-                       form.c \
-                       formelement.c \
-                       formelementcheck.c \
-                       formelementemail.c \
-                       formelementfile.c \
-                       formelementhidden.c \
-                       formelementpassword.c \
-                       formelementradio.c \
-                       formelementselect.c \
-                       formelementstring.c \
-                       formelementtext.c \
-                       formelementtextarea.c \
-                       formelementsubmit.c
+                           form.c \
+                           formelement.c \
+                           formelementcheck.c \
+                           formelementemail.c \
+                           formelementfile.c \
+                           formelementhidden.c \
+                           formelementpassword.c \
+                           formelementradio.c \
+                           formelementselect.c \
+                           formelementstring.c \
+                           formelementtext.c \
+                           formelementtextarea.c \
+                           formelementsubmit.c
 
 libzakformcgi_la_LDFLAGS = -no-undefined
 
index 9cf7abc6ccac0ce610b2eb3ff519a2ae6ecab58a..debf1d432cfd1f81ed46c624b2a1e8b08c7d4b7f 100644 (file)
@@ -20,6 +20,8 @@
        #include <config.h>
 #endif
 
+#include <glib/gi18n-lib.h>
+
 #include "form.h"
 #include "formelementstring.h"
 
@@ -27,13 +29,13 @@ static void zak_form_cgi_form_class_init (ZakFormCgiFormClass *class);
 static void zak_form_cgi_form_init (ZakFormCgiForm *zak_form_cgi_form);
 
 static void zak_form_cgi_form_set_property (GObject *object,
-                               guint property_id,
-                               const GValue *value,
-                               GParamSpec *pspec);
+                                            guint property_id,
+                                            const GValue *value,
+                                            GParamSpec *pspec);
 static void zak_form_cgi_form_get_property (GObject *object,
-                               guint property_id,
-                               GValue *value,
-                               GParamSpec *pspec);
+                                            guint property_id,
+                                            GValue *value,
+                                            GParamSpec *pspec);
 
 static void zak_form_cgi_form_dispose (GObject *gobject);
 static void zak_form_cgi_form_finalize (GObject *gobject);
@@ -50,6 +52,30 @@ struct _ZakFormCgiFormPrivate
 
 G_DEFINE_TYPE (ZakFormCgiForm, zak_form_cgi_form, ZAK_FORM_TYPE_FORM)
 
+#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_cgi_form_class_init (ZakFormCgiFormClass *class)
 {
@@ -83,11 +109,44 @@ zak_form_cgi_form_init (ZakFormCgiForm *zak_form_cgi_form)
 ZakFormCgiForm
 *zak_form_cgi_form_new (ZakCgiMain *zakcgimain, ...)
 {
+       gchar *localedir;
+
        ZakFormCgiForm *zak_form_cgi_form;
        ZakFormCgiFormPrivate *priv;
 
        va_list ap;
 
+#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_cgi_form = ZAK_FORM_CGI_FORM (g_object_new (zak_form_cgi_form_get_type (), NULL));
 
        priv = ZAK_FORM_CGI_FORM_GET_PRIVATE (zak_form_cgi_form);
index 5e049be1d0816abed3b66455857adcfcbd743bf9..af4a1fbcbb7d460f5de1ec2514eb0c7f644f4033 100644 (file)
@@ -20,6 +20,8 @@
        #include <config.h>
 #endif
 
+#include <glib/gi18n-lib.h>
+
 #include <libzakcgi/libzakcgi.h>
 #include "formelementfile.h"
 
@@ -50,6 +52,7 @@ struct _ZakFormCgiFormElementFilePrivate
                guint size_min;
                guint size_max;
                gchar *mime_type;
+               gchar *mime_type_message;
        };
 
 G_DEFINE_TYPE (ZakFormCgiFormElementFile, zak_form_cgi_form_element_file, ZAK_FORM_CGI_TYPE_FORM_ELEMENT)
@@ -79,6 +82,7 @@ zak_form_cgi_form_element_file_init (ZakFormCgiFormElementFile *zak_form_cgi_for
        priv->size_min = 0;
        priv->size_max = 0;
        priv->mime_type = NULL;
+       priv->mime_type_message = NULL;
 }
 
 /**
@@ -167,10 +171,11 @@ zak_form_cgi_form_element_file_xml_parsing (ZakFormElement *element, xmlNodePtr
                        else if (xmlStrcmp (cur->name, (const xmlChar *)"zak-cgi-mime-type") == 0)
                                {
                                        zak_form_cgi_form_element_file_set_mime_type (element, (gchar *)xmlNodeGetContent (cur));
+                                       zak_form_cgi_form_element_file_set_mime_type_message (element, (gchar *)xmlGetProp (cur, (const xmlChar *)"message"));
                                }
                        else
                                {
-                                       g_hash_table_replace (ht_attrs, g_strdup (cur->name), (gchar *)xmlNodeGetContent (cur));
+                                       g_hash_table_replace (ht_attrs, g_strdup ((gchar *)cur->name), (gchar *)xmlNodeGetContent (cur));
                                }
 
                        cur = cur->next;
@@ -262,6 +267,38 @@ gchar
                }
 }
 
+void
+zak_form_cgi_form_element_file_set_mime_type_message (ZakFormElement *element, const gchar *mime_type_message)
+{
+       ZakFormCgiFormElementFilePrivate *priv = ZAK_FORM_CGI_FORM_ELEMENT_FILE_GET_PRIVATE (element);
+
+       if (priv->mime_type_message != NULL)
+               {
+                       g_free (priv->mime_type_message);
+                       priv->mime_type_message = NULL;
+               }
+
+       if (mime_type_message != NULL)
+               {
+                       priv->mime_type_message = g_strdup (mime_type_message);
+               }
+}
+
+gchar
+*zak_form_cgi_form_element_file_get_mime_type_message (ZakFormElement *element)
+{
+       ZakFormCgiFormElementFilePrivate *priv = ZAK_FORM_CGI_FORM_ELEMENT_FILE_GET_PRIVATE (element);
+
+       if (priv->mime_type_message == NULL)
+               {
+                       return NULL;
+               }
+       else
+               {
+                       return g_strdup (priv->mime_type_message);
+               }
+}
+
 static gchar
 *zak_form_cgi_form_element_file_render (ZakFormCgiFormElement *element)
 {
@@ -352,6 +389,8 @@ zak_form_cgi_form_element_file_after_validating (ZakFormElement *element, GPtrAr
 {
        gboolean ret;
 
+       ZakCgiFile *cgi_file;
+
        ZakFormCgiFormElementFilePrivate *priv = ZAK_FORM_CGI_FORM_ELEMENT_FILE_GET_PRIVATE (element);
 
        ret = FALSE;
@@ -360,17 +399,76 @@ zak_form_cgi_form_element_file_after_validating (ZakFormElement *element, GPtrAr
            || priv->size_max > 0
            || priv->mime_type != NULL)
                {
-                       if (priv->size_min > 0)
+                       cgi_file = (ZakCgiFile *)g_value_get_boxed (zak_form_element_get_value_gvalue (element));
+                       if (cgi_file != NULL
+                           && cgi_file->content != NULL
+                           && cgi_file->name != NULL
+                           && g_strcmp0 (cgi_file->name, "") != 0)
                                {
-
+                                       if (priv->size_min > 0
+                                               && cgi_file->size < priv->size_min)
+                                               {
+                                                       g_ptr_array_add (ar_messages, g_strdup_printf (_("The size of «%s» must be greater than %d."),
+                                                                                                      zak_form_element_get_long_name (element),
+                                                                                                      priv->size_min));
+                                                       ret = TRUE;
+                                               }
+                                       if (priv->size_max > 0
+                                           && cgi_file->size > priv->size_max)
+                                               {
+                                                       g_ptr_array_add (ar_messages, g_strdup_printf (_("The size of «%s» cannot be greater than %d."),
+                                                                                                      zak_form_element_get_long_name (element),
+                                                                                                      priv->size_max));
+                                                       ret = TRUE;
+                                               }
+                                       if (priv->mime_type != NULL)
+                                               {
+                                                       gchar **splitted;
+                                                       guint l;
+                                                       guint i;
+                                                       gboolean found;
+                                                       gchar *mime;
+
+                                                       found = FALSE;
+
+                                                       mime = g_content_type_get_mime_type (cgi_file->content_type);
+
+                                                       if (mime == NULL)
+                                                               {
+                                                                       mime = g_strdup (cgi_file->content_type);
+                                                               }
+
+                                                       splitted = g_strsplit (priv->mime_type, "|", -1);
+                                                       l = g_strv_length (splitted);
+                                                       for (i = 0; i < l; i++)
+                                                               {
+                                                                       if (g_strcmp0 (mime, splitted[i]) == 0)
+                                                                               {
+                                                                                       found = TRUE;
+                                                                                       break;
+                                                                               }
+                                                               }
+                                                       g_strfreev (splitted);
+
+                                                       if (mime != NULL)
+                                                               {
+                                                                       g_free (mime);
+                                                               }
+
+                                                       if (!found)
+                                                               {
+                                                                       g_ptr_array_add (ar_messages, g_strdup_printf (_("The file for field «%s» must be of type «%s»."),
+                                                                                                                      zak_form_element_get_long_name (element),
+                                                                                                                      priv->mime_type_message != NULL ? priv->mime_type_message : priv->mime_type));
+                                                                       ret = TRUE;
+                                                               }
+                                               }
                                }
-                       if (priv->size_max > 0)
+                       else if (priv->size_min > 0)
                                {
-
-                               }
-                       if (priv->mime_type != NULL)
-                               {
-
+                                       g_ptr_array_add (ar_messages, g_strdup_printf (_("The field «%s» cannot be empty."),
+                                                                                      zak_form_element_get_long_name (element)));
+                                       ret = TRUE;
                                }
                }
 
index a5ecc4eb5ca9fb2e102b21291d33e0c67b16657d..2a16a25b16929b19626eb212ed12380cc74d40c4 100644 (file)
@@ -69,6 +69,9 @@ void zak_form_cgi_form_element_file_set_size_range (ZakFormElement *element, gui
 void zak_form_cgi_form_element_file_set_mime_type (ZakFormElement *element, const gchar *mime_type);
 gchar *zak_form_cgi_form_element_file_get_mime_type (ZakFormElement *element);
 
+void zak_form_cgi_form_element_file_set_mime_type_message (ZakFormElement *element, const gchar *mime_type_message);
+gchar *zak_form_cgi_form_element_file_get_mime_type_message (ZakFormElement *element);
+
 
 G_END_DECLS