From 651dda67cf3e96270b8f4bbbfba4519fc9e66cd5 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Mon, 2 Mar 2020 12:06:19 +0200 Subject: [PATCH] Respect autoload mailbox setting --- meli.conf.5 | 4 +- melib/src/collection.rs | 7 ++ src/components/mail/listing.rs | 10 +- src/components/mail/listing/compact.rs | 2 +- src/components/mail/listing/conversations.rs | 2 +- src/components/mail/listing/plain.rs | 2 +- src/components/mail/listing/thread.rs | 2 +- src/conf/accounts.rs | 120 ++++++++++--------- src/state.rs | 19 +-- 9 files changed, 87 insertions(+), 81 deletions(-) diff --git a/meli.conf.5 b/meli.conf.5 index 44220bc7..6ee5f969 100644 --- a/meli.conf.5 +++ b/meli.conf.5 @@ -218,7 +218,9 @@ example: .It Ic alias Ar String (optional) show a different name for this mailbox in the UI .It Ic autoload Ar boolean -(optional) load this mailbox on startup (not functional yet) +(optional) load this mailbox on startup +.\" default value +.Pq Em true .It Ic subscribe Ar boolean (optional) watch this mailbox for updates .\" default value diff --git a/melib/src/collection.rs b/melib/src/collection.rs index ae82f1c6..700034ab 100644 --- a/melib/src/collection.rs +++ b/melib/src/collection.rs @@ -391,6 +391,13 @@ impl Collection { pub fn contains_key(&self, env_hash: &EnvelopeHash) -> bool { self.envelopes.read().unwrap().contains_key(env_hash) } + + pub fn new_mailbox(&mut self, mailbox_hash: MailboxHash) { + if !self.mailboxes.contains_key(&mailbox_hash) { + self.mailboxes.insert(mailbox_hash, Default::default()); + self.threads.insert(mailbox_hash, Threads::default()); + } + } } impl Index<&MailboxHash> for Collection { diff --git a/src/components/mail/listing.rs b/src/components/mail/listing.rs index 84ab04d5..07ba1bc2 100644 --- a/src/components/mail/listing.rs +++ b/src/components/mail/listing.rs @@ -1010,8 +1010,11 @@ impl Listing { for (i, &(depth, mailbox_hash)) in a.entries.iter().enumerate() { if mailboxes[&mailbox_hash].is_subscribed() { - match context.accounts[a.index].status(mailbox_hash) { - Ok(_) => { + match context.accounts[a.index][&mailbox_hash].status { + crate::conf::accounts::MailboxStatus::Failed(_) => { + lines.push((depth, i, mailbox_hash, None)); + } + _ => { lines.push(( depth, i, @@ -1019,9 +1022,6 @@ impl Listing { mailboxes[&mailbox_hash].count().ok().map(|(v, _)| v), )); } - Err(_) => { - lines.push((depth, i, mailbox_hash, None)); - } } } } diff --git a/src/components/mail/listing/compact.rs b/src/components/mail/listing/compact.rs index 2aa5c664..ac626aea 100644 --- a/src/components/mail/listing/compact.rs +++ b/src/components/mail/listing/compact.rs @@ -131,7 +131,7 @@ impl MailListingTrait for CompactListing { // Get mailbox as a reference. // - match context.accounts[self.cursor_pos.0].status(self.cursor_pos.1) { + match context.accounts[self.cursor_pos.0].load(self.cursor_pos.1) { Ok(()) => {} Err(_) => { let default_cell = { diff --git a/src/components/mail/listing/conversations.rs b/src/components/mail/listing/conversations.rs index 53487ccf..921b38e1 100644 --- a/src/components/mail/listing/conversations.rs +++ b/src/components/mail/listing/conversations.rs @@ -117,7 +117,7 @@ impl MailListingTrait for ConversationsListing { } // Get mailbox as a reference. // - match context.accounts[self.cursor_pos.0].status(self.cursor_pos.1) { + match context.accounts[self.cursor_pos.0].load(self.cursor_pos.1) { Ok(()) => {} Err(_) => { let default_cell = { diff --git a/src/components/mail/listing/plain.rs b/src/components/mail/listing/plain.rs index 1e796fd4..becce36e 100644 --- a/src/components/mail/listing/plain.rs +++ b/src/components/mail/listing/plain.rs @@ -131,7 +131,7 @@ impl MailListingTrait for PlainListing { // Get mailbox as a reference. // - match context.accounts[self.cursor_pos.0].status(self.cursor_pos.1) { + match context.accounts[self.cursor_pos.0].load(self.cursor_pos.1) { Ok(()) => {} Err(_) => { let default_cell = { diff --git a/src/components/mail/listing/thread.rs b/src/components/mail/listing/thread.rs index 3ddb079c..d88d4c77 100644 --- a/src/components/mail/listing/thread.rs +++ b/src/components/mail/listing/thread.rs @@ -88,7 +88,7 @@ impl MailListingTrait for ThreadListing { // Get mailbox as a reference. // - match context.accounts[self.cursor_pos.0].status(self.cursor_pos.1) { + match context.accounts[self.cursor_pos.0].load(self.cursor_pos.1) { Ok(_) => {} Err(_) => { let default_cell = { diff --git a/src/conf/accounts.rs b/src/conf/accounts.rs index c8a7ae20..cd272689 100644 --- a/src/conf/accounts.rs +++ b/src/conf/accounts.rs @@ -62,7 +62,7 @@ pub enum MailboxStatus { impl Default for MailboxStatus { fn default() -> Self { - MailboxStatus::Parsing(0, 0) + MailboxStatus::None } } @@ -97,7 +97,7 @@ impl MailboxEntry { match self.status { MailboxStatus::Available => self.name().to_string(), MailboxStatus::Failed(ref e) => e.to_string(), - MailboxStatus::None => "Not subscribed, is this a bug?".to_string(), + MailboxStatus::None => "Retrieving mailbox".to_string(), MailboxStatus::Parsing(done, total) => { format!("Parsing messages. [{}/{}]", done, total) } @@ -334,16 +334,17 @@ impl Account { continue; } mailbox_entries.entry(*h).and_modify(|entry| { - entry.status = MailboxStatus::Parsing(0, 0); - entry.worker = Account::new_worker( - f.clone(), - &mut self.backend, - &self.work_context, - self.notify_fn.clone(), - ); + if entry.conf.mailbox_conf.autoload { + entry.status = MailboxStatus::Parsing(0, 0); + entry.worker = Account::new_worker( + f.clone(), + &mut self.backend, + &self.work_context, + self.notify_fn.clone(), + ); + } }); - collection.mailboxes.insert(*h, Default::default()); - collection.threads.insert(*h, Threads::default()); + collection.new_mailbox(*h); } build_mailboxes_order(&mut tree, &mailbox_entries, &mut mailboxes_order); @@ -716,39 +717,7 @@ impl Account { &self.name } - fn load_mailbox(&mut self, mailbox_hash: MailboxHash, payload: Result>) { - if payload.is_err() { - self.mailbox_entries - .entry(mailbox_hash) - .and_modify(|entry| { - entry.status = MailboxStatus::Failed(payload.unwrap_err()); - }); - self.sender - .send(ThreadEvent::UIEvent(UIEvent::StartupCheck(mailbox_hash))) - .unwrap(); - return; - } - let envelopes = payload - .unwrap() - .into_iter() - .map(|e| (e.hash(), e)) - .collect::>(); - if let Some(updated_mailboxes) = - self.collection - .merge(envelopes, mailbox_hash, self.sent_mailbox) - { - for f in updated_mailboxes { - self.sender - .send(ThreadEvent::UIEvent(UIEvent::StartupCheck(f))) - .unwrap(); - } - } - self.sender - .send(ThreadEvent::UIEvent(UIEvent::StartupCheck(mailbox_hash))) - .unwrap(); - } - - pub fn status(&mut self, mailbox_hash: MailboxHash) -> result::Result<(), usize> { + pub fn load(&mut self, mailbox_hash: MailboxHash) -> result::Result<(), usize> { if mailbox_hash == 0 { return Err(0); } @@ -761,22 +730,65 @@ impl Account { .as_mut() { None => { - return if self.mailbox_entries[&mailbox_hash].status.is_available() - || (self.mailbox_entries[&mailbox_hash].status.is_parsing() - && self.collection.mailboxes.contains_key(&mailbox_hash)) - { - Ok(()) - } else { - Err(0) - }; + return match self.mailbox_entries[&mailbox_hash].status { + MailboxStatus::Available | MailboxStatus::Parsing(_, _) + if self.collection.mailboxes.contains_key(&mailbox_hash) => + { + Ok(()) + } + MailboxStatus::None => { + let handle = Account::new_worker( + self.mailbox_entries[&mailbox_hash].ref_mailbox.clone(), + &mut self.backend, + &self.work_context, + self.notify_fn.clone(), + ); + self.mailbox_entries + .entry(mailbox_hash) + .and_modify(|entry| { + entry.worker = handle; + }); + self.collection.new_mailbox(mailbox_hash); + Err(0) + } + _ => Err(0), + } } Some(ref mut w) => match debug!(w.poll()) { Ok(AsyncStatus::NoUpdate) => { break; } - Ok(AsyncStatus::Payload(envs)) => { + Ok(AsyncStatus::Payload(payload)) => { debug!("got payload in status for {}", mailbox_hash); - self.load_mailbox(mailbox_hash, envs); + if payload.is_err() { + self.mailbox_entries + .entry(mailbox_hash) + .and_modify(|entry| { + entry.status = MailboxStatus::Failed(payload.unwrap_err()); + }); + self.sender + .send(ThreadEvent::UIEvent(UIEvent::StartupCheck(mailbox_hash))) + .unwrap(); + return Err(0); + } + let envelopes = payload + .unwrap() + .into_iter() + .map(|e| (e.hash(), e)) + .collect::>(); + if let Some(updated_mailboxes) = + self.collection + .merge(envelopes, mailbox_hash, self.sent_mailbox) + { + for f in updated_mailboxes { + self.sender + .send(ThreadEvent::UIEvent(UIEvent::StartupCheck(f))) + .unwrap(); + } + } + self.sender + .send(ThreadEvent::UIEvent(UIEvent::StartupCheck(mailbox_hash))) + .unwrap(); } Ok(AsyncStatus::Finished) => { debug!("got finished in status for {}", mailbox_hash); diff --git a/src/state.rs b/src/state.rs index c4e9cd6e..e5a4db9b 100644 --- a/src/state.rs +++ b/src/state.rs @@ -37,7 +37,6 @@ use fnv::FnvHashMap; use smallvec::SmallVec; use std::env; use std::io::Write; -use std::result; use std::thread; use termion::raw::IntoRawMode; use termion::screen::AlternateScreen; @@ -129,20 +128,6 @@ impl Context { pub fn restore_input(&self) { self.input.restore(self.sender.clone()); } - pub fn account_status( - &mut self, - idx_a: usize, - mailbox_hash: MailboxHash, - ) -> result::Result<(), usize> { - match self.accounts[idx_a].status(mailbox_hash) { - Ok(()) => { - self.replies - .push_back(UIEvent::MailboxUpdate((idx_a, mailbox_hash))); - Ok(()) - } - Err(n) => Err(n), - } - } pub fn is_online(&mut self, account_pos: usize) -> Result<()> { let Context { @@ -379,7 +364,7 @@ impl State { pub fn refresh_event(&mut self, event: RefreshEvent) { let hash = event.hash(); if let Some(&idxa) = self.context.mailbox_hashes.get(&hash) { - if self.context.accounts[idxa].status(hash).is_err() { + if self.context.accounts[idxa].load(hash).is_err() { self.context.replies.push_back(UIEvent::from(event)); return; } @@ -932,7 +917,7 @@ impl State { } UIEvent::WorkerProgress(mailbox_hash) => { if let Some(&account_idx) = self.context.mailbox_hashes.get(&mailbox_hash) { - let _ = self.context.accounts[account_idx].status(mailbox_hash); + let _ = self.context.accounts[account_idx].load(mailbox_hash); } return; }