krateio 1.0.0

Show job progress in the Plasma notification area via the KDE JobView D-Bus protocol
Documentation
mod common;

use std::sync::mpsc;
use std::time::Duration;

use zbus::zvariant::OwnedObjectPath;

#[derive(Debug, PartialEq)]
enum Event {
    View {
        app_name: String,
        app_icon_name: String,
        capabilities: i32,
    },
    InfoMessage(String),
    Percent(u32),
    TotalAmount(u64, String),
    ProcessedAmount(u64, String),
    Terminate(String),
}

struct Server {
    tx: mpsc::Sender<Event>,
}

#[zbus::interface(name = "org.kde.JobViewServer")]
impl Server {
    #[zbus(name = "requestView")]
    fn request_view(
        &self,
        app_name: String,
        app_icon_name: String,
        capabilities: i32,
    ) -> OwnedObjectPath {
        self.tx
            .send(Event::View {
                app_name,
                app_icon_name,
                capabilities,
            })
            .unwrap();
        OwnedObjectPath::try_from("/JobView_1").unwrap()
    }
}

struct View {
    tx: mpsc::Sender<Event>,
}

#[zbus::interface(name = "org.kde.JobViewV2")]
impl View {
    #[zbus(name = "setInfoMessage")]
    fn set_info_message(&self, message: String) {
        self.tx.send(Event::InfoMessage(message)).unwrap();
    }

    #[zbus(name = "setPercent")]
    fn set_percent(&self, percent: u32) {
        self.tx.send(Event::Percent(percent)).unwrap();
    }

    #[zbus(name = "setTotalAmount")]
    fn set_total_amount(&self, amount: u64, unit: String) {
        self.tx.send(Event::TotalAmount(amount, unit)).unwrap();
    }

    #[zbus(name = "setProcessedAmount")]
    fn set_processed_amount(&self, amount: u64, unit: String) {
        self.tx.send(Event::ProcessedAmount(amount, unit)).unwrap();
    }

    #[zbus(name = "terminate")]
    fn terminate(&self, error_message: String) {
        self.tx.send(Event::Terminate(error_message)).unwrap();
    }
}

fn recv(rx: &mpsc::Receiver<Event>) -> Event {
    rx.recv_timeout(Duration::from_secs(10)).expect("event")
}

#[test]
fn falls_back_to_legacy_protocol() {
    let Some(bus) = common::Bus::spawn() else {
        eprintln!("dbus-daemon not available, skipping");
        return;
    };
    unsafe { std::env::set_var("DBUS_SESSION_BUS_ADDRESS", &bus.address) };

    let (tx, rx) = mpsc::channel();
    let _server_conn = zbus::blocking::connection::Builder::session()
        .unwrap()
        .name("org.kde.JobViewServer")
        .unwrap()
        .serve_at("/JobViewServer", Server { tx: tx.clone() })
        .unwrap()
        .serve_at("/JobView_1", View { tx })
        .unwrap()
        .build()
        .unwrap();

    let tracker = krateio::Tracker::new("org.test.app")
        .unwrap()
        .with_app_name("Test App")
        .with_app_icon("system-software-update");

    let job = tracker
        .job()
        .info_message("Starting")
        .cancellable(true)
        .suspendable(true)
        .start()
        .unwrap();

    assert_eq!(
        recv(&rx),
        Event::View {
            app_name: "Test App".into(),
            app_icon_name: "system-software-update".into(),
            capabilities: 3,
        }
    );
    assert_eq!(recv(&rx), Event::InfoMessage("Starting".into()));

    job.progress(krateio::Unit::Files, 5, 10).unwrap();
    assert_eq!(recv(&rx), Event::TotalAmount(10, "files".into()));
    assert_eq!(recv(&rx), Event::ProcessedAmount(5, "files".into()));
    assert_eq!(recv(&rx), Event::Percent(50));

    job.set_title("ignored on v2").unwrap();

    job.finish().unwrap();
    assert_eq!(recv(&rx), Event::Terminate(String::new()));
}