-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatusMainFormController.cs
More file actions
517 lines (434 loc) · 18.1 KB
/
Copy pathStatusMainFormController.cs
File metadata and controls
517 lines (434 loc) · 18.1 KB
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace c2flux
{
public sealed class StatusMainFormController
{
private readonly AppSettings _settings;
private readonly Form _owner;
private readonly AntdUI.Label _statusLabel;
private readonly AntdUI.Progress _scanProgress;
private readonly ToolStripStatusLabel _informationLabel;
private readonly ToolStripStatusLabel _warningLabel;
private readonly ToolStripStatusLabel _errorLabel;
// Size statusbar symbols (info, warning, error) / scale
private const int AlertStatusSymbolSize = 11;
[System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
private static extern bool GetDiskFreeSpace(
string lpRootPathName,
out uint lpSectorsPerCluster,
out uint lpBytesPerSector,
out uint lpNumberOfFreeClusters,
out uint lpTotalNumberOfClusters);
public StatusMainFormController(
AppSettings settings,
Form owner,
AntdUI.Label statusLabel,
AntdUI.Progress scanProgress,
ToolStripStatusLabel informationLabel,
ToolStripStatusLabel warningLabel,
ToolStripStatusLabel errorLabel)
{
_settings = settings;
_owner = owner;
_statusLabel = statusLabel;
_scanProgress = scanProgress;
_informationLabel = informationLabel;
_warningLabel = warningLabel;
_errorLabel = errorLabel;
}
public void ConfigureAlertStatusStrip()
{
ConfigureAlertStatusLabel(
_informationLabel,
StatusSymbolKind.Information,
LocalizationService.GetText("Alert.ToolTipInformation"),
new Padding(3, 0, 0, 0));
ConfigureAlertStatusLabel(
_warningLabel,
StatusSymbolKind.Warning,
LocalizationService.GetText("Alert.ToolTipWarning"),
Padding.Empty);
ConfigureAlertStatusLabel(
_errorLabel,
StatusSymbolKind.Error,
LocalizationService.GetText("Alert.ToolTipError"),
Padding.Empty);
UpdateAlertStatusStrip();
}
public void ApplyLocalizedTexts()
{
if (_informationLabel != null)
{
_informationLabel.ToolTipText = LocalizationService.GetText("Alert.ToolTipInformation");
}
if (_warningLabel != null)
{
_warningLabel.ToolTipText = LocalizationService.GetText("Alert.ToolTipWarning");
}
if (_errorLabel != null)
{
_errorLabel.ToolTipText = LocalizationService.GetText("Alert.ToolTipError");
}
}
public void AppAlertLogChanged(object sender, EventArgs e)
{
if (_owner == null || _owner.IsDisposed)
return;
if (_owner.InvokeRequired)
{
_owner.BeginInvoke(new Action(UpdateAlertStatusStrip));
return;
}
UpdateAlertStatusStrip();
}
public void UpdateAlertStatusStrip()
{
if (_informationLabel == null || _warningLabel == null || _errorLabel == null)
return;
_informationLabel.Text = AppAlertLog.GetUnconfirmedCount(AppAlertSeverity.Information).ToString();
_warningLabel.Text = AppAlertLog.GetUnconfirmedCount(AppAlertSeverity.Warning).ToString();
_errorLabel.Text = AppAlertLog.GetUnconfirmedCount(AppAlertSeverity.Error).ToString();
}
public void ToolStripAlertLabelClick(object sender, EventArgs e)
{
using AlertHistoryForm alertHistoryForm = new AlertHistoryForm(_settings);
alertHistoryForm.ShowDialog(_owner);
}
public void SetStatusText(string text)
{
_statusLabel.Text = text;
}
public void SetStatusTextByKey(string localizationKey)
{
_statusLabel.Text = LocalizationService.GetText(localizationKey);
}
public void SetFormattedStatusText(string localizationKey, params object[] args)
{
_statusLabel.Text = string.Format(LocalizationService.GetText(localizationKey), args);
}
public void SetSelectedEntrySummary(
FileSystemEntry entry,
int fileCount)
{
if (entry == null)
{
_statusLabel.Text =
LocalizationService.GetText("Common.Ready");
return;
}
long clusterSize = GetClusterSize(entry.FullPath);
string entryDisplayName = GetEntryStatusDisplayPath(entry);
_statusLabel.Text = string.Format(
"{0} | Size: {1} | Files: {2:N0} | Cluster-Size: {3:N0}",
entryDisplayName,
SizeFormatter.Format(entry.SizeBytes),
Math.Max(0, fileCount),
clusterSize);
}
private static string GetEntryStatusDisplayPath(FileSystemEntry entry)
{
if (entry == null)
return string.Empty;
if (!string.IsNullOrWhiteSpace(entry.FullPath))
return entry.FullPath;
return string.IsNullOrWhiteSpace(entry.Name)
? string.Empty
: entry.Name;
}
public void SetScanProgress(
double? percent,
TimeSpan? elapsed,
bool visible)
{
if (_scanProgress == null)
return;
if (_scanProgress.InvokeRequired)
{
_scanProgress.BeginInvoke(
new Action(
() => SetScanProgress(
percent,
elapsed,
visible)));
return;
}
double value = percent.HasValue
? Math.Max(0D, Math.Min(100D, percent.Value))
: 0D;
TimeSpan elapsedValue =
elapsed.GetValueOrDefault();
_scanProgress.UseSystemText = true;
_scanProgress.Animation = 0;
_scanProgress.Fill = AntdThemeService.Accent;
_scanProgress.Text = string.Format(
"{0:0.0} % | {1:0.0} s",
value,
elapsedValue.TotalSeconds);
_scanProgress.Value = (float)(value / 100D);
_scanProgress.Visible = visible;
_scanProgress.Refresh();
SetMainWindowTitle(
visible && percent.HasValue
? value
: null);
}
public void SetScanProgressPending(
float value,
TimeSpan? elapsed,
bool visible)
{
if (_scanProgress == null)
return;
if (_scanProgress.InvokeRequired)
{
_scanProgress.BeginInvoke(
new Action(
() => SetScanProgressPending(
value,
elapsed,
visible)));
return;
}
float normalizedValue = Math.Max(0F, Math.Min(1F, value));
TimeSpan elapsedValue = elapsed.GetValueOrDefault();
_scanProgress.UseSystemText = false;
_scanProgress.Animation = 0;
_scanProgress.Fill = AntdThemeService.Accent;
_scanProgress.Text = string.Format(
"{0:0.0} s",
elapsedValue.TotalSeconds);
_scanProgress.Value = normalizedValue;
_scanProgress.Visible = visible;
_scanProgress.Refresh();
SetMainWindowTitle(null);
}
public void SetStorageHistoryPostProcessingProgress(
float value,
TimeSpan? elapsed,
bool visible)
{
if (_scanProgress == null)
return;
if (_scanProgress.InvokeRequired)
{
_scanProgress.BeginInvoke(
new Action(
() => SetStorageHistoryPostProcessingProgress(
value,
elapsed,
visible)));
return;
}
float normalizedValue = Math.Max(0F, Math.Min(1F, value));
TimeSpan elapsedValue = elapsed.GetValueOrDefault();
_scanProgress.UseSystemText = false;
_scanProgress.Animation = 0;
_scanProgress.Fill = Color.Orange;
_scanProgress.Text = string.Format(
"{0:0.0} s",
elapsedValue.TotalSeconds);
_scanProgress.Value = normalizedValue;
_scanProgress.Visible = visible;
_scanProgress.Refresh();
_statusLabel.Text =
LocalizationService.GetText(
"Settings.StorageHistoryDetails");
_owner.Text =
AppConstants.FullApplicationName +
" - " +
LocalizationService.GetText(
"Settings.StorageHistoryDetails") +
" - " +
string.Format(
"{0:0.0} s",
elapsedValue.TotalSeconds);
}
public void UpdateStatusStripForDrive(string rootPath)
{
UpdateStatusStripForDrive(
rootPath,
null);
}
public void UpdateStatusStripForDrive(
string rootPath,
int? fileCount)
{
try
{
string driveRootPath = Path.GetPathRoot(rootPath);
if (string.IsNullOrWhiteSpace(driveRootPath))
{
_statusLabel.Text = LocalizationService.GetText("Common.Ready");
SetStatusProgressText(null);
return;
}
DriveInfo driveInfo = new DriveInfo(driveRootPath);
long clusterSize = GetClusterSize(driveRootPath);
string driveName = driveInfo.Name;
_statusLabel.Text = fileCount.HasValue
? string.Format(
"{0} | Size: {1} | Free: {2} | Files: {3:N0} | Cluster-Size: {4:N0}",
driveName,
SizeFormatter.Format(driveInfo.TotalSize),
SizeFormatter.Format(driveInfo.AvailableFreeSpace),
fileCount.Value,
clusterSize)
: string.Format(
"{0} | Size: {1} | Free: {2} | Cluster-Size: {3:N0}",
driveName,
SizeFormatter.Format(driveInfo.TotalSize),
SizeFormatter.Format(driveInfo.AvailableFreeSpace),
clusterSize);
SetStatusProgressText(null);
}
catch
{
_statusLabel.Text = LocalizationService.GetText("Common.Ready");
SetStatusProgressText(null);
}
}
public void SetMainWindowTitleForCacheVerification()
{
string title = AppConstants.FullApplicationName + " - " + LocalizationService.GetText("Status.TitleCacheVerification");
_owner.Text = title;
}
public void SetStatusProgressText(double? percent)
{
SetScanProgress(
percent,
null,
percent.HasValue);
}
public void SetScanHistorySaveProgress(
int percent,
TimeSpan? elapsed)
{
int value = Math.Max(0, Math.Min(100, percent));
_statusLabel.Text = LocalizationService.Format(
"Status.ScanHistorySaving",
value);
SetScanProgress(
value,
elapsed,
true);
_owner.Text =
AppConstants.FullApplicationName +
" - " +
LocalizationService.Format(
"Status.ScanHistorySavingTitle",
value);
}
public void ReportSkippedDirectories(int skippedDirectories, List<string> skippedDirectoryDetails)
{
if (skippedDirectories <= 0)
return;
List<string> expectedSkippedDirectoryDetails = new List<string>();
List<string> warningSkippedDirectoryDetails = new List<string>();
foreach (string skippedDirectoryDetail in skippedDirectoryDetails)
{
string[] lines = skippedDirectoryDetail
.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
string skippedDirectoryPath = lines.Length > 0 ? lines[0] : string.Empty;
string skippedDirectoryReason = skippedDirectoryDetail;
string normalizedSkippedDirectoryPath = skippedDirectoryPath
.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
bool isExpectedSystemDirectory =
normalizedSkippedDirectoryPath.EndsWith(
Path.DirectorySeparatorChar + "System Volume Information",
StringComparison.OrdinalIgnoreCase) &&
(skippedDirectoryReason.IndexOf("0xC0000022", StringComparison.OrdinalIgnoreCase) >= 0 ||
skippedDirectoryReason.IndexOf("Zugriff verweigert", StringComparison.OrdinalIgnoreCase) >= 0 ||
skippedDirectoryReason.IndexOf("Access is denied", StringComparison.OrdinalIgnoreCase) >= 0 ||
skippedDirectoryReason.IndexOf("Access denied", StringComparison.OrdinalIgnoreCase) >= 0);
if (isExpectedSystemDirectory)
{
expectedSkippedDirectoryDetails.Add(skippedDirectoryDetail);
}
else
{
warningSkippedDirectoryDetails.Add(skippedDirectoryDetail);
}
}
int unknownSkippedDirectories = Math.Max(0, skippedDirectories - skippedDirectoryDetails.Count);
if (unknownSkippedDirectories > 0)
{
warningSkippedDirectoryDetails.Add(LocalizationService.Format("Alert.UnknownSkippedDirectories", unknownSkippedDirectories));
}
if (expectedSkippedDirectoryDetails.Count > 0)
{
string expectedSkippedDirectoryMessage = expectedSkippedDirectoryDetails.Count == 1
? LocalizationService.GetText("Alert.ExpectedSystemDirectorySingle")
: LocalizationService.Format("Alert.ExpectedSystemDirectoryMultiple", expectedSkippedDirectoryDetails.Count);
string expectedSkippedDirectoryDetailsText = string.Join(Environment.NewLine + Environment.NewLine, expectedSkippedDirectoryDetails);
AppAlertLog.AddInformation(LocalizationService.GetText("Alert.Scan"), expectedSkippedDirectoryMessage, expectedSkippedDirectoryDetailsText);
}
if (warningSkippedDirectoryDetails.Count > 0)
{
string skippedDirectoryMessage = warningSkippedDirectoryDetails.Count == 1
? LocalizationService.GetText("Alert.SkippedDirectorySingle")
: LocalizationService.Format("Alert.SkippedDirectoryMultiple", warningSkippedDirectoryDetails.Count);
string skippedDirectoryDetailsText = string.Join(Environment.NewLine + Environment.NewLine, warningSkippedDirectoryDetails);
AppAlertLog.AddWarning(LocalizationService.GetText("Alert.Scan"), skippedDirectoryMessage, skippedDirectoryDetailsText);
}
}
private void ConfigureAlertStatusLabel(
ToolStripStatusLabel label,
StatusSymbolKind symbolKind,
string toolTipText,
Padding margin)
{
if (label == null)
return;
Image oldImage = label.Image;
label.Image = StatusSymbolRenderer.CreateBitmap(symbolKind, AlertStatusSymbolSize);
oldImage?.Dispose();
label.ImageScaling = ToolStripItemImageScaling.None;
label.ImageAlign = ContentAlignment.MiddleCenter;
label.TextAlign = ContentAlignment.MiddleLeft;
label.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
label.Margin = margin;
label.Padding = Padding.Empty;
label.ToolTipText = toolTipText;
label.Click -= ToolStripAlertLabelClick;
label.Click += ToolStripAlertLabelClick;
}
private void SetMainWindowTitle(double? scanPercent)
{
string title = AppConstants.FullApplicationName;
if (scanPercent.HasValue)
{
double value = Math.Max(0D, Math.Min(100D, scanPercent.Value));
if (value >= 100D)
{
title += " - " + LocalizationService.GetText("Status.ScanCompletedTitle");
}
else
{
title += " - " + LocalizationService.GetText("Status.ScanTitlePrefix") + value.ToString("0.0") + "%";
}
}
_owner.Text = title;
}
private long GetClusterSize(string rootPath)
{
string driveRootPath = Path.GetPathRoot(rootPath);
if (string.IsNullOrWhiteSpace(driveRootPath))
return 0;
bool success = GetDiskFreeSpace(
driveRootPath,
out uint sectorsPerCluster,
out uint bytesPerSector,
out uint numberOfFreeClusters,
out uint totalNumberOfClusters);
if (!success)
{
return 0;
}
return (long)sectorsPerCluster * bytesPerSector;
}
}
}