diff --git a/src/session/mod.rs b/src/session/mod.rs index 99512ede..7c38401b 100644 --- a/src/session/mod.rs +++ b/src/session/mod.rs @@ -188,6 +188,13 @@ mod imp { fn properties() -> &'static [glib::ParamSpec] { static PROPERTIES: Lazy> = Lazy::new(|| { vec![ + glib::ParamSpecString::new( + "session-id", + "Session ID", + "The unique ID of this session", + None, + glib::ParamFlags::READABLE, + ), glib::ParamSpecObject::new( "item-list", "Item List", @@ -217,6 +224,7 @@ mod imp { fn property(&self, obj: &Self::Type, _id: usize, pspec: &glib::ParamSpec) -> glib::Value { match pspec.name() { + "session-id" => obj.session_id().to_value(), "item-list" => obj.item_list().to_value(), "user" => obj.user().to_value(), "offline" => obj.is_offline().to_value(), @@ -289,8 +297,8 @@ impl Session { glib::Object::new(&[]).expect("Failed to create Session") } - pub fn session_id(&self) -> &str { - self.imp().info.get().unwrap().id() + pub fn session_id(&self) -> Option<&str> { + self.imp().info.get().map(|info| info.id()) } pub fn select_room(&self, room: Option) { diff --git a/src/window.rs b/src/window.rs index c9394912..7e499a8d 100644 --- a/src/window.rs +++ b/src/window.rs @@ -176,7 +176,7 @@ impl Window { let priv_ = &self.imp(); let prev_has_sessions = self.has_sessions(); - priv_.sessions.add_child(session); + priv_.sessions.add_named(session, session.session_id()); priv_.sessions.set_visible_child(session); // We need to grab the focus so that keyboard shortcuts work session.grab_focus(); @@ -199,7 +199,7 @@ impl Window { // If the session was a new login that was logged out before being ready, go // back to the login screen. - if priv_.login.current_session_id().as_deref() == Some(session.session_id()) { + if priv_.login.current_session_id().as_deref() == session.session_id() { priv_.login.restore_client(); self.switch_to_login_page(); } else if let Some(child) = priv_.sessions.first_child() { @@ -256,6 +256,24 @@ impl Window { self.imp().sessions.pages().n_items() > 0 } + /// Get the session with the given ID. + pub fn session_by_id(&self, session_id: &str) -> Option { + self.imp() + .sessions + .child_by_name(session_id) + .and_then(|w| w.downcast().ok()) + } + + /// The ID of the currently visible session, if any. + pub fn current_session_id(&self) -> Option { + let priv_ = self.imp(); + priv_ + .main_stack + .visible_child() + .filter(|child| child == priv_.sessions.upcast_ref::())?; + priv_.sessions.visible_child_name().map(Into::into) + } + pub fn save_window_size(&self) -> Result<(), glib::BoolError> { let settings = Application::default().settings();