Philipp Hörist pushed to branch master at gajim / gajim
Commits: 35eaea69 by Philipp Hörist at 2022-10-28T18:15:50+02:00 refactor: Move event handler out of gui_interface
- - - - -
4 changed files:
- gajim/common/application.py - gajim/gtk/application.py - gajim/gtk/main.py - gajim/gui_interface.py
Changes:
===================================== gajim/common/application.py ===================================== @@ -41,11 +41,12 @@ from gajim.common import passwords from gajim.common.commands import ChatCommands from gajim.common.dbus import logind -from gajim.common.events import AccountDisconnected from gajim.common.events import AccountDisabled +from gajim.common.events import AccountDisconnected from gajim.common.events import AccountEnabled from gajim.common.events import AllowGajimUpdateCheck from gajim.common.events import GajimUpdateAvailable +from gajim.common.events import SignedIn from gajim.common.client import Client from gajim.common.helpers import make_http_request from gajim.common.helpers import get_random_string @@ -59,8 +60,9 @@ from gajim.common.storage.archive import MessageArchiveStorage
-class CoreApplication: +class CoreApplication(ged.EventHelper): def __init__(self) -> None: + ged.EventHelper.__init__(self) self._profiling_session = None
def _init_core(self) -> None: @@ -111,6 +113,10 @@ def _init_core(self) -> None: for account in app.settings.get_active_accounts(): app.connections[account] = Client(account)
+ self.register_events([ + ('signed-in', ged.CORE, self._on_signed_in), + ]) + @property def _log(self) -> logging.Logger: return app.log('gajim.application') @@ -363,3 +369,10 @@ def remove_account(self, account: str) -> None: passwords.delete_password(account) app.settings.remove_account(account) app.app.remove_account_actions(account) + + def _on_signed_in(self, event: SignedIn) -> None: + client = app.get_client(event.account) + app.storage.archive.insert_jid(client.get_own_jid().bare) + + if client.get_module('MAM').available: + client.get_module('MAM').request_archive_on_signin()
===================================== gajim/gtk/application.py ===================================== @@ -267,9 +267,9 @@ def _startup(self) -> None: menus.build_accounts_menu() self.update_app_actions_state()
- app.ged.register_event_handler('feature-discovered', - ged.CORE, - self._on_feature_discovered) + self.register_event('feature-discovered', + ged.CORE, + self._on_feature_discovered)
from gajim.gui.main import MainWindow MainWindow()
===================================== gajim/gtk/main.py ===================================== @@ -37,6 +37,7 @@ from gajim.common.const import SimpleClientState from gajim.common.ged import EventHelper from gajim.common.i18n import _ +from gajim.common.helpers import play_sound from gajim.common.modules.bytestream import is_transfer_active from gajim.plugins.pluginmanager import PluginManifest from gajim.plugins.repository import PluginRepository @@ -129,6 +130,8 @@ def __init__(self) -> None: ('password-required', ged.GUI1, self._on_password_required), ('http-auth', ged.GUI1, self._on_http_auth), ('muc-added', ged.GUI1, self._on_muc_added), + ('message-sent', ged.GUI1, self._on_message_sent), + ('signed-in', ged.GUI1, self._on_signed_in), ])
app.plugin_repository.connect('plugin-updates-available', @@ -319,6 +322,21 @@ def _on_muc_added(self, event: events.MucAdded) -> None:
self.add_group_chat(event.account, event.jid)
+ def _on_message_sent(self, event: events.MessageSent) -> None: + if not event.play_sound: + return + + enabled = app.settings.get_soundevent_settings( + 'message_sent')['enabled'] + if enabled: + if isinstance(event.jid, list) and len(event.jid) > 1: + return + play_sound('message_sent', event.account) + + def _on_signed_in(self, event: events.SignedIn) -> None: + if app.settings.get('ask_online_status'): + self.show_account_page(event.account) + def _add_actions(self) -> None: for action, variant_type, enabled in MAIN_WIN_ACTIONS: if variant_type is not None:
===================================== gajim/gui_interface.py ===================================== @@ -52,7 +52,6 @@ from nbxmpp import JID
from gajim.common import app -from gajim.common import ged from gajim.common import exceptions from gajim.common import proxy65_manager from gajim.common import socks5 @@ -87,8 +86,6 @@ def __init__(self): app.interface = self app.thread_interface = ThreadInterface
- self.handlers = {} - app.idlequeue = idlequeue.get_idlequeue() # resolve and keep current record of resolved hosts app.socks5queue = socks5.SocksQueue( @@ -101,7 +98,6 @@ def __init__(self):
app.proxy65_manager = proxy65_manager.Proxy65Manager(app.idlequeue)
- self._create_core_handlers_list() self._register_core_handlers()
self.instances: dict[str, Any] = {} @@ -111,61 +107,6 @@ def __init__(self): app.to_be_removed[acc] = [] app.nicks[acc] = app.settings.get_account_setting(acc, 'name')
- def _create_core_handlers_list(self) -> None: - # pylint: disable=line-too-long - self.handlers = { - 'signed-in': [self.handle_event_signed_in], - 'message-sent': [self.handle_event_msgsent], - } - # pylint: enable=line-too-long - - def _register_core_handlers(self) -> None: - ''' - Register core handlers in Global Events Dispatcher (GED). - - This is part of rewriting whole events handling system to use GED. - ''' - for event_name, event_handlers in self.handlers.items(): - for event_handler in event_handlers: - prio = ged.GUI1 - if isinstance(event_handler, tuple): - prio = event_handler[1] - event_handler = event_handler[0] - app.ged.register_event_handler( - event_name, - prio, - event_handler) - - @staticmethod - def handle_event_signed_in(event): - ''' - SIGNED_IN event is emitted when we sign in, so handle it - ''' - # ('SIGNED_IN', account, ()) - # block signed in notifications for 30 seconds - - # Add our own JID into the DB - app.storage.archive.insert_jid(event.conn.get_own_jid().bare) - account = event.conn.name - - if event.conn.get_module('MAM').available: - event.conn.get_module('MAM').request_archive_on_signin() - - if app.settings.get('ask_online_status'): - app.window.show_account_page(account) - - @staticmethod - def handle_event_msgsent(event): - if not event.play_sound: - return - - enabled = app.settings.get_soundevent_settings( - 'message_sent')['enabled'] - if enabled: - if isinstance(event.jid, list) and len(event.jid) > 1: - return - helpers.play_sound('message_sent', event.account) - # Jingle File Transfer @staticmethod def handle_event_file_error(title: str, message: str) -> None:
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/commit/35eaea69c1f065c2e5178ff9781a55a7c...