summaryrefslogtreecommitdiff
path: root/src/cmd/stats.rs
blob: e8e8325ad1d0d2c8e6909834f7ae956d63b63a95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use crate::cmd::{open_submaildir, MailStorage};
use crate::de_jmhoffmann_jwebmail_mailstorage::Call_Stats;

pub fn stats(ms: &MailStorage, call: &mut dyn Call_Stats, folder: String) -> varlink::Result<()> {
    if let Some(maildir_path) = ms.maildir_path.read().unwrap().clone() {
        let maildir = open_submaildir(maildir_path, &folder);

        let mail_count = maildir.count_cur() as i64;
        let unread_count = maildir
            .list_cur()
            .filter(|x| x.as_ref().map_or(false, |z| !z.is_seen()))
            .count() as i64;
        let byte_size: u64 = maildir
            .path()
            .join("cur")
            .read_dir()
            .map_err(varlink::map_context!())?
            .map(|x| x.map_or(0, |z| z.metadata().map_or(0, |y| y.len())))
            .sum();

        call.reply(mail_count, unread_count, byte_size as i64)
    } else {
        call.reply_not_initialized()
    }
}