diff --git a/src/calibre/gui2/add.py b/src/calibre/gui2/add.py index 086f40feee..f40cf0ff75 100644 --- a/src/calibre/gui2/add.py +++ b/src/calibre/gui2/add.py @@ -183,32 +183,33 @@ class DBAdder(QObject): # {{{ identical_book_list = self.db.find_identical_books(mi) if identical_book_list: # books with same author and nearly same title exist in db self.merged_books.add(mi.title) - a_new_record_has_been_created = False - for identical_book in identical_book_list: - if gprefs['automerge'] == 'ignore': - self.add_formats(identical_book, formats, replace=False) - if gprefs['automerge'] == 'overwrite': - self.add_formats(identical_book, formats, replace=True) - if gprefs['automerge'] == 'new record' and not a_new_record_has_been_created: - ''' - We are here because we have at least one book record in the db that matches the one file/format being processed - We need to check if the file/format being processed matches a format in the matching book record. - If so, create new record (as below), else, add to existing record, as above. - Test if format exists in matching record. identical_book is an id, formats is a FQPN path in a list - ''' - for path in formats: - fmt = os.path.splitext(path)[-1].replace('.', '').upper() - ib_fmts = self.db.formats(identical_book, index_is_id=True) - if ib_fmts and fmt in ib_fmts: # Create a new record - if not a_new_record_has_been_created: - id_ = self.db.create_book_entry(mi, cover=cover, add_duplicates=True) - self.number_of_books_added += 1 - self.add_formats(id_, formats) - a_new_record_has_been_created = True - else: #new record not required - self.add_formats(identical_book, formats, replace=False) + seen_fmts = set([]) - else: # books with same author and nearly same title do not exist in db + for identical_book in identical_book_list: + ib_fmts = self.db.formats(identical_book, index_is_id=True) + if ib_fmts: + seen_fmts |= set(ib_fmts.split(',')) + replace = gprefs['automerge'] == 'overwrite' + self.add_formats(identical_book, formats, + replace=replace) + if gprefs['automerge'] == 'new record': + incoming_fmts = \ + set([os.path.splitext(path)[-1].replace('.', + '').upper() for path in formats]) + if incoming_fmts.intersection(seen_fmts): + # There was at least one duplicate format + # so create a new record and put the + # incoming formats into it + # We should arguably put only the duplicate + # formats, but no real harm is done by having + # all formats + id_ = self.db.create_book_entry(mi, cover=cover, + add_duplicates=True) + self.number_of_books_added += 1 + self.add_formats(id_, formats) + + else: + # books with same author and nearly same title do not exist in db id_ = self.db.create_book_entry(mi, cover=cover, add_duplicates=True) self.number_of_books_added += 1 self.add_formats(id_, formats)