-
Notifications
You must be signed in to change notification settings - Fork 15
/
UserSettings.cs
543 lines (474 loc) · 17 KB
/
UserSettings.cs
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*
* Copyright (c) 2022 Johnny Shaw. All rights reserved.
*/
using System;
using System.IO;
using System.Windows;
using System.Text.Json;
using System.Threading;
using System.Diagnostics;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text.Json.Serialization;
namespace VMPlex
{
/// <summary>
/// Root of the user settings. Configured via vmplex-settings.json.
/// </summary>
public class Settings
{
[JsonInclude, JsonPropertyOrder(-1), JsonPropertyName("$schema")]
public string _schema = "https://raw.githubusercontent.com/0xf005ba11/vmplex-ws/main/VMPlex/UserSettingsSchema.json";
/// <summary>
/// If true portions of the interface are styled in a compact style.
/// </summary>
[JsonInclude]
public bool CompactMode { get; set; } = false;
/// <summary>
/// Optionally sets the font size for certain elements in the UI.
/// </summary>
[JsonInclude]
public double FontSize { get; set; } = 14;
/// <summary>
/// When starting VMPlex will remember and reopen previously opened tabs.
/// </summary>
[JsonInclude]
public bool RememberTabs { get; set; } = true;
/// <summary>
/// When true certain tool bar actions will prompt for confirmation
/// before executing. Like rebooting, shutting down, resetting virtual
/// machines.
/// </summary>
[JsonInclude]
public bool ConfirmToolBarActions { get; set; } = true;
/// <summary>
/// Defines the debugger to use when launching one for a given virtual
/// machine. This is supplied as the file name starting the debugger
/// process.
/// </summary>
[JsonInclude]
public string Debugger { get; set; } = "windbgx";
/// <summary>
/// A list of virtual machines. VMPlex will populate this with known
/// virtual machines on the user's behalf.
/// </summary>
[JsonInclude]
public List<VmConfig> VirtualMachines { get; set; } = new List<VmConfig>();
/// <summary>
/// List of remote desktop connection settings for connecting to machines
/// that aren't Hyper-V managed virtual machines.
/// </summary>
[JsonInclude]
public List<RdpSettings> RdpConnections { get; set; } = new List<RdpSettings>();
/// <summary>
/// Window settings, generally users don't need to edit this. Used to
/// persist state of the window.
/// </summary>
[JsonInclude]
public WindowSettings MainWindow { get; set; } = new WindowSettings();
/// <summary>
/// Manager page tab widths, generally users don't need to edit this.
/// Used to persist state of the manager page tab widths.
/// </summary>
[JsonInclude]
public double[] ManagerTabWidths { get; set; } = new double[]
{
170,
75,
148,
150,
83,
94,
300
};
}
/// <summary>
/// User settings for a given virtual machine.
/// </summary>
public class VmConfig
{
/// <summary>
/// The GUID of the virtual machine as reported by Hyper-V. VMPlex will
/// populate this on the user's behalf.
/// </summary>
[JsonInclude]
public string Guid { get; set; } = "";
/// <summary>
/// The friendly name of the virtual machine as reported by Hyper-V.
/// VMPlex will populate this on this user's behalf.
/// </summary>
[JsonInclude]
public string Name { get; set; } = "";
/// <summary>
/// Arguments passed to the debugger when launching one for this
/// virtual machine. As an example, when using windbg and debugging
/// the target virtual machine over the network this would be in a
/// form similar to "-k net:port=50000,key=1.2.3.4 -T WIN11X64".
/// References the documentation for your debugger.
/// </summary>
[JsonInclude]
public string DebuggerArguments { get; set; } = "";
/// <summary>
/// Values for the virtual machine tab open action.
/// </summary>
public enum TabActionOpen
{
/// <summary>
/// No action is taken when opening the virtual machine tab.
/// </summary>
None = 0,
/// <summary>
/// Opening a virtual machine tab resumes the virtual machine.
/// </summary>
Resume = 1,
}
/// <summary>
/// Specifies the action to take when opening the virtual machine tab.
/// </summary>
[JsonInclude]
public TabActionOpen OnTabOpen { get; set; } = TabActionOpen.None;
/// <summary>
/// Values for the virtual machine tab close action.
/// </summary>
public enum TabActionClose
{
/// <summary>
/// No action is taken when closing the virtual machine tab.
/// </summary>
None = 0,
/// <summary>
/// Closing the virtual machine tab pauses the virtual machine.
/// </summary>
Pause = 1,
/// <summary>
/// Closing the virtual machine tab saves the virtual machine.
/// </summary>
Save = 2,
}
/// <summary>
/// Specifies the action to take when closing the virtual machine tab.
/// </summary>
public TabActionClose OnTabClose { get; set; } = TabActionClose.None;
/// <summary>
/// Values for the virtual machine action when VMPlex starts.
/// </summary>
public enum ApplicationActionStart
{
/// <summary>
/// No action is taken when starting VMPlex.
/// </summary>
None = 0,
/// <summary>
/// When starting VMPlex the virtual machine is resumed.
/// </summary>
Resume = 1,
}
/// <summary>
/// Specifies the action to take when starting VMPlex.
/// </summary>
public ApplicationActionStart OnApplicationStart { get; set; } = ApplicationActionStart.None;
/// <summary>
/// Values for the virtual machine action when VMPlex exits.
/// </summary>
public enum ApplicationActionExit
{
/// <summary>
/// No action is taken when exiting VMPlex.
/// </summary>
None = 0,
/// <summary>
/// When exiting VMPlex the virtual machine is paused.
/// </summary>
Pause = 1,
/// <summary>
/// When exiting VMPlex the virtual machine is saved.
/// </summary>
Save = 2,
}
/// <summary>
/// Specifies the action to take when exiting VMPlex.
/// </summary>
public ApplicationActionExit OnApplicationExit { get; set; } = ApplicationActionExit.None;
/// <summary>
/// Indicates if the virtual machine tab is open and at what index.
/// Used in conjunction with RememberTabs.
/// </summary>
[JsonInclude]
public int TabIndex { get; set; } = -1;
/// <summary>
/// Optional RDP settings used when connecting to this virtual machine.
/// </summary>
[JsonInclude]
public RdpSettings RdpSettings { get; set; } = new RdpSettings();
}
/// <summary>
/// User settings for a RDP connections.
/// </summary>
public class RdpSettings
{
/// <summary>
/// Domain for the RDP connection.
/// </summary>
[JsonInclude]
public string Domain { get; set; } = "";
/// <summary>
/// Server for the RDP connection.
/// </summary>
[JsonInclude]
public string Server { get; set; } = "localhost";
/// <summary>
/// The default enhanced session state when connecting.
/// </summary>
[JsonInclude]
public bool DefaultEnhancedSession { get; set; } = true;
/// <summary>
/// Specifies if redirection of the clipboard is allowed.
/// </summary>
[JsonInclude]
public bool RedirectClipboard { get; set; } = true;
/// <summary>
/// Values for the audio redirection mode.
/// </summary>
public enum AudioRedirectionModeSetting
{
/// <summary>
/// Audio redirection is enabled and the option for redirection is
/// "Bring to this computer". This is the default mode.
/// </summary>
Redirect = 0,
/// <summary>
/// Audio redirection is enabled and the option is "Leave at
/// remote computer". The "Leave at remote computer" option is
/// supported only when connecting remotely to a host computer
/// that is running Windows Vista. If the connection is to a host
/// computer that is running Windows Server 2008, the option
/// "Leave at remote computer" is changed to "Do not play".
/// </summary>
PlayOnServer = 1,
/// <summary>
/// Audio redirection is enabled and the mode is "Do not play".
/// </summary>
None = 2
}
/// <summary>
/// Sets different values for the audio redirection mode.
/// </summary>
[JsonInclude]
public AudioRedirectionModeSetting AudioRedirectionMode { get; set; } = AudioRedirectionModeSetting.Redirect;
/// <summary>
/// Specifies if the default audio input device is captured.
/// </summary>
[JsonInclude]
public bool AudioCaptureRedirectionMode { get; set; } = false;
/// <summary>
/// Specifies if redirection of disk drives is allowed.
/// </summary>
[JsonInclude]
public bool RedirectDrives { get; set; } = false;
/// <summary>
/// Specifies if redirection of devices is allowed.
/// This defaults to false.
/// </summary>
[JsonInclude]
public bool RedirectDevices { get; set; } = false;
/// <summary>
/// Specifies if redirection of smart cards is allowed.
/// </summary>
[JsonInclude]
public bool RedirectSmartCards { get; set; } = false;
/// <summary>
/// Specifies the initial remote desktop width, in pixels.
/// </summary>
[JsonInclude]
public int DesktopWidth { get; set; } = 1024;
/// <summary>
/// Specifies the initial remote desktop height, in pixels.
/// </summary>
[JsonInclude]
public int DesktopHeight { get; set; } = 768;
[JsonIgnore]
public string DisplayText
{
get { return Domain.Length > 0 ? $"{Domain}\\{Server}" : Server; }
}
}
/// <summary>
/// Window settings.
/// </summary>
public class WindowSettings
{
[JsonInclude]
public double Width { get; set; } = 1200;
[JsonInclude]
public double Height { get; set; } = 900;
[JsonInclude]
public double Top { get; set; } = -1;
[JsonInclude]
public double Left { get; set; } = -1;
[JsonInclude]
public WindowState State { get; set; } = WindowState.Normal;
}
public class UserSettings : INotifyPropertyChanged
{
public Settings Settings { get { lock (Lock) { return ActiveSettings; } } }
public string UserSettingsFile { get; private set; } = Path.GetFullPath("vmplex-settings.json");
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyChange(string Name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Name));
}
public static UserSettings Instance
{
get
{
if (_instance is null)
{
lock (_instanceLock)
{
if (_instance is null)
{
_instance = new UserSettings();
}
}
}
return _instance;
}
}
public UserSettings()
{
SettingsFileWatcher = new FileSystemWatcher()
{
Path = Path.GetDirectoryName(UserSettingsFile),
Filter = Path.GetFileName(UserSettingsFile),
EnableRaisingEvents = true
};
SettingsFileWatcher.Changed += OnChanged;
SettingsFileWatcher.Deleted += OnChanged;
SettingsFileWatcher.Created += OnChanged;
SettingsFileWatcher.Renamed += OnRenamed;
}
public void OpenInEditor()
{
var process = new Process();
process.StartInfo = new ProcessStartInfo()
{
FileName = UserSettingsFile,
UseShellExecute = true
};
try
{
process.Start();
}
catch (Exception exc)
{
UI.MessageBox.Show(
MessageBoxImage.Error,
"VMPlex Settings Error",
$"Failed to open settings file \"{UserSettingsFile}\"\n{exc.Message}");
}
}
public Settings Mutate(Func<Settings, Settings> Mutator)
{
Settings settings;
lock (Lock)
{
settings = Mutator(ActiveSettings);
var json = JsonSerializer.Serialize(settings, JsonSerializeOpts);
File.WriteAllText(UserSettingsFile, json);
}
NotifyChange();
return settings;
}
public void Load()
{
lock (Lock)
{
if (!File.Exists(UserSettingsFile))
{
ActiveSettings = new Settings();
}
else
{
var json = File.ReadAllText(UserSettingsFile);
ActiveSettings = JsonSerializer.Deserialize<Settings>(json, JsonSerializeOpts);
ActiveSettings._schema = (new Settings())._schema;
}
}
}
public void Save()
{
lock (Lock)
{
var json = JsonSerializer.Serialize(ActiveSettings, JsonSerializeOpts);
File.WriteAllText(UserSettingsFile, json);
}
}
private void OnChanged(object sender, FileSystemEventArgs e)
{
ReloadOnChange();
}
private void OnRenamed(object sender, RenamedEventArgs e)
{
ReloadOnChange();
}
private void ReloadOnChange()
{
//
// This is disgusting... but the editor could be contending with
// us trying to reload settings... keep trying for a short period.
//
Exception exception = null;
for (int i = 0; i < 5; i++, Thread.Sleep(100))
{
try
{
Load();
exception = null;
break;
}
catch (Exception exc)
{
exception = exc;
continue;
}
}
if (exception == null)
{
NotifyChange();
}
else
{
//
// Don't nag too often since the change notification can fire
// back-to-back depending on the file writing method and
// changes. Also, we don't have access to the modern WFP here
// so show an old school message box.
//
if ((DateTime.Now - LastReloadErrorTime).Seconds > 3)
{
UI.MessageBox.Show(
MessageBoxImage.Error,
"VMPlex Settings Error",
$"Failed to load settings file \"{UserSettingsFile}\"\n{exception.Message}");
LastReloadErrorTime = DateTime.Now;
}
}
}
private static UserSettings _instance;
private static object _instanceLock = new object();
private object Lock = new object();
private Settings ActiveSettings = new Settings();
private FileSystemWatcher SettingsFileWatcher;
private JsonSerializerOptions JsonSerializeOpts = new JsonSerializerOptions
{
WriteIndented = true,
IncludeFields = true,
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals,
Converters =
{
new JsonStringEnumConverter()
}
};
private DateTime LastReloadErrorTime = DateTime.Now;
}
}