From db5a0683cc10f719be23ba7e82ce0ffba8967e24 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sun, 29 Jan 2017 10:10:47 +0100 Subject: [PATCH] Added and managed property 'enabled' to validators and filters. --- src/formelement.c | 42 +++++++------------------ src/formelementfilter.c | 61 +++++++++++++++++++++++++++++++++--- src/formelementfilter.h | 5 ++- src/formelementvalidator.c | 64 ++++++++++++++++++++++++++++++++------ src/formelementvalidator.h | 5 ++- src/formvalidator.c | 64 +++++++++++++++++++++++++++++++++----- src/formvalidator.h | 4 ++- 7 files changed, 190 insertions(+), 55 deletions(-) diff --git a/src/formelement.c b/src/formelement.c index ccf762a..bfc4e20 100644 --- a/src/formelement.c +++ b/src/formelement.c @@ -199,8 +199,8 @@ zak_form_element_init (ZakFormElement *zak_form_element) priv->to_load = TRUE; priv->to_save = TRUE; - priv->pa_filters = NULL; - priv->pa_validators = NULL; + priv->pa_filters = g_ptr_array_new (); + priv->pa_validators = g_ptr_array_new (); priv->pa_messages = NULL; } @@ -217,11 +217,6 @@ zak_form_element_add_filter (ZakFormElement *element, ZakFormElementFilter *filt priv = zak_form_element_get_instance_private (element); - if (priv->pa_filters == NULL) - { - priv->pa_filters = g_ptr_array_new (); - } - g_ptr_array_add (priv->pa_filters, filter); } @@ -239,11 +234,6 @@ zak_form_element_filter (ZakFormElement *element) priv = zak_form_element_get_instance_private (element); - if (priv->pa_filters == NULL) - { - return; - } - for (i = 0; i < priv->pa_filters->len; i++) { gchar *value; @@ -1045,11 +1035,6 @@ zak_form_element_add_validator (ZakFormElement *element, ZakFormElementValidator priv = zak_form_element_get_instance_private (element); - if (priv->pa_validators == NULL) - { - priv->pa_validators = g_ptr_array_new (); - } - g_ptr_array_add (priv->pa_validators, validator); } @@ -1080,23 +1065,20 @@ zak_form_element_is_valid (ZakFormElement *element) priv->pa_messages = NULL; } - if (priv->pa_validators != NULL) - { - value = zak_form_element_get_value (element); + value = zak_form_element_get_value (element); - for (i = 0; i < priv->pa_validators->len; i++) + for (i = 0; i < priv->pa_validators->len; i++) + { + ZakFormElementValidator *validator = (ZakFormElementValidator *)g_ptr_array_index (priv->pa_validators, i); + if (!zak_form_element_validator_validate (validator, value)) { - ZakFormElementValidator *validator = (ZakFormElementValidator *)g_ptr_array_index (priv->pa_validators, i); - if (!zak_form_element_validator_validate (validator, value)) + if (priv->pa_messages == NULL) { - if (priv->pa_messages == NULL) - { - priv->pa_messages = g_ptr_array_new (); - } - g_ptr_array_add (priv->pa_messages, (gpointer)g_strdup (zak_form_element_validator_get_message (validator))); - - ret = FALSE; + priv->pa_messages = g_ptr_array_new (); } + g_ptr_array_add (priv->pa_messages, (gpointer)g_strdup (zak_form_element_validator_get_message (validator))); + + ret = FALSE; } } diff --git a/src/formelementfilter.c b/src/formelementfilter.c index ba30a32..fe3ce9a 100644 --- a/src/formelementfilter.c +++ b/src/formelementfilter.c @@ -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 @@ -22,6 +22,12 @@ #include "formelementfilter.h" +enum + { + PROP_0, + PROP_ENABLED + }; + static void zak_form_element_filter_class_init (ZakFormElementFilterClass *class); static void zak_form_element_filter_init (ZakFormElementFilter *zak_form_element_filter); @@ -39,10 +45,10 @@ static void zak_form_element_filter_finalize (GObject *gobject); typedef struct { - gpointer nothing; + gboolean enabled; } ZakFormElementFilterPrivate; -G_DEFINE_ABSTRACT_TYPE (ZakFormElementFilter, zak_form_element_filter, G_TYPE_OBJECT) +G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ZakFormElementFilter, zak_form_element_filter, G_TYPE_OBJECT) static void zak_form_element_filter_class_init (ZakFormElementFilterClass *class) @@ -53,6 +59,13 @@ zak_form_element_filter_class_init (ZakFormElementFilterClass *class) object_class->get_property = zak_form_element_filter_get_property; object_class->dispose = zak_form_element_filter_dispose; object_class->finalize = zak_form_element_filter_finalize; + + g_object_class_install_property (object_class, PROP_ENABLED, + g_param_spec_boolean ("enabled", + "Enabled", + "Enabled", + TRUE, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); } static void @@ -68,17 +81,47 @@ gchar g_return_val_if_fail (ZAK_FORM_IS_ELEMENT_FILTER (self), g_strdup ("")); - if (ZAK_FORM_ELEMENT_FILTER_GET_CLASS (self)->filter != NULL) + if (zak_form_element_filter_get_enabled (self) + && ZAK_FORM_ELEMENT_FILTER_GET_CLASS (self)->filter != NULL) { ret = ZAK_FORM_ELEMENT_FILTER_GET_CLASS (self)->filter (self, value); } else { - ret = g_strdup (""); + ret = g_strdup (value); } + return ret; } +/** + * zak_form_element_filter_get_enabled: + * @filter: + * + * Returns: whether @filter is enabled. + */ +gboolean +zak_form_element_filter_get_enabled (ZakFormElementFilter *filter) +{ + ZakFormElementFilterPrivate *priv = zak_form_element_filter_get_instance_private (filter); + + return priv->enabled; +} + +/** + * zak_form_element_filter_set_enabled: + * @filter: + * @enabled: + * + */ +void +zak_form_element_filter_set_enabled (ZakFormElementFilter *filter, gboolean enabled) +{ + ZakFormElementFilterPrivate *priv = zak_form_element_filter_get_instance_private (filter); + + priv->enabled = enabled; +} + /* PRIVATE */ static void zak_form_element_filter_set_property (GObject *object, @@ -91,6 +134,10 @@ zak_form_element_filter_set_property (GObject *object, switch (property_id) { + case PROP_ENABLED: + zak_form_element_filter_set_enabled (zak_form_element_filter, g_value_get_boolean (value)); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; @@ -108,6 +155,10 @@ zak_form_element_filter_get_property (GObject *object, switch (property_id) { + case PROP_ENABLED: + g_value_set_boolean (value, zak_form_element_filter_get_enabled (zak_form_element_filter)); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; diff --git a/src/formelementfilter.h b/src/formelementfilter.h index cc92929..1dffefb 100644 --- a/src/formelementfilter.h +++ b/src/formelementfilter.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 @@ -37,6 +37,9 @@ struct _ZakFormElementFilterClass gchar *zak_form_element_filter_filter (ZakFormElementFilter *self, const gchar *value); +gboolean zak_form_element_filter_get_enabled (ZakFormElementFilter *filter); +void zak_form_element_filter_set_enabled (ZakFormElementFilter *filter, gboolean enabled); + G_END_DECLS diff --git a/src/formelementvalidator.c b/src/formelementvalidator.c index cf0b8e0..65fb8db 100644 --- a/src/formelementvalidator.c +++ b/src/formelementvalidator.c @@ -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 @@ -27,6 +27,7 @@ enum { PROP_0, + PROP_ENABLED, PROP_MESSAGE }; @@ -47,7 +48,8 @@ static void zak_form_element_validator_finalize (GObject *gobject); typedef struct { - gchar *message; + gboolean enabled; + gchar *message; } ZakFormElementValidatorPrivate; G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ZakFormElementValidator, zak_form_element_validator, G_TYPE_OBJECT) @@ -62,12 +64,19 @@ 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_ENABLED, + 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 @@ -84,7 +93,8 @@ zak_form_element_validator_validate (ZakFormElementValidator *self, const gchar g_return_val_if_fail (ZAK_FORM_IS_ELEMENT_VALIDATOR (self), FALSE); ret = TRUE; - if (ZAK_FORM_ELEMENT_VALIDATOR_GET_CLASS (self)->validate != NULL) + if (zak_form_element_validator_get_enabled (self) + && ZAK_FORM_ELEMENT_VALIDATOR_GET_CLASS (self)->validate != NULL) { ret = ZAK_FORM_ELEMENT_VALIDATOR_GET_CLASS (self)->validate (self, value); } @@ -120,6 +130,34 @@ gchar return g_strdup (priv->message); } +/** + * zak_form_element_validator_get_enabled: + * @validator: + * + * Returns: whether @validator is enabled. + */ +gboolean +zak_form_element_validator_get_enabled (ZakFormElementValidator *validator) +{ + ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (validator); + + return priv->enabled; +} + +/** + * zak_form_element_validator_set_enabled: + * @validator: + * @enabled: + * + */ +void +zak_form_element_validator_set_enabled (ZakFormElementValidator *validator, gboolean enabled) +{ + ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (validator); + + priv->enabled = enabled; +} + /* PRIVATE */ static void zak_form_element_validator_set_property (GObject *object, @@ -132,8 +170,12 @@ zak_form_element_validator_set_property (GObject *object, switch (property_id) { + case PROP_ENABLED: + zak_form_element_validator_set_enabled (zak_form_element_validator, g_value_get_boolean (value)); + break; + case PROP_MESSAGE: - zak_form_element_validator_set_message (zak_form_element_validator, g_value_dup_string (value)); + zak_form_element_validator_set_message (zak_form_element_validator, g_value_dup_string (value)); break; default: @@ -153,6 +195,10 @@ zak_form_element_validator_get_property (GObject *object, switch (property_id) { + case PROP_ENABLED: + g_value_set_boolean (value, zak_form_element_validator_get_enabled (zak_form_element_validator)); + break; + case PROP_MESSAGE: g_value_set_string (value, zak_form_element_validator_get_message (zak_form_element_validator)); break; diff --git a/src/formelementvalidator.h b/src/formelementvalidator.h index 06ab451..2ef9407 100644 --- a/src/formelementvalidator.h +++ b/src/formelementvalidator.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 @@ -41,6 +41,9 @@ void zak_form_element_validator_set_message (ZakFormElementValidator *validator, const gchar *message); gchar *zak_form_element_validator_get_message (ZakFormElementValidator *validator); +gboolean zak_form_element_validator_get_enabled (ZakFormElementValidator *validator); +void zak_form_element_validator_set_enabled (ZakFormElementValidator *validator, gboolean enabled); + G_END_DECLS diff --git a/src/formvalidator.c b/src/formvalidator.c index 56b1d7c..eba0131 100644 --- a/src/formvalidator.c +++ b/src/formvalidator.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 @@ -20,11 +20,14 @@ #include #endif +#include + #include "formvalidator.h" enum { PROP_0, + PROP_ENABLED, PROP_MESSAGE }; @@ -45,7 +48,8 @@ static void zak_form_validator_finalize (GObject *gobject); typedef struct { - gchar *message; + gboolean enabled; + gchar *message; } ZakFormValidatorPrivate; G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ZakFormValidator, zak_form_validator, G_TYPE_OBJECT) @@ -60,12 +64,19 @@ 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_ENABLED, + 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 @@ -82,7 +93,8 @@ zak_form_validator_validate (ZakFormValidator *self) g_return_val_if_fail (ZAK_FORM_IS_VALIDATOR (self), FALSE); ret = TRUE; - if (ZAK_FORM_VALIDATOR_GET_CLASS (self)->validate != NULL) + if (zak_form_validator_get_enabled (self) + && ZAK_FORM_VALIDATOR_GET_CLASS (self)->validate != NULL) { ret = ZAK_FORM_VALIDATOR_GET_CLASS (self)->validate (self); } @@ -118,6 +130,34 @@ gchar return g_strdup (priv->message); } +/** + * zak_form_validator_get_enabled: + * @validator: + * + * Returns: whether @validator is enabled. + */ +gboolean +zak_form_validator_get_enabled (ZakFormValidator *validator) +{ + ZakFormValidatorPrivate *priv = zak_form_validator_get_instance_private (validator); + + return priv->enabled; +} + +/** + * zak_form_validator_set_enabled: + * @validator: + * @enabled: + * + */ +void +zak_form_validator_set_enabled (ZakFormValidator *validator, gboolean enabled) +{ + ZakFormValidatorPrivate *priv = zak_form_validator_get_instance_private (validator); + + priv->enabled = enabled; +} + /* PRIVATE */ static void zak_form_validator_set_property (GObject *object, @@ -130,6 +170,10 @@ zak_form_validator_set_property (GObject *object, switch (property_id) { + case PROP_ENABLED: + zak_form_validator_set_enabled (zak_form_validator, g_value_get_boolean (value)); + break; + case PROP_MESSAGE: zak_form_validator_set_message (zak_form_validator, g_value_dup_string (value)); break; @@ -151,6 +195,10 @@ zak_form_validator_get_property (GObject *object, switch (property_id) { + case PROP_ENABLED: + g_value_set_boolean (value, zak_form_validator_get_enabled (zak_form_validator)); + break; + case PROP_MESSAGE: g_value_set_string (value, zak_form_validator_get_message (zak_form_validator)); break; diff --git a/src/formvalidator.h b/src/formvalidator.h index c80677b..c4586ec 100644 --- a/src/formvalidator.h +++ b/src/formvalidator.h @@ -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 @@ -41,6 +41,8 @@ void zak_form_validator_set_message (ZakFormValidator *validator, const gchar *message); gchar *zak_form_validator_get_message (ZakFormValidator *validator); +gboolean zak_form_validator_get_enabled (ZakFormValidator *validator); +void zak_form_validator_set_enabled (ZakFormValidator *validator, gboolean enabled); G_END_DECLS -- 2.49.0