From e38b0b2bff4a44fe2c772e90f2471acf2b87d9ef Mon Sep 17 00:00:00 2001
From: Nico Alt <nicoalt@posteo.org>
Date: Thu, 17 Dec 2020 16:40:14 +0100
Subject: [PATCH] Put more responsibilities into MainWindowController

---
 briar-gtk/briar_gtk/actions/window.py         |   2 +-
 .../briar_gtk/controllers/main_window.py      | 132 ++++++++++++++++++
 briar-gtk/briar_gtk/views/main_window.py      | 102 --------------
 briar-gtk/briar_gtk/views/sidebar.py          |   1 -
 briar-gtk/briar_gtk/window.py                 |   1 +
 5 files changed, 134 insertions(+), 104 deletions(-)

diff --git a/briar-gtk/briar_gtk/actions/window.py b/briar-gtk/briar_gtk/actions/window.py
index 40cb3a4..345270f 100644
--- a/briar-gtk/briar_gtk/actions/window.py
+++ b/briar-gtk/briar_gtk/actions/window.py
@@ -93,4 +93,4 @@ class WindowActions(Actions):
 
     # pylint: disable=unused-argument
     def _open_private_chat(self, action, contact_id):
-        self.widget.current_container.open_private_chat(contact_id.get_int32())
+        self.widget.current_controller.open_private_chat(contact_id.get_int32())
diff --git a/briar-gtk/briar_gtk/controllers/main_window.py b/briar-gtk/briar_gtk/controllers/main_window.py
index 11dca81..445c617 100644
--- a/briar-gtk/briar_gtk/controllers/main_window.py
+++ b/briar-gtk/briar_gtk/controllers/main_window.py
@@ -2,6 +2,9 @@
 # SPDX-License-Identifier: AGPL-3.0-only
 # License-Filename: LICENSE.md
 
+from briar_wrapper.models.contacts import Contacts
+
+from briar_gtk.containers.private_chat import PrivateChatContainer
 from briar_gtk.controllers.main_menu import MainMenuController
 from briar_gtk.controllers.private_chat import PrivateChatController
 from briar_gtk.controllers.sidebar import SidebarController
@@ -16,8 +19,17 @@ class MainWindowController():
     def __init__(self, main_window_view, builder):
         self._main_window_view = main_window_view
         self._builder = builder
+        self._signals = list()
 
         self._setup_children()
+        self._load_content()
+        self._setup_destroy_listener()
+
+    def open_private_chat(self, contact_id):
+        contact_name = self._get_contact_name(contact_id)
+        self._prepare_chat_view(contact_name)
+        self._setup_private_chat_widget(contact_name, contact_id)
+        self._current_contact_id = contact_id
 
     def _setup_children(self):
         self._sidebar_view = SidebarView(self._builder)
@@ -31,3 +43,123 @@ class MainWindowController():
         self._main_menu_view = MainMenuView()
         self._main_menu_controller = MainMenuController(
             self._main_menu_view, APP().api)
