From 5ba180632fa061e0b930ad583791f14787539e35 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sat, 12 Sep 2015 13:06:21 +0200 Subject: [PATCH] Added ZakCgiFormElementTextArea. --- src/Makefile.am | 2 + src/formelementtextarea.c | 260 ++++++++++++++++++++++++++++++++++++++ src/formelementtextarea.h | 60 +++++++++ src/tag.c | 9 +- tests/form.c | 5 + tests/tag.c | 3 + 6 files changed, 338 insertions(+), 1 deletion(-) create mode 100644 src/formelementtextarea.c create mode 100644 src/formelementtextarea.h diff --git a/src/Makefile.am b/src/Makefile.am index 10dc6f0..10235f3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -13,6 +13,7 @@ libzakcgi_la_SOURCES = commons.c \ formelementhidden.c \ formelementpassword.c \ formelementtext.c \ + formelementtextarea.c \ formelementsubmit.c \ main.c \ session.c \ @@ -29,6 +30,7 @@ libzakcgi_include_HEADERS = \ formelementhidden.h \ formelementpassword.h \ formelementtext.h \ + formelementtextarea.h \ formelementsubmit.h \ main.h \ session.h \ diff --git a/src/formelementtextarea.c b/src/formelementtextarea.c new file mode 100644 index 0000000..ad02655 --- /dev/null +++ b/src/formelementtextarea.c @@ -0,0 +1,260 @@ +/* + * Copyright (C) 2015 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser 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 Lesser 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 + */ + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include + +#include "commons.h" +#include "tag.h" +#include "formelementtextarea.h" + +static void zak_cgi_form_element_text_area_class_init (ZakCgiFormElementTextAreaClass *class); +static void zak_cgi_form_element_text_area_init (ZakCgiFormElementTextArea *zak_cgi_form_element_text_area); + +static gchar *zak_cgi_form_element_text_area_render (ZakCgiFormElement *element); + +static gboolean zak_cgi_form_element_text_area_is_valid (ZakCgiFormElement *element); + +static void zak_cgi_form_element_text_area_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void zak_cgi_form_element_text_area_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static void zak_cgi_form_element_text_area_dispose (GObject *gobject); +static void zak_cgi_form_element_text_area_finalize (GObject *gobject); + +#define ZAK_CGI_FORM_ELEMENT_TEXT_AREA_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ZAK_CGI_TYPE_FORM_ELEMENT_TEXT_AREA, ZakCgiFormElementTextAreaPrivate)) + +typedef struct _ZakCgiFormElementTextAreaPrivate ZakCgiFormElementTextAreaPrivate; +struct _ZakCgiFormElementTextAreaPrivate + { + gpointer empty; + }; + +G_DEFINE_TYPE (ZakCgiFormElementTextArea, zak_cgi_form_element_text_area, ZAK_CGI_TYPE_FORM_ELEMENT) + +static void +zak_cgi_form_element_text_area_class_init (ZakCgiFormElementTextAreaClass *klass) +{ + 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_area_set_property; + object_class->get_property = zak_cgi_form_element_text_area_get_property; + object_class->dispose = zak_cgi_form_element_text_area_dispose; + object_class->finalize = zak_cgi_form_element_text_area_finalize; + + elem_class->render = zak_cgi_form_element_text_area_render; + elem_class->is_valid = zak_cgi_form_element_text_area_is_valid; + + g_type_class_add_private (object_class, sizeof (ZakCgiFormElementTextAreaPrivate)); +} + +static void +zak_cgi_form_element_text_area_init (ZakCgiFormElementTextArea *zak_cgi_form_element_text_area) +{ + ZakCgiFormElementTextAreaPrivate *priv = ZAK_CGI_FORM_ELEMENT_TEXT_AREA_GET_PRIVATE (zak_cgi_form_element_text_area); + +} + +/** + * zak_cgi_form_element_text_area_new: + * @id: + * @validation_regex: + * @...: + * + * Returns: the newly created #ZakCgiFormElementTextArea object. + */ +ZakCgiFormElement +*zak_cgi_form_element_text_area_new (const gchar *id, + const gchar *validation_regex, + ...) +{ + va_list ap; + + ZakCgiFormElementTextArea *zak_cgi_form_element_text_area; + + zak_cgi_form_element_text_area = ZAK_CGI_FORM_ELEMENT_TEXT_AREA (g_object_new (zak_cgi_form_element_text_area_get_type (), NULL)); + + va_start (ap, validation_regex); + + ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_text_area_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_text_area), + id, + validation_regex, + zak_cgi_commons_valist_to_ghashtable (ap)); + + return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_text_area); +} + +static gchar +*zak_cgi_form_element_text_area_render (ZakCgiFormElement *element) +{ + gchar *ret; + + GHashTable *ht_attrs; + + GValue *value; + + ZakCgiFormElementClass *klass; + + klass = (ZakCgiFormElementClass *)g_type_class_peek_parent (ZAK_CGI_FORM_ELEMENT_TEXT_AREA_GET_CLASS (ZAK_CGI_FORM_ELEMENT_TEXT_AREA (element))); + + ht_attrs = klass->get_ht_attrs (element); + + value = zak_cgi_form_element_get_value (element); + if (value != NULL) + { + g_hash_table_insert (ht_attrs, (gpointer)"zak-cgi-content", (gpointer)g_value_get_string (value)); + } + + ret = zak_cgi_tag_tag_ht ("textarea", zak_cgi_form_element_get_id (element), ht_attrs); + + return ret; +} + +static gboolean +zak_cgi_form_element_text_area_check_value (const gchar *validation_regex, GValue *value) +{ + gboolean ret; + + GRegex *regex; + GError *error; + + if (G_VALUE_HOLDS (value, G_TYPE_STRING)) + { + error = NULL; + regex = g_regex_new (validation_regex, 0, 0, &error); + if (regex == NULL + || error != NULL) + { + syslog (LOG_MAKEPRI(LOG_SYSLOG, LOG_DEBUG), "Error on creating regex: %s.", + error->message != NULL ? error->message : "no details"); + return FALSE; + } + + ret = g_regex_match ((const GRegex *)regex, g_value_get_string (value), 0, NULL); + } + else + { + ret = FALSE; + } + + return ret; +} + +static gboolean +zak_cgi_form_element_text_area_is_valid (ZakCgiFormElement *element) +{ + gboolean ret; + + GValue *gval; + + gchar *str_regex; + + gval = zak_cgi_form_element_get_value (element); + + g_object_get (G_OBJECT (element), + "validation-regex", &str_regex, + NULL); + + if (G_VALUE_HOLDS (gval, G_TYPE_PTR_ARRAY)) + { + guint i; + GPtrArray *ar = (GPtrArray *)g_value_get_boxed (gval); + for (i = 0; i < ar->len; i++) + { + if (!zak_cgi_form_element_text_area_check_value (str_regex, (GValue *)g_ptr_array_index (ar, i))) + { + ret = FALSE; + break; + } + } + } + else + { + ret = zak_cgi_form_element_text_area_check_value (str_regex, gval); + } + + return ret; +} + +/* PRIVATE */ +static void +zak_cgi_form_element_text_area_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + ZakCgiFormElementTextArea *zak_cgi_form_element_text_area = (ZakCgiFormElementTextArea *)object; + ZakCgiFormElementTextAreaPrivate *priv = ZAK_CGI_FORM_ELEMENT_TEXT_AREA_GET_PRIVATE (zak_cgi_form_element_text_area); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_cgi_form_element_text_area_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) +{ + ZakCgiFormElementTextArea *zak_cgi_form_element_text_area = (ZakCgiFormElementTextArea *)object; + ZakCgiFormElementTextAreaPrivate *priv = ZAK_CGI_FORM_ELEMENT_TEXT_AREA_GET_PRIVATE (zak_cgi_form_element_text_area); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_cgi_form_element_text_area_dispose (GObject *gobject) +{ + ZakCgiFormElementTextArea *zak_cgi_form_element_text_area = (ZakCgiFormElementTextArea *)gobject; + ZakCgiFormElementTextAreaPrivate *priv = ZAK_CGI_FORM_ELEMENT_TEXT_AREA_GET_PRIVATE (zak_cgi_form_element_text_area); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->dispose (gobject); +} + +static void +zak_cgi_form_element_text_area_finalize (GObject *gobject) +{ + ZakCgiFormElementTextArea *zak_cgi_form_element_text_area = (ZakCgiFormElementTextArea *)gobject; + ZakCgiFormElementTextAreaPrivate *priv = ZAK_CGI_FORM_ELEMENT_TEXT_AREA_GET_PRIVATE (zak_cgi_form_element_text_area); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->finalize (gobject); +} diff --git a/src/formelementtextarea.h b/src/formelementtextarea.h new file mode 100644 index 0000000..03f132f --- /dev/null +++ b/src/formelementtextarea.h @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2015 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser 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 Lesser 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 + */ + +#ifndef __ZAK_CGI_FORM_ELEMENT_TEXT_AREA_H__ +#define __ZAK_CGI_FORM_ELEMENT_TEXT_AREA_H__ + + +#include + +#include "formelement.h" + + +G_BEGIN_DECLS + + +#define ZAK_CGI_TYPE_FORM_ELEMENT_TEXT_AREA (zak_cgi_form_element_text_area_get_type ()) +#define ZAK_CGI_FORM_ELEMENT_TEXT_AREA(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ZAK_CGI_TYPE_FORM_ELEMENT_TEXT_AREA, ZakCgiFormElementTextArea)) +#define ZAK_CGI_FORM_ELEMENT_TEXT_AREA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ZAK_CGI_TYPE_FORM_ELEMENT_TEXT_AREA, ZakCgiFormElementTextAreaClass)) +#define ZAK_CGI_IS_FORM_ELEMENT_TEXT_AREA(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ZAK_CGI_TYPE_FORM_ELEMENT_TEXT_AREA)) +#define ZAK_CGI_IS_FORM_ELEMENT_TEXT_AREA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ZAK_CGI_TYPE_FORM_ELEMENT_TEXT_AREA)) +#define ZAK_CGI_FORM_ELEMENT_TEXT_AREA_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), ZAK_CGI_TYPE_FORM_ELEMENT_TEXT_AREA, ZakCgiFormElementTextAreaClass)) + +typedef struct _ZakCgiFormElementTextArea ZakCgiFormElementTextArea; +typedef struct _ZakCgiFormElementTextAreaClass ZakCgiFormElementTextAreaClass; + +struct _ZakCgiFormElementTextArea + { + ZakCgiFormElement parent_instance; + }; + +struct _ZakCgiFormElementTextAreaClass + { + ZakCgiFormElementClass parent_class; + }; + + +GType zak_cgi_form_element_text_area_get_type (void); + +ZakCgiFormElement *zak_cgi_form_element_text_area_new (const gchar *id, const gchar *validation_regex, ...); + + +G_END_DECLS + + +#endif /* __ZAK_CGI_FORM_ELEMENT_TEXT_AREA_H__ */ diff --git a/src/tag.c b/src/tag.c index 9dbdf17..2fb1325 100644 --- a/src/tag.c +++ b/src/tag.c @@ -92,7 +92,14 @@ static gchar id); } - if (!with_content) + if (!with_content + && g_strcmp0 (name, "textarea") == 0) + { + g_string_append_printf (str, + ">", + name); + } + else if (!with_content) { g_string_append (str, " />"); diff --git a/tests/form.c b/tests/form.c index fc57356..9de748e 100644 --- a/tests/form.c +++ b/tests/form.c @@ -22,6 +22,7 @@ #include #include #include +#include #include int @@ -59,6 +60,10 @@ main (int argc, char *argv[]) zak_cgi_form_element_set_label (element, "The password", NULL); zak_cgi_form_add_element (form, element); + element = zak_cgi_form_element_text_area_new ("txtarea", "aaa", NULL); + zak_cgi_form_element_set_label (element, "The text area", NULL); + zak_cgi_form_add_element (form, element); + element = zak_cgi_form_element_hidden_new ("hdn", "aaa", NULL); zak_cgi_form_add_element (form, element); diff --git a/tests/tag.c b/tests/tag.c index 273ced3..437a030 100644 --- a/tests/tag.c +++ b/tests/tag.c @@ -36,6 +36,9 @@ main (int argc, char *argv[]) "zak-cgi-content", "the content of the text area", NULL)); + g_string_append_printf (str, "%s
\n", zak_cgi_tag_tag ("textarea", "textarea-nocont", + NULL)); + g_string_append_printf (str, "%s
\n", zak_cgi_tag_img ("theimage", "src", "pippo.png", NULL)); g_string_append_printf (str, "%s
\n", zak_cgi_tag_text ("thetext", NULL)); -- 2.49.0