--- /dev/null
+/*
+ * Copyright (C) 2011 Andrea Zagli <azagli@libero.it>
+ *
+ * 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 <libgda-ui/libgda-ui.h>
+
+#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 */
--- /dev/null
+/*
+ * Copyright (C) 2011 Andrea Zagli <azagli@libero.it>
+ *
+ * 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 <glib.h>
+#include <glib-object.h>
+
+#include <gtk/gtk.h>
+
+#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__ */