Refactor websettings for new config

This commit is contained in:
Florian Bruhin 2017-06-13 13:47:06 +02:00
parent 129ee33ffb
commit af134eb861
5 changed files with 158 additions and 185 deletions

View File

@ -166,13 +166,13 @@ def _set_user_agent(profile):
profile.setHttpUserAgent(config.val.content.user_agent)
def update_settings(section, option):
def update_settings(option):
"""Update global settings when qwebsettings changed."""
websettings.update_mappings(MAPPINGS, section, option)
if section == 'ui' and option in ['hide-scrollbar', 'user-stylesheet']:
websettings.update_mappings(MAPPINGS, option)
if option in ['scrollbar.hide', 'content.user_stylesheet']:
_init_stylesheet(default_profile)
_init_stylesheet(private_profile)
elif section == 'network' and option == 'user-agent':
elif option == 'content.user_agent':
_set_user_agent(default_profile)
_set_user_agent(private_profile)
@ -211,7 +211,7 @@ def init(args):
# We need to do this here as a WORKAROUND for
# https://bugreports.qt.io/browse/QTBUG-58650
if not qtutils.version_check('5.9'):
PersistentCookiePolicy().set(config.val.content.cookies_store)
PersistentCookiePolicy().set(config.val.content.cookies.store)
Attribute(QWebEngineSettings.FullScreenSupportEnabled).set(True)
websettings.init_mappings(MAPPINGS)
@ -236,79 +236,70 @@ def shutdown():
MAPPINGS = {
'content': {
'allow-images':
Attribute(QWebEngineSettings.AutoLoadImages),
'allow-javascript':
Attribute(QWebEngineSettings.JavascriptEnabled),
'javascript-can-open-windows-automatically':
Attribute(QWebEngineSettings.JavascriptCanOpenWindows),
'javascript-can-access-clipboard':
Attribute(QWebEngineSettings.JavascriptCanAccessClipboard),
'allow-plugins':
Attribute(QWebEngineSettings.PluginsEnabled),
'hyperlink-auditing':
Attribute(QWebEngineSettings.HyperlinkAuditingEnabled),
'local-content-can-access-remote-urls':
Attribute(QWebEngineSettings.LocalContentCanAccessRemoteUrls),
'local-content-can-access-file-urls':
Attribute(QWebEngineSettings.LocalContentCanAccessFileUrls),
'webgl':
Attribute(QWebEngineSettings.WebGLEnabled),
},
'input': {
'spatial-navigation':
Attribute(QWebEngineSettings.SpatialNavigationEnabled),
'links-included-in-focus-chain':
Attribute(QWebEngineSettings.LinksIncludedInFocusChain),
},
'fonts': {
'web-family-standard':
FontFamilySetter(QWebEngineSettings.StandardFont),
'web-family-fixed':
FontFamilySetter(QWebEngineSettings.FixedFont),
'web-family-serif':
FontFamilySetter(QWebEngineSettings.SerifFont),
'web-family-sans-serif':
FontFamilySetter(QWebEngineSettings.SansSerifFont),
'web-family-cursive':
FontFamilySetter(QWebEngineSettings.CursiveFont),
'web-family-fantasy':
FontFamilySetter(QWebEngineSettings.FantasyFont),
'web-size-minimum':
Setter(QWebEngineSettings.setFontSize,
args=[QWebEngineSettings.MinimumFontSize]),
'web-size-minimum-logical':
Setter(QWebEngineSettings.setFontSize,
args=[QWebEngineSettings.MinimumLogicalFontSize]),
'web-size-default':
Setter(QWebEngineSettings.setFontSize,
args=[QWebEngineSettings.DefaultFontSize]),
'web-size-default-fixed':
Setter(QWebEngineSettings.setFontSize,
args=[QWebEngineSettings.DefaultFixedFontSize]),
},
'ui': {
'smooth-scrolling':
Attribute(QWebEngineSettings.ScrollAnimatorEnabled),
},
'storage': {
'local-storage':
Attribute(QWebEngineSettings.LocalStorageEnabled),
'cache-size':
# 0: automatically managed by QtWebEngine
DefaultProfileSetter('setHttpCacheMaximumSize', default=0),
},
'general': {
'xss-auditing':
Attribute(QWebEngineSettings.XSSAuditingEnabled),
'default-encoding':
Setter(QWebEngineSettings.setDefaultTextEncoding),
}
'content.images':
Attribute(QWebEngineSettings.AutoLoadImages),
'content.javascript.enabled':
Attribute(QWebEngineSettings.JavascriptEnabled),
'content.javascript.can_open_windows_automatically':
Attribute(QWebEngineSettings.JavascriptCanOpenWindows),
'content.javascript.can_access_clipboard':
Attribute(QWebEngineSettings.JavascriptCanAccessClipboard),
'content.plugins':
Attribute(QWebEngineSettings.PluginsEnabled),
'content.hyperlink_auditing':
Attribute(QWebEngineSettings.HyperlinkAuditingEnabled),
'content.local_content_can_access_remote_urls':
Attribute(QWebEngineSettings.LocalContentCanAccessRemoteUrls),
'content.local_content_can_access_file_urls':
Attribute(QWebEngineSettings.LocalContentCanAccessFileUrls),
'content.webgl':
Attribute(QWebEngineSettings.WebGLEnabled),
'content.local_storage':
Attribute(QWebEngineSettings.LocalStorageEnabled),
'content.cache_size':
# 0: automatically managed by QtWebEngine
DefaultProfileSetter('setHttpCacheMaximumSize', default=0),
'content.xss_auditing':
Attribute(QWebEngineSettings.XSSAuditingEnabled),
'content.default_encoding':
Setter(QWebEngineSettings.setDefaultTextEncoding),
'input.spatial_navigation':
Attribute(QWebEngineSettings.SpatialNavigationEnabled),
'input.links_included_in_focus_chain':
Attribute(QWebEngineSettings.LinksIncludedInFocusChain),
'fonts.web.family.standard':
FontFamilySetter(QWebEngineSettings.StandardFont),
'fonts.web.family.fixed':
FontFamilySetter(QWebEngineSettings.FixedFont),
'fonts.web.family.serif':
FontFamilySetter(QWebEngineSettings.SerifFont),
'fonts.web.family.sans_serif':
FontFamilySetter(QWebEngineSettings.SansSerifFont),
'fonts.web.family.cursive':
FontFamilySetter(QWebEngineSettings.CursiveFont),
'fonts.web.family.fantasy':
FontFamilySetter(QWebEngineSettings.FantasyFont),
'fonts.web.size.minimum':
Setter(QWebEngineSettings.setFontSize,
args=[QWebEngineSettings.MinimumFontSize]),
'fonts.web.size.minimum_logical':
Setter(QWebEngineSettings.setFontSize,
args=[QWebEngineSettings.MinimumLogicalFontSize]),
'fonts.web.size.default':
Setter(QWebEngineSettings.setFontSize,
args=[QWebEngineSettings.DefaultFontSize]),
'fonts.web.size.default_fixed':
Setter(QWebEngineSettings.setFontSize,
args=[QWebEngineSettings.DefaultFixedFontSize]),
'scrolling.smooth':
Attribute(QWebEngineSettings.ScrollAnimatorEnabled),
}
try:
MAPPINGS['general']['print-element-backgrounds'] = Attribute(
MAPPINGS['content.print_element_backgrounds'] = Attribute(
QWebEngineSettings.PrintElementBackgrounds)
except AttributeError:
# Added in Qt 5.8
@ -317,4 +308,4 @@ except AttributeError:
if qtutils.version_check('5.9'):
# https://bugreports.qt.io/browse/QTBUG-58650
MAPPINGS['content']['cookies-store'] = PersistentCookiePolicy()
MAPPINGS['content.cookies.store'] = PersistentCookiePolicy()

View File

@ -111,12 +111,11 @@ def _set_user_stylesheet():
QWebSettings.globalSettings().setUserStyleSheetUrl(url)
def update_settings(section, option):
def update_settings(option):
"""Update global settings when qwebsettings changed."""
if section == 'ui' and option in ['hide-scrollbar', 'user-stylesheet']:
if option in ['scrollbar.hide', 'content.user_stylesheet']:
_set_user_stylesheet()
websettings.update_mappings(MAPPINGS, section, option)
websettings.update_mappings(MAPPINGS, option)
def init(_args):
@ -152,96 +151,79 @@ def shutdown():
MAPPINGS = {
'content': {
'allow-images':
Attribute(QWebSettings.AutoLoadImages),
'allow-javascript':
Attribute(QWebSettings.JavascriptEnabled),
'javascript-can-open-windows-automatically':
Attribute(QWebSettings.JavascriptCanOpenWindows),
'javascript-can-close-windows':
Attribute(QWebSettings.JavascriptCanCloseWindows),
'javascript-can-access-clipboard':
Attribute(QWebSettings.JavascriptCanAccessClipboard),
'allow-plugins':
Attribute(QWebSettings.PluginsEnabled),
'webgl':
Attribute(QWebSettings.WebGLEnabled),
'hyperlink-auditing':
Attribute(QWebSettings.HyperlinkAuditingEnabled),
'local-content-can-access-remote-urls':
Attribute(QWebSettings.LocalContentCanAccessRemoteUrls),
'local-content-can-access-file-urls':
Attribute(QWebSettings.LocalContentCanAccessFileUrls),
'cookies-accept':
CookiePolicy(),
},
'network': {
'dns-prefetch':
Attribute(QWebSettings.DnsPrefetchEnabled),
},
'input': {
'spatial-navigation':
Attribute(QWebSettings.SpatialNavigationEnabled),
'links-included-in-focus-chain':
Attribute(QWebSettings.LinksIncludedInFocusChain),
},
'fonts': {
'web-family-standard':
FontFamilySetter(QWebSettings.StandardFont),
'web-family-fixed':
FontFamilySetter(QWebSettings.FixedFont),
'web-family-serif':
FontFamilySetter(QWebSettings.SerifFont),
'web-family-sans-serif':
FontFamilySetter(QWebSettings.SansSerifFont),
'web-family-cursive':
FontFamilySetter(QWebSettings.CursiveFont),
'web-family-fantasy':
FontFamilySetter(QWebSettings.FantasyFont),
'web-size-minimum':
Setter(QWebSettings.setFontSize,
args=[QWebSettings.MinimumFontSize]),
'web-size-minimum-logical':
Setter(QWebSettings.setFontSize,
args=[QWebSettings.MinimumLogicalFontSize]),
'web-size-default':
Setter(QWebSettings.setFontSize,
args=[QWebSettings.DefaultFontSize]),
'web-size-default-fixed':
Setter(QWebSettings.setFontSize,
args=[QWebSettings.DefaultFixedFontSize]),
},
'ui': {
'zoom-text-only':
Attribute(QWebSettings.ZoomTextOnly),
'frame-flattening':
Attribute(QWebSettings.FrameFlatteningEnabled),
# user-stylesheet is handled separately
'smooth-scrolling':
Attribute(QWebSettings.ScrollAnimatorEnabled),
#'accelerated-compositing':
# Attribute(QWebSettings.AcceleratedCompositingEnabled),
#'tiled-backing-store':
# Attribute(QWebSettings.TiledBackingStoreEnabled),
},
'storage': {
'offline-web-application-cache':
Attribute(QWebSettings.OfflineWebApplicationCacheEnabled),
'local-storage':
Attribute(QWebSettings.LocalStorageEnabled,
QWebSettings.OfflineStorageDatabaseEnabled),
'maximum-pages-in-cache':
StaticSetter(QWebSettings.setMaximumPagesInCache),
},
'general': {
'developer-extras':
Attribute(QWebSettings.DeveloperExtrasEnabled),
'print-element-backgrounds':
Attribute(QWebSettings.PrintElementBackgrounds),
'xss-auditing':
Attribute(QWebSettings.XSSAuditingEnabled),
'default-encoding':
Setter(QWebSettings.setDefaultTextEncoding),
}
'content.images':
Attribute(QWebSettings.AutoLoadImages),
'content.javascript.enabled':
Attribute(QWebSettings.JavascriptEnabled),
'content.javascript.can_open_windows_automatically':
Attribute(QWebSettings.JavascriptCanOpenWindows),
'content.javascript.can_close_windows':
Attribute(QWebSettings.JavascriptCanCloseWindows),
'content.javascript.can_access_clipboard':
Attribute(QWebSettings.JavascriptCanAccessClipboard),
'content.plugins':
Attribute(QWebSettings.PluginsEnabled),
'content.webgl':
Attribute(QWebSettings.WebGLEnabled),
'content.hyperlink_auditing':
Attribute(QWebSettings.HyperlinkAuditingEnabled),
'content.local_content_can_access_remote_urls':
Attribute(QWebSettings.LocalContentCanAccessRemoteUrls),
'content.local_content_can_access_file_urls':
Attribute(QWebSettings.LocalContentCanAccessFileUrls),
'content.cookies.accept':
CookiePolicy(),
'content.dns_prefetch':
Attribute(QWebSettings.DnsPrefetchEnabled),
'content.frame-flattening':
Attribute(QWebSettings.FrameFlatteningEnabled),
'content.offline_web_application_cache':
Attribute(QWebSettings.OfflineWebApplicationCacheEnabled),
'content.local_storage':
Attribute(QWebSettings.LocalStorageEnabled,
QWebSettings.OfflineStorageDatabaseEnabled),
'content.maximum_pages_in_cache':
StaticSetter(QWebSettings.setMaximumPagesInCache),
'content.developer_extras':
Attribute(QWebSettings.DeveloperExtrasEnabled),
'content.print_element_backgrounds':
Attribute(QWebSettings.PrintElementBackgrounds),
'content.xss_auditing':
Attribute(QWebSettings.XSSAuditingEnabled),
'content.default_encoding':
Setter(QWebSettings.setDefaultTextEncoding),
# content.user_stylesheet is handled separately
'input.spatial_navigation':
Attribute(QWebSettings.SpatialNavigationEnabled),
'input.links_included_in_focus_chain':
Attribute(QWebSettings.LinksIncludedInFocusChain),
'fonts.web.family.standard':
FontFamilySetter(QWebSettings.StandardFont),
'fonts.web.family.fixed':
FontFamilySetter(QWebSettings.FixedFont),
'fonts.web.family.serif':
FontFamilySetter(QWebSettings.SerifFont),
'fonts.web.family.sans_serif':
FontFamilySetter(QWebSettings.SansSerifFont),
'fonts.web.family.cursive':
FontFamilySetter(QWebSettings.CursiveFont),
'fonts.web.family.fantasy':
FontFamilySetter(QWebSettings.FantasyFont),
'fonts.web.size.minimum':
Setter(QWebSettings.setFontSize, args=[QWebSettings.MinimumFontSize]),
'fonts.web.size.minimum_logical':
Setter(QWebSettings.setFontSize,
args=[QWebSettings.MinimumLogicalFontSize]),
'fonts.web.size.default':
Setter(QWebSettings.setFontSize, args=[QWebSettings.DefaultFontSize]),
'fonts.web.size.default_fixed':
Setter(QWebSettings.setFontSize,
args=[QWebSettings.DefaultFixedFontSize]),
'zoom.text_only':
Attribute(QWebSettings.ZoomTextOnly),
'scrolling.smooth':
Attribute(QWebSettings.ScrollAnimatorEnabled),
}

View File

@ -55,8 +55,7 @@ UNSET = object()
# FIXME:conf for new config
val = None
instance = None
def get(*args, **kwargs):
@ -183,11 +182,12 @@ def init(parent=None):
Args:
parent: The parent to pass to QObjects which get initialized.
"""
global val
global val, instance
# _init_main_config(parent)
configdata.init()
newconfig.init(parent)
val = newconfig.val
instance = newconfig.instance
_init_key_config(parent)
_init_misc()

View File

@ -28,6 +28,7 @@ from qutebrowser.utils import utils, objreg
# An easy way to access the config from other code via config.val.foo
val = None
instance = None
_change_filters = []
@ -192,8 +193,9 @@ def init(parent):
new_config.read_defaults()
objreg.register('config', new_config)
global val
global val, instance
val = ConfigContainer(new_config)
instance = new_config
for cf in _change_filters:
cf.validate()

View File

@ -192,21 +192,19 @@ class FontFamilySetter(Setter):
def init_mappings(mappings):
"""Initialize all settings based on a settings mapping."""
for sectname, section in mappings.items():
for optname, mapping in section.items():
value = config.get(sectname, optname)
log.config.vdebug("Setting {} -> {} to {!r}".format(
sectname, optname, value))
mapping.set(value)
for option, mapping in mappings.items():
value = config.instance.get(option)
log.config.vdebug("Setting {} to {!r}".format(option, value))
mapping.set(value)
def update_mappings(mappings, section, option):
def update_mappings(mappings, option):
"""Update global settings when QWeb(Engine)Settings changed."""
try:
mapping = mappings[section][option]
mapping = mappings[option]
except KeyError:
return
value = config.get(section, option)
value = config.instance.get(option)
mapping.set(value)