use std::{collections::HashMap, os::fd::BorrowedFd};
use futures_util::Stream;
use zbus::zvariant::{Fd, SerializeDict, Type, Value};
use crate::{proxy::Proxy, Error};
#[derive(SerializeDict, Debug, Type, Default)]
#[zvariant(signature = "dict")]
struct TransferOptions {
writeable: Option<bool>,
#[zvariant(rename = "autostop")]
auto_stop: Option<bool>,
}
impl TransferOptions {
#[must_use]
pub fn writeable(mut self, writeable: impl Into<Option<bool>>) -> Self {
self.writeable = writeable.into();
self
}
#[must_use]
pub fn auto_stop(mut self, auto_stop: impl Into<Option<bool>>) -> Self {
self.auto_stop = auto_stop.into();
self
}
}
#[derive(Debug)]
#[doc(alias = "org.freedesktop.portal.FileTransfer")]
pub struct FileTransfer<'a>(Proxy<'a>);
impl<'a> FileTransfer<'a> {
pub async fn new() -> Result<FileTransfer<'a>, Error> {
let proxy = Proxy::new_documents("org.freedesktop.portal.FileTransfer").await?;
Ok(Self(proxy))
}
#[doc(alias = "AddFiles")]
pub async fn add_files(&self, key: &str, fds: &[&BorrowedFd<'_>]) -> Result<(), Error> {
let options: HashMap<&str, Value<'_>> = HashMap::new();
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) -> Result<Vec<String>, Error> {
let options: HashMap<&str, Value<'_>> = HashMap::new();
self.0.call("RetrieveFiles", &(key, options)).await
}
pub async fn start_transfer(&self, writeable: bool, auto_stop: bool) -> Result<String, Error> {
let options = TransferOptions::default()
.writeable(writeable)
.auto_stop(auto_stop);
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<'a> std::ops::Deref for FileTransfer<'a> {
type Target = zbus::Proxy<'a>;
fn deref(&self) -> &Self::Target {
&self.0
}
}