diff --git a/package-lock.json b/package-lock.json index 69943842d..ecd0d2ae0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "qbz-nix", - "version": "1.1.8-rc-5", + "version": "1.1.9", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "qbz-nix", - "version": "1.1.8-rc-5", + "version": "1.1.9", "license": "MIT", "dependencies": { "@tauri-apps/api": "^2", diff --git a/package.json b/package.json index 158f321ca..42b23548e 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "preview": "vite preview", "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch", + "check:no-t-shadow": "bash ./scripts/check-no-t-shadow.sh", "test": "vitest", "test:run": "vitest run", "tauri": "tauri" diff --git a/scripts/check-no-t-shadow.sh b/scripts/check-no-t-shadow.sh new file mode 100644 index 000000000..fcddea8fc --- /dev/null +++ b/scripts/check-no-t-shadow.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash +set -euo pipefail + +ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +SRC_DIR="${ROOT_DIR}/src" + +echo "Checking for forbidden i18n 't' shadowing patterns in .svelte files..." + +had_issues=0 + +check_pattern() { + local label="$1" + local pattern="$2" + if rg -n --glob '*.svelte' "${pattern}" "${SRC_DIR}" >/tmp/qbz_t_shadow_check.out 2>/dev/null; then + echo + echo "Found ${label}:" + cat /tmp/qbz_t_shadow_check.out + had_issues=1 + fi +} + +# Template loop variable shadowing. +check_pattern "template each-loop shadowing (as t)" '\{#each[^\n]* as t\b' + +# Dangerous callbacks in files using $t(...). +while IFS= read -r file; do + if rg -q '\b(map|forEach|filter|find|some|every|reduce)\(\s*t\s*=>|\b(map|forEach|filter|find|some|every|reduce)\(\s*\([^)]*,\s*t\s*\)\s*=>' "${file}"; then + echo + echo "Found callback shadowing in file using \$t(...): ${file}" + rg -n '\b(map|forEach|filter|find|some|every|reduce)\(\s*t\s*=>|\b(map|forEach|filter|find|some|every|reduce)\(\s*\([^)]*,\s*t\s*\)\s*=>' "${file}" + had_issues=1 + fi +done < <(rg -l --glob '*.svelte' '\$t\(' "${SRC_DIR}") + +# Any $derived expression using variable name t. +check_pattern "usage of t inside \$derived" '\$derived\([^)]*\bt\b' + +if [[ "${had_issues}" -ne 0 ]]; then + echo + echo "check-no-t-shadow: FAIL" + exit 1 +fi + +echo "check-no-t-shadow: OK" diff --git a/src-tauri/src/api_server/mod.rs b/src-tauri/src/api_server/mod.rs index 125e22597..6a328c302 100644 --- a/src-tauri/src/api_server/mod.rs +++ b/src-tauri/src/api_server/mod.rs @@ -936,6 +936,7 @@ async fn play_album( album_id: Some(album.id.clone()), artist_id: t.performer.as_ref().map(|p| p.id), streamable: t.streamable, + source: Some("qobuz".to_string()), } }).collect() } else { diff --git a/src-tauri/src/commands/playback_context.rs b/src-tauri/src/commands/playback_context.rs index 20517858c..0df4b1fe0 100644 --- a/src-tauri/src/commands/playback_context.rs +++ b/src-tauri/src/commands/playback_context.rs @@ -37,6 +37,7 @@ pub fn set_playback_context( let content_source = match source.as_str() { "qobuz" => ContentSource::Qobuz, "local" => ContentSource::Local, + "plex" => ContentSource::Plex, _ => return Err(format!("Invalid source: {}", source)), }; diff --git a/src-tauri/src/commands/radio.rs b/src-tauri/src/commands/radio.rs index 0478e78be..96c4e0040 100644 --- a/src-tauri/src/commands/radio.rs +++ b/src-tauri/src/commands/radio.rs @@ -327,6 +327,7 @@ fn track_to_queue_track(track: &Track) -> QueueTrack { album_id, artist_id, streamable: track.streamable, + source: Some("qobuz".to_string()), } } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 401a001d3..db41df4cc 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -27,6 +27,7 @@ pub mod musicbrainz; pub mod network; pub mod offline; pub mod playback_context; +pub mod plex; pub mod player; pub mod playlist_import; pub mod queue; @@ -687,6 +688,24 @@ pub fn run() { cast::dlna::commands::dlna_stop, cast::dlna::commands::dlna_seek, cast::dlna::commands::dlna_set_volume, + // Plex LAN-only POC commands + plex::plex_ping, + plex::plex_get_music_sections, + plex::plex_get_section_tracks, + plex::plex_get_track_metadata, + plex::plex_play_track, + plex::plex_auth_pin_start, + plex::plex_auth_pin_check, + plex::plex_open_auth_url, + plex::plex_cache_get_sections, + plex::plex_cache_save_sections, + plex::plex_cache_get_tracks, + plex::plex_cache_save_tracks, + plex::plex_cache_update_track_quality, + plex::plex_cache_get_albums, + plex::plex_cache_get_album_tracks, + plex::plex_cache_search_tracks, + plex::plex_cache_clear, // AirPlay casting commands - DISABLED until RAOP implementation is complete // See docs/AIRPLAY_IMPLEMENTATION_STATUS.md for details // cast::airplay::commands::airplay_start_discovery, diff --git a/src-tauri/src/playback_context/mod.rs b/src-tauri/src/playback_context/mod.rs index d35d3ce83..36cb23cdd 100644 --- a/src-tauri/src/playback_context/mod.rs +++ b/src-tauri/src/playback_context/mod.rs @@ -32,6 +32,7 @@ pub enum ContextType { pub enum ContentSource { Qobuz, Local, + Plex, } /// Playback context - the semantic origin of playback diff --git a/src-tauri/src/plex/mod.rs b/src-tauri/src/plex/mod.rs new file mode 100644 index 000000000..bdcc16479 --- /dev/null +++ b/src-tauri/src/plex/mod.rs @@ -0,0 +1,1346 @@ +//! Plex LAN-only POC integration. +//! +//! This module intentionally avoids transcoding endpoints and uses `/library/parts/.../file...` +//! so playback uses original media bytes served by Plex Media Server. + +use std::collections::hash_map::DefaultHasher; +use std::collections::HashMap; +use std::hash::{Hash, Hasher}; +use std::time::Duration; + +use reqwest::header::{HeaderMap, HeaderValue}; +use rusqlite::{Connection, params}; +use serde::{Deserialize, Serialize}; +use tauri::State; + +use crate::AppState; + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct PlexServerInfo { + pub friendly_name: Option, + pub version: Option, + pub machine_identifier: Option, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct PlexMusicSection { + pub key: String, + pub title: String, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct PlexTrack { + pub rating_key: String, + pub title: String, + pub artist: Option, + pub album: Option, + pub duration_ms: Option, + pub artwork_path: Option, + pub part_key: Option, + pub container: Option, + pub codec: Option, + pub channels: Option, + pub bitrate_kbps: Option, + pub sampling_rate_hz: Option, + pub bit_depth: Option, +} + +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct PlexPlayResult { + pub rating_key: String, + pub part_key: String, + pub part_url: String, + pub bytes: usize, + pub direct_play_confirmed: bool, + pub content_type: Option, + pub sampling_rate_hz: Option, + pub bit_depth: Option, +} + +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct PlexPinStartResult { + pub pin_id: u64, + pub code: String, + pub auth_url: String, + pub expires_in: Option, +} + +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct PlexPinCheckResult { + pub authorized: bool, + pub expired: bool, + pub auth_token: Option, + pub expires_in: Option, +} + +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct PlexCachedAlbum { + pub id: String, + pub title: String, + pub artist: String, + pub artwork_path: Option, + pub track_count: u32, + pub total_duration_secs: u64, + pub format: String, + pub bit_depth: Option, + pub sample_rate: u32, + pub source: String, +} + +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct PlexCachedTrack { + pub id: u64, + pub rating_key: String, + pub title: String, + pub artist: String, + pub album: String, + pub duration_secs: u64, + pub format: String, + pub bit_depth: Option, + pub sample_rate: u32, + pub artwork_path: Option, + pub source: String, + pub album_key: String, +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct PlexTrackQualityUpdate { + pub rating_key: String, + pub container: Option, + pub sampling_rate_hz: Option, + pub bit_depth: Option, +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(rename_all = "camelCase")] +struct PlexPinResponse { + id: u64, + code: String, + #[serde(default)] + auth_token: Option, + #[serde(default)] + expires_in: Option, +} + +#[derive(Debug, Default)] +struct TrackBuilder { + rating_key: Option, + title: Option, + artist: Option, + album: Option, + duration_ms: Option, + artwork_path: Option, + part_key: Option, + container: Option, + codec: Option, + channels: Option, + bitrate_kbps: Option, + sampling_rate_hz: Option, + bit_depth: Option, +} + +fn normalize_base_url(base_url: &str) -> String { + base_url.trim_end_matches('/').to_string() +} + +fn now_epoch_secs() -> i64 { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_secs() as i64) + .unwrap_or(0) +} + +fn open_plex_cache_db() -> Result { + let data_dir = dirs::data_dir() + .ok_or("Could not determine data directory")? + .join("qbz"); + std::fs::create_dir_all(&data_dir) + .map_err(|e| format!("Failed to create Plex cache dir: {}", e))?; + + let db_path = data_dir.join("plex_cache.db"); + let conn = Connection::open(db_path) + .map_err(|e| format!("Failed to open Plex cache database: {}", e))?; + + conn.execute_batch( + " + CREATE TABLE IF NOT EXISTS plex_cache_sections ( + section_key TEXT PRIMARY KEY, + title TEXT NOT NULL, + server_id TEXT, + updated_at INTEGER NOT NULL + ); + + CREATE TABLE IF NOT EXISTS plex_cache_tracks ( + rating_key TEXT PRIMARY KEY, + section_key TEXT NOT NULL, + server_id TEXT, + title TEXT NOT NULL, + artist TEXT, + album TEXT, + duration_ms INTEGER, + artwork_path TEXT, + part_key TEXT, + container TEXT, + codec TEXT, + channels INTEGER, + bitrate_kbps INTEGER, + sampling_rate_hz INTEGER, + bit_depth INTEGER, + updated_at INTEGER NOT NULL + ); + CREATE INDEX IF NOT EXISTS idx_plex_cache_tracks_section ON plex_cache_tracks(section_key); + " + ).map_err(|e| format!("Failed to initialize Plex cache schema: {}", e))?; + + Ok(conn) +} + +fn build_plex_client() -> Result { + let mut headers = HeaderMap::new(); + headers.insert("X-Plex-Product", HeaderValue::from_static("QBZ")); + headers.insert("X-Plex-Version", HeaderValue::from_static("0.1-poc")); + headers.insert("X-Plex-Device", HeaderValue::from_static("QBZ Desktop")); + headers.insert( + "X-Plex-Platform", + HeaderValue::from_static(std::env::consts::OS), + ); + headers.insert( + "X-Plex-Client-Identifier", + HeaderValue::from_static("qbz-plex-lan-poc"), + ); + + reqwest::Client::builder() + .default_headers(headers) + .timeout(Duration::from_secs(120)) + .connect_timeout(Duration::from_secs(8)) + .build() + .map_err(|e| format!("Failed to create Plex HTTP client: {}", e)) +} + +fn build_plex_auth_client(client_identifier: &str) -> Result { + let mut headers = HeaderMap::new(); + headers.insert("X-Plex-Product", HeaderValue::from_static("QBZ")); + headers.insert("X-Plex-Version", HeaderValue::from_static("0.1-poc")); + headers.insert("X-Plex-Device", HeaderValue::from_static("QBZ Desktop")); + headers.insert( + "X-Plex-Platform", + HeaderValue::from_static(std::env::consts::OS), + ); + headers.insert( + "X-Plex-Client-Identifier", + HeaderValue::from_str(client_identifier) + .map_err(|e| format!("Invalid Plex client identifier: {}", e))?, + ); + headers.insert("Accept", HeaderValue::from_static("application/json")); + + reqwest::Client::builder() + .default_headers(headers) + .timeout(Duration::from_secs(20)) + .connect_timeout(Duration::from_secs(8)) + .build() + .map_err(|e| format!("Failed to create Plex auth HTTP client: {}", e)) +} + +fn build_plex_auth_url() -> String { + "https://plex.tv/link".to_string() +} + +fn with_token(url: &str, token: &str) -> String { + let sep = if url.contains('?') { "&" } else { "?" }; + format!("{url}{sep}X-Plex-Token={token}") +} + +fn parse_u64(v: Option) -> Option { + v.and_then(|s| s.parse::().ok()) +} + +fn parse_u32(v: Option) -> Option { + v.and_then(|s| s.parse::().ok()) +} + +fn decode_xml_entities(input: &str) -> String { + let mut out = String::with_capacity(input.len()); + let bytes = input.as_bytes(); + let mut i = 0usize; + + while i < bytes.len() { + if bytes[i] == b'&' { + let mut j = i + 1; + while j < bytes.len() && bytes[j] != b';' && (j - i) <= 12 { + j += 1; + } + + if j < bytes.len() && bytes[j] == b';' { + let entity = &input[i + 1..j]; + let decoded = match entity { + "amp" => Some('&'), + "lt" => Some('<'), + "gt" => Some('>'), + "quot" => Some('"'), + "apos" => Some('\''), + _ if entity.starts_with("#x") || entity.starts_with("#X") => { + u32::from_str_radix(&entity[2..], 16).ok().and_then(char::from_u32) + } + _ if entity.starts_with('#') => { + entity[1..].parse::().ok().and_then(char::from_u32) + } + _ => None, + }; + + if let Some(ch) = decoded { + out.push(ch); + i = j + 1; + continue; + } + } + } + + if let Some(ch) = input[i..].chars().next() { + out.push(ch); + i += ch.len_utf8(); + } else { + break; + } + } + + out +} + +fn normalize_album_title(artist: Option<&str>, album: &str) -> String { + let trimmed_album = album.trim(); + let Some(artist_value) = artist.map(str::trim).filter(|a| !a.is_empty()) else { + return trimmed_album.to_string(); + }; + + for sep in [" - ", " — ", " – ", ": "] { + let prefix = format!("{artist_value}{sep}"); + if trimmed_album.starts_with(&prefix) { + return trimmed_album[prefix.len()..].trim().to_string(); + } + } + + trimmed_album.to_string() +} + +fn get_attr(tag: &str, key: &str) -> Option { + let tag_bytes = tag.as_bytes(); + let key_bytes = key.as_bytes(); + if key_bytes.is_empty() || tag_bytes.len() < key_bytes.len() + 2 { + return None; + } + + let mut i = 0usize; + while i + key_bytes.len() + 2 <= tag_bytes.len() { + if &tag_bytes[i..i + key_bytes.len()] != key_bytes { + i += 1; + continue; + } + + let prev = if i == 0 { b' ' } else { tag_bytes[i - 1] }; + if !prev.is_ascii_whitespace() && prev != b'<' { + i += 1; + continue; + } + + let eq_idx = i + key_bytes.len(); + if eq_idx >= tag_bytes.len() || tag_bytes[eq_idx] != b'=' { + i += 1; + continue; + } + let quote_idx = eq_idx + 1; + if quote_idx >= tag_bytes.len() || tag_bytes[quote_idx] != b'"' { + i += 1; + continue; + } + + let value_start = quote_idx + 1; + let value_rel_end = tag[value_start..].find('"')?; + let value_end = value_start + value_rel_end; + return Some(decode_xml_entities(tag[value_start..value_end].trim())); + } + + None +} + +fn find_first_tag(xml: &str, tag_name: &str) -> Option { + let needle = format!("<{tag_name}"); + let start = xml.find(&needle)?; + let rest = &xml[start..]; + let end = rest.find('>')?; + Some(rest[..=end].to_string()) +} + +fn collect_start_tags(xml: &str, tag_name: &str) -> Vec { + let mut tags = Vec::new(); + let open = format!("<{tag_name}"); + let mut offset = 0usize; + + while let Some(pos) = xml[offset..].find(&open) { + let start = offset + pos; + let rest = &xml[start..]; + let Some(end) = rest.find('>') else { + break; + }; + tags.push(rest[..=end].to_string()); + offset = start + end + 1; + } + + tags +} + +fn collect_tag_blocks(xml: &str, tag_name: &str) -> Vec<(String, String)> { + let mut blocks = Vec::new(); + let open = format!("<{tag_name}"); + let close = format!(""); + let mut offset = 0usize; + + while let Some(pos) = xml[offset..].find(&open) { + let start = offset + pos; + let rest = &xml[start..]; + let Some(open_end_rel) = rest.find('>') else { + break; + }; + let open_end = start + open_end_rel; + let start_tag = &xml[start..=open_end]; + let is_self_closing = start_tag.trim_end().ends_with("/>"); + + if is_self_closing { + blocks.push((start_tag.to_string(), String::new())); + offset = open_end + 1; + continue; + } + + let after_open = open_end + 1; + let Some(close_rel) = xml[after_open..].find(&close) else { + break; + }; + let close_start = after_open + close_rel; + let inner = &xml[after_open..close_start]; + blocks.push((start_tag.to_string(), inner.to_string())); + offset = close_start + close.len(); + } + + blocks +} + +fn parse_server_info(xml: &str) -> PlexServerInfo { + let tag = find_first_tag(xml, "MediaContainer"); + PlexServerInfo { + friendly_name: tag.as_ref().and_then(|t| get_attr(t, "friendlyName")), + version: tag.as_ref().and_then(|t| get_attr(t, "version")), + machine_identifier: tag.as_ref().and_then(|t| get_attr(t, "machineIdentifier")), + } +} + +fn parse_music_sections(xml: &str) -> Vec { + let mut sections = Vec::new(); + for tag in collect_start_tags(xml, "Directory") { + if get_attr(&tag, "type").as_deref() != Some("artist") { + continue; + } + if let (Some(key), Some(title)) = (get_attr(&tag, "key"), get_attr(&tag, "title")) { + sections.push(PlexMusicSection { key, title }); + } + } + sections +} + +fn parse_track_block(start_tag: &str, inner_xml: &str) -> Option { + let mut t = TrackBuilder { + rating_key: get_attr(start_tag, "ratingKey"), + title: get_attr(start_tag, "title"), + artist: get_attr(start_tag, "grandparentTitle") + .or_else(|| get_attr(start_tag, "originalTitle")), + album: get_attr(start_tag, "parentTitle"), + duration_ms: parse_u64(get_attr(start_tag, "duration")), + artwork_path: get_attr(start_tag, "thumb") + .or_else(|| get_attr(start_tag, "parentThumb")) + .or_else(|| get_attr(start_tag, "grandparentThumb")), + ..Default::default() + }; + + if let Some(title) = t.title.as_mut() { + *title = title.trim().to_string(); + } + if let Some(artist) = t.artist.as_mut() { + *artist = artist.trim().to_string(); + } + if let Some(album) = t.album.as_mut() { + let normalized = normalize_album_title(t.artist.as_deref(), album); + *album = normalized; + } + + for media_tag in collect_start_tags(inner_xml, "Media") { + t.container = get_attr(&media_tag, "container"); + t.bitrate_kbps = parse_u32(get_attr(&media_tag, "bitrate")); + t.sampling_rate_hz = parse_u32(get_attr(&media_tag, "samplingRate")); + t.bit_depth = parse_u32(get_attr(&media_tag, "bitDepth")); + break; + } + + // Stream metadata is more accurate for audio details than Media-level fields. + // Prefer selected audio stream when available, otherwise use the first audio stream. + let mut selected_audio_stream: Option = None; + let mut first_audio_stream: Option = None; + for stream_tag in collect_start_tags(inner_xml, "Stream") { + let is_audio = get_attr(&stream_tag, "streamType").as_deref() == Some("2") + || get_attr(&stream_tag, "codecType").as_deref() == Some("audio"); + if !is_audio { + continue; + } + if first_audio_stream.is_none() { + first_audio_stream = Some(stream_tag.clone()); + } + if get_attr(&stream_tag, "selected").as_deref() == Some("1") { + selected_audio_stream = Some(stream_tag); + break; + } + } + + if let Some(stream_tag) = selected_audio_stream.or(first_audio_stream) { + if let Some(codec) = get_attr(&stream_tag, "codec") { + t.codec = Some(codec); + } + if let Some(channels) = parse_u32(get_attr(&stream_tag, "channels")) { + t.channels = Some(channels); + } + if let Some(bitrate) = parse_u32(get_attr(&stream_tag, "bitrate")) { + t.bitrate_kbps = Some(bitrate); + } + if let Some(rate) = parse_u32(get_attr(&stream_tag, "samplingRate")) { + t.sampling_rate_hz = Some(rate); + } + if let Some(depth) = parse_u32(get_attr(&stream_tag, "bitDepth")) { + t.bit_depth = Some(depth); + } + } + + for part_tag in collect_start_tags(inner_xml, "Part") { + t.part_key = get_attr(&part_tag, "key"); + if t.part_key.is_some() { + break; + } + } + + let (Some(rating_key), Some(title)) = (t.rating_key, t.title) else { + return None; + }; + + Some(PlexTrack { + rating_key, + title, + artist: t.artist, + album: t.album, + duration_ms: t.duration_ms, + artwork_path: t.artwork_path, + part_key: t.part_key, + container: t.container, + codec: t.codec, + channels: t.channels, + bitrate_kbps: t.bitrate_kbps, + sampling_rate_hz: t.sampling_rate_hz, + bit_depth: t.bit_depth, + }) +} + +fn parse_tracks(xml: &str, limit: Option) -> Vec { + let mut tracks = Vec::new(); + + for (start_tag, inner_xml) in collect_tag_blocks(xml, "Track") { + if let Some(track) = parse_track_block(&start_tag, &inner_xml) { + tracks.push(track); + if let Some(max) = limit { + if tracks.len() >= max as usize { + break; + } + } + } + } + + tracks +} + +fn synthetic_track_id(rating_key: &str) -> u64 { + let mut hasher = DefaultHasher::new(); + rating_key.hash(&mut hasher); + hasher.finish() +} + +fn playback_track_id(rating_key: &str) -> u64 { + rating_key + .parse::() + .unwrap_or_else(|_| synthetic_track_id(rating_key)) +} + +fn plex_album_key(artist: &str, album: &str) -> String { + let mut hasher = DefaultHasher::new(); + artist.hash(&mut hasher); + "::".hash(&mut hasher); + album.hash(&mut hasher); + format!("plex:{}", hasher.finish()) +} + +fn is_direct_part_key(part_key: &str) -> bool { + part_key.starts_with("/library/parts/") && part_key.contains("/file") +} + +#[tauri::command] +pub async fn plex_ping(base_url: String, token: String) -> Result { + let client = build_plex_client()?; + let base = normalize_base_url(&base_url); + let url = with_token(&format!("{base}/"), &token); + + let xml = client + .get(url) + .send() + .await + .map_err(|e| format!("Plex ping request failed: {}", e))? + .error_for_status() + .map_err(|e| format!("Plex ping status error: {}", e))? + .text() + .await + .map_err(|e| format!("Failed to read Plex ping response: {}", e))?; + + Ok(parse_server_info(&xml)) +} + +#[tauri::command] +pub async fn plex_get_music_sections( + base_url: String, + token: String, +) -> Result, String> { + let client = build_plex_client()?; + let base = normalize_base_url(&base_url); + let url = with_token(&format!("{base}/library/sections"), &token); + + let xml = client + .get(url) + .send() + .await + .map_err(|e| format!("Plex sections request failed: {}", e))? + .error_for_status() + .map_err(|e| format!("Plex sections status error: {}", e))? + .text() + .await + .map_err(|e| format!("Failed to read Plex sections response: {}", e))?; + + Ok(parse_music_sections(&xml)) +} + +#[tauri::command] +pub async fn plex_get_section_tracks( + base_url: String, + token: String, + section_key: String, + limit: Option, +) -> Result, String> { + let client = build_plex_client()?; + let base = normalize_base_url(&base_url); + let list_url = format!("{base}/library/sections/{section_key}/all?type=10"); + let url = with_token(&list_url, &token); + + let xml = client + .get(url) + .send() + .await + .map_err(|e| format!("Plex tracks request failed: {}", e))? + .error_for_status() + .map_err(|e| format!("Plex tracks status error: {}", e))? + .text() + .await + .map_err(|e| format!("Failed to read Plex tracks response: {}", e))?; + + // Treat limit=0 as "no limit" to match frontend semantics. + let effective_limit = limit.filter(|v| *v > 0); + Ok(parse_tracks(&xml, effective_limit)) +} + +#[tauri::command] +pub async fn plex_get_track_metadata( + base_url: String, + token: String, + rating_key: String, +) -> Result { + let client = build_plex_client()?; + let base = normalize_base_url(&base_url); + let detail_url = format!("{base}/library/metadata/{rating_key}"); + let url = with_token(&detail_url, &token); + + let xml = client + .get(url) + .send() + .await + .map_err(|e| format!("Plex track metadata request failed: {}", e))? + .error_for_status() + .map_err(|e| format!("Plex track metadata status error: {}", e))? + .text() + .await + .map_err(|e| format!("Failed to read Plex track metadata response: {}", e))?; + + parse_tracks(&xml, Some(1)) + .into_iter() + .next() + .ok_or_else(|| "Plex track metadata not found".to_string()) +} + +#[tauri::command] +pub async fn plex_auth_pin_start(client_identifier: String) -> Result { + let client = build_plex_auth_client(&client_identifier)?; + let pin = client + .post("https://plex.tv/api/v2/pins?strong=false") + .send() + .await + .map_err(|e| format!("Plex auth pin request failed: {}", e))? + .error_for_status() + .map_err(|e| format!("Plex auth pin status error: {}", e))? + .json::() + .await + .map_err(|e| format!("Failed to parse Plex auth pin response: {}", e))?; + + Ok(PlexPinStartResult { + pin_id: pin.id, + code: pin.code.clone(), + auth_url: build_plex_auth_url(), + expires_in: pin.expires_in, + }) +} + +#[tauri::command] +pub async fn plex_auth_pin_check( + client_identifier: String, + pin_id: u64, + code: Option, +) -> Result { + let client = build_plex_auth_client(&client_identifier)?; + let base_url = format!("https://plex.tv/api/v2/pins/{}", pin_id); + let request = if let Some(pin_code) = code { + client.get(format!("{}?code={}", base_url, pin_code)) + } else { + client.get(base_url) + }; + let pin = request + .send() + .await + .map_err(|e| format!("Plex auth pin check request failed: {}", e))? + .error_for_status() + .map_err(|e| format!("Plex auth pin check status error: {}", e))? + .json::() + .await + .map_err(|e| format!("Failed to parse Plex auth pin check response: {}", e))?; + + Ok(PlexPinCheckResult { + authorized: pin.auth_token.is_some(), + expired: pin.expires_in == Some(0), + auth_token: pin.auth_token, + expires_in: pin.expires_in, + }) +} + +#[tauri::command] +pub async fn plex_open_auth_url(url: String) -> Result<(), String> { + open::that(&url).map_err(|e| format!("Failed to open browser: {}", e)) +} + +#[tauri::command] +pub fn plex_cache_get_sections() -> Result, String> { + let conn = open_plex_cache_db()?; + let mut stmt = conn + .prepare( + "SELECT section_key, title + FROM plex_cache_sections + ORDER BY title COLLATE NOCASE", + ) + .map_err(|e| format!("Failed to prepare Plex cache sections query: {}", e))?; + + let rows = stmt + .query_map([], |row| { + Ok(PlexMusicSection { + key: row.get(0)?, + title: row.get(1)?, + }) + }) + .map_err(|e| format!("Failed to query Plex cache sections: {}", e))?; + + let mut sections = Vec::new(); + for row in rows { + sections.push(row.map_err(|e| format!("Failed to read Plex cache section row: {}", e))?); + } + Ok(sections) +} + +#[tauri::command] +pub fn plex_cache_save_sections( + server_id: Option, + sections: Vec, +) -> Result { + let mut conn = open_plex_cache_db()?; + let tx = conn + .transaction() + .map_err(|e| format!("Failed to start Plex cache sections transaction: {}", e))?; + + let now = now_epoch_secs(); + for section in §ions { + tx.execute( + "INSERT INTO plex_cache_sections (section_key, title, server_id, updated_at) + VALUES (?1, ?2, ?3, ?4) + ON CONFLICT(section_key) DO UPDATE SET + title = excluded.title, + server_id = excluded.server_id, + updated_at = excluded.updated_at", + params![section.key, section.title, server_id, now], + ) + .map_err(|e| format!("Failed to upsert Plex cache section: {}", e))?; + } + + tx.commit() + .map_err(|e| format!("Failed to commit Plex cache sections transaction: {}", e))?; + Ok(sections.len()) +} + +#[tauri::command] +pub fn plex_cache_get_tracks( + section_key: Option, + limit: Option, +) -> Result, String> { + let conn = open_plex_cache_db()?; + let max = limit.unwrap_or(200) as i64; + let mut tracks = Vec::new(); + + if let Some(section) = section_key { + let mut stmt = conn + .prepare( + "SELECT rating_key, title, artist, album, duration_ms, artwork_path, part_key, container, + codec, channels, bitrate_kbps, sampling_rate_hz, bit_depth + FROM plex_cache_tracks + WHERE section_key = ?1 + ORDER BY artist COLLATE NOCASE, album COLLATE NOCASE, title COLLATE NOCASE + LIMIT ?2", + ) + .map_err(|e| format!("Failed to prepare Plex cache tracks query: {}", e))?; + let rows = stmt + .query_map(params![section, max], |row| { + Ok(PlexTrack { + rating_key: row.get(0)?, + title: decode_xml_entities(row.get::<_, String>(1)?.trim()), + artist: row + .get::<_, Option>(2)? + .map(|v| decode_xml_entities(v.trim())), + album: row + .get::<_, Option>(3)? + .map(|v| decode_xml_entities(v.trim())), + duration_ms: row.get(4)?, + artwork_path: row.get(5)?, + part_key: row.get(6)?, + container: row.get(7)?, + codec: row.get(8)?, + channels: row.get(9)?, + bitrate_kbps: row.get(10)?, + sampling_rate_hz: row.get(11)?, + bit_depth: row.get(12)?, + }) + }) + .map_err(|e| format!("Failed to query Plex cache tracks: {}", e))?; + for row in rows { + tracks.push(row.map_err(|e| format!("Failed to read Plex cache track row: {}", e))?); + } + } else { + let mut stmt = conn + .prepare( + "SELECT rating_key, title, artist, album, duration_ms, artwork_path, part_key, container, + codec, channels, bitrate_kbps, sampling_rate_hz, bit_depth + FROM plex_cache_tracks + ORDER BY updated_at DESC + LIMIT ?1", + ) + .map_err(|e| format!("Failed to prepare Plex cache tracks query: {}", e))?; + let rows = stmt + .query_map(params![max], |row| { + Ok(PlexTrack { + rating_key: row.get(0)?, + title: decode_xml_entities(row.get::<_, String>(1)?.trim()), + artist: row + .get::<_, Option>(2)? + .map(|v| decode_xml_entities(v.trim())), + album: row + .get::<_, Option>(3)? + .map(|v| decode_xml_entities(v.trim())), + duration_ms: row.get(4)?, + artwork_path: row.get(5)?, + part_key: row.get(6)?, + container: row.get(7)?, + codec: row.get(8)?, + channels: row.get(9)?, + bitrate_kbps: row.get(10)?, + sampling_rate_hz: row.get(11)?, + bit_depth: row.get(12)?, + }) + }) + .map_err(|e| format!("Failed to query Plex cache tracks: {}", e))?; + for row in rows { + tracks.push(row.map_err(|e| format!("Failed to read Plex cache track row: {}", e))?); + } + } + + Ok(tracks) +} + +#[tauri::command] +pub fn plex_cache_get_albums() -> Result, String> { + let conn = open_plex_cache_db()?; + let mut stmt = conn + .prepare( + "SELECT artist, album, duration_ms, artwork_path, container, sampling_rate_hz, bit_depth + FROM plex_cache_tracks", + ) + .map_err(|e| format!("Failed to prepare Plex cache album aggregation query: {}", e))?; + + let rows = stmt + .query_map([], |row| { + Ok(( + row.get::<_, Option>(0)?, + row.get::<_, Option>(1)?, + row.get::<_, Option>(2)?, + row.get::<_, Option>(3)?, + row.get::<_, Option>(4)?, + row.get::<_, Option>(5)?, + row.get::<_, Option>(6)?, + )) + }) + .map_err(|e| format!("Failed to query Plex cache tracks for album aggregation: {}", e))?; + + let mut grouped: HashMap = HashMap::new(); + + for row in rows { + let (artist_opt, album_opt, duration_ms_opt, artwork_path, container, sampling_rate_hz_opt, bit_depth_opt) = + row.map_err(|e| format!("Failed to read Plex cache aggregation row: {}", e))?; + let artist = artist_opt + .map(|v| decode_xml_entities(v.trim())) + .filter(|v| !v.is_empty()) + .unwrap_or_else(|| "Unknown Artist".to_string()); + let album_raw = album_opt + .map(|v| decode_xml_entities(v.trim())) + .filter(|v| !v.is_empty()) + .unwrap_or_else(|| "Unknown Album".to_string()); + let album = normalize_album_title(Some(&artist), &album_raw); + let album_key = plex_album_key(&artist, &album); + + let entry = grouped.entry(album_key.clone()).or_insert_with(|| PlexCachedAlbum { + id: album_key.clone(), + title: album.clone(), + artist: artist.clone(), + artwork_path: artwork_path.clone(), + track_count: 0, + total_duration_secs: 0, + format: container.clone().unwrap_or_else(|| "flac".to_string()), + bit_depth: bit_depth_opt.map(|v| v as u32), + sample_rate: sampling_rate_hz_opt.map(|v| v as u32).unwrap_or(44100), + source: "plex".to_string(), + }); + + entry.track_count += 1; + if let Some(duration_ms) = duration_ms_opt { + entry.total_duration_secs += (duration_ms as u64) / 1000; + } + if entry.artwork_path.is_none() && artwork_path.is_some() { + entry.artwork_path = artwork_path; + } + if let Some(container_value) = container { + if entry.format == "flac" || entry.format.is_empty() { + entry.format = container_value; + } + } + if let Some(rate) = sampling_rate_hz_opt { + let rate_u = rate as u32; + if rate_u > entry.sample_rate { + entry.sample_rate = rate_u; + } + } + if let Some(depth) = bit_depth_opt { + let depth_u = depth as u32; + if depth_u > entry.bit_depth.unwrap_or(0) { + entry.bit_depth = Some(depth_u); + } + } + } + + let mut albums: Vec = grouped.into_values().collect(); + albums.sort_by(|a, b| { + let artist_cmp = a.artist.to_lowercase().cmp(&b.artist.to_lowercase()); + if artist_cmp != std::cmp::Ordering::Equal { + return artist_cmp; + } + a.title.to_lowercase().cmp(&b.title.to_lowercase()) + }); + Ok(albums) +} + +#[tauri::command] +pub fn plex_cache_get_album_tracks(album_key: String) -> Result, String> { + let conn = open_plex_cache_db()?; + let mut stmt = conn + .prepare( + "SELECT rating_key, title, artist, album, duration_ms, container, bit_depth, sampling_rate_hz, artwork_path + FROM plex_cache_tracks + ORDER BY title COLLATE NOCASE", + ) + .map_err(|e| format!("Failed to prepare Plex cache album tracks query: {}", e))?; + + let rows = stmt + .query_map([], |row| { + Ok(( + row.get::<_, String>(0)?, + row.get::<_, String>(1)?, + row.get::<_, Option>(2)?, + row.get::<_, Option>(3)?, + row.get::<_, Option>(4)?, + row.get::<_, Option>(5)?, + row.get::<_, Option>(6)?, + row.get::<_, Option>(7)?, + row.get::<_, Option>(8)?, + )) + }) + .map_err(|e| format!("Failed to query Plex cache album tracks: {}", e))?; + + let mut tracks = Vec::new(); + for row in rows { + let (rating_key, title, artist_opt, album_opt, duration_ms_opt, container_opt, bit_depth_opt, sampling_rate_opt, artwork_path) = + row.map_err(|e| format!("Failed to read Plex cache album track row: {}", e))?; + let artist = artist_opt + .map(|v| decode_xml_entities(v.trim())) + .filter(|v| !v.is_empty()) + .unwrap_or_else(|| "Unknown Artist".to_string()); + let album_raw = album_opt + .map(|v| decode_xml_entities(v.trim())) + .filter(|v| !v.is_empty()) + .unwrap_or_else(|| "Unknown Album".to_string()); + let album = normalize_album_title(Some(&artist), &album_raw); + if plex_album_key(&artist, &album) != album_key { + continue; + } + tracks.push(PlexCachedTrack { + id: playback_track_id(&rating_key), + rating_key, + title: decode_xml_entities(title.trim()), + artist, + album, + duration_secs: duration_ms_opt.map(|v| (v as u64) / 1000).unwrap_or(0), + format: container_opt.unwrap_or_else(|| "flac".to_string()), + bit_depth: bit_depth_opt.map(|v| v as u32), + sample_rate: sampling_rate_opt.map(|v| v as u32).unwrap_or(44100), + artwork_path, + source: "plex".to_string(), + album_key: album_key.clone(), + }); + } + Ok(tracks) +} + +#[tauri::command] +pub fn plex_cache_search_tracks(query: String, limit: Option) -> Result, String> { + let conn = open_plex_cache_db()?; + let max = limit.unwrap_or(5000) as i64; + let needle = format!("%{}%", query.to_lowercase()); + let mut stmt = conn + .prepare( + "SELECT rating_key, title, artist, album, duration_ms, container, bit_depth, sampling_rate_hz, artwork_path + FROM plex_cache_tracks + WHERE ?1 = '' OR + lower(title) LIKE ?2 OR + lower(COALESCE(artist, '')) LIKE ?2 OR + lower(COALESCE(album, '')) LIKE ?2 + ORDER BY artist COLLATE NOCASE, album COLLATE NOCASE, title COLLATE NOCASE + LIMIT ?3", + ) + .map_err(|e| format!("Failed to prepare Plex cache search query: {}", e))?; + + let rows = stmt + .query_map(params![query.trim(), needle, max], |row| { + Ok(( + row.get::<_, String>(0)?, + row.get::<_, String>(1)?, + row.get::<_, Option>(2)?, + row.get::<_, Option>(3)?, + row.get::<_, Option>(4)?, + row.get::<_, Option>(5)?, + row.get::<_, Option>(6)?, + row.get::<_, Option>(7)?, + row.get::<_, Option>(8)?, + )) + }) + .map_err(|e| format!("Failed to query Plex cache search tracks: {}", e))?; + + let mut tracks = Vec::new(); + for row in rows { + let (rating_key, title, artist_opt, album_opt, duration_ms_opt, container_opt, bit_depth_opt, sampling_rate_opt, artwork_path) = + row.map_err(|e| format!("Failed to read Plex cache search row: {}", e))?; + let artist = artist_opt + .map(|v| decode_xml_entities(v.trim())) + .filter(|v| !v.is_empty()) + .unwrap_or_else(|| "Unknown Artist".to_string()); + let album_raw = album_opt + .map(|v| decode_xml_entities(v.trim())) + .filter(|v| !v.is_empty()) + .unwrap_or_else(|| "Unknown Album".to_string()); + let album = normalize_album_title(Some(&artist), &album_raw); + tracks.push(PlexCachedTrack { + id: playback_track_id(&rating_key), + rating_key: rating_key.clone(), + title: decode_xml_entities(title.trim()), + artist: artist.clone(), + album: album.clone(), + duration_secs: duration_ms_opt.map(|v| (v as u64) / 1000).unwrap_or(0), + format: container_opt.unwrap_or_else(|| "flac".to_string()), + bit_depth: bit_depth_opt.map(|v| v as u32), + sample_rate: sampling_rate_opt.map(|v| v as u32).unwrap_or(44100), + artwork_path, + source: "plex".to_string(), + album_key: plex_album_key(&artist, &album), + }); + } + Ok(tracks) +} + +#[tauri::command] +pub fn plex_cache_save_tracks( + server_id: Option, + section_key: String, + tracks: Vec, +) -> Result { + let mut conn = open_plex_cache_db()?; + let tx = conn + .transaction() + .map_err(|e| format!("Failed to start Plex cache tracks transaction: {}", e))?; + + tx.execute( + "DELETE FROM plex_cache_tracks WHERE section_key = ?1", + params![section_key], + ) + .map_err(|e| format!("Failed to clear old Plex cache tracks for section: {}", e))?; + + let now = now_epoch_secs(); + for track in &tracks { + tx.execute( + "INSERT INTO plex_cache_tracks + (rating_key, section_key, server_id, title, artist, album, duration_ms, artwork_path, + part_key, container, codec, channels, bitrate_kbps, sampling_rate_hz, bit_depth, updated_at) + VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16) + ON CONFLICT(rating_key) DO UPDATE SET + section_key = excluded.section_key, + server_id = excluded.server_id, + title = excluded.title, + artist = excluded.artist, + album = excluded.album, + duration_ms = excluded.duration_ms, + artwork_path = excluded.artwork_path, + part_key = excluded.part_key, + container = excluded.container, + codec = excluded.codec, + channels = excluded.channels, + bitrate_kbps = excluded.bitrate_kbps, + sampling_rate_hz = excluded.sampling_rate_hz, + bit_depth = excluded.bit_depth, + updated_at = excluded.updated_at", + params![ + track.rating_key, + section_key, + server_id, + track.title, + track.artist, + track.album, + track.duration_ms.map(|v| v as i64), + track.artwork_path, + track.part_key, + track.container, + track.codec, + track.channels.map(|v| v as i64), + track.bitrate_kbps.map(|v| v as i64), + track.sampling_rate_hz.map(|v| v as i64), + track.bit_depth.map(|v| v as i64), + now, + ], + ) + .map_err(|e| format!("Failed to upsert Plex cache track: {}", e))?; + } + + tx.commit() + .map_err(|e| format!("Failed to commit Plex cache tracks transaction: {}", e))?; + Ok(tracks.len()) +} + +#[tauri::command] +pub fn plex_cache_update_track_quality(updates: Vec) -> Result { + if updates.is_empty() { + return Ok(0); + } + + let mut conn = open_plex_cache_db()?; + let tx = conn + .transaction() + .map_err(|e| format!("Failed to start Plex cache quality update transaction: {}", e))?; + + let now = now_epoch_secs(); + let mut updated_rows = 0usize; + for update in &updates { + let affected = tx + .execute( + "UPDATE plex_cache_tracks + SET container = COALESCE(?2, container), + sampling_rate_hz = COALESCE(?3, sampling_rate_hz), + bit_depth = COALESCE(?4, bit_depth), + updated_at = ?5 + WHERE rating_key = ?1", + params![ + update.rating_key, + update.container, + update.sampling_rate_hz.map(|v| v as i64), + update.bit_depth.map(|v| v as i64), + now, + ], + ) + .map_err(|e| format!("Failed to update Plex cache track quality: {}", e))?; + updated_rows += affected; + } + + tx.commit() + .map_err(|e| format!("Failed to commit Plex cache quality update transaction: {}", e))?; + + Ok(updated_rows) +} + +#[tauri::command] +pub fn plex_cache_clear() -> Result<(), String> { + let conn = open_plex_cache_db()?; + conn.execute("DELETE FROM plex_cache_tracks", []) + .map_err(|e| format!("Failed to clear Plex cache tracks: {}", e))?; + conn.execute("DELETE FROM plex_cache_sections", []) + .map_err(|e| format!("Failed to clear Plex cache sections: {}", e))?; + Ok(()) +} + +#[tauri::command] +pub async fn plex_play_track( + base_url: String, + token: String, + rating_key: String, + app_state: State<'_, AppState>, +) -> Result { + let client = build_plex_client()?; + let base = normalize_base_url(&base_url); + + let metadata_url = with_token(&format!("{base}/library/metadata/{rating_key}"), &token); + let metadata_xml = client + .get(metadata_url) + .send() + .await + .map_err(|e| format!("Plex metadata request failed: {}", e))? + .error_for_status() + .map_err(|e| format!("Plex metadata status error: {}", e))? + .text() + .await + .map_err(|e| format!("Failed to read Plex metadata response: {}", e))?; + + let mut tracks = parse_tracks(&metadata_xml, Some(1)); + let track = tracks + .pop() + .ok_or_else(|| format!("Track {rating_key} not found in Plex metadata"))?; + + let part_key = track + .part_key + .clone() + .ok_or_else(|| format!("Track {rating_key} does not include a playable Part key"))?; + + let part_url = with_token(&format!("{base}{part_key}"), &token); + let part_response = client + .get(&part_url) + .send() + .await + .map_err(|e| format!("Plex part request failed: {}", e))? + .error_for_status() + .map_err(|e| format!("Plex part status error: {}", e))?; + + let content_type = part_response + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .map(|s| s.to_string()); + + let bytes = part_response + .bytes() + .await + .map_err(|e| format!("Failed to read Plex media bytes: {}", e))?; + + let playback_id = playback_track_id(&rating_key); + app_state + .player + .play_data(bytes.to_vec(), playback_id) + .map_err(|e| format!("Failed to play Plex track: {}", e))?; + + Ok(PlexPlayResult { + rating_key, + part_key: part_key.clone(), + part_url, + bytes: bytes.len(), + direct_play_confirmed: is_direct_part_key(&part_key), + content_type, + sampling_rate_hz: track.sampling_rate_hz, + bit_depth: track.bit_depth, + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parses_music_sections() { + let xml = r#" + + + "#; + let sections = parse_music_sections(xml); + assert_eq!(sections.len(), 1); + assert_eq!(sections[0].key, "1"); + } + + #[test] + fn parses_tracks_with_stream_audio_metadata() { + let xml = r#" + + + + + + + "#; + let tracks = parse_tracks(xml, Some(10)); + assert_eq!(tracks.len(), 1); + assert_eq!(tracks[0].rating_key, "42"); + assert_eq!( + tracks[0].part_key.as_deref(), + Some("/library/parts/999/file.flac") + ); + assert_eq!(tracks[0].artwork_path.as_deref(), Some("/library/metadata/42/thumb/1")); + assert_eq!(tracks[0].sampling_rate_hz, Some(96000)); + assert_eq!(tracks[0].bit_depth, Some(24)); + } + + #[test] + fn direct_part_key_detection() { + assert!(is_direct_part_key("/library/parts/1234/file.flac")); + assert!(!is_direct_part_key("/music/:/transcode/universal/start")); + } + + #[test] + fn playback_track_id_prefers_numeric_rating_key() { + assert_eq!(playback_track_id("48012"), 48012); + } +} diff --git a/src-tauri/src/queue/mod.rs b/src-tauri/src/queue/mod.rs index 147d782af..98d1235c7 100644 --- a/src-tauri/src/queue/mod.rs +++ b/src-tauri/src/queue/mod.rs @@ -33,6 +33,9 @@ pub struct QueueTrack { /// Whether the track is streamable on Qobuz (false = removed/unavailable) #[serde(default = "default_streamable")] pub streamable: bool, + /// Optional origin source (e.g. "qobuz", "local", "plex") + #[serde(default)] + pub source: Option, } fn default_streamable() -> bool { diff --git a/src-tauri/src/session_store/mod.rs b/src-tauri/src/session_store/mod.rs index 903fed019..6798f5f29 100644 --- a/src-tauri/src/session_store/mod.rs +++ b/src-tauri/src/session_store/mod.rs @@ -27,6 +27,8 @@ pub struct PersistedQueueTrack { pub artist_id: Option, #[serde(default = "default_streamable")] pub streamable: bool, + #[serde(default)] + pub source: Option, } /// Represents the full persisted session state @@ -110,7 +112,8 @@ impl SessionStore { artwork_url TEXT, hires INTEGER NOT NULL DEFAULT 0, bit_depth INTEGER, - sample_rate REAL + sample_rate REAL, + source TEXT ); -- Insert default row if not exists @@ -158,6 +161,22 @@ impl SessionStore { ); } + let has_source: bool = conn + .query_row( + "SELECT COUNT(*) FROM pragma_table_info('queue_tracks') WHERE name = 'source'", + [], + |row| row.get(0), + ) + .unwrap_or(0) > 0; + + if !has_source { + let _ = conn.execute_batch( + " + ALTER TABLE queue_tracks ADD COLUMN source TEXT; + " + ); + } + Ok(Self { conn }) } @@ -181,8 +200,8 @@ impl SessionStore { // Insert queue tracks for (pos, track) in session.queue_tracks.iter().enumerate() { if let Err(e) = self.conn.execute( - "INSERT INTO queue_tracks (position, track_id, title, artist, album, duration_secs, artwork_url, hires, bit_depth, sample_rate, is_local, album_id, artist_id) - VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13)", + "INSERT INTO queue_tracks (position, track_id, title, artist, album, duration_secs, artwork_url, hires, bit_depth, sample_rate, is_local, album_id, artist_id, source) + VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14)", params![ pos as i64, track.id as i64, @@ -197,6 +216,7 @@ impl SessionStore { track.is_local as i64, track.album_id, track.artist_id.map(|v| v as i64), + track.source, ], ) { let _ = self.conn.execute("ROLLBACK", []); @@ -258,7 +278,7 @@ impl SessionStore { // Load queue tracks let mut stmt = self.conn - .prepare("SELECT track_id, title, artist, album, duration_secs, artwork_url, hires, bit_depth, sample_rate, is_local, album_id, artist_id FROM queue_tracks ORDER BY position") + .prepare("SELECT track_id, title, artist, album, duration_secs, artwork_url, hires, bit_depth, sample_rate, is_local, album_id, artist_id, source FROM queue_tracks ORDER BY position") .map_err(|e| format!("Failed to prepare queue query: {}", e))?; let tracks: Vec = stmt @@ -277,6 +297,7 @@ impl SessionStore { album_id: row.get(10)?, artist_id: row.get::<_, Option>(11)?.map(|v| v as u64), streamable: true, // Default to true for persisted tracks + source: row.get(12)?, }) }) .map_err(|e| format!("Failed to query queue tracks: {}", e))? diff --git a/src/lib/components/AlbumCard.svelte b/src/lib/components/AlbumCard.svelte index aea808fa3..d9346e947 100644 --- a/src/lib/components/AlbumCard.svelte +++ b/src/lib/components/AlbumCard.svelte @@ -35,8 +35,8 @@ onOpenContainingFolder?: () => void; onReDownloadAlbum?: () => void; downloadStateVersion?: number; - /** Source badge for local library: 'user' for local files, 'qobuz_download' for Qobuz offline */ - sourceBadge?: 'user' | 'qobuz_download'; + /** Source badge for local library: 'user' | 'qobuz_download' | 'plex' */ + sourceBadge?: 'user' | 'qobuz_download' | 'plex'; artistId?: number; onArtistClick?: (artistId: number) => void; } @@ -216,7 +216,7 @@ {#if !imageError && artwork} - {title} + {title} {/if} @@ -276,9 +276,15 @@ {#if sourceBadge} -
+
{#if sourceBadge === 'user'} + {:else if sourceBadge === 'plex'} + Plex {:else} Qobuz {/if} @@ -337,7 +343,7 @@ overflow: hidden; } - .artwork-container img { + .artwork-container .artwork-image { position: absolute; inset: 0; width: 100%; @@ -392,6 +398,7 @@ width: 24px; height: 24px; color: var(--text-secondary); + border-radius: 4px; } .source-badge.local-badge { @@ -405,6 +412,13 @@ height: 24px; } + .source-badge .plex-logo-icon { + width: 18px; + height: 18px; + object-fit: contain; + filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.45)); + } + .action-overlay { position: absolute; inset: -1px; diff --git a/src/lib/components/PlaylistModal.svelte b/src/lib/components/PlaylistModal.svelte index 3f1df6565..7f186c79a 100644 --- a/src/lib/components/PlaylistModal.svelte +++ b/src/lib/components/PlaylistModal.svelte @@ -356,7 +356,7 @@ const tracks = await invoke>('library_get_tracks_by_ids', { trackIds }); - localTrackPaths = tracks.map(t => t.file_path); + localTrackPaths = tracks.map(track => track.file_path); } await invoke('add_tracks_to_pending_playlist', { @@ -419,7 +419,7 @@ const tracks = await invoke>('library_get_tracks_by_ids', { trackIds }); - localTrackPaths = tracks.map(t => t.file_path); + localTrackPaths = tracks.map(track => track.file_path); } // Create pending playlist for sync when back online diff --git a/src/lib/components/TrackRow.svelte b/src/lib/components/TrackRow.svelte index 4c5d75950..515865b0e 100644 --- a/src/lib/components/TrackRow.svelte +++ b/src/lib/components/TrackRow.svelte @@ -1,6 +1,7 @@