From b45099206a100cd6a8613fd5f31ea0e3a1b014c0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 28 Jan 2011 17:52:27 -0700 Subject: [PATCH 1/3] Fix regression that broke content server when user categories are present --- src/calibre/library/server/browse.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/calibre/library/server/browse.py b/src/calibre/library/server/browse.py index 3e4687be95..a18def29de 100644 --- a/src/calibre/library/server/browse.py +++ b/src/calibre/library/server/browse.py @@ -356,9 +356,9 @@ class BrowseServer(object): if category in category_icon_map: icon = category_icon_map[category] elif meta['is_custom']: - icon = category_icon_map[':custom'] + icon = category_icon_map['custom:'] elif meta['kind'] == 'user': - icon = category_icon_map[':user'] + icon = category_icon_map['user:'] else: icon = 'blank.png' cats.append((meta['name'], category, icon)) From a489c90adf8f521e81024e10d63192f2382d2ce9 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 28 Jan 2011 20:44:34 -0700 Subject: [PATCH 2/3] Add search to the plugin preferences dialog --- src/calibre/gui2/__init__.py | 3 + src/calibre/gui2/preferences/plugins.py | 116 +++++++++++++++++++++-- src/calibre/gui2/preferences/plugins.ui | 48 ++++++++++ src/calibre/utils/search_query_parser.py | 4 +- 4 files changed, 161 insertions(+), 10 deletions(-) diff --git a/src/calibre/gui2/__init__.py b/src/calibre/gui2/__init__.py index b00097f5b2..9150172fc1 100644 --- a/src/calibre/gui2/__init__.py +++ b/src/calibre/gui2/__init__.py @@ -120,6 +120,8 @@ def _config(): help='Search history for the LRF viewer') c.add_opt('scheduler_search_history', default=[], help='Search history for the recipe scheduler') + c.add_opt('plugin_search_history', default=[], + help='Search history for the recipe scheduler') c.add_opt('worker_limit', default=6, help=_('Maximum number of waiting worker processes')) c.add_opt('get_social_metadata', default=True, @@ -138,6 +140,7 @@ def _config(): help=_('Show the average rating per item indication in the tag browser')) c.add_opt('disable_animations', default=False, help=_('Disable UI animations')) + c.add_opt return ConfigProxy(c) config = _config() diff --git a/src/calibre/gui2/preferences/plugins.py b/src/calibre/gui2/preferences/plugins.py index ba5b921d44..1edd4fe5f9 100644 --- a/src/calibre/gui2/preferences/plugins.py +++ b/src/calibre/gui2/preferences/plugins.py @@ -17,11 +17,14 @@ from calibre.customize.ui import initialized_plugins, is_disabled, enable_plugin remove_plugin from calibre.gui2 import NONE, error_dialog, info_dialog, choose_files, \ question_dialog +from calibre.utils.search_query_parser import SearchQueryParser +from calibre.utils.icu import lower -class PluginModel(QAbstractItemModel): # {{{ +class PluginModel(QAbstractItemModel, SearchQueryParser): # {{{ def __init__(self, *args): QAbstractItemModel.__init__(self, *args) + SearchQueryParser.__init__(self, ['all']) self.icon = QVariant(QIcon(I('plugins.png'))) p = QIcon(self.icon).pixmap(32, 32, QIcon.Disabled, QIcon.On) self.disabled_icon = QVariant(QIcon(p)) @@ -40,6 +43,72 @@ class PluginModel(QAbstractItemModel): # {{{ for plugins in self._data.values(): plugins.sort(cmp=lambda x, y: cmp(x.name.lower(), y.name.lower())) + def universal_set(self): + ans = set([]) + for c, category in enumerate(self.categories): + ans.add((c, -1)) + for p, plugin in enumerate(self._data[category]): + ans.add((c, p)) + return ans + + def get_matches(self, location, query, candidates=None): + if candidates is None: + candidates = self.universal_set() + ans = set([]) + if not query: + return ans + query = lower(query) + for c, p in candidates: + if p < 0: + if query in lower(self.categories[c]): + ans.add((c, p)) + else: + try: + plugin = self._data[self.categories[c]][p] + except: + continue + if query in lower(plugin.name) or query in lower(plugin.author) or \ + query in lower(plugin.description): + ans.add((c, p)) + return ans + + def find(self, query): + query = query.strip() + matches = self.parse(query) + if not matches: + return QModelIndex() + matches = list(sorted(matches)) + c, p = matches[0] + cat_idx = self.index(c, 0, QModelIndex()) + if p == -1: + return cat_idx + return self.index(p, 0, cat_idx) + + def find_next(self, idx, query, backwards=False): + query = query.strip() + matches = self.parse(query) + if not matches: + return idx + if idx.parent().isValid(): + loc = (idx.parent().row(), idx.row()) + else: + loc = (idx.row(), -1) + if loc not in matches: + return self.find(query) + if len(matches) == 1: + return QModelIndex() + matches = list(sorted(matches)) + i = matches.index(loc) + if backwards: + ans = i - 1 if i - 1 >= 0 else len(matches)-1 + else: + ans = i + 1 if i + 1 < len(matches) else 0 + + ans = matches[ans] + + return self.index(ans[0], 0, QModelIndex()) if ans[1] < 0 else \ + self.index(ans[1], 0, self.index(ans[0], 0, QModelIndex())) + def index(self, row, column, parent): if not self.hasIndex(row, column, parent): return QModelIndex() @@ -127,6 +196,7 @@ class PluginModel(QAbstractItemModel): # {{{ return plugin return NONE + # }}} class ConfigWidget(ConfigWidgetBase, Ui_Form): @@ -144,6 +214,42 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): self.customize_plugin_button.clicked.connect(self.customize_plugin) self.remove_plugin_button.clicked.connect(self.remove_plugin) self.button_plugin_add.clicked.connect(self.add_plugin) + self.search.initialize('plugin_search_history', + help_text=_('Search for plugin')) + self.search.search.connect(self.find) + self.next_button.clicked.connect(self.find_next) + self.previous_button.clicked.connect(self.find_previous) + + def find(self, query): + idx = self._plugin_model.find(query) + if not idx.isValid(): + return info_dialog(self, _('No matches'), + _('Could not find any matching plugins'), show=True, + show_copy_button=False) + self.highlight_index(idx) + + def highlight_index(self, idx): + self.plugin_view.scrollTo(idx) + self.plugin_view.selectionModel().select(idx, + self.plugin_view.selectionModel().ClearAndSelect) + self.plugin_view.setCurrentIndex(idx) + + def find_next(self, *args): + idx = self.plugin_view.currentIndex() + if not idx.isValid(): + idx = self._plugin_model.index(0, 0) + idx = self._plugin_model.find_next(idx, + unicode(self.search.currentText())) + self.highlight_index(idx) + + def find_previous(self, *args): + idx = self.plugin_view.currentIndex() + if not idx.isValid(): + idx = self._plugin_model.index(0, 0) + idx = self._plugin_model.find_next(idx, + unicode(self.search.currentText()), backwards=True) + self.highlight_index(idx) + def toggle_plugin(self, *args): self.modify_plugin(op='toggle') @@ -184,13 +290,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): show=True, show_copy_button=False) idx = self._plugin_model.plugin_to_index_by_properties(plugin) if idx.isValid(): - self.plugin_view.scrollTo(idx, - self.plugin_view.PositionAtCenter) - self.plugin_view.scrollTo(idx, - self.plugin_view.PositionAtCenter) - self.plugin_view.selectionModel().select(idx, - self.plugin_view.selectionModel().ClearAndSelect) - self.plugin_view.setCurrentIndex(idx) + self.highlight_index(idx) else: error_dialog(self, _('No valid plugin path'), _('%s is not a valid plugin path')%path).exec_() diff --git a/src/calibre/gui2/preferences/plugins.ui b/src/calibre/gui2/preferences/plugins.ui index 83a904eb08..ebf422dfe3 100644 --- a/src/calibre/gui2/preferences/plugins.ui +++ b/src/calibre/gui2/preferences/plugins.ui @@ -24,6 +24,47 @@ + + + + + + + + + + 0 + 0 + + + + &Next + + + + :/images/arrow-down.png:/images/arrow-down.png + + + + + + + + 0 + 0 + + + + &Previous + + + + :/images/arrow-up.png:/images/arrow-up.png + + + + + @@ -84,6 +125,13 @@ + + + SearchBox2 + QComboBox +
calibre/gui2/search_box.h
+
+
diff --git a/src/calibre/utils/search_query_parser.py b/src/calibre/utils/search_query_parser.py index 4e4da9d1df..a50ca20fc1 100644 --- a/src/calibre/utils/search_query_parser.py +++ b/src/calibre/utils/search_query_parser.py @@ -260,12 +260,12 @@ class SearchQueryParser(object): ''' Should return the set of matches for :param:'location` and :param:`query`. - The search must be performed over all entries is :param:`candidates` is + The search must be performed over all entries if :param:`candidates` is None otherwise only over the items in candidates. :param:`location` is one of the items in :member:`SearchQueryParser.DEFAULT_LOCATIONS`. :param:`query` is a string literal. - :param: None or a subset of the set returned by :meth:`universal_set`. + :return: None or a subset of the set returned by :meth:`universal_set`. ''' return set([]) From ad33d230e6a666401d897311d388057ef1ff0bdf Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 28 Jan 2011 23:06:30 -0700 Subject: [PATCH 3/3] Implement restore database in the GUI --- src/calibre/gui2/actions/choose_library.py | 17 ++- src/calibre/gui2/dialogs/restore_library.py | 115 ++++++++++++++++++++ 2 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 src/calibre/gui2/dialogs/restore_library.py diff --git a/src/calibre/gui2/actions/choose_library.py b/src/calibre/gui2/actions/choose_library.py index fd20d88049..7034380a56 100644 --- a/src/calibre/gui2/actions/choose_library.py +++ b/src/calibre/gui2/actions/choose_library.py @@ -16,7 +16,6 @@ from calibre.utils.config import prefs from calibre.gui2 import gprefs, warning_dialog, Dispatcher, error_dialog, \ question_dialog, info_dialog from calibre.gui2.actions import InterfaceAction -from calibre.gui2.dialogs.check_library import CheckLibraryDialog, DBCheck class LibraryUsageStats(object): # {{{ @@ -139,6 +138,12 @@ class ChooseLibraryAction(InterfaceAction): None, None), attr='action_check_library') ac.triggered.connect(self.check_library, type=Qt.QueuedConnection) self.maintenance_menu.addAction(ac) + ac = self.create_action(spec=(_('Restore database'), 'lt.png', + None, None), + attr='action_restore_database') + ac.triggered.connect(self.restore_database, type=Qt.QueuedConnection) + self.maintenance_menu.addAction(ac) + self.choose_menu.addMenu(self.maintenance_menu) def pick_random(self, *args): @@ -267,7 +272,17 @@ class ChooseLibraryAction(InterfaceAction): _('Metadata will be backed up while calibre is running, at the ' 'rate of approximately 1 book every three seconds.'), show=True) + def restore_database(self): + from calibre.gui2.dialogs.restore_library import restore_database + m = self.gui.library_view.model() + m.stop_metadata_backup() + db = m.db + db.prefs.disable_setting = True + if restore_database(db, self.gui): + self.gui.library_moved(db.library_path, call_close=False) + def check_library(self): + from calibre.gui2.dialogs.check_library import CheckLibraryDialog, DBCheck self.gui.library_view.save_state() m = self.gui.library_view.model() m.stop_metadata_backup() diff --git a/src/calibre/gui2/dialogs/restore_library.py b/src/calibre/gui2/dialogs/restore_library.py new file mode 100644 index 0000000000..dd1befc11b --- /dev/null +++ b/src/calibre/gui2/dialogs/restore_library.py @@ -0,0 +1,115 @@ +#!/usr/bin/env python +# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai + +__license__ = 'GPL v3' +__copyright__ = '2011, Kovid Goyal ' +__docformat__ = 'restructuredtext en' + +from PyQt4.Qt import QDialog, QLabel, QVBoxLayout, QDialogButtonBox, \ + QProgressBar, QSize, QTimer, pyqtSignal, Qt + +from calibre.library.restore import Restore +from calibre.gui2 import error_dialog, question_dialog, warning_dialog, \ + info_dialog + +class DBRestore(QDialog): + + update_signal = pyqtSignal(object, object) + + def __init__(self, parent, library_path): + QDialog.__init__(self, parent) + self.l = QVBoxLayout() + self.setLayout(self.l) + self.l1 = QLabel(''+_('Restoring database from backups, do not' + ' interrupt, this will happen in two stages')+'...') + self.setWindowTitle(_('Restoring database')) + self.l.addWidget(self.l1) + self.pb = QProgressBar(self) + self.l.addWidget(self.pb) + self.pb.setMaximum(0) + self.pb.setMinimum(0) + self.msg = QLabel('') + self.l.addWidget(self.msg) + self.msg.setWordWrap(True) + self.bb = QDialogButtonBox(QDialogButtonBox.Cancel) + self.l.addWidget(self.bb) + self.bb.rejected.connect(self.reject) + self.resize(self.sizeHint() + QSize(100, 50)) + self.error = None + self.rejected = False + self.library_path = library_path + self.update_signal.connect(self.do_update, type=Qt.QueuedConnection) + + self.restorer = Restore(library_path, self) + self.restorer.daemon = True + + # Give the metadata backup thread time to stop + QTimer.singleShot(2000, self.start) + + + def start(self): + self.restorer.start() + QTimer.singleShot(10, self.update) + + def reject(self): + self.rejected = True + self.restorer.progress_callback = lambda x, y: x + QDialog.rejecet(self) + + def update(self): + if self.restorer.is_alive(): + QTimer.singleShot(10, self.update) + else: + self.restorer.progress_callback = lambda x, y: x + self.accept() + + def __call__(self, msg, step): + self.update_signal.emit(msg, step) + + def do_update(self, msg, step): + if msg is None: + self.pb.setMaximum(step) + else: + self.msg.setText(msg) + self.pb.setValue(step) + + +def restore_database(db, parent=None): + if not question_dialog(parent, _('Are you sure?'), '

'+ + _('Your list of books, with all their metadata is ' + 'stored in a single file, called a database. ' + 'In addition, metadata for each individual ' + 'book is stored in that books\' folder, as ' + 'a backup.' + '

This operation will rebuild ' + 'the database from the individual book ' + 'metadata. This is useful if the ' + 'database has been corrupted and you get a ' + 'blank list of books. Note that restoring only ' + 'restores books, not any settings stored in the ' + 'database, or any custom recipes.' + '

Do you want to restore the database?')): + return False + db.conn.close() + d = DBRestore(parent, db.library_path) + d.exec_() + r = d.restorer + d.restorer = None + if d.rejected: + return True + if r.tb is not None: + error_dialog(parent, _('Failed'), + _('Restoring database failed, click Show details to see details'), + det_msg=r.tb, show=True) + else: + if r.errors_occurred: + warning_dialog(parent, _('Success'), + _('Restoring the database succeeded with some warnings', + ' click Show details to see the details.'), + det_msg=r.report, show=True) + else: + info_dialog(parent, _('Success'), + _('Restoring database was successful'), show=True, + show_copy_button=False) + return True +