]> saetta.ns0.it Git - zakform/libzakform/commitdiff
Added ZakFormElementValidatorNumber.
authorAndrea Zagli <azagli@libero.it>
Mon, 13 Apr 2020 14:17:28 +0000 (16:17 +0200)
committerAndrea Zagli <azagli@libero.it>
Mon, 13 Apr 2020 14:17:28 +0000 (16:17 +0200)
src/Makefile.am
src/formelementvalidatornumber.c [new file with mode: 0644]
src/formelementvalidatornumber.h [new file with mode: 0644]

index 4b3af31cfc53b472f4f05326b9cf64d90aac1331..8eed3d2a9defc4fb3ea1771fbc31aaad40d563e5 100644 (file)
@@ -26,6 +26,7 @@ libzakform_la_SOURCES = \
                         formelementvalidator.c \
                         formelementvalidatordate.c \
                         formelementvalidatornotempty.c \
+                        formelementvalidatornumber.c \
                         formelementvalidatorregex.c \
                         formiprovider.c \
                         formvalidator.c \
@@ -48,6 +49,7 @@ gir_include = \
                              formelementvalidator.h \
                              formelementvalidatordate.h \
                              formelementvalidatornotempty.h \
+                             formelementvalidatornumber.h \
                              formelementvalidatorregex.h \
                              formiprovider.h \
                              formvalidator.h \
