]> saetta.ns0.it Git - libgdaex/commitdiff
Merge master.
authorAndrea Zagli <azagli@libero.it>
Mon, 25 Jul 2011 07:24:36 +0000 (09:24 +0200)
committerAndrea Zagli <azagli@libero.it>
Mon, 25 Jul 2011 07:24:36 +0000 (09:24 +0200)
1  2 
.gitignore
configure.ac
src/Makefile.am
src/gdaex.c
src/gdaex.h
src/libgdaex.h
tests/Makefile.am

diff --cc .gitignore
index 6c6b3455091098a33ba52e441f895733359075df,fe2982b923fe835a6dcbc45dd421e187a3a97d1d..219890fba9a15ad28dafbb4d38a67b17efa63c92
@@@ -32,6 -32,6 +32,7 @@@ tests/.deps
  *.la
  *.bak
  libgdaex*tar*
- tests/test_prefix
+ tests/test_prefix*
 +tests/query_editor
+ tests/grid
 -tests/grid.exe
 +tests/*.exe
diff --cc configure.ac
index b7e616b8cf9d9d80863333ebe79ed7a6e96c1022,a57926d1a7b8aadd23c5aed55f17c6bf7f6b3a00..f929ef6d7679dfa1ddec5fd8b123682ac63deccf
@@@ -2,7 -2,7 +2,7 @@@
  # Process this file with autoconf to produce a configure script.
  
  AC_PREREQ(2.59)
- AC_INIT([libgdaex], [0.3.0], [azagli@libero.it])
 -AC_INIT([libgdaex], [0.3.1], [azagli@libero.it])
++AC_INIT([libgdaex], [0.4.0], [azagli@libero.it])
  AC_CONFIG_SRCDIR([src/gdaex.c])
  AM_CONFIG_HEADER([config.h])
  
diff --cc src/Makefile.am
index fd7f6c45d63e57688ece4442581c3a69983d1105,167a7ca4c0126134f9ab67919a4dc46e28f20ab0..55c1b66921c29785e13c008e1f611ebdafcb0978
@@@ -10,12 -7,14 +10,16 @@@ LIBS = $(GDAEX_LIBS
  lib_LTLIBRARIES = libgdaex.la
  
  libgdaex_la_SOURCES = gdaex.c \
-                       queryeditor.c
++                      queryeditor.c \
+                       grid.c \
+                       gridcolumn.c
  
  libgdaex_la_LDFLAGS = -no-undefined
  
- include_HEADERS = libgdaex.h \
-                   gdaex.h \
-                   queryeditor.h
libgdaex_include_HEADERS = libgdaex.h \
+                            gdaex.h \
+                            grid.h \
 -                           gridcolumn.h
++                           gridcolumn.h \
++                           queryeditor.h
  
  libgdaex_includedir = $(includedir)/libgdaex
diff --cc src/gdaex.c
index 65afd824843a2fd1e5e5b09b9326dbe53f1b6cdc,7f1bce6be2d68d00fb7c5a788fc7bae61448229e..e03ece52f48307cf95a0ba3d8ced0fb1566f33ed
@@@ -1668,30 -2469,228 +2503,252 @@@ gdaex_get_chr_quoting (GdaEx *gdaex
        return chr;
  }
  
 +const gchar
 +*gdaex_get_guifile (GdaEx *gdaex)
 +{
 +      GdaExPrivate *priv;
 +
 +      g_return_val_if_fail (IS_GDAEX (gdaex), NULL);
 +
 +      priv = GDAEX_GET_PRIVATE (gdaex);
 +
 +      return (const gchar *)g_strdup (priv->guifile);
 +}
 +
 +GtkBuilder
 +*gdaex_get_gtkbuilder (GdaEx *gdaex)
 +{
 +      GdaExPrivate *priv;
 +
 +      g_return_val_if_fail (IS_GDAEX (gdaex), NULL);
 +
 +      priv = GDAEX_GET_PRIVATE (gdaex);
 +
 +      return priv->gtkbuilder;
 +}
 +
+ void
+ gdaex_fill_liststore_from_sql (GdaEx *gdaex,
+                                GtkListStore *lstore,
+                                const gchar *sql,
+                                guint *cols_formatted,
+                                gchar *(*cols_format_func) (GdaDataModelIter *, guint))
+ {
+       GdaDataModel *dm;
+       gchar *_sql;
+       g_return_if_fail (IS_GDAEX (gdaex));
+       g_return_if_fail (sql != NULL);
+       _sql = g_strstrip (g_strdup (sql));
+       g_return_if_fail (g_strcmp0 (_sql, "") != 0);
+       dm = gdaex_query (gdaex, _sql);
+       gdaex_fill_liststore_from_datamodel (gdaex, lstore, dm, cols_formatted, cols_format_func);
+ }
+ void
+ gdaex_fill_liststore_from_datamodel (GdaEx *gdaex,
+                                      GtkListStore *lstore,
+                                      GdaDataModel *dm,
+                                      guint *cols_formatted,
+                                      gchar *(*cols_format_func) (GdaDataModelIter *, guint))
+ {
+       GtkTreeIter iter;
+       GdaDataModelIter *gda_iter;
+       guint cols;
+       guint col;
+       GType col_gtype;
+       GdaColumn *gcol;
+       GType gcol_gtype;
+       gint *columns;
+       GValue *values;
+       gdouble dval;
+       GDateTime *gdatetime;
+       g_return_if_fail (IS_GDAEX (gdaex));
+       g_return_if_fail (GTK_IS_LIST_STORE (lstore));
+       g_return_if_fail (GDA_IS_DATA_MODEL (dm));
+       cols = gtk_tree_model_get_n_columns (GTK_TREE_MODEL (lstore));
+       gtk_list_store_clear (lstore);
+       gda_iter = gda_data_model_create_iter (dm);
+       if (gda_iter == NULL)
+               {
+                       return;
+               }
+       columns = g_malloc0 (cols * sizeof (gint));
+       values = g_malloc0 (cols * sizeof (GValue));
+       while (gda_data_model_iter_move_next (gda_iter))
+               {
+                       gtk_list_store_append (lstore, &iter);
+                       for (col = 0; col < cols; col++)
+                               {
+                                       columns[col] = col;
+                                       col_gtype = gtk_tree_model_get_column_type (GTK_TREE_MODEL (lstore), col);
+                                       GValue gval = {0};
+                                       g_value_init (&gval, col_gtype);
+                                       switch (col_gtype)
+                                               {
+                                                       case G_TYPE_STRING:
+                                                               gcol = gda_data_model_describe_column (dm, col);
+                                                               gcol_gtype = gda_column_get_g_type (gcol);
+                                                               switch (gcol_gtype)
+                                                                       {
+                                                                               case G_TYPE_STRING:
+                                                                                       g_value_set_string (&gval, gdaex_data_model_iter_get_value_stringify_at (gda_iter, col));
+                                                                                       break;
+                                                                               case G_TYPE_BOOLEAN:
+                                                                                       g_value_set_string (&gval, gdaex_data_model_iter_get_value_boolean_at (gda_iter, col) ? "X" : "");
+                                                                                       break;
+                                                                               case G_TYPE_INT:
+                                                                               case G_TYPE_FLOAT:
+                                                                               case G_TYPE_DOUBLE:
+                                                                                       dval = gdaex_data_model_iter_get_value_double_at (gda_iter, col);
+                                                                                       g_value_set_string (&gval, gdaex_format_money (dval, -1, FALSE));
+                                                                                       break;
+                                                                               default:
+                                                                                       if (cols_format_func != NULL)
+                                                                                               {
+                                                                                                       g_value_set_string (&gval, (*cols_format_func) (gda_iter, col));
+                                                                                               }
+                                                                                       else if (gcol_gtype == G_TYPE_DATE
+                                                                                                || gcol_gtype == GDA_TYPE_TIMESTAMP
+                                                                                                || gcol_gtype == G_TYPE_DATE_TIME)
+                                                                                               {
+                                                                                                       gdatetime = gdaex_data_model_iter_get_value_gdatetime_at (gda_iter, col);
+                                                                                                       /* TODO find default format from locale */
+                                                                                                       g_value_set_string (&gval, g_date_time_format (gdatetime, gcol_gtype == G_TYPE_DATE ? "%d/%m/%Y" : "%d/%m/%Y %H.%M.%S"));
+                                                                                               }
+                                                                                       else
+                                                                                               {
+                                                                                                       g_value_set_string (&gval, gda_value_stringify (gda_data_model_iter_get_value_at (gda_iter, col)));
+                                                                                               }
+                                                                                       break;
+                                                                       }
+                                                               values[col] = gval;
+                                                               break;
+                                                       case G_TYPE_INT:
+                                                               g_value_set_int (&gval, gdaex_data_model_iter_get_value_integer_at (gda_iter, col));
+                                                               values[col] = gval;
+                                                               break;
+                                                       case G_TYPE_FLOAT:
+                                                               g_value_set_float (&gval, gdaex_data_model_iter_get_value_float_at (gda_iter, col));
+                                                               values[col] = gval;
+                                                               break;
+                                                       case G_TYPE_DOUBLE:
+                                                               g_value_set_double (&gval, gdaex_data_model_iter_get_value_double_at (gda_iter, col));
+                                                               values[col] = gval;
+                                                               break;
+                                                       case G_TYPE_BOOLEAN:
+                                                               g_value_set_boolean (&gval, gdaex_data_model_iter_get_value_boolean_at (gda_iter, col));
+                                                               values[col] = gval;
+                                                               break;
+                                                       default:
+                                                               values[col] = *gda_value_new_from_string (gdaex_data_model_iter_get_value_stringify_at (gda_iter, col), col_gtype);
+                                                               break;
+                                               }
+                               }
+                       gtk_list_store_set_valuesv (lstore, &iter, columns, values, cols);
+               }
+ }
+ gchar
+ *gdaex_format_money (gdouble number,
+                      gint decimals,
+                      gboolean with_currency_symbol)
+ {
+       gchar *ret;
+       GRegex *regex;
+       GError *error;
+       gchar *str_format;
+       gchar *str;
+       gssize str_len;
+       /* TODO
+        * - get number of decimals from locale
+        * - get grouping char from locale
+        * - get currency symbol from locale
+        */
+       ret = g_strdup ("");
+       error = NULL;
+       regex = g_regex_new ("(^[-\\d]?\\d+)(\\d\\d\\d)", 0, 0, &error);
+       if (error != NULL)
+               {
+                       g_warning ("Error on creating regex: %s.",
+                                  error->message != NULL ? error->message : "no details");
+                       return "";
+               }
+       str_format = g_strdup_printf ("%%0%sf", decimals == 0 ? ".0" : (decimals < 0 ? ".2" : g_strdup_printf (".%d", decimals)));
+       ret = g_strdup_printf (str_format, number);
+       while (TRUE)
+               {
+                       error = NULL;
+                       str_len = g_utf8_strlen (ret, -1);
+                       str = g_regex_replace ((const GRegex *)regex,
+                                              ret, str_len, 0,
+                                              "\\1.\\2", 0,
+                                              &error);
+                       if (error != NULL)
+                               {
+                                       g_warning ("Error on regex replacing: %s.",
+                                                  error->message != NULL ? error->message : "no details");
+                                       g_regex_unref (regex);
+                                       return "";
+                               }
+                       if (g_strcmp0 (ret, str) != 0)
+                               {
+                                       ret = g_strdup (str);
+                                       g_free (str);
+                               }
+                       else
+                               {
+                                       break;
+                               }
+               }
+       if (with_currency_symbol)
+               {
+                       ret = g_strconcat ("€ ", ret, NULL);
+               }
+       g_regex_unref (regex);
+       return ret;
+ }
  /* PRIVATE */
  static void
  gdaex_create_connection_parser (GdaEx *gdaex)
