2022-11-28 16:16:33 -08:00
|
|
|
use serde::{Serialize, Deserialize};
|
|
|
|
use tauri::State;
|
|
|
|
|
2022-12-22 16:36:32 -08:00
|
|
|
use crate::config::AppConfig;
|
2023-05-06 12:01:56 -07:00
|
|
|
use crate::credentials::{Session,BaseCredentials};
|
|
|
|
use crate::errors::*;
|
2022-12-19 16:20:46 -08:00
|
|
|
use crate::clientinfo::Client;
|
2023-05-06 12:01:56 -07:00
|
|
|
use crate::state::AppState;
|
2023-08-02 19:57:37 -07:00
|
|
|
use crate::terminal;
|
2022-11-28 16:16:33 -08:00
|
|
|
|
|
|
|
|
2022-12-22 16:36:32 -08:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2022-11-29 16:13:09 -08:00
|
|
|
pub struct Request {
|
|
|
|
pub id: u64,
|
2022-12-20 16:11:49 -08:00
|
|
|
pub clients: Vec<Option<Client>>,
|
2023-05-06 12:01:56 -07:00
|
|
|
pub base: bool,
|
2022-11-29 16:13:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-22 16:36:32 -08:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2022-11-28 16:16:33 -08:00
|
|
|
pub struct RequestResponse {
|
|
|
|
pub id: u64,
|
|
|
|
pub approval: Approval,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-22 16:36:32 -08:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2022-11-28 16:16:33 -08:00
|
|
|
pub enum Approval {
|
2022-11-27 22:03:15 -08:00
|
|
|
Approved,
|
|
|
|
Denied,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-28 16:16:33 -08:00
|
|
|
#[tauri::command]
|
2023-05-02 11:33:18 -07:00
|
|
|
pub async fn respond(response: RequestResponse, app_state: State<'_, AppState>) -> Result<(), SendResponseError> {
|
|
|
|
app_state.send_response(response).await
|
2022-11-28 16:16:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
2022-12-29 16:40:48 -08:00
|
|
|
pub async fn unlock(passphrase: String, app_state: State<'_, AppState>) -> Result<(), UnlockError> {
|
2023-05-01 23:03:34 -07:00
|
|
|
app_state.unlock(&passphrase).await
|
2022-12-03 21:47:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
2023-05-02 15:24:35 -07:00
|
|
|
pub async fn get_session_status(app_state: State<'_, AppState>) -> Result<String, ()> {
|
|
|
|
let session = app_state.session.read().await;
|
|
|
|
let status = match *session {
|
2022-12-03 21:47:09 -08:00
|
|
|
Session::Locked(_) => "locked".into(),
|
2023-05-01 23:03:34 -07:00
|
|
|
Session::Unlocked{..} => "unlocked".into(),
|
2022-12-03 21:47:09 -08:00
|
|
|
Session::Empty => "empty".into()
|
2023-05-02 15:24:35 -07:00
|
|
|
};
|
|
|
|
Ok(status)
|
2022-11-28 16:16:33 -08:00
|
|
|
}
|
|
|
|
|
2022-12-13 21:50:34 -08:00
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
pub async fn save_credentials(
|
2023-05-01 23:03:34 -07:00
|
|
|
credentials: BaseCredentials,
|
2022-12-13 21:50:34 -08:00
|
|
|
passphrase: String,
|
|
|
|
app_state: State<'_, AppState>
|
2022-12-29 16:40:48 -08:00
|
|
|
) -> Result<(), UnlockError> {
|
2023-05-06 12:01:56 -07:00
|
|
|
app_state.new_creds(credentials, &passphrase).await
|
2022-12-13 21:50:34 -08:00
|
|
|
}
|
2022-12-22 16:36:32 -08:00
|
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
2023-05-02 15:24:35 -07:00
|
|
|
pub async fn get_config(app_state: State<'_, AppState>) -> Result<AppConfig, ()> {
|
|
|
|
let config = app_state.config.read().await;
|
|
|
|
Ok(config.clone())
|
2022-12-22 16:36:32 -08:00
|
|
|
}
|
2023-04-25 22:10:14 -07:00
|
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
2023-04-26 15:49:08 -07:00
|
|
|
pub async fn save_config(config: AppConfig, app_state: State<'_, AppState>) -> Result<(), String> {
|
|
|
|
app_state.update_config(config)
|
|
|
|
.await
|
2023-04-28 14:33:04 -07:00
|
|
|
.map_err(|e| format!("Error saving config: {e}"))?;
|
|
|
|
Ok(())
|
2023-04-25 22:10:14 -07:00
|
|
|
}
|
2023-08-02 19:57:37 -07:00
|
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
2023-09-11 16:00:58 -07:00
|
|
|
pub async fn launch_terminal(base: bool) -> Result<(), LaunchTerminalError> {
|
2023-08-03 16:35:15 -07:00
|
|
|
terminal::launch(base).await
|
2023-08-02 19:57:37 -07:00
|
|
|
}
|
2023-09-14 15:04:25 -07:00
|
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
pub async fn get_setup_errors(app_state: State<'_, AppState>) -> Result<Vec<String>, ()> {
|
|
|
|
Ok(app_state.setup_errors.clone())
|
|
|
|
}
|