Repository navigation
Expand file tree
/
Copy pathMainWindow.AppMgmt.cs
More file actions
1026 lines (960 loc) · 49.4 KB
/
Copy pathMainWindow.AppMgmt.cs
File metadata and controls
1026 lines (960 loc) · 49.4 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
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using GuardWui3.Models;
using GuardWui3.Services;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Automation;
using Microsoft.UI.Xaml.Automation.Peers;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Windows.UI;
namespace GuardWui3;
// App Management page: the installed-app scan and filter, list export (with
// optional settings folders), and import/reinstall (with optional settings
// restore).
public sealed partial class MainWindow : Window
{
// =====================================================================
// APP SCAN
// =====================================================================
private async void OnRefreshApps(object sender, RoutedEventArgs e)
{
// Cross-page: a backup, system image, reinstall, or export on another
// page would otherwise run concurrently with this scan (see
// IsAnyJobRunning). The automatic/lazy scans (first tab navigation,
// the post-winget-install and post-import rescans) are deliberately
// NOT gated - only this explicit Refresh button is, so switching tabs
// while something else runs doesn't pop up an unexpected dialog for a
// read-only inventory scan the user never asked to start.
if (IsAnyJobRunning)
{
await ShowMessageAsync("GUARD", Capitalize(RunningJobLabel()!) + " is currently running. Wait for it to finish before scanning apps.");
return;
}
_appScanned = true;
ScanApps(announceStart: true);
}
// notifyCompletion: announce the completion summary as a settled UIA
// notification instead of the live region. The post-install rescan needs
// it: its summary lands right after the winget dialog's close (focus
// churn) and the queued "winget is now installed" notification, and a
// live-region change there is dropped (see AnnounceNotification's note),
// so NVDA never heard the new counts.
private void ScanApps(bool announceStart, bool notifyCompletion = false)
{
if (_scanning) return;
_scanning = true;
SetAppBusy(true);
_appStatusText = "Scanning installed apps (this can take a few seconds)...";
// A tab-switch scan shows this silently (the tab selection is already
// being announced); an explicit Refresh announces it, since nothing else
// is speaking then. Either way the completion summary is announced below.
if (announceStart) AnnounceAppStatus(); else UpdateStatusBar();
var th = new Thread(() =>
{
ScanResult? res = null; string? err = null;
try { res = AppInventory.DetectApps(); }
catch (Exception ex) { err = ex.Message; }
DispatcherQueue.TryEnqueue(() =>
{
bool wingetHint = false;
if (err != null) { _appStatusText = "Scan failed: " + err; }
else if (res != null)
{
_wingetAvailable = res.WingetAvailable;
_wingetChecked = true;
_allApps.Clear();
_allApps.AddRange(res.Apps);
int auto = 0, man = 0;
foreach (var a in res.Apps) { if (a.CanAuto) auto++; else man++; }
if (_wingetAvailable)
{
_appStatusText = res.Apps.Count + " apps found. " + auto + " reinstallable via winget, " + man + " manual.";
HideWingetOffer();
}
else
{
_appStatusText = res.Apps.Count + " apps found. winget is not installed, so apps cannot be reinstalled automatically.";
ShowWingetOffer();
wingetHint = true;
}
ApplyFilter();
}
_scanning = false; SetAppBusy(false);
if (wingetHint)
{
// The Ctrl+I hint rides in the speech only, like the update
// notice's Ctrl+U line; the persistent status stays short.
UpdateStatusBar();
if (_activePage == 1)
AnnounceNotification(_appStatusText + " Press Control+I to install it.");
}
else if (notifyCompletion)
{
// 2000ms so the "winget is now installed" notification (at
// its own 2000ms settle) finishes first even when the scan
// comes back quickly; ImportantMostRecent would otherwise
// cut it off mid-sentence.
UpdateStatusBar();
if (_activePage == 1) AnnounceSettled(_appStatusText, 2000);
}
else AnnounceAppStatus();
});
}) { IsBackground = true };
th.Start();
}
private void SetAppBusy(bool busy)
{
bool e = !busy;
BtnAppRefresh.IsEnabled = e;
BtnAppExport.IsEnabled = e;
BtnAppImport.IsEnabled = e;
BtnAppUpdateAll.IsEnabled = e;
ChkExportSettings.IsEnabled = e;
BtnAppAll.IsEnabled = e;
BtnAppNone.IsEnabled = e;
SetNavBusy(1, busy);
}
// Mark a page's job as running or stopped. The nav ring (driven from the snapshot
// in ApplyPageProgress) keeps an in-progress job discoverable from any page;
// start/finish speech is left to the existing job notifications (raised on the
// always-present status bar), so this adds none.
private void SetNavBusy(int page, bool busy)
{
Progress(page, p =>
{
p.Running = busy;
// A fresh job starts indeterminate (spinning) until its first real
// percentage arrives; the per-job progress calls then switch the ring to
// a determinate arc. A job that never reports a percentage (the app scan)
// just keeps spinning.
if (busy) { p.Indeterminate = true; p.Value = 0; }
});
}
private void OnFilterChanged(object sender, TextChangedEventArgs e)
{
_appFilter = (TxtAppFilter.Text ?? "").Trim();
ApplyFilter();
}
private void ApplyFilter()
{
AppRows.Clear();
string f = _appFilter.ToLowerInvariant();
foreach (var a in _allApps)
{
if (f.Length == 0 || MatchesFilter(a, f))
AppRows.Add(a);
}
UpdateAppCount();
}
private void UpdateAppCount()
{
if (LblAppCount == null) return;
string text = _appFilter.Length == 0
? Plural(_allApps.Count)
: AppRows.Count + " of " + Plural(_allApps.Count);
if (text == LblAppCount.Text) return;
LblAppCount.Text = text;
// Announce the new count only while the user is typing in the filter box
// (the immediate feedback they need); a scan or import already announces
// its own summary, so speaking the count too would double up. Identical
// counts across keystrokes stay silent (text unchanged).
if (TxtAppFilter.FocusState != FocusState.Unfocused)
Announce(LblAppCount);
}
private static string Plural(int n) => n == 1 ? "1 app" : n + " apps";
private static bool MatchesFilter(AppEntry a, string f)
{
if (Contains(a.Name, f) || Contains(a.Publisher, f) || Contains(a.Id, f) || Contains(a.SourceLabel, f))
return true;
// Type aliases not covered by SourceLabel
return a.Source switch
{
"manual" => f is "installer",
"msstore" => f is "msstore" or "ms store" or "microsoft store",
_ => false,
};
}
private static bool Contains(string? s, string f) =>
!string.IsNullOrEmpty(s) && s.ToLowerInvariant().IndexOf(f, StringComparison.Ordinal) >= 0;
private void OnSelectAll(object sender, RoutedEventArgs e) { foreach (var a in AppRows) a.Include = true; }
private void OnSelectNone(object sender, RoutedEventArgs e) { foreach (var a in AppRows) a.Include = false; }
// =====================================================================
// EXPORT / IMPORT
// =====================================================================
// One Export action covers both outputs: the app-list JSON always, plus
// the ticked apps' settings folders when "Also export app settings" is
// ticked. The settings step runs first because it is the only part the
// user can cancel, and cancelling must abort the whole export - a Cancel
// that still wrote the list would be a surprise partial result (untick
// the option to export the list alone).
private async void OnExportApps(object sender, RoutedEventArgs e)
{
if (_exporting)
{
await ShowMessageAsync("GUARD", "An export is already running. Wait for it to finish.");
return;
}
// Cross-page: a backup, system image, reinstall, or scan on another
// page would otherwise run concurrently with this export (see
// IsAnyJobRunning). _exporting is already false here (checked above),
// so this only sees the OTHER pages' flags.
if (IsAnyJobRunning)
{
await ShowMessageAsync("GUARD", Capitalize(RunningJobLabel()!) + " is currently running. Wait for it to finish before exporting.");
return;
}
// Only this page's fields: HarvestUi would pull the File Backup page's
// unsaved edits into the live config, and the save below would then
// persist edits the user never saved.
HarvestAppUi();
if (string.IsNullOrEmpty(_cfg.AppListDest))
{
await ShowMessageAsync("GUARD", "Enter an app list destination first.\n\nType a folder path next to \"List destination\", or use the Browse button to pick one.");
return;
}
var picked = new List<AppEntry>();
foreach (var a in _allApps) if (a.Include) picked.Add(a);
if (picked.Count == 0)
{
await ShowMessageAsync("GUARD", "Tick at least one app to export.");
return;
}
try { if (!Directory.Exists(_cfg.AppListDest)) Directory.CreateDirectory(_cfg.AppListDest); }
catch (Exception ex)
{
await ShowMessageAsync("GUARD", "Destination is not reachable:\n" + _cfg.AppListDest + "\n\n" + ex.Message);
return;
}
_exporting = true;
SetAppBusy(true);
try
{
// ---- Settings confirmation step (only when opted in) ----
bool wantSettings = _cfg.ExportAppSettings;
bool noMatches = false;
List<AppSettingsCandidate>? chosen = null;
if (wantSettings)
{
// Progress slot (not the main line), with an indeterminate bar
// while the disk is walked. Focus has not moved yet, so the
// spoken cue is a plain (undelayed) UIA notification.
SetExportProgress("Looking for settings folders for " + picked.Count + " ticked app(s)...", indeterminate: true);
AnnounceNotification("Looking for settings folders for " + picked.Count + " app(s)...");
// Candidate matching walks the disk, so it runs off the UI
// thread; sizes are measured later, inside the open dialog.
var candidates = await System.Threading.Tasks.Task.Run(
() => AppSettingsExport.FindCandidates(picked));
if (candidates.Count == 0)
{
// Nothing to confirm and nothing the user can act on here;
// the list export still goes ahead and the summary says
// why no settings folders came with it.
noMatches = true;
}
else
{
// The matching is heuristic; nothing is copied until the
// user has confirmed (and could untick) every match.
var dlg = new Views.AppSettingsDialog(candidates) { XamlRoot = Content.XamlRoot };
if (await ShowDialogAsync(dlg) != ContentDialogResult.Primary)
{
// Settled: the dialog just closed (a focus change) which
// would otherwise cut the cancellation message off.
SetExportOutcome("Export cancelled. Nothing was exported.");
AnnounceSettled("Export cancelled. Nothing was exported.");
return;
}
chosen = new List<AppSettingsCandidate>();
foreach (var c in candidates) if (c.Include) chosen.Add(c);
}
}
// ---- Export folder: one per export, so the app list and its
// settings are always paired and repeated exports never overwrite
// each other. The folder name carries the uniqueness, so the list
// inside is always app-list.json (no numbered names). ----
var file = new AppListFile
{
Exported = DateTime.Now.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture),
Machine = Environment.MachineName
};
var items = new List<AppListItem>();
foreach (var a in picked)
items.Add(new AppListItem
{
Name = a.Name, Id = a.Id, Source = a.Source, Version = a.Version,
Publisher = a.Publisher, InstallLocation = a.InstallLocation, PublisherUrl = a.PublisherUrl
});
file.Apps = items.ToArray();
string exportDir = MakeUniqueExportDir(_cfg.AppListDest);
Directory.CreateDirectory(exportDir);
string path = Path.Combine(exportDir, GuardPaths.AppListFileName);
AppListIo.Write(path, file);
// ---- Settings copy (after the list, so a cancel above never
// leaves either output behind) ----
string summary = "Exported " + picked.Count + " app(s) to " + Path.GetFileName(exportDir) + ".";
string detail = "Exported " + picked.Count + " apps to:\n" + path;
if (wantSettings)
{
if (noMatches)
{
string why = " No settings folders matched the ticked apps. Apps that keep settings in the registry, in ProgramData, or under a folder name unlike the app name are not found by this search.";
summary += why;
detail += "\n" + why.Trim();
}
else if (chosen!.Count == 0)
{
summary += " No settings folders were ticked, so none were copied.";
detail += "\n\nNo settings folders were ticked, so none were copied.";
}
else
{
// A determinate progress bar (by bytes, from the measured
// sizes) plus a spoken first line: a large folder (an Electron
// profile, say) otherwise copies with no feedback between
// per-folder lines and looks frozen, with no screen-reader cue.
// Per convention only the first line is spoken; the bar carries
// the rest and the following dialog reads the summary.
long totalBytes = 0;
foreach (var c in chosen) totalBytes += c.Bytes;
double barMax = totalBytes > 0 ? totalBytes : 1;
AppProgress.IsIndeterminate = false;
SetProgress(AppProgress, AppProgressLabel, barMax, 0, "Copying settings...");
bool announced = false;
var stats = await System.Threading.Tasks.Task.Run(() =>
AppSettingsExport.CopyCandidates(chosen, exportDir,
onFolder: msg => DispatcherQueue.TryEnqueue(() =>
{
AppProgressLabel.Text = msg;
_pageProg[1].Text = msg; ApplyPageProgress(1);
if (!announced) { announced = true; AnnounceSettled(msg); }
}),
onBytes: done => DispatcherQueue.TryEnqueue(() =>
{
AppProgress.Value = done;
_pageProg[1].Value = done; ApplyPageProgress(1);
})));
string copied = "Copied " + stats.Folders + " settings folder(s): " + stats.Files + " file(s)."
+ (stats.SkippedFiles > 0
? " " + stats.SkippedFiles + " file(s) were locked or unreadable and were skipped."
: "")
+ (stats.SkippedFolders > 0
? " " + stats.SkippedFolders + " subfolder(s) could not be read (permissions) and were left out entirely."
: "");
summary += " " + copied;
detail += "\n\n" + copied
+ "\nThe settings are inside this export's folder. To restore later, use "
+ "Import List and pick this app-list.json.";
}
}
// Remembering the list destination is a convenience, not part of the
// export: the files are already written by this point, so a failure
// here must not fall into the catch below and report a finished
// export as failed.
try { SettingsStore.SaveAppList(_cfg); }
catch (Exception ex) { DebugLog.Log("settings", "could not persist the app list destination", ex); }
// Outcome goes in the progress slot (like a backup/reinstall outcome),
// not the main line: it is not announced here (the dialog below reads
// it), and the main line keeps the inventory status so reading the bar
// does not say the same thing twice.
SetExportOutcome(summary);
await ShowMessageAsync("GUARD", detail);
}
catch (Exception ex)
{
SetExportOutcome("Export failed: " + ex.Message);
await ShowMessageAsync("GUARD", "Export failed:\n" + ex.Message);
}
finally
{
// Never leave a spinning/indeterminate bar behind on any exit path.
AppProgress.IsIndeterminate = false;
Progress(1, p => p.Indeterminate = false);
_exporting = false;
SetAppBusy(false);
}
}
// A unique, sortable folder for one export under the chosen destination, so
// each export's list and settings stay paired and repeats never collide.
private static string MakeUniqueExportDir(string dest)
{
string baseName = "app-export-" + DateTime.Now.ToString("yyyy-MM-dd_HHmm", CultureInfo.InvariantCulture);
string dir = Path.Combine(dest, baseName);
for (int n = 2; Directory.Exists(dir); n++) dir = Path.Combine(dest, baseName + "-" + n);
return dir;
}
// Export status shows in the status bar's progress slot, never the main
// status line, so reading the bar never says the same thing twice (the main
// line keeps the inventory status, as during a backup or reinstall). The slot
// is not a live region; spoken cues are raised separately via
// AnnounceNotification / AnnounceSettled.
private void SetExportProgress(string text, bool indeterminate)
{
AppProgress.IsIndeterminate = indeterminate;
AppProgressLabel.Text = text;
Progress(1, p =>
{
p.Indeterminate = indeterminate;
p.Text = text;
p.AreaVisible = true;
p.BarVisible = true;
});
}
// Terminal export state (success summary, cancellation, failure): show it in
// the progress slot with no moving bar, and keep the slot visible so the
// read-status-bar hotkey reports the outcome while App Management is focused.
private void SetExportOutcome(string text)
{
AppProgress.IsIndeterminate = false;
AppProgressLabel.Text = text;
Progress(1, p =>
{
p.Indeterminate = false;
p.BarVisible = false;
p.AreaVisible = true;
p.Text = text;
});
}
private async void OnImportApps(object sender, RoutedEventArgs e)
{
Windows.Storage.StorageFile? file;
try
{
var picker = new Windows.Storage.Pickers.FileOpenPicker();
WinRT.Interop.InitializeWithWindow.Initialize(picker, WindowHandle);
picker.FileTypeFilter.Add(".json");
picker.FileTypeFilter.Add("*");
file = await picker.PickSingleFileAsync();
}
catch (Exception ex)
{
await ShowMessageAsync("GUARD", "Could not open the file picker:\n\n" + ex.Message);
return;
}
if (file == null) return;
AppListFile? f;
try { f = AppListIo.Read(file.Path); }
catch (Exception ex)
{
await ShowMessageAsync("GUARD", "That file could not be read as an app list:\n\n" + ex.Message);
return;
}
if (f?.Apps == null || f.Apps.Length == 0)
{
await ShowMessageAsync("GUARD", "The app list is empty.");
return;
}
// The imported apps live in their OWN list (this dialog), so the
// installed-apps list behind the tab - the source for Export - is left
// untouched. _allApps and the main list are deliberately not modified.
var imported = new List<AppEntry>();
int auto = 0, man = 0;
foreach (var it in f.Apps)
{
// A literal "apps": [null] deserializes to a real null ELEMENT, which
// the Length check above cannot see; dereferencing it crashed GUARD
// outright (async void, and nothing marks it handled) instead of
// skipping one unusable row.
if (it is null) continue;
var a = new AppEntry
{
Name = it.Name ?? "",
Id = it.Id ?? "",
Source = string.IsNullOrEmpty(it.Source) ? (string.IsNullOrEmpty(it.Id) ? "manual" : "winget") : it.Source!,
Version = it.Version ?? "",
Publisher = it.Publisher ?? "",
InstallLocation = it.InstallLocation ?? "",
PublisherUrl = it.PublisherUrl ?? "",
Include = true
};
if (a.CanAuto) auto++; else man++;
imported.Add(a);
}
// Re-checked after the loop, not only before it: a file whose entries
// were ALL unusable passes the Length check but leaves nothing to show,
// and an import dialog listing "0 app(s)" says less than the plain
// empty-list message does.
if (imported.Count == 0)
{
await ShowMessageAsync("GUARD", "The app list is empty.");
return;
}
// Settings bundle written beside the list (sibling AppSettings folder +
// manifest)? Its presence enables the dialog's Reinstall & Restore option.
string listDir = Path.GetDirectoryName(file.Path) ?? "";
var manifest = AppSettingsRestore.TryLoadBundle(listDir);
string mac = f.Machine ?? "?";
string exp = f.Exported ?? "";
string header = imported.Count + " app(s) from " + mac +
(exp.Length > 0 ? " (" + exp + ")" : "") + ". " + auto + " reinstallable, " + man + " manual." +
(manifest != null
? " A settings bundle was found beside this list."
: " No settings bundle was found beside this list.");
var dlg = new Views.AppImportDialog(imported, header, manifest != null) { XamlRoot = Content.XamlRoot };
var result = await ShowDialogAsync(dlg);
if (result == ContentDialogResult.None)
{
_appStatusText = "Import cancelled. Nothing was changed.";
AnnounceAppStatus();
return;
}
// Ticked winget/store apps become reinstall targets; the rest are counted
// as manual (skipped by winget, but their settings can still be restored).
var targets = new List<AppEntry>();
int manual = 0;
foreach (var a in imported)
{
if (!a.Include) continue;
if (a.CanAuto && !string.IsNullOrEmpty(a.Id)) targets.Add(a);
else manual++;
}
// Primary = Reinstall Selected; Secondary = Reinstall & Restore Settings.
if (result == ContentDialogResult.Primary)
{
if (targets.Count == 0)
{
await ShowMessageAsync("GUARD", "None of the ticked apps can be reinstalled automatically.\n\nOnly \"Winget\" and \"Store\" apps reinstall automatically; \"Manual\" apps must be reinstalled by hand.");
return;
}
await ExecuteReinstall(targets, restore: null, BtnAppImport);
return;
}
// ---- Reinstall & Restore Settings ----
var candidates = AppSettingsRestore.BuildCandidates(manifest!, listDir);
if (candidates.Count == 0)
{
// The manifest names folders that are no longer on disk beside the
// list; fall back to reinstalling the apps alone.
if (targets.Count == 0)
{
await ShowMessageAsync("GUARD", "The settings folders named by this list could not be found beside it, and none of the ticked apps reinstall automatically. Nothing to do.");
return;
}
await ShowMessageAsync("GUARD", "The settings folders named by this list could not be found beside it, so only the apps will be reinstalled.\n\nKeep the AppSettings folder next to the list file to restore settings.");
await ExecuteReinstall(targets, restore: null, BtnAppImport);
return;
}
var restoreHeader = new System.Text.StringBuilder();
if (targets.Count > 0)
{
restoreHeader.Append("GUARD will reinstall " + targets.Count + " app(s) via winget, one at a time");
if (manual > 0) restoreHeader.Append(", skipping " + manual + " ticked \"Manual\" app(s)");
restoreHeader.Append(", then restore the ticked settings folders below. ");
}
else
{
restoreHeader.Append("None of the ticked apps reinstall automatically, so GUARD will only restore the ticked settings folders below. ");
}
restoreHeader.Append("A folder marked \"Replaces existing\" already exists on this PC; your current copy is renamed aside (.guard-old-) before it is replaced, so you can undo.");
var sdlg = new Views.AppSettingsRestoreDialog(candidates, restoreHeader.ToString())
{
XamlRoot = Content.XamlRoot,
Title = targets.Count > 0 ? "Reinstall and Restore App Settings" : "Restore App Settings",
PrimaryButtonText = targets.Count > 0 ? "Reinstall & Restore" : "Restore",
};
if (await ShowDialogAsync(sdlg) != ContentDialogResult.Primary)
{
_appStatusText = "Reinstall cancelled. Nothing was changed.";
AnnounceAppStatus();
return;
}
var chosen = new List<AppSettingsRestoreCandidate>();
foreach (var c in candidates) if (c.Include) chosen.Add(c);
if (targets.Count == 0 && chosen.Count == 0)
{
_appStatusText = "Nothing was selected to reinstall or restore.";
AnnounceAppStatus();
return;
}
await ExecuteReinstall(targets, chosen.Count > 0 ? chosen : null, BtnAppImport);
}
// =====================================================================
// REINSTALL
// =====================================================================
// Runs the winget install phase, then (when a restore set is given) the
// settings-restore phase, as one cancellable job under the run-feedback focus
// discipline. Launched from the Import List dialog (see OnImportApps), not a
// tab button, so it acts on the saved list, not the installed apps.
// launcherButton is the button that opened that dialog, so focus returns there
// when the job ends. restore is null for an apps-only run.
private async System.Threading.Tasks.Task ExecuteReinstall(
List<AppEntry> targets, List<AppSettingsRestoreCandidate>? restore, Control launcherButton)
{
// Belt-and-suspenders: SetAppBusy already disables Import while a run is
// live, so this path should be unreachable during one.
if (_reinstalling) return;
// Cross-page: a backup, system image, export, or scan on another page
// would otherwise run concurrently with this reinstall (see
// IsAnyJobRunning). _reinstalling is already false here (checked
// above), so this only sees the OTHER pages' flags.
if (IsAnyJobRunning)
{
await ShowMessageAsync("GUARD", Capitalize(RunningJobLabel()!) + " is currently running. Wait for it to finish before starting a reinstall.");
return;
}
// Claimed before any await below (the winget probe and install-offer
// dialog included), not after: otherwise a second click landing in that
// window would slip past the _reinstalling guard above and start a
// second concurrent reinstall sharing _reinstallCts. Same hazard
// RunScript guards against in MainWindow.FileBackup.cs. Reset on the
// winget-declined early return below since that path skips the run
// entirely; the core run's own finally resets it on every other exit.
_reinstalling = true;
// winget gate: the install phase needs winget, and a fresh PC restoring
// a saved list is exactly where it is missing. _wingetAvailable can be
// stale in both directions (installed outside GUARD since the scan), so
// re-probe cheaply before offering the install dialog. Restore-only
// runs (no targets) never need winget and skip the gate.
if (targets.Count > 0 && !_wingetAvailable)
{
if (await System.Threading.Tasks.Task.Run(WingetBootstrap.Probe))
{
_wingetAvailable = true;
_wingetChecked = true;
HideWingetOffer();
}
else if (!await ShowWingetInstallDialogAsync(targets.Count == 1
? "The ticked app needs winget to reinstall automatically. GUARD will install winget first, then reinstall the app."
: targets.Count + " ticked apps need winget to reinstall automatically. GUARD will install winget first, then reinstall them."))
{
_reinstalling = false;
_appStatusText = "Reinstall cancelled: winget is not installed.";
AnnounceAppStatus();
return;
}
}
_reinstallCts = new CancellationTokenSource();
var ct = _reinstallCts.Token;
// Shared focus discipline (BeginRunBusy): Stop takes focus before
// SetAppBusy greys out the button that launched the job.
Control? launcher = BeginRunBusy(BtnAppStop, launcherButton);
SetAppBusy(true);
TxtAppOutput.Text = "";
int restoreCount = restore?.Count ?? 0;
int totalSteps = targets.Count + restoreCount;
SetProgress(AppProgress, AppProgressLabel, totalSteps > 0 ? totalSteps : 1, 0, "Starting...");
ShowStatusBarProgress(1, true);
int ok = 0, fail = 0, attempted = 0;
AppSettingsRestoreStats? rstats = null;
string outcome;
// try/finally like RunScript's: without it an unexpected exception in the
// worker (this method is async void upstream) would leave _reinstalling
// stuck true and the page's buttons disabled for the rest of the session.
try
{
await System.Threading.Tasks.Task.Run(() =>
{
// ---- Install phase ----
for (int i = 0; i < targets.Count; i++)
{
if (ct.IsCancellationRequested) break;
var app = targets[i];
string installing = "Installing: " + app.Name + " (" + (i + 1) + " of " + targets.Count + ")";
SetProgress(AppProgress, AppProgressLabel, totalSteps, i, installing);
// First item only, like the backup run's start announcement.
if (i == 0) AnnounceSettled(installing);
AppendOut(TxtAppOutput, "\r\n=== Installing " + app.Name + " [" + app.Id + "] ===\r\n");
int code;
try { code = ProcessRunner.RunWingetInstall(app.Id, s => AppendOut(TxtAppOutput, s), ct); }
catch (Exception ex) { AppendOut(TxtAppOutput, "ERROR: " + ex.Message + "\r\n"); code = -1; }
// A cancel mid-install kills winget, which surfaces as a nonzero
// exit code; do not count a killed item as a failure.
if (ct.IsCancellationRequested) break;
attempted++;
if (code == 0) ok++; else fail++;
SetProgress(AppProgress, AppProgressLabel, totalSteps, attempted, "");
}
// ---- Restore phase (after the installs, so an app's freshly
// installed defaults are overwritten with the restored settings, and
// a cancel during installs skips restore entirely). Per-folder
// progress updates the bar silently, like the export copy. ----
if (!ct.IsCancellationRequested && restore != null)
{
AppendOut(TxtAppOutput, "\r\n=== Restoring app settings ===\r\n");
int baseStep = targets.Count;
int done = 0;
rstats = AppSettingsRestore.RestoreCandidates(restore, msg =>
{
int step = baseStep + done;
done++;
SetProgress(AppProgress, AppProgressLabel, totalSteps, step, msg);
AppendOut(TxtAppOutput, msg + "\r\n");
// When there is no install phase, the restore start is the
// job's first spoken line (the install loop spoke otherwise).
if (targets.Count == 0 && step == 0) AnnounceSettled(msg);
}, ct);
}
});
outcome = BuildReinstallOutcome(ct.IsCancellationRequested, targets.Count, attempted, ok, fail, rstats);
}
catch (Exception ex)
{
outcome = "Reinstall failed: " + ex.Message;
}
finally
{
_reinstalling = false;
_reinstallCts.Dispose();
_reinstallCts = null;
// Re-enable the launchers first so EndRunBusy's focus restore
// lands on a live control.
SetAppBusy(false);
EndRunBusy(BtnAppStop, launcher);
ShowStatusBarProgress(1, false);
}
// Back on the UI thread; the DispatcherQueue is FIFO, so everything the
// worker enqueued (output, progress) has already landed by now and the
// summary always prints last. Announced last (after the focus events
// above) so the focus announcement cannot cancel the summary speech.
AppProgressLabel.Text = outcome;
Progress(1, p => p.Text = outcome);
AppendOut(TxtAppOutput, "\r\n--- " + outcome + " ---\r\n");
AnnounceSettled(outcome, 2000);
}
// The end-of-job line, covering whichever phases ran: install counts (when
// any apps were targeted) plus the restore tally (whenever the restore phase
// ran at all).
//
// The restore tally is reported on the CANCELLED path too. It used to be
// dropped there, which mattered most in the one case it could not afford to:
// ManualRecoveryPaths is the only place GUARD ever names where a displaced
// settings folder ended up, so stopping a job mid-restore hid the one line
// the user needed to get their data back. A restore-only run also read
// "Cancelled after 0 of 0 app(s): 0 installed, 0 failed", which describes
// nothing that happened.
private static string BuildReinstallOutcome(
bool cancelled, int targetCount, int attempted, int ok, int fail, AppSettingsRestoreStats? rstats)
{
string s;
if (cancelled)
s = targetCount > 0
? "Cancelled after " + attempted + " of " + targetCount + " app(s): " + ok +
" installed, " + fail + " failed. Apps already installed stay installed."
: "Cancelled.";
else if (targetCount > 0)
s = "Done. " + ok + " installed, " + fail + " failed.";
else
s = "";
if (rstats != null)
{
string r = (cancelled ? "Settings restore stopped partway: restored " : "Restored ")
+ rstats.Folders + " settings folder(s)"
+ (rstats.Replaced > 0 ? " (" + rstats.Replaced + " existing folder(s) kept aside)" : "") + ".";
if (rstats.SkippedFolders > 0)
r += " " + rstats.SkippedFolders + " folder(s) were in use and skipped.";
// Named apart from "in use": nothing about the target was the
// problem, so telling the user to close an app would send them the
// wrong way. Their existing settings are exactly as they were.
if (rstats.SourceUnavailable > 0)
r += " " + rstats.SourceUnavailable + " folder(s) could not be read from the saved copy"
+ " (is the drive holding it still connected?) and were left as they were.";
if (rstats.SkippedFiles > 0)
r += " " + rstats.SkippedFiles + " file(s) were locked and skipped.";
if (rstats.PartialFolders > 0)
r += " " + rstats.PartialFolders + " folder(s) had a subfolder that could not be read (permissions) and was left out entirely.";
// Distinct from "skipped": these were moved aside to make room for
// the restore and could not be put back, so the user's original data
// is not where they left it - the path is the only way back to it.
if (rstats.ManualRecoveryPaths.Count > 0)
r += " " + rstats.ManualRecoveryPaths.Count + " folder(s) could not be fully restored; your original data is saved at: "
+ string.Join("; ", rstats.ManualRecoveryPaths);
s = s.Length > 0 ? s + " " + r : r;
}
return s.Length > 0 ? s : (cancelled ? "Cancelled." : "Done.");
}
private void OnStopReinstall(object sender, RoutedEventArgs e) => _reinstallCts?.Cancel();
// =====================================================================
// UPDATE ALL APPS
// =====================================================================
// Runs `winget upgrade --all` ELEVATED. A non-elevated winget can only get a
// per-installer UAC prompt, which some machine-scope MSI installers fail and
// MSIX packages (e.g. WSL) cannot use at all - they need winget itself
// elevated (0x80073d28 "administrator privileges required"). So GUARD asks
// for approval once up front and every installer inherits it. Output can't
// cross the elevation boundary, so winget writes to a log the run tails
// (the system-image pattern). An elevated run can't be killed from this
// non-elevated process, so there is no Stop; the confirm dialog says so.
private bool _updateAllElevated;
private async void OnUpdateAllApps(object sender, RoutedEventArgs e)
{
if (_reinstalling) return;
// Cross-page: a backup, system image, export, or scan on another page
// would otherwise run concurrently with this update (see
// IsAnyJobRunning). _reinstalling is already false here (checked
// above), so this only sees the OTHER pages' flags.
if (IsAnyJobRunning)
{
await ShowMessageAsync("GUARD", Capitalize(RunningJobLabel()!) + " is currently running. Wait for it to finish before updating apps.");
return;
}
// Claimed before any await below (the winget probe, install-offer
// dialog, and confirm dialog all included), not after: otherwise a
// second click landing in that window would slip past the
// _reinstalling guard above and start a second concurrent update run.
// Same hazard RunScript guards against in MainWindow.FileBackup.cs.
// Reset on both early returns below (winget declined, confirm
// declined) since they skip the run entirely; the run's own finally
// resets it on every other exit.
_reinstalling = true;
// Same winget gate as the reinstall path: probe first (the cached flag
// can be stale in both directions), then offer the install dialog.
if (!_wingetAvailable)
{
if (await System.Threading.Tasks.Task.Run(WingetBootstrap.Probe))
{
_wingetAvailable = true;
_wingetChecked = true;
HideWingetOffer();
}
else if (!await ShowWingetInstallDialogAsync(
"Updating apps needs winget. GUARD will install winget first, then check for app updates."))
{
_reinstalling = false;
_appStatusText = "Update cancelled: winget is not installed.";
AnnounceAppStatus();
return;
}
}
if (!await ShowConfirmAsync("GUARD",
"Update every app winget knows to its latest version now?\n\n" +
"GUARD asks for Administrator approval once, so apps that install for all users (and Store-delivered apps like WSL) can update too. This can take a while and cannot be stopped once it starts; some apps may briefly show their own setup windows.",
"Update All", "Cancel"))
{
_reinstalling = false;
return;
}
_updateAllElevated = true;
// No Stop for an elevated run (it can't be killed from here), so move
// focus to the live output before SetAppBusy greys the launcher, and
// leave the Stop button disabled.
TxtAppOutput.Text = "";
AppOutputExpander.IsExpanded = true;
TxtAppOutput.Focus(FocusState.Programmatic);
SetAppBusy(true);
AppendOut(TxtAppOutput, "=== Updating all apps (winget upgrade --all, as Administrator) ===\r\n");
SetExportProgress("Updating apps (winget)...", indeterminate: true);
ShowStatusBarProgress(1, true);
AnnounceSettled("Updating every app. This needs Administrator approval and cannot be stopped once it starts; progress appears under Output details.");
string outcome;
try { outcome = await RunElevatedUpdateAllAsync(); }
catch (Exception ex) { outcome = "App updates failed: " + ex.Message; }
finally
{
_reinstalling = false;
_updateAllElevated = false;
SetAppBusy(false);
BtnAppUpdateAll.Focus(FocusState.Programmatic);
ShowStatusBarProgress(1, false);
}
SetExportOutcome(outcome);
AppendOut(TxtAppOutput, "\r\n--- " + outcome + " ---\r\n");
AnnounceSettled(outcome, 2000);
// Versions changed under the inventory's feet; rescan quietly so the
// list (and its export) reflects what is now installed.
if (_appScanned) ScanApps(announceStart: false);
}
private async System.Threading.Tasks.Task<string> RunElevatedUpdateAllAsync()
{
string log = GuardPaths.AppUpdateLogPath;
GuardPaths.EnsureLogsDir();
try { System.IO.File.WriteAllText(log, ""); } catch { }
var tail = new LogTail(log, startAtEnd: false);
// winget under an elevated cmd, redirecting all output to the log (cmd's
// redirect keeps winget's own byte stream, so the tail reads it as
// winget wrote it). --include-unknown so packages with an undetectable
// installed version are considered too. $LASTEXITCODE is winget's.
string inner = "winget upgrade --all --silent --include-unknown --disable-interactivity "
+ "--accept-package-agreements --accept-source-agreements > \"" + log + "\" 2>&1";
string script = "& cmd.exe /c '" + ProcessRunner.PsQuote(inner) + "'\nexit $LASTEXITCODE";
string? err = null;
var runTask = System.Threading.Tasks.Task.Run(() => ProcessRunner.RunPowerShellElevatedCode(script, out err));
while (!runTask.IsCompleted)
{
await System.Threading.Tasks.Task.Delay(500);
PumpAppUpdateLog(tail);
}
PumpAppUpdateLog(tail);
int code = await runTask;
if (code == ProcessRunner.ElevationDeclined)
return "App updates cancelled - Administrator approval was declined.";
if (code == ProcessRunner.ElevationLaunchFailed)
return "App updates could not start" + (err != null ? " - " + err : ".");
// Name the apps that failed rather than making the user hunt the log for
// "failed": parse the per-app blocks winget printed. Best-effort, so on
// any parse miss (localized winget) we fall back to the exit-code text.
string logText = "";
try { logText = System.IO.File.ReadAllText(log); } catch { }
var (total, failed) = ParseUpdateAllLog(logText);
if (failed.Count > 0)
{
AppendOut(TxtAppOutput, "\r\nCould not update: " + string.Join(", ", failed) + "\r\n");
int updated = Math.Max(0, total - failed.Count);
string names = failed.Count <= 4
? string.Join(", ", failed)
: string.Join(", ", failed.GetRange(0, 4)) + ", and " + (failed.Count - 4) + " more";
return "App updates done: " + updated + " updated, " + failed.Count
+ " could not be updated (" + names + ").";
}
return DescribeWingetUpgradeExit(code);
}
private void PumpAppUpdateLog(LogTail tail)
{
foreach (var line in tail.ReadNewLines()) AppendOut(TxtAppOutput, line + "\r\n");
}
// Per-app success/failure from a `winget upgrade --all` log. Each app prints
// a "(n/m) <Found> <Name> [<Id>]" header (the bracketed id and the (n/m)
// counter are locale-neutral structure; the verb and result lines are not).
// A block counts as failed when it carries a failure marker and no success
// marker; anything unrecognized is left uncounted, so a localized winget
// just yields an empty list and the caller uses winget's exit code instead.
private static readonly System.Text.RegularExpressions.Regex UpdateHeaderRx =
new(@"^\(\d+/\d+\)\s+\S+\s+(.+?)\s+\[[^\]]+\]",
System.Text.RegularExpressions.RegexOptions.Multiline);
private static (int total, List<string> failed) ParseUpdateAllLog(string logText)
{
var failed = new List<string>();
var matches = UpdateHeaderRx.Matches(logText ?? "");
for (int i = 0; i < matches.Count; i++)
{
string name = matches[i].Groups[1].Value.Trim();
int start = matches[i].Index + matches[i].Length;
int end = i + 1 < matches.Count ? matches[i + 1].Index : logText!.Length;
string block = logText!.Substring(start, end - start);
bool ok = block.Contains("Successfully installed", StringComparison.OrdinalIgnoreCase);
bool bad = !ok && block.Contains("failed", StringComparison.OrdinalIgnoreCase);
if (bad) failed.Add(name);
}
return (matches.Count, failed);