Respect autoload mailbox setting

master
Manos Pitsidianakis 2020-03-02 12:06:19 +02:00
parent 106dae3334
commit 651dda67cf
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
9 changed files with 87 additions and 81 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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));
}
}
}
}

View File

@ -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 = {

View File

@ -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 = {

View File

@ -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 = {

View File

@ -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 = {

View File

@ -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<Vec<Envelope>>) {
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::<FnvHashMap<EnvelopeHash, Envelope>>();
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::<FnvHashMap<EnvelopeHash, Envelope>>();
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);

View File

@ -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;
}