Browse Source

ui/accounts: add save_special method for mail

Add save_special method in Accounts. save_special() saves mail to the
first folder_type (eg Draft, Sent, Inbox) folder it finds or to any
other as fall over.
tags/alpha-0.4.1
Manos Pitsidianakis 2 years ago
parent
commit
3a3b815b3a
Signed by: epilys GPG Key ID: 73627C2F690DF710
  1. 101
      ui/src/components/mail/compose.rs
  2. 13
      ui/src/components/mail/view.rs
  3. 47
      ui/src/conf/accounts.rs

101
ui/src/components/mail/compose.rs

@ -357,64 +357,6 @@ impl Composer {
}
}
}
fn save_draft(&mut self, context: &mut Context, folder_type: SpecialUseMailbox, flags: Flag) {
let mut failure = true;
let draft = std::mem::replace(&mut self.draft, Draft::default());
let draft = draft.finalise().unwrap();
for folder in &[
&context.accounts[self.account_cursor].special_use_folder(folder_type),
&context.accounts[self.account_cursor].special_use_folder(SpecialUseMailbox::Inbox),
&context.accounts[self.account_cursor].special_use_folder(SpecialUseMailbox::Normal),
] {
if folder.is_none() {
continue;
}
let folder = folder.unwrap();
if let Err(e) =
context.accounts[self.account_cursor].save(draft.as_bytes(), folder, Some(flags))
{
debug!("{:?} could not save draft msg", e);
log(
format!(
"Could not save draft in '{}' folder: {}.",
folder,
e.to_string()
),
ERROR,
);
context.replies.push_back(UIEvent::Notification(
Some(format!("Could not save draft in '{}' folder.", folder)),
e.into(),
Some(NotificationType::ERROR),
));
} else {
failure = false;
break;
}
}
if failure {
let file = create_temp_file(draft.as_bytes(), None, None, false);
debug!("message saved in {}", file.path.display());
log(
format!(
"Message was stored in {} so that you can restore it manually.",
file.path.display()
),
INFO,
);
context.replies.push_back(UIEvent::Notification(
Some("Could not save in any folder".into()),
format!(
"Message was stored in {} so that you can restore it manually.",
file.path.display()
),
Some(NotificationType::INFO),
));
}
}
}
impl Component for Composer {
@ -632,16 +574,19 @@ impl Component for Composer {
context,
self.account_cursor,
self.draft.clone(),
SpecialUseMailbox::Sent,
Flag::SEEN,
) {
self.save_draft(context, SpecialUseMailbox::Sent, Flag::SEEN);
context
.replies
.push_back(UIEvent::Action(Tab(Kill(self.id))));
} else {
self.save_draft(
save_draft(
self.draft.clone().finalise().unwrap().as_bytes(),
context,
SpecialUseMailbox::Drafts,
Flag::SEEN | Flag::DRAFT,
self.account_cursor,
);
}
}
@ -685,10 +630,12 @@ impl Component for Composer {
}
'n' => {}
'y' => {
self.save_draft(
save_draft(
self.draft.clone().finalise().unwrap().as_bytes(),
context,
SpecialUseMailbox::Drafts,
Flag::SEEN | Flag::DRAFT,
self.account_cursor,
);
context.replies.push_back(UIEvent::Action(Tab(Kill(u))));
return true;
@ -1122,6 +1069,8 @@ pub fn send_draft(
context: &mut Context,
account_cursor: usize,
mut draft: Draft,
folder_type: SpecialUseMailbox,
flags: Flag,
) -> bool {
use std::io::Write;
use std::process::{Command, Stdio};
@ -1136,6 +1085,7 @@ pub fn send_draft(
));
return false;
}
let bytes;
let (cmd, args) = (parts[0], &parts[1..]);
let mut msmtp = Command::new(cmd)
.args(args)
@ -1204,9 +1154,9 @@ pub fn send_draft(
}
draft.attachments.push(output.unwrap());
}
let draft = draft.finalise().unwrap();
bytes = draft.finalise().unwrap();
stdin
.write_all(draft.as_bytes())
.write_all(bytes.as_bytes())
.expect("Failed to write to stdin");
}
let output = msmtp.wait().expect("Failed to wait on mailer");
@ -1235,5 +1185,30 @@ pub fn send_draft(
));
log(error_message, ERROR);
}
save_draft(
bytes.as_bytes(),
context,
folder_type,
flags,
account_cursor,
);
true
}
pub fn save_draft(
bytes: &[u8],
context: &mut Context,
folder_type: SpecialUseMailbox,
flags: Flag,
account_cursor: usize,
) {
if let Err(MeliError { summary, details }) =
context.accounts[account_cursor].save_special(bytes, folder_type, flags)
{
context.replies.push_back(UIEvent::Notification(
summary,
details,
Some(NotificationType::ERROR),
));
}
}

13
ui/src/components/mail/view.rs

@ -1221,7 +1221,7 @@ impl Component for MailView {
self.coordinates.0,
),
);
if super::compose::send_draft(
return super::compose::send_draft(
ToggleFlag::False,
/* FIXME: refactor to avoid unsafe.
*
@ -1232,14 +1232,9 @@ impl Component for MailView {
unsafe { &mut *(unsafe_context) },
self.coordinates.0,
draft,
) {
context.replies.push_back(UIEvent::Notification(
Some("Sent unsubscribe email.".into()),
"Sent unsubscribe email".to_string(),
None,
));
return true;
}
SpecialUseMailbox::Sent,
Flag::SEEN,
);
}
}
list_management::ListAction::Url(url) => {

47
ui/src/conf/accounts.rs

@ -873,6 +873,53 @@ impl Account {
}
}
pub fn save_special(
&self,
bytes: &[u8],
folder_type: SpecialUseMailbox,
flags: Flag,
) -> Result<()> {
let mut failure = true;
for folder in &[
self.special_use_folder(folder_type),
self.special_use_folder(SpecialUseMailbox::Inbox),
self.special_use_folder(SpecialUseMailbox::Normal),
] {
if folder.is_none() {
continue;
}
let folder = folder.unwrap();
if let Err(e) = self.save(bytes, folder, Some(flags)) {
debug!("{:?} could not save msg", e);
melib::log(
format!("Could not save in '{}' folder: {}.", folder, e.to_string()),
melib::ERROR,
);
} else {
failure = false;
break;
}
}
if failure {
let file = crate::types::create_temp_file(bytes, None, None, false);
debug!("message saved in {}", file.path.display());
melib::log(
format!(
"Message was stored in {} so that you can restore it manually.",
file.path.display()
),
melib::INFO,
);
return Err(MeliError::new(format!(
"Message was stored in {} so that you can restore it manually.",
file.path.display()
))
.set_summary("Could not save in any folder"));
}
Ok(())
}
pub fn save(&self, bytes: &[u8], folder: &str, flags: Option<Flag>) -> Result<()> {
if self.settings.account.read_only() {
return Err(MeliError::new(format!(

Loading…
Cancel
Save