/ src / settings-ui / Settings.UI / ViewModels / FileLocksmithViewModel.cs
FileLocksmithViewModel.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.Globalization;
  7  using System.Text.Json;
  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.SerializationContext;
 14  
 15  namespace Microsoft.PowerToys.Settings.UI.ViewModels
 16  {
 17      public partial class FileLocksmithViewModel : Observable
 18      {
 19          private GeneralSettings GeneralSettingsConfig { get; set; }
 20  
 21          private readonly SettingsUtils _settingsUtils;
 22  
 23          private FileLocksmithSettings Settings { get; set; }
 24  
 25          private const string ModuleName = FileLocksmithSettings.ModuleName;
 26  
 27          private string _settingsConfigFileFolder = string.Empty;
 28  
 29          public FileLocksmithViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "")
 30          {
 31              _settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
 32  
 33              // To obtain the general settings configurations of PowerToys Settings.
 34              ArgumentNullException.ThrowIfNull(settingsRepository);
 35  
 36              GeneralSettingsConfig = settingsRepository.SettingsConfig;
 37  
 38              try
 39              {
 40                  FileLocksmithLocalProperties localSettings = _settingsUtils.GetSettingsOrDefault<FileLocksmithLocalProperties>(GetSettingsSubPath(), "file-locksmith-settings.json");
 41                  Settings = new FileLocksmithSettings(localSettings);
 42              }
 43              catch (Exception)
 44              {
 45                  FileLocksmithLocalProperties localSettings = new FileLocksmithLocalProperties();
 46                  Settings = new FileLocksmithSettings(localSettings);
 47                  _settingsUtils.SaveSettings(localSettings.ToJsonString(), GetSettingsSubPath(), "file-locksmith-settings.json");
 48              }
 49  
 50              InitializeEnabledValue();
 51  
 52              // set the callback functions value to handle outgoing IPC message.
 53              SendConfigMSG = ipcMSGCallBackFunc;
 54  
 55              _fileLocksmithEnabledOnContextExtendedMenu = Settings.Properties.ExtendedContextMenuOnly.Value;
 56          }
 57  
 58          public string GetSettingsSubPath()
 59          {
 60              return _settingsConfigFileFolder + "\\" + ModuleName;
 61          }
 62  
 63          private void InitializeEnabledValue()
 64          {
 65              _enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredFileLocksmithEnabledValue();
 66              if (_enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled)
 67              {
 68                  // Get the enabled state from GPO.
 69                  _enabledStateIsGPOConfigured = true;
 70                  _isFileLocksmithEnabled = _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled;
 71              }
 72              else
 73              {
 74                  _isFileLocksmithEnabled = GeneralSettingsConfig.Enabled.FileLocksmith;
 75              }
 76          }
 77  
 78          public bool IsFileLocksmithEnabled
 79          {
 80              get => _isFileLocksmithEnabled;
 81              set
 82              {
 83                  if (_enabledStateIsGPOConfigured)
 84                  {
 85                      // If it's GPO configured, shouldn't be able to change this state.
 86                      return;
 87                  }
 88  
 89                  if (_isFileLocksmithEnabled != value)
 90                  {
 91                      _isFileLocksmithEnabled = value;
 92  
 93                      GeneralSettingsConfig.Enabled.FileLocksmith = value;
 94                      OnPropertyChanged(nameof(IsFileLocksmithEnabled));
 95  
 96                      OutGoingGeneralSettings outgoing = new OutGoingGeneralSettings(GeneralSettingsConfig);
 97                      SendConfigMSG(outgoing.ToString());
 98  
 99                      // TODO: Implement when this module has properties.
100                      NotifySettingsChanged();
101                  }
102              }
103          }
104  
105          public bool EnabledOnContextExtendedMenu
106          {
107              get
108              {
109                  return _fileLocksmithEnabledOnContextExtendedMenu;
110              }
111  
112              set
113              {
114                  if (value != _fileLocksmithEnabledOnContextExtendedMenu)
115                  {
116                      _fileLocksmithEnabledOnContextExtendedMenu = value;
117                      Settings.Properties.ExtendedContextMenuOnly.Value = value;
118                      OnPropertyChanged(nameof(EnabledOnContextExtendedMenu));
119  
120                      NotifySettingsChanged();
121                  }
122              }
123          }
124  
125          public bool IsEnabledGpoConfigured
126          {
127              get => _enabledStateIsGPOConfigured;
128          }
129  
130          private void NotifySettingsChanged()
131          {
132              // Using InvariantCulture as this is an IPC message
133              SendConfigMSG(
134                     string.Format(
135                         CultureInfo.InvariantCulture,
136                         "{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
137                         FileLocksmithSettings.ModuleName,
138                         JsonSerializer.Serialize(Settings, SourceGenerationContextContext.Default.FileLocksmithSettings)));
139          }
140  
141          private Func<string, int> SendConfigMSG { get; }
142  
143          private GpoRuleConfigured _enabledGpoRuleConfiguration;
144          private bool _enabledStateIsGPOConfigured;
145          private bool _isFileLocksmithEnabled;
146          private bool _fileLocksmithEnabledOnContextExtendedMenu;
147  
148          public void RefreshEnabledState()
149          {
150              InitializeEnabledValue();
151              OnPropertyChanged(nameof(IsFileLocksmithEnabled));
152          }
153      }
154  }