ProfileVariablesSet.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.Collections.ObjectModel;
  7  using System.Threading.Tasks;
  8  
  9  using CommunityToolkit.Mvvm.ComponentModel;
 10  using EnvironmentVariablesUILib.Helpers;
 11  
 12  namespace EnvironmentVariablesUILib.Models
 13  {
 14      public partial class ProfileVariablesSet : VariablesSet
 15      {
 16          [ObservableProperty]
 17          private bool _isEnabled;
 18  
 19          public ProfileVariablesSet()
 20              : base()
 21          {
 22              Type = VariablesSetType.Profile;
 23              IconPath = ProfileIconPath;
 24          }
 25  
 26          public ProfileVariablesSet(Guid id, string name)
 27              : base(id, name, VariablesSetType.Profile)
 28          {
 29              IconPath = ProfileIconPath;
 30          }
 31  
 32          public Task Apply()
 33          {
 34              return Task.Run(() =>
 35              {
 36                  foreach (var variable in Variables)
 37                  {
 38                      var applyToSystem = variable.ApplyToSystem;
 39  
 40                      // Get existing variable with the same name if it exist
 41                      var variableToOverride = EnvironmentVariablesHelper.GetExisting(variable.Name);
 42  
 43                      // It exists. Rename it to preserve it.
 44                      if (variableToOverride != null && variableToOverride.ParentType == VariablesSetType.User)
 45                      {
 46                          variableToOverride.Name = EnvironmentVariablesHelper.GetBackupVariableName(variableToOverride, this.Name);
 47  
 48                          // Backup the variable
 49                          if (!EnvironmentVariablesHelper.SetVariableWithoutNotify(variableToOverride))
 50                          {
 51                              LoggerInstance.Logger.LogError("Failed to set backup variable.");
 52                          }
 53                      }
 54  
 55                      if (!EnvironmentVariablesHelper.SetVariableWithoutNotify(variable))
 56                      {
 57                          LoggerInstance.Logger.LogError("Failed to set profile variable.");
 58                      }
 59                  }
 60  
 61                  EnvironmentVariablesHelper.NotifyEnvironmentChange();
 62              });
 63          }
 64  
 65          public Task UnApply()
 66          {
 67              return Task.Run(() =>
 68              {
 69                  foreach (var variable in Variables)
 70                  {
 71                      UnapplyVariable(variable);
 72                  }
 73  
 74                  EnvironmentVariablesHelper.NotifyEnvironmentChange();
 75              });
 76          }
 77  
 78          public void UnapplyVariable(Variable variable)
 79          {
 80              // Unset the variable
 81              if (!EnvironmentVariablesHelper.UnsetVariableWithoutNotify(variable))
 82              {
 83                  LoggerInstance.Logger.LogError("Failed to unset variable.");
 84              }
 85  
 86              var originalName = variable.Name;
 87              var backupName = EnvironmentVariablesHelper.GetBackupVariableName(variable, this.Name);
 88  
 89              // Get backup variable if it exist
 90              var backupVariable = EnvironmentVariablesHelper.GetExisting(backupName);
 91  
 92              if (backupVariable != null)
 93              {
 94                  var variableToRestore = new Variable(originalName, backupVariable.Values, backupVariable.ParentType);
 95  
 96                  if (!EnvironmentVariablesHelper.UnsetVariableWithoutNotify(backupVariable))
 97                  {
 98                      LoggerInstance.Logger.LogError("Failed to unset backup variable.");
 99                  }
100  
101                  if (!EnvironmentVariablesHelper.SetVariableWithoutNotify(variableToRestore))
102                  {
103                      LoggerInstance.Logger.LogError("Failed to restore backup variable.");
104                  }
105              }
106          }
107  
108          public bool IsCorrectlyApplied()
109          {
110              if (!IsEnabled)
111              {
112                  return false;
113              }
114  
115              foreach (var variable in Variables)
116              {
117                  var applied = EnvironmentVariablesHelper.GetExisting(variable.Name);
118                  if (applied != null && applied.Values == variable.Values && applied.ParentType == VariablesSetType.User)
119                  {
120                      continue;
121                  }
122  
123                  return false;
124              }
125  
126              return true;
127          }
128  
129          public bool IsApplicable()
130          {
131              foreach (var variable in Variables)
132              {
133                  if (!variable.Validate())
134                  {
135                      return false;
136                  }
137  
138                  // Get existing variable with the same name if it exist
139                  var variableToOverride = EnvironmentVariablesHelper.GetExisting(variable.Name);
140  
141                  // It exists. Backup is needed.
142                  if (variableToOverride != null && variableToOverride.ParentType == VariablesSetType.User)
143                  {
144                      variableToOverride.Name = EnvironmentVariablesHelper.GetBackupVariableName(variableToOverride, this.Name);
145                      if (!variableToOverride.Validate())
146                      {
147                          return false;
148                      }
149                  }
150              }
151  
152              return true;
153          }
154  
155          public ProfileVariablesSet Clone()
156          {
157              var clone = new ProfileVariablesSet(this.Id, this.Name);
158              clone.Variables = new ObservableCollection<Variable>(this.Variables);
159              clone.IsEnabled = this.IsEnabled;
160  
161              return clone;
162          }
163      }
164  }