2013-12-13 09:07:52 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 - Juan Ferrer Toribio
|
|
|
|
*
|
|
|
|
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "glade-vn.h"
|
|
|
|
#include <vn/glade/vn-iterator.h>
|
|
|
|
|
2014-02-11 12:16:39 +00:00
|
|
|
static void glade_db_iterator_on_param_col_name_changed (GladeProperty * property,
|
2013-12-13 09:07:52 +00:00
|
|
|
GValue * old, GValue * new, GladeWidget * param)
|
|
|
|
{
|
2014-02-11 12:16:39 +00:00
|
|
|
gchar * name = g_value_dup_string (new);
|
|
|
|
GladeProject * project;
|
2013-12-13 09:07:52 +00:00
|
|
|
GladeWidget * parent = glade_widget_get_parent (param);
|
|
|
|
|
2014-02-11 12:16:39 +00:00
|
|
|
if (!name || name[0] == '\0' || !parent
|
2013-12-13 09:07:52 +00:00
|
|
|
|| G_OBJECT_TYPE (glade_widget_get_object (parent)) != VN_TYPE_ITERATOR)
|
|
|
|
return;
|
|
|
|
|
2014-02-11 12:16:39 +00:00
|
|
|
project = glade_widget_get_project (param);
|
|
|
|
glade_project_set_widget_name (project, param, g_strdelimit (name, "_", '-'));
|
|
|
|
g_free (name);
|
2013-12-13 09:07:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void glade_db_iterator_add_child (GladeWidgetAdaptor * adaptor,
|
|
|
|
DbIterator * parent, DbParam * child)
|
|
|
|
{
|
|
|
|
GladeWidget * param = glade_widget_get_from_gobject (G_OBJECT (child));
|
|
|
|
GladeProperty * name_prop = glade_widget_get_property (param, "column-name");
|
|
|
|
|
|
|
|
g_signal_connect (name_prop, "value-changed",
|
2014-02-11 12:16:39 +00:00
|
|
|
G_CALLBACK (glade_db_iterator_on_param_col_name_changed), param);
|
2013-12-13 09:07:52 +00:00
|
|
|
|
2014-02-11 12:16:39 +00:00
|
|
|
vn_iterator_add_param (parent, child);
|
2013-12-13 09:07:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void glade_db_iterator_remove_child (GladeWidgetAdaptor * adaptor,
|
|
|
|
DbIterator * parent, DbParam * child)
|
|
|
|
{
|
2014-02-11 12:16:39 +00:00
|
|
|
vn_iterator_remove_param (parent, child);
|
2013-12-13 09:07:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void glade_db_iterator_replace_child (GladeWidgetAdaptor * adaptor,
|
|
|
|
DbIterator * container, DbParam * current, DbParam * new)
|
|
|
|
{
|
|
|
|
glade_db_iterator_remove_child (adaptor, container, current);
|
|
|
|
glade_db_iterator_add_child (adaptor, container, new);
|
|
|
|
}
|
|
|
|
|
2014-02-11 12:16:39 +00:00
|
|
|
GList * glade_db_iterator_get_children (GladeWidgetAdaptor * adaptor,
|
|
|
|
DbIterator * parent)
|
|
|
|
{
|
|
|
|
return vn_iterator_get_params (parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void glade_db_iterator_child_selected (GladeBaseEditor * editor,
|
|
|
|
GladeWidget * child, gpointer data)
|
|
|
|
{
|
|
|
|
glade_base_editor_add_label (editor, _("Parameter"));
|
|
|
|
glade_base_editor_add_default_properties (editor, child);
|
|
|
|
glade_base_editor_add_label (editor, _("Properties"));
|
|
|
|
glade_base_editor_add_editable (editor, child, GLADE_PAGE_GENERAL);
|
|
|
|
}
|
|
|
|
|
2013-12-13 09:07:52 +00:00
|
|
|
gboolean glade_db_iterator_add_verify (GladeWidgetAdaptor * adaptor,
|
|
|
|
DbIterator * container, DbParam * child, gboolean user_feedback)
|
|
|
|
{
|
|
|
|
if (DB_IS_PARAM (child))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (user_feedback)
|
|
|
|
{
|
|
|
|
GladeWidgetAdaptor * object_adaptor =
|
|
|
|
glade_widget_adaptor_get_by_type (DB_TYPE_PARAM);
|
|
|
|
|
|
|
|
glade_util_ui_message (glade_app_get_window ()
|
|
|
|
,GLADE_UI_INFO
|
|
|
|
,NULL
|
|
|
|
,"Only objects of type %s can be added to objects of type %s."
|
|
|
|
,glade_widget_adaptor_get_title (object_adaptor)
|
|
|
|
,glade_widget_adaptor_get_title (adaptor)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean glade_db_iterator_move_child (GladeBaseEditor * editor,
|
|
|
|
GladeWidget * parent, GladeWidget * child, gpointer data)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-02-11 12:16:39 +00:00
|
|
|
static void glade_db_iterator_on_launch_editor (GObject * iterator)
|
2013-12-13 09:07:52 +00:00
|
|
|
{
|
|
|
|
GladeBaseEditor * editor;
|
|
|
|
GladeEditable * param_editor;
|
|
|
|
GtkWidget * window;
|
|
|
|
GladeWidget * widget = glade_widget_get_from_gobject (iterator);
|
|
|
|
GladeWidgetAdaptor * adaptor = glade_widget_get_adaptor (widget);
|
|
|
|
|
|
|
|
param_editor = glade_widget_adaptor_create_editable (adaptor, GLADE_PAGE_GENERAL);
|
|
|
|
editor = glade_base_editor_new (glade_widget_get_object (widget),
|
|
|
|
param_editor, _("Parameter"), DB_TYPE_PARAM, NULL);
|
|
|
|
|
2014-02-11 12:16:39 +00:00
|
|
|
g_signal_connect (editor, "child-selected",
|
|
|
|
G_CALLBACK (glade_db_iterator_child_selected), NULL);
|
|
|
|
g_signal_connect (editor, "move-child",
|
|
|
|
G_CALLBACK (glade_db_iterator_move_child), NULL);
|
2013-12-13 09:07:52 +00:00
|
|
|
|
|
|
|
gtk_widget_show (GTK_WIDGET (editor));
|
|
|
|
window = glade_base_editor_pack_new_window (editor, _("Iterator Editor"), NULL);
|
|
|
|
gtk_widget_show (window);
|
|
|
|
}
|
|
|
|
|
2014-02-11 12:16:39 +00:00
|
|
|
static void glade_db_iterator_on_generate_params (GladeWidget * iterator)
|
2013-12-13 09:07:52 +00:00
|
|
|
{
|
|
|
|
gboolean use_file = TRUE;
|
|
|
|
gchar * sql = NULL;
|
|
|
|
gchar * message = _("The model must have \"SQL\" set and \"Use file\" set to 'No'.");
|
|
|
|
const gchar * name = glade_widget_get_name (iterator);
|
|
|
|
SqlSelect * select;
|
|
|
|
GladeWidget * model_w;
|
|
|
|
DbModel * model = NULL;
|
2014-02-11 12:16:39 +00:00
|
|
|
GList * params = vn_iterator_get_params
|
|
|
|
(DB_ITERATOR (glade_widget_get_object (iterator)));
|
2013-12-13 09:07:52 +00:00
|
|
|
|
|
|
|
if (params)
|
|
|
|
{
|
|
|
|
glade_util_ui_message (glade_app_get_window (), GLADE_UI_INFO, NULL,
|
|
|
|
_("%s can't have children before automatic generation."), name);
|
|
|
|
g_list_free (params);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
g_list_free (params);
|
|
|
|
|
|
|
|
glade_widget_property_get (iterator, "model", &model, NULL);
|
|
|
|
|
|
|
|
if (!model)
|
|
|
|
{
|
|
|
|
glade_util_ui_message (glade_app_get_window (), GLADE_UI_INFO, NULL,
|
|
|
|
_("%s must have \"Model\" set. %s"), name, message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
model_w = glade_widget_get_from_gobject (model);
|
|
|
|
glade_widget_property_get (model_w, "use-file", &use_file);
|
|
|
|
glade_widget_property_get (model_w, "sql", &sql);
|
|
|
|
|
|
|
|
if (use_file || !sql)
|
|
|
|
{
|
2014-05-29 08:37:06 +00:00
|
|
|
glade_util_ui_message (glade_app_get_window (), GLADE_UI_INFO, NULL, message, NULL);
|
2013-12-13 09:07:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
select = SQL_SELECT (sql_parser_parse (sql, NULL));
|
|
|
|
|
|
|
|
if (select && SQL_IS_SELECT (select))
|
|
|
|
{
|
|
|
|
//TODO Cambios cuando se use g_object_get (select, "fields", &fields, NULL)
|
|
|
|
GSList * l, * fields = select->expr;
|
|
|
|
GladeProject * project = glade_widget_get_project (iterator);
|
2014-02-11 12:16:39 +00:00
|
|
|
fields = g_slist_reverse (fields);
|
2013-12-13 09:07:52 +00:00
|
|
|
|
|
|
|
for (l = fields; l; l = l->next)
|
|
|
|
{
|
|
|
|
gchar * param_name, * param_new_name = NULL;
|
|
|
|
GladeWidget * param;
|
|
|
|
|
|
|
|
param = glade_command_create (glade_widget_adaptor_get_by_type (DB_TYPE_PARAM),
|
|
|
|
iterator, NULL, project);
|
|
|
|
|
2014-02-11 12:16:39 +00:00
|
|
|
param_name = sql_select_get_column_name (select, SQL_EXPR (l->data));
|
2013-12-13 09:07:52 +00:00
|
|
|
|
2014-02-11 12:16:39 +00:00
|
|
|
if (param_name)
|
|
|
|
{
|
|
|
|
if (!glade_project_available_widget_name (project, param, param_name))
|
|
|
|
param_new_name = glade_project_new_widget_name (project, param, param_name);
|
2013-12-13 09:07:52 +00:00
|
|
|
|
2014-02-11 12:16:39 +00:00
|
|
|
glade_project_set_widget_name (project, param, param_new_name ? param_new_name : param_name);
|
|
|
|
glade_widget_property_set (param, "column-name", param_name);
|
2013-12-13 09:07:52 +00:00
|
|
|
|
2014-02-11 12:16:39 +00:00
|
|
|
g_free (param_new_name);
|
|
|
|
g_free (param_name);
|
|
|
|
}
|
2013-12-13 09:07:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void glade_db_iterator_action_activate (GladeWidgetAdaptor * adaptor,
|
|
|
|
GObject * object, const gchar * action_path)
|
|
|
|
{
|
|
|
|
if (!g_strcmp0 (action_path, "launch-editor"))
|
2014-02-11 12:16:39 +00:00
|
|
|
glade_db_iterator_on_launch_editor (object);
|
2013-12-13 09:07:52 +00:00
|
|
|
else if (!g_strcmp0 (action_path, "generate-params"))
|
2014-02-11 12:16:39 +00:00
|
|
|
glade_db_iterator_on_generate_params (glade_widget_get_from_gobject (object));
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX Workaround for an error in Glade while loading child params
|
|
|
|
static GSList * correction_data = NULL;
|
|
|
|
|
|
|
|
static void clean_correction_data ()
|
|
|
|
{
|
|
|
|
GSList * l;
|
|
|
|
|
|
|
|
for (l = correction_data; l; l = l->next)
|
|
|
|
{
|
|
|
|
g_free (l->data);
|
|
|
|
}
|
|
|
|
g_slist_free (correction_data);
|
|
|
|
correction_data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void glade_db_iterator_read_widget (GladeWidgetAdaptor * adaptor,
|
|
|
|
GladeWidget * widget, GladeXmlNode * node)
|
|
|
|
{
|
|
|
|
GSList * l;
|
|
|
|
GList * n, * children;
|
|
|
|
GladeXmlNode * links_node;
|
|
|
|
|
|
|
|
if (!glade_xml_node_verify (node, GLADE_XML_TAG_WIDGET))
|
|
|
|
return;
|
|
|
|
|
|
|
|
GWA_GET_CLASS (G_TYPE_OBJECT)->read_widget (adaptor, widget, node);
|
|
|
|
|
|
|
|
if ((links_node = glade_xml_search_child (node, "child")) == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Param name correction
|
|
|
|
if (!correction_data)
|
|
|
|
return;
|
|
|
|
|
|
|
|
children = glade_widget_adaptor_get_children (adaptor,
|
|
|
|
glade_widget_get_object (widget));
|
|
|
|
|
|
|
|
for (n = children; n; n = n->next)
|
|
|
|
{
|
|
|
|
GladeWidget * w = glade_widget_get_from_gobject (G_OBJECT (n->data));
|
|
|
|
const gchar * name = glade_widget_get_name (w);
|
|
|
|
|
|
|
|
for (l = correction_data; l; l = l->next)
|
|
|
|
if (g_str_has_suffix (name, (gchar *)l->data))
|
|
|
|
glade_widget_set_name (w, (gchar *) l->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
clean_correction_data ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void glade_db_param_read_widget (GladeWidgetAdaptor * adaptor,
|
|
|
|
GladeWidget * widget, GladeXmlNode * node)
|
|
|
|
{
|
|
|
|
GladeWidget * parent;
|
|
|
|
|
|
|
|
if (!glade_xml_node_verify (node, GLADE_XML_TAG_WIDGET))
|
|
|
|
return;
|
|
|
|
|
|
|
|
GWA_GET_CLASS (G_TYPE_OBJECT)->read_widget (adaptor, widget, node);
|
|
|
|
|
|
|
|
parent = glade_widget_get_parent (widget);
|
|
|
|
if (!parent || !DB_IS_ITERATOR (glade_widget_get_object (parent)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
correction_data = g_slist_prepend (correction_data,
|
|
|
|
g_strdup (glade_widget_get_name (widget)));
|
2013-12-13 09:07:52 +00:00
|
|
|
}
|