I am trying to update my plugin to use the new API, but I ran into a problem when using "db.new_api.add_books" instead of "db.import_book".
After changing my code to use add_books the new books no longer appear in the main table of books in the GUI. All that appears is a blank row at the end created by my call to gui.library_view.model().books_added(number_added_ books).
I looked at the code for add_books in calibre.db.legacy and saw that after calling self.new_api.add_books it later calls self.data.books_added.
Code:
def add_books(self, paths, formats, metadata, add_duplicates=True, return_ids=False):
books = [(mi, {fmt:path}) for mi, path, fmt in zip(metadata, paths, formats)]
book_ids, duplicates = self.new_api.add_books(books, add_duplicates=add_duplicates, dbapi=self)
if duplicates:
paths, formats, metadata = [], [], []
for mi, format_map in duplicates:
metadata.append(mi)
for fmt, path in format_map.iteritems():
formats.append(fmt)
paths.append(path)
duplicates = (paths, formats, metadata)
ids = book_ids if return_ids else len(book_ids)
if book_ids:
self.data.books_added(book_ids)
return duplicates or None, ids
I added a similar call in my plugin and this solved the problem.
So I am wondering if it is best to leave it this way or is there something else I should be doing to properly update the GUI after adding books in the new API?