use super::{HandleToken, DESTINATION, PATH};
use crate::{helpers::call_request_method, Error, WindowIdentifier};
use zvariant_derive::{DeserializeDict, SerializeDict, TypeDict};
#[derive(SerializeDict, DeserializeDict, TypeDict, Clone, Debug, Default)]
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, TypeDict)]
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::azync::Proxy<'a>);
impl<'a> AccountProxy<'a> {
pub async fn new(connection: &zbus::azync::Connection) -> Result<AccountProxy<'a>, Error> {
let proxy = zbus::ProxyBuilder::new_bare(connection)
.interface("org.freedesktop.portal.Account")
.path(PATH)?
.destination(DESTINATION)
.build_async()
.await?;
Ok(Self(proxy))
}
pub fn inner(&self) -> &zbus::azync::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.0,
&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::azync::Connection::new_session().await?;
let proxy = AccountProxy::new(&connection).await?;
proxy.user_information(identifier, reason).await
}