melib: filter subscribed folders in MailBackend::new()

embed
Manos Pitsidianakis 2019-09-06 12:48:17 +03:00
parent 64dc44f18f
commit c3e3c98fb0
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
6 changed files with 31 additions and 13 deletions

View File

@ -37,7 +37,7 @@ use std::ops::Deref;
use fnv::FnvHashMap;
use std;
pub type BackendCreator = Box<Fn(&AccountSettings) -> Box<MailBackend>>;
pub type BackendCreator = Box<Fn(&AccountSettings, Box<Fn(&str) -> bool>) -> Box<MailBackend>>;
/// A hashmap containing all available mail backends.
/// An abstraction over any available backends.
@ -58,15 +58,15 @@ impl Backends {
};
b.register(
"maildir".to_string(),
Box::new(|| Box::new(|f| Box::new(MaildirType::new(f)))),
Box::new(|| Box::new(|f, i| Box::new(MaildirType::new(f, i)))),
);
b.register(
"mbox".to_string(),
Box::new(|| Box::new(|f| Box::new(MboxType::new(f)))),
Box::new(|| Box::new(|f, i| Box::new(MboxType::new(f, i)))),
);
b.register(
"imap".to_string(),
Box::new(|| Box::new(|f| Box::new(ImapType::new(f)))),
Box::new(|| Box::new(|f, i| Box::new(ImapType::new(f, i)))),
);
b
}

View File

@ -651,7 +651,7 @@ macro_rules! exit_on_error {
}
}
impl ImapType {
pub fn new(s: &AccountSettings) -> Self {
pub fn new(s: &AccountSettings, is_subscribed: Box<Fn(&str) -> bool>) -> Self {
use std::io::prelude::*;
use std::net::TcpStream;
debug!(s);
@ -779,17 +779,26 @@ impl ImapType {
drop(conn);
m.folders = m.imap_folders();
m.folders.retain(|_, f| is_subscribed(f.path()));
let keys = m
.folders
.keys()
.cloned()
.collect::<FnvHashSet<FolderHash>>();
for f in m.folders.values_mut() {
f.children.retain(|c| keys.contains(c));
}
/*
for f in m.folders.keys() {
m.folder_connections.insert(
*f,
Arc::new(Mutex::new(exit_on_error!(s returning m.new_connection()))),
);
}
}*/
m
}
pub fn shell(&mut self) {
self.folders();
let mut conn = self.connection.lock().unwrap();
let mut res = String::with_capacity(8 * 1024);

View File

@ -529,7 +529,7 @@ impl MailBackend for MaildirType {
}
impl MaildirType {
pub fn new(settings: &AccountSettings) -> Self {
pub fn new(settings: &AccountSettings, is_subscribed: Box<Fn(&str) -> bool>) -> Self {
let mut folders: FnvHashMap<FolderHash, MaildirFolder> = Default::default();
fn recurse_folders<P: AsRef<Path>>(
folders: &mut FnvHashMap<FolderHash, MaildirFolder>,
@ -627,6 +627,7 @@ impl MaildirType {
.count();
folders.get_mut(&root_hash).map(|f| f.children = children);
}
folders.retain(|_, f| is_subscribed(f.path()));
let hash_indexes = Arc::new(Mutex::new(FnvHashMap::with_capacity_and_hasher(
folders.len(),

View File

@ -540,7 +540,7 @@ impl MailBackend for MboxType {
}
impl MboxType {
pub fn new(s: &AccountSettings) -> Self {
pub fn new(s: &AccountSettings, _is_subscribed: Box<Fn(&str) -> bool>) -> Self {
let path = Path::new(s.root_folder.as_str());
if !path.exists() {
panic!(

View File

@ -562,12 +562,14 @@ impl Listing {
}
*depth -= 1;
}
let mut keys = entries.keys().cloned().collect::<Vec<FolderHash>>();
keys.sort_unstable_by(|a, b| folders_order[a].partial_cmp(&folders_order[b]).unwrap());
/* Start with roots */
for f in entries.keys() {
if entries[f].parent().is_none() {
for f in keys {
if entries[&f].parent().is_none() {
print(
*f,
f,
&mut depth,
&mut inc,
&entries,

View File

@ -192,7 +192,13 @@ struct FolderNode {
impl Account {
pub fn new(name: String, settings: AccountConf, map: &Backends, notify_fn: NotifyFn) -> Self {
let mut backend = map.get(settings.account().format())(settings.account());
let s = settings.clone();
let mut backend = map.get(settings.account().format())(
settings.account(),
Box::new(move |path: &str| {
s.folder_confs.contains_key(path) && s.folder_confs[path].subscribe.is_true()
}),
);
let mut ref_folders: FnvHashMap<FolderHash, Folder> = backend.folders();
let mut folders: FnvHashMap<FolderHash, MailboxEntry> =
FnvHashMap::with_capacity_and_hasher(ref_folders.len(), Default::default());