mod commands; mod detection; mod processing; mod state; mod tray; use recorder_core::pipeline; use recorder_core::secrets::KeyringSecrets; use recorder_core::store::Store; use tauri::{Manager, WindowEvent}; use tauri_plugin_autostart::MacosLauncher; use tauri_plugin_log::{RotationStrategy, Target, TargetKind, TimezoneStrategy}; use crate::state::AppState; /// Command-line flag the login item starts the app with; the main window /// then stays hidden in the tray. const AUTOSTART_ARG: &str = "--autostart"; /// Size at which the log file is rotated; two old files are kept. const LOG_FILE_SIZE: u128 = 1_000_000; #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { tauri::Builder::default() // Must be registered first: a second launch only focuses the first. .plugin(tauri_plugin_single_instance::init(|app, _argv, _cwd| { commands::show_main(app); })) .plugin(tauri_plugin_autostart::init( MacosLauncher::LaunchAgent, Some(vec![AUTOSTART_ARG]), )) .plugin(tauri_plugin_notification::init()) .setup(|app| { // Release builds log to a file in the app log directory (on // Windows %LOCALAPPDATA%\eu.euvino.callrecorder\logs), debug // builds to stdout as well. Never log API keys or transcripts. let mut targets = vec![Target::new(TargetKind::LogDir { file_name: None })]; if cfg!(debug_assertions) { targets.push(Target::new(TargetKind::Stdout)); } app.handle().plugin( tauri_plugin_log::Builder::new() .targets(targets) .level(log::LevelFilter::Info) .max_file_size(LOG_FILE_SIZE) .rotation_strategy(RotationStrategy::KeepSome(2)) .timezone_strategy(TimezoneStrategy::UseLocal) .build(), )?; let data_dir = app.path().app_data_dir()?; let config_dir = app.path().app_config_dir()?; std::fs::create_dir_all(&data_dir)?; let store = Store::open(&data_dir.join("calls.sqlite"))?; let recovery = pipeline::recover_on_startup(&store)?; log::info!("startup recovery: {recovery:?}"); let state = AppState::new( store, Box::new(KeyringSecrets), config_dir.join("settings.json"), data_dir.join("recordings"), ); let autostart = state.settings().autostart; app.manage(state); commands::apply_autostart(app.handle(), autostart); processing::delete_leftover_jobs(app.handle(), recovery.failed); tray::create(app.handle())?; detection::spawn(app.handle().clone()); if !std::env::args().any(|arg| arg == AUTOSTART_ARG) { commands::show_main(app.handle()); } Ok(()) }) .on_window_event(|window, event| { // Closing a window only hides it; the app keeps running in the // tray (for call detection) until "Beenden". if let WindowEvent::CloseRequested { api, .. } = event { api.prevent_close(); let _ = window.hide(); } }) .invoke_handler(tauri::generate_handler![ commands::get_settings, commands::save_settings, commands::set_api_key, commands::has_api_key, commands::list_devices, commands::test_devices, commands::get_recording_state, commands::start_manual_recording, commands::stop_recording, commands::discard_current_recording, commands::get_pending_prompt, commands::get_call_brief, commands::discard_call, commands::transcribe_call, commands::retry_call, ]) .run(tauri::generate_context!()) .expect("error while building tauri application"); }