<property name="orientation">vertical</property>
<property name="spacing">5</property>
<child>
- <placeholder/>
+ <object class="GtkTable" id="table1">
+ <property name="visible">True</property>
+ <property name="n_rows">3</property>
+ <property name="n_columns">2</property>
+ <property name="column_spacing">5</property>
+ <property name="row_spacing">5</property>
+ <child>
+ <object class="GtkLabel" id="label1">
+ <property name="visible">True</property>
+ <property name="xalign">0</property>
+ <property name="label" translatable="yes">ID</property>
+ </object>
+ <packing>
+ <property name="x_options">GTK_FILL</property>
+ <property name="y_options">GTK_FILL</property>
+ </packing>
+ </child>
+ <child>
+ <object class="GtkLabel" id="label2">
+ <property name="visible">True</property>
+ <property name="xalign">0</property>
+ <property name="label" translatable="yes">lbl_id</property>
+ </object>
+ <packing>
+ <property name="left_attach">1</property>
+ <property name="right_attach">2</property>
+ <property name="y_options">GTK_FILL</property>
+ </packing>
+ </child>
+ <child>
+ <object class="GtkLabel" id="label3">
+ <property name="visible">True</property>
+ <property name="label" translatable="yes">Name</property>
+ </object>
+ <packing>
+ <property name="top_attach">1</property>
+ <property name="bottom_attach">2</property>
+ <property name="x_options">GTK_FILL</property>
+ <property name="y_options">GTK_FILL</property>
+ </packing>
+ </child>
+ <child>
+ <object class="GtkEntry" id="entry1">
+ <property name="visible">True</property>
+ <property name="can_focus">True</property>
+ <property name="max_length">30</property>
+ <property name="invisible_char">●</property>
+ </object>
+ <packing>
+ <property name="left_attach">1</property>
+ <property name="right_attach">2</property>
+ <property name="top_attach">1</property>
+ <property name="bottom_attach">2</property>
+ <property name="y_options">GTK_FILL</property>
+ </packing>
+ </child>
+ <child>
+ <object class="GtkFrame" id="frame1">
+ <property name="visible">True</property>
+ <property name="label_xalign">0</property>
+ <child>
+ <placeholder/>
+ </child>
+ <child type="label">
+ <object class="GtkLabel" id="label4">
+ <property name="visible">True</property>
+ <property name="label" translatable="yes"><b>Connection string</b></property>
+ <property name="use_markup">True</property>
+ </object>
+ </child>
+ </object>
+ <packing>
+ <property name="right_attach">2</property>
+ <property name="top_attach">2</property>
+ <property name="bottom_attach">3</property>
+ </packing>
+ </child>
+ </object>
+ <packing>
+ <property name="position">0</property>
+ </packing>
</child>
<child>
<object class="GtkHButtonBox" id="hbuttonbox2">
--- /dev/null
+/*
+ * Copyright (C) 2010 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 <sql-parser/gda-sql-parser.h>
+
+#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);
+}
#include <sql-parser/gda-sql-parser.h>
#include "datasources.h"
+#include "datasource.h"
static void datasources_class_init (DatasourcesClass *klass);
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,
static void
datasources_edit (Datasources *datasources)
{
- /*GtkTreeIter iter;
+ GtkTreeIter iter;
guint id;
DatasourcesPrivate *priv = DATASOURCES_GET_PRIVATE (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);
}
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
DatasourcesPrivate *priv = DATASOURCES_GET_PRIVATE ((Datasources *)user_data);
datasources_edit ((Datasources *)user_data);
-}
\ No newline at end of file
+}