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

roster.py « gtk « gajim - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 24dcd49aceb882ee98fdea9382f5bb5e93101d5e (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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
# 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 Any
from typing import Literal

import locale
import logging
from collections import defaultdict
from enum import IntEnum

from gi.repository import Gdk
from gi.repository import Gio
from gi.repository import GLib
from gi.repository import Gtk
from nbxmpp import JID
from nbxmpp.const import PresenceShow
from nbxmpp.namespaces import Namespace

from gajim.common import app
from gajim.common import ged
from gajim.common.const import AvatarSize
from gajim.common.const import PresenceShowExt
from gajim.common.const import StyleAttr
from gajim.common.events import ApplicationEvent
from gajim.common.events import RosterPush
from gajim.common.events import RosterReceived
from gajim.common.helpers import event_filter
from gajim.common.i18n import _
from gajim.common.modules.contacts import BareContact

from gajim.gtk.builder import get_builder
from gajim.gtk.dialogs import ConfirmationDialog
from gajim.gtk.dialogs import DialogButton
from gajim.gtk.menus import get_roster_menu
from gajim.gtk.tooltips import ContactTooltip
from gajim.gtk.util import EventHelper
from gajim.gtk.util import GajimPopover
from gajim.gtk.util import open_window

log = logging.getLogger('gajim.gtk.roster')


DEFAULT_GROUP = _('Contacts')


class Column(IntEnum):
    AVATAR = 0
    TEXT = 1
    IS_CONTACT = 2
    JID_OR_GROUP = 3
    VISIBLE = 4


class Roster(Gtk.ScrolledWindow, EventHelper):
    def __init__(self, account: str) -> None:
        Gtk.ScrolledWindow.__init__(self)
        EventHelper.__init__(self)
        self.set_size_request(200, -1)
        self.set_vexpand(True)
        self.get_style_context().add_class('roster')

        self._account = account
        self._client = app.get_client(account)
        self._contacts = self._client.get_module('Contacts')

        self._roster_tooltip = ContactTooltip()

        self._ui = get_builder('roster.ui')
        self._ui.roster_treeview.set_model(None)
        self.add(self._ui.roster_treeview)

        self._contact_refs: dict[
            JID, list[Gtk.TreeRowReference]] = defaultdict(list)
        self._group_refs: dict[str, Gtk.TreeRowReference] = {}

        self._store = self._ui.contact_store
        self._store.set_sort_func(Column.TEXT, self._tree_compare_iters)

        self._roster = self._ui.roster_treeview
        self._roster.set_has_tooltip(True)
        self._roster.connect('query-tooltip', self._on_query_tooltip)

        # Drag and Drop
        entries = [Gtk.TargetEntry.new(
            'ROSTER_ITEM',
            Gtk.TargetFlags.SAME_APP,
            0)]
        self._roster.enable_model_drag_source(
            Gdk.ModifierType.BUTTON1_MASK,
            entries,
            Gdk.DragAction.MOVE)
        self._roster.enable_model_drag_dest(entries, Gdk.DragAction.DEFAULT)
        self._roster.connect('drag-drop', self._on_drag_drop)
        self._roster.connect('drag-data-received', self._on_drag_data_received)

        self._ui.contact_column.set_cell_data_func(self._ui.text_renderer,
                                                   self._text_cell_data_func)
        self.connect('destroy', self._on_destroy)
        self._ui.connect_signals(self)

        self.register_events([
            ('roster-received', ged.CORE, self._on_roster_received),
            ('theme-update', ged.GUI2, self._on_theme_update),
            ('roster-push', ged.GUI2, self._on_roster_push),
        ])

        roster_size = self._client.get_module('Roster').get_size()
        self._high_performance = roster_size > 1000

        self._modelfilter = self._store.filter_new()
        if self._high_performance:
            self._modelfilter.set_visible_func(self._visible_func)
        else:
            self._modelfilter.set_visible_column(Column.VISIBLE)
        self._filter_enabled = False
        self._filter_string = ''

        app.settings.connect_signal('showoffline', self._on_setting_changed)
        app.settings.connect_signal('sort_by_show_in_roster',
                                    self._on_setting_changed)

        self._action_signal_ids = self._connect_actions()
        self._initial_draw()

    def _connect_actions(self) -> dict[Gio.Action, int]:
        app_actions = [
            (f'{self._account}-contact-info', self._on_contact_info),
            (f'{self._account}-modify-gateway', self._on_modify_gateway),
            (f'{self._account}-execute-command', self._on_execute_command),
            (f'{self._account}-block-contact', self._on_block_contact),
            (f'{self._account}-remove-contact', self._on_remove_contact),
        ]

        signal_ids: dict[Gio.Action, int] = {}
        for action in app_actions:
            action_name, func = action
            action = app.app.lookup_action(action_name)
            assert action is not None
            signal_id = action.connect('activate', func)
            signal_ids[action] = signal_id

        return signal_ids

    def _get_contact(self, jid: str) -> BareContact:
        contact = self._client.get_module('Contacts').get_contact(jid)
        assert isinstance(contact, BareContact)
        return contact

    def _on_theme_update(self, _event: ApplicationEvent) -> None:
        self.redraw()

    @staticmethod
    def _on_drag_drop(treeview: Gtk.TreeView,
                      drag_context: Gdk.DragContext,
                      _x_coord: int,
                      _y_coord: int,
                      timestamp: int) -> bool:

        treeview.stop_emission_by_name('drag-drop')
        target_list = treeview.drag_dest_get_target_list()
        target = treeview.drag_dest_find_target(drag_context, target_list)
        treeview.drag_get_data(drag_context, target, timestamp)
        return True

    def _on_drag_data_received(self,
                               treeview: Gtk.TreeView,
                               _drag_context: Gdk.DragContext,
                               x_coord: int,
                               y_coord: int,
                               _selection_data: Gtk.SelectionData,
                               _info: int,
                               _timestamp: int) -> None:

        treeview.stop_emission_by_name('drag-data-received')
        if treeview.get_selection().count_selected_rows() == 0:
            # No selections, nothing dragged from treeview
            return

        drop_info = treeview.get_dest_row_at_pos(x_coord, y_coord)
        if not drop_info:
            return

        path_dest, _ = drop_info

        # Source: the row being dragged
        rows = treeview.get_selection().get_selected_rows()
        assert rows is not None
        model, paths = rows
        path_source = paths[0]
        iter_source = model.get_iter(path_source)
        iter_source_parent = model.iter_parent(iter_source)
        if iter_source_parent is None:
            # Dragged a group
            return

        source_group = model[iter_source_parent][Column.JID_OR_GROUP]

        jid = model[iter_source][Column.JID_OR_GROUP]

        # Destination: the row receiving the drop
        iter_dest = model.get_iter(path_dest)
        iter_dest_parent = model.iter_parent(iter_dest)
        if iter_dest_parent is None:
            # Dropped on a group
            dest_group = model[iter_dest][Column.JID_OR_GROUP]
        else:
            dest_group = model[iter_dest_parent][Column.JID_OR_GROUP]

        if source_group == dest_group:
            return

        if DEFAULT_GROUP == dest_group:
            self._client.get_module('Roster').set_groups(jid, None)
            return

        self._client.get_module('Roster').change_group(jid,
                                                       source_group,
                                                       dest_group)

    def _on_query_tooltip(self,
                          treeview: Gtk.TreeView,
                          x_coord: int,
                          y_coord: int,
                          _keyboard_mode: bool,
                          tooltip: Gtk.Tooltip) -> bool:

        row = treeview.get_path_at_pos(x_coord, y_coord)
        if row is None:
            self._roster_tooltip.clear_tooltip()
            return False

        path = row[0]
        if path is None:
            self._roster_tooltip.clear_tooltip()
            return False

        model = treeview.get_model()
        if model is None:
            self._roster_tooltip.clear_tooltip()
            return False

        iter_ = model.get_iter(path)
        if not model[iter_][Column.IS_CONTACT]:
            # It’s a group
            self._roster_tooltip.clear_tooltip()
            return False

        contact = self._contacts.get_bare_contact(
            model[iter_][Column.JID_OR_GROUP])
        assert isinstance(contact, BareContact)
        value, widget = self._roster_tooltip.get_tooltip(contact)
        tooltip.set_custom(widget)
        return value

    def _on_setting_changed(self, *args: Any) -> None:
        self._refilter()

    def _on_contact_info(self,
                         _action: Gio.SimpleAction,
                         param: GLib.Variant) -> None:

        app.window.contact_info(self._account, param.get_string())

    def _on_modify_gateway(self,
                           _action: Gio.SimpleAction,
                           param: GLib.Variant) -> None:
        open_window(
            'ServiceRegistration',
            account=self._account,
            address=JID.from_string(param.get_string()))

    def _on_execute_command(self,
                            _action: Gio.SimpleAction,
                            param: GLib.Variant) -> None:

        app.window.execute_command(self._account, param.get_string())

    def _on_block_contact(self,
                          _action: Gio.SimpleAction,
                          param: GLib.Variant):

        app.window.block_contact(self._account, param.get_string())

    def _on_remove_contact(self,
                           _action: Gio.SimpleAction,
                           param: GLib.Variant):

        jid = JID.from_string(param.get_string())
        selected_contact = self._contacts.get_contact(jid)
        assert isinstance(selected_contact, BareContact)
        if selected_contact.is_gateway:
            # Check for transport users in roster and warn about removing the
            # transport if there are any
            has_transport_contacts = False
            for contact in self._client.get_module('Roster').iter_contacts():
                if contact.jid.domain == selected_contact.jid.domain:
                    has_transport_contacts = True
                    break

            def _on_remove():
                self._client.get_module('Gateway').unsubscribe(
                    str(selected_contact.jid))

            if has_transport_contacts:
                ConfirmationDialog(
                    _('Remove Transport'),
                    _('Transport "%s" will be '
                      'removed') % selected_contact.name,
                    _('You will no longer be able to send and receive '
                      'messages from and to contacts using this transport.'),
                    [DialogButton.make('Cancel'),
                     DialogButton.make('Remove',
                                       callback=_on_remove)],
                    transient_for=app.window).show()
            else:
                _on_remove()
            return

        app.window.remove_contact(self._account, jid)

    def _on_roster_row_activated(self,
                                 _treeview: Gtk.TreeView,
                                 path: Gtk.TreePath,
                                 _column: Gtk.TreeViewColumn) -> None:

        converted_path = self._modelfilter.convert_path_to_child_path(path)
        assert converted_path is not None
        iter_ = self._store.get_iter(converted_path)
        if self._store.iter_parent(iter_) is None:
            # This is a group row
            return

        jid = JID.from_string(self._store[iter_][Column.JID_OR_GROUP])
        app.window.add_chat(self._account, jid, 'contact', select=True)

    def _on_roster_button_press_event(self,
                                      treeview: Gtk.TreeView,
                                      event: Gdk.EventButton) -> None:

        if event.button == Gdk.BUTTON_PRIMARY:
            return

        pos = treeview.get_path_at_pos(int(event.x), int(event.y))
        if pos is None:
            return

        path, _, _, _ = pos
        if path is None:
            return

        path = self._modelfilter.convert_path_to_child_path(path)
        assert path is not None
        iter_ = self._store.get_iter(path)
        if self._store.iter_parent(iter_) is None:
            # Group row
            return

        jid = self._store[iter_][Column.JID_OR_GROUP]

        if event.button == Gdk.BUTTON_SECONDARY:
            self._show_contact_menu(jid, treeview, event)

        jid = JID.from_string(jid)
        if event.button == Gdk.BUTTON_MIDDLE:
            app.window.add_chat(self._account, jid, 'contact', select=True)

    @staticmethod
    def _on_focus_out(treeview: Gtk.TreeView, _event: Gdk.EventFocus) -> None:
        treeview.get_selection().unselect_all()

    def _show_contact_menu(self,
                           jid: str,
                           treeview: Gtk.TreeView,
                           event: Gdk.EventButton) -> None:

        contact = self._contacts.get_bare_contact(jid)
        assert isinstance(contact, BareContact)
        gateway_register = contact.is_gateway and contact.supports(
            Namespace.REGISTER)

        menu = get_roster_menu(self._account, jid, gateway=gateway_register)
        popover = GajimPopover(menu, relative_to=treeview, event=event)
        popover.popup()

    def set_search_string(self, text: str) -> None:
        self._filter_string = text.lower()
        self._filter_enabled = bool(text)
        self._refilter()

    def _visible_func(self,
                      model: Gtk.TreeModelFilter,
                      iter_: Gtk.TreeIter,
                      *_data: Any) -> bool:

        if not self._filter_enabled:
            return True

        if not model[iter_][Column.IS_CONTACT]:
            return True

        return self._filter_string in model[iter_][Column.TEXT].lower()

    def _get_contact_visible(self, contact: BareContact) -> bool:
        if self._filter_enabled:
            return self._filter_string in contact.name.lower()

        if app.settings.get('showoffline'):
            return True

        if contact.show is PresenceShowExt.OFFLINE:
            return False

        return True

    def _set_model(self) -> None:
        self._roster.set_model(self._modelfilter)

    def _unset_model(self) -> None:
        self._roster.set_model(None)

    def redraw(self) -> None:
        self._unset_model()
        self._set_model()
        self._roster.expand_all()

    def _reset_roster(self) -> None:
        self._unset_model()
        self._enable_sort(False)
        self._clear()
        self._initial_draw()

    def _enable_sort(self, enable: bool) -> None:
        column = Gtk.TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID
        if enable:
            column = Column.TEXT

        self._store.set_sort_column_id(column, Gtk.SortType.ASCENDING)

    def _initial_draw(self) -> None:
        for contact in self._client.get_module('Roster').iter_contacts():
            self._add_or_update_contact(contact)

        self._enable_sort(True)
        self._set_model()
        self._roster.expand_all()

    def _connect_contact_signals(self, contact: BareContact) -> None:
        contact.connect('presence-update', self._on_contact_update)
        contact.connect('caps-update', self._on_contact_update)
        contact.connect('avatar-update', self._on_contact_update)
        contact.connect('blocking-update', self._on_contact_update)

    def _on_contact_update(self,
                           contact: BareContact,
                           _signal_name: str) -> None:

        self._draw_contact(contact)

    @event_filter(['account'])
    def _on_roster_received(self, _event: RosterReceived) -> None:
        self._reset_roster()

    @event_filter(['account'])
    def _on_roster_push(self, event: RosterPush) -> None:
        contact = self._contacts.get_contact(str(event.item.jid))
        assert isinstance(contact, BareContact)

        if event.item.subscription == 'remove':
            self._remove_contact(contact)
        else:
            self._add_or_update_contact(contact)

    def _get_current_groups(self, jid: JID) -> set[str]:
        groups: set[str] = set()
        for ref in self._contact_refs[jid]:
            iter_ = self._get_iter_from_ref(ref)
            group_iter = self._store.iter_parent(iter_)
            assert group_iter is not None
            groups.add(self._store[group_iter][Column.JID_OR_GROUP])
        return groups

    def _add_group(self, group_name: str) -> Gtk.TreeIter:
        group_iter = self._get_group_iter(group_name)
        if group_iter is not None:
            return group_iter

        group_iter = self._store.append(
            None, [None, group_name, False, group_name, True])
        group_path = self._store.get_path(group_iter)
        group_ref = Gtk.TreeRowReference(self._store, group_path)
        self._group_refs[group_name] = group_ref
        return group_iter

    def _add_contact_to_group(self,
                              contact: BareContact,
                              group: str) -> None:

        group_iter = self._add_group(group)
        iter_ = self._store.append(
            group_iter,
            [None, contact.name, True, str(contact.jid), True])

        ref = Gtk.TreeRowReference(self._store, self._store.get_path(iter_))
        self._contact_refs[contact.jid].append(ref)

    def _add_contact_to_groups(self,
                               contact: BareContact,
                               groups: set[str]) -> None:
        for group in groups:
            self._add_contact_to_group(contact, group)

    def _remove_contact_from_groups(self,
                                    contact: BareContact,
                                    groups: set[str]) -> None:
        if not groups:
            return

        refs = self._contact_refs[contact.jid]
        for ref in refs:
            iter_ = self._get_iter_from_ref(ref)
            group_iter = self._store.iter_parent(iter_)
            assert group_iter is not None
            group = self._store[group_iter][Column.JID_OR_GROUP]
            if group in groups:
                path = ref.get_path()
                assert path is not None
                iter_ = self._store.get_iter(path)
                self._store.remove(iter_)
                self._contact_refs[contact.jid].remove(ref)

        self._check_for_empty_groups()

    def _remove_contact(self, contact: BareContact) -> None:
        contact.disconnect(self)
        refs = self._contact_refs.pop(contact.jid)
        for ref in refs:
            iter_ = self._get_iter_from_ref(ref)
            self._store.remove(iter_)

        self._check_for_empty_groups()

    def _check_for_empty_groups(self) -> None:
        for ref in list(self._group_refs.values()):
            group_iter = self._get_iter_from_ref(ref)
            if self._store.iter_has_child(group_iter):
                continue
            group = self._store[group_iter][Column.JID_OR_GROUP]
            self._store.remove(group_iter)
            del self._group_refs[group]

    def _add_or_update_contact(self, contact: BareContact) -> None:
        self._connect_contact_signals(contact)
        new_groups = set(contact.groups or [DEFAULT_GROUP])
        groups = self._get_current_groups(contact.jid)

        add_to_groups = new_groups - groups
        remove_from_groups = groups - new_groups

        self._add_contact_to_groups(contact, add_to_groups)
        self._remove_contact_from_groups(contact, remove_from_groups)

        self._draw_groups()
        self._draw_contact(contact)

    def _draw_groups(self) -> None:
        for group in self._group_refs:
            self._draw_group(group)

    def _draw_group(self, group_name: str) -> None:
        group_iter = self._get_group_iter(group_name)
        if not group_iter:
            return

        if self._roster.get_model() is not None:
            group_path = self._store.get_path(group_iter)
            self._roster.expand_row(group_path, False)

        total_users = self._get_total_user_count()
        group_users = self._store.iter_n_children(group_iter)
        group_name += f' ({group_users}/{total_users})'

        self._store[group_iter][Column.TEXT] = group_name

    def _refilter(self) -> None:
        if self._high_performance:
            self._modelfilter.refilter()
            self._roster.expand_all()
            return

        for group in self._store:
            group_is_visible = False
            for child in group.iterchildren():
                contact = self._contacts.get_bare_contact(
                    child[Column.JID_OR_GROUP])
                assert isinstance(contact, BareContact)
                is_visible = self._get_contact_visible(contact)
                child[Column.VISIBLE] = is_visible
                if is_visible:
                    group_is_visible = True

            group[Column.VISIBLE] = group_is_visible
        self._roster.expand_all()

    def _draw_contact(self, contact: BareContact) -> None:
        for ref in self._contact_refs[contact.jid]:
            self._draw_contact_row(ref, contact)
        self._roster.expand_all()

    def _draw_contact_row(self,
                          ref: Gtk.TreeRowReference,
                          contact: BareContact):

        iter_ = self._get_iter_from_ref(ref)

        name = GLib.markup_escape_text(contact.name)
        if contact.is_blocked:
            name = f'<span strikethrough="true">{name}</span>'
        self._store[iter_][Column.TEXT] = name

        surface = contact.get_avatar(
            AvatarSize.ROSTER, self.get_scale_factor())
        self._store[iter_][Column.AVATAR] = surface
        self._store[iter_][Column.VISIBLE] = self._get_contact_visible(contact)

    def _get_total_user_count(self) -> int:
        count = 0
        for group_row in self._store:
            count += self._store.iter_n_children(group_row.iter)
        return count

    def _get_iter_from_ref(self, ref: Gtk.TreeRowReference) -> Gtk.TreeIter:
        path = ref.get_path()
        assert path is not None
        return self._store.get_iter(path)

    def _get_group_iter(self, group_name: str) -> Gtk.TreeIter | None:
        try:
            ref = self._group_refs[group_name]
        except KeyError:
            return None

        path = ref.get_path()
        if path is None:
            return None
        return self._store.get_iter(path)

    @staticmethod
    def _text_cell_data_func(_column: Gtk.TreeViewColumn,
                             renderer: Gtk.CellRenderer,
                             model: Gtk.TreeModel,
                             iter_: Gtk.TreeIter,
                             _user_data: Literal[None]) -> None:

        has_parent = bool(model.iter_parent(iter_))
        style = 'contact' if has_parent else 'group'

        bgcolor = app.css_config.get_value(f'.gajim-{style}-row',
                                           StyleAttr.BACKGROUND)
        renderer.set_property('cell-background', bgcolor)

        color = app.css_config.get_value(f'.gajim-{style}-row',
                                         StyleAttr.COLOR)
        renderer.set_property('foreground', color)

        desc = app.css_config.get_font(f'.gajim-{style}-row')
        renderer.set_property('font-desc', desc)

        if not has_parent:
            renderer.set_property('weight', 600)
            renderer.set_property('ypad', 6)

    def _tree_compare_iters(self,
                            model: Gtk.TreeModel,
                            iter1: Gtk.TreeIter,
                            iter2: Gtk.TreeIter,
                            _user_data: Literal[None]) -> int:
        '''
        Compare two iterators to sort them
        '''

        is_contact = model.iter_parent(iter1)
        if is_contact:
            name1 = model[iter1][Column.TEXT]
            name2 = model[iter2][Column.TEXT]

            if not app.settings.get('sort_by_show_in_roster'):
                return locale.strcoll(name1.lower(), name2.lower())

            contact1 = self._contacts.get_bare_contact(
                model[iter1][Column.JID_OR_GROUP])
            assert isinstance(contact1, BareContact)
            contact2 = self._contacts.get_bare_contact(
                model[iter2][Column.JID_OR_GROUP])
            assert isinstance(contact2, BareContact)

            if contact1.show != contact2.show:
                if contact1.show == PresenceShow.DND:
                    return 1
                if contact2.show == PresenceShow.DND:
                    return -1
                return -1 if contact1.show > contact2.show else 1

            return locale.strcoll(name1.lower(), name2.lower())

        # Group
        group1 = model[iter1][Column.JID_OR_GROUP]
        group2 = model[iter2][Column.JID_OR_GROUP]
        return -1 if group1 < group2 else 1

    def _clear(self):
        self._contact_refs.clear()
        self._group_refs.clear()
        self._store.clear()

    def _disconnect_actions(self) -> None:
        for action, signal_id in self._action_signal_ids.items():
            action.disconnect(signal_id)
        self._action_signal_ids.clear()

    @staticmethod
    def _dummy_tree_compare(
        _model: Gtk.TreeModel,
        _iter1: Gtk.TreeIter,
        _iter2: Gtk.TreeIter,
        _user_data: Any
    ) -> int:
        return 0

    def _on_destroy(self, _roster: Roster) -> None:
        app.settings.disconnect_signals(self)
        self._disconnect_actions()
        self._contact_refs.clear()
        self._group_refs.clear()
        self._unset_model()
        self._enable_sort(False)
        # Set dummy method, otherwise store will not get destroyed
        self._store.set_sort_func(Column.TEXT, self._dummy_tree_compare)
        self._store.clear()
        self._roster_tooltip.destroy()
        del self._roster
        del self._store
        del self._roster_tooltip
        del self._contacts
        app.check_finalize(self)