Add global device preferences. Add interface to override device preferences. Add override to smartapp to force metadata management to automatic for the device. Make changes everywhere else reference device_prefs instead of prefs for metadata management.

This commit is contained in:
Charles Haley 2012-10-03 10:09:48 +02:00
parent f386fcb539
commit 7a79a9307d
7 changed files with 52 additions and 35 deletions

View File

@ -5,6 +5,7 @@ from collections import namedtuple
from calibre.customize import Plugin from calibre.customize import Plugin
from calibre.constants import iswindows from calibre.constants import iswindows
from calibre.utils.config import prefs
class DevicePlugin(Plugin): class DevicePlugin(Plugin):
""" """
@ -626,6 +627,21 @@ class DevicePlugin(Plugin):
''' '''
pass pass
def specialize_global_preferences(self, device_prefs, add_specializations):
'''
Implement this method if your device wants to override a particular
preference. You must ensure that all call sites that want a preference
that can be overridden use device_prefs['something'] instead
of prefs['something']. If add_specializations is True, then your
method should call device_prefs.set_overrides(pref=val, pref=val, ...).
If add_specializations is False, then your method should call
device_prefs.set_overrides() to remove any previous specialization.
Currently used for:
metadata management (prefs['manage_device_metadata'])
'''
pass
# Dynamic control interface. # Dynamic control interface.
# The following methods are probably called on the GUI thread. Any driver # The following methods are probably called on the GUI thread. Any driver
# that implements these methods must take pains to be thread safe, because # that implements these methods must take pains to be thread safe, because

View File

