From ec3321c3850cf69a4eea62f552b9a0f0412bc89e Mon Sep 17 00:00:00 2001 From: Reuben Green Date: Sat, 10 Aug 2019 16:01:20 +0100 Subject: [PATCH] Add a confirmation dialog when closing a window with multiple open tabs. Adds a confirmation dialog to ask for user confirmation when closing a window with multiple open tabs. The user can choose to close the window, close the current tab, or cancel. The user can deactivate this confirmation via a checkbox in the dialog. (Bug #15758) --- thunar/thunar-preferences.c | 14 ++++++ thunar/thunar-window.c | 99 +++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) diff --git a/thunar/thunar-preferences.c b/thunar/thunar-preferences.c index 916ee345..6539a11b 100644 --- a/thunar/thunar-preferences.c +++ b/thunar/thunar-preferences.c @@ -96,6 +96,7 @@ enum PROP_MISC_THUMBNAIL_MODE, PROP_MISC_THUMBNAIL_DRAW_FRAMES, PROP_MISC_FILE_SIZE_BINARY, + PROP_MISC_CONFIRM_CLOSE_MULTIPLE_TABS, PROP_SHORTCUTS_ICON_EMBLEMS, PROP_SHORTCUTS_ICON_SIZE, PROP_TREE_ICON_EMBLEMS, @@ -762,6 +763,19 @@ thunar_preferences_class_init (ThunarPreferencesClass *klass) TRUE, EXO_PARAM_READWRITE); + /** + * ThunarPreferences:misc-confirm-close-multiple-tabs: + * + * Ask the user for confirmation before closing a window with + * multiple tabs. + **/ + preferences_props[PROP_MISC_CONFIRM_CLOSE_MULTIPLE_TABS] = + g_param_spec_boolean ("misc-confirm-close-multiple-tabs", + "ConfirmCloseMultipleTabs", + NULL, + TRUE, + EXO_PARAM_READWRITE); + /** * ThunarPreferences:shortcuts-icon-emblems: * diff --git a/thunar/thunar-window.c b/thunar/thunar-window.c index 7755725b..ceaa8b4e 100644 --- a/thunar/thunar-window.c +++ b/thunar/thunar-window.c @@ -94,6 +94,9 @@ enum static void thunar_window_dispose (GObject *object); static void thunar_window_finalize (GObject *object); +static gboolean thunar_window_delete (GtkWidget *widget, + GdkEvent *event, + gpointer data); static void thunar_window_get_property (GObject *object, guint prop_id, GValue *value, @@ -744,6 +747,9 @@ thunar_window_init (ThunarWindow *window) "misc-small-toolbar-icons", &small_icons, NULL); + /* set up a handler to confirm exit when there are multiple tabs open */ + g_signal_connect (window, "delete-event", G_CALLBACK (thunar_window_delete), NULL); + /* connect to the volume monitor */ window->device_monitor = thunar_device_monitor_get (); g_signal_connect (window->device_monitor, "device-pre-unmount", G_CALLBACK (thunar_window_device_pre_unmount), window); @@ -1085,6 +1091,99 @@ thunar_window_finalize (GObject *object) +static gboolean thunar_window_delete( GtkWidget *widget, + GdkEvent *event, + gpointer data ) +{ + GtkWidget *dialog, *label, *vbox, *hbox, *button, *image, *checkbox; + GtkNotebook *notebook; + const gchar *title; + gchar *message, *markup; + gboolean confirm_close_multiple_tabs; + gint response, n_tabs; + + _thunar_return_val_if_fail (THUNAR_IS_WINDOW (widget),FALSE); + + /* if we don't have muliple tabs then just exit */ + notebook = GTK_NOTEBOOK (THUNAR_WINDOW (widget)->notebook); + n_tabs = gtk_notebook_get_n_pages (GTK_NOTEBOOK (THUNAR_WINDOW (widget)->notebook)); + if (n_tabs < 2) + return FALSE; + + /* check if the user has disabled confirmation of closing multiple tabs, and just exit if so */ + g_object_get (G_OBJECT (THUNAR_WINDOW (widget)->preferences), + "misc-confirm-close-multiple-tabs", &confirm_close_multiple_tabs, + NULL); + if(!confirm_close_multiple_tabs) + return FALSE; + + /* ask the user for confirmation (this code is adapted from the corresponing code in xfce4-terminal) */ + dialog = gtk_dialog_new (); + gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (widget)); + gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE); + gtk_window_set_modal (GTK_WINDOW (dialog), TRUE); + gtk_window_set_title (GTK_WINDOW (dialog), _("Warning")); + + button = xfce_gtk_button_new_mixed ("gtk-cancel", _("_Cancel")); + gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, GTK_RESPONSE_CANCEL); + + message = g_strdup_printf (_("This window has %d tabs open. Closing this window\n" + "will also close all its tabs."), n_tabs); + title = _("Close all tabs?"); + + button = xfce_gtk_button_new_mixed ("window-close", _("Close T_ab")); + gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, GTK_RESPONSE_CLOSE); + gtk_widget_grab_focus (button); + + button = xfce_gtk_button_new_mixed ("application-exit", _("Close _Window")); + gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, GTK_RESPONSE_YES); + gtk_widget_grab_focus (button); + + hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6); + gtk_container_set_border_width (GTK_CONTAINER (hbox), 8); + gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), hbox, TRUE, TRUE, 0); + + image = gtk_image_new_from_icon_name ("dialog-warning", GTK_ICON_SIZE_DIALOG); + gtk_widget_set_halign (image, GTK_ALIGN_CENTER); + gtk_widget_set_valign (image, GTK_ALIGN_START); + gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0); + + vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6); + gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0); + + markup = g_strdup_printf ("%s\n\n%s", title, message); + g_free (message); + + label = g_object_new (GTK_TYPE_LABEL, + "label", markup, + "use-markup", TRUE, + "xalign", 0.0, + NULL); + gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, TRUE, 0); + + g_free (markup); + + checkbox = gtk_check_button_new_with_mnemonic (_("Do _not ask me again")); + gtk_box_pack_start (GTK_BOX (vbox), checkbox, FALSE, FALSE, 0); + + gtk_widget_show_all (dialog); + + response = gtk_dialog_run (GTK_DIALOG (dialog)); + if (response == GTK_RESPONSE_YES && gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (checkbox))) + g_object_set (G_OBJECT (THUNAR_WINDOW (widget)->preferences), + "misc-confirm-close-multiple-tabs", FALSE, NULL); + + gtk_widget_destroy (dialog); + + if(response == GTK_RESPONSE_YES) + return FALSE; + if(response == GTK_RESPONSE_CLOSE) + gtk_notebook_remove_page (notebook, gtk_notebook_get_current_page(notebook)); + return TRUE; +} + + + static void thunar_window_get_property (GObject *object, guint prop_id, -- 2.20.1