Daniel Brötzmann pushed to branch move-workspace-chats-on-remove at gajim / gajim
Commits: 6a2a3eca by wurstsalat at 2022-08-28T12:16:08+02:00 imprv: Workspaces: Move chats when removing workspace
Closes #11098
- - - - -
6 changed files:
- gajim/gtk/chat_list_stack.py - gajim/gtk/main.py - gajim/gtk/menus.py - gajim/gtk/structs.py - gajim/gtk/workspace_dialog.py - gajim/gtk/workspace_side_bar.py
Changes:
===================================== gajim/gtk/chat_list_stack.py ===================================== @@ -243,16 +243,21 @@ def _move_chat_to_workspace(self, 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
===================================== gajim/gtk/main.py ===================================== @@ -62,6 +62,7 @@ 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 @@ def _remove_workspace(self, 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,
===================================== 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)
===================================== 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
===================================== gajim/gtk/workspace_dialog.py ===================================== @@ -24,8 +24,6 @@
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 @@ def _on_key_press(self, _widget: Gtk.Widget, event: Gdk.EventKey) -> None: 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()
===================================== gajim/gtk/workspace_side_bar.py ===================================== @@ -158,6 +158,7 @@ def _process_chat_list_drop(self, 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 @@ def store_workspace_order(self) -> None: 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())
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/commit/6a2a3ecaa311ca489720a5376d1899237...