@ -34,7 +34,7 @@ from calibre.library import current_library_name
from calibre.library.server import server_config as content_server_config from calibre.library.server import server_config as content_server_config
from calibre.ptempfile import PersistentTemporaryFile from calibre.ptempfile import PersistentTemporaryFile
from calibre.utils.ipc import eintr_retry_call from calibre.utils.ipc import eintr_retry_call
from calibre.utils.config import from_json, tweaks from calibre.utils.config import from_json, tweaks, prefs
from calibre.utils.date import isoformat, now from calibre.utils.date import isoformat, now
from calibre.utils.filenames import ascii_filename as sanitize, shorten_components_to from calibre.utils.filenames import ascii_filename as sanitize, shorten_components_to
from calibre.utils.mdns import (publish as publish_zeroconf, unpublish as from calibre.utils.mdns import (publish as publish_zeroconf, unpublish as
@ -1197,6 +1197,14 @@ class SMART_DEVICE_APP(DeviceConfig, DevicePlugin):
self.plugboards = plugboards self.plugboards = plugboards
self.plugboard_func = pb_func self.plugboard_func = pb_func
@synchronous('sync_lock')
def specialize_global_preferences(self, device_prefs, add_specializations):
self._debug('add', add_specializations)
if add_specializations:
device_prefs.set_overrides(manage_device_metadata='on_connect')
else:
device_prefs.set_overrides()
@synchronous('sync_lock') @synchronous('sync_lock')
def startup(self): def startup(self):
self.listen_socket = None self.listen_socket = None

View File

@ -12,7 +12,7 @@ from calibre.devices.mime import mime_type_ext
from calibre.devices.interface import BookList as _BookList from calibre.devices.interface import BookList as _BookList
from calibre.constants import preferred_encoding from calibre.constants import preferred_encoding
from calibre import isbytestring, force_unicode from calibre import isbytestring, force_unicode
from calibre.utils.config import prefs, tweaks from calibre.utils.config import device_prefs, tweaks
from calibre.utils.icu import strcmp from calibre.utils.icu import strcmp
from calibre.utils.formatter import EvalFormatter from calibre.utils.formatter import EvalFormatter
@ -124,7 +124,7 @@ class CollectionsBookList(BookList):
def get_collections(self, collection_attributes): def get_collections(self, collection_attributes):
from calibre.devices.usbms.driver import debug_print from calibre.devices.usbms.driver import debug_print
debug_print('Starting get_collections:', prefs['manage_device_metadata']) debug_print('Starting get_collections:', device_prefs['manage_device_metadata'])
debug_print('Renaming rules:', tweaks['sony_collection_renaming_rules']) debug_print('Renaming rules:', tweaks['sony_collection_renaming_rules'])
debug_print('Formatting template:', tweaks['sony_collection_name_template']) debug_print('Formatting template:', tweaks['sony_collection_name_template'])
debug_print('Sorting rules:', tweaks['sony_collection_sorting_rules']) debug_print('Sorting rules:', tweaks['sony_collection_sorting_rules'])
@ -132,7 +132,7 @@ class CollectionsBookList(BookList):
# Complexity: we can use renaming rules only when using automatic # Complexity: we can use renaming rules only when using automatic
# management. Otherwise we don't always have the metadata to make the # management. Otherwise we don't always have the metadata to make the
# right decisions # right decisions
use_renaming_rules = prefs['manage_device_metadata'] == 'on_connect' use_renaming_rules = device_prefs['manage_device_metadata'] == 'on_connect'
collections = {} collections = {}
@ -169,7 +169,7 @@ class CollectionsBookList(BookList):
# book in all existing collections. Do not add any new ones. # book in all existing collections. Do not add any new ones.
attrs = ['device_collections'] attrs = ['device_collections']
if getattr(book, '_new_book', False): if getattr(book, '_new_book', False):
if prefs['manage_device_metadata'] == 'manual': if device_prefs['manage_device_metadata'] == 'manual':
# Ensure that the book is in all the book's existing # Ensure that the book is in all the book's existing
# collections plus all metadata collections # collections plus all metadata collections
attrs += collection_attributes attrs += collection_attributes
@ -178,7 +178,7 @@ class CollectionsBookList(BookList):
# thing. The book's existing collections are ignored. Put # thing. The book's existing collections are ignored. Put
# the book in collections defined by its metadata. # the book in collections defined by its metadata.
attrs = collection_attributes attrs = collection_attributes
elif prefs['manage_device_metadata'] == 'on_connect': elif device_prefs['manage_device_metadata'] == 'on_connect':
# For existing books, modify the collections only if the user # For existing books, modify the collections only if the user
# specified 'on_connect' # specified 'on_connect'
attrs = collection_attributes attrs = collection_attributes

View File

@ -30,7 +30,7 @@ from calibre.devices.apple.driver import ITUNES_ASYNC
from calibre.devices.folder_device.driver import FOLDER_DEVICE from calibre.devices.folder_device.driver import FOLDER_DEVICE
from calibre.devices.bambook.driver import BAMBOOK, BAMBOOKWifi from calibre.devices.bambook.driver import BAMBOOK, BAMBOOKWifi
from calibre.constants import DEBUG from calibre.constants import DEBUG
from calibre.utils.config import prefs, tweaks from calibre.utils.config import tweaks, device_prefs
from calibre.utils.magick.draw import thumbnail from calibre.utils.magick.draw import thumbnail
from calibre.library.save_to_disk import find_plugboard from calibre.library.save_to_disk import find_plugboard
# }}} # }}}
@ -210,6 +210,8 @@ class DeviceManager(Thread): # {{{
return return
self.connected_device = dev self.connected_device = dev
self.connected_device.specialize_global_preferences(device_prefs,
add_specializations=True)
self.connected_device_kind = device_kind self.connected_device_kind = device_kind
self.connected_slot(True, device_kind) self.connected_slot(True, device_kind)
@ -235,6 +237,8 @@ class DeviceManager(Thread): # {{{
# is being shut down. # is being shut down.
self.connected_device.shutdown() self.connected_device.shutdown()
self.call_shutdown_on_disconnect = False self.call_shutdown_on_disconnect = False
self.connected_device.specialize_global_preferences(device_prefs,
add_specializations=False)
self.connected_device = None self.connected_device = None
self._device_information = None self._device_information = None
@ -1648,7 +1652,7 @@ class DeviceMixin(object): # {{{
x = x.lower() if x else '' x = x.lower() if x else ''
return string_pat.sub('', x) return string_pat.sub('', x)
update_metadata = prefs['manage_device_metadata'] == 'on_connect' update_metadata = device_prefs['manage_device_metadata'] == 'on_connect'
get_covers = False get_covers = False
if update_metadata and self.device_manager.is_device_connected: if update_metadata and self.device_manager.is_device_connected:

View File

@ -98,23 +98,6 @@ class SmartdeviceDialog(QDialog, Ui_Dialog):
if pw: if pw:
self.password_box.setText(pw) self.password_box.setText(pw)
self.auto_mgmt_button = QPushButton(_('Enable automatic metadata management'))
self.auto_mgmt_button.clicked.connect(self.auto_mgmt_button_clicked)
self.auto_mgmt_button.setToolTip('<p>' +
_('Enabling automatic metadata management tells calibre to send any '
'changes you made to books\' metadata when your device is '
'connected, which is the most useful setting when using the wireless '
'device interface. If automatic metadata management is not '
'enabled, changes are sent only when you re-send the book. You can '
'get more information or change this preference to some other '
'choice at Preferences -> Sending books to devices -> '
'Metadata management')
+ '</p>')
self.buttonBox.addButton(self.auto_mgmt_button, QDialogButtonBox.ActionRole)
if prefs['manage_device_metadata'] == 'on_connect':
self.auto_mgmt_button.setText(_('Automatic metadata management is enabled'))
self.auto_mgmt_button.setEnabled(False)
forced_ip = self.device_manager.get_option('smartdevice', 'force_ip_address') forced_ip = self.device_manager.get_option('smartdevice', 'force_ip_address')
if forced_ip: if forced_ip:
self.ip_addresses.setText(forced_ip) self.ip_addresses.setText(forced_ip)
@ -123,11 +106,6 @@ class SmartdeviceDialog(QDialog, Ui_Dialog):
self.resize(self.sizeHint()) self.resize(self.sizeHint())
def auto_mgmt_button_clicked(self):
self.auto_mgmt_button.setText(_('Automatic metadata management is enabled'))
self.auto_mgmt_button.setEnabled(False)
prefs.set('manage_device_metadata', 'on_connect')
def use_fixed_port_changed(self, state): def use_fixed_port_changed(self, state):
self.fixed_port.setEnabled(state == Qt.Checked) self.fixed_port.setEnabled(state == Qt.Checked)

View File

@ -16,7 +16,7 @@ from calibre.utils.pyparsing import ParseException
from calibre.ebooks.metadata import fmt_sidx, authors_to_string, string_to_authors from calibre.ebooks.metadata import fmt_sidx, authors_to_string, string_to_authors
from calibre.ebooks.metadata.book.base import SafeFormat from calibre.ebooks.metadata.book.base import SafeFormat
from calibre.ptempfile import PersistentTemporaryFile from calibre.ptempfile import PersistentTemporaryFile
from calibre.utils.config import tweaks, prefs from calibre.utils.config import tweaks, prefs, device_prefs
from calibre.utils.date import dt_factory, qt_to_dt, as_local_time from calibre.utils.date import dt_factory, qt_to_dt, as_local_time
from calibre.utils.icu import sort_key from calibre.utils.icu import sort_key
from calibre.utils.search_query_parser import SearchQueryParser from calibre.utils.search_query_parser import SearchQueryParser
@ -1152,7 +1152,7 @@ class DeviceBooksModel(BooksModel): # {{{
(cname != 'collections' or \ (cname != 'collections' or \
(callable(getattr(self.db, 'supports_collections', None)) and \ (callable(getattr(self.db, 'supports_collections', None)) and \
self.db.supports_collections() and \ self.db.supports_collections() and \
prefs['manage_device_metadata']=='manual')): device_prefs['manage_device_metadata']=='manual')):
flags |= Qt.ItemIsEditable flags |= Qt.ItemIsEditable
return flags return flags
@ -1447,7 +1447,7 @@ class DeviceBooksModel(BooksModel): # {{{
self.editable = ['title', 'authors', 'collections'] self.editable = ['title', 'authors', 'collections']
else: else:
self.editable = [] self.editable = []
if prefs['manage_device_metadata']=='on_connect': if device_prefs['manage_device_metadata']=='on_connect':
self.editable = [] self.editable = []
# }}} # }}}

View File

@ -368,7 +368,18 @@ class JSONConfig(XMLConfig):
dict.__setitem__(self, key, val) dict.__setitem__(self, key, val)
self.commit() self.commit()
class DevicePrefs:
def __init__(self, global_prefs):
self.global_prefs = global_prefs
self.overrides = {}
def set_overrides(self, **kwargs):
self.overrides = kwargs.copy()
def __getitem__(self, key):
return self.overrides.get(key, self.global_prefs[key])
device_prefs = DevicePrefs(prefs)