-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindowViewModel.cs
324 lines (265 loc) · 7.82 KB
/
MainWindowViewModel.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
using CommunityToolkit.Diagnostics;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Management.Infrastructure;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.IO.IsolatedStorage;
using System.Security;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Xceed.Wpf.Toolkit;
namespace HyperVPeek
{
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public partial class MainWindowViewModel : ObservableObject
{
#region Settings
[ObservableProperty]
[property: JsonProperty]
private string _targetHostname = string.Empty;
[ObservableProperty]
[property: JsonProperty]
private string _targetDomain = string.Empty;
[ObservableProperty]
[property: JsonProperty]
private string _username = string.Empty;
[ObservableProperty]
[property: JsonProperty]
private bool _autoRefresh = true;
#endregion Settings
#region State
[ObservableProperty]
private string _status = "Disconnected";
[ObservableProperty]
private bool _isDisconnected = true;
[ObservableProperty]
private bool _isConnected = false;
[ObservableProperty]
private bool _exceededMaxEnvelopeSize = false;
[ObservableProperty]
private ObservableCollection<string> _virtualMachines = new();
[ObservableProperty]
private BitmapSource? _lastVirtualMachineImage;
[ObservableProperty]
private string _selectedVirtualMachine = string.Empty;
[ObservableProperty]
private double _virtualMachinePreviewWidth = 0d;
[ObservableProperty]
private double _virtualMachinePreviewHeight = 0d;
[ObservableProperty]
private Transform _virtualMachineRenderTransform = Transform.Identity;
private readonly RemoteHyperVModel _model = new();
#endregion State
private const string ERROR_WSMAN_MAX_ENVELOPE_SIZE_EXCEEDED = "0x80338048";
private const string SettingsFileName = "settings.conf";
private readonly JsonSerializerSettings _jsonSettings = new()
{
Formatting = Formatting.Indented,
DefaultValueHandling = DefaultValueHandling.Include
};
private static readonly IsolatedStorageFile IsoStore =
IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
[RelayCommand]
private void Connect(WatermarkPasswordBox pwBox)
{
if (!IsDisconnected)
{
return;
}
Status = $"Connecting...";
try
{
bool didConnect = _model.Connect(TargetDomain, TargetHostname, Username, pwBox.SecurePassword);
IsConnected = _model.ConnectionState == ConnectionState.Connected;
IsDisconnected = _model.ConnectionState == ConnectionState.Disconnected;
ExceededMaxEnvelopeSize = false;
}
catch (Exception ex)
{
Status = $"Failed to connect: {ex.Message}";
return;
}
Status = $"Connected";
}
[RelayCommand]
private void Disconnect()
{
if (!IsConnected)
{
return;
}
Status = $"Disconnecting...";
bool didDisconnect = _model.Disconnect();
IsConnected = _model.ConnectionState == ConnectionState.Connected;
IsDisconnected = _model.ConnectionState == ConnectionState.Disconnected;
ExceededMaxEnvelopeSize = false;
Status = $"Disconnected";
}
[RelayCommand]
private void UpdateVirtualMachineList()
{
Status = $"Getting virtual machine list...";
VirtualMachines.Clear();
IEnumerable<string> machines = GetVirtualMachineList();
foreach (string machine in machines)
{
VirtualMachines.Add(machine);
}
Status = $"Got virtual machine list ({VirtualMachines.Count})";
}
public IEnumerable<string> GetVirtualMachineList()
{
if (!IsConnected)
{
return Array.Empty<string>();
}
try
{
return _model.GetVirtualMachineList();
}
catch (RemoteHyperVException rhvex)
{
Status = rhvex.Message;
}
catch (CimException cex)
{
Status = $"Cim Error: {cex.Message}";
}
return Array.Empty<string>();
}
[RelayCommand]
private void RefreshVirtualMachineImage()
{
if (VirtualMachinePreviewWidth == 0 ||
VirtualMachinePreviewHeight == 0)
{
return;
}
LastVirtualMachineImage = GetVirtualMachinePreview(
SelectedVirtualMachine,
VirtualMachinePreviewWidth,
VirtualMachinePreviewHeight);
}
public BitmapSource? GetVirtualMachinePreview(string systemName, double imageWidth, double imageHeight)
{
if (!IsConnected)
{
Status = $"Can't get image for {systemName}, not connected";
return null;
}
if (string.IsNullOrEmpty(systemName))
{
Status = "Please select a VM";
return null;
}
if (!SelectedVirtualMachine.Equals(systemName, StringComparison.InvariantCultureIgnoreCase))
{
SelectedVirtualMachine = systemName;
}
VirtualMachinePreviewWidth = imageWidth;
VirtualMachinePreviewHeight = imageHeight;
Status = $"Getting image for {systemName}";
ushort width = (ushort)imageWidth;
ushort height = (ushort)imageHeight;
try
{
byte[] imageData = _model.GetVirtualMachinePreview(systemName, width, height);
if (imageData.Length > 0)
{
ExceededMaxEnvelopeSize = false;
}
double dpiX = 96d;
double dpiY = 96d;
PixelFormat pixelFormat = PixelFormats.Bgr565;
int stride = ((width * pixelFormat.BitsPerPixel) + 7) / 8;
Status = $"{systemName}: last updated {DateTimeOffset.Now}";
return BitmapSource.Create(width, height, dpiX, dpiY, pixelFormat, null, imageData, stride);
}
catch (RemoteHyperVException rhvex)
{
Status = rhvex.Message;
}
catch (CimException cex)
{
if (cex.MessageId.Contains(ERROR_WSMAN_MAX_ENVELOPE_SIZE_EXCEEDED))
{
ExceededMaxEnvelopeSize = true;
Status = "Error: message envelope size exceeded the maximum allowed value";
}
else
{
Status = $"Cim Error: {cex.Message}";
}
}
return null;
}
[RelayCommand]
private void SetMaxEnvelopeSize(uint sizeKilobytes)
{
Guard.IsGreaterThan(sizeKilobytes, 0);
if (!IsConnected)
{
Status = $"Can't send SetmaxEnvelopeSize command, not connected";
return;
}
ExceededMaxEnvelopeSize = false;
bool sent = false;
try
{
sent = _model.SetMaxEnvelopeSize(sizeKilobytes);
Status = sent
? "Sent SetmaxEnvelopeSize command"
: "Failed to send SetmaxEnvelopeSize command";
}
catch (RemoteHyperVException rhvex)
{
Status = rhvex.Message;
}
catch (CimException cex)
{
Status = $"Cim Error: {cex.Message}";
}
}
[RelayCommand]
private void LoadSettings()
{
if (!IsoStore.FileExists(SettingsFileName))
{
return;
}
try
{
using IsolatedStorageFileStream stream = IsoStore.OpenFile(SettingsFileName, FileMode.Open, FileAccess.Read);
using StreamReader reader = new(stream);
using JsonTextReader jsonReader = new(reader);
var serializer = JsonSerializer.Create(_jsonSettings);
serializer.Populate(jsonReader, this);
}
catch (Exception ex)
{
Status = $"Unable to load settings: {ex.Message}";
}
}
[RelayCommand]
private void SaveSettings()
{
try
{
using IsolatedStorageFileStream stream = IsoStore.OpenFile(SettingsFileName, FileMode.Create, FileAccess.Write);
using StreamWriter writer = new(stream);
string serializedSettings = JsonConvert.SerializeObject(this, _jsonSettings);
writer.Write(serializedSettings);
}
catch (Exception ex)
{
Status = $"Unable to save settings: {ex.Message}";
}
}
partial void OnSelectedVirtualMachineChanged(string value) => RefreshVirtualMachineImage();
}
}