fractal/session_list/
failed_session.rs

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
use std::{ops::Deref, sync::Arc};

use gtk::{glib, prelude::*, subclass::prelude::*};

use super::{SessionInfo, SessionInfoImpl};
use crate::{
    components::AvatarData, prelude::*, secret::StoredSession, utils::matrix::ClientSetupError,
};

#[derive(Clone, Debug, glib::Boxed)]
#[boxed_type(name = "BoxedClientSetupError")]
pub struct BoxedClientSetupError(pub Arc<ClientSetupError>);

impl Deref for BoxedClientSetupError {
    type Target = Arc<ClientSetupError>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

mod imp {
    use std::cell::OnceCell;

    use super::*;

    #[derive(Debug, Default, glib::Properties)]
    #[properties(wrapper_type = super::FailedSession)]
    pub struct FailedSession {
        /// The error encountered when initializing the session.
        #[property(get, construct_only)]
        error: OnceCell<BoxedClientSetupError>,
        /// The data for the avatar representation for this session.
        avatar_data: OnceCell<AvatarData>,
    }

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

    #[glib::derived_properties]
    impl ObjectImpl for FailedSession {}

    impl SessionInfoImpl for FailedSession {
        fn avatar_data(&self) -> AvatarData {
            self.avatar_data
                .get_or_init(|| {
                    let avatar_data = AvatarData::new();
                    avatar_data.set_display_name(self.obj().user_id().to_string());
                    avatar_data
                })
                .clone()
        }
    }
}

glib::wrapper! {
    /// A Matrix user session that encountered an error when initializing the client.
    pub struct FailedSession(ObjectSubclass<imp::FailedSession>)
        @extends SessionInfo;
}

impl FailedSession {
    /// Constructs a new `FailedSession` with the given info and error.
    pub fn new(stored_session: &StoredSession, error: ClientSetupError) -> Self {
        glib::Object::builder()
            .property("info", stored_session)
            .property("error", BoxedClientSetupError(Arc::new(error)))
            .build()
    }
}