From b88c3c573d271c0588efb4eaa2cb8c6c9c6c08e0 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Thu, 16 Sep 2021 16:27:21 +0300 Subject: [PATCH] Add add_addresses_to_contacts command --- src/command.rs | 19 ++++++++++- src/command/actions.rs | 1 + src/components/mail/view.rs | 64 +++++++++++++++++++++++-------------- 3 files changed, 59 insertions(+), 25 deletions(-) diff --git a/src/command.rs b/src/command.rs index a5d45fc6..d6b845a8 100644 --- a/src/command.rs +++ b/src/command.rs @@ -742,6 +742,17 @@ Alternatives(&[to_stream!(One(Literal("add-attachment")), One(Filepath)), to_str } ) }, + { tags: ["add-addresses-to-contacts "], + desc: "add-addresses-to-contacts", + tokens: &[One(Literal("add-addresses-to-contacts"))], + parser:( + fn add_addresses_to_contacts(input: &[u8]) -> IResult<&[u8], Action> { + let (input, _) = tag("add-addresses-to-contacts")(input.trim())?; + let (input, _) = eof(input)?; + Ok((input, View(AddAddressesToContacts))) + } + ) + }, { tags: ["tag", "tag add", "tag remove"], desc: "tag [add/remove], edits message's tags.", tokens: &[One(Literal("tag")), One(Alternatives(&[to_stream!(One(Literal("add"))), to_stream!(One(Literal("remove")))]))], @@ -900,7 +911,13 @@ fn account_action(input: &[u8]) -> IResult<&[u8], Action> { } fn view(input: &[u8]) -> IResult<&[u8], Action> { - alt((filter, pipe, save_attachment, export_mail))(input) + alt(( + filter, + pipe, + save_attachment, + export_mail, + add_addresses_to_contacts, + ))(input) } pub fn parse_command(input: &[u8]) -> Result { diff --git a/src/command/actions.rs b/src/command/actions.rs index 66bd7a73..6439e0b6 100644 --- a/src/command/actions.rs +++ b/src/command/actions.rs @@ -78,6 +78,7 @@ pub enum ViewAction { Filter(String), SaveAttachment(usize, String), ExportMail(String), + AddAddressesToContacts, } #[derive(Debug)] diff --git a/src/components/mail/view.rs b/src/components/mail/view.rs index ee142d80..b2e04b54 100644 --- a/src/components/mail/view.rs +++ b/src/components/mail/view.rs @@ -1030,6 +1030,41 @@ impl MailView { )))); None } + + fn start_contact_selector(&mut self, context: &mut Context) { + let account = &context.accounts[&self.coordinates.0]; + if !account.contains_key(self.coordinates.2) { + context + .replies + .push_back(UIEvent::StatusEvent(StatusEvent::DisplayMessage( + "Email not found".into(), + ))); + return; + } + let envelope: EnvelopeRef = account.collection.get_env(self.coordinates.2); + + let mut entries = Vec::new(); + for addr in envelope.from().iter().chain(envelope.to().iter()) { + let mut new_card: Card = Card::new(); + new_card.set_email(addr.get_email()); + if let Some(display_name) = addr.get_display_name() { + new_card.set_name(display_name); + } + entries.push((new_card, format!("{}", addr))); + } + drop(envelope); + self.mode = ViewMode::ContactSelector(Selector::new( + "select contacts to add", + entries, + false, + Some(Box::new(move |id: ComponentId, results: &[Card]| { + Some(UIEvent::FinishedUIDialog(id, Box::new(results.to_vec()))) + })), + context, + )); + self.dirty = true; + self.initialised = false; + } } impl Component for MailView { @@ -1943,36 +1978,17 @@ impl Component for MailView { ); return true; } + UIEvent::Action(View(ViewAction::AddAddressesToContacts)) => { + self.start_contact_selector(context); + return true; + } UIEvent::Input(ref key) if !self.mode.is_contact_selector() && shortcut!( key == shortcuts[MailView::DESCRIPTION]["add_addresses_to_contacts"] ) => { - let account = &context.accounts[&self.coordinates.0]; - let envelope: EnvelopeRef = account.collection.get_env(self.coordinates.2); - - let mut entries = Vec::new(); - for addr in envelope.from().iter().chain(envelope.to().iter()) { - let mut new_card: Card = Card::new(); - new_card.set_email(addr.get_email()); - if let Some(display_name) = addr.get_display_name() { - new_card.set_name(display_name); - } - entries.push((new_card, format!("{}", addr))); - } - drop(envelope); - self.mode = ViewMode::ContactSelector(Selector::new( - "select contacts to add", - entries, - false, - Some(Box::new(move |id: ComponentId, results: &[Card]| { - Some(UIEvent::FinishedUIDialog(id, Box::new(results.to_vec()))) - })), - context, - )); - self.dirty = true; - self.initialised = false; + self.start_contact_selector(context); return true; } UIEvent::Input(Key::Esc) | UIEvent::Input(Key::Alt(''))