diff --git a/qutebrowser/app.py b/qutebrowser/app.py index 867a24d52..b755b0239 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -44,7 +44,8 @@ import qutebrowser.resources # pylint: disable=unused-import from qutebrowser.completion.models import instances as completionmodels from qutebrowser.commands import cmdutils, runners, cmdexc from qutebrowser.config import style, config, websettings, configexc -from qutebrowser.browser import bookmarks, quickmarks, cookies, cache, adblock, history +from qutebrowser.browser import (bookmarks, quickmarks, cookies, cache, + adblock, history) from qutebrowser.browser.network import qutescheme, proxy, networkmanager from qutebrowser.mainwindow import mainwindow from qutebrowser.misc import readline, ipc, savemanager, sessions, crashsignal diff --git a/qutebrowser/browser/bookmarks.py b/qutebrowser/browser/bookmarks.py index d1693b358..aac331015 100644 --- a/qutebrowser/browser/bookmarks.py +++ b/qutebrowser/browser/bookmarks.py @@ -20,13 +20,12 @@ """Manager for bookmarks. -Note we violate our general QUrl rule by storing url strings in the marks +Note we violate our general QUrl rule by storing url strings in the bookmarks OrderedDict. This is because we read them from a file at start and write them to a file on shutdown, so it makes sense to keep them as strings here. """ import os.path -import functools import collections from PyQt5.QtCore import pyqtSignal, QUrl, QObject @@ -41,17 +40,17 @@ class BookmarkManager(QObject): """Manager for bookmarks. Attributes: - marks: An OrderedDict of all bookmarks. + bookmarks: An OrderedDict of all bookmarks. _lineparser: The LineParser used for the bookmarks, or None (when qutebrowser is started with -c ''). Signals: changed: Emitted when anything changed. added: Emitted when a new bookmark was added. - arg 0: The name of the bookmark. + arg 0: The title of the bookmark. arg 1: The URL of the bookmark, as string. removed: Emitted when an existing bookmark was removed. - arg 0: The name of the bookmark. + arg 0: The title of the bookmark. """ changed = pyqtSignal() @@ -62,7 +61,7 @@ class BookmarkManager(QObject): """Initialize and read bookmarks.""" super().__init__(parent) - self.marks = collections.OrderedDict() + self.bookmarks = collections.OrderedDict() if standarddir.config() is None: self._lineparser = None @@ -74,11 +73,11 @@ class BookmarkManager(QObject): # Ignore empty or whitespace-only lines. continue try: - key, url = line.rsplit(maxsplit=1) + url, title = line.split(maxsplit=1) except ValueError: message.error(0, "Invalid bookmark '{}'".format(line)) else: - self.marks[key] = url + self.bookmarks[url] = title filename = os.path.join(standarddir.config(), 'bookmarks') objreg.get('save-manager').add_saveable( 'bookmark-manager', self.save, self.changed, @@ -88,17 +87,17 @@ class BookmarkManager(QObject): """Save the bookmarks to disk.""" if self._lineparser is not None: self._lineparser.data = [' '.join(tpl) - for tpl in self.marks.items()] + for tpl in self.bookmarks.items()] self._lineparser.save() @cmdutils.register(instance='bookmark-manager', win_id='win_id') - def bookmark_add(self, win_id, url, name): + def bookmark_add(self, win_id, url, title): """Add a new bookmark. Args: win_id: The window ID to display the errors in. url: The url to add as bookmark. - name: The name for the new bookmark. + title: The title for the new bookmark. """ if not url.isValid(): urlutils.invalid_url_error(win_id, url, "save quickmark") @@ -107,30 +106,30 @@ class BookmarkManager(QObject): # We don't raise cmdexc.CommandError here as this can be called async # via prompt_save. - if not name: - message.error(win_id, "Can't set mark with empty name!") + if not title: + message.error(win_id, "Can't set mark with empty title!") return if not urlstr: message.error(win_id, "Can't set mark with empty URL!") return - self.marks[name] = urlstr + self.bookmarks[urlstr] = title self.changed.emit() - self.added.emit(name, urlstr) + self.added.emit(title, urlstr) message.info(win_id, "Bookmarks added") @cmdutils.register(instance='bookmark-manager', maxsplit=0, - completion=[usertypes.Completion.bookmark_by_name]) - def bookmark_del(self, name): + completion=[usertypes.Completion.bookmark_by_title]) + def bookmark_del(self, url): """Delete a bookmark. Args: - name: The name of the bookmark to delete. + url: The url of the bookmark to delete. """ try: - del self.marks[name] + del self.bookmarks[url] except KeyError: - raise cmdexc.CommandError("Bookmark '{}' not found!".format(name)) + raise cmdexc.CommandError("Bookmark '{}' not found!".format(url)) else: self.changed.emit() - self.removed.emit(name) + self.removed.emit(url) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 41b379c51..42400f38a 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -993,7 +993,8 @@ class CommandDispatcher: def bookmark_save(self): """Save the current page as a bookmark.""" bookmark_manager = objreg.get('bookmark-manager') - bookmark_manager.bookmark_add(self._win_id, self._current_url(), self._current_title()) + bookmark_manager.bookmark_add(self._win_id, self._current_url(), + self._current_title()) @cmdutils.register(instance='command-dispatcher', name='inspector', scope='window') diff --git a/qutebrowser/completion/models/instances.py b/qutebrowser/completion/models/instances.py index 50e73cab5..2277cf5b9 100644 --- a/qutebrowser/completion/models/instances.py +++ b/qutebrowser/completion/models/instances.py @@ -106,19 +106,20 @@ def init_quickmark_completions(): model = _init_model(miscmodels.QuickmarkCompletionModel, 'name') _instances[usertypes.Completion.quickmark_by_name] = model + @pyqtSlot() def init_bookmark_completions(): """Initialize bookmark completion models.""" log.completion.debug("Initializing bookmark completion.") try: _instances[usertypes.Completion.bookmark_by_url].deleteLater() - _instances[usertypes.Completion.bookmark_by_name].deleteLater() + _instances[usertypes.Completion.bookmark_by_title].deleteLater() except KeyError: pass model = _init_model(miscmodels.BookmarkCompletionModel, 'url') _instances[usertypes.Completion.bookmark_by_url] = model - model = _init_model(miscmodels.BookmarkCompletionModel, 'name') - _instances[usertypes.Completion.bookmark_by_name] = model + model = _init_model(miscmodels.BookmarkCompletionModel, 'title') + _instances[usertypes.Completion.bookmark_by_title] = model @pyqtSlot() @@ -143,7 +144,7 @@ INITIALIZERS = { usertypes.Completion.quickmark_by_url: init_quickmark_completions, usertypes.Completion.quickmark_by_name: init_quickmark_completions, usertypes.Completion.bookmark_by_url: init_bookmark_completions, - usertypes.Completion.bookmark_by_name: init_bookmark_completions, + usertypes.Completion.bookmark_by_title: init_bookmark_completions, usertypes.Completion.sessions: init_session_completion, } @@ -184,7 +185,7 @@ def init(): bookmark_manager = objreg.get('bookmark-manager') bookmark_manager.changed.connect( functools.partial(update, [usertypes.Completion.bookmark_by_url, - usertypes.Completion.bookmark_by_name])) + usertypes.Completion.bookmark_by_title])) session_manager = objreg.get('session-manager') session_manager.update_completion.connect( functools.partial(update, [usertypes.Completion.sessions])) diff --git a/qutebrowser/completion/models/miscmodels.py b/qutebrowser/completion/models/miscmodels.py index f9af679ce..faea69269 100644 --- a/qutebrowser/completion/models/miscmodels.py +++ b/qutebrowser/completion/models/miscmodels.py @@ -111,6 +111,27 @@ class QuickmarkCompletionModel(base.BaseCompletionModel): match_field)) +class BookmarkCompletionModel(base.BaseCompletionModel): + + """A CompletionModel filled with all bookmarks.""" + + # pylint: disable=abstract-method + + def __init__(self, match_field='url', parent=None): + super().__init__(parent) + cat = self.new_category("Bookmarks") + bookmarks = objreg.get('bookmark-manager').bookmarks.items() + if match_field == 'url': + for bm_url, bm_title in bookmarks: + self.new_item(cat, bm_url, bm_title) + elif match_field == 'title': + for bm_url, bm_title in bookmarks: + self.new_item(cat, bm_title, bm_url) + else: + raise ValueError("Invalid value '{}' for match_field!".format( + match_field)) + + class SessionCompletionModel(base.BaseCompletionModel): """A CompletionModel filled with session names.""" diff --git a/qutebrowser/completion/models/urlmodel.py b/qutebrowser/completion/models/urlmodel.py index 4927ec65a..eacd1aebd 100644 --- a/qutebrowser/completion/models/urlmodel.py +++ b/qutebrowser/completion/models/urlmodel.py @@ -44,9 +44,9 @@ class UrlCompletionModel(base.BaseCompletionModel): self._history_cat = self.new_category("History") bookmark_manager = objreg.get('bookmark-manager') - bookmarks = bookmark_manager.marks.items() - for bm_name, bm_url in bookmarks: - self._add_bookmark_entry(bm_name, bm_url) + bookmarks = bookmark_manager.bookmarks.items() + for bm_url, bm_title in bookmarks: + self._add_bookmark_entry(bm_title, bm_url) bookmark_manager.added.connect(self.on_bookmark_added) bookmark_manager.removed.connect(self.on_bookmark_removed) @@ -82,14 +82,14 @@ class UrlCompletionModel(base.BaseCompletionModel): """ self.new_item(self._quickmark_cat, url, name) - def _add_bookmark_entry(self, name, url): + def _add_bookmark_entry(self, title, url): """Add a new bookmark entry to the completion. Args: - name: The name of the new bookmark. + title: The title of the new bookmark. url: The URL of the new bookmark. """ - self.new_item(self._bookmark_cat, url, name) + self.new_item(self._bookmark_cat, url, title) @config.change_filter('completion', 'timestamp-format') def reformat_timestamps(self): @@ -138,24 +138,24 @@ class UrlCompletionModel(base.BaseCompletionModel): break @pyqtSlot(str, str) - def on_bookmark_added(self, name, url): + def on_bookmark_added(self, title, url): """Called when a bookmark has been added by the user. Args: - name: The name of the new bookmark. + title: The title of the new bookmark. url: The url of the new bookmark, as string. """ - self._add_bookmark_entry(name, url) + self._add_bookmark_entry(title, url) @pyqtSlot(str) - def on_bookmark_removed(self, name): + def on_bookmark_removed(self, url): """Called when a bookmark has been removed by the user. Args: - name: The name of the bookmark which has been removed. + url: The url of the bookmark which has been removed. """ for i in range(self._bookmark_cat.rowCount()): - name_item = self._bookmark_cat.child(i, 1) - if name_item.data(Qt.DisplayRole) == name: + url_item = self._bookmark_cat.child(i, 1) + if url_item.data(Qt.DisplayRole) == url: self._bookmark_cat.removeRow(i) break diff --git a/qutebrowser/utils/usertypes.py b/qutebrowser/utils/usertypes.py index 3de7012f4..2ee55e96f 100644 --- a/qutebrowser/utils/usertypes.py +++ b/qutebrowser/utils/usertypes.py @@ -238,7 +238,7 @@ KeyMode = enum('KeyMode', ['normal', 'hint', 'command', 'yesno', 'prompt', Completion = enum('Completion', ['command', 'section', 'option', 'value', 'helptopic', 'quickmark_by_url', 'quickmark_by_name', 'bookmark_by_url', - 'bookmark_by_name', 'url', 'sessions']) + 'bookmark_by_title', 'url', 'sessions']) # Exit statuses for errors. Needs to be an int for sys.exit.