-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathMainWindow.xaml.cs
271 lines (240 loc) · 11.4 KB
/
MainWindow.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//
// Windows 10 Manual Update
// Copyright 2016 Vyacheslav Napadovsky.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
namespace w10mu {
class UpdateItem {
readonly dynamic _update;
int GetSizeOrder(decimal size) {
int order;
for (order = 0; size > 1024; ++order) {
size /= 1024;
}
return order;
}
string SizeToString(decimal size, int order, bool addsuffix) {
string fmt;
switch (order) {
case 0: fmt = "{0} B"; break;
case 1: fmt = "{0} KB"; size /= 1024; break;
case 2: fmt = "{0} MB"; size /= 1024 * 1024; break;
default: fmt = "{0} GB"; size /= 1024 * 1024 * 1024; break;
}
return addsuffix
? string.Format(fmt, (int)size)
: ((int)size).ToString();
}
public UpdateItem(dynamic update) {
_update = update;
//IsChecked = _update.AutoSelectOnWebSites; // this line will select them all
IsChecked = _update.IsMandatory;
var sb = new StringBuilder();
if (EulaAccepted == false)
sb.Append("[EULA NOT ACCEPTED] ");
sb.AppendFormat("{0}\n", _update.Title);
if (_update.Description != null)
sb.AppendFormat("{0}\n", _update.Description);
if (_update.MoreInfoUrls != null && _update.MoreInfoUrls.Count > 0) {
sb.AppendFormat("More info:\n");
for (int i = 0; i < _update.MoreInfoUrls.Count; ++i)
sb.AppendFormat("{0}\n", _update.MoreInfoUrls.Item(i));
}
if (_update.EulaText != null)
sb.AppendFormat("EULA TEXT:\n{0}\n\n", _update.EulaText);
if (_update.ReleaseNotes != null)
sb.AppendFormat("Release Notes:\n{0}\n\n", _update.ReleaseNotes);
dynamic bundle = _update.BundledUpdates;
if (bundle != null && bundle.Count > 0) {
sb.AppendFormat("This update contains {0} packages:\n", bundle.Count);
for (int i = 0; i < bundle.Count; ++i) {
var item = new UpdateItem(bundle.Item(i));
var desc = item.Description;
desc = desc.Substring(0, desc.Length - 1);
sb.AppendFormat("#{0}: {1}\n", i + 1, desc.Replace("\n", "\n * "));
}
}
decimal minSize = _update.MinDownloadSize;
decimal maxSize = _update.MaxDownloadSize;
string sizeString;
if (minSize == 0 || minSize == maxSize) {
sizeString = SizeToString(maxSize, GetSizeOrder(maxSize), true);
}
else {
int order = Math.Max(GetSizeOrder(minSize), GetSizeOrder(maxSize));
sizeString = string.Format("{0} - {1}",
SizeToString(minSize, order, false),
SizeToString(maxSize, order, true)
);
}
Title = string.Format("{0} ({1})", _update.Title, sizeString);
Description = sb.ToString();
}
public bool IsChecked { get; set; }
public string Title { get; }
public string Description { get; }
public dynamic Update { get { return _update; } }
public bool EulaAccepted { get { return _update.EulaAccepted; } }
public Brush Background {
get {
return Brushes.Transparent;
//return IsHidden ? SystemColors.InactiveCaptionTextBrush : Brushes.Transparent;
}
}
}
public partial class MainWindow : Window {
private dynamic _updateSession = null;
private dynamic _updateSearcher = null;
private dynamic _searchResult = null;
async Task SearchForUpdates() {
_status.Text = "Searching for updates...";
await Task.Run(() => {
_searchResult = _updateSearcher.Search("IsInstalled=0");
});
_status.Text = "Search completed.";
var list = new List<UpdateItem>();
int count = _searchResult.Updates.Count;
_installButton.IsEnabled = count > 0;
if (count > 0) {
for (int i = 0; i < _searchResult.Updates.Count; ++i)
list.Add(new UpdateItem(_searchResult.Updates.Item(i)));
}
else {
_status.Text = "There are no applicable updates.";
}
_list.ItemsSource = list;
}
protected override async void OnActivated(EventArgs e) {
base.OnActivated(e);
if (_updateSession == null) {
try {
_updateSession = Activator.CreateInstance(Type.GetTypeFromProgID("Microsoft.Update.Session"));
_updateSession.ClientApplicationID = "Windows 10 Manual Update";
_updateSearcher = _updateSession.CreateUpdateSearcher();
await SearchForUpdates();
_installButton.IsEnabled = true;
}
catch (Exception ex) {
MessageBox.Show(this, ex.ToString(), "Exception has occured!");
}
}
}
public MainWindow() {
InitializeComponent();
}
private async void Install_Click(object sender, RoutedEventArgs e) {
_installButton.IsEnabled = false;
try {
var list = _list.ItemsSource as List<UpdateItem>;
dynamic updatesToInstall = Activator.CreateInstance(Type.GetTypeFromProgID("Microsoft.Update.UpdateColl"));
foreach (var item in list) {
if (!item.IsChecked)
continue;
if (!item.EulaAccepted) {
if (MessageBox.Show(this, item.Update.EulaText, "Do you accept this license agreement?", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
continue;
item.Update.AcceptEula();
}
updatesToInstall.Add(item.Update);
}
if (updatesToInstall.Count == 0) {
_status.Text = "All applicable updates were skipped.";
}
else {
_status.Text = "Downloading updates...";
dynamic downloader = _updateSession.CreateUpdateDownloader();
downloader.Updates = updatesToInstall;
await Task.Run(() => { downloader.Download(); });
if (MessageBox.Show(this, "Installation ready. Continue?", "Notice", MessageBoxButton.YesNo) == MessageBoxResult.Yes) {
_status.Text = "Installing updates...";
dynamic installer = _updateSession.CreateUpdateInstaller();
installer.Updates = updatesToInstall;
dynamic installationResult = null;
await Task.Run(() => { installationResult = installer.Install(); });
var sb = new StringBuilder();
if (installationResult.RebootRequired == true)
sb.Append("[REBOOT REQUIRED] ");
sb.AppendFormat("Code: {0}\n", installationResult.ResultCode);
sb.Append("Listing of updates installed:\n");
for (int i = 0; i < updatesToInstall.Count; ++i) {
sb.AppendFormat("{0} : {1}\n",
installationResult.GetUpdateResult(i).ResultCode,
updatesToInstall.Item(i).Title);
}
MessageBox.Show(this, sb.ToString(), "Installation Result");
}
await SearchForUpdates();
}
}
catch (Exception ex) {
MessageBox.Show(this, ex.ToString(), "Exception has occured!");
}
_installButton.IsEnabled = true;
}
private void ListSelectionChanged(object sender, SelectionChangedEventArgs e) {
foreach (UpdateItem item in e.AddedItems) {
_description.Document.Blocks.Clear();
var p = new Paragraph();
p.Inlines.Add(new Run(item.Description));
p.EnableHyperlinks();
_description.Document.Blocks.Add(p);
break;
}
}
}
static class RichEditExtensions {
public static void EnableHyperlinks(this Paragraph p) {
string paragraphText = new TextRange(p.ContentStart, p.ContentEnd).Text;
foreach (string word in paragraphText.Split(' ', '\n', '\t').ToList()) {
if (word.IndexOf("//") != -1 && Uri.IsWellFormedUriString(word, UriKind.Absolute)) {
Uri uri = new Uri(word, UriKind.RelativeOrAbsolute);
if (!uri.IsAbsoluteUri)
uri = new Uri(@"http://" + word, UriKind.Absolute);
for (TextPointer position = p.ContentStart;
position != null;
position = position.GetNextContextPosition(LogicalDirection.Forward))
{
if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) {
string textRun = position.GetTextInRun(LogicalDirection.Forward);
int indexInRun = textRun.IndexOf(word);
if (indexInRun >= 0) {
TextPointer start = position.GetPositionAtOffset(indexInRun);
TextPointer end = start.GetPositionAtOffset(word.Length);
var link = new Hyperlink(start, end);
link.NavigateUri = uri;
link.RequestNavigate += (sender, args) => Process.Start(args.Uri.ToString());
break;
}
}
}
}
}
}
}
}