comparison ServerMonitor/Forms/ServerForm.cs @ 8:052aa62cb42a

Single instance. Add autorun option. Add icons. General enhancements.
author Brad Greco <brad@bgreco.net>
date Sat, 09 Mar 2019 20:14:03 -0500
parents b6fe203af9d5
children 2db36ab759de
comparison
equal deleted inserted replaced
7:8486ab7d2357 8:052aa62cb42a
52 monitor.CheckStatusChanged += Monitor_CheckStatusChanged; 52 monitor.CheckStatusChanged += Monitor_CheckStatusChanged;
53 CheckGrid.ClearSelection(); 53 CheckGrid.ClearSelection();
54 if (isNewServer) 54 if (isNewServer)
55 { 55 {
56 LoginComboBox.SelectedIndex = 0; 56 LoginComboBox.SelectedIndex = 0;
57 Icon = CheckStatus.Success.GetIcon();
57 } 58 }
58 else 59 else
59 { 60 {
60 SetTitle(); 61 SetTitle();
62 SetIcon();
61 NameTextBox.Text = Server.Name; 63 NameTextBox.Text = Server.Name;
62 HostTextBox.Text = Server.Host; 64 HostTextBox.Text = Server.Host;
65 EnabledCheckBox.Checked = Server.Enabled;
63 PortTextBox.Text = Server.Port.ToString(); 66 PortTextBox.Text = Server.Port.ToString();
64 UsernameTextBox.Text = Server.Username; 67 UsernameTextBox.Text = Server.Username;
65 PasswordTextBox.Text = "********************"; 68 PasswordTextBox.Text = "********************";
66 LoginComboBox.SelectedIndex = (int)Server.LoginType; 69 LoginComboBox.SelectedIndex = (int)Server.LoginType;
67 KeyTextBox.Text = Server.KeyFile; 70 KeyTextBox.Text = Server.KeyFile;
69 } 72 }
70 73
71 BindChangeListeners(); 74 BindChangeListeners();
72 FormatImageButtons(); 75 FormatImageButtons();
73 UpdateCheckButtons(); 76 UpdateCheckButtons();
77
78 if (NameTextBox.Text == string.Empty)
79 ActiveControl = NameTextBox;
74 } 80 }
75 81
76 public void Show(bool activate) 82 public void Show(bool activate)
77 { 83 {
78 if (!activate) 84 if (!activate)
83 private void Monitor_CheckStatusChanged(object sender, CheckStatusChangedEventArgs e) 89 private void Monitor_CheckStatusChanged(object sender, CheckStatusChangedEventArgs e)
84 { 90 {
85 if (e.Check.Server != Server) 91 if (e.Check.Server != Server)
86 return; 92 return;
87 CheckGrid.Refresh(); 93 CheckGrid.Refresh();
94 SetIcon();
88 if (e.CheckResult != null && logResults != null) 95 if (e.CheckResult != null && logResults != null)
89 { 96 {
90 logResults.Insert(0, e.CheckResult); 97 logResults.Insert(0, e.CheckResult);
91 if (logResultsFiltered != null && MatchesFilter(e.CheckResult)) 98 if (logResultsFiltered != null && MatchesFilter(e.CheckResult))
92 logResultsFiltered.Insert(0, e.CheckResult); 99 logResultsFiltered.Insert(0, e.CheckResult);
100 /// </param> 107 /// </param>
101 private void UpdateServer(bool forceSave = true) 108 private void UpdateServer(bool forceSave = true)
102 { 109 {
103 Server.Name = NameTextBox.Text; 110 Server.Name = NameTextBox.Text;
104 Server.Host = HostTextBox.Text.Trim(); 111 Server.Host = HostTextBox.Text.Trim();
112 Server.Enabled = EnabledCheckBox.Checked;
105 Server.Port = int.TryParse(PortTextBox.Text, out int port) ? port : 0; 113 Server.Port = int.TryParse(PortTextBox.Text, out int port) ? port : 0;
106 Server.Username = UsernameTextBox.Text.Trim(); 114 Server.Username = UsernameTextBox.Text.Trim();
107 Server.LoginType = (LoginType)LoginComboBox.SelectedIndex; 115 Server.LoginType = (LoginType)LoginComboBox.SelectedIndex;
108 Server.KeyFile = KeyTextBox.Text.Trim(); 116 Server.KeyFile = KeyTextBox.Text.Trim();
109 if (changedPassword) 117 if (changedPassword)
119 private void SetTitle(string title = null) 127 private void SetTitle(string title = null)
120 { 128 {
121 title = (title ?? Server.Name) + (Server.Enabled ? "" : " (disabled)"); 129 title = (title ?? Server.Name) + (Server.Enabled ? "" : " (disabled)");
122 Text = title; 130 Text = title;
123 TitleLabel.Text = title; 131 TitleLabel.Text = title;
132 }
133
134 private void SetIcon()
135 {
136 if (Server != null)
137 Icon = Server.Status.GetIcon();
124 } 138 }
125 139
126 private void NameTextBox_TextChanged(object sender, EventArgs e) 140 private void NameTextBox_TextChanged(object sender, EventArgs e)
127 { 141 {
128 SetTitle(NameTextBox.Text); 142 SetTitle(NameTextBox.Text);
294 308
295 private void CheckGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 309 private void CheckGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
296 { 310 {
297 if (e.ColumnIndex == StatusColumn.Index) 311 if (e.ColumnIndex == StatusColumn.Index)
298 { 312 {
299 e.Value = ((CheckStatus)e.Value).GetIcon(); 313 e.Value = ((CheckStatus)e.Value).GetImage();
300 } 314 }
301 } 315 }
302 316
303 private void LogGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 317 private void LogGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
304 { 318 {
305 if (e.ColumnIndex == LogStatusColumn.Index) 319 if (e.ColumnIndex == LogStatusColumn.Index)
306 { 320 {
307 e.Value = ((CheckStatus)e.Value).GetIcon(); 321 e.Value = ((CheckStatus)e.Value).GetImage();
308 } 322 }
309 } 323 }
310 324
311 private void CheckBindingSource_ListChanged(object sender, ListChangedEventArgs e) 325 private void CheckBindingSource_ListChanged(object sender, ListChangedEventArgs e)
312 { 326 {
313 if (Server?.Checks != null) 327 if (Server?.Checks != null)
314 { 328 {
315 //TODO this might result in the selection being reset to all (if a new check is added, for example.) Restore selected index.
316 LogCheckComboBox.Items.Clear(); 329 LogCheckComboBox.Items.Clear();
317 LogCheckComboBox.Items.Add("(All)"); 330 LogCheckComboBox.Items.Add("(All)");
318 LogCheckComboBox.Items.AddRange(Server.Checks.ToArray()); 331 LogCheckComboBox.Items.AddRange(Server.Checks.ToArray());
319 LogCheckComboBox.SelectedIndex = 0; 332 LogCheckComboBox.SelectedIndex = 0;
320 } 333 }
398 OpenPrivateKey(monitor, Server, this); 411 OpenPrivateKey(monitor, Server, this);
399 } 412 }
400 413
401 public static void OpenPrivateKey(ServerMonitor monitor, Server server, IWin32Window owner) 414 public static void OpenPrivateKey(ServerMonitor monitor, Server server, IWin32Window owner)
402 { 415 {
403 if (server.LoginType != LoginType.PrivateKey) 416 if (server.LoginType != LoginType.PrivateKey || server.KeyFile.IsNullOrEmpty())
404 return; 417 return;
405 418
406 KeyStatus keyStatus = monitor.OpenPrivateKey(server.KeyFile); 419 KeyStatus keyStatus = monitor.OpenPrivateKey(server.KeyFile);
407 if (keyStatus == KeyStatus.NeedPassword) 420 if (keyStatus == KeyStatus.NeedPassword)
408 { 421 {
426 MessageBox.Show("The private key file " + server.KeyFile + " is not accessible or does not exist.", "Cannot open private key", MessageBoxButtons.OK, MessageBoxIcon.Error); 439 MessageBox.Show("The private key file " + server.KeyFile + " is not accessible or does not exist.", "Cannot open private key", MessageBoxButtons.OK, MessageBoxIcon.Error);
427 return; 440 return;
428 } 441 }
429 } 442 }
430 443
444 private void EnabledCheckBox_Click(object sender, EventArgs e)
445 {
446 bool enabled = EnabledCheckBox.Checked;
447 if (enabled)
448 OpenPrivateKey(monitor, Server, this);
449 Server.Enabled = enabled;
450 EnabledCheckBox.Checked = Server.Enabled;
451 }
452
453 //private void EnabledCheckBox_CheckedChanged(object sender, EventArgs e)
454 //{
455 // bool enabled = EnabledCheckBox.Checked;
456 // if (enabled)
457 // OpenPrivateKey(monitor, Server, this);
458 // EnabledCheckBox.Checked = Server.Enabled;
459 //}
460
431 private void Server_EnabledChanged(object sender, EventArgs e) 461 private void Server_EnabledChanged(object sender, EventArgs e)
432 { 462 {
433 SetTitle(); 463 SetTitle();
464 EnabledCheckBox.Checked = Server.Enabled;
434 } 465 }
435 } 466 }
436 } 467 }