]> saetta.ns0.it Git - zakform/hashtable/commitdiff
Added functions ::load_from_hashtable and ::get_values_as_hashtable. master
authorAndrea Zagli <azagli@libero.it>
Mon, 30 Jan 2017 17:14:34 +0000 (18:14 +0100)
committerAndrea Zagli <azagli@libero.it>
Mon, 30 Jan 2017 17:14:34 +0000 (18:14 +0100)
Use of GValue.

src/form.c
src/form.h

index 0c6e56968760acd514554374fddb0794df686385..83c491424ce4d97cd5d724af2661baacbcab8e55 100644 (file)
@@ -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;
 }
index a2a43abc230ab83217d7972cd44e5cecb9b43b72..0b93d092b119e786efda161cdfa3efd5c8b3b339 100644 (file)
@@ -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