LD-Uninstaller/src-tauri/src/commands/app_commands.rs

85 lines
3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use crate::error::{AppError, AppResult};
use crate::models::ApplicationEntry;
use crate::services::{DiscoveryService, UninstallerService};
use crate::state::AppState;
use std::path::PathBuf;
use tauri::State;
#[tauri::command]
pub async fn get_installed_apps(state: State<'_, AppState>) -> AppResult<Vec<ApplicationEntry>> {
let apps = tokio::task::spawn_blocking(DiscoveryService::scan_all_applications)
.await
.map_err(|e| AppError::Execution(format!("Tarama görevi yürütülemedi: {}", e)))??;
let mut cache = state.cached_apps.write().await;
*cache = apps.clone();
Ok(apps)
}
#[tauri::command]
pub async fn uninstall_application(
app_id: String,
quiet: bool,
state: State<'_, AppState>,
) -> AppResult<bool> {
// 1. Kilit kapsamını (lock scope) anında sınırla: uygulamayı kopyala ve kilidi hemen bırak
let app_to_uninstall = {
let cache = state.cached_apps.read().await;
cache
.iter()
.find(|a| a.id == app_id)
.cloned()
.ok_or_else(|| AppError::NotFound(format!("Uygulama bulunamadı (ID: {})", app_id)))?
};
// 2. Senkron süreç bekleme döngüsünü spawn_blocking ile Tokio thread pool'dan ayır (Deadlock önlendi)
tokio::task::spawn_blocking(move || {
UninstallerService::uninstall(&app_to_uninstall, quiet)
})
.await
.map_err(|e| AppError::Execution(format!("Kaldırma süreci yürütülemedi: {}", e)))?
}
#[tauri::command]
pub async fn export_apps_csv(file_path: String, state: State<'_, AppState>) -> AppResult<String> {
let apps = {
let cache = state.cached_apps.read().await;
cache.clone()
};
let target_path = if file_path.trim().is_empty() || file_path.contains("Default\\Desktop") {
if let Ok(user_profile) = std::env::var("USERPROFILE") {
PathBuf::from(user_profile).join("Desktop").join("LD_Uninstaller_Apps.csv")
} else {
PathBuf::from("LD_Uninstaller_Apps.csv")
}
} else {
PathBuf::from(file_path)
};
let target_path_str = target_path.to_string_lossy().to_string();
tokio::task::spawn_blocking(move || -> AppResult<String> {
let mut wtr = csv::Writer::from_path(&target_path)
.map_err(|e| AppError::Io(std::io::Error::other(e)))?;
wtr.write_record(["Ad", "Sürüm", "Yayımcı", "Yükleme Konumu", "Boyut (KB)", "Kaldırma Komutu"])
.map_err(|e| AppError::Io(std::io::Error::other(e)))?;
for app in &apps {
wtr.write_record([
&app.display_name,
&app.display_version,
&app.publisher,
&app.install_location,
&app.estimated_size_kb.to_string(),
&app.uninstall_string,
]).map_err(|e| AppError::Io(std::io::Error::other(e)))?;
}
wtr.flush().map_err(|e| AppError::Io(std::io::Error::other(e)))?;
Ok(target_path_str)
})
.await
.map_err(|e| AppError::Execution(format!("Dışa aktarma yürütülemedi: {}", e)))?
}