141 lines
3.7 KiB
Rust
141 lines
3.7 KiB
Rust
use log;
|
|
use std::collections::HashMap;
|
|
use std::path::PathBuf;
|
|
use tokio::sync::Mutex;
|
|
|
|
use crate::pkg::{Package, PkgKey};
|
|
use crate::pkg_store::InstallResult;
|
|
use crate::profile::Profile;
|
|
use crate::appdata::AppData;
|
|
use crate::{liner, start};
|
|
|
|
use tauri::State;
|
|
|
|
#[tauri::command]
|
|
pub async fn startline(state: State<'_, Mutex<AppData>>) -> Result<(), String> {
|
|
log::debug!("invoke: startline");
|
|
|
|
let appd = state.lock().await;
|
|
|
|
if let Some(p) = &appd.profile {
|
|
// TODO if p.needsUpdate
|
|
liner::line_up(p).await.expect("Line-up failed");
|
|
start::start(p).map_err(|e| { log::error!("Error launching: {}", e.to_string()); e.to_string() }).map(|_| ())
|
|
//Ok(())
|
|
} else {
|
|
Err("No profile".to_owned())
|
|
}
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn install_package(state: State<'_, tokio::sync::Mutex<AppData>>, key: PkgKey) -> Result<InstallResult, String> {
|
|
log::debug!("invoke: install_package");
|
|
|
|
let mut appd = state.lock().await;
|
|
let rv = appd.pkgs.install_package(&key, true)
|
|
.await
|
|
.map_err(|e| e.to_string());
|
|
|
|
|
|
// if rv.is_ok() {
|
|
// _ = appd.toggle_package(key, true);
|
|
// }
|
|
|
|
rv
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn delete_package(state: State<'_, tokio::sync::Mutex<AppData>>, key: PkgKey) -> Result<(), String> {
|
|
log::debug!("invoke: delete_package");
|
|
|
|
let mut appd = state.lock().await;
|
|
appd.pkgs.delete_package(&key, true)
|
|
.await
|
|
.map_err(|e| e.to_string())
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn get_package(state: State<'_, tokio::sync::Mutex<AppData>>, key: PkgKey) -> Result<Package, String> {
|
|
log::debug!("invoke: get_package");
|
|
|
|
let appd = state.lock().await;
|
|
appd.pkgs.get(key)
|
|
.map_err(|e| e.to_string())
|
|
.cloned()
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn toggle_package(state: State<'_, tokio::sync::Mutex<AppData>>, key: PkgKey, enable: bool) -> Result<(), String> {
|
|
log::debug!("invoke: toggle_package");
|
|
|
|
let mut appd = state.lock().await;
|
|
appd.toggle_package(key, enable)
|
|
.map_err(|e| e.to_string())
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn reload_all_packages(state: State<'_, tokio::sync::Mutex<AppData>>) -> Result<(), String> {
|
|
log::debug!("invoke: reload_all_packages");
|
|
|
|
let mut appd = state.lock().await;
|
|
appd.pkgs.reload_all()
|
|
.await
|
|
.map_err(|e| e.to_string())
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn get_all_packages(state: State<'_, Mutex<AppData>>) -> Result<HashMap<PkgKey, Package>, ()> {
|
|
log::debug!("invoke: get_all_packages");
|
|
|
|
let appd = state.lock().await;
|
|
|
|
Ok(appd.pkgs.get_all())
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn fetch_listings(state: State<'_, Mutex<AppData>>) -> Result<(), String> {
|
|
log::debug!("invoke: fetch_listings");
|
|
|
|
let mut appd = state.lock().await;
|
|
appd.pkgs.fetch_listings().await
|
|
.map_err(|e| e.to_string())
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn get_current_profile(state: State<'_, Mutex<AppData>>) -> Result<Option<Profile>, ()> {
|
|
log::debug!("invoke: get_current_profile");
|
|
|
|
let appd = state.lock().await;
|
|
Ok(appd.profile.clone())
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn save_profile(state: State<'_, Mutex<AppData>>) -> Result<(), ()> {
|
|
log::debug!("invoke: save_profile");
|
|
|
|
let appd = state.lock().await;
|
|
if let Some(p) = &appd.profile {
|
|
p.save().await;
|
|
} else {
|
|
log::warn!("No profile to save");
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub async fn init_profile(
|
|
state: State<'_, Mutex<AppData>>,
|
|
exe_path: PathBuf
|
|
) -> Result<Profile, String> {
|
|
log::debug!("invoke: init_profile({})", exe_path.to_string_lossy());
|
|
|
|
let mut appd = state.lock().await;
|
|
let new_profile = Profile::new(exe_path);
|
|
|
|
new_profile.save().await;
|
|
appd.profile = Some(new_profile.clone());
|
|
|
|
Ok(new_profile)
|
|
}
|