]> saetta.ns0.it Git - libzakcgi/commitdiff
Form: started rendering.
authorAndrea Zagli <azagli@libero.it>
Wed, 26 Aug 2015 13:35:30 +0000 (15:35 +0200)
committerAndrea Zagli <azagli@libero.it>
Wed, 26 Aug 2015 13:35:30 +0000 (15:35 +0200)
.gitignore
src/form.c
src/form.h
src/formelement.c
src/formelement.h
src/formelementtext.c
src/formelementtext.h
tests/Makefile.am
tests/form.c [new file with mode: 0644]

index 162ccb3f022077cbadf9a53bcd65a0251e4a6486..6affd6dc69c98c17885c9ecb700f2188e85d2fa8 100644 (file)
@@ -52,6 +52,7 @@ Rules-quot
 *.csv
 tests/cookies
 tests/env
+tests/form
 tests/querystring
 tests/redirect
 tests/session
index 53fa02ecac45df42e9f1eebbde65ebe172d6a742..7b1862ab4a77264f9aec444c73f36af0dae1d38b 100644 (file)
@@ -118,7 +118,7 @@ zak_cgi_form_add_element (ZakCgiForm *zakcgiform, ZakCgiFormElement *element)
                }
        else
                {
-                       g_hash_table_insert (priv->ht_elems, g_strdup (id), element);
+                       g_hash_table_insert (priv->ht_elems, g_strdup (id), g_object_ref (element));
                        ret = TRUE;
                }
 
@@ -127,6 +127,38 @@ zak_cgi_form_add_element (ZakCgiForm *zakcgiform, ZakCgiFormElement *element)
        return ret;
 }
 
+/**
+ * zak_cgi_form_render:
+ * @zakcgiform:
+ *
+ * Returns:
+ */
+gchar
+*zak_cgi_form_render (ZakCgiForm *zakcgiform)
+{
+       GString *str;
+
+       GHashTableIter iter;
+       gpointer key;
+       gpointer value;
+
+       ZakCgiFormPrivate *priv;
+
+       priv = ZAK_CGI_FORM_GET_PRIVATE (zakcgiform);
+
+       str = g_string_new ("<form>");
+
+       g_hash_table_iter_init (&iter, priv->ht_elems);
+       while (g_hash_table_iter_next (&iter, &key, &value))
+               {
+                       g_string_append_printf (str, "\n%s", zak_cgi_form_element_render ((ZakCgiFormElement *)value));
+               }
+
+       g_string_append (str, "</form>");
+
+       return g_strdup (str->str);
+}
+
 /* PRIVATE */
 static void
 zak_cgi_form_set_property (GObject *object,
index 4b074fbb03d32b5f267ce5abad8d7ea1548fbcac..15952942e82e876fb43b3aa3ee29c3e5e37a786a 100644 (file)
@@ -56,6 +56,8 @@ ZakCgiForm *zak_cgi_form_new (ZakCgiMain *zakcgimain);
 
 gboolean zak_cgi_form_add_element (ZakCgiForm *zakcgiform, ZakCgiFormElement *element);
 
+gchar *zak_cgi_form_render (ZakCgiForm *zakcgiform);
+
 
 G_END_DECLS
 
index 1f30f21cfdce8cd145efc41943d9393dc466fc5c..60cbc840c3521dda3979649f819001a72e32996e 100644 (file)
@@ -34,6 +34,13 @@ enum
 static void zak_cgi_form_element_class_init (ZakCgiFormElementClass *class);
 static void zak_cgi_form_element_init (ZakCgiFormElement *zak_cgi_form_element);
 
+static void zak_cgi_form_element_construct (ZakCgiFormElement *element,
+                                                                                       const gchar *id,
+                                                                                       const gchar *validation_regex,
+                                                                                       GHashTable *ht_attrs);
+
+static GHashTable *zak_cgi_form_element_get_ht_attrs (ZakCgiFormElement *element);
+
 static void zak_cgi_form_element_set_property (GObject *object,
                                guint property_id,
                                const GValue *value,
@@ -76,6 +83,9 @@ zak_cgi_form_element_class_init (ZakCgiFormElementClass *class)
                                                              "Validation regex",
                                                              "",
                                                              G_PARAM_READWRITE));
