diff --git a/crates/qbz-core/src/core.rs b/crates/qbz-core/src/core.rs index b32effe27..39aa64adb 100644 --- a/crates/qbz-core/src/core.rs +++ b/crates/qbz-core/src/core.rs @@ -1197,6 +1197,29 @@ impl QbzCore { .map_err(CoreError::Api) } + /// Follow (subscribe to) a Qobuz playlist so it appears in the user's Qobuz + /// account across every Qobuz client (and in their user-playlists list). + pub async fn subscribe_playlist(&self, playlist_id: u64) -> Result<(), CoreError> { + let client = self.client.read().await; + let client = client.as_ref().ok_or(CoreError::NotInitialized)?; + + client + .subscribe_playlist(playlist_id) + .await + .map_err(CoreError::Api) + } + + /// Unfollow (unsubscribe from) a Qobuz playlist. + pub async fn unsubscribe_playlist(&self, playlist_id: u64) -> Result<(), CoreError> { + let client = self.client.read().await; + let client = client.as_ref().ok_or(CoreError::NotInitialized)?; + + client + .unsubscribe_playlist(playlist_id) + .await + .map_err(CoreError::Api) + } + /// Delete a playlist pub async fn delete_playlist(&self, playlist_id: u64) -> Result<(), CoreError> { let client = self.client.read().await; diff --git a/crates/qbz-ui/ui/playlist/PlaylistView.slint b/crates/qbz-ui/ui/playlist/PlaylistView.slint index b71d6fafc..ede9be989 100644 --- a/crates/qbz-ui/ui/playlist/PlaylistView.slint +++ b/crates/qbz-ui/ui/playlist/PlaylistView.slint @@ -590,6 +590,34 @@ export component PlaylistView inherits Rectangle { } } } + // Follow on Qobuz (subscribe) — only a non-owned Qobuz + // playlist; syncs to the user's Qobuz account. + if !PlaylistState.is-local && !PlaylistState.is-owner: VerticalLayout { + horizontal-stretch: 0; + alignment: center; + CircleAction { + icon: @image-url("../assets/icons/user-plus.svg"); + active: PlaylistState.is-following; + tooltip: PlaylistState.is-following ? @tr("Following on Qobuz") : @tr("Follow on Qobuz"); + clicked => { + root.media-action("playlist", PlaylistState.id, "follow"); + } + } + } + // Copy to your library — clone a non-owned Qobuz playlist + // into your own playlists (hidden once copied). + if !PlaylistState.is-local && !PlaylistState.is-owner + && !PlaylistState.is-copied: VerticalLayout { + horizontal-stretch: 0; + alignment: center; + CircleAction { + icon: @image-url("../assets/icons/copy.svg"); + tooltip: @tr("Copy to your library"); + clicked => { + root.media-action("playlist", PlaylistState.id, "copy"); + } + } + } // Make the whole playlist available offline — // resolves Qobuz membership, hidden for LOCAL ones. if !PlaylistState.is-local: VerticalLayout { diff --git a/crates/qbz-ui/ui/state.slint b/crates/qbz-ui/ui/state.slint index bc264387c..ee8aa12a4 100644 --- a/crates/qbz-ui/ui/state.slint +++ b/crates/qbz-ui/ui/state.slint @@ -1860,6 +1860,12 @@ export global PlaylistState { in property is-favorite: false; // True when the playlist is one of the user's own (editable). in property is-owner: false; + // Followed (subscribed) on Qobuz — an account-level follow that syncs + // across Qobuz clients, distinct from the internal favorite heart. + in property is-following: false; + // Already copied into the user's own playlists this session (hides the + // Copy action so it isn't duplicated). + in property is-copied: false; // LOCAL playlist (id "local:", library.db entity — never on // Qobuz). Qobuz-bound header actions (favorite / cache) hide; the // "Upload to Qobuz" action appears when not offline-only. diff --git a/crates/qbz/src/main.rs b/crates/qbz/src/main.rs index 0d07c5146..222c7e2d4 100644 --- a/crates/qbz/src/main.rs +++ b/crates/qbz/src/main.rs @@ -2826,10 +2826,20 @@ fn navigate_playlist( // plex rows = tokenized Plex thumbs. let (http_jobs, local_jobs, plex_jobs) = playlist::artwork_jobs(&data); let pid = data.id.clone(); + // Seed the INTERNAL favorite heart from the library db (the open + // handler otherwise only sets is_owner, so the heart was always + // un-filled on open). + let fav = pid.parse::().ok().is_some_and(|id| { + crate::library_db::with_db(|db| db.get_favorite_playlist_ids()) + .unwrap_or_default() + .contains(&id) + }); let _ = weak.upgrade_in_event_loop(move |w| { playlist::apply(&w, data); let owned = sidebar::contains(&w, &pid); - w.global::().set_is_owner(owned); + let st = w.global::(); + st.set_is_owner(owned); + st.set_is_favorite(fav); }); if !http_jobs.is_empty() { artwork::spawn_loads(http_jobs, weak.clone(), image_cache.clone()); @@ -9002,27 +9012,146 @@ fn main() -> Result<(), Box> { } } ("playlist", "favorite") => { - // Favorite/unfavorite the open playlist. + // Favorite/unfavorite the open playlist. This is an INTERNAL + // qbz library flag (Qobuz /favorite/create does NOT accept + // playlist_ids), mirroring the Favorites-card toggle and + // Tauri's v2_playlist_set_favorite. Surfaced in + // Favorites>Playlists + Library>Playlists. if let Some(w) = weak.upgrade() { let pid = w.global::().get_id().to_string(); - // LOCAL playlists have no Qobuz favorite — the UI - // hides the button; guard the call path too (the - // favorite endpoint takes the id as a string, so a - // "local:" id COULD otherwise leak through). if local_playlist::is_local_id(&pid) { return; } + let Ok(pid_u64) = pid.parse::() else { + return; + }; let was_fav = w.global::().get_is_favorite(); - w.global::().set_is_favorite(!was_fav); + let new_fav = !was_fav; + w.global::().set_is_favorite(new_fav); + let weak2 = weak.clone(); + handle.spawn_blocking(move || { + let ok = crate::library_db::with_db(|db| { + db.set_playlist_favorite(pid_u64, new_fav) + }); + if !matches!(ok, Some(Ok(()))) { + log::error!("[qbz-slint] toggle playlist favorite failed"); + let _ = weak2.upgrade_in_event_loop(move |w| { + w.global::().set_is_favorite(was_fav); + }); + } + }); + } + } + ("playlist", "copy") => { + // Copy a Qobuz playlist into the user's own playlists + // (create + add every track). INTERNAL — mirrors Tauri's + // (mis-named) v2_subscribe_playlist. The copy is owned, so it + // shows up in Library>Playlists via get_user_playlists. + if let Some(w) = weak.upgrade() { + let pid = w.global::().get_id().to_string(); + if local_playlist::is_local_id(&pid) { + return; + } + let Ok(pid_u64) = pid.parse::() else { + return; + }; let runtime = runtime.clone(); + let weak2 = weak.clone(); handle.spawn(async move { - let res = if was_fav { - runtime.core().remove_favorite("playlist", &pid).await + let source = match runtime.core().get_playlist(pid_u64).await { + Ok(p) => p, + Err(e) => { + log::error!("[qbz-slint] copy playlist: get source failed: {e}"); + crate::toast::error_weak(&weak2, "Failed to copy playlist"); + return; + } + }; + let track_ids: Vec = source + .tracks + .as_ref() + .map(|t| t.items.iter().map(|track| track.id).collect()) + .unwrap_or_default(); + if track_ids.is_empty() { + crate::toast::error_weak(&weak2, "Playlist has no tracks to copy"); + return; + } + let attribution = format!( + "\n\n---\nOriginally curated by {} on Qobuz", + source.owner.name + ); + let new_description = match source.description { + Some(ref d) if !d.is_empty() => Some(format!("{d}{attribution}")), + _ => Some(attribution.trim_start().to_string()), + }; + let new_playlist = match runtime + .core() + .create_playlist(&source.name, new_description.as_deref(), false) + .await + { + Ok(p) => p, + Err(e) => { + log::error!("[qbz-slint] copy playlist: create failed: {e}"); + crate::toast::error_weak(&weak2, "Failed to copy playlist"); + return; + } + }; + if let Err(e) = runtime + .core() + .add_tracks_to_playlist(new_playlist.id, &track_ids) + .await + { + log::error!("[qbz-slint] copy playlist: add tracks failed: {e}"); + } + // Best-effort: carry over the source artwork. + if let Some(img) = + source.images.as_ref().and_then(|i| i.first()).cloned() + { + let new_id = new_playlist.id; + let _ = tokio::task::spawn_blocking(move || { + crate::library_db::with_db(|db| { + db.update_playlist_artwork(new_id, Some(img.as_str())) + }); + }) + .await; + } + let _ = weak2.upgrade_in_event_loop(move |w| { + w.global::().set_is_copied(true); + crate::toast::success(&w, "Copied to your library"); + }); + crate::playback::refresh_sidebar(true); + }); + } + } + ("playlist", "follow") => { + // Follow/unfollow on Qobuz (subscribe). Appears in the user's + // Qobuz account across clients and in Library>Playlists via + // get_user_playlists (as a non-owned subscribed playlist). + if let Some(w) = weak.upgrade() { + let pid = w.global::().get_id().to_string(); + if local_playlist::is_local_id(&pid) { + return; + } + let Ok(pid_u64) = pid.parse::() else { + return; + }; + let was_following = w.global::().get_is_following(); + let make = !was_following; + w.global::().set_is_following(make); + let runtime = runtime.clone(); + let weak2 = weak.clone(); + handle.spawn(async move { + let res = if make { + runtime.core().subscribe_playlist(pid_u64).await } else { - runtime.core().add_favorite("playlist", &pid).await + runtime.core().unsubscribe_playlist(pid_u64).await }; if let Err(e) = res { - log::error!("[qbz-slint] toggle playlist favorite failed: {e}"); + log::error!("[qbz-slint] toggle playlist follow failed: {e}"); + let _ = weak2.upgrade_in_event_loop(move |w| { + w.global::().set_is_following(was_following); + }); + } else { + crate::playback::refresh_sidebar(true); } }); } diff --git a/crates/qbz/src/playlist_manager.rs b/crates/qbz/src/playlist_manager.rs index c21c14d9b..9fde47dde 100644 --- a/crates/qbz/src/playlist_manager.rs +++ b/crates/qbz/src/playlist_manager.rs @@ -209,6 +209,38 @@ where }) .collect(); + // Surface INTERNAL favorites (hearted playlists) that are neither owned nor + // subscribed — they don't come back from get_user_playlists, so without this + // a favorited-but-not-followed Qobuz playlist would be invisible here. + // Online only (offline can't fetch them); mirrors Favorites>Playlists. + if !crate::offline_mode::engine().is_offline() { + let fav_ids = + crate::library_db::with_db(|db| db.get_favorite_playlist_ids()).unwrap_or_default(); + let known: std::collections::HashSet = playlists.iter().map(|p| p.id).collect(); + for fid in fav_ids { + if known.contains(&fid) { + continue; + } + if let Ok(p) = runtime.core().get_playlist(fid).await { + let s = settings.get(&fid).cloned().unwrap_or_default(); + playlists.push(PmPlaylist { + id: fid, + name: p.name.clone(), + tracks_count: p.tracks_count, + duration: p.duration, + local_count: local_counts.get(&fid).copied().unwrap_or(0), + play_count: play_counts.get(&fid).copied().unwrap_or(0), + is_favorite: true, + is_hidden: s.hidden, + folder_id: s.folder_id.filter(|f| folder_ids.contains(f)), + position: s.position, + cover_urls: cover_urls(&p), + offline_available: snapshot_available.contains(&fid), + }); + } + } + } + // D11.b — OFFLINE: the Qobuz fetch is gate-refused (empty), so the // reachable playlists are synthesized locally: the MIXED ones (>= 1 // local sidecar row) plus — B8 — the snapshot-available ones (>= 1