[libgda] GdaBrowser: generalized usage of meta data to help creating data sources in data manager perspective



commit dad236c8d2e34c73db9fadc7966306abfe00d448
Author: Vivien Malerba <malerba gnome-db org>
Date:   Wed Jan 12 21:48:25 2011 +0100

    GdaBrowser: generalized usage of meta data to help creating data sources in data manager perspective

 po/POTFILES.in                              |    1 +
 tools/browser/browser-connection.c          |    4 +-
 tools/browser/data-manager/Makefile.am      |    2 +
 tools/browser/data-manager/analyser.c       |  295 +++++++++++++++++++++++++++
 tools/browser/data-manager/analyser.h       |   38 ++++
 tools/browser/data-manager/data-console.c   |   27 +++
 tools/browser/data-manager/data-widget.c    |  247 +----------------------
 tools/browser/data-manager/ui-spec-editor.c |   26 +++
 tools/browser/data-manager/ui-spec-editor.h |    3 +-
 9 files changed, 400 insertions(+), 243 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 8686417..ebc6f99 100755
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -266,6 +266,7 @@ tools/browser/common/gdaui-data-import.c
 tools/browser/common/gdaui-entry-import.c
 tools/browser/common/objects-cloud.c
 tools/browser/common/ui-formgrid.c
+tools/browser/data-manager/analyser.c
 tools/browser/data-manager/data-console.c
 tools/browser/data-manager/data-favorite-selector.c
 tools/browser/data-manager/data-manager-perspective.c
diff --git a/tools/browser/browser-connection.c b/tools/browser/browser-connection.c
index 267a238..becd1bb 100644
--- a/tools/browser/browser-connection.c
+++ b/tools/browser/browser-connection.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009 - 2010 Vivien Malerba
+ * Copyright (C) 2009 - 2011 Vivien Malerba
  *
  * This Library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public License as
