From b0dd4d29581ca1adbb1301c19757b865d57b960b Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Wed, 5 Dec 2012 11:47:49 +0100 Subject: [PATCH] Aggiunta la funzione SolipaAllegato::get_blob. --- data/libsolipa/gui/libsolipa.ui | 6 +- src/allegato.c | 100 ++++++++++++++++++++++++++++++-- src/allegato.h | 5 ++ 3 files changed, 104 insertions(+), 7 deletions(-) diff --git a/data/libsolipa/gui/libsolipa.ui b/data/libsolipa/gui/libsolipa.ui index d185fa8..cbce9d3 100644 --- a/data/libsolipa/gui/libsolipa.ui +++ b/data/libsolipa/gui/libsolipa.ui @@ -847,9 +847,6 @@ 2 5 5 - - - True @@ -1049,6 +1046,9 @@ GTK_FILL + + + diff --git a/src/allegato.c b/src/allegato.c index 76380f6..8479b48 100644 --- a/src/allegato.c +++ b/src/allegato.c @@ -254,6 +254,8 @@ solipa_allegato_apri_allegato (GtkWindow *transient, const gchar *filename) GError *error; GList *lfiles; + g_return_if_fail (filename != NULL); + gfile = g_file_new_for_commandline_arg (filename); if (gfile == NULL) { @@ -343,10 +345,100 @@ solipa_allegato_salva_in_db (GdaConnection *gda_con, const gchar *sql, return TRUE; } -void solipa_allegato_add_to_gtkform (SolipaAllegato *solipa_allegato, GtkForm *form, - const gchar *percorso_field, - const gchar *data_field, - const gchar *dimensione_field) +const gchar +*solipa_allegato_get_blob (GdaEx *gdaex, + const gchar *sql, + const gchar *percorso_field_name, + const gchar *blob_field_name) +{ + GdaDataModel *dm; + GError *error; + gint fin; + gchar *filename_tmp; + const GValue *value; + const gchar *percorso; + gchar *filename_orig; + const GdaBlob *blob; + gint i; + + gchar *ret; + + ret = NULL; + + dm = gdaex_query (gdaex, sql); + if (dm != NULL) + { + error = NULL; + value = gda_data_model_get_value_at (dm, + gda_data_model_get_column_index (dm, blob_field_name), + 0, &error); + if (!gda_value_is_null (value) && gda_value_isa (value, GDA_TYPE_BLOB)) + { + blob = gda_value_get_blob (value); + + filename_orig = g_strdup (""); + error = NULL; + value = gda_data_model_get_value_at (dm, + gda_data_model_get_column_index (dm, percorso_field_name), + 0, &error); + if (!gda_value_is_null (value)) + { + percorso = g_value_get_string (value); + filename_orig = g_path_get_basename (percorso); + } + + error = NULL; + fin = g_file_open_tmp (g_strdup_printf ("solipa-XXXXXX-%s", filename_orig), + &filename_tmp, &error); + if (fin > 0) + { + if (gda_blob_op_read_all (blob->op, blob)) + { + if (write (fin, blob->data.data, blob->data.binary_length) < 0) + { + g_warning ("Errore nella scrittura del file temporaneo."); + g_free (ret); + ret = NULL; + } + else + { + ret = g_strdup (filename_tmp); + } +#ifdef G_OS_WIN32 + _commit (fin); +#else + fsync (fin); +#endif + } + else + { + g_warning ("Errore nella lettura dell'allegato."); + g_free (ret); + ret = NULL; + } + + close (fin); + } + else + { + g_warning ("Errore nella lettura dell'allegato: %s.", error->message); + g_free (ret); + ret = NULL; + } + + g_free (filename_tmp); + g_free (filename_orig); + } + } + + return ret; +} + +void +solipa_allegato_add_to_gtkform (SolipaAllegato *solipa_allegato, GtkForm *form, + const gchar *percorso_field, + const gchar *data_field, + const gchar *dimensione_field) { SolipaAllegatoPrivate *priv; diff --git a/src/allegato.h b/src/allegato.h index 22ec0d5..51f9e56 100644 --- a/src/allegato.h +++ b/src/allegato.h @@ -67,6 +67,11 @@ guint solipa_allegato_get_dimensione (SolipaAllegato *solipa_allegato); gboolean solipa_allegato_get_salva_in_db (SolipaAllegato *solipa_allegato); void solipa_allegato_set_salva_in_db (SolipaAllegato *solipa_allegato, gboolean salva); +const gchar *solipa_allegato_get_blob (GdaEx *gdaex, + const gchar *sql, + const gchar *percorso_field_name, + const gchar *blob_field_name); + void solipa_allegato_add_to_gtkform (SolipaAllegato *solipa_allegato, GtkForm *form, const gchar *percorso_field, const gchar *data_field, -- 2.49.0