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