/ src / settings-ui / Settings.UI / ViewModels / PowerRenameViewModel.cs
PowerRenameViewModel.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.IO;
  7  using System.Runtime.CompilerServices;
  8  using System.Threading.Tasks;
  9  using System.Windows.Input;
 10  
 11  using global::PowerToys.GPOWrapper;
 12  using ManagedCommon;
 13  using Microsoft.PowerToys.Settings.UI.Helpers;
 14  using Microsoft.PowerToys.Settings.UI.Library;
 15  using Microsoft.PowerToys.Settings.UI.Library.Helpers;
 16  using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
 17  
 18  namespace Microsoft.PowerToys.Settings.UI.ViewModels
 19  {
 20      public partial class PowerRenameViewModel : Observable
 21      {
 22          private GeneralSettings GeneralSettingsConfig { get; set; }
 23  
 24          private readonly SettingsUtils _settingsUtils;
 25  
 26          private const string ModuleName = PowerRenameSettings.ModuleName;
 27  
 28          private string _settingsConfigFileFolder = string.Empty;
 29  
 30          private PowerRenameSettings Settings { get; set; }
 31  
 32          private Func<string, int> SendConfigMSG { get; }
 33  
 34          public PowerRenameViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "")
 35          {
 36              // Update Settings file folder:
 37              _settingsConfigFileFolder = configFileSubfolder;
 38              _settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
 39  
 40              ArgumentNullException.ThrowIfNull(settingsRepository);
 41  
 42              GeneralSettingsConfig = settingsRepository.SettingsConfig;
 43  
 44              try
 45              {
 46                  PowerRenameLocalProperties localSettings = _settingsUtils.GetSettingsOrDefault<PowerRenameLocalProperties>(GetSettingsSubPath(), "power-rename-settings.json");
 47                  Settings = new PowerRenameSettings(localSettings);
 48              }
 49              catch (Exception e)
 50              {
 51                  Logger.LogError($"Exception encountered while reading {ModuleName} settings.", e);
 52  #if DEBUG
 53                  if (e is ArgumentException || e is ArgumentNullException || e is PathTooLongException)
 54                  {
 55                      throw;
 56                  }
 57  #endif
 58                  PowerRenameLocalProperties localSettings = new PowerRenameLocalProperties();
 59                  Settings = new PowerRenameSettings(localSettings);
 60                  _settingsUtils.SaveSettings(localSettings.ToJsonString(), GetSettingsSubPath(), "power-rename-settings.json");
 61              }
 62  
 63              // set the callback functions value to handle outgoing IPC message.
 64              SendConfigMSG = ipcMSGCallBackFunc;
 65  
 66              _powerRenameEnabledOnContextMenu = Settings.Properties.ShowIcon.Value;
 67              _powerRenameEnabledOnContextExtendedMenu = Settings.Properties.ExtendedContextMenuOnly.Value;
 68              _powerRenameRestoreFlagsOnLaunch = Settings.Properties.PersistState.Value;
 69              _powerRenameMaxDispListNumValue = Settings.Properties.MaxMRUSize.Value;
 70              _autoComplete = Settings.Properties.MRUEnabled.Value;
 71              _powerRenameUseBoostLib = Settings.Properties.UseBoostLib.Value;
 72  
 73              // Initialize extension helpers
 74              HeifExtension = new StoreExtensionHelper(
 75                  "Microsoft.HEIFImageExtension_8wekyb3d8bbwe",
 76                  "ms-windows-store://pdp/?ProductId=9PMMSR1CGPWG",
 77                  "HEIF");
 78  
 79              AvifExtension = new StoreExtensionHelper(
 80                  "Microsoft.AV1VideoExtension_8wekyb3d8bbwe",
 81                  "ms-windows-store://pdp/?ProductId=9MVZQVXJBQ9V",
 82                  "AV1");
 83  
 84              InitializeEnabledValue();
 85          }
 86  
 87          private void InitializeEnabledValue()
 88          {
 89              _enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredPowerRenameEnabledValue();
 90              if (_enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled)
 91              {
 92                  // Get the enabled state from GPO.
 93                  _enabledStateIsGPOConfigured = true;
 94                  _powerRenameEnabled = _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled;
 95              }
 96              else
 97              {
 98                  _powerRenameEnabled = GeneralSettingsConfig.Enabled.PowerRename;
 99              }
100          }
101  
102          private GpoRuleConfigured _enabledGpoRuleConfiguration;
103          private bool _enabledStateIsGPOConfigured;
104          private bool _powerRenameEnabled;
105          private bool _powerRenameEnabledOnContextMenu;
106          private bool _powerRenameEnabledOnContextExtendedMenu;
107          private bool _powerRenameRestoreFlagsOnLaunch;
108          private int _powerRenameMaxDispListNumValue;
109          private bool _autoComplete;
110          private bool _powerRenameUseBoostLib;
111  
112          public bool IsEnabled
113          {
114              get
115              {
116                  return _powerRenameEnabled;
117              }
118  
119              set
120              {
121                  if (_enabledStateIsGPOConfigured)
122                  {
123                      // If it's GPO configured, shouldn't be able to change this state.
124                      return;
125                  }
126  
127                  if (value != _powerRenameEnabled)
128                  {
129                      GeneralSettingsConfig.Enabled.PowerRename = value;
130                      OutGoingGeneralSettings snd = new OutGoingGeneralSettings(GeneralSettingsConfig);
131  
132                      SendConfigMSG(snd.ToString());
133  
134                      _powerRenameEnabled = value;
135                      OnPropertyChanged(nameof(IsEnabled));
136                      RaisePropertyChanged(nameof(GlobalAndMruEnabled));
137                  }
138              }
139          }
140  
141          public bool IsEnabledGpoConfigured
142          {
143              get => _enabledStateIsGPOConfigured;
144          }
145  
146          public bool MRUEnabled
147          {
148              get
149              {
150                  return _autoComplete;
151              }
152  
153              set
154              {
155                  if (value != _autoComplete)
156                  {
157                      _autoComplete = value;
158                      Settings.Properties.MRUEnabled.Value = value;
159                      RaisePropertyChanged();
160                      RaisePropertyChanged(nameof(GlobalAndMruEnabled));
161                  }
162              }
163          }
164  
165          public bool GlobalAndMruEnabled
166          {
167              get
168              {
169                  return _autoComplete && _powerRenameEnabled;
170              }
171          }
172  
173          public bool EnabledOnContextMenu
174          {
175              get
176              {
177                  return _powerRenameEnabledOnContextMenu;
178              }
179  
180              set
181              {
182                  if (value != _powerRenameEnabledOnContextMenu)
183                  {
184                      _powerRenameEnabledOnContextMenu = value;
185                      Settings.Properties.ShowIcon.Value = value;
186                      RaisePropertyChanged();
187                  }
188              }
189          }
190  
191          public bool EnabledOnContextExtendedMenu
192          {
193              get
194              {
195                  return _powerRenameEnabledOnContextExtendedMenu;
196              }
197  
198              set
199              {
200                  if (value != _powerRenameEnabledOnContextExtendedMenu)
201                  {
202                      _powerRenameEnabledOnContextExtendedMenu = value;
203                      Settings.Properties.ExtendedContextMenuOnly.Value = value;
204                      RaisePropertyChanged();
205                  }
206              }
207          }
208  
209          public bool RestoreFlagsOnLaunch
210          {
211              get
212              {
213                  return _powerRenameRestoreFlagsOnLaunch;
214              }
215  
216              set
217              {
218                  if (value != _powerRenameRestoreFlagsOnLaunch)
219                  {
220                      _powerRenameRestoreFlagsOnLaunch = value;
221                      Settings.Properties.PersistState.Value = value;
222                      RaisePropertyChanged();
223                  }
224              }
225          }
226  
227          public int MaxDispListNum
228          {
229              get
230              {
231                  return _powerRenameMaxDispListNumValue;
232              }
233  
234              set
235              {
236                  if (value != _powerRenameMaxDispListNumValue)
237                  {
238                      _powerRenameMaxDispListNumValue = value;
239                      Settings.Properties.MaxMRUSize.Value = value;
240                      RaisePropertyChanged();
241                  }
242              }
243          }
244  
245          public bool UseBoostLib
246          {
247              get
248              {
249                  return _powerRenameUseBoostLib;
250              }
251  
252              set
253              {
254                  if (value != _powerRenameUseBoostLib)
255                  {
256                      _powerRenameUseBoostLib = value;
257                      Settings.Properties.UseBoostLib.Value = value;
258                      RaisePropertyChanged();
259                  }
260              }
261          }
262  
263          public string GetSettingsSubPath()
264          {
265              return _settingsConfigFileFolder + "\\" + ModuleName;
266          }
267  
268          private void RaisePropertyChanged([CallerMemberName] string propertyName = null)
269          {
270              // Notify UI of property change
271              OnPropertyChanged(propertyName);
272  
273              if (SendConfigMSG != null)
274              {
275                  SndPowerRenameSettings snd = new SndPowerRenameSettings(Settings);
276                  SndModuleSettings<SndPowerRenameSettings> ipcMessage = new SndModuleSettings<SndPowerRenameSettings>(snd);
277                  SendConfigMSG(ipcMessage.ToJsonString());
278              }
279          }
280  
281          public void RefreshEnabledState()
282          {
283              InitializeEnabledValue();
284              OnPropertyChanged(nameof(IsEnabled));
285              OnPropertyChanged(nameof(GlobalAndMruEnabled));
286          }
287  
288          // Store extension helpers
289          public StoreExtensionHelper HeifExtension { get; private set; }
290  
291          public StoreExtensionHelper AvifExtension { get; private set; }
292  
293          // Convenience properties for XAML binding
294          public bool IsHeifExtensionInstalled => HeifExtension.IsInstalled;
295  
296          public bool IsAvifExtensionInstalled => AvifExtension.IsInstalled;
297  
298          public ICommand InstallHeifExtensionCommand => HeifExtension.InstallCommand;
299  
300          public ICommand InstallAvifExtensionCommand => AvifExtension.InstallCommand;
301  
302          public void RefreshHeifExtensionStatus()
303          {
304              HeifExtension.RefreshStatus();
305              OnPropertyChanged(nameof(IsHeifExtensionInstalled));
306          }
307  
308          public void RefreshAvifExtensionStatus()
309          {
310              AvifExtension.RefreshStatus();
311              OnPropertyChanged(nameof(IsAvifExtensionInstalled));
312          }
313      }
314  }