v0.38.0: several imported songs are identified as one batch

No identify dialog pops up mid-download any more: songs are looked up
quietly as they land and reviewed together once the last one is in, as
rows in the app's columns. Songs that are one album get the album-wide
fields once, unified, plus a cover from the album-art search. Ticked
rows apply as one undo step; unticked ones get the ordinary dialog
after. Popups wait for LinTunes to be the app in front.
This commit is contained in:
2026-10-09 01:42:13 -07:00
parent f164a22b64
commit f079d26416
9 changed files with 1150 additions and 8 deletions
+136 -4
View File
@@ -33,7 +33,11 @@ from lintunes.gui.device_sync_dialog import (
DeviceSyncSettingsDialog, resolve_sync_playlists,
)
from lintunes.gui.export_dialog import ExportKindDialog, WebMixDialog
from lintunes.gui.identify_batch_dialog import IdentifyBatchDialog
from lintunes.gui.identify_dialog import IdentifyDialog
from lintunes.identify_batch import (
IdentifyCollector, album_art_query, analyze,
)
from lintunes.gui.sidebar import SIDEBAR_INSET, SidebarPanel
from lintunes.gui.library_view import LibraryView
from lintunes.gui.playlist_view import PlaylistView
@@ -250,6 +254,13 @@ class MainWindow(QMainWindow):
self._url_identify = True # False after "Stop Identifying"
self._url_note = ""
self._url_progress = None # UrlImportProgress, for several songs
# Several songs are looked up quietly while they download, and
# reviewed together once the last one has landed.
self._url_batch = None # identify_batch.IdentifyCollector
# Popups waiting for LinTunes to be the app in front (they must
# never land on top of another app).
self._popups_waiting: list = []
self._popups_hooked = False
# Separate instance from the playback inhibitor: pausing music
# mid-sync must not drop the sync's hold. Logout flag included so
# GNOME's shutdown/restart dialog names the transfer as the blocker.
@@ -1341,6 +1352,15 @@ class MainWindow(QMainWindow):
if problem else "")
spotify = dialog.spotify_tracks()
if self._url_batch is not None:
self._url_batch.cancel()
self._url_batch = None
if max(len(spotify), len(chosen)) > 1:
batch = IdentifyCollector(
self._prefs.acoustid.get("api_key", "").strip(), self)
batch.progress.connect(self._on_url_batch_progress)
batch.ready.connect(self._on_url_batch_ready)
self._url_batch = batch
if spotify:
# Spotify won't hand over audio: each song is found on YouTube
# and arrives already wearing Spotify's tags.
@@ -1421,12 +1441,113 @@ class MainWindow(QMainWindow):
if self._url_progress is not None:
self._url_progress.landed(bool(imported))
if imported and self._url_identify:
self._enqueue_identify(imported)
if self._url_batch is not None:
self._url_batch.add(imported) # reviewed when all are in
else:
self._enqueue_identify(imported)
def _finish_url_import(self):
if self._url_worker is not None:
self._url_worker.cleanup()
self._hide_sync_widgets()
if self._url_batch is not None:
self._url_batch.close() # no more songs are coming
def _on_url_batch_progress(self, done: int, total: int):
worker = self._url_worker
if worker is None or not worker.busy():
self.statusBar().showMessage(
f"Identifying the imported songs: {done} of {total}…")
def _on_url_batch_ready(self, entries: list):
"""Every song is downloaded and looked up: one window for them all
(one song, if that's all that landed, gets the ordinary one)."""
if self.sender() is not self._url_batch:
return
self._url_batch = None
library = self._manager.library
entries = [(t, c) for t, c in entries if t.track_id in library.tracks]
if not entries or not self._url_identify:
return
self.statusBar().clearMessage()
if len(entries) == 1:
analysis = analyze(entries)
self._when_app_active(
lambda: self._review_individually(analysis.rows))
return
analysis = analyze(entries)
self._when_app_active(lambda: self._show_batch_review(analysis))
def _show_batch_review(self, analysis):
dialog = IdentifyBatchDialog(analysis, album_art_query(analysis),
self)
if not dialog.exec():
self.statusBar().showMessage(
"Identify cancelled — the songs keep the tags they have",
6000)
return
if dialog.outcome == dialog.REVIEW_ALL:
self._review_individually(dialog.all_rows())
return
edits = dialog.edits()
if edits:
# The edit funnel writes the tags, may relocate the files, and
# records the whole list as one undo step.
self._manager.edit_many_track_fields(edits)
if dialog.selected_image:
library = self._manager.library
tracks = [library.tracks[r.track.track_id]
for r in dialog.all_rows()
if r.track.track_id in library.tracks]
self._embed_album_art(tracks, dialog.selected_image,
dialog.selected_mime)
if edits:
self.statusBar().showMessage(
f"Updated tags for {len(edits)} song(s)", 6000)
self._review_individually(dialog.unchecked_rows())
def _review_individually(self, rows: list):
"""The ordinary Identify window, one song at a time, from lookups
already made."""
rows = [r for r in rows if r.candidates
and r.track.track_id in self._manager.library.tracks]
for i, row in enumerate(rows):
track = self._manager.library.tracks[row.track.track_id]
dialog = IdentifyDialog(track, row.candidates,
remaining=len(rows) - i - 1, parent=self)
if dialog.exec():
fields = dialog.result_fields()
if fields:
self._manager.edit_track_fields(track.track_id, fields)
elif dialog.cancel_all:
return
def _when_app_active(self, show):
"""Run ``show`` (which opens a window over this one) now if LinTunes
is the app in front, else once it is. A dialog parented to the main
window only ever stacks over LinTunes, but one opened while another
app has focus would still jump in front of it. Meanwhile the window
asks for attention instead."""
app = QApplication.instance()
if app.applicationState() == Qt.ApplicationState.ApplicationActive:
show()
return
self._popups_waiting.append(show)
QApplication.alert(self)
if not self._popups_hooked:
self._popups_hooked = True
app.applicationStateChanged.connect(self._on_app_state_changed)
def _on_app_state_changed(self, state):
if (state == Qt.ApplicationState.ApplicationActive
and self._popups_waiting):
waiting, self._popups_waiting = self._popups_waiting, []
def run():
for show in waiting:
show()
# After the activation event has finished being delivered.
QTimer.singleShot(0, run)
def _on_url_finished(self, summary: dict):
self._finish_url_import()
@@ -1453,8 +1574,9 @@ class MainWindow(QMainWindow):
self._finish_url_import()
if self._url_progress is not None:
self._url_progress.finish(False)
QMessageBox.warning(self, "Import from URL",
f"Couldn't import from that link:\n\n{message}")
self._when_app_active(lambda: QMessageBox.warning(
self, "Import from URL",
f"Couldn't import from that link:\n\n{message}"))
def _confirm_cancel_url_import(self):
box = QMessageBox(self)
@@ -1778,7 +1900,14 @@ class MainWindow(QMainWindow):
f"and the filename says nothing", 6000)
else:
self.statusBar().clearMessage()
dialog = IdentifyDialog(track, result["candidates"],
self._when_app_active(
lambda: self._show_identify(track, result["candidates"]))
return
self._identify_next()
def _show_identify(self, track, candidates):
if track.track_id in self._manager.library.tracks:
dialog = IdentifyDialog(track, candidates,
remaining=len(self._identify_queue),
parent=self)
accepted = dialog.exec()
@@ -1796,6 +1925,9 @@ class MainWindow(QMainWindow):
# Also covers songs a URL import has yet to download: they
# still import, they just don't get a dialog each.
self._url_identify = False
if self._url_batch is not None:
self._url_batch.cancel()
self._url_batch = None
self.statusBar().clearMessage()
return
self._identify_next()