mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 02:34:06 -04:00
OS X: When deleting books, put the files into the recycle bin instead of deleting them permanently
This commit is contained in:
parent
ab1795de9a
commit
fe896f0c8e
@ -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,10 +958,7 @@ 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:
|
self.rmtree(path)
|
||||||
winshell.delete_file(path, no_confirm=True, silent=True)
|
|
||||||
except:
|
|
||||||
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:
|
||||||
self.rmtree(parent)
|
self.rmtree(parent)
|
||||||
|
59
src/calibre/utils/recycle_bin.py
Normal file
59
src/calibre/utils/recycle_bin.py
Normal 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)
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user