From: Andrea Zagli Date: Sun, 16 Apr 2017 09:17:18 +0000 (+0200) Subject: Added property id to FormElementValidator (refs #1102). X-Git-Tag: v0.1.0~1^2~24 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=55b3895af3b2e39d2e62ef60f99bc1235b53d297;p=zakform%2Flibzakform Added property id to FormElementValidator (refs #1102). Refactored ZakFormElementValidator::xml_parsing. --- diff --git a/src/form.c b/src/form.c index 181fe1b..c58bbeb 100644 --- a/src/form.c +++ b/src/form.c @@ -40,7 +40,6 @@ typedef gboolean (* FormElementExtensionXmlParsingFunc) (GObject *, xmlNodePtr); typedef ZakFormElementFilter *(* FormElementFilterConstructorFunc) (void); typedef gboolean (* FormElementFilterXmlParsingFunc) (ZakFormElementFilter *, xmlNodePtr); typedef ZakFormElementValidator *(* FormElementValidatorConstructorFunc) (void); -typedef gboolean (* FormElementValidatorXmlParsingFunc) (ZakFormElementValidator *, xmlNodePtr); typedef ZakFormValidator *(* FormValidatorConstructorFunc) (void); static void zak_form_form_class_init (ZakFormFormClass *class); @@ -170,7 +169,6 @@ zak_form_form_element_xml_parsing (ZakFormForm *zakform, ZakFormElement *element FormElementFilterConstructorFunc filter_constructor; FormElementFilterXmlParsingFunc filter_xml_parsing; FormElementValidatorConstructorFunc validator_constructor; - FormElementValidatorXmlParsingFunc validator_xml_parsing; gboolean to_unlink; xmlNode *xnode_tmp; @@ -272,15 +270,7 @@ zak_form_form_element_xml_parsing (ZakFormForm *zakform, ZakFormElement *element validator = validator_constructor (); zak_form_element_add_validator (element, 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, xnode); - } - } + zak_form_element_validator_xml_parsing (validator, xnode); break; } diff --git a/src/formelementvalidator.c b/src/formelementvalidator.c index 65fb8db..063b3b2 100644 --- a/src/formelementvalidator.c +++ b/src/formelementvalidator.c @@ -27,6 +27,7 @@ enum { PROP_0, + PROP_ID, PROP_ENABLED, PROP_MESSAGE }; @@ -48,6 +49,7 @@ static void zak_form_element_validator_finalize (GObject *gobject); typedef struct { + gchar *id; gboolean enabled; gchar *message; } ZakFormElementValidatorPrivate; @@ -64,19 +66,26 @@ zak_form_element_validator_class_init (ZakFormElementValidatorClass *class) object_class->dispose = zak_form_element_validator_dispose; object_class->finalize = zak_form_element_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,45 @@ zak_form_element_validator_init (ZakFormElementValidator *zak_form_element_valid ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (zak_form_element_validator); } +gboolean +zak_form_element_validator_xml_parsing (ZakFormElementValidator *self, xmlNode *xnode) +{ + gboolean ret; + + gchar *prop; + + g_return_val_if_fail (ZAK_FORM_IS_ELEMENT_VALIDATOR (self), FALSE); + + ret = TRUE; + + prop = (gchar *)xmlGetProp (xnode, (const xmlChar *)"id"); + if (prop != NULL) + { + zak_form_element_validator_set_id (self, prop); + g_free (prop); + } + + prop = (gchar *)xmlGetProp (xnode, (const xmlChar *)"message"); + if (prop != NULL) + { + zak_form_element_validator_set_message (self, prop); + g_free (prop); + } + + prop = (gchar *)xmlGetProp (xnode, (const xmlChar *)"disabled"); + if (prop != NULL) + { + zak_form_element_validator_set_enabled (self, FALSE); + g_free (prop); + } + + if (ZAK_FORM_ELEMENT_VALIDATOR_GET_CLASS (self)->xml_parsing != NULL) + { + ret = ZAK_FORM_ELEMENT_VALIDATOR_GET_CLASS (self)->xml_parsing (self, xnode); + } + return ret; +} + gboolean zak_form_element_validator_validate (ZakFormElementValidator *self, const gchar *value) { @@ -101,6 +149,42 @@ zak_form_element_validator_validate (ZakFormElementValidator *self, const gchar return ret; } +/** + * zak_form_element_validator_set_id: + * @validator: + * @id: + * + */ +void +zak_form_element_validator_set_id (ZakFormElementValidator *validator, + const gchar *id) +{ + ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (validator); + + if (id == NULL) + { + priv->id = g_strdup (""); + } + else + { + priv->id = g_strdup (id); + } +} + +/** + * zak_form_element_validator_get_id: + * @validator: + * + * Returns: + */ +gchar +*zak_form_element_validator_get_id (ZakFormElementValidator *validator) +{ + ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (validator); + + return g_strdup (priv->id); +} + /** * zak_form_element_validator_set_message: * @validator: @@ -109,7 +193,7 @@ zak_form_element_validator_validate (ZakFormElementValidator *self, const gchar */ void zak_form_element_validator_set_message (ZakFormElementValidator *validator, - const gchar *message) + const gchar *message) { ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (validator); @@ -127,7 +211,7 @@ gchar { ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (validator); - return g_strdup (priv->message); + return g_strdup (priv->message); } /** @@ -161,15 +245,19 @@ zak_form_element_validator_set_enabled (ZakFormElementValidator *validator, gboo /* PRIVATE */ static void zak_form_element_validator_set_property (GObject *object, - guint property_id, - const GValue *value, - GParamSpec *pspec) + guint property_id, + const GValue *value, + GParamSpec *pspec) { ZakFormElementValidator *zak_form_element_validator = (ZakFormElementValidator *)object; ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (zak_form_element_validator); switch (property_id) { + case PROP_ID: + zak_form_element_validator_set_id (zak_form_element_validator, g_value_dup_string (value)); + break; + case PROP_ENABLED: zak_form_element_validator_set_enabled (zak_form_element_validator, g_value_get_boolean (value)); break; @@ -186,15 +274,19 @@ zak_form_element_validator_set_property (GObject *object, static void zak_form_element_validator_get_property (GObject *object, - guint property_id, - GValue *value, - GParamSpec *pspec) + guint property_id, + GValue *value, + GParamSpec *pspec) { ZakFormElementValidator *zak_form_element_validator = (ZakFormElementValidator *)object; ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (zak_form_element_validator); switch (property_id) { + case PROP_ID: + g_value_set_string (value, zak_form_element_validator_get_id (zak_form_element_validator)); + break; + case PROP_ENABLED: g_value_set_boolean (value, zak_form_element_validator_get_enabled (zak_form_element_validator)); break; diff --git a/src/formelementvalidator.h b/src/formelementvalidator.h index 2ef9407..4f9d9c5 100644 --- a/src/formelementvalidator.h +++ b/src/formelementvalidator.h @@ -21,6 +21,8 @@ #include +#include + G_BEGIN_DECLS @@ -32,13 +34,19 @@ struct _ZakFormElementValidatorClass { GObjectClass parent_class; + gboolean (*xml_parsing) (ZakFormElementValidator *self, xmlNode *xnode); gboolean (*validate) (ZakFormElementValidator *self, const gchar *value); }; +gboolean zak_form_element_validator_xml_parsing (ZakFormElementValidator *self, xmlNode *xnode); gboolean zak_form_element_validator_validate (ZakFormElementValidator *self, const gchar *value); +void zak_form_element_validator_set_id (ZakFormElementValidator *validator, + const gchar *id); +gchar *zak_form_element_validator_get_id (ZakFormElementValidator *validator); + void zak_form_element_validator_set_message (ZakFormElementValidator *validator, - const gchar *message); + const gchar *message); gchar *zak_form_element_validator_get_message (ZakFormElementValidator *validator); gboolean zak_form_element_validator_get_enabled (ZakFormElementValidator *validator); diff --git a/src/formelementvalidatordate.c b/src/formelementvalidatordate.c index df7d0f5..b10bc94 100644 --- a/src/formelementvalidatordate.c +++ b/src/formelementvalidatordate.c @@ -40,6 +40,7 @@ static void zak_form_element_validator_date_get_property (GObject *object, 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_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode); static gboolean zak_form_element_validator_date_validate (ZakFormElementValidator *validator_date, const gchar *value); struct _ZakFormElementValidatorDate @@ -70,6 +71,7 @@ zak_form_element_validator_date_class_init (ZakFormElementValidatorDateClass *cl object_class->dispose = zak_form_element_validator_date_dispose; object_class->finalize = zak_form_element_validator_date_finalize; + parent_class->xml_parsing = zak_form_element_validator_date_xml_parsing; parent_class->validate = zak_form_element_validator_date_validate; g_type_class_add_private (object_class, sizeof (ZakFormElementValidatorDatePrivate)); @@ -135,7 +137,7 @@ gchar * @xnode: * */ -gboolean +static 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)); diff --git a/src/formelementvalidatordate.h b/src/formelementvalidatordate.h index 13b5f38..84cf21c 100644 --- a/src/formelementvalidatordate.h +++ b/src/formelementvalidatordate.h @@ -38,8 +38,6 @@ 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 diff --git a/src/formelementvalidatornotempty.c b/src/formelementvalidatornotempty.c index a3ef4e2..87d976e 100644 --- a/src/formelementvalidatornotempty.c +++ b/src/formelementvalidatornotempty.c @@ -44,6 +44,7 @@ static void zak_form_element_validator_notempty_get_property (GObject *object, static void zak_form_element_validator_notempty_dispose (GObject *gobject); static void zak_form_element_validator_notempty_finalize (GObject *gobject); +static gboolean zak_form_element_validator_notempty_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode); static gboolean zak_form_element_validator_notempty_validate (ZakFormElementValidator *validator_notempty, const gchar *value); struct _ZakFormElementValidatorNotempty @@ -74,16 +75,17 @@ zak_form_element_validator_notempty_class_init (ZakFormElementValidatorNotemptyC object_class->dispose = zak_form_element_validator_notempty_dispose; object_class->finalize = zak_form_element_validator_notempty_finalize; + parent_class->xml_parsing = zak_form_element_validator_notempty_xml_parsing; parent_class->validate = zak_form_element_validator_notempty_validate; g_type_class_add_private (object_class, sizeof (ZakFormElementValidatorNotemptyPrivate)); g_object_class_install_property (object_class, PROP_AS_EMPTY_STRING, - g_param_spec_string ("as-empty-string", - "As empty string", - "As empty string", - "", - G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); + g_param_spec_string ("as-empty-string", + "As empty string", + "As empty string", + "", + G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); } static void @@ -113,7 +115,7 @@ ZakFormElementValidatorNotempty * @xnode: * */ -gboolean +static gboolean zak_form_element_validator_notempty_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode) { ZakFormElementValidatorNotemptyPrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_NOTEMPTY_GET_PRIVATE (validator); diff --git a/src/formelementvalidatornotempty.h b/src/formelementvalidatornotempty.h index 5a6b662..f64d06c 100644 --- a/src/formelementvalidatornotempty.h +++ b/src/formelementvalidatornotempty.h @@ -35,8 +35,6 @@ G_DECLARE_FINAL_TYPE (ZakFormElementValidatorNotempty, zak_form_element_validato ZakFormElementValidatorNotempty *zak_form_element_validator_notempty_new (void); -gboolean zak_form_element_validator_notempty_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode); - void zak_form_element_validator_notempty_set_as_empty_string (ZakFormElementValidatorNotempty *validator, const gchar *as_empty_string); gchar *zak_form_element_validator_notempty_get_as_empty_string (ZakFormElementValidatorNotempty *validator); diff --git a/src/formelementvalidatorregex.c b/src/formelementvalidatorregex.c index a0d5b2b..8614ebf 100644 --- a/src/formelementvalidatorregex.c +++ b/src/formelementvalidatorregex.c @@ -44,6 +44,7 @@ static void zak_form_element_validator_regex_get_property (GObject *object, static void zak_form_element_validator_regex_dispose (GObject *gobject); static void zak_form_element_validator_regex_finalize (GObject *gobject); +static gboolean zak_form_element_validator_regex_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode); static gboolean zak_form_element_validator_regex_validate (ZakFormElementValidator *validator_regex, const gchar *value); struct _ZakFormElementValidatorRegex @@ -74,6 +75,7 @@ zak_form_element_validator_regex_class_init (ZakFormElementValidatorRegexClass * object_class->dispose = zak_form_element_validator_regex_dispose; object_class->finalize = zak_form_element_validator_regex_finalize; + parent_class->xml_parsing = zak_form_element_validator_regex_xml_parsing; parent_class->validate = zak_form_element_validator_regex_validate; g_type_class_add_private (object_class, sizeof (ZakFormElementValidatorRegexPrivate)); @@ -117,7 +119,7 @@ ZakFormElementValidatorRegex * @xnode: * */ -gboolean +static gboolean zak_form_element_validator_regex_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode) { zak_form_element_validator_regex_set_regex (ZAK_FORM_ELEMENT_VALIDATOR_REGEX (validator), (gchar *)xmlNodeGetContent (xnode)); diff --git a/src/formelementvalidatorregex.h b/src/formelementvalidatorregex.h index 4bb8b42..127e1a0 100644 --- a/src/formelementvalidatorregex.h +++ b/src/formelementvalidatorregex.h @@ -34,7 +34,6 @@ G_BEGIN_DECLS G_DECLARE_FINAL_TYPE (ZakFormElementValidatorRegex, zak_form_element_validator_regex, ZAK_FORM, ELEMENT_VALIDATOR_REGEX, ZakFormElementValidator) ZakFormElementValidatorRegex *zak_form_element_validator_regex_new (void); -gboolean zak_form_element_validator_regex_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode); void zak_form_element_validator_regex_set_regex (ZakFormElementValidatorRegex *validator, const gchar *regex); gchar *zak_form_element_validator_regex_get_regex (ZakFormElementValidatorRegex *validator);