Files
hoerwerk/src-tauri/src/lib.rs

116 lines
3.4 KiB
Rust

mod audible;
mod download;
use audible::{AudibleClient, AuthStatus, Book, CliAudibleClient};
use download::{DownloadMgr, SharedDownloadMgr};
use tauri::Manager;
#[tauri::command]
fn auth_status(client: tauri::State<CliAudibleClient>) -> Result<AuthStatus, String> {
client.auth_status()
}
#[tauri::command]
fn start_login(client: tauri::State<CliAudibleClient>) -> Result<bool, String> {
client.start_login()
}
#[tauri::command]
fn start_decrypt(client: tauri::State<CliAudibleClient>) -> Result<bool, String> {
client.start_decrypt()
}
#[tauri::command]
fn download_list(dl: tauri::State<SharedDownloadMgr>) -> Vec<download::DlItem> {
dl.list()
}
#[tauri::command]
fn download_enqueue(dl: tauri::State<SharedDownloadMgr>, asin: String) -> Result<download::DlItem, String> {
dl.enqueue(&asin)
}
#[tauri::command]
fn download_cancel(dl: tauri::State<SharedDownloadMgr>, asin: String) -> Result<download::DlItem, String> {
dl.cancel(&asin)
}
#[tauri::command]
fn download_delete(dl: tauri::State<SharedDownloadMgr>, asin: String) -> Result<(), String> {
dl.delete(&asin)
}
/// Bibliothek laden: externer CLI-Export dauert (Netzwerk) — läuft deshalb
/// auf einem Blocking-Thread, damit die UI nicht friert.
#[tauri::command]
async fn get_library(
client: tauri::State<'_, CliAudibleClient>,
) -> Result<Vec<Book>, String> {
let c = client.inner().clone();
tauri::async_runtime::spawn_blocking(move || c.library())
.await
.map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))?
}
/// Lokale Titel auch ohne Netz/Anmeldung — ffprobe-basiert.
#[tauri::command]
async fn get_local_books() -> Result<Vec<Book>, String> {
tauri::async_runtime::spawn_blocking(audible::local_books)
.await
.map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))
}
/// Titel, die Audible bewusst nicht als Datei liefert (Nur-Streaming).
#[tauri::command]
async fn stream_only() -> Result<Vec<String>, String> {
tauri::async_runtime::spawn_blocking(download::stream_only_load)
.await
.map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))
}
#[tauri::command]
async fn get_alter_notes() -> Result<audible::Labels, String> {
tauri::async_runtime::spawn_blocking(audible::alter_notes)
.await
.map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))
}
#[tauri::command]
async fn set_alter_note(
asin: String,
note: Option<String>,
) -> Result<(), String> {
tauri::async_runtime::spawn_blocking(move || audible::set_alter_note(&asin, note))
.await
.map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))?
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_opener::init())
.manage(CliAudibleClient::new())
.manage(DownloadMgr::new())
.setup(|app| {
app.state::<SharedDownloadMgr>()
.attach(app.handle().clone());
Ok(())
})
.invoke_handler(tauri::generate_handler![
auth_status,
start_login,
start_decrypt,
get_library,
get_local_books,
stream_only,
get_alter_notes,
set_alter_note,
download_list,
download_enqueue,
download_cancel,
download_delete
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}