/*
* meli
*
* 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::*;
use crate::melib::text_processing::TextProcessing;
use melib::backends::AccountHash;
use melib::CardId;
use std::cmp;
#[derive(Debug, PartialEq)]
enum ViewMode {
List,
View(ComponentId),
}
#[derive(Debug)]
struct AccountMenuEntry {
name: String,
hash: AccountHash,
// Index in the config account vector.
index: usize,
}
#[derive(Debug)]
pub struct ContactList {
accounts: Vec<AccountMenuEntry>,
cursor_pos: usize,
new_cursor_pos: usize,
account_pos: usize,
length: usize,
data_columns: DataColumns,
initialized: bool,
theme_default: ThemeAttribute,
id_positions: Vec<CardId>,
mode: ViewMode,
dirty: bool,
sidebar_divider: char,
sidebar_divider_theme: ThemeAttribute,
menu_visibility: bool,
movement: Option<PageMovement>,
cmd_buf: String,
view: Option<ContactManager>,
ratio: usize, // right/(container width) * 100
id: ComponentId,
}
impl fmt::Display for ContactList {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ContactList::DESCRIPTION)
}
}
impl ContactList {
const DESCRIPTION: &'static str = "contact list";
pub fn new(context: &Context) -> Self {
let accounts = context
.accounts
.iter()
.enumerate()
.map(|(i, (h, a))| AccountMenuEntry {
name: a.name().to_string(),
hash: *h,
index: i,
})
.collect();
ContactList {
accounts,
cursor_pos: 0,
new_cursor_pos: 0,
length: 0,
account_pos: 0,
id_positions: Vec::new(),
mode: ViewMode::List,
data_columns: DataColumns::default(),
theme_default: crate::conf::value(context, "theme_default"),
initialized: false,
dirty: true,
movement: None,
cmd_buf: String::with_capacity(8),
view: None,
ratio: 90,
sidebar_divider: context.settings.listing.sidebar_divider,
sidebar_divider_theme: conf::value(context, "mail.sidebar_divider"),
menu_visibility: true,
id: ComponentId::new_v4(),
}
}
pub fn for_account(pos: usize, context: &Context) -> Self {
ContactList {
account_pos: pos,
..Self::new(context)
}
}
fn initialize(&mut self, context: &mut Context) {
let account = &context.accounts[self.account_pos];
let book = &account.address_book;
self.length = book.len();
self.id_positions.clear();
if self.id_positions.capacity() < book.len() {
self.id_positions.reserve(book.len());
}
self.dirty = true;
let mut min_width = ("Name".len(), "E-mail".len(), 0, "external".len(), 0, 0);
for c in book.values() {
/* name */
min_width.0 = cmp::max(min_width.0, c.name().split_graphemes().len());
/* email */
min_width.1 = cmp::max(min_width.1, c.email().split_graphemes().len());
/* url */
min_width.2 = cmp::max(min_width.2, c.url().split_graphemes().len());
}
/* name column */
self.data_columns.columns[0] =
CellBuffer::new_with_context(min_width.0, self.length, None, context);
/* email column */
self.data_columns.columns[1] =
CellBuffer::new_with_context(min_width.1, self.length, None, context);
/* url column */
self.data_columns.columns[2] =
CellBuffer::new_with_context(min_width.2, self.length, None, context);
/* source column */
self.data_columns.columns[3] =
CellBuffer::new_with_context("external".len(), self.length, None, context);
let account = &context.accounts[self.account_pos];
let book = &account.address_book;
let mut book_values = book.values().collect::<Vec<&Card>>();
book_values.sort_unstable_by_key