Files
lintunes/lintunes/preferences.py
T

190 lines
7.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import copy
import json
from pathlib import Path
from PyQt6.QtCore import QObject, pyqtSignal
DEFAULTS = {
"ui_scale": "medium", # small | medium | large
"highlight": "blue", # see theme.HIGHLIGHT_COLORS
"highlight_palette": {}, # slot name -> "#rrggbb" the user picked
"now_playing_font": None, # None=prefer Century Gothic, ""=use app
# default, "Family"=use app
"volume": 0.8, # logical 0..1 master output volume
"normalize_volume": False, # play every song at one loudness (loudness.py)
"visualizer_mode": "on", # on (color) | dim (gray) | off — click cycle
"visualizer_style": "bars", # bars | radial | laser | kaleidostrobe
# The styles' tunables — ranges in visualizer_styles.LIMITS.
# Fade times in seconds, 0 = off: how long what a bar or dot left
# behind, or the laser's past sweeps, take to fade into the background.
"visualizer_bars_fade": 0.0,
"visualizer_bars_count": 20, # 1 … 1024 (one per pixel, at most)
"visualizer_ghost_fade": 1.0, # the radial's
"visualizer_laser_afterimage": 0.1,
"visualizer_laser_width": 1.5, # pixels
"visualizer_laser_dot": 3.0, # pixels across
"visualizer_laser_head": True, # a dot at the moving beam's tip
"visualizer_laser_glow": True, # the haze/scatter bloom
"visualizer_laser_grid": False, # an oscilloscope's dot graticule
"visualizer_laser_grid_dot": 1.0, # its dots, × their automatic size
"visualizer_presets": {}, # {style: {name: settings}}
"visualizer_laser_low_hz": 20, # the speaker under the mirror:
"visualizer_laser_high_hz": 20000, # what it plays (ends = no filter)
"visualizer_radial_spokes": 120, # frequency bands around the circle
"visualizer_radial_levels": 20, # volume steps along each spoke
"visualizer_radial_dot": 1.0, # dot size, × the automatic size
"visualizer_radial_normalize": True, # even out bass vs treble
# Per-area grayscale overrides (0=black .. 255=white). None = inherit the
# current/default look for that surface, so nothing changes until the user
# drags a slider in Preferences.
"color_background": None, # outer chrome (QPalette.Window)
"color_button": None, # rounded transport button boxes
"color_now_playing_bg": None, # panel behind the now-playing text
"color_now_playing_text": None, # the now-playing title/artist text
"color_stripe": None, # alternating row stripes (QPalette.AlternateBase)
"color_visualizer_gray": None, # visualizer bars in dim (gray) mode
"color_base": None, # the lists' background (QPalette.Base)
# Dark mode (Round 101): the same sliders again under dark_<key>, and a
# tint per mode — a hue laid over the grays at tint_level percent.
"dark_mode": False,
"menu_bar": "always", # always | alt (hidden until Alt; gui/menu_bar.py)
"tint_color": "#3584E4",
"tint_level": 0,
"dark_tint_color": "#3584E4",
"dark_tint_level": 0,
"lastfm": {
"api_key": "",
"api_secret": "",
"username": "",
"session_key": "",
"scrobble_enabled": False,
},
# Track identification (Identify Track…). The application key is the
# user's own, registered free at acoustid.org/new-application.
"acoustid": {
"api_key": "",
},
# Which playlists belong on the andTunes device, by persistent_id. Kept
# here rather than on the Playlist so it stays out of the conflict-merge
# machinery — and because riding the syncthing share is the point: both
# machines should agree on what the Rabbit is carrying.
"device_sync": {
"playlist_ids": [],
# Fully ticked folders: everything under them syncs, including
# playlists added to them later (gui/device_sync_dialog.py).
"folder_ids": [],
"copy_art": True,
},
# Connections ▸ Import Shazams: the Rabbit's Shazams already dealt with
# (imported, unticked, or already owned), request id → date. Synced, so
# the other desktop doesn't offer them again either.
"shazam_import": {
"handled": {},
},
# Which machine runs friend sharing (Cassette): {machine_id, hostname}.
# Synced so the other machine can name it; see lintunes/cassette/host.py.
"cassette_host": None,
# Importing: convert FLAC/WAV/AIFF/ALAC to an MP3 at this bitrate
# (importers/lossless.py). The original file is never touched.
"import_compress": False,
"import_compress_kbps": 256,
"import_compress_cassette": False, # ...songs from Cassette friends too
# The quiet "tink" when a sync, export or URL import finishes.
"sound_effects": True,
# The yearly Wrapped (storage/listen_history.py). Synced on purpose: one
# tick and both desktops — and the Rabbit, via the sync — keep a history.
"wrapped": {
"track_history": False,
# This year's trimmed history in every friend's cassette folder.
"share_with_friends": False,
},
}
class Preferences(QObject):
"""App preferences persisted to <data_dir>/preferences.json (so they ride
the same syncthing share as the library)."""
changed = pyqtSignal()
def __init__(self, data_dir: Path, parent=None):
super().__init__(parent)
self._path = Path(data_dir) / "preferences.json"
self._data = copy.deepcopy(DEFAULTS)
if self._path.exists():
try:
with open(self._path, "r", encoding="utf-8") as f:
stored = json.load(f)
_deep_merge(self._data, stored)
except (ValueError, OSError):
pass
def get(self, key: str, default=None):
return self._data.get(key, default)
def set(self, key: str, value):
if self._data.get(key) == value:
return
self._data[key] = value
self.save()
self.changed.emit()
def set_live(self, key: str, value):
"""Update a preference and notify listeners *without* writing to disk —
for live drag-preview (e.g. the color sliders). Call save() to persist
once the user stops dragging."""
self._data[key] = value
self.changed.emit()
def update_lastfm(self, **fields):
self._data["lastfm"].update(fields)
self.save()
self.changed.emit()
@property
def lastfm(self) -> dict:
return self._data["lastfm"]
def update_acoustid(self, **fields):
self._data["acoustid"].update(fields)
self.save()
self.changed.emit()
@property
def acoustid(self) -> dict:
return self._data["acoustid"]
def update_device_sync(self, **fields):
self._data["device_sync"].update(fields)
self.save()
self.changed.emit()
@property
def device_sync(self) -> dict:
return self._data["device_sync"]
def update_wrapped(self, **fields):
self._data["wrapped"].update(fields)
self.save()
self.changed.emit()
@property
def wrapped(self) -> dict:
return self._data["wrapped"]
def save(self):
self._path.parent.mkdir(parents=True, exist_ok=True)
tmp = self._path.with_suffix(".json.tmp")
with open(tmp, "w", encoding="utf-8") as f:
json.dump(self._data, f, indent=2)
tmp.replace(self._path)
def _deep_merge(base: dict, override: dict):
for key, value in override.items():
if isinstance(value, dict) and isinstance(base.get(key), dict):
_deep_merge(base[key], value)
else:
base[key] = value