/ src / settings-ui / Settings.UI / ViewModels / EnvironmentVariablesViewModel.cs
EnvironmentVariablesViewModel.cs
  1  // Copyright (c) Microsoft Corporation
  2  // The Microsoft Corporation licenses this file to you under the MIT license.
  3  // See the LICENSE file in the project root for more information.
  4  
  5  using System;
  6  using System.Runtime.CompilerServices;
  7  using System.Threading;
  8  
  9  using global::PowerToys.GPOWrapper;
 10  using Microsoft.PowerToys.Settings.UI.Library;
 11  using Microsoft.PowerToys.Settings.UI.Library.Helpers;
 12  using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
 13  using Microsoft.PowerToys.Settings.UI.Library.ViewModels.Commands;
 14  using PowerToys.Interop;
 15  using Settings.UI.Library.Enumerations;
 16  
 17  namespace Microsoft.PowerToys.Settings.UI.ViewModels
 18  {
 19      public partial class EnvironmentVariablesViewModel : Observable
 20      {
 21          private bool _isElevated;
 22          private GpoRuleConfigured _enabledGpoRuleConfiguration;
 23          private bool _enabledStateIsGPOConfigured;
 24          private bool _isEnabled;
 25  
 26          private SettingsUtils SettingsUtils { get; set; }
 27  
 28          private GeneralSettings GeneralSettingsConfig { get; set; }
 29  
 30          private EnvironmentVariablesSettings Settings { get; set; }
 31  
 32          private Func<string, int> SendConfigMSG { get; }
 33  
 34          public ButtonClickCommand LaunchEventHandler => new ButtonClickCommand(Launch);
 35  
 36          public bool IsEnabled
 37          {
 38              get => _isEnabled;
 39  
 40              set
 41              {
 42                  if (_enabledStateIsGPOConfigured)
 43                  {
 44                      // If it's GPO configured, shouldn't be able to change this state.
 45                      return;
 46                  }
 47  
 48                  if (value != _isEnabled)
 49                  {
 50                      _isEnabled = value;
 51  
 52                      // Set the status in the general settings configuration
 53                      GeneralSettingsConfig.Enabled.EnvironmentVariables = value;
 54                      OutGoingGeneralSettings snd = new OutGoingGeneralSettings(GeneralSettingsConfig);
 55  
 56                      SendConfigMSG(snd.ToString());
 57                      OnPropertyChanged(nameof(IsEnabled));
 58                  }
 59              }
 60          }
 61  
 62          public bool IsEnabledGpoConfigured
 63          {
 64              get => _enabledStateIsGPOConfigured;
 65          }
 66  
 67          public bool LaunchAdministratorEnabled => IsEnabled && !_isElevated;
 68  
 69          public bool LaunchAdministrator
 70          {
 71              get => Settings.Properties.LaunchAdministrator;
 72              set
 73              {
 74                  if (value != Settings.Properties.LaunchAdministrator)
 75                  {
 76                      Settings.Properties.LaunchAdministrator = value;
 77                      NotifyPropertyChanged();
 78                  }
 79              }
 80          }
 81  
 82          public EnvironmentVariablesViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<EnvironmentVariablesSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc, bool isElevated)
 83          {
 84              SettingsUtils = settingsUtils;
 85              GeneralSettingsConfig = settingsRepository.SettingsConfig;
 86              Settings = moduleSettingsRepository.SettingsConfig;
 87              SendConfigMSG = ipcMSGCallBackFunc;
 88              _isElevated = isElevated;
 89              InitializeEnabledValue();
 90          }
 91  
 92          private void InitializeEnabledValue()
 93          {
 94              _enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredEnvironmentVariablesEnabledValue();
 95              if (_enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled)
 96              {
 97                  // Get the enabled state from GPO.
 98                  _enabledStateIsGPOConfigured = true;
 99                  _isEnabled = _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled;
100              }
101              else
102              {
103                  _isEnabled = GeneralSettingsConfig.Enabled.EnvironmentVariables;
104              }
105          }
106  
107          public void Launch()
108          {
109              string eventName = !_isElevated && LaunchAdministrator
110                  ? Constants.ShowEnvironmentVariablesAdminSharedEvent()
111                  : Constants.ShowEnvironmentVariablesSharedEvent();
112  
113              using (var eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, eventName))
114              {
115                  eventHandle.Set();
116              }
117          }
118  
119          public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
120          {
121              OnPropertyChanged(propertyName);
122              SettingsUtils.SaveSettings(Settings.ToJsonString(), EnvironmentVariablesSettings.ModuleName);
123          }
124  
125          public void RefreshEnabledState()
126          {
127              InitializeEnabledValue();
128              OnPropertyChanged(nameof(IsEnabled));
129          }
130      }
131  }