From 261cb5917e3f8c1d6fbf7810dfa772b6babd2883 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Mon, 30 Jan 2017 18:14:34 +0100 Subject: [PATCH] Added functions ::load_from_hashtable and ::get_values_as_hashtable. Use of GValue. --- src/form.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++------ src/form.h | 3 ++ 2 files changed, 96 insertions(+), 11 deletions(-) diff --git a/src/form.c b/src/form.c index 0c6e569..83c4914 100644 --- a/src/form.c +++ b/src/form.c @@ -43,6 +43,8 @@ static void zak_form_hashtable_provider_get_property (GObject *object, static void zak_form_hashtable_provider_dispose (GObject *gobject); static void zak_form_hashtable_provider_finalize (GObject *gobject); +static GValue *zak_form_hashtable_provider_new_gvalue_from_element (ZakFormElement *element); + static GHashTable *zak_form_hashtable_provider_get_group (ZakFormHashtableProvider *zak_form_hashtable_provider, GPtrArray *elements); static gboolean zak_form_hashtable_provider_load (ZakFormIProvider *provider, GPtrArray *elements); @@ -113,6 +115,96 @@ ZakFormHashtableProvider return zak_form_hashtable_provider; } +static gchar +*zak_form_hashtable_provider_gvalue_stringify (GValue *value) +{ + gchar *ret; + + if (G_VALUE_HOLDS (value, G_TYPE_STRING)) + { + ret = (gchar *)g_value_get_string (value); + } + else if (G_VALUE_HOLDS (value, G_TYPE_INT)) + { + ret = g_strdup_printf ("%d", g_value_get_int (value)); + } + else if (G_VALUE_HOLDS (value, G_TYPE_FLOAT)) + { + ret = g_strdup_printf ("%f", g_value_get_float (value)); + } + else if (G_VALUE_HOLDS (value, G_TYPE_DOUBLE)) + { + ret = g_strdup_printf ("%f", g_value_get_float (value)); + } + else if (G_VALUE_HOLDS (value, G_TYPE_BOOLEAN)) + { + ret = g_strdup (g_value_get_boolean (value) ? "TRUE" : "FALSE"); + } + else if (G_VALUE_HOLDS (value, G_TYPE_DATE_TIME)) + { + ret = zak_utils_gdatetime_format ((GDateTime *)g_value_get_boxed (value), "%F %T"); + } + else + { + ret = g_strdup (""); + } + + return g_strdup (ret); +} + +/** + * zak_form_hashtable_provider_load_from_hashtable: + * @provider: + * @elements: + * @ht: + */ +void +zak_form_hashtable_provider_load_from_hashtable (ZakFormHashtableProvider *provider, GPtrArray *elements, GHashTable *ht) +{ + guint i; + + for (i = 0; i < elements->len; i++) + { + ZakFormElement *element = (ZakFormElement *)g_ptr_array_index (elements, i); + if (zak_form_element_get_to_load (element)) + { + zak_form_element_set_value (element, + zak_form_hashtable_provider_gvalue_stringify ((GValue *)g_hash_table_lookup (ht, zak_form_element_get_name (element)))); + zak_form_element_set_as_original_value (element); + } + } +} + +/** + * zak_form_hashtable_provider_get_values_as_hashtable: + * @provider: + * @elements: + * + * Returns: + */ +GHashTable +*zak_form_hashtable_provider_get_values_as_hashtable (ZakFormHashtableProvider *provider, GPtrArray *elements) +{ + GHashTable *ht; + + guint i; + + ht = g_hash_table_new (g_str_hash, g_str_equal); + + for (i = 0; i < elements->len; i++) + { + ZakFormElement *element = (ZakFormElement *)g_ptr_array_index (elements, i); + if (zak_form_element_get_to_save (element)) + { + g_hash_table_insert (ht, + zak_form_element_get_name (element), + zak_form_hashtable_provider_new_gvalue_from_element (element)); + } + } + + return ht; +} + /* PRIVATE */ static void zak_form_hashtable_provider_set_property (GObject *object, @@ -216,8 +308,6 @@ zak_form_hashtable_provider_load (ZakFormIProvider *provider, GPtrArray *element { gboolean ret; - guint i; - GHashTable *group; ZakFormHashtableProviderPrivate *priv = zak_form_hashtable_provider_get_instance_private (ZAK_FORM_HASHTABLE_PROVIDER (provider)); @@ -226,15 +316,7 @@ zak_form_hashtable_provider_load (ZakFormIProvider *provider, GPtrArray *element group = zak_form_hashtable_provider_get_group (ZAK_FORM_HASHTABLE_PROVIDER (provider), elements); - for (i = 0; i < elements->len; i++) - { - ZakFormElement *element = (ZakFormElement *)g_ptr_array_index (elements, i); - if (zak_form_element_get_to_load (element)) - { - zak_form_element_set_value (element, g_hash_table_lookup (group, zak_form_element_get_name (element))); - zak_form_element_set_as_original_value (element); - } - } + zak_form_hashtable_provider_load_from_hashtable (ZAK_FORM_HASHTABLE_PROVIDER (provider), elements, group); return ret; } diff --git a/src/form.h b/src/form.h index a2a43ab..0b93d09 100644 --- a/src/form.h +++ b/src/form.h @@ -34,6 +34,9 @@ G_DECLARE_FINAL_TYPE (ZakFormHashtableProvider, zak_form_hashtable_provider, ZAK ZakFormHashtableProvider *zak_form_hashtable_provider_new (void); +void zak_form_hashtable_provider_load_from_hashtable (ZakFormHashtableProvider *provider, GPtrArray *elements, GHashTable *ht); +GHashTable *zak_form_hashtable_provider_get_values_as_hashtable (ZakFormHashtableProvider *provider, GPtrArray *elements); + G_END_DECLS -- 2.49.0