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:
|
||||
//! - User credentials (encrypted)
|
||||
//! - Audio preferences
|
||||
//! - Download preferences
|
||||
//! - UI preferences
|
||||
//! - Local playlists
|
||||
//! - Cached favorites
|
||||
|
||||
pub mod audio_settings;
|
||||
pub mod download_settings;
|
||||
|
||||
pub use audio_settings::{
|
||||
AudioSettings,
|
||||
@@ -18,3 +20,12 @@ pub use audio_settings::{
|
||||
set_audio_dac_passthrough,
|
||||
set_audio_sample_rate,
|
||||
};
|
||||
|
||||
pub use download_settings::{
|
||||
DownloadSettings,
|
||||
DownloadSettingsState,
|
||||
get_download_settings,
|
||||
set_download_root,
|
||||
set_show_downloads_in_library,
|
||||
validate_download_root,
|
||||
};
|
||||
Reference in new issue
Block a user