From 4ae7a57d45b3b1a2b3bc195a99fd509b7f5e5760 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Tue, 23 Jun 2020 19:25:01 +0300 Subject: [PATCH] Add save-draft command --- meli.1 | 2 ++ src/components/mail/compose.rs | 39 ++++++++++++++++++++++++++-------- src/conf/accounts.rs | 11 +++++----- src/execute.rs | 13 +++++++++++- src/execute/actions.rs | 1 + 5 files changed, 51 insertions(+), 15 deletions(-) diff --git a/meli.1 b/meli.1 index 083e86e4..f20a55fc 100644 --- a/meli.1 +++ b/meli.1 @@ -355,6 +355,8 @@ remove attachment with given index .It Cm toggle sign toggle between signing and not signing this message. If the gpg invocation fails then the mail won't be sent. +.It Cm save-draft +saves a copy of the draft in the Draft folder .El .Pp generic commands: diff --git a/src/components/mail/compose.rs b/src/components/mail/compose.rs index 141d26a9..c7a93b65 100644 --- a/src/components/mail/compose.rs +++ b/src/components/mail/compose.rs @@ -1070,6 +1070,16 @@ impl Component for Composer { self.dirty = true; return true; } + Action::Compose(ComposeAction::SaveDraft) => { + save_draft( + self.draft.clone().finalise().unwrap().as_bytes(), + context, + SpecialUsageMailbox::Drafts, + Flag::SEEN | Flag::DRAFT, + self.account_cursor, + ); + return true; + } Action::Compose(ComposeAction::ToggleSign) => { let is_true = self.sign_mail.is_true(); self.sign_mail = ToggleFlag::from(!is_true); @@ -1338,14 +1348,25 @@ pub fn save_draft( flags: Flag, account_cursor: usize, ) { - if let Err(MeliError { - summary, details, .. - }) = context.accounts[account_cursor].save_special(bytes, mailbox_type, flags) - { - context.replies.push_back(UIEvent::Notification( - summary.map(|s| s.into()), - details.into(), - Some(NotificationType::ERROR), - )); + match context.accounts[account_cursor].save_special(bytes, mailbox_type, flags) { + Err(MeliError { + summary, details, .. + }) => { + context.replies.push_back(UIEvent::Notification( + summary.map(|s| s.into()), + details.into(), + Some(NotificationType::ERROR), + )); + } + Ok(mailbox_hash) => { + context.replies.push_back(UIEvent::Notification( + Some("Draft saved".into()), + format!( + "Draft saved in `{}`", + &context.accounts[account_cursor].mailbox_entries[&mailbox_hash].name + ), + Some(NotificationType::INFO), + )); + } } } diff --git a/src/conf/accounts.rs b/src/conf/accounts.rs index ae000bde..1fe29380 100644 --- a/src/conf/accounts.rs +++ b/src/conf/accounts.rs @@ -896,8 +896,8 @@ impl Account { bytes: &[u8], mailbox_type: SpecialUsageMailbox, flags: Flag, - ) -> Result<()> { - let mut failure = true; + ) -> Result { + let mut saved_at: Option = None; for mailbox in &[ self.special_use_mailbox(mailbox_type), self.special_use_mailbox(SpecialUsageMailbox::Inbox), @@ -918,12 +918,14 @@ impl Account { melib::ERROR, ); } else { - failure = false; + saved_at = Some(mailbox); break; } } - if failure { + if let Some(mailbox_hash) = saved_at { + Ok(mailbox_hash) + } else { let file = crate::types::create_temp_file(bytes, None, None, false); debug!("message saved in {}", file.path.display()); melib::log( @@ -939,7 +941,6 @@ impl Account { )) .set_summary("Could not save in any mailbox")); } - Ok(()) } pub fn save(&self, bytes: &[u8], mailbox_hash: MailboxHash, flags: Option) -> Result<()> { diff --git a/src/execute.rs b/src/execute.rs index abf2d321..2eaf1b63 100644 --- a/src/execute.rs +++ b/src/execute.rs @@ -482,6 +482,17 @@ define_commands!([ } ) }, + { tags: ["save-draft"], + desc: "save draft", + tokens: &[One(Literal("save-draft"))], + parser:( + fn save_draft(input: &[u8]) -> IResult<&[u8], Action> { + let (input, _) = tag("save-draft")(input.trim())?; + let (input, _) = eof(input)?; + Ok((input, Compose(SaveDraft))) + } + ) + }, { tags: ["toggle sign "], desc: "switch between sign/unsign for this draft", tokens: &[One(Literal("toggle")), One(Literal("sign"))], @@ -678,7 +689,7 @@ fn listing_action(input: &[u8]) -> IResult<&[u8], Action> { } fn compose_action(input: &[u8]) -> IResult<&[u8], Action> { - alt((add_attachment, remove_attachment, toggle_sign))(input) + alt((add_attachment, remove_attachment, toggle_sign, save_draft))(input) } fn account_action(input: &[u8]) -> IResult<&[u8], Action> { diff --git a/src/execute/actions.rs b/src/execute/actions.rs index ea807838..4c1d6b4d 100644 --- a/src/execute/actions.rs +++ b/src/execute/actions.rs @@ -83,6 +83,7 @@ pub enum ComposeAction { AddAttachment(String), AddAttachmentPipe(String), RemoveAttachment(usize), + SaveDraft, ToggleSign, }