Hi,

 I have been using darktable for a few months now and I really like it. I
recently thought of a feature that would be useful (to me at least):
sending deleted files to trash instead of permanently deleting them.

 Since I'm a C++ dev for my day job, I decided to try to implement it. I've
attached the patch for the master branch. It works well as far as I could
test it.

 I hope this can be merged!

Guillaume

___________________________________________________________________________
darktable developer mailing list
to unsubscribe send a mail to darktable-dev+unsubscr...@lists.darktable.org
From 3728987b1be1841c77b052182e22ec6576d6b007 Mon Sep 17 00:00:00 2001
From: Guillaume Benny <benny...@gmail.com>
Date: Tue, 28 Jul 2015 17:39:40 -0400
Subject: [PATCH] Add send_to_trash config

This config send files to trash instead of permanently deleting files on
system that supports it.
---
 data/darktableconfig.xml.in     |  7 +++++++
 src/control/jobs/control_jobs.c | 30 +++++++++++++++++++++++++++---
 2 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/data/darktableconfig.xml.in b/data/darktableconfig.xml.in
index 9202f3b..7427716 100644
--- a/data/darktableconfig.xml.in
+++ b/data/darktableconfig.xml.in
@@ -204,6 +204,13 @@
     <longdescription>always ask the user before any image file is deleted.</longdescription>
   </dtconfig>
   <dtconfig prefs="gui">
+    <name>send_to_trash</name>
+    <type>bool</type>
+    <default>false</default>
+    <shortdescription>send files to trash when erasing images</shortdescription>
+    <longdescription>send files to trash instead of permanently deleting files on system that supports it</longdescription>
+  </dtconfig>
+  <dtconfig prefs="gui">
     <name>ask_before_move</name>
     <type>bool</type>
     <default>true</default>
diff --git a/src/control/jobs/control_jobs.c b/src/control/jobs/control_jobs.c
index c2431ef..080db35 100644
--- a/src/control/jobs/control_jobs.c
+++ b/src/control/jobs/control_jobs.c
@@ -39,6 +39,7 @@
 
 #include <glib.h>
 #include <glib/gstdio.h>
+#include <gio/gio.h>
 #ifndef __WIN32__
 #include <glob.h>
 #endif
@@ -663,6 +664,29 @@ static int32_t dt_control_remove_images_job_run(dt_job_t *job)
 }
 
 
+static void delete_file_from_disk(const char *filename)
+{
+  GError *gerror = NULL;
+  GFile *gfile = NULL;
+  int must_unlink = !dt_conf_get_bool("send_to_trash");
+
+  if (!must_unlink)
+  {
+    gfile = g_file_new_for_path(filename);
+    if (!g_file_trash(gfile, NULL, &gerror))
+    {
+      if (g_error_matches(gerror, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
+        must_unlink = TRUE;
+    }
+
+    if (gerror != NULL)
+      g_object_unref(gfile);
+  }
+  if (must_unlink)
+    (void)g_unlink(filename);
+}
+
+
 static int32_t dt_control_delete_images_job_run(dt_job_t *job)
 {
   int imgid = -1;
@@ -708,7 +732,7 @@ static int32_t dt_control_delete_images_job_run(dt_job_t *job)
     {
       // there are no further duplicates so we can remove the source data file
       dt_image_remove(imgid);
-      (void)g_unlink(filename);
+      delete_file_from_disk(filename);
 
       // all sidecar files - including left-overs - can be deleted;
       // left-overs can result when previously duplicates have been REMOVED;
@@ -757,7 +781,7 @@ static int32_t dt_control_delete_images_job_run(dt_job_t *job)
       GList *file_iter = g_list_first(files);
       while(file_iter != NULL)
       {
-        (void)g_unlink(file_iter->data);
+        delete_file_from_disk(file_iter->data);
         file_iter = g_list_next(file_iter);
       }
 
@@ -772,7 +796,7 @@ static int32_t dt_control_delete_images_job_run(dt_job_t *job)
       g_strlcat(filename, ".xmp", sizeof(filename));
 
       dt_image_remove(imgid);
-      (void)g_unlink(filename);
+      delete_file_from_disk(filename);
     }
 
     t = g_list_delete_link(t, t);
-- 
2.3.6

Reply via email to