@@ -1867,7 +1867,7 @@ typedef struct {
  * preferences to set the GdaHolder's plugin attribute
  */
 static gboolean
-foreach_ui_plugins_for_params (GdaSqlAnyPart *part, ParamsData *data, GError **error)
+foreach_ui_plugins_for_params (GdaSqlAnyPart *part, ParamsData *data, G_GNUC_UNUSED GError **error)
 {
 	if (part->type != GDA_SQL_ANY_EXPR)
 		return TRUE;
diff --git a/tools/browser/data-manager/Makefile.am b/tools/browser/data-manager/Makefile.am
index 865001b..2eade22 100644
--- a/tools/browser/data-manager/Makefile.am
+++ b/tools/browser/data-manager/Makefile.am
@@ -17,6 +17,8 @@ marshal.c: marshal.list $(GLIB_GENMARSHAL) marshal.h
 	$(GLIB_GENMARSHAL) $< --body --prefix=_dm_marshal > $@
 
 libperspective_la_SOURCES = \
+	analyser.c \
+	analyser.h \
 	marshal.c \
 	marshal.h \
 	data-console.h \
diff --git a/tools/browser/data-manager/analyser.c b/tools/browser/data-manager/analyser.c
new file mode 100644
index 0000000..94158d9
--- /dev/null
+++ b/tools/browser/data-manager/analyser.c
@@ -0,0 +1,295 @@
+/* 
+ * Copyright (C) 2011 The GNOME Foundation.
+ *
+ * AUTHORS:
+ *      Vivien Malerba <malerba gnome-db org>
+ *
+ * This Library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this Library; see the file COPYING.LIB.  If not,
+ * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <glib/gi18n-lib.h>
+#include "analyser.h"
+#include "../browser-window.h"
+
+static gchar *compute_fk_dependency (GdaMetaTableForeignKey *fkey, GSList *selfields, gboolean reverse,
+				     DataSource *source, xmlNodePtr *out_sourcespec);
+
+static void add_data_source_mitem_activated_cb (GtkMenuItem *mitem, DataSourceManager *mgr);
+
+GSList *
+data_manager_add_proposals_to_menu (GtkWidget *menu,
+				    DataSourceManager *mgr, DataSource *source,
+				    GtkWidget *error_attach_widget)
+{
+	g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
+	g_return_val_if_fail (IS_DATA_SOURCE (source), NULL);
+	g_return_val_if_fail (GTK_IS_WIDGET (error_attach_widget), NULL);
+
+	GtkWidget *mitem;
+	gboolean add_separator = TRUE;
+	GSList *added_list = NULL;
+
+	GdaStatement *stmt;
+	stmt = data_source_get_statement (source);
+	if (gda_statement_get_statement_type (stmt) == GDA_SQL_STATEMENT_SELECT) {
+		GSList *fields = NULL, *flist;
+		GdaSqlStatement *sql_statement;
+		BrowserConnection *bcnc;
+		GHashTable *hash; /* key = a menu string, value= 0x1 */
+		
+		g_object_get (G_OBJECT (stmt), "structure", &sql_statement, NULL);
+		
+		bcnc = data_source_manager_get_browser_cnc (mgr);
+		if (browser_connection_check_sql_statement_validify (bcnc, sql_statement, NULL))
+			fields = ((GdaSqlStatementSelect *) sql_statement->contents)->expr_list;
+		
+		hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+		for (flist = fields; flist; flist = flist->next) {
+			GdaSqlSelectField *select_field = (GdaSqlSelectField*) flist->data;
+			if (!select_field->validity_meta_table_column)
+				continue;
+			GdaMetaDbObject *dbo;
+			GdaMetaTableColumn *field;
+			dbo = select_field->validity_meta_object;
+			field = select_field->validity_meta_table_column;
+			if (dbo->obj_type == GDA_META_DB_TABLE) {
+				GdaMetaTable *mtable;
+				mtable = GDA_META_TABLE (dbo);
+				
+				GSList *fklist;
+				for (fklist = mtable->reverse_fk_list; fklist; fklist = fklist->next) {
+					GdaMetaTableForeignKey *fkey;
+					fkey = (GdaMetaTableForeignKey *) fklist->data;
+					gchar *tmp;
+					xmlNodePtr sourcespec = NULL;
+					if (fkey->meta_table->obj_type != GDA_META_DB_TABLE)
+						continue;
+					tmp = compute_fk_dependency (fkey, fields, FALSE,
+								     source, &sourcespec);
+					if (!tmp)
+						continue;
+					if (g_hash_table_lookup (hash, tmp)) {
+						g_free (tmp);
+						continue;
+					}
+					
+					if (add_separator) {
+						mitem = gtk_separator_menu_item_new ();
+						gtk_widget_show (mitem);
+						gtk_menu_shell_append (GTK_MENU_SHELL (menu), mitem);
+						add_separator = FALSE;
+					}
+					
+					mitem = gtk_menu_item_new_with_label (tmp);
+					added_list = g_slist_prepend (added_list, mitem);
+					g_object_set_data_full ((GObject*) mitem, "xml", sourcespec,
+								(GDestroyNotify) xmlFreeNode);
+					g_object_set_data_full (G_OBJECT (mitem), "attachwidget",
+								g_object_ref (error_attach_widget),
+								g_object_unref);
+					g_signal_connect (mitem, "activate",
+							  G_CALLBACK (add_data_source_mitem_activated_cb),
+							  mgr);
+					gtk_widget_show (mitem);
+					gtk_menu_shell_append (GTK_MENU_SHELL (menu), mitem);
+					g_hash_table_insert (hash, tmp, (gpointer) 0x1);
+				}
+				for (fklist = mtable->fk_list; fklist; fklist = fklist->next) {
+					GdaMetaTableForeignKey *fkey;
+					fkey = (GdaMetaTableForeignKey *) fklist->data;
+					gchar *tmp;
+					xmlNodePtr sourcespec = NULL;
+					if (fkey->depend_on->obj_type != GDA_META_DB_TABLE)
+						continue;
+					tmp = compute_fk_dependency (fkey, fields, TRUE,
+								     source, &sourcespec);
+					if (!tmp)
+						continue;
+					if (g_hash_table_lookup (hash, tmp)) {
+						g_free (tmp);
+						continue;
+					}
+					
+					if (add_separator) {
+						mitem = gtk_separator_menu_item_new ();
+						gtk_widget_show (mitem);
+						gtk_menu_shell_append (GTK_MENU_SHELL (menu), mitem);
+						add_separator = FALSE;
+					}
+					
+					mitem = gtk_menu_item_new_with_label (tmp);
+					added_list = g_slist_prepend (added_list, mitem);
+					g_object_set_data_full ((GObject*) mitem, "xml", sourcespec,
+								(GDestroyNotify) xmlFreeNode);
+					g_object_set_data_full (G_OBJECT (mitem), "attachwidget",
+								g_object_ref (error_attach_widget),
+								g_object_unref);
+					g_signal_connect (mitem, "activate",
+							  G_CALLBACK (add_data_source_mitem_activated_cb),
+							  mgr);
+					gtk_widget_show (mitem);
+					gtk_menu_shell_append (GTK_MENU_SHELL (menu), mitem);
+					g_hash_table_insert (hash, tmp, (gpointer) 0x1);
+				}
+			}
+		}
+		g_hash_table_destroy (hash);
+		gda_sql_statement_free (sql_statement);
+	}
+
+	return added_list;
+}
+
+/*
+ * Returns: a new list of #GdaSqlSelectField (which are already listed in @selfields), or %NULL
+ */
+static gchar *
+compute_fk_dependency (GdaMetaTableForeignKey *fkey, GSList *selfields, gboolean reverse,
+		       DataSource *source, xmlNodePtr *out_sourcespec)
+{
+	GString *string = NULL;
+	gint i;
+	gint *index_array;
+	GdaMetaTable *table;
+
+	*out_sourcespec = NULL;
+
+	if (reverse) {
+		table = GDA_META_TABLE (fkey->meta_table);
+		index_array = fkey->fk_cols_array;
+	}
+	else {
+		table = GDA_META_TABLE (fkey->depend_on);
+		index_array = fkey->ref_pk_cols_array;
+	}
+	for (i = 0; i < fkey->cols_nb; i++) {
+		gint pos;
+		GdaMetaTableColumn *col;
+		pos = index_array[i] - 1;
+		col = g_slist_nth_data (table->columns, pos);
+
+		/* make sure @col is among @selfields */
+		GSList *flist;
+		gboolean found = FALSE;
+		for (flist = selfields; flist; flist = flist->next) {
+			GdaSqlSelectField *select_field = (GdaSqlSelectField*) flist->data;
+			if (!select_field->validity_meta_object ||
+			    !select_field->validity_meta_table_column)
+				continue;
+			GdaMetaTableColumn *field;
+			field = select_field->validity_meta_table_column;
+			if (field == col) {
+				found = TRUE;
+				if (reverse) {
+					if (!string) {
+						string = g_string_new (_("Obtain referenced data in table "));
+						g_string_append_printf (string, "%s from ",
+									fkey->depend_on->obj_short_name);
+					}
+					else
+						g_string_append (string, ", ");
+					g_string_append_printf (string, "column n.%d (",
+								g_slist_position (selfields, flist) + 1);
+					if (select_field->as)
+						g_string_append (string, select_field->as);
+					else
+						g_string_append (string,
+								 select_field->validity_meta_table_column->column_name);
+					g_string_append_c (string, ')');
+				}
+				else {
+					if (!string) {
+						string = g_string_new (_("List referencing data in "));
+						g_string_append_printf (string, "%s.",
+									fkey->meta_table->obj_short_name);
+					}
+					else
+						g_string_append (string, ", ");
+					g_string_append (string, fkey->fk_names_array [i]);
+				}
+				break;
+			}
+		}
+
+		if (! found) {
+			if (string) {
+				g_string_free (string, TRUE);
+				string = NULL;
+			}
+			break;
+		}
+	}
+
+	if (string) {
+		xmlNodePtr node, snode;
+		node = xmlNewNode (NULL, BAD_CAST "table");
+		xmlSetProp (node, BAD_CAST "name",
+ 			    BAD_CAST (reverse ? GDA_META_DB_OBJECT (fkey->depend_on)->obj_short_name : 
+				      GDA_META_DB_OBJECT (fkey->meta_table)->obj_short_name));
+
+		snode = xmlNewChild (node, NULL, BAD_CAST "depend", NULL);
+		xmlSetProp (snode, BAD_CAST "foreign_key_table",
+			    BAD_CAST GDA_META_DB_OBJECT (table)->obj_short_name);
+
+		xmlSetProp (snode, BAD_CAST "id", BAD_CAST data_source_get_id (source));
+
+		gint i;
+		for (i = 0; i < fkey->cols_nb; i++)
+			xmlNewChild (snode, NULL, BAD_CAST "column", BAD_CAST (fkey->fk_names_array[i]));
+
+		*out_sourcespec = node;
+		return g_string_free (string, FALSE);
+	}
+	else
+		return NULL;
+}
+
+static void
+add_data_source_mitem_activated_cb (GtkMenuItem *mitem, DataSourceManager *mgr)
+{
+	DataSource *source;
+	GError *lerror = NULL;
+	xmlNodePtr sourcespec;
+	BrowserConnection *bcnc;
+
+	bcnc = data_source_manager_get_browser_cnc (mgr);
+	sourcespec = (xmlNodePtr) g_object_get_data ((GObject*) mitem, "xml");
+	source = data_source_new_from_xml_node (bcnc, sourcespec, &lerror);
+	if (source) {
+		data_source_manager_add_source (mgr, source);
+		g_object_unref (source);
+	}
+	else {
+		BrowserWindow *bwin;
+		GtkWidget *attachwidget;
+		attachwidget = g_object_get_data (G_OBJECT (mitem), "attachwidget");
+		g_assert (attachwidget);
+		bwin = BROWSER_WINDOW (gtk_widget_get_toplevel (attachwidget));
+		browser_window_show_notice_printf (bwin, GTK_MESSAGE_ERROR, "data-widget-add-new-source",
+						   _("Error adding new data source: %s"),
+						   lerror && lerror->message ? lerror->message :
+						   _("No detail"));
+		g_clear_error (&lerror);
+	}
+
+#ifdef GDA_DEBUG_NO
+	xmlBufferPtr buffer;
+	buffer = xmlBufferCreate ();
+	xmlNodeDump (buffer, NULL, sourcespec, 0, 1);
+	g_print ("Source to ADD: [%s]\n", (gchar *) xmlBufferContent (buffer));
+	xmlBufferFree (buffer);
+#endif
+}
diff --git a/tools/browser/data-manager/analyser.h b/tools/browser/data-manager/analyser.h
new file mode 100644
index 0000000..0a06318
--- /dev/null
+++ b/tools/browser/data-manager/analyser.h
@@ -0,0 +1,38 @@
+/* 
+ * Copyright (C) 2011 The GNOME Foundation.
+ *
+ * AUTHORS:
+ * 	Vivien Malerba <malerba gnome-db org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#ifndef __ANALYSER_H__
+#define __ANALYSER_H__
+
+#include <libgda/libgda.h>
+#include <gtk/gtk.h>
+#include "data-source.h"
+#include "data-source-manager.h"
+
+G_BEGIN_DECLS
+
+GSList *data_manager_add_proposals_to_menu (GtkWidget *menu,
+					    DataSourceManager *mgr, DataSource *source,
+					    GtkWidget *error_attach_widget);
+
+G_END_DECLS
+
+#endif
diff --git a/tools/browser/data-manager/data-console.c b/tools/browser/data-manager/data-console.c
index 227f7be..3a03202 100644
--- a/tools/browser/data-manager/data-console.c
+++ b/tools/browser/data-manager/data-console.c
@@ -38,6 +38,7 @@
 #include <libgda-ui/libgda-ui.h>
 #include "data-source-manager.h"
 #include <gdk/gdkkeysyms.h>
+#include "analyser.h"
 
 #define MAIN_PAGE_EDITORS 0
 #define MAIN_PAGE_DATA 1
@@ -84,6 +85,7 @@ struct _DataConsolePrivate {
 	GtkWidget *add_source_button;
 	GtkWidget *add_source_menu;
 	gpointer   add_source_menu_index; /* used to know if @add_source_menu needs to be rebuilt */
