use std::os::fd::AsFd;
use futures_util::Stream;
use serde::Serialize;
use zbus::zvariant::{Fd, Type, as_value::optional};
use crate::{Error, proxy::Proxy};
#[derive(Serialize, Type, Debug, Default)]
#[zvariant(signature = "dict")]
pub struct StartTransferOptions {
#[serde(with = "optional", skip_serializing_if = "Option::is_none")]
writeable: Option<bool>,
#[serde(
rename = "autostop",
with = "optional",
skip_serializing_if = "Option::is_none"
)]
auto_stop: Option<bool>,
}
impl StartTransferOptions {
#[must_use]
pub fn set_writeable(mut self, writeable: impl Into<Option<bool>>) -> Self {
self.writeable = writeable.into();
self
}
#[must_use]
pub fn set_auto_stop(mut self, auto_stop: impl Into<Option<bool>>) -> Self {
self.auto_stop = auto_stop.into();
self
}
}
#[derive(Default, Debug, Serialize, Type)]
#[zvariant(signature = "dict")]
pub struct AddFilesOptions {}
#[derive(Default, Debug, Serialize, Type)]
#[zvariant(signature = "dict")]
pub struct RetrieveFilesOptions {}
#[derive(Debug)]
#[doc(alias = "org.freedesktop.portal.FileTransfer")]
pub struct FileTransfer(Proxy<'static>);
impl FileTransfer {
pub async fn new() -> Result<Self, Error> {
let proxy = Proxy::new_documents("org.freedesktop.portal.FileTransfer").await?;
Ok(Self(proxy))
}
pub async fn with_connection(connection: zbus::Connection) -> Result<Self, Error> {
let proxy =
Proxy::new_documents_with_connection(connection, "org.freedesktop.portal.FileTransfer")
.await?;
Ok(Self(proxy))
}
pub fn version(&self) -> u32 {
self.0.version()
}
#[doc(alias = "AddFiles")]
pub async fn add_files(
&self,
key: &str,
fds: &[impl AsFd],
options: AddFilesOptions,
) -> Result<(), Error> {
let files: Vec<Fd> = fds.iter().map(Fd::from).collect();
self.0.call("AddFiles", &(key, files, options)).await
}
#[doc(alias = "RetrieveFiles")]
pub async fn retrieve_files(
&self,
key: &str,
options: RetrieveFilesOptions,
) -> Result<Vec<String>, Error> {
self.0.call("RetrieveFiles", &(key, options)).await
}
#[doc(alias = "StartTransfer")]
pub async fn start_transfer(&self, options: StartTransferOptions) -> Result<String, Error> {
self.0.call("StartTransfer", &(options)).await
}
#[doc(alias = "StopTransfer")]
pub async fn stop_transfer(&self, key: &str) -> Result<(), Error> {
self.0.call("StopTransfer", &(key)).await
}
#[doc(alias = "TransferClosed")]
pub async fn transfer_closed(&self) -> Result<impl Stream<Item = String>, Error> {
self.0.signal("TransferClosed").await
}
}
impl std::ops::Deref for FileTransfer {
type Target = zbus::Proxy<'static>;
fn deref(&self) -> &Self::Target {
&self.0
}
}