window: Allow to get a Session by ID

This commit is contained in:
Kévin Commaille 2022-10-22 17:13:22 +02:00
parent 9122a7f926
commit 642790c289
No known key found for this signature in database
GPG key ID: DD507DAE96E8245C
2 changed files with 30 additions and 4 deletions

View file

@ -188,6 +188,13 @@ mod imp {
fn properties() -> &'static [glib::ParamSpec] {
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = 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<Room>) {

View file

@ -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<Session> {
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<String> {
let priv_ = self.imp();
priv_
.main_stack
.visible_child()
.filter(|child| child == priv_.sessions.upcast_ref::<gtk::Widget>())?;
priv_.sessions.visible_child_name().map(Into::into)
}
pub fn save_window_size(&self) -> Result<(), glib::BoolError> {
let settings = Application::default().settings();