qutebrowser/qutebrowser/browser/quickmarks.py

109 lines
3.3 KiB
Python
Raw Normal View History

2014-06-19 09:04:37 +02:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2014-05-22 16:44:47 +02:00
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# 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 <http://www.gnu.org/licenses/>.
2014-06-20 22:57:32 +02:00
"""Manager for quickmarks.
Note we violate our general QUrl rule by storing url strings in the marks
OrderedDict. This is because we read them from a file at start and write them
to a file on shutdown, so it makes semse to keep them as strings her.e
"""
2014-05-22 16:44:47 +02:00
2014-08-26 19:10:14 +02:00
import functools
import collections
2014-05-22 16:44:47 +02:00
from PyQt5.QtCore import QStandardPaths, QUrl
2014-05-22 16:44:47 +02:00
2014-08-26 20:25:11 +02:00
from qutebrowser.utils import message, usertypes, utils, qtutils
from qutebrowser.commands import cmdexc, cmdutils
2014-08-26 19:10:14 +02:00
from qutebrowser.config import lineparser
2014-05-22 16:44:47 +02:00
2014-08-26 19:10:14 +02:00
marks = collections.OrderedDict()
2014-05-22 16:44:47 +02:00
linecp = None
def init():
"""Read quickmarks from the config file."""
2014-05-22 15:44:16 +02:00
global linecp
2014-08-26 19:10:14 +02:00
confdir = utils.get_standard_dir(QStandardPaths.ConfigLocation)
linecp = lineparser.LineConfigParser(confdir, 'quickmarks')
2014-05-22 16:44:47 +02:00
for line in linecp:
2014-06-25 11:03:26 +02:00
try:
key, url = line.split(maxsplit=1)
except ValueError:
2014-06-26 07:58:00 +02:00
message.error("Invalid quickmark '{}'".format(line))
2014-06-25 11:03:26 +02:00
else:
marks[key] = url
2014-05-22 16:44:47 +02:00
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.
Args:
url: The quickmark url as a QUrl.
"""
qtutils.ensure_valid(url)
urlstr = url.toString(QUrl.RemovePassword | QUrl.FullyEncoded)
2014-08-26 19:10:14 +02:00
message.ask_async("Add quickmark:", usertypes.PromptMode.text,
functools.partial(quickmark_add, urlstr))
2014-05-22 16:44:47 +02:00
@cmdutils.register()
2014-06-23 16:12:35 +02:00
def quickmark_add(urlstr, name):
2014-05-22 16:44:47 +02:00
"""Add a new quickmark.
Args:
2014-06-23 16:12:35 +02:00
urlstr: The url to add as quickmark, as string.
2014-05-22 16:44:47 +02:00
name: The name for the new quickmark.
"""
if not name:
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError("Can't set mark with empty name!")
2014-06-23 16:12:35 +02:00
if not urlstr:
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError("Can't set mark with empty URL!")
2014-05-22 16:44:47 +02:00
def set_mark():
2014-05-22 15:44:16 +02:00
"""Really set the quickmark."""
2014-06-23 16:12:35 +02:00
marks[name] = urlstr
2014-05-22 16:44:47 +02:00
if name in marks:
2014-06-26 07:58:00 +02:00
message.confirm_async("Override existing quickmark?", set_mark,
default=True)
2014-05-22 16:44:47 +02:00
else:
set_mark()
def get(name):
2014-06-20 22:57:32 +02:00
"""Get the URL of the quickmark named name as a QUrl."""
2014-05-22 16:44:47 +02:00
if name not in marks:
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError(
"Quickmark '{}' does not exist!".format(name))
2014-06-20 22:57:32 +02:00
urlstr = marks[name]
2014-06-20 23:57:52 +02:00
url = QUrl(urlstr)
if not url.isValid():
2014-08-26 19:10:14 +02:00
raise cmdexc.CommandError(
"Invalid URL for quickmark {}: {} ({})".format(name, urlstr,
url.errorString()))
2014-06-20 23:57:52 +02:00
return url