+	GSList    *customized_menu_items_list; /* list of GtkMenuItem in @add_source_menu */
 };
 
 static void data_console_class_init (DataConsoleClass *klass);
@@ -186,6 +188,8 @@ data_console_dispose (GObject *object)
 	if (dconsole->priv) {
 		if (dconsole->priv->add_source_menu)
 			gtk_widget_destroy (dconsole->priv->add_source_menu);
+		if (dconsole->priv->customized_menu_items_list)
+			g_slist_free (dconsole->priv->customized_menu_items_list);
 		if (dconsole->priv->params_form)
 			gtk_widget_destroy (dconsole->priv->params_form);
 		if (dconsole->priv->popup_container)
@@ -749,7 +753,18 @@ static void
 add_source_clicked_cb (G_GNUC_UNUSED GtkButton *button, DataConsole *dconsole)
 {
 	GdaMetaStruct *mstruct;
+	DataSource *current_source;
+
 	mstruct = browser_connection_get_meta_struct (dconsole->priv->bcnc);
+	current_source = ui_spec_editor_get_selected_source (UI_SPEC_EDITOR (dconsole->priv->ui_sped));
+
+	/* remove customization, if any */
+	if (dconsole->priv->customized_menu_items_list) {
+		g_slist_foreach (dconsole->priv->customized_menu_items_list,
+				 (GFunc) gtk_widget_destroy, NULL);
+		g_slist_free (dconsole->priv->customized_menu_items_list);
+		dconsole->priv->customized_menu_items_list = NULL;
+	}
 
 	if (dconsole->priv->add_source_menu &&
 	    (dconsole->priv->add_source_menu_index != (gpointer) mstruct)) {
@@ -758,6 +773,11 @@ add_source_clicked_cb (G_GNUC_UNUSED GtkButton *button, DataConsole *dconsole)
 		dconsole->priv->add_source_menu_index = NULL;
 	}
 	if (dconsole->priv->add_source_menu) {
+		if (current_source)
+			dconsole->priv->customized_menu_items_list =
+				data_manager_add_proposals_to_menu (dconsole->priv->add_source_menu,
+								    dconsole->priv->mgr,
+								    current_source, GTK_WIDGET (dconsole));
 		gtk_menu_popup (GTK_MENU (dconsole->priv->add_source_menu), NULL, NULL,
 			NULL, NULL, 0,
 			gtk_get_current_event_time ());
@@ -828,6 +848,13 @@ add_source_clicked_cb (G_GNUC_UNUSED GtkButton *button, DataConsole *dconsole)
 	}
 	dconsole->priv->add_source_menu_index = (gpointer) mstruct;
 	dconsole->priv->add_source_menu = menu;
+
+	if (current_source)
+		dconsole->priv->customized_menu_items_list =
+			data_manager_add_proposals_to_menu (dconsole->priv->add_source_menu,
+							    dconsole->priv->mgr,
+							    current_source, GTK_WIDGET (dconsole));
+
 	gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
 			NULL, NULL, 0,
 			gtk_get_current_event_time ());
diff --git a/tools/browser/data-manager/data-widget.c b/tools/browser/data-manager/data-widget.c
index d403ed6..40cebb8 100644
--- a/tools/browser/data-manager/data-widget.c
+++ b/tools/browser/data-manager/data-widget.c
@@ -29,6 +29,7 @@
 #include "../browser-window.h"
 #include "../support.h"
 #include "data-source-editor.h"
+#include "analyser.h"
 
 /*
  * The DataPart structure represents the execution of a single DataSource 
@@ -323,40 +324,6 @@ remove_data_source_mitem_activated_cb (G_GNUC_UNUSED GtkMenuItem *mitem, DataPar
 }
 
 static void
-add_data_source_mitem_activated_cb (GtkMenuItem *mitem, DataPart *part)
-{
-	DataSource *source;
-	GError *lerror = NULL;
-	xmlNodePtr sourcespec;
-	BrowserConnection *bcnc;
-
-	bcnc = data_source_manager_get_browser_cnc (part->dwid->priv->mgr);
-	sourcespec = (xmlNodePtr) g_object_get_data ((GObject*) mitem, "xml");
-	source = data_source_new_from_xml_node (bcnc, sourcespec, &lerror);
-	if (source) {
-		data_source_manager_add_source (part->dwid->priv->mgr, source);
-		g_object_unref (source);
-	}
-	else {
-		BrowserWindow *bwin;
-		bwin = BROWSER_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (part->dwid)));
-		browser_window_show_notice_printf (bwin, GTK_MESSAGE_ERROR, "data-widget-add-new-source",
-						   _("Error adding new data source: %s"),
-						   lerror && lerror->message ? lerror->message :
-						   _("No detail"));
-		g_clear_error (&lerror);
-	}
-
-#ifdef GDA_DEBUG_NO
-	xmlBufferPtr buffer;
-	buffer = xmlBufferCreate ();
-	xmlNodeDump (buffer, NULL, sourcespec, 0, 1);
-	g_print ("Source to ADD: [%s]\n", (gchar *) xmlBufferContent (buffer));
-	xmlBufferFree (buffer);
-#endif
-}
-
-static void
 data_source_props_activated_cb (GtkCheckMenuItem *mitem, DataPart *part)
 {
 	if (gtk_check_menu_item_get_active (mitem)) {
@@ -376,14 +343,12 @@ data_source_props_activated_cb (GtkCheckMenuItem *mitem, DataPart *part)
 		gtk_notebook_set_current_page (part->nb, part->edit_widget_previous_page);
 }
 
-static gchar *compute_fk_dependency (GdaMetaTableForeignKey *fkey, GSList *selfields, gboolean reverse,
-				     DataPart *part, xmlNodePtr *out_sourcespec);
 static void
 data_source_menu_clicked_cb (G_GNUC_UNUSED GtkButton *button, DataPart *part)
 {
 	if (! part->menu) {
 		GtkWidget *menu, *mitem;
-		gboolean add_separator = TRUE;
+		GSList *added_list;
 		menu = gtk_menu_new ();
 		part->menu = menu;
 
@@ -399,215 +364,17 @@ data_source_menu_clicked_cb (G_GNUC_UNUSED GtkButton *button, DataPart *part)
 		gtk_widget_show (mitem);
 		gtk_menu_shell_append (GTK_MENU_SHELL (menu), mitem);
 
-		GdaStatement *stmt;
-		stmt = data_source_get_statement (part->source);
-		if (gda_statement_get_statement_type (stmt) == GDA_SQL_STATEMENT_SELECT) {
-			GSList *fields = NULL, *flist;
-			GdaSqlStatement *sql_statement;
-			BrowserConnection *bcnc;
-			GHashTable *hash; /* key = a menu string, value= 0x1 */
-
-			g_object_get (G_OBJECT (stmt), "structure", &sql_statement, NULL);
-
-			bcnc = data_source_manager_get_browser_cnc (part->dwid->priv->mgr);
-			if (browser_connection_check_sql_statement_validify (bcnc, sql_statement, NULL))
-				fields = ((GdaSqlStatementSelect *) sql_statement->contents)->expr_list;
-
-			hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
-			for (flist = fields; flist; flist = flist->next) {
-				GdaSqlSelectField *select_field = (GdaSqlSelectField*) flist->data;
-				if (!select_field->validity_meta_table_column)
-					continue;
-				GdaMetaDbObject *dbo;
-				GdaMetaTableColumn *field;
-				dbo = select_field->validity_meta_object;
-				field = select_field->validity_meta_table_column;
-				if (dbo->obj_type == GDA_META_DB_TABLE) {
-					GdaMetaTable *mtable;
-					mtable = GDA_META_TABLE (dbo);
-
-					GSList *fklist;
-					for (fklist = mtable->reverse_fk_list; fklist; fklist = fklist->next) {
-						GdaMetaTableForeignKey *fkey;
-						fkey = (GdaMetaTableForeignKey *) fklist->data;
-						gchar *tmp;
-						xmlNodePtr sourcespec = NULL;
-						if (fkey->meta_table->obj_type != GDA_META_DB_TABLE)
-							continue;
-						tmp = compute_fk_dependency (fkey, fields, FALSE,
-									     part, &sourcespec);
-						if (!tmp)
-							continue;
-						if (g_hash_table_lookup (hash, tmp)) {
-							g_free (tmp);
-							continue;
-						}
-
-						if (add_separator) {
-							mitem = gtk_separator_menu_item_new ();
-							gtk_widget_show (mitem);
-							gtk_menu_shell_append (GTK_MENU_SHELL (menu), mitem);
-							add_separator = FALSE;
-						}
-
-						mitem = gtk_menu_item_new_with_label (tmp);
-						g_object_set_data_full ((GObject*) mitem, "xml", sourcespec,
-									(GDestroyNotify) xmlFreeNode);
-						g_signal_connect (mitem, "activate",
-								  G_CALLBACK (add_data_source_mitem_activated_cb),
-								  part);
-						gtk_widget_show (mitem);
-						gtk_menu_shell_append (GTK_MENU_SHELL (menu), mitem);
-						g_hash_table_insert (hash, tmp, (gpointer) 0x1);
-					}
-					for (fklist = mtable->fk_list; fklist; fklist = fklist->next) {
-						GdaMetaTableForeignKey *fkey;
-						fkey = (GdaMetaTableForeignKey *) fklist->data;
-						gchar *tmp;
-						xmlNodePtr sourcespec = NULL;
-						if (fkey->depend_on->obj_type != GDA_META_DB_TABLE)
-							continue;
-						tmp = compute_fk_dependency (fkey, fields, TRUE,
-									     part, &sourcespec);
-						if (!tmp)
-							continue;
-						if (g_hash_table_lookup (hash, tmp)) {
-							g_free (tmp);
-							continue;
-						}
-
-						if (add_separator) {
-							mitem = gtk_separator_menu_item_new ();
-							gtk_widget_show (mitem);
-							gtk_menu_shell_append (GTK_MENU_SHELL (menu), mitem);
-							add_separator = FALSE;
-						}
-
-						mitem = gtk_menu_item_new_with_label (tmp);
-						g_object_set_data_full ((GObject*) mitem, "xml", sourcespec,
-									(GDestroyNotify) xmlFreeNode);
-						g_signal_connect (mitem, "activate",
-								  G_CALLBACK (add_data_source_mitem_activated_cb),
-								  part);
-						gtk_widget_show (mitem);
-						gtk_menu_shell_append (GTK_MENU_SHELL (menu), mitem);
-						g_hash_table_insert (hash, tmp, (gpointer) 0x1);
-					}
-				}
-			}
-			g_hash_table_destroy (hash);
-			gda_sql_statement_free (sql_statement);
-		}
+		added_list = data_manager_add_proposals_to_menu (menu, part->dwid->priv->mgr,
+								 part->source, GTK_WIDGET (part->dwid));
+		if (added_list)
+			g_slist_free (added_list);
 	}
