]> saetta.ns0.it Git - zakform/libzakform/commitdiff
Added and managed property 'enabled' to validators and filters.
authorAndrea Zagli <azagli@libero.it>
Sun, 29 Jan 2017 09:10:47 +0000 (10:10 +0100)
committerAndrea Zagli <azagli@libero.it>
Sun, 29 Jan 2017 09:10:47 +0000 (10:10 +0100)
src/formelement.c
src/formelementfilter.c
src/formelementfilter.h
src/formelementvalidator.c
src/formelementvalidator.h
src/formvalidator.c
src/formvalidator.h

index ccf762a56ce5c2cef67fec59918a31ee58beabdd..bfc4e20adc654d08dce092af810b0c7c1cecc728 100644 (file)
@@ -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;
                                }
                }
 
index ba30a32bc554e7204e7aec2401b4ae9da2332330..fe3ce9ad7baebb8a0b6e39ae09f3bf1c3360a4d5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-2017 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
 
 #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;
index cc929295604317ff1c8bdea3e2ff14ce0815b657..1dffefbb26352073d99274a6c05b5edf3ff6425e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-2017 Andrea Zagli <azagli@libero.it>
  *
  * 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
 
index cf0b8e08bc560af0c03e932ef3b823103d14c8c5..65fb8db09c5bae1af2f83cbeee0358def04fb00f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-2017 Andrea Zagli <azagli@libero.it>
  *
  * 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;
index 06ab451969af0d9638333d711b380666773d5529..2ef9407dcfcca18b3c04cdba6853407df570ef5b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-2017 Andrea Zagli <azagli@libero.it>
  *
  * 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
 
index 56b1d7c2cd25fa2fa080a2f0b0c3403b42fb5b47..eba01317dd82bdebd87dead54fa5e2aeed55b395 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015-2016 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-2017 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
        #include <config.h>
 #endif
 
+#include <glib/gi18n-lib.h>
+
 #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;
index c80677b66b85eb8460d9256a7b2af5372556b192..c4586ec2f5aa6f7fa329497a687960d76940dfb3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015-2016 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-2017 Andrea Zagli <azagli@libero.it>
  *
  * 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