fold metadata mappe

This commit is contained in:
Erica Z 2024-11-21 22:19:23 +01:00
parent 32c349fba9
commit 0aa7984092

View file

@ -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<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()
}
}
pub struct Player {
window: SendWeakRef<Window>,
}
@ -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