add UserDataPaths module for per-user data isolation

Central path provider that holds the current user_id behind RwLock.
Provides user-scoped data and cache directory paths:
- ~/.local/share/qbz/users/{uid}/
- ~/.cache/qbz/users/{uid}/

Registered as Tauri managed state. No behavior change yet -
user paths are not used by any module until activate_user_session
is implemented.
This commit is contained in:
vicrodh committed 2026-02-05 22:38:39 -06:00
1 parent b988eb7896
commit 441acafba5
2 files changed
+90

No files matched your search

+5
View File
@@ -34,6 +34,7 @@ pub mod session_store;
pub mod share;
pub mod tray;
pub mod updates;
pub mod user_data;
pub mod visualizer;
use std::sync::Arc;
@@ -285,11 +286,15 @@ pub fn run() {
let enable_tray = tray_settings.enable_tray;
let close_to_tray = tray_settings.close_to_tray;
// Initialize per-user data paths (no user active yet until login)
let user_data_paths = user_data::UserDataPaths::new();
tauri::Builder::default()
.plugin(tauri_plugin_opener::init())
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_clipboard_manager::init())
.manage(AppState::with_device_and_settings(saved_device, audio_settings))
.manage(user_data_paths)
.setup(move |app| {
// Initialize system tray icon (only if enabled)
if enable_tray {
+85
View File
@@ -0,0 +1,85 @@
//! Per-user data path management
//!
//! Each Qobuz user gets their own subdirectory under the app's data/cache paths.
//! This module provides the central path provider that all per-user state modules
//! use to determine where to store their databases.
use std::path::PathBuf;
use std::sync::RwLock;
/// Central path provider for per-user data isolation.
///
/// Holds the current user_id and provides methods to get
/// user-scoped data and cache directories.
pub struct UserDataPaths {
user_id: RwLock<Option<u64>>,
}
impl UserDataPaths {
pub fn new() -> Self {
Self {
user_id: RwLock::new(None),
}
}
/// Set the current user after login
pub fn set_user(&self, user_id: u64) {
*self.user_id.write().expect("UserDataPaths write lock poisoned") = Some(user_id);
log::info!("UserDataPaths: active user set to {}", user_id);
}
/// Clear the current user on logout
pub fn clear_user(&self) {
*self.user_id.write().expect("UserDataPaths write lock poisoned") = None;
log::info!("UserDataPaths: active user cleared");
}
/// Get the current user ID, if set
pub fn current_user_id(&self) -> Option<u64> {
*self.user_id.read().expect("UserDataPaths read lock poisoned")
}
/// Get the user-scoped data directory: ~/.local/share/qbz/users/{uid}/
pub fn user_data_dir(&self) -> Result<PathBuf, String> {
let uid = self.user_id.read()
.map_err(|e| format!("UserDataPaths read lock error: {}", e))?
.ok_or("No active user - please log in")?;
let base = dirs::data_dir()
.ok_or("Could not determine data directory")?
.join("qbz")
.join("users")
.join(uid.to_string());
Ok(base)
}
/// Get the user-scoped cache directory: ~/.cache/qbz/users/{uid}/
pub fn user_cache_dir(&self) -> Result<PathBuf, String> {
let uid = self.user_id.read()
.map_err(|e| format!("UserDataPaths read lock error: {}", e))?
.ok_or("No active user - please log in")?;
let base = dirs::cache_dir()
.ok_or("Could not determine cache directory")?
.join("qbz")
.join("users")
.join(uid.to_string());
Ok(base)
}
/// Get the global (non-user-scoped) data directory: ~/.local/share/qbz/
pub fn global_data_dir() -> Result<PathBuf, String> {
dirs::data_dir()
.ok_or_else(|| "Could not determine data directory".to_string())
.map(|d| d.join("qbz"))
}
/// Get the global (non-user-scoped) cache directory: ~/.cache/qbz/
pub fn global_cache_dir() -> Result<PathBuf, String> {
dirs::cache_dir()
.ok_or_else(|| "Could not determine cache directory".to_string())
.map(|d| d.join("qbz"))
}
}