Because GHashTable doesn't respect the inserting order.
formelementhidden.c \
formelementpassword.c \
formelementselect.c \
+ formelementstring.c \
formelementtext.c \
formelementtextarea.c \
formelementsubmit.c \
formelementhidden.h \
formelementpassword.h \
formelementselect.h \
+ formelementstring.h \
formelementtext.h \
formelementtextarea.h \
formelementsubmit.h \
#include "commons.h"
#include "form.h"
+#include "formelementstring.h"
static void zak_cgi_form_class_init (ZakCgiFormClass *class);
static void zak_cgi_form_init (ZakCgiForm *zak_cgi_form);
{
ZakCgiMain *zakcgimain;
GHashTable *ht_attrs;
- guint elems;
- GHashTable *ht_elems;
+ GPtrArray *ar_elems;
};
G_DEFINE_TYPE (ZakCgiForm, zak_cgi_form, G_TYPE_OBJECT)
priv->zakcgimain = NULL;
priv->ht_attrs = NULL;
- priv->elems = 0;
- priv->ht_elems = g_hash_table_new (g_str_hash, g_str_equal);
+ priv->ar_elems = g_ptr_array_new ();
}
/**
return zak_cgi_form;
}
+static guint
+get_idx (ZakCgiForm *zakcgiform, const gchar *id)
+{
+ guint idx;
+ gchar *element_id;
+
+ ZakCgiFormPrivate *priv;
+
+ priv = ZAK_CGI_FORM_GET_PRIVATE (zakcgiform);
+
+ for (idx = 0; idx < priv->ar_elems->len; idx++)
+ {
+ element_id = zak_cgi_form_element_get_id ((ZakCgiFormElement *)g_ptr_array_index (priv->ar_elems, idx));
+ if (g_strcmp0 (element_id, id) == 0)
+ {
+ break;
+ }
+ g_free (element_id);
+ }
+ if (idx == priv->ar_elems->len)
+ {
+ idx = -1;
+ }
+
+ return idx;
+}
+
/**
* zak_cgi_form_add_element:
* @zakcgiform:
id = zak_cgi_form_element_get_id (element);
- if (g_hash_table_lookup (priv->ht_elems, id))
+ if (get_idx (zakcgiform, id) > -1)
{
g_warning ("You cannot add an element with id already present in the form.");
ret = FALSE;
}
else
{
- priv->elems++;
- g_hash_table_insert (priv->ht_elems, g_strdup (id), g_object_ref (element));
+ g_ptr_array_add (priv->ar_elems, g_object_ref (element));
ret = TRUE;
}
zak_cgi_form_add_str (ZakCgiForm *zakcgiform, const gchar *str)
{
gboolean ret;
- gchar *id;
- ZakCgiFormPrivate *priv;
+ ZakCgiFormElement *element;
- priv = ZAK_CGI_FORM_GET_PRIVATE (zakcgiform);
-
- id = g_strdup_printf ("{id_%d}", priv->elems++);
-
- if (g_hash_table_lookup (priv->ht_elems, id))
- {
- priv->elems--;
- g_warning ("You cannot add an element with id already present in the form.");
- ret = FALSE;
- }
- else
- {
- g_hash_table_insert (priv->ht_elems, g_strdup (id), g_strdup (str));
- ret = TRUE;
- }
-
- g_free (id);
+ element = zak_cgi_form_element_string_new (str);
+ ret = zak_cgi_form_add_element (zakcgiform, element);
return ret;
}
void
zak_cgi_form_bind (ZakCgiForm *zakcgiform)
{
- GHashTableIter iter;
- gpointer key;
- gpointer value;
+ guint i;
GValue *gval;
priv = ZAK_CGI_FORM_GET_PRIVATE (zakcgiform);
- g_hash_table_iter_init (&iter, priv->ht_elems);
- while (g_hash_table_iter_next (&iter, &key, &value))
+ for (i == 0; i < priv->ar_elems->len; i++)
{
- if (!g_str_has_prefix ((gchar *)key, "{id_"))
+ ZakCgiFormElement *element = (ZakCgiFormElement *)g_ptr_array_index (priv->ar_elems, i);
+ if (!ZAK_CGI_IS_FORM_ELEMENT_STRING (element))
{
- gval = zak_cgi_main_get_stdin_field (priv->zakcgimain, (gchar *)key);
+ gval = zak_cgi_main_get_stdin_field (priv->zakcgimain, zak_cgi_form_element_get_id (element));
if (gval != NULL)
{
- zak_cgi_form_element_set_value ((ZakCgiFormElement *)value, gval);
+ zak_cgi_form_element_set_value (element, gval);
}
}
}
gboolean
zak_cgi_form_is_valid (ZakCgiForm *zakcgiform)
{
- GHashTableIter iter;
- gpointer key;
- gpointer value;
+ guint i;
gboolean ret;
ret = TRUE;
- g_hash_table_iter_init (&iter, priv->ht_elems);
- while (g_hash_table_iter_next (&iter, &key, &value))
+ for (i == 0; i < priv->ar_elems->len; i++)
{
- if (!g_str_has_prefix ((gchar *)key, "{id_"))
+ ZakCgiFormElement *element = (ZakCgiFormElement *)g_ptr_array_index (priv->ar_elems, i);
+ if (!ZAK_CGI_IS_FORM_ELEMENT_STRING (element))
{
- if (!zak_cgi_form_element_is_valid ((ZakCgiFormElement *)value))
+ if (!zak_cgi_form_element_is_valid (element))
{
ret = FALSE;
break;
- };
+ }
}
}
{
GString *str;
- GHashTableIter iter;
- gpointer key;
- gpointer value;
+ guint i;
gchar *tmp;
g_string_append (str, tmp);
g_free (tmp);
- g_hash_table_iter_init (&iter, priv->ht_elems);
- while (g_hash_table_iter_next (&iter, &key, &value))
+ for (i == 0; i < priv->ar_elems->len; i++)
{
- if (g_str_has_prefix ((gchar *)key, "{id_"))
- {
- g_string_append_printf (str, "\n%s", (gchar *)value);
- }
- else
- {
- tmp = zak_cgi_form_element_render ((ZakCgiFormElement *)value);
- g_string_append_printf (str, "\n%s", tmp);
- g_free (tmp);
- }
+ ZakCgiFormElement *element = (ZakCgiFormElement *)g_ptr_array_index (priv->ar_elems, i);
+ tmp = zak_cgi_form_element_render (element);
+ g_string_append_printf (str, "\n%s", tmp);
+ g_free (tmp);
}
g_string_append (str, "\n</form>");
--- /dev/null
+/*
+ * 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 "formelementstring.h"
+
+static void zak_cgi_form_element_string_class_init (ZakCgiFormElementStringClass *class);
+static void zak_cgi_form_element_string_init (ZakCgiFormElementString *zak_cgi_form_element_string);
+
+static gchar *zak_cgi_form_element_string_render (ZakCgiFormElement *element);
+
+static gboolean zak_cgi_form_element_string_is_valid (ZakCgiFormElement *element);
+
+static void zak_cgi_form_element_string_set_property (GObject *object,
+ guint property_id,
+ const GValue *value,
+ GParamSpec *pspec);
+static void zak_cgi_form_element_string_get_property (GObject *object,
+ guint property_id,
+ GValue *value,
+ GParamSpec *pspec);
+
+static void zak_cgi_form_element_string_dispose (GObject *gobject);
+static void zak_cgi_form_element_string_finalize (GObject *gobject);
+
+#define ZAK_CGI_FORM_ELEMENT_STRING_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ZAK_CGI_TYPE_FORM_ELEMENT_STRING, ZakCgiFormElementStringPrivate))
+
+typedef struct _ZakCgiFormElementStringPrivate ZakCgiFormElementStringPrivate;
+struct _ZakCgiFormElementStringPrivate
+ {
+ gchar *str;
+ };
+
+G_DEFINE_TYPE (ZakCgiFormElementString, zak_cgi_form_element_string, ZAK_CGI_TYPE_FORM_ELEMENT)
+
+static void
+zak_cgi_form_element_string_class_init (ZakCgiFormElementStringClass *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_string_set_property;
+ object_class->get_property = zak_cgi_form_element_string_get_property;
+ object_class->dispose = zak_cgi_form_element_string_dispose;
+ object_class->finalize = zak_cgi_form_element_string_finalize;
+
+ elem_class->render = zak_cgi_form_element_string_render;
+ elem_class->is_valid = zak_cgi_form_element_string_is_valid;
+
+ g_type_class_add_private (object_class, sizeof (ZakCgiFormElementStringPrivate));
+}
+
+static void
+zak_cgi_form_element_string_init (ZakCgiFormElementString *zak_cgi_form_element_string)
+{
+ ZakCgiFormElementStringPrivate *priv = ZAK_CGI_FORM_ELEMENT_STRING_GET_PRIVATE (zak_cgi_form_element_string);
+
+ priv->str = NULL;
+}
+
+/**
+ * zak_cgi_form_element_string_new:
+ * @str:
+ *
+ * Returns: the newly created #ZakCgiFormElementString object.
+ */
+ZakCgiFormElement
+*zak_cgi_form_element_string_new (const gchar *str)
+{
+ ZakCgiFormElementString *zak_cgi_form_element_string;
+
+ zak_cgi_form_element_string = ZAK_CGI_FORM_ELEMENT_STRING (g_object_new (zak_cgi_form_element_string_get_type (), NULL));
+
+ ZakCgiFormElementStringPrivate *priv = ZAK_CGI_FORM_ELEMENT_STRING_GET_PRIVATE (zak_cgi_form_element_string);
+
+ priv->str = g_strdup (str);
+
+ return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_string);
+}
+
+static gchar
+*zak_cgi_form_element_string_render (ZakCgiFormElement *element)
+{
+ gchar *ret;
+
+ ZakCgiFormElementStringPrivate *priv = ZAK_CGI_FORM_ELEMENT_STRING_GET_PRIVATE (element);
+
+ ret = g_strdup (priv->str);
+
+ return ret;
+}
+
+static gboolean
+zak_cgi_form_element_string_check_value (const gchar *validation_regex, GValue *value)
+{
+ return TRUE;
+}
+
+static gboolean
+zak_cgi_form_element_string_is_valid (ZakCgiFormElement *element)
+{
+ return TRUE;
+}
+
+/* PRIVATE */
+static void
+zak_cgi_form_element_string_set_property (GObject *object,
+ guint property_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ ZakCgiFormElementString *zak_cgi_form_element_string = (ZakCgiFormElementString *)object;
+ ZakCgiFormElementStringPrivate *priv = ZAK_CGI_FORM_ELEMENT_STRING_GET_PRIVATE (zak_cgi_form_element_string);
+
+ switch (property_id)
+ {
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+ break;
+ }
+}
+
+static void
+zak_cgi_form_element_string_get_property (GObject *object,
+ guint property_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ ZakCgiFormElementString *zak_cgi_form_element_string = (ZakCgiFormElementString *)object;
+ ZakCgiFormElementStringPrivate *priv = ZAK_CGI_FORM_ELEMENT_STRING_GET_PRIVATE (zak_cgi_form_element_string);
+
+ switch (property_id)
+ {
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+ break;
+ }
+}
+
+static void
+zak_cgi_form_element_string_dispose (GObject *gobject)
+{
+ ZakCgiFormElementString *zak_cgi_form_element_string = (ZakCgiFormElementString *)gobject;
+ ZakCgiFormElementStringPrivate *priv = ZAK_CGI_FORM_ELEMENT_STRING_GET_PRIVATE (zak_cgi_form_element_string);
+
+
+
+ GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject));
+ parent_class->dispose (gobject);
+}
+
+static void
+zak_cgi_form_element_string_finalize (GObject *gobject)
+{
+ ZakCgiFormElementString *zak_cgi_form_element_string = (ZakCgiFormElementString *)gobject;
+ ZakCgiFormElementStringPrivate *priv = ZAK_CGI_FORM_ELEMENT_STRING_GET_PRIVATE (zak_cgi_form_element_string);
+
+
+
+ GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject));
+ parent_class->finalize (gobject);
+}
--- /dev/null
+/*
+ * 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_STRING_H__
+#define __ZAK_CGI_FORM_ELEMENT_STRING_H__
+
+
+#include <glib-object.h>
+
+#include "formelement.h"
+
+
+G_BEGIN_DECLS
+
+
+#define ZAK_CGI_TYPE_FORM_ELEMENT_STRING (zak_cgi_form_element_string_get_type ())
+#define ZAK_CGI_FORM_ELEMENT_STRING(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ZAK_CGI_TYPE_FORM_ELEMENT_STRING, ZakCgiFormElementString))
+#define ZAK_CGI_FORM_ELEMENT_STRING_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ZAK_CGI_TYPE_FORM_ELEMENT_STRING, ZakCgiFormElementStringClass))
+#define ZAK_CGI_IS_FORM_ELEMENT_STRING(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ZAK_CGI_TYPE_FORM_ELEMENT_STRING))
+#define ZAK_CGI_IS_FORM_ELEMENT_STRING_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ZAK_CGI_TYPE_FORM_ELEMENT_STRING))
+#define ZAK_CGI_FORM_ELEMENT_STRING_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), ZAK_CGI_TYPE_FORM_ELEMENT_STRING, ZakCgiFormElementStringClass))
+
+typedef struct _ZakCgiFormElementString ZakCgiFormElementString;
+typedef struct _ZakCgiFormElementStringClass ZakCgiFormElementStringClass;
+
+struct _ZakCgiFormElementString
+ {
+ ZakCgiFormElement parent_instance;
+ };
+
+struct _ZakCgiFormElementStringClass
+ {
+ ZakCgiFormElementClass parent_class;
+ };
+
+
+GType zak_cgi_form_element_string_get_type (void);
+
+ZakCgiFormElement *zak_cgi_form_element_string_new (const gchar *str);
+
+
+G_END_DECLS
+
+
+#endif /* __ZAK_CGI_FORM_ELEMENT_STRING_H__ */
/* no form-control for submit button */
g_string_erase (str_attr_class, form_control - str_attr_class->str, 12);
}
- g_string_free (str_attr_class, TRUE);
if (g_strstr_len (attr_class, -1, "btn btn-default") == NULL)
{
}
g_free (attr_class);
+ g_string_free (str_attr_class, TRUE);
}
else
{