add per-user isolation support to all remaining modules

Add new_at() constructors, Option<Store> wrappers, new_empty()/init_at()/
teardown() methods, and update all commands to unwrap Option for:
- config: audio_settings, download_settings, favorites_cache,
  favorites_preferences, legal_settings, playback_preferences,
  remote_control_settings, subscription_state, tray_settings
- data: api_cache, artist_blacklist, artist_vectors, library,
  listenbrainz, lyrics, musicbrainz, offline, offline_cache,
  radio_engine, reco_store, updates
This commit is contained in:
vicrodh committed 2026-02-05 23:11:07 -06:00
1 parent 48dac0ec3a
commit aab3e67641
29 files changed
+1174 -429

No files matched your search

+64 -22
View File
@@ -5,6 +5,7 @@
use crate::audio::{AlsaPlugin, AudioBackendType};
use rusqlite::{Connection, params};
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::sync::{Arc, Mutex};
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -55,15 +56,11 @@ pub struct AudioSettingsStore {
}
impl AudioSettingsStore {
pub fn new() -> Result<Self, String> {
let data_dir = dirs::data_dir()
.ok_or("Could not determine data directory")?
.join("qbz");
std::fs::create_dir_all(&data_dir)
fn open_at(dir: &Path, db_name: &str) -> Result<Self, String> {
std::fs::create_dir_all(dir)
.map_err(|e| format!("Failed to create data directory: {}", e))?;
let db_path = data_dir.join("audio_settings.db");
let db_path = dir.join(db_name);
let conn = Connection::open(&db_path)
.map_err(|e| format!("Failed to open audio settings database: {}", e))?;
@@ -97,6 +94,17 @@ impl AudioSettingsStore {
Ok(Self { conn })
}
pub fn new() -> Result<Self, String> {
let data_dir = dirs::data_dir()
.ok_or("Could not determine data directory")?
.join("qbz");
Self::open_at(&data_dir, "audio_settings.db")
}
pub fn new_at(base_dir: &Path) -> Result<Self, String> {
Self::open_at(base_dir, "audio_settings.db")
}
pub fn get_settings(&self) -> Result<AudioSettings, String> {
self.conn
.query_row(
@@ -267,15 +275,36 @@ impl AudioSettingsStore {
/// Thread-safe wrapper
pub struct AudioSettingsState {
pub store: Arc<Mutex<AudioSettingsStore>>,
pub store: Arc<Mutex<Option<AudioSettingsStore>>>,
}
impl AudioSettingsState {
pub fn new() -> Result<Self, String> {
Ok(Self {
store: Arc::new(Mutex::new(AudioSettingsStore::new()?)),
store: Arc::new(Mutex::new(Some(AudioSettingsStore::new()?))),
})
}
pub fn new_empty() -> Self {
Self {
store: Arc::new(Mutex::new(None)),
}
}
pub fn init_at(&self, base_dir: &Path) -> Result<(), String> {
let new_store = AudioSettingsStore::new_at(base_dir)?;
let mut guard = self.store.lock()
.map_err(|_| "Failed to lock audio settings store".to_string())?;
*guard = Some(new_store);
Ok(())
}
pub fn teardown(&self) -> Result<(), String> {
let mut guard = self.store.lock()
.map_err(|_| "Failed to lock audio settings store".to_string())?;
*guard = None;
Ok(())
}
}
// Tauri commands
@@ -283,7 +312,8 @@ impl AudioSettingsState {
pub fn get_audio_settings(
state: tauri::State<'_, AudioSettingsState>,
) -> Result<AudioSettings, String> {
let store = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let guard = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.get_settings()
}
@@ -304,7 +334,8 @@ pub fn set_audio_output_device(
normalized_device
);
let store = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let guard = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_output_device(normalized_device.as_deref())
}
@@ -313,7 +344,8 @@ pub fn set_audio_exclusive_mode(
state: tauri::State<'_, AudioSettingsState>,
enabled: bool,
) -> Result<(), String> {
let store = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let guard = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_exclusive_mode(enabled)
}
@@ -322,7 +354,8 @@ pub fn set_audio_dac_passthrough(
state: tauri::State<'_, AudioSettingsState>,
enabled: bool,
) -> Result<(), String> {
let store = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let guard = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_dac_passthrough(enabled)
}
@@ -331,7 +364,8 @@ pub fn set_audio_sample_rate(
state: tauri::State<'_, AudioSettingsState>,
rate: Option<u32>,
) -> Result<(), String> {
let store = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let guard = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_sample_rate(rate)
}
@@ -340,7 +374,8 @@ pub fn set_audio_backend_type(
state: tauri::State<'_, AudioSettingsState>,
backend_type: Option<AudioBackendType>,
) -> Result<(), String> {
let store = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let guard = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_backend_type(backend_type)
}
@@ -349,7 +384,8 @@ pub fn set_audio_alsa_plugin(
state: tauri::State<'_, AudioSettingsState>,
plugin: Option<AlsaPlugin>,
) -> Result<(), String> {
let store = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let guard = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_alsa_plugin(plugin)
}
@@ -358,7 +394,8 @@ pub fn set_audio_alsa_hardware_volume(
state: tauri::State<'_, AudioSettingsState>,
enabled: bool,
) -> Result<(), String> {
let store = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let guard = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_alsa_hardware_volume(enabled)
}
@@ -367,7 +404,8 @@ pub fn set_audio_stream_first_track(
state: tauri::State<'_, AudioSettingsState>,
enabled: bool,
) -> Result<(), String> {
let store = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let guard = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_stream_first_track(enabled)
}
@@ -376,7 +414,8 @@ pub fn set_audio_stream_buffer_seconds(
state: tauri::State<'_, AudioSettingsState>,
seconds: u8,
) -> Result<(), String> {
let store = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let guard = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_stream_buffer_seconds(seconds)
}
@@ -385,7 +424,8 @@ pub fn set_audio_streaming_only(
state: tauri::State<'_, AudioSettingsState>,
enabled: bool,
) -> Result<(), String> {
let store = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let guard = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_streaming_only(enabled)
}
@@ -394,7 +434,8 @@ pub fn set_audio_limit_quality_to_device(
state: tauri::State<'_, AudioSettingsState>,
enabled: bool,
) -> Result<(), String> {
let store = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let guard = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_limit_quality_to_device(enabled)
}
@@ -403,6 +444,7 @@ pub fn set_audio_device_max_sample_rate(
state: tauri::State<'_, AudioSettingsState>,
rate: Option<u32>,
) -> Result<(), String> {
let store = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let guard = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_device_max_sample_rate(rate)
}
+30 -15
View File
@@ -4,6 +4,7 @@
use rusqlite::{Connection, params};
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::sync::{Arc, Mutex};
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -33,15 +34,11 @@ pub struct DownloadSettingsStore {
}
impl DownloadSettingsStore {
pub fn new() -> Result<Self, String> {
let data_dir = dirs::data_dir()
.ok_or("Could not determine data directory")?
.join("qbz");
std::fs::create_dir_all(&data_dir)
fn open_at(dir: &Path, db_name: &str) -> Result<Self, String> {
std::fs::create_dir_all(dir)
.map_err(|e| format!("Failed to create data directory: {}", e))?;
let db_path = data_dir.join("download_settings.db");
let db_path = dir.join(db_name);
let conn = Connection::open(&db_path)
.map_err(|e| format!("Failed to open download settings database: {}", e))?;
@@ -64,6 +61,17 @@ impl DownloadSettingsStore {
Ok(Self { conn })
}
pub fn new() -> Result<Self, String> {
let data_dir = dirs::data_dir()
.ok_or("Could not determine data directory")?
.join("qbz");
Self::open_at(&data_dir, "download_settings.db")
}
pub fn new_at(base_dir: &Path) -> Result<Self, String> {
Self::open_at(base_dir, "download_settings.db")
}
pub fn get_settings(&self) -> Result<DownloadSettings, String> {
self.conn
.query_row(
@@ -100,11 +108,15 @@ impl DownloadSettingsStore {
}
}
pub type DownloadSettingsState = Arc<Mutex<DownloadSettingsStore>>;
pub type DownloadSettingsState = Arc<Mutex<Option<DownloadSettingsStore>>>;
pub fn create_download_settings_state() -> Result<DownloadSettingsState, String> {
let store = DownloadSettingsStore::new()?;
Ok(Arc::new(Mutex::new(store)))
Ok(Arc::new(Mutex::new(Some(store))))
}
pub fn create_empty_download_settings_state() -> DownloadSettingsState {
Arc::new(Mutex::new(None))
}
// Tauri commands
@@ -114,7 +126,8 @@ pub fn get_download_settings(
state: tauri::State<DownloadSettingsState>,
) -> Result<DownloadSettings, String> {
log::info!("Command: get_download_settings");
let store = state.lock().map_err(|e| format!("Lock error: {}", e))?;
let guard = state.lock().map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.get_settings()
}
@@ -124,7 +137,7 @@ pub fn set_download_root(
state: tauri::State<DownloadSettingsState>,
) -> Result<(), String> {
log::info!("Command: set_download_root to: {}", path);
// Basic validation
let path_obj = std::path::Path::new(&path);
if !path_obj.exists() {
@@ -134,7 +147,8 @@ pub fn set_download_root(
return Err("Path is not a directory".to_string());
}
let store = state.lock().map_err(|e| format!("Lock error: {}", e))?;
let guard = state.lock().map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_download_root(&path)
}
@@ -144,16 +158,17 @@ pub fn set_show_downloads_in_library(
state: tauri::State<DownloadSettingsState>,
) -> Result<(), String> {
log::info!("Command: set_show_downloads_in_library to: {}", show);
let store = state.lock().map_err(|e| format!("Lock error: {}", e))?;
let guard = state.lock().map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_show_in_library(show)
}
#[tauri::command]
pub fn validate_download_root(path: String) -> Result<bool, String> {
log::info!("Command: validate_download_root: {}", path);
let path_obj = std::path::Path::new(&path);
if !path_obj.exists() {
return Ok(false);
}
+64 -22
View File
@@ -12,6 +12,7 @@
use rusqlite::{params, Connection};
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::sync::{Arc, Mutex};
/// Represents a cached favorite track
@@ -37,15 +38,11 @@ pub struct FavoritesCacheStore {
}
impl FavoritesCacheStore {
pub fn new() -> Result<Self, String> {
let data_dir = dirs::data_dir()
.ok_or("Could not determine data directory")?
.join("qbz");
std::fs::create_dir_all(&data_dir)
fn open_at(dir: &Path, db_name: &str) -> Result<Self, String> {
std::fs::create_dir_all(dir)
.map_err(|e| format!("Failed to create data directory: {}", e))?;
let db_path = data_dir.join("favorites_cache.db");
let db_path = dir.join(db_name);
let conn = Connection::open(&db_path)
.map_err(|e| format!("Failed to open favorites cache database: {}", e))?;
@@ -80,6 +77,17 @@ impl FavoritesCacheStore {
Ok(Self { conn })
}
pub fn new() -> Result<Self, String> {
let data_dir = dirs::data_dir()
.ok_or("Could not determine data directory")?
.join("qbz");
Self::open_at(&data_dir, "favorites_cache.db")
}
pub fn new_at(base_dir: &Path) -> Result<Self, String> {
Self::open_at(base_dir, "favorites_cache.db")
}
// ============ Track favorites ============
pub fn get_favorite_track_ids(&self) -> Result<Vec<i64>, String> {
@@ -298,16 +306,37 @@ impl FavoritesCacheStore {
// ============ Tauri State ============
pub struct FavoritesCacheState {
pub store: Arc<Mutex<FavoritesCacheStore>>,
pub store: Arc<Mutex<Option<FavoritesCacheStore>>>,
}
impl FavoritesCacheState {
pub fn new() -> Result<Self, String> {
let store = FavoritesCacheStore::new()?;
Ok(Self {
store: Arc::new(Mutex::new(store)),
store: Arc::new(Mutex::new(Some(store))),
})
}
pub fn new_empty() -> Self {
Self {
store: Arc::new(Mutex::new(None)),
}
}
pub fn init_at(&self, base_dir: &Path) -> Result<(), String> {
let new_store = FavoritesCacheStore::new_at(base_dir)?;
let mut guard = self.store.lock()
.map_err(|_| "Failed to lock favorites cache store".to_string())?;
*guard = Some(new_store);
Ok(())
}
pub fn teardown(&self) -> Result<(), String> {
let mut guard = self.store.lock()
.map_err(|_| "Failed to lock favorites cache store".to_string())?;
*guard = None;
Ok(())
}
}
// ============ Tauri Commands ============
@@ -317,10 +346,11 @@ impl FavoritesCacheState {
pub fn get_cached_favorite_tracks(
state: tauri::State<FavoritesCacheState>,
) -> Result<Vec<i64>, String> {
let store = state
let guard = state
.store
.lock()
.map_err(|_| "Failed to lock favorites cache store".to_string())?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.get_favorite_track_ids()
}
@@ -329,10 +359,11 @@ pub fn get_cached_favorite_tracks(
pub fn get_cached_favorite_albums(
state: tauri::State<FavoritesCacheState>,
) -> Result<Vec<String>, String> {
let store = state
let guard = state
.store
.lock()
.map_err(|_| "Failed to lock favorites cache store".to_string())?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.get_favorite_album_ids()
}
@@ -341,10 +372,11 @@ pub fn get_cached_favorite_albums(
pub fn get_cached_favorite_artists(
state: tauri::State<FavoritesCacheState>,
) -> Result<Vec<i64>, String> {
let store = state
let guard = state
.store
.lock()
.map_err(|_| "Failed to lock favorites cache store".to_string())?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.get_favorite_artist_ids()
}
@@ -354,10 +386,11 @@ pub fn cache_favorite_track(
track_id: i64,
state: tauri::State<FavoritesCacheState>,
) -> Result<(), String> {
let store = state
let guard = state
.store
.lock()
.map_err(|_| "Failed to lock favorites cache store".to_string())?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.add_favorite_track(track_id)
}
@@ -367,10 +400,11 @@ pub fn uncache_favorite_track(
track_id: i64,
state: tauri::State<FavoritesCacheState>,
) -> Result<(), String> {
let store = state
let guard = state
.store
.lock()
.map_err(|_| "Failed to lock favorites cache store".to_string())?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.remove_favorite_track(track_id)
}
@@ -380,10 +414,11 @@ pub fn cache_favorite_album(
album_id: String,
state: tauri::State<FavoritesCacheState>,
) -> Result<(), String> {
let store = state
let guard = state
.store
.lock()
.map_err(|_| "Failed to lock favorites cache store".to_string())?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.add_favorite_album(&album_id)
}
@@ -393,10 +428,11 @@ pub fn uncache_favorite_album(
album_id: String,
state: tauri::State<FavoritesCacheState>,
) -> Result<(), String> {
let store = state
let guard = state
.store
.lock()
.map_err(|_| "Failed to lock favorites cache store".to_string())?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.remove_favorite_album(&album_id)
}
@@ -406,10 +442,11 @@ pub fn cache_favorite_artist(
artist_id: i64,
state: tauri::State<FavoritesCacheState>,
) -> Result<(), String> {
let store = state
let guard = state
.store
.lock()
.map_err(|_| "Failed to lock favorites cache store".to_string())?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.add_favorite_artist(artist_id)
}
@@ -419,10 +456,11 @@ pub fn uncache_favorite_artist(
artist_id: i64,
state: tauri::State<FavoritesCacheState>,
) -> Result<(), String> {
let store = state
let guard = state
.store
.lock()
.map_err(|_| "Failed to lock favorites cache store".to_string())?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.remove_favorite_artist(artist_id)
}
@@ -432,10 +470,11 @@ pub fn sync_cached_favorite_tracks(
track_ids: Vec<i64>,
state: tauri::State<FavoritesCacheState>,
) -> Result<(), String> {
let store = state
let guard = state
.store
.lock()
.map_err(|_| "Failed to lock favorites cache store".to_string())?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.sync_favorite_tracks(&track_ids)
}
@@ -445,10 +484,11 @@ pub fn sync_cached_favorite_albums(
album_ids: Vec<String>,
state: tauri::State<FavoritesCacheState>,
) -> Result<(), String> {
let store = state
let guard = state
.store
.lock()
.map_err(|_| "Failed to lock favorites cache store".to_string())?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.sync_favorite_albums(&album_ids)
}
@@ -458,10 +498,11 @@ pub fn sync_cached_favorite_artists(
artist_ids: Vec<i64>,
state: tauri::State<FavoritesCacheState>,
) -> Result<(), String> {
let store = state
let guard = state
.store
.lock()
.map_err(|_| "Failed to lock favorites cache store".to_string())?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.sync_favorite_artists(&artist_ids)
}
@@ -470,9 +511,10 @@ pub fn sync_cached_favorite_artists(
pub fn clear_favorites_cache(
state: tauri::State<FavoritesCacheState>,
) -> Result<(), String> {
let store = state
let guard = state
.store
.lock()
.map_err(|_| "Failed to lock favorites cache store".to_string())?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.clear_all()
}
+51 -21
View File
@@ -35,15 +35,11 @@ pub struct FavoritesPreferencesStore {
}
impl FavoritesPreferencesStore {
pub fn new() -> Result<Self, String> {
let data_dir = dirs::data_dir()
.ok_or("Could not determine data directory")?
.join("qbz");
std::fs::create_dir_all(&data_dir)
fn open_at(dir: &Path, db_name: &str) -> Result<Self, String> {
std::fs::create_dir_all(dir)
.map_err(|e| format!("Failed to create data directory: {}", e))?;
let db_path = data_dir.join("favorites_preferences.db");
let db_path = dir.join(db_name);
let conn = Connection::open(&db_path)
.map_err(|e| format!("Failed to open favorites preferences database: {}", e))?;
@@ -62,7 +58,7 @@ impl FavoritesPreferencesStore {
let has_icon_background = conn
.prepare("SELECT icon_background FROM favorites_preferences LIMIT 1")
.is_ok();
if !has_icon_background {
conn.execute(
"ALTER TABLE favorites_preferences ADD COLUMN icon_background TEXT",
@@ -74,6 +70,17 @@ impl FavoritesPreferencesStore {
Ok(Self { conn })
}
pub fn new() -> Result<Self, String> {
let data_dir = dirs::data_dir()
.ok_or("Could not determine data directory")?
.join("qbz");
Self::open_at(&data_dir, "favorites_preferences.db")
}
pub fn new_at(base_dir: &Path) -> Result<Self, String> {
Self::open_at(base_dir, "favorites_preferences.db")
}
fn favorites_icon_dir() -> Result<PathBuf, String> {
let cache_dir = dirs::cache_dir()
.ok_or("Could not determine cache directory")?
@@ -127,7 +134,7 @@ impl FavoritesPreferencesStore {
pub fn get_preferences(&self) -> Result<FavoritesPreferences, String> {
let mut stmt = self.conn.prepare("SELECT custom_icon_path, custom_icon_preset, icon_background, tab_order FROM favorites_preferences WHERE id = 1")
.map_err(|e| format!("Failed to prepare select: {}", e))?;
let result = stmt.query_row([], |row| {
let custom_icon_path: Option<String> = row.get(0)?;
let custom_icon_preset: Option<String> = row.get(1)?;
@@ -136,7 +143,7 @@ impl FavoritesPreferencesStore {
let custom_icon_path = custom_icon_path
.and_then(|value| if value.trim().is_empty() { None } else { Some(value) });
let tab_order: Vec<String> = serde_json::from_str(&tab_order_str).unwrap_or_else(|_| {
vec![
"tracks".to_string(),
@@ -194,7 +201,7 @@ impl FavoritesPreferencesStore {
let tab_order_str = serde_json::to_string(&prefs.tab_order)
.map_err(|e| format!("Failed to serialize tab_order: {}", e))?;
self.conn.execute(
"INSERT OR REPLACE INTO favorites_preferences (id, custom_icon_path, custom_icon_preset, icon_background, tab_order)
VALUES (1, ?1, ?2, ?3, ?4)",
@@ -206,26 +213,48 @@ impl FavoritesPreferencesStore {
}
pub struct FavoritesPreferencesState {
pub store: Arc<Mutex<FavoritesPreferencesStore>>,
pub store: Arc<Mutex<Option<FavoritesPreferencesStore>>>,
}
impl FavoritesPreferencesState {
pub fn new() -> Result<Self, String> {
let store = FavoritesPreferencesStore::new()?;
Ok(Self {
store: Arc::new(Mutex::new(store)),
store: Arc::new(Mutex::new(Some(store))),
})
}
pub fn new_empty() -> Self {
Self {
store: Arc::new(Mutex::new(None)),
}
}
pub fn init_at(&self, base_dir: &Path) -> Result<(), String> {
let new_store = FavoritesPreferencesStore::new_at(base_dir)?;
let mut guard = self.store.lock()
.map_err(|_| "Failed to lock favorites preferences store".to_string())?;
*guard = Some(new_store);
Ok(())
}
pub fn teardown(&self) -> Result<(), String> {
let mut guard = self.store.lock()
.map_err(|_| "Failed to lock favorites preferences store".to_string())?;
*guard = None;
Ok(())
}
}
#[tauri::command]
pub fn get_favorites_preferences(
state: tauri::State<FavoritesPreferencesState>,
) -> Result<FavoritesPreferences, String> {
let store = state
let guard = state
.store
.lock()
.map_err(|_| "Failed to lock favorites preferences store".to_string())?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.get_preferences()
}
@@ -234,10 +263,11 @@ pub fn save_favorites_preferences(
prefs: FavoritesPreferences,
state: tauri::State<FavoritesPreferencesState>,
) -> Result<FavoritesPreferences, String> {
let store = state
let guard = state
.store
.lock()
.map_err(|_| "Failed to lock favorites preferences store".to_string())?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.save_preferences(prefs)
}
@@ -251,25 +281,25 @@ pub fn create_table(conn: &Connection) -> Result<()> {
)",
[],
)?;
// Migration: Add icon_background column if it doesn't exist
let has_icon_background = conn
.prepare("SELECT icon_background FROM favorites_preferences LIMIT 1")
.is_ok();
if !has_icon_background {
conn.execute(
"ALTER TABLE favorites_preferences ADD COLUMN icon_background TEXT",
[],
)?;
}
Ok(())
}
pub fn load_preferences(conn: &Connection) -> Result<FavoritesPreferences> {
let mut stmt = conn.prepare("SELECT custom_icon_path, custom_icon_preset, icon_background, tab_order FROM favorites_preferences WHERE id = 1")?;
let result = stmt.query_row([], |row| {
let custom_icon_path: Option<String> = row.get(0)?;
let custom_icon_preset: Option<String> = row.get(1)?;
@@ -278,7 +308,7 @@ pub fn load_preferences(conn: &Connection) -> Result<FavoritesPreferences> {
let custom_icon_path = custom_icon_path
.and_then(|value| if value.trim().is_empty() { None } else { Some(value) });
let tab_order: Vec<String> = serde_json::from_str(&tab_order_str).unwrap_or_else(|_| {
vec![
"tracks".to_string(),
@@ -305,7 +335,7 @@ pub fn load_preferences(conn: &Connection) -> Result<FavoritesPreferences> {
pub fn save_preferences(conn: &Connection, prefs: &FavoritesPreferences) -> Result<()> {
let tab_order_str = serde_json::to_string(&prefs.tab_order).unwrap();
conn.execute(
"INSERT OR REPLACE INTO favorites_preferences (id, custom_icon_path, custom_icon_preset, icon_background, tab_order)
VALUES (1, ?1, ?2, ?3, ?4)",
+31 -11
View File
@@ -5,6 +5,7 @@
use rusqlite::{Connection, params};
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::sync::{Arc, Mutex};
use log::info;
@@ -27,15 +28,11 @@ pub struct LegalSettingsStore {
}
impl LegalSettingsStore {
pub fn new() -> Result<Self, String> {
let data_dir = dirs::data_dir()
.ok_or("Could not determine data directory")?
.join("qbz");
std::fs::create_dir_all(&data_dir)
fn open_at(dir: &Path, db_name: &str) -> Result<Self, String> {
std::fs::create_dir_all(dir)
.map_err(|e| format!("Failed to create data directory: {}", e))?;
let db_path = data_dir.join("legal_settings.db");
let db_path = dir.join(db_name);
let conn = Connection::open(&db_path)
.map_err(|e| format!("Failed to open legal settings database: {}", e))?;
@@ -58,6 +55,17 @@ impl LegalSettingsStore {
Ok(Self { conn })
}
pub fn new() -> Result<Self, String> {
let data_dir = dirs::data_dir()
.ok_or("Could not determine data directory")?
.join("qbz");
Self::open_at(&data_dir, "legal_settings.db")
}
pub fn new_at(base_dir: &Path) -> Result<Self, String> {
Self::open_at(base_dir, "legal_settings.db")
}
pub fn get_settings(&self) -> Result<LegalSettings, String> {
self.conn
.query_row(
@@ -86,7 +94,16 @@ impl LegalSettingsStore {
}
}
pub type LegalSettingsState = Arc<Mutex<LegalSettingsStore>>;
pub type LegalSettingsState = Arc<Mutex<Option<LegalSettingsStore>>>;
pub fn create_legal_settings_state() -> Result<LegalSettingsState, String> {
let store = LegalSettingsStore::new()?;
Ok(Arc::new(Mutex::new(Some(store))))
}
pub fn create_empty_legal_settings_state() -> LegalSettingsState {
Arc::new(Mutex::new(None))
}
// Tauri commands
@@ -94,7 +111,8 @@ pub type LegalSettingsState = Arc<Mutex<LegalSettingsStore>>;
pub fn get_legal_settings(
state: tauri::State<'_, LegalSettingsState>,
) -> Result<LegalSettings, String> {
let store = state.lock().map_err(|e| format!("Lock error: {}", e))?;
let guard = state.lock().map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.get_settings()
}
@@ -102,7 +120,8 @@ pub fn get_legal_settings(
pub fn get_qobuz_tos_accepted(
state: tauri::State<'_, LegalSettingsState>,
) -> Result<bool, String> {
let store = state.lock().map_err(|e| format!("Lock error: {}", e))?;
let guard = state.lock().map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
let settings = store.get_settings()?;
Ok(settings.qobuz_tos_accepted)
}
@@ -112,6 +131,7 @@ pub fn set_qobuz_tos_accepted(
state: tauri::State<'_, LegalSettingsState>,
accepted: bool,
) -> Result<(), String> {
let store = state.lock().map_err(|e| format!("Lock error: {}", e))?;
let guard = state.lock().map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_qobuz_tos_accepted(accepted)
}
+47 -12
View File
@@ -4,6 +4,7 @@
use rusqlite::{Connection, params};
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::sync::{Arc, Mutex};
use log::info;
@@ -64,15 +65,11 @@ pub struct PlaybackPreferencesStore {
}
impl PlaybackPreferencesStore {
pub fn new() -> Result<Self, String> {
let data_dir = dirs::data_dir()
.ok_or("Could not determine data directory")?
.join("qbz");
std::fs::create_dir_all(&data_dir)
fn open_at(dir: &Path, db_name: &str) -> Result<Self, String> {
std::fs::create_dir_all(dir)
.map_err(|e| format!("Failed to create data directory: {}", e))?;
let db_path = data_dir.join("playback_preferences.db");
let db_path = dir.join(db_name);
let conn = Connection::open(&db_path)
.map_err(|e| format!("Failed to open playback preferences database: {}", e))?;
@@ -118,6 +115,17 @@ impl PlaybackPreferencesStore {
Ok(Self { conn })
}
pub fn new() -> Result<Self, String> {
let data_dir = dirs::data_dir()
.ok_or("Could not determine data directory")?
.join("qbz");
Self::open_at(&data_dir, "playback_preferences.db")
}
pub fn new_at(base_dir: &Path) -> Result<Self, String> {
Self::open_at(base_dir, "playback_preferences.db")
}
pub fn get_preferences(&self) -> Result<PlaybackPreferences, String> {
self.conn
.query_row(
@@ -158,27 +166,54 @@ impl PlaybackPreferencesStore {
/// Global state wrapper for thread-safe access
pub struct PlaybackPreferencesState {
store: Arc<Mutex<PlaybackPreferencesStore>>,
pub store: Arc<Mutex<Option<PlaybackPreferencesStore>>>,
}
impl PlaybackPreferencesState {
pub fn new() -> Result<Self, String> {
let store = PlaybackPreferencesStore::new()?;
Ok(Self {
store: Arc::new(Mutex::new(store)),
store: Arc::new(Mutex::new(Some(store))),
})
}
pub fn new_empty() -> Self {
Self {
store: Arc::new(Mutex::new(None)),
}
}
pub fn init_at(&self, base_dir: &Path) -> Result<(), String> {
let new_store = PlaybackPreferencesStore::new_at(base_dir)?;
let mut guard = self.store.lock()
.map_err(|_| "Failed to lock playback preferences store".to_string())?;
*guard = Some(new_store);
Ok(())
}
pub fn teardown(&self) -> Result<(), String> {
let mut guard = self.store.lock()
.map_err(|_| "Failed to lock playback preferences store".to_string())?;
*guard = None;
Ok(())
}
pub fn get_preferences(&self) -> Result<PlaybackPreferences, String> {
self.store.lock().unwrap().get_preferences()
let guard = self.store.lock().map_err(|_| "Failed to lock playback preferences store".to_string())?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.get_preferences()
}
pub fn set_autoplay_mode(&self, mode: AutoplayMode) -> Result<(), String> {
self.store.lock().unwrap().set_autoplay_mode(mode)
let guard = self.store.lock().map_err(|_| "Failed to lock playback preferences store".to_string())?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_autoplay_mode(mode)
}
pub fn set_show_context_icon(&self, show: bool) -> Result<(), String> {
self.store.lock().unwrap().set_show_context_icon(show)
let guard = self.store.lock().map_err(|_| "Failed to lock playback preferences store".to_string())?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_show_context_icon(show)
}
}
+116 -46
View File
@@ -9,6 +9,7 @@ use base64::Engine;
use rand::RngCore;
use rusqlite::{params, Connection};
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::sync::{Arc, Mutex};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
@@ -35,15 +36,11 @@ pub struct RemoteControlSettingsStore {
}
impl RemoteControlSettingsStore {
pub fn new() -> Result<Self, String> {
let data_dir = dirs::data_dir()
.ok_or("Could not determine data directory")?
.join("qbz");
std::fs::create_dir_all(&data_dir)
fn open_at(dir: &Path, db_name: &str) -> Result<Self, String> {
std::fs::create_dir_all(dir)
.map_err(|e| format!("Failed to create data directory: {}", e))?;
let db_path = data_dir.join("remote_control_settings.db");
let db_path = dir.join(db_name);
let conn = Connection::open(&db_path)
.map_err(|e| format!("Failed to open remote control settings database: {}", e))?;
@@ -69,6 +66,17 @@ impl RemoteControlSettingsStore {
Ok(Self { conn })
}
pub fn new() -> Result<Self, String> {
let data_dir = dirs::data_dir()
.ok_or("Could not determine data directory")?
.join("qbz");
Self::open_at(&data_dir, "remote_control_settings.db")
}
pub fn new_at(base_dir: &Path) -> Result<Self, String> {
Self::open_at(base_dir, "remote_control_settings.db")
}
pub fn get_settings(&self) -> Result<RemoteControlSettings, String> {
let mut settings = self.conn
.query_row(
@@ -146,49 +154,75 @@ impl RemoteControlSettingsStore {
/// Global state wrapper for thread-safe access
pub struct RemoteControlSettingsState {
store: Arc<Mutex<RemoteControlSettingsStore>>,
pub store: Arc<Mutex<Option<RemoteControlSettingsStore>>>,
}
impl RemoteControlSettingsState {
pub fn new() -> Result<Self, String> {
Ok(Self {
store: Arc::new(Mutex::new(RemoteControlSettingsStore::new()?)),
store: Arc::new(Mutex::new(Some(RemoteControlSettingsStore::new()?))),
})
}
pub fn new_empty() -> Self {
Self {
store: Arc::new(Mutex::new(None)),
}
}
pub fn init_at(&self, base_dir: &Path) -> Result<(), String> {
let new_store = RemoteControlSettingsStore::new_at(base_dir)?;
let mut guard = self.store.lock()
.map_err(|_| "Failed to lock remote control settings store".to_string())?;
*guard = Some(new_store);
Ok(())
}
pub fn teardown(&self) -> Result<(), String> {
let mut guard = self.store.lock()
.map_err(|_| "Failed to lock remote control settings store".to_string())?;
*guard = None;
Ok(())
}
pub fn get_settings(&self) -> Result<RemoteControlSettings, String> {
self.store
let guard = self.store
.lock()
.map_err(|e| format!("Lock error: {}", e))?
.get_settings()
.map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.get_settings()
}
pub fn set_enabled(&self, enabled: bool) -> Result<(), String> {
self.store
let guard = self.store
.lock()
.map_err(|e| format!("Lock error: {}", e))?
.set_enabled(enabled)
.map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_enabled(enabled)
}
pub fn set_port(&self, port: u16) -> Result<(), String> {
self.store
let guard = self.store
.lock()
.map_err(|e| format!("Lock error: {}", e))?
.set_port(port)
.map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_port(port)
}
pub fn set_secure(&self, secure: bool) -> Result<(), String> {
self.store
let guard = self.store
.lock()
.map_err(|e| format!("Lock error: {}", e))?
.set_secure(secure)
.map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_secure(secure)
}
pub fn regenerate_token(&self) -> Result<String, String> {
self.store
let guard = self.store
.lock()
.map_err(|e| format!("Lock error: {}", e))?
.regenerate_token()
.map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.regenerate_token()
}
}
@@ -250,15 +284,11 @@ pub struct AllowedOriginsStore {
}
impl AllowedOriginsStore {
pub fn new() -> Result<Self, String> {
let data_dir = dirs::data_dir()
.ok_or("Could not determine data directory")?
.join("qbz");
std::fs::create_dir_all(&data_dir)
fn open_at(dir: &Path, db_name: &str) -> Result<Self, String> {
std::fs::create_dir_all(dir)
.map_err(|e| format!("Failed to create data directory: {}", e))?;
let db_path = data_dir.join("remote_control_settings.db");
let db_path = dir.join(db_name);
let conn = Connection::open(&db_path)
.map_err(|e| format!("Failed to open allowed origins database: {}", e))?;
@@ -290,6 +320,17 @@ impl AllowedOriginsStore {
Ok(Self { conn })
}
pub fn new() -> Result<Self, String> {
let data_dir = dirs::data_dir()
.ok_or("Could not determine data directory")?
.join("qbz");
Self::open_at(&data_dir, "remote_control_settings.db")
}
pub fn new_at(base_dir: &Path) -> Result<Self, String> {
Self::open_at(base_dir, "remote_control_settings.db")
}
/// Get all allowed origins
pub fn get_origins(&self) -> Result<Vec<AllowedOrigin>, String> {
let mut stmt = self.conn
@@ -372,48 +413,77 @@ impl AllowedOriginsStore {
/// Global state wrapper for thread-safe access to allowed origins
pub struct AllowedOriginsState {
store: Arc<Mutex<AllowedOriginsStore>>,
pub store: Arc<Mutex<Option<AllowedOriginsStore>>>,
}
impl AllowedOriginsState {
pub fn new() -> Result<Self, String> {
Ok(Self {
store: Arc::new(Mutex::new(AllowedOriginsStore::new()?)),
store: Arc::new(Mutex::new(Some(AllowedOriginsStore::new()?))),
})
}
pub fn new_empty() -> Self {
Self {
store: Arc::new(Mutex::new(None)),
}
}
pub fn init_at(&self, base_dir: &Path) -> Result<(), String> {
let new_store = AllowedOriginsStore::new_at(base_dir)?;
let mut guard = self.store.lock()
.map_err(|_| "Failed to lock allowed origins store".to_string())?;
*guard = Some(new_store);
Ok(())
}
pub fn teardown(&self) -> Result<(), String> {
let mut guard = self.store.lock()
.map_err(|_| "Failed to lock allowed origins store".to_string())?;
*guard = None;
Ok(())
}
pub fn get_origins(&self) -> Result<Vec<AllowedOrigin>, String> {
self.store
let guard = self.store
.lock()
.map_err(|e| format!("Lock error: {}", e))?
.get_origins()
.map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.get_origins()
}
pub fn is_origin_allowed(&self, origin: &str) -> bool {
self.store
.lock()
.map(|s| s.is_origin_allowed(origin))
.map(|guard| {
guard.as_ref()
.map(|s| s.is_origin_allowed(origin))
.unwrap_or(false)
})
.unwrap_or(false)
}
pub fn add_origin(&self, origin: &str) -> Result<AllowedOrigin, String> {
self.store
let guard = self.store
.lock()
.map_err(|e| format!("Lock error: {}", e))?
.add_origin(origin)
.map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.add_origin(origin)
}
pub fn remove_origin(&self, id: i64) -> Result<(), String> {
self.store
let guard = self.store
.lock()
.map_err(|e| format!("Lock error: {}", e))?
.remove_origin(id)
.map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.remove_origin(id)
}
pub fn restore_defaults(&self) -> Result<(), String> {
self.store
let guard = self.store
.lock()
.map_err(|e| format!("Lock error: {}", e))?
.restore_defaults()
.map_err(|e| format!("Lock error: {}", e))?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.restore_defaults()
}
}
+20 -9
View File
@@ -6,6 +6,7 @@
use rusqlite::{Connection, params};
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::sync::{Arc, Mutex};
const GRACE_PERIOD_SECS: i64 = 3 * 24 * 60 * 60; // 3 days
@@ -37,15 +38,11 @@ pub struct SubscriptionStateStore {
}
impl SubscriptionStateStore {
pub fn new() -> Result<Self, String> {
let data_dir = dirs::data_dir()
.ok_or("Could not determine data directory")?
.join("qbz");
std::fs::create_dir_all(&data_dir)
fn open_at(dir: &Path, db_name: &str) -> Result<Self, String> {
std::fs::create_dir_all(dir)
.map_err(|e| format!("Failed to create data directory: {}", e))?;
let db_path = data_dir.join("subscription_state.db");
let db_path = dir.join(db_name);
let conn = Connection::open(&db_path)
.map_err(|e| format!("Failed to open subscription state database: {}", e))?;
@@ -64,6 +61,17 @@ impl SubscriptionStateStore {
Ok(Self { conn })
}
pub fn new() -> Result<Self, String> {
let data_dir = dirs::data_dir()
.ok_or("Could not determine data directory")?
.join("qbz");
Self::open_at(&data_dir, "subscription_state.db")
}
pub fn new_at(base_dir: &Path) -> Result<Self, String> {
Self::open_at(base_dir, "subscription_state.db")
}
pub fn get_state(&self) -> Result<SubscriptionState, String> {
self.conn
.query_row(
@@ -138,10 +146,13 @@ impl SubscriptionStateStore {
}
}
pub type SubscriptionStateState = Arc<Mutex<SubscriptionStateStore>>;
pub type SubscriptionStateState = Arc<Mutex<Option<SubscriptionStateStore>>>;
pub fn create_subscription_state() -> Result<SubscriptionStateState, String> {
let store = SubscriptionStateStore::new()?;
Ok(Arc::new(Mutex::new(store)))
Ok(Arc::new(Mutex::new(Some(store))))
}
pub fn create_empty_subscription_state() -> SubscriptionStateState {
Arc::new(Mutex::new(None))
}
+50 -13
View File
@@ -7,6 +7,7 @@
use rusqlite::{Connection, params};
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::sync::{Arc, Mutex};
use log::info;
@@ -35,15 +36,11 @@ pub struct TraySettingsStore {
}
impl TraySettingsStore {
pub fn new() -> Result<Self, String> {
let data_dir = dirs::data_dir()
.ok_or("Could not determine data directory")?
.join("qbz");
std::fs::create_dir_all(&data_dir)
fn open_at(dir: &Path, db_name: &str) -> Result<Self, String> {
std::fs::create_dir_all(dir)
.map_err(|e| format!("Failed to create data directory: {}", e))?;
let db_path = data_dir.join("tray_settings.db");
let db_path = dir.join(db_name);
let conn = Connection::open(&db_path)
.map_err(|e| format!("Failed to open tray settings database: {}", e))?;
@@ -69,6 +66,17 @@ impl TraySettingsStore {
Ok(Self { conn })
}
pub fn new() -> Result<Self, String> {
let data_dir = dirs::data_dir()
.ok_or("Could not determine data directory")?
.join("qbz");
Self::open_at(&data_dir, "tray_settings.db")
}
pub fn new_at(base_dir: &Path) -> Result<Self, String> {
Self::open_at(base_dir, "tray_settings.db")
}
pub fn get_settings(&self) -> Result<TraySettings, String> {
self.conn
.query_row(
@@ -121,31 +129,60 @@ impl TraySettingsStore {
/// Global state wrapper for thread-safe access
pub struct TraySettingsState {
store: Arc<Mutex<TraySettingsStore>>,
pub store: Arc<Mutex<Option<TraySettingsStore>>>,
}
impl TraySettingsState {
pub fn new() -> Result<Self, String> {
let store = TraySettingsStore::new()?;
Ok(Self {
store: Arc::new(Mutex::new(store)),
store: Arc::new(Mutex::new(Some(store))),
})
}
pub fn new_empty() -> Self {
Self {
store: Arc::new(Mutex::new(None)),
}
}
pub fn init_at(&self, base_dir: &Path) -> Result<(), String> {
let new_store = TraySettingsStore::new_at(base_dir)?;
let mut guard = self.store.lock()
.map_err(|_| "Failed to lock tray settings store".to_string())?;
*guard = Some(new_store);
Ok(())
}
pub fn teardown(&self) -> Result<(), String> {
let mut guard = self.store.lock()
.map_err(|_| "Failed to lock tray settings store".to_string())?;
*guard = None;
Ok(())
}
pub fn get_settings(&self) -> Result<TraySettings, String> {
self.store.lock().unwrap().get_settings()
let guard = self.store.lock().map_err(|_| "Failed to lock tray settings store".to_string())?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.get_settings()
}
pub fn set_enable_tray(&self, value: bool) -> Result<(), String> {
self.store.lock().unwrap().set_enable_tray(value)
let guard = self.store.lock().map_err(|_| "Failed to lock tray settings store".to_string())?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_enable_tray(value)
}
pub fn set_minimize_to_tray(&self, value: bool) -> Result<(), String> {
self.store.lock().unwrap().set_minimize_to_tray(value)
let guard = self.store.lock().map_err(|_| "Failed to lock tray settings store".to_string())?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_minimize_to_tray(value)
}
pub fn set_close_to_tray(&self, value: bool) -> Result<(), String> {
self.store.lock().unwrap().set_close_to_tray(value)
let guard = self.store.lock().map_err(|_| "Failed to lock tray settings store".to_string())?;
let store = guard.as_ref().ok_or("No active session - please log in")?;
store.set_close_to_tray(value)
}
}