From 8cddbec9e3ed0bc2b8cfdf61e18e721ae7712dbc Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 22 May 2014 16:44:47 +0200 Subject: [PATCH] Implement quickmarks --- TODO | 9 ---- qutebrowser/app.py | 7 +++ qutebrowser/browser/commands.py | 24 +++++++++ qutebrowser/browser/quickmarks.py | 87 +++++++++++++++++++++++++++++++ qutebrowser/config/configdata.py | 4 ++ 5 files changed, 122 insertions(+), 9 deletions(-) create mode 100644 qutebrowser/browser/quickmarks.py diff --git a/TODO b/TODO index de7a39421..3733db85a 100644 --- a/TODO +++ b/TODO @@ -160,15 +160,6 @@ Show Bookmarks (command bookmarks, aliases: bmarks, bmas). gB Show Bookmarks, open bookmark in a new tab (command tab_bookmarks, aliases: tabmarks). -m -Add a quickmark (command save_quickmark, aliases: quickmark, qmark). - -b -Open quickmark (command quickmark, aliases: qmarks). - -B -Open quickmark in a new tab (command tab_quickmark, aliases: tabqmarks). - tab-handling ------------ diff --git a/qutebrowser/app.py b/qutebrowser/app.py index ac1604d75..a5b5a268b 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -50,6 +50,7 @@ import qutebrowser.network.qutescheme as qutescheme import qutebrowser.config.websettings as websettings import qutebrowser.network.proxy as proxy import qutebrowser.utils.message as message +import qutebrowser.browser.quickmarks as quickmarks from qutebrowser.network.networkmanager import NetworkManager from qutebrowser.config.config import ConfigManager from qutebrowser.keyinput.modeman import ModeManager @@ -133,6 +134,7 @@ class QuteBrowser(QApplication): self._handle_segfault() self._init_modes() websettings.init() + quickmarks.init() proxy.init() self.cookiejar = CookieJar() self.networkmanager = NetworkManager(self.cookiejar) @@ -624,6 +626,11 @@ class QuteBrowser(QApplication): self.cookiejar.save() except AttributeError: logging.exception("Could not save cookies.") + # Save quickmarks + try: + quickmarks.save() + except AttributeError: + logging.exception("Could not save quickmarks.") # Shut down tabs try: self.mainwindow.tabs.shutdown_complete.connect(partial( diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 153008570..c892b5bdb 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -33,6 +33,7 @@ import qutebrowser.browser.hints as hints import qutebrowser.utils.url as urlutils import qutebrowser.utils.message as message import qutebrowser.utils.webelem as webelem +import qutebrowser.browser.quickmarks as quickmarks from qutebrowser.utils.misc import check_overflow, shell_escape from qutebrowser.utils.editor import ExternalEditor from qutebrowser.commands.exceptions import CommandError @@ -601,6 +602,29 @@ class CommandDispatcher(QObject): runner.run(cmd, *args, env={'QUTE_URL': url}) self._userscript_runners.append(runner) + @cmdutils.register(instance='mainwindow.tabs.cmd') + def quickmark_save(self): + """Save the current page as a quickmark.""" + quickmarks.prompt_save(self._tabs.currentWidget().url()) + + @cmdutils.register(instance='mainwindow.tabs.cmd') + def quickmark_load(self, name): + """Load a quickmark.""" + url = quickmarks.get(name) + self._tabs.currentWidget().openurl(url) + + @cmdutils.register(instance='mainwindow.tabs.cmd') + def quickmark_load_tab(self, name): + """Load a quickmark in a new tab.""" + url = quickmarks.get(name) + self._tabs.tabopen(url, background=False) + + @cmdutils.register(instance='mainwindow.tabs.cmd') + def quickmark_load_tab_bg(self, name): + """Load a quickmark in a new background tab.""" + url = quickmarks.get(name) + self._tabs.tabopen(url, background=True) + @cmdutils.register(instance='mainwindow.tabs.cmd', modes=['insert'], hide=True) def open_editor(self): diff --git a/qutebrowser/browser/quickmarks.py b/qutebrowser/browser/quickmarks.py new file mode 100644 index 000000000..06f44c7c6 --- /dev/null +++ b/qutebrowser/browser/quickmarks.py @@ -0,0 +1,87 @@ +# Copyright 2014 Florian Bruhin (The Compiler) +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see . + +"""Manager for quickmarks.""" + +from functools import partial +from collections import OrderedDict + +from PyQt5.QtCore import QStandardPaths + +import qutebrowser.utils.message as message +import qutebrowser.commands.utils as cmdutils +from qutebrowser.utils.usertypes import PromptMode +from qutebrowser.config.lineparser import LineConfigParser +from qutebrowser.utils.misc import get_standard_dir +from qutebrowser.utils.url import urlstring +from qutebrowser.commands.exceptions import CommandError + + +marks = OrderedDict() +linecp = None + + +def init(): + """Read quickmarks from the config file.""" + global marks, linecp + confdir = get_standard_dir(QStandardPaths.ConfigLocation) + linecp = LineConfigParser(confdir, 'quickmarks') + for line in linecp: + key, url = line.split(maxsplit=1) + marks[key] = url + + +def save(): + """Save the quickmarks to disk.""" + linecp.data = [' '.join(tpl) for tpl in marks.items()] + linecp.save() + + +def prompt_save(url): + """Prompt for a new quickmark name to be added and add it.""" + message.question("Add quickmark:", PromptMode.text, + partial(quickmark_add, url)) + + +@cmdutils.register() +def quickmark_add(url, name): + """Add a new quickmark. + + Args: + url: The url to add as quickmark. + name: The name for the new quickmark. + """ + if not name: + raise CommandError("Can't set mark with empty name!") + if not url: + raise CommandError("Can't set mark with empty URL!") + + def set_mark(): + marks[name] = urlstring(url) + + if name in marks: + message.confirm_action("Override existing quickmark?", set_mark, + default=True) + else: + set_mark() + + +def get(name): + """Get the URL of the quickmark named name.""" + if name not in marks: + raise CommandError("Quickmark '{}' does not exist!".format(name)) + return marks[name] diff --git a/qutebrowser/config/configdata.py b/qutebrowser/config/configdata.py index 8158f1237..e1bc88103 100644 --- a/qutebrowser/config/configdata.py +++ b/qutebrowser/config/configdata.py @@ -627,6 +627,10 @@ DATA = OrderedDict([ ('pP', 'paste sel'), ('Pp', 'paste-tab'), ('PP', 'paste-tab sel'), + ('m', 'quickmark-save'), + ('b', 'quickmark-load'), + ('B', 'quickmark-load-tab'), + (';b', 'quickmark-load-tab-bg'), ('sf', 'save'), ('ss', 'set'), ('sl', 'set-temp'),