From: Andrea Zagli Date: Sat, 14 Aug 2010 09:23:07 +0000 (+0200) Subject: Started single datasource management. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=1898f1f76661f9a59f7f0bd0981cb0a36a671a71;p=zakaudit%2Fgui Started single datasource management. --- diff --git a/data/audit-gui/gui/audit-gui.gui b/data/audit-gui/gui/audit-gui.gui index 2f99d0e..da6ed87 100644 --- a/data/audit-gui/gui/audit-gui.gui +++ b/data/audit-gui/gui/audit-gui.gui @@ -209,7 +209,87 @@ vertical 5 - + + True + 3 + 2 + 5 + 5 + + + True + 0 + ID + + + GTK_FILL + GTK_FILL + + + + + True + 0 + lbl_id + + + 1 + 2 + GTK_FILL + + + + + True + Name + + + 1 + 2 + GTK_FILL + GTK_FILL + + + + + True + True + 30 + + + + 1 + 2 + 1 + 2 + GTK_FILL + + + + + True + 0 + + + + + + True + <b>Connection string</b> + True + + + + + 2 + 2 + 3 + + + + + 0 + diff --git a/src/Makefile.am b/src/Makefile.am index c24d390..eaeb64e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,8 +10,10 @@ bin_PROGRAMS = audit-gui audit_gui_SOURCES = \ commons.h \ + datasource.c \ + datasource.h \ datasources.c \ datasources.h \ main.c \ openauditds.c \ - openauditds.h \ No newline at end of file + openauditds.h diff --git a/src/datasource.c b/src/datasource.c new file mode 100644 index 0000000..bccdd0b --- /dev/null +++ b/src/datasource.c @@ -0,0 +1,379 @@ +/* + * 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 + +#include "datasource.h" + +static void datasource_class_init (DatasourceClass *klass); +static void datasource_init (Datasource *datasource); + +static void datasource_load (Datasource *datasource); +static void datasource_save (Datasource *datasource); + +static void datasource_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void datasource_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static void datasource_on_wlogin_changed (GdauiLogin *gdauilogin, + gboolean arg1, + gpointer user_data); + +static void datasource_on_btn_cancel_clicked (GtkButton *button, + gpointer user_data); +static void datasource_on_btn_save_clicked (GtkButton *button, + gpointer user_data); + +#define DATASOURCE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_DATASOURCE, DatasourcePrivate)) + +typedef struct _DatasourcePrivate DatasourcePrivate; +struct _DatasourcePrivate + { + Commons *commons; + + GtkWidget *w; + GtkWidget *wlogin; + + gint id; + }; + +G_DEFINE_TYPE (Datasource, datasource, G_TYPE_OBJECT) + +static void +datasource_class_init (DatasourceClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (object_class, sizeof (DatasourcePrivate)); + + object_class->set_property = datasource_set_property; + object_class->get_property = datasource_get_property; + + /** + * Datasource::updated: + * @datasource: + * + */ + klass->updated_signal_id = g_signal_new ("updated", + G_TYPE_FROM_CLASS (object_class), + G_SIGNAL_RUN_LAST, + 0, + NULL, + NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, + 0); +} + +static void +datasource_init (Datasource *datasource) +{ + DatasourcePrivate *priv = DATASOURCE_GET_PRIVATE (datasource); +} + +/** + * datasource_new: + * @commons: + * @id: + * + * Returns: the newly created #Datasource object. + */ +Datasource +*datasource_new (Commons *commons, gint id) +{ + GError *error; + GdauiLoginMode mode; + + Datasource *a = DATASOURCE (g_object_new (datasource_get_type (), NULL)); + + DatasourcePrivate *priv = DATASOURCE_GET_PRIVATE (a); + + priv->commons = commons; + + error = NULL; + gtk_builder_add_objects_from_file (priv->commons->gtkbuilder, priv->commons->guifile, + g_strsplit ("w_datasource", "|", -1), + &error); + if (error != NULL) + { + g_warning ("Errore: %s.", error->message); + return NULL; + } + + priv->w = GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, "w_datasource")); + + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button3"), + "clicked", G_CALLBACK (datasource_on_btn_cancel_clicked), (gpointer *)a); + g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button4"), + "clicked", G_CALLBACK (datasource_on_btn_save_clicked), (gpointer *)a); + + /* creating login widget */ + priv->wlogin = gdaui_login_new (NULL); + + g_object_get (G_OBJECT (priv->wlogin), "mode", &mode, NULL); + mode |= GDA_UI_LOGIN_HIDE_DSN_SELECTION_MODE; + gdaui_login_set_mode (GDAUI_LOGIN (priv->wlogin), mode); + + g_signal_connect (G_OBJECT (priv->wlogin), "changed", + G_CALLBACK (datasource_on_wlogin_changed), (gpointer *)a); + + gtk_container_add (GTK_CONTAINER (gtk_builder_get_object (priv->commons->gtkbuilder, "frame1")), + priv->wlogin); + + gtk_widget_set_sensitive (GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, "button4")), FALSE); + + priv->id = id; + if (priv->id == 0) + { + gtk_label_set_text (GTK_LABEL (gtk_builder_get_object (priv->commons->gtkbuilder, "label2")), ""); + } + else + { + gtk_label_set_text (GTK_LABEL (gtk_builder_get_object (priv->commons->gtkbuilder, "label2")), g_strdup_printf ("%d", priv->id)); + datasource_load (a); + } + + return a; +} + +/** + * datasource_get_widget: + * @datasource: + * + */ +GtkWidget +*datasource_get_widget (Datasource *datasource) +{ + DatasourcePrivate *priv = DATASOURCE_GET_PRIVATE (datasource); + + return priv->w; +} + +/* PRIVATE */ +static void +datasource_load (Datasource *datasource) +{ + DatasourcePrivate *priv = DATASOURCE_GET_PRIVATE (datasource); + + GError *error; + gchar *sql; + GdaStatement *stmt; + GdaDataModel *dm; + GdaDsnInfo *info; + gchar *username; + gchar *password; + + sql = g_strdup_printf ("SELECT id, name, cnc_string FROM datasources WHERE id = %d", + priv->id); + stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL); + dm = gda_connection_statement_execute_select (priv->commons->gdacon, stmt, NULL, &error); + g_object_unref (stmt); + if (dm != NULL && gda_data_model_get_n_rows (dm) == 1) + { + gtk_entry_set_text (GTK_ENTRY (gtk_builder_get_object (priv->commons->gtkbuilder, "entry1")), + gda_value_stringify (gda_data_model_get_value_at (dm, 1, 0, NULL))); + + info = g_malloc0 (sizeof (GdaDsnInfo)); + gda_connection_string_split (gda_value_stringify (gda_data_model_get_value_at (dm, 2, 0, NULL)), + &info->cnc_string, + &info->provider, + &username, + &password); + g_message ("PROVIDER %s CNC %s USER %s PWD %s", info->provider, info->cnc_string, username, password); + gdaui_login_set_connection_information (GDAUI_LOGIN (priv->wlogin), info); + } + else + { + GtkWidget *dialog; + dialog = gtk_message_dialog_new (GTK_WINDOW (priv->w), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "Record %d doesn't exist.", priv->id); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + } +} + +static void +datasource_save (Datasource *datasource) +{ + const GdaDsnInfo *info; + GError *error; + gchar *sql; + GdaStatement *stmt; + GdaDataModel *dm; + GtkWidget *dialog; + + gchar *cncstring; + + DatasourceClass *klass = DATASOURCE_GET_CLASS (datasource); + + DatasourcePrivate *priv = DATASOURCE_GET_PRIVATE (datasource); + + info = gdaui_login_get_connection_information (GDAUI_LOGIN (priv->wlogin)); + + cncstring = g_strconcat (info->provider, "://", + (info->auth_string != NULL ? g_strdup_printf ("%s;", info->auth_string) : ""), + info->cnc_string, NULL); + + if (priv->id == 0) + { + /* find the new id */ + guint new_id; + + new_id = 0; + sql = g_strdup_printf ("SELECT COALESCE (MAX (id), 0) FROM datasources"); + stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL); + dm = gda_connection_statement_execute_select (priv->commons->gdacon, stmt, NULL, &error); + g_object_unref (stmt); + if (dm != NULL && gda_data_model_get_n_rows (dm) == 1) + { + new_id = g_value_get_int (gda_data_model_get_value_at (dm, 0, 0, NULL)); + } + new_id++; + + sql = g_strdup_printf ("INSERT INTO datasources (id, name, cnc_string)" + " VALUES (%d, '%s', '%s')", + new_id, + gtk_entry_get_text (GTK_ENTRY (gtk_builder_get_object (priv->commons->gtkbuilder, "entry1"))), + cncstring); + stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL); + } + else + { + sql = g_strdup_printf ("UPDATE datasources" + " SET" + " name = '%s'," + " cnc_string = '%s'" + " WHERE id = %d", + gtk_entry_get_text (GTK_ENTRY (gtk_builder_get_object (priv->commons->gtkbuilder, "entry1"))), + cncstring, + priv->id); + stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL); + } + + error = NULL; + if (gda_connection_statement_execute_non_select (priv->commons->gdacon, stmt, NULL, NULL, &error) == 1) + { + g_object_unref (stmt); + + g_signal_emit (datasource, klass->updated_signal_id, 0); + + if (priv->id == 0) + { + priv->id = strtol (gtk_label_get_text (GTK_LABEL (gtk_builder_get_object (priv->commons->gtkbuilder, "label2"))), NULL, 10); + } + + dialog = gtk_message_dialog_new (GTK_WINDOW (priv->w), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_INFO, + GTK_BUTTONS_OK, + "Saved with success."); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + } + else + { + if (priv->id == 0) + { + gtk_label_set_text (GTK_LABEL (gtk_builder_get_object (priv->commons->gtkbuilder, "label2")), ""); + } + + dialog = gtk_message_dialog_new (GTK_WINDOW (priv->w), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "Error on saving.\n\n%s", + (error != NULL && error->message != NULL ? error->message : "No details.")); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + } +} + +static void +datasource_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) +{ + Datasource *datasource = DATASOURCE (object); + + DatasourcePrivate *priv = DATASOURCE_GET_PRIVATE (datasource); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +datasource_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) +{ + Datasource *datasource = DATASOURCE (object); + + DatasourcePrivate *priv = DATASOURCE_GET_PRIVATE (datasource); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +/* CALLBACK */ +static void +datasource_on_wlogin_changed (GdauiLogin *gdauilogin, + gboolean arg1, + gpointer user_data) +{ + Datasource *ds = (Datasource *)user_data; + + DatasourcePrivate *priv = DATASOURCE_GET_PRIVATE (ds); + + gboolean is_valid; + + g_object_get (G_OBJECT (priv->wlogin), "valid", &is_valid, NULL); + gtk_widget_set_sensitive (GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, "button4")), + is_valid); +} + +static void +datasource_on_btn_cancel_clicked (GtkButton *button, + gpointer user_data) +{ + Datasource *datasource = (Datasource *)user_data; + + DatasourcePrivate *priv = DATASOURCE_GET_PRIVATE (datasource); + + gtk_widget_destroy (priv->w); +} + +static void +datasource_on_btn_save_clicked (GtkButton *button, + gpointer user_data) +{ + datasource_save ((Datasource *)user_data); +} diff --git a/src/datasource.h b/src/datasource.h new file mode 100644 index 0000000..32a9998 --- /dev/null +++ b/src/datasource.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 __DATASOURCE_H__ +#define __DATASOURCE_H__ + +#include +#include + +#include +#include + +#include "commons.h" + +G_BEGIN_DECLS + + +#define TYPE_DATASOURCE (datasource_get_type ()) +#define DATASOURCE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_DATASOURCE, Datasource)) +#define DATASOURCE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_DATASOURCE, DatasourceClass)) +#define IS_DATASOURCE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_DATASOURCE)) +#define IS_DATASOURCE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_DATASOURCE)) +#define DATASOURCE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_DATASOURCE, DatasourceClass)) + + +typedef struct _Datasource Datasource; +typedef struct _DatasourceClass DatasourceClass; + +struct _Datasource + { + GObject parent; + }; + +struct _DatasourceClass + { + GObjectClass parent_class; + + guint updated_signal_id; + }; + +GType datasource_get_type (void) G_GNUC_CONST; + +Datasource *datasource_new (Commons *commons, gint id); + +GtkWidget *datasource_get_widget (Datasource *datasource); + + +G_END_DECLS + +#endif /* __DATASOURCE_H__ */ diff --git a/src/datasources.c b/src/datasources.c index bcaede7..4c17331 100644 --- a/src/datasources.c +++ b/src/datasources.c @@ -20,6 +20,7 @@ #include #include "datasources.h" +#include "datasource.h" static void datasources_class_init (DatasourcesClass *klass); static void datasources_init (Datasources *datasources); @@ -27,7 +28,7 @@ static void datasources_init (Datasources *datasources); static void datasources_load (Datasources *datasources); static void datasources_edit (Datasources *datasources); -static void datasources_on_costo_aggiornato (gpointer instance, gpointer user_data); +static void datasources_on_datasource_updated (gpointer instance, gpointer user_data); static void datasources_set_property (GObject *object, guint property_id, @@ -195,7 +196,7 @@ datasources_load (Datasources *datasources) static void datasources_edit (Datasources *datasources) { - /*GtkTreeIter iter; + GtkTreeIter iter; guint id; DatasourcesPrivate *priv = DATASOURCES_GET_PRIVATE (datasources); @@ -204,33 +205,33 @@ datasources_edit (Datasources *datasources) { GtkWidget *w; - gtk_tree_model_get (GTK_TREE_MODEL (priv->lstore_datasources), &iter, - COL_ID, &id, + gtk_tree_model_get (GTK_TREE_MODEL (priv->tstore_datasources), &iter, + COL_ID_DS, &id, -1); - Datasourceso *c = datasourceso_new (priv->commons, id); + Datasource *c = datasource_new (priv->commons, id); - g_signal_connect (G_OBJECT (c), "aggiornato", - G_CALLBACK (datasources_on_costo_aggiornato), (gpointer)datasources); + g_signal_connect (G_OBJECT (c), "updated", + G_CALLBACK (datasources_on_datasource_updated), (gpointer)datasources); - w = datasourceso_get_widget (c); - gtk_window_set_transient_for (GTK_WINDOW (w), priv->wtransient); - gtk_widget_show (w); + w = datasource_get_widget (c); + gtk_window_set_transient_for (GTK_WINDOW (w), GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main"))); + gtk_widget_show_all (w); } else { - GtkWidget *dialog = gtk_message_dialog_new (priv->wtransient, + GtkWidget *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 datasourceso"); gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_destroy (dialog); - }*/ + } } static void -datasources_on_costo_aggiornato (gpointer instance, gpointer user_data) +datasources_on_datasource_updated (gpointer instance, gpointer user_data) { datasources_load ((Datasources *)user_data); } @@ -270,19 +271,19 @@ static void datasources_on_btn_new_clicked (GtkButton *button, gpointer user_data) { - /*GtkWidget *w; + GtkWidget *w; Datasources *datasources = (Datasources *)user_data; DatasourcesPrivate *priv = DATASOURCES_GET_PRIVATE (datasources); - Datasourceso *c = datasourceso_new (priv->commons, 0); + Datasource *c = datasource_new (priv->commons, 0); - g_signal_connect (G_OBJECT (c), "aggiornato", - G_CALLBACK (datasources_on_costo_aggiornato), (gpointer)datasources); + g_signal_connect (G_OBJECT (c), "updated", + G_CALLBACK (datasources_on_datasource_updated), (gpointer)datasources); - w = datasourceso_get_widget (c); - gtk_window_set_transient_for (GTK_WINDOW (w), priv->wtransient); - gtk_widget_show (w);*/ + w = datasource_get_widget (c); + gtk_window_set_transient_for (GTK_WINDOW (w), GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main"))); + gtk_widget_show_all (w); } static void @@ -347,4 +348,4 @@ datasources_on_trv_datasources_row_activated (GtkTreeView *tree_view, DatasourcesPrivate *priv = DATASOURCES_GET_PRIVATE ((Datasources *)user_data); datasources_edit ((Datasources *)user_data); -} \ No newline at end of file +}