summaryrefslogtreecommitdiffstats
path: root/src/main_view.rs
blob: 9c92a7e8e01b2e520a360a50347fd027fd6922f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use std::path::PathBuf;
use anyhow::Result;
use cursive::{View, Printer, XY, direction::Direction, view::Selector, Rect, event::Event, event::EventResult};
use cursive::view::Nameable;
use cursive::Cursive;
use cursive::views::NamedView;
use mailparse::MailHeaderMap;
use mailparse::ParsedMail;

use crate::mailstore::{ MailStore, Mail };

pub const MAIN_VIEW_NAME: &'static str = "main_view";

pub struct MainView {
    tabs: cursive_tabs::TabView<usize>,
}

impl View for MainView {
    fn draw(&self, printer: &Printer) {
        self.tabs.draw(printer)
    }

    fn layout(&mut self, xy: XY<usize>) {
        self.tabs.layout(xy)
    }

    fn needs_relayout(&self) -> bool {
        self.tabs.needs_relayout()
    }

    fn required_size(&mut self, constraint: XY<usize>) -> XY<usize> {
        self.tabs.required_size(constraint)
    }

    fn on_event(&mut self, e: Event) -> EventResult {
        self.tabs.on_event(e)
    }

    fn call_on_any<'a>(&mut self, s: &Selector, tpl: &'a mut (dyn FnMut(&mut (dyn View + 'static)) + 'a)) {
        self.tabs.call_on_any(s, tpl);
    }

    fn focus_view(&mut self, s: &Selector) -> Result<(), ()> {
        self.tabs.focus_view(s)
    }

    fn take_focus(&mut self, source: Direction) -> bool {
        self.tabs.take_focus(source)
    }

    fn important_area(&self, view_size: XY<usize>) -> Rect {
        self.tabs.important_area(view_size)
    }

    fn type_name(&self) -> &'static str {
        self.tabs.type_name()
    }

}

impl MainView {
    pub fn new() -> NamedView<Self> {
        let mut tabs = cursive_tabs::TabView::default();
        tabs.add_tab(0, cursive::views::TextView::new("Test"));
        MainView { tabs }.with_name(MAIN_VIEW_NAME)
    }

    pub fn load_maildir(&mut self, pb: PathBuf) -> Result<()> {
        let mut md = MailStore::from_path(pb);
        md.load()?;
        let list_view = MainView::list_view_for(md.cur_mail().iter().map(Mail::parsed))?;

        self.tabs.add_tab(1, list_view.with_name("Tab"));
        Ok(())
    }

    fn list_view_for<'a, I>(i: I) -> Result<cursive::views::ListView>
        where I: Iterator<Item = &'a ParsedMail>
    {
        let mut lv = cursive::views::ListView::new();

        for elem in i {
            let s = format!("{}: {} -> {} | {}",
                elem.headers.get_first_value("Date").unwrap_or_else(|| String::from("No date")),
                elem.headers.get_first_value("From").unwrap_or_else(|| String::from("No From")),
                elem.headers.get_first_value("To").unwrap_or_else(|| String::from("No To")),
                elem.headers.get_first_value("Subject").unwrap_or_else(|| String::from("No Subject")));

            lv.add_child("", cursive::views::TextView::new(s));
        }

        Ok(lv)
    }

}