diff --git a/src/calibre/devices/interface.py b/src/calibre/devices/interface.py index 954f19560f..610af7a687 100644 --- a/src/calibre/devices/interface.py +++ b/src/calibre/devices/interface.py @@ -648,6 +648,14 @@ class DevicePlugin(Plugin): ''' device_prefs.set_overrides() + def set_library_info(self, library_name, library_uuid, field_metadata): + ''' + Implement this method if you want information about the current calibre + library. This method is called at startup and when the calibre library + changes while connected. + ''' + pass + # Dynamic control interface. # The following methods are probably called on the GUI thread. Any driver # that implements these methods must take pains to be thread safe, because diff --git a/src/calibre/devices/smart_device_app/driver.py b/src/calibre/devices/smart_device_app/driver.py index 4be2829413..6e10266992 100644 --- a/src/calibre/devices/smart_device_app/driver.py +++ b/src/calibre/devices/smart_device_app/driver.py @@ -244,6 +244,7 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin): 'OK' : 0, 'BOOK_DONE' : 11, 'CALIBRE_BUSY' : 18, + 'SET_LIBRARY_INFO' : 19, 'DELETE_BOOK' : 13, 'DISPLAY_MESSAGE' : 17, 'FREE_SPACE' : 5, @@ -1040,6 +1041,8 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin): self._debug('Cache uses lpaths', self.client_cache_uses_lpaths) self.can_send_ok_to_sendbook = result.get('canSendOkToSendbook', False) self._debug('Can send OK to sendbook', self.can_send_ok_to_sendbook) + self.can_accept_library_info = result.get('canAcceptLibraryInfo', False) + self._debug('Can accept library info', self.can_accept_library_info) if not self.settings().extra_customization[self.OPT_USE_METADATA_CACHE]: self.client_can_use_metadata_cache = False @@ -1512,6 +1515,16 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin): self.plugboards = plugboards self.plugboard_func = pb_func + @synchronous('sync_lock') + def set_library_info(self, library_name, library_uuid, field_metadata): + self._debug(library_name, library_uuid) + if self.can_accept_library_info: + self._call_client('SET_LIBRARY_INFO', + {'libraryName' : library_name, + 'libraryUuid': library_uuid, + 'fieldMetadata': field_metadata.all_metadata()}, + print_debug_info=True) + @synchronous('sync_lock') def specialize_global_preferences(self, device_prefs): device_prefs.set_overrides(manage_device_metadata='on_connect') diff --git a/src/calibre/gui2/device.py b/src/calibre/gui2/device.py index 705f3d4b62..268a0d8351 100644 --- a/src/calibre/gui2/device.py +++ b/src/calibre/gui2/device.py @@ -34,6 +34,7 @@ from calibre.constants import DEBUG from calibre.utils.config import tweaks, device_prefs from calibre.utils.magick.draw import thumbnail from calibre.library.save_to_disk import find_plugboard +from calibre.library import current_library_name # }}} class DeviceJob(BaseJob): # {{{ @@ -473,6 +474,17 @@ class DeviceManager(Thread): # {{{ return self.create_job_step(self._get_device_information, done, description=_('Get device information'), to_job=add_as_step_to_job) + def _set_library_information(self, library_name, library_uuid, field_metadata): + '''Give the device the current library information''' + self.device.set_library_info(library_name, library_uuid, field_metadata) + + def set_library_information(self, done, library_name, library_uuid, + field_metadata, add_as_step_to_job=None): + '''Give the device the current library information''' + return self.create_job_step(self._set_library_information, done, + args=[library_name, library_uuid, field_metadata], + description=_('Get device information'), to_job=add_as_step_to_job) + def slow_driveinfo(self): ''' Update the stored device information with the driveinfo if the device indicates that getting driveinfo is slow ''' @@ -1081,6 +1093,10 @@ class DeviceMixin(object): # {{{ self.device_manager.device.icon) self.bars_manager.update_bars() self.status_bar.device_connected(info[0]) + db = self.library_view.model().db + self.device_manager.set_library_information(None, current_library_name(), + db.library_id, db.field_metadata, + add_as_step_to_job=job) self.device_manager.books(FunctionDispatcher(self.metadata_downloaded), add_as_step_to_job=job) @@ -1638,6 +1654,12 @@ class DeviceMixin(object): # {{{ self.set_books_in_library(self.booklists(), reset=True, force_send=True) self.refresh_ondevice() + def set_current_library_information(self, library_name, library_uuid, field_metadata): + self.device_manager.set_current_library_uuid(library_uuid) + if self.device_manager.is_device_connected: + self.device_manager.set_library_information(None, library_name, + library_uuid, field_metadata) + def book_on_device(self, id, reset=False): ''' Return an indication of whether the given book represented by its db id diff --git a/src/calibre/gui2/ui.py b/src/calibre/gui2/ui.py index 38eac12ccc..ee9cd006a1 100644 --- a/src/calibre/gui2/ui.py +++ b/src/calibre/gui2/ui.py @@ -47,6 +47,7 @@ from calibre.gui2.auto_add import AutoAdder from calibre.gui2.proceed import ProceedQuestion from calibre.gui2.dialogs.message_box import JobError from calibre.gui2.job_indicator import Pointer +from calibre.library import current_library_name class Listener(Thread): # {{{ @@ -419,7 +420,8 @@ class Main(MainWindow, MainWindowMixin, DeviceMixin, EmailMixin, # {{{ traceback.print_exc() if ac.plugin_path is None: raise - self.device_manager.set_current_library_uuid(db.library_id) + self.set_current_library_information(current_library_name(), db.library_id, + db.field_metadata) self.keyboard.finalize() self.auto_adder = AutoAdder(gprefs['auto_add_path'], self) @@ -693,7 +695,8 @@ class Main(MainWindow, MainWindowMixin, DeviceMixin, EmailMixin, # {{{ self.memory_view.reset() self.card_a_view.reset() self.card_b_view.reset() - self.device_manager.set_current_library_uuid(db.library_id) + self.set_current_library_information(current_library_name(), db.library_id, + db.field_metadata) self.library_view.set_current_row(0) # Run a garbage collection now so that it does not freeze the # interface later