From: Andrea Zagli Date: Sat, 2 Jan 2021 09:23:38 +0000 (+0100) Subject: Added FormElementValidatorFile. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=3c4425a3d4b98ca5b420eed23e9feca430700e3b;p=zakform%2Fcgi Added FormElementValidatorFile. --- diff --git a/po/POTFILES.in b/po/POTFILES.in index e2f362b..032a25d 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -1,3 +1,4 @@ # List of source files which contain translatable strings. src/form.c src/formelementfile.c +src/formelementvalidatorfile.c diff --git a/po/it.po b/po/it.po index 70ede34..106d748 100644 --- a/po/it.po +++ b/po/it.po @@ -1,5 +1,5 @@ # libzakformcgi italian translation -# Copyright (C) 2017 Andrea Zagli +# Copyright (C) 2017-2020 Andrea Zagli # This file is distributed under the same license as the libzakform package. # Andrea Zagli , 2017. # @@ -7,7 +7,7 @@ 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" +"POT-Creation-Date: 2021-01-02 10:20+0100\n" "PO-Revision-Date: 2017-05-16 12:44+0200\n" "Last-Translator: Andrea Zagli \n" "Language-Team: Italian \n" @@ -17,22 +17,26 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n" -#: ../src/formelementfile.c:411 +#: ../src/form.c:207 +msgid "Element without id." +msgstr "Elemento senza id" + +#: ../src/formelementvalidatorfile.c:330 #, c-format -msgid "The size of «%s» must be greater than %d." -msgstr "La dimensione di «%s» deve essere superiore a %d." +msgid "The size must be greater than %d." +msgstr "La dimensione deve essere superiore a %d." -#: ../src/formelementfile.c:419 +#: ../src/formelementvalidatorfile.c:338 #, c-format -msgid "The size of «%s» cannot be greater than %d." -msgstr "La dimensione di «%s» non può essere superiore a %d." +msgid "The size cannot be greater than %d." +msgstr "La dimensione non può essere superiore a %d." -#: ../src/formelementfile.c:447 +#: ../src/formelementvalidatorfile.c:379 #, c-format -msgid "The file for field «%s» must be of type «%s»." -msgstr "Il file per «%s» deve essere del tipo «%s»." +msgid "The file must be of type «%s»." +msgstr "Il file deve essere del tipo «%s»." -#: ../src/formelementfile.c:466 +#: ../src/formelementvalidatorfile.c:388 #, c-format -msgid "The field «%s» cannot be empty." -msgstr "Il campo «%s» non può essere vuoto." +msgid "The field cannot be empty." +msgstr "Il campo non può essere vuoto." diff --git a/src/Makefile.am b/src/Makefile.am index 6b733e0..ad65f9f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -29,7 +29,8 @@ libzakformcgi_la_SOURCES = commons.c \ formelementstring.c \ formelementtext.c \ formelementtextarea.c \ - formelementsubmit.c + formelementsubmit.c \ + formelementvalidatorfile.c libzakformcgi_la_LDFLAGS = -no-undefined @@ -48,7 +49,8 @@ libzakformcgi_include_HEADERS = \ formelementstring.h \ formelementtext.h \ formelementtextarea.h \ - formelementsubmit.h + formelementsubmit.h \ + formelementvalidatorfile.h libzakformcgi_includedir = $(includedir)/libzakformcgi diff --git a/src/formelementfile.c b/src/formelementfile.c index 4fa89aa..27cf672 100644 --- a/src/formelementfile.c +++ b/src/formelementfile.c @@ -24,6 +24,7 @@ #include #include "formelementfile.h" +#include "formelementvalidatorfile.h" static void zak_form_cgi_form_element_file_class_init (ZakFormCgiFormElementFileClass *class); static void zak_form_cgi_form_element_file_init (ZakFormCgiFormElementFile *zak_form_cgi_form_element_file); @@ -47,10 +48,7 @@ static gboolean zak_form_cgi_form_element_file_after_validating (ZakFormElement typedef struct _ZakFormCgiFormElementFilePrivate ZakFormCgiFormElementFilePrivate; struct _ZakFormCgiFormElementFilePrivate { - guint size_min; - guint size_max; - gchar *mime_type; - gchar *mime_type_message; + ZakFormElementValidator *validator_file; }; G_DEFINE_TYPE_WITH_PRIVATE (ZakFormCgiFormElementFile, zak_form_cgi_form_element_file, ZAK_FORM_CGI_TYPE_FORM_ELEMENT) @@ -77,10 +75,11 @@ zak_form_cgi_form_element_file_init (ZakFormCgiFormElementFile *zak_form_cgi_for { ZakFormCgiFormElementFilePrivate *priv = zak_form_cgi_form_element_file_get_instance_private (zak_form_cgi_form_element_file); - priv->size_min = 0; - priv->size_max = 0; - priv->mime_type = NULL; - priv->mime_type_message = NULL; + priv->validator_file = (ZakFormElementValidator *)zak_form_cgi_form_element_validator_file_new (); + + zak_form_element_validator_set_id (ZAK_FORM_ELEMENT_VALIDATOR (priv->validator_file), "{default}"); + + zak_form_element_add_validator (ZAK_FORM_ELEMENT (zak_form_cgi_form_element_file), ZAK_FORM_ELEMENT_VALIDATOR (priv->validator_file)); } /** @@ -188,7 +187,7 @@ zak_form_cgi_form_element_file_set_size_min (ZakFormElement *element, guint min) { ZakFormCgiFormElementFilePrivate *priv = zak_form_cgi_form_element_file_get_instance_private (ZAK_FORM_CGI_FORM_ELEMENT_FILE (element)); - priv->size_min = min; + zak_form_cgi_form_element_validator_file_set_size_min (priv->validator_file, min); } guint @@ -196,7 +195,7 @@ zak_form_cgi_form_element_file_get_size_min (ZakFormElement *element) { ZakFormCgiFormElementFilePrivate *priv = zak_form_cgi_form_element_file_get_instance_private (ZAK_FORM_CGI_FORM_ELEMENT_FILE (element)); - return priv->size_min; + return zak_form_cgi_form_element_validator_file_get_size_min (priv->validator_file); } void @@ -204,7 +203,7 @@ zak_form_cgi_form_element_file_set_size_max (ZakFormElement *element, guint max) { ZakFormCgiFormElementFilePrivate *priv = zak_form_cgi_form_element_file_get_instance_private (ZAK_FORM_CGI_FORM_ELEMENT_FILE (element)); - priv->size_max = max; + zak_form_cgi_form_element_validator_file_set_size_max (priv->validator_file, max); } guint @@ -212,14 +211,16 @@ zak_form_cgi_form_element_file_get_size_max (ZakFormElement *element) { ZakFormCgiFormElementFilePrivate *priv = zak_form_cgi_form_element_file_get_instance_private (ZAK_FORM_CGI_FORM_ELEMENT_FILE (element)); - return priv->size_max; + return zak_form_cgi_form_element_validator_file_get_size_max (priv->validator_file); } void zak_form_cgi_form_element_file_set_size_range (ZakFormElement *element, guint min, guint max) { - zak_form_cgi_form_element_file_set_size_min (element, min); - zak_form_cgi_form_element_file_set_size_max (element, max); + ZakFormCgiFormElementFilePrivate *priv = zak_form_cgi_form_element_file_get_instance_private (ZAK_FORM_CGI_FORM_ELEMENT_FILE (element)); + + zak_form_cgi_form_element_validator_file_set_size_min (priv->validator_file, min); + zak_form_cgi_form_element_validator_file_set_size_min (priv->validator_file, max); } void @@ -227,16 +228,7 @@ zak_form_cgi_form_element_file_set_mime_type (ZakFormElement *element, const gch { ZakFormCgiFormElementFilePrivate *priv = zak_form_cgi_form_element_file_get_instance_private (ZAK_FORM_CGI_FORM_ELEMENT_FILE (element)); - if (priv->mime_type != NULL) - { - g_free (priv->mime_type); - priv->mime_type = NULL; - } - - if (mime_type != NULL) - { - priv->mime_type = g_strdup (mime_type); - } + zak_form_cgi_form_element_validator_file_set_mime_type (priv->validator_file, mime_type); } gchar @@ -244,14 +236,7 @@ gchar { ZakFormCgiFormElementFilePrivate *priv = zak_form_cgi_form_element_file_get_instance_private (ZAK_FORM_CGI_FORM_ELEMENT_FILE (element)); - if (priv->mime_type == NULL) - { - return NULL; - } - else - { - return g_strdup (priv->mime_type); - } + return zak_form_cgi_form_element_validator_file_get_mime_type (priv->validator_file); } void @@ -259,16 +244,7 @@ zak_form_cgi_form_element_file_set_mime_type_message (ZakFormElement *element, c { ZakFormCgiFormElementFilePrivate *priv = zak_form_cgi_form_element_file_get_instance_private (ZAK_FORM_CGI_FORM_ELEMENT_FILE (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); - } + zak_form_cgi_form_element_validator_file_set_mime_type_message (priv->validator_file, mime_type_message); } gchar @@ -276,14 +252,7 @@ gchar { ZakFormCgiFormElementFilePrivate *priv = zak_form_cgi_form_element_file_get_instance_private (ZAK_FORM_CGI_FORM_ELEMENT_FILE (element)); - if (priv->mime_type_message == NULL) - { - return NULL; - } - else - { - return g_strdup (priv->mime_type_message); - } + return zak_form_cgi_form_element_validator_file_get_mime_type_message (priv->validator_file); } static gchar @@ -374,90 +343,8 @@ zak_form_cgi_form_element_file_finalize (GObject *gobject) static gboolean zak_form_cgi_form_element_file_after_validating (ZakFormElement *element, GPtrArray *ar_messages) { - gboolean ret; - - ZakCgiFile *cgi_file; - ZakFormCgiFormElementFilePrivate *priv = zak_form_cgi_form_element_file_get_instance_private (ZAK_FORM_CGI_FORM_ELEMENT_FILE (element)); - ret = FALSE; - - if (priv->size_min > 0 - || priv->size_max > 0 - || priv->mime_type != NULL) - { - 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; - } - } - } - else if (priv->size_min > 0) - { - g_ptr_array_add (ar_messages, g_strdup_printf (_("The field «%s» cannot be empty."), - zak_form_element_get_long_name (element))); - ret = TRUE; - } - } - - return ret; + return zak_form_element_validator_validate (priv->validator_file, + zak_form_element_get_value_gvalue (element)); } diff --git a/src/formelementvalidatorfile.c b/src/formelementvalidatorfile.c new file mode 100644 index 0000000..bdd8a4a --- /dev/null +++ b/src/formelementvalidatorfile.c @@ -0,0 +1,407 @@ +/* + * Copyright (C) 2020 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include + +#include +#include "formelementvalidatorfile.h" + +enum + { + PROP_0 + }; + +static void zak_form_cgi_form_element_validator_file_class_init (ZakFormCgiFormElementValidatorFileClass *class); +static void zak_form_cgi_form_element_validator_file_init (ZakFormCgiFormElementValidatorFile *zak_form_element); + +static void zak_form_cgi_form_element_validator_file_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void zak_form_cgi_form_element_validator_file_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static void zak_form_cgi_form_element_validator_file_dispose (GObject *gobject); +static void zak_form_cgi_form_element_validator_file_finalize (GObject *gobject); + +static gboolean zak_form_cgi_form_element_validator_file_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode); +static gboolean zak_form_cgi_form_element_validator_file_validate (ZakFormElementValidator *validator_file, GValue *value); +static void zak_form_cgi_form_element_validator_file_get_xml (ZakFormElementValidator *validator, xmlNode *xnode, const gchar *version); + +struct _ZakFormCgiFormElementValidatorFile +{ + ZakFormElementValidator parent_instance; + + /* Other members, including private data. */ +}; + +typedef struct _ZakFormCgiFormElementValidatorFilePrivate ZakFormCgiFormElementValidatorFilePrivate; +struct _ZakFormCgiFormElementValidatorFilePrivate + { + guint size_min; + guint size_max; + gchar *mime_type; + gchar *mime_type_message; + }; + +G_DEFINE_TYPE_WITH_PRIVATE (ZakFormCgiFormElementValidatorFile, zak_form_cgi_form_element_validator_file, ZAK_FORM_TYPE_ELEMENT_VALIDATOR) + +static void +zak_form_cgi_form_element_validator_file_class_init (ZakFormCgiFormElementValidatorFileClass *class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (class); + ZakFormElementValidatorClass *parent_class = ZAK_FORM_ELEMENT_VALIDATOR_CLASS (class); + + object_class->set_property = zak_form_cgi_form_element_validator_file_set_property; + object_class->get_property = zak_form_cgi_form_element_validator_file_get_property; + object_class->dispose = zak_form_cgi_form_element_validator_file_dispose; + object_class->finalize = zak_form_cgi_form_element_validator_file_finalize; + + parent_class->xml_parsing = zak_form_cgi_form_element_validator_file_xml_parsing; + parent_class->validate = zak_form_cgi_form_element_validator_file_validate; + parent_class->get_xml = zak_form_cgi_form_element_validator_file_get_xml; +} + +static void +zak_form_cgi_form_element_validator_file_init (ZakFormCgiFormElementValidatorFile *zak_form_element) +{ + ZakFormCgiFormElementValidatorFilePrivate *priv = zak_form_cgi_form_element_validator_file_get_instance_private (zak_form_element); + + priv->size_min = 0; + priv->size_max = 0; + priv->mime_type = NULL; + priv->mime_type_message = NULL; +} + +/** + * zak_form_cgi_form_element_validator_file_new: + * + * Returns: the newly created #ZakFormCgiFormElementValidatorFile object. + */ +ZakFormCgiFormElementValidatorFile +*zak_form_cgi_form_element_validator_file_new () +{ + ZakFormCgiFormElementValidatorFile *zak_form_cgi_form_element_validator_file; + + zak_form_cgi_form_element_validator_file = ZAK_FORM_CGI_FORM_ELEMENT_VALIDATOR_FILE (g_object_new (zak_form_cgi_form_element_validator_file_get_type (), NULL)); + + return zak_form_cgi_form_element_validator_file; +} + +/** + * zak_form_cgi_form_element_validator_file_xml_parsing: + * @validator: + * @xnode: + * + */ +static gboolean +zak_form_cgi_form_element_validator_file_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode) +{ + ZakFormCgiFormElementValidatorFilePrivate *priv = zak_form_cgi_form_element_validator_file_get_instance_private (ZAK_FORM_CGI_FORM_ELEMENT_VALIDATOR_FILE (validator)); + + if (xmlStrcmp (xnode->name, (const xmlChar *)"size-min") == 0) + { + zak_form_cgi_form_element_validator_file_set_size_min (validator, strtol ((gchar *)xmlNodeGetContent (xnode), NULL, 10)); + } + else if (xmlStrcmp (xnode->name, (const xmlChar *)"size-max") == 0) + { + zak_form_cgi_form_element_validator_file_set_size_max (validator, strtol ((gchar *)xmlNodeGetContent (xnode), NULL, 10)); + } + else if (xmlStrcmp (xnode->name, (const xmlChar *)"mime-type") == 0) + { + zak_form_cgi_form_element_validator_file_set_mime_type (validator, (gchar *)xmlNodeGetContent (xnode)); + zak_form_cgi_form_element_validator_file_set_mime_type_message (validator, (gchar *)xmlGetProp (xnode, (const xmlChar *)"message")); + } + + return TRUE; +} + +void +zak_form_cgi_form_element_validator_file_set_size_min (ZakFormElementValidator *validator, guint min) +{ + ZakFormCgiFormElementValidatorFilePrivate *priv = zak_form_cgi_form_element_validator_file_get_instance_private (ZAK_FORM_CGI_FORM_ELEMENT_VALIDATOR_FILE (validator)); + + priv->size_min = min; +} + +guint +zak_form_cgi_form_element_validator_file_get_size_min (ZakFormElementValidator *validator) +{ + ZakFormCgiFormElementValidatorFilePrivate *priv = zak_form_cgi_form_element_validator_file_get_instance_private (ZAK_FORM_CGI_FORM_ELEMENT_VALIDATOR_FILE (validator)); + + return priv->size_min; +} + +void +zak_form_cgi_form_element_validator_file_set_size_max (ZakFormElementValidator *validator, guint max) +{ + ZakFormCgiFormElementValidatorFilePrivate *priv = zak_form_cgi_form_element_validator_file_get_instance_private (ZAK_FORM_CGI_FORM_ELEMENT_VALIDATOR_FILE (validator)); + + priv->size_max = max; +} + +guint +zak_form_cgi_form_element_validator_file_get_size_max (ZakFormElementValidator *validator) +{ + ZakFormCgiFormElementValidatorFilePrivate *priv = zak_form_cgi_form_element_validator_file_get_instance_private (ZAK_FORM_CGI_FORM_ELEMENT_VALIDATOR_FILE (validator)); + + return priv->size_max; +} + +void +zak_form_cgi_form_element_validator_file_set_size_range (ZakFormElementValidator *validator, guint min, guint max) +{ + zak_form_cgi_form_element_validator_file_set_size_min (validator, min); + zak_form_cgi_form_element_validator_file_set_size_max (validator, max); +} + +void +zak_form_cgi_form_element_validator_file_set_mime_type (ZakFormElementValidator *validator, const gchar *mime_type) +{ + ZakFormCgiFormElementValidatorFilePrivate *priv = zak_form_cgi_form_element_validator_file_get_instance_private (ZAK_FORM_CGI_FORM_ELEMENT_VALIDATOR_FILE (validator)); + + if (priv->mime_type != NULL) + { + g_free (priv->mime_type); + priv->mime_type = NULL; + } + + if (mime_type != NULL) + { + priv->mime_type = g_strdup (mime_type); + } +} + +gchar +*zak_form_cgi_form_element_validator_file_get_mime_type (ZakFormElementValidator *validator) +{ + ZakFormCgiFormElementValidatorFilePrivate *priv = zak_form_cgi_form_element_validator_file_get_instance_private (ZAK_FORM_CGI_FORM_ELEMENT_VALIDATOR_FILE (validator)); + + if (priv->mime_type == NULL) + { + return NULL; + } + else + { + return g_strdup (priv->mime_type); + } +} + +void +zak_form_cgi_form_element_validator_file_set_mime_type_message (ZakFormElementValidator *validator, const gchar *mime_type_message) +{ + ZakFormCgiFormElementValidatorFilePrivate *priv = zak_form_cgi_form_element_validator_file_get_instance_private (ZAK_FORM_CGI_FORM_ELEMENT_VALIDATOR_FILE (validator)); + + 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_validator_file_get_mime_type_message (ZakFormElementValidator *validator) +{ + ZakFormCgiFormElementValidatorFilePrivate *priv = zak_form_cgi_form_element_validator_file_get_instance_private (ZAK_FORM_CGI_FORM_ELEMENT_VALIDATOR_FILE (validator)); + + if (priv->mime_type_message == NULL) + { + return NULL; + } + else + { + return g_strdup (priv->mime_type_message); + } +} + +/* PRIVATE */ +static void +zak_form_cgi_form_element_validator_file_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + ZakFormCgiFormElementValidatorFile *validator = (ZakFormCgiFormElementValidatorFile *)object; + ZakFormCgiFormElementValidatorFilePrivate *priv = zak_form_cgi_form_element_validator_file_get_instance_private (validator); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_form_cgi_form_element_validator_file_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) +{ + ZakFormCgiFormElementValidatorFile *validator = (ZakFormCgiFormElementValidatorFile *)object; + ZakFormCgiFormElementValidatorFilePrivate *priv = zak_form_cgi_form_element_validator_file_get_instance_private (validator); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_form_cgi_form_element_validator_file_dispose (GObject *gobject) +{ + ZakFormCgiFormElementValidatorFile *validator = (ZakFormCgiFormElementValidatorFile *)gobject; + ZakFormCgiFormElementValidatorFilePrivate *priv = zak_form_cgi_form_element_validator_file_get_instance_private (validator); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->dispose (gobject); +} + +static void +zak_form_cgi_form_element_validator_file_finalize (GObject *gobject) +{ + ZakFormCgiFormElementValidatorFile *validator = (ZakFormCgiFormElementValidatorFile *)gobject; + ZakFormCgiFormElementValidatorFilePrivate *priv = zak_form_cgi_form_element_validator_file_get_instance_private (validator); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->finalize (gobject); +} + +static gboolean +zak_form_cgi_form_element_validator_file_validate (ZakFormElementValidator *validator, + GValue *value) +{ + gboolean ret; + + ZakCgiFile *cgi_file; + + ZakFormCgiFormElementValidatorFilePrivate *priv = zak_form_cgi_form_element_validator_file_get_instance_private (ZAK_FORM_CGI_FORM_ELEMENT_VALIDATOR_FILE (validator)); + + g_return_val_if_fail (value != NULL, FALSE); + + ret = TRUE; + + if (priv->size_min > 0 + || priv->size_max > 0 + || priv->mime_type != NULL) + { + cgi_file = (ZakCgiFile *)g_value_get_boxed (value); + 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) + { + zak_form_element_validator_set_message (validator, + g_strdup_printf (_("The size must be greater than %d."), + priv->size_min)); + ret = FALSE; + } + if (priv->size_max > 0 + && cgi_file->size > priv->size_max) + { + zak_form_element_validator_set_message (validator, + g_strdup_printf (_("The size cannot be greater than %d."), + priv->size_max)); + ret = FALSE; + } + 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) + { + zak_form_element_validator_set_message (validator, + g_strdup_printf (_("The file must be of type «%s»."), + priv->mime_type_message != NULL ? priv->mime_type_message : priv->mime_type)); + ret = FALSE; + } + } + } + else if (priv->size_min > 0) + { + zak_form_element_validator_set_message (validator, + g_strdup_printf (_("The field cannot be empty."))); + ret = FALSE; + } + } + + return ret; +} + +static void +zak_form_cgi_form_element_validator_file_get_xml (ZakFormElementValidator *validator, xmlNode *xnode, const gchar *version) +{ + ZakFormCgiFormElementValidatorFile *validator_file = ZAK_FORM_CGI_FORM_ELEMENT_VALIDATOR_FILE (validator); + ZakFormCgiFormElementValidatorFilePrivate *priv = zak_form_cgi_form_element_validator_file_get_instance_private (validator_file); + + xmlSetProp (xnode, (const xmlChar *)"type", (const xmlChar *)"zak_form_cgi_form_element_validator_file"); + + xmlSetProp (xnode, (const xmlChar *)"size-min", (const xmlChar *)g_strdup_printf ("%d", zak_form_cgi_form_element_validator_file_get_size_min (validator))); + xmlSetProp (xnode, (const xmlChar *)"size-max", (const xmlChar *)g_strdup_printf ("%d", zak_form_cgi_form_element_validator_file_get_size_max (validator))); + xmlSetProp (xnode, (const xmlChar *)"mime-type", (const xmlChar *)zak_form_cgi_form_element_validator_file_get_mime_type (validator)); +} diff --git a/src/formelementvalidatorfile.h b/src/formelementvalidatorfile.h new file mode 100644 index 0000000..3cdfa3f --- /dev/null +++ b/src/formelementvalidatorfile.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2020 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __ZAK_FORM_CGI_FORM_ELEMENT_VALIDATOR_FILE_H__ +#define __ZAK_FORM_CGI_FORM_ELEMENT_VALIDATOR_FILE_H__ + + +#include + +#include + +#include + + +G_BEGIN_DECLS + + +#define ZAK_FORM_TYPE_ELEMENT_VALIDATOR_FILE zak_form_cgi_form_element_validator_file_get_type () +G_DECLARE_FINAL_TYPE (ZakFormCgiFormElementValidatorFile, zak_form_cgi_form_element_validator_file, ZAK_FORM_CGI_FORM, ELEMENT_VALIDATOR_FILE, ZakFormElementValidator) + +ZakFormCgiFormElementValidatorFile *zak_form_cgi_form_element_validator_file_new (void); + +void zak_form_cgi_form_element_validator_file_set_size_min (ZakFormElementValidator *validator, guint min); +guint zak_form_cgi_form_element_validator_file_get_size_min (ZakFormElementValidator *validator); + +void zak_form_cgi_form_element_validator_file_set_size_max (ZakFormElementValidator *validator, guint max); +guint zak_form_cgi_form_element_validator_file_get_size_max (ZakFormElementValidator *validator); + +void zak_form_cgi_form_element_validator_file_set_size_range (ZakFormElementValidator *validator, guint min, guint max); + +void zak_form_cgi_form_element_validator_file_set_mime_type (ZakFormElementValidator *validator, const gchar *mime_type); +gchar *zak_form_cgi_form_element_validator_file_get_mime_type (ZakFormElementValidator *validator); + +void zak_form_cgi_form_element_validator_file_set_mime_type_message (ZakFormElementValidator *validator, const gchar *mime_type_message); +gchar *zak_form_cgi_form_element_validator_file_get_mime_type_message (ZakFormElementValidator *validator); + + +G_END_DECLS + + +#endif /* __ZAK_FORM_CGI_FORM_ELEMENT_VALIDATOR_FILE_H__ */