2019-02-15 09:06:42 +02:00
|
|
|
/*
|
|
|
|
* meli - contacts module
|
|
|
|
*
|
|
|
|
* Copyright 2019 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::*;
|
2020-05-10 21:14:49 +03:00
|
|
|
use std::collections::HashMap;
|
2019-02-15 09:06:42 +02:00
|
|
|
|
2019-02-21 15:31:01 +02:00
|
|
|
mod contact_list;
|
|
|
|
|
|
|
|
pub use self::contact_list::*;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum ViewMode {
|
2019-10-20 11:14:29 +03:00
|
|
|
ReadOnly,
|
2020-02-19 16:57:37 +02:00
|
|
|
Discard(UIDialog<char>),
|
2019-10-20 11:14:29 +03:00
|
|
|
Edit,
|
2019-03-03 22:11:15 +02:00
|
|
|
//New,
|
2019-02-21 15:31:01 +02:00
|
|
|
}
|
|
|
|
|
2019-02-15 09:06:42 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ContactManager {
|
2019-04-10 22:01:02 +03:00
|
|
|
id: ComponentId,
|
2019-10-20 11:14:29 +03:00
|
|
|
parent_id: ComponentId,
|
2019-02-18 23:14:06 +02:00
|
|
|
pub card: Card,
|
2019-02-21 15:31:01 +02:00
|
|
|
mode: ViewMode,
|
2020-10-13 17:04:40 +03:00
|
|
|
form: FormWidget<bool>,
|
2019-02-25 12:00:17 +02:00
|
|
|
account_pos: usize,
|
2019-02-15 09:06:42 +02:00
|
|
|
content: CellBuffer,
|
2020-02-09 00:30:50 +02:00
|
|
|
theme_default: ThemeAttribute,
|
2019-02-15 09:06:42 +02:00
|
|
|
dirty: bool,
|
2019-10-20 11:14:29 +03:00
|
|
|
has_changes: bool,
|
|
|
|
|
2019-02-15 09:06:42 +02:00
|
|
|
initialized: bool,
|
|
|
|
}
|
|
|
|
|
2020-02-09 00:30:50 +02:00
|
|
|
impl fmt::Display for ContactManager {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "contacts")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ContactManager {
|
|
|
|
fn new(context: &Context) -> Self {
|
|
|
|
let theme_default: ThemeAttribute = crate::conf::value(context, "theme_default");
|
2019-02-15 09:06:42 +02:00
|
|
|
ContactManager {
|
2019-02-21 15:31:01 +02:00
|
|
|
id: Uuid::nil(),
|
2019-10-20 11:14:29 +03:00
|
|
|
parent_id: Uuid::nil(),
|
2019-02-18 23:14:06 +02:00
|
|
|
card: Card::new(),
|
2019-10-20 11:14:29 +03:00
|
|
|
mode: ViewMode::Edit,
|
2019-02-25 11:11:56 +02:00
|
|
|
form: FormWidget::default(),
|
2019-02-25 12:00:17 +02:00
|
|
|
account_pos: 0,
|
2020-12-02 20:47:48 +02:00
|
|
|
content: CellBuffer::new_with_context(100, 1, None, context),
|
2020-02-09 00:30:50 +02:00
|
|
|
theme_default,
|
2019-02-15 09:06:42 +02:00
|
|
|
dirty: true,
|
2019-10-20 11:14:29 +03:00
|
|
|
has_changes: false,
|
2019-02-15 09:06:42 +02:00
|
|
|
initialized: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 23:14:06 +02:00
|
|
|
fn initialize(&mut self) {
|
2019-03-03 22:11:15 +02:00
|
|
|
let (width, _) = self.content.size();
|
2019-02-18 23:14:06 +02:00
|
|
|
|
2019-03-03 22:11:15 +02:00
|
|
|
let (x, _) = write_string_to_grid(
|
2019-02-18 23:14:06 +02:00
|
|
|
"Last edited: ",
|
|
|
|
&mut self.content,
|
|
|
|
Color::Byte(250),
|
2020-02-09 00:30:50 +02:00
|
|
|
self.theme_default.bg,
|
|
|
|
self.theme_default.attrs,
|
2019-10-20 11:14:29 +03:00
|
|
|
((0, 0), (width - 1, 0)),
|
2019-11-18 13:06:30 +02:00
|
|
|
None,
|
2019-03-14 12:19:25 +02:00
|
|
|
);
|
2019-10-20 11:14:29 +03:00
|
|
|
let (x, y) = write_string_to_grid(
|
2019-02-18 23:14:06 +02:00
|
|
|
&self.card.last_edited(),
|
|
|
|
&mut self.content,
|
|
|
|
Color::Byte(250),
|
2020-02-09 00:30:50 +02:00
|
|
|
self.theme_default.bg,
|
|
|
|
self.theme_default.attrs,
|
2019-10-20 11:14:29 +03:00
|
|
|
((x, 0), (width - 1, 0)),
|
2019-11-18 13:06:30 +02:00
|
|
|
None,
|
2019-03-14 12:19:25 +02:00
|
|
|
);
|
2019-10-20 11:14:29 +03:00
|
|
|
|
|
|
|
if self.card.external_resource() {
|
|
|
|
self.mode = ViewMode::ReadOnly;
|
2021-01-05 13:51:31 +02:00
|
|
|
let _ = self.content.resize(self.content.size().0, 2, None);
|
2019-10-24 20:35:30 +03:00
|
|
|
write_string_to_grid(
|
2019-10-20 11:14:29 +03:00
|
|
|
"This contact's origin is external and cannot be edited within meli.",
|
|
|
|
&mut self.content,
|
|
|
|
Color::Byte(250),
|
2020-02-09 00:30:50 +02:00
|
|
|
self.theme_default.bg,
|
|
|
|
self.theme_default.attrs,
|
2019-10-20 11:14:29 +03:00
|
|
|
((x, y), (width - 1, y)),
|
2019-11-18 13:06:30 +02:00
|
|
|
None,
|
2019-10-20 11:14:29 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-13 17:04:40 +03:00
|
|
|
self.form = FormWidget::new(("Save".into(), true));
|
2019-10-20 11:14:29 +03:00
|
|
|
self.form.add_button(("Cancel(Esc)".into(), false));
|
2019-03-14 12:19:25 +02:00
|
|
|
self.form
|
2020-10-13 17:04:40 +03:00
|
|
|
.push(("NAME".into(), self.card.name().to_string().into()));
|
2019-03-14 12:19:25 +02:00
|
|
|
self.form.push((
|
2019-07-06 20:36:59 +03:00
|
|
|
"ADDITIONAL NAME".into(),
|
2020-10-13 17:04:40 +03:00
|
|
|
self.card.additionalname().to_string().into(),
|
|
|
|
));
|
|
|
|
self.form.push((
|
|
|
|
"NAME PREFIX".into(),
|
|
|
|
self.card.name_prefix().to_string().into(),
|
|
|
|
));
|
|
|
|
self.form.push((
|
|
|
|
"NAME SUFFIX".into(),
|
|
|
|
self.card.name_suffix().to_string().into(),
|
2019-03-14 12:19:25 +02:00
|
|
|
));
|
|
|
|
self.form
|
2020-10-13 17:04:40 +03:00
|
|
|
.push(("E-MAIL".into(), self.card.email().to_string().into()));
|
2019-03-14 12:19:25 +02:00
|
|
|
self.form
|
2020-10-13 17:04:40 +03:00
|
|
|
.push(("URL".into(), self.card.url().to_string().into()));
|
2019-03-14 12:19:25 +02:00
|
|
|
self.form
|
2020-10-13 17:04:40 +03:00
|
|
|
.push(("KEY".into(), self.card.key().to_string().into()));
|
2019-10-20 11:14:29 +03:00
|
|
|
for (k, v) in self.card.extra_properties() {
|
2020-10-13 17:04:40 +03:00
|
|
|
self.form.push((k.to_string().into(), v.to_string().into()));
|
2019-10-20 11:14:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_parent_id(&mut self, new_val: ComponentId) {
|
|
|
|
self.parent_id = new_val;
|
2019-02-18 23:14:06 +02:00
|
|
|
}
|
2019-02-15 09:06:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for ContactManager {
|
|
|
|
fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
|
|
|
|
if !self.initialized {
|
2019-02-18 23:14:06 +02:00
|
|
|
self.initialize();
|
2019-02-15 09:06:42 +02:00
|
|
|
self.initialized = true;
|
|
|
|
}
|
2019-02-25 11:11:56 +02:00
|
|
|
|
|
|
|
let upper_left = upper_left!(area);
|
|
|
|
let bottom_right = bottom_right!(area);
|
2019-10-20 11:14:29 +03:00
|
|
|
|
|
|
|
if self.dirty {
|
|
|
|
let (width, _height) = self.content.size();
|
|
|
|
clear_area(
|
|
|
|
grid,
|
|
|
|
(upper_left, set_y(bottom_right, get_y(upper_left) + 1)),
|
2020-02-09 00:30:50 +02:00
|
|
|
self.theme_default,
|
2019-10-20 11:14:29 +03:00
|
|
|
);
|
|
|
|
copy_area_with_break(grid, &self.content, area, ((0, 0), (width - 1, 0)));
|
|
|
|
self.dirty = false;
|
|
|
|
}
|
|
|
|
|
2019-03-14 12:19:25 +02:00
|
|
|
self.form.draw(
|
|
|
|
grid,
|
2019-10-20 11:14:29 +03:00
|
|
|
(set_y(upper_left, get_y(upper_left) + 2), bottom_right),
|
2019-03-14 12:19:25 +02:00
|
|
|
context,
|
|
|
|
);
|
2019-10-20 11:14:29 +03:00
|
|
|
match self.mode {
|
|
|
|
ViewMode::Discard(ref mut selector) => {
|
|
|
|
/* Let user choose whether to quit with/without saving or cancel */
|
2020-11-25 20:54:35 +02:00
|
|
|
selector.draw(grid, area, context);
|
2019-10-20 11:14:29 +03:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2019-02-15 09:06:42 +02:00
|
|
|
context.dirty_areas.push_back(area);
|
|
|
|
}
|
|
|
|
|
2019-02-26 17:50:47 +02:00
|
|
|
fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool {
|
2020-12-02 20:47:48 +02:00
|
|
|
match event {
|
|
|
|
UIEvent::ConfigReload { old_settings: _ } => {
|
|
|
|
self.theme_default = crate::conf::value(context, "theme_default");
|
|
|
|
self.content = CellBuffer::new_with_context(100, 1, None, context);
|
|
|
|
self.initialized = false;
|
|
|
|
self.set_dirty(true);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2019-10-20 11:14:29 +03:00
|
|
|
match self.mode {
|
|
|
|
ViewMode::Discard(ref mut selector) => {
|
|
|
|
if selector.process_event(event, context) {
|
2019-12-14 18:50:05 +02:00
|
|
|
self.set_dirty(true);
|
2019-10-20 11:14:29 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ViewMode::Edit => {
|
|
|
|
if let &mut UIEvent::Input(Key::Esc) = event {
|
|
|
|
if self.can_quit_cleanly(context) {
|
|
|
|
context
|
|
|
|
.replies
|
|
|
|
.push_back(UIEvent::Action(Tab(Kill(self.parent_id))));
|
|
|
|
}
|
|
|
|
return true;
|
2019-03-14 12:19:25 +02:00
|
|
|
}
|
2019-10-20 11:14:29 +03:00
|
|
|
if self.form.process_event(event, context) {
|
|
|
|
match self.form.buttons_result() {
|
|
|
|
None => {}
|
|
|
|
Some(true) => {
|
|
|
|
let fields = std::mem::replace(&mut self.form, FormWidget::default())
|
|
|
|
.collect()
|
|
|
|
.unwrap();
|
2020-05-10 21:14:49 +03:00
|
|
|
let fields: HashMap<String, String> = fields
|
2019-10-20 11:14:29 +03:00
|
|
|
.into_iter()
|
|
|
|
.map(|(s, v)| {
|
|
|
|
(
|
2020-10-13 17:04:40 +03:00
|
|
|
s.to_string(),
|
2019-10-20 11:14:29 +03:00
|
|
|
match v {
|
|
|
|
Field::Text(v, _) => v.as_str().to_string(),
|
2020-10-13 17:04:40 +03:00
|
|
|
Field::Choice(mut v, c) => v.remove(c).to_string(),
|
2019-10-20 11:14:29 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let mut new_card = Card::from(fields);
|
|
|
|
new_card.set_id(*self.card.id());
|
|
|
|
context.accounts[self.account_pos]
|
|
|
|
.address_book
|
|
|
|
.add_card(new_card);
|
|
|
|
context.replies.push_back(UIEvent::StatusEvent(
|
|
|
|
StatusEvent::DisplayMessage("Saved.".into()),
|
|
|
|
));
|
|
|
|
context.replies.push_back(UIEvent::ComponentKill(self.id));
|
|
|
|
}
|
|
|
|
Some(false) => {
|
|
|
|
context.replies.push_back(UIEvent::ComponentKill(self.id));
|
|
|
|
}
|
|
|
|
}
|
2019-12-14 18:50:05 +02:00
|
|
|
self.set_dirty(true);
|
2019-10-20 11:14:29 +03:00
|
|
|
if let UIEvent::InsertInput(_) = event {
|
|
|
|
self.has_changes = true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ViewMode::ReadOnly => {
|
|
|
|
if let &mut UIEvent::Input(Key::Esc) = event {
|
|
|
|
if self.can_quit_cleanly(context) {
|
|
|
|
context.replies.push_back(UIEvent::ComponentKill(self.id));
|
|
|
|
}
|
|
|
|
return true;
|
2019-03-14 12:19:25 +02:00
|
|
|
}
|
2019-02-25 11:11:56 +02:00
|
|
|
}
|
2019-03-14 12:19:25 +02:00
|
|
|
}
|
2019-02-15 09:06:42 +02:00
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_dirty(&self) -> bool {
|
2019-10-20 11:14:29 +03:00
|
|
|
self.dirty
|
|
|
|
|| self.form.is_dirty()
|
|
|
|
|| if let ViewMode::Discard(ref selector) = self.mode {
|
|
|
|
selector.is_dirty()
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2019-02-15 09:06:42 +02:00
|
|
|
}
|
|
|
|
|
2019-12-14 18:50:05 +02:00
|
|
|
fn set_dirty(&mut self, value: bool) {
|
|
|
|
self.dirty = value;
|
|
|
|
self.form.set_dirty(value);
|
2019-10-20 11:14:29 +03:00
|
|
|
if let ViewMode::Discard(ref mut selector) = self.mode {
|
2019-12-14 18:50:05 +02:00
|
|
|
selector.set_dirty(value);
|
2019-10-20 11:14:29 +03:00
|
|
|
}
|
2019-02-15 09:06:42 +02:00
|
|
|
}
|
|
|
|
|
2019-04-10 22:01:02 +03:00
|
|
|
fn id(&self) -> ComponentId {
|
|
|
|
self.id
|
|
|
|
}
|
2019-10-20 11:14:29 +03:00
|
|
|
|
2019-04-10 22:01:02 +03:00
|
|
|
fn set_id(&mut self, id: ComponentId) {
|
|
|
|
self.id = id;
|
2019-02-15 09:06:42 +02:00
|
|
|
}
|
2019-10-20 11:14:29 +03:00
|
|
|
|
|
|
|
fn can_quit_cleanly(&mut self, context: &Context) -> bool {
|
|
|
|
if !self.has_changes {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-19 16:57:37 +02:00
|
|
|
let parent_id = self.parent_id;
|
2019-10-20 11:14:29 +03:00
|
|
|
/* Play it safe and ask user for confirmation */
|
2020-02-19 16:57:37 +02:00
|
|
|
self.mode = ViewMode::Discard(UIDialog::new(
|
2019-10-20 11:14:29 +03:00
|
|
|
"this contact has unsaved changes",
|
|
|
|
vec![
|
|
|
|
('x', "quit without saving".to_string()),
|
|
|
|
('y', "save draft and quit".to_string()),
|
|
|
|
('n', "cancel".to_string()),
|
|
|
|
],
|
|
|
|
true,
|
2020-02-22 11:47:13 +02:00
|
|
|
Some(Box::new(move |_, results: &[char]| match results[0] {
|
2020-02-19 16:57:37 +02:00
|
|
|
'x' => Some(UIEvent::Action(Tab(Kill(parent_id)))),
|
|
|
|
'n' => None,
|
|
|
|
'y' => None,
|
|
|
|
_ => None,
|
2020-02-22 11:47:13 +02:00
|
|
|
})),
|
2019-10-20 11:14:29 +03:00
|
|
|
context,
|
|
|
|
));
|
2019-12-14 18:50:05 +02:00
|
|
|
self.set_dirty(true);
|
2019-10-20 11:14:29 +03:00
|
|
|
false
|
|
|
|
}
|
2019-02-15 09:06:42 +02:00
|
|
|
}
|