4 Commits

Author SHA1 Message Date
f8281640f1 Update to rust 2024 2025-12-07 20:11:38 +02:00
a21295ecc8 Major update
too lazy to describe
2025-12-07 19:46:47 +02:00
67a948bb66 Other crap i added months ago 2025-03-11 21:35:12 +02:00
ad89b0c64d Fix hard crash on fresh start, add some more context to errors 2025-03-11 21:34:57 +02:00
36 changed files with 2377 additions and 1551 deletions

11
Cargo.lock generated
View File

@@ -1387,6 +1387,12 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2" checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2"
[[package]]
name = "downcast-rs"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "117240f60069e65410b3ae1bb213295bd828f707b5bec6596a1afc8793ce0cbc"
[[package]] [[package]]
name = "ecolor" name = "ecolor"
version = "0.27.2" version = "0.27.2"
@@ -4933,7 +4939,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "056535ced7a150d45159d3a8dc30f91a2e2d588ca0b23f70e56033622b8016f6" checksum = "056535ced7a150d45159d3a8dc30f91a2e2d588ca0b23f70e56033622b8016f6"
dependencies = [ dependencies = [
"cc", "cc",
"downcast-rs", "downcast-rs 1.2.1",
"rustix 0.38.39", "rustix 0.38.39",
"scoped-tls", "scoped-tls",
"smallvec", "smallvec",
@@ -5792,12 +5798,14 @@ dependencies = [
"anyhow", "anyhow",
"camino", "camino",
"dirs", "dirs",
"downcast-rs 2.0.2",
"eframe", "eframe",
"egui", "egui",
"egui_extras", "egui_extras",
"lazy_static", "lazy_static",
"log", "log",
"rfd", "rfd",
"url",
"uuid", "uuid",
"xmpd-cache", "xmpd-cache",
"xmpd-cliargs", "xmpd-cliargs",
@@ -5840,6 +5848,7 @@ dependencies = [
"lazy_static", "lazy_static",
"serde", "serde",
"toml 0.8.19", "toml 0.8.19",
"xmpd-cliargs",
] ]
[[package]] [[package]]

View File

@@ -14,6 +14,7 @@ members=[
[workspace.package] [workspace.package]
version="2.0.0" version="2.0.0"
edition="2024"
repository="https://git.mcorangehq.xyz/XOR64/xmpd/" repository="https://git.mcorangehq.xyz/XOR64/xmpd/"
license="GPL-3.0" license="GPL-3.0"
authors=[ authors=[
@@ -48,3 +49,4 @@ toml = "0.8.19"
rfd = "0.15.1" rfd = "0.15.1"
rodio = { version = "0.20.1", features = ["symphonia-all"] } rodio = { version = "0.20.1", features = ["symphonia-all"] }
image = "0.25.5" image = "0.25.5"
downcast-rs = "2.0.2"

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "xmpd-cache" name = "xmpd-cache"
edition = "2021" edition.workspace = true
readme="README.md" readme="README.md"
authors.workspace = true authors.workspace = true
version.workspace = true version.workspace = true

View File

@@ -30,11 +30,12 @@ impl IconCacheDl {
pub fn download(&mut self, sid: uuid::Uuid, song: Song) -> crate::Result<()> { pub fn download(&mut self, sid: uuid::Uuid, song: Song) -> crate::Result<()> {
match song.icon_type().clone() { match song.icon_type().clone() {
IconType::FromSource => { IconType::FromSource => {
let tooling = xmpd_settings::Settings::get()?.tooling.clone(); let settings = xmpd_settings::Settings::get()?.clone();
let tooling = settings.tooling.clone();
match song.source_type() { match song.source_type() {
SourceType::Youtube => { SourceType::Youtube => {
self.jobs.insert(sid.clone(), DlStatus::Downloading); self.jobs.insert(sid.clone(), DlStatus::Downloading);
let mut path = xmpd_cliargs::CLIARGS.cache_path(); let mut path = settings.cache_settings.cache_path.clone();
path.push("icons"); path.push("icons");
path.push(sid.to_string()); path.push(sid.to_string());
@@ -91,7 +92,7 @@ impl IconCacheDl {
anyhow::bail!("Url without extension, cant continue"); anyhow::bail!("Url without extension, cant continue");
}; };
let ext = ext.to_string_lossy().to_string(); let ext = ext.to_string_lossy().to_string();
let mut path = xmpd_cliargs::CLIARGS.cache_path(); let mut path = xmpd_settings::Settings::get()?.clone().cache_settings.cache_path;
path.push("icons"); path.push("icons");
path.push(sid.to_string()); path.push(sid.to_string());
path.set_extension(ext); path.set_extension(ext);

View File

@@ -1,4 +1,5 @@
use std::{collections::HashMap, ffi::OsStr, process::{Command, Stdio}, sync::{Arc, Mutex, MutexGuard}}; use std::{collections::HashMap, ffi::OsStr, process::{Command, Stdio}, sync::{Arc, Mutex, MutexGuard}};
use camino::{Utf8Path, Utf8PathBuf};
use xmpd_manifest::song::{Song, SourceType}; use xmpd_manifest::song::{Song, SourceType};
@@ -10,9 +11,16 @@ lazy_static::lazy_static!(
pub enum SongStatus { pub enum SongStatus {
Downloading, Downloading,
Converting, Converting,
Failed(String),
Done Done
} }
#[cfg(target_family = "windows")]
const PATH_SEP: char = ';';
#[cfg(target_family = "unix")]
const PATH_SEP: char = ':';
#[derive(Debug, Default, Clone)] #[derive(Debug, Default, Clone)]
pub struct SongCacheDl { pub struct SongCacheDl {
pub jobs: HashMap<uuid::Uuid, SongStatus>, pub jobs: HashMap<uuid::Uuid, SongStatus>,
@@ -30,11 +38,14 @@ impl SongCacheDl {
self.current_jobs >= 5 self.current_jobs >= 5
} }
pub fn download(&mut self, sid: uuid::Uuid, song: Song) -> crate::Result<()> { pub fn download(&mut self, sid: uuid::Uuid, song: Song) -> crate::Result<()> {
self.current_jobs += 1; self.current_jobs += 1;
let song_format = xmpd_settings::Settings::get().unwrap().tooling.song_format.clone(); let song_format = xmpd_settings::Settings::get().unwrap().tooling.song_format.clone();
let tooling = xmpd_settings::Settings::get()?.tooling.clone(); let settings = xmpd_settings::Settings::get()?.clone();
let mut song_cache_d = xmpd_cliargs::CLIARGS.cache_path(); let tooling = settings.tooling.clone();
let mut song_cache_d = settings.cache_settings.cache_path.clone();
song_cache_d.push("songs"); song_cache_d.push("songs");
match song.source_type() { match song.source_type() {
SourceType::Youtube | SourceType::Youtube |
@@ -59,16 +70,27 @@ impl SongCacheDl {
let dl_child = dl_cmd.spawn()?; let dl_child = dl_cmd.spawn()?;
self.jobs.insert(sid, SongStatus::Downloading); self.jobs.insert(sid, SongStatus::Downloading);
std::thread::spawn(move || { std::thread::spawn(move || {
let mut err_line = String::new();
if let Ok(output) = dl_child.wait_with_output() { if let Ok(output) = dl_child.wait_with_output() {
for line in String::from_utf8(output.stdout).unwrap().lines() { for line in String::from_utf8(output.stdout).unwrap().lines() {
if line.contains("ERROR") {
err_line = line.to_string();
}
log::info!("CMD: {}", line); log::info!("CMD: {}", line);
} }
for line in String::from_utf8(output.stderr).unwrap().lines() { for line in String::from_utf8(output.stderr).unwrap().lines() {
if line.contains("ERROR") {
err_line = line.to_string();
}
log::error!("CMD: {}", line); log::error!("CMD: {}", line);
} }
} }
let mut cache = SONG_CACHE_DL.lock().unwrap(); let mut cache = SONG_CACHE_DL.lock().unwrap();
cache.jobs.insert(sid, SongStatus::Done); if song_p.exists() {
cache.jobs.insert(sid, SongStatus::Done);
} else {
cache.jobs.insert(sid, SongStatus::Failed(err_line));
}
cache.current_jobs -= 1; cache.current_jobs -= 1;
}); });
} }
@@ -116,9 +138,12 @@ impl SongCacheDl {
log::debug!("from: {from:?} to: {to:?}"); log::debug!("from: {from:?} to: {to:?}");
std::fs::copy(&from, &to).unwrap(); std::fs::copy(&from, &to).unwrap();
from.pop(); from.pop();
std::fs::remove_dir_all(from).unwrap();
let mut cache = SONG_CACHE_DL.lock().unwrap(); let mut cache = SONG_CACHE_DL.lock().unwrap();
cache.jobs.insert(sid, SongStatus::Done); if let Err(_) = std::fs::remove_dir_all(from) {
cache.jobs.insert(sid, SongStatus::Failed(String::from("Unknown")));
} else {
cache.jobs.insert(sid, SongStatus::Done);
}
cache.current_jobs -= 1; cache.current_jobs -= 1;
}); });
} }

View File

@@ -1,4 +1,6 @@
use std::{collections::HashMap, path::PathBuf, str::FromStr, sync::{mpsc::{self, Receiver, Sender}, Arc, Mutex, MutexGuard}, time::Duration}; use std::{collections::HashMap, path::PathBuf, str::FromStr, sync::{mpsc::{self, Receiver, Sender}, Arc, Mutex, MutexGuard}, time::Duration};
use anyhow::anyhow;
use camino::{Utf8Path, Utf8PathBuf};
use downloader::song::SongStatus; use downloader::song::SongStatus;
use xmpd_manifest::song::Song; use xmpd_manifest::song::Song;
@@ -41,18 +43,40 @@ impl Cache {
Err(e) => Err(anyhow::anyhow!(format!("{e:?}"))), Err(e) => Err(anyhow::anyhow!(format!("{e:?}"))),
} }
} }
fn check_if_tool_exists(&self, tool_path: &Utf8Path) -> crate::Result<()> {
if std::fs::metadata(tool_path).is_ok() {
return Ok(());
}
if let Ok(path) = std::env::var("PATH") {
for p in path.split(":") {
let p_str = Utf8PathBuf::from(p).join(tool_path);
if std::fs::metadata(p_str).is_ok() {
return Ok(());
}
}
}
anyhow::bail!("Tool {} was not found", tool_path)
}
pub fn init(&mut self) -> Result<Receiver<Message>> { pub fn init(&mut self) -> Result<Receiver<Message>> {
// Check for missing tooling
let tooling = xmpd_settings::Settings::get()?.tooling.clone();
self.check_if_tool_exists(&tooling.ytdlp_path)?;
self.check_if_tool_exists(&tooling.spotdl_path)?;
self.check_if_tool_exists(&tooling.ffmpeg_path)?;
let (internal_tx, cache_rx) = mpsc::channel::<Message>(); let (internal_tx, cache_rx) = mpsc::channel::<Message>();
// let (internal_rx, cache_tx) = mpsc::channel::<Message>(); // let (internal_rx, cache_tx) = mpsc::channel::<Message>();
start_cache_mv_thread(internal_tx); start_cache_mv_thread(internal_tx);
self.cache_dir = xmpd_cliargs::CLIARGS.cache_path(); self.cache_dir = xmpd_settings::Settings::get()?.cache_settings.cache_path.clone();
std::fs::create_dir_all(&self.cache_dir)?;
{ // Get cached songs { // Get cached songs
let mut song_cache_dir = self.cache_dir.clone(); let mut song_cache_dir = self.cache_dir.clone();
std::fs::create_dir_all(&song_cache_dir)?;
song_cache_dir.push("songs"); song_cache_dir.push("songs");
for file in song_cache_dir.read_dir_utf8()? { std::fs::create_dir_all(&song_cache_dir)?;
for file in song_cache_dir.read_dir_utf8().map_err(|e| anyhow!("failed to read cache dir: {e}"))? {
if let Ok(file) = file { if let Ok(file) = file {
if !file.file_type()?.is_file() { if !file.file_type()?.is_file() {
log::warn!("Non song file in: {}", file.path()); log::warn!("Non song file in: {}", file.path());
@@ -78,8 +102,15 @@ impl Cache {
} }
pub fn download_song_to_cache(&mut self, sid: uuid::Uuid, song: Song) { pub fn download_song_to_cache(&mut self, sid: uuid::Uuid, song: Song) {
self.song_queue.push((sid, song)); let song_format = xmpd_settings::Settings::get().unwrap().tooling.song_format.clone();
self.song_cache.insert(sid, DlStatus::Downloading); let mut p = self.cache_dir.clone();
p.push("songs");
p.push(format!("{sid}.{song_format}"));
if !p.exists() {
log::info!("p: {p:?}");
self.song_queue.push((sid, song));
self.song_cache.insert(sid, DlStatus::Downloading);
}
} }
pub fn download_icon_to_cache(&mut self, sid: uuid::Uuid, song: Song) { pub fn download_icon_to_cache(&mut self, sid: uuid::Uuid, song: Song) {
self.icon_queue.push((sid, song)); self.icon_queue.push((sid, song));
@@ -97,7 +128,7 @@ impl Cache {
} }
macro_rules! he { macro_rules! he {
($tx:expr, $val:expr) => { ($tx:expr_2021, $val:expr_2021) => {
match $val { match $val {
Ok(v) => v, Ok(v) => v,
Err(e) => { Err(e) => {
@@ -119,7 +150,7 @@ fn start_cache_mv_thread(tx: Sender<Message>) {
for (sid, status) in &dlc.jobs { for (sid, status) in &dlc.jobs {
if *status == SongStatus::Done { if *status == SongStatus::Done {
let mut cache = he!(tx, CACHE.lock()); let mut cache = he!(tx, CACHE.lock());
let mut song_p = xmpd_cliargs::CLIARGS.cache_path().clone(); let mut song_p = he!(tx, xmpd_settings::Settings::get()).cache_settings.cache_path.clone();
song_p.push("songs"); song_p.push("songs");
song_p.push(sid.clone().to_string()); song_p.push(sid.clone().to_string());
let song_p = song_p.with_extension(&song_format); let song_p = song_p.with_extension(&song_format);
@@ -128,6 +159,11 @@ fn start_cache_mv_thread(tx: Sender<Message>) {
cache.song_cache.insert(sid.clone(), DlStatus::Done(Some(song_p.into()))); cache.song_cache.insert(sid.clone(), DlStatus::Done(Some(song_p.into())));
done_jobs.push(sid.clone()); done_jobs.push(sid.clone());
} }
} else if let SongStatus::Failed(e) = status {
let mut cache = he!(tx, CACHE.lock());
let _ = tx.send(Message::Error(std::file!(), std::line!() as usize, format!("Failed to download song {sid}: {e}")));
cache.song_cache.insert(sid.clone(), DlStatus::Error(std::file!(), std::line!() as usize, format!("Failed to download song {sid}: {e}")));
done_jobs.push(sid.clone());
} }
} }
for sid in done_jobs { for sid in done_jobs {

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "xmpd-cliargs" name = "xmpd-cliargs"
edition = "2021" edition.workspace = true
readme="README.md" readme="README.md"
version.workspace = true version.workspace = true
authors.workspace = true authors.workspace = true

View File

@@ -10,72 +10,28 @@ lazy_static::lazy_static!(
#[derive(Debug, clap::Parser)] #[derive(Debug, clap::Parser)]
pub struct CliArgs { pub struct CliArgs {
/// Manifest path /// Manifest path
#[arg(long, short, default_value_t=get_default_manifest_path())] #[arg(long, short)]
manifest: camino::Utf8PathBuf, manifest: Option<camino::Utf8PathBuf>,
/// settings file path /// settings file path
#[arg(long, short, default_value_t=get_default_settings_path())] #[arg(long, short, default_value="./settings.toml")]
settings: camino::Utf8PathBuf, settings: camino::Utf8PathBuf,
/// Cache dir path /// Cache dir path
#[arg(long, short, default_value_t=get_default_cache_path())] #[arg(long, short)]
cache: camino::Utf8PathBuf, cache: Option<camino::Utf8PathBuf>,
/// Debug mode /// Debug mode
#[arg(long, short)] #[arg(long, short)]
pub debug: bool, pub debug: bool,
} }
impl CliArgs { impl CliArgs {
pub fn manifest_path(&self) -> PathBuf { pub fn manifest_path(&self) -> Option<Utf8PathBuf> {
self.manifest.clone().into_std_path_buf() self.manifest.clone()
} }
pub fn settings_path(&self) -> PathBuf { pub fn settings_path(&self) -> Utf8PathBuf {
self.settings.clone().into_std_path_buf() self.settings.clone()
} }
pub fn cache_path(&self) -> Utf8PathBuf { pub fn cache_path(&self) -> Option<Utf8PathBuf> {
self.cache.clone() self.cache.clone()
} }
} }
#[allow(irrefutable_let_patterns)] // Broken?
fn get_default_settings_path() -> camino::Utf8PathBuf {
if let Ok(p) = std::env::var("XMPD_SETTINGS_PATH") {
if let Ok(p) = camino::Utf8PathBuf::from_str(&p) {
return p;
}
}
if let Some(mut p) = dirs::config_dir() {
p.push("xmpd");
p.push("config.toml");
return camino::Utf8PathBuf::from_path_buf(p).expect("Invalid os path");
}
unreachable!()
}
#[allow(irrefutable_let_patterns)] // Broken?
fn get_default_manifest_path() -> camino::Utf8PathBuf {
if let Ok(p) = std::env::var("XMPD_MANIFEST_PATH") {
if let Ok(p) = camino::Utf8PathBuf::from_str(&p) {
return p;
}
}
if let Some(mut p) = dirs::config_dir() {
p.push("xmpd");
p.push("manifest.json");
return camino::Utf8PathBuf::from_path_buf(p).expect("Invalid os path");
}
unreachable!()
}
#[allow(irrefutable_let_patterns)] // Broken?
fn get_default_cache_path() -> camino::Utf8PathBuf {
if let Ok(p) = std::env::var("XMPD_CACHE_PATH") {
if let Ok(p) = camino::Utf8PathBuf::from_str(&p) {
return p;
}
}
if let Some(mut p) = dirs::cache_dir() {
p.push("xmpd");
return camino::Utf8PathBuf::from_path_buf(p).expect("Invalid os path");
}
unreachable!()
}

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "xmpd-core" name = "xmpd-core"
edition = "2021" edition.workspace = true
readme="README.md" readme="README.md"
version.workspace = true version.workspace = true
authors.workspace = true authors.workspace = true

View File

@@ -11,7 +11,10 @@ fn main() -> Result<()> {
let cliargs = &xmpd_cliargs::CLIARGS; let cliargs = &xmpd_cliargs::CLIARGS;
logger::init(&cliargs); logger::init(&cliargs);
log::debug!("Initialising settings"); log::debug!("Initialising settings");
xmpd_settings::Settings::get()?.load(Some(cliargs.settings_path()))?; {
xmpd_settings::Settings::get()?.load(Some(cliargs.settings_path().into_std_path_buf()))?;
xmpd_settings::Settings::get()?.load_cli_args(cliargs);
}
log::debug!("Starting gui"); log::debug!("Starting gui");
xmpd_gui::start()?; xmpd_gui::start()?;
Ok(()) Ok(())

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "xmpd-gui" name = "xmpd-gui"
edition = "2021" edition.workspace = true
readme="README.md" readme="README.md"
authors.workspace = true authors.workspace = true
version.workspace = true version.workspace = true
@@ -33,3 +33,5 @@ uuid.workspace = true
camino.workspace = true camino.workspace = true
rfd.workspace = true rfd.workspace = true
dirs.workspace = true dirs.workspace = true
downcast-rs.workspace = true
url.workspace = true

View File

@@ -1,5 +1,5 @@
use egui::{CursorIcon, RichText, Sense, TextBuffer}; use egui::{CursorIcon, RichText, Sense, TextBuffer};
use xmpd_manifest::store::BaseStore; use xmpd_manifest::store::{BaseStore, StoreExtras};
use crate::utils::SearchType; use crate::utils::SearchType;
use super::{CompGetter, CompUi}; use super::{CompGetter, CompUi};
@@ -23,12 +23,7 @@ impl CompUi for LeftNav {
ui.vertical(|ui| { ui.vertical(|ui| {
let len = state.manifest.store().get_songs().len(); let len = state.manifest.store().get_songs().len();
add_playlist_tab(ui, &None, "All Songs", None, len, w); add_playlist_tab(ui, &None, "All Songs", None, len, w);
let mut playlists: Vec<_> = state.manifest.store().get_playlists().into_iter().collect(); let playlists = state.manifest.store().get_playlists_sorted();
playlists.sort_by(|a, b| {
let a = a.1.name().to_lowercase();
let b = b.1.name().to_lowercase();
a.cmp(&b)
});
let search_text = handle_error_ui!(header::Header::get()).search_text.clone(); let search_text = handle_error_ui!(header::Header::get()).search_text.clone();
let (qtyp, qtxt) = crate::utils::SearchType::from_str(&search_text); let (qtyp, qtxt) = crate::utils::SearchType::from_str(&search_text);
for (pid, playlist) in playlists.iter() { for (pid, playlist) in playlists.iter() {

View File

@@ -1,9 +1,11 @@
use anyhow::anyhow;
use egui::{Color32, CursorIcon, ImageSource, RichText, Sense, Vec2}; use egui::{Color32, CursorIcon, ImageSource, RichText, Sense, Vec2};
use xmpd_cache::DlStatus; use xmpd_cache::DlStatus;
use xmpd_manifest::{query, song::Song, store::BaseStore}; use xmpd_manifest::{query, song::Song, store::{BaseStore, StoreExtras}};
use crate::utils::SearchType; use crate::{components::toast::{Toast, ToastType}, utils::SearchType, windows::WindowId};
use std::any::Any;
use super::{CompGetter, CompUi}; use super::{CompGetter, CompUi};
use downcast_rs::Downcast;
pub mod header; pub mod header;
@@ -24,7 +26,7 @@ impl CompUi for SongList {
sl.playable_songs = Self::get_playable_songs(&songs)?; sl.playable_songs = Self::get_playable_songs(&songs)?;
if let Some((sid, _)) = songs.first() { if let Some((sid, _)) = songs.first() {
if sl.selected_sid == Default::default() { if sl.selected_sid == Default::default() {
sl.selected_sid = sid.clone(); sl.selected_sid = (*sid).clone();
} }
} }
} }
@@ -34,8 +36,8 @@ impl CompUi for SongList {
.show(ui, |ui| { .show(ui, |ui| {
ui.vertical(|ui| { ui.vertical(|ui| {
ui.add_space(3.0); ui.add_space(3.0);
for (sid, song) in disp_songs { for sid in disp_songs {
handle_error_ui!(Self::display_song_tab(ui, state, &sid, &song)); handle_error_ui!(Self::display_song_tab(ui, state, &sid));
} }
}); });
}); });
@@ -44,40 +46,19 @@ impl CompUi for SongList {
} }
impl SongList { impl SongList {
fn get_and_sort_songs(state: &mut crate::GuiState) -> crate::Result<Vec<(uuid::Uuid, Song)>> { fn get_and_sort_songs(state: &mut crate::GuiState) -> crate::Result<Vec<(&uuid::Uuid, &Song)>> {
let pid = super::left_nav::LeftNav::get()?.selected_playlist_id.clone(); let pid = super::left_nav::LeftNav::get()?.selected_playlist_id.clone();
match pid { match pid {
None => { None => {
let songs = state.manifest.store().get_songs().clone().into_iter(); Ok(state.manifest.store().get_songs_sorted())
let mut songs: Vec<_> = songs.collect();
songs.sort_by(|a, b| {
let a = a.1.name().to_lowercase();
let b = b.1.name().to_lowercase();
a.cmp(&b)
});
Ok(songs)
} }
Some(pid) => { Some(pid) => {
let Some(playlist) = state.manifest.store().get_playlist(&pid) else { Ok(state.manifest.store().get_playlist_songs_sorted(pid).expect("Invalid pid"))
anyhow::bail!("Couldnt find playlist (corruption?)");
};
let mut songs = Vec::new();
for sid in playlist.songs() {
if let Some(song) = state.manifest.store().get_song(&sid) {
songs.push((sid.clone(), song.clone()));
}
}
songs.sort_by(|a, b| {
let a = a.1.name().to_lowercase();
let b = b.1.name().to_lowercase();
a.cmp(&b)
});
Ok(songs)
} }
} }
} }
fn get_songs_to_display(songs: &Vec<(uuid::Uuid, Song)>) -> crate::Result<Vec<(uuid::Uuid, Song)>>{ fn get_songs_to_display<'a>(songs: &'a [(&uuid::Uuid, &Song)]) -> crate::Result<Vec<uuid::Uuid>>{
let mut to_display = Vec::new(); let mut to_display = Vec::new();
let query = {header::Header::get()?.search_text.clone()}; let query = {header::Header::get()?.search_text.clone()};
let (query_type, query_text) = crate::utils::SearchType::from_str(&query); let (query_type, query_text) = crate::utils::SearchType::from_str(&query);
@@ -105,15 +86,16 @@ impl SongList {
}; };
if should_display { if should_display {
to_display.push((sid.clone(), song.clone())); to_display.push((*sid).clone());
} }
} }
Ok(to_display) Ok(to_display)
} }
fn display_song_tab(ui: &mut egui::Ui, state: &mut crate::GuiState, sid: &uuid::Uuid, song: &Song) -> crate::Result<()> { fn display_song_tab(ui: &mut egui::Ui, state: &mut crate::GuiState, sid: &uuid::Uuid) -> crate::Result<()> {
let mut clicked = false; let mut clicked = false;
ui.horizontal(|ui| { ui.horizontal(|ui| {
let song = handle_option!("(internal)", state.manifest.store().get_song(sid)).clone();
let theme = handle_error_ui!(xmpd_settings::Settings::get()).theme.clone(); let theme = handle_error_ui!(xmpd_settings::Settings::get()).theme.clone();
// let icon_status = handle_error_ui!(xmpd_cache::Cache::get()).get_cached_icon_status(&sid).clone(); // let icon_status = handle_error_ui!(xmpd_cache::Cache::get()).get_cached_icon_status(&sid).clone();
let img = ui.add( let img = ui.add(
@@ -171,7 +153,7 @@ impl SongList {
} }
} }
// label.context_menu(|ui| handle_error_ui!(Self::show_context_menu(ui, sid, song))); label.context_menu(|ui| handle_error_ui!(Self::show_context_menu(state, ui, sid, &song)));
ui.monospace( ui.monospace(
RichText::new(format!("By {}", song.author())) RichText::new(format!("By {}", song.author()))
.color(theme.dim_text_color) .color(theme.dim_text_color)
@@ -185,14 +167,14 @@ impl SongList {
match status { match status {
Some(DlStatus::Done(_)) => { Some(DlStatus::Done(_)) => {
//let img = egui::Image::new(crate::data::CHECK_ICON) let img = egui::Image::new(crate::data::CHECK_ICON)
// .tint(Color32::LIGHT_GREEN) .tint(Color32::LIGHT_GREEN)
// .sense(Sense::hover()) .sense(Sense::hover())
// .fit_to_exact_size(Vec2::new(16.0, 16.0)); .fit_to_exact_size(Vec2::new(16.0, 16.0));
//ui.add(img).on_hover_ui(|ui| { ui.add(img).on_hover_ui(|ui| {
// ui.label(format!("Path: {p}")); ui.label(format!("Id: {sid}"));
//}); });
} }
Some(DlStatus::Downloading) => { Some(DlStatus::Downloading) => {
let spinner = egui::Spinner::new() let spinner = egui::Spinner::new()
@@ -200,7 +182,7 @@ impl SongList {
.size(16.0); .size(16.0);
ui.add(spinner); ui.add(spinner);
} }
Some(DlStatus::Error(e, ..)) => { Some(DlStatus::Error(_, _, e)) => {
let img = egui::Image::new(crate::data::WARN_ICON) let img = egui::Image::new(crate::data::WARN_ICON)
.tint(Color32::LIGHT_YELLOW) .tint(Color32::LIGHT_YELLOW)
.sense(Sense::hover()) .sense(Sense::hover())
@@ -274,20 +256,46 @@ impl SongList {
self.play_song(next, state)?; self.play_song(next, state)?;
Ok(()) Ok(())
} }
fn get_playable_songs(songs: &Vec<(uuid::Uuid, Song)>) -> crate::Result<Vec<uuid::Uuid>> { fn get_playable_songs(songs: &[(&uuid::Uuid, &Song)]) -> crate::Result<Vec<uuid::Uuid>> {
let mut playable_songs = Vec::new(); let mut playable_songs = Vec::new();
for (sid, _) in songs { for (sid, _) in songs {
if let Some(DlStatus::Done(_)) = xmpd_cache::Cache::get()?.get_cached_song_status(&sid) { if let Some(DlStatus::Done(_)) = xmpd_cache::Cache::get()?.get_cached_song_status(&sid) {
playable_songs.push(sid.clone()); playable_songs.push((*sid).clone());
} }
} }
Ok(playable_songs) Ok(playable_songs)
} }
//fn show_context_menu(ui: &mut egui::Ui, sid: &uuid::Uuid, song: &Song) -> crate::Result<()> { fn show_context_menu(state: &mut crate::GuiState, ui: &mut egui::Ui, sid: &uuid::Uuid, song: &Song) -> crate::Result<()> {
// if ui.button("Download icon").clicked() { if ui.button("Edit").clicked() {
// xmpd_cache::Cache::get()?.download_icon_to_cache(sid.clone(), song.clone()); // TODO: Implement song editing
// } Toast::get().unwrap().show_toast("Not Implemented", "Song editing is not implemented", ToastType::Error);
// Ok(()) //state.windows.toggle(&crate::windows::WindowId::NewSong, status);
//} ui.close_menu();
}
if ui.button("Add to playlist").clicked() {
// TODO: Implement song editing
Toast::get().unwrap().show_toast("Not Implemented", "Adding songs to another playlist is not implemented, go to that playlist and press add song", ToastType::Error);
//state.windows.toggle(&WindowId::AddSongToPl, true);
//let mut windows = crate::windows::WINDOWS.lock().map_err(|e| anyhow!("{e}"))?;
//let mut w = windows.get_mut(&WindowId::AddSongToPl);
//let w = w.as_any_mut();
ui.close_menu();
}
if ui.button("Songs by artist").clicked() {
crate::components::song_list::header::Header::get()?.search_text = format!("author:{}", song.author());
ui.close_menu();
}
if ui.button(RichText::new("Remove from playlist").color(Color32::RED)).clicked() {
Toast::get().unwrap().show_toast("Not Implemented", "Removing songs from playlists is not implemented", ToastType::Error);
ui.close_menu();
}
if ui.button(RichText::new("Remove song globally").color(Color32::RED)).clicked() {
Toast::get().unwrap().show_toast("Not Implemented", "Removing songs globally is not implemented", ToastType::Error);
ui.close_menu();
}
Ok(())
}
} }

View File

@@ -1,6 +1,6 @@
use std::path::PathBuf; use std::path::PathBuf;
use egui::TextBuffer; use egui::{Layout, TextBuffer};
use xmpd_manifest::store::{JsonStore, TomlStore}; use xmpd_manifest::store::{JsonStore, TomlStore};
use crate::windows::WindowId; use crate::windows::WindowId;
@@ -28,6 +28,9 @@ impl CompUi for TopNav {
} }
}); });
ui.menu_button("Manifest", |ui| { ui.menu_button("Manifest", |ui| {
if ui.button("Add New Song").clicked() {
state.windows.toggle(&WindowId::NewSong, true);
}
if ui.button("Save").clicked() { if ui.button("Save").clicked() {
handle_error_ui!(state.manifest.save()); handle_error_ui!(state.manifest.save());
ui.close_menu(); ui.close_menu();
@@ -65,7 +68,10 @@ impl CompUi for TopNav {
} }
}); });
#[cfg(debug_assertions)]
ui.with_layout(Layout::right_to_left(egui::Align::Max), |ui| {
ui.label(format!("ft: {} ms", state.debug_info.last_frame_time.as_millis()));
});
}); });
}); });
let mut used = false; let mut used = false;

View File

@@ -1,6 +1,6 @@
#![feature(async_closure)] use std::path::Path;
use std::time::{Duration, Instant};
use std::time::Duration; use anyhow::anyhow;
use xmpd_manifest::{store::JsonStore, Manifest}; use xmpd_manifest::{store::JsonStore, Manifest};
#[macro_use] #[macro_use]
@@ -16,14 +16,28 @@ const W_NAME: &str = "xmpd v2.0.0a";
type Result<T> = anyhow::Result<T>; type Result<T> = anyhow::Result<T>;
pub fn start() -> Result<()> { pub fn start() -> Result<()> {
let cache_rx = xmpd_cache::Cache::get()?.init()?; let manifest_p = xmpd_settings::Settings::get()?.cache_settings.manifest_path.clone().into_std_path_buf();
let cache_rx = xmpd_cache::Cache::get()
.map_err(|e| anyhow!("Failed to get cache: {e}"))?
.init()
.map_err(|e| anyhow!("Failed to init cache: {e}"))?;
let options = eframe::NativeOptions::default(); let options = eframe::NativeOptions::default();
let mut state = GuiState::new()?; let mut state = GuiState::new(&manifest_p)?;
let res = eframe::run_simple_native(W_NAME, options, move |ctx, _frame| { let res = eframe::run_simple_native(W_NAME, options, move |ctx, _frame| {
#[cfg(debug_assertions)]
let f_start = Instant::now();
egui_extras::install_image_loaders(ctx); egui_extras::install_image_loaders(ctx);
state.windows.clone().draw_all(ctx, &mut state); windows::Windows::draw_all(ctx, &mut state);
handle_error_ui!(main_window::draw(ctx, &mut state, &cache_rx)); handle_error_ui!(main_window::draw(ctx, &mut state, &cache_rx));
ctx.request_repaint_after(Duration::from_millis(500)); ctx.request_repaint_after(Duration::from_millis(500));
#[cfg(debug_assertions)]
{
let f_end = Instant::now();
state.debug_info.last_frame_time = f_end.duration_since(f_start);
}
}); });
if let Err(e) = res { // dumb err value by eframe if let Err(e) = res { // dumb err value by eframe
anyhow::bail!(e.to_string()); anyhow::bail!(e.to_string());
@@ -31,19 +45,38 @@ pub fn start() -> Result<()> {
Ok(()) Ok(())
} }
#[cfg(debug_assertions)]
#[derive(Debug, Default)]
pub struct DebugInfo {
pub last_frame_time: Duration,
}
pub struct GuiState { pub struct GuiState {
#[cfg(debug_assertions)]
pub debug_info: DebugInfo,
pub manifest: Manifest<JsonStore>, pub manifest: Manifest<JsonStore>,
pub windows: windows::Windows, pub windows: windows::Windows,
pub player: xmpd_player::Player, pub player: xmpd_player::Player,
} }
impl GuiState { impl GuiState {
pub fn new() -> Result<Self> { #[cfg(debug_assertions)]
pub fn new(manifest_p: &Path) -> Result<Self> {
Ok(Self {
debug_info: DebugInfo {
last_frame_time: Default::default()
},
player: xmpd_player::Player::new(),
manifest: Manifest::new(manifest_p)?,
windows: windows::Windows::new(),
})
}
#[cfg(not(debug_assertions))]
pub fn new(manifest_p: &Path) -> Result<Self> {
Ok(Self { Ok(Self {
player: xmpd_player::Player::new(), player: xmpd_player::Player::new(),
manifest: Manifest::new(&xmpd_cliargs::CLIARGS.manifest_path())?, manifest: Manifest::new(manifest_p)?,
windows: windows::Windows::new(), windows: windows::Windows::new(),
}) })
} }

View File

@@ -17,7 +17,7 @@ macro_rules! component_register {
} }
macro_rules! handle_error_ui { macro_rules! handle_error_ui {
($val:expr) => { ($val:expr_2021) => {
match $val { match $val {
Ok(v) => v, Ok(v) => v,
Err(e) => { Err(e) => {
@@ -37,7 +37,7 @@ macro_rules! handle_error_ui {
} }
macro_rules! handle_option { macro_rules! handle_option {
($reason:expr, $val:expr) => { ($reason:expr_2021, $val:expr_2021) => {
if let Some(v) = $val { if let Some(v) = $val {
v v
} else { } else {

View File

@@ -30,13 +30,15 @@ pub fn draw(ctx: &egui::Context, state: &mut GuiState, cache_rx: &Receiver<Messa
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.set_height(main_height); ui.set_height(main_height);
ui.vertical(|ui| { ui.vertical(|ui| {
ui.set_height(main_height);
ui.group(|ui| { ui.group(|ui| {
//ui.set_height(main_height * 0.1); //ui.set_height(main_height * 0.1);
ui.set_max_width(left_nav_width); ui.set_max_width(left_nav_width);
handle_error_ui!(crate::components::left_nav::header::Header::draw(ui, state)); handle_error_ui!(crate::components::left_nav::header::Header::draw(ui, state));
}); });
let avail = ui.available_size();
ui.group(|ui| { ui.group(|ui| {
// ui.set_height(main_height * 0.9); ui.set_height(avail.y);
ui.set_max_width(left_nav_width); ui.set_max_width(left_nav_width);
handle_error_ui!(crate::components::left_nav::LeftNav::draw(ui, state)); handle_error_ui!(crate::components::left_nav::LeftNav::draw(ui, state));
}); });

View File

@@ -1,9 +1,16 @@
use egui::{RichText, Sense, TextEdit, TopBottomPanel};
use xmpd_cache::DlStatus;
use xmpd_manifest::{song::Song, store::{BaseStore, StoreExtras}};
use crate::{components::{CompGetter, toast::{Toast, ToastType}}, windows::WindowId};
use super::Window; use super::Window;
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct AddSongW { pub struct AddSongW {
sid: uuid::Uuid,
pid: Option<uuid::Uuid>,
} }
impl Window for AddSongW { impl Window for AddSongW {
@@ -13,8 +20,134 @@ impl Window for AddSongW {
fn default_title() -> &'static str where Self: Sized { fn default_title() -> &'static str where Self: Sized {
"Add Song to Playlist" "Add Song to Playlist"
} }
fn draw(&mut self, ui: &mut egui::Ui, _: &mut crate::GuiState) -> crate::Result<()> { fn draw(&mut self, ui: &mut egui::Ui, state: &mut crate::GuiState) -> crate::Result<()> {
ui.label("Hello from other window!"); let mut save = false;
self.pid = crate::components::left_nav::LeftNav::get()?.selected_playlist_id.clone();
let theme = xmpd_settings::Settings::get()?.theme.clone();
let songs: Vec<_> = state.manifest.store().get_songs_sorted();
if self.sid.is_nil() {
if let Some(sid) = songs.first() {
self.sid = sid.0.clone();
}
}
let avail = ui.available_size();
ui.horizontal(|ui| {
ui.vertical(|ui| {
ui.group(|ui| {
ui.set_height(avail.y);
let img = egui::Image::new(crate::data::NOTE_ICON)
.tint(theme.accent_color)
.fit_to_exact_size(egui::Vec2::new(64.0, 64.0));
ui.add(img);
let mut name = String::new();
let mut author = String::new();
if let Some(song) = state.manifest.store().get_song(&self.sid) {
name = song.name().to_string();
author = song.author().to_string();
}
ui.horizontal(|ui| {
ui.style_mut().spacing.text_edit_width = 150.0;
ui.label("Name: ");
ui.add_enabled(false, TextEdit::singleline(&mut name));
});
ui.horizontal(|ui| {
ui.style_mut().spacing.text_edit_width = 150.0;
ui.label("Author: ");
ui.add_enabled(false, TextEdit::singleline(&mut author));
});
});
});
ui.vertical(|ui| {
ui.group(|ui| {
egui::ScrollArea::vertical()
.id_source("song_list_song_add")
.drag_to_scroll(false)
.show(ui, |ui| {
ui.vertical(|ui| {
for (sid, song) in songs {
let resp = ui.group(|ui| {
let avail = ui.available_size();
ui.horizontal(|ui| {
ui.set_width(avail.x);
let img = egui::Image::new(crate::data::NOTE_ICON)
.tint(theme.accent_color)
.fit_to_exact_size(egui::Vec2::new(32.0, 32.0));
ui.add(img);
ui.vertical(|ui| {
let status = {
handle_error_ui!(xmpd_cache::Cache::get()).get_cached_song_status(&sid).clone()
};
let label = if self.sid == *sid {
RichText::new(song.name())
.color(theme.accent_color)
} else if matches!(status, Some(DlStatus::Done(_))) {
RichText::new(song.name())
.color(theme.text_color)
} else {
RichText::new(song.name())
.color(theme.dim_text_color)
};
ui.label(label);
});
});
});
if resp.response.interact(Sense::click()).clicked() {
self.sid = sid.clone();
}
}
});
}
);
});
let theme = handle_error_ui!(xmpd_settings::Settings::get()).theme.clone();
TopBottomPanel::bottom("bottom_bar")
.frame(
egui::Frame::none()
.fill(theme.primary_bg_color)
.stroke(egui::Stroke::new(
1.0,
theme.secondary_bg_color,
)),
)
.show(ui.ctx(), |ui| {
ui.style_mut().visuals.override_text_color = Some(theme.text_color);
ui.add_space(3.0);
ui.horizontal(|ui| {
// ui.add_space(3.0);
if ui.button("Add").clicked() {
save = true;
}
if ui.button("Cancel").clicked() {
state.windows.toggle(&WindowId::AddSongToPl, false);
}
if ui.button("Close").clicked() {
state.windows.toggle(&WindowId::AddSongToPl, false);
}
});
});
})
});
if save {
match &self.pid {
Some(pid) => {
let pl = state.manifest.store_mut().get_playlist_mut(pid);
match pl {
Some(pl) => pl.add_song(&self.sid),
None => Toast::get().unwrap().show_toast("Not Allowed", "You cant add a song to the 'All Songs' playlist", ToastType::Error)
};
}
None => (),
}
}
Ok(()) Ok(())
} }
fn set_value<V>(&mut self, k: String, v: Box<V>) where Self: Sized {
}
} }

View File

@@ -62,6 +62,9 @@ impl Window for DebugW {
}); });
Ok(()) Ok(())
} }
fn set_value<V>(&mut self, k: String, v: Box<V>) where Self: Sized {
}
} }
impl DebugW { impl DebugW {

View File

@@ -17,4 +17,7 @@ impl Window for ErrorW {
ui.label("Hello from other window!"); ui.label("Hello from other window!");
Ok(()) Ok(())
} }
fn set_value<V>(&mut self, k: String, v: Box<V>) where Self: Sized {
}
} }

View File

@@ -3,15 +3,15 @@ use egui::{ViewportBuilder, ViewportId};
use crate::GuiState; use crate::GuiState;
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
mod debug; pub mod debug;
mod error; pub mod error;
mod settings; pub mod settings;
mod add_song; pub mod add_song;
mod new_song; pub mod new_song;
mod new_playlist; pub mod new_playlist;
lazy_static::lazy_static!( lazy_static::lazy_static!(
static ref WINDOWS: Arc<Mutex<HashMap<WindowId, Box<dyn Window>>>> = Arc::new(Mutex::new(HashMap::new())); pub static ref WINDOWS: Arc<Mutex<HashMap<WindowId, Box<dyn Window>>>> = Arc::new(Mutex::new(HashMap::new()));
static ref OPEN_WINDOWS: Arc<Mutex<HashSet<WindowId>>> = Arc::new(Mutex::new(HashSet::new())); static ref OPEN_WINDOWS: Arc<Mutex<HashSet<WindowId>>> = Arc::new(Mutex::new(HashSet::new()));
); );
@@ -22,6 +22,7 @@ pub trait Window: std::fmt::Debug + Send {
fn close(&self) where Self: Sized{ fn close(&self) where Self: Sized{
OPEN_WINDOWS.lock().unwrap().remove(&Self::id()); OPEN_WINDOWS.lock().unwrap().remove(&Self::id());
} }
fn set_value<V>(&mut self, k: String, v: Box<V>) where Self: Sized;
} }
#[derive(Debug, Clone, Hash, PartialEq, PartialOrd, Ord, Eq)] #[derive(Debug, Clone, Hash, PartialEq, PartialOrd, Ord, Eq)]
@@ -67,13 +68,13 @@ impl Windows {
WINDOWS.lock().unwrap().insert(WT::id(), Box::<WT>::default()); WINDOWS.lock().unwrap().insert(WT::id(), Box::<WT>::default());
} }
pub fn draw_all(&mut self, ctx: &egui::Context, state: &mut GuiState) { pub fn draw_all(ctx: &egui::Context, state: &mut GuiState) {
let theme = handle_error_ui!(xmpd_settings::Settings::get()).theme.clone(); let theme = handle_error_ui!(xmpd_settings::Settings::get()).theme.clone();
for (win_id, (vp_id, builder)) in &self.windows { for (win_id, (vp_id, builder)) in state.windows.windows.clone().into_iter() {
if self.is_open(&win_id) { if state.windows.is_open(&win_id) {
ctx.show_viewport_immediate(vp_id.clone(), builder.clone(), |ctx, _vp_class| { ctx.show_viewport_immediate(vp_id.clone(), builder.clone(), |ctx, _vp_class| {
ctx.input(|inp| { ctx.input(|inp| {
self.toggle(win_id, !inp.viewport().close_requested()); state.windows.toggle(&win_id, !inp.viewport().close_requested());
}); });
egui::CentralPanel::default() egui::CentralPanel::default()
.frame( .frame(
@@ -104,4 +105,10 @@ impl Windows {
pub fn is_open(&self, id: &WindowId) -> bool { pub fn is_open(&self, id: &WindowId) -> bool {
OPEN_WINDOWS.lock().unwrap().contains(&id) OPEN_WINDOWS.lock().unwrap().contains(&id)
} }
pub fn set_value(&self, id: &WindowId, k: impl ToString, v: impl ToString) -> crate::Result<()> {
// WINDOWS.lock().unwrap().get_mut(&win_id).unwrap().set_value();
//
Ok(())
}
} }

View File

@@ -99,4 +99,7 @@ impl Window for NewPlaylistW {
}); });
Ok(()) Ok(())
} }
fn set_value<V>(&mut self, k: String, v: Box<V>) where Self: Sized {
}
} }

View File

@@ -1,9 +1,20 @@
use std::str::FromStr;
use egui::{Sense, Vec2};
use xmpd_manifest::{song::{Song, SourceType}, store::BaseStore};
use crate::{components::{CompGetter, toast::{Toast, ToastType}}, windows::WindowId};
use super::Window; use super::Window;
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct NewSongW { pub struct NewSongW {
name: String,
author: String,
source_t: SourceType,
source_url: String,
source_url_old: String,
} }
impl Window for NewSongW { impl Window for NewSongW {
@@ -13,8 +24,113 @@ impl Window for NewSongW {
fn default_title() -> &'static str where Self: Sized { fn default_title() -> &'static str where Self: Sized {
"New Song" "New Song"
} }
fn draw(&mut self, ui: &mut egui::Ui, _: &mut crate::GuiState) -> crate::Result<()> { fn draw(&mut self, ui: &mut egui::Ui, state: &mut crate::GuiState) -> crate::Result<()> {
ui.label("Hello from other window!"); let theme = xmpd_settings::Settings::get()?.theme.clone();
let img_size = 64.0;
let img_spacing = 10.0;
ui.vertical(|ui| {
let mut rect = egui::Rect::ZERO;
rect.set_width(img_size);
rect.set_height(img_size);
rect.set_top(img_spacing);
rect.set_left(img_spacing);
let rect_int = ui.interact(rect, "new_playlist_w".into(), Sense::click());
if rect_int.hovered() {
ui.allocate_ui_at_rect(rect, |ui| {
ui.group(|ui| {
let img = egui::Image::new(crate::data::PLUS_ICON)
.tint(theme.accent_color)
.fit_to_exact_size(Vec2::new(img_size, img_size));
//.paint_at(ui, rect);
ui.add(img);
});
});
} else {
ui.allocate_ui_at_rect(rect, |ui| {
ui.group(|ui| {
let img = egui::Image::new(crate::data::NOTE_ICON)
.tint(theme.accent_color)
.fit_to_exact_size(Vec2::new(img_size, img_size));
//.paint_at(ui, rect);
ui.add(img);
});
});
}
if rect_int.clicked() {
// TODO: Add a way to add custom icons
Toast::get().unwrap().show_toast("Not Implemented", "Adding icons to songs is not implemented", ToastType::Error);
}
ui.add_space(img_spacing);
ui.horizontal(|ui| {
ui.label("Name: ");
ui.text_edit_singleline(&mut self.name);
});
ui.horizontal(|ui| {
ui.label("Author: ");
ui.text_edit_singleline(&mut self.author);
});
ui.horizontal(|ui| {
ui.label("Source Type: ");
egui::ComboBox::new("new_song_song_t_sel", "")
.selected_text(self.source_t.to_string())
.show_ui(ui, |ui| {
ui.selectable_value(&mut self.source_t, SourceType::Youtube, SourceType::Youtube.to_string());
ui.selectable_value(&mut self.source_t, SourceType::Spotify, SourceType::Spotify.to_string());
ui.selectable_value(&mut self.source_t, SourceType::Soundcloud, SourceType::Soundcloud.to_string());
//ui.selectable_value(&mut self.source_t, SourceType::HttpBare, SourceType::HttpBare.to_string());
//ui.selectable_value(&mut self.source_t, SourceType::HttpZip, SourceType::HttpZip.to_string());
//ui.selectable_value(&mut self.source_t, SourceType::Http7z, SourceType::Http7z.to_string());
}
);
});
ui.horizontal(|ui| {
ui.label("Source URL: ");
ui.text_edit_singleline(&mut self.source_url);
if self.source_url != self.source_url_old {
if let Some(t) = SourceType::from_url(&handle_error_ui!(url::Url::from_str(&self.source_url))) {
self.source_t = t;
}
}
});
ui.with_layout(egui::Layout::bottom_up(egui::Align::Max), |ui| {
ui.add_space(3.0);
ui.horizontal(|ui| {
ui.add_space(3.0);
if ui.button("Close").clicked() {
self.author = String::from("New Song");
self.name = String::from("Unknown");
self.source_t = SourceType::Youtube;
self.source_url = String::default();
state.windows.toggle(&WindowId::NewSong, false);
}
if ui.button("Add").clicked() {
let mut s = handle_error_ui!(Song::new_from_str(&self.source_url, self.source_t));
s.set_name(&self.name);
s.set_author(&self.author);
state.manifest.store_mut().get_songs_mut().insert(uuid::Uuid::new_v4(), s);
self.author = String::from("New Song");
self.name = String::from("Unknown");
self.source_t = SourceType::Youtube;
self.source_url = String::default();
}
if ui.button("Cancel").clicked() {
self.author = String::from("New Song");
self.name = String::from("Unknown");
self.source_t = SourceType::Youtube;
self.source_url = String::default();
state.windows.toggle(&WindowId::NewSong, false);
}
});
});
});
Ok(()) Ok(())
} }
fn set_value<V>(&mut self, _: String, _: Box<V>) where Self: Sized {
}
} }

View File

@@ -85,6 +85,9 @@ impl Window for SettingsW {
}); });
Ok(()) Ok(())
} }
fn set_value<V>(&mut self, k: String, v: Box<V>) where Self: Sized {
}
} }
impl SettingsW { impl SettingsW {

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "xmpd-manifest" name = "xmpd-manifest"
edition = "2021" edition.workspace = true
readme="README.md" readme="README.md"
authors.workspace = true authors.workspace = true
version.workspace = true version.workspace = true

View File

@@ -1,5 +1,7 @@
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use anyhow::anyhow;
#[cfg(test)] #[cfg(test)]
pub mod tests; pub mod tests;
@@ -59,12 +61,13 @@ impl<ST: store::BaseStore + Clone> Manifest<ST> {
} }
pub fn get_song_as_path(&self, sid: uuid::Uuid) -> Result<PathBuf> { pub fn get_song_as_path(&self, sid: uuid::Uuid) -> Result<PathBuf> {
let ext = &xmpd_settings::Settings::get()?.tooling.song_format; let settings = &xmpd_settings::Settings::get()?;
let mut p = xmpd_cliargs::CLIARGS.cache_path().into_std_path_buf(); let ext = &settings.tooling.song_format;
let mut p = settings.cache_settings.cache_path.clone();
p.push("songs"); p.push("songs");
p.push(sid.to_string()); p.push(sid.to_string());
p.set_extension(ext); p.set_extension(ext);
Ok(p) Ok(p.into_std_path_buf())
} }
} }

View File

@@ -83,8 +83,9 @@ impl Song {
} }
} }
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize, PartialEq, PartialOrd)] #[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize, PartialEq, PartialOrd, Default)]
pub enum SourceType { pub enum SourceType {
#[default]
Youtube, Youtube,
Spotify, Spotify,
Soundcloud, Soundcloud,
@@ -94,7 +95,7 @@ pub enum SourceType {
} }
impl SourceType { impl SourceType {
fn from_url(url: &url::Url) -> Option<Self> { pub fn from_url(url: &url::Url) -> Option<Self> {
match url.host_str() { match url.host_str() {
Some("youtube.com") | Some("youtu.be") => Some("youtube.com") | Some("youtu.be") =>
Some(Self::Youtube), Some(Self::Youtube),

View File

@@ -15,6 +15,8 @@ pub struct JsonStore {
playlists: HashMap<Uuid, Playlist> playlists: HashMap<Uuid, Playlist>
} }
impl super::StoreExtras for JsonStore {}
impl super::BaseStore for JsonStore { impl super::BaseStore for JsonStore {
fn get_default_file_contents() -> &'static str { fn get_default_file_contents() -> &'static str {
&DEFAULT_TEXT &DEFAULT_TEXT

View File

@@ -40,3 +40,55 @@ pub trait BaseStore {
fn get_original_path(&self) -> &Path; fn get_original_path(&self) -> &Path;
} }
pub trait StoreExtras: BaseStore {
fn get_songs_sorted(&self) -> Vec<(&Uuid, &Song)> {
let mut songs: Vec<_> = self.get_songs().iter().collect();
songs.sort_by(|a, b| {
let a = a.1.name().to_lowercase();
let b = b.1.name().to_lowercase();
a.cmp(&b)
});
songs
}
fn get_songs_sorted_cloned(&self) -> Vec<(Uuid, Song)> {
let mut songs = Vec::new();
for song in self.get_songs_sorted() {
songs.push((song.0.clone(), song.1.clone()));
}
songs
}
fn get_playlists_sorted(&self) -> Vec<(&Uuid, &Playlist)> {
let mut songs: Vec<_> = self.get_playlists().iter().collect();
songs.sort_by(|a, b| {
let a = a.1.name().to_lowercase();
let b = b.1.name().to_lowercase();
a.cmp(&b)
});
songs
}
fn get_playlist_songs_sorted(&self, pid: uuid::Uuid) -> Option<Vec<(&Uuid, &Song)>> {
let songs_ids: Vec<_> = self.get_playlist(&pid)?.songs().iter().collect();
let mut songs = Vec::new();
for sid in songs_ids {
songs.push((sid, self.get_song(sid)?));
}
songs.sort_by(|a, b| {
let a = a.1.name().to_lowercase();
let b = b.1.name().to_lowercase();
a.cmp(&b)
});
Some(songs)
}
fn get_playlist_songs_sorted_cloned(&self, pid: uuid::Uuid) -> Option<Vec<(Uuid, Song)>> {
let mut songs = Vec::new();
for song in self.get_playlist_songs_sorted(pid)? {
songs.push((song.0.clone(), song.1.clone()));
}
Some(songs)
}
}

View File

@@ -15,6 +15,8 @@ pub struct TomlStore {
playlists: HashMap<Uuid, Playlist> playlists: HashMap<Uuid, Playlist>
} }
impl super::StoreExtras for TomlStore {}
impl super::BaseStore for TomlStore { impl super::BaseStore for TomlStore {
fn get_default_file_contents() -> &'static str { fn get_default_file_contents() -> &'static str {
&DEFAULT_TEXT &DEFAULT_TEXT

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "xmpd-player" name = "xmpd-player"
edition = "2021" edition.workspace = true
version.workspace = true version.workspace = true
repository.workspace = true repository.workspace = true
license.workspace = true license.workspace = true

View File

@@ -1,12 +1,13 @@
[package] [package]
name = "xmpd-settings" name = "xmpd-settings"
edition = "2021" edition.workspace = true
version.workspace = true version.workspace = true
repository.workspace = true repository.workspace = true
license.workspace = true license.workspace = true
authors.workspace = true authors.workspace = true
[dependencies] [dependencies]
xmpd-cliargs.path = "../xmpd-cliargs"
anyhow.workspace = true anyhow.workspace = true
camino.workspace = true camino.workspace = true
egui.workspace = true egui.workspace = true

View File

@@ -0,0 +1,29 @@
use camino::Utf8PathBuf;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Cache {
#[serde(default="Cache::default_cache_path")]
pub cache_path: Utf8PathBuf,
#[serde(default="Cache::default_manifest_path")]
pub manifest_path: Utf8PathBuf,
}
impl Default for Cache {
fn default() -> Self {
Self {
cache_path: Self::default_cache_path(),
manifest_path: Self::default_manifest_path(),
}
}
}
impl Cache {
fn default_cache_path() -> Utf8PathBuf {
Utf8PathBuf::from("./cache")
}
fn default_manifest_path() -> Utf8PathBuf {
Utf8PathBuf::from("./manifest.json")
}
}

View File

@@ -5,6 +5,7 @@ use tooling::Tooling;
pub mod theme; pub mod theme;
pub mod tooling; pub mod tooling;
pub mod cache;
lazy_static::lazy_static!( lazy_static::lazy_static!(
static ref SETTINGS: Arc<Mutex<Settings>> = Arc::new(Mutex::new(Settings::default())); static ref SETTINGS: Arc<Mutex<Settings>> = Arc::new(Mutex::new(Settings::default()));
@@ -20,6 +21,8 @@ pub struct Settings {
pub theme: Theme, pub theme: Theme,
#[serde(default)] #[serde(default)]
pub tooling: Tooling, pub tooling: Tooling,
#[serde(default)]
pub cache_settings: cache::Cache
} }
impl Settings { impl Settings {
@@ -54,6 +57,16 @@ impl Settings {
self.settings_path = path; self.settings_path = path;
Ok(()) Ok(())
} }
pub fn load_cli_args(&mut self, cli_args: &xmpd_cliargs::CliArgs) {
if let Some(mp) = cli_args.manifest_path() {
self.cache_settings.manifest_path = mp;
}
if let Some(cp) = cli_args.cache_path() {
self.cache_settings.cache_path = cp;
}
}
} }

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "xmpd-tooling" name = "xmpd-tooling"
edition = "2021" edition.workspace = true
version.workspace = true version.workspace = true
repository.workspace = true repository.workspace = true
license.workspace = true license.workspace = true