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),
'allow-javascript': 'content.javascript.enabled':
Attribute(QWebEngineSettings.JavascriptEnabled), Attribute(QWebEngineSettings.JavascriptEnabled),
'javascript-can-open-windows-automatically': 'content.javascript.can_open_windows_automatically':
Attribute(QWebEngineSettings.JavascriptCanOpenWindows), Attribute(QWebEngineSettings.JavascriptCanOpenWindows),
'javascript-can-access-clipboard': 'content.javascript.can_access_clipboard':
Attribute(QWebEngineSettings.JavascriptCanAccessClipboard), Attribute(QWebEngineSettings.JavascriptCanAccessClipboard),
'allow-plugins': 'content.plugins':
Attribute(QWebEngineSettings.PluginsEnabled), Attribute(QWebEngineSettings.PluginsEnabled),
'hyperlink-auditing': 'content.hyperlink_auditing':
Attribute(QWebEngineSettings.HyperlinkAuditingEnabled), Attribute(QWebEngineSettings.HyperlinkAuditingEnabled),
'local-content-can-access-remote-urls': 'content.local_content_can_access_remote_urls':
Attribute(QWebEngineSettings.LocalContentCanAccessRemoteUrls), Attribute(QWebEngineSettings.LocalContentCanAccessRemoteUrls),
'local-content-can-access-file-urls': 'content.local_content_can_access_file_urls':
Attribute(QWebEngineSettings.LocalContentCanAccessFileUrls), Attribute(QWebEngineSettings.LocalContentCanAccessFileUrls),
'webgl': 'content.webgl':
Attribute(QWebEngineSettings.WebGLEnabled), Attribute(QWebEngineSettings.WebGLEnabled),
}, 'content.local_storage':
'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), Attribute(QWebEngineSettings.LocalStorageEnabled),
'cache-size': 'content.cache_size':
# 0: automatically managed by QtWebEngine # 0: automatically managed by QtWebEngine
DefaultProfileSetter('setHttpCacheMaximumSize', default=0), DefaultProfileSetter('setHttpCacheMaximumSize', default=0),
}, 'content.xss_auditing':
'general': {
'xss-auditing':
Attribute(QWebEngineSettings.XSSAuditingEnabled), Attribute(QWebEngineSettings.XSSAuditingEnabled),
'default-encoding': 'content.default_encoding':
Setter(QWebEngineSettings.setDefaultTextEncoding), 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: 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),
'allow-javascript': 'content.javascript.enabled':
Attribute(QWebSettings.JavascriptEnabled), Attribute(QWebSettings.JavascriptEnabled),
'javascript-can-open-windows-automatically': 'content.javascript.can_open_windows_automatically':
Attribute(QWebSettings.JavascriptCanOpenWindows), Attribute(QWebSettings.JavascriptCanOpenWindows),
'javascript-can-close-windows': 'content.javascript.can_close_windows':
Attribute(QWebSettings.JavascriptCanCloseWindows), Attribute(QWebSettings.JavascriptCanCloseWindows),
'javascript-can-access-clipboard': 'content.javascript.can_access_clipboard':
Attribute(QWebSettings.JavascriptCanAccessClipboard), Attribute(QWebSettings.JavascriptCanAccessClipboard),
'allow-plugins': 'content.plugins':
Attribute(QWebSettings.PluginsEnabled), Attribute(QWebSettings.PluginsEnabled),
'webgl': 'content.webgl':
Attribute(QWebSettings.WebGLEnabled), Attribute(QWebSettings.WebGLEnabled),
'hyperlink-auditing': 'content.hyperlink_auditing':
Attribute(QWebSettings.HyperlinkAuditingEnabled), Attribute(QWebSettings.HyperlinkAuditingEnabled),
'local-content-can-access-remote-urls': 'content.local_content_can_access_remote_urls':
Attribute(QWebSettings.LocalContentCanAccessRemoteUrls), Attribute(QWebSettings.LocalContentCanAccessRemoteUrls),
'local-content-can-access-file-urls': 'content.local_content_can_access_file_urls':
Attribute(QWebSettings.LocalContentCanAccessFileUrls), Attribute(QWebSettings.LocalContentCanAccessFileUrls),
'cookies-accept': 'content.cookies.accept':
CookiePolicy(), CookiePolicy(),
}, 'content.dns_prefetch':
'network': {
'dns-prefetch':
Attribute(QWebSettings.DnsPrefetchEnabled), Attribute(QWebSettings.DnsPrefetchEnabled),
}, 'content.frame-flattening':
'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), Attribute(QWebSettings.FrameFlatteningEnabled),
# user-stylesheet is handled separately 'content.offline_web_application_cache':
'smooth-scrolling':
Attribute(QWebSettings.ScrollAnimatorEnabled),
#'accelerated-compositing':
# Attribute(QWebSettings.AcceleratedCompositingEnabled),
#'tiled-backing-store':
# Attribute(QWebSettings.TiledBackingStoreEnabled),
},
'storage': {
'offline-web-application-cache':
Attribute(QWebSettings.OfflineWebApplicationCacheEnabled), Attribute(QWebSettings.OfflineWebApplicationCacheEnabled),
'local-storage': 'content.local_storage':
Attribute(QWebSettings.LocalStorageEnabled, Attribute(QWebSettings.LocalStorageEnabled,
QWebSettings.OfflineStorageDatabaseEnabled), QWebSettings.OfflineStorageDatabaseEnabled),
'maximum-pages-in-cache': 'content.maximum_pages_in_cache':
StaticSetter(QWebSettings.setMaximumPagesInCache), StaticSetter(QWebSettings.setMaximumPagesInCache),
}, 'content.developer_extras':
'general': {
'developer-extras':
Attribute(QWebSettings.DeveloperExtrasEnabled), Attribute(QWebSettings.DeveloperExtrasEnabled),
'print-element-backgrounds': 'content.print_element_backgrounds':
Attribute(QWebSettings.PrintElementBackgrounds), Attribute(QWebSettings.PrintElementBackgrounds),
'xss-auditing': 'content.xss_auditing':
Attribute(QWebSettings.XSSAuditingEnabled), Attribute(QWebSettings.XSSAuditingEnabled),
'default-encoding': 'content.default_encoding':
Setter(QWebSettings.setDefaultTextEncoding), 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 # 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(
sectname, optname, value))
mapping.set(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)