From: Andrea Zagli Date: Sun, 1 Nov 2015 09:22:09 +0000 (+0100) Subject: Migrated ZakFormElementIValidator from interface to abstract class. X-Git-Tag: debian/0.0.1-1~47 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=2970ad766dc55e85bcd92217620ff53ce219eade;p=zakform%2Flibzakform Migrated ZakFormElementIValidator from interface to abstract class. --- diff --git a/src/Makefile.am b/src/Makefile.am index ff33003..fc8f1c1 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,7 +10,7 @@ libzakform_la_SOURCES = \ formelement.c \ formelementifilter.c \ formelementfiltertrim.c \ - formelementivalidator.c \ + formelementvalidator.c \ formelementvalidatornotempty.c \ formelementvalidatorregex.c @@ -22,7 +22,7 @@ libzakform_include_HEADERS = \ formelement.h \ formelementifilter.h \ formelementfiltertrim.h \ - formelementivalidator.h \ + formelementvalidator.h \ formelementvalidatornotempty.h \ formelementvalidatorregex.h diff --git a/src/formelement.c b/src/formelement.c index feb0345..53e9785 100644 --- a/src/formelement.c +++ b/src/formelement.c @@ -167,7 +167,7 @@ gchar * */ void -zak_form_element_add_validator (ZakFormElement *element, ZakFormElementIValidator *validator) +zak_form_element_add_validator (ZakFormElement *element, ZakFormElementValidator *validator) { ZakFormElementPrivate *priv; @@ -208,7 +208,7 @@ zak_form_element_is_valid (ZakFormElement *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), + if (!zak_form_element_validator_validate ((ZakFormElementValidator *)g_ptr_array_index (priv->pa_validators, i), value)) { ret = FALSE; diff --git a/src/formelement.h b/src/formelement.h index 199a8f8..cb3594c 100644 --- a/src/formelement.h +++ b/src/formelement.h @@ -23,7 +23,7 @@ #include #include "formelementifilter.h" -#include "formelementivalidator.h" +#include "formelementvalidator.h" G_BEGIN_DECLS @@ -44,7 +44,7 @@ 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); +void zak_form_element_add_validator (ZakFormElement *element, ZakFormElementValidator *validator); gboolean zak_form_element_is_valid (ZakFormElement *element); diff --git a/src/formelementivalidator.c b/src/formelementivalidator.c deleted file mode 100644 index 8cd6705..0000000 --- a/src/formelementivalidator.c +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2015 Andrea Zagli - * - * 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 - */ - -#include "formelementivalidator.h" - -G_DEFINE_INTERFACE (ZakFormElementIValidator, zak_form_element_ivalidator, G_TYPE_OBJECT); - -static void -zak_form_element_ivalidator_default_init (ZakFormElementIValidatorInterface *iface) -{ - /* add properties and signals to the interface here */ -} - -gboolean -zak_form_element_ivalidator_validate (ZakFormElementIValidator *self, const gchar *value) -{ - g_return_val_if_fail (ZAK_FORM_IS_ELEMENT_IVALIDATOR (self), FALSE); - - return ZAK_FORM_ELEMENT_IVALIDATOR_GET_IFACE (self)->validate (self, value); -} diff --git a/src/formelementivalidator.h b/src/formelementivalidator.h deleted file mode 100644 index 4ef234d..0000000 --- a/src/formelementivalidator.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (C) 2015 Andrea Zagli - * - * 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_IVALIDATOR_H__ -#define __ZAK_FORM_ELEMENT_IVALIDATOR_H__ - -#include - - -G_BEGIN_DECLS - - -#define ZAK_TYPE_FORM_ELEMENT_IVALIDATOR zak_form_element_ivalidator_get_type () -G_DECLARE_INTERFACE (ZakFormElementIValidator, zak_form_element_ivalidator, ZAK_FORM, ELEMENT_IVALIDATOR, GObject) - -struct _ZakFormElementIValidatorInterface -{ - GTypeInterface parent_iface; - - gboolean (*validate) (ZakFormElementIValidator *self, const gchar *value); -}; - -gboolean zak_form_element_ivalidator_validate (ZakFormElementIValidator *self, const gchar *value); - - -G_END_DECLS - - -#endif /* __ZAK_FORM_ELEMENT_IVALIDATOR_H__ */ diff --git a/src/formelementvalidator.c b/src/formelementvalidator.c new file mode 100644 index 0000000..e2c9b0e --- /dev/null +++ b/src/formelementvalidator.c @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2015 Andrea Zagli + * + * 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 +#endif + +#include "formelementvalidator.h" + +static void zak_form_element_validator_class_init (ZakFormElementValidatorClass *class); +static void zak_form_element_validator_init (ZakFormElementValidator *zak_form_element_validator); + +static void zak_form_element_validator_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void zak_form_element_validator_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static void zak_form_element_validator_dispose (GObject *gobject); +static void zak_form_element_validator_finalize (GObject *gobject); + +typedef struct + { + gpointer nothing; + } ZakFormElementValidatorPrivate; + +G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ZakFormElementValidator, zak_form_element_validator, G_TYPE_OBJECT) + +static void +zak_form_element_validator_class_init (ZakFormElementValidatorClass *class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (class); + + object_class->set_property = zak_form_element_validator_set_property; + object_class->get_property = zak_form_element_validator_get_property; + object_class->dispose = zak_form_element_validator_dispose; + object_class->finalize = zak_form_element_validator_finalize; +} + +static void +zak_form_element_validator_init (ZakFormElementValidator *zak_form_element_validator) +{ + ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (zak_form_element_validator); +} + +gboolean +zak_form_element_validator_validate (ZakFormElementValidator *self, const gchar *value) +{ + gboolean ret; + + g_return_val_if_fail (ZAK_FORM_IS_ELEMENT_VALIDATOR (self), FALSE); + + ret = TRUE; + if (ZAK_FORM_ELEMENT_VALIDATOR_GET_CLASS (self)->validate != NULL) + { + ret = ZAK_FORM_ELEMENT_VALIDATOR_GET_CLASS (self)->validate (self, value); + } + return ret; +} + +/* PRIVATE */ +static void +zak_form_element_validator_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + ZakFormElementValidator *zak_form_element_validator = (ZakFormElementValidator *)object; + ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (zak_form_element_validator); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_form_element_validator_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) +{ + ZakFormElementValidator *zak_form_element_validator = (ZakFormElementValidator *)object; + ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (zak_form_element_validator); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_form_element_validator_dispose (GObject *gobject) +{ + ZakFormElementValidator *zak_form_element_validator = (ZakFormElementValidator *)gobject; + ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (zak_form_element_validator); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->dispose (gobject); +} + +static void +zak_form_element_validator_finalize (GObject *gobject) +{ + ZakFormElementValidator *zak_form_element_validator = (ZakFormElementValidator *)gobject; + ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (zak_form_element_validator); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->finalize (gobject); +} diff --git a/src/formelementvalidator.h b/src/formelementvalidator.h new file mode 100644 index 0000000..b1b8145 --- /dev/null +++ b/src/formelementvalidator.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2015 Andrea Zagli + * + * 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_H__ +#define __ZAK_FORM_ELEMENT_VALIDATOR_H__ + +#include + + +G_BEGIN_DECLS + + +#define ZAK_FORM_TYPE_ELEMENT_VALIDATOR zak_form_element_validator_get_type () +G_DECLARE_DERIVABLE_TYPE (ZakFormElementValidator, zak_form_element_validator, ZAK_FORM, ELEMENT_VALIDATOR, GObject) + +struct _ZakFormElementValidatorClass +{ + GObjectClass parent_class; + + gboolean (*validate) (ZakFormElementValidator *self, const gchar *value); +}; + +gboolean zak_form_element_validator_validate (ZakFormElementValidator *self, const gchar *value); + + +G_END_DECLS + + +#endif /* __ZAK_FORM_ELEMENT_VALIDATOR_H__ */ diff --git a/src/formelementvalidatornotempty.c b/src/formelementvalidatornotempty.c index 5bd4a61..ede9aee 100644 --- a/src/formelementvalidatornotempty.c +++ b/src/formelementvalidatornotempty.c @@ -20,11 +20,10 @@ #include #endif -#include "formelementivalidator.h" +#include "formelementvalidator.h" #include "formelementvalidatornotempty.h" static void zak_form_element_validator_notempty_class_init (ZakFormElementValidatorNotemptyClass *class); -static void zak_form_element_validator_notempty_interface_init (ZakFormElementIValidatorInterface *iface); static void zak_form_element_validator_notempty_init (ZakFormElementValidatorNotempty *zak_form_element); static void zak_form_element_validator_notempty_set_property (GObject *object, @@ -39,7 +38,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, const gchar *value); +static gboolean zak_form_element_validator_notempty_validate (ZakFormElementValidator *validator_notempty, const gchar *value); struct _ZakFormElementValidatorNotempty { @@ -56,27 +55,22 @@ struct _ZakFormElementValidatorNotemptyPrivate gpointer nothing; }; -G_DEFINE_TYPE_WITH_CODE (ZakFormElementValidatorNotempty, zak_form_element_validator_notempty, G_TYPE_OBJECT, - G_IMPLEMENT_INTERFACE (ZAK_TYPE_FORM_ELEMENT_IVALIDATOR, - zak_form_element_validator_notempty_interface_init)) +G_DEFINE_TYPE (ZakFormElementValidatorNotempty, zak_form_element_validator_notempty, ZAK_FORM_TYPE_ELEMENT_VALIDATOR) static void zak_form_element_validator_notempty_class_init (ZakFormElementValidatorNotemptyClass *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_notempty_set_property; object_class->get_property = zak_form_element_validator_notempty_get_property; object_class->dispose = zak_form_element_validator_notempty_dispose; object_class->finalize = zak_form_element_validator_notempty_finalize; - g_type_class_add_private (object_class, sizeof (ZakFormElementValidatorNotemptyPrivate)); -} + parent_class->validate = zak_form_element_validator_notempty_validate; -static void -zak_form_element_validator_notempty_interface_init (ZakFormElementIValidatorInterface *iface) -{ - iface->validate = zak_form_element_validator_notempty_validate; + g_type_class_add_private (object_class, sizeof (ZakFormElementValidatorNotemptyPrivate)); } static void @@ -160,7 +154,7 @@ zak_form_element_validator_notempty_finalize (GObject *gobject) } static gboolean -zak_form_element_validator_notempty_validate (ZakFormElementIValidator *validator_notempty, +zak_form_element_validator_notempty_validate (ZakFormElementValidator *validator_notempty, const gchar *value) { gboolean ret; diff --git a/src/formelementvalidatornotempty.h b/src/formelementvalidatornotempty.h index d96c407..4ee42e1 100644 --- a/src/formelementvalidatornotempty.h +++ b/src/formelementvalidatornotempty.h @@ -22,12 +22,14 @@ #include +#include + G_BEGIN_DECLS #define ZAK_TYPE_FORM_ELEMENT_VALIDATOR_NOTEMPTY zak_form_element_validator_notempty_get_type () -G_DECLARE_FINAL_TYPE (ZakFormElementValidatorNotempty, zak_form_element_validator_notempty, ZAK_FORM, ELEMENT_VALIDATOR_NOTEMPTY, GObject) +G_DECLARE_FINAL_TYPE (ZakFormElementValidatorNotempty, zak_form_element_validator_notempty, ZAK_FORM, ELEMENT_VALIDATOR_NOTEMPTY, ZakFormElementValidator) ZakFormElementValidatorNotempty *zak_form_element_validator_notempty_new (void); diff --git a/src/formelementvalidatorregex.c b/src/formelementvalidatorregex.c index 7243f56..ac9ab17 100644 --- a/src/formelementvalidatorregex.c +++ b/src/formelementvalidatorregex.c @@ -20,13 +20,10 @@ #include #endif -#include - -#include "formelementivalidator.h" +#include "formelementvalidator.h" #include "formelementvalidatorregex.h" static void zak_form_element_validator_regex_class_init (ZakFormElementValidatorRegexClass *class); -static void zak_form_element_validator_regex_interface_init (ZakFormElementIValidatorInterface *iface); static void zak_form_element_validator_regex_init (ZakFormElementValidatorRegex *zak_form_element); static void zak_form_element_validator_regex_set_property (GObject *object, @@ -41,7 +38,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, const gchar *value); +static gboolean zak_form_element_validator_regex_validate (ZakFormElementValidator *validator_regex, const gchar *value); struct _ZakFormElementValidatorRegex { @@ -58,27 +55,22 @@ struct _ZakFormElementValidatorRegexPrivate gchar *regex; }; -G_DEFINE_TYPE_WITH_CODE (ZakFormElementValidatorRegex, zak_form_element_validator_regex, G_TYPE_OBJECT, - G_IMPLEMENT_INTERFACE (ZAK_TYPE_FORM_ELEMENT_IVALIDATOR, - zak_form_element_validator_regex_interface_init)) +G_DEFINE_TYPE (ZakFormElementValidatorRegex, zak_form_element_validator_regex, ZAK_FORM_TYPE_ELEMENT_VALIDATOR) static void zak_form_element_validator_regex_class_init (ZakFormElementValidatorRegexClass *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_regex_set_property; object_class->get_property = zak_form_element_validator_regex_get_property; object_class->dispose = zak_form_element_validator_regex_dispose; object_class->finalize = zak_form_element_validator_regex_finalize; - g_type_class_add_private (object_class, sizeof (ZakFormElementValidatorRegexPrivate)); -} + parent_class->validate = zak_form_element_validator_regex_validate; -static void -zak_form_element_validator_regex_interface_init (ZakFormElementIValidatorInterface *iface) -{ - iface->validate = zak_form_element_validator_regex_validate; + g_type_class_add_private (object_class, sizeof (ZakFormElementValidatorRegexPrivate)); } static void @@ -172,7 +164,7 @@ zak_form_element_validator_regex_finalize (GObject *gobject) } static gboolean -zak_form_element_validator_regex_validate (ZakFormElementIValidator *validator_regex, +zak_form_element_validator_regex_validate (ZakFormElementValidator *validator_regex, const gchar *value) { gboolean ret; @@ -189,8 +181,8 @@ zak_form_element_validator_regex_validate (ZakFormElementIValidator *validator_r if (regex == NULL || error != NULL) { - syslog (LOG_MAKEPRI(LOG_SYSLOG, LOG_DEBUG), "Error on creating regex: %s.", - error->message != NULL ? error->message : "no details"); + g_warning ("Error on creating regex: %s.", + error->message != NULL ? error->message : "no details"); return FALSE; } diff --git a/src/formelementvalidatorregex.h b/src/formelementvalidatorregex.h index 2d7808f..7add429 100644 --- a/src/formelementvalidatorregex.h +++ b/src/formelementvalidatorregex.h @@ -22,12 +22,14 @@ #include +#include + G_BEGIN_DECLS #define ZAK_TYPE_FORM_ELEMENT_VALIDATOR_REGEX zak_form_element_validator_regex_get_type () -G_DECLARE_FINAL_TYPE (ZakFormElementValidatorRegex, zak_form_element_validator_regex, ZAK_FORM, ELEMENT_VALIDATOR_REGEX, GObject) +G_DECLARE_FINAL_TYPE (ZakFormElementValidatorRegex, zak_form_element_validator_regex, ZAK_FORM, ELEMENT_VALIDATOR_REGEX, ZakFormElementValidator) ZakFormElementValidatorRegex *zak_form_element_validator_regex_new (const gchar *regex); diff --git a/src/libzakform.h b/src/libzakform.h index d83e071..359fac7 100644 --- a/src/libzakform.h +++ b/src/libzakform.h @@ -27,7 +27,7 @@ #include #include -#include +#include #include #include