From 0cec813d4017ed4879cb62589621adb3e0f85ef2 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Mon, 28 Dec 2015 15:05:04 +0100 Subject: [PATCH] Added ZakFormElementValidatorDate. --- src/Makefile.am | 2 + src/form.c | 8 ++ src/formelementvalidatordate.c | 237 ++++++++++++++++++++++++++++++++ src/formelementvalidatordate.h | 47 +++++++ src/formelementvalidatorregex.c | 1 - 5 files changed, 294 insertions(+), 1 deletion(-) create mode 100644 src/formelementvalidatordate.c create mode 100644 src/formelementvalidatordate.h diff --git a/src/Makefile.am b/src/Makefile.am index c0380a3..6922a3f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -13,6 +13,7 @@ libzakform_la_SOURCES = \ formelementfilter.c \ formelementfiltertrim.c \ formelementvalidator.c \ + formelementvalidatordate.c \ formelementvalidatornotempty.c \ formelementvalidatorregex.c \ formiprovider.c @@ -26,6 +27,7 @@ libzakform_include_HEADERS = \ formelementfilter.h \ formelementfiltertrim.h \ formelementvalidator.h \ + formelementvalidatordate.h \ formelementvalidatornotempty.h \ formelementvalidatorregex.h \ formiprovider.h diff --git a/src/form.c b/src/form.c index 8c5cf71..c0b66eb 100644 --- a/src/form.c +++ b/src/form.c @@ -199,6 +199,10 @@ zak_form_form_element_xml_parsing (ZakFormForm *zakform, ZakFormElement *element } } } + if (i >= priv->ar_modules->len) + { + g_warning ("Filter «%s» not found.", type); + } } else if (xmlStrcmp (xnode->name, (const xmlChar *)"validator") == 0) { @@ -230,6 +234,10 @@ zak_form_form_element_xml_parsing (ZakFormForm *zakform, ZakFormElement *element } } } + if (i >= priv->ar_modules->len) + { + g_warning ("Validator «%s» not found.", type); + } } xnode = xnode->next; diff --git a/src/formelementvalidatordate.c b/src/formelementvalidatordate.c new file mode 100644 index 0000000..2546c61 --- /dev/null +++ b/src/formelementvalidatordate.c @@ -0,0 +1,237 @@ +/* + * Copyright (C) 2015 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 "formelementvalidator.h" +#include "formelementvalidatordate.h" + +static void zak_form_element_validator_date_class_init (ZakFormElementValidatorDateClass *class); +static void zak_form_element_validator_date_init (ZakFormElementValidatorDate *zak_form_element); + +static void zak_form_element_validator_date_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void zak_form_element_validator_date_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static void zak_form_element_validator_date_dispose (GObject *gobject); +static void zak_form_element_validator_date_finalize (GObject *gobject); + +static gboolean zak_form_element_validator_date_validate (ZakFormElementValidator *validator_date, const gchar *value); + +struct _ZakFormElementValidatorDate +{ + ZakFormElementValidator parent_instance; + + /* Other members, including private data. */ +}; + +#define ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ZAK_TYPE_FORM_ELEMENT_VALIDATOR_DATE, ZakFormElementValidatorDatePrivate)) + +typedef struct _ZakFormElementValidatorDatePrivate ZakFormElementValidatorDatePrivate; +struct _ZakFormElementValidatorDatePrivate + { + gchar *format; + }; + +G_DEFINE_TYPE (ZakFormElementValidatorDate, zak_form_element_validator_date, ZAK_FORM_TYPE_ELEMENT_VALIDATOR) + +static void +zak_form_element_validator_date_class_init (ZakFormElementValidatorDateClass *class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (class); + ZakFormElementValidatorClass *parent_class = ZAK_FORM_ELEMENT_VALIDATOR_CLASS (class); + + object_class->set_property = zak_form_element_validator_date_set_property; + object_class->get_property = zak_form_element_validator_date_get_property; + object_class->dispose = zak_form_element_validator_date_dispose; + object_class->finalize = zak_form_element_validator_date_finalize; + + parent_class->validate = zak_form_element_validator_date_validate; + + g_type_class_add_private (object_class, sizeof (ZakFormElementValidatorDatePrivate)); +} + +static void +zak_form_element_validator_date_init (ZakFormElementValidatorDate *zak_form_element) +{ + ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (zak_form_element); + + priv->format = NULL; +} + +/** + * zak_form_element_validator_date_new: + * + * Returns: the newly created #ZakFormElementValidatorDate object. + */ +ZakFormElementValidatorDate +*zak_form_element_validator_date_new () +{ + ZakFormElementValidatorDate *zak_form_element_validator_date; + + zak_form_element_validator_date = ZAK_FORM_ELEMENT_VALIDATOR_DATE (g_object_new (zak_form_element_validator_date_get_type (), NULL)); + + return zak_form_element_validator_date; +} + +/** + * zak_form_element_validator_date_set_format: + * @validator: + * @format: + * + */ +void +zak_form_element_validator_date_set_format (ZakFormElementValidatorDate *validator, const gchar *format) +{ + ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (validator); + + if (priv->format != NULL) + { + g_free (priv->format); + } + priv->format = g_strdup (format); +} + +/** + * zak_form_element_validator_date_get_format: + * @validator: + * + */ +gchar +*zak_form_element_validator_date_get_format (ZakFormElementValidatorDate *validator) +{ + ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (validator); + + return g_strdup (priv->format); +} + +/** + * zak_form_element_validator_date_xml_parsing: + * @validator: + * @xnode: + * + */ +gboolean +zak_form_element_validator_date_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode) +{ + ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (ZAK_FORM_ELEMENT_VALIDATOR_DATE (validator)); + + zak_form_element_validator_date_set_format (ZAK_FORM_ELEMENT_VALIDATOR_DATE (validator), (gchar *)xmlNodeGetContent (xnode)); + + return TRUE; +} + +/* PRIVATE */ +static void +zak_form_element_validator_date_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + ZakFormElementValidatorDate *zak_form_element = (ZakFormElementValidatorDate *)object; + ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (zak_form_element); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_form_element_validator_date_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) +{ + ZakFormElementValidatorDate *zak_form_element = (ZakFormElementValidatorDate *)object; + ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (zak_form_element); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_form_element_validator_date_dispose (GObject *gobject) +{ + ZakFormElementValidatorDate *zak_form_element = (ZakFormElementValidatorDate *)gobject; + ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (zak_form_element); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->dispose (gobject); +} + +static void +zak_form_element_validator_date_finalize (GObject *gobject) +{ + ZakFormElementValidatorDate *zak_form_element = (ZakFormElementValidatorDate *)gobject; + ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (zak_form_element); + + g_free (priv->format); + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->finalize (gobject); +} + +static gboolean +zak_form_element_validator_date_validate (ZakFormElementValidator *validator_date, + const gchar *value) +{ + gboolean ret; + gchar *_value; + GDateTime *gdt; + + ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (validator_date); + + g_return_val_if_fail (value != NULL, FALSE); + + _value = g_strstrip (g_strdup (value)); + if (g_strcmp0 (_value, "") == 0) + { + ret = TRUE; + } + else + { + gdt = zak_utils_get_gdatetime_from_string (_value, priv->format); + ret = (gdt != NULL); + + if (gdt != NULL) + { + g_date_time_unref (gdt); + } + } + g_free (_value); + + return ret; +} diff --git a/src/formelementvalidatordate.h b/src/formelementvalidatordate.h new file mode 100644 index 0000000..accea52 --- /dev/null +++ b/src/formelementvalidatordate.h @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2015 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_ELEMENT_VALIDATOR_DATE_H__ +#define __ZAK_FORM_ELEMENT_VALIDATOR_DATE_H__ + + +#include + +#include + +#include "formelementvalidator.h" + + +G_BEGIN_DECLS + + +#define ZAK_TYPE_FORM_ELEMENT_VALIDATOR_DATE zak_form_element_validator_date_get_type () +G_DECLARE_FINAL_TYPE (ZakFormElementValidatorDate, zak_form_element_validator_date, ZAK_FORM, ELEMENT_VALIDATOR_DATE, ZakFormElementValidator) + +ZakFormElementValidatorDate *zak_form_element_validator_date_new (void); + +void zak_form_element_validator_date_set_format (ZakFormElementValidatorDate *validator, const gchar *format); +gchar *zak_form_element_validator_date_get_format (ZakFormElementValidatorDate *validator); + +gboolean zak_form_element_validator_date_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode); + + +G_END_DECLS + + +#endif /* __ZAK_FORM_ELEMENT_VALIDATOR_DATE_H__ */ diff --git a/src/formelementvalidatorregex.c b/src/formelementvalidatorregex.c index 6279769..17db5eb 100644 --- a/src/formelementvalidatorregex.c +++ b/src/formelementvalidatorregex.c @@ -120,7 +120,6 @@ zak_form_element_validator_regex_xml_parsing (ZakFormElementValidator *validator g_free (priv->regex); } priv->regex = g_strdup ((gchar *)xmlNodeGetContent (xnode)); - g_message ("regex: %s", priv->regex); return TRUE; } -- 2.49.0