From 53569a29ee0b112a83b430a6b661c57a1d5d7791 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Mon, 23 May 2011 16:48:42 +0200 Subject: [PATCH] Added multiple db connections in a single form. Added silent compilation. --- configure.ac | 2 + data/gtkform.dtd | 12 ++- src/form.c | 196 +++++++++++++++++++++++++++++++++++++++++++++-- src/form.h | 3 + src/widget.c | 44 +++++++---- src/widget.h | 3 +- 6 files changed, 234 insertions(+), 26 deletions(-) diff --git a/configure.ac b/configure.ac index 10699cc..cb2f1b8 100644 --- a/configure.ac +++ b/configure.ac @@ -9,6 +9,8 @@ AC_CONFIG_HEADER([config.h]) AM_INIT_AUTOMAKE AM_MAINTAINER_MODE +m4_ifdef([AM_SILENT_RULES],[AM_SILENT_RULES([yes])]) + AC_CONFIG_MACRO_DIR([m4]) AC_CANONICAL_SYSTEM diff --git a/data/gtkform.dtd b/data/gtkform.dtd index caac165..c0db411 100644 --- a/data/gtkform.dtd +++ b/data/gtkform.dtd @@ -1,9 +1,17 @@ - + + + + + + + - + key = NULL; priv->gdaex = NULL; priv->groups = NULL; + priv->db_connections = NULL; } /** @@ -335,6 +346,7 @@ static void parse_widget_on_change (GtkForm *form, xmlNodePtr xnode, GtkFormWidget *fwidget) { gchar *what; + gchar *connection_name; gchar *widget_name; gchar *sql; @@ -353,6 +365,10 @@ parse_widget_on_change (GtkForm *form, xmlNodePtr xnode, GtkFormWidget *fwidget) { 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); @@ -362,20 +378,24 @@ parse_widget_on_change (GtkForm *form, xmlNodePtr xnode, GtkFormWidget *fwidget) } 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; @@ -388,7 +408,7 @@ parse_widget_on_change (GtkForm *form, xmlNodePtr xnode, GtkFormWidget *fwidget) { op->when = GTK_FORM_WIDGET_ON_CHANGE_OP_UPDATE_BOTH; } - + gtk_form_widget_add_on_change_op (fwidget, (GtkFormWidgetOnChangeOp *)op); } } @@ -442,6 +462,34 @@ parse_group (GtkForm *form, xmlNodePtr xnode) } } +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: @@ -844,6 +892,10 @@ gtk_form_load_from_xml (GtkForm *form, xmlDoc *xmldoc, GtkBuilder *gtkbuilder) { parse_group (form, cur); } + else if (xmlStrcmp (cur->name, (const xmlChar *)"db_connection") == 0) + { + parse_db_connection (form, cur); + } cur = cur->next; } @@ -2340,6 +2392,136 @@ GtkFormGroup 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, diff --git a/src/form.h b/src/form.h index 931f5a4..c046be6 100644 --- a/src/form.h +++ b/src/form.h @@ -135,6 +135,9 @@ gboolean gtk_form_fill_from_hashtable (GtkForm *form, GHashTable *hashtable); gboolean gtk_form_add_group (GtkForm *form, GtkFormGroup *group); GtkFormGroup *gtk_form_get_group_by_name (GtkForm *form, const gchar *group_name); +gboolean gtk_form_add_db_connection (GtkForm *form, const gchar *name, const gchar *connection_string); +GdaEx *gtk_form_get_db_connection_by_name (GtkForm *form, const gchar *name); + G_END_DECLS diff --git a/src/widget.c b/src/widget.c index 8ae6807..ddd4cc0 100644 --- a/src/widget.c +++ b/src/widget.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2010 Andrea Zagli + * Copyright (C) 2005-2011 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -305,7 +305,17 @@ execute_on_change_op (GtkFormWidget *fwidget, GtkFormWidgetOnChangeOpUpdate *op) 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); @@ -315,11 +325,11 @@ execute_on_change_op (GtkFormWidget *fwidget, GtkFormWidgetOnChangeOpUpdate *op) 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 @@ -339,8 +349,8 @@ execute_on_change_op (GtkFormWidget *fwidget, GtkFormWidgetOnChangeOpUpdate *op) 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); @@ -349,23 +359,25 @@ execute_on_change_op (GtkFormWidget *fwidget, GtkFormWidgetOnChangeOpUpdate *op) 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 @@ -403,8 +415,8 @@ gtk_form_widget_set_value_stringify (GtkFormWidget *fwidget, const gchar *value) { 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); } @@ -432,8 +444,8 @@ gtk_form_widget_set_value_stringify (GtkFormWidget *fwidget, const gchar *value) { 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); } diff --git a/src/widget.h b/src/widget.h index 052284d..e2462eb 100644 --- a/src/widget.h +++ b/src/widget.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2010 Andrea Zagli + * Copyright (C) 2005-2011 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -97,6 +97,7 @@ typedef struct { GtkFormWidgetOnChangeOp op; gchar *form_widget_name; + gchar *connection_name; gchar *sql; GtkFormWidgetOnChangeOpUpdateWhen when; } GtkFormWidgetOnChangeOpUpdate; -- 2.49.0