From: Andrea Zagli Date: Mon, 24 Aug 2015 17:10:03 +0000 (+0200) Subject: Added functions Form::add_element and FormElement::get_id. X-Git-Tag: v0.1.0~3^2~54 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=33883432077313b2aa9e8154dd2e65140467ff37;p=libzakcgi Added functions Form::add_element and FormElement::get_id. --- diff --git a/src/form.c b/src/form.c index 0efed69..53fa02e 100644 --- a/src/form.c +++ b/src/form.c @@ -45,6 +45,7 @@ typedef struct _ZakCgiFormPrivate ZakCgiFormPrivate; struct _ZakCgiFormPrivate { ZakCgiMain *zakcgimain; + GHashTable *ht_elems; }; G_DEFINE_TYPE (ZakCgiForm, zak_cgi_form, G_TYPE_OBJECT) @@ -68,6 +69,7 @@ zak_cgi_form_init (ZakCgiForm *zak_cgi_form) ZakCgiFormPrivate *priv = ZAK_CGI_FORM_GET_PRIVATE (zak_cgi_form); priv->zakcgimain = NULL; + priv->ht_elems = g_hash_table_new (g_str_hash, g_str_equal); } /** @@ -90,6 +92,41 @@ ZakCgiForm return zak_cgi_form; } +/** + * zak_cgi_form_add_element: + * @zakcgiform: + * @element: + * + * Returns: #TRUE if @element is added; FALSE otherwise. + */ +gboolean +zak_cgi_form_add_element (ZakCgiForm *zakcgiform, ZakCgiFormElement *element) +{ + gboolean ret; + gchar *id; + + ZakCgiFormPrivate *priv; + + priv = ZAK_CGI_FORM_GET_PRIVATE (zakcgiform); + + id = zak_cgi_form_element_get_id (element); + + if (g_hash_table_lookup (priv->ht_elems, id)) + { + g_warning ("You cannot add an element with id already present in the form."); + ret = FALSE; + } + else + { + g_hash_table_insert (priv->ht_elems, g_strdup (id), element); + ret = TRUE; + } + + g_free (id); + + return ret; +} + /* PRIVATE */ static void zak_cgi_form_set_property (GObject *object, diff --git a/src/form.h b/src/form.h index 785ba72..4b074fb 100644 --- a/src/form.h +++ b/src/form.h @@ -23,6 +23,7 @@ #include #include "main.h" +#include "formelement.h" G_BEGIN_DECLS @@ -53,6 +54,8 @@ GType zak_cgi_form_get_type (void); ZakCgiForm *zak_cgi_form_new (ZakCgiMain *zakcgimain); +gboolean zak_cgi_form_add_element (ZakCgiForm *zakcgiform, ZakCgiFormElement *element); + G_END_DECLS diff --git a/src/formelement.c b/src/formelement.c index 647e587..1ff8fd0 100644 --- a/src/formelement.c +++ b/src/formelement.c @@ -147,6 +147,16 @@ ZakCgiFormElement return zak_cgi_form_element; } +gchar +*zak_cgi_form_element_get_id (ZakCgiFormElement *element) +{ + ZakCgiFormElementPrivate *priv; + + priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (element); + + return g_strdup (priv->id); +} + void zak_cgi_form_element_set_validation_regex (ZakCgiFormElement *element, const gchar *validation_regex) { diff --git a/src/formelement.h b/src/formelement.h index e2ec6ee..b30c50c 100644 --- a/src/formelement.h +++ b/src/formelement.h @@ -56,6 +56,8 @@ GType zak_cgi_form_element_get_type (void); ZakCgiFormElement *zak_cgi_form_element_new (const gchar *id, const gchar *validation_regex, ...); +gchar *zak_cgi_form_element_get_id (ZakCgiFormElement *element); + void zak_cgi_form_element_set_validation_regex (ZakCgiFormElement *element, const gchar *validation_regex); gchar *zak_cgi_form_element_get_validation_regex (ZakCgiFormElement *element);