From 49f2d40b445a80503c5e883fa625f90bb5441d51 Mon Sep 17 00:00:00 2001 From: George Hafiz Date: Thu, 14 Oct 2021 12:40:54 +0100 Subject: [PATCH] Add `userRating` entity to mpris metadata, with 0 and 1 corresponding to unsaved/not-liked and saved/liked in Spotify, respectively. --- src/main.rs | 5 +++-- src/mpris.rs | 49 +++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 140d927..2d82c77 100644 --- a/src/main.rs +++ b/src/main.rs @@ -186,15 +186,16 @@ async fn main() -> Result<(), String> { let queue = Arc::new(queue::Queue::new(spotify.clone(), cfg.clone())); + let library = Arc::new(Library::new(&event_manager, spotify.clone(), cfg.clone())); + #[cfg(feature = "mpris")] let mpris_manager = Arc::new(mpris::MprisManager::new( event_manager.clone(), spotify.clone(), queue.clone(), + library.clone(), )); - let library = Arc::new(Library::new(&event_manager, spotify.clone(), cfg.clone())); - let mut cmd_manager = CommandManager::new( spotify.clone(), queue.clone(), diff --git a/src/mpris.rs b/src/mpris.rs index 9b826d8..bcfcdb9 100644 --- a/src/mpris.rs +++ b/src/mpris.rs @@ -13,6 +13,7 @@ use log::{debug, warn}; use crate::album::Album; use crate::episode::Episode; use crate::events::EventManager; +use crate::library::Library; use crate::playable::Playable; use crate::playlist::Playlist; use crate::queue::{Queue, RepeatSetting}; @@ -35,7 +36,7 @@ fn get_playbackstatus(spotify: Spotify) -> String { .to_string() } -fn get_metadata(playable: Option, spotify: Spotify) -> Metadata { +fn get_metadata(playable: Option, spotify: Spotify, library: Arc) -> Metadata { let mut hm: Metadata = HashMap::new(); // Fetch full track details in case this playable is based on a SimplifiedTrack @@ -146,6 +147,20 @@ fn get_metadata(playable: Option, spotify: Spotify) -> Metadata { .unwrap_or_default(), )), ); + hm.insert( + "xesam:userRating".to_string(), + Variant(Box::new( + playable + .and_then(|p| p.track()) + .map( + |t| match library.is_saved_track(&Playable::Track(t.clone())) { + true => 1.0, + false => 0.0, + }, + ) + .unwrap_or(0.0) as f64, + )), + ); hm } @@ -154,6 +169,7 @@ fn run_dbus_server( ev: EventManager, spotify: Spotify, queue: Arc, + library: Arc, rx: mpsc::Receiver, ) { let conn = Rc::new( @@ -277,10 +293,15 @@ fn run_dbus_server( let property_metadata = { let spotify = spotify.clone(); let queue = queue.clone(); + let library = library.clone(); f.property::>>, _>("Metadata", ()) .access(Access::Read) .on_get(move |iter, _| { - let hm = get_metadata(queue.clone().get_current(), spotify.clone()); + let hm = get_metadata( + queue.clone().get_current(), + spotify.clone(), + library.clone(), + ); iter.append(hm); Ok(()) @@ -690,7 +711,11 @@ fn run_dbus_server( changed.interface_name = "org.mpris.MediaPlayer2.Player".to_string(); changed.changed_properties.insert( "Metadata".to_string(), - Variant(Box::new(get_metadata(state.1, spotify.clone()))), + Variant(Box::new(get_metadata( + state.1, + spotify.clone(), + library.clone(), + ))), ); changed @@ -710,21 +735,33 @@ pub struct MprisManager { tx: mpsc::Sender, queue: Arc, spotify: Spotify, + library: Arc, } impl MprisManager { - pub fn new(ev: EventManager, spotify: Spotify, queue: Arc) -> Self { + pub fn new( + ev: EventManager, + spotify: Spotify, + queue: Arc, + library: Arc, + ) -> Self { let (tx, rx) = mpsc::channel::(); { let spotify = spotify.clone(); let queue = queue.clone(); + let library = library.clone(); std::thread::spawn(move || { - run_dbus_server(ev, spotify.clone(), queue.clone(), rx); + run_dbus_server(ev, spotify.clone(), queue.clone(), library.clone(), rx); }); } - MprisManager { tx, queue, spotify } + MprisManager { + tx, + queue, + spotify, + library, + } } pub fn update(&self) {