Expose a config_changed signal to extensions
This commit is contained in:
parent
42790e7623
commit
1b1872e464
@ -165,6 +165,7 @@ def init(args, crash_handler):
|
||||
qApp.setQuitOnLastWindowClosed(False)
|
||||
_init_icon()
|
||||
|
||||
loader.init()
|
||||
loader.load_components()
|
||||
try:
|
||||
_init_modules(args, crash_handler)
|
||||
|
@ -104,12 +104,10 @@ class HostBlocker:
|
||||
self._done_count = 0
|
||||
|
||||
self._local_hosts_file = str(data_dir / 'blocked-hosts')
|
||||
self._update_files()
|
||||
self.update_files()
|
||||
|
||||
self._config_hosts_file = str(config_dir / 'blocked-hosts')
|
||||
|
||||
config.instance.changed.connect(self._update_files)
|
||||
|
||||
def is_blocked(self, url, first_party_url=None):
|
||||
"""Check if the given URL (as QUrl) is blocked."""
|
||||
if first_party_url is not None and not first_party_url.isValid():
|
||||
@ -296,7 +294,7 @@ class HostBlocker:
|
||||
len(self._blocked_hosts), self._done_count))
|
||||
|
||||
@config.change_filter('content.host_blocking.lists')
|
||||
def _update_files(self):
|
||||
def update_files(self):
|
||||
"""Update files when the config changed."""
|
||||
if not config.val.content.host_blocking.lists:
|
||||
try:
|
||||
@ -331,4 +329,5 @@ def init(context):
|
||||
host_blocker = HostBlocker(data_dir=context.data_dir,
|
||||
config_dir=context.config_dir,
|
||||
args=context.args)
|
||||
context.signals.config_changed.connect(host_blocker.update_files)
|
||||
host_blocker.read_hosts()
|
||||
|
@ -28,7 +28,10 @@ import pathlib
|
||||
|
||||
import attr
|
||||
|
||||
from PyQt5.QtCore import pyqtSignal, QObject
|
||||
|
||||
from qutebrowser import components
|
||||
from qutebrowser.config import config
|
||||
from qutebrowser.utils import log, standarddir, objreg
|
||||
|
||||
|
||||
@ -40,6 +43,14 @@ class InitContext:
|
||||
data_dir = attr.ib() # type: pathlib.Path
|
||||
config_dir = attr.ib() # type: pathlib.Path
|
||||
args = attr.ib() # type: argparse.Namespace
|
||||
signals = attr.ib() # type: ExtensionSignals
|
||||
|
||||
|
||||
class ExtensionSignals(QObject):
|
||||
|
||||
"""Signals exposed to an extension."""
|
||||
|
||||
config_changed = pyqtSignal(str)
|
||||
|
||||
|
||||
@attr.s
|
||||
@ -61,6 +72,12 @@ class ExtensionInfo:
|
||||
name = attr.ib() # type: str
|
||||
|
||||
|
||||
# Global extension signals, shared between all extensions.
|
||||
# At some point we might want to make this per-extension, but then we'll need
|
||||
# to find out what to set as its Qt parent so it's kept alive.
|
||||
_extension_signals = ExtensionSignals()
|
||||
|
||||
|
||||
def add_module_info(module: types.ModuleType) -> ModuleInfo:
|
||||
"""Add ModuleInfo to a module (if not added yet)."""
|
||||
# pylint: disable=protected-access
|
||||
@ -121,7 +138,8 @@ def _get_init_context() -> InitContext:
|
||||
"""Get an InitContext object."""
|
||||
return InitContext(data_dir=pathlib.Path(standarddir.data()),
|
||||
config_dir=pathlib.Path(standarddir.config()),
|
||||
args=objreg.get('args'))
|
||||
args=objreg.get('args'),
|
||||
signals=_extension_signals)
|
||||
|
||||
|
||||
def _load_component(info: ExtensionInfo) -> types.ModuleType:
|
||||
@ -136,3 +154,7 @@ def _load_component(info: ExtensionInfo) -> types.ModuleType:
|
||||
mod_info.init_hook(_get_init_context())
|
||||
|
||||
return mod
|
||||
|
||||
|
||||
def init() -> None:
|
||||
config.instance.changed.connect(_extension_signals.config_changed)
|
||||
|
Loading…
Reference in New Issue
Block a user