ashpd/desktop/
open_uri.rsuse std::os::fd::AsFd;
use url::Url;
use zbus::zvariant::{Fd, SerializeDict, Type};
use super::{HandleToken, Request};
use crate::{proxy::Proxy, ActivationToken, Error, WindowIdentifier};
#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct OpenDirOptions {
handle_token: HandleToken,
activation_token: Option<ActivationToken>,
}
#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct OpenFileOptions {
handle_token: HandleToken,
writeable: Option<bool>,
ask: Option<bool>,
activation_token: Option<ActivationToken>,
}
#[derive(Debug)]
struct OpenURIProxy<'a>(Proxy<'a>);
impl<'a> OpenURIProxy<'a> {
pub async fn new() -> Result<OpenURIProxy<'a>, Error> {
let proxy = Proxy::new_desktop("org.freedesktop.portal.OpenURI").await?;
Ok(Self(proxy))
}
pub async fn open_directory(
&self,
identifier: Option<&WindowIdentifier>,
directory: &impl AsFd,
options: OpenDirOptions,
) -> Result<Request<()>, Error> {
let identifier = identifier.map(|i| i.to_string()).unwrap_or_default();
self.0
.empty_request(
&options.handle_token,
"OpenDirectory",
&(&identifier, Fd::from(directory), &options),
)
.await
}
pub async fn open_file(
&self,
identifier: Option<&WindowIdentifier>,
file: &impl AsFd,
options: OpenFileOptions,
) -> Result<Request<()>, Error> {
let identifier = identifier.map(|i| i.to_string()).unwrap_or_default();
self.0
.empty_request(
&options.handle_token,
"OpenFile",
&(&identifier, Fd::from(file), &options),
)
.await
}
pub async fn open_uri(
&self,
identifier: Option<&WindowIdentifier>,
uri: &url::Url,
options: OpenFileOptions,
) -> Result<Request<()>, Error> {
let identifier = identifier.map(|i| i.to_string()).unwrap_or_default();
self.0
.empty_request(
&options.handle_token,
"OpenURI",
&(&identifier, uri, &options),
)
.await
}
}
impl<'a> std::ops::Deref for OpenURIProxy<'a> {
type Target = zbus::Proxy<'a>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Debug, Default)]
#[doc(alias = "org.freedesktop.portal.OpenURI")]
#[doc(alias = "xdp_portal_open_uri")]
pub struct OpenFileRequest {
identifier: Option<WindowIdentifier>,
options: OpenFileOptions,
}
impl OpenFileRequest {
#[must_use]
pub fn identifier(mut self, identifier: impl Into<Option<WindowIdentifier>>) -> Self {
self.identifier = identifier.into();
self
}
#[must_use]
pub fn writeable(mut self, writeable: impl Into<Option<bool>>) -> Self {
self.options.writeable = writeable.into();
self
}
#[must_use]
pub fn ask(mut self, ask: impl Into<Option<bool>>) -> Self {
self.options.ask = ask.into();
self
}
#[must_use]
pub fn activation_token(
mut self,
activation_token: impl Into<Option<ActivationToken>>,
) -> Self {
self.options.activation_token = activation_token.into();
self
}
pub async fn send_file(self, file: &impl AsFd) -> Result<Request<()>, Error> {
let proxy = OpenURIProxy::new().await?;
proxy
.open_file(self.identifier.as_ref(), file, self.options)
.await
}
pub async fn send_uri(self, uri: &Url) -> Result<Request<()>, Error> {
let proxy = OpenURIProxy::new().await?;
proxy
.open_uri(self.identifier.as_ref(), uri, self.options)
.await
}
}
#[derive(Debug, Default)]
#[doc(alias = "xdp_portal_open_directory")]
#[doc(alias = "org.freedesktop.portal.OpenURI")]
pub struct OpenDirectoryRequest {
identifier: Option<WindowIdentifier>,
options: OpenDirOptions,
}
impl OpenDirectoryRequest {
#[must_use]
pub fn identifier(mut self, identifier: impl Into<Option<WindowIdentifier>>) -> Self {
self.identifier = identifier.into();
self
}
#[must_use]
pub fn activation_token(
mut self,
activation_token: impl Into<Option<ActivationToken>>,
) -> Self {
self.options.activation_token = activation_token.into();
self
}
pub async fn send(self, directory: &impl AsFd) -> Result<Request<()>, Error> {
let proxy = OpenURIProxy::new().await?;
proxy
.open_directory(self.identifier.as_ref(), directory, self.options)
.await
}
}