Philipp Hörist pushed to branch master at gajim / gajim

Commits:

4 changed files:

Changes:

  • gajim/common/modules/base.py
    1
    +# This file is part of Gajim.
    
    2
    +#
    
    3
    +# Gajim is free software; you can redistribute it and/or modify
    
    4
    +# it under the terms of the GNU General Public License as published
    
    5
    +# by the Free Software Foundation; version 3 only.
    
    6
    +#
    
    7
    +# Gajim is distributed in the hope that it will be useful,
    
    8
    +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    
    9
    +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    
    10
    +# GNU General Public License for more details.
    
    11
    +#
    
    12
    +# You should have received a copy of the GNU General Public License
    
    13
    +# along with Gajim.  If not, see <http://www.gnu.org/licenses/>.
    
    14
    +
    
    15
    +import logging
    
    16
    +from functools import partial
    
    17
    +from unittest.mock import Mock
    
    18
    +
    
    19
    +from gajim.common import app
    
    20
    +
    
    21
    +log = logging.getLogger('gajim.c.m.base')
    
    22
    +
    
    23
    +
    
    24
    +class BaseModule:
    
    25
    +
    
    26
    +    _nbxmpp_extends = ''
    
    27
    +    _nbxmpp_methods = []
    
    28
    +
    
    29
    +    def __init__(self, con):
    
    30
    +        self._con = con
    
    31
    +        self._account = con.name
    
    32
    +        self._nbxmpp_callbacks = {}
    
    33
    +        self.handlers = []
    
    34
    +
    
    35
    +    def __getattr__(self, key):
    
    36
    +        if key not in self._nbxmpp_methods:
    
    37
    +            raise AttributeError
    
    38
    +        if not app.account_is_connected(self._account):
    
    39
    +            log.warning('Account %s not connected, cant use %s',
    
    40
    +                        self._account, key)
    
    41
    +            return
    
    42
    +
    
    43
    +        module = self._con.connection.get_module(self._nbxmpp_extends)
    
    44
    +
    
    45
    +        return partial(getattr(module, key),
    
    46
    +                       callback=self._nbxmpp_callbacks.get(key))
    
    47
    +
    
    48
    +    def _nbxmpp(self, module_name=None):
    
    49
    +        if not app.account_is_connected(self._account):
    
    50
    +            log.warning('Account %s not connected, cant use nbxmpp method',
    
    51
    +                        self._account)
    
    52
    +            return Mock()
    
    53
    +
    
    54
    +        if module_name is None:
    
    55
    +            return self._con.connection
    
    56
    +        return self._con.connection.get_module(module_name)
    
    57
    +
    
    58
    +    def _register_callback(self, method, callback):
    
    59
    +        self._nbxmpp_callbacks[method] = callback

  • gajim/common/modules/blocking.py
    ... ... @@ -15,30 +15,28 @@
    15 15
     # XEP-0191: Blocking Command
    
    16 16
     
    
    17 17
     import logging
    
    18
    -from functools import wraps
    
    19 18
     
    
    20 19
     import nbxmpp
    
    21 20
     
    
    22 21
     from gajim.common import app
    
    23 22
     from gajim.common.nec import NetworkEvent
    
    24 23
     from gajim.common.nec import NetworkIncomingEvent
    
    24
    +from gajim.common.modules.base import BaseModule
    
    25 25
     
    
    26 26
     log = logging.getLogger('gajim.c.m.blocking')
    
    27 27
     
    
    28 28
     
    
    29
    -def ensure_online(func):
    
    30
    -    @wraps(func)
    
    31
    -    def func_wrapper(self, *args, **kwargs):
    
    32
    -        if not app.account_is_connected(self._account):
    
    33
    -            return
    
    34
    -        return func(self, *args, **kwargs)
    
    35
    -    return func_wrapper
    
    29
    +class Blocking(BaseModule):
    
    36 30
     
    
    31
    +    _nbxmpp_extends = 'Blocking'
    
    32
    +    _nbxmpp_methods = [
    
    33
    +        'block',
    
    34
    +        'unblock',
    
    35
    +        'get_blocking_list',
    
    36
    +    ]
    
    37 37
     
    
    38
    -class Blocking:
    
    39 38
         def __init__(self, con):
    
    40
    -        self._con = con
    
    41
    -        self._account = con.name
    
    39
    +        BaseModule.__init__(self, con)
    
    42 40
     
    
    43 41
             self.blocked = []
    
    44 42
     
    
    ... ... @@ -46,22 +44,10 @@ class Blocking:
    46 44
                 ('iq', self._blocking_push_received, 'set', nbxmpp.NS_BLOCKING)
    
    47 45
             ]
    
    48 46
     
    
    49
    -        self.supported = False
    
    50
    -
    
    51
    -        self._nbmxpp_methods = [
    
    52
    -            'block',
    
    53
    -            'unblock',
    
    54
    -        ]
    
    47
    +        self._register_callback('get_blocking_list',
    
    48
    +                                self._blocking_list_received)
    
    55 49
     
    
    56
    -    def __getattr__(self, key):
    
    57
    -        if key not in self._nbmxpp_methods:
    
    58
    -            raise AttributeError
    
    59
    -        if not app.account_is_connected(self._account):
    
    60
    -            log.warning('Account %s not connected, cant use %s',
    
    61
    -                        self._account, key)
    
    62
    -            return
    
    63
    -        module = self._con.connection.get_module('Blocking')
    
    64
    -        return getattr(module, key)
    
    50
    +        self.supported = False
    
    65 51
     
    
    66 52
         def pass_disco(self, from_, _identities, features, _data, _node):
    
    67 53
             if nbxmpp.NS_BLOCKING not in features:
    
    ... ... @@ -75,15 +61,6 @@ class Blocking:
    75 61
     
    
    76 62
             log.info('Discovered blocking: %s', from_)
    
    77 63
     
    
    78
    -    @ensure_online
    
    79
    -    def get_blocking_list(self, callback=None):
    
    80
    -        log.info('Request list')
    
    81
    -        if callback is None:
    
    82
    -            callback = self._blocking_list_received
    
    83
    -
    
    84
    -        self._con.connection.get_module('Blocking').get_blocking_list(
    
    85
    -            callback=callback)
    
    86
    -
    
    87 64
         def _blocking_list_received(self, result):
    
    88 65
             if result.is_error:
    
    89 66
                 log.info('Error: %s', result.error)
    
    ... ... @@ -98,7 +75,7 @@ class Blocking:
    98 75
             childs = reply.getChildren()
    
    99 76
             for child in childs:
    
    100 77
                 reply.delChild(child)
    
    101
    -        self._con.connection.send(reply)
    
    78
    +        self._nbxmpp().send(reply)
    
    102 79
     
    
    103 80
             changed_list = []
    
    104 81
     
    
    ... ... @@ -149,7 +126,7 @@ class Blocking:
    149 126
             log.info('Presence probe: %s', jid)
    
    150 127
             # Send a presence Probe to get the current Status
    
    151 128
             probe = nbxmpp.Presence(jid, 'probe', frm=self._con.get_own_jid())
    
    152
    -        self._con.connection.send(probe)
    
    129
    +        self._nbxmpp().send(probe)
    
    153 130
     
    
    154 131
     
    
    155 132
     class BlockingEvent(NetworkIncomingEvent):
    

  • gajim/common/modules/muc.py
    ... ... @@ -30,14 +30,32 @@ from gajim.common.helpers import AdditionalDataDict
    30 30
     from gajim.common.caps_cache import muc_caps_cache
    
    31 31
     from gajim.common.nec import NetworkEvent
    
    32 32
     from gajim.common.modules.bits_of_binary import store_bob_data
    
    33
    +from gajim.common.modules.base import BaseModule
    
    34
    +
    
    33 35
     
    
    34 36
     log = logging.getLogger('gajim.c.m.muc')
    
    35 37
     
    
    36 38
     
    
    37
    -class MUC:
    
    39
    +class MUC(BaseModule):
    
    40
    +
    
    41
    +    _nbxmpp_extends = 'MUC'
    
    42
    +    _nbxmpp_methods = [
    
    43
    +        'get_affiliation',
    
    44
    +        'set_role',
    
    45
    +        'set_affiliation',
    
    46
    +        'set_config',
    
    47
    +        'set_subject',
    
    48
    +        'cancel_config',
    
    49
    +        'send_captcha',
    
    50
    +        'decline',
    
    51
    +        'invite',
    
    52
    +        'request_config',
    
    53
    +        'request_voice',
    
    54
    +        'destroy',
    
    55
    +    ]
    
    56
    +
    
    38 57
         def __init__(self, con):
    
    39
    -        self._con = con
    
    40
    -        self._account = con.name
    
    58
    +        BaseModule.__init__(self, con)
    
    41 59
     
    
    42 60
             self.handlers = [
    
    43 61
                 StanzaHandler(name='presence',
    
    ... ... @@ -75,28 +93,7 @@ class MUC:
    75 93
                               priority=49)
    
    76 94
             ]
    
    77 95
     
    
    78
    -        self._nbmxpp_methods = [
    
    79
    -            'get_affiliation',
    
    80
    -            'set_role',
    
    81
    -            'set_affiliation',
    
    82
    -            'set_config',
    
    83
    -            'set_subject',
    
    84
    -            'cancel_config',
    
    85
    -            'send_captcha',
    
    86
    -            'decline',
    
    87
    -            'request_voice',
    
    88
    -            'destroy',
    
    89
    -        ]
    
    90
    -
    
    91
    -    def __getattr__(self, key):
    
    92
    -        if key not in self._nbmxpp_methods:
    
    93
    -            raise AttributeError
    
    94
    -        if not app.account_is_connected(self._account):
    
    95
    -            log.warning('Account %s not connected, cant use %s',
    
    96
    -                        self._account, key)
    
    97
    -            return
    
    98
    -        module = self._con.connection.get_module('MUC')
    
    99
    -        return getattr(module, key)
    
    96
    +        self._register_callback('request_config', self._config_received)
    
    100 97
     
    
    101 98
         def pass_disco(self, from_, identities, features, _data, _node):
    
    102 99
             for identity in identities:
    
    ... ... @@ -423,24 +420,13 @@ class MUC:
    423 420
                 raise nbxmpp.NodeProcessed
    
    424 421
     
    
    425 422
         def invite(self, room, to, reason=None, continue_=False):
    
    426
    -        if not app.account_is_connected(self._account):
    
    427
    -            return
    
    428
    -
    
    429 423
             type_ = InviteType.MEDIATED
    
    430 424
             contact = app.contacts.get_contact_from_full_jid(self._account, to)
    
    431 425
             if contact and contact.supports(nbxmpp.NS_CONFERENCE):
    
    432 426
                 type_ = InviteType.DIRECT
    
    433 427
     
    
    434 428
             password = app.gc_passwords.get(room, None)
    
    435
    -        self._con.connection.get_module('MUC').invite(
    
    436
    -            room, to, reason, password, continue_, type_)
    
    437
    -
    
    438
    -    def request_config(self, room_jid):
    
    439
    -        if not app.account_is_connected(self._account):
    
    440
    -            return
    
    441
    -
    
    442
    -        self._con.connection.get_module('MUC').request_config(
    
    443
    -            room_jid, callback=self._config_received)
    
    429
    +        self._nbxmpp('MUC').invite(room, to, reason, password, continue_, type_)
    
    444 430
     
    
    445 431
         def _config_received(self, result):
    
    446 432
             if result.is_error:
    

  • gajim/gtk/tooltips.py
    ... ... @@ -449,10 +449,9 @@ class RosterTooltip(StatusTable):
    449 449
             scale = self._ui.tooltip_grid.get_scale_factor()
    
    450 450
             surface = app.contacts.get_avatar(
    
    451 451
                 account, self.prim_contact.jid, AvatarSize.TOOLTIP, scale)
    
    452
    -        if surface is None:
    
    453
    -            return
    
    454
    -        self._ui.avatar.set_from_surface(surface)
    
    455
    -        self._ui.avatar.show()
    
    452
    +        if surface is not None:
    
    453
    +            self._ui.avatar.set_from_surface(surface)
    
    454
    +            self._ui.avatar.show()
    
    456 455
     
    
    457 456
             app.plugin_manager.gui_extension_point(
    
    458 457
                 'roster_tooltip_populate', self, contacts, self._ui.tooltip_grid)