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 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, ) -> Result { let p = pl.inner().clone(); tauri::async_runtime::spawn_blocking(move || p.load(&asin, &title)) .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}"))? } /// 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() { 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()); // Tray-Icon (T-032) 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, &[&show, &quit])?; TrayIconBuilder::with_id("hoerwerk-tray") .icon(app.default_window_icon().unwrap().clone()) .tooltip("Hörwerk") .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)?; // Letztes Buch pausiert fortsetzen (T-024), sobald mpv bereit ist 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_details, stream_only, get_alter_notes, set_alter_note, player_load, player_toggle, player_seek, player_set_position, player_set_chapter, player_set_speed, player_stop, player_snapshot, autostart_enabled, autostart_set, bookmark_list, bookmark_add, bookmark_remove, bookmark_set_note, download_list, download_enqueue, download_cancel, download_delete ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); }