[fractal/robust-prepared-handler: 3/3] Make "prepared" signal handling more robust




commit 667446f0a3ba9db1b3534bde2cd0fbc9da241127
Author: Alejandro Domínguez <adomu net-c com>
Date:   Wed Jul 14 19:32:31 2021 +0200

    Make "prepared" signal handling more robust

 src/login.rs       | 32 ++++++++++++++++++--------------
 src/session/mod.rs |  1 +
 src/window.rs      |  3 ++-
 3 files changed, 21 insertions(+), 15 deletions(-)
---
diff --git a/src/login.rs b/src/login.rs
index 98a7bdc9..b226bb97 100644
--- a/src/login.rs
+++ b/src/login.rs
@@ -127,20 +127,7 @@ impl Login {
 
         let session = Session::new();
 
-        session.connect_prepared(clone!(@weak self as obj, @strong session => move |_| {
-            if let Some(error) = session.get_error() {
-                let error_message = &imp::Login::from_instance(&obj).error_message;
-                error_message.set_text(&error.to_string());
-                error_message.show();
-                debug!("Failed to create a new session: {:?}", error);
-
-                obj.unfreeze();
-            } else {
-                debug!("A new session was prepared");
-                obj.emit_by_name("new-session", &[&session]).unwrap();
-                obj.clean();
-            }
-        }));
+        self.set_handler_for_prepared_session(&session);
 
         session.login_with_password(
             build_homeserver_url(homeserver.as_str()).unwrap(),
@@ -193,6 +180,23 @@ impl Login {
     pub fn default_widget(&self) -> gtk::Widget {
         imp::Login::from_instance(&self).next_button.get().upcast()
     }
+
+    pub fn set_handler_for_prepared_session(&self, session: &Session) {
+        session.connect_prepared(clone!(@weak self as login => move |session| {
+            if let Some(error) = session.get_error() {
+                let error_message = &imp::Login::from_instance(&login).error_message;
+                error_message.set_text(&error.to_string());
+                error_message.show();
+                debug!("Failed to create a new session: {:?}", error);
+
+                login.unfreeze();
+            } else {
+                debug!("A new session was prepared");
+                login.emit_by_name("new-session", &[&session]).unwrap();
+                login.clean();
+            }
+        }));
+    }
 }
 
 fn build_homeserver_url(server: &str) -> Result<Url, ParseError> {
diff --git a/src/session/mod.rs b/src/session/mod.rs
index 10512fc5..3101d45c 100644
--- a/src/session/mod.rs
+++ b/src/session/mod.rs
@@ -419,6 +419,7 @@ impl Session {
     }
 
     pub fn connect_prepared<F: Fn(&Self) + 'static>(&self, f: F) -> glib::SignalHandlerId {
+        log::debug!("Prepared!");
         self.connect_local("prepared", true, move |values| {
             let obj = values[0].get::<Self>().unwrap();
 
diff --git a/src/window.rs b/src/window.rs
index d51a4282..91c85dee 100644
--- a/src/window.rs
+++ b/src/window.rs
@@ -130,10 +130,11 @@ impl Window {
     fn restore_sessions(&self) {
         match secret::restore_sessions() {
             Ok(sessions) => {
+                let login = &imp::Window::from_instance(self).login.get();
                 for stored_session in sessions {
                     let session = Session::new();
+                    login.set_handler_for_prepared_session(&session);
                     session.login_with_previous_session(stored_session);
-                    self.add_session(&session);
                 }
 
                 self.switch_to_sessions_page();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]