Skip to content

Commit

Permalink
Trash bin: Add a button to clear the bin. Fixes #2017232 [Enhancement…
Browse files Browse the repository at this point in the history
… Request: One-click 'empty recycle bin'](https://bugs.launchpad.net/calibre/+bug/2017232)
  • Loading branch information
kovidgoyal committed Apr 21, 2023
1 parent f7fb20e commit a8229ff
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/calibre/db/backend.py
Expand Up @@ -1984,6 +1984,12 @@ def read_backup(self, path):
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)
Expand Down
4 changes: 4 additions & 0 deletions src/calibre/db/cache.py
Expand Up @@ -2676,6 +2676,10 @@ def close(self):
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()
Expand Down
15 changes: 14 additions & 1 deletion src/calibre/gui2/trash.py
@@ -1,7 +1,6 @@
#!/usr/bin/env python
# License: GPLv3 Copyright: 2023, Kovid Goyal <kovid at kovidgoyal.net>


import time
import traceback
from operator import attrgetter
Expand All @@ -15,6 +14,7 @@
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

Expand Down Expand Up @@ -145,6 +145,7 @@ def setup_ui(self):
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)
Expand All @@ -153,6 +154,18 @@ def setup_ui(self):
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('<p>'+_('All books and formats will be <b>permanently deleted</b>! 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):
Expand Down

0 comments on commit a8229ff

Please sign in to comment.