From 5556a1604c9ffb17b353a58269f27e4d02f39cda Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 10 Apr 2011 12:11:07 -0600 Subject: [PATCH] ... --- src/calibre/customize/ui.py | 11 ++ src/calibre/ebooks/metadata/sources/amazon.py | 3 +- .../gui2/preferences/metadata_sources.py | 147 +++++++++++++++++- .../gui2/preferences/metadata_sources.ui | 32 +++- 4 files changed, 184 insertions(+), 9 deletions(-) diff --git a/src/calibre/customize/ui.py b/src/calibre/customize/ui.py index b8abbf9b03..e8011e9ad8 100644 --- a/src/calibre/customize/ui.py +++ b/src/calibre/customize/ui.py @@ -75,6 +75,17 @@ def enable_plugin(plugin_or_name): ep.add(x) config['enabled_plugins'] = ep +def restore_plugin_state_to_default(plugin_or_name): + x = getattr(plugin_or_name, 'name', plugin_or_name) + dp = config['disabled_plugins'] + if x in dp: + dp.remove(x) + config['disabled_plugins'] = dp + ep = config['enabled_plugins'] + if x in ep: + ep.remove(x) + config['enabled_plugins'] = ep + default_disabled_plugins = set([ 'Douban Books', 'Douban.com covers', 'Nicebooks', 'Nicebooks covers', 'Kent District Library' diff --git a/src/calibre/ebooks/metadata/sources/amazon.py b/src/calibre/ebooks/metadata/sources/amazon.py index a102a550b5..2ad5bfbacc 100644 --- a/src/calibre/ebooks/metadata/sources/amazon.py +++ b/src/calibre/ebooks/metadata/sources/amazon.py @@ -341,7 +341,8 @@ class Amazon(Source): # Insufficient metadata to make an identify query return None - latin1q = dict([(x.encode('latin1'), y.encode('latin1')) for x, y in + latin1q = dict([(x.encode('latin1', 'ignore'), y.encode('latin1', + 'ignore')) for x, y in q.iteritems()]) url = 'http://www.amazon.%s/s/?'%domain + urlencode(latin1q) return url diff --git a/src/calibre/gui2/preferences/metadata_sources.py b/src/calibre/gui2/preferences/metadata_sources.py index 3a10bdce3e..a912e6f956 100644 --- a/src/calibre/gui2/preferences/metadata_sources.py +++ b/src/calibre/gui2/preferences/metadata_sources.py @@ -7,14 +7,157 @@ __license__ = 'GPL v3' __copyright__ = '2011, Kovid Goyal ' __docformat__ = 'restructuredtext en' +from operator import attrgetter + +from PyQt4.Qt import (QAbstractTableModel, Qt) + from calibre.gui2.preferences import ConfigWidgetBase, test_widget from calibre.gui2.preferences.metadata_sources_ui import Ui_Form +from calibre.ebooks.metadata.sources.base import msprefs +from calibre.customize.ui import (all_metadata_plugins, is_disabled, + enable_plugin, disable_plugin, restore_plugin_state_to_default) +from calibre.gui2 import NONE + +class SourcesModel(QAbstractTableModel): # {{{ + + def __init__(self, parent=None): + QAbstractTableModel.__init__(self, parent) + + self.plugins = [] + self.enabled_overrides = {} + self.cover_overrides = {} + + def initialize(self): + self.plugins = list(all_metadata_plugins()) + self.plugins.sort(key=attrgetter('name')) + self.enabled_overrides = {} + self.cover_overrides = {} + self.reset() + + def rowCount(self, parent=None): + return len(self.plugins) + + def columnCount(self, parent=None): + return 2 + + def headerData(self, section, orientation, role): + if orientation == Qt.Horizontal and role == Qt.DisplayRole: + if section == 0: + return _('Source') + if section == 1: + return _('Cover priority') + return NONE + + def data(self, index, role): + try: + plugin = self.plugins[index.row()] + except: + return NONE + col = index.column() + + if role == Qt.DisplayRole: + if col == 0: + return plugin.name + elif col == 1: + orig = msprefs['cover_priorities'].get(plugin.name, 1) + return self.cover_overrides.get(plugin, orig) + elif role == Qt.CheckStateRole and col == 0: + orig = Qt.Unchecked if is_disabled(plugin) else Qt.Checked + return self.enabled_overrides.get(plugin, orig) + + return NONE + + def setData(self, index, val, role): + try: + plugin = self.plugins[index.row()] + except: + return False + col = index.column() + ret = False + if col == 0 and role == Qt.CheckStateRole: + val, ok = val.toInt() + if ok: + self.enabled_overrides[plugin] = val + ret = True + if col == 1 and role == Qt.EditRole: + val, ok = val.toInt() + if ok: + self.cover_overrides[plugin] = val + ret = True + if ret: + self.dataChanged.emit(index, index) + return ret + + + def flags(self, index): + col = index.column() + ans = QAbstractTableModel.flags(self, index) + if col == 0: + return ans | Qt.ItemIsUserCheckable + return Qt.ItemIsEditable | ans + + def commit(self): + for plugin, val in self.enabled_overrides.iteritems(): + if val == Qt.Checked: + enable_plugin(plugin) + elif val == Qt.Unchecked: + disable_plugin(plugin) + + if self.cover_overrides: + cp = msprefs['cover_priorities'] + for plugin, val in self.cover_overrides.iteritems(): + if val == 1: + cp.pop(plugin.name, None) + else: + cp[plugin.name] = val + msprefs['cover_priorities'] = cp + + self.enabled_overrides = {} + self.cover_overrides = {} + + def restore_defaults(self): + del msprefs['cover_priorities'] + self.enabled_overrides = {} + self.cover_overrides = {} + for plugin in self.plugins: + restore_plugin_state_to_default(plugin) + self.reset() + +# }}} class ConfigWidget(ConfigWidgetBase, Ui_Form): - pass + + def genesis(self, gui): + r = self.register + r('txt_comments', msprefs) + r('max_tags', msprefs) + r('wait_after_first_identify_result', msprefs) + r('wait_after_first_cover_result', msprefs) + + self.configure_plugin_button.clicked.connect(self.configure_plugin) + self.sources_model = SourcesModel(self) + self.sources_view.setModel(self.sources_model) + self.sources_model.dataChanged.connect(self.changed_signal) + + def configure_plugin(self): + pass + + def initialize(self): + ConfigWidgetBase.initialize(self) + self.sources_model.initialize() + self.sources_view.resizeColumnsToContents() + + def restore_defaults(self): + ConfigWidgetBase.restore_defaults(self) + self.sources_model.restore_defaults() + self.changed_signal.emit() + + def commit(self): + self.sources_model.commit() + return ConfigWidgetBase.commit(self) if __name__ == '__main__': from PyQt4.Qt import QApplication app = QApplication([]) - test_widget('Interface', 'Behavior') + test_widget('Sharing', 'Metadata download') diff --git a/src/calibre/gui2/preferences/metadata_sources.ui b/src/calibre/gui2/preferences/metadata_sources.ui index b8aabdd4fe..68ac6352cf 100644 --- a/src/calibre/gui2/preferences/metadata_sources.ui +++ b/src/calibre/gui2/preferences/metadata_sources.ui @@ -30,8 +30,8 @@ - <p>Disable any metadata sources you do not want by unchecking them. You can also set the cover priority. Covers from sources that have a higher (smaller) priority will be preferred when bulk downloading metadata. -<p>Double click on a metadata source to customize it. + Disable any metadata sources you do not want by unchecking them. You can also set the cover priority. Covers from sources that have a higher (smaller) priority will be preferred when bulk downloading metadata. + true @@ -39,7 +39,25 @@ - + + + QAbstractItemView::SingleSelection + + + QAbstractItemView::SelectRows + + + + + + + Configure selected source + + + + :/images/plugins.png:/images/plugins.png + + @@ -51,7 +69,7 @@ - + If you uncheck any fields, metadata for those fields will not be downloaded @@ -100,7 +118,7 @@ - Max. time to wait after &cover is found: + Max. time to wait after first &cover is found: opt_wait_after_first_cover_result @@ -121,6 +139,8 @@ - + + +