Skip to main content

fractal/session_view/explore/
server.rs

1use gtk::{glib, prelude::*, subclass::prelude::*};
2use ruma::OwnedServerName;
3
4mod imp {
5    use std::{cell::OnceCell, marker::PhantomData};
6
7    use super::*;
8
9    #[derive(Debug, Default, glib::Properties)]
10    #[properties(wrapper_type = super::ExploreServer)]
11    pub struct ExploreServer {
12        /// The server to query.
13        ///
14        /// If this is `None`, our own homeserver will be queried.
15        server: OnceCell<OwnedServerName>,
16        /// The server to query, as a string.
17        #[property(get = Self::server_string)]
18        server_string: PhantomData<Option<String>>,
19        /// The third-party network to query, if any.
20        ///
21        /// If this is `None`, the Matrix network will be queried.
22        #[property(get, construct_only)]
23        third_party_network: OnceCell<Option<String>>,
24        /// The name of the server to display in the list.
25        #[property(get, construct_only)]
26        name: OnceCell<String>,
27    }
28
29    #[glib::object_subclass]
30    impl ObjectSubclass for ExploreServer {
31        const NAME: &'static str = "ExploreServer";
32        type Type = super::ExploreServer;
33    }
34
35    #[glib::derived_properties]
36    impl ObjectImpl for ExploreServer {}
37
38    impl ExploreServer {
39        /// Initialize the server and network.
40        pub(super) fn init_server(&self, server: OwnedServerName) {
41            self.server
42                .set(server)
43                .expect("server should not be initialized");
44        }
45
46        /// The server to query.
47        ///
48        /// If this is `None`, our own homeserver will be queried.
49        pub(super) fn server(&self) -> Option<&OwnedServerName> {
50            self.server.get()
51        }
52
53        /// The server to query, as a string.
54        fn server_string(&self) -> Option<String> {
55            self.server().map(ToString::to_string)
56        }
57    }
58}
59
60glib::wrapper! {
61    /// A server with an optional third-party network that can be queried to search for public rooms.
62    pub struct ExploreServer(ObjectSubclass<imp::ExploreServer>);
63}
64
65impl ExploreServer {
66    /// Construct an `ExploreServer` for the Matrix network on the default
67    /// server.
68    pub(crate) fn with_default_server(name: &str) -> Self {
69        glib::Object::builder().property("name", name).build()
70    }
71
72    /// Construct an `ExploreServer` for the given third-party protocol on the
73    /// default server.
74    pub(crate) fn with_third_party_protocol(
75        desc: &str,
76        protocol_id: &str,
77        instance_id: &str,
78    ) -> Self {
79        glib::Object::builder()
80            .property("name", format!("{desc} ({protocol_id})"))
81            .property("third-party-network", instance_id)
82            .build()
83    }
84
85    /// Construct an `ExploreServer` for the Matrix network on the given server.
86    pub(crate) fn with_server(server: OwnedServerName) -> Self {
87        let obj = glib::Object::builder::<Self>()
88            .property("name", server.as_str())
89            .build();
90        obj.imp().init_server(server);
91        obj
92    }
93
94    /// The server to query.
95    ///
96    /// If this is `None`, our own homeserver will be queried.
97    pub(crate) fn server(&self) -> Option<&OwnedServerName> {
98        self.imp().server()
99    }
100}