2017-07-28 17:45:19 +03:00
|
|
|
/*
|
|
|
|
* meli - mailbox module.
|
|
|
|
*
|
|
|
|
* Copyright 2017 Manos Pitsidianakis
|
2017-09-01 15:24:32 +03:00
|
|
|
*
|
2017-07-28 17:45:19 +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 */
|
2017-09-07 23:00:08 +03:00
|
|
|
pub mod backends;
|
2017-09-01 15:24:32 +03:00
|
|
|
use error::Result;
|
2018-08-19 14:54:32 +03:00
|
|
|
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
|
|
|
|
2017-09-07 23:00:08 +03:00
|
|
|
use std::option::Option;
|
2017-07-28 17:45:19 +03:00
|
|
|
|
2017-10-01 17:31:20 +03:00
|
|
|
/// `Mailbox` represents a folder of mail.
|
2018-08-11 18:00:21 +03:00
|
|
|
#[derive(Debug)]
|
2017-09-16 15:05:28 +03:00
|
|
|
pub struct Mailbox {
|
2018-07-11 23:12:25 +03:00
|
|
|
pub folder: Folder,
|
2017-09-16 14:14:08 +03:00
|
|
|
pub collection: Vec<Envelope>,
|
2018-08-14 17:36:45 +03:00
|
|
|
pub threads: Threads,
|
2017-07-28 17:45:19 +03:00
|
|
|
}
|
|
|
|
|
2018-08-11 18:00:21 +03:00
|
|
|
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 {
|
2018-08-11 18:00:21 +03:00
|
|
|
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> {
|
2017-09-30 20:02:22 +03:00
|
|
|
let mut collection: Vec<Envelope> = collection?;
|
2018-07-20 12:44:04 +03:00
|
|
|
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 {
|
2018-08-11 18:00:21 +03:00
|
|
|
folder: (*folder).clone(),
|
2018-08-23 15:36:52 +03:00
|
|
|
collection,
|
|
|
|
threads,
|
2017-09-01 15:24:32 +03:00
|
|
|
})
|
2017-07-28 17:45:19 +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()
|
2017-07-28 17:45:19 +03:00
|
|
|
}
|
2018-07-20 12:44:04 +03:00
|
|
|
pub fn threaded_mail(&self, i: usize) -> usize {
|
2018-08-14 17:36:45 +03:00
|
|
|
self.threads.thread_to_mail(i)
|
2017-07-28 17:45:19 +03:00
|
|
|
}
|
2018-07-20 12:44:04 +03:00
|
|
|
pub fn mail_and_thread(&mut self, i: usize) -> (&mut Envelope, Container) {
|
2017-09-16 15:05:28 +03:00
|
|
|
let x = &mut self.collection.as_mut_slice()[i];
|
2018-07-20 12:44:04 +03:00
|
|
|
let thread = self.threads[x.thread()];
|
2017-09-16 15:05:28 +03:00
|
|
|
(x, thread)
|
2017-07-28 17:45:19 +03:00
|
|
|
}
|
2018-07-20 12:44:04 +03:00
|
|
|
pub fn thread(&self, i: usize) -> &Container {
|
2017-09-01 15:24:32 +03:00
|
|
|
&self.threads[i]
|
2017-07-28 17:45:19 +03:00
|
|
|
}
|
|
|
|
}
|