diff options
author | wurstsalat <mailtrash@posteo.de> | 2022-08-19 20:41:19 +0300 |
---|---|---|
committer | wurstsalat <mailtrash@posteo.de> | 2022-08-28 13:16:08 +0300 |
commit | 6a2a3ecaa311ca489720a5376d1899237009e87f (patch) | |
tree | 595539ca53fdb68c134498b367282154def706a2 | |
parent | 8431f218054de1bfb6ffb29ecd32b24d0c735032 (diff) |
imprv: Workspaces: Move chats when removing workspacemove-workspace-chats-on-remove
Closes #11098
-rw-r--r-- | gajim/gtk/chat_list_stack.py | 13 | ||||
-rw-r--r-- | gajim/gtk/main.py | 51 | ||||
-rw-r--r-- | gajim/gtk/menus.py | 8 | ||||
-rw-r--r-- | gajim/gtk/structs.py | 1 | ||||
-rw-r--r-- | gajim/gtk/workspace_dialog.py | 22 | ||||
-rw-r--r-- | gajim/gtk/workspace_side_bar.py | 15 |
6 files changed, 73 insertions, 37 deletions
diff --git a/gajim/gtk/chat_list_stack.py b/gajim/gtk/chat_list_stack.py index 6b5c416b8..2b3b407cf 100644 --- a/gajim/gtk/chat_list_stack.py +++ b/gajim/gtk/chat_list_stack.py @@ -243,16 +243,21 @@ class ChatListStack(Gtk.Stack, EventHelper): if not workspace_id: workspace_id = app.window.add_workspace(switch=False) - current_chatlist = cast(ChatList, self.get_visible_child()) - type_ = current_chatlist.get_chat_type(params.account, params.jid) + if params.source_workspace_id: + source_chatlist = self.get_chatlist(params.source_workspace_id) + else: + source_chatlist = cast(ChatList, self.get_visible_child()) + + type_ = source_chatlist.get_chat_type(params.account, params.jid) if type_ is None: return - current_chatlist.remove_chat(params.account, params.jid) + + 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(current_chatlist.workspace_id) + self.store_open_chats(source_chatlist.workspace_id) self.store_open_chats(workspace_id) @structs.actionmethod diff --git a/gajim/gtk/main.py b/gajim/gtk/main.py index 2718b00a7..340ee3f29 100644 --- a/gajim/gtk/main.py +++ b/gajim/gtk/main.py @@ -62,6 +62,7 @@ from .util import set_urgency_hint from .structs import AccountJidParam from .structs import AddChatActionParams from .structs import actionmethod +from .structs import ChatListEntryParam if TYPE_CHECKING: from .control import ChatControl @@ -626,18 +627,52 @@ class MainWindow(Gtk.ApplicationWindow, EventHelper): self.remove_workspace(workspace_id) def remove_workspace(self, workspace_id: str) -> None: + if len(app.settings.get_workspaces()) == 1: + log.warning('Tried to remove the only workspace') + return + was_active = self.get_active_workspace() == workspace_id + chat_list = self.get_chat_list(workspace_id) + open_chats = chat_list.get_open_chats() + + def _continue_removing_workspace(): + new_workspace_id = self._workspace_side_bar.get_other_workspace( + workspace_id) + if new_workspace_id is None: + log.warning('No other workspaces found') + return - success = self._workspace_side_bar.remove_workspace(workspace_id) - if not success: + for open_chat in open_chats: + params = ChatListEntryParam( + workspace_id=new_workspace_id, + source_workspace_id=workspace_id, + account=open_chat['account'], + jid=open_chat['jid']) + self.activate_action('move-chat-to-workspace', + params.to_variant()) + + if was_active: + self.activate_workspace(new_workspace_id) + + self._workspace_side_bar.remove_workspace(workspace_id) + self._chat_page.remove_chat_list(workspace_id) + app.settings.remove_workspace(workspace_id) + + if open_chats: + ConfirmationDialog( + _('Remove Workspace'), + _('Remove Workspace'), + _('This workspace contains chats. All chats will be moved to ' + 'the next workspace. Remove anyway?'), + [DialogButton.make('Cancel', + text=_('_No')), + DialogButton.make('Remove', + callback=_continue_removing_workspace)] + ).show() return - if was_active: - new_active_id = self._workspace_side_bar.get_first_workspace() - self.activate_workspace(new_active_id) - - self._chat_page.remove_chat_list(workspace_id) - app.settings.remove_workspace(workspace_id) + # No chats in chat list, it is save to remove this workspace + _continue_removing_workspace() def _activate_workspace(self, _action: Gio.SimpleAction, diff --git a/gajim/gtk/menus.py b/gajim/gtk/menus.py index 2da96b419..fad5ef862 100644 --- a/gajim/gtk/menus.py +++ b/gajim/gtk/menus.py @@ -410,6 +410,7 @@ def get_chat_list_row_menu(workspace_id: str, menu = GajimMenu() params = ChatListEntryParam(workspace_id=workspace_id, + source_workspace_id='', account=account, jid=jid) @@ -423,6 +424,7 @@ def get_chat_list_row_menu(workspace_id: str, submenu.add_item(name, 'win.move-chat-to-workspace', params) params = ChatListEntryParam(workspace_id='', + source_workspace_id='', account=account, jid=jid) @@ -450,6 +452,7 @@ def get_workspace_params(current_workspace_id: str, continue name = app.settings.get_workspace_setting(workspace_id, 'name') params = ChatListEntryParam(workspace_id=workspace_id, + source_workspace_id=current_workspace_id, account=account, jid=jid) yield name, params @@ -577,9 +580,12 @@ def get_format_menu() -> GajimMenu: def get_workspace_menu(workspace_id: str) -> GajimMenu: menuitems: MenuItemListT = [ (_('Edit…'), 'win.edit-workspace', f'"{workspace_id}"'), - (_('Remove'), 'win.remove-workspace', f'"{workspace_id}"'), ] + if len(app.settings.get_workspaces()) > 1: + menuitems.append( + (_('Remove'), 'win.remove-workspace', f'"{workspace_id}"')) + return GajimMenu.from_list(menuitems) diff --git a/gajim/gtk/structs.py b/gajim/gtk/structs.py index 640cff1bd..2c79b5406 100644 --- a/gajim/gtk/structs.py +++ b/gajim/gtk/structs.py @@ -55,6 +55,7 @@ class AddChatActionParams(VariantMixin): @dataclass class ChatListEntryParam(VariantMixin): workspace_id: str + source_workspace_id: str account: str jid: JID diff --git a/gajim/gtk/workspace_dialog.py b/gajim/gtk/workspace_dialog.py index 595b8c393..17bb2b09d 100644 --- a/gajim/gtk/workspace_dialog.py +++ b/gajim/gtk/workspace_dialog.py @@ -24,8 +24,6 @@ from gajim.common.i18n import _ from .avatar import make_workspace_avatar from .avatar_selector import AvatarSelector -from .dialogs import ConfirmationDialog -from .dialogs import DialogButton from .builder import get_builder from .util import rgba_to_float from .util import make_rgba @@ -93,25 +91,9 @@ class WorkspaceDialog(Gtk.ApplicationWindow): self.destroy() def _on_remove_workspace(self, _button: Gtk.Button) -> None: - def _on_remove(): - assert self._workspace_id is not None - app.window.remove_workspace(self._workspace_id) - self.destroy() assert self._workspace_id is not None - chat_list = app.window.get_chat_list(self._workspace_id) - open_chats = chat_list.get_open_chats() - if len(open_chats) > 0: - ConfirmationDialog( - _('Remove Workspace'), - _('Remove Workspace'), - _('This workspace contains chats. Remove anyway?'), - [DialogButton.make('Cancel', - text=_('_No')), - DialogButton.make('Remove', - callback=_on_remove)]).show() - return - # No chats in chat list, it is save to remove workspace - _on_remove() + app.window.remove_workspace(self._workspace_id) + self.destroy() def _on_cancel(self, _button: Gtk.Button) -> None: self.destroy() diff --git a/gajim/gtk/workspace_side_bar.py b/gajim/gtk/workspace_side_bar.py index 60c7aafda..af233d072 100644 --- a/gajim/gtk/workspace_side_bar.py +++ b/gajim/gtk/workspace_side_bar.py @@ -158,6 +158,7 @@ class WorkspaceSideBar(Gtk.ListBox): workspace_id = '' params = ChatListEntryParam(workspace_id=workspace_id, + source_workspace_id='', account=account, jid=jid) app.window.activate_action('move-chat-to-workspace', @@ -221,12 +222,18 @@ class WorkspaceSideBar(Gtk.ListBox): order.remove('add') app.settings.set_app_setting('workspace_order', order) - def remove_workspace(self, workspace_id: str) -> bool: - if len(self._workspaces) == 1: - return False + def remove_workspace(self, workspace_id: str) -> None: row = self._workspaces.pop(workspace_id) self.remove(row) - return True + + def get_other_workspace(self, + exclude_workspace_id: str + ) -> Optional[str]: + + for workspace in self._workspaces.values(): + if workspace.workspace_id != exclude_workspace_id: + return workspace.workspace_id + return None def activate_workspace(self, workspace_id: str) -> None: row = cast(CommonWorkspace, self.get_selected_row()) |