186 lines
6.7 KiB
C#
186 lines
6.7 KiB
C#
/*
|
|
* ThreadPilot - Advanced Windows Process and Power Plan Manager
|
|
* Copyright (C) 2025 Prime Build
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, version 3 only.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using ThreadPilot.Models;
|
|
using ThreadPilot.Services;
|
|
using ThreadPilot.ViewModels;
|
|
|
|
namespace ThreadPilot
|
|
{
|
|
public partial class MainWindowViewModel : BaseViewModel
|
|
{
|
|
private readonly IProcessMonitorManagerService? _processMonitorManagerService;
|
|
private readonly INotificationService? _notificationService;
|
|
private readonly IElevationService? _elevationService;
|
|
private readonly ISecurityService? _securityService;
|
|
|
|
[ObservableProperty]
|
|
private bool isProcessMonitoringActive = false;
|
|
|
|
[ObservableProperty]
|
|
private string processMonitoringStatusText = "Process Monitoring: Inactive";
|
|
|
|
[ObservableProperty]
|
|
private bool isRunningAsAdministrator = false;
|
|
|
|
[ObservableProperty]
|
|
private string elevationStatusText = "Checking elevation status...";
|
|
|
|
[ObservableProperty]
|
|
private bool showElevationPrompt = false;
|
|
|
|
public MainWindowViewModel(
|
|
ILogger<MainWindowViewModel> logger,
|
|
IEnhancedLoggingService? enhancedLoggingService = null,
|
|
IProcessMonitorManagerService? processMonitorManagerService = null,
|
|
INotificationService? notificationService = null,
|
|
IElevationService? elevationService = null,
|
|
ISecurityService? securityService = null)
|
|
: base(logger, enhancedLoggingService)
|
|
{
|
|
_processMonitorManagerService = processMonitorManagerService;
|
|
_notificationService = notificationService;
|
|
_elevationService = elevationService;
|
|
_securityService = securityService;
|
|
}
|
|
|
|
public override async Task InitializeAsync()
|
|
{
|
|
await ExecuteAsync(async () =>
|
|
{
|
|
// Subscribe to service events
|
|
if (_processMonitorManagerService != null)
|
|
{
|
|
_processMonitorManagerService.ServiceStatusChanged += OnServiceStatusChanged;
|
|
}
|
|
|
|
// Initialize status
|
|
await UpdateStatusAsync();
|
|
UpdateElevationStatus();
|
|
|
|
await LogUserActionAsync("MainWindow", "Initialized main window", "Application startup");
|
|
}, "Initializing main window...");
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task ToggleProcessMonitoringAsync()
|
|
{
|
|
if (_processMonitorManagerService == null) return;
|
|
|
|
await ExecuteAsync(async () =>
|
|
{
|
|
if (IsProcessMonitoringActive)
|
|
{
|
|
await _processMonitorManagerService.StopAsync();
|
|
await LogUserActionAsync("ProcessMonitoring", "Stopped process monitoring", "User action");
|
|
}
|
|
else
|
|
{
|
|
await _processMonitorManagerService.StartAsync();
|
|
await LogUserActionAsync("ProcessMonitoring", "Started process monitoring", "User action");
|
|
}
|
|
}, IsProcessMonitoringActive ? "Stopping monitoring..." : "Starting monitoring...");
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task RequestElevationAsync()
|
|
{
|
|
if (_elevationService == null) return;
|
|
|
|
await ExecuteAsync(async () =>
|
|
{
|
|
var success = await _elevationService.RequestElevationIfNeeded();
|
|
if (success)
|
|
{
|
|
await LogUserActionAsync("Elevation", "Requested elevation", "User action");
|
|
}
|
|
else
|
|
{
|
|
await LogUserActionAsync("Elevation", "Elevation request failed or cancelled", "User action");
|
|
}
|
|
}, "Requesting elevation...");
|
|
}
|
|
|
|
private async Task UpdateStatusAsync()
|
|
{
|
|
try
|
|
{
|
|
// Update process monitoring status
|
|
if (_processMonitorManagerService != null)
|
|
{
|
|
IsProcessMonitoringActive = _processMonitorManagerService.IsRunning;
|
|
ProcessMonitoringStatusText = IsProcessMonitoringActive
|
|
? "Process Monitoring: Active"
|
|
: "Process Monitoring: Inactive";
|
|
}
|
|
|
|
// Update elevation status
|
|
UpdateElevationStatus();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SetError("Failed to update status", ex);
|
|
}
|
|
}
|
|
|
|
private void UpdateElevationStatus()
|
|
{
|
|
if (_elevationService == null)
|
|
{
|
|
IsRunningAsAdministrator = false;
|
|
ElevationStatusText = "Elevation service not available";
|
|
ShowElevationPrompt = false;
|
|
return;
|
|
}
|
|
|
|
IsRunningAsAdministrator = _elevationService.IsRunningAsAdministrator();
|
|
ElevationStatusText = _elevationService.GetElevationStatus();
|
|
ShowElevationPrompt = !IsRunningAsAdministrator;
|
|
}
|
|
|
|
private void OnServiceStatusChanged(object? sender, ServiceStatusEventArgs e)
|
|
{
|
|
// Marshal UI updates to the UI thread to prevent cross-thread access exceptions
|
|
System.Windows.Application.Current.Dispatcher.InvokeAsync(() =>
|
|
{
|
|
IsProcessMonitoringActive = e.IsRunning;
|
|
ProcessMonitoringStatusText = $"Process Monitoring: {e.Status}";
|
|
});
|
|
}
|
|
|
|
public void UpdateProcessMonitoringStatus(bool isActive, string status)
|
|
{
|
|
IsProcessMonitoringActive = isActive;
|
|
ProcessMonitoringStatusText = $"Process Monitoring: {status}";
|
|
}
|
|
|
|
protected override void OnDispose()
|
|
{
|
|
if (_processMonitorManagerService != null)
|
|
{
|
|
_processMonitorManagerService.ServiceStatusChanged -= OnServiceStatusChanged;
|
|
}
|
|
|
|
base.OnDispose();
|
|
}
|
|
}
|
|
}
|