use zbus::zvariant::{DeserializeDict, SerializeDict, Type};
use super::{HandleToken, DESTINATION, PATH};
use crate::{helpers::call_request_method, Error, WindowIdentifier};
#[derive(SerializeDict, DeserializeDict, Type, Clone, Debug, Default)]
#[zvariant(signature = "dict")]
struct UserInfoOptions {
handle_token: HandleToken,
reason: Option<String>,
}
impl UserInfoOptions {
pub fn reason(mut self, reason: &str) -> Self {
self.reason = Some(reason.to_string());
self
}
}
#[derive(Debug, SerializeDict, DeserializeDict, Clone, Type)]
#[zvariant(signature = "dict")]
pub struct UserInfo {
id: String,
name: String,
image: String,
}
impl UserInfo {
pub fn id(&self) -> &str {
&self.id
}
pub fn name(&self) -> &str {
&self.name
}
pub fn image(&self) -> &str {
&self.image
}
}
#[derive(Debug)]
#[doc(alias = "org.freedesktop.portal.Account")]
pub struct AccountProxy<'a>(zbus::Proxy<'a>);
impl<'a> AccountProxy<'a> {
pub async fn new(connection: &zbus::Connection) -> Result<AccountProxy<'a>, Error> {
let proxy = zbus::ProxyBuilder::new_bare(connection)
.interface("org.freedesktop.portal.Account")?
.path(PATH)?
.destination(DESTINATION)?
.build()
.await?;
Ok(Self(proxy))
}
pub fn inner(&self) -> &zbus::Proxy<'_> {
&self.0
}
#[doc(alias = "GetUserInformation")]
pub async fn user_information(
&self,
identifier: &WindowIdentifier,
reason: &str,
) -> Result<UserInfo, Error> {
let options = UserInfoOptions::default().reason(reason);
call_request_method(
self.inner(),
&options.handle_token,
"GetUserInformation",
&(&identifier, &options),
)
.await
}
}
#[doc(alias = "xdp_portal_get_user_information")]
#[doc(alias = "get_user_information")]
pub async fn user_information(
identifier: &WindowIdentifier,
reason: &str,
) -> Result<UserInfo, Error> {
let connection = zbus::Connection::session().await?;
let proxy = AccountProxy::new(&connection).await?;
proxy.user_information(identifier, reason).await
}