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) profile.setHttpUserAgent(config.val.content.user_agent)
def update_settings(section, option): def update_settings(option):
"""Update global settings when qwebsettings changed.""" """Update global settings when qwebsettings changed."""
websettings.update_mappings(MAPPINGS, section, option) websettings.update_mappings(MAPPINGS, option)
if section == 'ui' and option in ['hide-scrollbar', 'user-stylesheet']: if option in ['scrollbar.hide', 'content.user_stylesheet']:
_init_stylesheet(default_profile) _init_stylesheet(default_profile)
_init_stylesheet(private_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(default_profile)
_set_user_agent(private_profile) _set_user_agent(private_profile)
@ -211,7 +211,7 @@ def init(args):
# We need to do this here as a WORKAROUND for # We need to do this here as a WORKAROUND for
# https://bugreports.qt.io/browse/QTBUG-58650 # https://bugreports.qt.io/browse/QTBUG-58650
if not qtutils.version_check('5.9'): 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) Attribute(QWebEngineSettings.FullScreenSupportEnabled).set(True)
websettings.init_mappings(MAPPINGS) websettings.init_mappings(MAPPINGS)
@ -236,79 +236,70 @@ def shutdown():
MAPPINGS = { MAPPINGS = {
'content': { 'content.images':
'allow-images': Attribute(QWebEngineSettings.AutoLoadImages),
Attribute(QWebEngineSettings.AutoLoadImages), 'content.javascript.enabled':
'allow-javascript': Attribute(QWebEngineSettings.JavascriptEnabled),
Attribute(QWebEngineSettings.JavascriptEnabled), 'content.javascript.can_open_windows_automatically':
'javascript-can-open-windows-automatically': Attribute(QWebEngineSettings.JavascriptCanOpenWindows),
Attribute(QWebEngineSettings.JavascriptCanOpenWindows), 'content.javascript.can_access_clipboard':
'javascript-can-access-clipboard': Attribute(QWebEngineSettings.JavascriptCanAccessClipboard),
Attribute(QWebEngineSettings.JavascriptCanAccessClipboard), 'content.plugins':
'allow-plugins': Attribute(QWebEngineSettings.PluginsEnabled),
Attribute(QWebEngineSettings.PluginsEnabled), 'content.hyperlink_auditing':
'hyperlink-auditing': Attribute(QWebEngineSettings.HyperlinkAuditingEnabled),
Attribute(QWebEngineSettings.HyperlinkAuditingEnabled), 'content.local_content_can_access_remote_urls':
'local-content-can-access-remote-urls': Attribute(QWebEngineSettings.LocalContentCanAccessRemoteUrls),
Attribute(QWebEngineSettings.LocalContentCanAccessRemoteUrls), 'content.local_content_can_access_file_urls':
'local-content-can-access-file-urls': Attribute(QWebEngineSettings.LocalContentCanAccessFileUrls),
Attribute(QWebEngineSettings.LocalContentCanAccessFileUrls), 'content.webgl':
'webgl': Attribute(QWebEngineSettings.WebGLEnabled),
Attribute(QWebEngineSettings.WebGLEnabled), 'content.local_storage':
}, Attribute(QWebEngineSettings.LocalStorageEnabled),
'input': { 'content.cache_size':
'spatial-navigation': # 0: automatically managed by QtWebEngine
Attribute(QWebEngineSettings.SpatialNavigationEnabled), DefaultProfileSetter('setHttpCacheMaximumSize', default=0),
'links-included-in-focus-chain': 'content.xss_auditing':
Attribute(QWebEngineSettings.LinksIncludedInFocusChain), Attribute(QWebEngineSettings.XSSAuditingEnabled),
}, 'content.default_encoding':
'fonts': { Setter(QWebEngineSettings.setDefaultTextEncoding),
'web-family-standard':
FontFamilySetter(QWebEngineSettings.StandardFont), 'input.spatial_navigation':
'web-family-fixed': Attribute(QWebEngineSettings.SpatialNavigationEnabled),
FontFamilySetter(QWebEngineSettings.FixedFont), 'input.links_included_in_focus_chain':
'web-family-serif': Attribute(QWebEngineSettings.LinksIncludedInFocusChain),
FontFamilySetter(QWebEngineSettings.SerifFont),
'web-family-sans-serif': 'fonts.web.family.standard':
FontFamilySetter(QWebEngineSettings.SansSerifFont), FontFamilySetter(QWebEngineSettings.StandardFont),
'web-family-cursive': 'fonts.web.family.fixed':
FontFamilySetter(QWebEngineSettings.CursiveFont), FontFamilySetter(QWebEngineSettings.FixedFont),
'web-family-fantasy': 'fonts.web.family.serif':
FontFamilySetter(QWebEngineSettings.FantasyFont), FontFamilySetter(QWebEngineSettings.SerifFont),
'web-size-minimum': 'fonts.web.family.sans_serif':
Setter(QWebEngineSettings.setFontSize, FontFamilySetter(QWebEngineSettings.SansSerifFont),
args=[QWebEngineSettings.MinimumFontSize]), 'fonts.web.family.cursive':
'web-size-minimum-logical': FontFamilySetter(QWebEngineSettings.CursiveFont),
Setter(QWebEngineSettings.setFontSize, 'fonts.web.family.fantasy':
args=[QWebEngineSettings.MinimumLogicalFontSize]), FontFamilySetter(QWebEngineSettings.FantasyFont),
'web-size-default': 'fonts.web.size.minimum':
Setter(QWebEngineSettings.setFontSize, Setter(QWebEngineSettings.setFontSize,
args=[QWebEngineSettings.DefaultFontSize]), args=[QWebEngineSettings.MinimumFontSize]),
'web-size-default-fixed': 'fonts.web.size.minimum_logical':
Setter(QWebEngineSettings.setFontSize, Setter(QWebEngineSettings.setFontSize,
args=[QWebEngineSettings.DefaultFixedFontSize]), args=[QWebEngineSettings.MinimumLogicalFontSize]),
}, 'fonts.web.size.default':
'ui': { Setter(QWebEngineSettings.setFontSize,
'smooth-scrolling': args=[QWebEngineSettings.DefaultFontSize]),
Attribute(QWebEngineSettings.ScrollAnimatorEnabled), 'fonts.web.size.default_fixed':
}, Setter(QWebEngineSettings.setFontSize,
'storage': { args=[QWebEngineSettings.DefaultFixedFontSize]),
'local-storage':
Attribute(QWebEngineSettings.LocalStorageEnabled), 'scrolling.smooth':
'cache-size': Attribute(QWebEngineSettings.ScrollAnimatorEnabled),
# 0: automatically managed by QtWebEngine
DefaultProfileSetter('setHttpCacheMaximumSize', default=0),
},
'general': {
'xss-auditing':
Attribute(QWebEngineSettings.XSSAuditingEnabled),
'default-encoding':
Setter(QWebEngineSettings.setDefaultTextEncoding),
}
} }
try: try:
MAPPINGS['general']['print-element-backgrounds'] = Attribute( MAPPINGS['content.print_element_backgrounds'] = Attribute(
QWebEngineSettings.PrintElementBackgrounds) QWebEngineSettings.PrintElementBackgrounds)
except AttributeError: except AttributeError:
# Added in Qt 5.8 # Added in Qt 5.8
@ -317,4 +308,4 @@ except AttributeError:
if qtutils.version_check('5.9'): if qtutils.version_check('5.9'):
# https://bugreports.qt.io/browse/QTBUG-58650 # 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) QWebSettings.globalSettings().setUserStyleSheetUrl(url)
def update_settings(section, option): def update_settings(option):
"""Update global settings when qwebsettings changed.""" """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() _set_user_stylesheet()
websettings.update_mappings(MAPPINGS, option)
websettings.update_mappings(MAPPINGS, section, option)
def init(_args): def init(_args):
@ -152,96 +151,79 @@ def shutdown():
MAPPINGS = { MAPPINGS = {
'content': { 'content.images':
'allow-images': Attribute(QWebSettings.AutoLoadImages),
Attribute(QWebSettings.AutoLoadImages), 'content.javascript.enabled':
'allow-javascript': Attribute(QWebSettings.JavascriptEnabled),
Attribute(QWebSettings.JavascriptEnabled), 'content.javascript.can_open_windows_automatically':
'javascript-can-open-windows-automatically': Attribute(QWebSettings.JavascriptCanOpenWindows),
Attribute(QWebSettings.JavascriptCanOpenWindows), 'content.javascript.can_close_windows':
'javascript-can-close-windows': Attribute(QWebSettings.JavascriptCanCloseWindows),
Attribute(QWebSettings.JavascriptCanCloseWindows), 'content.javascript.can_access_clipboard':
'javascript-can-access-clipboard': Attribute(QWebSettings.JavascriptCanAccessClipboard),
Attribute(QWebSettings.JavascriptCanAccessClipboard), 'content.plugins':
'allow-plugins': Attribute(QWebSettings.PluginsEnabled),
Attribute(QWebSettings.PluginsEnabled), 'content.webgl':
'webgl': Attribute(QWebSettings.WebGLEnabled),
Attribute(QWebSettings.WebGLEnabled), 'content.hyperlink_auditing':
'hyperlink-auditing': Attribute(QWebSettings.HyperlinkAuditingEnabled),
Attribute(QWebSettings.HyperlinkAuditingEnabled), 'content.local_content_can_access_remote_urls':
'local-content-can-access-remote-urls': Attribute(QWebSettings.LocalContentCanAccessRemoteUrls),
Attribute(QWebSettings.LocalContentCanAccessRemoteUrls), 'content.local_content_can_access_file_urls':
'local-content-can-access-file-urls': Attribute(QWebSettings.LocalContentCanAccessFileUrls),
Attribute(QWebSettings.LocalContentCanAccessFileUrls), 'content.cookies.accept':
'cookies-accept': CookiePolicy(),
CookiePolicy(), 'content.dns_prefetch':
}, Attribute(QWebSettings.DnsPrefetchEnabled),
'network': { 'content.frame-flattening':
'dns-prefetch': Attribute(QWebSettings.FrameFlatteningEnabled),
Attribute(QWebSettings.DnsPrefetchEnabled), 'content.offline_web_application_cache':
}, Attribute(QWebSettings.OfflineWebApplicationCacheEnabled),
'input': { 'content.local_storage':
'spatial-navigation': Attribute(QWebSettings.LocalStorageEnabled,
Attribute(QWebSettings.SpatialNavigationEnabled), QWebSettings.OfflineStorageDatabaseEnabled),
'links-included-in-focus-chain': 'content.maximum_pages_in_cache':
Attribute(QWebSettings.LinksIncludedInFocusChain), StaticSetter(QWebSettings.setMaximumPagesInCache),
}, 'content.developer_extras':
'fonts': { Attribute(QWebSettings.DeveloperExtrasEnabled),
'web-family-standard': 'content.print_element_backgrounds':
FontFamilySetter(QWebSettings.StandardFont), Attribute(QWebSettings.PrintElementBackgrounds),
'web-family-fixed': 'content.xss_auditing':
FontFamilySetter(QWebSettings.FixedFont), Attribute(QWebSettings.XSSAuditingEnabled),
'web-family-serif': 'content.default_encoding':
FontFamilySetter(QWebSettings.SerifFont), Setter(QWebSettings.setDefaultTextEncoding),
'web-family-sans-serif': # content.user_stylesheet is handled separately
FontFamilySetter(QWebSettings.SansSerifFont),
'web-family-cursive': 'input.spatial_navigation':
FontFamilySetter(QWebSettings.CursiveFont), Attribute(QWebSettings.SpatialNavigationEnabled),
'web-family-fantasy': 'input.links_included_in_focus_chain':
FontFamilySetter(QWebSettings.FantasyFont), Attribute(QWebSettings.LinksIncludedInFocusChain),
'web-size-minimum':
Setter(QWebSettings.setFontSize, 'fonts.web.family.standard':
args=[QWebSettings.MinimumFontSize]), FontFamilySetter(QWebSettings.StandardFont),
'web-size-minimum-logical': 'fonts.web.family.fixed':
Setter(QWebSettings.setFontSize, FontFamilySetter(QWebSettings.FixedFont),
args=[QWebSettings.MinimumLogicalFontSize]), 'fonts.web.family.serif':
'web-size-default': FontFamilySetter(QWebSettings.SerifFont),
Setter(QWebSettings.setFontSize, 'fonts.web.family.sans_serif':
args=[QWebSettings.DefaultFontSize]), FontFamilySetter(QWebSettings.SansSerifFont),
'web-size-default-fixed': 'fonts.web.family.cursive':
Setter(QWebSettings.setFontSize, FontFamilySetter(QWebSettings.CursiveFont),
args=[QWebSettings.DefaultFixedFontSize]), 'fonts.web.family.fantasy':
}, FontFamilySetter(QWebSettings.FantasyFont),
'ui': { 'fonts.web.size.minimum':
'zoom-text-only': Setter(QWebSettings.setFontSize, args=[QWebSettings.MinimumFontSize]),
Attribute(QWebSettings.ZoomTextOnly), 'fonts.web.size.minimum_logical':
'frame-flattening': Setter(QWebSettings.setFontSize,
Attribute(QWebSettings.FrameFlatteningEnabled), args=[QWebSettings.MinimumLogicalFontSize]),
# user-stylesheet is handled separately 'fonts.web.size.default':
'smooth-scrolling': Setter(QWebSettings.setFontSize, args=[QWebSettings.DefaultFontSize]),
Attribute(QWebSettings.ScrollAnimatorEnabled), 'fonts.web.size.default_fixed':
#'accelerated-compositing': Setter(QWebSettings.setFontSize,
# Attribute(QWebSettings.AcceleratedCompositingEnabled), args=[QWebSettings.DefaultFixedFontSize]),
#'tiled-backing-store':
# Attribute(QWebSettings.TiledBackingStoreEnabled), 'zoom.text_only':
}, Attribute(QWebSettings.ZoomTextOnly),
'storage': { 'scrolling.smooth':
'offline-web-application-cache': Attribute(QWebSettings.ScrollAnimatorEnabled),
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),
}
} }

View File

@ -55,8 +55,7 @@ UNSET = object()
# FIXME:conf for new config # FIXME:conf for new config
val = None val = None
instance = None
def get(*args, **kwargs): def get(*args, **kwargs):
@ -183,11 +182,12 @@ def init(parent=None):
Args: Args:
parent: The parent to pass to QObjects which get initialized. parent: The parent to pass to QObjects which get initialized.
""" """
global val global val, instance
# _init_main_config(parent) # _init_main_config(parent)
configdata.init() configdata.init()
newconfig.init(parent) newconfig.init(parent)
val = newconfig.val val = newconfig.val
instance = newconfig.instance
_init_key_config(parent) _init_key_config(parent)
_init_misc() _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 # An easy way to access the config from other code via config.val.foo
val = None val = None
instance = None
_change_filters = [] _change_filters = []
@ -192,8 +193,9 @@ def init(parent):
new_config.read_defaults() new_config.read_defaults()
objreg.register('config', new_config) objreg.register('config', new_config)
global val global val, instance
val = ConfigContainer(new_config) val = ConfigContainer(new_config)
instance = new_config
for cf in _change_filters: for cf in _change_filters:
cf.validate() cf.validate()

View File

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