From f22b216f57e78359d74335f6d1441cf07f6f3110 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Thu, 12 Aug 2010 13:47:31 +0200 Subject: [PATCH] Inizio gestione nodi. --- data/organigramma/gui/organigramma.gui | 66 +++- src/Makefile.am | 4 + src/main.c | 11 +- src/nodi.c | 491 +++++++++++++++++++++++++ src/nodi.h | 66 ++++ src/nodo.c | 338 +++++++++++++++++ src/nodo.h | 66 ++++ 7 files changed, 1019 insertions(+), 23 deletions(-) create mode 100644 src/nodi.c create mode 100644 src/nodi.h create mode 100644 src/nodo.c create mode 100644 src/nodo.h diff --git a/data/organigramma/gui/organigramma.gui b/data/organigramma/gui/organigramma.gui index e2578b6..c041d28 100644 --- a/data/organigramma/gui/organigramma.gui +++ b/data/organigramma/gui/organigramma.gui @@ -49,6 +49,7 @@ True _Organigramma True + @@ -183,7 +184,7 @@ True - 5 + 6 2 5 5 @@ -194,8 +195,8 @@ Tipo - 1 - 2 + 2 + 3 GTK_FILL GTK_FILL @@ -213,8 +214,8 @@ 1 2 - 1 - 2 + 2 + 3 GTK_FILL @@ -225,8 +226,8 @@ Nome - 2 - 3 + 3 + 4 GTK_FILL GTK_FILL @@ -241,8 +242,8 @@ 1 2 - 2 - 3 + 3 + 4 GTK_FILL @@ -254,8 +255,8 @@ Descrizione - 3 - 4 + 4 + 5 GTK_FILL GTK_FILL @@ -277,8 +278,8 @@ 1 2 - 3 - 4 + 4 + 5 @@ -288,6 +289,8 @@ Nodo superiore + 1 + 2 GTK_FILL GTK_FILL @@ -332,23 +335,39 @@ 1 2 + 1 + 2 GTK_FILL + 0 lbl_id - 4 - 5 - GTK_FILL + 1 + 2 GTK_FILL + + + + + + True + 0 + ID + + + GTK_FILL + GTK_FILL + + 0 @@ -411,6 +430,17 @@ 5 vertical 5 + + + True + <b>Nodi</b> + True + + + False + 0 + + True @@ -440,7 +470,7 @@ - 0 + 1 @@ -521,7 +551,7 @@ False - 1 + 2 diff --git a/src/Makefile.am b/src/Makefile.am index 051617b..3429f13 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -13,6 +13,10 @@ bin_PROGRAMS = organigramma organigramma_SOURCES = \ commons.h \ main.c \ + nodi.c \ + nodi.h \ + nodo.c \ + nodo.h \ tipinodo.c \ tipinodo.h \ tiponodo.c \ diff --git a/src/main.c b/src/main.c index 4a8b7bd..e362940 100644 --- a/src/main.c +++ b/src/main.c @@ -28,6 +28,7 @@ #include #include "commons.h" +#include "nodi.h" #include "tipinodo.h" static Commons *commons; @@ -51,18 +52,18 @@ main_set_vbx_body_child (GtkWidget *child) gtk_box_pack_start (GTK_BOX (vbx_body), vbx_body_child, TRUE, TRUE, 0); } -/*G_MODULE_EXPORT void -on_mnu_view_prenotazioni_activate (GtkMenuItem *menuitem, +G_MODULE_EXPORT void +on_mnu_view_nodi_activate (GtkMenuItem *menuitem, gpointer user_data) { GtkWidget *vbx; - Prenotazioni *m = prenotazioni_new (commons, FALSE); + Nodi *m = nodi_new (commons, FALSE); - vbx = prenotazioni_get_widget (m); + vbx = nodi_get_widget (m); main_set_vbx_body_child (vbx); -}*/ +} G_MODULE_EXPORT void on_mnu_tabelle_tipi_nodo_activate (GtkMenuItem *menuitem, diff --git a/src/nodi.c b/src/nodi.c new file mode 100644 index 0000000..c62ecca --- /dev/null +++ b/src/nodi.c @@ -0,0 +1,491 @@ +/* + * 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 "nodi.h" +#include "nodo.h" + +static void nodi_class_init (NodiClass *klass); +static void nodi_init (Nodi *nodi); + +static void nodi_carica (Nodi *nodi); +static void nodi_modifica (Nodi *nodi); + +static void nodi_selezionato (Nodi *nodi); +static void nodi_on_nodo_aggiornato (gpointer instance, gpointer user_data); + +static void nodi_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void nodi_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static void nodi_on_btn_nuovo_clicked (GtkButton *button, + gpointer user_data); +static void nodi_on_btn_modifica_clicked (GtkButton *button, + gpointer user_data); +static void nodi_on_btn_elimina_clicked (GtkButton *button, + gpointer user_data); +static void nodi_on_trv_nodi_row_activated (GtkTreeView *tree_view, + GtkTreePath *tree_path, + GtkTreeViewColumn *column, + gpointer user_data); + +static void nodi_on_btn_ricerca_clicked (GtkButton *button, + gpointer user_data); + +static void nodi_on_btn_annulla_clicked (GtkButton *button, + gpointer user_data); +static void nodi_on_btn_seleziona_clicked (GtkButton *button, + gpointer user_data); + +#define NODI_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_NODI, NodiPrivate)) + +enum +{ + COL_ID, + COL_NOME +}; + +typedef struct _NodiPrivate NodiPrivate; +struct _NodiPrivate + { + Commons *commons; + + GtkWidget *widget; + GtkTreeSelection *selection; + GtkTreeStore *tstore_nodi; + + gboolean selezione; + GtkWindow *wtransient; + }; + +G_DEFINE_TYPE (Nodi, nodi, G_TYPE_OBJECT) + +static void +nodi_class_init (NodiClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (object_class, sizeof (NodiPrivate)); + + object_class->set_property = nodi_set_property; + object_class->get_property = nodi_get_property; + + /** + * Nodi::selezionato: + * @nodi: + * + */ + klass->selezionato_signal_id = g_signal_new ("selezionato", + G_TYPE_FROM_CLASS (object_class), + G_SIGNAL_RUN_LAST, + 0, + NULL, + NULL, + g_cclosure_marshal_VOID__UINT, + G_TYPE_NONE, + 1, G_TYPE_UINT); +} + +static void +nodi_init (Nodi *nodi) +{ + NodiPrivate *priv = NODI_GET_PRIVATE (nodi); +} + +/** + * nodi_new: + * @commons: + * @selezione: + * + * Returns: the newly created #Nodi object. + */ +Nodi +*nodi_new (Commons *commons, gboolean selezione) +{ + GError *error; + + Nodi *a = NODI (g_object_new (nodi_get_type (), NULL)); + + NodiPrivate *priv = NODI_GET_PRIVATE (a); + + priv->commons = commons; + priv->selezione = selezione; + + error = NULL; + gtk_builder_add_objects_from_file (priv->commons->gtkbuilder, priv->commons->guifile, + g_strsplit (g_strconcat ("tstore_nodi|", + (selezione ? "w_nodi" : "vbx_nodi"), + NULL), "|", -1), + &error); + if (error != NULL) + { + g_warning ("Errore: %s.", error->message); + return NULL; + } + + priv->widget = GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, (selezione ? "w_nodi" : "vbx_nodi"))); + priv->selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (gtk_builder_get_object (priv->commons->gtkbuilder, "treeview1"))); + priv->tstore_nodi = GTK_TREE_STORE (gtk_builder_get_object (priv->commons->gtkbuilder, "tstore_nodi")); + + if (priv->selezione) + { + priv->wtransient = GTK_WINDOW (priv->widget); + } + else + { + priv->wtransient = GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")); + } + + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button1"), + "clicked", G_CALLBACK (nodi_on_btn_nuovo_clicked), (gpointer)a); + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button2"), + "clicked", G_CALLBACK (nodi_on_btn_modifica_clicked), (gpointer)a); + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button3"), + "clicked", G_CALLBACK (nodi_on_btn_elimina_clicked), (gpointer)a); + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "treeview1"), + "row-activated", G_CALLBACK (nodi_on_trv_nodi_row_activated), (gpointer)a); + + if (priv->selezione) + { + gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, "label1"))); + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button14"), + "clicked", G_CALLBACK (nodi_on_btn_annulla_clicked), (gpointer)a); + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button15"), + "clicked", G_CALLBACK (nodi_on_btn_seleziona_clicked), (gpointer)a); + } + else + { + gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, "button14"))); + gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, "button15"))); + } + + nodi_carica (a); + + return a; +} + +/** + * nodi_get_widget: + * @nodi: + * + */ +GtkWidget +*nodi_get_widget (Nodi *nodi) +{ + NodiPrivate *priv = NODI_GET_PRIVATE (nodi); + + return priv->widget; +} + +/* PRIVATE */ +static void +nodi_carica (Nodi *nodi) +{ + GtkTreeIter iter; + GdaDataModel *dm; + + gchar *sql_where; + gchar *sql; + + gchar *gcval; + gint ival; + + gint rows; + gint row; + + NodiPrivate *priv = NODI_GET_PRIVATE (nodi); + + gtk_tree_store_clear (priv->tstore_nodi); + + sql_where = g_strdup (""); + + /*if (priv->parametri_ricerca != NULL) + { + gcval = (gchar *)g_value_get_string ((GValue *)g_hash_table_lookup (priv->parametri_ricerca, "ragione_sociale")); + if (gcval != NULL && g_strcmp0 (g_strstrip (gcval), "") != 0) + { + sql_where = g_strconcat (sql_where, " AND ragione_sociale ILIKE '%%", + gdaex_strescape (gcval, NULL), + "%%'", + NULL); + } + gcval = (gchar *)g_value_get_string ((GValue *)g_hash_table_lookup (priv->parametri_ricerca, "codice_fiscale")); + if (gcval != NULL && g_strcmp0 (g_strstrip (gcval), "") != 0) + { + sql_where = g_strconcat (sql_where, " AND codice_fiscale = '", + gdaex_strescape (gcval, NULL), + "'", + NULL); + } + gcval = (gchar *)g_value_get_string ((GValue *)g_hash_table_lookup (priv->parametri_ricerca, "partita_iva")); + if (gcval != NULL && g_strcmp0 (g_strstrip (gcval), "") != 0) + { + sql_where = g_strconcat (sql_where, " AND partita_iva = '", + gdaex_strescape (gcval, NULL), + "'", + NULL); + } + ival = g_value_get_int ((GValue *)g_hash_table_lookup (priv->parametri_ricerca, "id_citta")); + if (ival != 0) + { + sql_where = g_strconcat (sql_where, " AND id_citta = ", + g_strdup_printf ("%d", ival), + NULL); + } + }*/ + + sql = g_strdup_printf ("SELECT c.id, c.nome" + " FROM nodi c" + " WHERE c.status <> 'E'" + "%s" + " ORDER BY c.nome", sql_where); + dm = gdaex_query (priv->commons->gdaex, sql); + if (dm != NULL) + { + rows = gda_data_model_get_n_rows (dm); + for (row = 0; row < rows; row++) + { + gtk_tree_store_append (priv->tstore_nodi, &iter, NULL); + + gtk_tree_store_set (priv->tstore_nodi, &iter, + COL_ID, gdaex_data_model_get_field_value_stringify_at (dm, row, "id"), + COL_NOME, gdaex_data_model_get_field_value_stringify_at (dm, row, "nome"), + -1); + } + + g_object_unref (dm); + } +} + +static void +nodi_modifica (Nodi *nodi) +{ + GtkTreeIter iter; + guint id; + + NodiPrivate *priv = NODI_GET_PRIVATE (nodi); + + if (gtk_tree_selection_get_selected (priv->selection, NULL, &iter)) + { + GtkWidget *w; + + gtk_tree_model_get (GTK_TREE_MODEL (priv->tstore_nodi), &iter, + COL_ID, &id, + -1); + + Nodo *c = nodo_new (priv->commons, id); + + g_signal_connect (G_OBJECT (c), "aggiornato", + G_CALLBACK (nodi_on_nodo_aggiornato), (gpointer)nodi); + + w = nodo_get_widget (c); + gtk_window_set_transient_for (GTK_WINDOW (w), priv->wtransient); + gtk_widget_show (w); + } + else + { + GtkWidget *dialog = gtk_message_dialog_new (priv->wtransient, + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "Occorre prima selezionare un tipo nodo"); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + } +} + +static void +nodi_selezionato (Nodi *nodi) +{ + GtkTreeIter iter; + guint id; + + NodiClass *klass = NODI_GET_CLASS (nodi); + NodiPrivate *priv = NODI_GET_PRIVATE (nodi); + + if (gtk_tree_selection_get_selected (priv->selection, NULL, &iter)) + { + gtk_tree_model_get (GTK_TREE_MODEL (priv->tstore_nodi), &iter, + COL_ID, &id, + -1); + + g_signal_emit (G_OBJECT (nodi), klass->selezionato_signal_id, 0, id); + + gtk_widget_destroy (priv->widget); + g_object_unref (G_OBJECT (nodi)); + } + else + { + GtkWidget *dialog = gtk_message_dialog_new (priv->wtransient, + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "Occorre prima selezionare un tipo nodo"); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + } +} + +static void +nodi_on_nodo_aggiornato (gpointer instance, gpointer user_data) +{ + nodi_carica ((Nodi *)user_data); +} + +static void +nodi_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) +{ + Nodi *nodi = NODI (object); + + NodiPrivate *priv = NODI_GET_PRIVATE (nodi); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +nodi_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) +{ + Nodi *nodi = NODI (object); + + NodiPrivate *priv = NODI_GET_PRIVATE (nodi); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +/* CALLBACK */ +static void +nodi_on_btn_nuovo_clicked (GtkButton *button, + gpointer user_data) +{ + GtkWidget *w; + + Nodi *nodi = (Nodi *)user_data; + NodiPrivate *priv = NODI_GET_PRIVATE (nodi); + + Nodo *c = nodo_new (priv->commons, 0); + + g_signal_connect (G_OBJECT (c), "aggiornato", + G_CALLBACK (nodi_on_nodo_aggiornato), (gpointer)nodi); + + w = nodo_get_widget (c); + gtk_window_set_transient_for (GTK_WINDOW (w), priv->wtransient); + gtk_widget_show (w); +} + +static void +nodi_on_btn_modifica_clicked (GtkButton *button, + gpointer user_data) +{ + nodi_modifica ((Nodi *)user_data); +} + +static void +nodi_on_btn_elimina_clicked (GtkButton *button, + gpointer user_data) +{ + GtkWidget *dialog; + gboolean risp; + + GtkTreeIter iter; + guint id; + + Nodi *nodi = (Nodi *)user_data; + NodiPrivate *priv = NODI_GET_PRIVATE (nodi); + + if (gtk_tree_selection_get_selected (priv->selection, NULL, &iter)) + { + dialog = gtk_message_dialog_new (priv->wtransient, + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_QUESTION, + GTK_BUTTONS_YES_NO, + "Sicuro di voler eliminare il tipo nodo selezionato?"); + risp = gtk_dialog_run (GTK_DIALOG (dialog)); + if (risp == GTK_RESPONSE_YES) + { + gtk_tree_model_get (GTK_TREE_MODEL (priv->tstore_nodi), &iter, + COL_ID, &id, + -1); + + gdaex_execute (priv->commons->gdaex, + g_strdup_printf ("UPDATE nodi SET status = 'E' WHERE id = %d", id)); + + nodi_carica (nodi); + } + gtk_widget_destroy (dialog); + } + else + { + dialog = gtk_message_dialog_new (priv->wtransient, + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "Occorre prima selezionare un tipo nodo"); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + } +} + +static void +nodi_on_trv_nodi_row_activated (GtkTreeView *tree_view, + GtkTreePath *tree_path, + GtkTreeViewColumn *column, + gpointer user_data) +{ + NodiPrivate *priv = NODI_GET_PRIVATE ((Nodi *)user_data); + + if (priv->selezione) + { + nodi_selezionato ((Nodi *)user_data); + } + else + { + nodi_modifica ((Nodi *)user_data); + } +} + +static void +nodi_on_btn_annulla_clicked (GtkButton *button, + gpointer user_data) +{ + NodiPrivate *priv = NODI_GET_PRIVATE ((Nodi *)user_data); + + gtk_widget_destroy (priv->widget); + g_object_unref (G_OBJECT (user_data)); +} + +static void +nodi_on_btn_seleziona_clicked (GtkButton *button, + gpointer user_data) +{ + nodi_selezionato ((Nodi *)user_data); +} diff --git a/src/nodi.h b/src/nodi.h new file mode 100644 index 0000000..3e06507 --- /dev/null +++ b/src/nodi.h @@ -0,0 +1,66 @@ +/* + * 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 __NODI_H__ +#define __NODI_H__ + +#include +#include + +#include +#include + +#include "commons.h" + +G_BEGIN_DECLS + + +#define TYPE_NODI (nodi_get_type ()) +#define NODI(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_NODI, Nodi)) +#define NODI_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_NODI, NodiClass)) +#define IS_NODI(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_NODI)) +#define IS_NODI_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_NODI)) +#define NODI_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_NODI, NodiClass)) + + +typedef struct _Nodi Nodi; +typedef struct _NodiClass NodiClass; + +struct _Nodi + { + GObject parent; + }; + +struct _NodiClass + { + GObjectClass parent_class; + + guint selezionato_signal_id; + }; + +GType nodi_get_type (void) G_GNUC_CONST; + +Nodi *nodi_new (Commons *commons, gboolean selezione); + +GtkWidget *nodi_get_widget (Nodi *nodi); + + +G_END_DECLS + +#endif /* __NODI_H__ */ diff --git a/src/nodo.c b/src/nodo.c new file mode 100644 index 0000000..d3b65a2 --- /dev/null +++ b/src/nodo.c @@ -0,0 +1,338 @@ +/* + * 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 "nodo.h" + +static void nodo_class_init (NodoClass *klass); +static void nodo_init (Nodo *nodo); + +static void nodo_carica (Nodo *nodo); +static void nodo_salva (Nodo *nodo); + +static gboolean nodo_conferma_chiusura (Nodo *nodo); + +static void nodo_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void nodo_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static gboolean nodo_on_w_nodo_delete_event (GtkWidget *widget, + GdkEvent *event, + gpointer user_data); + +static void nodo_on_btn_annulla_clicked (GtkButton *button, + gpointer user_data); +static void nodo_on_btn_salva_clicked (GtkButton *button, + gpointer user_data); + +#define NODO_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_NODO, NodoPrivate)) + +enum +{ + LBL_ID +}; + +typedef struct _NodoPrivate NodoPrivate; +struct _NodoPrivate + { + Commons *commons; + + GtkForm *form; + + GtkWidget *w; + + GObject **objects; + + gint id; + }; + +G_DEFINE_TYPE (Nodo, nodo, G_TYPE_OBJECT) + +static void +nodo_class_init (NodoClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (object_class, sizeof (NodoPrivate)); + + object_class->set_property = nodo_set_property; + object_class->get_property = nodo_get_property; + + /** + * Nodo::aggiornato: + * @nodo: + * + */ + klass->aggiornato_signal_id = g_signal_new ("aggiornato", + G_TYPE_FROM_CLASS (object_class), + G_SIGNAL_RUN_LAST, + 0, + NULL, + NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, + 0); +} + +static void +nodo_init (Nodo *nodo) +{ + NodoPrivate *priv = NODO_GET_PRIVATE (nodo); +} + +/** + * nodo_new: + * @commons: + * @id: + * + * Returns: the newly created #Nodo object. + */ +Nodo +*nodo_new (Commons *commons, gint id) +{ + GError *error; + + Nodo *a = NODO (g_object_new (nodo_get_type (), NULL)); + + NodoPrivate *priv = NODO_GET_PRIVATE (a); + + priv->commons = commons; + + error = NULL; + gtk_builder_add_objects_from_file (priv->commons->gtkbuilder, priv->commons->guifile, + g_strsplit ("w_nodo", "|", -1), + &error); + if (error != NULL) + { + g_warning ("Errore: %s.", error->message); + return NULL; + } + + priv->form = gtk_form_new_from_file (g_build_filename (priv->commons->formdir, "nodo.form", NULL), priv->commons->gtkbuilder); + + g_object_set (priv->form, "gdaex", priv->commons->gdaex, NULL); + + priv->w = GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, "w_nodo")); + + priv->objects = gtk_form_get_objects_by_name (priv->form, + "label6", + NULL); + + g_signal_connect (priv->w, + "delete-event", G_CALLBACK (nodo_on_w_nodo_delete_event), (gpointer *)a); + + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button4"), + "clicked", G_CALLBACK (nodo_on_btn_annulla_clicked), (gpointer *)a); + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button5"), + "clicked", G_CALLBACK (nodo_on_btn_salva_clicked), (gpointer *)a); + + priv->id = id; + + if (priv->id == 0) + { + gtk_label_set_text (GTK_LABEL (priv->objects[LBL_ID]), ""); + + gtk_form_set_as_origin (priv->form); + } + else + { + gtk_label_set_text (GTK_LABEL (priv->objects[LBL_ID]), g_strdup_printf ("%d", priv->id)); + nodo_carica (a); + } + + return a; +} + +/** + * nodo_get_widget: + * @nodo: + * + */ +GtkWidget +*nodo_get_widget (Nodo *nodo) +{ + NodoPrivate *priv = NODO_GET_PRIVATE (nodo); + + return priv->w; +} + +/* PRIVATE */ +static void +nodo_carica (Nodo *nodo) +{ + NodoPrivate *priv = NODO_GET_PRIVATE (nodo); + + if (gtk_form_fill_from_table (priv->form)) + { + } +} + +static void +nodo_salva (Nodo *nodo) +{ + GError *error = NULL; + gchar *sql; + GtkWidget *dialog; + + GDate *da; + GDate *a; + + NodoClass *klass = NODO_GET_CLASS (nodo); + + NodoPrivate *priv = NODO_GET_PRIVATE (nodo); + + if (!gtk_form_check (priv->form, (priv->id != 0), NULL, TRUE, priv->w, TRUE)) + { + return; + } + + if (priv->id == 0) + { + sql = gtk_form_get_sql (priv->form, GTK_FORM_SQL_INSERT); + } + else + { + sql = gtk_form_get_sql (priv->form, GTK_FORM_SQL_UPDATE); + } + + if (gdaex_execute (priv->commons->gdaex, sql) == 1) + { + g_signal_emit (nodo, klass->aggiornato_signal_id, 0); + + gtk_form_set_as_origin (priv->form); + + if (priv->id == 0) + { + priv->id = strtol (gtk_label_get_text (GTK_LABEL (priv->objects[LBL_ID])), NULL, 10); + } + + dialog = gtk_message_dialog_new (GTK_WINDOW (priv->w), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_INFO, + GTK_BUTTONS_OK, + "Salvataggio eseguito con successo."); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + } + else + { + if (priv->id == 0) + { + gtk_label_set_text (GTK_LABEL (priv->objects[LBL_ID]), ""); + } + + dialog = gtk_message_dialog_new (GTK_WINDOW (priv->w), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "Errore durante il salvataggio."); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + } +} + +static void +nodo_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) +{ + Nodo *nodo = NODO (object); + + NodoPrivate *priv = NODO_GET_PRIVATE (nodo); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +nodo_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) +{ + Nodo *nodo = NODO (object); + + NodoPrivate *priv = NODO_GET_PRIVATE (nodo); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static gboolean +nodo_conferma_chiusura (Nodo *nodo) +{ + GtkWidget *dialog; + + gboolean ret; + + NodoPrivate *priv = NODO_GET_PRIVATE (nodo); + + ret = TRUE; + if (gtk_form_is_changed (priv->form)) + { + dialog = gtk_message_dialog_new (GTK_WINDOW (priv->w), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_QUESTION, + GTK_BUTTONS_YES_NO, + "Sicuro di voler chiudere senza salvare?"); + if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_NO) + { + ret = FALSE; + } + gtk_widget_destroy (dialog); + } + + return ret; +} + +/* CALLBACK */ +static gboolean +nodo_on_w_nodo_delete_event (GtkWidget *widget, + GdkEvent *event, + gpointer user_data) +{ + return !nodo_conferma_chiusura ((Nodo *)user_data); +} + +static void +nodo_on_btn_annulla_clicked (GtkButton *button, + gpointer user_data) +{ + Nodo *nodo = (Nodo *)user_data; + + NodoPrivate *priv = NODO_GET_PRIVATE (nodo); + + if (nodo_conferma_chiusura (nodo)) gtk_widget_destroy (priv->w); +} + +static void +nodo_on_btn_salva_clicked (GtkButton *button, + gpointer user_data) +{ + nodo_salva ((Nodo *)user_data); +} diff --git a/src/nodo.h b/src/nodo.h new file mode 100644 index 0000000..c0bc04d --- /dev/null +++ b/src/nodo.h @@ -0,0 +1,66 @@ +/* + * 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 __NODO_H__ +#define __NODO_H__ + +#include +#include + +#include +#include + +#include "commons.h" + +G_BEGIN_DECLS + + +#define TYPE_NODO (nodo_get_type ()) +#define NODO(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_NODO, Nodo)) +#define NODO_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_NODO, NodoClass)) +#define IS_NODO(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_NODO)) +#define IS_NODO_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_NODO)) +#define NODO_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_NODO, NodoClass)) + + +typedef struct _Nodo Nodo; +typedef struct _NodoClass NodoClass; + +struct _Nodo + { + GObject parent; + }; + +struct _NodoClass + { + GObjectClass parent_class; + + guint aggiornato_signal_id; + }; + +GType nodo_get_type (void) G_GNUC_CONST; + +Nodo *nodo_new (Commons *commons, gint id); + +GtkWidget *nodo_get_widget (Nodo *nodo); + + +G_END_DECLS + +#endif /* __NODO_H__ */ -- 2.49.0