From: Andrea Zagli Date: Mon, 12 Jun 2017 20:11:06 +0000 (+0200) Subject: Start of implementation of FormElementArray. X-Git-Tag: v0.1.0~1^2~11^2~1 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=dead59e2b9c47d1cc95a7261168799cb3d7c65d0;p=zakform%2Flibzakform Start of implementation of FormElementArray. --- diff --git a/src/Makefile.am b/src/Makefile.am index dfa41ba..4b3af31 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -19,6 +19,7 @@ libzakform_la_SOURCES = \ form.c \ formelement.c \ formelementextension.c \ + formelementarray.c \ formelementfilter.c \ formelementfilterluc.c \ formelementfiltertrim.c \ @@ -40,6 +41,7 @@ gir_include = \ form.h \ formelement.h \ formelementextension.h \ + formelementarray.h \ formelementfilter.h \ formelementfilterluc.h \ formelementfiltertrim.h \ diff --git a/src/form.c b/src/form.c index 1c90d3a..7fd21a0 100644 --- a/src/form.c +++ b/src/form.c @@ -179,16 +179,14 @@ zak_form_form_load_from_xml (ZakFormForm *zakform, xmlDoc *xmldoc) cur = xmlDocGetRootElement (xmldoc); if (cur != NULL) { - if (xmlStrcmp (cur->name, (const xmlChar *)"zakform") == 0) + if (xmlStrEqual (cur->name, (const xmlChar *)"zakform")) { ret = TRUE; - priv = zak_form_form_get_instance_private (zakform); - cur = cur->children; while (cur) { - if (xmlStrcmp (cur->name, (const xmlChar *)"element") == 0) + if (xmlStrEqual (cur->name, (const xmlChar *)"element")) { element = NULL; diff --git a/src/formelementarray.c b/src/formelementarray.c new file mode 100644 index 0000000..a69d614 --- /dev/null +++ b/src/formelementarray.c @@ -0,0 +1,312 @@ +/* + * 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 "commons.h" +#include "formelementarray.h" + +static void zak_form_element_array_class_init (ZakFormElementArrayClass *class); +static void zak_form_element_array_init (ZakFormElementArray *zak_form_element_array); + +static void zak_form_element_array_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void zak_form_element_array_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static void zak_form_element_array_dispose (GObject *gobject); +static void zak_form_element_array_finalize (GObject *gobject); + +static GValue *zak_form_element_array_get_value (ZakFormElementArray *element); +static gboolean zak_form_element_array_set_value (ZakFormElementArray *element, GValue *value); +static void zak_form_element_array_set_as_original_value (ZakFormElement *element); + +#define ZAK_FORM_ELEMENT_ARRAY_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ZAK_FORM_TYPE_ELEMENT_ARRAY, ZakFormElementArrayPrivate)) + +typedef struct _ZakFormElementArrayPrivate ZakFormElementArrayPrivate; +struct _ZakFormElementArrayPrivate + { + GPtrArray *ar_elements; + }; + +G_DEFINE_TYPE (ZakFormElementArray, zak_form_element_array, ZAK_FORM_TYPE_ELEMENT) + +static void +zak_form_element_array_class_init (ZakFormElementArrayClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + ZakFormElementClass *elem_class = ZAK_FORM_ELEMENT_CLASS (klass); + + object_class->set_property = zak_form_element_array_set_property; + object_class->get_property = zak_form_element_array_get_property; + object_class->dispose = zak_form_element_array_dispose; + object_class->finalize = zak_form_element_array_finalize; + + elem_class->get_value = zak_form_element_array_get_value; + elem_class->set_value = zak_form_element_array_set_value; + elem_class->set_as_original_value = zak_form_element_array_set_as_original_value; + + elem_class->xml_parsing = zak_form_element_array_xml_parsing; + + g_type_class_add_private (object_class, sizeof (ZakFormElementArrayPrivate)); + + /** + * ZakFormElementArray::element-add: + * @element: + * + */ + klass->element_added_signal_id = g_signal_new ("element-added", + G_TYPE_FROM_CLASS (object_class), + G_SIGNAL_RUN_LAST, + 0, + NULL, + NULL, + g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, + 1, G_TYPE_OBJECT); +} + +static void +zak_form_element_array_init (ZakFormElementArray *zak_form_element_array) +{ + ZakFormElementArrayPrivate *priv = ZAK_FORM_ELEMENT_ARRAY_GET_PRIVATE (zak_form_element_array); + + priv->ar_elements = g_ptr_array_new (); +} + +/** + * zak_form_element_array_new: + * + * Returns: the newly created #ZakFormElementArray object. + */ +ZakFormElement +*zak_form_element_array_new () +{ + ZakFormElementArray *zak_form_element_array; + + zak_form_element_array = ZAK_FORM_ELEMENT_ARRAY (g_object_new (zak_form_element_array_get_type (), NULL)); + + return ZAK_FORM_ELEMENT (zak_form_element_array); +} + +/** + * zak_form_element_array_xml_parsing: + * @element: + * @xmlnode: + * + */ +void +zak_form_element_array_xml_parsing (ZakFormElement *element, xmlNodePtr xmlnode) +{ + xmlNode *cur; + xmlNode *cur_clean; + + gchar *type; + + ZakFormElement *element_to_add; + + ZakFormElementConstructorFunc element_constructor; + + cur = xmlnode->children; + while (cur != NULL) + { + if (xmlStrEqual (cur->name, (const xmlChar *)"signal-element-added")) + { + GModule *module; + GCallback func; + gchar *handler_name; + + module = g_module_open (NULL, G_MODULE_BIND_LAZY); + if (module != NULL) + { + handler_name = (gchar *)xmlNodeGetContent (cur); + + if (!g_module_symbol (module, handler_name, (gpointer)&func)) + { + g_warning (_("Could not find signal handler '%s'."), handler_name); + } + else + { + g_signal_connect (element, "element-added", func, NULL); + } + + g_free (handler_name); + } + } + else if (xmlStrEqual (cur->name, (const xmlChar *)"element")) + { + element_to_add = NULL; + + type = (gchar *)xmlGetProp (cur, (const xmlChar *)"type"); + + element_constructor = zak_form_get_form_element (type); + if (element_constructor != NULL) + { + element_to_add = element_constructor (); + if (element_to_add != NULL) + { + zak_form_element_array_add_element (element, element_to_add); + + cur_clean = xmlCopyNode (cur, 1); + zak_form_element_xml_parsing (element_to_add, cur_clean); + + xmlUnlinkNode (cur_clean); + xmlFreeNode (cur_clean); + } + } + else + { + g_warning (_("Unknown element type «%s»."), type); + } + } + + cur = cur->next; + } +} + +/** + * zak_form_element_array_add_element: + * @element: + * @element_to_add: + * + */ +void +zak_form_element_array_add_element (ZakFormElement *element, ZakFormElement *element_to_add) +{ + ZakFormElementArrayClass *klass = ZAK_FORM_ELEMENT_ARRAY_GET_CLASS (element); + ZakFormElementArrayPrivate *priv = ZAK_FORM_ELEMENT_ARRAY_GET_PRIVATE (element); + + g_ptr_array_add (priv->ar_elements, element_to_add); + + g_signal_emit (G_OBJECT (element), klass->element_added_signal_id, 0, element_to_add); +} + +/** + * zak_form_element_array_get_elements: + * @element: + * + * Returns: + */ +GPtrArray +*zak_form_element_array_get_elements (ZakFormElement *element) +{ + ZakFormElementArrayPrivate *priv = ZAK_FORM_ELEMENT_ARRAY_GET_PRIVATE (ZAK_FORM_ELEMENT_ARRAY (element)); + + return priv->ar_elements; +} + +/* PRIVATE */ +static void +zak_form_element_array_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + ZakFormElementArray *zak_form_element_array = (ZakFormElementArray *)object; + ZakFormElementArrayPrivate *priv = ZAK_FORM_ELEMENT_ARRAY_GET_PRIVATE (zak_form_element_array); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_form_element_array_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) +{ + ZakFormElementArray *zak_form_element_array = (ZakFormElementArray *)object; + ZakFormElementArrayPrivate *priv = ZAK_FORM_ELEMENT_ARRAY_GET_PRIVATE (zak_form_element_array); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_form_element_array_dispose (GObject *gobject) +{ + ZakFormElementArray *zak_form_element_array = (ZakFormElementArray *)gobject; + ZakFormElementArrayPrivate *priv = ZAK_FORM_ELEMENT_ARRAY_GET_PRIVATE (zak_form_element_array); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->dispose (gobject); +} + +static void +zak_form_element_array_finalize (GObject *gobject) +{ + ZakFormElementArray *zak_form_element_array = (ZakFormElementArray *)gobject; + ZakFormElementArrayPrivate *priv = ZAK_FORM_ELEMENT_ARRAY_GET_PRIVATE (zak_form_element_array); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->finalize (gobject); +} + +static GValue +*zak_form_element_array_get_value (ZakFormElementArray *element) +{ + GValue *ret; + + + return ret; +} + +static gboolean +zak_form_element_array_set_value (ZakFormElementArray *element, GValue *value) +{ + + + return TRUE; +} + +static void +zak_form_element_array_set_as_original_value (ZakFormElement *element) +{ + guint i; + + ZakFormElementArrayPrivate *priv = ZAK_FORM_ELEMENT_ARRAY_GET_PRIVATE (element); + + for (i = 0; i < priv->ar_elements->len; i++) + { + ZakFormElement *element = (ZakFormElement *)g_ptr_array_index (priv->ar_elements, i); + + zak_form_element_set_as_original_value (element); + } +} diff --git a/src/formelementarray.h b/src/formelementarray.h new file mode 100644 index 0000000..90845b2 --- /dev/null +++ b/src/formelementarray.h @@ -0,0 +1,68 @@ +/* + * 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_ARRAY_H__ +#define __ZAK_FORM_ELEMENT_ARRAY_H__ + + +#include + +#include "formelement.h" + + +G_BEGIN_DECLS + + +#define ZAK_FORM_TYPE_ELEMENT_ARRAY (zak_form_element_array_get_type ()) +#define ZAK_FORM_ELEMENT_ARRAY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ZAK_FORM_TYPE_ELEMENT_ARRAY, ZakFormElementArray)) +#define ZAK_FORM_ELEMENT_ARRAY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ZAK_FORM_TYPE_ELEMENT_ARRAY, ZakFormElementArrayClass)) +#define ZAK_FORM_IS_ELEMENT_ARRAY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ZAK_FORM_TYPE_ELEMENT_ARRAY)) +#define ZAK_FORM_IS_ELEMENT_ARRAY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ZAK_FORM_TYPE_ELEMENT_ARRAY)) +#define ZAK_FORM_ELEMENT_ARRAY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), ZAK_FORM_TYPE_ELEMENT_ARRAY, ZakFormElementArrayClass)) + +typedef struct _ZakFormElementArray ZakFormElementArray; +typedef struct _ZakFormElementArrayClass ZakFormElementArrayClass; + +struct _ZakFormElementArray + { + ZakFormElement parent_instance; + }; + +struct _ZakFormElementArrayClass + { + ZakFormElementClass parent_class; + + guint element_added_signal_id; + }; + + +GType zak_form_element_array_get_type (void); + +ZakFormElement *zak_form_element_array_new (void); + +void zak_form_element_array_xml_parsing (ZakFormElement *element, xmlNodePtr xmlnode); + +void zak_form_element_array_add_element (ZakFormElement *element, ZakFormElement *element_to_add); + +GPtrArray *zak_form_element_array_get_elements (ZakFormElement *element); + + +G_END_DECLS + + +#endif /* __ZAK_FORM_ELEMENT_ARRAY_H__ */ diff --git a/src/libzakform.h b/src/libzakform.h index da08276..9e30e6f 100644 --- a/src/libzakform.h +++ b/src/libzakform.h @@ -24,6 +24,8 @@ #include +#include + #include #include