diff --git a/src/calibre/gui2/device.py b/src/calibre/gui2/device.py index ab98470a22..855d05ff58 100644 --- a/src/calibre/gui2/device.py +++ b/src/calibre/gui2/device.py @@ -1013,29 +1013,29 @@ class DeviceGUI(object): break return loc - def set_books_in_library(self, booklist): - ''' - Set the 'in_library' attribute for all books on a device to True if a - book on the device is in the library, else False - ''' - # First build a cache of the library, so the search isn't On**2 - cache = {} + def set_books_in_library(self, booklist, reset = False): + if reset: + self.book_in_library_cache = None + return + + # First build a self.book_in_library_cache of the library, so the search isn't On**2 + self.book_in_library_cache = {} for id, title in self.library_view.model().db.all_titles(): title = re.sub('(?u)\W|[_]', '', title.lower()) - if title not in cache: - cache[title] = {'authors':set(), 'db_ids':set()} + if title not in self.book_in_library_cache: + self.book_in_library_cache[title] = {'authors':set(), 'db_ids':set()} au = self.library_view.model().db.authors(id, index_is_id=True) authors = au.lower() if au else '' authors = re.sub('(?u)\W|[_]', '', authors) - cache[title]['authors'].add(authors) - cache[title]['db_ids'].add(id) + self.book_in_library_cache[title]['authors'].add(authors) + self.book_in_library_cache[title]['db_ids'].add(id) # Now iterate through all the books on the device, setting the in_library field for book in booklist: book_title = book.title.lower() if book.title else '' book_title = re.sub('(?u)\W|[_]', '', book_title) book.in_library = False - d = cache.get(book_title, None) + d = self.book_in_library_cache.get(book_title, None) if d is not None: if book.db_id in d['db_ids']: book.in_library = True diff --git a/src/calibre/gui2/library.py b/src/calibre/gui2/library.py index 5d4686b4d2..90dc3eb1ea 100644 --- a/src/calibre/gui2/library.py +++ b/src/calibre/gui2/library.py @@ -1266,13 +1266,6 @@ class DeviceBooksModel(BooksModel): self.search_engine = OnDeviceSearch(self) self.editable = True self.book_in_library = None - self.loc = None - - def set_book_in_library_func(self, func, loc): - self.book_in_library = func - self.loc = loc - # Not convinced that this should be here ... - func(self.db) def mark_for_deletion(self, job, rows): self.marked_for_deletion[job] = self.indices(rows) diff --git a/src/calibre/gui2/ui.py b/src/calibre/gui2/ui.py index df7c246e76..4f5e71174c 100644 --- a/src/calibre/gui2/ui.py +++ b/src/calibre/gui2/ui.py @@ -992,15 +992,16 @@ class Main(MainWindow, Ui_MainWindow, DeviceGUI): else: self.device_job_exception(job) return + self.set_books_in_library(None, reset=True) mainlist, cardalist, cardblist = job.result self.memory_view.set_database(mainlist) - self.memory_view.model().set_book_in_library_func(self.set_books_in_library, 'main') + self.set_books_in_library(mainlist) self.memory_view.set_editable(self.device_manager.device.CAN_SET_METADATA) self.card_a_view.set_database(cardalist) - self.card_a_view.model().set_book_in_library_func(self.set_books_in_library, 'carda') + self.set_books_in_library(cardalist) self.card_a_view.set_editable(self.device_manager.device.CAN_SET_METADATA) self.card_b_view.set_database(cardblist) - self.card_b_view.model().set_book_in_library_func(self.set_books_in_library, 'cardb') + self.set_books_in_library(cardblist) self.card_b_view.set_editable(self.device_manager.device.CAN_SET_METADATA) for view in (self.memory_view, self.card_a_view, self.card_b_view): view.sortByColumn(3, Qt.DescendingOrder)