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
parent
a059e4ad4c
commit
3a3b815b3a
|
@ -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 {
|
impl Component for Composer {
|
||||||
|
@ -632,16 +574,19 @@ impl Component for Composer {
|
||||||
context,
|
context,
|
||||||
self.account_cursor,
|
self.account_cursor,
|
||||||
self.draft.clone(),
|
self.draft.clone(),
|
||||||
|
SpecialUseMailbox::Sent,
|
||||||
|
Flag::SEEN,
|
||||||
) {
|
) {
|
||||||
self.save_draft(context, SpecialUseMailbox::Sent, Flag::SEEN);
|
|
||||||
context
|
context
|
||||||
.replies
|
.replies
|
||||||
.push_back(UIEvent::Action(Tab(Kill(self.id))));
|
.push_back(UIEvent::Action(Tab(Kill(self.id))));
|
||||||
} else {
|
} else {
|
||||||
self.save_draft(
|
save_draft(
|
||||||
|
self.draft.clone().finalise().unwrap().as_bytes(),
|
||||||
context,
|
context,
|
||||||
SpecialUseMailbox::Drafts,
|
SpecialUseMailbox::Drafts,
|
||||||
Flag::SEEN | Flag::DRAFT,
|
Flag::SEEN | Flag::DRAFT,
|
||||||
|
self.account_cursor,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -685,10 +630,12 @@ impl Component for Composer {
|
||||||
}
|
}
|
||||||
'n' => {}
|
'n' => {}
|
||||||
'y' => {
|
'y' => {
|
||||||
self.save_draft(
|
save_draft(
|
||||||
|
self.draft.clone().finalise().unwrap().as_bytes(),
|
||||||
context,
|
context,
|
||||||
SpecialUseMailbox::Drafts,
|
SpecialUseMailbox::Drafts,
|
||||||
Flag::SEEN | Flag::DRAFT,
|
Flag::SEEN | Flag::DRAFT,
|
||||||
|
self.account_cursor,
|
||||||
);
|
);
|
||||||
context.replies.push_back(UIEvent::Action(Tab(Kill(u))));
|
context.replies.push_back(UIEvent::Action(Tab(Kill(u))));
|
||||||
return true;
|
return true;
|
||||||
|
@ -1122,6 +1069,8 @@ pub fn send_draft(
|
||||||
context: &mut Context,
|
context: &mut Context,
|
||||||
account_cursor: usize,
|
account_cursor: usize,
|
||||||
mut draft: Draft,
|
mut draft: Draft,
|
||||||
|
folder_type: SpecialUseMailbox,
|
||||||
|
flags: Flag,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::process::{Command, Stdio};
|
use std::process::{Command, Stdio};
|
||||||
|
@ -1136,6 +1085,7 @@ pub fn send_draft(
|
||||||
));
|
));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
let bytes;
|
||||||
let (cmd, args) = (parts[0], &parts[1..]);
|
let (cmd, args) = (parts[0], &parts[1..]);
|
||||||
let mut msmtp = Command::new(cmd)
|
let mut msmtp = Command::new(cmd)
|
||||||
.args(args)
|
.args(args)
|
||||||
|
@ -1204,9 +1154,9 @@ pub fn send_draft(
|
||||||
}
|
}
|
||||||
draft.attachments.push(output.unwrap());
|
draft.attachments.push(output.unwrap());
|
||||||
}
|
}
|
||||||
let draft = draft.finalise().unwrap();
|
bytes = draft.finalise().unwrap();
|
||||||
stdin
|
stdin
|
||||||
.write_all(draft.as_bytes())
|
.write_all(bytes.as_bytes())
|
||||||
.expect("Failed to write to stdin");
|
.expect("Failed to write to stdin");
|
||||||
}
|
}
|
||||||
let output = msmtp.wait().expect("Failed to wait on mailer");
|
let output = msmtp.wait().expect("Failed to wait on mailer");
|
||||||
|
@ -1235,5 +1185,30 @@ pub fn send_draft(
|
||||||
));
|
));
|
||||||
log(error_message, ERROR);
|
log(error_message, ERROR);
|
||||||
}
|
}
|
||||||
|
save_draft(
|
||||||
|
bytes.as_bytes(),
|
||||||
|
context,
|
||||||
|
folder_type,
|
||||||
|
flags,
|
||||||
|
account_cursor,
|
||||||
|
);
|
||||||
true
|
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),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1221,7 +1221,7 @@ impl Component for MailView {
|
||||||
self.coordinates.0,
|
self.coordinates.0,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
if super::compose::send_draft(
|
return super::compose::send_draft(
|
||||||
ToggleFlag::False,
|
ToggleFlag::False,
|
||||||
/* FIXME: refactor to avoid unsafe.
|
/* FIXME: refactor to avoid unsafe.
|
||||||
*
|
*
|
||||||
|
@ -1232,14 +1232,9 @@ impl Component for MailView {
|
||||||
unsafe { &mut *(unsafe_context) },
|
unsafe { &mut *(unsafe_context) },
|
||||||
self.coordinates.0,
|
self.coordinates.0,
|
||||||
draft,
|
draft,
|
||||||
) {
|
SpecialUseMailbox::Sent,
|
||||||
context.replies.push_back(UIEvent::Notification(
|
Flag::SEEN,
|
||||||
Some("Sent unsubscribe email.".into()),
|
);
|
||||||
"Sent unsubscribe email".to_string(),
|
|
||||||
None,
|
|
||||||
));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
list_management::ListAction::Url(url) => {
|
list_management::ListAction::Url(url) => {
|
||||||
|
|
|
@ -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<()> {
|
pub fn save(&self, bytes: &[u8], folder: &str, flags: Option<Flag>) -> Result<()> {
|
||||||
if self.settings.account.read_only() {
|
if self.settings.account.read_only() {
|
||||||
return Err(MeliError::new(format!(
|
return Err(MeliError::new(format!(
|
||||||
|
|
Loading…
Reference in New Issue