use std::os::fd::OwnedFd;
use serde::Serialize;
use zbus::zvariant::{self, SerializeDict, Type};
use super::{HandleToken, Request};
use crate::{
ActivationToken, Error, WindowIdentifier, proxy::Proxy,
window_identifier::MaybeWindowIdentifierExt,
};
#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct EmailOptions {
handle_token: HandleToken,
address: Option<String>,
addresses: Option<Vec<String>>,
cc: Option<Vec<String>>,
bcc: Option<Vec<String>>,
subject: Option<String>,
body: Option<String>,
attachment_fds: Option<Vec<zvariant::OwnedFd>>,
activation_token: Option<ActivationToken>,
}
#[derive(Debug)]
#[doc(alias = "org.freedesktop.portal.Email")]
struct EmailProxy(Proxy<'static>);
impl EmailProxy {
pub async fn new() -> Result<Self, Error> {
let proxy = Proxy::new_desktop("org.freedesktop.portal.Email").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.Email").await?;
Ok(Self(proxy))
}
#[doc(alias = "ComposeEmail")]
pub async fn compose(
&self,
identifier: Option<&WindowIdentifier>,
options: EmailOptions,
) -> Result<Request<()>, Error> {
let identifier = identifier.to_string_or_empty();
self.0
.empty_request(
&options.handle_token,
"ComposeEmail",
&(&identifier, &options),
)
.await
}
}
impl std::ops::Deref for EmailProxy {
type Target = zbus::Proxy<'static>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Debug, Default)]
#[doc(alias = "xdp_portal_compose_email")]
pub struct EmailRequest {
identifier: Option<WindowIdentifier>,
options: EmailOptions,
connection: Option<zbus::Connection>,
}
impl EmailRequest {
#[must_use]
pub fn identifier(mut self, identifier: impl Into<Option<WindowIdentifier>>) -> Self {
self.identifier = identifier.into();
self
}
#[must_use]
pub fn address<'a>(mut self, address: impl Into<Option<&'a str>>) -> Self {
self.options.address = address.into().map(ToOwned::to_owned);
self
}
#[must_use]
pub fn addresses<P: IntoIterator<Item = I>, I: AsRef<str> + Type + Serialize>(
mut self,
addresses: impl Into<Option<P>>,
) -> Self {
self.options.addresses = addresses
.into()
.map(|a| a.into_iter().map(|s| s.as_ref().to_owned()).collect());
self
}
#[must_use]
pub fn bcc<P: IntoIterator<Item = I>, I: AsRef<str> + Type + Serialize>(
mut self,
bcc: impl Into<Option<P>>,
) -> Self {
self.options.bcc = bcc
.into()
.map(|a| a.into_iter().map(|s| s.as_ref().to_owned()).collect());
self
}
#[must_use]
pub fn cc<P: IntoIterator<Item = I>, I: AsRef<str> + Type + Serialize>(
mut self,
cc: impl Into<Option<P>>,
) -> Self {
self.options.cc = cc
.into()
.map(|a| a.into_iter().map(|s| s.as_ref().to_owned()).collect());
self
}
#[must_use]
pub fn subject<'a>(mut self, subject: impl Into<Option<&'a str>>) -> Self {
self.options.subject = subject.into().map(ToOwned::to_owned);
self
}
#[must_use]
pub fn body<'a>(mut self, body: impl Into<Option<&'a str>>) -> Self {
self.options.body = body.into().map(ToOwned::to_owned);
self
}
#[must_use]
pub fn attach(mut self, attachment: OwnedFd) -> Self {
self.add_attachment(attachment);
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 fn add_attachment(&mut self, attachment: OwnedFd) {
let attachment = zvariant::OwnedFd::from(attachment);
match self.options.attachment_fds {
Some(ref mut attachments) => attachments.push(attachment),
None => {
self.options.attachment_fds.replace(vec![attachment]);
}
};
}
#[must_use]
pub fn connection(mut self, connection: Option<zbus::Connection>) -> Self {
self.connection = connection;
self
}
pub async fn send(self) -> Result<Request<()>, Error> {
let proxy = if let Some(connection) = self.connection {
EmailProxy::with_connection(connection).await?
} else {
EmailProxy::new().await?
};
proxy.compose(self.identifier.as_ref(), self.options).await
}
}