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