OS X: When deleting books, put the files into the recycle bin instead of deleting them permanently

This commit is contained in:
Kovid Goyal 2010-11-16 14:29:05 -07:00
parent ab1795de9a
commit fe896f0c8e
2 changed files with 61 additions and 30 deletions

View File

@ -36,33 +36,8 @@ from calibre.utils.config import prefs, tweaks
from calibre.utils.search_query_parser import saved_searches, set_saved_searches from calibre.utils.search_query_parser import saved_searches, set_saved_searches
from calibre.ebooks import BOOK_EXTENSIONS, check_ebook_format from calibre.ebooks import BOOK_EXTENSIONS, check_ebook_format
from calibre.utils.magick.draw import save_cover_data_to from calibre.utils.magick.draw import save_cover_data_to
from calibre.utils.recycle_bin import delete_file, delete_tree
if iswindows:
import calibre.utils.winshell as winshell
def delete_file(path):
try:
winshell.delete_file(path, silent=True, no_confirm=True)
except:
os.remove(path)
def delete_tree(path, permanent=False):
if permanent:
try:
# For completely mysterious reasons, sometimes a file is left open
# leading to access errors. If we get an exception, wait and hope
# that whatever has the file (the O/S?) lets go of it.
shutil.rmtree(path)
except:
traceback.print_exc()
time.sleep(1)
shutil.rmtree(path)
else:
try:
if not permanent:
winshell.delete_file(path, silent=True, no_confirm=True)
except:
delete_tree(path, permanent=True)
copyfile = os.link if hasattr(os, 'link') else shutil.copyfile copyfile = os.link if hasattr(os, 'link') else shutil.copyfile
@ -983,9 +958,6 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
path = None path = None
self.data.remove(id) self.data.remove(id)
if path and os.path.exists(path): if path and os.path.exists(path):
try:
winshell.delete_file(path, no_confirm=True, silent=True)
except:
self.rmtree(path) self.rmtree(path)
parent = os.path.dirname(path) parent = os.path.dirname(path)
if len(os.listdir(parent)) == 0: if len(os.listdir(parent)) == 0:

View File

@ -0,0 +1,59 @@
#!/usr/bin/env python
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
__license__ = 'GPL v3'
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
import os, shutil, time
from functools import partial
from calibre import isbytestring
from calibre.constants import iswindows, isosx, plugins, filesystem_encoding
recycle = None
if iswindows:
import calibre.utils.winshell as winshell
recycle = partial(winshell.delete_file, silent=True, no_confirm=True)
elif isosx:
u = plugins['usbobserver'][0]
if hasattr(u, 'send2trash'):
def recycle(path):
if isbytestring(path):
path = path.decode(filesystem_encoding)
u.send2trash(path)
def delete_file(path):
if callable(recycle):
try:
recycle(path)
return
except:
import traceback
traceback.print_exc()
os.remove(path)
def delete_tree(path, permanent=False):
if permanent:
try:
# For completely mysterious reasons, sometimes a file is left open
# leading to access errors. If we get an exception, wait and hope
# that whatever has the file (the O/S?) lets go of it.
shutil.rmtree(path)
except:
import traceback
traceback.print_exc()
time.sleep(1)
shutil.rmtree(path)
else:
if callable(recycle):
try:
recycle(path)
return
except:
import traceback
traceback.print_exc()
delete_tree(path, permanent=True)