meli/melib/src/mailbox/mod.rs

99 lines
2.7 KiB
Rust
Raw Normal View History

/*
* meli - mailbox module.
*
* Copyright 2017 Manos Pitsidianakis
2017-09-01 15:24:32 +03:00
*
* 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/>.
*/
2018-08-06 22:20:34 +03:00
/*!
* Mail related code.
*
* This module handles reading emails from various backends, handling account data etc
*/
2017-09-01 15:24:32 +03:00
pub mod email;
pub use self::email::*;
2017-09-05 16:41:29 +03:00
/* Mail backends. Currently only maildir is supported */
pub mod backends;
2017-09-01 15:24:32 +03:00
use error::Result;
use mailbox::backends::{folder_default, Folder};
2017-10-01 17:31:20 +03:00
pub mod thread;
2018-08-23 15:36:52 +03:00
pub use mailbox::thread::{build_threads, Container, SortField, SortOrder, Threads};
2017-09-01 15:24:32 +03:00
use std::option::Option;
2017-10-01 17:31:20 +03:00
/// `Mailbox` represents a folder of mail.
#[derive(Debug)]
pub struct Mailbox {
2018-07-11 23:12:25 +03:00
pub folder: Folder,
pub collection: Vec<Envelope>,
2018-08-14 17:36:45 +03:00
pub threads: Threads,
}
impl Clone for Mailbox {
fn clone(&self) -> Self {
Mailbox {
folder: self.folder.clone(),
collection: self.collection.clone(),
threads: self.threads.clone(),
}
}
}
2018-08-14 17:36:45 +03:00
impl Default for Mailbox {
fn default() -> Self {
2018-07-11 17:07:51 +03:00
Mailbox {
folder: folder_default(),
2018-08-14 17:36:45 +03:00
collection: Vec::default(),
threads: Threads::default(),
2018-07-11 17:07:51 +03:00
}
}
2018-08-14 17:36:45 +03:00
}
impl Mailbox {
2018-07-27 21:37:56 +03:00
pub fn new(
folder: &Folder,
sent_folder: &Option<Result<Mailbox>>,
collection: Result<Vec<Envelope>>,
) -> Result<Mailbox> {
let mut collection: Vec<Envelope> = collection?;
collection.sort_by(|a, b| a.date().cmp(&b.date()));
2018-08-14 17:36:45 +03:00
let threads = build_threads(&mut collection, sent_folder);
2017-09-01 15:24:32 +03:00
Ok(Mailbox {
folder: (*folder).clone(),
2018-08-23 15:36:52 +03:00
collection,
threads,
2017-09-01 15:24:32 +03:00
})
}
2018-08-23 15:36:52 +03:00
pub fn is_empty(&self) -> bool {
self.collection.is_empty()
}
2018-07-11 18:58:57 +03:00
pub fn len(&self) -> usize {
2017-09-28 18:06:35 +03:00
self.collection.len()
}
pub fn threaded_mail(&self, i: usize) -> usize {
2018-08-14 17:36:45 +03:00
self.threads.thread_to_mail(i)
}
pub fn mail_and_thread(&mut self, i: usize) -> (&mut Envelope, Container) {
let x = &mut self.collection.as_mut_slice()[i];
let thread = self.threads[x.thread()];
(x, thread)
}
pub fn thread(&self, i: usize) -> &Container {
2017-09-01 15:24:32 +03:00
&self.threads[i]
}
}