diff --git a/melib/src/backends.rs b/melib/src/backends.rs index e3981f33..8ea1fbf1 100644 --- a/melib/src/backends.rs +++ b/melib/src/backends.rs @@ -299,11 +299,12 @@ pub type ResultFuture = Result> + Send pub trait MailBackend: ::std::fmt::Debug + Send + Sync { fn is_async(&self) -> bool; fn is_remote(&self) -> bool; - fn is_online(&self) -> Result<()>; + fn is_online(&self) -> Result<()> { + Err(MeliError::new("Unimplemented.")) + } fn is_online_async(&self) -> ResultFuture<()> { Err(MeliError::new("Unimplemented.")) } - fn connect(&mut self) {} fn get(&mut self, mailbox: &Mailbox) -> Result>>>; fn get_async( &mut self, diff --git a/melib/src/backends/imap.rs b/melib/src/backends/imap.rs index f97a770d..2d4a93e8 100644 --- a/melib/src/backends/imap.rs +++ b/melib/src/backends/imap.rs @@ -194,18 +194,6 @@ impl MailBackend for ImapType { } fn is_online(&self) -> Result<()> { - //if let Ok(mut g) = try_lock(&self.connection, None) { - // let _ = g.connect(); - //} - try_lock( - &self.uid_store.is_online, - Some(std::time::Duration::from_millis(12)), - )? - .1 - .clone() - } - - fn connect(&mut self) { let connection = self.connection.clone(); let _ = std::thread::Builder::new() .name(format!("{} connecting", self.account_name.as_str(),)) @@ -214,6 +202,12 @@ impl MailBackend for ImapType { let _ = g.connect(); } }); + try_lock( + &self.uid_store.is_online, + Some(std::time::Duration::from_millis(12)), + )? + .1 + .clone() } fn get(&mut self, mailbox: &Mailbox) -> Result>>> { diff --git a/melib/src/backends/imap_async.rs b/melib/src/backends/imap_async.rs index ba61d2d6..df4feb5c 100644 --- a/melib/src/backends/imap_async.rs +++ b/melib/src/backends/imap_async.rs @@ -282,34 +282,13 @@ impl MailBackend for ImapType { fn is_online_async(&self) -> ResultFuture<()> { let connection = self.connection.clone(); Ok(Box::pin(async move { - debug!("INSIDE is_online_async()"); let mut conn = connection.lock().await; - debug!(conn.connect().await)?; + conn.connect().await?; Ok(()) })) } - fn is_online(&self) -> Result<()> { - Ok(()) - //if let Ok(mut g) = try_lock(&self.connection, None) { - // let _ = g.connect(); - //} - } - - fn connect(&mut self) { - /* - let connection = self.connection.clone(); - let _ = std::thread::Builder::new() - .name(format!("{} connecting", self.account_name.as_str(),)) - .spawn(move || { - if let Ok(mut g) = try_lock(&connection, None) { - let _ = g.connect(); - } - }); - */ - } - fn get(&mut self, _mailbox: &Mailbox) -> Result>>> { Err(MeliError::new("Unimplemented.")) } diff --git a/melib/src/backends/jmap.rs b/melib/src/backends/jmap.rs index 9394a124..737e974e 100644 --- a/melib/src/backends/jmap.rs +++ b/melib/src/backends/jmap.rs @@ -199,16 +199,13 @@ impl MailBackend for JmapType { } fn is_online(&self) -> Result<()> { - self.online.lock().unwrap().1.clone() - } - - fn connect(&mut self) { - if self.is_online().is_err() + if self.online.lock().unwrap().1.is_err() && Instant::now().duration_since(self.online.lock().unwrap().0) >= std::time::Duration::new(2, 0) { let _ = self.mailboxes(); } + self.online.lock().unwrap().1.clone() } fn get(&mut self, mailbox: &Mailbox) -> Result>>> { diff --git a/src/conf/accounts.rs b/src/conf/accounts.rs index 091062e0..9cfee0cf 100644 --- a/src/conf/accounts.rs +++ b/src/conf/accounts.rs @@ -152,8 +152,8 @@ pub struct Account { sender: Sender, event_queue: VecDeque<(MailboxHash, RefreshEvent)>, notify_fn: Arc, - is_async: bool, - is_remote: bool, + pub is_async: bool, + pub is_remote: bool, } pub enum JobRequest { @@ -344,12 +344,11 @@ impl Account { } } } - - Ok(Account { + let mut ret = Account { index, hash, name, - is_online: false, + is_online: !backend.is_remote(), mailbox_entries: Default::default(), mailboxes_order: Default::default(), tree: Default::default(), @@ -366,7 +365,13 @@ impl Account { is_async: backend.is_async(), is_remote: backend.is_remote(), backend: Arc::new(RwLock::new(backend)), - }) + }; + + if !ret.is_remote && !ret.is_async { + ret.init(None)?; + } + + Ok(ret) } fn init(&mut self, ref_mailboxes: Option>) -> Result<()> { @@ -1400,14 +1405,10 @@ impl Account { /* Call only in Context::is_online, since only Context can launch the watcher threads if an * account goes from offline to online. */ pub fn is_online(&mut self) -> Result<()> { - if !self.is_remote { + if !self.is_remote && !self.is_async { return Ok(()); } - if !self.is_online { - self.backend.write().unwrap().connect(); - } - if self.is_async { if self.is_online { return Ok(()); @@ -1425,9 +1426,6 @@ impl Account { self.init(None)?; } self.is_online = ret.is_ok(); - if !self.is_online { - self.backend.write().unwrap().connect(); - } ret } } diff --git a/src/plugins/backend.rs b/src/plugins/backend.rs index d6feb470..b87cbbd1 100644 --- a/src/plugins/backend.rs +++ b/src/plugins/backend.rs @@ -93,8 +93,6 @@ impl MailBackend for PluginBackend { } } - fn connect(&mut self) {} - fn get(&mut self, mailbox: &Mailbox) -> Result>>> { let mut w = AsyncBuilder::new(); let _mailbox_hash = mailbox.hash(); diff --git a/src/state.rs b/src/state.rs index 0953019d..9befc52a 100644 --- a/src/state.rs +++ b/src/state.rs @@ -358,6 +358,9 @@ impl State { s.switch_to_alternate_screen(); debug!("inserting mailbox hashes:"); for i in 0..s.context.accounts.len() { + if !s.context.accounts[i].is_remote { + s.context.accounts[i].watch(); + } if s.context.is_online(i).is_ok() && s.context.accounts[i].is_empty() { //return Err(MeliError::new(format!( // "Account {} has no mailboxes configured.",