[Thunar-dev] [PATCH] New option: Show confirmation dialog before moving file(s) to Trash

Vladimír Chren vlado.chren at inmail.sk
Sun Jan 29 22:27:33 CET 2017


Hello.

I'm proposing a patch for optionally showing confirmation dialog before
moving file(s) to trash. It happened to me NOT ONCE that I accidentally
pressed 'Delete' key and then I must have looked up the file in trash
and restore it. Undo option in menu would be some solution to this but
it's not present anyway.

In worst case this may happen even without knowing and then the file
might be lost completely when you empty the trash before you'll need to
work with it.

I've read about this issue in some forums too and people where looking
for this option.

Of course I'm proposing this option to be enabled by default, it might
disabled too by setting "misc-ask-move-to-trash" preference default
value to FALSE.


I'm open to make modifications or changes to better integrate this
option in Thunar codebase if necessary. I've spend only short time
working on this.


Here is the patch.


diff --git a/thunar/thunar-application.c b/thunar/thunar-application.c
index 8ce6683..fee6b7b 100644
--- a/thunar/thunar-application.c
+++ b/thunar/thunar-application.c
@@ -1846,8 +1846,64 @@ thunar_application_unlink_files
(ThunarApplication *application,
     }
   else
     {
-      /* launch the "Move to Trash" operation */
-      thunar_application_trash (application, parent, path_list);
+      gboolean misc_ask_move_to_trash;
+      gboolean launch_move_to_trash = TRUE;
+
+      /* get boolean property for confirmation before moving file(s)
to trash */
+      g_object_get (G_OBJECT (application->preferences),
+                "misc-ask-move-to-trash", &misc_ask_move_to_trash,
+                NULL);
+
+      if (misc_ask_move_to_trash)
+        {
+          /* parse the parent pointer */
+          screen = thunar_util_parse_parent (parent, &window);
+
+          /* generate the question to confirm the move to trash
operation */
+          if (G_LIKELY (n_path_list == 1))
+            {
+              message = g_strdup_printf (_("Are you sure that you want
to\nmove \"%s\" to trash ?"),
+                                         thunar_file_get_display_name
(THUNAR_FILE (file_list->data)));
+            }
+          else
+            {
+              message = g_strdup_printf (ngettext ("Are you sure that
you want to \nmove the selected file to trash?",
+                                                   "Are you sure that
you want to \nmove the %u selected files to trash?",
+                                                   n_path_list),
+                                         n_path_list);
+            }
+
+          /* ask the user to confirm the move to trash operation */
+          dialog = gtk_message_dialog_new (window,
+                                           GTK_DIALOG_MODAL |
GTK_DIALOG_DESTROY_WITH_PARENT,
+                                           GTK_MESSAGE_QUESTION,
+                                           GTK_BUTTONS_NONE,
+                                           "%s", message);
+          if (G_UNLIKELY (window == NULL && screen != NULL))
+            gtk_window_set_screen (GTK_WINDOW (dialog), screen);
+          gtk_dialog_add_buttons (GTK_DIALOG (dialog),
+                                  GTK_STOCK_CANCEL,
GTK_RESPONSE_CANCEL,
+                                  _("Move to Trash"),
GTK_RESPONSE_YES,
+                                  NULL);
+          gtk_dialog_set_default_response (GTK_DIALOG (dialog),
GTK_RESPONSE_YES);
+          gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG
(dialog),
+                                                    _("If you move a
file to trash, you can restore it later."));
+          response = gtk_dialog_run (GTK_DIALOG (dialog));
+          gtk_widget_destroy (dialog);
+          g_free (message);
+
+          if (G_UNLIKELY (response != GTK_RESPONSE_YES))
+            {
+              launch_move_to_trash = FALSE;
+            }
+        }
+
+      /* perform the move to trash operation */
+      if (G_LIKELY (launch_move_to_trash))
+        {
+          /* launch the "Move to Trash" operation */
+          thunar_application_trash (application, parent, path_list);
+        }
     }
 
   /* release the path list */
diff --git a/thunar/thunar-preferences.c b/thunar/thunar-preferences.c
index 9a86ae1..f45fd3b 100644
--- a/thunar/thunar-preferences.c
+++ b/thunar/thunar-preferences.c
@@ -91,6 +91,7 @@ enum
   PROP_MISC_TEXT_BESIDE_ICONS,
   PROP_MISC_THUMBNAIL_MODE,
   PROP_MISC_FILE_SIZE_BINARY,
+  PROP_MISC_ASK_MOVE_TO_TRASH,
   PROP_SHORTCUTS_ICON_EMBLEMS,
   PROP_SHORTCUTS_ICON_SIZE,
   PROP_TREE_ICON_EMBLEMS,
@@ -706,6 +707,19 @@ thunar_preferences_class_init
(ThunarPreferencesClass *klass)
                             EXO_PARAM_READWRITE);
 
   /**
+   * ThunarPreferences:misc-ask-move-to-trash:
+   *
+   * Whether to show confirmation dialog before moving file(s)
+   * to trash.
+   **/
+  preferences_props[PROP_MISC_ASK_MOVE_TO_TRASH] =
+      g_param_spec_boolean ("misc-ask-move-to-trash",
+                            "MiscAskMoveToTrash",
+                            NULL,
+                            TRUE,
+                            EXO_PARAM_READWRITE);
+
+  /**
    * ThunarPreferences:shortcuts-icon-emblems:
    *
    * Whether to display emblems for file icons (if defined) in the

-------------- next part --------------
A non-text attachment was scrubbed...
Name: confirm_move_to_trash.patch
Type: text/x-patch
Size: 4356 bytes
Desc: not available
URL: <https://mail.xfce.org/pipermail/thunar-dev/attachments/20170129/5b80ea5a/attachment.bin>


More information about the Thunar-dev mailing list