/*
- * Copyright (C) 2010 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2010-2011 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
#include <config.h>
#endif
+#include <locale.h>
+
#include <errno.h>
#include <math.h>
#include <gio/gio.h>
#include <gtk/gtk.h>
+#include <libgdaex.h>
+#include <libgtkform/fielddatetime.h>
+
#include "solipa.h"
#include "utils.h"
}
}
+ if (with_currency_symbol)
+ {
+ ret = g_strconcat ("\80 ", ret, NULL);
+ }
+
+ g_regex_unref (regex);
+
+ return ret;
+}
+
+gdouble
+solipa_unformat_money (const gchar *value)
+{
+ gdouble ret;
+
+ GRegex *regex;
+ GError *error;
+
+ gchar *str;
+
+ ret = 0.0;
+
+ error = NULL;
+ regex = g_regex_new ("[\80 .]", 0, 0, &error);
+ if (error != NULL)
+ {
+ g_warning ("Error on creating regex: %s.",
+ error->message != NULL ? error->message : "no details");
+ return ret;
+ }
+
+ error = NULL;
+ str = g_regex_replace ((const GRegex *)regex,
+ value, -1, 0,
+ "", 0,
+ &error);
+ if (error != NULL)
+ {
+ g_warning ("Error on regex replacing: %s.",
+ error->message != NULL ? error->message : "no details");
+ g_regex_unref (regex);
+ return ret;
+ }
+
+ ret = g_strtod (str, NULL);
+
g_regex_unref (regex);
return ret;
return ret;
}
+const gchar
+*solipa_gtk_tree_model_get_value_string (GtkTreeModel *model, GtkTreeIter *iter, gint column)
+{
+ const gchar *ret;
+ GValue *gval;
+
+ ret = NULL;
+
+ gval = g_new0 (GValue, 1);
+ gtk_tree_model_get_value (model, iter, column, gval);
+
+ ret = g_value_get_string (gval);
+ g_value_unset (gval);
+
+ return ret;
+}
+
+gint
+solipa_gtk_tree_model_get_value_integer (GtkTreeModel *model, GtkTreeIter *iter, gint column)
+{
+ gint ret;
+ GValue *gval;
+
+ ret = 0;
+
+ gval = g_new0 (GValue, 1);
+ gtk_tree_model_get_value (model, iter, column, gval);
+
+ if (G_VALUE_HOLDS_STRING (gval))
+ {
+ /* try conversion */
+ ret = (gint)trunc ((gfloat)solipa_unformat_money (g_value_get_string (gval)));
+ }
+ else if (G_VALUE_HOLDS_INT (gval))
+ {
+ ret = g_value_get_int (gval);
+ }
+ else if (G_VALUE_HOLDS_UINT (gval))
+ {
+ ret = (gint)g_value_get_uint (gval);
+ }
+ g_value_unset (gval);
+
+ return ret;
+}
+
+gdouble
+solipa_gtk_tree_model_get_value_double (GtkTreeModel *model, GtkTreeIter *iter, gint column)
+{
+ gdouble ret;
+ GValue *gval;
+
+ ret = 0.0;
+
+ gval = g_new0 (GValue, 1);
+ gtk_tree_model_get_value (model, iter, column, gval);
+
+ if (G_VALUE_HOLDS_STRING (gval))
+ {
+ /* try conversion */
+ ret = solipa_unformat_money (g_value_get_string (gval));
+ }
+ else
+ {
+ ret = g_value_get_double (gval);
+ }
+ g_value_unset (gval);
+
+ return ret;
+}
+
+const gchar
+*solipa_gtk_tree_model_get_value_double_sql (GtkTreeModel *model, GtkTreeIter *iter, gint column)
+{
+ char *cur;
+
+ const gchar *ret;
+ gdouble res;
+
+ res = solipa_gtk_tree_model_get_value_double (model, iter, column);
+
+ cur = g_strdup (setlocale (LC_NUMERIC, NULL));
+ setlocale (LC_NUMERIC, "C");
+ gda_locale_changed ();
+
+ ret = g_strdup_printf ("%f", res);
+
+ setlocale (LC_NUMERIC, cur);
+ gda_locale_changed ();
+ g_free (cur);
+
+ return ret;
+}
+
+struct tm
+*solipa_gtk_tree_model_get_value_tm (GtkTreeModel *model, GtkTreeIter *iter, gint column, const gchar *format)
+{
+ struct tm *ret;
+ const gchar *value;
+
+ ret = NULL;
+
+ value = solipa_gtk_tree_model_get_value_string (model, iter, column);
+
+ ret = gtk_form_field_datetime_get_tm_from_str (value, format);
+
+ return ret;
+}
+
+const gchar
+*solipa_tm_to_sql (struct tm *tm_data, const gchar *format)
+{
+ const gchar *ret;
+ gchar *_format;
+
+ ret = g_strdup ("NULL");
+
+ if (tm_data != NULL)
+ {
+ if (format == NULL)
+ {
+ _format = g_strdup ("%Y-%m-%d %H:%M");
+ }
+ else
+ {
+ _format = g_strdup (format);
+ g_strstrip (_format);
+ if (g_strcmp0 (_format, "") == 0)
+ {
+ _format = g_strdup ("%Y-%m-%d %H:%M");
+ }
+ }
+ ret = g_strdup_printf ("'%s'", gtk_form_field_datetime_get_str_from_tm (tm_data, _format));
+ }
+
+ return ret;
+}
+
/**
* This function is copied from
* http://bugzilla.gnome.org/show_bug.cgi?id=524831.
/*
- * Copyright (C) 2010 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2010-2011 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
guint n_columns);
gchar *solipa_format_money (gdouble number, gint decimals, gboolean with_currency_symbol);
+gdouble solipa_unformat_money (const gchar *value);
struct tm *solipa_get_now_tm (void);
GDate *solipa_get_today_gdate (void);
gdouble solipa_round (gdouble value, guint n_decimals);
+const gchar *solipa_gtk_tree_model_get_value_string (GtkTreeModel *model, GtkTreeIter *iter, gint column);
+gint solipa_gtk_tree_model_get_value_integer (GtkTreeModel *model, GtkTreeIter *iter, gint column);
+gdouble solipa_gtk_tree_model_get_value_double (GtkTreeModel *model, GtkTreeIter *iter, gint column);
+const gchar *solipa_gtk_tree_model_get_value_double_sql (GtkTreeModel *model, GtkTreeIter *iter, gint column);
+struct tm *solipa_gtk_tree_model_get_value_tm (GtkTreeModel *model, GtkTreeIter *iter, gint column, const gchar *format);
+
+const gchar *solipa_tm_to_sql (struct tm *tm_data, const gchar *format);
+
gchar *g_mkdtemp (gchar *tmpl);