-
Notifications
You must be signed in to change notification settings - Fork 0
/
InstallKmsKeys.xaml.cs
130 lines (114 loc) · 4.91 KB
/
InstallKmsKeys.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using HGM.Hotbird64.Vlmcs;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
// ReSharper disable once CheckNamespace
namespace HGM.Hotbird64.LicenseManager
{
public partial class InstallKmsKeys
{
private readonly LicenseMachine machine;
private readonly ObservableCollection<KmsLicense> kmsLicenses = new ObservableCollection<KmsLicense>();
public bool NeedsRefresh { get; private set; }
public InstallKmsKeys(MainWindow mainWindow, LicenseMachine machine) : base(mainWindow)
{
bool isWindowsActivated;
this.machine = machine;
InitializeComponent();
DataContext = this;
TopElement.LayoutTransform = Scaler;
Loaded += (s, e) =>
{
machine.GetKmsLicenses(out isWindowsActivated, kmsLicenses);
DataGrid.ItemsSource = kmsLicenses;
DataGrid.Items.SortDescriptions.Add(new SortDescription(nameof(KmsLicense.DisplayName), ListSortDirection.Ascending));
if (!isWindowsActivated)
{
KmsLicense firstWindowsLicense = kmsLicenses.FirstOrDefault(l => l.ApplicationID == Kms.WinGuid);
if (firstWindowsLicense != null) firstWindowsLicense.IsRadioButtonChecked = true;
}
foreach (KmsLicense kmsLicense in kmsLicenses.Where(l => l.ApplicationID != Kms.WinGuid))
{
kmsLicense.IsCheckBoxChecked = kmsLicense.IsNotActivated;
}
Install_Click(s, e);
};
}
private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Background = App.DatagridBackgroundBrushes[e.Row.GetIndex() % App.DatagridBackgroundBrushes.Count];
}
protected override void OnClosing(CancelEventArgs e)
{
if (ProgressBar.IsIndeterminate)
{
e.Cancel = true;
MessageBox.Show(this, "Please wait for the current action to complete", "Be Patient", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
base.OnClosing(e);
}
private async void InstallButton_Click(object sender, RoutedEventArgs e)
{
ProgressBar.IsIndeterminate = true;
ProgressBar.Visibility = Visibility.Visible;
InstallButton.Visibility = Visibility.Collapsed;
ResultColumn.Visibility = Visibility.Visible;
LicenseStatusColumn.Visibility = Visibility.Collapsed;
PartialProductKeyColumn.Visibility = Visibility.Collapsed;
CancelButton.IsEnabled = false;
LabelStatus.Text = "Installing GVLKs...";
try
{
foreach (KmsLicense kmsLicense in kmsLicenses) kmsLicense.IsControlEnabled = false;
foreach (KmsLicense kmsLicense in kmsLicenses.Where(l => l.ApplicationID == Kms.WinGuid ? !l.IsRadioButtonChecked : !l.IsCheckBoxChecked))
{
kmsLicense.InstallMessage = "N/A";
kmsLicense.InstallToolTip = "The GVLK has not been selected for installation";
kmsLicense.InstallSuccess = false;
}
await Task.Run(() =>
{
Parallel.ForEach(kmsLicenses.Where(l => l.ApplicationID == Kms.WinGuid ? l.IsRadioButtonChecked : l.IsCheckBoxChecked), kmsLicense =>
{
try
{
string licenseProvider = machine.InstallProductKey((string)kmsLicense.Gvlk);
NeedsRefresh = true;
Dispatcher.InvokeAsync(() =>
{
kmsLicense.InstallMessage = "Success";
kmsLicense.InstallToolTip = $"Installed by {licenseProvider}";
kmsLicense.InstallSuccess = true;
});
}
catch (Exception ex)
{
Dispatcher.InvokeAsync(() =>
{
kmsLicense.InstallMessage = "Failure";
kmsLicense.InstallToolTip = ex.Message;
kmsLicense.InstallSuccess = false;
});
}
});
});
}
finally
{
CancelButton.IsEnabled = true;
LabelStatus.Text = "Finished";
ProgressBar.Visibility = Visibility.Collapsed;
ProgressBar.IsIndeterminate = false;
if (NeedsRefresh) MainWindow.Button_Refresh_Clicked(null, null);
}
}
private void Install_Click(object sender, RoutedEventArgs e)
{
InstallButton.IsEnabled = kmsLicenses.Any(l => l.IsCheckBoxChecked || l.IsRadioButtonChecked);
}
}
}