From 6f4a75ebc6b81da4726782da2a75b58ccf315d70 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sun, 30 Apr 2017 11:17:54 +0200 Subject: [PATCH] Added signals ZakFormElement before and after validating. --- .gitignore | 1 + configure.ac | 5 +++ src/Makefile.am | 8 ++++ src/formelement.c | 81 +++++++++++++++++++++++++++++++--------- src/formelement.h | 3 ++ src/zakform_marshal.list | 1 + 6 files changed, 82 insertions(+), 17 deletions(-) create mode 100644 src/zakform_marshal.list diff --git a/.gitignore b/.gitignore index 8129d06..6fce3bf 100644 --- a/.gitignore +++ b/.gitignore @@ -50,3 +50,4 @@ intltool-* Rules-quot *.exe *.csv +src/*marshal*.[ch] \ No newline at end of file diff --git a/configure.ac b/configure.ac index d86c09b..1f2bffe 100644 --- a/configure.ac +++ b/configure.ac @@ -32,6 +32,11 @@ AC_PROG_RANLIB GTK_DOC_CHECK(1.0) IT_PROG_INTLTOOL +dnl ****************************** +dnl glib-genmarshal +dnl ****************************** +AC_PATH_PROG(GLIB_GENMARSHAL, glib-genmarshal) + dnl ****************************** dnl Translations dnl ****************************** diff --git a/src/Makefile.am b/src/Makefile.am index 1947bbc..022e7ff 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -7,8 +7,15 @@ AM_CPPFLAGS = $(ZAKFORM_CFLAGS) \ lib_LTLIBRARIES = libzakform.la +zakformmarshal.c: zakformmarshal.h zakform_marshal.list $(GLIB_GENMARSHAL) + $(GLIB_GENMARSHAL) $(srcdir)/zakform_marshal.list --body --prefix=_zak_form_marshal > $(srcdir)/$@ + +zakformmarshal.h: zakform_marshal.list $(GLIB_GENMARSHAL) + $(GLIB_GENMARSHAL) $(srcdir)/zakform_marshal.list --header --prefix=_zak_form_marshal > $(srcdir)/$@ + libzakform_la_SOURCES = \ commons.c \ + zakformmarshal.c \ form.c \ formelement.c \ formelementfilter.c \ @@ -28,6 +35,7 @@ libzakform_la_LDFLAGS = -no-undefined libzakform_include_HEADERS = \ libzakform.h \ commons.h \ + zakformmarshal.h \ form.h \ formelement.h \ formelementfilter.h \ diff --git a/src/formelement.c b/src/formelement.c index ce68b32..aa4bf6d 100644 --- a/src/formelement.c +++ b/src/formelement.c @@ -24,6 +24,7 @@ #include +#include "zakformmarshal.h" #include "formelement.h" enum @@ -150,10 +151,10 @@ zak_form_element_class_init (ZakFormElementClass *class) g_object_class_install_property (object_class, PROP_FORMAT, g_param_spec_boxed ("format", - "Format", - "Format", - G_TYPE_HASH_TABLE, - G_PARAM_READWRITE)); + "Format", + "Format", + G_TYPE_HASH_TABLE, + G_PARAM_READWRITE)); g_object_class_install_property (object_class, PROP_VISIBLE, g_param_spec_boolean ("visible", @@ -182,6 +183,36 @@ zak_form_element_class_init (ZakFormElementClass *class) "To save", TRUE, G_PARAM_READWRITE)); + + /** + * ZakFormElement::before_validating: + * @element: + * + */ + class->before_validating_signal_id = g_signal_new ("before-validating", + G_TYPE_FROM_CLASS (object_class), + G_SIGNAL_RUN_LAST, + 0, + NULL, + NULL, + _zak_form_marshal_BOOLEAN__POINTER, + G_TYPE_BOOLEAN, + 1, G_TYPE_POINTER); + + /** + * ZakFormElement::after_validating: + * @element: + * + */ + class->after_validating_signal_id = g_signal_new ("after-validating", + G_TYPE_FROM_CLASS (object_class), + G_SIGNAL_RUN_LAST, + 0, + NULL, + NULL, + _zak_form_marshal_BOOLEAN__POINTER, + G_TYPE_BOOLEAN, + 1, G_TYPE_POINTER); } static void @@ -1167,15 +1198,19 @@ gboolean zak_form_element_is_valid (ZakFormElement *element) { gboolean ret; + gboolean ret_callback; gchar *value; guint i; ZakFormElementPrivate *priv; + ZakFormElementClass *klass = ZAK_FORM_ELEMENT_GET_CLASS (element); + priv = zak_form_element_get_instance_private (element); ret = TRUE; + ret_callback = FALSE; zak_form_element_filter (element); @@ -1185,6 +1220,15 @@ zak_form_element_is_valid (ZakFormElement *element) priv->pa_messages = NULL; } + priv->pa_messages = g_ptr_array_new (); + + /* signal before_validating */ + g_signal_emit (element, klass->before_validating_signal_id, 0, (gpointer)priv->pa_messages, &ret_callback); + if (ret_callback) + { + ret = FALSE; + } + value = zak_form_element_get_value (element); for (i = 0; i < priv->pa_validators->len; i++) @@ -1192,16 +1236,19 @@ zak_form_element_is_valid (ZakFormElement *element) ZakFormElementValidator *validator = (ZakFormElementValidator *)g_ptr_array_index (priv->pa_validators, i); if (!zak_form_element_validator_validate (validator, value)) { - if (priv->pa_messages == NULL) - { - priv->pa_messages = g_ptr_array_new (); - } g_ptr_array_add (priv->pa_messages, (gpointer)g_strdup (zak_form_element_validator_get_message (validator))); ret = FALSE; } } + /* signal after_validating */ + g_signal_emit (element, klass->after_validating_signal_id, 0, (gpointer)priv->pa_messages, &ret_callback); + if (ret_callback) + { + ret = FALSE; + } + return ret; } @@ -1430,27 +1477,27 @@ zak_form_element_xml_parsing (ZakFormElement *element, xmlNode *xmlnode) { to_unlink = FALSE; - if (xmlStrcmp (cur->name, (const xmlChar *)"name") == 0) + if (xmlStrcmp (cur->name, (const xmlChar *)"name") == 0) { zak_form_element_set_name (element, (const gchar *)xmlNodeGetContent (cur)); to_unlink = TRUE; } - else if (xmlStrcmp (cur->name, (const xmlChar *)"is-key") == 0) + else if (xmlStrcmp (cur->name, (const xmlChar *)"is-key") == 0) { zak_form_element_set_is_key (element, xmlStrEqual ((const gchar *)xmlNodeGetContent (cur), "TRUE")); to_unlink = TRUE; } - else if (xmlStrcmp (cur->name, (const xmlChar *)"type") == 0) + else if (xmlStrcmp (cur->name, (const xmlChar *)"type") == 0) { zak_form_element_set_provider_type (element, (const gchar *)xmlNodeGetContent (cur)); to_unlink = TRUE; } - else if (xmlStrcmp (cur->name, (const xmlChar *)"default-value") == 0) + else if (xmlStrcmp (cur->name, (const xmlChar *)"default-value") == 0) { zak_form_element_set_default_value (element, (const gchar *)xmlNodeGetContent (cur)); to_unlink = TRUE; } - else if (xmlStrcmp (cur->name, (const xmlChar *)"format") == 0) + else if (xmlStrcmp (cur->name, (const xmlChar *)"format") == 0) { GHashTable *ht; @@ -1471,22 +1518,22 @@ zak_form_element_xml_parsing (ZakFormElement *element, xmlNode *xmlnode) g_hash_table_unref (ht); to_unlink = TRUE; } - else if (xmlStrcmp (cur->name, (const xmlChar *)"visible") == 0) + else if (xmlStrcmp (cur->name, (const xmlChar *)"visible") == 0) { zak_form_element_set_visible (element, xmlStrEqual ((const gchar *)xmlNodeGetContent (cur), "TRUE")); to_unlink = TRUE; } - else if (xmlStrcmp (cur->name, (const xmlChar *)"editable") == 0) + else if (xmlStrcmp (cur->name, (const xmlChar *)"editable") == 0) { zak_form_element_set_editable (element, xmlStrEqual ((const gchar *)xmlNodeGetContent (cur), "TRUE")); to_unlink = TRUE; } - else if (xmlStrcmp (cur->name, (const xmlChar *)"to-load") == 0) + else if (xmlStrcmp (cur->name, (const xmlChar *)"to-load") == 0) { zak_form_element_set_to_load (element, xmlStrEqual ((const gchar *)xmlNodeGetContent (cur), "TRUE")); to_unlink = TRUE; } - else if (xmlStrcmp (cur->name, (const xmlChar *)"to-save") == 0) + else if (xmlStrcmp (cur->name, (const xmlChar *)"to-save") == 0) { zak_form_element_set_to_save (element, xmlStrEqual ((const gchar *)xmlNodeGetContent (cur), "TRUE")); to_unlink = TRUE; diff --git a/src/formelement.h b/src/formelement.h index 22acdef..dc24082 100644 --- a/src/formelement.h +++ b/src/formelement.h @@ -48,6 +48,9 @@ struct _ZakFormElementClass void (*set_editable) (ZakFormElement *element, gboolean editable); gboolean (*get_editable) (ZakFormElement *element); + + guint before_validating_signal_id; + guint after_validating_signal_id; }; void zak_form_element_set_name (ZakFormElement *element, const gchar *name); diff --git a/src/zakform_marshal.list b/src/zakform_marshal.list new file mode 100644 index 0000000..92c9338 --- /dev/null +++ b/src/zakform_marshal.list @@ -0,0 +1 @@ +BOOLEAN:POINTER -- 2.49.0