182 lines
6.1 KiB
C#
182 lines
6.1 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
using System.Windows;
|
||
using System.Windows.Input;
|
||
using LDUninstaller.Core.Bulk;
|
||
using LDUninstaller.Core.Models;
|
||
|
||
namespace LDUninstaller.UI.Dialogs
|
||
{
|
||
public partial class BulkWizardWindow : Window
|
||
{
|
||
private readonly List<ApplicationUninstallerEntry> _targetApps;
|
||
private BulkUninstallTask? _bulkTask;
|
||
private CancellationTokenSource? _cts;
|
||
private bool _isRunning;
|
||
|
||
public List<ApplicationUninstallerEntry> UninstalledApps { get; private set; } = new();
|
||
|
||
public BulkWizardWindow(List<ApplicationUninstallerEntry> apps)
|
||
{
|
||
InitializeComponent();
|
||
_targetApps = apps;
|
||
Loaded += BulkWizardWindow_Loaded;
|
||
Closing += BulkWizardWindow_Closing;
|
||
}
|
||
|
||
private void BulkWizardWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e)
|
||
{
|
||
if (_isRunning)
|
||
{
|
||
var res = MessageBox.Show(
|
||
"Kaldırma işlemleri arka planda devam ediyor. İptal edip çıkmak istediğinize emin misiniz?",
|
||
"İptal Onayı",
|
||
MessageBoxButton.YesNo,
|
||
MessageBoxImage.Warning);
|
||
|
||
if (res == MessageBoxResult.Yes)
|
||
{
|
||
_cts?.Cancel();
|
||
}
|
||
else
|
||
{
|
||
e.Cancel = true;
|
||
}
|
||
}
|
||
}
|
||
|
||
private void BulkWizardWindow_Loaded(object sender, RoutedEventArgs e)
|
||
{
|
||
if (_targetApps.Count == 1)
|
||
{
|
||
txtTitle.Text = $"Kaldırma - {_targetApps[0].DisplayName}";
|
||
txtSubHeader.Text = $"'{_targetApps[0].DisplayName}' kaldırılacak ve işlem bitiminde sistem kalıntıları otomatik olarak ekrana getirilecektir.";
|
||
pnlConcurrent.Visibility = Visibility.Collapsed;
|
||
btnStart.Content = "🗑️ Şimdi Kaldır";
|
||
}
|
||
else
|
||
{
|
||
txtTitle.Text = $"Toplu Kaldırma - {_targetApps.Count} Yazılım";
|
||
txtSubHeader.Text = $"{_targetApps.Count} adet yazılım akıllı çakışma matrisiyle sırayla kaldırılacak ve işlem bitiminde tüm kalıntılar ekrana getirilecektir.";
|
||
pnlConcurrent.Visibility = Visibility.Visible;
|
||
btnStart.Content = $"🚀 Kaldırmayı Başlat ({_targetApps.Count} Yazılım)";
|
||
}
|
||
|
||
var items = _targetApps.Select(a => new BulkUninstallEntry(a, true)).ToList();
|
||
dgQueue.ItemsSource = items;
|
||
}
|
||
|
||
private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
|
||
{
|
||
if (e.ChangedButton == MouseButton.Left)
|
||
DragMove();
|
||
}
|
||
|
||
private void BtnClose_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
CancelOrClose();
|
||
}
|
||
|
||
private void BtnCancel_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
CancelOrClose();
|
||
}
|
||
|
||
private void CancelOrClose()
|
||
{
|
||
if (_isRunning)
|
||
{
|
||
var res = MessageBox.Show(
|
||
"Kaldırma işlemleri halen devam ediyor. İptal etmek istediğinize emin misiniz?",
|
||
"İşlemi İptal Et",
|
||
MessageBoxButton.YesNo,
|
||
MessageBoxImage.Question);
|
||
|
||
if (res == MessageBoxResult.Yes)
|
||
{
|
||
_cts?.Cancel();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
try { DialogResult = false; } catch { }
|
||
Close();
|
||
}
|
||
}
|
||
|
||
private async void BtnStart_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
btnStart.IsEnabled = false;
|
||
btnCancel.Content = "İptal Et";
|
||
chkRestorePoint.IsEnabled = false;
|
||
chkPreferQuiet.IsEnabled = false;
|
||
cmbConcurrent.IsEnabled = false;
|
||
_isRunning = true;
|
||
|
||
_cts = new CancellationTokenSource();
|
||
int concurrent = cmbConcurrent.SelectedIndex + 1;
|
||
|
||
_bulkTask = new BulkUninstallTask(_targetApps, chkPreferQuiet.IsChecked == true)
|
||
{
|
||
CreateRestorePoint = chkRestorePoint.IsChecked == true,
|
||
MaxConcurrentQuiet = concurrent,
|
||
EnableAutomatizer = true
|
||
};
|
||
|
||
dgQueue.ItemsSource = _bulkTask.Items;
|
||
pbProgress.Maximum = _targetApps.Count;
|
||
pbProgress.Value = 0;
|
||
|
||
_bulkTask.OnItemStarted += item =>
|
||
{
|
||
Dispatcher.Invoke(() => dgQueue.Items.Refresh());
|
||
};
|
||
|
||
_bulkTask.OnItemCompleted += item =>
|
||
{
|
||
Dispatcher.Invoke(() =>
|
||
{
|
||
dgQueue.Items.Refresh();
|
||
pbProgress.Value = Math.Min(pbProgress.Maximum, pbProgress.Value + 1);
|
||
});
|
||
};
|
||
|
||
_bulkTask.OnLog += msg =>
|
||
{
|
||
Dispatcher.Invoke(() => txtStatus.Text = msg);
|
||
};
|
||
|
||
try
|
||
{
|
||
await _bulkTask.ExecuteAsync(_cts.Token);
|
||
UninstalledApps = _bulkTask.Items
|
||
.Where(i => i.IsEffectivelyUninstalled)
|
||
.Select(i => i.Entry)
|
||
.ToList();
|
||
|
||
txtStatus.Text = $"Kaldırma tamamlandı! ({UninstalledApps.Count}/{_targetApps.Count} başarılı)";
|
||
await Task.Delay(400);
|
||
|
||
try { DialogResult = true; } catch { }
|
||
Close();
|
||
}
|
||
catch (OperationCanceledException)
|
||
{
|
||
txtStatus.Text = "Kullanıcı tarafından iptal edildi.";
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
txtStatus.Text = $"Hata: {ex.Message}";
|
||
}
|
||
finally
|
||
{
|
||
_isRunning = false;
|
||
btnCancel.Content = "Kapat";
|
||
btnStart.IsEnabled = true;
|
||
}
|
||
}
|
||
}
|
||
}
|