+
+    def _get_contact_name(self, contact_id):
+        name = ""
+        for contact in Contacts(APP().api).get():
+            if contact["contactId"] == contact_id:
+                name = contact["author"]["name"]
+                if "alias" in contact:
+                    name = contact["alias"]
+                break
+        return name
+
+    def _prepare_chat_view(self, contact_name):
+        main_content_stack = self._builder.get_object("main_content_stack")
+        chat_placeholder = main_content_stack.get_child_by_name(
+            "chat_placeholder")
+        if self._no_chat_opened():
+            chat_placeholder.hide()
+        else:
+            self._clear_history_container()
+
+        chat_view = main_content_stack.get_child_by_name("chat_view")
+        chat_view.show()
+        main_window_leaflet = self._builder.get_object("main_window_leaflet")
+        main_content_container = self._builder.get_object(
+            "main_content_container")
+        main_window_leaflet.set_visible_child(main_content_container)
+        contact_name_label = self._builder.get_object("contact_name")
+        contact_name_label.set_text(contact_name)
+        self._builder.get_object("chat_menu_button").show()
+
+    def _no_chat_opened(self):
+        main_content_stack = self._builder.get_object("main_content_stack")
+        chat_placeholder = main_content_stack.get_child_by_name(
+            "chat_placeholder")
+        return chat_placeholder.get_visible()
+
+    def _clear_history_container(self):
+        history_container = self._builder.get_object("history_container")
+        children = history_container.get_children()
+        for child in children:
+            child.destroy()
+        if hasattr(self, "_selected_contact"):
+            del self._selected_contact
+
+    # pylint: disable=no-member
+    def _load_content(self):
+        socket_listener = APP().api.socket_listener
+        self._setup_contact_added_listeners(socket_listener)
+        self._setup_message_received_listeners(socket_listener)
+
+    def _setup_contact_added_listeners(self, socket_listener):
+        signal_id = socket_listener.connect("ContactAddedEvent",
+                                            self._notify_contact_added)
+        self._signals.append(signal_id)
+
+    def _setup_message_received_listeners(self, socket_listener):
+        signal_id = socket_listener.connect("ConversationMessageReceivedEvent",
+                                            self._notify_message_received)
+        self._signals.append(signal_id)
+
+    # pylint: disable=unused-argument
+    def _notify_contact_added(self, message):
+        self._notify(
+            _("Contact added"),  # context: "Notification"
+            NOTIFICATION_CONTACT_ADDED
+        )
+
+    # pylint: disable=unused-argument
+    def _notify_message_received(self, message):
+        self._notify(
+            _("New private message"),  # context: "Notification"
+            NOTIFICATION_PRIVATE_MESSAGE
+        )
+
+    @staticmethod
+    def _notify(title, identifier):
+        if APP().window.is_active():
+            return
+        notification = Gio.Notification.new(title)
+        notification.set_priority(Gio.NotificationPriority.HIGH)
+        APP().send_notification(identifier, notification)
+
+    def _setup_destroy_listener(self):
+        self._main_window_view.connect("destroy", self._on_destroy)
+
+    # pylint: disable=unused-argument
+    def _on_destroy(self, widget):
+        self._disconnect_signals()
+
+    def _disconnect_signals(self):
+        self._sidebar_controller.disconnect_signals()
+        for signal in self._signals:
+            APP().api.socket_listener.disconnect(signal)
+
+    def _setup_private_chat_widget(self, contact_name, contact_id):
+        self._current_private_chat_widget = PrivateChatContainer(
+            contact_name, contact_id)
+        history_container = self._builder.get_object("history_container")
+        history_container.add(self._current_private_chat_widget)
+        history_container.show_all()
+
+        self._disconnect_chat_entry_signals()
+        chat_entry = self._builder.get_object("chat_entry")
+        self._chat_entry_signal_id = chat_entry.connect(
+            "activate", self._on_chat_entry_activate
+        )
+        chat_entry.grab_focus()
+
+    def _disconnect_chat_entry_signals(self):
+        if not hasattr(self, "_chat_entry_signal_id"):
+            return
+        chat_entry = self._builder.get_object("chat_entry")
+        chat_entry.disconnect(self._chat_entry_signal_id)
+        del self._chat_entry_signal_id
+
+    def _on_chat_entry_activate(self, widget):
+        if len(widget.get_text()) == 0:
+            return
+        self._current_private_chat_widget.send_message(widget)
+        self._refresh_contacts()
diff --git a/briar-gtk/briar_gtk/views/main_window.py b/briar-gtk/briar_gtk/views/main_window.py
index 88489bb..75e1756 100644
--- a/briar-gtk/briar_gtk/views/main_window.py
+++ b/briar-gtk/briar_gtk/views/main_window.py
@@ -33,10 +33,8 @@ class MainWindowView(Gtk.Overlay):
     def __init__(self, builder):
         super().__init__()
         self._builder = builder
-        self._signals = list()
 
         self._setup_view()
-        self._load_content()
 
     @property
     def main_window_leaflet(self):
@@ -79,12 +77,6 @@ class MainWindowView(Gtk.Overlay):
         about_dialog = AboutDialogWidget()
         about_dialog.show()
 
-    def open_private_chat(self, contact_id):
-        contact_name = self._get_contact_name(contact_id)
-        self._prepare_chat_view(contact_name)
-        self._setup_private_chat_widget(contact_name, contact_id)
-        self._current_contact_id = contact_id
-
     def show_sidebar(self):
         self.main_window_leaflet.set_visible_child(
             self._builder.get_object("sidebar_box"))
@@ -168,55 +160,12 @@ class MainWindowView(Gtk.Overlay):
             self.show_sidebar()
         widget.destroy()
 
-    def _prepare_chat_view(self, contact_name):
-        if self._no_chat_opened():
-            self.chat_placeholder.hide()
-        else:
-            self._clear_history_container()
-
-        self.chat_view.show()
-        self.main_window_leaflet.set_visible_child(
-            self.main_content_container)
-        self.contact_name_label.set_text(contact_name)
-        self._builder.get_object("chat_menu_button").show()
-
-    def _setup_private_chat_widget(self, contact_name, contact_id):
-        self._current_private_chat_widget = PrivateChatContainer(
-            contact_name, contact_id)
-        self.history_container.add(self._current_private_chat_widget)
-        self.history_container.show_all()
-
-        self._disconnect_chat_entry_signals()
-        self._chat_entry_signal_id = self.chat_entry.connect(
-            "activate", self._on_chat_entry_activate
-        )
-        self.chat_entry.grab_focus()
-
-    def _on_chat_entry_activate(self, widget):
-        if len(widget.get_text()) == 0:
-            return
-        self._current_private_chat_widget.send_message(widget)
-        self._refresh_contacts()
-
     def _disconnect_chat_entry_signals(self):
         if not hasattr(self, "_chat_entry_signal_id"):
             return
         self.chat_entry.disconnect(self._chat_entry_signal_id)
         del self._chat_entry_signal_id
 
