Compare commits

...

2 commits

Author SHA1 Message Date
0aa7984092 fold metadata mappe 2024-11-21 22:19:23 +01:00
32c349fba9 refactor 1 2024-11-21 22:07:44 +01:00

View file

@ -1,123 +1,13 @@
use crate::{model::Song, ui::Window}; use crate::ui::Window;
use adw::prelude::*; use adw::prelude::*;
use gtk::glib::SendWeakRef; use gtk::glib::SendWeakRef;
use std::collections::HashMap; use std::collections::HashMap;
use tracing::{event, Level}; use tracing::{event, Level};
use zbus::object_server::{InterfaceRef, SignalEmitter}; 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 pub const MICROSECONDS: f64 = 1e6; // in a second
#[derive(Default)]
pub struct MetadataMap {
// mpris
track_id: Option<OwnedObjectPath>,
length: Option<i64>,
art_url: Option<String>,
// xesam
album: Option<String>,
//album_artist: Option<Vec<String>>,
artist: Option<Vec<String>>,
//as_text: Option<String>,
//audio_bpm: Option<i32>,
//auto_rating: Option<f32>,
//comment: Option<Vec<String>>,
//composer: Option<Vec<String>>,
content_created: Option<chrono::NaiveDateTime>,
//disc_number: Option<i32>,
//first_used: Option<chrono::DateTime>,
genre: Option<Vec<String>>,
//last_used: Option<chrono::DateTime>,
//lyricist: Option<Vec<String>>,
title: Option<String>,
track_number: Option<i32>,
//url: Option<String>,
//use_count: Option<String>,
user_rating: Option<f32>,
}
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()
}
fn as_hash_map(&self) -> HashMap<&'static str, OwnedValue> {
let mut map = HashMap::new();
if let Some(track_id) = &self.track_id {
map.insert(
"mpris:trackid",
Value::new(track_id.as_ref()).try_into().unwrap(),
);
}
if let Some(art_url) = &self.art_url {
map.insert(
"mpris:artUrl",
Value::new(art_url.to_string()).try_into().unwrap(),
);
}
if let Some(length) = &self.length {
map.insert("mpris:length", Value::new(length).try_into().unwrap());
}
if let Some(album) = &self.album {
map.insert("xesam:album", Value::new(album).try_into().unwrap());
}
if let Some(artist) = &self.artist {
map.insert("xesam:artist", Value::new(artist).try_into().unwrap());
}
if let Some(content_created) = &self.content_created {
map.insert(
"xesam:contentCreated",
Value::new(content_created.format("%+").to_string())
.try_into()
.unwrap(),
);
}
if let Some(genre) = &self.genre {
map.insert("xesam:genre", Value::new(genre).try_into().unwrap());
}
if let Some(track_number) = self.track_number {
map.insert(
"xesam:trackNumber",
Value::new(track_number).try_into().unwrap(),
);
}
if let Some(title) = &self.title {
map.insert("xesam:title", Value::new(title).try_into().unwrap());
}
if let Some(user_rating) = self.user_rating {
map.insert(
"xesam:userRating",
Value::new(user_rating).try_into().unwrap(),
);
}
map
}
}
pub struct Player { pub struct Player {
window: SendWeakRef<Window>, window: SendWeakRef<Window>,
} }
@ -264,8 +154,40 @@ impl Player {
#[zbus(property)] #[zbus(property)]
fn metadata(&self) -> HashMap<&'static str, OwnedValue> { fn metadata(&self) -> HashMap<&'static str, OwnedValue> {
// TODO: no need to MetadataMap wrapper anymore let mut map = HashMap::new();
MetadataMap::from_playbin_song(self.window().song().as_ref()).as_hash_map()
if let Some(song) = self.window().song() {
map.insert(
"mpris:trackid",
Value::new(format!("/eu/callcc/audrey/Track/{}", song.counter()))
.try_into()
.unwrap(),
);
map.insert(
"mpris:length",
Value::new((self.window().duration() * MICROSECONDS) as i64)
.try_into()
.unwrap(),
);
map.insert(
"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
} }
#[zbus(property)] #[zbus(property)]