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

Commits:

5 changed files:

Changes:

  • gajim/common/modules/blocking.py
    ... ... @@ -38,7 +38,7 @@ class Blocking(BaseModule):
    38 38
         def __init__(self, con):
    
    39 39
             BaseModule.__init__(self, con)
    
    40 40
     
    
    41
    -        self.blocked = []
    
    41
    +        self.blocked = set()
    
    42 42
     
    
    43 43
             self.handlers = [
    
    44 44
                 StanzaHandler(name='iq',
    
    ... ... @@ -61,6 +61,9 @@ def pass_disco(self, info):
    61 61
     
    
    62 62
             self._log.info('Discovered blocking: %s', info.jid)
    
    63 63
     
    
    64
    +    def is_blocked(self, jid):
    
    65
    +        return jid in self.blocked
    
    66
    +
    
    64 67
         @as_task
    
    65 68
         def get_blocking_list(self):
    
    66 69
             _task = yield
    
    ... ... @@ -70,9 +73,9 @@ def get_blocking_list(self):
    70 73
             raise_if_error(blocking_list)
    
    71 74
     
    
    72 75
             self.blocked = blocking_list
    
    73
    -        app.nec.push_incoming_event(NetworkEvent('blocking',
    
    74
    -                                                 conn=self._con,
    
    75
    -                                                 changed=self.blocked))
    
    76
    +        for contact in self._get_contacts_from_jids(blocking_list):
    
    77
    +            contact.set_blocked()
    
    78
    +
    
    76 79
             yield blocking_list
    
    77 80
     
    
    78 81
         @as_task
    
    ... ... @@ -93,42 +96,50 @@ def _blocking_push_received(self, _con, _stanza, properties):
    93 96
             if not properties.is_blocking:
    
    94 97
                 return
    
    95 98
     
    
    96
    -        changed_list = []
    
    97
    -
    
    98 99
             if properties.blocking.unblock_all:
    
    99
    -            self.blocked = []
    
    100
    -            for jid in self.blocked:
    
    101
    -                self._presence_probe(jid)
    
    102
    -            self._log.info('Unblock all Push')
    
    103
    -
    
    104
    -        for jid in properties.blocking.unblock:
    
    105
    -            changed_list.append(jid)
    
    106
    -            if jid not in self.blocked:
    
    107
    -                continue
    
    108
    -            self.blocked.remove(jid)
    
    109
    -            self._presence_probe(jid)
    
    110
    -            self._log.info('Unblock Push: %s', jid)
    
    100
    +            unblock = set(self.blocked)
    
    101
    +            self.blocked = set()
    
    102
    +            self._unblock_jids(unblock)
    
    103
    +            raise nbxmpp.NodeProcessed
    
    104
    +
    
    105
    +        if properties.blocking.unblock:
    
    106
    +            self.blocked -= properties.blocking.unblock
    
    107
    +            self._unblock_jids(properties.blocking.unblock)
    
    108
    +
    
    109
    +        if properties.blocking.block:
    
    110
    +            block = properties.blocking.block
    
    111
    +            self.blocked.update(block)
    
    112
    +            for contact in self._get_contacts_from_jids(block):
    
    113
    +                self._log.info('Block Push: %s', contact.jid)
    
    114
    +                contact.set_blocked()
    
    115
    +
    
    116
    +        raise nbxmpp.NodeProcessed
    
    117
    +
    
    118
    +    def _unblock_jids(self, jids):
    
    119
    +        for contact in self._get_contacts_from_jids(jids):
    
    120
    +            contact.set_unblocked()
    
    121
    +            self._presence_probe(contact.jid)
    
    122
    +            self._log.info('Unblock Push: %s', contact.jid)
    
    111 123
     
    
    112
    -        for jid in properties.blocking.block:
    
    113
    -            if jid in self.blocked:
    
    124
    +    def _get_contacts_from_jids(self, jids):
    
    125
    +        for jid in jids:
    
    126
    +            if jid.resource is not None:
    
    127
    +                # Currently not supported by GUI
    
    114 128
                     continue
    
    115
    -            changed_list.append(jid)
    
    116
    -            self.blocked.append(jid)
    
    117
    -            self._set_contact_offline(str(jid))
    
    118
    -            self._log.info('Block Push: %s', jid)
    
    119 129
     
    
    120
    -        app.nec.push_incoming_event(NetworkEvent('blocking',
    
    121
    -                                                 conn=self._con,
    
    122
    -                                                 changed=changed_list))
    
    130
    +            if jid.is_domain:
    
    131
    +                module = self._con.get_module('Contacts')
    
    132
    +                contacts = module.get_contacts_with_domain(jid.domain)
    
    133
    +                for contact in contacts:
    
    134
    +                    if contact.is_groupchat:
    
    135
    +                        # Currently not supported by GUI
    
    136
    +                        continue
    
    123 137
     
    
    124
    -        raise nbxmpp.NodeProcessed
    
    138
    +                    yield contact
    
    139
    +
    
    140
    +                continue
    
    125 141
     
    
    126
    -    def _set_contact_offline(self, jid: str) -> None:
    
    127
    -        # TODO
    
    128
    -        return
    
    129
    -        # contact_list = app.contacts.get_contacts(self._account, jid)
    
    130
    -        # for contact in contact_list:
    
    131
    -        #     contact.show = 'offline'
    
    142
    +            yield self._get_contact(jid)
    
    132 143
     
    
    133 144
         def _presence_probe(self, jid: JID) -> None:
    
    134 145
             self._log.info('Presence probe: %s', jid)
    

  • gajim/common/modules/contacts.py
    ... ... @@ -83,6 +83,13 @@ def get_contact(self, jid, groupchat=False):
    83 83
         def get_group_chat_contact(self, jid):
    
    84 84
             return self.get_contact(jid, groupchat=True)
    
    85 85
     
    
    86
    +    def get_contacts_with_domain(self, domain):
    
    87
    +        contacts = []
    
    88
    +        for contact in self._contacts.values():
    
    89
    +            if contact.jid.domain == domain:
    
    90
    +                contacts.append(contact)
    
    91
    +        return contacts
    
    92
    +
    
    86 93
     
    
    87 94
     class CommonContact(Observable):
    
    88 95
         def __init__(self, logger, jid, account):
    
    ... ... @@ -234,6 +241,16 @@ def subscription(self):
    234 241
         def groups(self):
    
    235 242
             return self._get_roster_attr('groups')
    
    236 243
     
    
    244
    +    @property
    
    245
    +    def is_blocked(self):
    
    246
    +        return self._module('Blocking').is_blocked(self._jid)
    
    247
    +
    
    248
    +    def set_blocked(self):
    
    249
    +        self.update_presence(UNKNOWN_PRESENCE)
    
    250
    +        self.notify('blocking-update')
    
    251
    +
    
    252
    +    def set_unblocked(self):
    
    253
    +        self.notify('blocking-update')
    
    237 254
     
    
    238 255
     
    
    239 256
     class ResourceContact(CommonContact):
    

  • gajim/gtk/blocking.py
    ... ... @@ -75,8 +75,8 @@ def _on_blocking_list_received(self, task):
    75 75
     
    
    76 76
             self._prev_blocked_jids = set(blocking_list)
    
    77 77
             self._ui.blocking_store.clear()
    
    78
    -        for item in blocking_list:
    
    79
    -            self._ui.blocking_store.append((item,))
    
    78
    +        for jid in blocking_list:
    
    79
    +            self._ui.blocking_store.append((str(jid),))
    
    80 80
     
    
    81 81
             self._set_grid_state(True)
    
    82 82
             self._disable_spinner()
    

  • gajim/gtk/main.py
    ... ... @@ -485,26 +485,15 @@ def execute_command(account, jid):
    485 485
         def block_contact(self, account, jid):
    
    486 486
             client = app.get_client(account)
    
    487 487
     
    
    488
    -        if jid_is_blocked(account, jid):
    
    488
    +        contact = client.get_module('Contacts').get_contact(jid)
    
    489
    +        if contact.is_blocked:
    
    489 490
                 client.get_module('Blocking').unblock([jid])
    
    490
    -            roster = self._account_pages[account].get_roster()
    
    491
    -            roster.draw_contact(jid)
    
    492 491
                 return
    
    493 492
     
    
    494 493
             # TODO: Keep "confirm_block" setting?
    
    495 494
             def _block_contact(report=None):
    
    496
    -            app.events.remove_events(account, jid)
    
    497
    -
    
    498
    -            contact = client.get_module('Contacts').get_contact(jid)
    
    499
    -            client.get_module('Blocking').block([jid], report)
    
    500
    -            if not contact.is_in_roster:
    
    501
    -                self.remove_chat(account, jid)
    
    502
    -                return
    
    503
    -
    
    504
    -            roster = self._account_pages[account].get_roster()
    
    505
    -            roster.draw_contact(jid)
    
    506
    -            # TODO: draw_contact draws too early (not blocked yet)
    
    507
    -            # TODO: Draw ChatList row differently?
    
    495
    +            client.get_module('Blocking').block([contact.jid], report)
    
    496
    +            self.remove_chat(account, contact.jid)
    
    508 497
     
    
    509 498
             ConfirmationDialog(
    
    510 499
                 _('Block Contact'),
    

  • gajim/gtk/roster.py
    ... ... @@ -330,18 +330,19 @@ def enable_sort(self, enable):
    330 330
     
    
    331 331
         def _initial_draw(self):
    
    332 332
             for contact in self._client.get_module('Roster').iter_contacts():
    
    333
    -            contact.connect('presence-update', self._on_presence_update)
    
    334
    -            contact.connect('avatar-update', self._on_avatar_update)
    
    333
    +            self._connect_contact_signals(contact)
    
    335 334
                 self._add_or_update_contact(contact)
    
    336 335
     
    
    337 336
             self.enable_sort(True)
    
    338 337
             self.set_model()
    
    339 338
             self._roster.expand_all()
    
    340 339
     
    
    341
    -    def _on_presence_update(self, contact, _signal_name):
    
    342
    -        self._draw_contact(contact)
    
    340
    +    def _connect_contact_signals(self, contact):
    
    341
    +        contact.connect('presence-update', self._on_contact_update)
    
    342
    +        contact.connect('avatar-update', self._on_contact_update)
    
    343
    +        contact.connect('blocking-update', self._on_contact_update)
    
    343 344
     
    
    344
    -    def _on_avatar_update(self, contact, _signal_name):
    
    345
    +    def _on_contact_update(self, contact, _signal_name):
    
    345 346
             self._draw_contact(contact)
    
    346 347
     
    
    347 348
         @event_filter(['account'])
    
    ... ... @@ -353,8 +354,11 @@ def _on_roster_push(self, event):
    353 354
             contact = self._get_contact(event.item.jid)
    
    354 355
     
    
    355 356
             if event.item.subscription == 'remove':
    
    357
    +            contact.disconnect(self)
    
    356 358
                 self._remove_contact(contact)
    
    357 359
             else:
    
    360
    +            if contact.jid not in self._contact_refs:
    
    361
    +                self._connect_contact_signals(contact)
    
    358 362
                 self._add_or_update_contact(contact)
    
    359 363
     
    
    360 364
         def _get_current_groups(self, jid):
    
    ... ... @@ -482,7 +486,7 @@ def _draw_contact_row(self, ref, contact):
    482 486
             iter_ = self._get_iter_from_ref(ref)
    
    483 487
     
    
    484 488
             name = GLib.markup_escape_text(contact.name)
    
    485
    -        if jid_is_blocked(self._account, contact.jid):
    
    489
    +        if contact.is_blocked:
    
    486 490
                 name = f'<span strikethrough="true">{name}</span>'
    
    487 491
             self._store[iter_][Column.TEXT] = name
    
    488 492