From bd76a42050be7bc9406c123332cb9c5a0ae56e8b Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sun, 2 Aug 2015 11:09:12 +0200 Subject: [PATCH] Added FormElementText. --- src/Makefile.am | 2 + src/formelement.c | 112 +++++++++++++++++++++++++- src/formelement.h | 12 ++- src/formelementtext.c | 179 ++++++++++++++++++++++++++++++++++++++++++ src/formelementtext.h | 64 +++++++++++++++ 5 files changed, 366 insertions(+), 3 deletions(-) create mode 100644 src/formelementtext.c create mode 100644 src/formelementtext.h diff --git a/src/Makefile.am b/src/Makefile.am index 3f2c79a..95ac0d7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,6 +8,7 @@ lib_LTLIBRARIES = libzakcgi.la libzakcgi_la_SOURCES = \ form.c \ formelement.c \ + formelementtext.c \ main.c \ session.c \ tag.c \ @@ -19,6 +20,7 @@ libzakcgi_include_HEADERS = \ libzakcgi.h \ form.h \ formelement.h \ + formelementtext.h \ main.h \ session.h \ tag.h \ diff --git a/src/formelement.c b/src/formelement.c index ebbb431..eb6d03c 100644 --- a/src/formelement.c +++ b/src/formelement.c @@ -24,6 +24,12 @@ #include "formelement.h" +enum +{ + PROP_0, + PROP_VALIDATION_REGEX +}; + static void zak_cgi_form_element_class_init (ZakCgiFormElementClass *class); static void zak_cgi_form_element_init (ZakCgiFormElement *zak_cgi_form_element); @@ -44,6 +50,7 @@ static void zak_cgi_form_element_finalize (GObject *gobject); typedef struct _ZakCgiFormElementPrivate ZakCgiFormElementPrivate; struct _ZakCgiFormElementPrivate { + gchar *validation_regex; }; G_DEFINE_TYPE (ZakCgiFormElement, zak_cgi_form_element, G_TYPE_OBJECT) @@ -59,6 +66,13 @@ zak_cgi_form_element_class_init (ZakCgiFormElementClass *class) object_class->finalize = zak_cgi_form_element_finalize; g_type_class_add_private (object_class, sizeof (ZakCgiFormElementPrivate)); + + g_object_class_install_property (object_class, PROP_VALIDATION_REGEX, + g_param_spec_string ("validation-regex", + "Validation regex", + "Validation regex", + "", + G_PARAM_READWRITE)); } static void @@ -66,16 +80,17 @@ zak_cgi_form_element_init (ZakCgiFormElement *zak_cgi_form_element) { ZakCgiFormElementPrivate *priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (zak_cgi_form_element); - + priv->validation_regex = NULL; } /** * zak_cgi_form_element_new: + * @validation_regex: * * Returns: the newly created #ZakCgiFormElement object. */ ZakCgiFormElement -*zak_cgi_form_element_new (void) +*zak_cgi_form_element_new (const gchar *validation_regex) { ZakCgiFormElement *zak_cgi_form_element; ZakCgiFormElementPrivate *priv; @@ -84,9 +99,93 @@ ZakCgiFormElement priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (zak_cgi_form_element); + zak_cgi_form_element_set_validation_regex (zak_cgi_form_element, validation_regex); + return zak_cgi_form_element; } +void +zak_cgi_form_element_set_validation_regex (ZakCgiFormElement *element, const gchar *validation_regex) +{ + ZakCgiFormElementPrivate *priv; + + priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (element); + + if (validation_regex == NULL) + { + if (priv->validation_regex != NULL) + { + g_free (priv->validation_regex); + } + priv->validation_regex = NULL; + } + else + { + priv->validation_regex = g_strdup (validation_regex); + } +} + +gchar +*zak_cgi_form_element_get_validation_regex (ZakCgiFormElement *element) +{ + gchar *ret; + + ZakCgiFormElementPrivate *priv; + + priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (element); + + if (priv->validation_regex != NULL) + { + ret = g_strdup (priv->validation_regex); + } + else + { + ret = NULL; + } + + return ret; +} + +/** + * zak_cgi_form_element_render: + * @element: + * + */ +gchar +*zak_cgi_form_element_render (ZakCgiFormElement *element) +{ + gchar *ret; + + ret = g_strdup (""); + + 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); + } + + return ret; +} + +/** + * zak_cgi_form_element_is_valid: + * @element: + * + */ +gboolean +zak_cgi_form_element_is_valid (ZakCgiFormElement *element) +{ + gboolean ret; + + ret = FALSE; + + 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); + } + + return ret; +} + /* PRIVATE */ static void zak_cgi_form_element_set_property (GObject *object, @@ -99,6 +198,10 @@ zak_cgi_form_element_set_property (GObject *object, switch (property_id) { + case PROP_VALIDATION_REGEX: + zak_cgi_form_element_set_validation_regex (zak_cgi_form_element, g_value_get_string (value)); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; @@ -116,6 +219,10 @@ zak_cgi_form_element_get_property (GObject *object, switch (property_id) { + case PROP_VALIDATION_REGEX: + g_value_set_string (value, zak_cgi_form_element_get_validation_regex (zak_cgi_form_element)); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; @@ -129,6 +236,7 @@ zak_cgi_form_element_dispose (GObject *gobject) ZakCgiFormElementPrivate *priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (zak_cgi_form_element); + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); parent_class->dispose (gobject); } diff --git a/src/formelement.h b/src/formelement.h index 561bec7..ac4e18c 100644 --- a/src/formelement.h +++ b/src/formelement.h @@ -46,12 +46,22 @@ struct _ZakCgiFormElement struct _ZakCgiFormElementClass { GObjectClass parent_class; + + gchar *(*render) (ZakCgiFormElement *element); + gboolean (*is_valid) (ZakCgiFormElement *element); }; GType zak_cgi_form_element_get_type (void); -ZakCgiFormElement *zak_cgi_form_element_new (void); +ZakCgiFormElement *zak_cgi_form_element_new (const gchar *validation_regex); + +void zak_cgi_form_element_set_validation_regex (ZakCgiFormElement *element, const gchar *validation_regex); +gchar *zak_cgi_form_element_get_validation_regex (ZakCgiFormElement *element); + +gchar *zak_cgi_form_element_render (ZakCgiFormElement *element); + +gboolean zak_cgi_form_element_is_valid (ZakCgiFormElement *element); G_END_DECLS diff --git a/src/formelementtext.c b/src/formelementtext.c new file mode 100644 index 0000000..eacc3df --- /dev/null +++ b/src/formelementtext.c @@ -0,0 +1,179 @@ +/* + * 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 "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 void zak_cgi_form_element_text_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void zak_cgi_form_element_text_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static void zak_cgi_form_element_text_dispose (GObject *gobject); +static void zak_cgi_form_element_text_finalize (GObject *gobject); + +#define ZAK_CGI_FORM_ELEMENT_TEXT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ZAK_CGI_TYPE_FORM_ELEMENT_TEXT, ZakCgiFormElementTextPrivate)) + +typedef struct _ZakCgiFormElementTextPrivate ZakCgiFormElementTextPrivate; +struct _ZakCgiFormElementTextPrivate + { + }; + +G_DEFINE_TYPE (ZakCgiFormElementText, zak_cgi_form_element_text, G_TYPE_OBJECT) + +static void +zak_cgi_form_element_text_class_init (ZakCgiFormElementTextClass *class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (class); + + 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; + + g_type_class_add_private (object_class, sizeof (ZakCgiFormElementTextPrivate)); +} + +static void +zak_cgi_form_element_text_init (ZakCgiFormElementText *zak_cgi_form_element_text) +{ + ZakCgiFormElementTextPrivate *priv = ZAK_CGI_FORM_ELEMENT_TEXT_GET_PRIVATE (zak_cgi_form_element_text); + +} + +/** + * zak_cgi_form_element_text_new: + * @validation_regex: + * + * Returns: the newly created #ZakCgiFormElementText object. + */ +ZakCgiFormElementText +*zak_cgi_form_element_text_new (const gchar *validation_regex) +{ + 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); + + if (validation_regex != NULL) + { + } + + return zak_cgi_form_element_text; +} + +/** + * zak_cgi_form_element_text_render: + * @element_text: + * + */ +gchar +*zak_cgi_form_element_text_render (ZakCgiFormElementText *element_text) +{ + gchar *ret; + + + return ret; +} + +/** + * zak_cgi_form_element_text_is_valid: + * @element_text: + * + */ +gboolean +zak_cgi_form_element_text_is_valid (ZakCgiFormElementText *element_text) +{ + gboolean ret; + + + return ret; +} + +/* PRIVATE */ +static void +zak_cgi_form_element_text_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + ZakCgiFormElementText *zak_cgi_form_element_text = (ZakCgiFormElementText *)object; + ZakCgiFormElementTextPrivate *priv = ZAK_CGI_FORM_ELEMENT_TEXT_GET_PRIVATE (zak_cgi_form_element_text); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_cgi_form_element_text_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) +{ + ZakCgiFormElementText *zak_cgi_form_element_text = (ZakCgiFormElementText *)object; + ZakCgiFormElementTextPrivate *priv = ZAK_CGI_FORM_ELEMENT_TEXT_GET_PRIVATE (zak_cgi_form_element_text); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_cgi_form_element_text_dispose (GObject *gobject) +{ + ZakCgiFormElementText *zak_cgi_form_element_text = (ZakCgiFormElementText *)gobject; + ZakCgiFormElementTextPrivate *priv = ZAK_CGI_FORM_ELEMENT_TEXT_GET_PRIVATE (zak_cgi_form_element_text); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->dispose (gobject); +} + +static void +zak_cgi_form_element_text_finalize (GObject *gobject) +{ + ZakCgiFormElementText *zak_cgi_form_element_text = (ZakCgiFormElementText *)gobject; + ZakCgiFormElementTextPrivate *priv = ZAK_CGI_FORM_ELEMENT_TEXT_GET_PRIVATE (zak_cgi_form_element_text); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->finalize (gobject); +} diff --git a/src/formelementtext.h b/src/formelementtext.h new file mode 100644 index 0000000..c05935e --- /dev/null +++ b/src/formelementtext.h @@ -0,0 +1,64 @@ +/* + * 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_H__ +#define __ZAK_CGI_FORM_ELEMENT_TEXT_H__ + + +#include + +#include "formelement.h" + + +G_BEGIN_DECLS + + +#define ZAK_CGI_TYPE_FORM_ELEMENT_TEXT (zak_cgi_form_element_text_get_type ()) +#define ZAK_CGI_FORM_ELEMENT_TEXT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ZAK_CGI_TYPE_FORM_ELEMENT_TEXT, ZakCgiFormElementText)) +#define ZAK_CGI_FORM_ELEMENT_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ZAK_CGI_TYPE_FORM_ELEMENT_TEXT, ZakCgiFormElementTextClass)) +#define ZAK_CGI_IS_FORM_ELEMENT_TEXT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ZAK_CGI_TYPE_FORM_ELEMENT_TEXT)) +#define ZAK_CGI_IS_FORM_ELEMENT_TEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ZAK_CGI_TYPE_FORM_ELEMENT_TEXT)) +#define ZAK_CGI_FORM_ELEMENT_TEXT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), ZAK_CGI_TYPE_FORM_ELEMENT_TEXT, ZakCgiFormElementTextClass)) + +typedef struct _ZakCgiFormElementText ZakCgiFormElementText; +typedef struct _ZakCgiFormElementTextClass ZakCgiFormElementTextClass; + +struct _ZakCgiFormElementText + { + ZakCgiFormElement parent_instance; + }; + +struct _ZakCgiFormElementTextClass + { + ZakCgiFormElementClass parent_class; + }; + + +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); + + +G_END_DECLS + + +#endif /* __ZAK_CGI_FORM_ELEMENT_TEXT_H__ */ -- 2.49.0