]> saetta.ns0.it Git - solipa/libsolipa/commitdiff
Aggiunta la possibilità di esportare un GtkTreeStore.
authorAndrea Zagli <azagli@libero.it>
Sat, 25 Dec 2010 08:02:46 +0000 (09:02 +0100)
committerAndrea Zagli <azagli@libero.it>
Sat, 25 Dec 2010 08:02:46 +0000 (09:02 +0100)
Aggiustamenti e correzioni vari.

src/utils.c
src/utils.h
tests/utils.c

index 3cea209e561437894796adbb6b41bdb11d77cff2..d8b90bdd9a8158a0accc2a3ef7bddb5f0ed09a20 100644 (file)
@@ -20,6 +20,8 @@
        #include <config.h>
 #endif
 
+#include <errno.h>
+
 #include <gio/gio.h>
 #include <gtk/gtk.h>
 
@@ -44,7 +46,86 @@ solipa_file_exists (const gchar *filename)
        return (g_file_query_exists (g_file_new_for_path (filename), NULL));
 }
 
-void
+static gboolean
+solipa_gtktreeiter_export_to_csv (GtkTreeModel *model, GtkTreeIter iter,
+                                  GOutputStream *ostream,
+                                  gchar *quote, gboolean quote_all,
+                                  gchar *separator,
+                                  GType *gtypes,
+                                  guint *columns, guint n_columns)
+{
+       gboolean ret;
+
+       GError *error;
+
+       gchar *row;
+       const gchar *str;
+
+       guint i;
+
+       GValue *gval;
+       GValue *gvalstr;
+
+       ret = TRUE;
+
+       do
+               {
+                       row = g_strdup ("");
+                       for (i = 0; i < n_columns; i++)
+                               {
+                                       gval = g_new0 (GValue, 1);
+                                       gvalstr = g_value_init (g_new0 (GValue, 1), G_TYPE_STRING);
+
+                                       gtk_tree_model_get_value (model, &iter, columns[i], gval);
+                                       g_value_transform (gval, gvalstr);
+
+                                       str = g_value_get_string (gvalstr);
+                                       row = g_strconcat (row,
+                                                  (i > 0 ? separator : ""),
+                                                  (quote_all || gtypes[i] == G_TYPE_STRING ? quote : ""),
+                                                  (str == NULL ? "" : str),
+                                                  (quote_all || gtypes[i] == G_TYPE_STRING ? quote : ""),
+                                                  NULL);
+
+                                       g_value_unset (gval);
+                                       g_value_unset (gvalstr);
+                               }
+                       row = g_strconcat (row, "\n", NULL);
+
+                       error = NULL;
+                       if (!g_output_stream_write_all (ostream,
+                                                       row, g_utf8_strlen (row, -1), NULL, NULL, &error))
+                               {
+                                       g_warning ("Errore nella scrittura del file di output: %s",
+                                                  error != NULL && error->message != NULL ? error->message : "nessun dettaglio");
+                                       return FALSE;
+                               }
+
+                       g_free (row);
+
+                       if (gtk_tree_model_iter_has_child (model, &iter))
+                               {
+                                       GtkTreeIter child;
+
+                                       gtk_tree_model_iter_children (model, &child, &iter);
+
+                                       if (!solipa_gtktreeiter_export_to_csv (model, child,
+                                                                         ostream,
+                                                                         quote, quote_all,
+                                                                         separator,
+                                                                         gtypes,
+                                                                         columns, n_columns))
+                                               {
+                                                       return FALSE;
+                                               }
+                               }
+               }
+       while (gtk_tree_model_iter_next (model, &iter));
+
+       return ret;
+}
+
+gboolean
 solipa_gtktreemodel_to_csv (GtkTreeModel *model, const gchar *filename,
                             gboolean overwrite,
                             const gchar *quote, gboolean quote_all,
@@ -62,24 +143,25 @@ solipa_gtktreemodel_to_csv (GtkTreeModel *model, const gchar *filename,
        GFileOutputStream *ostream;
        GError *error;
 
-       guint i;
-
        gchar *row;
 
+       guint i;
+
        GtkTreeIter iter;
-       GType *gtype;
        GType *gtypes;
-       GValue *gval;
-       GValue *gvalstr;
 
-       g_return_if_fail (GTK_IS_TREE_MODEL (model));
-       g_return_if_fail (filename != NULL);
-       g_return_if_fail (columns_title != NULL);
-       g_return_if_fail (columns != NULL);
-       g_return_if_fail (n_columns > 0);
+       gboolean ret;
+
+       g_return_val_if_fail (GTK_IS_TREE_MODEL (model), FALSE);
+       g_return_val_if_fail (filename != NULL, FALSE);
+       g_return_val_if_fail (columns_title != NULL, FALSE);
+       g_return_val_if_fail (columns != NULL, FALSE);
+       g_return_val_if_fail (n_columns > 0, FALSE);
 
        _filename = g_strstrip (g_strdup (filename));
-       g_return_if_fail (g_strcmp0 (_filename, "") != 0);
+       g_return_val_if_fail (g_strcmp0 (_filename, "") != 0, FALSE);
+
+       ret = TRUE;
 
        if (quote != NULL)
                {
@@ -120,7 +202,7 @@ solipa_gtktreemodel_to_csv (GtkTreeModel *model, const gchar *filename,
                                {
                                        g_warning ("Errore nella creazione del file di output: %s",
                                                   error != NULL && error->message != NULL ? error->message : "nessun dettaglio");
-                                       return;
+                                       return FALSE;
                                }
 
                        if (fields_name_first_row)
@@ -142,7 +224,7 @@ solipa_gtktreemodel_to_csv (GtkTreeModel *model, const gchar *filename,
                                                {
                                                        g_warning ("Errore nella scrittura del file di output: %s",
                                                                   error != NULL && error->message != NULL ? error->message : "nessun dettaglio");
-                                                       return;
+                                                       return FALSE;
                                                }
 
                                        g_free (row);
@@ -155,50 +237,28 @@ solipa_gtktreemodel_to_csv (GtkTreeModel *model, const gchar *filename,
                                        gtypes[i] = gtk_tree_model_get_column_type (model, columns[i]);
                                }
 
-                       do
-                               {
-                                       row = g_strdup ("");
-                                       for (i = 0; i < n_columns; i++)
-                                               {
-                                                       gval = g_new0 (GValue, 1);
-                                                       gvalstr = g_value_init (g_new0 (GValue, 1), G_TYPE_STRING);
-
-                                                       gtk_tree_model_get_value (model, &iter, columns[i], gval);
-                                                       g_value_transform (gval, gvalstr);
-
-                                                       row = g_strconcat (row,
-                                                                  (i > 0 ? _separator : ""),
-                                                                  (quote_all || gtypes[i] == G_TYPE_STRING ? _quote : ""),
-                                                                  g_value_get_string (gvalstr),
-                                                                  (quote_all || gtypes[i] == G_TYPE_STRING ? _quote : ""),
-                                                                  NULL);
-
-                                                       g_value_unset (gval);
-                                                       g_value_unset (gvalstr);
-                                               }
-                                       row = g_strconcat (row, "\n", NULL);
-
-                                       error = NULL;
-                                       if (!g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
-                                                                       row, g_utf8_strlen (row, -1), NULL, NULL, &error))
-                                               {
-                                                       g_warning ("Errore nella scrittura del file di output: %s",
-                                                                  error != NULL && error->message != NULL ? error->message : "nessun dettaglio");
-                                                       return;
-                                               }
-
-                                       g_free (row);
-                               }
-                       while (gtk_tree_model_iter_next (model, &iter));
+                       solipa_gtktreeiter_export_to_csv (model, iter,
+                                                         G_OUTPUT_STREAM (ostream),
+                                                         _quote, quote_all,
+                                                         _separator,
+                                                         gtypes,
+                                                         columns, n_columns);
 
                        error = NULL;
                        if (!g_output_stream_close (G_OUTPUT_STREAM (ostream), NULL, &error))
                                {
                                        g_warning ("Errore nella chiusura del file di output: %s",
                                                   error != NULL && error->message != NULL ? error->message : "nessun dettaglio");
+                                       ret = FALSE;
                                }
                        g_object_unref (ostream);
                }
+       else
+               {
+                       g_warning ("Nessun record da esportare.");
+               }
+
+       return ret;
 }
 
 static void
@@ -253,7 +313,7 @@ solipa_on_btn_unselect_all_clicked (GtkButton *button,
        solipa_gtkliststore_select_unselect_all ((GtkListStore *)user_data, FALSE);
 }
 
-void
+guint
 solipa_gtktreemodel_to_csv_gui (Solipa *solipa,
                                 GtkWindow *parent,
                                 GtkTreeModel *model,
@@ -275,16 +335,20 @@ solipa_gtktreemodel_to_csv_gui (Solipa *solipa,
        guint *columns;
        guint _n_columns;
 
-       g_return_if_fail (IS_SOLIPA (solipa));
+       guint ret;
+
+       g_return_val_if_fail (IS_SOLIPA (solipa), 0);
 
        builder = solipa_get_gtkbuilder (solipa);
 
        if (!GTK_IS_BUILDER (builder))
                {
-                       g_warning ("GtkBuilder not initialized.");
-                       return;
+                       g_warning ("GtkBuilder non inizializzato.");
+                       return 0;
                }
 
+       ret = 1;
+
        error = NULL;
        gtk_builder_add_objects_from_file (builder,
                                           solipa_get_guifile (solipa),
@@ -294,9 +358,9 @@ solipa_gtktreemodel_to_csv_gui (Solipa *solipa,
                                           &error);
        if (error != NULL)
                {
-                       g_warning ("Error: %s.",
+                       g_warning ("Errore nell'inzializzazione della gui: %s.",
                                   error->message != NULL ? error->message : "no details");
-                       return;
+                       return 0;
                }
 
        diag = GTK_WIDGET (gtk_builder_get_object (builder, "diag_gtktreemodel_to_csv"));
@@ -308,6 +372,11 @@ solipa_gtktreemodel_to_csv_gui (Solipa *solipa,
 
                        /* carico i nomi campi per la selezione nel model */
                        store = GTK_LIST_STORE (gtk_builder_get_object (builder, "lstore_treemodel_campi"));
+                       if (!GTK_IS_LIST_STORE (store))
+                               {
+                                       g_warning ("Errore nell'ottenimento del list store.");
+                                       return 0;
+                               }
 
                        g_signal_connect (gtk_builder_get_object (builder, "cellrenderertoggle1"),
                                           "toggled", G_CALLBACK (solipa_fixed_toggled), (gpointer)store);
@@ -371,7 +440,7 @@ solipa_gtktreemodel_to_csv_gui (Solipa *solipa,
                                        else
                                                {
                                                        gtk_widget_destroy (diag);
-                                                       return;
+                                                       return -1;
                                                }
                                } while (TRUE);
 
@@ -393,7 +462,7 @@ solipa_gtktreemodel_to_csv_gui (Solipa *solipa,
                                }
 
                        /* export */
-                       solipa_gtktreemodel_to_csv (model,
+                       if (!solipa_gtktreemodel_to_csv (model,
                                                    filename,
                                                    TRUE,
                                                    gtk_entry_get_text (GTK_ENTRY (gtk_builder_get_object (builder, "entry1"))),
@@ -402,10 +471,15 @@ solipa_gtktreemodel_to_csv_gui (Solipa *solipa,
                                                    gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (gtk_builder_get_object (builder, "checkbutton3"))),
                                                    columns_title,
                                                    columns,
-                                                   _n_columns);
+                                                   _n_columns))
+                               {
+                                       ret = 0;
+                               }
 
                        gtk_widget_destroy (diag);
                }
+
+       return ret;
 }
 
 gchar
index 843b83213f1b2db63d965e00396df471bfc9bb12..251e66c0d19a56888938312eaa57d53d5d6399b1 100644 (file)
@@ -29,7 +29,7 @@ G_BEGIN_DECLS
 
 gboolean solipa_file_exists (const gchar *filename);
 
-void solipa_gtktreemodel_to_csv (GtkTreeModel *model, const gchar *filename,
+gboolean solipa_gtktreemodel_to_csv (GtkTreeModel *model, const gchar *filename,
                                  gboolean overwrite,
                                  const gchar *quote, gboolean quote_all,
                                  const gchar *separator,
@@ -38,7 +38,7 @@ void solipa_gtktreemodel_to_csv (GtkTreeModel *model, const gchar *filename,
                                  guint *columns,
                                  guint n_columns);
 
-void solipa_gtktreemodel_to_csv_gui (Solipa *solipa,
+guint solipa_gtktreemodel_to_csv_gui (Solipa *solipa,
                                  GtkWindow *parent,
                                  GtkTreeModel *model,
                                  gchar **columns_title,
index c7488acfa0643162176ed5f7827a6c8467717e91..74f9056bbcd817e2b118d4a0783404e538188977 100644 (file)
@@ -24,12 +24,14 @@ main (int argc, char *argv[])
 {
        Solipa *solipa;
 
-       GtkListStore *model;
+       GtkListStore *lstore;
+       GtkTreeStore *tstore;
 
        gchar **columns_title;
        guint *columns;
 
        GtkTreeIter iter;
+       GtkTreeIter child;
 
        guint row;
        guint rows;
@@ -38,6 +40,8 @@ main (int argc, char *argv[])
 
        solipa = solipa_new ();
 
+       /* LIST STORE */
+
        columns_title = g_malloc0 (3 * sizeof (gchar));
        columns_title[0] = g_strdup ("ID");
        columns_title[1] = g_strdup ("Ragione Sociale");
@@ -47,7 +51,7 @@ main (int argc, char *argv[])
        columns[0] = 0;
        columns[1] = 2;
 
-       model = gtk_list_store_new (3,
+       lstore = gtk_list_store_new (3,
                                    G_TYPE_UINT,
                                    G_TYPE_STRING,
                                    G_TYPE_STRING);
@@ -55,15 +59,15 @@ main (int argc, char *argv[])
        rows = 10;
        for (row = 0; row < rows; row++)
                {
-                       gtk_list_store_append (model, &iter);
-                       gtk_list_store_set (model, &iter,
+                       gtk_list_store_append (lstore, &iter);
+                       gtk_list_store_set (lstore, &iter,
                                            0, row + 1,
                                            1, g_strdup_printf ("Ditta %d Srl", row + 1),
                                            2, g_strdup_printf ("Via dei rossi, %d", row + 1),
                                            -1);
                }
 
-       solipa_gtktreemodel_to_csv (GTK_TREE_MODEL (model), "treemodel.csv",
+       solipa_gtktreemodel_to_csv (GTK_TREE_MODEL (lstore), "treemodel_list.csv",
                                    TRUE,
                                    NULL, FALSE,
                                    NULL,
@@ -72,8 +76,63 @@ main (int argc, char *argv[])
                                    columns,
                                    2);
 
-       solipa_gtktreemodel_to_csv_gui (solipa, NULL, GTK_TREE_MODEL (model),
+       solipa_gtktreemodel_to_csv_gui (solipa, NULL, GTK_TREE_MODEL (lstore),
                                        columns_title, 3);
 
+       /* TREE STORE */
+
+       g_free (columns_title);
+       g_free (columns);
+
+       columns_title = g_malloc0 (5 * sizeof (gchar));
+       columns_title[0] = g_strdup ("ID");
+       columns_title[1] = g_strdup ("Ragione Sociale");
+       columns_title[2] = g_strdup ("Indirizzo");
+       columns_title[3] = g_strdup ("Data fattura");
+       columns_title[4] = g_strdup ("Importo");
+
+       columns = g_malloc0 (5 * sizeof (guint));
+       columns[0] = 0;
+       columns[1] = 1;
+       columns[2] = 2;
+       columns[3] = 3;
+       columns[4] = 4;
+
+       tstore = gtk_tree_store_new (5,
+                                   G_TYPE_UINT,
+                                   G_TYPE_STRING,
+                                   G_TYPE_STRING,
+                                   G_TYPE_STRING,
+                                   G_TYPE_STRING);
+
+       rows = 10;
+       for (row = 0; row < rows; row++)
+               {
+                       gtk_tree_store_append (tstore, &iter, NULL);
+                       gtk_tree_store_set (tstore, &iter,
+                                           0, row + 1,
+                                           1, g_strdup_printf ("Ditta %d Srl", row + 1),
+                                           2, g_strdup_printf ("Via dei rossi, %d", row + 1),
+                                           -1);
+
+                       gtk_tree_store_append (tstore, &child, &iter);
+                       gtk_tree_store_set (tstore, &child,
+                                           3, g_strdup_printf ("%02d/%02d/%04d",
+                                                               g_random_int_range (1, 31),
+                                                               g_random_int_range (1, 12),
+                                                               g_random_int_range (1990, 2011)),
+                                           4, solipa_format_money ((row + 1) * g_random_double_range (1000, 100000), -1, FALSE),
+                                           -1);
+               }
+
+       solipa_gtktreemodel_to_csv (GTK_TREE_MODEL (tstore), "treemodel_tree.csv",
+                                   TRUE,
+                                   NULL, FALSE,
+                                   NULL,
+                                   TRUE,
+                                   columns_title,
+                                   columns,
+                                   5);
+
        return 0;
 }