Debian Codegraph @

src/desktop/account.rs

raw
1 //! Access to the current logged user information such as the id, name
2 //! or their avatar uri.
3 //!
4 //! Wrapper of the DBus interface: [`org.freedesktop.portal.Account`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Account.html).
5 //!
6 //! ### Examples
7 //!
8 //! ```rust, no_run
9 //! use ashpd::desktop::account::UserInformation;
10 //!
11 //! async fn run() -> ashpd::Result<()> {
12 //! let response = UserInformation::request()
13 //! .reason("App would like to access user information")
14 //! .send()
15 //! .await?
16 //! .response()?;
17 //!
18 //! println!("Name: {}", response.name());
19 //! println!("ID: {}", response.id());
20 //!
21 //! Ok(())
22 //! }
23 //! ```
24
25 use serde::{Deserialize, Serialize};
26 use zbus::zvariant::{
27 Optional, Type,
28 as_value::{self, optional},
29 };
30
31 use super::HandleToken;
32 use crate::{Error, Uri, WindowIdentifier, desktop::request::Request, proxy::Proxy};
33
34 /// Options for requesting user information.
35 #[derive(Serialize, Deserialize, Type, Debug, Default)]
36 #[zvariant(signature = "dict")]
37 pub struct UserInformationOptions {
38 #[serde(with = "as_value", skip_deserializing)]
39 handle_token: HandleToken,
40 #[serde(default, with = "optional", skip_serializing_if = "Option::is_none")]
41 reason: Option<String>,
42 }
43
44 impl UserInformationOptions {
45 /// Sets a user-visible reason for the request.
46 #[must_use]
47 pub fn set_reason<'a>(mut self, reason: impl Into<Option<&'a str>>) -> Self {
48 self.reason = reason.into().map(ToOwned::to_owned);
49 self
50 }
51
52 /// Gets the user-visible reason for the request.
53 #[cfg(feature = "backend")]
54 pub fn reason(&self) -> Option<&str> {
55 self.reason.as_deref()
56 }
57 }
58
59 #[derive(Debug, Serialize, Deserialize, Type)]
60 /// The response of a [`UserInformationRequest`] request.
61 #[zvariant(signature = "dict")]
62 pub struct UserInformation {
63 #[serde(with = "as_value")]
64 id: String,
65 #[serde(with = "as_value")]
66 name: String,
67 #[serde(with = "as_value")]
68 image: Uri,
69 }
70
71 impl UserInformation {
72 #[cfg(feature = "backend")]
73 #[cfg_attr(docsrs, doc(cfg(feature = "backend")))]
74 /// Create a new instance of [`UserInformation`].
75 pub fn new(id: &str, name: &str, image: Uri) -> Self {
76 Self {
77 id: id.to_owned(),
78 name: name.to_owned(),
79 image,
80 }
81 }
82
83 /// User identifier.
84 pub fn id(&self) -> &str {
85 &self.id
86 }
87
88 /// User name.
89 pub fn name(&self) -> &str {
90 &self.name
91 }
92
93 /// User image uri.
94 pub fn image(&self) -> &Uri {
95 &self.image
96 }
97
98 /// Creates a new builder-pattern struct instance to construct
99 /// [`UserInformation`].
100 ///
101 /// This method returns an instance of [`UserInformationRequest`].
102 pub fn request() -> UserInformationRequest {
103 UserInformationRequest::default()
104 }
105 }
106
107 /// Wrapper of the DBus interface: [`org.freedesktop.portal.Account`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Account.html).
108 #[doc(alias = "org.freedesktop.portal.Account")]
109 pub struct AccountProxy(Proxy<'static>);
110
111 impl AccountProxy {
112 /// Create a new instance of [`AccountProxy`].
113 pub async fn new() -> Result<Self, Error> {
114 let proxy = Proxy::new_desktop("org.freedesktop.portal.Account").await?;
115 Ok(Self(proxy))
116 }
117
118 /// Create a new instance of [`AccountProxy`] with a specific connection.
119 pub async fn with_connection(connection: zbus::Connection) -> Result<Self, Error> {
120 let proxy =
121 Proxy::new_desktop_with_connection(connection, "org.freedesktop.portal.Account")
122 .await?;
123 Ok(Self(proxy))
124 }
125
126 /// Returns the portal interface version.
127 pub fn version(&self) -> u32 {
128 self.0.version()
129 }
130
131 /// Requests user information.
132 #[doc(alias = "GetUserInformation")]
133 pub async fn user_information(
134 &self,
135 identifier: Option<&WindowIdentifier>,
136 options: UserInformationOptions,
137 ) -> Result<Request<UserInformation>, Error> {
138 let identifier = Optional::from(identifier);
139 self.0
140 .request(
141 &options.handle_token,
142 "GetUserInformation",
143 (identifier, &options),
144 )
145 .await
146 }
147 }
148
149 impl std::ops::Deref for AccountProxy {
150 type Target = zbus::Proxy<'static>;
151
152 fn deref(&self) -> &Self::Target {
153 &self.0
154 }
155 }
156
157 #[doc(alias = "xdp_portal_get_user_information")]
158 #[doc(alias = "org.freedesktop.portal.Account")]
159 #[derive(Debug, Default)]
160 /// A [builder-pattern] type to construct [`UserInformation`].
161 ///
162 /// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
163 pub struct UserInformationRequest {
164 options: UserInformationOptions,
165 identifier: Option<WindowIdentifier>,
166 connection: Option<zbus::Connection>,
167 }
168
169 impl UserInformationRequest {
170 #[must_use]
171 /// Sets a user-visible reason for the request.
172 pub fn reason<'a>(mut self, reason: impl Into<Option<&'a str>>) -> Self {
173 self.options.reason = reason.into().map(ToOwned::to_owned);
174 self
175 }
176
177 #[must_use]
178 /// Sets a window identifier.
179 pub fn identifier(mut self, identifier: impl Into<Option<WindowIdentifier>>) -> Self {
180 self.identifier = identifier.into();
181 self
182 }
183
184 #[must_use]
185 /// Sets a connection to use other than the internal one.
186 pub fn connection(mut self, connection: Option<zbus::Connection>) -> Self {
187 self.connection = connection;
188 self
189 }
190
191 /// Build the [`UserInformation`].
192 pub async fn send(self) -> Result<Request<UserInformation>, Error> {
193 let proxy = if let Some(connection) = self.connection {
194 AccountProxy::with_connection(connection).await?
195 } else {
196 AccountProxy::new().await?
197 };
198 proxy
199 .user_information(self.identifier.as_ref(), self.options)
200 .await
201 }
202 }