]> saetta.ns0.it Git - libzakcgi/commitdiff
Added functions Form::add_element and FormElement::get_id.
authorAndrea Zagli <azagli@libero.it>
Mon, 24 Aug 2015 17:10:03 +0000 (19:10 +0200)
committerAndrea Zagli <azagli@libero.it>
Mon, 24 Aug 2015 17:10:49 +0000 (19:10 +0200)
src/form.c
src/form.h
src/formelement.c
src/formelement.h

index 0efed69919157dcffc0442701072dfd9ed2ad11e..53fa02ecac45df42e9f1eebbde65ebe172d6a742 100644 (file)
@@ -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,
index 785ba7288bbe86873bd6e0ee0c39b145e158692e..4b074fbb03d32b5f267ce5abad8d7ea1548fbcac 100644 (file)
@@ -23,6 +23,7 @@
 #include <glib-object.h>
 
 #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
 
index 647e587ddaaec8c187fb9ce8e6cf3d1a4b11ff0e..1ff8fd054537c81a82222a86057412d2807bdc78 100644 (file)
@@ -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)
 {
index e2ec6eebc04b8844bdb52def17c968185475ae8b..b30c50cc40cdf5c79cafc2db07ebbcfef0aa8660 100644 (file)
@@ -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);