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

65 lines
2.1 KiB
Python

"""
scan_worker.py — Kører fuld biblioteks-scanning i en baggrundstråd
så GUI ikke fryser.
"""
from PyQt6.QtCore import QThread, pyqtSignal
class ScanWorker(QThread):
"""
Kører _full_scan_all() i en baggrundstråd.
Sender status-opdateringer undervejs.
"""
status_update = pyqtSignal(str) # løbende statusbeskeder
scan_done = pyqtSignal(int) # antal behandlede filer
def __init__(self, watcher, parent=None):
super().__init__(parent)
self._watcher = watcher
self._total = 0
def run(self):
try:
from local.local_db import get_libraries
from local.tag_reader import is_supported
import os
libraries = get_libraries(active_only=True)
if not libraries:
self.status_update.emit("Ingen biblioteker konfigureret")
self.scan_done.emit(0)
return
total_processed = 0
for lib in libraries:
from pathlib import Path
path = Path(lib["path"])
name = path.name
if not path.exists():
self.status_update.emit(f"⚠ Mappe ikke fundet: {path}")
continue
self.status_update.emit(f"Scanner: {name}...")
# Tæl filer med os.walk — håndterer permission-fejl sikkert
count = 0
for dirpath, _, filenames in os.walk(str(path), followlinks=False):
for f in filenames:
if is_supported(f):
count += 1
self.status_update.emit(f"Scanner: {name} ({count} filer)...")
# Kør scanning
self._watcher._full_scan_library(lib["id"], str(path))
total_processed += count
self.status_update.emit(f"Scan færdig — {total_processed} filer gennemgået")
self.scan_done.emit(total_processed)
except Exception as e:
self.status_update.emit(f"Scan fejl: {e}")
self.scan_done.emit(0)