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)]
#[zvariant(signature = "dict")]
pub struct UserInformation {
id: String,
name: String,
image: url::Url,
}
impl UserInformation {
pub fn id(&self) -> &str {
&self.id
}
pub fn name(&self) -> &str {
&self.name
}
pub fn image(&self) -> &url::Url {
&self.image
}
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: &WindowIdentifier,
options: UserInformationOptions,
) -> Result<Request<UserInformation>, Error> {
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)]
pub struct UserInformationRequest {
options: UserInformationOptions,
identifier: WindowIdentifier,
}
impl UserInformationRequest {
#[must_use]
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]
pub fn identifier(mut self, identifier: impl Into<Option<WindowIdentifier>>) -> Self {
self.identifier = identifier.into().unwrap_or_default();
self
}
pub async fn send(self) -> Result<Request<UserInformation>, Error> {
let proxy = AccountProxy::new().await?;
proxy.user_information(&self.identifier, self.options).await
}
}