Daniel Brötzmann pushed to branch move-workspace-chats-on-remove at gajim / gajim

Commits:

6 changed files:

Changes:

  • gajim/gtk/chat_list_stack.py
    ... ... @@ -243,16 +243,21 @@ def _move_chat_to_workspace(self,
    243 243
             if not workspace_id:
    
    244 244
                 workspace_id = app.window.add_workspace(switch=False)
    
    245 245
     
    
    246
    -        current_chatlist = cast(ChatList, self.get_visible_child())
    
    247
    -        type_ = current_chatlist.get_chat_type(params.account, params.jid)
    
    246
    +        if params.source_workspace_id:
    
    247
    +            source_chatlist = self.get_chatlist(params.source_workspace_id)
    
    248
    +        else:
    
    249
    +            source_chatlist = cast(ChatList, self.get_visible_child())
    
    250
    +
    
    251
    +        type_ = source_chatlist.get_chat_type(params.account, params.jid)
    
    248 252
             if type_ is None:
    
    249 253
                 return
    
    250
    -        current_chatlist.remove_chat(params.account, params.jid)
    
    254
    +
    
    255
    +        source_chatlist.remove_chat(params.account, params.jid)
    
    251 256
     
    
    252 257
             new_chatlist = self.get_chatlist(workspace_id)
    
    253 258
             new_chatlist.add_chat(params.account, params.jid, type_, False, -1)
    
    254 259
     
    
    255
    -        self.store_open_chats(current_chatlist.workspace_id)
    
    260
    +        self.store_open_chats(source_chatlist.workspace_id)
    
    256 261
             self.store_open_chats(workspace_id)
    
    257 262
     
    
    258 263
         @structs.actionmethod
    

  • gajim/gtk/main.py
    ... ... @@ -62,6 +62,7 @@
    62 62
     from .structs import AccountJidParam
    
    63 63
     from .structs import AddChatActionParams
    
    64 64
     from .structs import actionmethod
    
    65
    +from .structs import ChatListEntryParam
    
    65 66
     
    
    66 67
     if TYPE_CHECKING:
    
    67 68
         from .control import ChatControl
    
    ... ... @@ -626,18 +627,52 @@ def _remove_workspace(self,
    626 627
                 self.remove_workspace(workspace_id)
    
    627 628
     
    
    628 629
         def remove_workspace(self, workspace_id: str) -> None:
    
    630
    +        if len(app.settings.get_workspaces()) == 1:
    
    631
    +            log.warning('Tried to remove the only workspace')
    
    632
    +            return
    
    633
    +
    
    629 634
             was_active = self.get_active_workspace() == workspace_id
    
    635
    +        chat_list = self.get_chat_list(workspace_id)
    
    636
    +        open_chats = chat_list.get_open_chats()
    
    637
    +
    
    638
    +        def _continue_removing_workspace():
    
    639
    +            new_workspace_id = self._workspace_side_bar.get_other_workspace(
    
    640
    +                workspace_id)
    
    641
    +            if new_workspace_id is None:
    
    642
    +                log.warning('No other workspaces found')
    
    643
    +                return
    
    630 644
     
    
    631
    -        success = self._workspace_side_bar.remove_workspace(workspace_id)
    
    632
    -        if not success:
    
    645
    +            for open_chat in open_chats:
    
    646
    +                params = ChatListEntryParam(
    
    647
    +                    workspace_id=new_workspace_id,
    
    648
    +                    source_workspace_id=workspace_id,
    
    649
    +                    account=open_chat['account'],
    
    650
    +                    jid=open_chat['jid'])
    
    651
    +                self.activate_action('move-chat-to-workspace',
    
    652
    +                                     params.to_variant())
    
    653
    +
    
    654
    +            if was_active:
    
    655
    +                self.activate_workspace(new_workspace_id)
    
    656
    +
    
    657
    +            self._workspace_side_bar.remove_workspace(workspace_id)
    
    658
    +            self._chat_page.remove_chat_list(workspace_id)
    
    659
    +            app.settings.remove_workspace(workspace_id)
    
    660
    +
    
    661
    +        if open_chats:
    
    662
    +            ConfirmationDialog(
    
    663
    +                _('Remove Workspace'),
    
    664
    +                _('Remove Workspace'),
    
    665
    +                _('This workspace contains chats. All chats will be moved to '
    
    666
    +                  'the next workspace. Remove anyway?'),
    
    667
    +                [DialogButton.make('Cancel',
    
    668
    +                                   text=_('_No')),
    
    669
    +                 DialogButton.make('Remove',
    
    670
    +                                   callback=_continue_removing_workspace)]
    
    671
    +            ).show()
    
    633 672
                 return
    
    634 673
     
    
    635
    -        if was_active:
    
    636
    -            new_active_id = self._workspace_side_bar.get_first_workspace()
    
    637
    -            self.activate_workspace(new_active_id)
    
    638
    -
    
    639
    -        self._chat_page.remove_chat_list(workspace_id)
    
    640
    -        app.settings.remove_workspace(workspace_id)
    
    674
    +        # No chats in chat list, it is save to remove this workspace
    
    675
    +        _continue_removing_workspace()
    
    641 676
     
    
    642 677
         def _activate_workspace(self,
    
    643 678
                                 _action: Gio.SimpleAction,
    

  • gajim/gtk/menus.py
    ... ... @@ -410,6 +410,7 @@ def get_chat_list_row_menu(workspace_id: str,
    410 410
         menu = GajimMenu()
    
    411 411
     
    
    412 412
         params = ChatListEntryParam(workspace_id=workspace_id,
    
    413
    +                                source_workspace_id='',
    
    413 414
                                     account=account,
    
    414 415
                                     jid=jid)
    
    415 416
     
    
    ... ... @@ -423,6 +424,7 @@ def get_chat_list_row_menu(workspace_id: str,
    423 424
                 submenu.add_item(name, 'win.move-chat-to-workspace', params)
    
    424 425
     
    
    425 426
         params = ChatListEntryParam(workspace_id='',
    
    427
    +                                source_workspace_id='',
    
    426 428
                                     account=account,
    
    427 429
                                     jid=jid)
    
    428 430
     
    
    ... ... @@ -450,6 +452,7 @@ def get_workspace_params(current_workspace_id: str,
    450 452
                 continue
    
    451 453
             name = app.settings.get_workspace_setting(workspace_id, 'name')
    
    452 454
             params = ChatListEntryParam(workspace_id=workspace_id,
    
    455
    +                                    source_workspace_id=current_workspace_id,
    
    453 456
                                         account=account,
    
    454 457
                                         jid=jid)
    
    455 458
             yield name, params
    
    ... ... @@ -577,9 +580,12 @@ def get_format_menu() -> GajimMenu:
    577 580
     def get_workspace_menu(workspace_id: str) -> GajimMenu:
    
    578 581
         menuitems: MenuItemListT = [
    
    579 582
             (_('Edit…'), 'win.edit-workspace', f'"{workspace_id}"'),
    
    580
    -        (_('Remove'), 'win.remove-workspace', f'"{workspace_id}"'),
    
    581 583
         ]
    
    582 584
     
    
    585
    +    if len(app.settings.get_workspaces()) > 1:
    
    586
    +        menuitems.append(
    
    587
    +            (_('Remove'), 'win.remove-workspace', f'"{workspace_id}"'))
    
    588
    +
    
    583 589
         return GajimMenu.from_list(menuitems)
    
    584 590
     
    
    585 591
     
    

  • gajim/gtk/structs.py
    ... ... @@ -55,6 +55,7 @@ class AddChatActionParams(VariantMixin):
    55 55
     @dataclass
    
    56 56
     class ChatListEntryParam(VariantMixin):
    
    57 57
         workspace_id: str
    
    58
    +    source_workspace_id: str
    
    58 59
         account: str
    
    59 60
         jid: JID
    
    60 61
     
    

  • gajim/gtk/workspace_dialog.py
    ... ... @@ -24,8 +24,6 @@
    24 24
     
    
    25 25
     from .avatar import make_workspace_avatar
    
    26 26
     from .avatar_selector import AvatarSelector
    
    27
    -from .dialogs import ConfirmationDialog
    
    28
    -from .dialogs import DialogButton
    
    29 27
     from .builder import get_builder
    
    30 28
     from .util import rgba_to_float
    
    31 29
     from .util import make_rgba
    
    ... ... @@ -93,25 +91,9 @@ def _on_key_press(self, _widget: Gtk.Widget, event: Gdk.EventKey) -> None:
    93 91
                 self.destroy()
    
    94 92
     
    
    95 93
         def _on_remove_workspace(self, _button: Gtk.Button) -> None:
    
    96
    -        def _on_remove():
    
    97
    -            assert self._workspace_id is not None
    
    98
    -            app.window.remove_workspace(self._workspace_id)
    
    99
    -            self.destroy()
    
    100 94
             assert self._workspace_id is not None
    
    101
    -        chat_list = app.window.get_chat_list(self._workspace_id)
    
    102
    -        open_chats = chat_list.get_open_chats()
    
    103
    -        if len(open_chats) > 0:
    
    104
    -            ConfirmationDialog(
    
    105
    -                _('Remove Workspace'),
    
    106
    -                _('Remove Workspace'),
    
    107
    -                _('This workspace contains chats. Remove anyway?'),
    
    108
    -                [DialogButton.make('Cancel',
    
    109
    -                                   text=_('_No')),
    
    110
    -                 DialogButton.make('Remove',
    
    111
    -                                   callback=_on_remove)]).show()
    
    112
    -            return
    
    113
    -        # No chats in chat list, it is save to remove workspace
    
    114
    -        _on_remove()
    
    95
    +        app.window.remove_workspace(self._workspace_id)
    
    96
    +        self.destroy()
    
    115 97
     
    
    116 98
         def _on_cancel(self, _button: Gtk.Button) -> None:
    
    117 99
             self.destroy()
    

  • gajim/gtk/workspace_side_bar.py
    ... ... @@ -158,6 +158,7 @@ def _process_chat_list_drop(self,
    158 158
                 workspace_id = ''
    
    159 159
     
    
    160 160
             params = ChatListEntryParam(workspace_id=workspace_id,
    
    161
    +                                    source_workspace_id='',
    
    161 162
                                         account=account,
    
    162 163
                                         jid=jid)
    
    163 164
             app.window.activate_action('move-chat-to-workspace',
    
    ... ... @@ -221,12 +222,18 @@ def store_workspace_order(self) -> None:
    221 222
             order.remove('add')
    
    222 223
             app.settings.set_app_setting('workspace_order', order)
    
    223 224
     
    
    224
    -    def remove_workspace(self, workspace_id: str) -> bool:
    
    225
    -        if len(self._workspaces) == 1:
    
    226
    -            return False
    
    225
    +    def remove_workspace(self, workspace_id: str) -> None:
    
    227 226
             row = self._workspaces.pop(workspace_id)
    
    228 227
             self.remove(row)
    
    229
    -        return True
    
    228
    +
    
    229
    +    def get_other_workspace(self,
    
    230
    +                            exclude_workspace_id: str
    
    231
    +                            ) -> Optional[str]:
    
    232
    +
    
    233
    +        for workspace in self._workspaces.values():
    
    234
    +            if workspace.workspace_id != exclude_workspace_id:
    
    235
    +                return workspace.workspace_id
    
    236
    +        return None
    
    230 237
     
    
    231 238
         def activate_workspace(self, workspace_id: str) -> None:
    
    232 239
             row = cast(CommonWorkspace, self.get_selected_row())