]> saetta.ns0.it Git - libzakcgi/commitdiff
Added ZakCgiFormElementPassword.
authorAndrea Zagli <azagli@libero.it>
Sat, 12 Sep 2015 10:15:23 +0000 (12:15 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 12 Sep 2015 10:15:23 +0000 (12:15 +0200)
src/Makefile.am
src/formelementpassword.c [new file with mode: 0644]
src/formelementpassword.h [new file with mode: 0644]
tests/form.c

index cdf95be02bfd535775119588571b51dd32ec5dc6..20ba7ee8ac4b2fd7444a7250ff673173b5cde9aa 100644 (file)
@@ -9,6 +9,7 @@ libzakcgi_la_SOURCES = commons.c \
                        commons.h \
                        form.c \
                        formelement.c \
+                       formelementpassword.c \
                        formelementtext.c \
                        formelementsubmit.c \
                        main.c \
@@ -22,6 +23,7 @@ libzakcgi_include_HEADERS = \
                             libzakcgi.h \
                             form.h \
                             formelement.h \
+                            formelementpassword.h \
                             formelementtext.h \
                             formelementsubmit.h \
                             main.h \
diff --git a/src/formelementpassword.c b/src/formelementpassword.c
new file mode 100644 (file)
index 0000000..2c32fe7
--- /dev/null
@@ -0,0 +1,265 @@
+/*
+ * 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 "formelementpassword.h"
+
+static void zak_cgi_form_element_password_class_init (ZakCgiFormElementPasswordClass *class);
+static void zak_cgi_form_element_password_init (ZakCgiFormElementPassword *zak_cgi_form_element_password);
+
+static gchar *zak_cgi_form_element_password_render (ZakCgiFormElement *element);
+
+static gboolean zak_cgi_form_element_password_is_valid (ZakCgiFormElement *element);
+
+static void zak_cgi_form_element_password_set_property (GObject *object,
+                               guint property_id,
+                               const GValue *value,
+                               GParamSpec *pspec);
+static void zak_cgi_form_element_password_get_property (GObject *object,
+                               guint property_id,
+                               GValue *value,
+                               GParamSpec *pspec);
+
+static void zak_cgi_form_element_password_dispose (GObject *gobject);
+static void zak_cgi_form_element_password_finalize (GObject *gobject);
+
+#define ZAK_CGI_FORM_ELEMENT_PASSWORD_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ZAK_CGI_TYPE_FORM_ELEMENT_PASSWORD, ZakCgiFormElementPasswordPrivate))
+
+typedef struct _ZakCgiFormElementPasswordPrivate ZakCgiFormElementPasswordPrivate;
+struct _ZakCgiFormElementPasswordPrivate
+       {
+               gpointer empty;
+       };
+
+G_DEFINE_TYPE (ZakCgiFormElementPassword, zak_cgi_form_element_password, ZAK_CGI_TYPE_FORM_ELEMENT)
+
+static void
+zak_cgi_form_element_password_class_init (ZakCgiFormElementPasswordClass *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_password_set_property;
+       object_class->get_property = zak_cgi_form_element_password_get_property;
+       object_class->dispose = zak_cgi_form_element_password_dispose;
+       object_class->finalize = zak_cgi_form_element_password_finalize;
+
+       elem_class->render = zak_cgi_form_element_password_render;
+       elem_class->is_valid = zak_cgi_form_element_password_is_valid;
+
+       g_type_class_add_private (object_class, sizeof (ZakCgiFormElementPasswordPrivate));
+}
+
+static void
+zak_cgi_form_element_password_init (ZakCgiFormElementPassword *zak_cgi_form_element_password)
+{
+       ZakCgiFormElementPasswordPrivate *priv = ZAK_CGI_FORM_ELEMENT_PASSWORD_GET_PRIVATE (zak_cgi_form_element_password);
+
+}
+
+/**
+ * zak_cgi_form_element_password_new:
+ * @id:
+ * @validation_regex:
+ * @...:
+ *
+ * Returns: the newly created #ZakCgiFormElementPassword object.
+ */
+ZakCgiFormElement
+*zak_cgi_form_element_password_new (const gchar *id,
+                                                                       const gchar *validation_regex,
+                                                                       ...)
+{
+       va_list ap;
+
+       GHashTable *ht_attrs;
+
+       ZakCgiFormElementPassword *zak_cgi_form_element_password;
+
+       zak_cgi_form_element_password = ZAK_CGI_FORM_ELEMENT_PASSWORD (g_object_new (zak_cgi_form_element_password_get_type (), NULL));
+
+       va_start (ap, validation_regex);
+
+       ht_attrs = zak_cgi_commons_valist_to_ghashtable (ap);
+       g_hash_table_replace (ht_attrs, "type", "password");
+
+       ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_password_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_password),
+                                                                                                                                                                       id,
+                                                                                                                                                                       validation_regex,
+                                                                                                                                                                       ht_attrs);
+
+       return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_password);
+}
+
+static gchar
+*zak_cgi_form_element_password_render (ZakCgiFormElement *element)
+{
+       gchar *ret;
+
+       GHashTable *ht_attrs;
+
+       GValue *value;
+
+       ZakCgiFormElementClass *klass;
+
+       klass = (ZakCgiFormElementClass *)g_type_class_peek_parent (ZAK_CGI_FORM_ELEMENT_PASSWORD_GET_CLASS (ZAK_CGI_FORM_ELEMENT_PASSWORD (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)"value", (gpointer)g_value_get_string (value));
+               }
+
+       ret = zak_cgi_tag_tag_ht ("input", zak_cgi_form_element_get_id (element), ht_attrs);
+
+       return ret;
+}
+
+static gboolean
+zak_cgi_form_element_password_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_password_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_password_check_value (str_regex, (GValue *)g_ptr_array_index (ar, i)))
+                                               {
+                                                       ret = FALSE;
+                                                       break;
+                                               }
+                               }
+               }
+       else
+               {
+                       ret = zak_cgi_form_element_password_check_value (str_regex, gval);
+               }
+
+       return ret;
+}
+
+/* PRIVATE */
+static void
+zak_cgi_form_element_password_set_property (GObject *object,
+                   guint property_id,
+                   const GValue *value,
+                   GParamSpec *pspec)
+{
+       ZakCgiFormElementPassword *zak_cgi_form_element_password = (ZakCgiFormElementPassword *)object;
+       ZakCgiFormElementPasswordPrivate *priv = ZAK_CGI_FORM_ELEMENT_PASSWORD_GET_PRIVATE (zak_cgi_form_element_password);
+
+       switch (property_id)
+               {
+                       default:
+                               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                               break;
+               }
+}
+
+static void
+zak_cgi_form_element_password_get_property (GObject *object,
+                   guint property_id,
+                   GValue *value,
+                   GParamSpec *pspec)
+{
+       ZakCgiFormElementPassword *zak_cgi_form_element_password = (ZakCgiFormElementPassword *)object;
+       ZakCgiFormElementPasswordPrivate *priv = ZAK_CGI_FORM_ELEMENT_PASSWORD_GET_PRIVATE (zak_cgi_form_element_password);
+
+       switch (property_id)
+               {
+                       default:
+                               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                               break;
+               }
+}
+
+static void
+zak_cgi_form_element_password_dispose (GObject *gobject)
+{
+       ZakCgiFormElementPassword *zak_cgi_form_element_password = (ZakCgiFormElementPassword *)gobject;
+       ZakCgiFormElementPasswordPrivate *priv = ZAK_CGI_FORM_ELEMENT_PASSWORD_GET_PRIVATE (zak_cgi_form_element_password);
+
+
+
+       GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject));
+       parent_class->dispose (gobject);
+}
+
+static void
+zak_cgi_form_element_password_finalize (GObject *gobject)
+{
+       ZakCgiFormElementPassword *zak_cgi_form_element_password = (ZakCgiFormElementPassword *)gobject;
+       ZakCgiFormElementPasswordPrivate *priv = ZAK_CGI_FORM_ELEMENT_PASSWORD_GET_PRIVATE (zak_cgi_form_element_password);
+
+
+
+       GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject));
+       parent_class->finalize (gobject);
+}
diff --git a/src/formelementpassword.h b/src/formelementpassword.h
new file mode 100644 (file)
index 0000000..e1fe765
--- /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_PASSWORD_H__
+#define __ZAK_CGI_FORM_ELEMENT_PASSWORD_H__
+
+
+#include <glib-object.h>
+
+#include "formelement.h"
+
+
+G_BEGIN_DECLS
+
+
+#define ZAK_CGI_TYPE_FORM_ELEMENT_PASSWORD                 (zak_cgi_form_element_password_get_type ())
+#define ZAK_CGI_FORM_ELEMENT_PASSWORD(obj)                 (G_TYPE_CHECK_INSTANCE_CAST ((obj), ZAK_CGI_TYPE_FORM_ELEMENT_PASSWORD, ZakCgiFormElementPassword))
+#define ZAK_CGI_FORM_ELEMENT_PASSWORD_CLASS(klass)         (G_TYPE_CHECK_CLASS_CAST ((klass), ZAK_CGI_TYPE_FORM_ELEMENT_PASSWORD, ZakCgiFormElementPasswordClass))
+#define ZAK_CGI_IS_FORM_ELEMENT_PASSWORD(obj)              (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ZAK_CGI_TYPE_FORM_ELEMENT_PASSWORD))
+#define ZAK_CGI_IS_FORM_ELEMENT_PASSWORD_CLASS(klass)      (G_TYPE_CHECK_CLASS_TYPE ((klass), ZAK_CGI_TYPE_FORM_ELEMENT_PASSWORD))
+#define ZAK_CGI_FORM_ELEMENT_PASSWORD_GET_CLASS(obj)       (G_TYPE_INSTANCE_GET_CLASS ((obj), ZAK_CGI_TYPE_FORM_ELEMENT_PASSWORD, ZakCgiFormElementPasswordClass))
+
+typedef struct _ZakCgiFormElementPassword ZakCgiFormElementPassword;
+typedef struct _ZakCgiFormElementPasswordClass ZakCgiFormElementPasswordClass;
+
+struct _ZakCgiFormElementPassword
+       {
+               ZakCgiFormElement parent_instance;
+       };
+
+struct _ZakCgiFormElementPasswordClass
+       {
+               ZakCgiFormElementClass parent_class;
+       };
+
+
+GType zak_cgi_form_element_password_get_type (void);
+
+ZakCgiFormElement *zak_cgi_form_element_password_new (const gchar *id, const gchar *validation_regex, ...);
+
+
+G_END_DECLS
+
+
+#endif /* __ZAK_CGI_FORM_ELEMENT_PASSWORD_H__ */
index cd48a8fcc4e9f50ca5c8de4c9607ff0f9297dabf..9b5986791fc6e4a5223867ee6895d640392bf0aa 100644 (file)
@@ -48,6 +48,10 @@ main (int argc, char *argv[])
 
        zak_cgi_form_add_str (form, "<h1>big big big</h1>");
 
+       element = zak_cgi_form_element_password_new ("pws", "aaa", NULL);
+       zak_cgi_form_element_set_label (element, "The password", NULL);
+       zak_cgi_form_add_element (form, element);
+
        element = zak_cgi_form_element_submit_new ("submit", "aaa", NULL);
        zak_cgi_form_add_element (form, element);