summaryrefslogtreecommitdiff
path: root/src/components/mail/listing/offline.rs
blob: 3fdcebe74a3f76a4c4c8411e0cbcabc14176d547 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * meli
 *
 * Copyright 2020 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::components::PageMovement;
use std::borrow::Cow;

#[derive(Debug)]
pub struct OfflineListing {
    cursor_pos: (AccountHash, MailboxHash),
    _row_updates: SmallVec<[EnvelopeHash; 8]>,
    _selection: HashMap<EnvelopeHash, bool>,
    messages: Vec<Cow<'static, str>>,
    dirty: bool,
    id: ComponentId,
}

impl MailListingTrait for OfflineListing {
    fn row_updates(&mut self) -> &mut SmallVec<[EnvelopeHash; 8]> {
        &mut self._row_updates
    }

    fn selection(&mut self) -> &mut HashMap<EnvelopeHash, bool> {
        &mut self._selection
    }

    fn get_focused_items(&self, _context: &Context) -> SmallVec<[EnvelopeHash; 8]> {
        SmallVec::new()
    }

    fn refresh_mailbox(&mut self, _context: &mut Context, _force: bool) {}
    fn redraw_threads_list(
        &mut self,
        _context: &Context,
        _items: Box<dyn Iterator<Item = ThreadHash>>,
    ) {
    }
    fn redraw_envelope_list(
        &mut self,
        _context: &Context,
        _items: Box<dyn Iterator<Item = EnvelopeHash>>,
    ) {
    }
}

impl ListingTrait for OfflineListing {
    fn coordinates(&self) -> (AccountHash, MailboxHash) {
        self.cursor_pos
    }

    fn set_coordinates(&mut self, coordinates: (AccountHash, MailboxHash)) {
        self.cursor_pos = coordinates;
    }

    fn highlight_line(
        &mut self,
        _grid: &mut CellBuffer,
        _area: Area,
        _idx: usize,
        _context: &Context,
    ) {
    }

    fn draw_list(&mut self, _: &mut CellBuffer, _: Area, _: &mut Context) {}

    fn unfocused(&self) -> bool {
        false
    }

    fn set_modifier_active(&mut self, _: bool) {}

    fn set_modifier_command(&mut self, _: Option<Modifier>) {}

    fn modifier_command(&self) -> Option<Modifier> {
        None
    }

    fn set_movement(&mut self, _: PageMovement) {}

    fn focus(&self) -> Focus {
        Focus::None
    }

    fn set_focus(&mut self, _new_value: Focus, _context: &mut Context) {}
}

impl fmt::Display for OfflineListing {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "mail")
    }
}

impl OfflineListing {
    pub fn new(cursor_pos: (AccountHash, MailboxHash)) -> Box<Self> {
        Box::new(OfflineListing {
            cursor_pos,
            _row_updates: SmallVec::new(),
            _selection: HashMap::default(),
            messages: vec![],
            dirty: true,
            id: ComponentId::new_v4(),
        })
    }
}

impl Component for OfflineListing {
    fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
        if !self.dirty {
            return;
        }
        self.dirty = false;
        let theme_default = conf::value(context, "theme_default");
        let text_unfocused = conf::value(context, "text.unfocused");
        let error_message = conf::value(context, "error_message");
        clear_area(grid, area, theme_default);
        if let Err(err) = context.is_online(self.cursor_pos.0) {
            let (x, _) = write_string_to_grid(
                "offline: ",
                grid,
                error_message.fg,
                error_message.bg,
                error_message.attrs,
                area,
                None,
            );
            write_string_to_grid(
                &err.to_string(),
                grid,
                error_message.fg,
                error_message.bg,
                error_message.attrs,
                (set_x(upper_left!(area), x + 1), bottom_right!(area)),
                Some(get_x(upper_left!(area))),
            );
            if let Some(msg) = self.messages.last() {
                write_string_to_grid(
                    msg,
                    grid,
                    text_unfocused.fg,
                    text_unfocused.bg,
                    Attr::BOLD,
                    (pos_inc((0, 1), upper_left!(area)), bottom_right!(area)),
                    None,
                );
            }
            for (i, msg) in self.messages.iter().rev().skip(1).enumerate() {
                write_string_to_grid(
                    msg,
                    grid,
                    text_unfocused.fg,
                    text_unfocused.bg,
                    text_unfocused.attrs,
                    (pos_inc((0, 2 + i), upper_left!(area)), bottom_right!(area)),
                    None,
                );
            }
        } else {
            let (_, mut y) = write_string_to_grid(
                "loading...",
                grid,
                conf::value(context, "highlight").fg,
                conf::value(context, "highlight").bg,
                conf::value(context, "highlight").attrs,
                area,
                None,
            );
            let mut jobs: SmallVec<[_; 64]> = context.accounts[&self.cursor_pos.0]
                .active_jobs
                .iter()
                .collect();
            jobs.sort_by_key(|(j, _)| *j);
            for (job_id, j) in jobs {
                write_string_to_grid(
                    &format!("{}: {:?}", job_id, j),
                    grid,
                    text_unfocused.fg,
                    text_unfocused.bg,
                    text_unfocused.attrs,
                    (set_y(upper_left!(area), y + 1), bottom_right!(area)),
                    None,
                );
                y += 1;
            }

            context
                .replies
                .push_back(UIEvent::AccountStatusChange(self.cursor_pos.0, None));
        }
        context.dirty_areas.push_back(area);
    }

    fn process_event(&mut self, event: &mut UIEvent, _context: &mut Context) -> bool {
        match event {
            UIEvent::AccountStatusChange(account_hash, msg)
                if *account_hash == self.cursor_pos.0 =>
            {
                if let Some(msg) = msg.clone() {
                    self.messages.push(msg);
                }
                self.dirty = true
            }
            _ => {}
        }
        false
    }

    fn is_dirty(&self) -> bool {
        self.dirty
    }

    fn set_dirty(&mut self, _value: bool) {
        self.dirty = true;
    }

    fn id(&self) -> ComponentId {
        self.id
    }

    fn set_id(&mut self, id: ComponentId) {
        self.id = id;
    }
}