Files
PowerToys/src/modules/FileLocksmith/FileLocksmithUI/ViewModels/MainViewModel.cs
Josh Soref bf16e10baf Updates for check-spelling v0.0.25 (#40386)
## Summary of the Pull Request

- #39572 updated check-spelling but ignored:
   > 🐣 Breaking Changes
[Code Scanning action requires a Code Scanning
Ruleset](https://github.com/check-spelling/check-spelling/wiki/Breaking-Change:-Code-Scanning-action-requires-a-Code-Scanning-Ruleset)
If you use SARIF reporting, then instead of the workflow yielding an 
when it fails, it will rely on [github-advanced-security
🤖](https://github.com/apps/github-advanced-security) to report the
failure. You will need to adjust your checks for PRs.

This means that check-spelling hasn't been properly doing its job 😦.

I'm sorry, I should have pushed a thing to this repo earlier,...

Anyway, as with most refreshes, this comes with a number of fixes, some
are fixes for typos that snuck in before the 0.0.25 upgrade, some are
for things that snuck in after, some are based on new rules in
spell-check-this, and some are hand written patterns based on running
through this repository a few times.

About the 🐣 **breaking change**: someone needs to create a ruleset for
this repository (see [Code Scanning action requires a Code Scanning
Ruleset: Sample ruleset

](https://github.com/check-spelling/check-spelling/wiki/Breaking-Change:-Code-Scanning-action-requires-a-Code-Scanning-Ruleset#sample-ruleset)).

The alternative to adding a ruleset is to change the condition to not
use sarif for this repository. In general, I think the github
integration from sarif is prettier/more helpful, so I think that it's
the better choice.

You can see an example of it working in:
- https://github.com/check-spelling-sandbox/PowerToys/pull/23

---------

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: Mike Griese <migrie@microsoft.com>
Co-authored-by: Dustin L. Howett <dustin@howett.net>
2025-07-08 17:16:52 -05:00

203 lines
5.8 KiB
C#

// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using ManagedCommon;
using PowerToys.FileLocksmithLib.Interop;
namespace PowerToys.FileLocksmithUI.ViewModels
{
#pragma warning disable CA1708 // Identifiers should differ by more than case
public partial class MainViewModel : ObservableObject, IDisposable
#pragma warning restore CA1708 // Identifiers should differ by more than case
{
public IAsyncRelayCommand LoadProcessesCommand { get; }
private bool _isLoading;
private bool _isElevated;
private string[] paths;
private bool _disposed;
private CancellationTokenSource _cancelProcessWatching;
public ObservableCollection<ProcessResult> Processes { get; } = new();
public bool IsLoading
{
get
{
return _isLoading;
}
set
{
_isLoading = value;
OnPropertyChanged(nameof(IsLoading));
}
}
public bool IsElevated
{
get
{
return _isElevated;
}
set
{
_isElevated = value;
OnPropertyChanged(nameof(IsElevated));
}
}
public string[] Paths
{
get => paths;
set
{
paths = value;
OnPropertyChanged(nameof(Paths));
}
}
public string PathsToString
{
get
{
return string.Join("\n", paths);
}
}
public MainViewModel()
{
paths = NativeMethods.ReadPathsFromFile();
Logger.LogInfo($"Starting FileLocksmith with {paths.Length} files selected.");
LoadProcessesCommand = new AsyncRelayCommand(LoadProcessesAsync);
}
private async Task LoadProcessesAsync()
{
IsLoading = true;
Processes.Clear();
if (_cancelProcessWatching is not null)
{
_cancelProcessWatching.Cancel();
}
_cancelProcessWatching = new CancellationTokenSource();
var processes_found = await FindProcesses(paths);
if (processes_found is not null)
{
foreach (ProcessResult p in processes_found)
{
Processes.Add(p);
WatchProcess(p, _cancelProcessWatching.Token);
}
}
IsLoading = false;
}
private async Task<List<ProcessResult>> FindProcesses(string[] paths)
{
var results = new List<ProcessResult>();
await Task.Run(() =>
{
results = NativeMethods.FindProcessesRecursive(paths)?.ToList();
});
return results;
}
private async void WatchProcess(ProcessResult process, CancellationToken token)
{
try
{
Process handle = Process.GetProcessById((int)process.pid);
try
{
await handle.WaitForExitAsync(token);
}
catch (TaskCanceledException)
{
// Nothing to do, normal operation
}
if (handle.HasExited)
{
Processes.Remove(process);
}
}
catch (Exception ex)
{
Logger.LogError($"Couldn't add a waiter to wait for a process to exit. PID = {process.pid} and Name = {process.name}.", ex);
Processes.Remove(process); // If we couldn't get a handle to the process or it has exited in the meanwhile, don't show it.
}
}
[RelayCommand]
public void EndTask(ProcessResult selectedProcess)
{
try
{
Process handle = Process.GetProcessById((int)selectedProcess.pid);
try
{
handle.Kill();
}
catch (Exception ex)
{
Logger.LogError($"Couldn't kill process {selectedProcess.name} with PID {selectedProcess.pid}.", ex);
}
}
catch (Exception ex)
{
Logger.LogError($"Couldn't get a handle to kill process {selectedProcess.name} with PID {selectedProcess.pid}. Likely has been killed already.", ex);
Processes.Remove(selectedProcess); // If we couldn't get a handle to the process, remove it from the list, since it's likely been killed already.
}
}
[RelayCommand]
public void RestartElevated()
{
if (NativeMethods.StartAsElevated(paths))
{
// TODO gentler exit
Environment.Exit(0);
}
else
{
// TODO report error?
Logger.LogError($"Couldn't restart as elevated.");
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_disposed = true;
}
}
}
}
}