diff --git a/src/calibre/gui2/__init__.py b/src/calibre/gui2/__init__.py index 35cc249acb..b3e128af82 100644 --- a/src/calibre/gui2/__init__.py +++ b/src/calibre/gui2/__init__.py @@ -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 diff --git a/src/calibre/gui2/add.py b/src/calibre/gui2/add.py index 7cdac3b845..972ea57cb9 100644 --- a/src/calibre/gui2/add.py +++ b/src/calibre/gui2/add.py @@ -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?'), diff --git a/src/calibre/gui2/auto_add.py b/src/calibre/gui2/auto_add.py index 0319a67210..6860f386d6 100644 --- a/src/calibre/gui2/auto_add.py +++ b/src/calibre/gui2/auto_add.py @@ -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) diff --git a/src/calibre/gui2/preferences/adding.py b/src/calibre/gui2/preferences/adding.py index 0cd0aa4657..d35df2a183 100644 --- a/src/calibre/gui2/preferences/adding.py +++ b/src/calibre/gui2/preferences/adding.py @@ -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) diff --git a/src/calibre/gui2/preferences/adding.ui b/src/calibre/gui2/preferences/adding.ui index 9b7401447c..73d305631d 100644 --- a/src/calibre/gui2/preferences/adding.ui +++ b/src/calibre/gui2/preferences/adding.ui @@ -193,6 +193,19 @@ Author matching is exact. + + + + 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. + + + Check for &duplicates when auto-adding files + + +