/*
* meli
*
* Copyright 2017-2018 Manos Pitsidianakis
*
* This file is part of meli.
*
* meli is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* meli is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with meli. If not, see .
*/
use std::{
collections::HashSet,
convert::TryFrom,
fmt::Write as _,
io::Write,
process::{Command, Stdio},
};
use melib::{
email::attachment_types::ContentType, list_management, mailto::Mailto, parser::BytesExt,
utils::datetime, Card, Draft, FlagOp, HeaderName, SpecialUsageMailbox,
};
use smallvec::SmallVec;
use super::*;
use crate::{accounts::JobRequest, jobs::JobId};
mod utils;
pub use utils::*;
mod thread;
pub use thread::*;
mod types;
pub use types::*;
pub mod state;
use state::*;
pub mod envelope;
pub use envelope::EnvelopeView;
pub mod filters;
pub use filters::*;
#[cfg(test)]
mod tests;
/// Contains an Envelope view, with sticky headers, a pager for the body, and
/// subviews for more menus
#[derive(Debug)]
pub struct MailView {
coordinates: Option<(AccountHash, MailboxHash, EnvelopeHash)>,
dirty: bool,
contact_selector: Option>>,
forward_dialog: Option>>>,
theme_default: ThemeAttribute,
active_jobs: HashSet,
initialized: bool,
state: MailViewState,
main_loop_handler: MainLoopHandler,
id: ComponentId,
}
impl std::fmt::Display for MailView {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.state.fmt(f)
}
}
impl Drop for MailView {
fn drop(&mut self) {
if let MailViewState::LoadingBody { ref mut handle, .. } = self.state {
if let Some(canceled) = handle.cancel() {
self.main_loop_handler
.send(UIEvent::StatusEvent(canceled).into());
}
}
}
}
impl MailView {
pub fn new(
coordinates: Option<(AccountHash, MailboxHash, EnvelopeHash)>,
initialize_now: bool,
context: &mut Context,
) -> Self {
let mut ret = Self {
coordinates,
dirty: true,
contact_selector: None,
forward_dialog: None,
theme_default: crate::conf::value(context, "mail.view.body"),
active_jobs: Default::default(),
initialized: false,
state: MailViewState::default(),
main_loop_handler: context.main_loop_handler.clone(),
id: ComponentId::default(),
};
if initialize_now {
ret.init_futures(context);
}
ret
}
fn init_futures(&mut self, context: &mut Context) {
log::trace!("MailView::init_futures");
self.theme_default = crate::conf::value(context, "mail.view.body");
let mut pending_action = None;
let Some(coordinates) = self.coordinates else {
return;
};
let account = &mut context.accounts[&coordinates.0];
if account.contains_key(coordinates.2) {
{
match account
.operation(coordinates.2)
.and_then(|mut op| op.as_bytes())
{
Ok(fut) => {
let mut handle = account
.main_loop_handler
.job_executor
.spawn_specialized("fetch_envelopes".into(), fut);
let job_id = handle.job_id;
pending_action = if let MailViewState::Init {
ref mut pending_action,
} = self.state
{
pending_action.take()
} else {
None
};
if let Ok(Some(bytes_result)) = try_recv_timeout!(&mut handle.chan) {
match bytes_result {
Ok(bytes) => {
MailViewState::load_bytes(self, bytes, context);
}
Err(err) => {
self.state = MailViewState::Error { err };
}
}
} else {
self.state = MailViewState::LoadingBody {
handle,
pending_action: pending_action.take(),
};
self.active_jobs.insert(job_id);
context
.replies
.push_back(UIEvent::StatusEvent(StatusEvent::NewJob(job_id)));
}
}
Err(err) => {
context.replies.push_back(UIEvent::StatusEvent(
StatusEvent::DisplayMessage(format!("Could not get message: {}", err)),
));
}
}
}
}
if let Some(p) = pending_action {
self.perform_action(p, context);
}
self.initialized = true;
}
fn perform_action(&mut self, action: PendingReplyAction, context: &mut Context) {
let Some(coordinates) = self.coordinates else {
return;
};
let (bytes, reply_body, env) = match self.state {
MailViewState::Init {
ref mut pending_action,
..
}
| MailViewState::LoadingBody {
ref mut pending_action,
..
} => {
*pending_action = Some(action);
return;
}
MailViewState::Loaded {
ref bytes,
ref env,
ref env_view,
..
} => (
bytes,
EnvelopeView::attachment_displays_to_text(&env_view.display, false),
env,
),
MailViewState::Error { .. } => {
return;
}
};
let composer = match action {
PendingReplyAction::Reply => {
Box::new(Composer::reply_to_select(coordinates, reply_body, context))
}
PendingReplyAction::ReplyToAuthor => {
Box::new(Composer::reply_to_author(coordinates, reply_body, context))
}
PendingReplyAction::ReplyToAll => {
Box::new(Composer::reply_to_all(coordinates, reply_body, context))
}
PendingReplyAction::ForwardAttachment => {
Box::new(Composer::forward(coordinates, bytes, env, true, context))
}
PendingReplyAction::ForwardInline => {
Box::new(Composer::forward(coordinates, bytes, env, false, context))
}
};
context
.replies
.push_back(UIEvent::Action(Tab(New(Some(composer)))));
}
pub fn update(
&mut self,
new_coordinates: (AccountHash, MailboxHash, EnvelopeHash),
context: &mut Context,
) {
if let MailViewState::LoadingBody { ref mut handle, .. } = self.state {
if let Some(canceled) = handle.cancel() {
context.replies.push_back(UIEvent::StatusEvent(canceled));
}
}
if self.coordinates != Some(new_coordinates) {
self.coordinates = Some(new_coordinates);
self.init_futures(context);
self.set_dirty(true);
}
}
fn start_contact_selector(&mut self, context: &mut Context) {
let Some(coordinates) = self.coordinates else {
return;
};
let account = &context.accounts[&coordinates.0];
if !account.contains_key(coordinates.2) {
context
.replies
.push_back(UIEvent::StatusEvent(StatusEvent::DisplayMessage(
"Email not found".into(),
)));
return;
}
let envelope: EnvelopeRef = account.collection.get_env(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())
.set_id(addr.get_email().into());
if let Some(display_name) = addr.get_display_name() {
new_card.set_name(display_name);
}
entries.push((new_card, format!("{}", addr)));
}
drop(envelope);
self.contact_selector = Some(Box::new(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;
}
}
impl Component for MailView {
fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
if !self.is_dirty() {
return;
}
let Some(coordinates) = self.coordinates else {
return;
};
if !self.initialized {
self.init_futures(context);
return;
}
{
let account = &context.accounts[&coordinates.0];
if !account.contains_key(coordinates.2) {
/* The envelope has been renamed or removed, so wait for the appropriate
* event to arrive */
return;
}
}
if let MailViewState::Loaded {
ref mut env_view, ..
} = self.state
{
{
let account = &mut context.accounts[&coordinates.0];
if !account.collection.get_env(coordinates.2).is_seen() {
if let Err(err) = account.set_flags(
coordinates.2.into(),
coordinates.1,
smallvec::smallvec![FlagOp::Set(Flag::SEEN)],
) {
context.replies.push_back(UIEvent::StatusEvent(
StatusEvent::DisplayMessage(format!(
"Could not set message as seen: {err}",
)),
));
}
}
}
env_view.draw(grid, area, context);
} else if let MailViewState::Error { ref err } = self.state {
grid.clear_area(area, self.theme_default);
context.dirty_areas.push_back(area);
context.replies.push_back(UIEvent::Notification {
title: Some("Failed to open e-mail".into()),
source: None,
body: err.to_string().into(),
kind: Some(NotificationType::Error(err.kind)),
});
log::error!("Failed to open envelope: {err}");
self.init_futures(context);
return;
} else {
grid.clear_area(area, self.theme_default);
context.dirty_areas.push_back(area);
return;
};
if let Some(ref mut s) = self.contact_selector.as_mut() {
s.draw(grid, area, context);
} else if let Some(ref mut s) = self.forward_dialog.as_mut() {
s.draw(grid, area, context);
}
self.dirty = false;
}
fn process_event(&mut self, mut event: &mut UIEvent, context: &mut Context) -> bool {
if let Some(ref mut s) = self.contact_selector {
if s.process_event(event, context) {
return true;
}
}
if let Some(ref mut s) = self.forward_dialog {
if s.process_event(event, context) {
return true;
}
}
let Some(coordinates) = self.coordinates else {
return false;
};
if coordinates.0.is_null() || coordinates.1.is_null() {
return false;
}
/* If envelope data is loaded, pass it to envelope views */
if self.state.process_event(event, context) {
return true;
}
match (
&mut self.contact_selector,
&mut self.forward_dialog,
&mut event,
) {
(Some(ref s), _, UIEvent::FinishedUIDialog(id, results)) if *id == s.id() => {
if let Some(results) = results.downcast_ref::>() {
let account = &mut context.accounts[&coordinates.0];
{
for card in results.iter() {
account.address_book.add_card(card.clone());
}
}
self.contact_selector = None;
}
self.set_dirty(true);
return true;
}
(_, Some(ref s), UIEvent::FinishedUIDialog(id, result)) if *id == s.id() => {
if let Some(result) = result.downcast_ref::