From a8229ffc9d6f894e50fe3bfc16e9b2a447a58510 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 21 Apr 2023 17:28:41 +0530 Subject: [PATCH] Trash bin: Add a button to clear the bin. Fixes #2017232 [Enhancement Request: One-click 'empty recycle bin'](https://bugs.launchpad.net/calibre/+bug/2017232) --- src/calibre/db/backend.py | 6 ++++++ src/calibre/db/cache.py | 4 ++++ src/calibre/gui2/trash.py | 15 ++++++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/calibre/db/backend.py b/src/calibre/db/backend.py index 8d4fd14fa6..cc90ceb1f2 100644 --- a/src/calibre/db/backend.py +++ b/src/calibre/db/backend.py @@ -1984,6 +1984,12 @@ class DB: def trash_dir(self): return os.path.abspath(os.path.join(self.library_path, TRASH_DIR_NAME)) + def clear_trash_dir(self): + tdir = self.trash_dir + if os.path.exists(tdir): + self.rmtree(tdir) + self.ensure_trash_dir() + def ensure_trash_dir(self): tdir = self.trash_dir os.makedirs(os.path.join(tdir, 'b'), exist_ok=True) diff --git a/src/calibre/db/cache.py b/src/calibre/db/cache.py index 5e64164b8b..0afb6cf519 100644 --- a/src/calibre/db/cache.py +++ b/src/calibre/db/cache.py @@ -2676,6 +2676,10 @@ class Cache: def is_closed(self): return self.backend.is_closed + @write_api + def clear_trash_bin(self): + self.backend.clear_trash_dir() + @read_api def list_trash_entries(self): books, formats = self.backend.list_trash_entries() diff --git a/src/calibre/gui2/trash.py b/src/calibre/gui2/trash.py index e666c2ff07..60ed7d80b3 100644 --- a/src/calibre/gui2/trash.py +++ b/src/calibre/gui2/trash.py @@ -1,7 +1,6 @@ #!/usr/bin/env python # License: GPLv3 Copyright: 2023, Kovid Goyal - import time import traceback from operator import attrgetter @@ -15,6 +14,7 @@ from typing import Iterator, List from calibre import fit_image from calibre.db.backend import DEFAULT_TRASH_EXPIRY_TIME_SECONDS, TrashEntry from calibre.gui2 import error_dialog +from calibre.gui2.dialogs.confirm_delete import confirm from calibre.gui2.widgets import BusyCursor from calibre.gui2.widgets2 import Dialog @@ -145,6 +145,7 @@ class TrashView(Dialog): la.setBuddy(ad) l.addLayout(h) + h = QHBoxLayout() l.addWidget(self.bb) self.restore_button = b = self.bb.addButton(_('&Restore selected'), QDialogButtonBox.ButtonRole.ActionRole) b.clicked.connect(self.restore_selected) @@ -153,6 +154,18 @@ class TrashView(Dialog): b.setToolTip(_('Remove the selected entries from the trash bin, thereby deleting them permanently')) b.setIcon(QIcon.ic('edit-clear.png')) b.clicked.connect(self.delete_selected) + self.clear_button = b = self.bb.addButton(_('&Clear'), QDialogButtonBox.ButtonRole.ResetRole) + b.clicked.connect(self.clear_all) + b.setIcon(QIcon.ic('dialog_warning.png')) + self.update_titles() + self.bb.button(QDialogButtonBox.StandardButton.Close).setFocus(Qt.FocusReason.OtherFocusReason) + + def clear_all(self): + if not confirm('

'+_('All books and formats will be permanently deleted! Are you sure?'), 'clear_trash_bin', self): + return + self.db.clear_trash_bin() + self.books.clear() + self.formats.clear() self.update_titles() def update_titles(self):