/*
* meli - ui crate
*
* 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 <http://www.gnu.org/licenses/>.
*/
use super::*;
use melib::Draft;
use mime_apps::query_mime_info;
use std::str::FromStr;
#[derive(Debug, PartialEq)]
enum Cursor {
Headers,
Body,
//Attachments,
}
#[derive(Debug)]
pub struct Composer {
reply_context: Option<((usize, usize), Box<ThreadView>)>, // (folder_index, thread_node_index)
account_cursor: usize,
cursor: Cursor,
pager: Pager,
draft: Draft,
form: FormWidget,
mode: ViewMode,
dirty: bool,
initialized: bool,
id: ComponentId,
}
impl Default for Composer {
fn default() -> Self {
Composer {
reply_context: None,
account_cursor: 0,
cursor: Cursor::Headers,
pager: Pager::default(),
draft: Draft::default(),
form: FormWidget::default(),
mode: ViewMode::Edit,
dirty: true,
initialized: false,
id: ComponentId::new_v4(),
}
}
}
#[derive(Debug)]
enum ViewMode {
Discard(Uuid),
Edit,
//Selector(Selector),
Overview,
}
impl ViewMode {
fn is_discard(&self) -> bool {
if let ViewMode::Discard(_) = self {
true
} else {
false
}
}
fn is_edit(&self) -> bool {
if let ViewMode::Edit = self {
true
} else {
false
}
}
fn is_overview(&self) -> bool {
if let ViewMode::Overview = self {
true
} else {
false
}
}
}
impl fmt::Display for Composer {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// TODO display subject/info
if self.reply_context.is_some() {
write!(f, "reply: {:8}", self.draft.headers()["Subject"])
} else {
write!(f, "compose")
}
}
}
impl Composer {
const DESCRIPTION: &'static str = "compose";
pub fn new(account_cursor: usize) -> Self {
Composer {
account_cursor,
id: ComponentId::new_v4(),
..Default::default()
}
}
/*
* coordinates: (account index, mailbox index, root set thread_node index)
* msg: index of message we reply to in thread_nodes
* context: current context
*/
pub fn edit(account_pos: usize, h: EnvelopeHash, context: &Context) -> Self {
let mut ret = Composer::default();
let op = context.accounts[account_pos].operation(h);
let envelope: &Envelope = context.accounts[account_pos].get_env(&h);
ret.draft = Draft::edit(envelope, op);
ret.account_cursor = account_pos;
ret
}
pub fn with_context(
coordinates: (usize, usize, usize),
msg: ThreadHash,
context: &Context,
) -> Self {
let account = &context.accounts[coordinates.0];
let mailbox = &account[coordinates.1].unwrap();
let threads = &account.collection.threads[&mailbox.folder.hash()];
let thread_nodes = &threads.thread_nodes();
let mut ret = Composer::default();
let p = &thread_nodes[&msg];
let parent_message = &account.collection[&p.message().unwrap()];
let mut op = account.operation(parent_message.hash());
let parent_bytes = op.as_bytes();
ret.draft = Draft::new_reply(parent_message, parent_bytes.unwrap());
ret.draft.headers_mut().insert(
"Subject".into(),
if p.show_subject() {
format!(
"Re: {}",
account.get_env(&p.message().unwrap()).subject().clone()
)
} else {
account.get_env(&p.message().unwrap()).subject().into()
},
);
ret.account_cursor = coordinates.0;
ret.reply_context = Some((
(coordinates.1, coordinates.2),
Box::new(ThreadView::new(coordinates, Some(msg), context)),
));
ret
}
pub fn set_draft(&mut self, draft: Draft) {
self.draft = draft;
self.update_form();
}
fn update_draft(&mut self) {
let header_values = self.form.values_mut();
let draft_header_map = self.draft.headers_mut();
/* avoid extra allocations by updating values instead of inserting */
for