use super::{HandleToken, DESTINATION, PATH};
use crate::{helpers::call_basic_response_method, Error, WindowIdentifier};
use std::os::unix::prelude::AsRawFd;
use zvariant::Fd;
use zvariant_derive::{DeserializeDict, SerializeDict, TypeDict};
#[derive(SerializeDict, DeserializeDict, TypeDict, Debug, Default)]
struct OpenDirOptions {
handle_token: HandleToken,
}
#[derive(SerializeDict, DeserializeDict, TypeDict, Debug, Default)]
struct OpenFileOptions {
handle_token: HandleToken,
writeable: Option<bool>,
ask: Option<bool>,
}
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
}
}
#[derive(Debug)]
#[doc(alias = "org.freedesktop.portal.OpenURI")]
pub struct OpenURIProxy<'a>(zbus::azync::Proxy<'a>);
impl<'a> OpenURIProxy<'a> {
pub async fn new(connection: &zbus::azync::Connection) -> Result<OpenURIProxy<'a>, Error> {
let proxy = zbus::ProxyBuilder::new_bare(connection)
.interface("org.freedesktop.portal.OpenURI")
.path(PATH)?
.destination(DESTINATION)
.build_async()
.await?;
Ok(Self(proxy))
}
pub fn inner(&self) -> &zbus::azync::Proxy<'_> {
&self.0
}
#[doc(alias = "OpenDirectory")]
pub async fn open_directory<F>(
&self,
identifier: WindowIdentifier,
directory: &F,
) -> Result<(), Error>
where
F: AsRawFd,
{
let options = OpenDirOptions::default();
call_basic_response_method(
&self.0,
&options.handle_token,
"OpenDirectory",
&(identifier, Fd::from(directory.as_raw_fd()), &options),
)
.await
}
#[doc(alias = "OpenFile")]
pub async fn open_file<F>(
&self,
identifier: WindowIdentifier,
file: &F,
writeable: bool,
ask: bool,
) -> Result<(), Error>
where
F: AsRawFd,
{
let options = OpenFileOptions::default().ask(ask).writeable(writeable);
call_basic_response_method(
&self.0,
&options.handle_token,
"OpenFile",
&(identifier, Fd::from(file.as_raw_fd()), &options),
)
.await
}
#[doc(alias = "OpenURI")]
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.0,
&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::azync::Connection::new_session().await?;
let proxy = OpenURIProxy::new(&connection).await?;
proxy.open_uri(identifier, uri, writeable, ask).await?;
Ok(())
}
pub async fn open_file<F: AsRawFd>(
identifier: WindowIdentifier,
file: &F,
writeable: bool,
ask: bool,
) -> Result<(), Error> {
let connection = zbus::azync::Connection::new_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<F: AsRawFd>(
identifier: WindowIdentifier,
directory: &F,
) -> Result<(), Error> {
let connection = zbus::azync::Connection::new_session().await?;
let proxy = OpenURIProxy::new(&connection).await?;
proxy.open_directory(identifier, directory).await?;
Ok(())
}