Welcome to mirror list, hosted at ThFree Co, Russian Federation.

chat_list_stack.py « gtk « gajim - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8362a7b4eb74cb63f55426ce0af9838c3e0eba2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# 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/>.

from __future__ import annotations

from typing import cast

from gi.repository import Gio
from gi.repository import GLib
from gi.repository import GObject
from gi.repository import Gtk
from nbxmpp import JID

from gajim.common import app
from gajim.common import events
from gajim.common import ged
from gajim.common.ged import EventHelper
from gajim.common.i18n import _

from gajim.gtk import structs
from gajim.gtk.chat_filter import ChatFilter
from gajim.gtk.chat_list import ChatList
from gajim.gtk.chat_list_row import ChatListRow
from gajim.gtk.dialogs import ConfirmationCheckDialog
from gajim.gtk.dialogs import DialogButton


class ChatListStack(Gtk.Stack, EventHelper):

    __gsignals__ = {
        'unread-count-changed': (GObject.SignalFlags.RUN_LAST,
                                 None,
                                 (str, int)),
        'chat-selected': (GObject.SignalFlags.RUN_LAST,
                          None,
                          (str, str, object)),
        'chat-unselected': (GObject.SignalFlags.RUN_LAST,
                            None,
                            ()),
        'chat-removed': (GObject.SignalFlags.RUN_LAST,
                         None,
                         (str, object, str)),
    }

    def __init__(self,
                 chat_filter: ChatFilter,
                 search_entry: Gtk.SearchEntry
                 ) -> None:
        Gtk.Stack.__init__(self)
        EventHelper.__init__(self)
        self.set_hexpand(True)
        self.set_vexpand(True)
        self.set_vhomogeneous(False)

        self._chat_lists: dict[str, ChatList] = {}

        self._last_visible_child_name: str = 'default'

        self.add_named(Gtk.Box(), 'default')

        self.connect('notify::visible-child-name', self._on_visible_child_name)
        search_entry.connect('search-changed', self._on_search_changed)
        chat_filter.connect('filter-changed', self._on_filter_changed)

        self._add_actions()
        self.show_all()

        self.register_events([
            ('message-received', ged.GUI2, self._on_event),
            ('mam-message-received', ged.GUI2, self._on_event),
            ('gc-message-received', ged.GUI2, self._on_event),
            ('message-updated', ged.GUI2, self._on_event),
            ('message-moderated', ged.GUI2, self._on_event),
            ('presence-received', ged.GUI2, self._on_event),
            ('message-sent', ged.GUI2, self._on_event),
            ('file-request-received', ged.GUI2, self._on_event),
            ('jingle-request-received', ged.GUI2, self._on_event),
        ])

    def _add_actions(self) -> None:
        actions = [
            ('toggle-chat-pinned', 'a{sv}', self._toggle_chat_pinned),
            ('move-chat-to-workspace', 'a{sv}', self._move_chat_to_workspace),
            ('mark-as-read', 'a{sv}', self._mark_as_read),
        ]

        for action in actions:
            action_name, variant, func = action
            variant = GLib.VariantType.new(variant)
            act = Gio.SimpleAction.new(action_name, variant)
            act.connect('activate', func)
            app.window.add_action(act)

    def _on_visible_child_name(self, _stack: Gtk.Stack, _param: str) -> None:
        if self._last_visible_child_name == self.get_visible_child_name():
            return

        if self._last_visible_child_name != 'default':
            chat_list = cast(
                ChatList,
                self.get_child_by_name(self._last_visible_child_name))
            chat_list.set_filter_text('')
        last_child = self.get_visible_child_name() or 'default'
        self._last_visible_child_name = last_child

    def get_chatlist(self, workspace_id: str) -> ChatList:
        return self._chat_lists[workspace_id]

    def get_selected_chat(self) -> ChatListRow | None:
        chat_list = self.get_current_chat_list()
        if chat_list is None:
            return None
        return chat_list.get_selected_chat()

    def get_current_chat_list(self) -> ChatList | None:
        workspace_id = self.get_visible_child_name()
        if workspace_id == 'empty' or workspace_id is None:
            return None

        return self._chat_lists[workspace_id]

    def is_chat_selected(self, account: str, jid: JID) -> bool:
        chat = self.get_selected_chat()
        if chat is None:
            return False
        return chat.account == account and chat.jid == jid

    def _on_filter_changed(self, _filter: ChatFilter, name: str) -> None:
        chat_list = cast(ChatList, self.get_visible_child())
        chat_list.set_filter(name)

    def _on_search_changed(self, search_entry: Gtk.SearchEntry) -> None:
        chat_list = cast(ChatList, self.get_visible_child())
        chat_list.set_filter_text(search_entry.get_text())

    def add_chat_list(self, workspace_id: str) -> ChatList:
        chat_list = ChatList(workspace_id)
        chat_list.connect('row-selected', self._on_row_selected)
        chat_list.connect('chat-order-changed', self._on_chat_order_changed)

        self._chat_lists[workspace_id] = chat_list
        self.add_named(chat_list, workspace_id)
        return chat_list

    def remove_chat_list(self, workspace_id: str) -> None:
        chat_list = self._chat_lists[workspace_id]
        self.remove(chat_list)
        for open_chat in chat_list.get_open_chats():
            self.remove_chat(workspace_id,
                             open_chat['account'],
                             open_chat['jid'])

        self._chat_lists.pop(workspace_id)
        chat_list.destroy()

    def _on_row_selected(self,
                         _chat_list: ChatList,
                         row: ChatListRow | None
                         ) -> None:
        if row is None:
            self.emit('chat-unselected')
            return

        self.emit('chat-selected', row.workspace_id, row.account, row.jid)

    def _on_chat_order_changed(self,
                               chat_list: ChatList
                               ) -> None:

        self.store_open_chats(chat_list.workspace_id)

    def show_chat_list(self, workspace_id: str) -> None:
        cur_workspace_id = self.get_visible_child_name()
        if cur_workspace_id != 'default' and cur_workspace_id is not None:
            self._chat_lists[cur_workspace_id].unselect_all()

        self.set_visible_child_name(workspace_id)

    def add_chat(self,
                 workspace_id: str,
                 account: str,
                 jid: JID,
                 type_: str,
                 pinned: bool,
                 position: int
                 ) -> None:

        chat_list = self._chat_lists.get(workspace_id)
        if chat_list is None:
            chat_list = self.add_chat_list(workspace_id)
        chat_list.add_chat(account, jid, type_, pinned, position)

    def select_chat(self, account: str, jid: JID) -> None:
        chat_list = self.find_chat(account, jid)
        if chat_list is None:
            return

        self.show_chat_list(chat_list.workspace_id)
        chat_list.select_chat(account, jid)

    def store_open_chats(self, workspace_id: str) -> None:
        chat_list = self._chat_lists[workspace_id]
        open_chats = chat_list.get_open_chats()
        app.settings.set_workspace_setting(
            workspace_id, 'chats', open_chats)

    @structs.actionmethod
    def _toggle_chat_pinned(self,
                            _action: Gio.SimpleAction,
                            params: structs.ChatListEntryParam
                            ) -> None:

        chat_list = self._chat_lists[params.workspace_id]
        chat_list.toggle_chat_pinned(params.account, params.jid)
        self.store_open_chats(params.workspace_id)

    @structs.actionmethod
    def _move_chat_to_workspace(self,
                                _action: Gio.SimpleAction,
                                params: structs.ChatListEntryParam
                                ) -> None:

        workspace_id = params.workspace_id
        if not workspace_id:
            workspace_id = app.window.add_workspace(switch=False)

        source_chatlist = self.get_chatlist(params.source_workspace_id)
        type_ = source_chatlist.get_chat_type(params.account, params.jid)
        if type_ is None:
            return

        source_chatlist.remove_chat(params.account, params.jid)

        new_chatlist = self.get_chatlist(workspace_id)
        new_chatlist.add_chat(params.account, params.jid, type_, False, -1)

        self.store_open_chats(source_chatlist.workspace_id)
        self.store_open_chats(workspace_id)

    @structs.actionmethod
    def _mark_as_read(self,
                      _action: Gio.SimpleAction,
                      params: structs.AccountJidParam
                      ) -> None:

        self.mark_as_read(params.account, params.jid)

    def remove_chat(self, workspace_id: str, account: str, jid: JID) -> None:
        chat_list = self._chat_lists[workspace_id]
        type_ = chat_list.get_chat_type(account, jid)

        def _leave(not_ask_again: bool) -> None:
            if not_ask_again:
                app.settings.set('confirm_close_muc', False)
            _remove()

        def _remove() -> None:
            chat_list.remove_chat(account, jid, emit_unread=False)
            self.store_open_chats(workspace_id)
            self.emit('chat-removed', account, jid, type_)

        if type_ != 'groupchat' or not app.settings.get('confirm_close_muc'):
            _remove()
            return

        client = app.get_client(account)
        contact = client.get_module('Contacts').get_contact(jid, groupchat=True)

        if contact.is_not_joined and client.state.is_available:
            # For example a chat opened from the search bar
            _remove()
            return

        ConfirmationCheckDialog(
            _('Leave Group Chat'),
            _('Are you sure you want to leave this group chat?'),
            _('If you close this chat, you will leave '
              '"%s".') % contact.name,
            _('_Do not ask me again'),
            [DialogButton.make('Cancel'),
             DialogButton.make('Accept',
                               text=_('_Leave'),
                               callback=_leave)],
            transient_for=app.window).show()

    def remove_chats_for_account(self, account: str) -> None:
        for workspace_id, chat_list in self._chat_lists.items():
            chat_list.remove_chats_for_account(account)
            self.store_open_chats(workspace_id)

    def find_chat(self, account: str, jid: JID) -> ChatList | None:
        for chat_list in self._chat_lists.values():
            if chat_list.contains_chat(account, jid):
                return chat_list
        return None

    def contains_chat(self, account: str, jid: JID,
                      workspace_id: str | None = None) -> bool:
        if workspace_id is None:
            return any(chat_list.contains_chat(account, jid) for
                       chat_list in self._chat_lists.values())

        chat_list = self._chat_lists[workspace_id]
        return chat_list.contains_chat(account, jid)

    def get_total_unread_count(self) -> int:
        count = 0
        for chat_list in self._chat_lists.values():
            count += chat_list.get_unread_count()
        return count

    def get_chat_unread_count(self,
                              account: str,
                              jid: JID,
                              include_silent: bool = False
                              ) -> int | None:
        for chat_list in self._chat_lists.values():
            count = chat_list.get_chat_unread_count(
                account, jid, include_silent)
            if count is not None:
                return count
        return None

    def set_chat_unread_count(self,
                              account: str,
                              jid: JID,
                              count: int
                              ) -> None:
        for chat_list in self._chat_lists.values():
            chat_list.set_chat_unread_count(account, jid, count)

    def mark_as_read(self, account: str, jid: JID) -> None:
        for chat_list in self._chat_lists.values():
            chat_list.mark_as_read(account, jid)

    def _on_event(self, event: events.ChatListEventT) -> None:
        jid = JID.from_string(event.jid)

        chat_list = self.find_chat(event.account, jid)
        if chat_list is None:
            return
        chat_list.process_event(event)