Files
lintunes/lintunes/gui/playlist_ops.py
T
trav b7f8761bd1 Import existing LinTunes project
Snapshot of the existing codebase before working through the TASKS.md
backlog. Real library data (data/) and the iTunes import fixture
(itunes-test-library/) are gitignored.
2026-06-26 21:12:01 -04:00

68 lines
2.4 KiB
Python

"""Shared playlist-add logic with a duplicate-confirmation prompt.
All GUI paths that add tracks to a playlist (sidebar drop, drop onto an open
playlist's track list, paste) route through here so the duplicate guard is
consistent for single- and multi-track adds.
"""
from PyQt6.QtWidgets import QMessageBox
def has_duplicates(track_ids, existing) -> bool:
"""Whether any of ``track_ids`` is already in ``existing``."""
existing = set(existing)
return any(tid in existing for tid in track_ids)
def filter_for_add(track_ids, existing, add_duplicates: bool) -> list[int]:
"""The ids to add, preserving order: all of them when ``add_duplicates``,
otherwise only the ones not already in ``existing``."""
if add_duplicates:
return list(track_ids)
existing = set(existing)
return [tid for tid in track_ids if tid not in existing]
def add_tracks_with_dup_check(manager, parent, pid, track_ids,
position=None) -> list[int]:
"""Add ``track_ids`` to playlist ``pid``, asking first if any already exist.
Returns the track ids actually added (empty if the user cancelled or there
was nothing to add).
"""
playlist = manager.library.playlists.get(pid)
if not playlist or not track_ids:
return []
existing = playlist.track_ids
if has_duplicates(track_ids, existing):
to_add = _resolve_duplicates(parent, track_ids, existing)
else:
to_add = list(track_ids)
if not to_add:
return []
manager.add_tracks_to_playlist(pid, to_add, position)
return to_add
def _resolve_duplicates(parent, track_ids, existing) -> list[int]:
"""Ask whether to re-add duplicates; return the resulting ids to add."""
box = QMessageBox(parent)
box.setIcon(QMessageBox.Icon.Question)
box.setWindowTitle("Duplicate songs")
box.setText("One or more songs are already in the playlist. "
"Add those ones again?")
add_again = box.addButton("Yes, add again",
QMessageBox.ButtonRole.AcceptRole)
skip = box.addButton("Skip duplicates", QMessageBox.ButtonRole.ActionRole)
box.setDefaultButton(skip)
box.exec()
clicked = box.clickedButton()
if clicked is add_again:
return filter_for_add(track_ids, existing, add_duplicates=True)
if clicked is skip:
return filter_for_add(track_ids, existing, add_duplicates=False)
return [] # dialog dismissed (Escape / close) -> add nothing