summaryrefslogtreecommitdiffstats
path: root/melib/src/mailbox/collection.rs
blob: 7a7f2e3392266bbe8f6541e8a4dbe638ef85c846 (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
use super::*;
use std::collections::BTreeMap;
use std::fs;
use std::io;
use std::ops::{Deref, DerefMut};

use fnv::FnvHashMap;

/// `Mailbox` represents a folder of mail.
#[derive(Debug, Clone, Default)]
pub struct Collection {
    folder: Folder,
    pub envelopes: FnvHashMap<EnvelopeHash, Envelope>,
    date_index: BTreeMap<UnixTimestamp, EnvelopeHash>,
    subject_index: Option<BTreeMap<String, EnvelopeHash>>,
    pub threads: Threads,
}

impl Drop for Collection {
    fn drop(&mut self) {
        let cache_dir =
            xdg::BaseDirectories::with_profile("meli", format!("{}_Thread", self.folder.hash()))
                .unwrap();
        if let Ok(cached) = cache_dir.place_cache_file("threads") {
            /* place result in cache directory */
            let f = match fs::File::create(cached) {
                Ok(f) => f,
                Err(e) => {
                    panic!("{}", e);
                }
            };
            let writer = io::BufWriter::new(f);
            bincode::serialize_into(writer, &self.threads).unwrap();
        }
    }
}

impl Collection {
    pub fn new(vec: Vec<Envelope>, folder: &Folder) -> Collection {
        let mut envelopes: FnvHashMap<EnvelopeHash, Envelope> =
            FnvHashMap::with_capacity_and_hasher(vec.len(), Default::default());
        for e in vec {
            envelopes.insert(e.hash(), e);
        }
        let date_index = BTreeMap::new();
        let subject_index = None;

        /* Scrap caching for now. When a cached threads file is loaded, we must remove/rehash the
         * thread nodes that shouldn't exist anymore (e.g. because their file moved from /new to
         * /cur, or it was deleted).
         */
        let threads = Threads::new(&mut envelopes);

        /*let cache_dir =
                            xdg::BaseDirectories::with_profile("meli", format!("{}_Thread", folder.hash()))
                                .unwrap();
                        if let Some(cached) = cache_dir.find_cache_file("threads") {
                            let reader = io::BufReader::new(fs::File::open(cached).unwrap());
                            let result: result::Result<Threads, _> = bincode::deserialize_from(reader);
                            let ret = if let Ok(mut cached_t) = result {
                use std::iter::FromIterator;
                                if cfg!(debug_assertions) {
        eprint!("{}:{}_{}:	", file!(), line!(), column!());
eprintln!("loaded cache, our hash set is {:?}\n and the cached one is {:?}", FnvHashSet::from_iter(envelopes.keys().cloned()), cached_t.hash_set);
        }
                                cached_t.amend(&mut envelopes);
                                cached_t
                            } else {
                                Threads::new(&mut envelopes)
                            };
                            ret
                        } else {
                            Threads::new(&mut envelopes)
                        };
                        */

        Collection {
            folder: folder.clone(),
            envelopes,
            date_index,
            subject_index,
            threads,
        }
    }

    pub fn len(&self) -> usize {
        self.envelopes.len()
    }

    pub fn is_empty(&self) -> bool {
        self.envelopes.is_empty()
    }

    pub fn remove(&mut self, envelope_hash: EnvelopeHash) {
        if cfg!(debug_assertions) {
            eprint!("{}:{}_{}:	", file!(), line!(), column!());
eprintln!("DEBUG: Removing {}", envelope_hash);
        }
        self.envelopes.remove(&envelope_hash);
        self.threads.remove(envelope_hash, &mut self.envelopes);
    }

    pub fn rename(&mut self, old_hash: EnvelopeHash, new_hash: EnvelopeHash) {
        if !self.envelopes.contains_key(&old_hash) {
            return;
        }
        let mut env = self.envelopes.remove(&old_hash).unwrap();
        env.set_hash(new_hash);
        self.envelopes.insert(new_hash, env);
        {
            if self
                .threads
                .update_envelope(old_hash, new_hash, &self.envelopes)
                .is_ok()
            {
                return;
            }
        }
        /* envelope is not in threads, so insert it */
        let env = self.envelopes.entry(new_hash).or_default() as *mut Envelope;
        unsafe {
            self.threads.insert(&mut (*env), &self.envelopes);
        }
    }

    pub fn update_envelope(&mut self, old_hash: EnvelopeHash, envelope: Envelope) {
        self.envelopes.remove(&old_hash);
        let new_hash = envelope.hash();
        self.envelopes.insert(new_hash, envelope);
        {
            if self
                .threads
                .update_envelope(old_hash, new_hash, &self.envelopes)
                .is_ok()
            {
                return;
            }
        }
        /* envelope is not in threads, so insert it */
        let env = self.envelopes.entry(new_hash).or_default() as *mut Envelope;
        unsafe {
            self.threads.insert(&mut (*env), &self.envelopes);
        }
    }

    pub fn insert(&mut self, envelope: Envelope) {
        let hash = envelope.hash();
        if cfg!(debug_assertions) {
            eprint!("{}:{}_{}:	", file!(), line!(), column!());
eprintln!("DEBUG: Inserting hash {} in {}", hash, self.folder.name());
        }
        self.envelopes.insert(hash, envelope);
        let env = self.envelopes.entry(hash).or_default() as *mut Envelope;
        unsafe {
            self.threads.insert(&mut (*env), &self.envelopes);
        }
    }
    pub(crate) fn insert_reply(&mut self, _envelope: &Envelope) {
        return;
        /*
                //self.insert(envelope);
                if cfg!(debug_assertions) {
        eprint!("{}:{}_{}:	", file!(), line!(), column!());
eprintln!("insert_reply in collections");
        }
                self.threads.insert_reply(envelope, &mut self.envelopes);
                */
    }
}

impl Deref for Collection {
    type Target = FnvHashMap<EnvelopeHash, Envelope>;

    fn deref(&self) -> &FnvHashMap<EnvelopeHash, Envelope> {
        &self.envelopes
    }
}

impl DerefMut for Collection {
    fn deref_mut(&mut self) -> &mut FnvHashMap<EnvelopeHash, Envelope> {
        &mut self.envelopes
    }
}