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.
jmap
Manos Pitsidianakis 2019-12-03 13:27:35 +02:00
parent a059e4ad4c
commit 3a3b815b3a
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
3 changed files with 89 additions and 72 deletions

View File

@ -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),
));
}
}

View File

@ -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) => {

View File

@ -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!(