-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Expand file tree
/
Copy pathHostsViewModel.cs
More file actions
279 lines (245 loc) · 9.04 KB
/
HostsViewModel.cs
File metadata and controls
279 lines (245 loc) · 9.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using global::PowerToys.GPOWrapper;
using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
using Microsoft.PowerToys.Settings.UI.Library.ViewModels.Commands;
using PowerToys.Interop;
using Settings.UI.Library.Enumerations;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public partial class HostsViewModel : Observable
{
private bool _isElevated;
private GpoRuleConfigured _enabledGpoRuleConfiguration;
private bool _enabledStateIsGPOConfigured;
private bool _isEnabled;
private SettingsUtils SettingsUtils { get; set; }
private GeneralSettings GeneralSettingsConfig { get; set; }
private HostsSettings Settings { get; set; }
private Func<string, int> SendConfigMSG { get; }
public ButtonClickCommand LaunchEventHandler => new ButtonClickCommand(Launch);
public ButtonClickCommand SelectBackupPathEventHandler => new ButtonClickCommand(SelectBackupPath);
public bool IsEnabled
{
get => _isEnabled;
set
{
if (_enabledStateIsGPOConfigured)
{
// If it's GPO configured, shouldn't be able to change this state.
return;
}
if (value != _isEnabled)
{
_isEnabled = value;
// Set the status in the general settings configuration
GeneralSettingsConfig.Enabled.Hosts = value;
OutGoingGeneralSettings snd = new OutGoingGeneralSettings(GeneralSettingsConfig);
SendConfigMSG(snd.ToString());
OnPropertyChanged(nameof(IsEnabled));
}
}
}
public bool IsEnabledGpoConfigured
{
get => _enabledStateIsGPOConfigured;
}
public bool LaunchAdministratorEnabled => IsEnabled && !_isElevated;
public bool ShowStartupWarning
{
get => Settings.Properties.ShowStartupWarning;
set
{
if (value != Settings.Properties.ShowStartupWarning)
{
Settings.Properties.ShowStartupWarning = value;
NotifyPropertyChanged();
}
}
}
public bool LoopbackDuplicates
{
get => Settings.Properties.LoopbackDuplicates;
set
{
if (value != Settings.Properties.LoopbackDuplicates)
{
Settings.Properties.LoopbackDuplicates = value;
NotifyPropertyChanged();
}
}
}
public bool LaunchAdministrator
{
get => Settings.Properties.LaunchAdministrator;
set
{
if (value != Settings.Properties.LaunchAdministrator)
{
Settings.Properties.LaunchAdministrator = value;
NotifyPropertyChanged();
}
}
}
public bool NoLeadingSpaces
{
get => Settings.Properties.NoLeadingSpaces;
set
{
if (value != Settings.Properties.NoLeadingSpaces)
{
Settings.Properties.NoLeadingSpaces = value;
NotifyPropertyChanged();
}
}
}
public int AdditionalLinesPosition
{
get => (int)Settings.Properties.AdditionalLinesPosition;
set
{
if (value != (int)Settings.Properties.AdditionalLinesPosition)
{
Settings.Properties.AdditionalLinesPosition = (HostsAdditionalLinesPosition)value;
NotifyPropertyChanged();
}
}
}
public int Encoding
{
get => (int)Settings.Properties.Encoding;
set
{
if (value != (int)Settings.Properties.Encoding)
{
Settings.Properties.Encoding = (HostsEncoding)value;
NotifyPropertyChanged();
}
}
}
public bool BackupHosts
{
get => Settings.Properties.BackupHosts;
set
{
if (value != Settings.Properties.BackupHosts)
{
Settings.Properties.BackupHosts = value;
NotifyPropertyChanged();
}
}
}
public string BackupPath
{
get => Settings.Properties.BackupPath;
set
{
if (value != Settings.Properties.BackupPath)
{
Settings.Properties.BackupPath = value;
NotifyPropertyChanged();
}
}
}
public int DeleteBackupsMode
{
get => (int)Settings.Properties.DeleteBackupsMode;
set
{
if (value != (int)Settings.Properties.DeleteBackupsMode)
{
Settings.Properties.DeleteBackupsMode = (HostsDeleteBackupMode)value;
NotifyPropertyChanged();
OnPropertyChanged(nameof(MinimumBackupsCount));
}
}
}
public int DeleteBackupsDays
{
get => Settings.Properties.DeleteBackupsDays;
set
{
if (value != Settings.Properties.DeleteBackupsDays)
{
Settings.Properties.DeleteBackupsDays = value;
NotifyPropertyChanged();
}
}
}
public int DeleteBackupsCount
{
get => Settings.Properties.DeleteBackupsCount;
set
{
if (value != Settings.Properties.DeleteBackupsCount)
{
Settings.Properties.DeleteBackupsCount = value;
NotifyPropertyChanged();
}
}
}
public int MinimumBackupsCount => DeleteBackupsMode == 1 ? 1 : 0;
public HostsViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<HostsSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc, bool isElevated)
{
SettingsUtils = settingsUtils;
GeneralSettingsConfig = settingsRepository.SettingsConfig;
Settings = moduleSettingsRepository.SettingsConfig;
SendConfigMSG = ipcMSGCallBackFunc;
_isElevated = isElevated;
InitializeEnabledValue();
}
private void InitializeEnabledValue()
{
_enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredHostsFileEditorEnabledValue();
if (_enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled)
{
// Get the enabled state from GPO.
_enabledStateIsGPOConfigured = true;
_isEnabled = _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled;
}
else
{
_isEnabled = GeneralSettingsConfig.Enabled.Hosts;
}
}
public void Launch()
{
string eventName = !_isElevated && LaunchAdministrator
? Constants.ShowHostsAdminSharedEvent()
: Constants.ShowHostsSharedEvent();
using (var eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, eventName))
{
eventHandle.Set();
}
}
public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
OnPropertyChanged(propertyName);
SettingsUtils.SaveSettings(Settings.ToJsonString(), HostsSettings.ModuleName);
}
public void RefreshEnabledState()
{
InitializeEnabledValue();
OnPropertyChanged(nameof(IsEnabled));
}
public void SelectBackupPath()
{
// This function was changed to use the shell32 API to open folder dialog
// as the old one (PickSingleFolderAsync) can't work when the process is elevated
// TODO: go back PickSingleFolderAsync when it's fixed
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(App.GetSettingsWindow());
var result = ShellGetFolder.GetFolderDialog(hwnd);
if (!string.IsNullOrEmpty(result))
{
BackupPath = result;
}
}
}
}