89 lines
2.8 KiB
Rust
89 lines
2.8 KiB
Rust
use crate::error::AppResult;
|
|
use crate::models::ApplicationEntry;
|
|
use std::fs;
|
|
use std::path::PathBuf;
|
|
use std::sync::RwLock;
|
|
use tokio::sync::RwLock as AsyncRwLock;
|
|
|
|
pub struct AppState {
|
|
pub exclusions: RwLock<Vec<String>>,
|
|
pub cached_apps: AsyncRwLock<Vec<ApplicationEntry>>,
|
|
pub config_dir: PathBuf,
|
|
}
|
|
|
|
impl AppState {
|
|
pub fn new() -> Self {
|
|
let local_appdata = std::env::var("LOCALAPPDATA")
|
|
.unwrap_or_else(|_| "C:\\Users\\Default\\AppData\\Local".to_string());
|
|
let config_dir = PathBuf::from(local_appdata).join("LDUninstaller");
|
|
|
|
let _ = fs::create_dir_all(&config_dir);
|
|
let _ = fs::create_dir_all(config_dir.join("Backups"));
|
|
|
|
let exclusions_file = config_dir.join("exclusions.json");
|
|
let exclusions = if exclusions_file.exists() {
|
|
match fs::read_to_string(&exclusions_file) {
|
|
Ok(content) => serde_json::from_str(&content).unwrap_or_default(),
|
|
Err(_) => Vec::new(),
|
|
}
|
|
} else {
|
|
Vec::new()
|
|
};
|
|
|
|
Self {
|
|
exclusions: RwLock::new(exclusions),
|
|
cached_apps: AsyncRwLock::new(Vec::new()),
|
|
config_dir,
|
|
}
|
|
}
|
|
|
|
pub fn save_exclusions(&self) -> AppResult<()> {
|
|
let exclusions_file = self.config_dir.join("exclusions.json");
|
|
let list = match self.exclusions.read() {
|
|
Ok(guard) => guard.clone(),
|
|
Err(_) => Vec::new(),
|
|
};
|
|
let json = serde_json::to_string_pretty(&list)?;
|
|
fs::write(exclusions_file, json)?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn add_exclusion(&self, path_or_key: String) -> AppResult<()> {
|
|
let normalized = path_or_key.trim().to_lowercase();
|
|
if normalized.is_empty() {
|
|
return Ok(());
|
|
}
|
|
if let Ok(mut guard) = self.exclusions.write() {
|
|
if !guard.iter().any(|item| item.to_lowercase() == normalized) {
|
|
guard.push(path_or_key);
|
|
}
|
|
}
|
|
self.save_exclusions()
|
|
}
|
|
|
|
pub fn remove_exclusion(&self, path_or_key: &str) -> AppResult<()> {
|
|
let normalized = path_or_key.trim().to_lowercase();
|
|
if let Ok(mut guard) = self.exclusions.write() {
|
|
guard.retain(|item| item.to_lowercase() != normalized);
|
|
}
|
|
self.save_exclusions()
|
|
}
|
|
|
|
pub fn is_excluded(&self, path_or_key: &str) -> bool {
|
|
let target = path_or_key.trim().to_lowercase();
|
|
if let Ok(guard) = self.exclusions.read() {
|
|
guard.iter().any(|ex| {
|
|
let ex_lower = ex.trim().to_lowercase();
|
|
target == ex_lower || target.starts_with(&format!("{}\\", ex_lower))
|
|
})
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Default for AppState {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|