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

Commits:

4 changed files:

Changes:

  • gajim/gtk/chat_page.py
    ... ... @@ -15,6 +15,9 @@
    15 15
     from typing import Optional
    
    16 16
     from typing import Generator
    
    17 17
     
    
    18
    +import time
    
    19
    +import logging
    
    20
    +
    
    18 21
     from gi.repository import Gtk
    
    19 22
     from gi.repository import GLib
    
    20 23
     from gi.repository import Gio
    
    ... ... @@ -31,6 +34,10 @@
    31 34
     from .chat_stack import ChatStack
    
    32 35
     from .search_view import SearchView
    
    33 36
     from .types import ControlType
    
    37
    +from .const import UNLOAD_CHAT_TIME
    
    38
    +
    
    39
    +
    
    40
    +log = logging.getLogger('gajim.gui.chat_page')
    
    34 41
     
    
    35 42
     
    
    36 43
     class ChatPage(Gtk.Box):
    
    ... ... @@ -43,6 +50,9 @@ class ChatPage(Gtk.Box):
    43 50
     
    
    44 51
         def __init__(self):
    
    45 52
             Gtk.Box.__init__(self)
    
    53
    +
    
    54
    +        self._chat_idle_time: dict[tuple[str, JID], Optional[float]] = {}
    
    55
    +
    
    46 56
             self._ui = get_builder('chat_paned.ui')
    
    47 57
             self.add(self._ui.paned)
    
    48 58
             self._ui.connect_signals(self)
    
    ... ... @@ -132,12 +142,41 @@ def _on_filter_revealer_toggled(self, toggle_button):
    132 142
         def _on_edit_workspace_clicked(_button):
    
    133 143
             app.window.activate_action('edit-workspace', GLib.Variant('s', ''))
    
    134 144
     
    
    135
    -    def _on_chat_selected(self, _chat_list_stack: ChatListStack,
    
    136
    -                          workspace_id: str, account: str,
    
    145
    +    def _reset_chat_idle_time(self, account: str, jid: JID) -> None:
    
    146
    +        # Set the idle time of the current chat to None
    
    147
    +        # and start the timer for the last one
    
    148
    +        for chat, idle_time in self._chat_idle_time.items():
    
    149
    +            if idle_time is None:
    
    150
    +                self._chat_idle_time[chat] = time.time()
    
    151
    +
    
    152
    +        self._chat_idle_time[(account, jid)] = None
    
    153
    +
    
    154
    +    def unload_idle_chats(self) -> bool:
    
    155
    +        log.debug('Unload idle chats')
    
    156
    +        for chat, idle_time in list(self._chat_idle_time.items()):
    
    157
    +            if idle_time is None:
    
    158
    +                continue
    
    159
    +
    
    160
    +            if time.time() - UNLOAD_CHAT_TIME > idle_time:
    
    161
    +                account, jid = chat
    
    162
    +                self._chat_stack.unload_chat(account, jid)
    
    163
    +                self._chat_idle_time.pop(chat)
    
    164
    +                log.debug('Chat %s:%s unloaded', account, jid)
    
    165
    +
    
    166
    +        # Return true because we call this method with
    
    167
    +        # GLib.timeout_add_seconds()
    
    168
    +        return True
    
    169
    +
    
    170
    +    def _on_chat_selected(self,
    
    171
    +                          chat_list_stack: ChatListStack,
    
    172
    +                          workspace_id: str,
    
    173
    +                          account: str,
    
    137 174
                               jid: JID) -> None:
    
    175
    +
    
    138 176
             self._chat_stack.show_chat(account, jid)
    
    139 177
             self._search_view.set_context(account, jid)
    
    140 178
             self.emit('chat-selected', workspace_id, account, jid)
    
    179
    +        self._reset_chat_idle_time(account, jid)
    
    141 180
     
    
    142 181
             control = self.get_control(account, jid)
    
    143 182
             if control is None:
    

  • gajim/gtk/chat_stack.py
    ... ... @@ -55,7 +55,6 @@ def __init__(self):
    55 55
     
    
    56 56
             self.show_all()
    
    57 57
             self._controls: Dict[Tuple[str, JID], ControlType] = {}
    
    58
    -        self._active_control: Optional[ControlType] = None
    
    59 58
     
    
    60 59
         def get_control(self, account: str, jid: JID) -> Optional[ControlType]:
    
    61 60
             try:
    
    ... ... @@ -101,8 +100,6 @@ def add_private_chat(self, account: str, jid: JID) -> None:
    101 100
     
    
    102 101
         def remove_chat(self, account: str, jid: JID) -> None:
    
    103 102
             control = self._controls.pop((account, jid))
    
    104
    -        if control == self._active_control:
    
    105
    -            self._active_control = None
    
    106 103
             self.remove(control.widget)
    
    107 104
             control.shutdown()
    
    108 105
     
    
    ... ... @@ -112,12 +109,15 @@ def show_chat(self, account: str, jid: JID) -> None:
    112 109
             if control is None:
    
    113 110
                 log.warning('No Control found for %s, %s', account, jid)
    
    114 111
                 return
    
    115
    -        if control != self._active_control:
    
    116
    -            if self._active_control is not None:
    
    117
    -                self._active_control.reset_view()
    
    118
    -        self._active_control = control
    
    112
    +
    
    119 113
             GLib.idle_add(control.focus)
    
    120 114
     
    
    115
    +    def unload_chat(self, account: str, jid: JID) -> None:
    
    116
    +        control = self.get_control(account, jid)
    
    117
    +        if control is None:
    
    118
    +            return
    
    119
    +        control.reset_view()
    
    120
    +
    
    121 121
         def clear(self) -> None:
    
    122 122
             self.set_visible_child_name('empty')
    
    123 123
     
    

  • gajim/gtk/const.py
    ... ... @@ -31,6 +31,8 @@
    31 31
     # Drag and drop target type URI list (for dropped files)
    
    32 32
     TARGET_TYPE_URI_LIST = 80
    
    33 33
     
    
    34
    +UNLOAD_CHAT_TIME = 300  # seconds
    
    35
    +
    
    34 36
     
    
    35 37
     @unique
    
    36 38
     class Theme(IntEnum):
    

  • gajim/gtk/main.py
    ... ... @@ -53,6 +53,7 @@
    53 53
     from .util import save_main_window_position
    
    54 54
     from .util import open_window
    
    55 55
     from .util import set_urgency_hint
    
    56
    +from .const import UNLOAD_CHAT_TIME
    
    56 57
     
    
    57 58
     log = logging.getLogger('gajim.gui.main')
    
    58 59
     
    
    ... ... @@ -126,11 +127,16 @@ def __init__(self):
    126 127
             self._add_actions2()
    
    127 128
     
    
    128 129
             self._prepare_window()
    
    130
    +        self._start_timers()
    
    129 131
     
    
    130 132
             if not app.is_display(Display.WAYLAND):
    
    131 133
                 app.interface.systray.connect_unread_changed(
    
    132 134
                     self._chat_page.get_chat_list_stack())
    
    133 135
     
    
    136
    +    def _start_timers(self):
    
    137
    +        GLib.timeout_add_seconds(UNLOAD_CHAT_TIME,
    
    138
    +                                 self._chat_page.unload_idle_chats)
    
    139
    +
    
    134 140
         def _prepare_window(self) -> None:
    
    135 141
             if app.settings.get('main_window_skip_taskbar'):
    
    136 142
                 self.set_property('skip-taskbar-hint', True)