-    def _no_chat_opened(self):
-        return self.chat_placeholder.get_visible()
-
-    def _get_contact_name(self, contact_id):
-        name = ""
-        for contact in Contacts(APP().api).get():
-            if contact["contactId"] == contact_id:
-                name = contact["author"]["name"]
-                if "alias" in contact:
-                    name = contact["alias"]
-                break
-        return name
-
     def _clear_history_container(self):
         children = self.history_container.get_children()
         for child in children:
@@ -227,7 +176,6 @@ class MainWindowView(Gtk.Overlay):
     def _setup_view(self):
         self._setup_main_window_stack()
         self._setup_headerbar_stack_holder()
-        self._setup_destroy_listener()
 
     def _setup_main_window_stack(self):
         main_window_stack = self._builder.get_object("main_window_stack")
@@ -240,55 +188,5 @@ class MainWindowView(Gtk.Overlay):
         headerbar_stack_holder.show_all()
         APP().window.set_titlebar(headerbar_stack_holder)
 
-    def _setup_destroy_listener(self):
-        self.connect("destroy", self._on_destroy)
-
-    # pylint: disable=unused-argument
-    def _on_destroy(self, widget):
-        self._disconnect_signals()
-
-    def _disconnect_signals(self):
-        self._sidebar_controller.disconnect_signals()
-        for signal in self._signals:
-            APP().api.socket_listener.disconnect(signal)
-
-    # pylint: disable=no-member
-    def _load_content(self):
-        socket_listener = APP().api.socket_listener
-        self._setup_contact_added_listeners(socket_listener)
-        self._setup_message_received_listeners(socket_listener)
-
-    def _setup_contact_added_listeners(self, socket_listener):
-        signal_id = socket_listener.connect("ContactAddedEvent",
-                                            self._notify_contact_added)
-        self._signals.append(signal_id)
-
-    def _setup_message_received_listeners(self, socket_listener):
-        signal_id = socket_listener.connect("ConversationMessageReceivedEvent",
-                                            self._notify_message_received)
-        self._signals.append(signal_id)
-
-    # pylint: disable=unused-argument
-    def _notify_contact_added(self, message):
-        self._notify(
-            _("Contact added"),  # context: "Notification"
-            NOTIFICATION_CONTACT_ADDED
-        )
-
-    # pylint: disable=unused-argument
-    def _notify_message_received(self, message):
-        self._notify(
-            _("New private message"),  # context: "Notification"
-            NOTIFICATION_PRIVATE_MESSAGE
-        )
-
-    @staticmethod
-    def _notify(title, identifier):
-        if APP().window.is_active():
-            return
-        notification = Gio.Notification.new(title)
-        notification.set_priority(Gio.NotificationPriority.HIGH)
-        APP().send_notification(identifier, notification)
-
     def _refresh_contacts(self):
         self._sidebar_controller.refresh_contacts()
diff --git a/briar-gtk/briar_gtk/views/sidebar.py b/briar-gtk/briar_gtk/views/sidebar.py
index 36672cc..5fb2372 100644
--- a/briar-gtk/briar_gtk/views/sidebar.py
+++ b/briar-gtk/briar_gtk/views/sidebar.py
@@ -19,7 +19,6 @@ class SidebarView():
         self._clear_contact_list()
 
         for contact in contact_list:
-            print(contact)
             contact_row = ContactRowWidget(contact)
             contacts_list_box = self._builder.get_object("contacts_list_box")
             contacts_list_box.add(contact_row)
diff --git a/briar-gtk/briar_gtk/window.py b/briar-gtk/briar_gtk/window.py
index e642a76..c12de54 100644
--- a/briar-gtk/briar_gtk/window.py
+++ b/briar-gtk/briar_gtk/window.py
@@ -107,6 +107,7 @@ class Window(Gtk.ApplicationWindow):
         main_window_controller = MainWindowController(
             main_window_view, builder)
         self._setup_container(main_window_view)
+        self.current_controller = main_window_controller
 
     def _setup_add_contact_container(self):
         self._setup_container(AddContactContainer())
-- 
GitLab