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

404 lines
13 KiB
Rust

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<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}"))?
}
// — Player (T-020/T-021) —
#[tauri::command]
async fn player_load(
pl: tauri::State<'_, SharedPlayer>,
asin: String,
title: String,
paused: bool,
) -> Result<PlayerState, String> {
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<PlayerState, String> {
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<PlayerState, String> {
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<PlayerState, String> {
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<PlayerState, String> {
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<PlayerState, String> {
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<PlayerState, String> {
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<BTreeMap<String, audible::SerieRef>, 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<audible::Prefs, String> {
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<audible::BookExtra, String> {
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<Vec<bookmarks::Bookmark>, 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<Vec<bookmarks::Bookmark>, String> {
tauri::async_runtime::spawn_blocking(move || bookmarks::add(&asin, time, &note))
.await
.map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))?
}
#[tauri::command]
async fn bookmark_remove(
asin: String,
index: usize,
) -> Result<Vec<bookmarks::Bookmark>, 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<Vec<bookmarks::Bookmark>, String> {
tauri::async_runtime::spawn_blocking(move || bookmarks::set_note(&asin, index, &note))
.await
.map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))?
}
#[tauri::command]
async fn autostart_enabled(app: tauri::AppHandle) -> Result<bool, String> {
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<PlayerState, String> {
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::<SharedDownloadMgr>()
.attach(app.handle().clone());
let p = app.state::<SharedPlayer>().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::<SharedPlayer>().shutdown();
audible::cancel_browser_login();
}
});
Ok(())
}
Err(e) => Err(e),
}
.expect("error while running tauri application");
}