2018-09-05 02:59:34 +02:00
|
|
|
# This file is part of Gajim.
|
|
|
|
#
|
|
|
|
# Gajim is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published
|
|
|
|
# by the Free Software Foundation; version 3 only.
|
|
|
|
#
|
|
|
|
# Gajim is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2008-08-18 18:35:14 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
Network Events Controller.
|
|
|
|
|
|
|
|
:author: Mateusz Biliński <mateusz@bilinski.it>
|
|
|
|
:since: 10th August 2008
|
|
|
|
:copyright: Copyright (2008) Mateusz Biliński <mateusz@bilinski.it>
|
2011-05-18 19:44:43 +02:00
|
|
|
:copyright: Copyright (2011) Yann Leboulanger <asterix@lagaule.org>
|
2008-08-18 18:35:14 +02:00
|
|
|
:license: GPL
|
|
|
|
'''
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
from typing import List # pylint: disable=unused-import
|
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
from gajim.common import app
|
2008-08-18 18:35:14 +02:00
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
class NetworkEventsController:
|
2010-04-08 01:20:17 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.incoming_events_generators = {}
|
|
|
|
'''
|
|
|
|
Keys: names of events
|
|
|
|
Values: list of class objects that are subclasses
|
|
|
|
of `NetworkIncomingEvent`
|
|
|
|
'''
|
2011-05-18 19:44:43 +02:00
|
|
|
self.outgoing_events_generators = {}
|
|
|
|
'''
|
|
|
|
Keys: names of events
|
|
|
|
Values: list of class objects that are subclasses
|
|
|
|
of `NetworkOutgoingEvent`
|
|
|
|
'''
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
def register_incoming_event(self, event_class):
|
|
|
|
for base_event_name in event_class.base_network_events:
|
2011-05-18 19:44:43 +02:00
|
|
|
event_list = self.incoming_events_generators.setdefault(
|
|
|
|
base_event_name, [])
|
2018-09-13 23:56:12 +02:00
|
|
|
if event_class not in event_list:
|
2010-07-20 10:35:38 +02:00
|
|
|
event_list.append(event_class)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
def unregister_incoming_event(self, event_class):
|
|
|
|
for base_event_name in event_class.base_network_events:
|
|
|
|
if base_event_name in self.incoming_events_generators:
|
2011-05-18 19:44:43 +02:00
|
|
|
self.incoming_events_generators[base_event_name].remove(
|
|
|
|
event_class)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
def register_outgoing_event(self, event_class):
|
2011-05-18 19:44:43 +02:00
|
|
|
for base_event_name in event_class.base_network_events:
|
|
|
|
event_list = self.outgoing_events_generators.setdefault(
|
|
|
|
base_event_name, [])
|
2018-09-13 23:56:12 +02:00
|
|
|
if event_class not in event_list:
|
2011-05-18 19:44:43 +02:00
|
|
|
event_list.append(event_class)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
def unregister_outgoing_event(self, event_class):
|
2011-05-18 19:44:43 +02:00
|
|
|
for base_event_name in event_class.base_network_events:
|
|
|
|
if base_event_name in self.outgoing_events_generators:
|
|
|
|
self.outgoing_events_generators[base_event_name].remove(
|
|
|
|
event_class)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
def push_incoming_event(self, event_object):
|
2010-07-20 10:35:38 +02:00
|
|
|
if event_object.generate():
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.ged.raise_event(event_object.name, event_object):
|
2011-05-01 22:09:50 +02:00
|
|
|
self._generate_events_based_on_incoming_event(event_object)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
def push_outgoing_event(self, event_object):
|
2011-05-18 19:44:43 +02:00
|
|
|
if event_object.generate():
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.ged.raise_event(event_object.name, event_object):
|
2011-05-18 19:44:43 +02:00
|
|
|
self._generate_events_based_on_outgoing_event(event_object)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
|
|
|
def _generate_events_based_on_incoming_event(self, event_object):
|
|
|
|
'''
|
|
|
|
:return: True if even_object should be dispatched through Global
|
|
|
|
Events Dispatcher, False otherwise. This can be used to replace
|
|
|
|
base events with those that more data computed (easier to use
|
|
|
|
by handlers).
|
|
|
|
:note: replacing mechanism is not implemented currently, but will be
|
|
|
|
based on attribute in new network events object.
|
|
|
|
'''
|
|
|
|
base_event_name = event_object.name
|
|
|
|
if base_event_name in self.incoming_events_generators:
|
2011-05-18 19:44:43 +02:00
|
|
|
for new_event_class in self.incoming_events_generators[
|
2018-09-13 23:56:12 +02:00
|
|
|
base_event_name]:
|
|
|
|
new_event_object = new_event_class(
|
|
|
|
None, base_event=event_object)
|
2010-04-08 01:20:17 +02:00
|
|
|
if new_event_object.generate():
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.ged.raise_event(new_event_object.name,
|
2018-09-13 23:56:12 +02:00
|
|
|
new_event_object):
|
2011-05-18 19:44:43 +02:00
|
|
|
self._generate_events_based_on_incoming_event(
|
|
|
|
new_event_object)
|
|
|
|
|
|
|
|
def _generate_events_based_on_outgoing_event(self, event_object):
|
|
|
|
'''
|
|
|
|
:return: True if even_object should be dispatched through Global
|
|
|
|
Events Dispatcher, False otherwise. This can be used to replace
|
|
|
|
base events with those that more data computed (easier to use
|
|
|
|
by handlers).
|
|
|
|
:note: replacing mechanism is not implemented currently, but will be
|
|
|
|
based on attribute in new network events object.
|
|
|
|
'''
|
|
|
|
base_event_name = event_object.name
|
|
|
|
if base_event_name in self.outgoing_events_generators:
|
|
|
|
for new_event_class in self.outgoing_events_generators[
|
2018-09-13 23:56:12 +02:00
|
|
|
base_event_name]:
|
|
|
|
new_event_object = new_event_class(
|
|
|
|
None, base_event=event_object)
|
2011-05-18 19:44:43 +02:00
|
|
|
if new_event_object.generate():
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.ged.raise_event(new_event_object.name,
|
2018-09-13 23:56:12 +02:00
|
|
|
new_event_object):
|
2011-05-18 19:44:43 +02:00
|
|
|
self._generate_events_based_on_outgoing_event(
|
|
|
|
new_event_object)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
|
|
|
|
class NetworkEvent:
|
2010-04-08 01:20:17 +02:00
|
|
|
name = ''
|
|
|
|
|
|
|
|
def __init__(self, new_name, **kwargs):
|
|
|
|
if new_name:
|
|
|
|
self.name = new_name
|
|
|
|
|
|
|
|
self.init()
|
|
|
|
|
2011-05-18 19:44:43 +02:00
|
|
|
self._set_kwargs_as_attributes(**kwargs)
|
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
def init(self):
|
|
|
|
pass
|
|
|
|
|
2010-07-20 10:35:38 +02:00
|
|
|
def generate(self):
|
|
|
|
'''
|
|
|
|
Generates new event (sets it's attributes) based on event object.
|
|
|
|
|
|
|
|
Base event object name is one of those in `base_network_events`.
|
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
Reference to base event object is stored in `self.base_event`
|
|
|
|
attribute.
|
2010-07-20 10:35:38 +02:00
|
|
|
|
|
|
|
Note that this is a reference, so modifications to that event object
|
|
|
|
are possible before dispatching to Global Events Dispatcher.
|
|
|
|
|
|
|
|
:return: True if generated event should be dispatched, False otherwise.
|
|
|
|
'''
|
|
|
|
return True
|
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
def _set_kwargs_as_attributes(self, **kwargs):
|
2013-01-02 13:54:02 +01:00
|
|
|
for k, v in kwargs.items():
|
2017-07-24 23:24:39 +02:00
|
|
|
if k not in ('name', 'base_network_events'):
|
|
|
|
setattr(self, k, v)
|
2010-04-08 01:20:17 +02:00
|
|
|
|
2017-07-24 23:24:39 +02:00
|
|
|
def _set_base_event_vars_as_attributes(self, event):
|
|
|
|
for k, v in vars(event).items():
|
|
|
|
if k not in ('name', 'base_network_events'):
|
|
|
|
setattr(self, k, v)
|
2010-08-27 22:45:16 +02:00
|
|
|
|
2018-09-13 23:56:12 +02:00
|
|
|
|
2008-08-18 18:35:14 +02:00
|
|
|
class NetworkIncomingEvent(NetworkEvent):
|
2018-09-13 23:56:12 +02:00
|
|
|
base_network_events = [] # type: List[str]
|
2010-04-08 01:20:17 +02:00
|
|
|
'''
|
|
|
|
Names of base network events that new event is going to be generated on.
|
|
|
|
'''
|
|
|
|
|
2008-08-18 18:35:14 +02:00
|
|
|
|
|
|
|
class NetworkOutgoingEvent(NetworkEvent):
|
2018-09-13 23:56:12 +02:00
|
|
|
base_network_events = [] # type: List[str]
|
2011-05-18 19:44:43 +02:00
|
|
|
'''
|
|
|
|
Names of base network events that new event is going to be generated on.
|
2012-04-28 22:45:17 +02:00
|
|
|
'''
|