use std::os::fd::AsFd;
use serde::Serialize;
use zbus::zvariant::{
Fd, Optional, Type,
as_value::{self, optional},
};
use super::{HandleToken, Request};
use crate::{ActivationToken, Error, Uri, WindowIdentifier, proxy::Proxy};
#[derive(Serialize, Type, Debug, Default)]
#[zvariant(signature = "dict")]
pub struct OpenDirOptions {
#[serde(with = "as_value")]
handle_token: HandleToken,
#[serde(with = "optional", skip_serializing_if = "Option::is_none")]
pub activation_token: Option<ActivationToken>,
}
#[derive(Serialize, Type, Debug, Default)]
#[zvariant(signature = "dict")]
pub struct OpenFileOptions {
#[serde(with = "as_value")]
handle_token: HandleToken,
#[serde(with = "optional", skip_serializing_if = "Option::is_none")]
pub writeable: Option<bool>,
#[serde(with = "optional", skip_serializing_if = "Option::is_none")]
pub ask: Option<bool>,
#[serde(with = "optional", skip_serializing_if = "Option::is_none")]
pub activation_token: Option<ActivationToken>,
}
#[derive(Debug, Serialize, Type, Default)]
#[zvariant(signature = "dict")]
pub struct SchemeSupportedOptions {}
#[derive(Debug)]
pub struct OpenURIProxy(Proxy<'static>);
impl OpenURIProxy {
pub async fn new() -> Result<OpenURIProxy, Error> {
let proxy = Proxy::new_desktop("org.freedesktop.portal.OpenURI").await?;
Ok(Self(proxy))
}
pub async fn with_connection(connection: zbus::Connection) -> Result<Self, Error> {
let proxy =
Proxy::new_desktop_with_connection(connection, "org.freedesktop.portal.OpenURI")
.await?;
Ok(Self(proxy))
}
pub fn version(&self) -> u32 {
self.0.version()
}
#[doc(alias = "OpenDirectory")]
pub async fn open_directory(
&self,
identifier: Option<&WindowIdentifier>,
directory: &impl AsFd,
options: OpenDirOptions,
) -> Result<Request<()>, Error> {
let identifier = Optional::from(identifier);
self.0
.empty_request(
&options.handle_token,
"OpenDirectory",
&(identifier, Fd::from(directory), &options),
)
.await
}
#[doc(alias = "OpenFile")]
pub async fn open_file(
&self,
identifier: Option<&WindowIdentifier>,
file: &impl AsFd,
options: OpenFileOptions,
) -> Result<Request<()>, Error> {
let identifier = Optional::from(identifier);
self.0
.empty_request(
&options.handle_token,
"OpenFile",
&(identifier, Fd::from(file), &options),
)
.await
}
#[doc(alias = "OpenURI")]
pub async fn open_uri(
&self,
identifier: Option<&WindowIdentifier>,
uri: &Uri,
options: OpenFileOptions,
) -> Result<Request<()>, Error> {
let identifier = Optional::from(identifier);
self.0
.empty_request(
&options.handle_token,
"OpenURI",
&(identifier, uri, &options),
)
.await
}
#[doc(alias = "SchemeSupported")]
pub async fn scheme_supported(
&self,
schema: &str,
options: SchemeSupportedOptions,
) -> Result<bool, Error> {
self.0.call("SchemeSupported", &(schema, &options)).await
}
}
impl std::ops::Deref for OpenURIProxy {
type Target = zbus::Proxy<'static>;
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,
connection: Option<zbus::Connection>,
}
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
}
#[must_use]
pub fn connection(mut self, connection: Option<zbus::Connection>) -> Self {
self.connection = connection;
self
}
pub async fn send_file(self, file: &impl AsFd) -> Result<Request<()>, Error> {
let proxy = if let Some(connection) = self.connection {
OpenURIProxy::with_connection(connection).await?
} else {
OpenURIProxy::new().await?
};
proxy
.open_file(self.identifier.as_ref(), file, self.options)
.await
}
pub async fn send_uri(self, uri: &Uri) -> Result<Request<()>, Error> {
let proxy = if let Some(connection) = self.connection {
OpenURIProxy::with_connection(connection).await?
} else {
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,
connection: Option<zbus::Connection>,
}
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
}
#[must_use]
pub fn connection(mut self, connection: Option<zbus::Connection>) -> Self {
self.connection = connection;
self
}
pub async fn send(self, directory: &impl AsFd) -> Result<Request<()>, Error> {
let proxy = if let Some(connection) = self.connection {
OpenURIProxy::with_connection(connection).await?
} else {
OpenURIProxy::new().await?
};
proxy
.open_directory(self.identifier.as_ref(), directory, self.options)
.await
}
}