use std::os::unix::prelude::AsRawFd;
use zbus::zvariant::{DeserializeDict, Fd, SerializeDict, Type};
use super::{HandleToken, DESTINATION, PATH};
use crate::{helpers::call_basic_response_method, Error, WindowIdentifier};
#[derive(SerializeDict, DeserializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct OpenDirOptions {
handle_token: HandleToken,
activation_token: Option<String>,
}
impl OpenDirOptions {
#[allow(dead_code)]
pub fn set_activation_token(&mut self, activation_token: &str) {
self.activation_token = Some(activation_token.to_string());
}
}
#[derive(SerializeDict, DeserializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct OpenFileOptions {
handle_token: HandleToken,
writeable: Option<bool>,
ask: Option<bool>,
activation_token: Option<String>,
}
impl OpenFileOptions {
pub fn writeable(mut self, writeable: bool) -> Self {
self.writeable = Some(writeable);
self
}
pub fn ask(mut self, ask: bool) -> Self {
self.ask = Some(ask);
self
}
#[allow(dead_code)]
pub fn set_activation_token(&mut self, activation_token: &str) {
self.activation_token = Some(activation_token.to_string());
}
}
#[derive(Debug)]
#[doc(alias = "org.freedesktop.portal.OpenURI")]
pub struct OpenURIProxy<'a>(zbus::Proxy<'a>);
impl<'a> OpenURIProxy<'a> {
pub async fn new(connection: &zbus::Connection) -> Result<OpenURIProxy<'a>, Error> {
let proxy = zbus::ProxyBuilder::new_bare(connection)
.interface("org.freedesktop.portal.OpenURI")?
.path(PATH)?
.destination(DESTINATION)?
.build()
.await?;
Ok(Self(proxy))
}
pub fn inner(&self) -> &zbus::Proxy<'_> {
&self.0
}
#[doc(alias = "OpenDirectory")]
#[doc(alias = "xdp_portal_open_directory")]
pub async fn open_directory(
&self,
identifier: &WindowIdentifier,
directory: &impl AsRawFd,
) -> Result<(), Error> {
let options = OpenDirOptions::default();
call_basic_response_method(
self.inner(),
&options.handle_token,
"OpenDirectory",
&(&identifier, Fd::from(directory.as_raw_fd()), &options),
)
.await
}
#[doc(alias = "OpenFile")]
pub async fn open_file(
&self,
identifier: &WindowIdentifier,
file: &impl AsRawFd,
writeable: bool,
ask: bool,
) -> Result<(), Error> {
let options = OpenFileOptions::default().ask(ask).writeable(writeable);
call_basic_response_method(
self.inner(),
&options.handle_token,
"OpenFile",
&(&identifier, Fd::from(file.as_raw_fd()), &options),
)
.await
}
#[doc(alias = "OpenURI")]
#[doc(alias = "xdp_portal_open_uri")]
pub async fn open_uri(
&self,
identifier: &WindowIdentifier,
uri: &str,
writeable: bool,
ask: bool,
) -> Result<(), Error> {
let options = OpenFileOptions::default().ask(ask).writeable(writeable);
call_basic_response_method(
self.inner(),
&options.handle_token,
"OpenURI",
&(&identifier, uri, &options),
)
.await
}
}
#[doc(alias = "xdp_portal_open_uri")]
pub async fn open_uri(
identifier: &WindowIdentifier,
uri: &str,
writeable: bool,
ask: bool,
) -> Result<(), Error> {
let connection = zbus::Connection::session().await?;
let proxy = OpenURIProxy::new(&connection).await?;
proxy.open_uri(identifier, uri, writeable, ask).await?;
Ok(())
}
pub async fn open_file(
identifier: &WindowIdentifier,
file: &impl AsRawFd,
writeable: bool,
ask: bool,
) -> Result<(), Error> {
let connection = zbus::Connection::session().await?;
let proxy = OpenURIProxy::new(&connection).await?;
proxy.open_file(identifier, file, writeable, ask).await?;
Ok(())
}
#[doc(alias = "xdp_portal_open_directory")]
pub async fn open_directory(
identifier: &WindowIdentifier,
directory: &impl AsRawFd,
) -> Result<(), Error> {
let connection = zbus::Connection::session().await?;
let proxy = OpenURIProxy::new(&connection).await?;
proxy.open_directory(identifier, directory).await?;
Ok(())
}