mod audible; mod bookmarks; mod download; mod player; use audible::{AudibleClient, AuthStatus, Book, CliAudibleClient}; use download::{DownloadMgr, SharedDownloadMgr}; use player::{MpvPlayer, Player, PlayerState, SharedPlayer}; use std::collections::BTreeMap; use tauri::Manager; use tauri::menu::{Menu, MenuItem}; use tauri::tray::TrayIconBuilder; use tauri_plugin_autostart::ManagerExt; #[tauri::command] fn auth_status(client: tauri::State) -> Result { client.auth_status() } #[tauri::command] fn start_login(client: tauri::State) -> Result { client.start_login() } #[tauri::command] fn start_decrypt(client: tauri::State) -> Result { client.start_decrypt() } #[tauri::command] fn download_list(dl: tauri::State) -> Vec { dl.list() } #[tauri::command] fn download_enqueue(dl: tauri::State, asin: String) -> Result { dl.enqueue(&asin) } #[tauri::command] fn download_cancel(dl: tauri::State, asin: String) -> Result { dl.cancel(&asin) } #[tauri::command] fn download_delete(dl: tauri::State, 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, 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, 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, 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 { 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, ) -> Result<(), String> { tauri::async_runtime::spawn_blocking(move || audible::set_alter_note(&asin, note)) .await .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))? } // — Player (T-020/T-021) — #[tauri::command] async fn player_load( pl: tauri::State<'_, SharedPlayer>, asin: String, title: String, paused: bool, ) -> Result { let p = pl.inner().clone(); tauri::async_runtime::spawn_blocking(move || p.load(&asin, &title, paused)) .await .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))? } #[tauri::command] async fn player_toggle(pl: tauri::State<'_, SharedPlayer>) -> Result { let p = pl.inner().clone(); tauri::async_runtime::spawn_blocking(move || p.toggle()) .await .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))? } #[tauri::command] async fn player_seek( pl: tauri::State<'_, SharedPlayer>, secs: f64, ) -> Result { let p = pl.inner().clone(); tauri::async_runtime::spawn_blocking(move || p.seek_by(secs)) .await .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))? } #[tauri::command] async fn player_set_position( pl: tauri::State<'_, SharedPlayer>, pos: f64, ) -> Result { let p = pl.inner().clone(); tauri::async_runtime::spawn_blocking(move || p.set_position(pos)) .await .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))? } #[tauri::command] async fn player_set_chapter( pl: tauri::State<'_, SharedPlayer>, idx: i64, ) -> Result { let p = pl.inner().clone(); tauri::async_runtime::spawn_blocking(move || p.set_chapter(idx)) .await .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))? } #[tauri::command] async fn player_set_speed( pl: tauri::State<'_, SharedPlayer>, speed: f64, ) -> Result { let p = pl.inner().clone(); tauri::async_runtime::spawn_blocking(move || p.set_speed(speed)) .await .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))? } #[tauri::command] async fn player_stop(pl: tauri::State<'_, SharedPlayer>) -> Result { let p = pl.inner().clone(); tauri::async_runtime::spawn_blocking(move || p.stop()) .await .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))? } /// Serien-Bulk (24 h-Cache) für die Bibliotheksgruppierung. #[tauri::command] async fn book_series_bulk(refresh: bool) -> Result, String> { tauri::async_runtime::spawn_blocking(move || audible::series_bulk(refresh)) .await .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))? } // — Browser-Login in der App — #[tauri::command] async fn start_browser_login(app: tauri::AppHandle) -> Result<(), String> { let dir = audible::default_auth_dir(); tauri::async_runtime::spawn_blocking(move || audible::start_browser_login(app, &dir)) .await .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))? } #[tauri::command] async fn submit_login_url(paste: String) -> Result<(), String> { tauri::async_runtime::spawn_blocking(move || audible::submit_login_url(&paste)) .await .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))? } #[tauri::command] async fn cancel_browser_login() -> Result<(), String> { tauri::async_runtime::spawn_blocking(audible::cancel_browser_login) .await .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}")) } /// App-Version aus dem Cargo-Paket (bleibt per make release-version synchron). #[tauri::command] fn app_version() -> String { env!("CARGO_PKG_VERSION").to_string() } #[tauri::command] async fn get_prefs() -> Result { tauri::async_runtime::spawn_blocking(audible::prefs_load) .await .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}")) } #[tauri::command] async fn set_prefs(prefs: audible::Prefs) -> Result<(), String> { tauri::async_runtime::spawn_blocking(move || audible::prefs_save(&prefs)) .await .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))? } /// Einzeldetails (Beschreibung u. a.) per Audible-API, on demand. #[tauri::command] async fn book_details(asin: String) -> Result { tauri::async_runtime::spawn_blocking(move || audible::fetch_details(&asin)) .await .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))? } #[tauri::command] async fn bookmark_list(asin: String) -> Result, String> { tauri::async_runtime::spawn_blocking(move || bookmarks::list(&asin)) .await .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))? } #[tauri::command] async fn bookmark_add( asin: String, time: f64, note: String, ) -> Result, String> { tauri::async_runtime::spawn_blocking(move || bookmarks::add(&asin, time, ¬e)) .await .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))? } #[tauri::command] async fn bookmark_remove( asin: String, index: usize, ) -> Result, String> { tauri::async_runtime::spawn_blocking(move || bookmarks::remove(&asin, index)) .await .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))? } #[tauri::command] async fn bookmark_set_note( asin: String, index: usize, note: String, ) -> Result, String> { tauri::async_runtime::spawn_blocking(move || bookmarks::set_note(&asin, index, ¬e)) .await .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))? } #[tauri::command] async fn autostart_enabled(app: tauri::AppHandle) -> Result { app.autolaunch() .is_enabled() .map_err(|e| format!("Autostart: {e}")) } #[tauri::command] async fn autostart_set(app: tauri::AppHandle, on: bool) -> Result<(), String> { let al = app.autolaunch(); if on { al.enable().map_err(|e| format!("Autostart aktivieren: {e}")) } else { al.disable().map_err(|e| format!("Autostart deaktivieren: {e}")) } } #[tauri::command] async fn player_snapshot(pl: tauri::State<'_, SharedPlayer>) -> Result { let p = pl.inner().clone(); tauri::async_runtime::spawn_blocking(move || p.snapshot()) .await .map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}")) } #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { match tauri::Builder::default() .plugin(tauri_plugin_opener::init()) .plugin(tauri_plugin_autostart::init( tauri_plugin_autostart::MacosLauncher::LaunchAgent, None, )) .manage(CliAudibleClient::new()) .manage(DownloadMgr::new()) .manage(MpvPlayer::new()) .setup(|app| { app.state::() .attach(app.handle().clone()); let p = app.state::().inner().clone(); p.attach(app.handle().clone()); player::spawn_mpris(p.clone()); let ver = MenuItem::with_id( app, "version", format!("Hörwerk v{}", env!("CARGO_PKG_VERSION")), false, None::<&str>, )?; let show = MenuItem::with_id(app, "show", "Hörwerk anzeigen", true, None::<&str>)?; let quit = MenuItem::with_id(app, "quit", "Beenden", true, None::<&str>)?; let menu = Menu::with_items(app, &[&ver, &show, &quit])?; TrayIconBuilder::with_id("hoerwerk-tray") .icon(app.default_window_icon().unwrap().clone()) .tooltip(format!("Hörwerk v{}", env!("CARGO_PKG_VERSION"))) .menu(&menu) .show_menu_on_left_click(false) .on_menu_event(|app, ev| match ev.id().as_ref() { "show" => { if let Some(w) = app.get_webview_window("main") { let _ = w.show(); let _ = w.set_focus(); } } "quit" => app.exit(0), _ => {} }) .build(app)?; drop(show); drop(quit); // Letztes Buch pausiert fortsetzen (T-024) std::thread::Builder::new() .name("hoerwerk-resume".into()) .spawn(move || { std::thread::sleep(std::time::Duration::from_millis(1200)); let _ = p.resume_last(); }) .expect("resume thread"); Ok(()) }) .invoke_handler(tauri::generate_handler![ auth_status, start_login, start_decrypt, get_library, get_local_books, book_series_bulk, book_details, stream_only, get_prefs, set_prefs, app_version, start_browser_login, submit_login_url, cancel_browser_login, player_load, player_toggle, player_seek, player_set_position, player_set_chapter, player_set_speed, player_stop, player_snapshot, bookmark_list, bookmark_add, bookmark_remove, bookmark_set_note, get_alter_notes, set_alter_note, autostart_enabled, autostart_set, download_list, download_enqueue, download_cancel, download_delete ]) .build(tauri::generate_context!()) { Ok(app) => { app.run(|app, event| { if let tauri::RunEvent::Exit { .. } = event { app.state::().shutdown(); audible::cancel_browser_login(); } }); Ok(()) } Err(e) => Err(e), } .expect("error while running tauri application"); }