qutebrowser/qutebrowser/config/websettings.py

196 lines
7.4 KiB
Python
Raw Normal View History

2014-04-16 15:49:56 +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-04-17 17:44:27 +02:00
"""Bridge from QWebSettings to our own settings.
Module attributes:
2014-05-03 16:13:32 +02:00
ATTRIBUTES: A mapping from internal setting names to QWebSetting enum
constants.
SETTERS: A mapping from setting names to QWebSetting setter method names.
2014-04-17 17:44:27 +02:00
settings: The global QWebSettings singleton instance.
"""
2014-04-16 15:49:56 +02:00
2014-05-04 01:33:01 +02:00
# pylint: disable=unnecessary-lambda
2014-04-16 15:49:56 +02:00
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWebKit import QWebSettings
2014-05-08 22:33:24 +02:00
from PyQt5.QtCore import QStandardPaths
2014-04-16 15:49:56 +02:00
import qutebrowser.config.config as config
2014-05-05 07:45:36 +02:00
from qutebrowser.utils.usertypes import enum
2014-05-08 22:33:24 +02:00
from qutebrowser.utils.misc import get_standard_dir
2014-04-16 15:49:56 +02:00
2014-05-05 07:45:36 +02:00
MapType = enum('attribute', 'setter', 'static_setter')
2014-04-16 15:49:56 +02:00
2014-05-04 01:50:37 +02:00
MAPPINGS = {
2014-04-16 15:49:56 +02:00
# noqa
2014-06-03 20:28:51 +02:00
'permissions': {
'allow-images':
(MapType.attribute, QWebSettings.AutoLoadImages),
'allow-javascript':
(MapType.attribute, QWebSettings.JavascriptEnabled),
'javascript-can-open-windows':
(MapType.attribute, QWebSettings.JavascriptCanOpenWindows),
'javascript-can-close-windows':
(MapType.attribute, QWebSettings.JavascriptCanCloseWindows),
'javascript-can-access-clipboard':
(MapType.attribute, QWebSettings.JavascriptCanAccessClipboard),
#'allow-java':
# (MapType.attribute, QWebSettings.JavaEnabled),
'allow-plugins':
(MapType.attribute, QWebSettings.PluginsEnabled),
'local-content-can-access-remote-urls':
(MapType.attribute, QWebSettings.LocalContentCanAccessRemoteUrls),
'local-content-can-access-file-urls':
(MapType.attribute, QWebSettings.LocalContentCanAccessFileUrls),
},
'network': {
'dns-prefetch':
(MapType.attribute, QWebSettings.DnsPrefetchEnabled),
},
'input': {
'spatial-navigation':
(MapType.attribute, QWebSettings.SpatialNavigationEnabled),
'links-included-in-focus-chain':
(MapType.attribute, QWebSettings.LinksIncludedInFocusChain),
},
'fonts': {
'web-family-standard':
(MapType.setter, lambda qws, v:
qws.setFontFamily(QWebSettings.StandardFont, v)),
'web-family-fixed':
(MapType.setter, lambda qws, v:
qws.setFontFamily(QWebSettings.FixedFont, v)),
'web-family-serif':
(MapType.setter, lambda qws, v:
qws.setFontFamily(QWebSettings.SerifFont, v)),
'web-family-sans-serif':
(MapType.setter, lambda qws, v:
qws.setFontFamily(QWebSettings.SansSerifFont, v)),
'web-family-cursive':
(MapType.setter, lambda qws, v:
qws.setFontFamily(QWebSettings.CursiveFont, v)),
'web-family-fantasy':
(MapType.setter, lambda qws, v:
qws.setFontFamily(QWebSettings.FantasyFont, v)),
'web-size-minimum':
(MapType.setter, lambda qws, v:
qws.setFontSize(QWebSettings.MinimumFontSize, v)),
'web-size-minimum-logical':
(MapType.setter, lambda qws, v:
qws.setFontSize(QWebSettings.MinimumLogicalFontSize, v)),
'web-size-default':
(MapType.setter, lambda qws, v:
qws.setFontSize(QWebSettings.DefaultFontSize, v)),
'web-size-default-fixed':
(MapType.setter, lambda qws, v:
qws.setFontSize(QWebSettings.DefaultFixedFontSize, v)),
},
'ui': {
'zoom-text-only':
(MapType.attribute, QWebSettings.ZoomTextOnly),
'frame-flattening':
(MapType.attribute, QWebSettings.FrameFlatteningEnabled),
'user-stylesheet':
(MapType.setter, lambda qws, v: qws.setUserStyleSheetUrl(v)),
'css-media-type':
(MapType.setter, lambda qws, v: qws.setCSSMediaType(v)),
#'accelerated-compositing':
# (MapType.attribute, QWebSettings.AcceleratedCompositingEnabled),
#'tiled-backing-store':
# (MapType.attribute, QWebSettings.TiledBackingStoreEnabled),
},
'storage': {
'offline-storage-database':
(MapType.attribute, QWebSettings.OfflineStorageDatabaseEnabled),
'offline-web-application-storage':
(MapType.attribute, QWebSettings.OfflineWebApplicationCacheEnabled),
'local-storage':
(MapType.attribute, QWebSettings.LocalStorageEnabled),
'maximum-pages-in-cache':
(MapType.static_setter, lambda v:
QWebSettings.setMaximumPagesInCache(v)),
'object-cache-capacities':
(MapType.static_setter, lambda v:
QWebSettings.setObjectCacheCapacities(*v)),
'offline-storage-default-quota':
(MapType.static_setter, lambda v:
QWebSettings.setOfflineStorageDefaultQuota(v)),
'offline-web-application-cache-quota':
(MapType.static_setter, lambda v:
QWebSettings.setOfflineWebApplicationCacheQuota(v)),
},
'general': {
'private-browsing':
(MapType.attribute, QWebSettings.PrivateBrowsingEnabled),
'developer-extras':
(MapType.attribute, QWebSettings.DeveloperExtrasEnabled),
'print-element-backgrounds':
(MapType.attribute, QWebSettings.PrintElementBackgrounds),
'xss-auditing':
(MapType.attribute, QWebSettings.XSSAuditingEnabled),
'site-specific-quirks':
(MapType.attribute, QWebSettings.SiteSpecificQuirksEnabled),
'default-encoding':
(MapType.setter, lambda qws, v: qws.setDefaultTextEncoding(v)),
}
2014-05-04 01:06:52 +02:00
}
2014-05-04 01:33:01 +02:00
2014-04-16 15:49:56 +02:00
settings = None
2014-05-04 01:50:37 +02:00
def _set_setting(typ, arg, value):
"""Set a QWebSettings setting.
Args:
2014-05-05 07:45:36 +02:00
typ: The type of the item
(MapType.attribute/MapType.setter/MapType.static_setter)
2014-05-04 01:50:37 +02:00
arg: The argument (attribute/handler)
value: The value to set.
"""
2014-05-05 07:45:36 +02:00
if typ == MapType.attribute:
2014-05-04 01:50:37 +02:00
settings.setAttribute(arg, value)
2014-05-05 07:45:36 +02:00
elif typ == MapType.setter and value is not None:
2014-05-04 01:50:37 +02:00
arg(settings, value)
2014-05-05 07:45:36 +02:00
elif typ == MapType.static_setter and value is not None:
2014-05-04 01:50:37 +02:00
arg(value)
2014-05-08 22:33:24 +02:00
def init():
"""Initialize the global QWebSettings."""
2014-04-16 15:49:56 +02:00
global settings
2014-05-08 22:33:24 +02:00
cachedir = get_standard_dir(QStandardPaths.CacheLocation)
2014-05-04 01:11:19 +02:00
QWebSettings.enablePersistentStorage(cachedir)
2014-04-16 15:49:56 +02:00
settings = QWebSettings.globalSettings()
2014-06-03 20:28:51 +02:00
for sectname, section in MAPPINGS.items():
for optname, (typ, arg) in section.items():
value = config.get(sectname, optname)
_set_setting(typ, arg, value)
2014-04-16 15:49:56 +02:00
@pyqtSlot(str, str)
def on_config_changed(section, option):
2014-04-16 15:49:56 +02:00
"""Update global settings when qwebsettings changed."""
2014-06-03 20:28:51 +02:00
try:
typ, arg = MAPPINGS[section][option]
except KeyError:
return
value = config.get(section, option)
_set_setting(typ, arg, value)