From ae777cecc2c0a377a653bd69d64fdb164cb226f0 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Wed, 8 Jun 2011 13:46:50 +0200 Subject: [PATCH] Aggiunto l'import delel vie da Civilia (da finire). Correzioni ai form. --- configure.ac | 1 + .../form/aggregazione_territoriale.form | 4 +- data/territorio/form/comune.form | 8 +- data/territorio/form/provincia.form | 8 +- data/territorio/form/regione.form | 8 +- data/territorio/form/stato.form | 4 +- .../form/tipo_aggregazione_territoriale.form | 4 +- data/territorio/form/via.form | 4 +- data/territorio/gui/territorio.gui | 9 +- src/Makefile.am | 2 + src/importaviecivilia.c | 243 ++++++++++++++++++ src/importaviecivilia.h | 61 +++++ src/main.c | 10 + 13 files changed, 355 insertions(+), 11 deletions(-) create mode 100644 src/importaviecivilia.c create mode 100644 src/importaviecivilia.h diff --git a/configure.ac b/configure.ac index 73bf331..19b7670 100644 --- a/configure.ac +++ b/configure.ac @@ -26,6 +26,7 @@ AC_PROG_RANLIB # Checks for libraries. PKG_CHECK_MODULES(TERRITORIO, [gio-2.0 >= 2.20 gtk+-2.0 >= 2.16 + libgda-ui-4.0 >= 4.2.0 libgdaex >= 0.3.0 libgtkform >= 0.1.3 libaute >= 0.0.2 diff --git a/data/territorio/form/aggregazione_territoriale.form b/data/territorio/form/aggregazione_territoriale.form index 9525285..823d6a1 100644 --- a/data/territorio/form/aggregazione_territoriale.form +++ b/data/territorio/form/aggregazione_territoriale.form @@ -21,7 +21,9 @@ - + + t + diff --git a/data/territorio/form/comune.form b/data/territorio/form/comune.form index 58012ce..ac6e092 100644 --- a/data/territorio/form/comune.form +++ b/data/territorio/form/comune.form @@ -11,11 +11,15 @@ - + + t + - + + t + diff --git a/data/territorio/form/provincia.form b/data/territorio/form/provincia.form index 6d64aff..d0a7e5c 100644 --- a/data/territorio/form/provincia.form +++ b/data/territorio/form/provincia.form @@ -11,11 +11,15 @@ - + + t + - + + t + diff --git a/data/territorio/form/regione.form b/data/territorio/form/regione.form index 934f803..247ebc0 100644 --- a/data/territorio/form/regione.form +++ b/data/territorio/form/regione.form @@ -11,11 +11,15 @@ - + + t + - + + t + diff --git a/data/territorio/form/stato.form b/data/territorio/form/stato.form index ea866e8..f9940bd 100644 --- a/data/territorio/form/stato.form +++ b/data/territorio/form/stato.form @@ -11,7 +11,9 @@ - + + t + diff --git a/data/territorio/form/tipo_aggregazione_territoriale.form b/data/territorio/form/tipo_aggregazione_territoriale.form index 86001de..4d08478 100644 --- a/data/territorio/form/tipo_aggregazione_territoriale.form +++ b/data/territorio/form/tipo_aggregazione_territoriale.form @@ -11,7 +11,9 @@ - + + t + diff --git a/data/territorio/form/via.form b/data/territorio/form/via.form index ca05d4e..85920a6 100644 --- a/data/territorio/form/via.form +++ b/data/territorio/form/via.form @@ -11,7 +11,9 @@ - + + t + diff --git a/data/territorio/gui/territorio.gui b/data/territorio/gui/territorio.gui index 96f9a37..ade36d4 100644 --- a/data/territorio/gui/territorio.gui +++ b/data/territorio/gui/territorio.gui @@ -156,6 +156,14 @@ + + + True + Vie da Ci_vilia + True + + + @@ -223,7 +231,6 @@ center-on-parent True normal - False Territorio 0.0.1 Andrea Zagli diff --git a/src/Makefile.am b/src/Makefile.am index 8e85020..f01272c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -26,6 +26,8 @@ territorio_SOURCES = \ importacomuniistat.h \ importaregioniprovinceistat.c \ importaregioniprovinceistat.h \ + importaviecivilia.c \ + importaviecivilia.h \ main.c \ province.c \ province.h \ diff --git a/src/importaviecivilia.c b/src/importaviecivilia.c new file mode 100644 index 0000000..d5d5bd4 --- /dev/null +++ b/src/importaviecivilia.c @@ -0,0 +1,243 @@ +/* + * Copyright (C) 2011 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 "importaviecivilia.h" + +static void territorio_importa_viecivilia_class_init (TerritorioImportaVieCiviliaClass *klass); +static void territorio_importa_viecivilia_init (TerritorioImportaVieCivilia *territorio_importa_viecivilia); + +static void territorio_importa_viecivilia_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void territorio_importa_viecivilia_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +#define TERRITORIO_IMPORTA_VIECIVILIA_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_TERRITORIO_IMPORTA_VIECIVILIA, TerritorioImportaVieCiviliaPrivate)) + +typedef struct _TerritorioImportaVieCiviliaPrivate TerritorioImportaVieCiviliaPrivate; +struct _TerritorioImportaVieCiviliaPrivate + { + TerritorioCommons *commons; + }; + +G_DEFINE_TYPE (TerritorioImportaVieCivilia, territorio_importa_viecivilia, G_TYPE_OBJECT) + +static void +territorio_importa_viecivilia_class_init (TerritorioImportaVieCiviliaClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (object_class, sizeof (TerritorioImportaVieCiviliaPrivate)); + + object_class->set_property = territorio_importa_viecivilia_set_property; + object_class->get_property = territorio_importa_viecivilia_get_property; +} + +static void +territorio_importa_viecivilia_init (TerritorioImportaVieCivilia *territorio_importa_viecivilia) +{ + TerritorioImportaVieCiviliaPrivate *priv = TERRITORIO_IMPORTA_VIECIVILIA_GET_PRIVATE (territorio_importa_viecivilia); +} + +/** + * territorio_importa_viecivilia_new: + * @commons: + * + * Returns: the newly created #TerritorioImportaVieCivilia object. + */ +TerritorioImportaVieCivilia +*territorio_importa_viecivilia_new (TerritorioCommons *commons) +{ + GtkWidget *dialog; + GtkWidget *carea; + GtkWidget *connection; + + GdaEx *gdaex; + + gchar *sql; + + GdaDataModel *dm; + GdaDataModelIter *iter; + + GdaDataModel *dm_territorio; + + const GdaDsnInfo *dsn; + + guint id_vie; + + TerritorioImportaVieCivilia *a = TERRITORIO_IMPORTA_VIECIVILIA (g_object_new (territorio_importa_viecivilia_get_type (), NULL)); + + TerritorioImportaVieCiviliaPrivate *priv = TERRITORIO_IMPORTA_VIECIVILIA_GET_PRIVATE (a); + + gdaui_init (); + + priv->commons = commons; + + GtkWindow *w_main = GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")); + + dialog = gtk_dialog_new_with_buttons ("Immettere i parametri di connessione al database Civilia dal quale importare", + w_main, + GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, + GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, + NULL); + gtk_dialog_set_has_separator (GTK_DIALOG (dialog), TRUE); + + carea = gtk_dialog_get_content_area (GTK_DIALOG (dialog)); + + connection = gdaui_login_new (NULL); + gdaui_login_set_mode (GDAUI_LOGIN (connection), GDA_UI_LOGIN_HIDE_DSN_SELECTION_MODE); + gtk_container_add (GTK_CONTAINER (carea), connection); + gtk_widget_show (connection); + + if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) + { + dsn = gdaui_login_get_connection_information (GDAUI_LOGIN (connection)); + gdaex = gdaex_new_from_string (g_strdup_printf ("%s://%s;%s", dsn->provider, dsn->cnc_string, dsn->auth_string)); + if (gdaex == NULL) + { + gtk_widget_destroy (dialog); + dialog = gtk_message_dialog_new (w_main, + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "Impossibile stabilire la connessione al database Civilia."); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + return NULL; + } + + sql = g_strdup_printf ("SELECT * FROM m1_tab_vie"); + dm = gdaex_query (gdaex, sql); + g_free (sql); + + if (dm != NULL) + { + iter = gda_data_model_create_iter (dm); + while (gda_data_model_iter_move_next (iter)) + { + /* controllo se esiste */ + sql = g_strdup_printf ("SELECT v.id" + " FROM vie AS v INNER JOIN comuni AS c ON v.id_comuni = c.id" + " INNER JOIN province AS p ON c.id_province = p.id" + " WHERE p.cod_istat || c.cod_istat = '%s'" + " AND v.nome = '%s'", + g_strdup_printf ("%06d", gdaex_data_model_iter_get_field_value_integer_at (iter, "M1_COMU_COD")), + gdaex_strescape (gdaex_data_model_iter_get_field_value_stringify_at (iter, "M1_VIA_DESC"), NULL)); + dm_territorio = gdaex_query (priv->commons->gdaex, sql); + if (dm_territorio == NULL || gda_data_model_get_n_rows (dm_territorio) < 1) + { + /* trovo un nuovo id */ + id_vie = 0; + sql = g_strdup_printf ("SELECT COALESCE (MAX (id), 0) FROM vie"); + dm_territorio = gdaex_query (priv->commons->gdaex, sql); + if (dm_territorio != NULL && gda_data_model_get_n_rows (dm_territorio) > 0) + { + id_vie = gdaex_data_model_get_value_integer_at (dm_territorio, 0, 0); + } + id_vie++; + + sql = g_strdup_printf ("INSERT INTO vie (id, nome, id_comuni, cap)" + " VALUES (%d, '%s'," + " (SELECT c.id FROM comuni AS c INNER JOIN province AS p ON c.id_province = p.id WHERE p.cod_istat || c.cod_istat = '%s')," + " '%s')", + id_vie, + gdaex_strescape (gdaex_data_model_iter_get_field_value_stringify_at (iter, "M1_VIA_DESC"), NULL), + g_strdup_printf ("%06d", gdaex_data_model_iter_get_field_value_integer_at (iter, "M1_COMU_COD")), + gdaex_strescape (gdaex_data_model_iter_get_field_value_stringify_at (iter, "M1_VIA_CAP"), NULL)); + if (gdaex_execute (priv->commons->gdaex, sql) == -1) + { + gtk_widget_destroy (dialog); + dialog = gtk_message_dialog_new (w_main, + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "Errore durante il salvataggio della via: %s.", + gdaex_data_model_iter_get_field_value_stringify_at (iter, "M1_VIA_DESC")); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + return NULL; + } + } + else + { + /* aggiorno la via */ + id_vie = gdaex_data_model_get_value_integer_at (dm_territorio, 0, 0); + } + } + } + else + { + gtk_widget_destroy (dialog); + dialog = gtk_message_dialog_new (w_main, + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "Impossibile leggere le vie dal database Civilia selezionato."); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + return NULL; + } + } + else + { + gtk_widget_destroy (dialog); + } + g_message ("Fine import."); + + return a; +} + +/* PRIVATE */ +static void +territorio_importa_viecivilia_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) +{ + TerritorioImportaVieCivilia *territorio_importa_viecivilia = TERRITORIO_IMPORTA_VIECIVILIA (object); + + TerritorioImportaVieCiviliaPrivate *priv = TERRITORIO_IMPORTA_VIECIVILIA_GET_PRIVATE (territorio_importa_viecivilia); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +territorio_importa_viecivilia_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) +{ + TerritorioImportaVieCivilia *territorio_importa_viecivilia = TERRITORIO_IMPORTA_VIECIVILIA (object); + + TerritorioImportaVieCiviliaPrivate *priv = TERRITORIO_IMPORTA_VIECIVILIA_GET_PRIVATE (territorio_importa_viecivilia); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +/* CALLBACK */ diff --git a/src/importaviecivilia.h b/src/importaviecivilia.h new file mode 100644 index 0000000..9accbfd --- /dev/null +++ b/src/importaviecivilia.h @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2011 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 __TERRITORIO_IMPORTA_VIECIVILIA_H__ +#define __TERRITORIO_IMPORTA_VIECIVILIA_H__ + +#include +#include + +#include + +#include "commons.h" + +G_BEGIN_DECLS + + +#define TYPE_TERRITORIO_IMPORTA_VIECIVILIA (territorio_importa_viecivilia_get_type ()) +#define TERRITORIO_IMPORTA_VIECIVILIA(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_TERRITORIO_IMPORTA_VIECIVILIA, TerritorioImportaVieCivilia)) +#define TERRITORIO_IMPORTA_VIECIVILIA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_TERRITORIO_IMPORTA_VIECIVILIA, TerritorioImportaVieCiviliaClass)) +#define IS_TERRITORIO_IMPORTA_VIECIVILIA(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_TERRITORIO_IMPORTA_VIECIVILIA)) +#define IS_TERRITORIO_IMPORTA_VIECIVILIA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_TERRITORIO_IMPORTA_VIECIVILIA)) +#define TERRITORIO_IMPORTA_VIECIVILIA_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_TERRITORIO_IMPORTA_VIECIVILIA, TerritorioImportaVieCiviliaClass)) + + +typedef struct _TerritorioImportaVieCivilia TerritorioImportaVieCivilia; +typedef struct _TerritorioImportaVieCiviliaClass TerritorioImportaVieCiviliaClass; + +struct _TerritorioImportaVieCivilia + { + GObject parent; + }; + +struct _TerritorioImportaVieCiviliaClass + { + GObjectClass parent_class; + }; + +GType territorio_importa_viecivilia_get_type (void) G_GNUC_CONST; + +TerritorioImportaVieCivilia *territorio_importa_viecivilia_new (TerritorioCommons *commons); + + +G_END_DECLS + +#endif /* __TERRITORIO_IMPORTA_VIECIVILIA_H__ */ diff --git a/src/main.c b/src/main.c index c26806a..0201359 100644 --- a/src/main.c +++ b/src/main.c @@ -34,6 +34,7 @@ #include "importacapcomuniitaliani.h" #include "importacomuniistat.h" #include "importaregioniprovinceistat.h" +#include "importaviecivilia.h" #include "province.h" #include "regioni.h" #include "stati.h" @@ -179,6 +180,15 @@ on_mnu_utilita_importa_capcomuniitaliani_activate (GtkMenuItem *menuitem, g_object_unref (m); } +G_MODULE_EXPORT void +on_mnu_utilita_importa_viecivilia_activate (GtkMenuItem *menuitem, + gpointer user_data) +{ + TerritorioImportaVieCivilia *m = territorio_importa_viecivilia_new (commons); + + g_object_unref (m); +} + G_MODULE_EXPORT void on_mnu_help_about_activate (GtkMenuItem *menuitem, gpointer user_data) -- 2.49.0