Files
LinedanceAfspiller/linedance-app/ui/library_manager.py
2026-04-11 00:38:04 +02:00

136 lines
5.0 KiB
Python

"""
library_manager.py — Dialog til at se og fjerne musikbiblioteker.
"""
from PyQt6.QtWidgets import (
QDialog, QVBoxLayout, QHBoxLayout, QLabel,
QPushButton, QListWidget, QListWidgetItem, QMessageBox,
)
from PyQt6.QtCore import Qt, pyqtSignal
class LibraryManagerDialog(QDialog):
library_removed = pyqtSignal(int) # library_id
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Administrer musikbiblioteker")
self.setMinimumWidth(500)
self.setMinimumHeight(320)
self._build_ui()
self._load()
def _build_ui(self):
layout = QVBoxLayout(self)
layout.setContentsMargins(16, 16, 16, 16)
layout.setSpacing(10)
lbl = QLabel("Aktive musikbiblioteker:")
lbl.setObjectName("track_meta")
layout.addWidget(lbl)
self._list = QListWidget()
layout.addWidget(self._list)
note = QLabel(
"Når du fjerner et bibliotek, slettes det fra overvågningen.\n"
"Sangene forbliver i databasen men markeres som manglende (⚠)."
)
note.setObjectName("result_count")
note.setWordWrap(True)
layout.addWidget(note)
btn_row = QHBoxLayout()
btn_add = QPushButton("+ Tilføj mappe")
btn_add.clicked.connect(self._add_folder)
btn_row.addWidget(btn_add)
btn_remove = QPushButton("✕ Fjern valgt")
btn_remove.clicked.connect(self._remove_selected)
btn_row.addWidget(btn_remove)
btn_scan = QPushButton("⟳ Scan alle")
btn_scan.setToolTip("Scan alle mapper for nye og ændrede filer")
btn_scan.clicked.connect(self._scan_all)
btn_row.addWidget(btn_scan)
btn_row.addStretch()
btn_close = QPushButton("Luk")
btn_close.clicked.connect(self.accept)
btn_row.addWidget(btn_close)
layout.addLayout(btn_row)
def _load(self):
self._list.clear()
try:
from local.local_db import get_libraries, get_db
libs = get_libraries(active_only=True) # kun aktive
for lib in libs:
from pathlib import Path
path = lib["path"]
exists = Path(path).exists()
last_scan = lib["last_full_scan"] or "aldrig"
if isinstance(last_scan, str) and len(last_scan) > 10:
last_scan = last_scan[:10]
with get_db() as conn:
count = conn.execute(
"SELECT COUNT(*) FROM songs WHERE library_id=? AND file_missing=0",
(lib["id"],)
).fetchone()[0]
exist_icon = "" if exists else " ⚠ mappe ikke fundet"
label = f"{path}{exist_icon}\n {count} sange · senest scannet: {last_scan}"
item = QListWidgetItem(label)
item.setData(Qt.ItemDataRole.UserRole, dict(lib))
if not exists:
from PyQt6.QtGui import QColor
item.setForeground(QColor("#5a6070"))
self._list.addItem(item)
except Exception as e:
print(f"Library manager load fejl: {e}")
def _scan_all(self):
mw = self.parent()
if hasattr(mw, "start_scan"):
mw.start_scan()
self._set_status("Scanning startet...")
def _set_status(self, text: str):
pass # kan udvides med statuslinje i dialogen
def _add_folder(self):
from PyQt6.QtWidgets import QFileDialog
folder = QFileDialog.getExistingDirectory(self, "Vælg musikmappe")
if folder:
mw = self.parent()
if hasattr(mw, "add_library_path"):
mw.add_library_path(folder)
# Genindlæs listen efter kort pause så DB er opdateret
from PyQt6.QtCore import QTimer
QTimer.singleShot(600, self._load)
def _remove_selected(self):
item = self._list.currentItem()
if not item:
return
lib = item.data(Qt.ItemDataRole.UserRole)
reply = QMessageBox.question(
self, "Fjern bibliotek",
f"Fjern overvågningen af:\n{lib['path']}\n\n"
"Sange i biblioteket forbliver i databasen men markeres som manglende.",
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
)
if reply == QMessageBox.StandardButton.Yes:
try:
mw = self.parent()
if hasattr(mw, "_watcher") and mw._watcher:
mw._watcher.remove_library(lib["id"])
else:
from local.local_db import remove_library
remove_library(lib["id"])
self.library_removed.emit(lib["id"])
if hasattr(mw, "_reload_library"):
mw._reload_library()
self._load()
except Exception as e:
QMessageBox.warning(self, "Fejl", f"Kunne ikke fjerne: {e}")