Skip to content
Snippets Groups Projects
Commit 1f907bf7 authored by Philipp Hörist's avatar Philipp Hörist
Browse files

Settings: Add notification about changed settings

parent 8afca1d1
No related branches found
No related tags found
No related merge requests found
......@@ -193,6 +193,10 @@ def __init__(self, jid, account, name='', groups=None, show='', status='',
self.pep = {}
def connect_signal(self, setting, func):
app.settings.connect_signal(
setting, func, self.account.name, self.jid)
def get_full_jid(self):
if self.resource:
return self.jid + '/' + self.resource
......
......@@ -21,8 +21,11 @@
import json
import logging
import sqlite3
import inspect
import weakref
from pathlib import Path
from collections import namedtuple
from collections import defaultdict
from gi.repository import GLib
......@@ -74,6 +77,38 @@ def __init__(self):
self._settings = {}
self._account_settings = {}
self._callbacks = defaultdict(list)
def connect_signal(self, setting, func, account=None, jid=None):
if not inspect.ismethod(func):
# static methods are not bound to an object so we can’t easily
# remove the func once it should not be called anymore
raise ValueError('Only bound methods can be connected')
func = weakref.WeakMethod(func)
self._callbacks[(setting, account, jid)].append(func)
def disconnect_signals(self, object_):
for _, handlers in self._callbacks.items():
for handler in list(handlers):
func = handler()
if func is None or func.__self__ is object_:
print('remove handler', handler)
handlers.remove(handler)
def _notify(self, setting, account=None, jid=None):
log.info('Signal: %s changed', setting)
callbacks = self._callbacks[(setting, account, jid)]
for func in list(callbacks):
if func() is None:
callbacks.remove(func)
continue
try:
func()(setting, account, jid)
except Exception:
log.exception('Error while executing signal callback')
def init(self) -> None:
self._setup_installation_defaults()
self._connect_database()
......@@ -388,6 +423,8 @@ def set_app_setting(self, setting: str, value: SETTING_TYPE) -> None:
del self._settings['app'][setting]
except KeyError:
pass
self._notify(setting)
return
default = APP_SETTINGS[setting]
......@@ -398,6 +435,7 @@ def set_app_setting(self, setting: str, value: SETTING_TYPE) -> None:
self._settings['app'][setting] = value
self._commit_settings('app')
self._notify(setting)
set = set_app_setting
......@@ -506,6 +544,7 @@ def set_account_setting(self,
self._account_settings[account]['account'][setting] = value
self._commit_account_settings(account)
self._notify(setting, account)
def get_group_chat_setting(self,
account: str,
......@@ -568,6 +607,7 @@ def set_group_chat_setting(self,
group_chat_settings[jid][setting] = value
self._commit_account_settings(account)
self._notify(setting, account, jid)
def get_contact_setting(self,
account: str,
......@@ -621,6 +661,7 @@ def set_contact_setting(self,
contact_settings[jid][setting] = value
self._commit_account_settings(account)
self._notify(setting, account, jid)
def set_soundevent_setting(self,
event_name: str,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment