/*
* 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 .
*/
#include "glade-vn.h"
#include
static void glade_db_iterator_on_name_changed (GladeProject * project,
GladeWidget * widget)
{
const gchar * new_name;
gchar * prefix, * old_name;
GList * n, * list;
if (G_OBJECT_TYPE (glade_widget_get_object (widget)) != VN_TYPE_ITERATOR)
return;
old_name = g_object_get_data (G_OBJECT (widget), "old-name");
prefix = g_strconcat (old_name, "-", NULL);
new_name = glade_widget_get_name (widget);
g_object_set_data_full (G_OBJECT (widget), "old-name",
g_strdup (new_name), (GDestroyNotify) g_free);
list = db_iterator_get_params (DB_ITERATOR (glade_widget_get_object (widget)));
for (n = list; n; n = n->next)
{
GladeWidget * param = glade_widget_get_from_gobject (G_OBJECT (n->data));
const gchar * child_name = glade_widget_get_name (param);
if (g_str_has_prefix (child_name, prefix))
{
glong offset = g_utf8_strlen (prefix, -1);
gchar * new_child_name = g_strconcat (new_name, "-", child_name + offset, NULL);
glade_project_set_widget_name (project, param, new_child_name);
g_free (new_child_name);
}
}
g_list_free (list);
g_free (prefix);
}
void glade_db_iterator_post_create (GladeWidgetAdaptor * adaptor,
DbIterator * iterator, GladeCreateReason * reason)
{
static gboolean connected = FALSE;
GladeWidget * widget = glade_widget_get_from_gobject (G_OBJECT (iterator));
GladeProject * project = glade_widget_get_project (widget);
g_object_set_data_full (G_OBJECT (widget), "old-name",
g_strdup (glade_widget_get_name (widget)), (GDestroyNotify) g_free);
if (!connected)
{
g_signal_connect (project, "widget-name-changed",
G_CALLBACK (glade_db_iterator_on_name_changed), NULL);
connected = TRUE;
}
}
static void glade_db_iterator_on_column_name_changed (GladeProperty * property,
GValue * old, GValue * new, GladeWidget * param)
{
const gchar * parent_name;
gchar * prefix;
GladeWidget * parent = glade_widget_get_parent (param);
if (!parent
|| G_OBJECT_TYPE (glade_widget_get_object (parent)) != VN_TYPE_ITERATOR)
return;
parent_name = glade_widget_get_name (parent);
prefix = g_strconcat (parent_name, "-", NULL);
if (g_str_has_prefix (glade_widget_get_name (param), prefix))
{
GladeProject * project = glade_widget_get_project (param);
gchar * new_name = g_strconcat (prefix, g_value_get_string (new), NULL);
glade_project_set_widget_name (project, param, new_name);
}
g_free (prefix);
}
void glade_db_iterator_add_child (GladeWidgetAdaptor * adaptor,
DbIterator * parent, DbParam * child)
{
gchar * new_name;
const gchar * param_name, * parent_name;
GladeWidget * param = glade_widget_get_from_gobject (G_OBJECT (child));
GladeProperty * name_prop = glade_widget_get_property (param, "column-name");
param_name = glade_widget_get_name (param);
parent_name = glade_widget_get_name (glade_widget_get_from_gobject (G_OBJECT (parent)));
new_name = g_strconcat (parent_name, "-",param_name, NULL);
glade_project_set_widget_name (glade_widget_get_project (param), param, new_name);
g_free (new_name);
g_signal_connect (name_prop, "value-changed",
G_CALLBACK (glade_db_iterator_on_column_name_changed), param);
db_iterator_add_param (parent, child);
}
void glade_db_iterator_remove_child (GladeWidgetAdaptor * adaptor,
DbIterator * parent, DbParam * child)
{
// db_iterator_remove_param (parent, child);
}
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);
}
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;
}
GList * glade_db_iterator_get_children (GladeWidgetAdaptor * adaptor,
DbIterator * parent)
{
return db_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);
}
static gboolean glade_db_iterator_move_child (GladeBaseEditor * editor,
GladeWidget * parent, GladeWidget * child, gpointer data)
{
return FALSE;
}
static void glade_db_iterator_launch_editor (GObject * iterator)
{
GladeBaseEditor * editor;
GladeEditable * param_editor;
GtkWidget * window;
GladeWidget * widget = glade_widget_get_from_gobject (iterator);
GladeWidgetAdaptor * adaptor = glade_widget_get_adaptor (widget);
widget = glade_widget_get_toplevel (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);
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);
gtk_widget_show (GTK_WIDGET (editor));
window = glade_base_editor_pack_new_window (editor, _("Iterator Editor"), NULL);
gtk_widget_show (window);
}
static void glade_db_iterator_generate_params (GladeWidget * iterator)
{
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;
GList * params = db_iterator_get_params (DB_ITERATOR (glade_widget_get_object (iterator)));
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)
{
glade_util_ui_message (glade_app_get_window (), GLADE_UI_INFO, NULL, message);
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);
for (l = fields; l; l = l->next)
{
gchar * column_name;
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);
column_name = sql_select_get_column_name (select, SQL_EXPR (l->data));
param_name = g_strconcat (name, "-"
,column_name ? column_name : "param"
,NULL);
if (!glade_project_available_widget_name (project, param, param_name))
param_new_name = glade_project_new_widget_name (project, param, param_name);
glade_project_set_widget_name (project, param, param_new_name ? param_new_name : param_name);
glade_widget_property_set (param, "column-name", column_name);
g_free (column_name);
g_free (param_new_name);
g_free (param_name);
}
}
}
void glade_db_iterator_action_activate (GladeWidgetAdaptor * adaptor,
GObject * object, const gchar * action_path)
{
if (!g_strcmp0 (action_path, "launch-editor"))
glade_db_iterator_launch_editor (object);
else if (!g_strcmp0 (action_path, "generate-params"))
glade_db_iterator_generate_params (glade_widget_get_from_gobject (object));
}