use super::{HandleToken, DESTINATION, PATH};
use crate::{helpers::call_basic_response_method, Error, WindowIdentifier};
use serde::Serialize;
use std::os::unix::prelude::AsRawFd;
use zvariant::Fd;
use zvariant_derive::{DeserializeDict, SerializeDict, TypeDict};
#[derive(SerializeDict, DeserializeDict, TypeDict, Clone, Debug, Default)]
pub struct Email {
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<Fd>>,
}
impl Email {
pub fn new() -> Self {
Self::default()
}
pub fn address(mut self, address: &str) -> Self {
self.address = Some(address.to_string());
self
}
pub fn addresses<S: AsRef<str> + zvariant::Type + Serialize>(
mut self,
addresses: &[S],
) -> Self {
self.addresses = Some(addresses.iter().map(|s| s.as_ref().to_string()).collect());
self
}
pub fn bcc<S: AsRef<str> + zvariant::Type + Serialize>(mut self, bcc: &[S]) -> Self {
self.bcc = Some(bcc.iter().map(|s| s.as_ref().to_string()).collect());
self
}
pub fn cc<S: AsRef<str> + zvariant::Type + Serialize>(mut self, cc: &[S]) -> Self {
self.cc = Some(cc.iter().map(|s| s.as_ref().to_string()).collect());
self
}
pub fn subject(mut self, subject: &str) -> Self {
self.subject = Some(subject.to_string());
self
}
pub fn body(mut self, body: &str) -> Self {
self.body = Some(body.to_string());
self
}
pub fn attach<F: AsRawFd>(mut self, attachment: &F) -> Self {
let attachment = Fd::from(attachment.as_raw_fd());
match self.attachment_fds {
Some(ref mut attachments) => attachments.push(attachment),
None => {
self.attachment_fds.replace(vec![attachment]);
}
};
self
}
}
#[derive(Debug)]
#[doc(alias = "org.freedesktop.portal.Email")]
pub struct EmailProxy<'a>(zbus::azync::Proxy<'a>);
impl<'a> EmailProxy<'a> {
pub async fn new(connection: &zbus::azync::Connection) -> Result<EmailProxy<'a>, Error> {
let proxy = zbus::ProxyBuilder::new_bare(connection)
.interface("org.freedesktop.portal.Email")
.path(PATH)?
.destination(DESTINATION)
.build_async()
.await?;
Ok(Self(proxy))
}
pub fn inner(&self) -> &zbus::azync::Proxy<'_> {
&self.0
}
#[doc(alias = "ComposeEmail")]
pub async fn compose_email(
&self,
identifier: WindowIdentifier,
email: Email,
) -> Result<(), Error> {
call_basic_response_method(
&self.0,
&email.handle_token,
"ComposeEmail",
&(identifier, &email),
)
.await
}
}
#[doc(alias = "xdp_portal_compose_email")]
pub async fn compose(window_identifier: WindowIdentifier, email: Email) -> Result<(), Error> {
let connection = zbus::azync::Connection::new_session().await?;
let proxy = EmailProxy::new(&connection).await?;
proxy.compose_email(window_identifier, email).await?;
Ok(())
}