diff --git a/src/calibre/devices/jetbook/driver.py b/src/calibre/devices/jetbook/driver.py index e4fd840dc0..71b825f5d8 100644 --- a/src/calibre/devices/jetbook/driver.py +++ b/src/calibre/devices/jetbook/driver.py @@ -55,7 +55,13 @@ class JETBOOK(USBMS): au = mi.format_authors() if not au: au = 'Unknown' - return '%s#%s%s' % (au, title, fileext) + suffix = '' + if getattr(mi, 'application_id', None) is not None: + base = fname.rpartition('.')[0] + suffix = '_%s'%mi.application_id + if base.endswith(suffix): + suffix = '' + return '%s#%s%s%s' % (au, title, fileext, suffix) @classmethod def metadata_from_path(cls, path): diff --git a/src/calibre/devices/kindle/driver.py b/src/calibre/devices/kindle/driver.py index 83eae78de0..c3ae0ef868 100644 --- a/src/calibre/devices/kindle/driver.py +++ b/src/calibre/devices/kindle/driver.py @@ -61,6 +61,11 @@ class KINDLE(USBMS): return mi def filename_callback(self, fname, mi): + if getattr(mi, 'application_id', None) is not None: + base = fname.rpartition('.')[0] + suffix = '_%s'%mi.application_id + if not base.endswith(suffix): + fname = base + suffix + '.' + fname.rpartition('.')[-1] if fname.startswith('.'): return 'x'+fname[1:] return fname diff --git a/src/calibre/devices/prs505/driver.py b/src/calibre/devices/prs505/driver.py index f4fc4b0d29..e73a341909 100644 --- a/src/calibre/devices/prs505/driver.py +++ b/src/calibre/devices/prs505/driver.py @@ -121,14 +121,6 @@ class PRS505(CLI, Device): self.report_progress(1.0, _('Getting list of books on device...')) return bl - def filename_callback(self, fname, mi): - if getattr(mi, 'application_id', None) is not None: - base = fname.rpartition('.')[0] - suffix = '_%s'%mi.application_id - if not base.endswith(suffix): - fname = base + suffix + '.' + fname.rpartition('.')[-1] - return fname - def upload_books(self, files, names, on_card=None, end_session=True, metadata=None): diff --git a/src/calibre/devices/usbms/device.py b/src/calibre/devices/usbms/device.py index 897baf82ca..ce6a17d731 100644 --- a/src/calibre/devices/usbms/device.py +++ b/src/calibre/devices/usbms/device.py @@ -784,8 +784,14 @@ class Device(DeviceConfig, DevicePlugin): def filename_callback(self, default, mi): ''' Callback to allow drivers to change the default file name - set by :method:`create_upload_path`. + set by :method:`create_upload_path`. By default, add the DB_ID + to the end of the string. Helps with ondevice doc matching ''' + if getattr(mi, 'application_id', None) is not None: + base = default.rpartition('.')[0] + suffix = '_%s'%mi.application_id + if not base.endswith(suffix): + default = base + suffix + '.' + default.rpartition('.')[-1] return default def sanitize_path_components(self, components): diff --git a/src/calibre/gui2/device.py b/src/calibre/gui2/device.py index b3c01e0119..ab98470a22 100644 --- a/src/calibre/gui2/device.py +++ b/src/calibre/gui2/device.py @@ -979,7 +979,7 @@ class DeviceGUI(object): if memory and memory[1]: self.library_view.model().delete_books_by_id(memory[1]) - def book_on_device(self, index, index_is_id=False, format=None, reset=False): + def book_on_device(self, index, format=None, reset=False): loc = [None, None, None] if reset: @@ -1001,9 +1001,9 @@ class DeviceGUI(object): self.book_on_device_cache[i][book_title]['authors'].add(book_authors) self.book_on_device_cache[i][book_title]['db_ids'].add(book.db_id) - db_title = self.library_view.model().db.title(index, index_is_id).lower() + db_title = self.library_view.model().db.title(index, index_is_id=True).lower() db_title = re.sub('(?u)\W|[_]', '', db_title) - au = self.library_view.model().db.authors(index, index_is_id) + au = self.library_view.model().db.authors(index, index_is_id=True) db_authors = au.lower() if au else '' db_authors = re.sub('(?u)\W|[_]', '', db_authors) for i, l in enumerate(self.booklists()): @@ -1022,19 +1022,25 @@ class DeviceGUI(object): 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()} 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] = id + cache[title]['authors'].add(authors) + 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_authors = authors_to_string(book.authors).lower() if book.authors else '' - book_authors = re.sub('(?u)\W|[_]', '', book_authors) - - if cache.get(book_title + book_authors, None) is not None: - book.in_library = True - else: - book.in_library = False + book.in_library = False + d = cache.get(book_title, None) + if d is not None: + if book.db_id in d['db_ids']: + book.in_library = True + continue + book_authors = authors_to_string(book.authors).lower() if book.authors else '' + book_authors = re.sub('(?u)\W|[_]', '', book_authors) + if book_authors in d['authors']: + book.in_library = True diff --git a/src/calibre/library/caches.py b/src/calibre/library/caches.py index 8830d0538a..9ed150733a 100644 --- a/src/calibre/library/caches.py +++ b/src/calibre/library/caches.py @@ -518,7 +518,7 @@ class ResultCache(SearchQueryParser): try: self._data[id] = db.conn.get('SELECT * from meta2 WHERE id=?', (id,))[0] self._data[id].append(db.has_cover(id, index_is_id=True)) - self._data[id].append(db.book_on_device_string(id, index_is_id=True)) + self._data[id].append(db.book_on_device_string(id)) except IndexError: return None try: @@ -534,7 +534,7 @@ class ResultCache(SearchQueryParser): for id in ids: self._data[id] = db.conn.get('SELECT * from meta2 WHERE id=?', (id,))[0] self._data[id].append(db.has_cover(id, index_is_id=True)) - self._data[id].append(db.book_on_device_string(id, index_is_id=True)) + self._data[id].append(db.book_on_device_string(id)) self._map[0:0] = ids self._map_filtered[0:0] = ids @@ -555,7 +555,7 @@ class ResultCache(SearchQueryParser): for item in self._data: if item is not None: item.append(db.has_cover(item[0], index_is_id=True)) - item.append(db.book_on_device_string(item[0], index_is_id=True)) + item.append(db.book_on_device_string(item[0])) self._map = [i[0] for i in self._data if i is not None] if field is not None: self.sort(field, ascending) diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index 4bc96d2c20..a50c840930 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -470,14 +470,14 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): im = PILImage.open(f) im.convert('RGB').save(path, 'JPEG') - def book_on_device(self, index, index_is_id=False): + def book_on_device(self, index): if self.book_on_device_func: - return self.book_on_device_func(index, index_is_id) + return self.book_on_device_func(index) return None - def book_on_device_string(self, index, index_is_id=False): + def book_on_device_string(self, index): loc = [] - on = self.book_on_device(index, index_is_id) + on = self.book_on_device(index) if on is not None: m, a, b = on if m is not None: