From: Andrea Zagli Date: Mon, 12 Jun 2017 19:35:13 +0000 (+0200) Subject: Added class ZakFormElementExtension. X-Git-Tag: v0.1.0~1^2~6^2~3 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=457e4d485efeb1da6db96f04876cd8746c3feb18;p=zakform%2Flibzakform Added class ZakFormElementExtension. --- diff --git a/src/Makefile.am b/src/Makefile.am index 83af3a3..dfa41ba 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -18,6 +18,7 @@ libzakform_la_SOURCES = \ zakformmarshal.c \ form.c \ formelement.c \ + formelementextension.c \ formelementfilter.c \ formelementfilterluc.c \ formelementfiltertrim.c \ @@ -38,6 +39,7 @@ gir_include = \ zakformmarshal.h \ form.h \ formelement.h \ + formelementextension.h \ formelementfilter.h \ formelementfilterluc.h \ formelementfiltertrim.h \ diff --git a/src/form.c b/src/form.c index 9b701fb..94baf67 100644 --- a/src/form.c +++ b/src/form.c @@ -33,9 +33,6 @@ #include #endif -typedef ZakFormElement *(* FormElementConstructorFunc) (void); -typedef GObject *(* FormElementExtensionConstructorFunc) (void); -typedef gboolean (* FormElementExtensionXmlParsingFunc) (GObject *, xmlNodePtr); typedef ZakFormValidator *(* FormValidatorConstructorFunc) (void); static void zak_form_form_class_init (ZakFormFormClass *class); @@ -156,12 +153,11 @@ zak_form_form_element_xml_parsing (ZakFormForm *zakform, ZakFormElement *element gchar *type; guint i; - GObject *extension; + ZakFormElementExtension *extension; ZakFormElementFilter *filter; ZakFormElementValidator *validator; - FormElementExtensionConstructorFunc extension_constructor; - FormElementExtensionXmlParsingFunc extension_xml_parsing; + ZakFormElementExtensionConstructorFunc extension_constructor; ZakFormElementFilterConstructorFunc filter_constructor; ZakFormElementValidatorConstructorFunc validator_constructor; @@ -179,35 +175,17 @@ zak_form_form_element_xml_parsing (ZakFormForm *zakform, ZakFormElement *element { type = (gchar *)xmlGetProp (xnode, (const xmlChar *)"type"); - /* for each module */ - for (i = 0; i < priv->ar_modules->len; i++) + extension_constructor = zak_form_form_get_form_element_extension (zakform, type); + if (extension_constructor != NULL) { - if (g_module_symbol ((GModule *)g_ptr_array_index (priv->ar_modules, i), - g_strconcat (type, "_new", NULL), - (gpointer *)&extension_constructor)) - { - if (extension_constructor != NULL) - { - extension = extension_constructor (); - zak_form_element_add_extension (element, extension); - - if (g_module_symbol ((GModule *)g_ptr_array_index (priv->ar_modules, i), - g_strconcat (type, "_xml_parsing", NULL), - (gpointer *)&extension_xml_parsing)) - { - if (extension_xml_parsing != NULL) - { - extension_xml_parsing (extension, xnode); - } - } + extension = extension_constructor (); + zak_form_element_add_extension (element, extension); - break; - } - } + zak_form_element_extension_xml_parsing (extension, xnode); } - if (i >= priv->ar_modules->len) + else { - g_warning ("Filter «%s» not found.", type); + g_warning ("Extension «%s» not found.", type); } to_unlink = TRUE; @@ -354,6 +332,25 @@ zak_form_form_get_form_element_validator (ZakFormForm *zakform, const gchar *nam return validator_constructor; } +/** + * zak_form_form_get_form_element_extension: + * @zakform: + * @namespace: + * + * Returns: + */ +ZakFormElementExtensionConstructorFunc +zak_form_form_get_form_element_extension (ZakFormForm *zakform, const gchar *namespace) +{ + ZakFormElementExtensionConstructorFunc extension_constructor; + + g_return_val_if_fail (ZAK_FORM_IS_FORM (zakform), NULL); + + extension_constructor = (ZakFormElementExtensionConstructorFunc)_zak_form_form_get_module_new (zakform, namespace); + + return extension_constructor; +} + /** * zak_form_form_load_from_xml: * @zakform: diff --git a/src/form.h b/src/form.h index 7737f7b..0e05f9d 100644 --- a/src/form.h +++ b/src/form.h @@ -25,6 +25,7 @@ #include #include "formelement.h" +#include "formelementextension.h" #include "formiprovider.h" #include "formvalidator.h" @@ -53,6 +54,9 @@ ZakFormElementFilterConstructorFunc zak_form_form_get_form_element_filter (ZakFo typedef ZakFormElementValidator *(* ZakFormElementValidatorConstructorFunc) (void); ZakFormElementValidatorConstructorFunc zak_form_form_get_form_element_validator (ZakFormForm *zakform, const gchar *namespace); +typedef ZakFormElementExtension *(* ZakFormElementExtensionConstructorFunc) (void); +ZakFormElementExtensionConstructorFunc zak_form_form_get_form_element_extension (ZakFormForm *zakform, const gchar *namespace); + gboolean zak_form_form_load_from_xml (ZakFormForm *zakform, xmlDoc *xmldoc); gboolean zak_form_form_load_from_file (ZakFormForm *zakform, const gchar *filename); diff --git a/src/formelement.c b/src/formelement.c index dccfe91..47d5bce 100644 --- a/src/formelement.c +++ b/src/formelement.c @@ -242,7 +242,7 @@ zak_form_element_init (ZakFormElement *zak_form_element) * */ void -zak_form_element_add_extension (ZakFormElement *element, GObject *extension) +zak_form_element_add_extension (ZakFormElement *element, ZakFormElementExtension *extension) { ZakFormElementPrivate *priv; diff --git a/src/formelement.h b/src/formelement.h index 9fb99eb..22bcbc1 100644 --- a/src/formelement.h +++ b/src/formelement.h @@ -26,6 +26,7 @@ #include "formelementfilter.h" #include "formelementvalidator.h" +#include "formelementextension.h" G_BEGIN_DECLS @@ -118,7 +119,7 @@ gboolean zak_form_element_get_to_save (ZakFormElement *element); void zak_form_element_clear (ZakFormElement *element); -void zak_form_element_add_extension (ZakFormElement *element, GObject *extension); +void zak_form_element_add_extension (ZakFormElement *element, ZakFormElementExtension *extension); GPtrArray *zak_form_element_get_extensions (ZakFormElement *element); GPtrArray *zak_form_element_get_extensions_by_type (ZakFormElement *element, GType type); diff --git a/src/formelementextension.c b/src/formelementextension.c new file mode 100644 index 0000000..7a9f964 --- /dev/null +++ b/src/formelementextension.c @@ -0,0 +1,150 @@ +/* + * Copyright (C) 2017 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 + +#include + +#include "formelementextension.h" + +enum +{ + PROP_0 +}; + +static void zak_form_element_extension_class_init (ZakFormElementExtensionClass *class); +static void zak_form_element_extension_init (ZakFormElementExtension *zak_form_element); + +static void zak_form_element_extension_construct (ZakFormElementExtension *element, + const gchar *id, + GHashTable *ht_attrs); + +static void zak_form_element_extension_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void zak_form_element_extension_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static void zak_form_element_extension_dispose (GObject *gobject); +static void zak_form_element_extension_finalize (GObject *gobject); + +typedef struct + { + gpointer useless; + } ZakFormElementExtensionPrivate; + +G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ZakFormElementExtension, zak_form_element_extension, G_TYPE_OBJECT) + +static void +zak_form_element_extension_class_init (ZakFormElementExtensionClass *class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (class); + + object_class->set_property = zak_form_element_extension_set_property; + object_class->get_property = zak_form_element_extension_get_property; + object_class->dispose = zak_form_element_extension_dispose; + object_class->finalize = zak_form_element_extension_finalize; +} + +static void +zak_form_element_extension_init (ZakFormElementExtension *zak_form_element) +{ + ZakFormElementExtensionPrivate *priv = zak_form_element_extension_get_instance_private (zak_form_element); + +} + +/** + * zak_form_element_extension_xml_parsing: + * @element: + * @xmlnode: + * + */ +void +zak_form_element_extension_xml_parsing (ZakFormElementExtension *element, xmlNode *xmlnode) +{ + if (ZAK_FORM_ELEMENT_EXTENSION_GET_CLASS (element)->xml_parsing != NULL) + { + ZAK_FORM_ELEMENT_EXTENSION_GET_CLASS (element)->xml_parsing (element, xmlnode); + } +} + +/* PRIVATE */ +static void +zak_form_element_extension_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + ZakFormElementExtension *zak_form_element_extension = (ZakFormElementExtension *)object; + ZakFormElementExtensionPrivate *priv = zak_form_element_extension_get_instance_private (zak_form_element_extension); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_form_element_extension_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) +{ + ZakFormElementExtension *zak_form_element_extension = (ZakFormElementExtension *)object; + ZakFormElementExtensionPrivate *priv = zak_form_element_extension_get_instance_private (zak_form_element_extension); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_form_element_extension_dispose (GObject *gobject) +{ + ZakFormElementExtension *zak_form_element_extension = (ZakFormElementExtension *)gobject; + ZakFormElementExtensionPrivate *priv = zak_form_element_extension_get_instance_private (zak_form_element_extension); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->dispose (gobject); +} + +static void +zak_form_element_extension_finalize (GObject *gobject) +{ + ZakFormElementExtension *zak_form_element_extension = (ZakFormElementExtension *)gobject; + ZakFormElementExtensionPrivate *priv = zak_form_element_extension_get_instance_private (zak_form_element_extension); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->finalize (gobject); +} diff --git a/src/formelementextension.h b/src/formelementextension.h new file mode 100644 index 0000000..35fac40 --- /dev/null +++ b/src/formelementextension.h @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2017 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_EXTENSION_H__ +#define __ZAK_FORM_ELEMENT_EXTENSION_H__ + + +#include + +#include + + +G_BEGIN_DECLS + + +#define ZAK_FORM_TYPE_ELEMENT_EXTENSION zak_form_element_extension_get_type () +G_DECLARE_DERIVABLE_TYPE (ZakFormElementExtension, zak_form_element_extension, ZAK_FORM, ELEMENT_EXTENSION, GObject) + +struct _ZakFormElementExtensionClass + { + GObjectClass parent_class; + + void (*xml_parsing) (ZakFormElementExtension *element, xmlNode *xmlnode); + }; + +void zak_form_element_extension_xml_parsing (ZakFormElementExtension *element, xmlNode *xmlnode); + + +G_END_DECLS + + +#endif /* __ZAK_FORM_ELEMENT_EXTENSION_H__ */ diff --git a/src/libzakform.h b/src/libzakform.h index 63e78ab..da08276 100644 --- a/src/libzakform.h +++ b/src/libzakform.h @@ -24,6 +24,8 @@ #include +#include + #include #include #include