1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use gtk::{glib, glib::clone, prelude::*, subclass::prelude::*};
use matrix_sdk::ruma::directory::PublicRoomsChunk;

use crate::{
    components::{AvatarData, AvatarImage, AvatarUriSource},
    session::model::{Room, RoomList},
};

mod imp {
    use std::cell::{Cell, OnceCell, RefCell};

    use glib::signal::SignalHandlerId;

    use super::*;

    #[derive(Debug, Default, glib::Properties)]
    #[properties(wrapper_type = super::PublicRoom)]
    pub struct PublicRoom {
        /// The list of rooms in this session.
        #[property(get, construct_only)]
        pub room_list: OnceCell<RoomList>,
        /// The server that returned the room.
        #[property(get, construct_only)]
        pub server: OnceCell<String>,
        pub matrix_public_room: OnceCell<PublicRoomsChunk>,
        /// The [`AvatarData`] of this room.
        #[property(get)]
        pub avatar_data: OnceCell<AvatarData>,
        /// The `Room` object for this room, if the user is already a member of
        /// this room.
        #[property(get)]
        pub room: RefCell<Option<Room>>,
        /// Whether the room is pending.
        ///
        /// A room is pending when the user clicked to join it.
        #[property(get)]
        pub pending: Cell<bool>,
        pub room_handler: RefCell<Option<SignalHandlerId>>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for PublicRoom {
        const NAME: &'static str = "PublicRoom";
        type Type = super::PublicRoom;
    }

    #[glib::derived_properties]
    impl ObjectImpl for PublicRoom {
        fn constructed(&self) {
            self.parent_constructed();
            let obj = self.obj();

            let avatar_data = if let Some(session) = obj.room_list().session() {
                AvatarData::with_image(AvatarImage::new(
                    &session,
                    AvatarUriSource::Room,
                    None,
                    None,
                ))
            } else {
                AvatarData::new()
            };

            self.avatar_data.set(avatar_data).unwrap();

            obj.room_list().connect_pending_rooms_changed(clone!(
                #[weak]
                obj,
                move |_| {
                    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()),
                    );
                }
            ));
        }

        fn dispose(&self) {
            if let Some(handler_id) = self.room_handler.take() {
                self.obj().room_list().disconnect(handler_id);
            }
        }
    }
}

glib::wrapper! {
    /// A room in a homeserver's public directory.
    pub struct PublicRoom(ObjectSubclass<imp::PublicRoom>);
}

impl PublicRoom {
    pub fn new(room_list: &RoomList, server: &str) -> Self {
        glib::Object::builder()
            .property("room-list", room_list)
            .property("server", server)
            .build()
    }

    /// Set the `Room` object for this room.
    fn set_room(&self, room: Room) {
        self.imp().room.replace(Some(room));
        self.notify_room();
    }

    /// Set whether this room is pending.
    fn set_pending(&self, pending: bool) {
        if self.pending() == pending {
            return;
        }

        self.imp().pending.set(pending);
        self.notify_pending();
    }

    pub fn set_matrix_public_room(&self, room: PublicRoomsChunk) {
        let imp = self.imp();

        if let Some(display_name) = room.name.clone() {
            self.avatar_data().set_display_name(display_name);
        }
        self.avatar_data()
            .image()
            .unwrap()
            .set_uri_and_info(room.avatar_url.clone(), None);

        if let Some(room) = self.room_list().get(&room.room_id) {
            self.set_room(room);
        } else {
            let room_id = room.room_id.clone();
            let handler_id = self.room_list().connect_items_changed(clone!(
                #[weak(rename_to = obj)]
                self,
                move |room_list, _, _, _| {
                    if let Some(room) = room_list.get(&room_id) {
                        if let Some(handler_id) = obj.imp().room_handler.take() {
                            obj.set_room(room);
                            room_list.disconnect(handler_id);
                        }
                    }
                }
            ));

            imp.room_handler.replace(Some(handler_id));
        }

        self.set_pending(self.room_list().is_pending_room((*room.room_id).into()));

        imp.matrix_public_room.set(room).unwrap();
    }

    pub fn matrix_public_room(&self) -> Option<&PublicRoomsChunk> {
        self.imp().matrix_public_room.get()
    }

    /// The display name for this room.
    ///
    /// Returns an empty string if there is no matrix public room.
    pub fn display_name(&self) -> String {
        let Some(matrix_public_room) = self.matrix_public_room() else {
            return String::new();
        };

        matrix_public_room
            .name
            .as_deref()
            .or(matrix_public_room
                .canonical_alias
                .as_ref()
                .map(|a| a.as_str()))
            .unwrap_or_else(|| matrix_public_room.room_id.as_str())
            .to_owned()
    }
}