Auto-adding: Add an option to check for duplicates when auto adding. Fixes #926962 (Adding books from autofolder causes duplicates)

This commit is contained in:
Kovid Goyal 2012-02-05 15:49:13 +05:30
parent ca647fe34b
commit d6f161df98
5 changed files with 58 additions and 10 deletions

View File

@ -102,6 +102,7 @@ gprefs.defaults['cb_fullscreen'] = False
gprefs.defaults['worker_max_time'] = 0 gprefs.defaults['worker_max_time'] = 0
gprefs.defaults['show_files_after_save'] = True gprefs.defaults['show_files_after_save'] = True
gprefs.defaults['auto_add_path'] = None gprefs.defaults['auto_add_path'] = None
gprefs.defaults['auto_add_check_for_duplicates'] = False
# }}} # }}}
NONE = QVariant() #: Null value to return from the data function of item models NONE = QVariant() #: Null value to return from the data function of item models

View File

@ -382,7 +382,8 @@ class Adder(QObject): # {{{
if not duplicates: if not duplicates:
return self.duplicates_processed() return self.duplicates_processed()
self.pd.hide() self.pd.hide()
files = [x[0].title for x in duplicates] files = [_('%s by %s')%(x[0].title, x[0].format_field('authors')[1])
for x in duplicates]
if question_dialog(self._parent, _('Duplicates found!'), if question_dialog(self._parent, _('Duplicates found!'),
_('Books with the same title as the following already ' _('Books with the same title as the following already '
'exist in the database. Add them anyway?'), 'exist in the database. Add them anyway?'),

View File

@ -15,6 +15,7 @@ from PyQt4.Qt import (QFileSystemWatcher, QObject, Qt, pyqtSignal, QTimer)
from calibre import prints from calibre import prints
from calibre.ptempfile import PersistentTemporaryDirectory from calibre.ptempfile import PersistentTemporaryDirectory
from calibre.ebooks import BOOK_EXTENSIONS from calibre.ebooks import BOOK_EXTENSIONS
from calibre.gui2 import question_dialog, gprefs
class Worker(Thread): class Worker(Thread):
@ -159,12 +160,11 @@ class AutoAdder(QObject):
count = 0 count = 0
needs_rescan = False needs_rescan = False
duplicates = []
for fname, tdir in data.iteritems(): for fname, tdir in data.iteritems():
paths = [os.path.join(self.worker.path, fname)] paths = [os.path.join(self.worker.path, fname)]
sz = os.path.join(tdir, 'size.txt') sz = os.path.join(tdir, 'size.txt')
if not os.access(sz, os.R_OK):
continue
try: try:
with open(sz, 'rb') as f: with open(sz, 'rb') as f:
sz = int(f.read()) sz = int(f.read())
@ -173,6 +173,11 @@ class AutoAdder(QObject):
' we tried to read metadata') ' we tried to read metadata')
except: except:
needs_rescan = True needs_rescan = True
try:
self.worker.staging.remove(fname)
except KeyError:
pass
continue continue
mi = os.path.join(tdir, 'metadata.opf') mi = os.path.join(tdir, 'metadata.opf')
@ -180,18 +185,45 @@ class AutoAdder(QObject):
continue continue
mi = [OPF(open(mi, 'rb'), tdir, mi = [OPF(open(mi, 'rb'), tdir,
populate_spine=False).to_book_metadata()] populate_spine=False).to_book_metadata()]
m.add_books(paths, [os.path.splitext(fname)[1][1:].upper()], mi, dups, num = m.add_books(paths,
add_duplicates=True) [os.path.splitext(fname)[1][1:].upper()], mi,
add_duplicates=not gprefs['auto_add_check_for_duplicates'])
if dups:
path = dups[0][0]
with open(os.path.join(tdir, 'dup_cache.'+dups[1][0].lower()),
'wb') as dest, open(path, 'rb') as src:
shutil.copyfileobj(src, dest)
dups[0][0] = dest.name
duplicates.append(dups)
try: try:
os.remove(paths[0]) os.remove(paths[0])
try: self.worker.staging.remove(fname)
self.worker.staging.remove(fname) except:
except KeyError: pass
pass count += num
if duplicates:
paths, formats, metadata = [], [], []
for p, f, mis in duplicates:
paths.extend(p)
formats.extend(f)
metadata.extend(mis)
files = [_('%s by %s')%(mi.title, mi.format_field('authors')[1])
for mi in metadata]
if question_dialog(self.parent(), _('Duplicates found!'),
_('Books with the same title as the following already '
'exist in the database. Add them anyway?'),
'\n'.join(files)):
dups, num = m.add_books(paths, formats, metadata,
add_duplicates=True)
count += num
for tdir in data.itervalues():
try:
shutil.rmtree(tdir) shutil.rmtree(tdir)
except: except:
pass pass
count += 1
if count > 0: if count > 0:
m.books_added(count) m.books_added(count)

View File

@ -32,6 +32,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
r('automerge', gprefs, choices=choices) r('automerge', gprefs, choices=choices)
r('new_book_tags', prefs, setting=CommaSeparatedList) r('new_book_tags', prefs, setting=CommaSeparatedList)
r('auto_add_path', gprefs, restart_required=True) r('auto_add_path', gprefs, restart_required=True)
r('auto_add_check_for_duplicates', gprefs)
self.filename_pattern = FilenamePattern(self) self.filename_pattern = FilenamePattern(self)
self.metadata_box.layout().insertWidget(0, self.filename_pattern) self.metadata_box.layout().insertWidget(0, self.filename_pattern)

View File

@ -193,6 +193,19 @@ Author matching is exact.</string>
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QCheckBox" name="opt_auto_add_check_for_duplicates">
<property name="toolTip">
<string>If set, this option will causes calibre to check if a file
being auto-added is already in the calibre library.
If it is, a meesage will pop up asking you whether
you want to add it anyway.</string>
</property>
<property name="text">
<string>Check for &amp;duplicates when auto-adding files</string>
</property>
</widget>
</item>
<item> <item>
<spacer name="verticalSpacer_2"> <spacer name="verticalSpacer_2">
<property name="orientation"> <property name="orientation">