From: Andrea Zagli Date: Wed, 26 Aug 2015 13:35:30 +0000 (+0200) Subject: Form: started rendering. X-Git-Tag: v0.1.0~3^2~50 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=1e7b7f313efcc9e45d1481a1d19d1f263e00853f;p=libzakcgi Form: started rendering. --- diff --git a/.gitignore b/.gitignore index 162ccb3..6affd6d 100644 --- a/.gitignore +++ b/.gitignore @@ -52,6 +52,7 @@ Rules-quot *.csv tests/cookies tests/env +tests/form tests/querystring tests/redirect tests/session diff --git a/src/form.c b/src/form.c index 53fa02e..7b1862a 100644 --- a/src/form.c +++ b/src/form.c @@ -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 ("
"); + + 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, "
"); + + return g_strdup (str->str); +} + /* PRIVATE */ static void zak_cgi_form_set_property (GObject *object, diff --git a/src/form.h b/src/form.h index 4b074fb..1595294 100644 --- a/src/form.h +++ b/src/form.h @@ -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 diff --git a/src/formelement.c b/src/formelement.c index 1f30f21..60cbc84 100644 --- a/src/formelement.c +++ b/src/formelement.c @@ -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, diff --git a/src/formelement.h b/src/formelement.h index b30c50c..32f15e4 100644 --- a/src/formelement.h +++ b/src/formelement.h @@ -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); diff --git a/src/formelementtext.c b/src/formelementtext.c index 0476b27..b376ad7 100644 --- a/src/formelementtext.c +++ b/src/formelementtext.c @@ -22,11 +22,17 @@ #include +#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; diff --git a/src/formelementtext.h b/src/formelementtext.h index c05935e..957c687 100644 --- a/src/formelementtext.h +++ b/src/formelementtext.h @@ -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 diff --git a/tests/Makefile.am b/tests/Makefile.am index a5b823b..6a2552d 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 index 0000000..18d6dde --- /dev/null +++ b/tests/form.c @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2015 Andrea Zagli + * + * 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 +#include +#include + +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; +}