Added ZakCgiFormElementTextArea.
authorAndrea Zagli <azagli@libero.it>
Sat, 12 Sep 2015 11:06:21 +0000 (13:06 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 12 Sep 2015 11:06:21 +0000 (13:06 +0200)
src/Makefile.am
src/formelementtextarea.c [new file with mode: 0644]
src/formelementtextarea.h [new file with mode: 0644]
src/tag.c
tests/form.c
tests/tag.c

index 10dc6f086f94562315a7eb83c6d1135c03bd60cd..10235f35c4b0a2da1e9b59b7532f69b941acc2d9 100644 (file)
@@ -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 (file)
index 0000000..ad02655
--- /dev/null
@@ -0,0 +1,260 @@
+/*
+ * 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 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 <config.h>
+#endif
+
+#include <syslog.h>
+
+#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 (file)
index 0000000..03f132f
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * 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 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 <glib-object.h>
+
+#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__ */
index 9dbdf1708451248e00cf87ed2c39e17a13affd9b..2fb132512bad8024909bb6b5bfff7e1a3ebb23da 100644 (file)
--- 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,
+                                                                       "></%s>",
+                                                                       name);
+               }
+       else if (!with_content)
                {
                        g_string_append (str,
                                                         " />");
index fc5735633abed80949cb00fd493da3128015b0f9..9de748ea136f8f2d68545e1f8d31d8571b581c97 100644 (file)
@@ -22,6 +22,7 @@
 #include <formelementhidden.h>
 #include <formelementpassword.h>
 #include <formelementtext.h>
+#include <formelementtextarea.h>
 #include <formelementsubmit.h>
 
 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);
 
index 273ced306d45f4944e8e3f3cc26ec5dfe1ba44e5..437a030e802ea13b8071594efeb9373e59b40944 100644 (file)
@@ -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<br/>\n", zak_cgi_tag_tag ("textarea", "textarea-nocont",
+                                                                         NULL));
+
        g_string_append_printf (str, "%s<br/>\n", zak_cgi_tag_img ("theimage", "src", "pippo.png", NULL));
 
        g_string_append_printf (str, "%s<br/>\n", zak_cgi_tag_text ("thetext", NULL));