+
+       class->construct = zak_cgi_form_element_construct;
+       class->get_ht_attrs = zak_cgi_form_element_get_ht_attrs;
 }
 
 static void
@@ -87,50 +97,6 @@ zak_cgi_form_element_init (ZakCgiFormElement *zak_cgi_form_element)
        priv->ht_attrs = NULL;
 }
 
-/**
- * zak_cgi_form_element_new:
- * @id:
- * @validation_regex:
- * @...:
- *
- * Returns: the newly created #ZakCgiFormElement object.
- */
-ZakCgiFormElement
-*zak_cgi_form_element_new (const gchar *id,
-                                                  const gchar *validation_regex,
-                                                  ...)
-{
-       va_list ap;
-
-       ZakCgiFormElement *zak_cgi_form_element;
-       ZakCgiFormElementPrivate *priv;
-
-       gchar *attr;
-       gchar *attr_value;
-
-       g_return_val_if_fail (id != NULL, NULL);
-
-       zak_cgi_form_element = ZAK_CGI_FORM_ELEMENT (g_object_new (zak_cgi_form_element_get_type (), NULL));
-
-       priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (zak_cgi_form_element);
-
-       priv->id = g_strdup (id);
-
-       zak_cgi_form_element_set_validation_regex (zak_cgi_form_element, validation_regex);
-
-       va_start (ap, validation_regex);
-       priv->ht_attrs = zak_cgi_commons_valist_to_ghashtable (ap);
-
-       g_hash_table_insert (priv->ht_attrs, "id", g_strdup (id));
-
-       if (g_hash_table_lookup (priv->ht_attrs, "name") == NULL)
-               {
-                       g_hash_table_insert (priv->ht_attrs, "name", g_strdup (id));
-               }
-
-       return zak_cgi_form_element;
-}
-
 gchar
 *zak_cgi_form_element_get_id (ZakCgiFormElement *element)
 {
@@ -197,7 +163,7 @@ gchar
 
        if (ZAK_CGI_IS_FORM_ELEMENT (element) && ZAK_CGI_FORM_ELEMENT_GET_CLASS (element)->render != NULL)
                {
-                       ret = ZAK_CGI_FORM_ELEMENT_CLASS (element)->render (element);
+                       ret = ZAK_CGI_FORM_ELEMENT_GET_CLASS (element)->render (element);
                }
 
        return ret;
@@ -217,13 +183,49 @@ zak_cgi_form_element_is_valid (ZakCgiFormElement *element)
 
        if (ZAK_CGI_IS_FORM_ELEMENT (element) && ZAK_CGI_FORM_ELEMENT_GET_CLASS (element)->is_valid != NULL)
                {
-                       ret = ZAK_CGI_FORM_ELEMENT_CLASS (element)->is_valid (element);
+                       ret = ZAK_CGI_FORM_ELEMENT_GET_CLASS (element)->is_valid (element);
                }
 
        return ret;
 }
 
 /* PRIVATE */
