From 3a3b815b3a9feef17ff06ae6c37d9f40ddc11ee5 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Tue, 3 Dec 2019 13:27:35 +0200 Subject: [PATCH] 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. --- ui/src/components/mail/compose.rs | 101 +++++++++++------------------- ui/src/components/mail/view.rs | 13 ++-- ui/src/conf/accounts.rs | 47 ++++++++++++++ 3 files changed, 89 insertions(+), 72 deletions(-) diff --git a/ui/src/components/mail/compose.rs b/ui/src/components/mail/compose.rs index 5edbd939..25c136fc 100644 --- a/ui/src/components/mail/compose.rs +++ b/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), + )); + } +} diff --git a/ui/src/components/mail/view.rs b/ui/src/components/mail/view.rs index 425411f2..51af4a83 100644 --- a/ui/src/components/mail/view.rs +++ b/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) => { diff --git a/ui/src/conf/accounts.rs b/ui/src/conf/accounts.rs index 409383da..7f22f690 100644 --- a/ui/src/conf/accounts.rs +++ b/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) -> Result<()> { if self.settings.account.read_only() { return Err(MeliError::new(format!(