Register modules defined by plugins

This commit is contained in:
Philipp Hörist 2018-08-24 21:39:03 +02:00 committed by Philipp Hörist
parent e1d01e0999
commit f25634bff5
5 changed files with 79 additions and 0 deletions

View File

@ -236,6 +236,9 @@ class AccountsWindow(Gtk.ApplicationWindow):
else: else:
app.connections[account] = Connection(account) app.connections[account] = Connection(account)
app.plugin_manager.register_modules_for_account(
app.connections[account])
# update variables # update variables
app.interface.instances[account] = { app.interface.instances[account] = {
'infos': {}, 'disco': {}, 'gc_config': {}, 'search': {}, 'infos': {}, 'disco': {}, 'gc_config': {}, 'search': {},

View File

@ -408,6 +408,9 @@ class ConnectionHandlers(ConnectionSocks5Bytestream,
self.continue_connect_info = None 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(StreamConflictReceivedEvent)
app.nec.register_incoming_event(NotificationEvent) app.nec.register_incoming_event(NotificationEvent)
@ -655,9 +658,11 @@ class ConnectionHandlers(ConnectionSocks5Bytestream,
for handler in modules.get_handlers(self): for handler in modules.get_handlers(self):
con.RegisterHandler(*handler) con.RegisterHandler(*handler)
self.handlers_registered = True
def _unregister_handlers(self): def _unregister_handlers(self):
if not self.connection: if not self.connection:
return return
for handler in modules.get_handlers(self): for handler in modules.get_handlers(self):
self.connection.UnregisterHandler(*handler) self.connection.UnregisterHandler(*handler)
self.handlers_registered = False

View File

@ -80,10 +80,27 @@ def register(con, *args, **kwargs):
_modules[con.name][name] = instance _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): def unregister(con):
for instance in _modules[con.name].values():
if hasattr(instance, 'cleanup'):
instance.cleanup()
del _modules[con.name] 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): def get(account, name):
try: try:
return _modules[account][name] return _modules[account][name]

View File

@ -288,6 +288,7 @@ class AccountCreationWizard:
100, self.update_progressbar) 100, self.update_progressbar)
# Get form from serveur # Get form from serveur
con = connection.Connection(self.account) con = connection.Connection(self.account)
app.plugin_manager.register_modules_for_account(con)
app.connections[self.account] = con app.connections[self.account] = con
con.new_account(self.account, config) con.new_account(self.account, config)
elif cur_page == 3: elif cur_page == 3:

View File

@ -38,6 +38,7 @@ import gajim
from gajim.common import app from gajim.common import app
from gajim.common import nec from gajim.common import nec
from gajim.common import configpaths from gajim.common import configpaths
from gajim.common import modules
from gajim.common.exceptions import PluginsystemError from gajim.common.exceptions import PluginsystemError
from gajim.plugins.helpers import log, log_calls, Singleton from gajim.plugins.helpers import log, log_calls, Singleton
@ -352,6 +353,38 @@ class PluginManager(metaclass=Singleton):
if plugin.encryption_name: if plugin.encryption_name:
del self.encryption_plugins[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') @log_calls('PluginManager')
def activate_plugin(self, plugin): def activate_plugin(self, plugin):
''' '''
@ -365,6 +398,7 @@ class PluginManager(metaclass=Singleton):
self._handle_all_gui_extension_points_with_plugin(plugin) self._handle_all_gui_extension_points_with_plugin(plugin)
self._register_events_handlers_in_ged(plugin) self._register_events_handlers_in_ged(plugin)
self._register_network_events_in_nec(plugin) self._register_network_events_in_nec(plugin)
self._register_modules_with_handlers(plugin)
self.active_plugins.append(plugin) self.active_plugins.append(plugin)
try: try:
@ -402,6 +436,7 @@ class PluginManager(metaclass=Singleton):
self._remove_events_handler_from_ged(plugin) self._remove_events_handler_from_ged(plugin)
self._remove_network_events_from_nec(plugin) self._remove_network_events_from_nec(plugin)
self._remove_name_from_encryption_plugins(plugin) self._remove_name_from_encryption_plugins(plugin)
self._unregister_modules_with_handlers(plugin)
# removing plug-in from active plug-ins list # removing plug-in from active plug-ins list
plugin.deactivate() plugin.deactivate()
@ -462,6 +497,24 @@ class PluginManager(metaclass=Singleton):
except GajimPluginActivateException: except GajimPluginActivateException:
pass 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): def _plugin_is_active_in_global_config(self, plugin):
return app.config.get_per('plugins', plugin.short_name, 'active') return app.config.get_per('plugins', plugin.short_name, 'active')