aboutsummaryrefslogtreecommitdiffstats
path: root/src/notification.rs
blob: 9c19b0b798fc5957035f57ea9d6280b402ecb335 (plain) (blame)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * This file is a part of RSPlayer
 * Licensed under the GPL-3.0 License
 * Copyright (C) 2026 Salmonik @ salmonik@tuta.io
 * https://codeberg.org/salmonik/RSPlayer
*/

use crate::queue::Track;

pub fn notify_track(track: &Track, format: Option<&str>) {
    let body = match format {
        Some(fmt) => track.format(fmt),
        None => match (track.artist.as_deref(), track.title.as_deref()) {
            (Some(a), Some(t)) => format!("{} - {}", a, t),
            (_, Some(t)) => t.to_string(),
            _ => track
                .path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("Unknown")
                .to_string(),
        },
    };

    // show() is a synchronous D-Bus call that blocks until the notification
    // daemon responds. Spawn it on a separate thread so it never blocks the
    // player event loop or CLI command processing.
    std::thread::spawn(move || {
        let _ = notify_rust::Notification::new()
            .summary("Now Playing")
            .body(&body)
            .appname("RSPlayer")
            .show();
    });
}

pub fn notify_queue_ended() {
    std::thread::spawn(|| {
        let _ = notify_rust::Notification::new()
            .summary("Playback Ended")
            .body("The queue is empty")
            .appname("RSPlayer")
            .show();
    });
}

pub fn notify_change(state: &str) {
    let summary = state.to_owned();
    let body = format!("Playback {}", state.to_lowercase());
    std::thread::spawn(move || {
        let _ = notify_rust::Notification::new()
            .summary(&summary)
            .body(&body)
            .appname("RSPlayer")
            .show();
    });
}

pub fn notify_error(context: &str) {
    let body = format!("{} - Check the logs for details", context);
    std::thread::spawn(move || {
        let _ = notify_rust::Notification::new()
            .summary("RSPlayer Error")
            .body(&body)
            .appname("RSPlayer")
            .show();
    });
}

pub fn notify(summary: &str, body: &str) {
    let summary = summary.to_owned();
    let body = body.to_owned();
    std::thread::spawn(move || {
        let _ = notify_rust::Notification::new()
            .summary(&summary)
            .body(&body)
            .appname("RSPlayer")
            .show();
    });
}