room-list: Port to glib::Properties macro

This commit is contained in:
Kévin Commaille 2023-12-12 20:24:12 +01:00
parent deaee7393d
commit f1a923f402
No known key found for this signature in database
GPG Key ID: 29A48C1F03620416
4 changed files with 36 additions and 58 deletions

View File

@ -24,12 +24,13 @@ use crate::{
mod imp {
use std::cell::RefCell;
use glib::{object::WeakRef, subclass::Signal};
use glib::subclass::Signal;
use once_cell::sync::Lazy;
use super::*;
#[derive(Debug, Default)]
#[derive(Debug, Default, glib::Properties)]
#[properties(wrapper_type = super::RoomList)]
pub struct RoomList {
/// The list of rooms.
pub list: RefCell<IndexMap<OwnedRoomId, Room>>,
@ -38,7 +39,9 @@ mod imp {
/// The list of rooms that were upgraded and for which we haven't joined
/// the successor yet.
pub tombstoned_rooms: RefCell<HashSet<OwnedRoomId>>,
pub session: WeakRef<Session>,
/// The current session.
#[property(get, construct_only)]
pub session: glib::WeakRef<Session>,
/// The rooms metainfo that allow to restore the RoomList in its
/// previous state.
///
@ -54,31 +57,8 @@ mod imp {
type Interfaces = (gio::ListModel,);
}
#[glib::derived_properties]
impl ObjectImpl for RoomList {
fn properties() -> &'static [glib::ParamSpec] {
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
vec![glib::ParamSpecObject::builder::<Session>("session")
.construct_only()
.build()]
});
PROPERTIES.as_ref()
}
fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
match pspec.name() {
"session" => self.obj().set_session(value.get().ok().as_ref()),
_ => unimplemented!(),
}
}
fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
match pspec.name() {
"session" => self.obj().session().to_value(),
_ => unimplemented!(),
}
}
fn signals() -> &'static [Signal] {
static SIGNALS: Lazy<Vec<Signal>> =
Lazy::new(|| vec![Signal::builder("pending-rooms-changed").build()]);
@ -128,21 +108,6 @@ impl RoomList {
glib::Object::builder().property("session", session).build()
}
/// The ancestor session.
pub fn session(&self) -> Session {
self.imp().session.upgrade().unwrap()
}
/// Set the ancestor session.
fn set_session(&self, session: Option<&Session>) {
let Some(session) = session else {
return;
};
let imp = self.imp();
imp.session.set(Some(session));
}
/// Get a snapshot of the rooms list.
pub fn snapshot(&self) -> Vec<Room> {
self.imp().list.borrow().values().cloned().collect()
@ -288,8 +253,10 @@ impl RoomList {
}
pub fn handle_response_rooms(&self, rooms: ResponseRooms) {
let Some(session) = self.session() else {
return;
};
let imp = self.imp();
let session = self.session();
let mut new_rooms = HashMap::new();
@ -349,7 +316,10 @@ impl RoomList {
identifier: OwnedRoomOrAliasId,
via: Vec<OwnedServerName>,
) -> Result<(), String> {
let client = self.session().client();
let Some(session) = self.session() else {
return Err("Failed to upgrade Session".to_owned());
};
let client = session.client();
let identifier_clone = identifier.clone();
self.pending_rooms_insert(identifier.clone());

View File

@ -29,7 +29,9 @@ impl RoomListMetainfo {
/// Load the rooms and their metainfo from the store.
pub async fn load_rooms(&self) -> IndexMap<OwnedRoomId, Room> {
let session = self.room_list().session();
let Some(session) = self.room_list().session() else {
return IndexMap::new();
};
let client = session.client();
let room_ids = client
.rooms()
@ -139,6 +141,9 @@ impl RoomListMetainfoInner {
/// Persist the metainfo in the store.
async fn persist(&self, rooms_metainfo: &RoomsMetainfoMap) {
let Some(session) = self.room_list().session() else {
return;
};
let value = match serde_json::to_vec(rooms_metainfo) {
Ok(value) => value,
Err(error) => {
@ -147,7 +152,7 @@ impl RoomListMetainfoInner {
}
};
let client = self.room_list().session().client();
let client = session.client();
let handle = spawn_tokio!(async move {
client
.store()

View File

@ -79,21 +79,22 @@ mod imp {
self.parent_constructed();
let obj = self.obj();
self.avatar_data
.set(AvatarData::with_image(AvatarImage::new(
&obj.room_list().session(),
None,
AvatarUriSource::Room,
)))
.unwrap();
let avatar_data = if let Some(session) = obj.room_list().session() {
AvatarData::with_image(AvatarImage::new(&session, None, AvatarUriSource::Room))
} else {
AvatarData::new()
};
self.avatar_data.set(avatar_data).unwrap();
obj.room_list()
.connect_pending_rooms_changed(clone!(@weak obj => move |_| {
if let Some(matrix_public_room) = obj.matrix_public_room() {
obj.set_pending(obj.room_list().session()
.room_list()
let Some(matrix_public_room) = obj.matrix_public_room() else {
return;
};
obj.set_pending(obj.room_list()
.is_pending_room((*matrix_public_room.room_id).into()));
}
}));
}

View File

@ -221,10 +221,12 @@ impl PublicRoomRow {
return;
};
let room_list = public_room.room_list();
let Some(session) = room_list.session() else {
return;
};
if let Some(room) = public_room.room() {
if let Some(window) = self.root().and_downcast::<Window>() {
let session = room_list.session();
window.show_room(session.session_id(), room.room_id());
}
} else if let Some(matrix_public_room) = public_room.matrix_public_room() {