libzakform_la_SOURCES = \
form.c \
+ formelement.c \
formelementifilter.c \
formelementfiltertrim.c \
formelementivalidator.c \
libzakform_include_HEADERS = \
libzakform.h \
form.h \
+ formelement.h \
formelementifilter.h \
formelementfiltertrim.h \
formelementivalidator.h \
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
--- /dev/null
+/*
+ * 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);
+}
--- /dev/null
+/*
+ * 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__ */
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
{
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;
}
/* 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);
{
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
}
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);
{
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
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
{
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;
}
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
{
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;
}
#include <libzakform/form.h>
+#include <libzakform/formelement.h>
+
#include <libzakform/formelementifilter.h>
#include <libzakform/formelementfiltertrim.h>