Added FormElement extensions.
authorAndrea Zagli <azagli@libero.it>
Mon, 13 Feb 2017 17:16:15 +0000 (18:16 +0100)
committerAndrea Zagli <azagli@libero.it>
Mon, 13 Feb 2017 17:16:15 +0000 (18:16 +0100)
src/form.c
src/formelement.c
src/formelement.h

index 65d27d87b9ffd0ed73e504f0083960a96b05b043..c1b54cffdc16d9a3abb6f6a1f45bed5b8bd5002d 100644 (file)
@@ -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)
                                                                                                                {
index bfc4e20adc654d08dce092af810b0c7c1cecc728..9e6db5e54da0a3f4ab73056c5ea7f2e7b4a8d912 100644 (file)
@@ -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:
index 48035332280000dedc4c49af5074cbb2bced7cb3..d68f6a518e3b48626ca47fabdc12a53cde25dbbe 100644 (file)
@@ -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);