]> saetta.ns0.it Git - zakform/libzakform/commitdiff
Added class FormElement (abstract).
authorAndrea Zagli <azagli@libero.it>
Wed, 28 Oct 2015 21:11:20 +0000 (22:11 +0100)
committerAndrea Zagli <azagli@libero.it>
Wed, 28 Oct 2015 21:11:20 +0000 (22:11 +0100)
12 files changed:
src/Makefile.am
src/form.h
src/formelement.c [new file with mode: 0644]
src/formelement.h [new file with mode: 0644]
src/formelementfiltertrim.c
src/formelementifilter.c
src/formelementifilter.h
src/formelementivalidator.c
src/formelementivalidator.h
src/formelementvalidatornotempty.c
src/formelementvalidatorregex.c
src/libzakform.h

index e2db130d21d5734ab3a61759ff9b233e0e1748de..ff33003326d50cb7777bc9bbd53d12300951e9a6 100644 (file)
@@ -7,6 +7,7 @@ lib_LTLIBRARIES = libzakform.la
 
 libzakform_la_SOURCES = \
                         form.c \
+                        formelement.c \
                         formelementifilter.c \
                         formelementfiltertrim.c \
                         formelementivalidator.c \
@@ -18,6 +19,7 @@ libzakform_la_LDFLAGS = -no-undefined
 libzakform_include_HEADERS = \
                              libzakform.h \
                              form.h \
+                             formelement.h \
                              formelementifilter.h \
                              formelementfiltertrim.h \
                              formelementivalidator.h \
index c166f678db8736c2a39e06a99dea448996fb355e..e7500552cc180287b9a1822b6ac30eb4aca75967 100644 (file)
@@ -26,7 +26,7 @@
 G_BEGIN_DECLS
 
 
-#define ZAK_TYPE_FORM_FORM zak_form_form_get_type ()
+#define ZAK_FORM_TYPE_FORM zak_form_form_get_type ()
 G_DECLARE_DERIVABLE_TYPE (ZakFormForm, zak_form_form, ZAK_FORM, FORM, GObject)
 
 struct _ZakFormFormClass