diff --cc src/gdaex.h
index 5c2f7e7e79150d1e57c096a9cb3f197cd9a2c07d,909faf606305067d02de5d14410d3821ba845df3..a9be34ac67cd3eb6bd398b602b0c353d3eb583eb
@@@ -141,10 -188,17 +188,21 @@@ gchar *gdaex_strescape (const gchar *so
  
  gchar gdaex_get_chr_quoting (GdaEx *gdaex);
  
 +const gchar *gdaex_get_guifile (GdaEx *gdaex);
 +
 +GtkBuilder *gdaex_get_gtkbuilder (GdaEx *gdaex);
 +
+ void gdaex_fill_liststore_from_sql (GdaEx *gdaex, GtkListStore *lstore, const gchar *sql,
+                                      guint *cols_formatted,
+                                      gchar *(*cols_format_func) (GdaDataModelIter *, guint));
+ void gdaex_fill_liststore_from_datamodel (GdaEx *gdaex, GtkListStore *lstore, GdaDataModel *dm,
+                                      guint *cols_formatted,
+                                      gchar *(*cols_format_func) (GdaDataModelIter *, guint));
+ gchar *gdaex_format_money (gdouble number,
+                      gint decimals,
+                      gboolean with_currency_symbol);
  
  G_END_DECLS
  
diff --cc src/libgdaex.h
index 65aab8d94e529ded3c5a053cd5d80702513a7c04,1ee2469bcd6e8442650f67b382b10df0d0c5ad11..5e3d8cacbf89b1403aff7dfee4d3594fd93ae4a8
@@@ -25,7 -25,8 +25,9 @@@
  
  
  #include "gdaex.h"
+ #include "grid.h"
+ #include "gridcolumn.h"
 +#include "queryeditor.h"
  
  
  #endif /* __LIBGDAEX_H__ */
index 1b7cac77f7a4a6ea0c2fbd5a83e2565ca1b8a9a6,8081d400d8109ccd344767b36574993f61756869..117678521841ce883e8a42ebfe1ccb94e408533e
@@@ -6,7 -6,7 +6,8 @@@ AM_CPPFLAGS = $(GDAEX_CFLAGS) 
                -DTESTSDIR="\"@abs_builddir@\""
  
  noinst_PROGRAMS = test_prefix \
-                   query_editor
++                  query_editor \
+                   grid
  
  LDADD = $(top_builddir)/src/libgdaex.la