From cf51e415aaa36fb49e8aa95ee20d86be7fb0400b Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sun, 8 Aug 2010 10:48:45 +0200 Subject: [PATCH] Started management gui. --- data/libaute-db/form/Makefile.am | 2 +- .../form/{autedb.form => user.form} | 0 data/libaute-db/gui/Makefile.am | 2 +- src/Makefile.am | 8 +- src/aute_db.c | 78 +++- src/user.c | 344 ++++++++++++++++++ src/user.h | 65 ++++ 7 files changed, 494 insertions(+), 5 deletions(-) rename data/libaute-db/form/{autedb.form => user.form} (100%) create mode 100644 src/user.c create mode 100644 src/user.h diff --git a/data/libaute-db/form/Makefile.am b/data/libaute-db/form/Makefile.am index cb45d2a..cb7c395 100644 --- a/data/libaute-db/form/Makefile.am +++ b/data/libaute-db/form/Makefile.am @@ -1,5 +1,5 @@ formdir = $(datadir)/libaute-db/form -form_DATA = autedb.form +form_DATA = user.form EXTRA_DIST = $(form_DATA) diff --git a/data/libaute-db/form/autedb.form b/data/libaute-db/form/user.form similarity index 100% rename from data/libaute-db/form/autedb.form rename to data/libaute-db/form/user.form diff --git a/data/libaute-db/gui/Makefile.am b/data/libaute-db/gui/Makefile.am index 30dc744..2134957 100644 --- a/data/libaute-db/gui/Makefile.am +++ b/data/libaute-db/gui/Makefile.am @@ -1,5 +1,5 @@ guidir = $(datadir)/libaute-db/gui -gui_DATA = autedb.glade +gui_DATA = autedb.gui EXTRA_DIST = $(gui_DATA) diff --git a/src/Makefile.am b/src/Makefile.am index dc8bd35..9234dbd 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,9 +1,11 @@ guidir = $(datadir)/libaute-db/gui +formdir = $(datadir)/libaute-db/form AM_CPPFLAGS = $(LIBAUTEDB_CFLAGS) \ $(LIBGCRYPT_CFLAGS) \ $(LIBCONFI_CFLAGS) \ - -DGUIDIR=\""$(guidir)"\" + -DGUIDIR=\""$(guidir)"\" \ + -DFORMDIR=\""$(formdir)"\" LIBS = $(LIBAUTEDB_LIBS) \ $(LIBGCRYPT_LIBS) \ @@ -12,6 +14,8 @@ LIBS = $(LIBAUTEDB_LIBS) \ libaute_pluginsdir = $(libdir)/libaute/plugins libaute_plugins_LTLIBRARIES = libaute-db.la -libaute_db_la_SOURCES = aute_db.c +libaute_db_la_SOURCES = aute_db.c \ + user.c \ + user.h libaute_db_la_LDFLAGS = -no-undefined diff --git a/src/aute_db.c b/src/aute_db.c index 265454d..ebff62e 100644 --- a/src/aute_db.c +++ b/src/aute_db.c @@ -29,9 +29,14 @@ #include #endif +#include "user.h" + static GtkBuilder *gtkbuilder; static GdaEx *gdaex; +static gchar *guifile; +static gchar *formdir; + static GtkWidget *txt_utente; static GtkWidget *txt_password; static GtkWidget *exp_cambio; @@ -237,10 +242,33 @@ autedb_load_users_list () } } +static void +autedb_on_user_aggiornato (gpointer instance, gpointer user_data) +{ + autedb_load_users_list (); +} + static void autedb_on_btn_new_clicked (GtkButton *button, gpointer user_data) { + GtkWidget *w; + + User *u = user_new (gtkbuilder, gdaex, guifile, formdir, NULL); + + g_signal_connect (G_OBJECT (u), "aggiornato", + G_CALLBACK (autedb_on_user_aggiornato), NULL); + + w = user_get_widget (u); + /*if (priv->selezione) + { + gtk_window_set_transient_for (GTK_WINDOW (w), GTK_WINDOW (priv->widget)); + } + else + { + gtk_window_set_transient_for (GTK_WINDOW (w), GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main"))); + }*/ + gtk_widget_show (w); } static void @@ -253,6 +281,43 @@ static void autedb_on_btn_delete_clicked (GtkButton *button, gpointer user_data) { + GtkWidget *dialog; + gboolean risp; + + GtkTreeIter iter; + gchar *code; + + if (gtk_tree_selection_get_selected (sel_users, NULL, &iter)) + { + /*dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_QUESTION, + GTK_BUTTONS_YES_NO, + "Sicuro di voler eliminare il modello selezionato?"); + risp = gtk_dialog_run (GTK_DIALOG (dialog));*/ + if (risp == GTK_RESPONSE_YES) + { + gtk_tree_model_get (GTK_TREE_MODEL (lstore_users), &iter, + COL_CODE, &code, + -1); + + gdaex_execute (gdaex, + g_strdup_printf ("UPDATE users SET status = 'E' WHERE code = '%s'", code)); + + autedb_load_users_list (); + } + /*gtk_widget_destroy (dialog);*/ + } + else + { + /*dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "Occorre prima selezionare un modello"); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog);*/ + } } static void @@ -286,7 +351,18 @@ gchar GUIDIR = g_build_filename (g_win32_get_package_installation_directory_of_module (NULL), "share", "libaute-db", "gui", NULL); #endif - if (!gtk_builder_add_objects_from_file (gtkbuilder, g_build_filename (GUIDIR, "autedb.gui", NULL), +#ifdef G_OS_WIN32 +#undef FORMDIR + + gchar *FORMDIR; + + FORMDIR = g_build_filename (g_win32_get_package_installation_directory_of_module (NULL), "share", "libaute-db", "form", NULL); +#endif + + formdir = g_strdup (FORMDIR); + + guifile = g_build_filename (GUIDIR, "autedb.gui", NULL); + if (!gtk_builder_add_objects_from_file (gtkbuilder, guifile, g_strsplit ("diag_main", "|", -1), &error)) { diff --git a/src/user.c b/src/user.c new file mode 100644 index 0000000..581ae3b --- /dev/null +++ b/src/user.c @@ -0,0 +1,344 @@ +/* + * 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 "user.h" + +static void user_class_init (UserClass *klass); +static void user_init (User *user); + +static void user_carica (User *user); +static void user_salva (User *user); + +static gboolean user_conferma_chiusura (User *user); + +static void user_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void user_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static gboolean user_on_w_user_delete_event (GtkWidget *widget, + GdkEvent *event, + gpointer user_data); + +static void user_on_btn_annulla_clicked (GtkButton *button, + gpointer user_data); +static void user_on_btn_salva_clicked (GtkButton *button, + gpointer user_data); + +#define USER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_USER, UserPrivate)) + +enum +{ + TXT_CODE +}; + +typedef struct _UserPrivate UserPrivate; +struct _UserPrivate + { + GtkBuilder *gtkbuilder; + GdaEx *gdaex; + gchar *guifile; + gchar *formdir; + + GtkForm *form; + + GtkWidget *w; + + GObject **objects; + + gchar *code; + }; + +G_DEFINE_TYPE (User, user, G_TYPE_OBJECT) + +static void +user_class_init (UserClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (object_class, sizeof (UserPrivate)); + + object_class->set_property = user_set_property; + object_class->get_property = user_get_property; + + /** + * User::aggiornato: + * @user: + * + */ + 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 +user_init (User *user) +{ + UserPrivate *priv = USER_GET_PRIVATE (user); + + priv->code = NULL; +} + +/** + * user_new: + * @gtkbuilder: + * @gdaex: + * @guifile: + * @formdir: + * @code: + * + * Returns: the newly created #User object. + */ +User +*user_new (GtkBuilder *gtkbuilder, GdaEx *gdaex, + const gchar *guifile, const gchar *formdir, const gchar *code) +{ + GError *error; + + User *a = USER (g_object_new (user_get_type (), NULL)); + + UserPrivate *priv = USER_GET_PRIVATE (a); + + priv->gtkbuilder = gtkbuilder; + priv->gdaex = gdaex; + priv->guifile = g_strdup (guifile); + priv->formdir = g_strdup (formdir); + + error = NULL; + gtk_builder_add_objects_from_file (priv->gtkbuilder, priv->guifile, + g_strsplit ("w_user", "|", -1), + &error); + if (error != NULL) + { + g_warning ("Errore: %s.", error->message); + return NULL; + } + + priv->form = gtk_form_new (); + g_object_set (priv->form, "gdaex", priv->gdaex, NULL); + gtk_form_load_from_file (priv->form, g_build_filename (priv->formdir, "user.form", NULL), priv->gtkbuilder); + + g_object_set (priv->form, "gdaex", priv->gdaex, NULL); + + priv->w = GTK_WIDGET (gtk_builder_get_object (priv->gtkbuilder, "w_user")); + + priv->objects = gtk_form_get_objects_by_name (priv->form, + "entry1", + NULL); + + g_signal_connect (priv->w, + "delete-event", G_CALLBACK (user_on_w_user_delete_event), (gpointer *)a); + + g_signal_connect (gtk_builder_get_object (priv->gtkbuilder, "button5"), + "clicked", G_CALLBACK (user_on_btn_annulla_clicked), (gpointer *)a); + g_signal_connect (gtk_builder_get_object (priv->gtkbuilder, "button6"), + "clicked", G_CALLBACK (user_on_btn_salva_clicked), (gpointer *)a); + + if (code != NULL) + { + priv->code = g_strstrip (g_strdup (code)); + } + if (priv->code == NULL || g_strcmp0 (priv->code, "") == 0) + { + gtk_form_clear (priv->form); + } + else + { + gtk_entry_set_text (GTK_ENTRY (priv->objects[TXT_CODE]), priv->code); + gtk_editable_set_editable (GTK_EDITABLE (priv->objects[TXT_CODE]), FALSE); + + user_carica (a); + } + + return a; +} + +/** + * user_get_widget: + * @user: + * + */ +GtkWidget +*user_get_widget (User *user) +{ + UserPrivate *priv = USER_GET_PRIVATE (user); + + return priv->w; +} + +/* PRIVATE */ +static void +user_carica (User *user) +{ + UserPrivate *priv = USER_GET_PRIVATE (user); + + if (gtk_form_fill_from_table (priv->form)) + { + } +} + +static void +user_salva (User *user) +{ + GError *error = NULL; + gchar *sql; + GtkWidget *dialog; + + GDate *da; + GDate *a; + + UserClass *klass = USER_GET_CLASS (user); + + UserPrivate *priv = USER_GET_PRIVATE (user); + + if (!gtk_form_check (priv->form, FALSE, NULL, TRUE, priv->w, TRUE)) + { + return; + } + + if (priv->code == NULL || g_strcmp0 (priv->code, "") == 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->gdaex, sql) == 1) + { + g_signal_emit (user, klass->aggiornato_signal_id, 0); + + gtk_form_set_as_origin (priv->form); + + 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 + { + 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 +user_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) +{ + User *user = USER (object); + + UserPrivate *priv = USER_GET_PRIVATE (user); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +user_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) +{ + User *user = USER (object); + + UserPrivate *priv = USER_GET_PRIVATE (user); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static gboolean +user_conferma_chiusura (User *user) +{ + GtkWidget *dialog; + + gboolean ret; + + UserPrivate *priv = USER_GET_PRIVATE (user); + + 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 +user_on_w_user_delete_event (GtkWidget *widget, + GdkEvent *event, + gpointer user_data) +{ + return !user_conferma_chiusura ((User *)user_data); +} + +static void +user_on_btn_annulla_clicked (GtkButton *button, + gpointer user_data) +{ + User *user = (User *)user_data; + + UserPrivate *priv = USER_GET_PRIVATE (user); + + if (user_conferma_chiusura (user)) gtk_widget_destroy (priv->w); +} + +static void +user_on_btn_salva_clicked (GtkButton *button, + gpointer user_data) +{ + user_salva ((User *)user_data); +} diff --git a/src/user.h b/src/user.h new file mode 100644 index 0000000..63e0d06 --- /dev/null +++ b/src/user.h @@ -0,0 +1,65 @@ +/* + * 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 __USER_H__ +#define __USER_H__ + +#include +#include + +#include +#include + +G_BEGIN_DECLS + + +#define TYPE_USER (user_get_type ()) +#define USER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_USER, User)) +#define USER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_USER, UserClass)) +#define IS_USER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_USER)) +#define IS_USER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_USER)) +#define USER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_USER, UserClass)) + + +typedef struct _User User; +typedef struct _UserClass UserClass; + +struct _User + { + GObject parent; + }; + +struct _UserClass + { + GObjectClass parent_class; + + guint aggiornato_signal_id; + }; + +GType user_get_type (void) G_GNUC_CONST; + +User *user_new (GtkBuilder *gtkbuilder, GdaEx *gdaex, + const gchar *guifile, const gchar *formdir, const gchar *code); + +GtkWidget *user_get_widget (User *user); + + +G_END_DECLS + +#endif /* __USER_H__ */ -- 2.49.0