mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-07 18:24:30 -04:00
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:
parent
a544567b3d
commit
60b3f216f7
54
src/calibre/devices/mtp/defaults.py
Normal file
54
src/calibre/devices/mtp/defaults.py
Normal 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 {}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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):
|
||||
|
@ -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
|
||||
|
@ -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):
|
||||
|
Loading…
x
Reference in New Issue
Block a user