From 60083e325c59a134586722d57ee515ae0b49d576 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Sun, 3 Oct 2010 12:57:48 +0100 Subject: [PATCH] Secret feature: sony driver can use plugboard when writing metadata to its DB. --- resources/default_tweaks.py | 10 +++++ src/calibre/devices/prs505/driver.py | 13 +++++- src/calibre/devices/prs505/sony_cache.py | 15 ++++--- src/calibre/gui2/device.py | 50 +++++++++++++++--------- 4 files changed, 60 insertions(+), 28 deletions(-) diff --git a/resources/default_tweaks.py b/resources/default_tweaks.py index 095eba0c3d..3b60e3410e 100644 --- a/resources/default_tweaks.py +++ b/resources/default_tweaks.py @@ -171,3 +171,13 @@ content_server_wont_display = [''] # level sorts, and if you are seeing a slowdown, reduce the value of this tweak. maximum_resort_levels = 5 + +# Tell the Sony driver to apply the plugboard specified by the given format +# before writing to its database. This can be used to change the title, etc, +# in the DB, and therefore what the Sony displays. +# Example: +# sony_db_use_plugboard_format='epub' +# Apply the epub plugboard before writing to the Sony DB. +# Default: '' +sony_db_use_plugboard_format='' + diff --git a/src/calibre/devices/prs505/driver.py b/src/calibre/devices/prs505/driver.py index 7952660c21..e69851253a 100644 --- a/src/calibre/devices/prs505/driver.py +++ b/src/calibre/devices/prs505/driver.py @@ -14,6 +14,7 @@ from calibre.devices.prs505 import CACHE_XML from calibre.devices.prs505.sony_cache import XMLCache from calibre import __appname__ from calibre.devices.usbms.books import CollectionsBookList +from calibre.utils.config import tweaks class PRS505(USBMS): @@ -63,6 +64,8 @@ class PRS505(USBMS): 'series, tags, authors' EXTRA_CUSTOMIZATION_DEFAULT = ', '.join(['series', 'tags']) + plugboard = None + def windows_filter_pnp_id(self, pnp_id): return '_LAUNCHER' in pnp_id @@ -150,7 +153,7 @@ class PRS505(USBMS): else: collections = [] debug_print('PRS505: collection fields:', collections) - c.update(blists, collections) + c.update(blists, collections, self.plugboard) c.write() USBMS.sync_booklists(self, booklists, end_session=end_session) @@ -163,3 +166,11 @@ class PRS505(USBMS): c.write() debug_print('PRS505: finished rebuild_collections') + def use_plugboard_ext(self): + ext = tweaks.get('sony_db_use_plugboard_format', None) + return ext + + def set_plugboard(self, pb): + if tweaks.get('sony_db_use_plugboard_format', None): + debug_print('PRS505: use plugboard', pb) + self.plugboard = pb \ No newline at end of file diff --git a/src/calibre/devices/prs505/sony_cache.py b/src/calibre/devices/prs505/sony_cache.py index 879f86d66a..5247e051f1 100644 --- a/src/calibre/devices/prs505/sony_cache.py +++ b/src/calibre/devices/prs505/sony_cache.py @@ -325,12 +325,6 @@ class XMLCache(object): for book in bl: record = lpath_map.get(book.lpath, None) if record is not None: - title = record.get('title', None) - if title is not None and title != book.title: - debug_print('Renaming title', book.title, 'to', title) - book.title = title - # Don't set the author, because the reader strips all but - # the first author. for thumbnail in record.xpath( 'descendant::*[local-name()="thumbnail"]'): for img in thumbnail.xpath( @@ -350,7 +344,7 @@ class XMLCache(object): # }}} # Update XML from JSON {{{ - def update(self, booklists, collections_attributes): + def update(self, booklists, collections_attributes, plugboard): debug_print('Starting update', collections_attributes) use_tz_var = False for i, booklist in booklists.items(): @@ -365,8 +359,13 @@ class XMLCache(object): record = lpath_map.get(book.lpath, None) if record is None: record = self.create_text_record(root, i, book.lpath) + if plugboard is not None: + newmi = book.deepcopy() + newmi.template_to_attribute(book, plugboard) + else: + newmi = book (gtz_count, ltz_count, use_tz_var) = \ - self.update_text_record(record, book, path, i, + self.update_text_record(record, newmi, path, i, gtz_count, ltz_count, use_tz_var) # Ensure the collections in the XML database are recorded for # this book diff --git a/src/calibre/gui2/device.py b/src/calibre/gui2/device.py index c8a2325a0a..d06b77b4e2 100644 --- a/src/calibre/gui2/device.py +++ b/src/calibre/gui2/device.py @@ -310,7 +310,13 @@ class DeviceManager(Thread): # {{{ self.device.sync_booklists(booklists, end_session=False) return self.device.card_prefix(end_session=False), self.device.free_space() - def sync_booklists(self, done, booklists): + def sync_booklists(self, done, booklists, plugboards): + if hasattr(self.connected_device, 'use_plugboard_ext') and \ + callable(self.connected_device.use_plugboard_ext): + ext = self.connected_device.use_plugboard_ext() + if ext is not None: + self.connected_device.set_plugboard( + self.find_plugboard(ext, plugboards)) return self.create_job(self._sync_booklists, done, args=[booklists], description=_('Send metadata to device')) @@ -319,28 +325,31 @@ class DeviceManager(Thread): # {{{ args=[booklist, on_card], description=_('Send collections to device')) + def find_plugboard(self, ext, plugboards): + dev_name = self.connected_device.__class__.__name__ + cpb = None + if ext in plugboards: + cpb = plugboards[ext] + elif plugboard_any_format_value in plugboards: + cpb = plugboards[plugboard_any_format_value] + if cpb is not None: + if dev_name in cpb: + cpb = cpb[dev_name] + elif plugboard_any_device_value in cpb: + cpb = cpb[plugboard_any_device_value] + else: + cpb = None + if DEBUG: + prints('Device using plugboard', ext, dev_name, cpb) + return cpb + def _upload_books(self, files, names, on_card=None, metadata=None, plugboards=None): '''Upload books to device: ''' if metadata and files and len(metadata) == len(files): for f, mi in zip(files, metadata): if isinstance(f, unicode): ext = f.rpartition('.')[-1].lower() - dev_name = self.connected_device.__class__.__name__ - cpb = None - if ext in plugboards: - cpb = plugboards[ext] - elif plugboard_any_format_value in plugboards: - cpb = plugboards[plugboard_any_format_value] - if cpb is not None: - if dev_name in cpb: - cpb = cpb[dev_name] - elif plugboard_any_device_value in cpb: - cpb = cpb[plugboard_any_device_value] - else: - cpb = None - - if DEBUG: - prints('Device using plugboard', ext, dev_name, cpb) + cpb = self.find_plugboard(ext, plugboards) if ext: try: if DEBUG: @@ -1247,8 +1256,9 @@ class DeviceMixin(object): # {{{ ''' Upload metadata to device. ''' + plugboards = self.library_view.model().db.prefs.get('plugboards', {}) self.device_manager.sync_booklists(Dispatcher(self.metadata_synced), - self.booklists()) + self.booklists(), plugboards) def metadata_synced(self, job): ''' @@ -1502,8 +1512,10 @@ class DeviceMixin(object): # {{{ if update_metadata: if self.device_manager.is_device_connected: + plugboards = self.library_view.model().db.prefs.get('plugboards', {}) self.device_manager.sync_booklists( - Dispatcher(self.metadata_synced), booklists) + Dispatcher(self.metadata_synced), booklists, + plugboards) return update_metadata # }}}