+
 	gtk_menu_popup (GTK_MENU (part->menu), NULL, NULL,
                         NULL, NULL, 0,
                         gtk_get_current_event_time ());
 }
 
-/*
- * Returns: a new list of #GdaSqlSelectField (which are already listed in @selfields), or %NULL
- */
-static gchar *
-compute_fk_dependency (GdaMetaTableForeignKey *fkey, GSList *selfields, gboolean reverse,
-		       DataPart *part, xmlNodePtr *out_sourcespec)
-{
-	GString *string = NULL;
-	gint i;
-	gint *index_array;
-	GdaMetaTable *table;
-
-	*out_sourcespec = NULL;
-
-	if (reverse) {
-		table = GDA_META_TABLE (fkey->meta_table);
-		index_array = fkey->fk_cols_array;
-	}
-	else {
-		table = GDA_META_TABLE (fkey->depend_on);
-		index_array = fkey->ref_pk_cols_array;
-	}
-	for (i = 0; i < fkey->cols_nb; i++) {
-		gint pos;
-		GdaMetaTableColumn *col;
-		pos = index_array[i] - 1;
-		col = g_slist_nth_data (table->columns, pos);
-
-		/* make sure @col is among @selfields */
-		GSList *flist;
-		gboolean found = FALSE;
-		for (flist = selfields; flist; flist = flist->next) {
-			GdaSqlSelectField *select_field = (GdaSqlSelectField*) flist->data;
-			if (!select_field->validity_meta_object ||
-			    !select_field->validity_meta_table_column)
-				continue;
-			GdaMetaTableColumn *field;
-			field = select_field->validity_meta_table_column;
-			if (field == col) {
-				found = TRUE;
-				if (reverse) {
-					if (!string) {
-						string = g_string_new (_("Obtain referenced data in table "));
-						g_string_append_printf (string, "%s from ",
-									fkey->depend_on->obj_short_name);
-					}
-					else
-						g_string_append (string, ", ");
-					g_string_append_printf (string, "column n.%d (",
-								g_slist_position (selfields, flist) + 1);
-					if (select_field->as)
-						g_string_append (string, select_field->as);
-					else
-						g_string_append (string,
-								 select_field->validity_meta_table_column->column_name);
-					g_string_append_c (string, ')');
-				}
-				else {
-					if (!string) {
-						string = g_string_new (_("List referencing data in "));
-						g_string_append_printf (string, "%s.",
-									fkey->meta_table->obj_short_name);
-					}
-					else
-						g_string_append (string, ", ");
-					g_string_append (string, fkey->fk_names_array [i]);
-				}
-				break;
-			}
-		}
-
-		if (! found) {
-			if (string) {
-				g_string_free (string, TRUE);
-				string = NULL;
-			}
-			break;
-		}
-	}
-
-	if (string) {
-		xmlNodePtr node, snode;
-		node = xmlNewNode (NULL, BAD_CAST "table");
-		xmlSetProp (node, BAD_CAST "name",
- 			    BAD_CAST (reverse ? GDA_META_DB_OBJECT (fkey->depend_on)->obj_short_name : 
-				      GDA_META_DB_OBJECT (fkey->meta_table)->obj_short_name));
-
-		snode = xmlNewChild (node, NULL, BAD_CAST "depend", NULL);
-		xmlSetProp (snode, BAD_CAST "foreign_key_table",
-			    BAD_CAST GDA_META_DB_OBJECT (table)->obj_short_name);
-
-		xmlSetProp (snode, BAD_CAST "id", BAD_CAST data_source_get_id (part->source));
-
-		gint i;
-		for (i = 0; i < fkey->cols_nb; i++)
-			xmlNewChild (snode, NULL, BAD_CAST "column", BAD_CAST (fkey->fk_names_array[i]));
-
-		*out_sourcespec = node;
-		return g_string_free (string, FALSE);
-	}
-	else
-		return NULL;
-}
-
 static void
 update_layout (DataWidget *dwid)
 {
diff --git a/tools/browser/data-manager/ui-spec-editor.c b/tools/browser/data-manager/ui-spec-editor.c
index c017767..559a761 100644
--- a/tools/browser/data-manager/ui-spec-editor.c
+++ b/tools/browser/data-manager/ui-spec-editor.c
@@ -490,3 +490,29 @@ ui_spec_editor_select_source (UiSpecEditor *editor, DataSource *source)
 		}
 	}
 }
