add album cover to notification

This commit is contained in:
nc
2021-07-10 21:12:08 -04:00
committed by Henrik Friedrichsen
parent 9eb99cc8be
commit 2edc22f251
2 changed files with 46 additions and 7 deletions

View File

@@ -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<String>) {
#[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);
}
}

View File

@@ -166,8 +166,7 @@ impl CoverView {
}
fn cache_path(&self, url: String) -> Option<PathBuf> {
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))?;