From: Andrea Zagli Date: Thu, 5 Nov 2015 19:38:48 +0000 (+0100) Subject: Added functions ZakFormForm::load_from_xml and ::load_from_file. X-Git-Tag: debian/0.0.1-1~40 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=854ecb76fd6d967452d316ad97ec50558dbc9133;p=zakform%2Flibzakform Added functions ZakFormForm::load_from_xml and ::load_from_file. --- diff --git a/configure.ac b/configure.ac index b05d446..5d14ebb 100644 --- a/configure.ac +++ b/configure.ac @@ -41,7 +41,8 @@ AM_GLIB_GNU_GETTEXT # Checks for libraries. PKG_CHECK_MODULES(ZAKFORM, [glib-2.0 >= 2.36 gobject-2.0 >= 2.36 - gmodule-2.0 >= 2.0.0]) + gmodule-2.0 >= 2.0.0 + libxml-2.0 >= 2.0.0]) AC_SUBST(ZAKFORM_CFLAGS) AC_SUBST(ZAKFORM_LIBS) diff --git a/libzakform.pc.in b/libzakform.pc.in index 0c0cf06..9cfc80a 100644 --- a/libzakform.pc.in +++ b/libzakform.pc.in @@ -7,6 +7,6 @@ modulesdir=@libdir@/@PACKAGE@/modules Name: @PACKAGE_NAME@ Description: Class for form. Version: @PACKAGE_VERSION@ -Requires: glib-2.0 >= 2.36 gobject-2.0 +Requires: glib-2.0 >= 2.36 gobject-2.0 gmodule-2.0 libxml-2.0 Libs: -L${libdir} -lzakform Cflags: -I${includedir} diff --git a/src/form.c b/src/form.c index 166262b..1661830 100644 --- a/src/form.c +++ b/src/form.c @@ -23,12 +23,18 @@ #include #include +#include + #include "form.h" +#include "formelement.h" #ifdef G_OS_WIN32 #include #endif +typedef ZakFormElement *(* FormElementConstructorFunc) (void); +typedef gboolean (* FormElementXmlParsingFunc) (ZakFormElement *, xmlNodePtr); + static void zak_form_form_class_init (ZakFormFormClass *class); static void zak_form_form_init (ZakFormForm *zak_form_form); @@ -136,6 +142,123 @@ zak_form_form_init (ZakFormForm *zak_form_form) zak_form_form_load_modules (zak_form_form); } +/** + * zak_form_form_load_from_xml: + * @zakform: + * @xmldoc: + * + */ +gboolean +zak_form_form_load_from_xml (ZakFormForm *zakform, xmlDoc *xmldoc) +{ + ZakFormFormPrivate *priv; + xmlNode *cur; + + gboolean ret; + + ZakFormElement *element; + gchar *type; + guint i; + + FormElementConstructorFunc element_constructor; + FormElementXmlParsingFunc element_xml_parsing; + + g_return_val_if_fail (ZAK_FORM_IS_FORM (zakform), FALSE); + g_return_val_if_fail (xmldoc != NULL, FALSE); + + ret = FALSE; + + cur = xmlDocGetRootElement (xmldoc); + if (cur != NULL) + { + if (xmlStrcmp (cur->name, (const xmlChar *)"zakform") == 0) + { + ret = TRUE; + + priv = zak_form_form_get_instance_private (zakform); + + cur = cur->children; + while (cur) + { + if (xmlStrcmp (cur->name, (const xmlChar *)"element") == 0) + { + element = NULL; + + type = xmlGetProp (cur, (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 *)&element_constructor)) + { + if (element_constructor != NULL) + { + element = element_constructor (); + if (element != NULL) + { + zak_form_form_add_element (zakform, element); + if (g_module_symbol ((GModule *)g_ptr_array_index (priv->ar_modules, i), + g_strconcat (type, "_xml_parsing", NULL), + (gpointer *)&element_xml_parsing)) + { + if (element_xml_parsing != NULL) + { + element_xml_parsing (element, cur); + } + } + } + break; + } + } + } + + if (element == NULL) + { + g_warning (_("Unknown element type «%s»."), type); + } + } + + cur = cur->next; + } + } + else + { + g_warning (_("The file is not a valid ZakForm definition file.")); + } + } + + return ret; +} + +/** + * zak_form_form_load_from_file: + * @zakform: + * @filename: + * + */ +gboolean +zak_form_form_load_from_file (ZakFormForm *zakform, const gchar *filename) +{ + xmlDoc *xdoc; + + gboolean ret; + + g_return_val_if_fail (ZAK_FORM_IS_FORM (zakform), FALSE); + g_return_val_if_fail (filename != NULL, FALSE); + + ret = FALSE; + + xdoc = xmlParseFile (filename); + if (xdoc != NULL) + { + ret = zak_form_form_load_from_xml (zakform, xdoc); + } + + return ret; +} + /** * zak_form_form_add_element: * @zakform: diff --git a/src/form.h b/src/form.h index f1179b6..0bb4bdc 100644 --- a/src/form.h +++ b/src/form.h @@ -22,6 +22,8 @@ #include +#include + #include "formelement.h" @@ -39,6 +41,9 @@ struct _ZakFormFormClass }; +gboolean zak_form_form_load_from_xml (ZakFormForm *zakform, xmlDoc *xmldoc); +gboolean zak_form_form_load_from_file (ZakFormForm *zakform, const gchar *filename); + gboolean zak_form_form_add_element (ZakFormForm *zakform, ZakFormElement *element); void zak_form_form_clear (ZakFormForm *zakform);