]> saetta.ns0.it Git - zakform/libzakform/commitdiff
Added property id to ZakFormValidator (refs #1102).
authorAndrea Zagli <azagli@libero.it>
Sun, 16 Apr 2017 08:44:11 +0000 (10:44 +0200)
committerAndrea Zagli <azagli@libero.it>
Sun, 16 Apr 2017 08:44:11 +0000 (10:44 +0200)
Refactored ZakFormValidator::xml_parsing.

src/form.c
src/formvalidator.c
src/formvalidator.h
src/formvalidatorcompare.c
src/formvalidatorcompare.h
src/formvalidatorcomparedate.c
src/formvalidatorcomparedate.h

index c1b54cffdc16d9a3abb6f6a1f45bed5b8bd5002d..f87771ba0e63bf9371c4c8d5f6e7b3bf26415dcf 100644 (file)
@@ -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;
                                                                                                                }
index eba01317dd82bdebd87dead54fa5e2aeed55b395..36216fdcfc03e955d2602167e02938247a60c4e6 100644 (file)
@@ -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;
index c4586ec2f5aa6f7fa329497a687960d76940dfb3..a5b43b5533d702f62cc9d04e5e9fbe1e0bc3cdec 100644 (file)
@@ -21,6 +21,8 @@
 
 #include <glib-object.h>
 
+#include <libxml/xpath.h>
+
 
 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);
index 901306ba461854b092d4ed2d2ee54706b6fc7e1c..edaf6f2be4e76afe4176b8cffb6632a1bbb60754 100644 (file)
@@ -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;
index 02139d06d7e05ab0a6fba88109eff3db343d10e0..07b48bb13b31f93768e42515c02a89a6a06f8918 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2016-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
@@ -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
index 78e436064028f505e3770d6c0d6a2c3e05cfdbb4..b7df370b4b84a03f68676e0c9645153057cea920 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
@@ -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);
index c8940caa34d3cc8e26d0fad6ce4ce639da5db423..52fac525a9676fd3645585eaf7aa06f3cccb5d0b 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
@@ -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