From b4ba8f569d0603decdca54c2a67842d39937e146 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Mon, 13 Feb 2017 18:16:15 +0100 Subject: [PATCH] Added FormElement extensions. --- src/form.c | 60 ++++++++++++++++++++++++++++++++++++++------- src/formelement.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++ src/formelement.h | 5 ++++ 3 files changed, 118 insertions(+), 9 deletions(-) diff --git a/src/form.c b/src/form.c index 65d27d8..c1b54cf 100644 --- a/src/form.c +++ b/src/form.c @@ -35,6 +35,8 @@ typedef ZakFormElement *(* FormElementConstructorFunc) (void); typedef gboolean (* FormElementXmlParsingFunc) (ZakFormElement *, xmlNodePtr); +typedef GObject *(* FormElementExtensionConstructorFunc) (void); +typedef gboolean (* FormElementExtensionXmlParsingFunc) (GObject *, xmlNodePtr); typedef ZakFormElementFilter *(* FormElementFilterConstructorFunc) (void); typedef gboolean (* FormElementFilterXmlParsingFunc) (ZakFormElementFilter *, xmlNodePtr); typedef ZakFormElementValidator *(* FormElementValidatorConstructorFunc) (void); @@ -160,9 +162,12 @@ zak_form_form_element_xml_parsing (ZakFormForm *zakform, ZakFormElement *element gchar *type; guint i; + GObject *extension; ZakFormElementFilter *filter; ZakFormElementValidator *validator; + FormElementExtensionConstructorFunc extension_constructor; + FormElementExtensionXmlParsingFunc extension_xml_parsing; FormElementFilterConstructorFunc filter_constructor; FormElementFilterXmlParsingFunc filter_xml_parsing; FormElementValidatorConstructorFunc validator_constructor; @@ -178,7 +183,7 @@ zak_form_form_element_xml_parsing (ZakFormForm *zakform, ZakFormElement *element { to_unlink = FALSE; - if (xmlStrcmp (xnode->name, (const xmlChar *)"filter") == 0) + if (xmlStrcmp (xnode->name, (const xmlChar *)"extension") == 0) { type = xmlGetProp (xnode, (const xmlChar *)"type"); @@ -186,8 +191,45 @@ zak_form_form_element_xml_parsing (ZakFormForm *zakform, ZakFormElement *element for (i = 0; i < priv->ar_modules->len; i++) { if (g_module_symbol ((GModule *)g_ptr_array_index (priv->ar_modules, i), - g_strconcat (type, "_new", NULL), - (gpointer *)&filter_constructor)) + 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); + } + } + + break; + } + } + } + if (i >= priv->ar_modules->len) + { + g_warning ("Filter «%s» not found.", type); + } + + to_unlink = TRUE; + } + else if (xmlStrcmp (xnode->name, (const xmlChar *)"filter") == 0) + { + type = xmlGetProp (xnode, (const xmlChar *)"type"); + + /* for each module */ + for (i = 0; i < priv->ar_modules->len; i++) + { + if (g_module_symbol ((GModule *)g_ptr_array_index (priv->ar_modules, i), + g_strconcat (type, "_new", NULL), + (gpointer *)&filter_constructor)) { if (filter_constructor != NULL) { @@ -195,8 +237,8 @@ zak_form_form_element_xml_parsing (ZakFormForm *zakform, ZakFormElement *element zak_form_element_add_filter (element, filter); if (g_module_symbol ((GModule *)g_ptr_array_index (priv->ar_modules, i), - g_strconcat (type, "_xml_parsing", NULL), - (gpointer *)&filter_xml_parsing)) + g_strconcat (type, "_xml_parsing", NULL), + (gpointer *)&filter_xml_parsing)) { if (filter_xml_parsing != NULL) { @@ -223,8 +265,8 @@ zak_form_form_element_xml_parsing (ZakFormForm *zakform, ZakFormElement *element for (i = 0; i < priv->ar_modules->len; i++) { if (g_module_symbol ((GModule *)g_ptr_array_index (priv->ar_modules, i), - g_strconcat (type, "_new", NULL), - (gpointer *)&validator_constructor)) + g_strconcat (type, "_new", NULL), + (gpointer *)&validator_constructor)) { if (validator_constructor != NULL) { @@ -232,8 +274,8 @@ zak_form_form_element_xml_parsing (ZakFormForm *zakform, ZakFormElement *element zak_form_element_add_validator (element, validator); if (g_module_symbol ((GModule *)g_ptr_array_index (priv->ar_modules, i), - g_strconcat (type, "_xml_parsing", NULL), - (gpointer *)&validator_xml_parsing)) + g_strconcat (type, "_xml_parsing", NULL), + (gpointer *)&validator_xml_parsing)) { if (validator_xml_parsing != NULL) { diff --git a/src/formelement.c b/src/formelement.c index bfc4e20..9e6db5e 100644 --- a/src/formelement.c +++ b/src/formelement.c @@ -79,6 +79,7 @@ typedef struct gboolean to_load; gboolean to_save; + GPtrArray *pa_extensions; GPtrArray *pa_filters; GPtrArray *pa_validators; GPtrArray *pa_messages; @@ -199,11 +200,72 @@ zak_form_element_init (ZakFormElement *zak_form_element) priv->to_load = TRUE; priv->to_save = TRUE; + priv->pa_extensions = g_ptr_array_new (); priv->pa_filters = g_ptr_array_new (); priv->pa_validators = g_ptr_array_new (); priv->pa_messages = NULL; } +/** + * zak_form_element_add_extension: + * @element: + * @extension: + * + */ +void +zak_form_element_add_extension (ZakFormElement *element, GObject *extension) +{ + ZakFormElementPrivate *priv; + + priv = zak_form_element_get_instance_private (element); + + g_ptr_array_add (priv->pa_extensions, extension); +} + +/** + * zak_form_element_get_extensions: + * @zakform: + * + * Returns: a #GPtrArray with the list of extensions in the form element. + */ +GPtrArray +*zak_form_element_get_extensions (ZakFormElement *element) +{ + ZakFormElementPrivate *priv = zak_form_element_get_instance_private (element); + + return priv->pa_extensions; +} + +/** + * zak_form_element_get_extensions_by_type: + * @element: + * @type: + * + * Returns: a #GPtrArray with the list of extensions in the form element filtered by @type. + */ +GPtrArray +*zak_form_element_get_extensions_by_type (ZakFormElement *element, GType type) +{ + GPtrArray *ar; + GObject *extension; + + guint i; + + ZakFormElementPrivate *priv = zak_form_element_get_instance_private (element); + + ar = g_ptr_array_new (); + for (i = 0; i < priv->pa_extensions->len; i++) + { + extension = (GObject *)g_ptr_array_index (priv->pa_extensions, i); + if (G_OBJECT_TYPE (extension) == type) + { + g_ptr_array_add (ar, extension); + } + } + + return ar; +} + /** * zak_form_element_add_filter: * @element: diff --git a/src/formelement.h b/src/formelement.h index 4803533..d68f6a5 100644 --- a/src/formelement.h +++ b/src/formelement.h @@ -93,6 +93,11 @@ 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); + +GPtrArray *zak_form_element_get_extensions (ZakFormElement *element); +GPtrArray *zak_form_element_get_extensions_by_type (ZakFormElement *element, GType type); + void zak_form_element_add_filter (ZakFormElement *element, ZakFormElementFilter *filter); void zak_form_element_filter (ZakFormElement *element); -- 2.49.0