feat(downloads): phase 1 - database schema and settings store
Add foundation for Qobuz downloads integration: - Create DownloadSettings store (download_root, show_in_library) - Add migration for local_tracks: source and qobuz_track_id columns - Add database indices for source and qobuz_track_id - Register Tauri commands for download settings - Commands: get/set download_settings, validate_download_root Phase 1 of downloads-library integration plan. cp
This commit is contained in:
1 parent
7eb12b36bf
commit
494d3d184e
4 files changed
+213
No files matched your search
@@ -0,0 +1,173 @@
|
|||||||
|
//! Download settings persistence
|
||||||
|
//!
|
||||||
|
//! Stores user preferences for download path and library integration.
|
||||||
|
|
||||||
|
use rusqlite::{Connection, params};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct DownloadSettings {
|
||||||
|
pub download_root: String,
|
||||||
|
pub show_in_library: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for DownloadSettings {
|
||||||
|
fn default() -> Self {
|
||||||
|
let default_root = dirs::cache_dir()
|
||||||
|
.unwrap_or_else(|| std::path::PathBuf::from("."))
|
||||||
|
.join("qbz-nix")
|
||||||
|
.join("audio")
|
||||||
|
.to_string_lossy()
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
Self {
|
||||||
|
download_root: default_root,
|
||||||
|
show_in_library: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct DownloadSettingsStore {
|
||||||
|
conn: Connection,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DownloadSettingsStore {
|
||||||
|
pub fn new() -> Result<Self, String> {
|
||||||
|
let data_dir = dirs::data_dir()
|
||||||
|
.ok_or("Could not determine data directory")?
|
||||||
|
.join("qbz-nix");
|
||||||
|
|
||||||
|
std::fs::create_dir_all(&data_dir)
|
||||||
|
.map_err(|e| format!("Failed to create data directory: {}", e))?;
|
||||||
|
|
||||||
|
let db_path = data_dir.join("download_settings.db");
|
||||||
|
let conn = Connection::open(&db_path)
|
||||||
|
.map_err(|e| format!("Failed to open download settings database: {}", e))?;
|
||||||
|
|
||||||
|
let default_settings = DownloadSettings::default();
|
||||||
|
|
||||||
|
conn.execute_batch(
|
||||||
|
"CREATE TABLE IF NOT EXISTS download_settings (
|
||||||
|
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||||
|
download_root TEXT NOT NULL,
|
||||||
|
show_in_library INTEGER NOT NULL DEFAULT 0
|
||||||
|
);"
|
||||||
|
).map_err(|e| format!("Failed to create download settings table: {}", e))?;
|
||||||
|
|
||||||
|
conn.execute(
|
||||||
|
"INSERT OR IGNORE INTO download_settings (id, download_root, show_in_library)
|
||||||
|
VALUES (1, ?1, 0)",
|
||||||
|
params![default_settings.download_root],
|
||||||
|
).map_err(|e| format!("Failed to initialize download settings: {}", e))?;
|
||||||
|
|
||||||
|
Ok(Self { conn })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_settings(&self) -> Result<DownloadSettings, String> {
|
||||||
|
self.conn
|
||||||
|
.query_row(
|
||||||
|
"SELECT download_root, show_in_library FROM download_settings WHERE id = 1",
|
||||||
|
[],
|
||||||
|
|row| {
|
||||||
|
Ok(DownloadSettings {
|
||||||
|
download_root: row.get(0)?,
|
||||||
|
show_in_library: row.get::<_, i64>(1)? != 0,
|
||||||
|
})
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.map_err(|e| format!("Failed to get download settings: {}", e))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_download_root(&self, path: &str) -> Result<(), String> {
|
||||||
|
self.conn
|
||||||
|
.execute(
|
||||||
|
"UPDATE download_settings SET download_root = ?1 WHERE id = 1",
|
||||||
|
params![path],
|
||||||
|
)
|
||||||
|
.map_err(|e| format!("Failed to set download root: {}", e))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_show_in_library(&self, show: bool) -> Result<(), String> {
|
||||||
|
self.conn
|
||||||
|
.execute(
|
||||||
|
"UPDATE download_settings SET show_in_library = ?1 WHERE id = 1",
|
||||||
|
params![show as i64],
|
||||||
|
)
|
||||||
|
.map_err(|e| format!("Failed to set show_in_library: {}", e))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type DownloadSettingsState = Arc<Mutex<DownloadSettingsStore>>;
|
||||||
|
|
||||||
|
pub fn create_download_settings_state() -> Result<DownloadSettingsState, String> {
|
||||||
|
let store = DownloadSettingsStore::new()?;
|
||||||
|
Ok(Arc::new(Mutex::new(store)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tauri commands
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
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))?;
|
||||||
|
store.get_settings()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn set_download_root(
|
||||||
|
path: String,
|
||||||
|
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() {
|
||||||
|
return Err("Path does not exist".to_string());
|
||||||
|
}
|
||||||
|
if !path_obj.is_dir() {
|
||||||
|
return Err("Path is not a directory".to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
let store = state.lock().map_err(|e| format!("Lock error: {}", e))?;
|
||||||
|
store.set_download_root(&path)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn set_show_downloads_in_library(
|
||||||
|
show: bool,
|
||||||
|
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))?;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
if !path_obj.is_dir() {
|
||||||
|
return Err("Path exists but is not a directory".to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check write permissions by trying to create a temp file
|
||||||
|
let test_file = path_obj.join(".qbz_write_test");
|
||||||
|
match std::fs::write(&test_file, b"test") {
|
||||||
|
Ok(_) => {
|
||||||
|
let _ = std::fs::remove_file(&test_file);
|
||||||
|
Ok(true)
|
||||||
|
}
|
||||||
|
Err(e) => Err(format!("No write permission: {}", e)),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,11 +3,13 @@
|
|||||||
//! Handles:
|
//! Handles:
|
||||||
//! - User credentials (encrypted)
|
//! - User credentials (encrypted)
|
||||||
//! - Audio preferences
|
//! - Audio preferences
|
||||||
|
//! - Download preferences
|
||||||
//! - UI preferences
|
//! - UI preferences
|
||||||
//! - Local playlists
|
//! - Local playlists
|
||||||
//! - Cached favorites
|
//! - Cached favorites
|
||||||
|
|
||||||
pub mod audio_settings;
|
pub mod audio_settings;
|
||||||
|
pub mod download_settings;
|
||||||
|
|
||||||
pub use audio_settings::{
|
pub use audio_settings::{
|
||||||
AudioSettings,
|
AudioSettings,
|
||||||
@@ -18,3 +20,12 @@ pub use audio_settings::{
|
|||||||
set_audio_dac_passthrough,
|
set_audio_dac_passthrough,
|
||||||
set_audio_sample_rate,
|
set_audio_sample_rate,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub use download_settings::{
|
||||||
|
DownloadSettings,
|
||||||
|
DownloadSettingsState,
|
||||||
|
get_download_settings,
|
||||||
|
set_download_root,
|
||||||
|
set_show_downloads_in_library,
|
||||||
|
validate_download_root,
|
||||||
|
};
|
||||||
@@ -137,6 +137,9 @@ pub fn run() {
|
|||||||
// Initialize audio settings state
|
// Initialize audio settings state
|
||||||
let audio_settings_state = config::audio_settings::AudioSettingsState::new()
|
let audio_settings_state = config::audio_settings::AudioSettingsState::new()
|
||||||
.expect("Failed to initialize audio settings");
|
.expect("Failed to initialize audio settings");
|
||||||
|
// Initialize download settings state
|
||||||
|
let download_settings_state = config::download_settings::create_download_settings_state()
|
||||||
|
.expect("Failed to initialize download settings");
|
||||||
// Initialize API keys state (for user-provided credentials)
|
// Initialize API keys state (for user-provided credentials)
|
||||||
let api_keys_state = api_keys::create_api_keys_state();
|
let api_keys_state = api_keys::create_api_keys_state();
|
||||||
|
|
||||||
@@ -248,6 +251,7 @@ pub fn run() {
|
|||||||
.manage(api_cache_state)
|
.manage(api_cache_state)
|
||||||
.manage(session_store_state)
|
.manage(session_store_state)
|
||||||
.manage(audio_settings_state)
|
.manage(audio_settings_state)
|
||||||
|
.manage(download_settings_state)
|
||||||
.manage(api_keys_state)
|
.manage(api_keys_state)
|
||||||
.invoke_handler(tauri::generate_handler![
|
.invoke_handler(tauri::generate_handler![
|
||||||
// Auth commands
|
// Auth commands
|
||||||
@@ -459,6 +463,11 @@ pub fn run() {
|
|||||||
config::audio_settings::set_audio_exclusive_mode,
|
config::audio_settings::set_audio_exclusive_mode,
|
||||||
config::audio_settings::set_audio_dac_passthrough,
|
config::audio_settings::set_audio_dac_passthrough,
|
||||||
config::audio_settings::set_audio_sample_rate,
|
config::audio_settings::set_audio_sample_rate,
|
||||||
|
// Download settings commands
|
||||||
|
config::download_settings::get_download_settings,
|
||||||
|
config::download_settings::set_download_root,
|
||||||
|
config::download_settings::set_show_downloads_in_library,
|
||||||
|
config::download_settings::validate_download_root,
|
||||||
// API keys commands (user-provided credentials)
|
// API keys commands (user-provided credentials)
|
||||||
api_keys::set_spotify_credentials,
|
api_keys::set_spotify_credentials,
|
||||||
api_keys::clear_spotify_credentials,
|
api_keys::clear_spotify_credentials,
|
||||||
|
|||||||
@@ -127,6 +127,26 @@ impl LibraryDatabase {
|
|||||||
|
|
||||||
/// Run schema migrations for existing databases
|
/// Run schema migrations for existing databases
|
||||||
fn run_migrations(&self) -> Result<(), LibraryError> {
|
fn run_migrations(&self) -> Result<(), LibraryError> {
|
||||||
|
// Migration: Add qobuz download tracking fields
|
||||||
|
let has_source: bool = self.conn
|
||||||
|
.query_row(
|
||||||
|
"SELECT COUNT(*) FROM pragma_table_info('local_tracks') WHERE name = 'source'",
|
||||||
|
[],
|
||||||
|
|row| row.get::<_, i32>(0),
|
||||||
|
)
|
||||||
|
.map(|count| count > 0)
|
||||||
|
.unwrap_or(false);
|
||||||
|
|
||||||
|
if !has_source {
|
||||||
|
log::info!("Running migration: adding source and qobuz_track_id to local_tracks");
|
||||||
|
self.conn.execute_batch(
|
||||||
|
"ALTER TABLE local_tracks ADD COLUMN source TEXT DEFAULT 'user';
|
||||||
|
ALTER TABLE local_tracks ADD COLUMN qobuz_track_id INTEGER;
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_tracks_source ON local_tracks(source);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_tracks_qobuz_id ON local_tracks(qobuz_track_id);"
|
||||||
|
).map_err(|e| LibraryError::Database(format!("Migration failed: {}", e)))?;
|
||||||
|
}
|
||||||
|
|
||||||
// Check if playlist_settings has the 'hidden' column (added in v2)
|
// Check if playlist_settings has the 'hidden' column (added in v2)
|
||||||
let has_hidden: bool = self.conn
|
let has_hidden: bool = self.conn
|
||||||
.query_row(
|
.query_row(
|
||||||
|
|||||||
Reference in new issue
Block a user