From: Andrea Zagli Date: Tue, 24 Aug 2010 11:08:35 +0000 (+0200) Subject: Gestione stati. X-Git-Tag: 0.0.1~7 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=4b8df8331fea2d550bba93adfc1dc7a263869777;p=solipa%2Fterritorio Gestione stati. --- diff --git a/data/territorio/form/Makefile.am b/data/territorio/form/Makefile.am index e8bfb71..68358ca 100644 --- a/data/territorio/form/Makefile.am +++ b/data/territorio/form/Makefile.am @@ -1,5 +1,6 @@ formdir = $(datadir)/$(PACKAGE)/form -form_DATA = +form_DATA = \ + stato.form EXTRA_DIST = $(form_DATA) diff --git a/data/territorio/form/stato.form b/data/territorio/form/stato.form new file mode 100644 index 0000000..ea866e8 --- /dev/null +++ b/data/territorio/form/stato.form @@ -0,0 +1,33 @@ + + + + stati
+ + + + t + t + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/data/territorio/gui/territorio.gui b/data/territorio/gui/territorio.gui index 3268556..2bd1907 100644 --- a/data/territorio/gui/territorio.gui +++ b/data/territorio/gui/territorio.gui @@ -7,6 +7,7 @@ center 600 400 + True @@ -28,6 +29,7 @@ True True True + @@ -47,6 +49,7 @@ True _Stati True + @@ -101,7 +104,16 @@ - + + True + vertical + + + + + + 1 + @@ -185,6 +197,18 @@ True True + lstore_stati + + + Nome + + + + 1 + + + + @@ -276,6 +300,14 @@ + + + + + + + + Stato - Territorio True diff --git a/docs/territorio/examples/territorio.conf b/docs/territorio/examples/territorio.conf index 91d9111..35b1960 100644 --- a/docs/territorio/examples/territorio.conf +++ b/docs/territorio/examples/territorio.conf @@ -1,6 +1,6 @@ [AUTE] plugin=/usr/local/lib/libaute/plugins/libaute-db -cnc_string=PostgreSQL://postgres:postgres@HOST=localhost;PORT=5432;DB_NAME=autedb +cnc_string=PostgreSQL://postgres:postgres@HOST=localhost;PORT=5432;DB_NAME=aute [DB] -cnc_string=PostgreSQL://postgres:postgres@HOST=localhost;PORT=5432;DB_NAME=organigramma +cnc_string=PostgreSQL://postgres:postgres@HOST=localhost;PORT=5432;DB_NAME=territorio diff --git a/src/Makefile.am b/src/Makefile.am index 022718f..5e62358 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -12,4 +12,8 @@ bin_PROGRAMS = territorio territorio_SOURCES = \ commons.h \ - main.c \ No newline at end of file + main.c \ + stati.c \ + stati.h \ + stato.c \ + stato.h \ No newline at end of file diff --git a/src/main.c b/src/main.c index 72696ef..fc489dc 100644 --- a/src/main.c +++ b/src/main.c @@ -28,6 +28,7 @@ #include #include "commons.h" +#include "stati.h" static Commons *commons; @@ -50,18 +51,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_nodi_activate (GtkMenuItem *menuitem, +G_MODULE_EXPORT void +on_mnu_view_stati_activate (GtkMenuItem *menuitem, gpointer user_data) { GtkWidget *vbx; - Nodi *m = nodi_new (commons, FALSE); + Stati *m = stati_new (commons, FALSE); - vbx = nodi_get_widget (m); + vbx = stati_get_widget (m); main_set_vbx_body_child (vbx); -}*/ +} G_MODULE_EXPORT void on_mnu_help_about_activate (GtkMenuItem *menuitem, @@ -191,7 +192,7 @@ main (int argc, char *argv[]) #endif - commons->guifile = g_build_filename (commons->guidir, "organigramma.gui", NULL); + commons->guifile = g_build_filename (commons->guidir, "territorio.gui", NULL); /* leggo la stringa di connessione al db */ error = NULL; diff --git a/src/stati.c b/src/stati.c new file mode 100644 index 0000000..1c27fb7 --- /dev/null +++ b/src/stati.c @@ -0,0 +1,445 @@ +/* + * 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 "stati.h" +#include "stato.h" + +static void stati_class_init (StatiClass *klass); +static void stati_init (Stati *stati); + +static void stati_carica (Stati *stati); +static void stati_modifica (Stati *stati); + +static void stati_selezionato (Stati *stati); +static void stati_on_stato_aggiornato (gpointer instance, gpointer user_data); + +static void stati_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void stati_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static void stati_on_btn_nuovo_clicked (GtkButton *button, + gpointer user_data); +static void stati_on_btn_modifica_clicked (GtkButton *button, + gpointer user_data); +static void stati_on_btn_elimina_clicked (GtkButton *button, + gpointer user_data); +static void stati_on_trv_stati_row_activated (GtkTreeView *tree_view, + GtkTreePath *tree_path, + GtkTreeViewColumn *column, + gpointer user_data); + +static void stati_on_btn_annulla_clicked (GtkButton *button, + gpointer user_data); +static void stati_on_btn_seleziona_clicked (GtkButton *button, + gpointer user_data); + +#define STATI_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_STATI, StatiPrivate)) + +enum +{ + COL_ID, + COL_NOME +}; + +typedef struct _StatiPrivate StatiPrivate; +struct _StatiPrivate + { + Commons *commons; + + GtkWidget *widget; + GtkTreeSelection *selection; + GtkListStore *lstore_stati; + + gboolean selezione; + GtkWindow *wtransient; + }; + +G_DEFINE_TYPE (Stati, stati, G_TYPE_OBJECT) + +static void +stati_class_init (StatiClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (object_class, sizeof (StatiPrivate)); + + object_class->set_property = stati_set_property; + object_class->get_property = stati_get_property; + + /** + * Stati::selezionato: + * @stati: + * + */ + 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 +stati_init (Stati *stati) +{ + StatiPrivate *priv = STATI_GET_PRIVATE (stati); +} + +/** + * stati_new: + * @commons: + * @selezione: + * + * Returns: the newly created #Stati object. + */ +Stati +*stati_new (Commons *commons, gboolean selezione) +{ + GError *error; + + Stati *a = STATI (g_object_new (stati_get_type (), NULL)); + + StatiPrivate *priv = STATI_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 ("lstore_stati|", + (selezione ? "w_stati" : "vbox2"), + 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_stati" : "vbox2"))); + priv->selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (gtk_builder_get_object (priv->commons->gtkbuilder, "treeview1"))); + priv->lstore_stati = GTK_LIST_STORE (gtk_builder_get_object (priv->commons->gtkbuilder, "lstore_stati")); + + 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 (stati_on_btn_nuovo_clicked), (gpointer)a); + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button2"), + "clicked", G_CALLBACK (stati_on_btn_modifica_clicked), (gpointer)a); + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button3"), + "clicked", G_CALLBACK (stati_on_btn_elimina_clicked), (gpointer)a); + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "treeview1"), + "row-activated", G_CALLBACK (stati_on_trv_stati_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, "button4"), + "clicked", G_CALLBACK (stati_on_btn_annulla_clicked), (gpointer)a); + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button5"), + "clicked", G_CALLBACK (stati_on_btn_seleziona_clicked), (gpointer)a); + } + else + { + gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, "button4"))); + gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, "button5"))); + } + + stati_carica (a); + + return a; +} + +/** + * stati_get_widget: + * @stati: + * + */ +GtkWidget +*stati_get_widget (Stati *stati) +{ + StatiPrivate *priv = STATI_GET_PRIVATE (stati); + + return priv->widget; +} + +/* PRIVATE */ +static void +stati_carica (Stati *stati) +{ + GtkTreeIter iter; + GdaDataModel *dm; + + gint rows; + gint row; + + StatiPrivate *priv = STATI_GET_PRIVATE (stati); + + gtk_list_store_clear (priv->lstore_stati); + + dm = gdaex_query (priv->commons->gdaex, + "SELECT c.id, c.nome" + " FROM stati AS c" + " WHERE c.status <> 'E'" + " ORDER BY c.nome"); + if (dm != NULL) + { + rows = gda_data_model_get_n_rows (dm); + for (row = 0; row < rows; row++) + { + gtk_list_store_append (priv->lstore_stati, &iter); + + gtk_list_store_set (priv->lstore_stati, &iter, + COL_ID, gdaex_data_model_get_field_value_integer_at (dm, row, "id"), + COL_NOME, gdaex_data_model_get_field_value_stringify_at (dm, row, "nome"), + -1); + } + + g_object_unref (dm); + } +} + +static void +stati_modifica (Stati *stati) +{ + GtkTreeIter iter; + guint id; + + StatiPrivate *priv = STATI_GET_PRIVATE (stati); + + if (gtk_tree_selection_get_selected (priv->selection, NULL, &iter)) + { + GtkWidget *w; + + gtk_tree_model_get (GTK_TREE_MODEL (priv->lstore_stati), &iter, + COL_ID, &id, + -1); + + Stato *c = stato_new (priv->commons, id); + + g_signal_connect (G_OBJECT (c), "aggiornato", + G_CALLBACK (stati_on_stato_aggiornato), (gpointer)stati); + + w = stato_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 uno stato"); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + } +} + +static void +stati_selezionato (Stati *stati) +{ + GtkTreeIter iter; + guint *id; + + StatiClass *klass = STATI_GET_CLASS (stati); + StatiPrivate *priv = STATI_GET_PRIVATE (stati); + + if (gtk_tree_selection_get_selected (priv->selection, NULL, &iter)) + { + gtk_tree_model_get (GTK_TREE_MODEL (priv->lstore_stati), &iter, + COL_ID, &id, + -1); + + g_signal_emit (G_OBJECT (stati), klass->selezionato_signal_id, 0, id); + + gtk_widget_destroy (priv->widget); + g_object_unref (G_OBJECT (stati)); + } + else + { + GtkWidget *dialog = gtk_message_dialog_new (priv->wtransient, + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "Occorre prima selezionare uno stato"); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + } +} + +static void +stati_on_stato_aggiornato (gpointer instance, gpointer user_data) +{ + stati_carica ((Stati *)user_data); +} + +static void +stati_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) +{ + Stati *stati = STATI (object); + + StatiPrivate *priv = STATI_GET_PRIVATE (stati); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +stati_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) +{ + Stati *stati = STATI (object); + + StatiPrivate *priv = STATI_GET_PRIVATE (stati); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +/* CALLBACK */ +static void +stati_on_btn_nuovo_clicked (GtkButton *button, + gpointer user_data) +{ + GtkWidget *w; + + Stati *stati = (Stati *)user_data; + StatiPrivate *priv = STATI_GET_PRIVATE (stati); + + Stato *c = stato_new (priv->commons, 0); + + g_signal_connect (G_OBJECT (c), "aggiornato", + G_CALLBACK (stati_on_stato_aggiornato), (gpointer)stati); + + w = stato_get_widget (c); + gtk_window_set_transient_for (GTK_WINDOW (w), priv->wtransient); + gtk_widget_show (w); +} + +static void +stati_on_btn_modifica_clicked (GtkButton *button, + gpointer user_data) +{ + stati_modifica ((Stati *)user_data); +} + +static void +stati_on_btn_elimina_clicked (GtkButton *button, + gpointer user_data) +{ + GtkWidget *dialog; + gboolean risp; + + GtkTreeIter iter; + guint id; + + Stati *stati = (Stati *)user_data; + StatiPrivate *priv = STATI_GET_PRIVATE (stati); + + 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 lo stato selezionato?"); + risp = gtk_dialog_run (GTK_DIALOG (dialog)); + if (risp == GTK_RESPONSE_YES) + { + gtk_tree_model_get (GTK_TREE_MODEL (priv->lstore_stati), &iter, + COL_ID, &id, + -1); + + gdaex_execute (priv->commons->gdaex, + g_strdup_printf ("UPDATE stati SET status = 'E' WHERE id = %d", id)); + + stati_carica (stati); + } + 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 uno stato"); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + } +} + +static void +stati_on_trv_stati_row_activated (GtkTreeView *tree_view, + GtkTreePath *tree_path, + GtkTreeViewColumn *column, + gpointer user_data) +{ + StatiPrivate *priv = STATI_GET_PRIVATE ((Stati *)user_data); + + if (priv->selezione) + { + stati_selezionato ((Stati *)user_data); + } + else + { + stati_modifica ((Stati *)user_data); + } +} + + +static void +stati_on_btn_annulla_clicked (GtkButton *button, + gpointer user_data) +{ + StatiPrivate *priv = STATI_GET_PRIVATE ((Stati *)user_data); + + gtk_widget_destroy (priv->widget); + g_object_unref (G_OBJECT (user_data)); +} + +static void +stati_on_btn_seleziona_clicked (GtkButton *button, + gpointer user_data) +{ + stati_selezionato ((Stati *)user_data); +} diff --git a/src/stati.h b/src/stati.h new file mode 100644 index 0000000..83e57bf --- /dev/null +++ b/src/stati.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 __STATI_H__ +#define __STATI_H__ + +#include +#include + +#include +#include + +#include "commons.h" + +G_BEGIN_DECLS + + +#define TYPE_STATI (stati_get_type ()) +#define STATI(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_STATI, Stati)) +#define STATI_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_STATI, StatiClass)) +#define IS_STATI(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_STATI)) +#define IS_STATI_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_STATI)) +#define STATI_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_STATI, StatiClass)) + + +typedef struct _Stati Stati; +typedef struct _StatiClass StatiClass; + +struct _Stati + { + GObject parent; + }; + +struct _StatiClass + { + GObjectClass parent_class; + + guint selezionato_signal_id; + }; + +GType stati_get_type (void) G_GNUC_CONST; + +Stati *stati_new (Commons *commons, gboolean selezione); + +GtkWidget *stati_get_widget (Stati *stati); + + +G_END_DECLS + +#endif /* __STATI_H__ */ diff --git a/src/stato.c b/src/stato.c new file mode 100644 index 0000000..2aa9707 --- /dev/null +++ b/src/stato.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 "stato.h" + +static void stato_class_init (StatoClass *klass); +static void stato_init (Stato *stato); + +static void stato_carica (Stato *stato); +static void stato_salva (Stato *stato); + +static gboolean stato_conferma_chiusura (Stato *stato); + +static void stato_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void stato_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static gboolean stato_on_w_stato_delete_event (GtkWidget *widget, + GdkEvent *event, + gpointer user_data); + +static void stato_on_btn_annulla_clicked (GtkButton *button, + gpointer user_data); +static void stato_on_btn_salva_clicked (GtkButton *button, + gpointer user_data); + +#define STATO_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_STATO, StatoPrivate)) + +enum +{ + LBL_ID +}; + +typedef struct _StatoPrivate StatoPrivate; +struct _StatoPrivate + { + Commons *commons; + + GtkForm *form; + + GtkWidget *w; + + GObject **objects; + + gint id; + }; + +G_DEFINE_TYPE (Stato, stato, G_TYPE_OBJECT) + +static void +stato_class_init (StatoClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (object_class, sizeof (StatoPrivate)); + + object_class->set_property = stato_set_property; + object_class->get_property = stato_get_property; + + /** + * Stato::aggiornato: + * @stato: + * + */ + 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 +stato_init (Stato *stato) +{ + StatoPrivate *priv = STATO_GET_PRIVATE (stato); +} + +/** + * stato_new: + * @commons: + * @id: + * + * Returns: the newly created #Stato object. + */ +Stato +*stato_new (Commons *commons, gint id) +{ + GError *error; + + Stato *a = STATO (g_object_new (stato_get_type (), NULL)); + + StatoPrivate *priv = STATO_GET_PRIVATE (a); + + priv->commons = commons; + + error = NULL; + gtk_builder_add_objects_from_file (priv->commons->gtkbuilder, priv->commons->guifile, + g_strsplit (g_strconcat ("w_stato", NULL), "|", -1), + &error); + if (error != NULL) + { + g_warning ("Errore: %s.", error->message); + return NULL; + } + + priv->form = gtk_form_new (); + g_object_set (G_OBJECT (priv->form), "gdaex", priv->commons->gdaex, NULL); + gtk_form_load_from_file (priv->form, g_build_filename (priv->commons->formdir, "stato.form", NULL), priv->commons->gtkbuilder); + + priv->w = GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, "w_stato")); + + priv->objects = gtk_form_get_objects_by_name (priv->form, + "label3", + NULL); + + g_signal_connect (priv->w, + "delete-event", G_CALLBACK (stato_on_w_stato_delete_event), (gpointer *)a); + + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button6"), + "clicked", G_CALLBACK (stato_on_btn_annulla_clicked), (gpointer *)a); + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button7"), + "clicked", G_CALLBACK (stato_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)); + stato_carica (a); + } + + return a; +} + +/** + * stato_get_widget: + * @stato: + * + */ +GtkWidget +*stato_get_widget (Stato *stato) +{ + StatoPrivate *priv = STATO_GET_PRIVATE (stato); + + return priv->w; +} + +/* PRIVATE */ +static void +stato_carica (Stato *stato) +{ + StatoPrivate *priv = STATO_GET_PRIVATE (stato); + + if (gtk_form_fill_from_table (priv->form)) + { + } +} + +static void +stato_salva (Stato *stato) +{ + GError *error = NULL; + gchar *sql; + GtkWidget *dialog; + + GDate *da; + GDate *a; + + StatoClass *klass = STATO_GET_CLASS (stato); + + StatoPrivate *priv = STATO_GET_PRIVATE (stato); + + 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 (stato, 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 +stato_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) +{ + Stato *stato = STATO (object); + + StatoPrivate *priv = STATO_GET_PRIVATE (stato); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +stato_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) +{ + Stato *stato = STATO (object); + + StatoPrivate *priv = STATO_GET_PRIVATE (stato); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static gboolean +stato_conferma_chiusura (Stato *stato) +{ + GtkWidget *dialog; + + gboolean ret; + + StatoPrivate *priv = STATO_GET_PRIVATE (stato); + + 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 +stato_on_w_stato_delete_event (GtkWidget *widget, + GdkEvent *event, + gpointer user_data) +{ + return !stato_conferma_chiusura ((Stato *)user_data); +} + +static void +stato_on_btn_annulla_clicked (GtkButton *button, + gpointer user_data) +{ + Stato *stato = (Stato *)user_data; + + StatoPrivate *priv = STATO_GET_PRIVATE (stato); + + if (stato_conferma_chiusura (stato)) gtk_widget_destroy (priv->w); +} + +static void +stato_on_btn_salva_clicked (GtkButton *button, + gpointer user_data) +{ + stato_salva ((Stato *)user_data); +} diff --git a/src/stato.h b/src/stato.h new file mode 100644 index 0000000..13949f5 --- /dev/null +++ b/src/stato.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 __STATO_H__ +#define __STATO_H__ + +#include +#include + +#include +#include + +#include "commons.h" + +G_BEGIN_DECLS + + +#define TYPE_STATO (stato_get_type ()) +#define STATO(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_STATO, Stato)) +#define STATO_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_STATO, StatoClass)) +#define IS_STATO(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_STATO)) +#define IS_STATO_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_STATO)) +#define STATO_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_STATO, StatoClass)) + + +typedef struct _Stato Stato; +typedef struct _StatoClass StatoClass; + +struct _Stato + { + GObject parent; + }; + +struct _StatoClass + { + GObjectClass parent_class; + + guint aggiornato_signal_id; + }; + +GType stato_get_type (void) G_GNUC_CONST; + +Stato *stato_new (Commons *commons, gint id); + +GtkWidget *stato_get_widget (Stato *stato); + + +G_END_DECLS + +#endif /* __STATO_H__ */