deltachat/scheduler/
connectivity.rs

1use core::fmt;
2use std::cmp::min;
3use std::{iter::once, ops::Deref, sync::Arc};
4
5use anyhow::Result;
6use humansize::{format_size, BINARY};
7use tokio::sync::Mutex;
8
9use crate::events::EventType;
10use crate::imap::{scan_folders::get_watched_folder_configs, FolderMeaning};
11use crate::quota::{QUOTA_ERROR_THRESHOLD_PERCENTAGE, QUOTA_WARN_THRESHOLD_PERCENTAGE};
12use crate::stock_str;
13use crate::{context::Context, log::LogExt};
14
15use super::InnerSchedulerState;
16
17/// Rough connectivity status for display in the status bar in the UI.
18#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumProperty, PartialOrd, Ord)]
19pub enum Connectivity {
20    /// Not connected.
21    ///
22    /// This may be because we just started,
23    /// because we lost connection and
24    /// were not able to connect and log in yet
25    /// or because I/O is not started.
26    NotConnected = 1000,
27
28    /// Attempting to connect and log in.
29    Connecting = 2000,
30
31    /// Fetching or sending messages.
32    Working = 3000,
33
34    /// We are connected but not doing anything.
35    ///
36    /// This is the most common state,
37    /// so mobile UIs display the profile name
38    /// instead of connectivity status in this state.
39    /// Desktop UI displays "Connected" in the tooltip,
40    /// which signals that no more messages
41    /// are coming in.
42    Connected = 4000,
43}
44
45// The order of the connectivities is important: worse connectivities (i.e. those at
46// the top) take priority. This means that e.g. if any folder has an error - usually
47// because there is no internet connection - the connectivity for the whole
48// account will be `Notconnected`.
49#[derive(Debug, Default, Clone, PartialEq, Eq, EnumProperty, PartialOrd)]
50enum DetailedConnectivity {
51    Error(String),
52    #[default]
53    Uninitialized,
54
55    /// Attempting to connect,
56    /// until we successfully log in.
57    Connecting,
58
59    /// Connection is just established,
60    /// there may be work to do.
61    Preparing,
62
63    /// There is actual work to do, e.g. there are messages in SMTP queue
64    /// or we detected a message on IMAP server that should be downloaded.
65    Working,
66
67    InterruptingIdle,
68
69    /// Connection is established and is idle.
70    Idle,
71
72    /// The folder was configured not to be watched or configured_*_folder is not set
73    NotConfigured,
74}
75
76impl DetailedConnectivity {
77    fn to_basic(&self) -> Option<Connectivity> {
78        match self {
79            DetailedConnectivity::Error(_) => Some(Connectivity::NotConnected),
80            DetailedConnectivity::Uninitialized => Some(Connectivity::NotConnected),
81            DetailedConnectivity::Connecting => Some(Connectivity::Connecting),
82            DetailedConnectivity::Working => Some(Connectivity::Working),
83            DetailedConnectivity::InterruptingIdle => Some(Connectivity::Working),
84
85            // At this point IMAP has just connected,
86            // but does not know yet if there are messages to download.
87            // We still convert this to Working state
88            // so user can see "Updating..." and not "Connected"
89            // which is reserved for idle state.
90            DetailedConnectivity::Preparing => Some(Connectivity::Working),
91
92            // Just don't return a connectivity, probably the folder is configured not to be
93            // watched or there is e.g. no "Sent" folder, so we are not interested in it
94            DetailedConnectivity::NotConfigured => None,
95
96            DetailedConnectivity::Idle => Some(Connectivity::Connected),
97        }
98    }
99
100    fn to_icon(&self) -> String {
101        match self {
102            DetailedConnectivity::Error(_)
103            | DetailedConnectivity::Uninitialized
104            | DetailedConnectivity::NotConfigured => "<span class=\"red dot\"></span>".to_string(),
105            DetailedConnectivity::Connecting => "<span class=\"yellow dot\"></span>".to_string(),
106            DetailedConnectivity::Preparing
107            | DetailedConnectivity::Working
108            | DetailedConnectivity::InterruptingIdle
109            | DetailedConnectivity::Idle => "<span class=\"green dot\"></span>".to_string(),
110        }
111    }
112
113    async fn to_string_imap(&self, context: &Context) -> String {
114        match self {
115            DetailedConnectivity::Error(e) => stock_str::error(context, e).await,
116            DetailedConnectivity::Uninitialized => "Not started".to_string(),
117            DetailedConnectivity::Connecting => stock_str::connecting(context).await,
118            DetailedConnectivity::Preparing | DetailedConnectivity::Working => {
119                stock_str::updating(context).await
120            }
121            DetailedConnectivity::InterruptingIdle | DetailedConnectivity::Idle => {
122                stock_str::connected(context).await
123            }
124            DetailedConnectivity::NotConfigured => "Not configured".to_string(),
125        }
126    }
127
128    async fn to_string_smtp(&self, context: &Context) -> String {
129        match self {
130            DetailedConnectivity::Error(e) => stock_str::error(context, e).await,
131            DetailedConnectivity::Uninitialized => {
132                "You did not try to send a message recently.".to_string()
133            }
134            DetailedConnectivity::Connecting => stock_str::connecting(context).await,
135            DetailedConnectivity::Working => stock_str::sending(context).await,
136
137            // We don't know any more than that the last message was sent successfully;
138            // since sending the last message, connectivity could have changed, which we don't notice
139            // until another message is sent
140            DetailedConnectivity::InterruptingIdle
141            | DetailedConnectivity::Preparing
142            | DetailedConnectivity::Idle => stock_str::last_msg_sent_successfully(context).await,
143            DetailedConnectivity::NotConfigured => "Not configured".to_string(),
144        }
145    }
146
147    fn all_work_done(&self) -> bool {
148        match self {
149            DetailedConnectivity::Error(_) => true,
150            DetailedConnectivity::Uninitialized => false,
151            DetailedConnectivity::Connecting => false,
152            DetailedConnectivity::Working => false,
153            DetailedConnectivity::InterruptingIdle => false,
154            DetailedConnectivity::Preparing => false, // Just connected, there may still be work to do.
155            DetailedConnectivity::NotConfigured => true,
156            DetailedConnectivity::Idle => true,
157        }
158    }
159}
160
161#[derive(Clone, Default)]
162pub(crate) struct ConnectivityStore(Arc<Mutex<DetailedConnectivity>>);
163
164impl ConnectivityStore {
165    async fn set(&self, context: &Context, v: DetailedConnectivity) {
166        {
167            *self.0.lock().await = v;
168        }
169        context.emit_event(EventType::ConnectivityChanged);
170    }
171
172    pub(crate) async fn set_err(&self, context: &Context, e: impl ToString) {
173        self.set(context, DetailedConnectivity::Error(e.to_string()))
174            .await;
175    }
176    pub(crate) async fn set_connecting(&self, context: &Context) {
177        self.set(context, DetailedConnectivity::Connecting).await;
178    }
179    pub(crate) async fn set_working(&self, context: &Context) {
180        self.set(context, DetailedConnectivity::Working).await;
181    }
182    pub(crate) async fn set_preparing(&self, context: &Context) {
183        self.set(context, DetailedConnectivity::Preparing).await;
184    }
185    pub(crate) async fn set_not_configured(&self, context: &Context) {
186        self.set(context, DetailedConnectivity::NotConfigured).await;
187    }
188    pub(crate) async fn set_idle(&self, context: &Context) {
189        self.set(context, DetailedConnectivity::Idle).await;
190    }
191
192    async fn get_detailed(&self) -> DetailedConnectivity {
193        self.0.lock().await.deref().clone()
194    }
195    async fn get_basic(&self) -> Option<Connectivity> {
196        self.0.lock().await.to_basic()
197    }
198    async fn get_all_work_done(&self) -> bool {
199        self.0.lock().await.all_work_done()
200    }
201}
202
203/// Set all folder states to InterruptingIdle in case they were `Idle` before.
204/// Called during `dc_maybe_network()` to make sure that `all_work_done()`
205/// returns false immediately after `dc_maybe_network()`.
206pub(crate) async fn idle_interrupted(inbox: ConnectivityStore, oboxes: Vec<ConnectivityStore>) {
207    let mut connectivity_lock = inbox.0.lock().await;
208    // For the inbox, we also have to set the connectivity to InterruptingIdle if it was
209    // NotConfigured before: If all folders are NotConfigured, dc_get_connectivity()
210    // returns Connected. But after dc_maybe_network(), dc_get_connectivity() must not
211    // return Connected until DC is completely done with fetching folders; this also
212    // includes scan_folders() which happens on the inbox thread.
213    if *connectivity_lock == DetailedConnectivity::Idle
214        || *connectivity_lock == DetailedConnectivity::NotConfigured
215    {
216        *connectivity_lock = DetailedConnectivity::InterruptingIdle;
217    }
218    drop(connectivity_lock);
219
220    for state in oboxes {
221        let mut connectivity_lock = state.0.lock().await;
222        if *connectivity_lock == DetailedConnectivity::Idle {
223            *connectivity_lock = DetailedConnectivity::InterruptingIdle;
224        }
225    }
226    // No need to send ConnectivityChanged, the user-facing connectivity doesn't change because
227    // of what we do here.
228}
229
230/// Set the connectivity to "Not connected" after a call to dc_maybe_network_lost().
231/// If we did not do this, the connectivity would stay "Connected" for quite a long time
232/// after `maybe_network_lost()` was called.
233pub(crate) async fn maybe_network_lost(context: &Context, stores: Vec<ConnectivityStore>) {
234    for store in &stores {
235        let mut connectivity_lock = store.0.lock().await;
236        if !matches!(
237            *connectivity_lock,
238            DetailedConnectivity::Uninitialized
239                | DetailedConnectivity::Error(_)
240                | DetailedConnectivity::NotConfigured,
241        ) {
242            *connectivity_lock = DetailedConnectivity::Error("Connection lost".to_string());
243        }
244    }
245    context.emit_event(EventType::ConnectivityChanged);
246}
247
248impl fmt::Debug for ConnectivityStore {
249    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
250        if let Ok(guard) = self.0.try_lock() {
251            write!(f, "ConnectivityStore {:?}", &*guard)
252        } else {
253            write!(f, "ConnectivityStore [LOCKED]")
254        }
255    }
256}
257
258impl Context {
259    /// Get the current connectivity, i.e. whether the device is connected to the IMAP server.
260    /// One of:
261    /// - DC_CONNECTIVITY_NOT_CONNECTED (1000-1999): Show e.g. the string "Not connected" or a red dot
262    /// - DC_CONNECTIVITY_CONNECTING (2000-2999): Show e.g. the string "Connecting…" or a yellow dot
263    /// - DC_CONNECTIVITY_WORKING (3000-3999): Show e.g. the string "Updating…" or a spinning wheel
264    /// - DC_CONNECTIVITY_CONNECTED (>=4000): Show e.g. the string "Connected" or a green dot
265    ///
266    /// We don't use exact values but ranges here so that we can split up
267    /// states into multiple states in the future.
268    ///
269    /// Meant as a rough overview that can be shown
270    /// e.g. in the title of the main screen.
271    ///
272    /// If the connectivity changes, a DC_EVENT_CONNECTIVITY_CHANGED will be emitted.
273    pub async fn get_connectivity(&self) -> Connectivity {
274        let lock = self.scheduler.inner.read().await;
275        let stores: Vec<_> = match *lock {
276            InnerSchedulerState::Started(ref sched) => sched
277                .boxes()
278                .map(|b| b.conn_state.state.connectivity.clone())
279                .collect(),
280            _ => return Connectivity::NotConnected,
281        };
282        drop(lock);
283
284        let mut connectivities = Vec::new();
285        for s in stores {
286            if let Some(connectivity) = s.get_basic().await {
287                connectivities.push(connectivity);
288            }
289        }
290        connectivities
291            .into_iter()
292            .min()
293            .unwrap_or(Connectivity::Connected)
294    }
295
296    /// Get an overview of the current connectivity, and possibly more statistics.
297    /// Meant to give the user more insight about the current status than
298    /// the basic connectivity info returned by dc_get_connectivity(); show this
299    /// e.g., if the user taps on said basic connectivity info.
300    ///
301    /// If this page changes, a DC_EVENT_CONNECTIVITY_CHANGED will be emitted.
302    ///
303    /// This comes as an HTML from the core so that we can easily improve it
304    /// and the improvement instantly reaches all UIs.
305    pub async fn get_connectivity_html(&self) -> Result<String> {
306        let mut ret = r#"<!DOCTYPE html>
307            <html>
308            <head>
309                <meta charset="UTF-8" />
310                <meta name="viewport" content="initial-scale=1.0; user-scalable=no" />
311                <style>
312                    ul {
313                        list-style-type: none;
314                        padding-left: 1em;
315                    }
316                    .dot {
317                        height: 0.9em; width: 0.9em;
318                        border: 1px solid #888;
319                        border-radius: 50%;
320                        display: inline-block;
321                        position: relative; left: -0.1em; top: 0.1em;
322                    }
323                    .bar {
324                        width: 90%;
325                        border: 1px solid #888;
326                        border-radius: .5em;
327                        margin-top: .2em;
328                        margin-bottom: 1em;
329                        position: relative; left: -0.2em;
330                    }
331                    .progress {
332                        min-width:1.8em;
333                        height: 1em;
334                        border-radius: .45em;
335                        color: white;
336                        text-align: center;
337                        padding-bottom: 2px;
338                    }
339                    .red {
340                        background-color: #f33b2d;
341                    }
342                    .green {
343                        background-color: #34c759;
344                    }
345                    .yellow {
346                        background-color: #fdc625;
347                    }
348                </style>
349            </head>
350            <body>"#
351            .to_string();
352
353        // =============================================================================================
354        //                              Get the states from the RwLock
355        // =============================================================================================
356
357        let lock = self.scheduler.inner.read().await;
358        let (folders_states, smtp) = match *lock {
359            InnerSchedulerState::Started(ref sched) => (
360                sched
361                    .boxes()
362                    .map(|b| (b.meaning, b.conn_state.state.connectivity.clone()))
363                    .collect::<Vec<_>>(),
364                sched.smtp.state.connectivity.clone(),
365            ),
366            _ => {
367                ret += &format!(
368                    "<h3>{}</h3>\n</body></html>\n",
369                    stock_str::not_connected(self).await
370                );
371                return Ok(ret);
372            }
373        };
374        drop(lock);
375
376        // =============================================================================================
377        // Add e.g.
378        //                              Incoming messages
379        //                               - "Inbox": Connected
380        //                               - "Sent": Connected
381        // =============================================================================================
382
383        let watched_folders = get_watched_folder_configs(self).await?;
384        let incoming_messages = stock_str::incoming_messages(self).await;
385        ret += &format!("<h3>{incoming_messages}</h3><ul>");
386        for (folder, state) in &folders_states {
387            let mut folder_added = false;
388
389            if let Some(config) = folder.to_config().filter(|c| watched_folders.contains(c)) {
390                let f = self.get_config(config).await.log_err(self).ok().flatten();
391
392                if let Some(foldername) = f {
393                    let detailed = &state.get_detailed().await;
394                    ret += "<li>";
395                    ret += &*detailed.to_icon();
396                    ret += " <b>";
397                    ret += &*escaper::encode_minimal(&foldername);
398                    ret += ":</b> ";
399                    ret += &*escaper::encode_minimal(&detailed.to_string_imap(self).await);
400                    ret += "</li>";
401
402                    folder_added = true;
403                }
404            }
405
406            if !folder_added && folder == &FolderMeaning::Inbox {
407                let detailed = &state.get_detailed().await;
408                if let DetailedConnectivity::Error(_) = detailed {
409                    // On the inbox thread, we also do some other things like scan_folders and run jobs
410                    // so, maybe, the inbox is not watched, but something else went wrong
411                    ret += "<li>";
412                    ret += &*detailed.to_icon();
413                    ret += " ";
414                    ret += &*escaper::encode_minimal(&detailed.to_string_imap(self).await);
415                    ret += "</li>";
416                }
417            }
418        }
419        ret += "</ul>";
420
421        // =============================================================================================
422        // Add e.g.
423        //                              Outgoing messages
424        //                                Your last message was sent successfully
425        // =============================================================================================
426
427        let outgoing_messages = stock_str::outgoing_messages(self).await;
428        ret += &format!("<h3>{outgoing_messages}</h3><ul><li>");
429        let detailed = smtp.get_detailed().await;
430        ret += &*detailed.to_icon();
431        ret += " ";
432        ret += &*escaper::encode_minimal(&detailed.to_string_smtp(self).await);
433        ret += "</li></ul>";
434
435        // =============================================================================================
436        // Add e.g.
437        //                              Storage on testrun.org
438        //                                1.34 GiB of 2 GiB used
439        //                                [======67%=====       ]
440        // =============================================================================================
441
442        let domain =
443            &deltachat_contact_tools::EmailAddress::new(&self.get_primary_self_addr().await?)?
444                .domain;
445        let storage_on_domain = stock_str::storage_on_domain(self, domain).await;
446        ret += &format!("<h3>{storage_on_domain}</h3><ul>");
447        let quota = self.quota.read().await;
448        if let Some(quota) = &*quota {
449            match &quota.recent {
450                Ok(quota) => {
451                    if !quota.is_empty() {
452                        for (root_name, resources) in quota {
453                            use async_imap::types::QuotaResourceName::*;
454                            for resource in resources {
455                                ret += "<li>";
456
457                                // root name is empty eg. for gmail and redundant eg. for riseup.
458                                // therefore, use it only if there are really several roots.
459                                if quota.len() > 1 && !root_name.is_empty() {
460                                    ret += &format!(
461                                        "<b>{}:</b> ",
462                                        &*escaper::encode_minimal(root_name)
463                                    );
464                                } else {
465                                    info!(
466                                        self,
467                                        "connectivity: root name hidden: \"{}\"", root_name
468                                    );
469                                }
470
471                                let messages = stock_str::messages(self).await;
472                                let part_of_total_used = stock_str::part_of_total_used(
473                                    self,
474                                    &resource.usage.to_string(),
475                                    &resource.limit.to_string(),
476                                )
477                                .await;
478                                ret += &match &resource.name {
479                                    Atom(resource_name) => {
480                                        format!(
481                                            "<b>{}:</b> {}",
482                                            &*escaper::encode_minimal(resource_name),
483                                            part_of_total_used
484                                        )
485                                    }
486                                    Message => {
487                                        format!("<b>{part_of_total_used}:</b> {messages}")
488                                    }
489                                    Storage => {
490                                        // do not use a special title needed for "Storage":
491                                        // - it is usually shown directly under the "Storage" headline
492                                        // - by the units "1 MB of 10 MB used" there is some difference to eg. "Messages: 1 of 10 used"
493                                        // - the string is not longer than the other strings that way (minus title, plus units) -
494                                        //   additional linebreaks on small displays are unlikely therefore
495                                        // - most times, this is the only item anyway
496                                        let usage = &format_size(resource.usage * 1024, BINARY);
497                                        let limit = &format_size(resource.limit * 1024, BINARY);
498                                        stock_str::part_of_total_used(self, usage, limit).await
499                                    }
500                                };
501
502                                let percent = resource.get_usage_percentage();
503                                let color = if percent >= QUOTA_ERROR_THRESHOLD_PERCENTAGE {
504                                    "red"
505                                } else if percent >= QUOTA_WARN_THRESHOLD_PERCENTAGE {
506                                    "yellow"
507                                } else {
508                                    "green"
509                                };
510                                let div_width_percent = min(100, percent);
511                                ret += &format!("<div class=\"bar\"><div class=\"progress {color}\" style=\"width: {div_width_percent}%\">{percent}%</div></div>");
512
513                                ret += "</li>";
514                            }
515                        }
516                    } else {
517                        ret += format!("<li>Warning: {domain} claims to support quota but gives no information</li>").as_str();
518                    }
519                }
520                Err(e) => {
521                    ret += format!("<li>{e}</li>").as_str();
522                }
523            }
524        } else {
525            let not_connected = stock_str::not_connected(self).await;
526            ret += &format!("<li>{not_connected}</li>");
527        }
528        ret += "</ul>";
529
530        // =============================================================================================
531
532        ret += "</body></html>\n";
533        Ok(ret)
534    }
535
536    /// Returns true if all background work is done.
537    async fn all_work_done(&self) -> bool {
538        let lock = self.scheduler.inner.read().await;
539        let stores: Vec<_> = match *lock {
540            InnerSchedulerState::Started(ref sched) => sched
541                .boxes()
542                .map(|b| &b.conn_state.state)
543                .chain(once(&sched.smtp.state))
544                .map(|state| state.connectivity.clone())
545                .collect(),
546            _ => return false,
547        };
548        drop(lock);
549
550        for s in &stores {
551            if !s.get_all_work_done().await {
552                return false;
553            }
554        }
555        true
556    }
557
558    /// Waits until background work is finished.
559    pub async fn wait_for_all_work_done(&self) {
560        // Ideally we could wait for connectivity change events,
561        // but sleep loop is good enough.
562
563        // First 100 ms sleep in chunks of 10 ms.
564        for _ in 0..10 {
565            if self.all_work_done().await {
566                break;
567            }
568            tokio::time::sleep(std::time::Duration::from_millis(10)).await;
569        }
570
571        // If we are not finished in 100 ms, keep waking up every 100 ms.
572        while !self.all_work_done().await {
573            tokio::time::sleep(std::time::Duration::from_millis(100)).await;
574        }
575    }
576}