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 fnv::FnvHashMap;
use std; 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. /// A hashmap containing all available mail backends.
/// An abstraction over any available backends. /// An abstraction over any available backends.
@ -58,15 +58,15 @@ impl Backends {
}; };
b.register( b.register(
"maildir".to_string(), "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( b.register(
"mbox".to_string(), "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( b.register(
"imap".to_string(), "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 b
} }

View File

@ -651,7 +651,7 @@ macro_rules! exit_on_error {
} }
} }
impl ImapType { 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::io::prelude::*;
use std::net::TcpStream; use std::net::TcpStream;
debug!(s); debug!(s);
@ -779,17 +779,26 @@ impl ImapType {
drop(conn); drop(conn);
m.folders = m.imap_folders(); 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() { for f in m.folders.keys() {
m.folder_connections.insert( m.folder_connections.insert(
*f, *f,
Arc::new(Mutex::new(exit_on_error!(s returning m.new_connection()))), Arc::new(Mutex::new(exit_on_error!(s returning m.new_connection()))),
); );
} }*/
m m
} }
pub fn shell(&mut self) { pub fn shell(&mut self) {
self.folders();
let mut conn = self.connection.lock().unwrap(); let mut conn = self.connection.lock().unwrap();
let mut res = String::with_capacity(8 * 1024); let mut res = String::with_capacity(8 * 1024);

View File

@ -529,7 +529,7 @@ impl MailBackend for MaildirType {
} }
impl 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(); let mut folders: FnvHashMap<FolderHash, MaildirFolder> = Default::default();
fn recurse_folders<P: AsRef<Path>>( fn recurse_folders<P: AsRef<Path>>(
folders: &mut FnvHashMap<FolderHash, MaildirFolder>, folders: &mut FnvHashMap<FolderHash, MaildirFolder>,
@ -627,6 +627,7 @@ impl MaildirType {
.count(); .count();
folders.get_mut(&root_hash).map(|f| f.children = children); 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( let hash_indexes = Arc::new(Mutex::new(FnvHashMap::with_capacity_and_hasher(
folders.len(), folders.len(),

View File

@ -540,7 +540,7 @@ impl MailBackend for MboxType {
} }
impl 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()); let path = Path::new(s.root_folder.as_str());
if !path.exists() { if !path.exists() {
panic!( panic!(

View File

@ -562,12 +562,14 @@ impl Listing {
} }
*depth -= 1; *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 */ /* Start with roots */
for f in entries.keys() { for f in keys {
if entries[f].parent().is_none() { if entries[&f].parent().is_none() {
print( print(
*f, f,
&mut depth, &mut depth,
&mut inc, &mut inc,
&entries, &entries,

View File

@ -192,7 +192,13 @@ struct FolderNode {
impl Account { impl Account {
pub fn new(name: String, settings: AccountConf, map: &Backends, notify_fn: NotifyFn) -> Self { 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 ref_folders: FnvHashMap<FolderHash, Folder> = backend.folders();
let mut folders: FnvHashMap<FolderHash, MailboxEntry> = let mut folders: FnvHashMap<FolderHash, MailboxEntry> =
FnvHashMap::with_capacity_and_hasher(ref_folders.len(), Default::default()); FnvHashMap::with_capacity_and_hasher(ref_folders.len(), Default::default());