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
use gtk::{glib, prelude::*, subclass::prelude::*};
use ruma::{OwnedDeviceId, OwnedUserId};
use url::Url;

use crate::{components::AvatarData, secret::StoredSession};

mod imp {
    use std::{cell::OnceCell, marker::PhantomData};

    use super::*;

    #[repr(C)]
    pub struct SessionInfoClass {
        pub parent_class: glib::object::ObjectClass,
        pub avatar_data: fn(&super::SessionInfo) -> AvatarData,
    }

    unsafe impl ClassStruct for SessionInfoClass {
        type Type = SessionInfo;
    }

    pub(super) fn session_info_avatar_data(this: &super::SessionInfo) -> AvatarData {
        let klass = this.class();
        (klass.as_ref().avatar_data)(this)
    }

    #[derive(Debug, Default, glib::Properties)]
    #[properties(wrapper_type = super::SessionInfo)]
    pub struct SessionInfo {
        /// The Matrix session's info.
        #[property(get, construct_only)]
        pub info: OnceCell<StoredSession>,
        /// The Matrix session's user ID, as a string.
        #[property(get = Self::user_id_string)]
        pub user_id_string: PhantomData<String>,
        /// The Matrix session's homeserver, as a string.
        #[property(get = Self::homeserver_string)]
        pub homeserver_string: PhantomData<String>,
        /// The Matrix session's device ID, as a string.
        #[property(get = Self::device_id_string)]
        pub device_id_string: PhantomData<String>,
        /// The local session's ID.
        #[property(get = Self::session_id)]
        pub session_id: PhantomData<String>,
        /// The avatar data to represent this session.
        #[property(get = Self::avatar_data)]
        pub avatar_data: PhantomData<AvatarData>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for SessionInfo {
        const NAME: &'static str = "SessionInfo";
        const ABSTRACT: bool = true;
        type Type = super::SessionInfo;
        type Class = SessionInfoClass;
    }

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

    impl SessionInfo {
        /// The Matrix session's info.
        pub fn info(&self) -> &StoredSession {
            self.info.get().unwrap()
        }

        /// The Matrix session's user ID, as a string.
        fn user_id_string(&self) -> String {
            self.info().user_id.to_string()
        }

        /// The Matrix session's homeserver, as a string.
        fn homeserver_string(&self) -> String {
            self.info().homeserver.to_string()
        }

        /// The Matrix session's device ID, as a string.
        fn device_id_string(&self) -> String {
            self.info().device_id.to_string()
        }

        /// The local session's ID.
        fn session_id(&self) -> String {
            self.info().id.clone()
        }

        /// The avatar data to represent this session.
        fn avatar_data(&self) -> AvatarData {
            session_info_avatar_data(&self.obj())
        }
    }
}

glib::wrapper! {
    /// Parent class of objects containing a Matrix session's info.
    ///
    /// Its main purpose is to be able to handle `Session`s that are being initialized, or where initialization failed.
    pub struct SessionInfo(ObjectSubclass<imp::SessionInfo>);
}

/// Public trait containing implemented methods for everything that derives from
/// `SessionInfo`.
///
/// To override the behavior of these methods, override the corresponding method
/// of `SessionInfoImpl`.
pub trait SessionInfoExt: 'static {
    /// The Matrix session's info.
    fn info(&self) -> &StoredSession;

    /// The Matrix session's user ID.
    fn user_id(&self) -> &OwnedUserId {
        &self.info().user_id
    }

    /// The Matrix session's homeserver.
    fn homeserver(&self) -> &Url {
        &self.info().homeserver
    }

    /// The Matrix session's device ID.
    fn device_id(&self) -> &OwnedDeviceId {
        &self.info().device_id
    }

    /// The local session's ID.
    fn session_id(&self) -> &str {
        &self.info().id
    }

    /// The avatar data to represent this session.
    #[allow(dead_code)]
    fn avatar_data(&self) -> AvatarData;
}

impl<O: IsA<SessionInfo>> SessionInfoExt for O {
    fn info(&self) -> &StoredSession {
        self.upcast_ref().imp().info()
    }

    fn avatar_data(&self) -> AvatarData {
        imp::session_info_avatar_data(self.upcast_ref())
    }
}

/// Public trait that must be implemented for everything that derives from
/// `SessionInfo`.
///
/// Overriding a method from this Trait overrides also its behavior in
/// `SessionInfoExt`.
pub trait SessionInfoImpl: ObjectImpl {
    fn avatar_data(&self) -> AvatarData;
}

// Make `SessionInfo` subclassable.
unsafe impl<T> IsSubclassable<T> for SessionInfo
where
    T: SessionInfoImpl,
    T::Type: IsA<SessionInfo>,
{
    fn class_init(class: &mut glib::Class<Self>) {
        Self::parent_class_init::<T>(class.upcast_ref_mut());
        let klass = class.as_mut();

        klass.avatar_data = avatar_data_trampoline::<T>;
    }
}

// Virtual method implementation trampolines.
fn avatar_data_trampoline<T>(this: &SessionInfo) -> AvatarData
where
    T: ObjectSubclass + SessionInfoImpl,
    T::Type: IsA<SessionInfo>,
{
    let this = this.downcast_ref::<T::Type>().unwrap();
    this.imp().avatar_data()
}