Add an 'Ignored devices' section to Preferences to allow users to easily un-ignore previously ignored devices

This commit is contained in:
Kovid Goyal 2012-09-21 00:12:56 +05:30
parent 3b04ab060a
commit 1b7dd5cff5
4 changed files with 115 additions and 2 deletions

View File

@ -1119,6 +1119,19 @@ class MetadataSources(PreferencesPlugin):
config_widget = 'calibre.gui2.preferences.metadata_sources'
description = _('Control how calibre downloads ebook metadata from the net')
class IgnoredDevices(PreferencesPlugin):
name = 'Ignored Devices'
icon = I('reader.png')
gui_name = _('Ignored devices')
category = 'Sharing'
gui_category = _('Sharing')
category_order = 4
name_order = 4
config_widget = 'calibre.gui2.preferences.ignored_devices'
description = _('Control which devices calibre will ignore when they are connected '
'to the computer.')
class Plugins(PreferencesPlugin):
name = 'Plugins'
icon = I('plugins.png')
@ -1167,7 +1180,7 @@ class Misc(PreferencesPlugin):
plugins += [LookAndFeel, Behavior, Columns, Toolbar, Search, InputOptions,
CommonOptions, OutputOptions, Adding, Saving, Sending, Plugboard,
Email, Server, Plugins, Tweaks, Misc, TemplateFunctions,
MetadataSources, Keyboard]
MetadataSources, Keyboard, IgnoredDevices]
#}}}

View File

@ -102,7 +102,9 @@ class DevicePlugin(Plugin):
#: If set to True, calibre will ask the user if they want to manage the
#: device with calibre, the first time it is detected. If you set this to
#: True you must implement :meth:`get_device_uid()` and
#: :meth:`ignore_connected_device()`.
#: :meth:`ignore_connected_device()` and
#: :meth:`get_user_blacklisted_devices` and
#: :meth:`set_user_blacklisted_devices`
ASK_TO_ALLOW_CONNECT = False
@classmethod
@ -611,6 +613,19 @@ class DevicePlugin(Plugin):
'''
raise NotImplementedError()
def get_user_blacklisted_devices(self):
'''
Return map of device uid to friendly name for all devices that the user
has asked to be ignored.
'''
return {}
def set_user_blacklisted_devices(self, devices):
'''
Set the list of device uids that should be ignored by this driver.
'''
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

View File

@ -481,8 +481,21 @@ class MTP_DEVICE(BASE):
def save_template(self):
return self.get_pref('send_template')
def get_user_blacklisted_devices(self):
bl = frozenset(self.prefs['blacklist'])
ans = {}
for dev, x in self.prefs['history'].iteritems():
name = x[0]
if dev in bl:
ans[dev] = name
return ans
def set_user_blacklisted_devices(self, devs):
self.prefs['blacklist'] = list(devs)
# }}}
if __name__ == '__main__':
dev = MTP_DEVICE(None)
dev.startup()

View File

@ -0,0 +1,72 @@
#!/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'
from PyQt4.Qt import (QLabel, QVBoxLayout, QListWidget, QListWidgetItem, Qt)
from calibre.gui2.preferences import ConfigWidgetBase, test_widget
class ConfigWidget(ConfigWidgetBase):
restart_critical = False
def genesis(self, gui):
self.gui = gui
self.l = l = QVBoxLayout()
self.setLayout(l)
self.la = la = QLabel(_(
'The list of devices that you have asked calibre to ignore. '
'Uncheck a device to have calibre stop ignoring it.'))
la.setWordWrap(True)
l.addWidget(la)
self.devices = f = QListWidget(self)
l.addWidget(f)
f.itemChanged.connect(self.changed_signal)
f.itemDoubleClicked.connect(self.toggle_item)
def toggle_item(self, item):
item.setCheckState(Qt.Checked if item.checkState() == Qt.Unchecked else
Qt.Unchecked)
def initialize(self):
self.devices.blockSignals(True)
self.devices.clear()
for dev in self.gui.device_manager.devices:
for d, name in dev.get_user_blacklisted_devices().iteritems():
item = QListWidgetItem('%s [%s]'%(name, d), self.devices)
item.setData(Qt.UserRole, (dev, d))
item.setFlags(Qt.ItemIsEnabled|Qt.ItemIsUserCheckable|Qt.ItemIsSelectable)
item.setCheckState(Qt.Checked)
self.devices.blockSignals(False)
def restore_defaults(self):
if self.devices.count() > 0:
self.devices.clear()
def commit(self):
devs = {}
for i in xrange(0, self.devices.count()):
e = self.devices.item(i)
dev, uid = e.data(Qt.UserRole).toPyObject()
if dev not in devs:
devs[dev] = []
if e.checkState() == Qt.Checked:
devs[dev].append(uid)
for dev, bl in devs.iteritems():
dev.set_user_blacklisted_devices(bl)
return True # Restart required
if __name__ == '__main__':
from PyQt4.Qt import QApplication
app = QApplication([])
test_widget('Sharing', 'Ignored Devices')