From: Andrea Zagli Date: Wed, 15 Dec 2010 16:37:13 +0000 (+0100) Subject: Aggiunta e testata la funzione di utilità solipa_gtktreemodel_to_csv. X-Git-Tag: Pre_EvolutionDataServer_2.32~17 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=89d894a55fd1867dccf5059cbbdbdd878c112f8e;p=solipa%2Flibsolipa Aggiunta e testata la funzione di utilità solipa_gtktreemodel_to_csv. --- diff --git a/.gitignore b/.gitignore index e86113a..d48b7cf 100644 --- a/.gitignore +++ b/.gitignore @@ -48,3 +48,5 @@ intltool-* Rules-quot *.exe tests/mail +tests/utils +*.csv diff --git a/configure.ac b/configure.ac index 6ca328c..67c870f 100644 --- a/configure.ac +++ b/configure.ac @@ -31,7 +31,8 @@ PKG_CHECK_MODULES(SOLIPA, [gobject-2.0 >= 2.24.0 glib-2.0 >= 2.24.0 libgdaex >= 0.2.2 camel-provider-1.2 >= 2.30 - gtk+-2.0 >= 2.20]) + gtk+-2.0 >= 2.20 + gio-2.0 >= 2.24]) AC_SUBST(SOLIPA_CFLAGS) AC_SUBST(SOLIPA_LIBS) diff --git a/src/Makefile.am b/src/Makefile.am index 46da5ff..21ac57e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -7,7 +7,8 @@ lib_LTLIBRARIES = libsolipa.la libsolipa_la_SOURCES = solipa.c \ allegato.c \ camelsession.c \ - mail.c + mail.c \ + utils.c libsolipa_la_LDFLAGS = -no-undefined @@ -15,6 +16,7 @@ libsolipa_include_HEADERS = libsolipa.h \ solipa.h \ allegato.h \ camelsession.h \ - mail.h + mail.h \ + utils.h libsolipa_includedir = $(includedir)/libsolipa diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..bc2d451 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,183 @@ +/* + * Copyright (C) 2010 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include + +#include "utils.h" + +void +solipa_gtktreemodel_to_csv (GtkTreeModel *model, const gchar *filename, + gboolean overwrite, + const gchar *quote, gboolean quote_all, + const gchar *separator, + gboolean fields_name_first_row, + gchar **columns_title, + guint *columns, + guint n_columns) +{ + gchar *_filename; + gchar *_quote; + gchar *_separator; + + GFile *fout; + GFileOutputStream *ostream; + GError *error; + + guint i; + + gchar *row; + + 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); + + _filename = g_strstrip (g_strdup (filename)); + g_return_if_fail (g_strcmp0 (_filename, "") != 0); + + if (quote != NULL) + { + _quote = g_strdup (quote); + } + else + { + _quote = g_strdup ("\""); + } + if (separator != NULL) + { + _separator = g_strdup (separator); + } + else + { + _separator = g_strdup (","); + } + + if (gtk_tree_model_get_iter_first (model, &iter)) + { + /* creo/sostituisco il file */ + fout = g_file_new_for_path (filename); + + error = NULL; + if (overwrite) + { + ostream = g_file_replace (fout, NULL, FALSE, + G_FILE_CREATE_NONE, + NULL, &error); + } + else + { + ostream = g_file_create (fout, + G_FILE_CREATE_NONE, + NULL, &error); + } + if (ostream == NULL) + { + g_warning ("Errore nella creazione del file di output: %s", + error != NULL && error->message != NULL ? error->message : "nessun dettaglio"); + return; + } + + if (fields_name_first_row) + { + /* nomi dei campi come priga riga */ + row = g_strdup (""); + for (i = 0; i < n_columns; i++) + { + row = g_strconcat (row, + (i > 0 ? _separator : ""), + _quote, columns_title[columns[i]], _quote, + NULL); + } + 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); + } + + /* cache dei GType delle colonne */ + gtypes = g_malloc0 (n_columns * sizeof (GType)); + for (i = 0; i < n_columns; i++) + { + 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)); + + 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"); + } + g_object_unref (ostream); + } +} + diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..1b952c1 --- /dev/null +++ b/src/utils.h @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2010 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __SOLIPA_UTILS_H__ +#define __SOLIPA_UTILS_H__ + +#include + +#include + + +G_BEGIN_DECLS + + +void solipa_gtktreemodel_to_csv (GtkTreeModel *model, const gchar *filename, + gboolean overwrite, + const gchar *quote, gboolean quote_all, + const gchar *separator, + gboolean fields_name_first_row, + gchar **columns_title, + guint *columns, + guint n_columns); + + +G_END_DECLS + + +#endif /* __SOLIPA_UTILS_H__ */ diff --git a/tests/Makefile.am b/tests/Makefile.am index 373e824..72b8782 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -9,4 +9,5 @@ LIBS = $(SOLIPA_LIBS) \ LDADD = $(top_builddir)/src/libsolipa.la -noinst_PROGRAMS = mail +noinst_PROGRAMS = mail \ + utils diff --git a/tests/utils.c b/tests/utils.c new file mode 100644 index 0000000..9b433df --- /dev/null +++ b/tests/utils.c @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2010 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include + +int +main (int argc, char *argv[]) +{ + GtkListStore *model; + + gchar **columns_title; + guint *columns; + + GtkTreeIter iter; + + guint row; + guint rows; + + gtk_init (&argc, &argv); + + columns_title = g_malloc0 (3 * sizeof (gchar)); + columns_title[0] = g_strdup ("ID"); + columns_title[1] = g_strdup ("Ragione Sociale"); + columns_title[2] = g_strdup ("Indirizzo"); + + columns = g_malloc0 (2 * sizeof (guint)); + columns[0] = 0; + columns[1] = 2; + + model = 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, + 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", + TRUE, + NULL, FALSE, + NULL, + TRUE, + columns_title, + columns, + 2); + + return 0; +}