diff --git a/src/formelement.c b/src/formelement.c
new file mode 100644 (file)
index 0000000..feb0345
--- /dev/null
@@ -0,0 +1,279 @@
+/*
+ * Copyright (C) 2015 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
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifdef HAVE_CONFIG_H
+       #include <config.h>
+#endif
+
+#include "formelement.h"
+
+enum
+{
+       PROP_0
+};
+
+static void zak_form_element_class_init (ZakFormElementClass *class);
+static void zak_form_element_init (ZakFormElement *zak_form_element);
+
+static void zak_form_element_construct (ZakFormElement *element,
+                                                                                       const gchar *id,
+                                                                                       GHashTable *ht_attrs);
+
+static void zak_form_element_set_property (GObject *object,
+                               guint property_id,
+                               const GValue *value,
+                               GParamSpec *pspec);
+static void zak_form_element_get_property (GObject *object,
+                               guint property_id,
+                               GValue *value,
+                               GParamSpec *pspec);
+
+static void zak_form_element_dispose (GObject *gobject);
+static void zak_form_element_finalize (GObject *gobject);
+
+typedef struct
+       {
+               gchar *value;
+               GPtrArray *pa_filters;
+               GPtrArray *pa_validators;
+       } ZakFormElementPrivate;
+
+G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ZakFormElement, zak_form_element, G_TYPE_OBJECT)
+
+static void
+zak_form_element_class_init (ZakFormElementClass *class)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (class);
+
+       object_class->set_property = zak_form_element_set_property;
+       object_class->get_property = zak_form_element_get_property;
+       object_class->dispose = zak_form_element_dispose;
+       object_class->finalize = zak_form_element_finalize;
+}
+
+static void
+zak_form_element_init (ZakFormElement *zak_form_element)
+{
+       ZakFormElementPrivate *priv = zak_form_element_get_instance_private (zak_form_element);
+
+       priv->value = NULL;
+       priv->pa_filters = NULL;
+       priv->pa_validators = NULL;
+}
+
+/**
+ * zak_form_element_add_filter:
+ * @element:
+ * @filter:
+ *
+ */
+void
+zak_form_element_add_filter (ZakFormElement *element, ZakFormElementIFilter *filter)
+{
+       ZakFormElementPrivate *priv;
+
+       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);
+}
+
+/**
+ * zak_form_element_filter:
+ * @element:
+ *
+ */
+void
+zak_form_element_filter (ZakFormElement *element)
+{
+       guint i;
+
+       ZakFormElementPrivate *priv;
+
+       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;
+                       gchar *val;
+
+                       value = zak_form_element_get_value (element);
+
+                       val = zak_form_element_ifilter_filter ((ZakFormElementIFilter *)g_ptr_array_index (priv->pa_filters, i),
+                                                                                                          value);
+                       zak_form_element_set_value (element, val);
+               }
+}
+
+/**
+ * zak_form_element_set_value:
+ * @element:
+ * @value:
+ *
+ */
+void
+zak_form_element_set_value (ZakFormElement *element, const gchar *value)
+{
+       ZakFormElementPrivate *priv;
+
+       priv = zak_form_element_get_instance_private (element);
+
+       priv->value = g_strdup (value);
+}
+
+/**
+ * zak_form_element_get_value:
+ * @element:
+ *
+ */
+gchar
+*zak_form_element_get_value (ZakFormElement *element)
+{
+       ZakFormElementPrivate *priv;
+
+       priv = zak_form_element_get_instance_private (element);
+
+       return g_strdup (priv->value);
+}
+
+/**
+ * zak_form_element_add_validator:
+ * @element:
+ * @validator:
+ *
+ */
+void
+zak_form_element_add_validator (ZakFormElement *element, ZakFormElementIValidator *validator)
+{
+       ZakFormElementPrivate *priv;
+
+       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);
+}
+
+/**
+ * zak_form_element_is_valid:
+ * @element:
+ *
+ */
+gboolean
+zak_form_element_is_valid (ZakFormElement *element)
+{
+       gboolean ret;
+
+       gchar *value;
+       guint i;
+
+       ZakFormElementPrivate *priv;
+
+       priv = zak_form_element_get_instance_private (element);
+
+       ret = TRUE;
+
+       zak_form_element_filter (element);
+
+       if (priv->pa_validators != NULL)
+               {
+                       value = zak_form_element_get_value (element);
+
+                       for (i = 0; i < priv->pa_validators->len; i++)
+                               {
+                                       if (!zak_form_element_ivalidator_validate ((ZakFormElementIValidator *)g_ptr_array_index (priv->pa_validators, i),
+                                                                                                                                  value))
+                                               {
+                                                       ret = FALSE;
+                                               }
+                               }
+               }
+
+       return ret;
+}
+
+/* PRIVATE */
+static void
+zak_form_element_set_property (GObject *object,
+                   guint property_id,
+                   const GValue *value,
+                   GParamSpec *pspec)
+{
+       ZakFormElement *zak_form_element = (ZakFormElement *)object;
+       ZakFormElementPrivate *priv = zak_form_element_get_instance_private (zak_form_element);
+
+       switch (property_id)
+               {
+                       default:
+                               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                               break;
+               }
+}
+
+static void
+zak_form_element_get_property (GObject *object,
+                   guint property_id,
+                   GValue *value,
+                   GParamSpec *pspec)
+{
+       ZakFormElement *zak_form_element = (ZakFormElement *)object;
+       ZakFormElementPrivate *priv = zak_form_element_get_instance_private (zak_form_element);
+
+       switch (property_id)
+               {
+                       default:
+                               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                               break;
+               }
+}
+
+static void
+zak_form_element_dispose (GObject *gobject)
+{
+       ZakFormElement *zak_form_element = (ZakFormElement *)gobject;
+       ZakFormElementPrivate *priv = zak_form_element_get_instance_private (zak_form_element);
+
+
+
+       GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject));
+       parent_class->dispose (gobject);
+}
+
+static void
+zak_form_element_finalize (GObject *gobject)
+{
+       ZakFormElement *zak_form_element = (ZakFormElement *)gobject;
+       ZakFormElementPrivate *priv = zak_form_element_get_instance_private (zak_form_element);
+
+
+
+       GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject));
+       parent_class->finalize (gobject);
+}
diff --git a/src/formelement.h b/src/formelement.h
new file mode 100644 (file)
index 0000000..199a8f8
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2015 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
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef __ZAK_FORM_ELEMENT_H__
+#define __ZAK_FORM_ELEMENT_H__
+
+
+#include <glib-object.h>
+
+#include "formelementifilter.h"
+#include "formelementivalidator.h"
+
+
+G_BEGIN_DECLS
+
+
+#define ZAK_FORM_TYPE_ELEMENT zak_form_element_get_type ()
+G_DECLARE_DERIVABLE_TYPE (ZakFormElement, zak_form_element, ZAK_FORM, ELEMENT, GObject)
+
+struct _ZakFormElementClass
+       {
+               GObjectClass parent_class;
+       };
+
+
+void zak_form_element_set_value (ZakFormElement *element, const gchar *value);
+gchar *zak_form_element_get_value (ZakFormElement *element);
+
+void zak_form_element_add_filter (ZakFormElement *element, ZakFormElementIFilter *filter);
+void zak_form_element_filter (ZakFormElement *element);
+
+void zak_form_element_add_validator (ZakFormElement *element, ZakFormElementIValidator *validator);
+gboolean zak_form_element_is_valid (ZakFormElement *element);
+
+
+G_END_DECLS
+
+
+#endif /* __ZAK_FORM_ELEMENT_H__ */
index 1ee5b2983b071f392bfe48dd0296beafdc5d60e3..f4b22bca90c8fb082ea9cf700027621f3e259946 100644 (file)
@@ -39,7 +39,7 @@ static void zak_form_element_filter_trim_get_property (GObject *object,
 static void zak_form_element_filter_trim_dispose (GObject *gobject);
 static void zak_form_element_filter_trim_finalize (GObject *gobject);
 
-static GValue *zak_form_element_filter_trim_filter (ZakFormElementIFilter *filter_trim, GValue *value);
+static gchar *zak_form_element_filter_trim_filter (ZakFormElementIFilter *filter_trim, const gchar *value);
 
 struct _ZakFormElementFilterTrim
 {
@@ -159,22 +159,18 @@ zak_form_element_filter_trim_finalize (GObject *gobject)
        parent_class->finalize (gobject);
 }
 
-static GValue
+static gchar
 *zak_form_element_filter_trim_filter (ZakFormElementIFilter *filter_trim,
-                                                                         GValue *value)
+                                                                         const gchar *value)
 {
-       GValue *ret;
+       gchar *ret;
        gchar *_value;
 
-       ret = g_new0 (GValue, 1);
-       g_value_init (ret, G_TYPE_STRING);
-       g_value_set_string (ret, g_strdup (""));
+       g_return_val_if_fail (value != NULL, g_strdup (""));
 
-       g_return_val_if_fail (value != NULL, ret);
+       _value = g_strdup (value);
 
-       _value = g_strdup (g_value_get_string (value));
-
-       g_value_set_string (ret, g_strdup (g_strstrip (_value)));
+    ret = g_strdup (g_strstrip (_value));
 
        return ret;
 }
