Philipp Hörist pushed to branch master at gajim / gajim
Commits: c8b014a4 by Philipp Hörist at 2022-10-06T00:00:19+02:00 refactor: Remove usage of app.connections
- - - - -
20 changed files:
- gajim/common/application.py - gajim/common/dbus/remote_control.py - gajim/common/helpers.py - gajim/common/modules/caps.py - gajim/common/passwords.py - gajim/gtk/account_side_bar.py - gajim/gtk/accounts.py - gajim/gtk/application.py - gajim/gtk/avatar.py - gajim/gtk/bookmarks.py - gajim/gtk/discovery.py - gajim/gtk/filetransfer.py - gajim/gtk/main_stack.py - gajim/gtk/mam_preferences.py - gajim/gtk/settings.py - gajim/gtk/start_chat.py - gajim/gtk/status_message_selector.py - gajim/gtk/synchronize_accounts.py - gajim/gui_interface.py - gajim/plugins/pluginmanager.py
Changes:
===================================== gajim/common/application.py ===================================== @@ -159,9 +159,9 @@ def start_shutdown(self, *args: Any, **kwargs: Any) -> None:
accounts_to_disconnect: dict[str, Client] = {}
- for account, client in app.connections.items(): - if app.account_is_available(account): - accounts_to_disconnect[account] = client + for client in app.get_clients(): + if client.state.is_available: + accounts_to_disconnect[client.account] = client
if not accounts_to_disconnect: self._quit_app() @@ -224,10 +224,10 @@ def _network_status_changed(self, self._log.info('Network connection available') else: self._log.info('Network connection lost') - for connection in app.connections.values(): - if (connection.state.is_connected or - connection.state.is_available): - connection.disconnect(gracefully=False, reconnect=True) + for client in app.get_clients(): + if (client.state.is_connected or + client.state.is_available): + client.disconnect(gracefully=False, reconnect=True)
def _check_for_updates(self) -> None: if not app.settings.get('check_for_update'):
===================================== gajim/common/dbus/remote_control.py ===================================== @@ -307,10 +307,12 @@ def change_status(status: str, message: str, account: str) -> bool: status = '' if account: if not status: - if account not in app.connections: + if account not in app.settings.get_active_accounts(): return False - status = app.connections[account].status - GLib.idle_add(app.connections[account].change_status, status, + status = app.get_client(account).status + + GLib.idle_add(app.get_client(account).change_status, + status, message) # pyright: ignore else: # account not specified, so change the status of all accounts @@ -318,13 +320,12 @@ def change_status(status: str, message: str, account: str) -> bool: if not app.settings.get_account_setting( acc, 'sync_with_global_status'): continue - if status: - status_ = status - else: - if acc not in app.connections: - continue - status_ = app.connections[acc].status - GLib.idle_add(app.connections[acc].change_status, status_, + + if not status: + status = app.get_client(acc).status + + GLib.idle_add(app.get_client(acc).change_status, + status, message) # pyright: ignore return False
@@ -346,15 +347,15 @@ def account_info(account: str) -> dict[str, str]: Show info on account: resource, jid, nick, prio, message ''' result: dict[str, str] = {} - if account in app.connections: + if account in app.settings.get_active_accounts(): # account is valid - con = app.connections[account] - result['status'] = con.status - result['name'] = con.name - result['jid'] = app.get_jid_from_account(con.name) - result['message'] = con.status_message - result['priority'] = str(con.priority) - result['resource'] = app.settings.get_account_setting(con.name, + client = app.get_client(account) + result['status'] = client.status + result['name'] = client.name + result['jid'] = app.get_jid_from_account(client.name) + result['message'] = client.status_message + result['priority'] = str(client.priority) + result['resource'] = app.settings.get_account_setting(client.name, 'resource') return result
===================================== gajim/common/helpers.py ===================================== @@ -509,11 +509,11 @@ def get_client_status(account: str) -> str:
def get_global_show() -> str: maxi = 0 - for account in app.connections: - if not app.settings.get_account_setting(account, + for client in app.get_clients(): + if not app.settings.get_account_setting(client.account, 'sync_with_global_status'): continue - status = get_client_status(account) + status = get_client_status(client.account) index = SHOW_LIST.index(status) if index > maxi: maxi = index @@ -523,14 +523,14 @@ def get_global_show() -> str: def get_global_status_message() -> str: maxi = 0 status_message = '' - for account, con in app.connections.items(): - if not app.settings.get_account_setting(account, + for client in app.get_clients(): + if not app.settings.get_account_setting(client.account, 'sync_with_global_status'): continue - index = SHOW_LIST.index(con.status) + index = SHOW_LIST.index(client.status) if index > maxi: maxi = index - status_message = con.status_message + status_message = client.status_message return status_message
@@ -539,7 +539,8 @@ def statuses_unified() -> bool: Test if all statuses are the same ''' reference = None - for account in app.connections: + for client in app.get_clients(): + account = client.account if not app.settings.get_account_setting(account, 'sync_with_global_status'): continue @@ -670,9 +671,11 @@ def get_optional_features(account: str) -> list[str]:
features.append(Namespace.NICK + '+notify')
- if app.connections[account].get_module('Bookmarks').nativ_bookmarks_used: + client = app.get_client(account) + + if client.get_module('Bookmarks').nativ_bookmarks_used: features.append(Namespace.BOOKMARKS_1 + '+notify') - elif app.connections[account].get_module('Bookmarks').pep_bookmarks_used: + elif client.get_module('Bookmarks').pep_bookmarks_used: features.append(Namespace.BOOKMARKS + '+notify') if app.is_installed('AV'): features.append(Namespace.JINGLE_RTP) @@ -686,8 +689,8 @@ def get_optional_features(account: str) -> list[str]:
def jid_is_blocked(account: str, jid: str) -> bool: - con = app.connections[account] - return jid in con.get_module('Blocking').blocked + client = app.get_client(account) + return jid in client.get_module('Blocking').blocked
def get_subscription_request_msg(account: Optional[str] = None) -> str:
===================================== gajim/common/modules/caps.py ===================================== @@ -193,9 +193,9 @@ def update_caps(self) -> None: if not app.account_is_available(self._account): return
- app.connections[self._account].change_status( - app.connections[self._account].status, - app.connections[self._account].status_message) + client = app.get_client(self._account) + client.change_status(client.status, + client.status_message)
def cleanup(self) -> None: self._remove_all_tasks()
===================================== gajim/common/passwords.py ===================================== @@ -145,8 +145,8 @@ def get_password(account_name: str) -> Optional[str]:
def save_password(account_name: str, password: str) -> bool: - if account_name in app.connections: - app.connections[account_name].password = password + if account_name in app.settings.get_active_accounts(): + app.get_client(account_name).password = password
if not app.settings.get_account_setting(account_name, 'savepass'): return True @@ -157,8 +157,8 @@ def save_password(account_name: str, password: str) -> bool:
def delete_password(account_name: str) -> None: - if account_name in app.connections: - app.connections[account_name].password = None + if account_name in app.settings.get_active_accounts(): + app.get_client(account_name).password = None
if app.settings.get('use_keyring'): return SecretPasswordStorage.delete_password(account_name)
===================================== gajim/gtk/account_side_bar.py ===================================== @@ -38,8 +38,7 @@ def __init__(self) -> None: self.get_style_context().add_class('account-sidebar') self.connect('row-activated', self._on_row_activated)
- self._accounts: list[str] = list(app.connections.keys()) - for account in self._accounts: + for account in app.settings.get_active_accounts(): self.add_account(account)
def add_account(self, account: str) -> None:
===================================== gajim/gtk/accounts.py ===================================== @@ -558,12 +558,12 @@ def _disable() -> None: switch.set_state(state) self._set_label(state)
- old_state = app.settings.get_account_setting(account, 'active') - if old_state == state: + account_is_active = app.settings.get_account_setting(account, 'active') + if account_is_active == state: return Gdk.EVENT_PROPAGATE
- if (account in app.connections and - not app.connections[account].state.is_disconnected): + if (account_is_active and + not app.get_client(account).state.is_disconnected): # Connecting or connected window = cast(AccountsWindow, get_app_window('AccountsWindow')) ConfirmationDialog( @@ -1003,7 +1003,7 @@ def __init__(self, account: str, parent: Gtk.Window) -> None:
def on_destroy(self, *args: Any) -> None: # Update priority - if self.account not in app.connections: + if self.account not in app.settings.get_active_accounts(): return client = app.get_client(self.account) show = client.status
===================================== gajim/gtk/application.py ===================================== @@ -271,7 +271,7 @@ def _startup(self) -> None: MainWindow()
def _open_uris(self, uris: list[str]) -> None: - accounts = list(app.connections.keys()) + accounts = app.settings.get_active_accounts() if not accounts: return
@@ -668,7 +668,7 @@ def _on_content_action(_action: Gio.SimpleAction, @staticmethod def _on_join_support_chat(_action: Gio.SimpleAction, _param: Optional[GLib.Variant]) -> None: - accounts = list(app.connections.keys()) + accounts = app.settings.get_active_accounts() if len(accounts) == 1: app.interface.show_add_join_groupchat( accounts[0], GAJIM_SUPPORT_JID)
===================================== gajim/gtk/avatar.py ===================================== @@ -456,8 +456,8 @@ def get_muc_surface(self, # (e.g. avatar cache deleted) app.storage.cache.set_muc(jid, 'avatar', None)
- con = app.connections[account] - name = get_groupchat_name(con, jid) + client = app.get_client(account) + name = get_groupchat_name(client, jid) letter = generate_avatar_letter(name) surface = generate_default_avatar(letter, str(jid), size, scale, style) self._cache[jid][(size, scale, None)] = surface
===================================== gajim/gtk/bookmarks.py ===================================== @@ -40,8 +40,8 @@ def __init__(self, account: str) -> None: self._ui = get_builder('bookmarks.ui') self.add(self._ui.bookmarks_grid)
- con = app.connections[account] - for bookmark in con.get_module('Bookmarks').bookmarks: + client = app.get_client(account) + for bookmark in client.get_module('Bookmarks').bookmarks: self._ui.bookmarks_store.append([str(bookmark.jid), bookmark.name, bookmark.nick,
===================================== gajim/gtk/discovery.py ===================================== @@ -368,8 +368,8 @@ def get_info(self, jid, node, cb, force=False, nofetch=False, args=()): self._cbs[cbkey].append(cb) else: self._cbs[cbkey] = [cb] - con = app.connections[self.account] - con.get_module('Discovery').disco_info( + client = app.get_client(self.account) + client.get_module('Discovery').disco_info( jid, node, callback=self._disco_info_received)
def get_items(self, jid, node, cb, force=False, nofetch=False, args=()): @@ -394,8 +394,8 @@ def get_items(self, jid, node, cb, force=False, nofetch=False, args=()): self._cbs[cbkey].append(cb) else: self._cbs[cbkey] = [cb] - con = app.connections[self.account] - con.get_module('Discovery').disco_items( + client = app.get_client(self.account) + client.get_module('Discovery').disco_items( jid, node, callback=self._disco_items_received)
def _disco_info_received(self, task): @@ -526,10 +526,10 @@ def __init__(self, account, jid='', node=None, address_entry=False,
# Get a ServicesCache object. try: - self.cache = app.connections[account].services_cache + self.cache = app.get_client(account).services_cache except AttributeError: self.cache = ServicesCache(account) - app.connections[account].services_cache = self.cache + app.get_client(account).services_cache = self.cache
if initial_identities: self.cache._on_agent_info(jid, node, initial_identities, [], None) @@ -1871,8 +1871,8 @@ def __init__(self, account, jid, node): self.subscribe_button = None self.unsubscribe_button = None
- con = app.connections[account] - con.get_module('PubSub').send_pb_subscription_query( + client = app.get_client(account) + client.get_module('PubSub').send_pb_subscription_query( jid, self._on_pep_subscriptions)
def _create_treemodel(self): @@ -2062,8 +2062,8 @@ def _on_subscribe_button_clicked(self, widget):
node = model.get_value(iter_, 1) # 1 = groupnode
- con = app.connections[self.account] - con.get_module('PubSub').send_pb_subscribe( + client = app.get_client(self.account) + client.get_module('PubSub').send_pb_subscribe( self.jid, node, self._on_pep_subscribe, groupnode=node)
def _on_unsubscribe_button_clicked(self, widget): @@ -2077,8 +2077,8 @@ def _on_unsubscribe_button_clicked(self, widget):
node = model.get_value(iter_, 1) # 1 = groupnode
- con = app.connections[self.account] - con.get_module('PubSub').send_pb_unsubscribe( + client = app.get_client(self.account) + client.get_module('PubSub').send_pb_unsubscribe( self.jid, node, self._on_pep_unsubscribe, groupnode=node)
def _on_pep_subscriptions(self, _nbxmpp_client, request): @@ -2185,10 +2185,10 @@ def _on_send_button_clicked(self, w): True)])
# Publish it to node - con = app.connections[self.account] - con.get_module('PubSub').publish(self.groupid, - item, - jid=self.servicejid) + client = app.get_client(self.account) + client.get_module('PubSub').publish(self.groupid, + item, + jid=self.servicejid)
# Close the window self.window.destroy()
===================================== gajim/gtk/filetransfer.py ===================================== @@ -601,7 +601,7 @@ def _remove_transfer(self, iter_, sid, file_props): if file_props.tt_account: # file transfer is set account = file_props.tt_account - if account in app.connections: + if account in app.settings.get_active_accounts(): # there is a connection to the account client = app.get_client(account) client.get_module('Bytestream').remove_transfer(file_props) @@ -937,7 +937,7 @@ def _on_cancel_button_clicked(self, widget): def cancel_transfer(self, file_props: FileProp) -> None: # TODO: does not cancel transfer somehow account = file_props.tt_account - if account is None or account not in app.connections: + if account is None or account not in app.settings.get_active_accounts(): return client = app.get_client(account) # Check if we are in a IBB transfer
===================================== gajim/gtk/main_stack.py ===================================== @@ -44,7 +44,7 @@ def __init__(self) -> None: self._chat_page.connect('chat-selected', self._on_chat_selected) self.add_named(self._chat_page, 'chats')
- for account in list(app.connections.keys()): + for account in app.settings.get_active_accounts(): self.add_account_page(account)
def add_account_page(self, account: str) -> None:
===================================== gajim/gtk/mam_preferences.py ===================================== @@ -47,7 +47,7 @@ def __init__(self, account: str) -> None: self.connect_after('key-press-event', self._on_key_press)
self.account = account - self._con = app.connections[account] + self._client = app.get_client(account) self._destroyed = False
self._ui = get_builder('mam_preferences.ui') @@ -63,7 +63,7 @@ def __init__(self, account: str) -> None:
self._activate_spinner()
- self._con.get_module('MAM').request_preferences( + self._client.get_module('MAM').request_preferences( callback=self._mam_prefs_received)
def _on_destroy(self, widget: MamPreferences) -> None: @@ -156,7 +156,7 @@ def _on_save(self, _button: Gtk.Button) -> None: always.append(jid) else: never.append(jid) - self._con.get_module('MAM').set_preferences( + self._client.get_module('MAM').set_preferences( default, always, never, callback=self._mam_prefs_saved)
def _activate_spinner(self) -> None:
===================================== gajim/gtk/settings.py ===================================== @@ -884,10 +884,10 @@ def show_dialog(self, parent: Gtk.Window) -> None:
def update_activatable(self) -> None: activatable = False - if self.account in app.connections: - con = app.connections[self.account] - activatable = (con.state.is_available and - con.get_module('Register').supported) + if self.account in app.settings.get_active_accounts(): + client = app.get_client(self.account) + activatable = (client.state.is_available and + client.get_module('Register').supported) self.set_activatable(activatable)
===================================== gajim/gtk/start_chat.py ===================================== @@ -660,7 +660,7 @@ def _start_search(self) -> None: accounts = app.get_connected_accounts() if not accounts: return - client = app.connections[accounts[0]].connection + client = app.get_client(accounts[0]).connection
text = self._ui.search_entry.get_text().strip() self._global_search_listbox.start_search()
===================================== gajim/gtk/status_message_selector.py ===================================== @@ -90,20 +90,20 @@ def _set_status_message(self, *args: Any) -> None: client = app.get_client(self._account) client.change_status(client.status, message) else: - for account in app.connections: + for client in app.get_clients(): if not app.settings.get_account_setting( - account, 'sync_with_global_status'): + client.account, 'sync_with_global_status'): continue - client = app.get_client(account) client.change_status(client.status, message)
def update(self) -> None: if self._account is None: message = get_global_status_message() else: - if self._account not in app.connections: + try: + client = app.get_client(self._account) + except KeyError: return - client = app.get_client(self._account) message = client.status_message
self._entry.set_text(message)
===================================== gajim/gtk/synchronize_accounts.py ===================================== @@ -93,7 +93,7 @@ def _init_accounts(self) -> None: model = self._ui.accounts_treeview.get_model() assert isinstance(model, Gtk.ListStore) model.clear() - for remote_account in app.connections: + for remote_account in app.settings.get_active_accounts(): if remote_account == self.account: # Do not show the account we're sync'ing continue
===================================== gajim/gui_interface.py ===================================== @@ -114,7 +114,7 @@ def __init__(self):
self.instances: dict[str, Any] = {}
- for acc in app.connections: + for acc in app.settings.get_active_accounts(): self.instances[acc] = { 'infos': {}, 'disco': {} @@ -545,7 +545,7 @@ def enable_account(self, account: str) -> None:
app.ged.raise_event(AccountEnabled(account=account))
- app.connections[account].change_status('online', '') + app.get_client(account).change_status('online', '') window = get_app_window('AccountsWindow') if window is not None: GLib.idle_add(window.enable_account, account, True) @@ -563,7 +563,7 @@ def disable_account(self, account: str) -> None: build_accounts_menu() app.app.update_app_actions_state()
- app.connections[account].cleanup() + app.get_client(account).cleanup() del app.connections[account] del self.instances[account] del app.nicks[account] @@ -592,8 +592,10 @@ def autoconnect(self) -> None: Auto connect at startup '''
- for account, con in app.connections.items(): - if not app.settings.get_account_setting(account, 'autoconnect'): + for client in app.get_clients(): + account = client.account + if not app.settings.get_account_setting(account, + 'autoconnect'): continue
status = 'online' @@ -606,7 +608,7 @@ def autoconnect(self) -> None: account, 'last_status_msg') status_message = helpers.from_one_line(status_message)
- con.change_status(status, status_message) + client.change_status(status, status_message)
def change_status(self, status: str, @@ -620,12 +622,12 @@ def change_status(self, self._change_status(account, status) return
- for acc in app.connections: - if not app.settings.get_account_setting(acc, + for client in app.get_clients(): + if not app.settings.get_account_setting(client.account, 'sync_with_global_status'): continue
- self._change_status(acc, status) + self._change_status(client.account, status)
@staticmethod def _change_status(account: str, status: str) -> None: @@ -684,8 +686,8 @@ def run(self, _application: Gtk.Application) -> None:
app.plugin_repository = PluginRepository()
- for con in app.connections.values(): - con.get_module('Roster').load_roster() + for client in app.get_clients(): + client.get_module('Roster').load_roster()
# get instances for windows/dialogs that will show_all()/hide() self.instances['file_transfers'] = FileTransfersWindow()
===================================== gajim/plugins/pluginmanager.py ===================================== @@ -480,7 +480,7 @@ def _remove_name_from_encryption_plugins(self, def _register_modules_with_handlers(self, plugin: GajimPlugin) -> None: if not hasattr(plugin, 'modules'): return - for client in app.connections.values(): + for client in app.get_clients(): for module in plugin.modules: instance, name = module.get_instance(client) modules.register_single_module(client, instance, name) @@ -491,7 +491,7 @@ def _register_modules_with_handlers(self, plugin: GajimPlugin) -> None: def _unregister_modules_with_handlers(self, plugin: GajimPlugin) -> None: if not hasattr(plugin, 'modules'): return - for client in app.connections.values(): + for client in app.get_clients(): for module in plugin.modules: instance = client.get_module(module.name) modules.unregister_single_module(client, module.name)
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/commit/c8b014a408585a7d8b1e321948ed98e22...