-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
770 lines (697 loc) · 33.2 KB
/
MainForm.cs
File metadata and controls
770 lines (697 loc) · 33.2 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
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
using Microsoft.Win32;
using MultiDisplayVCPServer.Properties;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MultiDisplayVCPServer
{
/// <summary>
/// The main window for the server application.
/// Handles user configuration, system tray icon, and server lifecycle events.
/// </summary>
public partial class MainForm : Form
{
// --- FIX: (IDE0090) 'new' expression can be simplified ---
private static readonly HttpClient httpClient = new();
private const string GitHubApiUrl = "https://api.github.com/repos/dog199200/MultiDisplayVCPServer/releases/latest";
/// <summary>
/// A set of common network ports that the server is forbidden from using
/// to prevent conflicts with standard services (e.g., HTTP, FTP, RDP).
/// </summary>
// --- FIX: (IDE0028 & IDE0090) Collection initialization can be simplified ---
private static readonly HashSet<int> forbiddenPorts = new()
{
20, 21, // FTP
22, // SSH
23, // Telnet
25, // SMTP
53, // DNS
80, // HTTP
110, // POP3
143, // IMAP
443, // HTTPS
3306, // MySQL
3389, // RDP
5432, // PostgreSQL
5900, // VNC
8080, // HTTP-alt
8443 // HTTPS-alt
};
/// <summary>
/// Initializes the form and its components.
/// </summary>
public MainForm()
{
Debug.WriteLine("MainForm() constructor started.");
try
{
Debug.WriteLine("Attempting settings upgrade...");
Settings.Default.Upgrade();
}
catch (Exception ex)
{
Debug.WriteLine($"Settings upgrade error: {ex.Message}");
}
InitializeComponent();
Debug.WriteLine("Components initialized.");
// Wire up event handlers for UI controls
// --- FIX: (IDE1006) Naming rule violation ---
portTextBox.Leave += PortTextBox_Leave;
passwordTextBox.Leave += PasswordTextBox_Leave;
runOnStartupCheckBox.CheckedChanged += RunOnStartupCheckBox_CheckedChanged;
minimizeToTrayCheckBox.CheckedChanged += MinimizeToTrayCheckBox_CheckedChanged;
this.showPasswordCheckBox.CheckedChanged += ShowPasswordCheckBox_CheckedChanged;
Debug.WriteLine("Event handlers wired up.");
Debug.WriteLine("MainForm() constructor finished.");
}
/// <summary>
/// Event handler for server state changes (e.g., Running, Stopped, Restarting).
/// Safely updates the UI from the correct thread.
/// </summary>
private void OnServerStateChanged(object? sender, int newState)
{
Debug.WriteLine($"OnServerStateChanged() called. New state: {newState}");
if (this.InvokeRequired)
{
Debug.WriteLine("Invoke required, dispatching to UI thread.");
this.BeginInvoke(new Action(() => UpdateUIForState(newState)));
}
else
{
Debug.WriteLine("Running on UI thread, calling UpdateUIForState directly.");
UpdateUIForState(newState);
}
}
/// <summary>
/// Updates the UI elements based on the server's state.
/// Disables configuration fields when the server is restarting.
/// </summary>
/// <param name="state">The new server state (0=Stopped, 1=Running, 2=Restarting).</param>
private void UpdateUIForState(int state)
{
Debug.WriteLine($"UpdateUIForState() called with state: {state}");
bool isEnabled = (state != 2); // Disable fields if state is "Restarting"
Debug.WriteLine($"Configuration fields will be set to Enabled: {isEnabled}");
SetConfigurationFieldsEnabled(isEnabled);
}
/// <summary>
/// Checks if a given TCP port is available to be bound.
/// </summary>
/// <param name="port">The port number to check.</param>
/// <returns>True if the port is available, otherwise false.</returns>
// --- NOTE: (CA1822) This *can* be static as the analyzer suggests ---
private static bool IsPortAvailable(int port)
{
Debug.WriteLine($"IsPortAvailable() checking port: {port}");
TcpListener tcpListener = null;
try
{
tcpListener = new TcpListener(IPAddress.Any, port);
tcpListener.Start();
Debug.WriteLine($"Port {port} is available.");
return true;
}
catch (SocketException ex)
{
Debug.WriteLine($"Port check failed for port {port}: {ex.Message}");
return false;
}
finally
{
tcpListener?.Stop();
Debug.WriteLine($"Port {port} check finished.");
}
}
/// <summary>
/// Handles the form's Load event.
/// Loads all saved settings, checks for updates, and then starts the server.
/// </summary>
private async void MainForm_Load(object? sender, EventArgs e)
{
Debug.WriteLine("MainForm_Load() started.");
// Load saved settings into UI
Debug.WriteLine("Loading settings into UI controls...");
portTextBox.Text = Settings.Default.Port.ToString();
passwordTextBox.Text = Settings.Default.Password;
passwordTextBox.PasswordChar = '*';
showPasswordCheckBox.Checked = false;
showPasswordCheckBox.ImageIndex = 0;
runOnStartupCheckBox.Checked = Settings.Default.RunOnStartup;
minimizeToTrayCheckBox.Checked = Settings.Default.MinimizeToTray;
// Sync tray menu item check states
runOnStartupToolStripMenuItem.Checked = Settings.Default.RunOnStartup;
minimizeToSystemTrayToolStripMenuItem.Checked = Settings.Default.MinimizeToTray;
Debug.WriteLine("Settings loaded.");
UpdateUIForState(Settings.Default.ServerState);
// Set the initial visibility of the form (hidden in tray or visible)
Debug.WriteLine("Setting initial form state...");
SetFormInitialState();
// Check for updates silently on launch
Debug.WriteLine("Starting silent update check...");
_ = CheckForUpdatesAsync(silentCheck: true);
Debug.WriteLine("Subscribing to system and program events...");
SystemEvents.DisplaySettingsChanged += OnDisplaySettingsChanged;
Program.ServerStateChanged += OnServerStateChanged;
Debug.WriteLine("MainForm_Load() finished.");
}
// --- FIX: (CS1998) 'async void' lacks 'await'. Made non-async. ---
private void OnDisplaySettingsChanged(object? sender, EventArgs e)
{
Debug.WriteLine($"[{DateTime.Now:HH:mm:ss}] Display settings changed. Triggering cache rebuild.");
// Call async helper and discard the task
_ = HandleDisplaySettingsChangedAsync();
}
// --- NEW: Helper method to do the async work ---
private async Task HandleDisplaySettingsChangedAsync()
{
Debug.WriteLine("HandleDisplaySettingsChangedAsync: Clearing WMI cache...");
MonitorWmiHelper.ClearWmiCache();
// Set server state to "Busy"
Debug.WriteLine("HandleDisplaySettingsChangedAsync: Setting server state to 2 (Busy).");
Program.SetServerState(2);
// Asynchronously rebuild the cache
Debug.WriteLine("HandleDisplaySettingsChangedAsync: Starting cache rebuild...");
await Program.BuildMonitorCacheAsync();
// Set state back to "Running"
Debug.WriteLine("HandleDisplaySettingsChangedAsync: Cache rebuild complete. Setting server state to 1 (Running).");
Program.SetServerState(1);
}
/// <summary>
/// Enables or disables the configuration text boxes and their visual style.
/// </summary>
private void SetConfigurationFieldsEnabled(bool enabled)
{
Debug.WriteLine($"SetConfigurationFieldsEnabled() setting to: {enabled}");
portTextBox.Enabled = enabled;
passwordTextBox.Enabled = enabled;
showPasswordCheckBox.Enabled = enabled;
if (enabled)
{
portTextBox.BackColor = System.Drawing.SystemColors.Window;
passwordTextBox.BackColor = System.Drawing.SystemColors.Window;
}
else
{
portTextBox.BackColor = System.Drawing.Color.LightGray;
passwordTextBox.BackColor = System.Drawing.Color.LightGray;
}
}
/// <summary>
/// Asynchronously calls the Program's RestartServer method and handles any exceptions
/// by showing a user-friendly MessageBox.
/// </summary>
private async Task RestartServerAndHandleUI()
{
Debug.WriteLine("RestartServerAndHandleUI() started.");
try
{
Debug.WriteLine("Awaiting Program.RestartServer()...");
await Task.Run(() => Program.RestartServer());
Debug.WriteLine("Server restart completed.");
}
catch (Exception ex)
{
Debug.WriteLine($"Server restart failed: {ex.Message}");
MessageBox.Show($"Server failed to start. The port may still be in use by another application. Details: {ex.InnerException?.Message ?? ex.Message}",
"Server Connection Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
/// <summary>
/// Sets the initial state of the form (minimized, in tray, or visible)
/// based on the "Run on Startup" and "Minimize to Tray" settings.
/// </summary>
// --- NOTE: (CA1822) This *can* be static as the analyzer suggests ---
private void SetFormInitialState()
{
Debug.WriteLine("SetFormInitialState() started.");
bool runOnStartup = Settings.Default.RunOnStartup;
bool minimizeToTray = Settings.Default.MinimizeToTray;
Debug.WriteLine($"Settings: RunOnStartup={runOnStartup}, MinimizeToTray={minimizeToTray}");
if (runOnStartup)
{
Debug.WriteLine("Running on startup.");
if (minimizeToTray)
{
Debug.WriteLine("Minimizing to system tray.");
this.Hide();
this.ShowInTaskbar = false;
notifyIcon1.Visible = true;
this.WindowState = FormWindowState.Minimized;
}
else
{
Debug.WriteLine("Minimizing to taskbar.");
this.ShowInTaskbar = true;
notifyIcon1.Visible = false;
this.WindowState = FormWindowState.Minimized;
}
}
else
{
Debug.WriteLine("Manual launch. Showing main window.");
this.Show();
this.ShowInTaskbar = true;
notifyIcon1.Visible = false;
this.WindowState = FormWindowState.Normal;
}
Debug.WriteLine("SetFormInitialState() finished.");
}
/// <summary>
/// Validates the port when the user leaves the text box.
/// Restarts the server if the port is valid and has changed.
/// </summary>
// --- FIX: (IDE1006) Naming rule violation ---
private async void PortTextBox_Leave(object? sender, EventArgs e)
{
Debug.WriteLine("portTextBox_Leave() event fired.");
int currentPort = Settings.Default.Port;
if (!int.TryParse(portTextBox.Text, out int newPort))
{
Debug.WriteLine("Port validation failed: Not an integer.");
MessageBox.Show("Please enter a valid whole number for the port.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
portTextBox.Text = currentPort.ToString();
return;
}
if (newPort < 1 || newPort > 65535)
{
Debug.WriteLine("Port validation failed: Out of range.");
MessageBox.Show("Port number must be between 1 and 65535.", "Port Configuration Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
portTextBox.Text = currentPort.ToString();
return;
}
if (forbiddenPorts.Contains(newPort))
{
Debug.WriteLine("Port validation failed: Port is in forbidden list.");
MessageBox.Show($"Port {newPort} is a common service port (like HTTP, FTP, etc.) and cannot be used. Please choose a different port.",
"Port Configuration Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
portTextBox.Text = currentPort.ToString();
return;
}
bool portChanged = currentPort != newPort;
Debug.WriteLine($"Port changed: {portChanged}");
if (portChanged)
{
Debug.WriteLine("Checking if new port is available...");
if (!IsPortAvailable(newPort))
{
Debug.WriteLine("Port validation failed: Port not available.");
MessageBox.Show($"Port {newPort} is already in use by another application. Please choose a different port.", "Port Configuration Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
portTextBox.Text = currentPort.ToString();
return;
}
Debug.WriteLine("Saving new port and restarting server...");
Settings.Default.Port = newPort;
Settings.Default.Save();
Debug.WriteLine($"Application settings saved. New active port: {newPort}.");
await RestartServerAndHandleUI();
}
Debug.WriteLine("portTextBox_Leave() finished.");
}
/// <summary>
/// Validates and saves the password when the user leaves the text box.
/// </summary>
// --- FIX: (IDE1006) Naming rule violation ---
private void PasswordTextBox_Leave(object? sender, EventArgs e)
{
Debug.WriteLine("passwordTextBox_Leave() event fired.");
if (string.IsNullOrWhiteSpace(passwordTextBox.Text))
{
Debug.WriteLine("Password validation failed: IsNullOrWhiteSpace.");
MessageBox.Show("Please enter a password for security.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
if (this.CanFocus)
{
passwordTextBox.Focus();
}
return;
}
if (Settings.Default.Password != passwordTextBox.Text)
{
Debug.WriteLine("Password changed. Saving new password.");
Settings.Default.Password = passwordTextBox.Text;
Settings.Default.Save();
}
Debug.WriteLine("passwordTextBox_Leave() finished.");
}
/// <summary>
/// Handles the "Run on Startup" checkbox change.
/// Saves the setting and updates the Windows Registry.
/// </summary>
// --- FIX: (IDE1006) Naming rule violation ---
private void RunOnStartupCheckBox_CheckedChanged(object? sender, EventArgs e)
{
Debug.WriteLine("runOnStartupCheckBox_CheckedChanged() event fired.");
bool newState = runOnStartupCheckBox.Checked;
if (Settings.Default.RunOnStartup != newState)
{
Debug.WriteLine($"RunOnStartup state changed to {newState}. Saving and updating registry.");
Settings.Default.RunOnStartup = newState;
Settings.Default.Save();
Program.SetStartup(newState);
runOnStartupToolStripMenuItem.Checked = newState;
}
}
/// <summary>
/// Handles the "Minimize to Tray" checkbox change.
/// Saves the setting and updates the tray icon's behavior.
/// </summary>
// --- FIX: (IDE1006) Naming rule violation ---
private void MinimizeToTrayCheckBox_CheckedChanged(object? sender, EventArgs e)
{
Debug.WriteLine("minimizeToTrayCheckBox_CheckedChanged() event fired.");
bool newState = minimizeToTrayCheckBox.Checked;
if (Settings.Default.MinimizeToTray != newState)
{
Debug.WriteLine($"MinimizeToTray state changed to {newState}. Saving and re-evaluating form state.");
Settings.Default.MinimizeToTray = newState;
Settings.Default.Save();
minimizeToSystemTrayToolStripMenuItem.Checked = newState;
// Re-evaluate form state based on new setting
SetFormInitialState();
}
}
/// <summary>
/// Helper method to save any pending changes in the focused text box
/// before minimizing or closing the form.
/// </summary>
/// <returns>True if save was successful, false if validation failed.</returns>
// --- NOTE: (CA1822) This cannot be static as it accesses instance controls ---
private bool SaveActiveSettings()
{
Debug.WriteLine("SaveActiveSettings() called.");
if (this.ActiveControl is TextBox activeTextBox)
{
if (activeTextBox == portTextBox)
{
Debug.WriteLine("Active control is portTextBox. Firing its Leave event.");
PortTextBox_Leave(activeTextBox, EventArgs.Empty);
}
else if (activeTextBox == passwordTextBox)
{
Debug.WriteLine("Active control is passwordTextBox. Firing its Leave event.");
PasswordTextBox_Leave(activeTextBox, EventArgs.Empty);
// If validation failed, passwordTextBox will re-gain focus.
if (this.ActiveControl == passwordTextBox)
{
Debug.WriteLine("Password validation failed, returning false.");
return false;
}
}
}
Debug.WriteLine("SaveActiveSettings() finished, returning true.");
return true;
}
/// <summary>
/// Handles the form's closing event.
/// Intercepts the close action to minimize to tray if the setting is enabled.
/// </summary>
private void MainForm_FormClosing(object? sender, FormClosingEventArgs e)
{
Debug.WriteLine($"MainForm_FormClosing() called. Reason: {e.CloseReason}");
Debug.WriteLine("Unsubscribing from events...");
Program.ServerStateChanged -= OnServerStateChanged;
SystemEvents.DisplaySettingsChanged -= OnDisplaySettingsChanged;
bool isClosingDueToUser = e.CloseReason == CloseReason.UserClosing;
bool shouldMinimize = minimizeToTrayCheckBox.Checked;
if (Settings.Default.MinimizeToTray != shouldMinimize)
{
Debug.WriteLine("Syncing MinimizeToTray setting before close.");
Settings.Default.MinimizeToTray = shouldMinimize;
Settings.Default.Save();
minimizeToSystemTrayToolStripMenuItem.Checked = shouldMinimize;
}
// If user clicks "X" and "Minimize to Tray" is on, intercept the close
if (isClosingDueToUser && shouldMinimize)
{
Debug.WriteLine("User clicked 'X' and MinimizeToTray is on. Intercepting close.");
if (!SaveActiveSettings())
{
Debug.WriteLine("SaveActiveSettings failed. Canceling close.");
e.Cancel = true; // Cancel close if validation fails
return;
}
this.Hide();
this.ShowInTaskbar = false;
notifyIcon1.Visible = true;
this.WindowState = FormWindowState.Minimized;
e.Cancel = true; // Cancel the form closing
Debug.WriteLine("Form hidden to tray.");
return;
}
// If not minimizing, save settings and shut down the server
Debug.WriteLine("Proceeding with form close.");
if (!SaveActiveSettings())
{
Debug.WriteLine("SaveActiveSettings failed. Canceling close.");
e.Cancel = true; // Cancel close if validation fails
return;
}
Debug.WriteLine("Shutting down server and hiding tray icon.");
Program.ShutdownServer();
notifyIcon1.Visible = false;
Debug.WriteLine("MainForm_FormClosing() finished.");
}
/// <summary>
/// Handles the form's resize event.
/// Hides the form and shows the tray icon if minimized.
/// </summary>
private void MainForm_Resize(object? sender, EventArgs e)
{
Debug.WriteLine($"MainForm_Resize() called. WindowState: {this.WindowState}");
if (this.WindowState == FormWindowState.Minimized && Settings.Default.MinimizeToTray)
{
Debug.WriteLine("Window minimized and MinimizeToTray is on.");
if (SaveActiveSettings()) // Only hide if settings are valid
{
Debug.WriteLine("Settings saved. Hiding form to tray.");
this.Hide();
this.ShowInTaskbar = false;
notifyIcon1.Visible = true;
}
else
{
Debug.WriteLine("SaveActiveSettings failed. Forcing window to stay normal.");
this.WindowState = FormWindowState.Normal;
}
}
}
#region Tray Menu Handlers
/// <summary>
/// Handles the "Configure" click from the tray icon menu.
/// Shows the main window.
/// </summary>
// --- FIX: (IDE1006) Naming rule violation ---
private void ConfigureToolStripMenuItem_Click(object? sender, EventArgs e)
{
Debug.WriteLine("TrayMenu: 'Configure' clicked.");
this.Show();
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
notifyIcon1.Visible = false;
}
/// <summary>
/// Handles the "Restart" click from the tray icon menu.
/// </summary>
// --- FIX: (IDE1006) Naming rule violation ---
private async void RestartToolStripMenuItem_Click(object? sender, EventArgs e)
{
Debug.WriteLine("TrayMenu: 'Restart' clicked.");
await RestartServerAndHandleUI();
}
/// <summary>
/// Toggles the "Run on Startup" setting from the tray icon menu.
/// </summary>
// --- FIX: (IDE1006) Naming rule violation ---
private void RunOnStartupToolStripMenuItem_Click(object? sender, EventArgs e)
{
Debug.WriteLine("TrayMenu: 'Run on Startup' clicked.");
// We set it to the *new* state, which is the opposite of the current checked state
bool newState = runOnStartupToolStripMenuItem.Checked;
Debug.WriteLine($"New state: {newState}");
runOnStartupCheckBox.Checked = newState;
}
/// <summary>
/// Toggles the "Minimize to Tray" setting from the tray icon menu.
/// </summary>
// --- FIX: (IDE1006) Naming rule violation ---
private void MinimizeToSystemTrayToolStripMenuItem_Click(object? sender, EventArgs e)
{
Debug.WriteLine("TrayMenu: 'Minimize to System Tray' clicked.");
bool newState = minimizeToSystemTrayToolStripMenuItem.Checked;
Debug.WriteLine($"New state: {newState}");
minimizeToTrayCheckBox.Checked = newState;
}
// --- FIX: (IDE1006) Naming rule violation ---
private async void CheckUpdatesToolStripMenuItem_Click(object? sender, EventArgs e)
{
Debug.WriteLine("TrayMenu: 'Check for Updates' clicked.");
await CheckForUpdatesAsync(silentCheck: false);
}
/// <summary>
/// Handles the "Exit" click from the tray icon menu.
/// Forces the "MinimizeToTray" setting off and exits the application.
/// </summary>
// --- FIX: (IDE1006) Naming rule violation ---
private void ExitToolStripMenuItem_Click(object? sender, EventArgs e)
{
Debug.WriteLine("TrayMenu: 'Exit' clicked.");
// We must set this to false, otherwise the FormClosing event
// will just re-minimize the app instead of closing it.
Debug.WriteLine("Forcing MinimizeToTray=false to ensure exit.");
Settings.Default.MinimizeToTray = false;
Settings.Default.Save();
Application.Exit();
}
/// <summary>
/// Handles double-clicking the tray icon (same as clicking "Configure").
/// </summary>
// --- FIX: (IDE1006) Naming rule violation ---
private void NotifyIcon1_DoubleClick(object? sender, EventArgs e)
{
Debug.WriteLine("TrayIcon: Double-clicked.");
ConfigureToolStripMenuItem_Click(sender, e);
}
#endregion
#region Form Control Handlers
/// <summary>
/// Toggles the password visibility in the text box.
/// </summary>
// --- FIX: (IDE1006) Naming rule violation ---
private void ShowPasswordCheckBox_CheckedChanged(object? sender, EventArgs e)
{
Debug.WriteLine($"showPasswordCheckBox_CheckedChanged() called. New state: {showPasswordCheckBox.Checked}");
if (showPasswordCheckBox.Checked)
{
Debug.WriteLine("Showing password text.");
passwordTextBox.PasswordChar = '\0'; // Show text
showPasswordCheckBox.ImageIndex = 1; // "hide" icon
}
else
{
Debug.WriteLine("Hiding password text.");
passwordTextBox.PasswordChar = '*'; // Hide text
showPasswordCheckBox.ImageIndex = 0; // "show" icon
}
}
#endregion
/// <summary>
/// Checks GitHub for a new version of the application.
/// </summary>
/// <param name="silentCheck">If true, suppresses success/error messages (used during launch).</param>
private async Task CheckForUpdatesAsync(bool silentCheck)
{
Debug.WriteLine($"CheckForUpdatesAsync() started. Silent: {silentCheck}");
// 1. Get current version
Version currentVersion = Assembly.GetExecutingAssembly().GetName().Version;
Debug.WriteLine($"Current version: {currentVersion}");
if (!silentCheck)
{
Debug.WriteLine("Manual check. Setting server state to Busy (2).");
Program.SetServerState(2);
MessageBox.Show("Checking for updates...", "Update Check", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
try
{
// 2. Make web request (GitHub API requires a User-Agent)
Debug.WriteLine("Sending request to GitHub API...");
httpClient.DefaultRequestHeaders.Add("User-Agent", "MultiDisplayVCPServer-Updater");
string jsonResponse = await httpClient.GetStringAsync(GitHubApiUrl);
// 3. Parse the JSON
Debug.WriteLine("Parsing JSON response...");
// --- FIX: (IDE0090) 'new' expression can be simplified ---
var release = JsonSerializer.Deserialize<GitHubRelease>(jsonResponse);
if (release == null || string.IsNullOrEmpty(release.TagName))
{
throw new Exception("Could not parse release information.");
}
// 4. Compare versions
// GitHub tags are often "v1.1.0", so we strip the "v"
Version latestVersion = new Version(release.TagName.TrimStart('v'));
Debug.WriteLine($"Latest version: {latestVersion}");
if (latestVersion > currentVersion)
{
Debug.WriteLine("New version found.");
// 5. New version found! Ask user to download.
var result = MessageBox.Show($"A new version ({release.TagName}) is available. You are currently on {currentVersion}.\n\nWould you like to open the download page?",
"Update Available",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
Debug.WriteLine("User clicked 'Yes'. Opening download page.");
// Find the installer asset (e.g., .msi or .exe)
string downloadUrl = release.HtmlUrl; // Default to the release page
var installerAsset = release.Assets?.FirstOrDefault(a => a.DownloadUrl.EndsWith(".msi") || a.DownloadUrl.EndsWith(".exe"));
if (installerAsset != null)
{
downloadUrl = installerAsset.DownloadUrl;
}
Debug.WriteLine($"Opening URL: {downloadUrl}");
// Open the URL in the default browser
Process.Start(new ProcessStartInfo(downloadUrl) { UseShellExecute = true });
}
else
{
Debug.WriteLine("User clicked 'No'.");
}
}
else if (!silentCheck) // Only show success message if run manually
{
Debug.WriteLine("Already on latest version.");
MessageBox.Show("You are already running the latest version.", "Up to Date", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
Debug.WriteLine($"Update check failed: {ex.Message}");
if (!silentCheck) // Only show error message if run manually
{
MessageBox.Show($"Failed to check for updates. Please check your internet connection or visit the GitHub page manually.\n\nError: {ex.Message}",
"Update Check Failed",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
finally
{
if (!silentCheck)
{
Debug.WriteLine("Manual check finished. Setting server state to Running (1).");
Program.SetServerState(1);
}
}
}
/// <summary>
/// DTO for deserializing the GitHub /releases/latest API response.
/// </summary>
private class GitHubRelease
{
[JsonPropertyName("tag_name")]
public string TagName { get; set; }
[JsonPropertyName("html_url")]
public string HtmlUrl { get; set; }
[JsonPropertyName("assets")]
public List<GitHubAsset> Assets { get; set; }
}
/// <summary>
/// DTO for deserializing a release asset from the GitHub API.
/// </summary>
private class GitHubAsset
{
[JsonPropertyName("browser_download_url")]
public string DownloadUrl { get; set; }
}
}
}