Update on device and in library columns when sending books to device

This commit is contained in:
Kovid Goyal 2010-05-09 16:35:45 -06:00
parent a2bf9e3696
commit 07282e7a79
3 changed files with 48 additions and 36 deletions

View File

@ -971,13 +971,29 @@ class DeviceGUI(object):
self.upload_booklists() self.upload_booklists()
books_to_be_deleted = []
if memory and memory[1]:
books_to_be_deleted = memory[1]
self.library_view.model().delete_books_by_id(books_to_be_deleted)
self.set_books_in_library(self.booklists(),
reset=bool(books_to_be_deleted))
view = self.card_a_view if on_card == 'carda' else self.card_b_view if on_card == 'cardb' else self.memory_view view = self.card_a_view if on_card == 'carda' else self.card_b_view if on_card == 'cardb' else self.memory_view
view.model().resort(reset=False) view.model().resort(reset=False)
view.model().research() view.model().research()
for f in files: for f in files:
getattr(f, 'close', lambda : True)() getattr(f, 'close', lambda : True)()
if memory and memory[1]:
self.library_view.model().delete_books_by_id(memory[1]) self.book_on_device(None, reset=True)
if metadata:
changed = set([])
for mi in metadata:
id_ = getattr(mi, 'application_id', None)
if id_ is not None:
changed.add(id_)
if changed:
self.library_view.model().refresh_ids(list(changed))
def book_on_device(self, index, format=None, reset=False): def book_on_device(self, index, format=None, reset=False):
loc = [None, None, None] loc = [None, None, None]
@ -1013,34 +1029,32 @@ class DeviceGUI(object):
break break
return loc return loc
def set_books_in_library(self, booklist, reset = False): def set_books_in_library(self, booklists, reset=False):
if reset: if reset:
self.book_in_library_cache = None # First build a self.book_in_library_cache of the library, so the search isn't On**2
return self.book_in_library_cache = {}
for id, title in self.library_view.model().db.all_titles():
# First build a self.book_in_library_cache of the library, so the search isn't On**2 title = re.sub('(?u)\W|[_]', '', title.lower())
self.book_in_library_cache = {} if title not in self.book_in_library_cache:
for id, title in self.library_view.model().db.all_titles(): self.book_in_library_cache[title] = {'authors':set(), 'db_ids':set()}
title = re.sub('(?u)\W|[_]', '', title.lower()) au = self.library_view.model().db.authors(id, index_is_id=True)
if title not in self.book_in_library_cache: authors = au.lower() if au else ''
self.book_in_library_cache[title] = {'authors':set(), 'db_ids':set()} authors = re.sub('(?u)\W|[_]', '', authors)
au = self.library_view.model().db.authors(id, index_is_id=True) self.book_in_library_cache[title]['authors'].add(authors)
authors = au.lower() if au else '' self.book_in_library_cache[title]['db_ids'].add(id)
authors = re.sub('(?u)\W|[_]', '', authors)
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 # Now iterate through all the books on the device, setting the in_library field
for book in booklist: for booklist in booklists:
book_title = book.title.lower() if book.title else '' for book in booklist:
book_title = re.sub('(?u)\W|[_]', '', book_title) book_title = book.title.lower() if book.title else ''
book.in_library = False book_title = re.sub('(?u)\W|[_]', '', book_title)
d = self.book_in_library_cache.get(book_title, None) book.in_library = False
if d is not None: d = self.book_in_library_cache.get(book_title, None)
if book.db_id in d['db_ids']: if d is not None:
book.in_library = True if book.db_id in d['db_ids']:
continue book.in_library = True
book_authors = authors_to_string(book.authors).lower() if book.authors else '' continue
book_authors = re.sub('(?u)\W|[_]', '', book_authors) book_authors = authors_to_string(book.authors).lower() if book.authors else ''
if book_authors in d['authors']: book_authors = re.sub('(?u)\W|[_]', '', book_authors)
book.in_library = True if book_authors in d['authors']:
book.in_library = True

View File

@ -390,8 +390,8 @@ class BooksModel(QAbstractTableModel):
if row == current_row: if row == current_row:
self.emit(SIGNAL('new_bookdisplay_data(PyQt_PyObject)'), self.emit(SIGNAL('new_bookdisplay_data(PyQt_PyObject)'),
self.get_book_display_info(row)) self.get_book_display_info(row))
self.emit(SIGNAL('dataChanged(QModelIndex,QModelIndex)'), self.dataChanged.emit(self.index(row, 0), self.index(row,
self.index(row, 0), self.index(row, self.columnCount(QModelIndex())-1)) self.columnCount(QModelIndex())-1))
def close(self): def close(self):
self.db.close() self.db.close()
@ -724,6 +724,7 @@ class BooksModel(QAbstractTableModel):
img = self.default_image img = self.default_image
return img return img
def build_data_convertors(self): def build_data_convertors(self):
def authors(r, idx=-1): def authors(r, idx=-1):
au = self.db.data[r][idx] au = self.db.data[r][idx]

View File

@ -992,16 +992,13 @@ class Main(MainWindow, Ui_MainWindow, DeviceGUI):
else: else:
self.device_job_exception(job) self.device_job_exception(job)
return return
self.set_books_in_library(None, reset=True) self.set_books_in_library(job.result, reset=True)
mainlist, cardalist, cardblist = job.result mainlist, cardalist, cardblist = job.result
self.memory_view.set_database(mainlist) self.memory_view.set_database(mainlist)
self.set_books_in_library(mainlist)
self.memory_view.set_editable(self.device_manager.device.CAN_SET_METADATA) self.memory_view.set_editable(self.device_manager.device.CAN_SET_METADATA)
self.card_a_view.set_database(cardalist) self.card_a_view.set_database(cardalist)
self.set_books_in_library(cardalist)
self.card_a_view.set_editable(self.device_manager.device.CAN_SET_METADATA) self.card_a_view.set_editable(self.device_manager.device.CAN_SET_METADATA)
self.card_b_view.set_database(cardblist) self.card_b_view.set_database(cardblist)
self.set_books_in_library(cardblist)
self.card_b_view.set_editable(self.device_manager.device.CAN_SET_METADATA) 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): for view in (self.memory_view, self.card_a_view, self.card_b_view):
view.sortByColumn(3, Qt.DescendingOrder) view.sortByColumn(3, Qt.DescendingOrder)