LD-Uninstaller/src-csharp/LDUninstaller.UI/Dialogs/LeftoverCleanWindow.xaml.cs

208 lines
7.5 KiB
C#
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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using LDUninstaller.Core.Junk;
using LDUninstaller.Core.Models;
namespace LDUninstaller.UI.Dialogs
{
public partial class LeftoverCleanWindow : Window
{
private readonly List<IJunkResult> _junkItems;
public LeftoverCleanWindow(ApplicationUninstallerEntry app, List<IJunkResult> junkItems)
{
_junkItems = junkItems ?? new List<IJunkResult>();
InitializeComponent();
txtTitle.Text = $"'{app.DisplayName}' Artık ve Kalıntı Dosyaları";
if (_junkItems.Count > 0)
{
dgJunk.Visibility = Visibility.Visible;
pnlNoJunk.Visibility = Visibility.Collapsed;
btnClean.Visibility = Visibility.Visible;
chkSelectAll.Visibility = Visibility.Visible;
// U2: Yalnızca Çok Yüksek (VeryGood) güven seviyesine sahip olanlar varsayılan olarak seçili gelir
foreach (var j in _junkItems)
{
j.Selected = (j.Confidence == ConfidenceLevel.VeryGood);
}
dgJunk.ItemsSource = _junkItems;
chkSelectAll.IsChecked = _junkItems.All(x => x.Selected);
UpdateCount();
}
else
{
dgJunk.Visibility = Visibility.Collapsed;
pnlNoJunk.Visibility = Visibility.Visible;
btnClean.Visibility = Visibility.Collapsed;
chkSelectAll.Visibility = Visibility.Collapsed;
btnExclude.Visibility = Visibility.Collapsed;
txtCount.Text = "0 kalıntı bulundu (Sistem tertemiz)";
}
}
private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
DragMove();
}
private void BtnClose_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void ChkSelectAll_Changed(object sender, RoutedEventArgs e)
{
if (_junkItems == null || dgJunk == null || txtCount == null) return;
bool sel = chkSelectAll?.IsChecked == true;
foreach (var j in _junkItems)
{
if (j.Confidence > ConfidenceLevel.Bad)
j.Selected = sel;
}
dgJunk.Items.Refresh();
UpdateCount();
}
private void JunkItemCheckBox_Click(object sender, RoutedEventArgs e)
{
UpdateCount();
}
private void UpdateCount()
{
if (_junkItems == null || txtCount == null || btnClean == null) return;
int count = _junkItems.Count(x => x.Selected);
txtCount.Text = $"{count} / {_junkItems.Count} öğe seçildi";
btnClean.IsEnabled = count > 0;
}
private void MenuCopyPath_Click(object sender, RoutedEventArgs e)
{
if (dgJunk.SelectedItem is IJunkResult item && !string.IsNullOrWhiteSpace(item.Path))
{
Clipboard.SetText(item.Path);
}
}
private void MenuExclude_Click(object sender, RoutedEventArgs e)
{
if (dgJunk.SelectedItem is IJunkResult item)
{
ExcludeItem(item);
}
}
private void BtnExclude_Click(object sender, RoutedEventArgs e)
{
if (dgJunk.SelectedItem is IJunkResult item)
{
ExcludeItem(item);
}
else
{
MessageBox.Show("Lütfen korumaya almak istediğiniz bir kalıntı öğesini seçin.", "Öğe Seçilmedi", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
private void ExcludeItem(IJunkResult item)
{
ExclusionManager.AddExclusion(item.Path, $"Kullanıcı Koruması: {item.EntryName}");
_junkItems.Remove(item);
dgJunk.ItemsSource = null;
dgJunk.ItemsSource = _junkItems;
UpdateCount();
MessageBox.Show(
$"Hedef koruma listesine (Exclusion List) eklendi:\n{item.Path}\n\nBu yol sonraki tüm taramalarda otomatik olarak korunacaktır.",
"Korumaya Alındı",
MessageBoxButton.OK,
MessageBoxImage.Information);
}
private async void BtnClean_Click(object sender, RoutedEventArgs e)
{
var toDelete = _junkItems.Where(x => x.Selected && x.Confidence > ConfidenceLevel.Bad).ToList();
if (toDelete.Count == 0) return;
var res = MessageBox.Show(
$"{toDelete.Count} adet kalıntı öğe Geri Dönüşüm Kutusu'na gönderilecek ve kayıt defteri anahtarları silinmeden önce .reg dosyası olarak yedeklenecektir.\n\nDevam edilsin mi?",
"Kalıntı Temizleme Onayı",
MessageBoxButton.YesNo,
MessageBoxImage.Question);
if (res != MessageBoxResult.Yes) return;
btnClean.IsEnabled = false;
btnClose.IsEnabled = false;
btnExclude.IsEnabled = false;
pbCleanProgress.Visibility = Visibility.Visible;
pbCleanProgress.Maximum = toDelete.Count;
pbCleanProgress.Value = 0;
string backupDir = JunkManager.BackupDirectory;
int successCount = 0;
var failedItems = new List<string>();
await Task.Run(() =>
{
foreach (var item in toDelete)
{
try
{
bool ok = item.Delete(backupDir);
if (ok)
successCount++;
else
failedItems.Add(item.Path);
}
catch (Exception ex)
{
failedItems.Add($"{item.Path} ({ex.Message})");
}
Dispatcher.Invoke(() =>
{
pbCleanProgress.Value++;
txtCount.Text = $"{pbCleanProgress.Value} / {toDelete.Count} temizleniyor...";
});
}
});
pbCleanProgress.Visibility = Visibility.Collapsed;
btnClose.IsEnabled = true;
if (failedItems.Count == 0)
{
MessageBox.Show(
$"{successCount} adet kalıntı dosya ve kayıt defteri anahtarı başarıyla temizlendi!\nKayıt defteri yedekleri:\n{backupDir}",
"Temizlik Tamamlandı",
MessageBoxButton.OK,
MessageBoxImage.Information);
}
else
{
string failedSummary = string.Join("\n", failedItems.Take(5));
if (failedItems.Count > 5) failedSummary += $"\n... ve {failedItems.Count - 5} diğer öğe";
MessageBox.Show(
$"{successCount} adet öğe başarıyla temizlendi.\n{failedItems.Count} öğe silinemedi (dosya kilitli veya erişim engellendi):\n\n{failedSummary}\n\nKayıt defteri yedekleri:\n{backupDir}",
"Kalıntı Temizleme Raporu",
MessageBoxButton.OK,
MessageBoxImage.Warning);
}
Close();
}
}
}