MTP driver: Add infrastructure to set per device default settings. Create default settings for Kindle Fire. Fix ejecting of devices broken on linux

This commit is contained in:
Kovid Goyal 2012-09-18 17:18:36 +05:30
parent a544567b3d
commit 60b3f216f7
5 changed files with 76 additions and 3 deletions

View File

@ -0,0 +1,54 @@
#!/usr/bin/env python
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:fdm=marker:ai
from __future__ import (unicode_literals, division, absolute_import,
print_function)
__license__ = 'GPL v3'
__copyright__ = '2012, Kovid Goyal <kovid at kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
import traceback, re
from calibre.constants import iswindows
class DeviceDefaults(object):
def __init__(self):
self.rules = (
# Amazon devices
({'vendor':0x1949}, {
'format_map': ['azw3', 'mobi', 'azw',
'azw1', 'azw4', 'pdf'],
'send_to': ['documents', 'books', 'kindle'],
}
),
)
def __call__(self, device, driver):
if iswindows:
vid = pid = 0xffff
m = re.search(r'(?i)vid_([0-9a-fA-F]+)&pid_([0-9a-fA-F]+)', device)
if m is not None:
try:
vid, pid = int(m.group(1), 16), int(m.group(2), 16)
except:
traceback.print_exc()
else:
vid, pid = device.vendor_id, device.product_id
for rule in self.rules:
tests = rule[0]
matches = True
for k, v in tests.iteritems():
if k == 'vendor' and v != vid:
matches = False
break
if k == 'product' and v != pid:
matches = False
break
if matches:
return rule[1]
return {}

View File

@ -14,6 +14,7 @@ from itertools import izip
from calibre import prints from calibre import prints
from calibre.constants import iswindows, numeric_version from calibre.constants import iswindows, numeric_version
from calibre.devices.mtp.base import debug from calibre.devices.mtp.base import debug
from calibre.devices.mtp.defaults import DeviceDefaults
from calibre.ptempfile import SpooledTemporaryFile, PersistentTemporaryDirectory from calibre.ptempfile import SpooledTemporaryFile, PersistentTemporaryDirectory
from calibre.utils.config import from_json, to_json, JSONConfig from calibre.utils.config import from_json, to_json, JSONConfig
from calibre.utils.date import now, isoformat, utcnow from calibre.utils.date import now, isoformat, utcnow
@ -42,6 +43,8 @@ class MTP_DEVICE(BASE):
BASE.__init__(self, *args, **kwargs) BASE.__init__(self, *args, **kwargs)
self.plugboards = self.plugboard_func = None self.plugboards = self.plugboard_func = None
self._prefs = None self._prefs = None
self.device_defaults = DeviceDefaults()
self.current_device_defaults = {}
@property @property
def prefs(self): def prefs(self):
@ -85,6 +88,8 @@ class MTP_DEVICE(BASE):
isoformat(utcnow())) isoformat(utcnow()))
self.prefs['history'] = h self.prefs['history'] = h
self.current_device_defaults = self.device_defaults(device, self)
# Device information {{{ # Device information {{{
def _update_drive_info(self, storage, location_code, name=None): def _update_drive_info(self, storage, location_code, name=None):
import uuid import uuid
@ -439,8 +444,13 @@ class MTP_DEVICE(BASE):
# Settings {{{ # Settings {{{
def get_pref(self, key): def get_pref(self, key):
return self.prefs.get('device-%s'%self.current_serial_num, {}).get(key, ''' Get the setting named key. First looks for a device specific setting.
self.prefs[key]) If that is not found looks for a device default and if that is not
found uses the global default.'''
dd = self.current_device_defaults if self.is_mtp_device_connected else {}
dev_settings = self.prefs.get('device-%s'%self.current_serial_num, {})
default_value = dd.get(key, self.prefs[key])
return dev_settings.get(key, default_value)
def config_widget(self): def config_widget(self):
from calibre.gui2.device_drivers.mtp_config import MTPConfig from calibre.gui2.device_drivers.mtp_config import MTPConfig

View File

@ -137,6 +137,10 @@ class MTP_DEVICE(MTPDeviceBase):
self.currently_connected_dev = None self.currently_connected_dev = None
self.current_serial_num = None self.current_serial_num = None
@property
def is_mtp_device_connected(self):
return self.currently_connected_dev is not None
@synchronous @synchronous
def startup(self): def startup(self):
p = plugins['libmtp'] p = plugins['libmtp']
@ -189,6 +193,7 @@ class MTP_DEVICE(MTPDeviceBase):
if not self.current_friendly_name: if not self.current_friendly_name:
self.current_friendly_name = self.dev.model_name or _('Unknown MTP device') self.current_friendly_name = self.dev.model_name or _('Unknown MTP device')
self.current_serial_num = snum self.current_serial_num = snum
self.currently_connected_dev = connected_device
@property @property
def filesystem_cache(self): def filesystem_cache(self):

View File

@ -246,6 +246,10 @@ class MTP_DEVICE(MTPDeviceBase):
self.dev = self._filesystem_cache = None self.dev = self._filesystem_cache = None
self.current_serial_num = None self.current_serial_num = None
@property
def is_mtp_device_connected(self):
return self.currently_connected_pnp_id is not None
def eject(self): def eject(self):
if self.currently_connected_pnp_id is None: return if self.currently_connected_pnp_id is None: return
self.eject_dev_on_next_scan = True self.eject_dev_on_next_scan = True

View File

@ -400,7 +400,7 @@ class MTPConfig(QTabWidget):
p = self.device.prefs.get(self.current_device_key, {}) p = self.device.prefs.get(self.current_device_key, {})
if not p: if not p:
self.device.prefs[self.current_device_key] = p self.device.prefs[self.current_device_key] = p
return p.get(key, self.device.prefs[key]) return self.device.get_pref(key)
@property @property
def device(self): def device(self):