From ad15344e398191ae63bde380b865c57c286e2732 Mon Sep 17 00:00:00 2001 From: Steven Rostedt Date: Mon, 14 Jun 2010 20:51:50 -0400 Subject: kernelshark: Have file dialog ask if we want to overwrite If someone selects a file that already exists and the command will be used to overwrite the file. Ask the user if they want to overwrite it instead of just doing so. Signed-off-by: Steven Rostedt --- kernel-shark.c | 6 +++--- trace-capture.c | 2 +- trace-dialog.c | 50 +++++++++++++++++++++++++++++++++++++++++++------- trace-graph-main.c | 6 +++--- trace-gui.h | 8 +++++--- trace-view-main.c | 6 +++--- 6 files changed, 58 insertions(+), 20 deletions(-) diff --git a/kernel-shark.c b/kernel-shark.c index 7c0f816..e97d612 100644 --- a/kernel-shark.c +++ b/kernel-shark.c @@ -290,7 +290,7 @@ load_clicked (gpointer data) struct tracecmd_input *handle; gchar *filename; - filename = trace_get_file_dialog("Load File"); + filename = trace_get_file_dialog("Load File", NULL, FALSE); if (!filename) return; @@ -321,7 +321,7 @@ load_filters_clicked (gpointer data) gchar *filename; int ret; - filename = trace_get_file_dialog("Load Filters"); + filename = trace_get_file_dialog("Load Filters", NULL, FALSE); if (!filename) return; @@ -407,7 +407,7 @@ save_filters_clicked (gpointer data) struct filter_task *hide_tasks; gchar *filename; - filename = trace_get_file_dialog("Save Filters"); + filename = trace_get_file_dialog("Save Filters", "Save", TRUE); if (!filename) return; diff --git a/trace-capture.c b/trace-capture.c index fda61fc..3fa5a1e 100644 --- a/trace-capture.c +++ b/trace-capture.c @@ -554,7 +554,7 @@ file_clicked (GtkWidget *widget, gpointer data) struct trace_capture *cap = data; gchar *filename; - filename = trace_get_file_dialog("Trace File"); + filename = trace_get_file_dialog("Trace File", "Save", TRUE); if (!filename) return; diff --git a/trace-dialog.c b/trace-dialog.c index 4fab040..4bddd08 100644 --- a/trace-dialog.c +++ b/trace-dialog.c @@ -250,13 +250,15 @@ void trace_show_help(GtkWidget *window, const gchar *link, GError **error) #endif } -void trace_dialog(GtkWindow *parent, enum trace_dialog_type type, - gchar *message, ...) +GtkResponseType trace_dialog(GtkWindow *parent, enum trace_dialog_type type, + gchar *message, ...) { GtkWidget *dialog; GtkMessageType mtype; + GtkButtonsType btype = GTK_BUTTONS_CLOSE; gchar *str; va_list ap; + int result; switch (type) { case TRACE_GUI_INFO: @@ -268,6 +270,10 @@ void trace_dialog(GtkWindow *parent, enum trace_dialog_type type, case TRACE_GUI_ERROR: mtype = GTK_MESSAGE_ERROR; break; + case TRACE_GUI_ASK: + mtype = GTK_MESSAGE_WARNING; + btype = GTK_BUTTONS_YES_NO; + break; } va_start(ap, message); @@ -277,26 +283,56 @@ void trace_dialog(GtkWindow *parent, enum trace_dialog_type type, dialog = gtk_message_dialog_new(parent, GTK_DIALOG_DESTROY_WITH_PARENT, mtype, - GTK_BUTTONS_CLOSE, + btype, "%s", str); g_free(str); - gtk_dialog_run(GTK_DIALOG(dialog)); + result = gtk_dialog_run(GTK_DIALOG(dialog)); gtk_widget_destroy(dialog); + + return result; } -gchar *trace_get_file_dialog(const gchar *title) +/** + * trace_get_file_dialog - pop up a file dialog to get a file + * @title: the title of the dialog + * @open: the text for the "open" button (NULL for default) + * @warn: if the file exists, warn and let them choose again. + * + * Returns: the filename if it should be used. NULL otherwise. + * The filename needs to be freed with g_free(). + */ +gchar *trace_get_file_dialog(const gchar *title, const char *open, + gboolean warn) { GtkWidget *dialog; + GtkResponseType ret; gchar *filename = NULL; + if (!open) + open = GTK_STOCK_OPEN; + dialog = gtk_file_chooser_dialog_new(title, NULL, GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, - GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, + open, GTK_RESPONSE_ACCEPT, NULL); - if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) + + again: + if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); + if (warn) { + ret = trace_dialog(GTK_WINDOW(dialog), TRACE_GUI_ASK, + "The file '%s' already exists.\n" + "Are you sure you want to replace it", + filename); + if (ret == GTK_RESPONSE_NO) { + g_free(filename); + filename = NULL; + goto again; + } + } + } gtk_widget_destroy(dialog); diff --git a/trace-graph-main.c b/trace-graph-main.c index b4f05fb..cc8bf6c 100644 --- a/trace-graph-main.c +++ b/trace-graph-main.c @@ -57,7 +57,7 @@ load_clicked (gpointer data) struct tracecmd_input *handle; gchar *filename; - filename = trace_get_file_dialog("Load File"); + filename = trace_get_file_dialog("Load File", NULL, FALSE); if (!filename) return; @@ -162,7 +162,7 @@ load_filters_clicked (gpointer data) struct tracecmd_xml_handle *handle; gchar *filename; - filename = trace_get_file_dialog("Load Filters"); + filename = trace_get_file_dialog("Load Filters", NULL, FALSE); if (!filename) return; @@ -189,7 +189,7 @@ save_filters_clicked (gpointer data) struct tracecmd_xml_handle *handle; gchar *filename; - filename = trace_get_file_dialog("Save Filters"); + filename = trace_get_file_dialog("Save Filters", "Save", TRUE); if (!filename) return; diff --git a/trace-gui.h b/trace-gui.h index a2177b0..ca9f6ca 100644 --- a/trace-gui.h +++ b/trace-gui.h @@ -27,6 +27,7 @@ enum trace_dialog_type { TRACE_GUI_INFO, TRACE_GUI_WARNING, TRACE_GUI_ERROR, + TRACE_GUI_ASK, }; GtkWidget *trace_status_bar_new(void); @@ -36,10 +37,11 @@ void trace_dialog_register_alt_warning(void (*alt)(const char *fmt, va_list ap)) void trace_show_help(GtkWidget *window, const gchar *link, GError **error); -void trace_dialog(GtkWindow *parent, enum trace_dialog_type type, - gchar *message, ...); +GtkResponseType trace_dialog(GtkWindow *parent, enum trace_dialog_type type, + gchar *message, ...); -gchar *trace_get_file_dialog(const gchar *title); +gchar *trace_get_file_dialog(const gchar *title, const char *open, + gboolean warn); GtkWidget * trace_create_combo_box(GtkWidget *hbox, const gchar *text, diff --git a/trace-view-main.c b/trace-view-main.c index 59d6838..636ebd1 100644 --- a/trace-view-main.c +++ b/trace-view-main.c @@ -68,7 +68,7 @@ load_clicked (gpointer data) struct tracecmd_input *handle; gchar *filename; - filename = trace_get_file_dialog("Load File"); + filename = trace_get_file_dialog("Load File", NULL, FALSE); if (!filename) return; @@ -91,7 +91,7 @@ load_filters_clicked (gpointer data) struct tracecmd_xml_handle *handle; gchar *filename; - filename = trace_get_file_dialog("Load Filters"); + filename = trace_get_file_dialog("Load Filters", NULL, FALSE); if (!filename) return; @@ -121,7 +121,7 @@ save_filters_clicked (gpointer data) struct tracecmd_xml_handle *handle; gchar *filename; - filename = trace_get_file_dialog("Save Filters"); + filename = trace_get_file_dialog("Save Filters", "Save", TRUE); if (!filename) return; -- cgit v1.2.2