Add add_addresses_to_contacts command

Manos Pitsidianakis 2021-09-16 16:27:21 +03:00
parent 32901f57d2
commit b88c3c573d
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
3 changed files with 59 additions and 25 deletions

View File

@ -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<Action, MeliError> {

View File

@ -78,6 +78,7 @@ pub enum ViewAction {
Filter(String),
SaveAttachment(usize, String),
ExportMail(String),
AddAddressesToContacts,
}
#[derive(Debug)]

View File

@ -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(''))