/* * 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_param_col_name_changed (GladeProperty * property, GValue * old, GValue * new, GladeWidget * param) { gchar * name = g_value_dup_string (new); GladeProject * project; GladeWidget * parent = glade_widget_get_parent (param); if (!name || name[0] == '\0' || !parent || G_OBJECT_TYPE (glade_widget_get_object (parent)) != VN_TYPE_ITERATOR) return; project = glade_widget_get_project (param); glade_project_set_widget_name (project, param, g_strdelimit (name, "_", '-')); g_free (name); } 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", G_CALLBACK (glade_db_iterator_on_param_col_name_changed), param); vn_iterator_add_param (parent, child); } void glade_db_iterator_remove_child (GladeWidgetAdaptor * adaptor, DbIterator * parent, DbParam * child) { vn_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); } 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); } 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; } static void glade_db_iterator_on_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); 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_on_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 = vn_iterator_get_params (DB_ITERATOR (glade_widget_get_object (iterator))); GError * err = NULL; 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, NULL); return; } select = SQL_SELECT (sql_parser_parse (sql, &err)); if (!err) { SqlList * list; GList * l, * fields; GladeProject * project = glade_widget_get_project (iterator); g_object_get (select, "fields", &list, NULL); fields = sql_list_get_items (list); glade_command_push_group (_("Automatic generation of params for %s"), name); for (l = fields; l; l = l->next) { const gchar * param_name = NULL; gchar * param_new_name = NULL; GladeWidget * param; SqlSelectField * field = l->data; param = glade_command_create (glade_widget_adaptor_get_by_type (DB_TYPE_PARAM), iterator, NULL, project); param_name = sql_select_field_get_alias (field); if (!param_name) { SqlExpr * expr; g_object_get (field, "expr", &expr, NULL); if (SQL_IS_FIELD (expr)) param_name = sql_field_get_name (SQL_FIELD (expr)); } 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); glade_project_set_widget_name (project, param, param_new_name ? param_new_name : param_name); glade_widget_property_set (param, "column-name", param_name); g_free (param_new_name); } } glade_command_pop_group (); g_object_unref (list); } else { g_warning ("%s", err->message); g_error_free (err); } } void glade_db_iterator_action_activate (GladeWidgetAdaptor * adaptor, GObject * object, const gchar * action_path) { if (!g_strcmp0 (action_path, "launch-editor")) glade_db_iterator_on_launch_editor (object); else if (!g_strcmp0 (action_path, "generate-params")) 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))); }