From f25634bff577c9d22f589f1a844ef8df2b05eb81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Fri, 24 Aug 2018 21:39:03 +0200 Subject: [PATCH] Register modules defined by plugins --- gajim/accounts_window.py | 3 ++ gajim/common/connection_handlers.py | 5 +++ gajim/common/modules/__init__.py | 17 +++++++++ gajim/gtk/account_wizard.py | 1 + gajim/plugins/pluginmanager.py | 53 +++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+) diff --git a/gajim/accounts_window.py b/gajim/accounts_window.py index 81c475c30..02f133929 100644 --- a/gajim/accounts_window.py +++ b/gajim/accounts_window.py @@ -236,6 +236,9 @@ class AccountsWindow(Gtk.ApplicationWindow): else: app.connections[account] = Connection(account) + app.plugin_manager.register_modules_for_account( + app.connections[account]) + # update variables app.interface.instances[account] = { 'infos': {}, 'disco': {}, 'gc_config': {}, 'search': {}, diff --git a/gajim/common/connection_handlers.py b/gajim/common/connection_handlers.py index 58b1b231d..5346a717a 100644 --- a/gajim/common/connection_handlers.py +++ b/gajim/common/connection_handlers.py @@ -408,6 +408,9 @@ class ConnectionHandlers(ConnectionSocks5Bytestream, self.continue_connect_info = None + # If handlers have been registered + self.handlers_registered = False + app.nec.register_incoming_event(StreamConflictReceivedEvent) app.nec.register_incoming_event(NotificationEvent) @@ -655,9 +658,11 @@ class ConnectionHandlers(ConnectionSocks5Bytestream, for handler in modules.get_handlers(self): con.RegisterHandler(*handler) + self.handlers_registered = True def _unregister_handlers(self): if not self.connection: return for handler in modules.get_handlers(self): self.connection.UnregisterHandler(*handler) + self.handlers_registered = False diff --git a/gajim/common/modules/__init__.py b/gajim/common/modules/__init__.py index 3aef57ac5..09de54712 100644 --- a/gajim/common/modules/__init__.py +++ b/gajim/common/modules/__init__.py @@ -80,10 +80,27 @@ def register(con, *args, **kwargs): _modules[con.name][name] = instance +def register_single(con, instance, name): + if con.name not in _modules: + raise ValueError('Unknown account name: %s' % con.name) + _modules[con.name][name] = instance + + def unregister(con): + for instance in _modules[con.name].values(): + if hasattr(instance, 'cleanup'): + instance.cleanup() del _modules[con.name] +def unregister_single(con, name): + if con.name not in _modules: + return + if name not in _modules[con.name]: + return + del _modules[con.name][name] + + def get(account, name): try: return _modules[account][name] diff --git a/gajim/gtk/account_wizard.py b/gajim/gtk/account_wizard.py index 5dfc71b55..86a46f4ac 100644 --- a/gajim/gtk/account_wizard.py +++ b/gajim/gtk/account_wizard.py @@ -288,6 +288,7 @@ class AccountCreationWizard: 100, self.update_progressbar) # Get form from serveur con = connection.Connection(self.account) + app.plugin_manager.register_modules_for_account(con) app.connections[self.account] = con con.new_account(self.account, config) elif cur_page == 3: diff --git a/gajim/plugins/pluginmanager.py b/gajim/plugins/pluginmanager.py index 2dbab0bf9..3084aec89 100644 --- a/gajim/plugins/pluginmanager.py +++ b/gajim/plugins/pluginmanager.py @@ -38,6 +38,7 @@ import gajim from gajim.common import app from gajim.common import nec from gajim.common import configpaths +from gajim.common import modules from gajim.common.exceptions import PluginsystemError from gajim.plugins.helpers import log, log_calls, Singleton @@ -352,6 +353,38 @@ class PluginManager(metaclass=Singleton): if plugin.encryption_name: del self.encryption_plugins[plugin.encryption_name] + def _register_modules_with_handlers(self, plugin): + if not hasattr(plugin, 'modules'): + return + for con in app.connections.values(): + for module in plugin.modules: + instance, name = module.get_instance(con) + if not module.zeroconf and con.name == 'Local': + continue + modules.register_single(con, instance, name) + + # If handlers have been registered, register the + # plugin handlers. Otherwise this will be done + # automatically on connecting + # in connection_handlers._register_handlers() + if con.handlers_registered: + for handler in instance.handlers: + con.connection.RegisterHandler(*handler) + + def _unregister_modules_with_handlers(self, plugin): + if not hasattr(plugin, 'modules'): + return + for con in app.connections.values(): + for module in plugin.modules: + instance = con.get_module(module.name) + modules.unregister_single(con, module.name) + + # Account is still connected and handlers are registered + # So just unregister the plugin handlers + if con.handlers_registered: + for handler in instance.handlers: + con.connection.UnregisterHandler(*handler) + @log_calls('PluginManager') def activate_plugin(self, plugin): ''' @@ -365,6 +398,7 @@ class PluginManager(metaclass=Singleton): self._handle_all_gui_extension_points_with_plugin(plugin) self._register_events_handlers_in_ged(plugin) self._register_network_events_in_nec(plugin) + self._register_modules_with_handlers(plugin) self.active_plugins.append(plugin) try: @@ -402,6 +436,7 @@ class PluginManager(metaclass=Singleton): self._remove_events_handler_from_ged(plugin) self._remove_network_events_from_nec(plugin) self._remove_name_from_encryption_plugins(plugin) + self._unregister_modules_with_handlers(plugin) # removing plug-in from active plug-ins list plugin.deactivate() @@ -462,6 +497,24 @@ class PluginManager(metaclass=Singleton): except GajimPluginActivateException: pass + def register_modules_for_account(self, con): + ''' + A new account has been added, register modules + of all active plugins + ''' + for plugin in self.plugins: + if not plugin.active: + return + + if not hasattr(plugin, 'modules'): + return + + for module in plugin.modules: + instance, name = module.get_instance(con) + if not module.zeroconf and con.name == 'Local': + continue + modules.register_single(con, instance, name) + def _plugin_is_active_in_global_config(self, plugin): return app.config.get_per('plugins', plugin.short_name, 'active')