diff --git a/src/formelementvalidatornumber.c b/src/formelementvalidatornumber.c
new file mode 100644 (file)
index 0000000..2bb4fcc
--- /dev/null
@@ -0,0 +1,298 @@
+/*
+ * Copyright (C) 2020 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 <glib/gi18n-lib.h>
+
+#include <libzakutils/libzakutils.h>
+
+#include "formelementvalidator.h"
+#include "formelementvalidatornumber.h"
+
+static void zak_form_element_validator_number_class_init (ZakFormElementValidatorNumberClass *class);
+static void zak_form_element_validator_number_init (ZakFormElementValidatorNumber *zak_form_element);
+
+static void zak_form_element_validator_number_set_property (GObject *object,
+                                                          guint property_id,
+                                                          const GValue *value,
+                                                          GParamSpec *pspec);
+static void zak_form_element_validator_number_get_property (GObject *object,
+                                                          guint property_id,
+                                                          GValue *value,
+                                                          GParamSpec *pspec);
+
+static void zak_form_element_validator_number_dispose (GObject *gobject);
+static void zak_form_element_validator_number_finalize (GObject *gobject);
+
+static gboolean zak_form_element_validator_number_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode);
+static gboolean zak_form_element_validator_number_validate (ZakFormElementValidator *validator_number, const gchar *value);
+
+struct _ZakFormElementValidatorNumber
+{
+       ZakFormElementValidator parent_instance;
+
+       /* Other members, including private data. */
+};
+
+#define ZAK_FORM_ELEMENT_VALIDATOR_NUMBER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ZAK_FORM_TYPE_ELEMENT_VALIDATOR_NUMBER, ZakFormElementValidatorNumberPrivate))
+
+typedef struct _ZakFormElementValidatorNumberPrivate ZakFormElementValidatorNumberPrivate;
+struct _ZakFormElementValidatorNumberPrivate
+       {
+               ZakFormCompareType compare_type;
+               gdouble compare_value;
+       };
+
+G_DEFINE_TYPE (ZakFormElementValidatorNumber, zak_form_element_validator_number, ZAK_FORM_TYPE_ELEMENT_VALIDATOR)
+
+static void
+zak_form_element_validator_number_class_init (ZakFormElementValidatorNumberClass *class)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (class);
+       ZakFormElementValidatorClass *parent_class = ZAK_FORM_ELEMENT_VALIDATOR_CLASS (class);
+
+       object_class->set_property = zak_form_element_validator_number_set_property;
+       object_class->get_property = zak_form_element_validator_number_get_property;
+       object_class->dispose = zak_form_element_validator_number_dispose;
+       object_class->finalize = zak_form_element_validator_number_finalize;
+
+       parent_class->xml_parsing = zak_form_element_validator_number_xml_parsing;
+       parent_class->validate = zak_form_element_validator_number_validate;
+
+       g_type_class_add_private (object_class, sizeof (ZakFormElementValidatorNumberPrivate));
+}
+
+static void
+zak_form_element_validator_number_init (ZakFormElementValidatorNumber *zak_form_element)
+{
+       ZakFormElementValidatorNumberPrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_NUMBER_GET_PRIVATE (zak_form_element);
+
+       priv->compare_type = ZAK_FORM_COMPARE_TYPE_NONE;
+       priv->compare_value = 0.0;
+}
+
+/**
+ * zak_form_element_validator_number_new:
+ *
+ * Returns: the newly created #ZakFormElementValidatorNumber object.
+ */
+ZakFormElementValidatorNumber
+*zak_form_element_validator_number_new ()
+{
+       ZakFormElementValidatorNumber *zak_form_element_validator_number;
+
+       zak_form_element_validator_number = ZAK_FORM_ELEMENT_VALIDATOR_NUMBER (g_object_new (zak_form_element_validator_number_get_type (), NULL));
+
+       return zak_form_element_validator_number;
+}
+
+/**
+ * zak_form_element_validator_number_set_compare_type:
+ * @validator:
+ * @type:
+ *
+ */
+void
+zak_form_element_validator_number_set_compare_type (ZakFormElementValidatorNumber *validator, ZakFormCompareType type)
+{
+       ZakFormElementValidatorNumberPrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_NUMBER_GET_PRIVATE (validator);
+
+       priv->compare_type = type;
+}
+
+/**
+ * zak_form_element_validator_number_get_compare_type:
+ * @validator:
+ *
+ */
+ZakFormCompareType
+zak_form_element_validator_number_get_compare_type (ZakFormElementValidatorNumber *validator)
+{
+       ZakFormElementValidatorNumberPrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_NUMBER_GET_PRIVATE (validator);
+
+       return priv->compare_type;
+}
+
+/**
+ * zak_form_element_validator_number_set_compare_value:
+ * @validator:
+ * @value:
+ *
+ */
+void
+zak_form_element_validator_number_set_compare_value (ZakFormElementValidatorNumber *validator, const gchar *value)
+{
+       ZakFormElementValidatorNumberPrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_NUMBER_GET_PRIVATE (validator);
+
+       if (value != NULL)
+               {
+                       priv->compare_value = g_strtod (value, NULL);
+               }
+       else
+               {
+                       priv->compare_value = 0.0;
+               }
+}
+
+/**
+ * zak_form_element_validator_number_get_compare_value:
+ * @validator:
+ *
+ */
+gchar
+*zak_form_element_validator_number_get_compare_value (ZakFormElementValidatorNumber *validator)
+{
+       gchar *ret;
+
+       ZakFormElementValidatorNumberPrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_NUMBER_GET_PRIVATE (validator);
+
+       ret = g_strdup_printf ("%f", priv->compare_value);
+
+       return ret;
+}
+
+/**
+ * zak_form_element_validator_number_xml_parsing:
+ * @validator:
+ * @xnode:
+ *
+ */
+static gboolean
+zak_form_element_validator_number_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode)
+{
+       gchar *prop;
+
+       ZakFormElementValidatorNumberPrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_NUMBER_GET_PRIVATE (ZAK_FORM_ELEMENT_VALIDATOR_NUMBER (validator));
+
+       prop = (gchar *)xmlGetProp (xnode, (const xmlChar *)"type_comp");
+       zak_form_element_validator_number_set_compare_type (ZAK_FORM_ELEMENT_VALIDATOR_NUMBER (validator), zak_form_get_compare_type_from_string (prop));
+       g_free (prop);
+
+       prop = (gchar *)xmlGetProp (xnode, (const xmlChar *)"value_comp");
+       zak_form_element_validator_number_set_compare_value (ZAK_FORM_ELEMENT_VALIDATOR_NUMBER (validator), prop);
+       g_free (prop);
+
+       return TRUE;
+}
+
+/* PRIVATE */
+static void
+zak_form_element_validator_number_set_property (GObject *object,
+                                              guint property_id,
+                                              const GValue *value,
+                                              GParamSpec *pspec)
+{
+       ZakFormElementValidatorNumber *zak_form_element = (ZakFormElementValidatorNumber *)object;
+       ZakFormElementValidatorNumberPrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_NUMBER_GET_PRIVATE (zak_form_element);
+
+       switch (property_id)
+               {
+                       default:
+                               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                               break;
+               }
+}
+
+static void
+zak_form_element_validator_number_get_property (GObject *object,
+                                              guint property_id,
+                                              GValue *value,
+                                              GParamSpec *pspec)
+{
+       ZakFormElementValidatorNumber *zak_form_element = (ZakFormElementValidatorNumber *)object;
+       ZakFormElementValidatorNumberPrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_NUMBER_GET_PRIVATE (zak_form_element);
+
+       switch (property_id)
+               {
+                       default:
+                               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                               break;
+               }
+}
+
+static void
+zak_form_element_validator_number_dispose (GObject *gobject)
+{
+       ZakFormElementValidatorNumber *zak_form_element = (ZakFormElementValidatorNumber *)gobject;
+       ZakFormElementValidatorNumberPrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_NUMBER_GET_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_validator_number_finalize (GObject *gobject)
+{
+       ZakFormElementValidatorNumber *zak_form_element = (ZakFormElementValidatorNumber *)gobject;
+       ZakFormElementValidatorNumberPrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_NUMBER_GET_PRIVATE (zak_form_element);
+
+       GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject));
+       parent_class->finalize (gobject);
+}
+
+static gboolean
+zak_form_element_validator_number_validate (ZakFormElementValidator *validator_number,
+                                          const gchar *value)
+{
+       gboolean ret;
+
+       gdouble _value;
+
+       gchar *msg;
+
+       ZakFormElementValidatorNumberPrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_NUMBER_GET_PRIVATE (validator_number);
+
+       g_return_val_if_fail (value != NULL, FALSE);
+
+       _value = g_strtod (value, NULL);
+
+       if (_value < priv->compare_value)
+               {
+                       ret = (priv->compare_type == ZAK_FORM_COMPARE_TYPE_LESSER
+                              || priv->compare_type == ZAK_FORM_COMPARE_TYPE_LESSER_EQUAL
+                              || priv->compare_type == ZAK_FORM_COMPARE_TYPE_NOT_EQUAL);
+               }
+       else if (_value == priv->compare_value)
+               {
+                       ret = (priv->compare_type == ZAK_FORM_COMPARE_TYPE_LESSER_EQUAL
+                              || priv->compare_type == ZAK_FORM_COMPARE_TYPE_EQUAL
+                              || priv->compare_type == ZAK_FORM_COMPARE_TYPE_GREATER_EQUAL);
+               }
+       else if (_value > priv->compare_value)
+               {
+                       ret = (priv->compare_type == ZAK_FORM_COMPARE_TYPE_GREATER
+                              || priv->compare_type == ZAK_FORM_COMPARE_TYPE_GREATER_EQUAL
+                              || priv->compare_type == ZAK_FORM_COMPARE_TYPE_NOT_EQUAL);
+               }
+
+       if (!ret)
+               {
+                       msg = g_strdup_printf (_("Value must be %s %f"),
+                                              zak_form_get_compare_type_stringify (priv->compare_type),
+                                              priv->compare_value);
+                       zak_form_element_validator_set_message (validator_number, msg);
+                       g_free (msg);
+               }
+
+       return ret;
+}
diff --git a/src/formelementvalidatornumber.h b/src/formelementvalidatornumber.h
new file mode 100644 (file)
index 0000000..d4a08d1
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2020 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_VALIDATOR_NUMBER_H__
+#define __ZAK_FORM_ELEMENT_VALIDATOR_NUMBER_H__
+
+
+#include <glib-object.h>
+
+#include <libxml/tree.h>
+
+#include "commons.h"
+#include "formelementvalidator.h"
+
+
+G_BEGIN_DECLS
+
+
+#define ZAK_FORM_TYPE_ELEMENT_VALIDATOR_NUMBER zak_form_element_validator_number_get_type ()
+G_DECLARE_FINAL_TYPE (ZakFormElementValidatorNumber, zak_form_element_validator_number, ZAK_FORM, ELEMENT_VALIDATOR_NUMBER, ZakFormElementValidator)
+
+ZakFormElementValidatorNumber *zak_form_element_validator_number_new (void);
+
+void zak_form_element_validator_number_set_compare_type (ZakFormElementValidatorNumber *validator, ZakFormCompareType type);
+ZakFormCompareType zak_form_element_validator_number_get_compare_type (ZakFormElementValidatorNumber *validator);
+
+void zak_form_element_validator_number_set_compare_value (ZakFormElementValidatorNumber *validator, const gchar *value);
+gchar *zak_form_element_validator_number_get_compare_value (ZakFormElementValidatorNumber *validator);
+
+
+G_END_DECLS
+
+
+#endif /* __ZAK_FORM_ELEMENT_VALIDATOR_NUMBER_H__ */