From 130a70114dcefd3e61b250ed0f277c3bc1b21389 Mon Sep 17 00:00:00 2001
From: mfg92 <mfg92@users.noreply.github.com>
Date: Thu, 17 Feb 2022 20:53:00 +0100
Subject: [PATCH 1/5] Add script create_thumbnails.lua

---
 contrib/create_thumbnails.lua | 86 +++++++++++++++++++++++++++++++++++
 1 file changed, 86 insertions(+)
 create mode 100644 contrib/create_thumbnails.lua

diff --git a/contrib/create_thumbnails.lua b/contrib/create_thumbnails.lua
new file mode 100644
index 00000000..10b4ac85
--- /dev/null
+++ b/contrib/create_thumbnails.lua
@@ -0,0 +1,86 @@
+--[[
+Create thumbnails plugin for darktable
+
+  darktable is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+  
+  darktable 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 General Public License for more details.
+  
+  You should have received a copy of the GNU General Public License
+  along with darktable.  If not, see <http://www.gnu.org/licenses/>.
+]]
+
+--[[About this Plugin
+This plugin adds the button 'create thumbnails' to 'selected iamge[s]' module of darktable's lighttable view
+
+
+----USAGE----
+Click the 'create thumbnails' button to let the script create full sized previews of all selected images.
+
+To create previews of all images of a collection:
+Use CTRL+A to select all images of current collection and then press the button.
+]]
+
+local dt = require "darktable"
+local du = require "lib/dtutils"
+
+du.check_min_api_version("8.0.0", "create_thumbnails_button")
+
+-- stop running thumbnail creation
+local function stop_job(job)
+    job.valid = false
+end
+
+-- add button to 'selected images' module
+dt.gui.libs.image.register_action(
+    "create_thumbnails_button",
+    "create thumbnails",
+    function(event, images)
+        dt.print_log("creating thumbnails for " .. #images .. " images...")
+
+        -- create a new progress_bar displayed in darktable.gui.libs.backgroundjobs
+        job = dt.gui.create_job("creating thumbnails...", true, stop_job)
+
+        for i, image in pairs(images) do
+            -- generate all thumbnails, a max value of 8 means that also a full size preview image is created
+            image:generate_cache(true, 0, 8)
+            
+            -- update progress_bar
+            job.percent = i / #images
+
+            -- sleep for a short moment to give stop_job callback function a chance to run
+            dt.control.sleep(10)
+
+            -- stop early if darktable is shutdown or the cancle button of the progress bar is pressed
+            if dt.control.ending or not job.valid then
+                dt.print_log("creating thumbnails canceled!")
+                break
+            end
+        end
+
+        dt.print_log("create thumbnails done!")
+
+        -- stop job and remove progress_bar from ui, but only if not alreay canceled
+        if(job.valid) then
+            job.valid = false
+        end
+    end,
+    "create full sized previews of all selected images"
+)
+
+-- clean up function that is called when this module is getting disabled
+local function destroy()
+    dt.gui.libs.image.destroy_action("create_thumbnails_button")
+end
+
+
+local script_data = {}
+script_data.destroy = destroy -- function to destory the script
+script_data.destroy_method = nil -- set to hide for libs since we can't destroy them commpletely yet
+script_data.restart = nil -- how to restart the (lib) script after it's been hidden - i.e. make it visible again
+return script_data

From 554fd032c735bbcdc509e9ffdd8848c8d33ad525 Mon Sep 17 00:00:00 2001
From: mfg92 <mfg92@users.noreply.github.com>
Date: Thu, 17 Feb 2022 20:58:54 +0100
Subject: [PATCH 2/5] Improve usage description of create_thumbnails.lua

---
 contrib/create_thumbnails.lua | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/contrib/create_thumbnails.lua b/contrib/create_thumbnails.lua
index 10b4ac85..addda71b 100644
--- a/contrib/create_thumbnails.lua
+++ b/contrib/create_thumbnails.lua
@@ -16,11 +16,11 @@ Create thumbnails plugin for darktable
 ]]
 
 --[[About this Plugin
-This plugin adds the button 'create thumbnails' to 'selected iamge[s]' module of darktable's lighttable view
+This plugin adds the button 'create thumbnails' to 'selected image[s]' module of darktable's lighttable view.
 
 
 ----USAGE----
-Click the 'create thumbnails' button to let the script create full sized previews of all selected images.
+Click the 'create thumbnails' button in the 'selected image[s]' module to let the script create full sized previews of all selected images.
 
 To create previews of all images of a collection:
 Use CTRL+A to select all images of current collection and then press the button.

From eb30836752689eda6c8e3664f5e2934c13afa55e Mon Sep 17 00:00:00 2001
From: mfg92 <mfg92@users.noreply.github.com>
Date: Thu, 17 Feb 2022 21:17:45 +0100
Subject: [PATCH 3/5] improve performance of create_thumbnails.lua

---
 contrib/create_thumbnails.lua | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/contrib/create_thumbnails.lua b/contrib/create_thumbnails.lua
index addda71b..8549a4c5 100644
--- a/contrib/create_thumbnails.lua
+++ b/contrib/create_thumbnails.lua
@@ -48,7 +48,8 @@ dt.gui.libs.image.register_action(
 
         for i, image in pairs(images) do
             -- generate all thumbnails, a max value of 8 means that also a full size preview image is created
-            image:generate_cache(true, 0, 8)
+            -- check if the mipmap cache directories exist only once
+            image:generate_cache(i == 1, 0, 8)
             
             -- update progress_bar
             job.percent = i / #images

From 1a5f765b1c8cf50a2b501fc80c1a48fc7622a6ee Mon Sep 17 00:00:00 2001
From: mfg92 <mfg92@users.noreply.github.com>
Date: Wed, 23 Feb 2022 18:53:16 +0100
Subject: [PATCH 4/5] improve logging and user feedback of
 create_thumbnails.lua

---
 contrib/create_thumbnails.lua | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/contrib/create_thumbnails.lua b/contrib/create_thumbnails.lua
index 8549a4c5..8a4b607a 100644
--- a/contrib/create_thumbnails.lua
+++ b/contrib/create_thumbnails.lua
@@ -36,19 +36,30 @@ local function stop_job(job)
     job.valid = false
 end
 
+-- return "1 image" if amount_of_images is 1 else "<amount_of_images> images"
+local function image_count_to_text(amount_of_images)
+    return amount_of_images .. " image" .. (amount_of_images == 1 and "" or "s")
+end
+
+-- print the given message to the user and log the given message
+local function print_and_log(message)
+    dt.print(message)
+    dt.print_log(message)
+end
+
 -- add button to 'selected images' module
 dt.gui.libs.image.register_action(
     "create_thumbnails_button",
     "create thumbnails",
     function(event, images)
-        dt.print_log("creating thumbnails for " .. #images .. " images...")
+        print_and_log("creating thumbnails for " .. image_count_to_text(#images) .. " ...")
 
         -- create a new progress_bar displayed in darktable.gui.libs.backgroundjobs
         job = dt.gui.create_job("creating thumbnails...", true, stop_job)
 
         for i, image in pairs(images) do
             -- generate all thumbnails, a max value of 8 means that also a full size preview image is created
-            -- check if the mipmap cache directories exist only once
+            -- check only once if the mipmap cache directories exist 
             image:generate_cache(i == 1, 0, 8)
             
             -- update progress_bar
@@ -59,16 +70,17 @@ dt.gui.libs.image.register_action(
 
             -- stop early if darktable is shutdown or the cancle button of the progress bar is pressed
             if dt.control.ending or not job.valid then
-                dt.print_log("creating thumbnails canceled!")
+                print_and_log("creating thumbnails canceled after processing " .. i .. "/" .. image_count_to_text(#images) .. "!")
                 break
             end
         end
 
-        dt.print_log("create thumbnails done!")
-
-        -- stop job and remove progress_bar from ui, but only if not alreay canceled
+        -- if job was not canceled
         if(job.valid) then
+            -- stop job and remove progress_bar from ui
             job.valid = false
+
+            print_and_log("creating thumbnails for " ..  image_count_to_text(#images) .. " done!")
         end
     end,
     "create full sized previews of all selected images"

From 5094d9e8ff8c9b98a7a787ce932e76e5b2417c9b Mon Sep 17 00:00:00 2001
From: mfg92 <mfg92@users.noreply.github.com>
Date: Wed, 23 Feb 2022 19:13:31 +0100
Subject: [PATCH 5/5] add 'delete thumbnails' button to create_thumbnails.lua

---
 contrib/create_thumbnails.lua | 47 ++++++++++++++++++++++++++++++++---
 1 file changed, 44 insertions(+), 3 deletions(-)

diff --git a/contrib/create_thumbnails.lua b/contrib/create_thumbnails.lua
index 8a4b607a..6bd976d1 100644
--- a/contrib/create_thumbnails.lua
+++ b/contrib/create_thumbnails.lua
@@ -16,14 +16,15 @@ Create thumbnails plugin for darktable
 ]]
 
 --[[About this Plugin
-This plugin adds the button 'create thumbnails' to 'selected image[s]' module of darktable's lighttable view.
+This plugin adds the buttons 'create thumbnails' and 'delete thumbnails' to 'selected image[s]' module of darktable's lighttable view.
 
 
 ----USAGE----
 Click the 'create thumbnails' button in the 'selected image[s]' module to let the script create full sized previews of all selected images.
+Click the 'delete thumbnails' button in the 'selected image[s]' module to let the script delete all previews/thumbnails of all selected images.
 
-To create previews of all images of a collection:
-Use CTRL+A to select all images of current collection and then press the button.
+To create (or delete) previews of all images of a collection:
+Use CTRL+A to select all images of current collection and then press the corresponding button.
 ]]
 
 local dt = require "darktable"
@@ -86,9 +87,49 @@ dt.gui.libs.image.register_action(
     "create full sized previews of all selected images"
 )
 
+
+-- add button to 'selected images' module
+dt.gui.libs.image.register_action(
+    "delete_thumbnails_button",
+    "delete thumbnails",
+    function(event, images)
+        print_and_log("deleting thumbnails of " .. image_count_to_text(#images) .. " ...")
+
+        -- create a new progress_bar displayed in darktable.gui.libs.backgroundjobs
+        job = dt.gui.create_job("deleting thumbnails...", true, stop_job)
+
+        for i, image in pairs(images) do
+            -- delete all thumbnails
+            image:drop_cache()
+            
+            -- update progress_bar
+            job.percent = i / #images
+
+            -- sleep for a short moment to give stop_job callback function a chance to run
+            dt.control.sleep(10)
+
+            -- stop early if darktable is shutdown or the cancle button of the progress bar is pressed
+            if dt.control.ending or not job.valid then
+                print_and_log("deleting thumbnails canceled after processing " .. i .. "/" .. image_count_to_text(#images) .. "!")
+                break
+            end
+        end
+
+        -- if job was not canceled
+        if(job.valid) then
+            -- stop job and remove progress_bar from ui
+            job.valid = false
+
+            print_and_log("deleting thumbnails of " ..  image_count_to_text(#images) .. " done!")
+        end
+    end,
+    "delete all thumbnails of all selected images"
+)
+
 -- clean up function that is called when this module is getting disabled
 local function destroy()
     dt.gui.libs.image.destroy_action("create_thumbnails_button")
+    dt.gui.libs.image.destroy_action("delete_thumbnails_button")
 end