#include <config.h>
#endif
+#include <errno.h>
+
#include <gio/gio.h>
#include <gtk/gtk.h>
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,
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)
{
{
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)
{
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);
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
solipa_gtkliststore_select_unselect_all ((GtkListStore *)user_data, FALSE);
}
-void
+guint
solipa_gtktreemodel_to_csv_gui (Solipa *solipa,
GtkWindow *parent,
GtkTreeModel *model,
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),
&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"));
/* 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);
else
{
gtk_widget_destroy (diag);
- return;
+ return -1;
}
} while (TRUE);
}
/* 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"))),
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
{
Solipa *solipa;
- GtkListStore *model;
+ GtkListStore *lstore;
+ GtkTreeStore *tstore;
gchar **columns_title;
guint *columns;
GtkTreeIter iter;
+ GtkTreeIter child;
guint row;
guint rows;
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");
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);
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,
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;
}