Philipp Hörist pushed to branch unify-control at gajim / gajim

Commits:

4 changed files:

Changes:

  • gajim/common/modules/contacts.py
    ... ... @@ -21,6 +21,7 @@
    21 21
     from typing import overload
    
    22 22
     
    
    23 23
     import cairo
    
    24
    +from gi.repository.GObject import Value
    
    24 25
     from nbxmpp.const import Affiliation
    
    25 26
     from nbxmpp.const import Chatstate
    
    26 27
     from nbxmpp.const import Role
    
    ... ... @@ -122,6 +123,56 @@ def _on_client_state_changed(self,
    122 123
             if state.is_disconnected:
    
    123 124
                 self._reset_presence()
    
    124 125
     
    
    126
    +    def add_chat_contact(self, jid: Union[str, JID]) -> BareContact:
    
    127
    +        if isinstance(jid, str):
    
    128
    +            jid = JID.from_string(jid)
    
    129
    +
    
    130
    +        contact = self._contacts.get(jid)
    
    131
    +        if contact is not None:
    
    132
    +            if not isinstance(contact, BareContact):
    
    133
    +                raise ValueError(f'Trying to add GroupchatContact {jid}, '
    
    134
    +                                 f'but contact exists already as {contact}')
    
    135
    +            return contact
    
    136
    +
    
    137
    +        contact = BareContact(self._log, jid, self._account)
    
    138
    +
    
    139
    +        self._contacts[jid] = contact
    
    140
    +        return contact
    
    141
    +
    
    142
    +    def add_group_chat_contact(self, jid: Union[str, JID]) -> GroupchatContact:
    
    143
    +        if isinstance(jid, str):
    
    144
    +            jid = JID.from_string(jid)
    
    145
    +
    
    146
    +        contact = self._contacts.get(jid)
    
    147
    +        if contact is not None:
    
    148
    +            if not isinstance(contact, GroupchatContact):
    
    149
    +                raise ValueError(f'Trying to add GroupchatContact {jid}, '
    
    150
    +                                 f'but contact exists already as {contact}')
    
    151
    +            return contact
    
    152
    +
    
    153
    +        contact = GroupchatContact(self._log, jid, self._account)
    
    154
    +
    
    155
    +        self._contacts[jid] = contact
    
    156
    +        return contact
    
    157
    +
    
    158
    +    def add_private_contact(self, jid: Union[str, JID]) -> GroupchatParticipant:
    
    159
    +        if isinstance(jid, str):
    
    160
    +            jid = JID.from_string(jid)
    
    161
    +
    
    162
    +        if jid.resource is None:
    
    163
    +            raise ValueError(f'Trying to add a bare JID as private {jid}')
    
    164
    +
    
    165
    +        contact = self._contacts.get(jid.bare)
    
    166
    +        if not isinstance(contact, GroupchatContact):
    
    167
    +            raise ValueError(f'Trying to add GroupchatParticipant {jid}, '
    
    168
    +                             f'to BareContact {contact}')
    
    169
    +
    
    170
    +        if contact is None:
    
    171
    +            group_chat_contact = self.add_group_chat_contact(jid.bare)
    
    172
    +            return group_chat_contact.add_resource(jid.resource)
    
    173
    +
    
    174
    +        return contact.add_resource(jid.resource)
    
    175
    +
    
    125 176
         def add_contact(self,
    
    126 177
                         jid: Union[str, JID],
    
    127 178
                         groupchat: bool = False) -> Union[BareContact,
    
    ... ... @@ -597,6 +648,10 @@ def add_resource(self, resource: str) -> GroupchatParticipant:
    597 648
             # codebase is type checked and it creates hard to track
    
    598 649
             # problems if we create a GroupchatParticipant without resource
    
    599 650
     
    
    651
    +        contact = self._resources.get(resource)
    
    652
    +        if contact is not None:
    
    653
    +            return contact
    
    654
    +
    
    600 655
             jid = self._jid.new_with(resource=resource)
    
    601 656
             assert isinstance(self._log, LogAdapter)
    
    602 657
             contact = GroupchatParticipant(self._log, jid, self._account)
    

  • gajim/gtk/chat_page.py
    ... ... @@ -224,6 +224,18 @@ def add_chat_for_workspace(self,
    224 224
                                    type_: str,
    
    225 225
                                    pinned: bool = False,
    
    226 226
                                    select: bool = False) -> None:
    
    227
    +
    
    228
    +        client = app.get_client(account)
    
    229
    +
    
    230
    +        if type_ == 'chat':
    
    231
    +            client.get_module('Contacts').add_chat_contact(jid)
    
    232
    +
    
    233
    +        elif type_ == 'groupchat':
    
    234
    +            client.get_module('Contacts').add_group_chat_contact(jid)
    
    235
    +
    
    236
    +        elif type_ == 'pm':
    
    237
    +            client.get_module('Contacts').add_group_private_contact(jid)
    
    238
    +
    
    227 239
             if self.chat_exists(account, jid):
    
    228 240
                 if select:
    
    229 241
                     self._chat_list_stack.select_chat(account, jid)
    

  • gajim/gtk/control.py
    ... ... @@ -134,7 +134,9 @@ def has_active_chat(self) -> bool:
    134 134
             return self.contact is not None
    
    135 135
     
    
    136 136
         def clear(self) -> None:
    
    137
    -        self.contact.disconnect_all_from_obj(self)
    
    137
    +        if self.contact is not None:
    
    138
    +            self.contact.disconnect_all_from_obj(self)
    
    139
    +
    
    138 140
             self.contact = None
    
    139 141
             self._clinet = None
    
    140 142
             self.account = None
    

  • gajim/gtk/main.py
    ... ... @@ -16,7 +16,6 @@
    16 16
     
    
    17 17
     from typing import Any
    
    18 18
     from typing import cast
    
    19
    -from typing import Generator
    
    20 19
     from typing import Optional
    
    21 20
     
    
    22 21
     import logging