From d8276abc4ced11a0ef4eb2eefe247ad1fafefd79 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Wed, 30 Dec 2015 17:53:26 +0100 Subject: [PATCH] Added ZakFormValidator. --- src/Makefile.am | 6 +- src/form.c | 100 +++++++++++++++++++++++- src/form.h | 4 + src/formvalidator.c | 186 ++++++++++++++++++++++++++++++++++++++++++++ src/formvalidator.h | 48 ++++++++++++ src/libzakform.h | 2 + 6 files changed, 343 insertions(+), 3 deletions(-) create mode 100644 src/formvalidator.c create mode 100644 src/formvalidator.h diff --git a/src/Makefile.am b/src/Makefile.am index 259e025..0b9634c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -17,7 +17,8 @@ libzakform_la_SOURCES = \ formelementvalidatordate.c \ formelementvalidatornotempty.c \ formelementvalidatorregex.c \ - formiprovider.c + formiprovider.c \ + formvalidator.c libzakform_la_LDFLAGS = -no-undefined @@ -32,7 +33,8 @@ libzakform_include_HEADERS = \ formelementvalidatordate.h \ formelementvalidatornotempty.h \ formelementvalidatorregex.h \ - formiprovider.h + formiprovider.h \ + formvalidator.h libzakform_includedir = $(includedir)/libzakform diff --git a/src/form.c b/src/form.c index c0b66eb..a8abfdd 100644 --- a/src/form.c +++ b/src/form.c @@ -26,7 +26,6 @@ #include #include "form.h" -#include "formelement.h" #include "formelementfilter.h" #include "formelementvalidator.h" @@ -40,6 +39,8 @@ typedef ZakFormElementFilter *(* FormElementFilterConstructorFunc) (void); typedef gboolean (* FormElementFilterXmlParsingFunc) (ZakFormElementFilter *, xmlNodePtr); typedef ZakFormElementValidator *(* FormElementValidatorConstructorFunc) (void); typedef gboolean (* FormElementValidatorXmlParsingFunc) (ZakFormElementValidator *, xmlNodePtr); +typedef ZakFormValidator *(* FormValidatorConstructorFunc) (void); +typedef gboolean (* FormValidatorXmlParsingFunc) (ZakFormValidator *, xmlNodePtr); static void zak_form_form_class_init (ZakFormFormClass *class); static void zak_form_form_init (ZakFormForm *zak_form_form); @@ -63,6 +64,7 @@ typedef struct { GPtrArray *ar_modules; GPtrArray *ar_elements; + GPtrArray *ar_validators; } ZakFormFormPrivate; G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ZakFormForm, zak_form_form, G_TYPE_OBJECT) @@ -113,6 +115,7 @@ zak_form_form_init (ZakFormForm *zak_form_form) priv->ar_modules = NULL; priv->ar_elements = g_ptr_array_new (); + priv->ar_validators = g_ptr_array_new (); #ifdef G_OS_WIN32 @@ -260,12 +263,17 @@ zak_form_form_load_from_xml (ZakFormForm *zakform, xmlDoc *xmldoc) gboolean ret; ZakFormElement *element; + ZakFormValidator *validator; + gchar *type; guint i; FormElementConstructorFunc element_constructor; FormElementXmlParsingFunc element_xml_parsing; + FormValidatorConstructorFunc validator_constructor; + FormValidatorXmlParsingFunc validator_xml_parsing; + g_return_val_if_fail (ZAK_FORM_IS_FORM (zakform), FALSE); g_return_val_if_fail (xmldoc != NULL, FALSE); @@ -326,6 +334,41 @@ zak_form_form_load_from_xml (ZakFormForm *zakform, xmlDoc *xmldoc) g_warning (_("Unknown element type «%s»."), type); } } + else if (xmlStrcmp (cur->name, (const xmlChar *)"validator") == 0) + { + type = xmlGetProp (cur, (const xmlChar *)"type"); + + /* for each module */ + for (i = 0; i < priv->ar_modules->len; i++) + { + if (g_module_symbol ((GModule *)g_ptr_array_index (priv->ar_modules, i), + g_strconcat (type, "_new", NULL), + (gpointer *)&validator_constructor)) + { + if (validator_constructor != NULL) + { + validator = validator_constructor (); + zak_form_form_add_validator (zakform, validator); + + if (g_module_symbol ((GModule *)g_ptr_array_index (priv->ar_modules, i), + g_strconcat (type, "_xml_parsing", NULL), + (gpointer *)&validator_xml_parsing)) + { + if (validator_xml_parsing != NULL) + { + validator_xml_parsing (validator, cur); + } + } + + break; + } + } + } + if (i >= priv->ar_modules->len) + { + g_warning ("Validator «%s» not found.", type); + } + } cur = cur->next; } @@ -393,6 +436,61 @@ zak_form_form_add_element (ZakFormForm *zakform, ZakFormElement *element) return ret; } +/** + * zak_form_form_get_element_by_id: + * @zakform: + * @id: + * + * Returns: the #ZakFormElement with @id. + */ +ZakFormElement +*zak_form_form_get_element_by_id (ZakFormForm *zakform, const gchar *id) +{ + ZakFormElement *ret; + + ZakFormFormPrivate *priv; + + guint i; + + priv = zak_form_form_get_instance_private (zakform); + + ret = NULL; + for (i = 0; i < priv->ar_elements->len; i++) + { + ZakFormElement *element = (ZakFormElement *)g_ptr_array_index (priv->ar_elements, i); + + if (g_strcmp0 (zak_form_element_get_name (element), id) == 0) + { + ret = element; + break; + } + } + + return ret; +} + +/** + * zak_form_form_add_validator: + * @zakform: + * @validator: + * + * Returns: #TRUE if @validator is added; FALSE otherwise. + */ +gboolean +zak_form_form_add_validator (ZakFormForm *zakform, ZakFormValidator *validator) +{ + gboolean ret; + + ZakFormFormPrivate *priv; + + priv = zak_form_form_get_instance_private (zakform); + + g_ptr_array_add (priv->ar_validators, g_object_ref (validator)); + ret = TRUE; + + return ret; +} + /** * zak_form_form_clear: * @zakform: a #ZakFormForm object. diff --git a/src/form.h b/src/form.h index 2c882a8..38780f1 100644 --- a/src/form.h +++ b/src/form.h @@ -26,6 +26,7 @@ #include "formelement.h" #include "formiprovider.h" +#include "formvalidator.h" G_BEGIN_DECLS @@ -48,6 +49,9 @@ gboolean zak_form_form_load_from_xml (ZakFormForm *zakform, xmlDoc *xmldoc); gboolean zak_form_form_load_from_file (ZakFormForm *zakform, const gchar *filename); gboolean zak_form_form_add_element (ZakFormForm *zakform, ZakFormElement *element); +ZakFormElement *zak_form_form_get_element_by_id (ZakFormForm *zakform, const gchar *id); + +gboolean zak_form_form_add_validator (ZakFormForm *zakform, ZakFormValidator *validator); void zak_form_form_clear (ZakFormForm *zakform); diff --git a/src/formvalidator.c b/src/formvalidator.c new file mode 100644 index 0000000..719a9a1 --- /dev/null +++ b/src/formvalidator.c @@ -0,0 +1,186 @@ +/* + * 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 "formvalidator.h" + +enum + { + PROP_0, + PROP_MESSAGE + }; + +static void zak_form_validator_class_init (ZakFormValidatorClass *class); +static void zak_form_validator_init (ZakFormValidator *zak_form_validator); + +static void zak_form_validator_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void zak_form_validator_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static void zak_form_validator_dispose (GObject *gobject); +static void zak_form_validator_finalize (GObject *gobject); + +typedef struct + { + gchar *message; + } ZakFormValidatorPrivate; + +G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ZakFormValidator, zak_form_validator, G_TYPE_OBJECT) + +static void +zak_form_validator_class_init (ZakFormValidatorClass *class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (class); + + object_class->set_property = zak_form_validator_set_property; + object_class->get_property = zak_form_validator_get_property; + object_class->dispose = zak_form_validator_dispose; + object_class->finalize = zak_form_validator_finalize; + + g_object_class_install_property (object_class, PROP_MESSAGE, + g_param_spec_string ("message", + "Message", + "Message", + "Invalid value", + G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); +} + +static void +zak_form_validator_init (ZakFormValidator *zak_form_validator) +{ + ZakFormValidatorPrivate *priv = zak_form_validator_get_instance_private (zak_form_validator); +} + +gboolean +zak_form_validator_validate (ZakFormValidator *self, GPtrArray *ar_elements) +{ + gboolean ret; + + g_return_val_if_fail (ZAK_FORM_IS_VALIDATOR (self), FALSE); + + ret = TRUE; + if (ZAK_FORM_VALIDATOR_GET_CLASS (self)->validate != NULL) + { + ret = ZAK_FORM_VALIDATOR_GET_CLASS (self)->validate (self, ar_elements); + } + return ret; +} + +/** + * zak_form_validator_set_message: + * @validator: + * @message: + * + */ +void +zak_form_validator_set_message (ZakFormValidator *validator, + const gchar *message) +{ + ZakFormValidatorPrivate *priv = zak_form_validator_get_instance_private (validator); + + priv->message = g_strdup (message); +} + +/** + * zak_form_validator_get_message: + * @validator: + * + * Returns: + */ +gchar +*zak_form_validator_get_message (ZakFormValidator *validator) +{ + ZakFormValidatorPrivate *priv = zak_form_validator_get_instance_private (validator); + + return g_strdup (priv->message); +} + +/* PRIVATE */ +static void +zak_form_validator_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + ZakFormValidator *zak_form_validator = (ZakFormValidator *)object; + ZakFormValidatorPrivate *priv = zak_form_validator_get_instance_private (zak_form_validator); + + switch (property_id) + { + case PROP_MESSAGE: + zak_form_validator_set_message (zak_form_validator, g_value_dup_string (value)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_form_validator_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) +{ + ZakFormValidator *zak_form_validator = (ZakFormValidator *)object; + ZakFormValidatorPrivate *priv = zak_form_validator_get_instance_private (zak_form_validator); + + switch (property_id) + { + case PROP_MESSAGE: + g_value_set_string (value, zak_form_validator_get_message (zak_form_validator)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_form_validator_dispose (GObject *gobject) +{ + ZakFormValidator *zak_form_validator = (ZakFormValidator *)gobject; + ZakFormValidatorPrivate *priv = zak_form_validator_get_instance_private (zak_form_validator); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->dispose (gobject); +} + +static void +zak_form_validator_finalize (GObject *gobject) +{ + ZakFormValidator *zak_form_validator = (ZakFormValidator *)gobject; + ZakFormValidatorPrivate *priv = zak_form_validator_get_instance_private (zak_form_validator); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->finalize (gobject); +} diff --git a/src/formvalidator.h b/src/formvalidator.h new file mode 100644 index 0000000..d94494e --- /dev/null +++ b/src/formvalidator.h @@ -0,0 +1,48 @@ +/* + * 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_VALIDATOR_H_ +#define _ZAK_FORM_VALIDATOR_H_ + +#include + + +G_BEGIN_DECLS + + +#define ZAK_FORM_TYPE_VALIDATOR zak_form_validator_get_type () +G_DECLARE_DERIVABLE_TYPE (ZakFormValidator, zak_form_validator, ZAK_FORM, VALIDATOR, GObject) + +struct _ZakFormValidatorClass +{ + GObjectClass parent_class; + + gboolean (*validate) (ZakFormValidator *self, GPtrArray *ar_elements); +}; + +gboolean zak_form_validator_validate (ZakFormValidator *self, GPtrArray *ar_elements); + +void zak_form_validator_set_message (ZakFormValidator *validator, + const gchar *message); +gchar *zak_form_validator_get_message (ZakFormValidator *validator); + + +G_END_DECLS + + +#endif /* _ZAK_FORM_VALIDATOR_H_ */ diff --git a/src/libzakform.h b/src/libzakform.h index 8274501..b5aaf41 100644 --- a/src/libzakform.h +++ b/src/libzakform.h @@ -35,5 +35,7 @@ #include +#include + #endif /* __LIBZAKFORM_H__ */ -- 2.49.0