From 60b3f216f7e62c2064d004f41d21539b9d8ad4c8 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 18 Sep 2012 17:18:36 +0530 Subject: [PATCH] MTP driver: Add infrastructure to set per device default settings. Create default settings for Kindle Fire. Fix ejecting of devices broken on linux --- src/calibre/devices/mtp/defaults.py | 54 +++++++++++++++++++ src/calibre/devices/mtp/driver.py | 14 ++++- src/calibre/devices/mtp/unix/driver.py | 5 ++ src/calibre/devices/mtp/windows/driver.py | 4 ++ src/calibre/gui2/device_drivers/mtp_config.py | 2 +- 5 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 src/calibre/devices/mtp/defaults.py diff --git a/src/calibre/devices/mtp/defaults.py b/src/calibre/devices/mtp/defaults.py new file mode 100644 index 0000000000..2b72aa77cc --- /dev/null +++ b/src/calibre/devices/mtp/defaults.py @@ -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 ' +__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 {} + + diff --git a/src/calibre/devices/mtp/driver.py b/src/calibre/devices/mtp/driver.py index fa1f87cf41..fa37d33889 100644 --- a/src/calibre/devices/mtp/driver.py +++ b/src/calibre/devices/mtp/driver.py @@ -14,6 +14,7 @@ from itertools import izip from calibre import prints from calibre.constants import iswindows, numeric_version from calibre.devices.mtp.base import debug +from calibre.devices.mtp.defaults import DeviceDefaults from calibre.ptempfile import SpooledTemporaryFile, PersistentTemporaryDirectory from calibre.utils.config import from_json, to_json, JSONConfig from calibre.utils.date import now, isoformat, utcnow @@ -42,6 +43,8 @@ class MTP_DEVICE(BASE): BASE.__init__(self, *args, **kwargs) self.plugboards = self.plugboard_func = None self._prefs = None + self.device_defaults = DeviceDefaults() + self.current_device_defaults = {} @property def prefs(self): @@ -85,6 +88,8 @@ class MTP_DEVICE(BASE): isoformat(utcnow())) self.prefs['history'] = h + self.current_device_defaults = self.device_defaults(device, self) + # Device information {{{ def _update_drive_info(self, storage, location_code, name=None): import uuid @@ -439,8 +444,13 @@ class MTP_DEVICE(BASE): # Settings {{{ def get_pref(self, key): - return self.prefs.get('device-%s'%self.current_serial_num, {}).get(key, - self.prefs[key]) + ''' Get the setting named key. First looks for a device specific setting. + 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): from calibre.gui2.device_drivers.mtp_config import MTPConfig diff --git a/src/calibre/devices/mtp/unix/driver.py b/src/calibre/devices/mtp/unix/driver.py index 5472834453..d86262c78b 100644 --- a/src/calibre/devices/mtp/unix/driver.py +++ b/src/calibre/devices/mtp/unix/driver.py @@ -137,6 +137,10 @@ class MTP_DEVICE(MTPDeviceBase): self.currently_connected_dev = None self.current_serial_num = None + @property + def is_mtp_device_connected(self): + return self.currently_connected_dev is not None + @synchronous def startup(self): p = plugins['libmtp'] @@ -189,6 +193,7 @@ class MTP_DEVICE(MTPDeviceBase): if not self.current_friendly_name: self.current_friendly_name = self.dev.model_name or _('Unknown MTP device') self.current_serial_num = snum + self.currently_connected_dev = connected_device @property def filesystem_cache(self): diff --git a/src/calibre/devices/mtp/windows/driver.py b/src/calibre/devices/mtp/windows/driver.py index 08f9490b54..22079c287b 100644 --- a/src/calibre/devices/mtp/windows/driver.py +++ b/src/calibre/devices/mtp/windows/driver.py @@ -246,6 +246,10 @@ class MTP_DEVICE(MTPDeviceBase): self.dev = self._filesystem_cache = 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): if self.currently_connected_pnp_id is None: return self.eject_dev_on_next_scan = True diff --git a/src/calibre/gui2/device_drivers/mtp_config.py b/src/calibre/gui2/device_drivers/mtp_config.py index d0ba5cb2e1..cd7e495225 100644 --- a/src/calibre/gui2/device_drivers/mtp_config.py +++ b/src/calibre/gui2/device_drivers/mtp_config.py @@ -400,7 +400,7 @@ class MTPConfig(QTabWidget): p = self.device.prefs.get(self.current_device_key, {}) if not p: self.device.prefs[self.current_device_key] = p - return p.get(key, self.device.prefs[key]) + return self.device.get_pref(key) @property def device(self):