+static void
+zak_cgi_form_element_construct (ZakCgiFormElement *element,
+                                                               const gchar *id,
+                                                               const gchar *validation_regex,
+                                                               GHashTable *ht_attrs)
+{
+       ZakCgiFormElementPrivate *priv;
+
+       g_return_val_if_fail (id != NULL, NULL);
+
+       priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (element);
+
+       priv->id = g_strdup (id);
+
+       zak_cgi_form_element_set_validation_regex (element, validation_regex);
+
+       priv->ht_attrs = ht_attrs;
+
+       if (g_hash_table_lookup (priv->ht_attrs, "name") == NULL)
+               {
+                       g_hash_table_insert (priv->ht_attrs, "name", g_strdup (id));
+               }
+
+       return;
+}
+
+static GHashTable
+*zak_cgi_form_element_get_ht_attrs (ZakCgiFormElement *element)
+{
+       ZakCgiFormElementPrivate *priv;
+
+       priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (element);
+
+       return priv->ht_attrs;
+}
+
 static void
 zak_cgi_form_element_set_property (GObject *object,
                    guint property_id,
index b30c50cc40cdf5c79cafc2db07ebbcfef0aa8660..32f15e4acd2b8448b2d0da09eb4763f23241b571 100644 (file)
@@ -47,6 +47,9 @@ struct _ZakCgiFormElementClass
        {
                GObjectClass parent_class;
 
+               void (*construct) (ZakCgiFormElement *element, const gchar *id, const gchar *validation_regex, GHashTable *ht_attrs);
+               GHashTable *(*get_ht_attrs) (ZakCgiFormElement *element);
+
                gchar *(*render) (ZakCgiFormElement *element);
                gboolean (*is_valid) (ZakCgiFormElement *element);
        };
@@ -54,8 +57,6 @@ struct _ZakCgiFormElementClass
 
 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);
index 0476b27a74925dab8e6c56452515a1d21383a887..b376ad783031817032ee5bb0ca17c0029c2ec215 100644 (file)
 
 #include <syslog.h>
 
+#include "commons.h"
+#include "tag.h"
 #include "formelementtext.h"
 
 static void zak_cgi_form_element_text_class_init (ZakCgiFormElementTextClass *class);
 static void zak_cgi_form_element_text_init (ZakCgiFormElementText *zak_cgi_form_element_text);
 
+static gchar *zak_cgi_form_element_text_render (ZakCgiFormElement *element);
+
+static gboolean zak_cgi_form_element_text_is_valid (ZakCgiFormElement *element);
+
 static void zak_cgi_form_element_text_set_property (GObject *object,
                                guint property_id,
                                const GValue *value,
@@ -44,20 +50,25 @@ static void zak_cgi_form_element_text_finalize (GObject *gobject);
 typedef struct _ZakCgiFormElementTextPrivate ZakCgiFormElementTextPrivate;
 struct _ZakCgiFormElementTextPrivate
        {
+               gpointer empty;
        };
 
 G_DEFINE_TYPE (ZakCgiFormElementText, zak_cgi_form_element_text, ZAK_CGI_TYPE_FORM_ELEMENT)
 
 static void
-zak_cgi_form_element_text_class_init (ZakCgiFormElementTextClass *class)
+zak_cgi_form_element_text_class_init (ZakCgiFormElementTextClass *klass)
 {
-       GObjectClass *object_class = G_OBJECT_CLASS (class);
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       ZakCgiFormElementClass *elem_class = ZAK_CGI_FORM_ELEMENT_CLASS (klass);
 
        object_class->set_property = zak_cgi_form_element_text_set_property;
        object_class->get_property = zak_cgi_form_element_text_get_property;
        object_class->dispose = zak_cgi_form_element_text_dispose;
        object_class->finalize = zak_cgi_form_element_text_finalize;
 
+       elem_class->render = zak_cgi_form_element_text_render;
+       elem_class->is_valid = zak_cgi_form_element_text_is_valid;
+
        g_type_class_add_private (object_class, sizeof (ZakCgiFormElementTextPrivate));
 }
 
@@ -70,48 +81,52 @@ zak_cgi_form_element_text_init (ZakCgiFormElementText *zak_cgi_form_element_text
 
 /**
  * zak_cgi_form_element_text_new:
+ * @id:
  * @validation_regex:
+ * @...:
  *
  * Returns: the newly created #ZakCgiFormElementText object.
  */
-ZakCgiFormElementText
-*zak_cgi_form_element_text_new (const gchar *validation_regex)
+ZakCgiFormElement
+*zak_cgi_form_element_text_new (const gchar *id,
+                                                               const gchar *validation_regex,
+                                                               ...)
 {
+       va_list ap;
+
        ZakCgiFormElementText *zak_cgi_form_element_text;
-       ZakCgiFormElementTextPrivate *priv;
 
        zak_cgi_form_element_text = ZAK_CGI_FORM_ELEMENT_TEXT (g_object_new (zak_cgi_form_element_text_get_type (), NULL));
 
-       priv = ZAK_CGI_FORM_ELEMENT_TEXT_GET_PRIVATE (zak_cgi_form_element_text);
+       va_start (ap, validation_regex);
 
-       if (validation_regex != NULL)
-               {
-               }
+       ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_text_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_text),
+                                                                                                                                                                       id,
+                                                                                                                                                                       validation_regex, zak_cgi_commons_valist_to_ghashtable (ap));
 
-       return zak_cgi_form_element_text;
+       return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_text);
 }
 
