-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
559 lines (480 loc) · 23.6 KB
/
MainWindow.xaml.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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
using System.Diagnostics;
using System.Management;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Text.Json.Serialization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using MaterialDesignColors;
using MaterialDesignThemes.Wpf;
namespace IDT2025
{
public partial class MainWindow : Window
{
private int toggleState = 0;
private const double SidebarWidth = 150;
private MainViewModel _viewModel;
private DispatcherTimer _vpnCheckTimer;
public string AppVersion { get; set; }
private readonly string currentVersion = "";
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
this.Icon = new BitmapImage(new Uri("pack://application:,,,/images/IDT.ico"));
currentVersion = GetFileVersion();
TitleVersionValue.Text = $" v{currentVersion}";
_viewModel = new MainViewModel();
DataContext = _viewModel;
// Set the default toggle state to case 0 on startup
SetInitialToggleState();
Loaded += MainWindow_Loaded; // Move this inside the constructor
// Initialize and start the VPN check timer
_vpnCheckTimer = new DispatcherTimer();
_vpnCheckTimer.Interval = TimeSpan.FromSeconds(10); // Check every 10 seconds
_vpnCheckTimer.Tick += VpnCheckTimer_Tick;
_vpnCheckTimer.Start();
LoadUserSettings();
LoadDashboard();
CheckForUpdatesAsync(); // Call CheckForUpdatesAsync when the window is loaded
StartUpdateCheck();
}
private string GetFileVersion()
{
var assembly = Assembly.GetExecutingAssembly();
var fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);
return fileVersionInfo.FileVersion;
}
private void StartUpdateCheck()
{
var updateTimer = new DispatcherTimer
{
Interval = TimeSpan.FromHours(1) // Check every hour
};
updateTimer.Tick += async (sender, e) => await CheckForUpdatesAsync();
updateTimer.Start();
}
private async Task CheckForUpdatesAsync()
{
// Debug.WriteLine("Starting CheckForUpdatesAsync...");
string updateUrl = "https://dev-documentation.wolterskluwerfs.com/IDT/update.json";
try
{
var handler = new HttpClientHandler
{
UseDefaultCredentials = true // Use the current user's Windows credentials
};
using HttpClient client = new HttpClient(handler);
// Debug.WriteLine("Sending HTTP GET request...");
HttpResponseMessage response = await client.GetAsync(updateUrl);
// Debug.WriteLine($"HTTP response received. Status code: {response.StatusCode}");
if (response.IsSuccessStatusCode)
{
string updateInfo = await response.Content.ReadAsStringAsync();
// Debug.WriteLine($"Update info received: {updateInfo}");
if (!string.IsNullOrEmpty(updateInfo))
{
try
{
// Debug.WriteLine("Attempting to deserialize JSON...");
var updateData = System.Text.Json.JsonSerializer.Deserialize<UpdateInfo>(updateInfo);
// Debug.WriteLine("JSON deserialization successful.");
if (updateData != null)
{
// Debug.WriteLine($"Latest version available: {updateData.LatestVersion}");
//MessageBox.Show($"Latest version available: {updateData.LatestVersion}", "Update Check", MessageBoxButton.OK, MessageBoxImage.Information);
if (IsNewVersionAvailable(updateData.LatestVersion))
{
// Debug.WriteLine("New version is available. Notifying user...");
NotifyUserOfUpdate(updateData);
}
else
{
// Debug.WriteLine("No new version available.");
}
}
else
{
// Debug.WriteLine("Deserialized update data is null.");
MessageBox.Show("Failed to parse update information.", "Update Check", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
catch (System.Text.Json.JsonException jsonEx)
{
// Debug.WriteLine($"JSON deserialization error: {jsonEx.Message}");
MessageBox.Show($"JSON deserialization error: {jsonEx.Message}", "Update Check", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
else
{
// Debug.WriteLine("Update information is empty.");
MessageBox.Show("Update information is empty.", "Update Check", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
else
{
// Debug.WriteLine($"Failed to check for updates. HTTP Status: {response.StatusCode} - {response.ReasonPhrase}");
MessageBox.Show($"Failed to check for updates. HTTP Status: {response.StatusCode} - {response.ReasonPhrase}. URL: {updateUrl}\n\nPlease check your VPN connection and try again.", "Update Check", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
catch (Exception ex)
{
// Debug.WriteLine($"Error checking for updates: {ex.Message}");
MessageBox.Show($"Error checking for updates: {ex.Message}. URL: {updateUrl}", "Update Check", MessageBoxButton.OK, MessageBoxImage.Error);
}
// Debug.WriteLine("CheckForUpdatesAsync completed.");
}
private bool IsNewVersionAvailable(string latestVersion)
{
Version current = new Version(currentVersion);
Version latest = new Version(latestVersion);
return latest > current;
}
private void NotifyUserOfUpdate(UpdateInfo updateData)
{
MessageBoxResult result = MessageBox.Show(
$"A new version ({updateData.LatestVersion}) is available. Would you like to download it?",
"Update Available",
MessageBoxButton.YesNo,
MessageBoxImage.Information);
if (result == MessageBoxResult.Yes)
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
{
FileName = updateData.DownloadUrl,
UseShellExecute = true
});
}
}
private void ApplyToggleState(int state)
{
var DashboardLabelCell = MainGrid.FindName("DashboardLabelCell") as Border;
var PubAssistLabelCell = MainGrid.FindName("PubAssistLabelCell") as Border;
var SingleFileLabelCell = MainGrid.FindName("SingleFileLabelCell") as Border;
var EditorLabelCell = MainGrid.FindName("EditorLabelCell") as Border;
var SettingsLabelCell = MainGrid.FindName("SettingsLabelCell") as Border;
var VpnLabelCell = MainGrid.FindName("VpnLabelCell") as Border;
switch (state)
{
case 0: // Sidebar open
if (DashboardLabelCell != null) DashboardLabelCell.Visibility = Visibility.Visible;
if (PubAssistLabelCell != null) PubAssistLabelCell.Visibility = Visibility.Visible;
if (SingleFileLabelCell != null) SingleFileLabelCell.Visibility = Visibility.Visible;
if (EditorLabelCell != null) EditorLabelCell.Visibility = Visibility.Visible;
if (SettingsLabelCell != null) SettingsLabelCell.Visibility = Visibility.Visible;
if (VpnLabelCell != null) VpnLabelCell.Visibility = Visibility.Visible;
DashboardIconCell.Visibility = Visibility.Visible;
PubAssistIconCell.Visibility = Visibility.Visible;
SingleFileIconCell.Visibility = Visibility.Visible;
EditorIconCell.Visibility = Visibility.Visible;
SettingsIconCell.Visibility = Visibility.Visible;
VpnIconCell.Visibility = Visibility.Visible;
TopNav.Visibility = Visibility.Collapsed;
// Adjust MainContent margin
MainContent.Margin = new Thickness(20, 0, 0, 0); // Adjust the left margin as needed
// Change ToggleNavButton Kind to ArrowLeftBox
if (ToggleNavButton != null)
{
ToggleNavButton.Kind = PackIconKind.ArrowLeftBox;
}
break;
case 1: // Sidebar partially closed
if (DashboardLabelCell != null) DashboardLabelCell.Visibility = Visibility.Collapsed;
if (PubAssistLabelCell != null) PubAssistLabelCell.Visibility = Visibility.Collapsed;
if (SingleFileLabelCell != null) SingleFileLabelCell.Visibility = Visibility.Collapsed;
if (EditorLabelCell != null) EditorLabelCell.Visibility = Visibility.Collapsed;
if (SettingsLabelCell != null) SettingsLabelCell.Visibility = Visibility.Collapsed;
if (VpnLabelCell != null) VpnLabelCell.Visibility = Visibility.Collapsed;
DashboardIconCell.Visibility = Visibility.Visible;
PubAssistIconCell.Visibility = Visibility.Visible;
SingleFileIconCell.Visibility = Visibility.Visible;
EditorIconCell.Visibility = Visibility.Visible;
SettingsIconCell.Visibility = Visibility.Visible;
VpnIconCell.Visibility = Visibility.Visible;
TopNav.Visibility = Visibility.Collapsed;
// Adjust MainContent margin
MainContent.Margin = new Thickness(-(SidebarWidth) + 20, 0, 0, 0); // Adjust the left margin as needed
break;
case 2: // Sidebar closed
if (DashboardLabelCell != null) DashboardLabelCell.Visibility = Visibility.Collapsed;
if (PubAssistLabelCell != null) PubAssistLabelCell.Visibility = Visibility.Collapsed;
if (SingleFileLabelCell != null) SingleFileLabelCell.Visibility = Visibility.Collapsed;
if (EditorLabelCell != null) EditorLabelCell.Visibility = Visibility.Collapsed;
if (SettingsLabelCell != null) SettingsLabelCell.Visibility = Visibility.Collapsed;
if (VpnLabelCell != null) VpnLabelCell.Visibility = Visibility.Collapsed;
DashboardIconCell.Visibility = Visibility.Collapsed;
PubAssistIconCell.Visibility = Visibility.Collapsed;
SingleFileIconCell.Visibility = Visibility.Collapsed;
EditorIconCell.Visibility = Visibility.Collapsed;
SettingsIconCell.Visibility = Visibility.Collapsed;
VpnIconCell.Visibility = Visibility.Collapsed;
TopNav.Visibility = Visibility.Visible;
// Adjust MainContent margin
MainContent.Margin = new Thickness(-200, 0, 0, 0); // Adjust the left margin as needed
// Change ToggleNavButton Kind to ArrowRightBox
if (ToggleNavButton != null)
{
ToggleNavButton.Kind = PackIconKind.ArrowRightBox;
}
break;
}
}
private void LoadUserSettings()
{
// Load settings
string userName = Properties.Settings.Default.UserName;
toggleState = Properties.Settings.Default.SidebarToggleState; // Load the sidebar toggle state
ApplyToggleState(toggleState); // Apply the loaded toggle state
}
private void SaveUserSettings()
{
// Modify settings
Properties.Settings.Default.UserName = "NewUserName";
Properties.Settings.Default.SidebarToggleState = toggleState; // Save the sidebar toggle state
// Save settings
Properties.Settings.Default.Save();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Save settings when the window is closing
SaveUserSettings();
}
private void LoadDashboard()
{
MainContent.Content = new Dashboard();
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
CheckVpnStatus(); // Call CheckVpnStatus when the window is loaded
}
private void VpnCheckTimer_Tick(object sender, EventArgs e)
{
CheckVpnStatus(); // Periodically check VPN status
}
private void CheckVpnStatus()
{
bool isConnected = false;
VpnIcon.ToolTip = "VPN Disconnected"; // Set the default tooltip
TopVpnIcon.ToolTip = "VPN Disconnected"; // Set the default tooltip
VpnIconCell.ToolTip = "VPN Disconnected"; // Set the default tooltip
// Use WMI to check VPN connection status
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID != NULL");
foreach (ManagementObject queryObj in searcher.Get())
{
var netConnectionId = queryObj["NetConnectionID"]?.ToString();
var netConnectionStatus = queryObj["NetConnectionStatus"]?.ToString();
var description = queryObj["Description"]?.ToString();
//Debug.WriteLine($"NetConnectionID: {netConnectionId}, NetConnectionStatus: {netConnectionStatus}, Description: {description}");
// Check for the specific VPN adapter description and ensure the status is connected
if (description != null &&
description.Equals("Cisco AnyConnect Virtual Miniport Adapter for Windows x64", StringComparison.OrdinalIgnoreCase) &&
netConnectionStatus == "2")
{
isConnected = true;
VpnIcon.ToolTip = "VPN Connected"; // Update the tooltip
TopVpnIcon.ToolTip = "VPN Connected"; // Update the tooltip
VpnIconCell.ToolTip = "VPN Connected"; // Update the tooltip
break;
}
}
//Debug.WriteLine($"VPN Connected: {isConnected}");
_viewModel.IsVpnConnected = isConnected;
// Update the VPN icon color based on the connection status
UpdateVpnIconColor(isConnected);
}
private void UpdateVpnIconColor(bool isConnected)
{
var vpnIcon = MainGrid.FindName("VpnIcon") as PackIcon;
var topVpnIcon = MainGrid.FindName("TopVpnIcon") as PackIcon;
if (vpnIcon != null)
{
vpnIcon.Foreground = isConnected ? new SolidColorBrush((Color)ColorConverter.ConvertFromString("#00cc00")) : Brushes.Red;
}
if (topVpnIcon != null)
{
topVpnIcon.Foreground = isConnected ? new SolidColorBrush((Color)ColorConverter.ConvertFromString("#00cc00")) : Brushes.Red;
}
}
private void SetInitialToggleState()
{
var DashboardLabelCell = MainGrid.FindName("DashboardLabelCell") as Border;
var PubAssistLabelCell = MainGrid.FindName("PubAssistLabelCell") as Border;
var SingleFileLabelCell = MainGrid.FindName("SingleFileLabelCell") as Border;
var EditorLabelCell = MainGrid.FindName("EditorLabelCell") as Border;
var SettingsLabelCell = MainGrid.FindName("SettingsLabelCell") as Border;
var VpnLabelCell = MainGrid.FindName("VpnLabelCell") as Border;
if (DashboardLabelCell != null) DashboardLabelCell.Visibility = Visibility.Visible;
if (PubAssistLabelCell != null) PubAssistLabelCell.Visibility = Visibility.Visible;
if (SingleFileLabelCell != null) SingleFileLabelCell.Visibility = Visibility.Visible;
if (EditorLabelCell != null) EditorLabelCell.Visibility = Visibility.Visible;
if (SettingsLabelCell != null) SettingsLabelCell.Visibility = Visibility.Visible;
if (VpnLabelCell != null) VpnLabelCell.Visibility = Visibility.Visible;
DashboardIconCell.Visibility = Visibility.Visible;
PubAssistIconCell.Visibility = Visibility.Visible;
SingleFileIconCell.Visibility = Visibility.Visible;
EditorIconCell.Visibility = Visibility.Visible;
SettingsIconCell.Visibility = Visibility.Visible;
VpnIconCell.Visibility = Visibility.Visible;
TopNav.Visibility = Visibility.Collapsed;
// Adjust MainContent margin
MainContent.Margin = new Thickness(20, 0, 0, 0); // Adjust the left margin as needed
}
private void NavToggleButton_Click(object sender, RoutedEventArgs e)
{
toggleState = (toggleState + 1) % 3;
//Debug.WriteLine($"Toggle state changed to {toggleState}");
ApplyToggleState(toggleState);
SaveUserSettings(); // Save the toggle state when it changes
}
private void DashboardIconCell_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MainContent.Content = new Dashboard();
}
private void PubAssistIconCell_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MainContent.Content = new PubAssist();
}
private void TopNavDashboardButton_Click(object sender, RoutedEventArgs e)
{
DashboardIconCell_MouseLeftButtonDown(DashboardIconCell, new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) { RoutedEvent = UIElement.MouseLeftButtonDownEvent });
}
private void TopNavPubAssistButton_Click(object sender, RoutedEventArgs e)
{
PubAssistIconCell_MouseLeftButtonDown(PubAssistIconCell, new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) { RoutedEvent = UIElement.MouseLeftButtonDownEvent });
}
private void SetNavLabelCellVisibility(Visibility visibility)
{
var navLabelCellStyle = FindResource("NavLabelCell") as Style;
if (navLabelCellStyle == null)
{
MessageBox.Show("NavLabelCell style not found.");
return;
}
foreach (var element in MainGrid.Children)
{
if (element is Border border)
{
//Debug.WriteLine($"Border Name: {border.Name}, Style: {border.Style}");
if (border.Style == navLabelCellStyle)
{
border.Visibility = visibility;
//Debug.WriteLine($"Set visibility of {border.Name} to {visibility}");
}
}
}
}
private void SetNavIconCellVisibility(Visibility visibility)
{
var navIconCellStyle = FindResource("NavIconCell") as Style;
if (navIconCellStyle == null)
{
MessageBox.Show("NavIconCell style not found.");
return;
}
foreach (var element in MainGrid.Children)
{
if (element is Border border)
{
//Debug.WriteLine($"Border Name: {border.Name}, Style: {border.Style}");
if (border.Style == navIconCellStyle)
{
border.Visibility = visibility;
//Debug.WriteLine($"Set visibility of {border.Name} to {visibility}");
}
}
}
}
private void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
ToggleWindowState();
}
else
{
DragMove();
}
}
private void ToggleWindowState()
{
if (this.WindowState == WindowState.Maximized)
{
this.WindowState = WindowState.Normal;
}
else
{
this.WindowState = WindowState.Maximized;
}
}
private void HelpButton_Click(object sender, RoutedEventArgs e)
{
if (HelpButton.ContextMenu != null)
{
HelpButton.ContextMenu.PlacementTarget = HelpButton;
HelpButton.ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
HelpButton.ContextMenu.IsOpen = true;
}
}
private void ViewHelp_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("View Help clicked.");
}
private void ReleaseNotes_Click(object sender, RoutedEventArgs e)
{
string url = "https://dev-documentation.wolterskluwerfs.com/IDT/ReleaseNotes/index.html";
Process.Start(new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
});
}
private void About_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("About IDT2025 clicked.");
}
private void MinimizeButton_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
private void MaximizeButton_Click(object sender, RoutedEventArgs e)
{
ToggleWindowState();
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void Window_StateChanged(object sender, EventArgs e)
{
// Handle state change if needed
}
private void SetDashboardLabelCellVisibility(Visibility visibility)
{
var dashboardLabelCell = MainGrid.FindName("DashboardLabelCell") as Border;
if (dashboardLabelCell != null)
{
dashboardLabelCell.Visibility = visibility;
//Debug.WriteLine($"Set visibility of DashboardLabelCell to {visibility}");
}
else
{
MessageBox.Show("DashboardLabelCell not found.");
}
}
}
class UpdateInfo
{
[JsonPropertyName("latestVersion")]
public string LatestVersion { get; set; }
[JsonPropertyName("downloadUrl")]
public string DownloadUrl { get; set; }
[JsonPropertyName("releaseNotes")]
public string ReleaseNotes { get; set; }
}
}