meli/melib/src/mailbox.rs

134 lines
4.0 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;
2019-04-04 14:21:52 +03:00
use crate::error::Result;
use self::backends::Folder;
2017-10-01 17:31:20 +03:00
pub mod thread;
2019-04-04 14:21:52 +03:00
pub use self::thread::{SortField, SortOrder, ThreadNode, Threads};
2018-09-06 13:05:35 +03:00
mod collection;
pub use self::collection::*;
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, Clone, Default)]
pub struct Mailbox {
2018-07-11 23:12:25 +03:00
pub folder: Folder,
name: String,
2018-09-06 13:05:35 +03:00
pub collection: Collection,
has_sent: bool,
}
2018-08-14 17:36:45 +03:00
impl Mailbox {
2018-09-17 07:53:16 +03:00
pub fn new(folder: Folder, envelopes: Result<Vec<Envelope>>) -> Result<Mailbox> {
2018-09-06 13:05:35 +03:00
let mut envelopes: Vec<Envelope> = envelopes?;
envelopes.sort_by(|a, b| a.date().cmp(&b.date()));
2018-09-17 07:53:16 +03:00
let collection = Collection::new(envelopes, &folder);
let name = folder.name().into();
2017-09-01 15:24:32 +03:00
Ok(Mailbox {
2018-09-17 07:53:16 +03:00
folder,
2018-08-23 15:36:52 +03:00
collection,
2018-10-14 19:49:16 +03:00
name,
..Default::default()
2017-09-01 15:24:32 +03:00
})
}
pub fn name(&self) -> &str {
&self.name
}
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 thread_to_mail_mut(&mut self, i: usize) -> &mut Envelope {
self.collection
.envelopes
.entry(self.collection.threads.thread_to_mail(i))
.or_default()
}
pub fn thread_to_mail(&self, i: usize) -> &Envelope {
&self.collection.envelopes[&self.collection.threads.thread_to_mail(i)]
}
2018-09-06 13:05:35 +03:00
pub fn threaded_mail(&self, i: usize) -> EnvelopeHash {
self.collection.threads.thread_to_mail(i)
}
2018-09-06 13:05:35 +03:00
pub fn mail_and_thread(&mut self, i: EnvelopeHash) -> (&mut Envelope, &ThreadNode) {
let thread;
{
let x = &mut self.collection.envelopes.entry(i).or_default();
thread = &self.collection.threads[x.thread()];
}
(self.collection.envelopes.entry(i).or_default(), thread)
}
2018-09-06 13:05:35 +03:00
pub fn thread(&self, i: usize) -> &ThreadNode {
&self.collection.threads.thread_nodes()[i]
}
2018-09-05 16:08:11 +03:00
2018-10-14 19:49:16 +03:00
pub fn insert_sent_folder(&mut self, _sent: &Mailbox) {
/*if !self.has_sent {
for envelope in sent.collection.envelopes.values() {
self.insert_reply(envelope);
}
self.has_sent = true;
2018-10-14 19:49:16 +03:00
}*/
}
pub fn rename(&mut self, old_hash: EnvelopeHash, new_hash: EnvelopeHash) {
self.collection.rename(old_hash, new_hash);
}
2018-09-05 16:08:11 +03:00
pub fn update(&mut self, old_hash: EnvelopeHash, envelope: Envelope) {
2018-10-14 19:49:16 +03:00
self.collection.update_envelope(old_hash, envelope);
2018-09-05 16:08:11 +03:00
}
pub fn insert(&mut self, envelope: Envelope) -> &Envelope {
2018-09-06 13:05:35 +03:00
let hash = envelope.hash();
2018-09-17 07:53:16 +03:00
self.collection.insert(envelope);
2018-09-06 13:05:35 +03:00
&self.collection[&hash]
2018-09-05 16:08:11 +03:00
}
2018-10-14 19:49:16 +03:00
pub fn insert_reply(&mut self, envelope: &Envelope) {
if cfg!(feature = "debug_log") {
eprintln!("mailbox insert reply {}", self.name);
}
self.collection.insert_reply(envelope);
}
2018-09-05 16:08:11 +03:00
pub fn remove(&mut self, envelope_hash: EnvelopeHash) {
2018-10-14 19:49:16 +03:00
self.collection.remove(envelope_hash);
// if cfg!(feature = "debug_log") { eprintln!("envelope_hash: {}\ncollection:\n{:?}", envelope_hash, self.collection); }
2018-09-05 16:08:11 +03:00
}
}