+
+/**
+ * ui_spec_editor_get_selected_source
+ * @sped: a #UiSpecEditor widget
+ *
+ * Get the currently selected data source for edition
+ *
+ * Returns: (transfer none): the #DataSource, or %NULL
+ */
+DataSource *
+ui_spec_editor_get_selected_source (UiSpecEditor *sped)
+{
+	g_return_val_if_fail (IS_UI_SPEC_EDITOR (sped), NULL);
+	GtkTreeModel *model;
+	GtkTreeSelection *select;
+	GtkTreeIter iter;
+
+	select = gtk_tree_view_get_selection (sped->priv->sources_tree);
+	if (gtk_tree_selection_get_selected (select, &model, &iter)) {
+		DataSource *source;
+		gtk_tree_model_get (model, &iter, COLUMN_DATA_SOURCE, &source, -1);
+		return source;
+	}
+	else
+		return NULL;
+}
diff --git a/tools/browser/data-manager/ui-spec-editor.h b/tools/browser/data-manager/ui-spec-editor.h
index 4fd6bc1..cd3d837 100644
--- a/tools/browser/data-manager/ui-spec-editor.h
+++ b/tools/browser/data-manager/ui-spec-editor.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010 The GNOME Foundation.
+ * Copyright (C) 2010 - 2011 The GNOME Foundation.
  *
  * AUTHORS:
  *      Vivien Malerba <malerba gnome-db org>
@@ -52,6 +52,7 @@ struct _UiSpecEditorClass {
 GType          ui_spec_editor_get_type      (void) G_GNUC_CONST;
 GtkWidget     *ui_spec_editor_new           (DataSourceManager *mgr);
 void           ui_spec_editor_select_source (UiSpecEditor *editor, DataSource *source);
+DataSource    *ui_spec_editor_get_selected_source (UiSpecEditor *editor);
 
 G_END_DECLS
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]