diff --git a/src/mpris/player.rs b/src/mpris/player.rs index 4bb9909..b36c111 100644 --- a/src/mpris/player.rs +++ b/src/mpris/player.rs @@ -1,69 +1,13 @@ -use crate::{model::Song, ui::Window}; +use crate::ui::Window; use adw::prelude::*; use gtk::glib::SendWeakRef; use std::collections::HashMap; use tracing::{event, Level}; use zbus::object_server::{InterfaceRef, SignalEmitter}; -use zbus::zvariant::{ObjectPath, OwnedObjectPath, OwnedValue, Value}; +use zbus::zvariant::{ObjectPath, OwnedValue, Value}; pub const MICROSECONDS: f64 = 1e6; // in a second -#[derive(Default)] -pub struct MetadataMap { - // mpris - track_id: Option, - length: Option, - art_url: Option, - // xesam - album: Option, - //album_artist: Option>, - artist: Option>, - //as_text: Option, - //audio_bpm: Option, - //auto_rating: Option, - //comment: Option>, - //composer: Option>, - content_created: Option, - //disc_number: Option, - //first_used: Option, - genre: Option>, - //last_used: Option, - //lyricist: Option>, - title: Option, - track_number: Option, - //url: Option, - //use_count: Option, - user_rating: Option, -} - -impl MetadataMap { - pub fn from_playbin_song(song: Option<&Song>) -> Self { - song.map(|song| MetadataMap { - // use a unique growing counter to identify tracks - track_id: Some({ - format!("/eu/callcc/audrey/Track/{}", song.counter()) - .try_into() - .unwrap() - }), - length: Some(song.duration() * MICROSECONDS as i64), - art_url: Some( - url::Url::from_file_path(audrey::globals::art_path()) - .unwrap() - .to_string(), - ), - album: Some(song.album()), - artist: Some(vec![song.artist()]), - //content_created: song.year().map(|year| chrono::NaiveDate::from_yo_opt(year, 1).unwrap()), // FIXME: replace this unwrap with Some(Err) -> None - //genre: Some(song.genre.iter().collect()), - title: Some(song.title()), - //track_number: song.track().map(|u| u as i32), - //user_rating: Some(if song.starred().is_none() { 0.0 } else { 1.0 }), - ..Default::default() - }) - .unwrap_or_default() - } -} - pub struct Player { window: SendWeakRef, } @@ -210,56 +154,37 @@ impl Player { #[zbus(property)] fn metadata(&self) -> HashMap<&'static str, OwnedValue> { - let metadata = MetadataMap::from_playbin_song(self.window().song().as_ref()); - let mut map = HashMap::new(); - if let Some(track_id) = &metadata.track_id { + if let Some(song) = self.window().song() { map.insert( "mpris:trackid", - Value::new(track_id.as_ref()).try_into().unwrap(), - ); - } - if let Some(art_url) = &metadata.art_url { - map.insert( - "mpris:artUrl", - Value::new(art_url.to_string()).try_into().unwrap(), - ); - } - if let Some(length) = &metadata.length { - map.insert("mpris:length", Value::new(length).try_into().unwrap()); - } - if let Some(album) = &metadata.album { - map.insert("xesam:album", Value::new(album).try_into().unwrap()); - } - if let Some(artist) = &metadata.artist { - map.insert("xesam:artist", Value::new(artist).try_into().unwrap()); - } - if let Some(content_created) = &metadata.content_created { - map.insert( - "xesam:contentCreated", - Value::new(content_created.format("%+").to_string()) + Value::new(format!("/eu/callcc/audrey/Track/{}", song.counter())) .try_into() .unwrap(), ); - } - if let Some(genre) = &metadata.genre { - map.insert("xesam:genre", Value::new(genre).try_into().unwrap()); - } - if let Some(track_number) = metadata.track_number { map.insert( - "xesam:trackNumber", - Value::new(track_number).try_into().unwrap(), + "mpris:length", + Value::new((self.window().duration() * MICROSECONDS) as i64) + .try_into() + .unwrap(), ); - } - if let Some(title) = &metadata.title { - map.insert("xesam:title", Value::new(title).try_into().unwrap()); - } - if let Some(user_rating) = metadata.user_rating { map.insert( - "xesam:userRating", - Value::new(user_rating).try_into().unwrap(), + "mpris:artUrl", + Value::new( + url::Url::from_file_path(audrey::globals::art_path()) + .unwrap() + .to_string(), + ) + .try_into() + .unwrap(), ); + map.insert("xesam:album", Value::new(song.album()).try_into().unwrap()); + map.insert( + "xesam:artist", + Value::new(vec![song.artist()]).try_into().unwrap(), + ); + map.insert("xesam:title", Value::new(song.title()).try_into().unwrap()); } map