use std::os::unix::prelude::AsRawFd;
use serde::Serialize;
use zbus::zvariant::{DeserializeDict, Fd, SerializeDict, Type};
use super::{HandleToken, DESTINATION, PATH};
use crate::{helpers::call_basic_response_method, Error, WindowIdentifier};
#[derive(SerializeDict, DeserializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
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()
}
#[must_use]
pub fn address(mut self, address: &str) -> Self {
self.address = Some(address.to_string());
self
}
pub fn set_address(&mut self, address: &str) {
self.address = Some(address.to_string());
}
#[must_use]
pub fn addresses(mut self, addresses: &[impl AsRef<str> + Type + Serialize]) -> Self {
self.addresses = Some(addresses.iter().map(|s| s.as_ref().to_string()).collect());
self
}
pub fn set_addresses(&mut self, addresses: &[impl AsRef<str> + Type + Serialize]) {
self.addresses = Some(addresses.iter().map(|s| s.as_ref().to_string()).collect());
}
#[must_use]
pub fn bcc(mut self, bcc: &[impl AsRef<str> + Type + Serialize]) -> Self {
self.bcc = Some(bcc.iter().map(|s| s.as_ref().to_string()).collect());
self
}
pub fn set_bcc(&mut self, bcc: &[impl AsRef<str> + Type + Serialize]) {
self.bcc = Some(bcc.iter().map(|s| s.as_ref().to_string()).collect());
}
#[must_use]
pub fn cc(mut self, cc: &[impl AsRef<str> + Type + Serialize]) -> Self {
self.cc = Some(cc.iter().map(|s| s.as_ref().to_string()).collect());
self
}
pub fn set_cc(&mut self, cc: &[impl AsRef<str> + Type + Serialize]) {
self.cc = Some(cc.iter().map(|s| s.as_ref().to_string()).collect());
}
#[must_use]
pub fn subject(mut self, subject: &str) -> Self {
self.subject = Some(subject.to_string());
self
}
pub fn set_subject(&mut self, subject: &str) {
self.subject = Some(subject.to_string());
}
#[must_use]
pub fn body(mut self, body: &str) -> Self {
self.body = Some(body.to_string());
self
}
pub fn set_body(&mut self, body: &str) {
self.body = Some(body.to_string());
}
#[must_use]
pub fn attach(mut self, attachment: &impl AsRawFd) -> 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::Proxy<'a>);
impl<'a> EmailProxy<'a> {
pub async fn new(connection: &zbus::Connection) -> Result<EmailProxy<'a>, Error> {
let proxy = zbus::ProxyBuilder::new_bare(connection)
.interface("org.freedesktop.portal.Email")?
.path(PATH)?
.destination(DESTINATION)?
.build()
.await?;
Ok(Self(proxy))
}
pub fn inner(&self) -> &zbus::Proxy<'_> {
&self.0
}
#[doc(alias = "ComposeEmail")]
pub async fn compose_email(
&self,
identifier: &WindowIdentifier,
email: Email,
) -> Result<(), Error> {
call_basic_response_method(
self.inner(),
&email.handle_token,
"ComposeEmail",
&(&identifier, &email),
)
.await
}
}
#[doc(alias = "xdp_portal_compose_email")]
pub async fn compose(identifier: &WindowIdentifier, email: Email) -> Result<(), Error> {
let connection = zbus::Connection::session().await?;
let proxy = EmailProxy::new(&connection).await?;
proxy.compose_email(identifier, email).await?;
Ok(())
}