Mercurial > servermonitor
annotate ServerMonitor/Forms/ServerSummaryForm.cs @ 40:c4fc74593a78 default tip
Mono fix
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Sat, 13 Jun 2020 13:28:20 -0400 |
parents | 9c0e18d65e8b |
children |
rev | line source |
---|---|
10 | 1 using NAppUpdate.Framework; |
2 using NAppUpdate.Framework.Sources; | |
3 using ServerMonitorApp.Properties; | |
4 | 4 using System; |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
5 using System.Collections.Generic; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
6 using System.ComponentModel; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
7 using System.Data; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
8 using System.Drawing; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
9 using System.Linq; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
10 using System.Windows.Forms; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
11 |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
12 namespace ServerMonitorApp |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
13 { |
16 | 14 /// <summary>Main application form that shows an overview of all servers.</summary> |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
15 public partial class ServerSummaryForm : Form |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
16 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
17 private readonly Dictionary<Server, ServerForm> serverForms = new Dictionary<Server, ServerForm>(); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
18 private ServerMonitor monitor; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
19 |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
20 public ServerSummaryForm() |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
21 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
22 InitializeComponent(); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
23 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
24 |
16 | 25 private void ServerSummaryForm_Load(object sender, EventArgs e) |
26 { | |
27 // Restore the window size from the previous session. | |
28 Size size = Settings.Default.SummaryFormSize; | |
29 if (size.Height > 0 && size.Width > 0) | |
30 Size = size; | |
31 | |
32 // Resize the images in buttons to fit the button size. | |
33 Helpers.FormatImageButton(NewServerButton); | |
19 | 34 Helpers.FormatImageButton(AboutButton); |
16 | 35 Helpers.FormatImageButton(SettingsButton); |
36 // Create the global server monitor object. | |
37 monitor = new ServerMonitor(this); | |
38 // Load the server configuration file. | |
39 while (true) | |
40 { | |
41 try | |
42 { | |
43 // If the configuration file is loaded successfully, proceed. | |
44 monitor.LoadServers(); | |
45 break; | |
46 } | |
47 catch (Exception ex) | |
48 { | |
49 // If there was an error loading the config file, show it. | |
50 DialogResult result = MessageBox.Show("Could not load servers. Please fix or delete the file " + monitor.ConfigFile + Environment.NewLine + Environment.NewLine | |
51 + "Error details:" + Environment.NewLine + ex.GetAllMessages(), | |
52 "Error loading servers", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error); | |
53 // If the error message was cancelled, exit. Otherwise, retry by continuing the loop. | |
54 if (result == DialogResult.Cancel) | |
55 { | |
56 Environment.Exit(1); | |
57 } | |
58 } | |
59 } | |
60 // Listen to server monitor events. | |
61 monitor.CheckStatusChanged += Monitor_CheckStatusChanged; | |
62 // Show the servers. | |
63 RefreshDisplay(); | |
64 // If any servers have encrypted private keys, attempt to open them immediately | |
65 // rather than interrupting the user later when they are first used. | |
66 CollectPrivateKeyPasswords(); | |
67 CheckForUpdate(); | |
68 } | |
69 | |
70 /// <summary>Shows a form to edit or create a server.</summary> | |
71 /// <param name="server">The server to edit. If null, a new server will be created.</param> | |
72 /// <param name="activate">Whether the server form should be activated.</param> | |
73 /// <returns>The created or existing server for for the server.</returns> | |
74 private ServerForm ShowServerForm(Server server, bool activate = true) | |
75 { | |
76 bool isNewServer = false; | |
77 if (server == null) | |
78 { | |
79 // Create a new server if none was given. | |
80 server = new Server(); | |
81 // The server is added to the server monitor immediately so that | |
82 // checks can be created and run. If the server was created by | |
83 // mistake, it will automatically be removed when the form is closed | |
84 // as long as no information was entered into the form. | |
85 monitor.AddServer(server); | |
86 isNewServer = true; | |
87 } | |
88 if (serverForms.TryGetValue(server, out ServerForm form)) | |
89 { | |
90 // If the server form is already open, just activate it if requested. | |
91 if (activate) | |
92 form.Activate(); | |
93 } | |
94 else | |
95 { | |
96 // Open a new server form for the server. | |
97 form = new ServerForm(monitor, server, isNewServer); | |
98 // Keep track of the form so it can be activated later if the server is clicked again. | |
99 serverForms[server] = form; | |
100 form.FormClosing += ServerForm_FormClosing; | |
101 form.Show(activate); | |
102 } | |
103 return form; | |
104 } | |
105 | |
106 /// <summary>Refreshes the server list with the server monitor data.</summary> | |
107 private void RefreshDisplay() | |
108 { | |
109 // Delete all server controls and recreate them. | |
110 ServerPanel.Controls.Clear(); | |
111 foreach (Server server in monitor.Servers) | |
112 { | |
113 // Subscribe to server events. | |
114 server.EnabledChanged -= Server_EnabledChanged; | |
115 server.EnabledChanged += Server_EnabledChanged; | |
116 // Create a server control and add it to the panel. | |
117 ServerSummaryControl control = new ServerSummaryControl(server); | |
118 control.ContextMenuStrip = ServerContextMenu; | |
119 control.Click += ServerSummaryControl_Click; | |
120 ServerPanel.Controls.Add(control); | |
121 } | |
122 // Refresh the form icon that depends on the status of all servers. | |
123 UpdateIcon(); | |
124 } | |
125 | |
126 /// <summary>Refreshes a single server control.</summary> | |
127 /// <param name="server">The server to refresh.</param> | |
128 private void RefreshServer(Server server) | |
129 { | |
23
3866c19535fd
Fix NullReferenceException when checks are executed on a brand new server.
Brad Greco <brad@bgreco.net>
parents:
19
diff
changeset
|
130 ServerPanel.Controls.Cast<ServerSummaryControl>().FirstOrDefault(c => c.Server == server)?.Refresh(); |
16 | 131 // The server's status might have changed, so refresh the form icon. |
132 UpdateIcon(); | |
133 } | |
134 | |
135 /// <summary>Flashes the taskbar button for a server form.</summary> | |
136 /// <param name="check">The check that needs attention.</param> | |
4 | 137 public void AlertServerForm(Check check) |
138 { | |
16 | 139 // Show the form, but don't activate it since the user did not initiate this event. |
4 | 140 ServerForm form = ShowServerForm(check.Server, false); |
16 | 141 // Flash the taskbar button. |
4 | 142 Win32Helpers.FlashWindowEx(form); |
16 | 143 // If the form was not already open, focus the Log tab and display |
144 // only the log entries for this check. Do not do this if the form | |
145 // was already open since the user might be in the middle of doing | |
146 // something with it. | |
147 if (!serverForms.ContainsKey(check.Server)) | |
4 | 148 { |
149 form.ShowLog(check); | |
150 } | |
151 } | |
152 | |
16 | 153 /// <summary>Shows a balloon popup with the results of a failed check.</summary> |
154 /// <param name="check">The check that failed.</param> | |
4 | 155 public void ShowBalloon(CheckResult result) |
156 { | |
157 string title = string.Format("{0}: {1} failed", result.Check.Server.Name, result.Check.Name); | |
158 NotifyIcon.Tag = result; | |
159 NotifyIcon.ShowBalloonTip(30000, title, result.Message, GetToolTipIcon(result.CheckStatus)); | |
160 } | |
161 | |
16 | 162 /// <summary>Updates the form icon to reflect a summary of the status of all servers.</summary> |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
163 private void UpdateIcon() |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
164 { |
16 | 165 // The status for the summary icon is the most severe status of all servers. |
166 // When performing the comparison: | |
167 // - Enabled servers use their current status. | |
168 // - If a server is disabled due to a locked private key, report a warning. | |
169 // Otherwise, report success to effectively ignore it. | |
170 // The integer value of the CheckStatus enum increases with the severity, | |
171 // so the maximum value of all servers gives the most severe status. | |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
172 CheckStatus status = monitor.Servers |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
173 .Select(s => s.Enabled |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
174 ? s.Status |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
175 : s.KeyStatus == KeyStatus.NeedPassword ? CheckStatus.Warning : CheckStatus.Success) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
176 .DefaultIfEmpty(CheckStatus.Success) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
177 .Max(); |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
178 Icon = status.GetIcon(); |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
179 NotifyIcon.Icon = Icon; |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
180 } |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
181 |
16 | 182 /// <summary>Prompts the user for the passwords to open all encrypted private keys.</summary> |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
183 private void CollectPrivateKeyPasswords() |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
184 { |
16 | 185 // List of paths to keyfiles. |
6
c1dffaac66fa
- Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents:
5
diff
changeset
|
186 List<string> triedKeys = new List<string>(); |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
187 foreach (Server server in monitor.Servers) |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
188 { |
16 | 189 // If the same private key is used for multiple servers, don't prompt |
190 // the user multiple times to open the same keyfile. | |
6
c1dffaac66fa
- Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents:
5
diff
changeset
|
191 if (triedKeys.Contains(server.KeyFile)) |
c1dffaac66fa
- Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents:
5
diff
changeset
|
192 continue; |
16 | 193 // Show the prompt. |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
194 ServerForm.OpenPrivateKey(monitor, server, this); |
16 | 195 // Keep track of the keyfile so we don't needlessly ask again. |
6
c1dffaac66fa
- Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents:
5
diff
changeset
|
196 triedKeys.Add(server.KeyFile); |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
197 } |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
198 } |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
199 |
16 | 200 /// <summary>Refreshes a server control when the server state changes.</summary> |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
201 private void Server_EnabledChanged(object sender, EventArgs e) |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
202 { |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
203 RefreshServer((Server)sender); |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
204 } |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
205 |
16 | 206 /// <summary>Refreshes a server control when the server status might have changed.</summary> |
4 | 207 private void Monitor_CheckStatusChanged(object sender, CheckStatusChangedEventArgs e) |
208 { | |
209 if (e.CheckResult != null) | |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
210 { |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
211 RefreshServer(e.Check.Server); |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
212 } |
4 | 213 } |
214 | |
16 | 215 /// <summary>Gets a Windows tooltip icon based on the severity of the message.</summary> |
216 /// <param name="status">The status of the check that will be reported in the balloon tip.</param> | |
4 | 217 private ToolTipIcon GetToolTipIcon(CheckStatus status) |
218 { | |
219 switch (status) | |
220 { | |
221 case CheckStatus.Error: return ToolTipIcon.Error; | |
222 case CheckStatus.Warning: return ToolTipIcon.Warning; | |
223 case CheckStatus.Information: return ToolTipIcon.Info; | |
224 default: return ToolTipIcon.None; | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
225 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
226 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
227 |
16 | 228 /// <summary>Shows a server form when a server control is clicked.</summary> |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
229 private void ServerSummaryControl_Click(object sender, EventArgs e) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
230 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
231 ShowServerForm(((ServerSummaryControl)sender).Server); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
232 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
233 |
16 | 234 /// <summary>Handles the closing of a server form.</summary> |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
235 private void ServerForm_FormClosing(object sender, FormClosingEventArgs e) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
236 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
237 ServerForm form = (ServerForm)sender; |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
238 form.FormClosing -= ServerForm_FormClosing; |
4 | 239 Server server = form.Server; |
16 | 240 // Remove the closed form from the list of open forms. |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
241 serverForms.Remove(form.Server); |
16 | 242 // If there is no user data associated with the server, it can be deleted. |
243 // This usually happens when the New Server button is clicked and the server form | |
244 // is closed without entering any information. | |
4 | 245 if (server.IsEmpty()) |
246 { | |
247 monitor.DeleteServer(server); | |
248 } | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
249 RefreshDisplay(); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
250 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
251 |
16 | 252 /// <summary>Shows a server form to create a new server.</summary> |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
253 private void NewServerButton_Click(object sender, EventArgs e) |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
254 { |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
255 ShowServerForm(null); |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
256 } |
4 | 257 |
16 | 258 /// <summary>Hides the main form instead of exiting the application based on user preferences.</summary> |
259 /// <remarks>Allows the monitor to run in the background without being shown in the taskbar.</remarks> | |
4 | 260 private void ServerSummaryForm_FormClosing(object sender, FormClosingEventArgs e) |
261 { | |
11
75ca86e0862c
Add setting to hide to notification area.
Brad Greco <brad@bgreco.net>
parents:
10
diff
changeset
|
262 if ((e.CloseReason == CloseReason.None || e.CloseReason == CloseReason.UserClosing) && Settings.Default.HideToNotificationArea) |
4 | 263 { |
264 Hide(); | |
265 e.Cancel = true; | |
266 } | |
267 } | |
268 | |
16 | 269 /// <summary>Shows the settings form.</summary> |
4 | 270 private void SettingsButton_Click(object sender, EventArgs e) |
271 { | |
272 new SettingsForm().Show(); | |
273 } | |
274 | |
16 | 275 /// <summary>Shows the details of a failed check when the balloon notification is clicked.</summary> |
4 | 276 private void NotifyIcon_BalloonTipClicked(object sender, EventArgs e) |
277 { | |
278 CheckResult result = (CheckResult)(sender as NotifyIcon).Tag; | |
279 ServerForm form = ShowServerForm(result.Check.Server); | |
280 form.ShowLog(result.Check); | |
281 form.WindowState = FormWindowState.Normal; | |
282 } | |
283 | |
16 | 284 /// <summary>Handles the server context menu.</summary> |
4 | 285 private void ServerContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e) |
286 { | |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
287 Server server = GetClickedServer((ContextMenuStrip)e.ClickedItem.Owner); |
4 | 288 if (e.ClickedItem == DeleteServerMenuItem) |
289 { | |
16 | 290 // Close the menu immediately so it doesn't stay open while the messagebox is shown. |
4 | 291 ServerContextMenu.Close(); |
16 | 292 // Show the server delete confirmation dialog. No option to not ask again |
293 // since it's a rare and very destructive event. | |
4 | 294 DialogResult result = MessageBox.Show( |
295 string.Format("The server \"{0}\" and its {1} {2} will be deleted.", server.Name, server.Checks.Count, server.Checks.Count == 1 ? "check" : "checks"), | |
296 "Delete server", | |
297 MessageBoxButtons.OKCancel, | |
16 | 298 MessageBoxIcon.Warning); |
4 | 299 if (result == DialogResult.OK) |
300 { | |
301 monitor.DeleteServer(server); | |
302 RefreshDisplay(); | |
303 } | |
304 } | |
305 else if (e.ClickedItem == ToggleEnableServerMenuItem) | |
306 { | |
307 bool enable = ToggleEnableServerMenuItem.Text == "Enable"; | |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
308 if (enable) |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
309 { |
16 | 310 // Close the menu immediately so it doesn't stay open while the messagebox is shown. |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
311 ServerContextMenu.Close(); |
16 | 312 // Attempt to open the private key for the server immediately since it has not |
313 // been opened yet. | |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
314 ServerForm.OpenPrivateKey(monitor, server, this); |
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
315 } |
4 | 316 server.Enabled = enable; |
317 RefreshDisplay(); | |
318 } | |
319 } | |
320 | |
16 | 321 /// <summary>Activates the appropriate Enable/Disable menu option based on the server's current state.</summary> |
4 | 322 private void ServerContextMenu_Opening(object sender, CancelEventArgs e) |
323 { | |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
324 Server server = GetClickedServer((ContextMenuStrip)sender); |
4 | 325 ToggleEnableServerMenuItem.Text = server.Enabled ? "Disable" : "Enable"; |
326 } | |
327 | |
16 | 328 /// <summary>Gets the server corresponding to a server context menu.</summary> |
5
b6fe203af9d5
Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents:
4
diff
changeset
|
329 private Server GetClickedServer(ContextMenuStrip menu) |
4 | 330 { |
331 return ((ServerSummaryControl)menu.SourceControl).Server; | |
332 } | |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
333 |
16 | 334 /// <summary>Saves the window size after it is resized so it can be restored next time the program is run.</summary> |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
335 private void ServerSummaryForm_ResizeEnd(object sender, EventArgs e) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
336 { |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
337 Settings.Default.SummaryFormSize = Size; |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
338 Settings.Default.Save(); |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
339 } |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
340 |
16 | 341 /// <summary>Shows the main form when the WM_SHOWMONITOR message is received.</summary> |
342 /// <remarks> | |
343 /// This is used to make this a single-instance application. When a second copy of the program | |
344 /// is launched, it sends this message to activate the first copy and then exits. | |
345 /// </remarks> | |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
346 protected override void WndProc(ref Message m) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
347 { |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
348 if (m.Msg == Win32Helpers.WM_SHOWMONITOR) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
349 ShowWindow(); |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
350 base.WndProc(ref m); |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
351 } |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
352 |
16 | 353 /// <summary>Handles clicks on the notification area icon.</summary> |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
354 private void NotifyIcon_MouseClick(object sender, MouseEventArgs e) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
355 { |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
356 if (e.Button == MouseButtons.Left) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
357 ShowWindow(); |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
358 else if (e.Button == MouseButtons.Right) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
359 NotificationIconMenu.Show(); |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
360 } |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
361 |
16 | 362 /// <summary>Shows the window.</summary> |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
363 private void ShowWindow() |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
364 { |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
365 if (WindowState == FormWindowState.Minimized) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
366 WindowState = FormWindowState.Normal; |
16 | 367 // Do various things to try to get this window on top. |
368 // We only do this as a result of user input, so it's ok. ;) | |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
369 Show(); |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
370 TopMost = true; |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
371 TopMost = false; |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
372 Activate(); |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
373 } |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
374 |
16 | 375 /// <summary>Handles clicks on the notification icon menu.</summary> |
8
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
376 private void NotificationIconMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
377 { |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
378 if (e.ClickedItem == ShowServerMonitorMenuItem) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
379 ShowWindow(); |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
380 else if (e.ClickedItem == ExitMenuItem) |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
381 Application.Exit(); |
052aa62cb42a
Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents:
6
diff
changeset
|
382 } |
10 | 383 |
16 | 384 /// <summary>Begins checking for program updates in the background.</summary> |
10 | 385 private void CheckForUpdate() |
386 { | |
34
9c0e18d65e8b
Build NuGet from source instead of using the NuGet package to fix the update notification always showing when the program is run from Windows startup.
Brad Greco <brad@bgreco.net>
parents:
30
diff
changeset
|
387 // The latest NuGet package of NAppUpdate (0.5.1) always thinks that an update is available |
9c0e18d65e8b
Build NuGet from source instead of using the NuGet package to fix the update notification always showing when the program is run from Windows startup.
Brad Greco <brad@bgreco.net>
parents:
30
diff
changeset
|
388 // when the program is run from Windows startup (https://github.com/synhershko/NAppUpdate/pull/122). |
9c0e18d65e8b
Build NuGet from source instead of using the NuGet package to fix the update notification always showing when the program is run from Windows startup.
Brad Greco <brad@bgreco.net>
parents:
30
diff
changeset
|
389 // The fix has already been made but not released, so the version of NAppUpdate included in |
9c0e18d65e8b
Build NuGet from source instead of using the NuGet package to fix the update notification always showing when the program is run from Windows startup.
Brad Greco <brad@bgreco.net>
parents:
30
diff
changeset
|
390 // this project was built from source instead of using the NuGet package. |
10 | 391 UpdateManager manager = UpdateManager.Instance; |
16 | 392 // Make the update manager happy if the program was just restarted to apply an update. |
10 | 393 manager.ReinstateIfRestarted(); |
30 | 394 manager.UpdateSource = new SimpleWebSource("https://www.bgreco.net/servermonitor/update.xml"); |
10 | 395 if (manager.State == UpdateManager.UpdateProcessState.NotChecked) |
396 manager.BeginCheckForUpdates(CheckForUpdatesCallback, null); | |
397 } | |
398 | |
16 | 399 /// <summary>Callback after the program update check completes.</summary> |
10 | 400 private void CheckForUpdatesCallback(IAsyncResult result) |
401 { | |
402 UpdateManager manager = UpdateManager.Instance; | |
403 if (manager.UpdatesAvailable > 0) | |
404 { | |
16 | 405 // Extract the new version number from the result. |
10 | 406 GetUpdateInfo(out string version, out string _); |
16 | 407 // If the user has not chosen to ignore this update, show a notification. |
10 | 408 if (Settings.Default.IgnoreUpdate != version) |
409 Invoke((MethodInvoker)(() => UpdatePanel.Show())); | |
410 } | |
411 } | |
412 | |
16 | 413 /// <summary>Applies the program updates.</summary> |
10 | 414 private void PrepareUpdatesCallback(IAsyncResult result) |
415 { | |
416 UpdateManager manager = UpdateManager.Instance; | |
417 manager.EndCheckForUpdates(result); | |
418 manager.ApplyUpdates(true); | |
419 } | |
420 | |
16 | 421 /// <summary>Shows information about a program update when the notification is clicked.</summary> |
10 | 422 private void UpdateLabel_Click(object sender, EventArgs e) |
423 { | |
16 | 424 // Extract the update information from the update manager result. |
10 | 425 GetUpdateInfo(out string version, out string changeMessage); |
426 string message = "Server Monitor version {0} is available for download." + Environment.NewLine | |
427 + Environment.NewLine | |
428 + "What's new:" + Environment.NewLine | |
429 + "{1}" + Environment.NewLine | |
430 + Environment.NewLine | |
431 + "Would you like to download and apply the update now?"; | |
432 using (UpdateDialog dialog = new UpdateDialog { Message = string.Format(message, version, changeMessage) }) | |
433 { | |
434 DialogResult result = dialog.ShowDialog(); | |
16 | 435 // If the user declined the update and asked not to be notified again, |
436 // save the preference so they will not be asked again for this version. | |
10 | 437 if (dialog.Checked && result == DialogResult.Cancel) |
438 { | |
439 Settings.Default.IgnoreUpdate = version; | |
440 Settings.Default.Save(); | |
441 UpdatePanel.Hide(); | |
442 } | |
16 | 443 // If Yes was not chosen, do not apply the update. |
10 | 444 if (result != DialogResult.OK) |
445 return; | |
446 } | |
447 UpdateManager.Instance.BeginPrepareUpdates(PrepareUpdatesCallback, null); | |
448 } | |
449 | |
16 | 450 /// <summary>Extracts the update information from the update manager result.</summary> |
10 | 451 private void GetUpdateInfo(out string version, out string changeMessage) |
452 { | |
16 | 453 // The update description is in the form {version}:{change message}. |
10 | 454 string[] parts = UpdateManager.Instance.Tasks.First().Description.Split(new char[] { ':' }, 2); |
455 version = parts[0]; | |
456 changeMessage = parts[1]; | |
457 } | |
19 | 458 |
459 private void AboutButton_Click(object sender, EventArgs e) | |
460 { | |
461 new AboutForm().Show(); | |
462 } | |
0
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
463 } |
3e1a2131f897
Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff
changeset
|
464 } |