/* * 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 Affero 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 Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ namespace ThreadPilot.Models { using System; using System.Collections.Generic; using System.Linq; using CommunityToolkit.Mvvm.ComponentModel; /// /// Condition types for profile triggers. /// public enum ProfileConditionType { SystemLoad, TimeOfDay, PowerState, ProcessCount, MemoryUsage, CpuTemperature, BatteryLevel, NetworkActivity, UserIdle, Custom, } /// /// Comparison operators for conditions. /// public enum ComparisonOperator { Equals, NotEquals, GreaterThan, LessThan, GreaterThanOrEqual, LessThanOrEqual, Contains, NotContains, Between, NotBetween, } /// /// Logical operators for combining conditions. /// public enum LogicalOperator { And, Or, Not, } /// /// System state information for condition evaluation. /// public class SystemState { public double CpuUsage { get; set; } public double MemoryUsage { get; set; } public int ProcessCount { get; set; } public DateTime CurrentTime { get; set; } = DateTime.Now; public bool IsOnBattery { get; set; } public int BatteryLevel { get; set; } public double CpuTemperature { get; set; } public bool IsUserIdle { get; set; } public TimeSpan UserIdleTime { get; set; } public double NetworkActivity { get; set; } public Dictionary CustomProperties { get; set; } = new(); } /// /// Individual condition for profile evaluation. /// public partial class ProfileCondition : ObservableObject { [ObservableProperty] private string name = string.Empty; [ObservableProperty] private ProfileConditionType conditionType; [ObservableProperty] private ComparisonOperator comparisonOperator; [ObservableProperty] private object? value; [ObservableProperty] private object? secondaryValue; // For Between/NotBetween operations [ObservableProperty] private bool isEnabled = true; [ObservableProperty] private string description = string.Empty; /// /// Evaluate this condition against the current system state. /// public bool Evaluate(ProcessModel process, SystemState systemState) { if (!this.IsEnabled) { return true; // Disabled conditions are considered true } try { var actualValue = this.GetActualValue(process, systemState); return CompareValues(actualValue, this.Value, this.SecondaryValue, this.ComparisonOperator); } catch (Exception) { return false; // Failed conditions are considered false } } private object? GetActualValue(ProcessModel process, SystemState systemState) { return this.ConditionType switch { ProfileConditionType.SystemLoad => systemState.CpuUsage, ProfileConditionType.TimeOfDay => systemState.CurrentTime.TimeOfDay.TotalHours, ProfileConditionType.PowerState => systemState.IsOnBattery, ProfileConditionType.ProcessCount => systemState.ProcessCount, ProfileConditionType.MemoryUsage => systemState.MemoryUsage, ProfileConditionType.CpuTemperature => systemState.CpuTemperature, ProfileConditionType.BatteryLevel => systemState.BatteryLevel, ProfileConditionType.NetworkActivity => systemState.NetworkActivity, ProfileConditionType.UserIdle => systemState.IsUserIdle, ProfileConditionType.Custom => systemState.CustomProperties.GetValueOrDefault(this.Name), _ => null, }; } private static bool CompareValues(object? actual, object? expected, object? secondary, ComparisonOperator op) { if (actual == null || expected == null) { return false; } return op switch { ComparisonOperator.Equals => actual.Equals(expected), ComparisonOperator.NotEquals => !actual.Equals(expected), ComparisonOperator.GreaterThan => Comparer.Default.Compare(actual, expected) > 0, ComparisonOperator.LessThan => Comparer.Default.Compare(actual, expected) < 0, ComparisonOperator.GreaterThanOrEqual => Comparer.Default.Compare(actual, expected) >= 0, ComparisonOperator.LessThanOrEqual => Comparer.Default.Compare(actual, expected) <= 0, ComparisonOperator.Contains => actual.ToString()?.Contains(expected.ToString() ?? string.Empty) ?? false, ComparisonOperator.NotContains => !(actual.ToString()?.Contains(expected.ToString() ?? string.Empty) ?? false), ComparisonOperator.Between => secondary != null && Comparer.Default.Compare(actual, expected) >= 0 && Comparer.Default.Compare(actual, secondary) <= 0, ComparisonOperator.NotBetween => secondary != null && !(Comparer.Default.Compare(actual, expected) >= 0 && Comparer.Default.Compare(actual, secondary) <= 0), _ => false, }; } } /// /// Group of conditions with logical operators. /// public partial class ConditionGroup : ObservableObject { [ObservableProperty] private string name = string.Empty; [ObservableProperty] private LogicalOperator logicalOperator = LogicalOperator.And; [ObservableProperty] private List conditions = new(); [ObservableProperty] private List subGroups = new(); [ObservableProperty] private bool isEnabled = true; /// /// Evaluate this condition group. /// public bool Evaluate(ProcessModel process, SystemState systemState) { if (!this.IsEnabled) { return true; } var conditionResults = this.Conditions.Select(c => c.Evaluate(process, systemState)).ToList(); var subGroupResults = this.SubGroups.Select(g => g.Evaluate(process, systemState)).ToList(); var allResults = conditionResults.Concat(subGroupResults).ToList(); if (!allResults.Any()) { return true; // No conditions means always true } return this.LogicalOperator switch { LogicalOperator.And => allResults.All(r => r), LogicalOperator.Or => allResults.Any(r => r), LogicalOperator.Not => !allResults.All(r => r), _ => false, }; } } /// /// Extended ProfileModel with conditional triggers. /// public partial class ConditionalProcessProfile : ProfileModel { [ObservableProperty] private List conditionGroups = new(); [ObservableProperty] private TimeSpan autoApplyDelay = TimeSpan.FromSeconds(5); [ObservableProperty] private int priority = 0; // Higher priority profiles are applied first [ObservableProperty] private bool isAutoApplyEnabled = true; [ObservableProperty] private DateTime lastEvaluated = DateTime.MinValue; [ObservableProperty] private DateTime lastApplied = DateTime.MinValue; [ObservableProperty] private bool wasLastEvaluationTrue = false; [ObservableProperty] private string lastEvaluationReason = string.Empty; /// /// Check if this profile should be applied based on conditions. /// public bool ShouldApply(ProcessModel process, SystemState systemState) { if (!this.IsAutoApplyEnabled) { return false; } this.LastEvaluated = DateTime.UtcNow; try { // If no condition groups, always apply (like regular profile) if (!this.ConditionGroups.Any()) { this.WasLastEvaluationTrue = true; this.LastEvaluationReason = "No conditions defined"; return true; } // Evaluate all condition groups (AND logic between groups) var results = this.ConditionGroups.Select(g => g.Evaluate(process, systemState)).ToList(); var shouldApply = results.All(r => r); this.WasLastEvaluationTrue = shouldApply; this.LastEvaluationReason = shouldApply ? "All condition groups satisfied" : $"Failed conditions: {string.Join(", ", this.ConditionGroups.Where((g, i) => !results[i]).Select(g => g.Name))}"; return shouldApply; } catch (Exception ex) { this.WasLastEvaluationTrue = false; this.LastEvaluationReason = $"Evaluation error: {ex.Message}"; return false; } } /// /// Check if enough time has passed since last application. /// public bool CanApplyNow() { return DateTime.UtcNow - this.LastApplied >= this.AutoApplyDelay; } /// /// Mark this profile as applied. /// public void MarkAsApplied() { this.LastApplied = DateTime.UtcNow; } } }