use zbus::zvariant::{DeserializeDict, SerializeDict, Type};
use super::HandleToken;
use crate::{
Error, WindowIdentifier, desktop::request::Request, proxy::Proxy,
window_identifier::MaybeWindowIdentifierExt,
};
#[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 {
#[cfg(feature = "backend_account")]
#[cfg_attr(docsrs, doc(cfg(feature = "backend_account")))]
pub fn new(id: &str, name: &str, image: url::Url) -> Self {
Self {
id: id.to_owned(),
name: name.to_owned(),
image,
}
}
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(Proxy<'static>);
impl AccountProxy {
pub async fn new() -> Result<Self, Error> {
let proxy = Proxy::new_desktop("org.freedesktop.portal.Account").await?;
Ok(Self(proxy))
}
pub async fn with_connection(connection: zbus::Connection) -> Result<Self, Error> {
let proxy =
Proxy::new_desktop_with_connection(connection, "org.freedesktop.portal.Account")
.await?;
Ok(Self(proxy))
}
#[doc(alias = "GetUserInformation")]
pub async fn user_information(
&self,
identifier: Option<&WindowIdentifier>,
options: UserInformationOptions,
) -> Result<Request<UserInformation>, Error> {
let identifier = identifier.to_string_or_empty();
self.0
.request(
&options.handle_token,
"GetUserInformation",
(&identifier, &options),
)
.await
}
}
impl std::ops::Deref for AccountProxy {
type Target = zbus::Proxy<'static>;
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: Option<WindowIdentifier>,
connection: Option<zbus::Connection>,
}
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();
self
}
#[must_use]
pub fn connection(mut self, connection: Option<zbus::Connection>) -> Self {
self.connection = connection;
self
}
pub async fn send(self) -> Result<Request<UserInformation>, Error> {
let proxy = if let Some(connection) = self.connection {
AccountProxy::with_connection(connection).await?
} else {
AccountProxy::new().await?
};
proxy
.user_information(self.identifier.as_ref(), self.options)
.await
}
}