From de206c40cbf8e879a218cb0c3e00f074c1585502 Mon Sep 17 00:00:00 2001 From: lovetox <philipp@hoerist.com> Date: Wed, 18 Nov 2020 22:52:48 +0100 Subject: [PATCH] Settings: Simplify context dependent settings --- gajim/common/contacts.py | 8 +---- gajim/common/modules/chat_markers.py | 8 +---- gajim/common/modules/mam.py | 9 +----- gajim/common/settings.py | 44 ++++++++++++++++++---------- gajim/groupchat_control.py | 9 +----- gajim/gtk/const.py | 2 +- gajim/gtk/groupchat_settings.py | 7 ++--- gajim/gtk/settings.py | 13 ++++---- 8 files changed, 41 insertions(+), 59 deletions(-) diff --git a/gajim/common/contacts.py b/gajim/common/contacts.py index f8316873fe..29b405894f 100644 --- a/gajim/common/contacts.py +++ b/gajim/common/contacts.py @@ -271,14 +271,8 @@ def can_notify(self): if not self.is_groupchat: raise ValueError - disco_info = app.storage.cache.get_last_disco_info(self.jid) - - context = 'public' - if disco_info is not None and disco_info.muc_is_members_only: - context = 'private' - all_ = app.settings.get('notify_on_all_muc_messages') - room = self.settings.get('notify_on_all_messages', context=context) + room = self.settings.get('notify_on_all_messages') return all_ or room diff --git a/gajim/common/modules/chat_markers.py b/gajim/common/modules/chat_markers.py index 930110cc00..cbf1794cdb 100644 --- a/gajim/common/modules/chat_markers.py +++ b/gajim/common/modules/chat_markers.py @@ -91,14 +91,8 @@ def _send_marker(self, contact, marker, id_, type_): jid = app.get_jid_without_resource(contact.jid) if type_ in ('gc', 'pm'): - disco_info = app.storage.cache.get_last_disco_info(jid) - - context = 'public' - if disco_info is not None and disco_info.muc_is_members_only: - context = 'private' - if not app.settings.get_group_chat_setting( - self._account, jid, 'send_marker', context=context): + self._account, jid, 'send_marker'): return else: if not app.settings.get_contact_setting( diff --git a/gajim/common/modules/mam.py b/gajim/common/modules/mam.py index 03e27bdc9d..f6cba8a901 100644 --- a/gajim/common/modules/mam.py +++ b/gajim/common/modules/mam.py @@ -374,16 +374,9 @@ def request_archive_on_signin(self): def request_archive_on_muc_join(self, jid): _task = yield - disco_info = app.storage.cache.get_last_disco_info(jid) - - context = 'public' - if disco_info is not None and disco_info.muc_is_members_only: - context = 'private' - threshold = app.settings.get_group_chat_setting(self._account, jid, - 'sync_threshold', - context=context) + 'sync_threshold') self._log.info('Threshold for %s: %s', jid, threshold) if threshold == SyncThreshold.NO_SYNC: diff --git a/gajim/common/settings.py b/gajim/common/settings.py index d11ba1fdcc..4500725bfa 100644 --- a/gajim/common/settings.py +++ b/gajim/common/settings.py @@ -601,8 +601,7 @@ def set_account_setting(self, def get_group_chat_setting(self, account: str, jid: str, - setting: str, - context: str = None) -> SETTING_TYPE: + setting: str) -> SETTING_TYPE: if account not in self._account_settings: raise ValueError(f'Account missing: {account}') @@ -613,17 +612,26 @@ def get_group_chat_setting(self, try: return self._account_settings[account]['group_chat'][jid][setting] except KeyError: + + context = get_muc_context(jid) + if context is None: + # If there is no disco info available + # to determine the context assume public + log.warning('Unable to determine context for: %s', jid) + context = 'public' + default = ACCOUNT_SETTINGS['group_chat'][setting] if default is HAS_APP_DEFAULT: - if context is not None: - return self.get_app_setting( - f'gc_{setting}_{context}_default') + context_default_setting = f'gc_{setting}_{context}_default' + if context_default_setting in APP_SETTINGS: + return self.get_app_setting(context_default_setting) return self.get_app_setting(f'gc_{setting}_default') if default is HAS_ACCOUNT_DEFAULT: - if context is not None: - return self.get_account_setting( - account, f'gc_{setting}_{context}_default') + context_default_setting = f'gc_{setting}_{context}_default' + if context_default_setting in ACCOUNT_SETTINGS['account']: + return self.get_account_setting(account, + context_default_setting) return self.get_account_setting(account, f'gc_{setting}_default') @@ -633,8 +641,7 @@ def set_group_chat_setting(self, account: str, jid: str, setting: str, - value: SETTING_TYPE, - context: str = None) -> None: + value: SETTING_TYPE) -> None: if account not in self._account_settings: raise ValueError(f'Account missing: {account}') @@ -645,14 +652,22 @@ def set_group_chat_setting(self, default = ACCOUNT_SETTINGS['group_chat'][setting] if default in (HAS_APP_DEFAULT, HAS_ACCOUNT_DEFAULT): + context = get_muc_context(jid) + if context is None: + # If there is no disco info available + # to determine the context assume public + log.warning('Unable to determine context for: %s', jid) + context = 'public' + default_store = APP_SETTINGS if default is HAS_ACCOUNT_DEFAULT: default_store = ACCOUNT_SETTINGS['account'] - if context is None: - default = default_store[f'gc_{setting}_default'] + context_default_setting = f'gc_{setting}_{context}_default' + if context_default_setting in default_store: + default = default_store[context_default_setting] else: - default = default_store[f'gc_{setting}_{context}_default'] + default = default_store[f'gc_{setting}_default'] if not isinstance(value, type(default)) and value is not None: raise TypeError(f'Invalid type for {setting}: ' @@ -687,8 +702,7 @@ def set_group_chat_settings(self, if context is not None: if get_muc_context(jid) != context: continue - self.set_group_chat_setting( - account, jid, setting, value, context) + self.set_group_chat_setting(account, jid, setting, value) def get_contact_setting(self, account: str, diff --git a/gajim/groupchat_control.py b/gajim/groupchat_control.py index f2e047f9bb..ff61ab62fe 100644 --- a/gajim/groupchat_control.py +++ b/gajim/groupchat_control.py @@ -178,7 +178,7 @@ def __init__(self, parent_win, contact, muc_data, acct): # Groupchat settings self._groupchat_settings_box = GroupChatSettings( - self.account, self.room_jid, self.context) + self.account, self.room_jid) self.xml.settings_scrolled_box.add(self._groupchat_settings_box) # Groupchat invite @@ -273,13 +273,6 @@ def room_name(self): def disco_info(self): return app.storage.cache.get_last_disco_info(self.contact.jid) - @property - def context(self): - disco_info = self.disco_info - if disco_info is None or not self.disco_info.muc_is_members_only: - return 'public' - return 'private' - def add_actions(self): super().add_actions() actions = [ diff --git a/gajim/gtk/const.py b/gajim/gtk/const.py index 697237e702..429a47aac4 100644 --- a/gajim/gtk/const.py +++ b/gajim/gtk/const.py @@ -22,7 +22,7 @@ Filter = namedtuple('Filter', 'name pattern default') Setting = namedtuple('Setting', 'kind label type value name callback data desc ' - 'bind inverted enabled_func context props') + 'bind inverted enabled_func props') Setting.__new__.__defaults__ = (None,) * len(Setting._fields) # type: ignore @unique diff --git a/gajim/gtk/groupchat_settings.py b/gajim/gtk/groupchat_settings.py index 20ff01496c..e120689e30 100644 --- a/gajim/gtk/groupchat_settings.py +++ b/gajim/gtk/groupchat_settings.py @@ -24,7 +24,7 @@ class GroupChatSettings(SettingsBox): - def __init__(self, account, jid, context): + def __init__(self, account, jid): SettingsBox.__init__(self, account, jid) self.get_style_context().add_class('settings-border') @@ -52,8 +52,7 @@ def __init__(self, account, jid, context): Setting(SettingKind.SWITCH, _('Notify on all Messages'), SettingType.GROUP_CHAT, - 'notify_on_all_messages', - context=context), + 'notify_on_all_messages'), Setting(SettingKind.SWITCH, _('Minimize on Close'), @@ -75,14 +74,12 @@ def __init__(self, account, jid, context): _('Send Chat Markers'), SettingType.GROUP_CHAT, 'send_marker', - context=context, desc=_('Let others know if you read up to this point')), Setting(SettingKind.POPOVER, _('Sync Threshold'), SettingType.GROUP_CHAT, 'sync_threshold', - context=context, props={'entries': THRESHOLD_OPTIONS}), ] diff --git a/gajim/gtk/settings.py b/gajim/gtk/settings.py index 388ffd74a2..3e92f82fc6 100644 --- a/gajim/gtk/settings.py +++ b/gajim/gtk/settings.py @@ -148,8 +148,7 @@ def __init__(self, desc, bind, inverted, - enabled_func, - context): + enabled_func): Gtk.ListBoxRow.__init__(self) self._grid = Gtk.Grid() @@ -163,7 +162,6 @@ def __init__(self, self.label = label self.account = account self.jid = jid - self.context = context self.name = name self.bind = bind self.inverted = inverted @@ -252,11 +250,10 @@ def get_value(self): return self.__get_value(self.type_, self.value, self.account, - self.jid, - self.context) + self.jid) @staticmethod - def __get_value(type_, value, account, jid, context): + def __get_value(type_, value, account, jid): if value is None: return None if type_ == SettingType.VALUE: @@ -267,7 +264,7 @@ def __get_value(type_, value, account, jid, context): if type_ == SettingType.GROUP_CHAT: return app.settings.get_group_chat_setting( - account, jid, value, context) + account, jid, value) if type_ == SettingType.CONFIG: return app.settings.get(value) @@ -308,7 +305,7 @@ def set_value(self, state): elif self.type_ == SettingType.GROUP_CHAT: app.settings.set_group_chat_setting( - self.account, self.jid, self.value, state, self.context) + self.account, self.jid, self.value, state) if self.callback is not None: self.callback(state, self.data) -- GitLab