From: Andrea Zagli Date: Sun, 16 Apr 2017 08:44:11 +0000 (+0200) Subject: Added property id to ZakFormValidator (refs #1102). X-Git-Tag: v0.1.0~1^2~26 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=b2204233b78d32dbd9cdca116b5a65452fd0214e;p=zakform%2Flibzakform Added property id to ZakFormValidator (refs #1102). Refactored ZakFormValidator::xml_parsing. --- diff --git a/src/form.c b/src/form.c index c1b54cf..f87771b 100644 --- a/src/form.c +++ b/src/form.c @@ -42,7 +42,6 @@ typedef gboolean (* FormElementFilterXmlParsingFunc) (ZakFormElementFilter *, xm typedef ZakFormElementValidator *(* FormElementValidatorConstructorFunc) (void); typedef gboolean (* FormElementValidatorXmlParsingFunc) (ZakFormElementValidator *, xmlNodePtr); typedef ZakFormValidator *(* FormValidatorConstructorFunc) (void); -typedef gboolean (* FormValidatorXmlParsingFunc) (ZakFormValidator *, xmlNodePtr, GPtrArray *); static void zak_form_form_class_init (ZakFormFormClass *class); static void zak_form_form_init (ZakFormForm *zak_form_form); @@ -336,7 +335,6 @@ zak_form_form_load_from_xml (ZakFormForm *zakform, xmlDoc *xmldoc) FormElementXmlParsingFunc element_xml_parsing; FormValidatorConstructorFunc validator_constructor; - FormValidatorXmlParsingFunc validator_xml_parsing; xmlXPathContextPtr xpcontext; xmlXPathObjectPtr xpresult; @@ -432,15 +430,7 @@ zak_form_form_load_from_xml (ZakFormForm *zakform, xmlDoc *xmldoc) 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, xnodeset->nodeTab[y], priv->ar_elements); - } - } + zak_form_validator_xml_parsing (validator, xnodeset->nodeTab[y], priv->ar_elements); break; } diff --git a/src/formvalidator.c b/src/formvalidator.c index eba0131..36216fd 100644 --- a/src/formvalidator.c +++ b/src/formvalidator.c @@ -27,6 +27,7 @@ enum { PROP_0, + PROP_ID, PROP_ENABLED, PROP_MESSAGE }; @@ -35,19 +36,20 @@ 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); + guint property_id, + const GValue *value, + GParamSpec *pspec); static void zak_form_validator_get_property (GObject *object, - guint property_id, - GValue *value, - GParamSpec *pspec); + 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 *id; gboolean enabled; gchar *message; } ZakFormValidatorPrivate; @@ -64,19 +66,26 @@ zak_form_validator_class_init (ZakFormValidatorClass *class) object_class->dispose = zak_form_validator_dispose; object_class->finalize = zak_form_validator_finalize; + g_object_class_install_property (object_class, PROP_ID, + g_param_spec_string ("id", + "Id", + "Id", + "", + G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); + g_object_class_install_property (object_class, PROP_ENABLED, - g_param_spec_boolean ("enabled", - "Enabled", - "Enabled", - TRUE, - G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); + g_param_spec_boolean ("enabled", + "Enabled", + "Enabled", + TRUE, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); g_object_class_install_property (object_class, PROP_MESSAGE, - g_param_spec_string ("message", - "Message", - "Message", - _("Invalid value"), - G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); + g_param_spec_string ("message", + "Message", + "Message", + _("Invalid value"), + G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); } static void @@ -85,6 +94,91 @@ zak_form_validator_init (ZakFormValidator *zak_form_validator) ZakFormValidatorPrivate *priv = zak_form_validator_get_instance_private (zak_form_validator); } +/** + * zak_form_validator_xml_parsing: + * @validator: + * @xnode: + * @ar_elements: + * + * Retuns: + */ +gboolean +zak_form_validator_xml_parsing (ZakFormValidator *validator, xmlNode *xnode, GPtrArray *ar_elements) +{ + gboolean ret; + + gchar *prop; + + g_return_val_if_fail (ZAK_FORM_IS_VALIDATOR (validator), FALSE); + + ret = TRUE; + + prop = (gchar *)xmlGetProp (xnode, (const xmlChar *)"id"); + if (prop != NULL) + { + zak_form_validator_set_id (validator, prop); + g_free (prop); + } + + prop = (gchar *)xmlGetProp (xnode, (const xmlChar *)"message"); + if (prop != NULL) + { + zak_form_validator_set_message (validator, prop); + g_free (prop); + } + + prop = (gchar *)xmlGetProp (xnode, (const xmlChar *)"disabled"); + if (prop != NULL) + { + zak_form_validator_set_enabled (validator, FALSE); + g_free (prop); + } + + if (ZAK_FORM_VALIDATOR_GET_CLASS (validator)->xml_parsing != NULL) + { + ret = ZAK_FORM_VALIDATOR_GET_CLASS (validator)->xml_parsing (validator, xnode, ar_elements); + } + + return ret; +} + +/** + * zak_form_validator_set_id: + * @validator: + * @id: + * + */ +void +zak_form_validator_set_id (ZakFormValidator *validator, + const gchar *id) +{ + ZakFormValidatorPrivate *priv = zak_form_validator_get_instance_private (validator); + + if (id == NULL) + { + priv->id = g_strdup (""); + } + else + { + priv->id = g_strdup (id); + } +} + +/** + * zak_form_validator_get_id: + * @validator: + * + * Returns: + */ +gchar +*zak_form_validator_get_id (ZakFormValidator *validator) +{ + ZakFormValidatorPrivate *priv = zak_form_validator_get_instance_private (validator); + + return g_strdup (priv->id); +} + + gboolean zak_form_validator_validate (ZakFormValidator *self) { @@ -109,7 +203,7 @@ zak_form_validator_validate (ZakFormValidator *self) */ void zak_form_validator_set_message (ZakFormValidator *validator, - const gchar *message) + const gchar *message) { ZakFormValidatorPrivate *priv = zak_form_validator_get_instance_private (validator); @@ -127,7 +221,7 @@ gchar { ZakFormValidatorPrivate *priv = zak_form_validator_get_instance_private (validator); - return g_strdup (priv->message); + return g_strdup (priv->message); } /** @@ -170,6 +264,10 @@ zak_form_validator_set_property (GObject *object, switch (property_id) { + case PROP_ID: + zak_form_validator_set_id (zak_form_validator, g_value_dup_string (value)); + break; + case PROP_ENABLED: zak_form_validator_set_enabled (zak_form_validator, g_value_get_boolean (value)); break; @@ -195,6 +293,10 @@ zak_form_validator_get_property (GObject *object, switch (property_id) { + case PROP_ID: + g_value_set_string (value, zak_form_validator_get_id (zak_form_validator)); + break; + case PROP_ENABLED: g_value_set_boolean (value, zak_form_validator_get_enabled (zak_form_validator)); break; diff --git a/src/formvalidator.h b/src/formvalidator.h index c4586ec..a5b43b5 100644 --- a/src/formvalidator.h +++ b/src/formvalidator.h @@ -21,6 +21,8 @@ #include +#include + G_BEGIN_DECLS @@ -32,13 +34,19 @@ struct _ZakFormValidatorClass { GObjectClass parent_class; + gboolean (*xml_parsing) (ZakFormValidator *self, xmlNode *xnode, GPtrArray *ar_elements); gboolean (*validate) (ZakFormValidator *self); }; +gboolean zak_form_validator_xml_parsing (ZakFormValidator *validator, xmlNode *xnode, GPtrArray *ar_elements); + +gchar *zak_form_validator_get_id (ZakFormValidator *validator); +void zak_form_validator_set_id (ZakFormValidator *validator, const gchar *id); + gboolean zak_form_validator_validate (ZakFormValidator *self); void zak_form_validator_set_message (ZakFormValidator *validator, - const gchar *message); + const gchar *message); gchar *zak_form_validator_get_message (ZakFormValidator *validator); gboolean zak_form_validator_get_enabled (ZakFormValidator *validator); diff --git a/src/formvalidatorcompare.c b/src/formvalidatorcompare.c index 901306b..edaf6f2 100644 --- a/src/formvalidatorcompare.c +++ b/src/formvalidatorcompare.c @@ -43,6 +43,7 @@ static void zak_form_validator_compare_get_property (GObject *object, static void zak_form_validator_compare_dispose (GObject *gobject); static void zak_form_validator_compare_finalize (GObject *gobject); +static gboolean zak_form_validator_compare_xml_parsing (ZakFormValidator *validator, xmlNode *xnode, GPtrArray *ar_elements); static gboolean zak_form_validator_compare_validate (ZakFormValidator *validator_notempty); struct _ZakFormValidatorCompare @@ -88,6 +89,7 @@ zak_form_validator_compare_class_init (ZakFormValidatorCompareClass *class) object_class->dispose = zak_form_validator_compare_dispose; object_class->finalize = zak_form_validator_compare_finalize; + parent_class->xml_parsing = zak_form_validator_compare_xml_parsing; parent_class->validate = zak_form_validator_compare_validate; g_type_class_add_private (object_class, sizeof (ZakFormValidatorComparePrivate)); @@ -131,7 +133,7 @@ ZakFormValidatorCompare * @ar_elements: * */ -gboolean +static gboolean zak_form_validator_compare_xml_parsing (ZakFormValidator *validator, xmlNode *xnode, GPtrArray *ar_elements) { gchar *prop; diff --git a/src/formvalidatorcompare.h b/src/formvalidatorcompare.h index 02139d0..07b48bb 100644 --- a/src/formvalidatorcompare.h +++ b/src/formvalidatorcompare.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Andrea Zagli + * Copyright (C) 2016-2017 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -34,7 +34,6 @@ G_BEGIN_DECLS G_DECLARE_FINAL_TYPE (ZakFormValidatorCompare, zak_form_validator_compare, ZAK_FORM, VALIDATOR_COMPARE, ZakFormValidator) ZakFormValidatorCompare *zak_form_validator_compare_new (void); -gboolean zak_form_validator_compare_xml_parsing (ZakFormValidator *validator, xmlNode *xnode, GPtrArray *ar_elements); G_END_DECLS diff --git a/src/formvalidatorcomparedate.c b/src/formvalidatorcomparedate.c index 78e4360..b7df370 100644 --- a/src/formvalidatorcomparedate.c +++ b/src/formvalidatorcomparedate.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015-2016 Andrea Zagli + * Copyright (C) 2015-2017 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -43,6 +43,7 @@ static void zak_form_validator_compare_date_get_property (GObject *object, static void zak_form_validator_compare_date_dispose (GObject *gobject); static void zak_form_validator_compare_date_finalize (GObject *gobject); +static gboolean zak_form_validator_compare_date_xml_parsing (ZakFormValidator *validator, xmlNode *xnode, GPtrArray *ar_elements); static gboolean zak_form_validator_compare_date_validate (ZakFormValidator *validator_notempty); struct _ZakFormValidatorCompareDate @@ -91,6 +92,7 @@ zak_form_validator_compare_date_class_init (ZakFormValidatorCompareDateClass *cl object_class->dispose = zak_form_validator_compare_date_dispose; object_class->finalize = zak_form_validator_compare_date_finalize; + parent_class->xml_parsing = zak_form_validator_compare_date_xml_parsing; parent_class->validate = zak_form_validator_compare_date_validate; g_type_class_add_private (object_class, sizeof (ZakFormValidatorCompareDatePrivate)); @@ -134,7 +136,7 @@ ZakFormValidatorCompareDate * @ar_elements: * */ -gboolean +static gboolean zak_form_validator_compare_date_xml_parsing (ZakFormValidator *validator, xmlNode *xnode, GPtrArray *ar_elements) { gchar *prop; @@ -185,9 +187,9 @@ zak_form_validator_compare_date_xml_parsing (ZakFormValidator *validator, xmlNod /* PRIVATE */ static void zak_form_validator_compare_date_set_property (GObject *object, - guint property_id, - const GValue *value, - GParamSpec *pspec) + guint property_id, + const GValue *value, + GParamSpec *pspec) { ZakFormValidatorCompareDate *validator = (ZakFormValidatorCompareDate *)object; ZakFormValidatorCompareDatePrivate *priv = ZAK_FORM_VALIDATOR_COMPARE_DATE_GET_PRIVATE (validator); @@ -202,9 +204,9 @@ zak_form_validator_compare_date_set_property (GObject *object, static void zak_form_validator_compare_date_get_property (GObject *object, - guint property_id, - GValue *value, - GParamSpec *pspec) + guint property_id, + GValue *value, + GParamSpec *pspec) { ZakFormValidatorCompareDate *validator = (ZakFormValidatorCompareDate *)object; ZakFormValidatorCompareDatePrivate *priv = ZAK_FORM_VALIDATOR_COMPARE_DATE_GET_PRIVATE (validator); diff --git a/src/formvalidatorcomparedate.h b/src/formvalidatorcomparedate.h index c8940ca..52fac52 100644 --- a/src/formvalidatorcomparedate.h +++ b/src/formvalidatorcomparedate.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 Andrea Zagli + * Copyright (C) 2015-2017 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -34,7 +34,6 @@ G_BEGIN_DECLS G_DECLARE_FINAL_TYPE (ZakFormValidatorCompareDate, zak_form_validator_compare_date, ZAK_FORM, VALIDATOR_COMPARE_DATE, ZakFormValidator) ZakFormValidatorCompareDate *zak_form_validator_compare_date_new (void); -gboolean zak_form_validator_compare_date_xml_parsing (ZakFormValidator *validator, xmlNode *xnode, GPtrArray *ar_elements); G_END_DECLS