diff --git a/src/queue.rs b/src/queue.rs index 867eb34..308212c 100644 --- a/src/queue.rs +++ b/src/queue.rs @@ -12,6 +12,9 @@ use crate::playable::Playable; use crate::spotify::Spotify; use crate::{config::Config, spotify::PlayerEvent}; +#[cfg(feature = "cover")] +use crate::ui; + #[derive(Display, Clone, Copy, PartialEq, Debug, Serialize, Deserialize)] pub enum RepeatSetting { #[serde(rename = "off")] @@ -262,11 +265,14 @@ impl Queue { let mut current = self.current_track.write().unwrap(); current.replace(index); self.spotify.update_track(); + + #[cfg(feature = "notify")] if self.cfg.values().notify.unwrap_or(false) { - #[cfg(feature = "notify")] - if let Err(e) = Notification::new().summary(&track.to_string()).show() { - error!("error showing notification: {:?}", e); - } + std::thread::spawn({ + let track_name = track.to_string(); + let cover_url = track.cover_url(); + move || send_notification(&track_name, cover_url) + }); } } @@ -403,3 +409,31 @@ impl Queue { self.spotify.clone() } } + +pub fn send_notification(track_name: &str, cover_url: Option) { + #[cfg(feature = "cover")] + let res = if let Some(u) = cover_url { + // download album cover image + let path = ui::cover::cache_path_for_url(u.to_string()); + + if !path.exists() { + if let Err(e) = ui::cover::download(u.to_string(), path.clone()) { + error!("Failed to download cover: {}", e); + } + } + + Notification::new() + .summary(track_name) + .icon(&path.to_str().unwrap()) + .show() + } else { + Notification::new().summary(track_name).show() + }; + + #[cfg(not(feature = "cover"))] + let res = Notification::new().summary(track_name).show(); + + if let Err(e) = res { + error!("Failed to send notification cover: {}", e); + } +} diff --git a/src/ui/cover.rs b/src/ui/cover.rs index cfa5330..2a407da 100644 --- a/src/ui/cover.rs +++ b/src/ui/cover.rs @@ -166,8 +166,7 @@ impl CoverView { } fn cache_path(&self, url: String) -> Option { - let mut path = crate::config::cache_path("covers"); - path.push(url.split("/").last().unwrap()); + let path = cache_path_for_url(url.clone()); let mut loading = self.loading.write().unwrap(); if loading.contains(&url) { @@ -291,7 +290,13 @@ impl ViewExt for CoverView { } } -fn download(url: String, path: PathBuf) -> Result<(), std::io::Error> { +pub fn cache_path_for_url(url: String) -> PathBuf { + let mut path = crate::config::cache_path("covers"); + path.push(url.split("/").last().unwrap()); + return path; +} + +pub fn download(url: String, path: PathBuf) -> Result<(), std::io::Error> { let mut resp = reqwest::blocking::get(&url) .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;