ashpd/desktop/account.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 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
//! Access to the current logged user information such as the id, name
//! or their avatar uri.
//!
//! Wrapper of the DBus interface: [`org.freedesktop.portal.Account`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Account.html).
//!
//! ### Examples
//!
//! ```rust, no_run
//! use ashpd::desktop::account::UserInformation;
//!
//! async fn run() -> ashpd::Result<()> {
//! let response = UserInformation::request()
//! .reason("App would like to access user information")
//! .send()
//! .await?
//! .response()?;
//!
//! println!("Name: {}", response.name());
//! println!("ID: {}", response.id());
//!
//! Ok(())
//! }
//! ```
use zbus::zvariant::{DeserializeDict, SerializeDict, Type};
use super::HandleToken;
use crate::{desktop::request::Request, proxy::Proxy, Error, WindowIdentifier};
#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct UserInformationOptions {
handle_token: HandleToken,
reason: Option<String>,
}
#[derive(Debug, DeserializeDict, SerializeDict, Type)]
/// The response of a [`UserInformationRequest`] request.
#[zvariant(signature = "dict")]
pub struct UserInformation {
id: String,
name: String,
image: url::Url,
}
impl UserInformation {
#[cfg(feature = "backend")]
#[cfg_attr(docsrs, doc(cfg(feature = "backend")))]
/// Create a new instance of [`UserInformation`].
pub fn new(id: &str, name: &str, image: url::Url) -> Self {
Self {
id: id.to_owned(),
name: name.to_owned(),
image,
}
}
/// User identifier.
pub fn id(&self) -> &str {
&self.id
}
/// User name.
pub fn name(&self) -> &str {
&self.name
}
/// User image uri.
pub fn image(&self) -> &url::Url {
&self.image
}
/// Creates a new builder-pattern struct instance to construct
/// [`UserInformation`].
///
/// This method returns an instance of [`UserInformationRequest`].
pub fn request() -> UserInformationRequest {
UserInformationRequest::default()
}
}
struct AccountProxy<'a>(Proxy<'a>);
impl<'a> AccountProxy<'a> {
pub async fn new() -> Result<AccountProxy<'a>, Error> {
let proxy = Proxy::new_desktop("org.freedesktop.portal.Account").await?;
Ok(Self(proxy))
}
pub async fn user_information(
&self,
identifier: Option<&WindowIdentifier>,
options: UserInformationOptions,
) -> Result<Request<UserInformation>, Error> {
let identifier = identifier.map(|i| i.to_string()).unwrap_or_default();
self.0
.request(
&options.handle_token,
"GetUserInformation",
(&identifier, &options),
)
.await
}
}
impl<'a> std::ops::Deref for AccountProxy<'a> {
type Target = zbus::Proxy<'a>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[doc(alias = "xdp_portal_get_user_information")]
#[doc(alias = "org.freedesktop.portal.Account")]
#[derive(Debug, Default)]
/// A [builder-pattern] type to construct [`UserInformation`].
///
/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
pub struct UserInformationRequest {
options: UserInformationOptions,
identifier: Option<WindowIdentifier>,
}
impl UserInformationRequest {
#[must_use]
/// Sets a user-visible reason for the request.
pub fn reason<'a>(mut self, reason: impl Into<Option<&'a str>>) -> Self {
self.options.reason = reason.into().map(ToOwned::to_owned);
self
}
#[must_use]
/// Sets a window identifier.
pub fn identifier(mut self, identifier: impl Into<Option<WindowIdentifier>>) -> Self {
self.identifier = identifier.into();
self
}
/// Build the [`UserInformation`].
pub async fn send(self) -> Result<Request<UserInformation>, Error> {
let proxy = AccountProxy::new().await?;
proxy
.user_information(self.identifier.as_ref(), self.options)
.await
}
}