-/**
- * zak_cgi_form_element_text_render:
- * @element_text:
- *
- */
-gchar
-*zak_cgi_form_element_text_render (ZakCgiFormElementText *element_text)
+static gchar
+*zak_cgi_form_element_text_render (ZakCgiFormElement *element)
 {
        gchar *ret;
 
+       GHashTable *ht_attrs;
+
+       ZakCgiFormElementClass *klass;
+
+       klass = (ZakCgiFormElementClass *)g_type_class_peek_parent (ZAK_CGI_FORM_ELEMENT_TEXT_GET_CLASS (ZAK_CGI_FORM_ELEMENT_TEXT (element)));
+
+       ht_attrs = klass->get_ht_attrs (element);
+
+       ret = zak_cgi_tag_text_ht (zak_cgi_form_element_get_id (element), ht_attrs);
 
        return ret;
 }
 
-/**
- * zak_cgi_form_element_text_is_valid:
- * @element_text:
- *
- */
-gboolean
-zak_cgi_form_element_text_is_valid (ZakCgiFormElementText *element_text)
+static gboolean
+zak_cgi_form_element_text_is_valid (ZakCgiFormElement *element)
 {
        gboolean ret;
 
index c05935ec89a291d65fb40d28d01b497c47cbd2b5..957c68788801cc13bfd897ae4851ac28d2bce78c 100644 (file)
@@ -51,11 +51,7 @@ struct _ZakCgiFormElementTextClass
 
 GType zak_cgi_form_element_text_get_type (void);
 
-ZakCgiFormElementText *zak_cgi_form_element_text_new (const gchar *validation_regex);
-
-gchar *zak_cgi_form_element_text_render (ZakCgiFormElementText *element_text);
-
-gboolean zak_cgi_form_element_text_is_valid (ZakCgiFormElementText *element_text);
+ZakCgiFormElement *zak_cgi_form_element_text_new (const gchar *id, const gchar *validation_regex, ...);
 
 
 G_END_DECLS
index a5b823b291287289e189c562795cd173cd3aa82e..6a2552d0b70c39570a369fedcee1c7d8a42b30b3 100644 (file)
@@ -11,6 +11,7 @@ LDADD = $(top_builddir)/src/libzakcgi.la
 noinst_PROGRAMS = \
                     cookies \
                     env \
+                    form \
                     querystring \
                     redirect \
                     session \
diff --git a/tests/form.c b/tests/form.c
new file mode 100644 (file)
index 0000000..18d6dde
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2015 Andrea Zagli <azagli@libero.it>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU 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 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
+ */
+
+#include <form.h>
+#include <formelement.h>
+#include <formelementtext.h>
+
+int
+main (int argc, char *argv[])
+{
+       GString *str;
+
+       ZakCgiForm *form;
+       ZakCgiFormElement *element;
+
+       form = zak_cgi_form_new (NULL);
+
+       str = g_string_new ("");
+
+       element = zak_cgi_form_element_text_new ("first", "aaa", NULL);
+       zak_cgi_form_add_element (form, element);
+
+       g_string_append (str, zak_cgi_form_render (form));
+
+       zak_cgi_main_out (NULL, str->str);
+
+       return 0;
+}