index 30a7259e72976cccf7f9177b6946f7d2fd69b125..22ba445022a5396bed088d3681b8696087e510f4 100644 (file)
@@ -26,8 +26,8 @@ zak_form_element_ifilter_default_init (ZakFormElementIFilterInterface *iface)
     /* add properties and signals to the interface here */
 }
 
-GValue
-*zak_form_element_ifilter_filter (ZakFormElementIFilter *self, GValue *value)
+gchar
+*zak_form_element_ifilter_filter (ZakFormElementIFilter *self, const gchar *value)
 {
        g_return_val_if_fail (ZAK_FORM_IS_ELEMENT_IFILTER (self), NULL);
 
index 10676bed265ec2fc07c6599a304039c9cd7a5daa..f6ff8568157c1f8af892b0247e1926269c9ba4eb 100644 (file)
@@ -32,10 +32,10 @@ struct _ZakFormElementIFilterInterface
 {
        GTypeInterface parent_iface;
 
-       GValue *(*filter) (ZakFormElementIFilter *self, GValue *value);
+       gchar *(*filter) (ZakFormElementIFilter *self, const gchar *value);
 };
 
-GValue *zak_form_element_ifilter_filter (ZakFormElementIFilter *self, GValue *value);
+gchar *zak_form_element_ifilter_filter (ZakFormElementIFilter *self, const gchar *value);
 
 
 G_END_DECLS
index 1b8bc6c34919346cd2b9c036a3eb2dbad496c6b6..8cd670596a8f50c74a2a44fbaa001a0ff91bca5a 100644 (file)
@@ -27,7 +27,7 @@ zak_form_element_ivalidator_default_init (ZakFormElementIValidatorInterface *ifa
 }
 
 gboolean
-zak_form_element_ivalidator_validate (ZakFormElementIValidator *self, GValue *value)
+zak_form_element_ivalidator_validate (ZakFormElementIValidator *self, const gchar *value)
 {
        g_return_val_if_fail (ZAK_FORM_IS_ELEMENT_IVALIDATOR (self), FALSE);
 
index c0d76ec2b68df9cb5a3e530c4c1428c90bf09e34..4ef234dc85469a5b6ab2bff6411ce8eaf1eb6b1b 100644 (file)
@@ -32,10 +32,10 @@ struct _ZakFormElementIValidatorInterface
 {
        GTypeInterface parent_iface;
 
-       gboolean (*validate) (ZakFormElementIValidator *self, GValue *value);
+       gboolean (*validate) (ZakFormElementIValidator *self, const gchar *value);
 };
 
-gboolean zak_form_element_ivalidator_validate (ZakFormElementIValidator *self, GValue *value);
+gboolean zak_form_element_ivalidator_validate (ZakFormElementIValidator *self, const gchar *value);
 
 
 G_END_DECLS
index 37ef0e85d8b27753f1e9e9ec7a1f4e5217578a46..5bd4a6183d4b5217c08b4b6e478a8ad2a9acf966 100644 (file)
@@ -39,7 +39,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_validate (ZakFormElementIValidator *validator_notempty, GValue *value);
+static gboolean zak_form_element_validator_notempty_validate (ZakFormElementIValidator *validator_notempty, const gchar *value);
 
 struct _ZakFormElementValidatorNotempty
 {
@@ -161,13 +161,13 @@ zak_form_element_validator_notempty_finalize (GObject *gobject)
 
 static gboolean
 zak_form_element_validator_notempty_validate (ZakFormElementIValidator *validator_notempty,
-                                                                                 GValue *value)
+                                                                                 const gchar *value)
 {
        gboolean ret;
 
        g_return_val_if_fail (value != NULL, FALSE);
 
-       ret = (g_strcmp0 (g_value_get_string (value), "") != 0);
+       ret = (g_strcmp0 (value, "") != 0);
 
        return ret;
 }
index 94fd0fc7088254f4f466e5b7e0a40cb081c36a51..7243f56479e8fb126030a166af5e02a556f498ff 100644 (file)
@@ -41,7 +41,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_validate (ZakFormElementIValidator *validator_regex, GValue *value);
+static gboolean zak_form_element_validator_regex_validate (ZakFormElementIValidator *validator_regex, const gchar *value);
 
 struct _ZakFormElementValidatorRegex
 {
@@ -173,33 +173,28 @@ zak_form_element_validator_regex_finalize (GObject *gobject)
 
 static gboolean
 zak_form_element_validator_regex_validate (ZakFormElementIValidator *validator_regex,
-                                                                                          GValue *value)
+                                                                                          const gchar *value)
 {
        gboolean ret;
 
        GRegex *regex;
        GError *error;
 
+       g_return_val_if_fail (value != NULL, FALSE);
+
        ZakFormElementValidatorRegexPrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_REGEX_GET_PRIVATE (validator_regex);
 
-       if (G_VALUE_HOLDS (value, G_TYPE_STRING))
-               {
-                       error = NULL;
-                       regex = g_regex_new (priv->regex, 0, 0, &error);
-                       if (regex == NULL
-                               || error != NULL)
-                               {
-                                       syslog (LOG_MAKEPRI(LOG_SYSLOG, LOG_DEBUG), "Error on creating regex: %s.",
-                                                       error->message != NULL ? error->message : "no details");
-                                       return FALSE;
-                               }
-
-                       ret = g_regex_match ((const GRegex *)regex, g_value_get_string (value), 0, NULL);
-               }
-       else
+       error = NULL;
+       regex = g_regex_new (priv->regex, 0, 0, &error);
+       if (regex == NULL
+               || error != NULL)
                {
-                       ret = FALSE;
+                       syslog (LOG_MAKEPRI(LOG_SYSLOG, LOG_DEBUG), "Error on creating regex: %s.",
+                                       error->message != NULL ? error->message : "no details");
+                       return FALSE;
                }
 
+       ret = g_regex_match ((const GRegex *)regex, value, 0, NULL);
+
        return ret;
 }
index 1e85421df1aa0e60dc028ef104871bb35a0cc839..74a8a96c2ed80b39b9207e7b590519958f8e4ae5 100644 (file)
@@ -22,6 +22,8 @@
 
 #include <libzakform/form.h>
 
+#include <libzakform/formelement.h>
+
 #include <libzakform/formelementifilter.h>
 #include <libzakform/formelementfiltertrim.h>