v0.17.0: the desktop already knows what the songs are

The first half of andTunes, a music player for the Rabbit R1. Auxio re-reads
Android's MediaStore on every launch, which is the "your songs will show up
here" hang — so the answer isn't a faster scanner, it's not scanning at all.
LinTunes already knows the artist, album and year of every file it just
copied, so it writes them into Music/andTunes/library.json and the app reads
one file instead of indexing a filesystem.

New lintunes/andtunes/: layout.py (a de-duplicated Media/<Artist>/<Album>/
tree, so a song in three playlists is stored once), manifest.py (pure, Qt-free),
art.py (one 480 px cover per album via QImage, cached and invalidated by the
audio file's mtime — which is exactly what embedding new art moves), and
sync.py, a third sibling of plan_export/ExportWorker.

Three rules the code depends on: planning never renders art (a mutagen open
per album, and planning runs on the GUI thread); the index is written last and
after a cancel lists only tracks whose files actually landed; and every delete
goes through layout.assert_inside, which is why the old Music/<Playlist>/
folders are structurally unreachable rather than merely un-referenced.

Connections gains "Sync to Rabbit" and "Rabbit Sync Settings…" — the ticked
set is the device's contents, so unticking is how a playlist comes off, which
nothing could do before. The selection rides preferences.json so both machines
agree. The old per-playlist sync stays, renamed "(Auxio)": removing it now
would leave the device full of files and nothing able to open them until the
app exists.

The app itself is rounds 48-50; its board is andtunes/TASKS.md, including the
measured screen facts (480x640 px at density override 160 — one dp is half its
usual physical size on a 2.88" panel).

Claude-Session: https://claude.ai/code/session_015wrTys5U1fLb4pBD2LKWzV
This commit is contained in:
2026-09-06 03:25:40 -04:00
parent 33d74ab1f5
commit c59aa26263
15 changed files with 1780 additions and 11 deletions
+119 -9
View File
@@ -10,6 +10,8 @@ from PyQt6.QtCore import Qt, QEvent, QTimer
from PyQt6.QtGui import QAction, QKeySequence
from lintunes import device_sync, mpris, music_folder, tagging, theme
from lintunes.andtunes import layout as andtunes_layout
from lintunes.andtunes import sync as andtunes_sync
from lintunes.art_search import AlbumArtFetcher
from lintunes.cast.controller import CastController
from lintunes.eventlog import log_control
@@ -20,6 +22,7 @@ from lintunes.player import Player
from lintunes.importers import file_importer
from lintunes.gui.album_art_dialog import AlbumArtDialog
from lintunes.gui.cast_dialog import ChromecastDialog
from lintunes.gui.device_sync_dialog import DeviceSyncSettingsDialog
from lintunes.gui.export_dialog import ExportKindDialog, WebMixDialog
from lintunes.gui.identify_dialog import IdentifyDialog
from lintunes.gui.sidebar import SidebarPanel
@@ -157,6 +160,7 @@ class MainWindow(QMainWindow):
self._sync_progress.hide()
self.statusBar().addPermanentWidget(self._sync_progress)
self._sync_worker = None
self._andtunes_worker = None
self._sync_device_name = ""
# Export shares the status-bar progress widgets with device sync —
# only one of the two may run at a time (each guards on the other).
@@ -333,8 +337,18 @@ class MainWindow(QMainWindow):
# Mnemonic on the "n": Alt+C already belongs to &Controls.
connections_menu = bar.addMenu("Co&nnections")
self._andtunes_action = self._add_action(
connections_menu, "Sync to Rabbit", "", self._sync_to_andtunes)
self._andtunes_action.setEnabled(False)
self._add_action(connections_menu, "Rabbit Sync Settings…", "",
self._show_device_sync_settings)
connections_menu.addSeparator()
# The per-playlist sync stays until andTunes can actually play a
# song: it is what feeds Auxio, and pulling it in the same round that
# adds the new tree would leave the device full of files and nothing
# able to open them.
self._sync_action = self._add_action(
connections_menu, "Sync Playlist to Rabbit", "",
connections_menu, "Sync Playlist to Rabbit (Auxio)", "",
self._sync_playlist_to_device)
self._sync_action.setEnabled(False)
connections_menu.addSeparator()
@@ -371,9 +385,12 @@ class MainWindow(QMainWindow):
busy = self._busy_worker() is not None
on_playlist = (self._content.currentWidget() is self._playlist_view
and bool(self._playlist_view.playlist_id))
device = device_sync.find_device()
self._sync_action.setEnabled(
not busy and on_playlist
and device_sync.find_device() is not None)
not busy and on_playlist and device is not None)
# Whole-library sync doesn't care which view is open — only that a
# device is plugged in and at least one playlist is ticked.
self._andtunes_action.setEnabled(not busy and device is not None)
if self._cast.is_connected():
self._cast_action.setText(
@@ -470,7 +487,8 @@ class MainWindow(QMainWindow):
def _busy_worker(self):
"""The transfer currently owning the status-bar progress widgets."""
for worker in (self._sync_worker, self._export_worker):
for worker in (self._sync_worker, self._andtunes_worker,
self._export_worker):
if worker is not None and worker.busy():
return worker
return None
@@ -479,7 +497,10 @@ class MainWindow(QMainWindow):
if self._export_worker is not None and self._export_worker.busy():
self._confirm_cancel_export()
return
if self._sync_worker is None or not self._sync_worker.busy():
worker = self._sync_worker
if self._andtunes_worker is not None and self._andtunes_worker.busy():
worker = self._andtunes_worker
if worker is None or not worker.busy():
return
box = QMessageBox(self)
box.setWindowTitle("Cancel transfer")
@@ -493,9 +514,8 @@ class MainWindow(QMainWindow):
box.exec()
# The transfer keeps running while the dialog is up — it may well
# have finished by now, in which case there is nothing to cancel.
if (box.clickedButton() is cancel_button
and self._sync_worker is not None):
self._sync_worker.cancel()
if box.clickedButton() is cancel_button and worker is not None:
worker.cancel()
def _on_sync_cancelled(self, summary):
self._sync_inhibitor.release()
@@ -530,6 +550,95 @@ class MainWindow(QMainWindow):
self._hide_sync_widgets()
QMessageBox.warning(self, "Sync failed", message)
# ---- connections: andTunes (whole-library sync) ----
def _show_device_sync_settings(self):
device = device_sync.find_device()
dialog = DeviceSyncSettingsDialog(
self._prefs, self._manager.library, self,
device_name=device.name if device else "Rabbit")
dialog.exec()
def _selected_sync_playlists(self) -> list:
"""The ticked playlists, in the order they were ticked in settings.
Ids that no longer resolve are skipped rather than cleaned up: a
playlist can be missing because the other machine hasn't synced yet,
and silently forgetting the choice would be worse than ignoring it.
"""
library = self._manager.library
return [library.playlists[pid]
for pid in self._prefs.device_sync.get("playlist_ids", [])
if pid in library.playlists]
def _sync_to_andtunes(self):
# Everything may have changed since the menu opened — re-verify.
if self._busy_worker() is not None:
return
device = device_sync.find_device()
if device is None:
return
playlists = self._selected_sync_playlists()
if not playlists:
QMessageBox.information(
self, "Nothing to sync",
"No playlists are set to sync yet.\n\n"
"Pick them in Connections → Rabbit Sync Settings…")
return
root = andtunes_layout.root_for(device)
try:
plan = andtunes_sync.plan_andtunes_sync(
playlists, self._manager.library.tracks, root, device,
copy_art=bool(self._prefs.device_sync.get("copy_art", True)))
free = shutil.disk_usage(device.root).free
except OSError as e:
QMessageBox.warning(self, "Sync failed",
f"Couldn't read the {device.name}: {e}")
return
needed = plan.bytes_to_copy + device_sync.SPACE_MARGIN
if free + plan.bytes_freed < needed:
QMessageBox.warning(
self, "Not enough space",
f"Syncing {len(playlists)} playlist(s) needs "
f"{device_sync.format_bytes(needed - plan.bytes_freed)}, but "
f"the {device.name} only has "
f"{device_sync.format_bytes(free)} free.")
return
self._andtunes_worker = andtunes_sync.AndTunesSyncWorker(plan, self)
self._andtunes_worker.progress.connect(self._on_andtunes_progress)
self._andtunes_worker.finished.connect(self._on_andtunes_finished)
self._andtunes_worker.cancelled.connect(self._on_sync_cancelled)
self._andtunes_worker.failed.connect(self._on_sync_failed)
self._sync_device_name = device.name
self._sync_label.setText(f"Copying to {device.name}")
self._sync_progress.setRange(0, max(plan.bytes_to_copy // 1024, 1))
self._sync_progress.setValue(0)
self._sync_cancel.show()
self._sync_label.show()
self._sync_progress.show()
self._sync_inhibitor.inhibit()
self._andtunes_worker.start()
def _on_andtunes_progress(self, done_kib, total_kib, label):
# Unlike the per-playlist sync this run has phases that move no audio
# bytes (album art, then the index), so the label carries the story.
self._sync_progress.setRange(0, total_kib)
self._sync_progress.setValue(done_kib)
self._sync_label.setText(f"{self._sync_device_name} · {label}")
def _on_andtunes_finished(self, summary):
self._sync_inhibitor.release()
self._hide_sync_widgets()
msg = (f"Synced {summary['playlists']} playlist(s) to the "
f"{summary['device']}: {summary['tracks']} tracks "
f"({summary['copied']} copied, {summary['kept']} up to date), "
f"{summary['albums']} covers, {summary['removed']} removed")
if summary["skipped"]:
msg += f", {summary['skipped']} skipped (no local file)"
self.statusBar().showMessage(msg, 10000)
# ---- export ----
def _refresh_export_action(self):
@@ -1309,7 +1418,8 @@ class MainWindow(QMainWindow):
def closeEvent(self, event):
# Quitting mid-transfer abandons a partial file on the device (it
# self-heals next sync, but silently) — make it a deliberate choice.
if self._sync_worker is not None and self._sync_worker.busy():
if self._busy_worker() is not None and self._busy_worker() is not (
self._export_worker):
box = QMessageBox(self)
box.setWindowTitle("Sync in progress")
box.setText(