typedef GtkFormWidget *(* FormWidgetConstructorFunc) (void);
+typedef struct
+{
+ gchar *name;
+ gchar *cnc_string;
+ GdaEx *gdaex;
+} GtkFormDbConnection;
+
static void gtk_form_class_init (GtkFormClass *class);
static void gtk_form_init (GtkForm *form);
static void parse_widget_on_change (GtkForm *form, xmlNodePtr xnode, GtkFormWidget *fwidget);
static void parse_group (GtkForm *form, xmlNodePtr xnode);
+static void parse_db_connection (GtkForm *form, xmlNodePtr xnode);
static void gtk_form_set_property (GObject *object,
guint property_id,
GList *modules;
GdaEx *gdaex;
- GSList *groups;
+ GSList *groups; /*GtkFormGroup */
+
+ GSList *db_connections; /* GtkFormDbConnection */
guint debug;
GFileOutputStream *log_file;
priv->key = NULL;
priv->gdaex = NULL;
priv->groups = NULL;
+ priv->db_connections = NULL;
}
/**
parse_widget_on_change (GtkForm *form, xmlNodePtr xnode, GtkFormWidget *fwidget)
{
gchar *what;
+ gchar *connection_name;
gchar *widget_name;
gchar *sql;
{
widget_name = (gchar *)xmlNodeGetContent (child);
}
+ else if (xmlStrcmp (child->name, (const xmlChar *)"connection-name") == 0)
+ {
+ connection_name = (gchar *)xmlNodeGetContent (child);
+ }
else if (xmlStrcmp (child->name, (const xmlChar *)"sql") == 0)
{
sql = (gchar *)xmlNodeGetContent (child);
}
if (sql != NULL && g_strcmp0 (g_strstrip (sql), "") != 0)
- {
+ {
GtkFormWidgetOnChangeOpUpdate *op = (GtkFormWidgetOnChangeOpUpdate *)g_malloc0 (sizeof (GtkFormWidgetOnChangeOpUpdate));
op->op.type = GTK_FORM_WIDGET_ON_CHANGE_OP_UPDATE;
+ if (connection_name != NULL && g_strcmp0 (g_strstrip (connection_name), "") != 0)
+ {
+ op->connection_name = g_strdup (connection_name);
+ }
if (widget_name != NULL && g_strcmp0 (g_strstrip (widget_name), "") != 0)
{
op->form_widget_name = g_strdup (widget_name);
}
op->sql = g_strdup (sql);
- gchar *when = (gchar *)xmlGetProp (xnode, (const xmlChar *)"when");
- if (when == NULL)
- {
+ gchar *when = (gchar *)xmlGetProp (xnode, (const xmlChar *)"when");
+ if (when == NULL)
+ {
op->when = GTK_FORM_WIDGET_ON_CHANGE_OP_UPDATE_BOTH;
- }
+ }
else if (g_strcmp0 (when, "before") == 0)
{
op->when = GTK_FORM_WIDGET_ON_CHANGE_OP_UPDATE_BEFORE;
{
op->when = GTK_FORM_WIDGET_ON_CHANGE_OP_UPDATE_BOTH;
}
-
+
gtk_form_widget_add_on_change_op (fwidget, (GtkFormWidgetOnChangeOp *)op);
}
}
}
}
+static void
+parse_db_connection (GtkForm *form, xmlNodePtr xnode)
+{
+ xmlNodePtr child;
+ gchar *name;
+ gchar *cnc_string;
+
+ name = (gchar *)xmlGetProp (xnode, "name");
+
+ child = xnode->children;
+ while (child != NULL)
+ {
+ if (xmlStrcmp (child->name, (const xmlChar *)"connection_string") == 0)
+ {
+ cnc_string = (gchar *)xmlNodeGetContent (child);
+ break;
+ }
+
+ child = child->next;
+ }
+
+ if (g_strcmp0 (g_strstrip (name), "") != 0
+ && g_strcmp0 (g_strstrip (cnc_string), "") != 0)
+ {
+ gtk_form_add_db_connection (form, name, cnc_string);
+ }
+}
+
/**
* gtk_form_load_from_xml:
* @form:
{
parse_group (form, cur);
}
+ else if (xmlStrcmp (cur->name, (const xmlChar *)"db_connection") == 0)
+ {
+ parse_db_connection (form, cur);
+ }
cur = cur->next;
}
return g;
}
+/**
+ * gtk_form_add_db_connection:
+ * @form:
+ * @name:
+ * @connection_string:
+ *
+ */
+gboolean
+gtk_form_add_db_connection (GtkForm *form, const gchar *name, const gchar *connection_string)
+{
+ gboolean ret;
+
+ gchar *_name;
+ gchar *_cnc_string;
+
+ GSList *dbs;
+ GtkFormDbConnection *dbc;
+ GtkFormDbConnection *dbc_new;
+
+ g_return_val_if_fail (IS_GTK_FORM (form), FALSE);
+ g_return_val_if_fail (name != NULL, FALSE);
+ g_return_val_if_fail (connection_string != NULL, FALSE);
+
+ GtkFormPrivate *priv;
+
+ ret = FALSE;
+
+ priv = GTK_FORM_GET_PRIVATE (form);
+
+ _name = g_strstrip (g_strdup (name));
+ if (g_strcmp0 (_name, "") == 0)
+ {
+ return FALSE;
+ }
+ _cnc_string = g_strstrip (g_strdup (connection_string));
+ if (g_strcmp0 (_cnc_string, "") == 0)
+ {
+ return FALSE;
+ }
+
+ dbc = NULL;
+ dbs = priv->db_connections;
+ while (dbs != NULL)
+ {
+ dbc = (GtkFormDbConnection *)dbs->data;
+ if (g_strcmp0 (dbc->cnc_string, _cnc_string) == 0)
+ {
+ break;
+ }
+ dbc = NULL;
+
+ dbs = dbs->next;
+ }
+
+ if (dbc != NULL)
+ {
+ if (g_strcmp0 (dbc->name, _name) != 0)
+ {
+ dbc_new = g_new0 (GtkFormDbConnection, 1);
+ dbc_new->name = g_strdup (_name);
+ dbc_new->cnc_string = g_strdup (_cnc_string);
+ dbc_new->gdaex = g_object_ref (dbc->gdaex);
+ priv->db_connections = g_slist_append (priv->db_connections, dbc_new);
+ }
+ }
+ else
+ {
+ dbc_new = g_new0 (GtkFormDbConnection, 1);
+ dbc_new->gdaex = gdaex_new_from_string (_cnc_string);
+ if (dbc_new->gdaex != NULL)
+ {
+ dbc_new->name = g_strdup (_name);
+ dbc_new->cnc_string = g_strdup (_cnc_string);
+
+ priv->db_connections = g_slist_append (priv->db_connections, dbc_new);
+ }
+ else
+ {
+ g_free (dbc_new);
+ return FALSE;
+ }
+ }
+
+ ret = TRUE;
+
+ return ret;
+}
+
+/**
+ * gtk_form_get_db_connection_by_name:
+ * @form:
+ * @name:
+ *
+ */
+GdaEx
+*gtk_form_get_db_connection_by_name (GtkForm *form, const gchar *name)
+{
+ GdaEx *gdaex;
+
+ gchar *_name;
+ GSList *dbs;
+ GtkFormDbConnection *dbc;
+
+ g_return_val_if_fail (IS_GTK_FORM (form), NULL);
+ g_return_val_if_fail (name != NULL, NULL);
+
+ _name = g_strstrip (g_strdup (name));
+
+ g_return_val_if_fail (g_strcmp0 (_name, "") != 0, NULL);
+
+ GtkFormPrivate *priv = GTK_FORM_GET_PRIVATE (form);
+
+ gdaex = NULL;
+
+ dbs = priv->db_connections;
+ while (dbs != NULL)
+ {
+ dbc = (GtkFormDbConnection *)dbs->data;
+ if (g_strcmp0 (dbc->name, _name) == 0)
+ {
+ gdaex = dbc->gdaex;
+ break;
+ }
+
+ dbs = dbs->next;
+ }
+
+ return gdaex;
+}
+
/* PRIVATE */
static void
gtk_form_set_property (GObject *object,
/*
- * Copyright (C) 2005-2010 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2005-2011 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
gdaex = NULL;
- g_object_get (G_OBJECT (priv->form), "gdaex", &gdaex, NULL);
+ if (op->connection_name != NULL
+ && g_strcmp0 (op->connection_name, "") != 0)
+ {
+ gdaex = gtk_form_get_db_connection_by_name (priv->form, op->connection_name);
+ }
+
+ if (!IS_GDAEX (gdaex))
+ {
+ g_object_get (G_OBJECT (priv->form), "gdaex", &gdaex, NULL);
+ }
+
if (IS_GDAEX (gdaex))
{
gdacon = (GdaConnection *)gdaex_get_gdaconnection (gdaex);
parser = gda_sql_parser_new ();
}
- error == NULL;
+ error = NULL;
stmt = gda_sql_parser_parse_string (parser, op->sql, NULL, &error);
if (stmt == NULL)
{
- g_warning (_("Error on parsing sql string: %s\n%s"), op->sql,
+ g_warning (_("Error on parsing sql string: %s\n%s."), op->sql,
(error != NULL && error->message != NULL ? error->message : "no details"));
}
else
error = NULL;
if (!gda_holder_set_value (h, gval, &error))
{
- g_warning (_("Error on setting holder's value: %s"),
- (error != NULL && error->message != NULL ? error->message : "no details"));
+ g_warning (_("Error on setting holder's value: %s."),
+ (error != NULL && error->message != NULL ? error->message : "no details"));
}
}
hs = g_slist_next (hs);
error = NULL;
res = gda_connection_statement_execute (gdacon, stmt, params,
- GDA_STATEMENT_MODEL_RANDOM_ACCESS,
- NULL, &error);
+ GDA_STATEMENT_MODEL_RANDOM_ACCESS,
+ NULL, &error);
if (res == NULL)
{
- g_warning (_("Error on executing GtkFormWidgetOnChangeOp: %s"),
- (error != NULL && error->message != NULL ? error->message : "no details"));
+ g_warning (_("Error on executing GtkFormWidgetOnChangeOp: %s."),
+ (error != NULL && error->message != NULL ? error->message : "no details"));
}
- else if (GDA_IS_DATA_MODEL (res) && gda_data_model_get_n_rows ((GdaDataModel *)res) == 1)
+ else if (GDA_IS_DATA_MODEL (res) && gda_data_model_get_n_rows ((GdaDataModel *)res) > 0)
{
gtk_form_widget_set_value_stringify (gtk_form_get_form_widget_from_name (priv->form, op->form_widget_name),
- gdaex_data_model_get_value_stringify_at ((GdaDataModel *)res, 0, 0));
+ gdaex_data_model_get_value_stringify_at ((GdaDataModel *)res, 0, 0));
+ g_object_unref (res);
}
else
{
gtk_form_widget_set_value_stringify (gtk_form_get_form_widget_from_name (priv->form, op->form_widget_name), "");
}
+ g_object_unref (stmt);
}
}
else
{
GtkFormWidgetOnChangeOpUpdate *op_update = (GtkFormWidgetOnChangeOpUpdate *)op;
if (IS_GTK_FORM (priv->form)
- && (op_update->when == GTK_FORM_WIDGET_ON_CHANGE_OP_UPDATE_BEFORE
- || op_update->when == GTK_FORM_WIDGET_ON_CHANGE_OP_UPDATE_BOTH))
+ && (op_update->when == GTK_FORM_WIDGET_ON_CHANGE_OP_UPDATE_BEFORE
+ || op_update->when == GTK_FORM_WIDGET_ON_CHANGE_OP_UPDATE_BOTH))
{
execute_on_change_op (fwidget, op_update);
}
{
GtkFormWidgetOnChangeOpUpdate *op_update = (GtkFormWidgetOnChangeOpUpdate *)op;
if (IS_GTK_FORM (priv->form)
- && (op_update->when == GTK_FORM_WIDGET_ON_CHANGE_OP_UPDATE_AFTER
- || op_update->when == GTK_FORM_WIDGET_ON_CHANGE_OP_UPDATE_BOTH))
+ && (op_update->when == GTK_FORM_WIDGET_ON_CHANGE_OP_UPDATE_AFTER
+ || op_update->when == GTK_FORM_WIDGET_ON_CHANGE_OP_UPDATE_BOTH))
{
execute_on_change_op (fwidget, op_update);
}