//! Small persisted app settings - currently just the TMDB API token ("Fetch Data" needs //! it; everything else in the app works offline without any settings at all). Stored as //! plain JSON in Tauri's per-OS app config directory (e.g. //! `~/Library/Application Support/com.renamr.app/settings.json` on macOS, //! `~/.config/renamr/settings.json` on Linux) - not the original FileBot's config //! location, since that's Java Preferences-backed and OS/JVM-specific; this is Renamr's //! own, analogous location for its own settings. use serde::{Deserialize, Serialize}; use std::fs; use std::path::PathBuf; use tauri::{AppHandle, Manager}; #[derive(Debug, Default, Serialize, Deserialize)] struct StoredSettings { #[serde(default)] tmdb_token: Option, } fn settings_path(app: &AppHandle) -> Result { let dir = app .path() .app_config_dir() .map_err(|e| format!("couldn't resolve the app config directory: {e}"))?; fs::create_dir_all(&dir).map_err(|e| format!("couldn't create {}: {e}", dir.display()))?; Ok(dir.join("settings.json")) } fn load(app: &AppHandle) -> StoredSettings { let Ok(path) = settings_path(app) else { return StoredSettings::default(); }; fs::read_to_string(path) .ok() .and_then(|contents| serde_json::from_str(&contents).ok()) .unwrap_or_default() } fn save(app: &AppHandle, settings: &StoredSettings) -> Result<(), String> { let path = settings_path(app)?; let json = serde_json::to_string_pretty(settings).map_err(|e| e.to_string())?; fs::write(&path, json).map_err(|e| format!("couldn't write {}: {e}", path.display())) } /// Returns the stored TMDB token, or `None` if nothing (or only blank text) is saved. pub fn tmdb_token(app: &AppHandle) -> Option { load(app).tmdb_token.filter(|t| !t.trim().is_empty()) } #[tauri::command] pub fn get_tmdb_token(app: AppHandle) -> Option { tmdb_token(&app) } #[tauri::command] pub fn save_tmdb_token(app: AppHandle, token: String) -> Result<(), String> { let mut settings = load(&app); let trimmed = token.trim().to_string(); settings.tmdb_token = if trimmed.is_empty() { None } else { Some(trimmed) }; save(&app, &settings) }