-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWizardForm.cs
578 lines (522 loc) · 18.8 KB
/
WizardForm.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using CSUACSelfElevation;
namespace TaskSchedulerConfig
{
public partial class WizardForm : Form
{
private bool autofix;
private bool canElevate;
private bool isElevated;
private Diagnostics diags;
public WizardForm()
{
InitializeComponent();
canElevate = UACHandler.IsUserInAdminGroup() && UACHandler.IsUacEnabled();
isElevated = canElevate && UACHandler.IsRunAsAdmin();
}
private enum ScanState { Privileges, Firewall, Services, Complete };
private void WizardForm_Load(object sender, EventArgs e)
{
}
private void wizardControl1_Cancelling(object sender, System.ComponentModel.CancelEventArgs e)
{
if (scanner.IsBusy) scanner.CancelAsync();
}
// ********* INTRO Page *************
private void intro_Initialize(object sender, AeroWizard.WizardPageInitEventArgs e)
{
runAsAdminPrompt.Visible = canElevate && !isElevated;
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
UACHandler.RestartElevated();
}
private void introWizPg_Commit(object sender, AeroWizard.WizardPageConfirmEventArgs e)
{
autofix = autoRepairCheck.Checked;
diags = new Diagnostics(null);
}
// ********* DETECT Page *************
private void scanLocal_Initialize(object sender, AeroWizard.WizardPageInitEventArgs e)
{
scanner.RunWorkerAsync();
}
// ********* NO ERROR Page *************
private void connRemoteBtn_Click(object sender, EventArgs e)
{
wizardControl1.NextPage(selectRemoteWizPg);
}
private void explOptionsBtn_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://taskscheduler.codeplex.com/wikipage?title=TaskSecurity");
}
private void closeBtn_Click(object sender, EventArgs e)
{
Close();
}
private void showLocalResults_HelpClicked(object sender, EventArgs e)
{
wizardControl1.NextPage(reportWizPg);
}
// ********* ERROR Page *************
private void completeWithProbWizPg_Initialize(object sender, AeroWizard.WizardPageInitEventArgs e)
{
// Fill out list box based on diagnostics
var items = new List<Control>();
foreach (var diag in diags.Issues)
items.Add(MakeStatusItem(diag));
if (autofix && items.Count > 0)
items.Insert(0, MakeTitleItem("Issues found"));
issueList.Controls.Clear();
items.Reverse();
if (items.Count > 0)
issueList.Controls.AddRange(items.ToArray());
else
wizardControl1.NextPage(completeNoProbWizPg);
}
private Control MakeStatusItem(Diagnostics.Diagnostic diag, bool indented = true)
{
var status = ItemStatus.Detected;
if (diag.HasIssue.HasValue && !diag.HasIssue.Value)
status = ItemStatus.Checked;
else
{
if (diag.Resolved.HasValue)
{
if (diag.Resolved.Value)
status = ItemStatus.Fixed;
}
else
{
if (diag.Resolution.RequiresConsent || diag.Resolution.RequiresElevation)
status = ItemStatus.NotFixed;
else
status = ItemStatus.None;
}
}
return MakeStatusItem(diag.Name, diag.Description, status, indented);
}
// ********* REPORT Page *************
private void reportWizPg_Initialize(object sender, AeroWizard.WizardPageInitEventArgs e)
{
var items = new List<Control>();
foreach (var diag in diags.Issues)
items.Add(MakeReportItem(diag));
if (items.Count > 0)
items.Insert(0, MakeTitleItem("Issues found"));
int iPos = items.Count;
foreach (var diag in diags.NonIsseus)
items.Add(MakeStatusItem(diag, false));
if (items.Count > iPos)
items.Insert(iPos, MakeTitleItem("\r\nIssues checked"));
localConfigList.Controls.Clear();
items.Reverse();
if (items.Count > 0)
localConfigList.Controls.AddRange(items.ToArray());
}
private Control MakeReportItem(Diagnostics.Diagnostic diag)
{
var c = MakeStatusItem(diag, false) as TableLayoutPanel;
c.RowCount = 2;
c.RowStyles.Add(new RowStyle(SizeType.AutoSize));
c.Controls.Add(MakeLabel(diag.Resolution.Name, diag.Resolution.Description, true), 0, 1);
string resText;
bool canRun = false;
if (diag.Resolved.HasValue)
resText = diag.Resolved.Value ? "Completed" : "Issue not present";
else
{
resText = diag.Resolution.RequiresConsent ? "Requires consent" : "Not run";
if (diag.Resolution.RequiresConsent && (!diag.Resolution.RequiresElevation || isElevated))
canRun = true;
}
c.Controls.Add(MakeLabel(resText, null, false, false, true), 1, 1);
if (canRun)
c.Controls.Add(MakeIcon(Properties.Resources.Run, "Click here to run the repair", diag), 2, 1);
return c;
}
// ********* REMOTE SERVER Page *************
private void selectRemote_Commit(object sender, AeroWizard.WizardPageConfirmEventArgs e)
{
diags = new Diagnostics(remoteSvrText.Text);
}
// ********* SCANNER METHODS *************
private void scanner_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
object defParam = null;
e.Result = diags;
var rp = new EventHandler<Diagnostics.ShowMessageEventArgs>((o, m) => scanner.ReportProgress(0, m.Message));
diags.ShowMessage += rp;
foreach (var d in diags)
{
if (d.Condition(defParam) && (!d.RequiresElevation || isElevated))
{
scanner.ReportProgress(0, $"Checking {d.Name}...");
try { d.HasIssue = d.Troubleshooter(defParam); }
catch (Exception ex) { e.Result = ex; System.Diagnostics.Debug.WriteLine($"Error troubleshooting {d.Name}: {ex}"); }
if (d.HasIssue.GetValueOrDefault(true) && autofix && !d.Resolution.RequiresConsent && (!d.Resolution.RequiresElevation || isElevated))
try { d.Resolution.Resolver(defParam); d.Resolved = !d.Troubleshooter(defParam); }
catch (Exception ex2) { System.Diagnostics.Debug.WriteLine($"Error resolving {d.Name}: {ex2}"); }
}
}
diags.ShowMessage -= rp;
}
private void scanner_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
scanLocalStatusLabel.Text = e.UserState?.ToString();
if (e.ProgressPercentage == 100)
wizardControl1.NextPage();
}
private void scanner_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
if (!e.Cancelled)
{
if (e.Error != null)
{
localResultLabel.Text = "An error occurred while troubleshooting. Error: " + e.Error.ToString();
wizardControl1.NextPage(completeWithProbWizPg);
}
else
{
int i = 0; foreach (var d in diags.Issues) i++;
wizardControl1.NextPage(i == 0 ? completeNoProbWizPg : completeWithProbWizPg);
}
}
}
/*private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
((sender as ListBox)?.Items[e.Index] as LBItem)?.DrawItem(e);
}
private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
var lb = sender as ListBox;
(lb?.Items[e.Index] as LBItem)?.MeasureItem(e, lb?.Font);
}*/
private Control MakeTitleItem(string text, string tooltip = null)
{
var c = MakeTableItem(1, 1);
c.Controls.Add(MakeLabel(text, tooltip, false, true, false), 0, 0);
return c;
}
private enum ItemStatus { None, Fixed, Detected, NotFixed, Checked }
private Control MakeStatusItem(string text, string tooltip, ItemStatus status, bool indented = false)
{
var c = MakeTableItem(1, 3);
c.ColumnStyles[1] = new ColumnStyle(SizeType.Absolute, 100);
c.ColumnStyles[2] = new ColumnStyle(SizeType.AutoSize);
c.Controls.Add(MakeLabel(text, tooltip, indented), 0, 0);
c.Controls.Add(MakeStatusLabel(status), 1, 0);
c.Controls.Add(MakeStatusIcon(status), 2, 0);
return c;
}
private Control MakeStatusIcon(ItemStatus status)
{
Image img = Properties.Resources.NotFixed;
if (status == ItemStatus.Checked || status == ItemStatus.Fixed)
img = Properties.Resources.Fixed;
if (status == ItemStatus.Detected)
img = Properties.Resources.Detected;
return MakeIcon(img);
}
private Control MakeStatusLabel(ItemStatus status)
{
string text;
switch (status)
{
case ItemStatus.Fixed:
text = "Fixed"; break;
case ItemStatus.Detected:
text = "Detected"; break;
case ItemStatus.NotFixed:
text = "Not fixed"; break;
case ItemStatus.Checked:
text = "Checked"; break;
default:
text = "Unknown"; break;
}
return MakeLabel(text);
}
private Label MakeLabel(string text, string tooltip = null, bool indented = false, bool bold = false, bool ital = false)
{
var fs = FontStyle.Regular;
if (bold) fs |= FontStyle.Bold;
if (ital) fs |= FontStyle.Italic;
var l = new Label
{
Anchor = AnchorStyles.Left | AnchorStyles.Right,
AutoSize = true,
Font = new Font(wizardControl1.Font, fs),
Margin = new Padding(0),
Padding = new Padding(indented ? 12 : 0, 2, 0, 2),
Text = text,
};
if (tooltip != null)
toolTip1.SetToolTip(l, tooltip);
return l;
}
private TableLayoutPanel MakeTableItem(int rows = 1, int cols = 1)
{
var c = new TableLayoutPanel
{
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
ColumnCount = cols,
Dock = DockStyle.Top,
Margin = new Padding(0),
RowCount = rows,
};
for (int i = 0; i < cols; i++)
c.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
for (int i = 0; i < rows; i++)
c.RowStyles.Add(new RowStyle(SizeType.AutoSize));
return c;
}
private PictureBox MakeIcon(Image image, string tooltip = null, Diagnostics.Diagnostic diag = null)
{
var p = new PictureBox
{
Anchor = AnchorStyles.Right,
Image = image,
Margin = new Padding(4, 2, 10, 2),
Size = new Size(16, 16),
TabStop = false,
};
if (tooltip != null)
toolTip1.SetToolTip(p, tooltip);
if (diag?.Resolution.Resolver != null)
p.Click += (s, e) => PromptAndRun(s, diag);
return p;
}
private void PromptAndRun(object sender, Diagnostics.Diagnostic diag)
{
var p = sender as PictureBox;
if (p?.Tag == null && diag.Resolution.Resolver != null && MessageBox.Show(this, $"Execute the following patch?\n\n{diag.Resolution.Name}", "Apply Fix", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
diag.Resolution.Resolver?.Invoke(null);
if (!diag.Troubleshooter(null))
{
if (p != null)
{
p.Image = Properties.Resources.Fixed;
p.Tag = 0;
var t = p.Parent as TableLayoutPanel;
if (t != null)
{
var l = t.GetControlFromPosition(1, 1) as Label;
if (l != null)
l.Text = "Fixed";
}
}
}
}
}
/*private abstract class LBItem
{
public const int lrPad = 10;
public const int tbPad = 2;
protected const int stW = 80;
protected string Text;
public string Tooltip;
public abstract void DrawItem(DrawItemEventArgs e);
public abstract void MeasureItem(MeasureItemEventArgs e, Font font);
}
private class LBTitleItem : LBItem
{
public LBTitleItem(string text) { Text = text; }
public override void MeasureItem(MeasureItemEventArgs e, Font font)
{
Size sz = new Size(e.ItemWidth, e.ItemHeight);
using (var f = new Font(font, FontStyle.Bold))
e.ItemHeight = TextRenderer.MeasureText(e.Graphics, Text, f, sz, TextFormatFlags.SingleLine).Height + (tbPad * 2);
}
public override void DrawItem(DrawItemEventArgs e)
{
e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
using (var f = new Font(e.Font, FontStyle.Bold))
TextRenderer.DrawText(e.Graphics, Text, f, e.Bounds, SystemColors.WindowText, TextFormatFlags.SingleLine | TextFormatFlags.VerticalCenter);
}
}
private class LBStatusItem : LBItem
{
private enum LBItemStatus { None, Fixed, Detected, NotFixed, Checked }
private LBItemStatus Status;
private bool indentLeft;
public LBStatusItem(Diagnostics.Diagnostic diag, bool leftIndent = true)
{
Text = diag.Name;
Status = LBItemStatus.Detected;
if (diag.HasIssue.HasValue && !diag.HasIssue.Value)
Status = LBItemStatus.Checked;
else
{
if (diag.Resolved.HasValue)
{
if (diag.Resolved.Value)
Status = LBItemStatus.Fixed;
}
else
{
if (diag.Resolution.RequiresConsent || diag.Resolution.RequiresElevation)
Status = LBItemStatus.NotFixed;
else
Status = LBItemStatus.None;
}
}
indentLeft = leftIndent;
}
public override void MeasureItem(MeasureItemEventArgs e, Font font)
{
Size sz = new Size(e.ItemWidth, e.ItemHeight);
e.ItemHeight = Math.Max(TextRenderer.MeasureText(e.Graphics, Text, font, sz, TextFormatFlags.SingleLine).Height, this.Image.Height) + (tbPad * 2);
}
public override void DrawItem(DrawItemEventArgs e)
{
int imgW = this.Image.Width;
int imgX = e.Bounds.Right - lrPad - imgW;
e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
e.Graphics.DrawImage(this.Image, new Rectangle(imgX, e.Bounds.Y + (e.Bounds.Height - this.Image.Height) / 2, imgW, imgW));
Rectangle stBounds = new Rectangle(e.Bounds.Right - (lrPad * 2) - imgW - stW, e.Bounds.Y, stW, e.Bounds.Height);
TextRenderer.DrawText(e.Graphics, StatusText, e.Font, stBounds, SystemColors.WindowText, TextFormatFlags.SingleLine | TextFormatFlags.VerticalCenter);
Rectangle tBounds = indentLeft ? Rectangle.Inflate(e.Bounds, -lrPad, 0) : new Rectangle(e.Bounds.Location, new Size(e.Bounds.Width - lrPad, e.Bounds.Height));
tBounds.Width -= (lrPad * 2 + imgW + stW);
TextRenderer.DrawText(e.Graphics, Text, e.Font, tBounds, SystemColors.WindowText, TextFormatFlags.SingleLine | TextFormatFlags.VerticalCenter);
}
private string StatusText
{
get
{
switch (Status)
{
case LBItemStatus.Fixed:
return "Fixed";
case LBItemStatus.Detected:
return "Detected";
case LBItemStatus.NotFixed:
return "Not fixed";
case LBItemStatus.Checked:
return "Checked";
default:
return "Unknown";
}
}
}
private Image Image
{
get
{
if (Status == LBItemStatus.Checked || Status == LBItemStatus.Fixed)
return Properties.Resources.Fixed;
if (Status == LBItemStatus.Detected)
return Properties.Resources.Detected;
return Properties.Resources.NotFixed;
}
}
}
private class LBResolutionItem : LBItem
{
public enum LBResStatus { NotRun, Completed, NotPresent, ReqConsent }
private LBResStatus Status;
public LBResolutionItem(Diagnostics.Diagnostic diag)
{
Text = diag.Resolution.Name;
if (diag.Resolved.HasValue)
Status = diag.Resolved.Value ? LBResStatus.Completed : LBResStatus.NotPresent;
else
Status = diag.Resolution.RequiresConsent ? LBResStatus.ReqConsent : LBResStatus.NotRun;
}
public override void MeasureItem(MeasureItemEventArgs e, Font font)
{
Size sz = new Size(e.ItemWidth, e.ItemHeight);
e.ItemHeight = TextRenderer.MeasureText(e.Graphics, Text, font, sz, TextFormatFlags.SingleLine).Height + tbPad;
}
public override void DrawItem(DrawItemEventArgs e)
{
int imgW = 16;
int imgX = e.Bounds.Right - lrPad - imgW;
e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
Rectangle stBounds = new Rectangle(e.Bounds.Right - (lrPad * 2) - imgW - stW, e.Bounds.Y, stW + lrPad + imgW, e.Bounds.Height);
using (Font f2 = new Font(e.Font, FontStyle.Italic))
TextRenderer.DrawText(e.Graphics, StatusText, f2, stBounds, SystemColors.WindowText, TextFormatFlags.SingleLine);
Rectangle tBounds = new Rectangle(e.Bounds.X + lrPad * 2, e.Bounds.Y, e.Bounds.Width - (lrPad * 2) - stBounds.Width, e.Bounds.Height);
TextRenderer.DrawText(e.Graphics, Text, e.Font, tBounds, SystemColors.WindowText, TextFormatFlags.SingleLine | TextFormatFlags.Top);
}
private string StatusText
{
get
{
switch (Status)
{
case LBResStatus.Completed:
return "Completed";
case LBResStatus.NotPresent:
return "Issue not present";
case LBResStatus.ReqConsent:
return "Requires consent";
default:
return "Not run";
}
}
}
}
private class LBRepairConfirmItem : LBItem
{
protected string TroubleText;
protected string RepairText;
public bool RepairChecked = true;
public bool Collapsed = false;
public LBRepairConfirmItem(Diagnostics.Diagnostic diag)
{
Text = diag.Name;
TroubleText = diag.Description;
RepairText = diag.Resolution.Name;
}
public override void MeasureItem(MeasureItemEventArgs e, Font font)
{
var m1 = new MeasureItemEventArgs(e.Graphics, e.Index, e.ItemHeight);
//TroubleLine.MeasureItem(m1, font);
var m2 = new MeasureItemEventArgs(e.Graphics, e.Index, e.ItemHeight);
//RepairLine.MeasureItem(m2, font);
e.ItemHeight = m1.ItemHeight + m2.ItemHeight;
}
public override void DrawItem(DrawItemEventArgs e)
{
var m1 = new MeasureItemEventArgs(e.Graphics, e.Index, e.Bounds.Height);
//TroubleLine.MeasureItem(m1, e.Font);
var r = e.Bounds; r.Height = m1.ItemHeight;
//TroubleLine.DrawItem(new DrawItemEventArgs(e.Graphics, e.Font, r, e.Index, e.State, e.ForeColor, e.BackColor));
r.Y += m1.ItemHeight; r.Height = e.Bounds.Height - m1.ItemHeight;
//RepairLine.DrawItem(new DrawItemEventArgs(e.Graphics, e.Font, r, e.Index, e.State, e.ForeColor, e.BackColor));
}
}
private class LBReportItem : LBItem
{
protected LBStatusItem TroubleLine;
protected LBResolutionItem RepairLine;
public LBReportItem(Diagnostics.Diagnostic diag)
{
TroubleLine = new LBStatusItem(diag, false);
RepairLine = new LBResolutionItem(diag);
}
public override void MeasureItem(MeasureItemEventArgs e, Font font)
{
var m1 = new MeasureItemEventArgs(e.Graphics, e.Index, e.ItemHeight);
TroubleLine.MeasureItem(m1, font);
var m2 = new MeasureItemEventArgs(e.Graphics, e.Index, e.ItemHeight);
RepairLine.MeasureItem(m2, font);
e.ItemHeight = m1.ItemHeight + m2.ItemHeight;
}
public override void DrawItem(DrawItemEventArgs e)
{
var m1 = new MeasureItemEventArgs(e.Graphics, e.Index, e.Bounds.Height);
TroubleLine.MeasureItem(m1, e.Font);
var r = e.Bounds; r.Height = m1.ItemHeight;
TroubleLine.DrawItem(new DrawItemEventArgs(e.Graphics, e.Font, r, e.Index, e.State, e.ForeColor, e.BackColor));
r.Y += m1.ItemHeight; r.Height = e.Bounds.Height - m1.ItemHeight;
RepairLine.DrawItem(new DrawItemEventArgs(e.Graphics, e.Font, r, e.Index, e.State, e.ForeColor, e.BackColor));
}
}*/
}
}