1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
/* mg-util.c
* Copyright (C) 2004 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; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "mg-defs.h"
#include "mg-util.h"
#include <libgda/libgda.h>
#include "mg-server.h"
#include "mg-query.h"
#include "mg-renderer.h"
#include "mg-entity.h"
/**
* mg_util_query_execute_modif
* @query: the #MgQuery to be executed
* @context: a #MgContext object
* @ask_confirm_insert:
* @ask_confirm_update:
* @ask_confirm_delete:
* @parent_window: a #GtkWindow object, or a #GtkWidget and the parent window will be found automatically
* @user_cancelled: a place to store if the user cancelled the query if the choice was given, or %NULL
* @query_error: a place to store if there was an error, or %NULL
*
* Executes @query and displays question and information dialogs if necessary.
* If a user confirmation was required and the user cancelled the execution, then
* the returned value is FALSE.
*
* Returns: TRUE if the query was executed.
*/
gboolean
mg_util_query_execute_modif (MgQuery *query, MgContext *context,
gboolean ask_confirm_insert,
gboolean ask_confirm_update,
gboolean ask_confirm_delete,
GtkWidget *parent_window,
gboolean *user_cancelled,
gboolean *query_error)
{
gchar *sql = NULL;
MgQueryType qtype;
gchar *confirm = NULL;
gboolean do_execute = TRUE;
gboolean allok = TRUE;
GError *error = NULL;
g_return_val_if_fail (query && IS_MG_QUERY (query), FALSE);
/* find the GtkWindow object for @parent_window */
while (parent_window && !GTK_IS_WINDOW (parent_window))
parent_window = gtk_widget_get_parent (parent_window);
sql = mg_renderer_render_as_sql (MG_RENDERER (query), context, 0, &error);
qtype = mg_query_get_query_type (query);
switch (qtype) {
case MG_QUERY_TYPE_INSERT:
if (ask_confirm_insert)
confirm = _("Execute the following insertion query ?");
break;
case MG_QUERY_TYPE_UPDATE:
if (ask_confirm_update)
confirm = _("Execute the following update query ?");
break;
case MG_QUERY_TYPE_DELETE:
if (ask_confirm_delete)
confirm = _("Execute the following deletion query ?");
break;
default:
g_assert_not_reached ();
}
if (user_cancelled)
*user_cancelled = FALSE;
if (query_error)
*query_error = FALSE;
if (sql) {
if (confirm) {
GtkWidget *dlg;
gint result;
gchar *msg;
msg = g_strdup_printf (_("<b><big>%s</big></b>\n"
"<small>The preferences require a confirmation for the "
"following query</small>\n\n%s"), confirm, sql);
dlg = gtk_message_dialog_new (GTK_WINDOW (parent_window), 0,
GTK_MESSAGE_QUESTION,
GTK_BUTTONS_YES_NO, msg);
g_free (msg);
gtk_label_set_use_markup (GTK_LABEL (GTK_MESSAGE_DIALOG (dlg)->label), TRUE);
result = gtk_dialog_run (GTK_DIALOG (dlg));
gtk_widget_destroy (dlg);
do_execute = (result == GTK_RESPONSE_YES);
if (user_cancelled)
*user_cancelled = !do_execute;
}
if (do_execute) {
MgConf *conf = mg_base_get_conf (MG_BASE (query));
#ifdef debug
g_print ("MODIF SQL: %s\n", sql);
#endif
mg_server_do_query (mg_conf_get_server (conf), sql, MG_SERVER_QUERY_SQL, &error);
if (error) {
GtkWidget *dlg;
gchar *message;
message = g_strdup (error->message);
g_error_free (error);
dlg = gtk_message_dialog_new (GTK_WINDOW (parent_window), 0,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
message);
g_free (message);
gtk_dialog_run (GTK_DIALOG (dlg));
gtk_widget_destroy (dlg);
allok = FALSE;
if (query_error)
*query_error = TRUE;
}
}
else
allok = FALSE;
g_free (sql);
}
else {
GtkWidget *dlg;
gchar *message;
if (error) {
message = g_strdup_printf (_("The following error occurred while preparing the query:\n%s"),
error->message);
g_error_free (error);
}
else
message = g_strdup_printf (_("An unknown error occurred while preparing the query."));
dlg = gtk_message_dialog_new (GTK_WINDOW (parent_window), 0,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
message);
g_free (message);
gtk_dialog_run (GTK_DIALOG (dlg));
gtk_widget_destroy (dlg);
allok = FALSE;
if (query_error)
*query_error = TRUE;
}
return allok;
}
|