]> saetta.ns0.it Git - libgtkform/commitdiff
Added multiple db connections in a single form.
authorAndrea Zagli <azagli@libero.it>
Mon, 23 May 2011 14:48:42 +0000 (16:48 +0200)
committerAndrea Zagli <azagli@libero.it>
Mon, 23 May 2011 14:48:42 +0000 (16:48 +0200)
Added silent compilation.

configure.ac
data/gtkform.dtd
src/form.c
src/form.h
src/widget.c
src/widget.h

index 10699cc0b9af993660e74d59da448b4db2cc04f8..cb2f1b8c1542c85996762ab1ff80a24b227650d1 100644 (file)
@@ -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
index caac1654a842a949c859dd26093ff4b18a723c3a..c0db411a1240275d0d841ef0b1ec93cb9cf44c19 100644 (file)
@@ -1,9 +1,17 @@
-<!ELEMENT gtkform (gtkbuilder-file?, table?, widget+, group*)>
+<!ELEMENT gtkform (gtkbuilder-file?, table?, db-connection*, widget+, group*)>
 
 <!ELEMENT gtkbuilder-file (#PCDATA)>
 
 <!ELEMENT table (#PCDATA)>
 
+<!ELEMENT db-connection (connection-string)>
+
+<!ATTLIST db-connection
+       name  CDATA  #REQUIRED
+>
+
+<!ELEMENT connection-string (#PCDATA)>
+
 <!ELEMENT widget (column-field?, sql?, return-value?, on-change*, field?)>
 
 <!ATTLIST widget
@@ -22,7 +30,7 @@
 
 <!ELEMENT return-value (#PCDATA)>
 
-<!ELEMENT on-change (widget-name?, sql, when?)>
+<!ELEMENT on-change (widget-name?, connection-name?, sql, when?)>
 
 <!ATTLIST on-change
        what (update) #REQUIRED
index 1c341c20b2d8c3a2fe7f1d2a2d37aad59c9edab7..d3908b5d5f25e20b034922ef6298e565535e5451 100644 (file)
@@ -67,11 +67,19 @@ enum
 
 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,
@@ -103,7 +111,9 @@ struct _GtkFormPrivate
                GList *modules;
                GdaEx *gdaex;
 
-               GSList *groups;
+               GSList *groups; /*GtkFormGroup */
+
+               GSList *db_connections; /* GtkFormDbConnection */
 
                guint debug;
                GFileOutputStream *log_file;
@@ -158,6 +168,7 @@ gtk_form_init (GtkForm *form)
        priv->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,
index 931f5a43d0959ac17d9ac1940155880d3fdf6b47..c046be6d41ad41737d71f57f5c0dd549b2637690 100644 (file)
@@ -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
 
 
index 8ae68073e8bdaf717e46044a7f90a18987023838..ddd4cc0dda9b43707f00e59210da905743b7b9b7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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
@@ -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);
                                                                                }
index 052284dfcbc6988223d08963dab7106a26951e9f..e2462eb989eba0852dccb01ec1a773198a8ae6d8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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
@@ -97,6 +97,7 @@ typedef struct
 {
        GtkFormWidgetOnChangeOp op;
        gchar *form_widget_name;
+       gchar *connection_name;
        gchar *sql;
        GtkFormWidgetOnChangeOpUpdateWhen when;
 } GtkFormWidgetOnChangeOpUpdate;