mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
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:
parent
ca647fe34b
commit
d6f161df98
@ -102,6 +102,7 @@ gprefs.defaults['cb_fullscreen'] = False
|
||||
gprefs.defaults['worker_max_time'] = 0
|
||||
gprefs.defaults['show_files_after_save'] = True
|
||||
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
|
||||
|
@ -382,7 +382,8 @@ class Adder(QObject): # {{{
|
||||
if not duplicates:
|
||||
return self.duplicates_processed()
|
||||
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!'),
|
||||
_('Books with the same title as the following already '
|
||||
'exist in the database. Add them anyway?'),
|
||||
|
@ -15,6 +15,7 @@ from PyQt4.Qt import (QFileSystemWatcher, QObject, Qt, pyqtSignal, QTimer)
|
||||
from calibre import prints
|
||||
from calibre.ptempfile import PersistentTemporaryDirectory
|
||||
from calibre.ebooks import BOOK_EXTENSIONS
|
||||
from calibre.gui2 import question_dialog, gprefs
|
||||
|
||||
class Worker(Thread):
|
||||
|
||||
@ -159,12 +160,11 @@ class AutoAdder(QObject):
|
||||
count = 0
|
||||
|
||||
needs_rescan = False
|
||||
duplicates = []
|
||||
|
||||
for fname, tdir in data.iteritems():
|
||||
paths = [os.path.join(self.worker.path, fname)]
|
||||
sz = os.path.join(tdir, 'size.txt')
|
||||
if not os.access(sz, os.R_OK):
|
||||
continue
|
||||
try:
|
||||
with open(sz, 'rb') as f:
|
||||
sz = int(f.read())
|
||||
@ -173,6 +173,11 @@ class AutoAdder(QObject):
|
||||
' we tried to read metadata')
|
||||
except:
|
||||
needs_rescan = True
|
||||
try:
|
||||
self.worker.staging.remove(fname)
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
continue
|
||||
|
||||
mi = os.path.join(tdir, 'metadata.opf')
|
||||
@ -180,18 +185,45 @@ class AutoAdder(QObject):
|
||||
continue
|
||||
mi = [OPF(open(mi, 'rb'), tdir,
|
||||
populate_spine=False).to_book_metadata()]
|
||||
m.add_books(paths, [os.path.splitext(fname)[1][1:].upper()], mi,
|
||||
add_duplicates=True)
|
||||
dups, num = m.add_books(paths,
|
||||
[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:
|
||||
os.remove(paths[0])
|
||||
try:
|
||||
self.worker.staging.remove(fname)
|
||||
except KeyError:
|
||||
pass
|
||||
self.worker.staging.remove(fname)
|
||||
except:
|
||||
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)
|
||||
except:
|
||||
pass
|
||||
count += 1
|
||||
|
||||
if count > 0:
|
||||
m.books_added(count)
|
||||
|
@ -32,6 +32,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
||||
r('automerge', gprefs, choices=choices)
|
||||
r('new_book_tags', prefs, setting=CommaSeparatedList)
|
||||
r('auto_add_path', gprefs, restart_required=True)
|
||||
r('auto_add_check_for_duplicates', gprefs)
|
||||
|
||||
self.filename_pattern = FilenamePattern(self)
|
||||
self.metadata_box.layout().insertWidget(0, self.filename_pattern)
|
||||
|
@ -193,6 +193,19 @@ Author matching is exact.</string>
|
||||
</property>
|
||||
</widget>
|
||||
</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 &duplicates when auto-adding files</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="verticalSpacer_2">
|
||||
<property name="orientation">
|
||||
|
Loading…
x
Reference in New Issue
Block a user