]> saetta.ns0.it Git - zakform/hashtable/commitdiff
Use of GValue.
authorAndrea Zagli <azagli@libero.it>
Mon, 30 Jan 2017 15:28:19 +0000 (16:28 +0100)
committerAndrea Zagli <azagli@libero.it>
Mon, 30 Jan 2017 15:28:19 +0000 (16:28 +0100)
src/form.c

index 2e121a6269c48b110229c9196f9a7f72347a460c..0c6e56968760acd514554374fddb0794df686385 100644 (file)
@@ -236,7 +236,66 @@ zak_form_hashtable_provider_load (ZakFormIProvider *provider, GPtrArray *element
                                }
                }
 
-       g_free (group);
+       return ret;
+}
+
+static GValue
+*zak_form_hashtable_provider_new_gvalue_from_element (ZakFormElement *element)
+{
+       GValue *ret;
+
+       gchar *value;
+       gchar *type;
+       GHashTable *format;
+
+       value = zak_form_element_get_value (element);
+       type = zak_form_element_get_provider_type (element);
+       format = zak_form_element_get_format (element);
+
+       if (g_ascii_strcasecmp (type, "integer") == 0)
+               {
+                       gchar *thousands_saparator;
+                       gdouble unformatted;
+
+                       thousands_saparator = (gchar *)g_hash_table_lookup (format, "thousands_separator");
+
+                       unformatted = zak_utils_unformat_money_full (value, thousands_saparator, NULL);
+
+                       ret = zak_utils_gvalue_new_int ((gint)unformatted);
+               }
+       else if (g_ascii_strcasecmp (type, "float") == 0)
+               {
+                       gchar *thousands_saparator;
+                       gchar *currency_symbol;
+                       gdouble unformatted;
+
+                       thousands_saparator = (gchar *)g_hash_table_lookup (format, "thousands_separator");
+                       currency_symbol = (gchar *)g_hash_table_lookup (format, "currency_symbol");
+
+                       unformatted = zak_utils_unformat_money_full (value, thousands_saparator, currency_symbol);
+                       ret = zak_utils_gvalue_new_float (unformatted);
+               }
+       else if (g_ascii_strcasecmp (type, "string") == 0)
+               {
+                       ret = zak_utils_gvalue_new_string (value);
+               }
+       else if (g_ascii_strcasecmp (type, "boolean") == 0)
+               {
+                       ret = zak_utils_gvalue_new_boolean (g_strcmp0 (value, "TRUE") == 0);
+               }
+       else if (g_ascii_strcasecmp (type, "date") == 0
+                || g_ascii_strcasecmp (type, "time") == 0
+                || g_ascii_strcasecmp (type, "datetime") == 0)
+               {
+                       GDateTime *gdt;
+
+                       gchar *datetime_format;
+
+                       datetime_format = (gchar *)g_hash_table_lookup (format, "content");
+                       gdt = zak_utils_get_gdatetime_from_string (value, datetime_format);
+
+                       ret = zak_utils_gvalue_new_gdatetime (gdt);
+               }
 
        return ret;
 }
@@ -248,7 +307,6 @@ zak_form_hashtable_provider_insert (ZakFormIProvider *provider, GPtrArray *eleme
 
        guint i;
 
-       gchar *value;
        GHashTable *group;
 
        ZakFormHashtableProviderPrivate *priv = zak_form_hashtable_provider_get_instance_private (ZAK_FORM_HASHTABLE_PROVIDER (provider));
@@ -262,18 +320,12 @@ zak_form_hashtable_provider_insert (ZakFormIProvider *provider, GPtrArray *eleme
                        ZakFormElement *element = (ZakFormElement *)g_ptr_array_index (elements, i);
                        if (zak_form_element_get_to_save (element))
                                {
-                                       value = zak_form_element_unformat (element, NULL);
-
                                        g_hash_table_insert (group,
                                                             zak_form_element_get_name (element),
-                                                            value);
-
-                                       g_free (value);
+                                                            zak_form_hashtable_provider_new_gvalue_from_element (element));
                                }
                }
 
-       g_free (group);
-
        return ret;
 }
 
@@ -308,7 +360,5 @@ zak_form_hashtable_provider_delete (ZakFormIProvider *provider, GPtrArray *eleme
                                }
                }
 
-       g_free (group);
-
        return ret;
 }