comparison ServerMonitor/Forms/ServerSummaryForm.cs @ 4:3142e52cbe69

Lots more progress
author Brad Greco <brad@bgreco.net>
date Sun, 10 Feb 2019 20:51:26 -0500
parents 3e1a2131f897
children b6fe203af9d5
comparison
equal deleted inserted replaced
3:96f0b028176d 4:3142e52cbe69
1 using System; 1 using ServerMonitorApp.Properties;
2 using System;
2 using System.Collections.Generic; 3 using System.Collections.Generic;
3 using System.ComponentModel; 4 using System.ComponentModel;
4 using System.Data; 5 using System.Data;
5 using System.Drawing; 6 using System.Drawing;
6 using System.Linq; 7 using System.Linq;
17 public ServerSummaryForm() 18 public ServerSummaryForm()
18 { 19 {
19 InitializeComponent(); 20 InitializeComponent();
20 } 21 }
21 22
23 public void AlertServerForm(Check check)
24 {
25 bool existingForm = serverForms.ContainsKey(check.Server);
26 ServerForm form = ShowServerForm(check.Server, false);
27 Win32Helpers.FlashWindowEx(form);
28 if (!existingForm)
29 {
30 form.ShowLog(check);
31 }
32 }
33
34 public void ShowBalloon(CheckResult result)
35 {
36 string title = string.Format("{0}: {1} failed", result.Check.Server.Name, result.Check.Name);
37 NotifyIcon.Tag = result;
38 NotifyIcon.ShowBalloonTip(30000, title, result.Message, GetToolTipIcon(result.CheckStatus));
39 }
40
22 private void ServerSummaryForm_Load(object sender, EventArgs e) 41 private void ServerSummaryForm_Load(object sender, EventArgs e)
23 { 42 {
24 Helpers.FormatImageButton(NewServerButton); 43 Helpers.FormatImageButton(NewServerButton);
25 monitor = new ServerMonitor(); 44 Helpers.FormatImageButton(SettingsButton);
45 monitor = new ServerMonitor(this);
26 while (true) 46 while (true)
27 { 47 {
28 try 48 try
29 { 49 {
30 monitor.LoadServers(); 50 monitor.LoadServers();
39 { 59 {
40 Environment.Exit(1); 60 Environment.Exit(1);
41 } 61 }
42 } 62 }
43 } 63 }
64 NotifyIcon.Icon = new Icon(Icon, 16, 16);
65 monitor.CheckStatusChanged += Monitor_CheckStatusChanged;
44 RefreshDisplay(); 66 RefreshDisplay();
45 } 67 }
46 68
47 private void RefreshDisplay() 69 private ServerForm ShowServerForm(Server server, bool activate = true)
48 {
49 ServerPanel.Controls.Clear();
50 foreach (Server server in monitor.Servers)
51 {
52 ServerSummaryControl control = new ServerSummaryControl(server);
53 control.Click += ServerSummaryControl_Click;
54 ServerPanel.Controls.Add(control);
55 }
56 }
57
58 private void ShowServerForm(Server server)
59 { 70 {
60 bool isNewServer = false; 71 bool isNewServer = false;
61 if (server == null) 72 if (server == null)
62 { 73 {
63 server = new Server(); 74 server = new Server();
64 monitor.AddServer(server); 75 monitor.AddServer(server);
65 isNewServer = true; 76 isNewServer = true;
66 } 77 }
67 if (serverForms.TryGetValue(server, out ServerForm form)) 78 if (serverForms.TryGetValue(server, out ServerForm form))
68 { 79 {
69 form.Activate(); 80 if (activate)
81 form.Activate();
70 } 82 }
71 else 83 else
72 { 84 {
73 form = new ServerForm(monitor, server, isNewServer); 85 form = new ServerForm(monitor, server, isNewServer);
74 serverForms[server] = form; 86 serverForms[server] = form;
75 form.FormClosing += ServerForm_FormClosing; 87 form.FormClosing += ServerForm_FormClosing;
76 form.Show(); 88 form.Show(activate);
89 }
90 return form;
91 }
92
93 private void RefreshDisplay()
94 {
95 ServerPanel.Controls.Clear();
96 foreach (Server server in monitor.Servers)
97 {
98 ServerSummaryControl control = new ServerSummaryControl(server);
99 control.ContextMenuStrip = ServerContextMenu;
100 control.Click += ServerSummaryControl_Click;
101 ServerPanel.Controls.Add(control);
102 }
103 }
104
105 private void Monitor_CheckStatusChanged(object sender, CheckStatusChangedEventArgs e)
106 {
107 if (e.CheckResult != null)
108 ServerPanel.Controls.Cast<ServerSummaryControl>().FirstOrDefault(c => c.Server == e.Check.Server).Refresh();
109 }
110
111 private ToolTipIcon GetToolTipIcon(CheckStatus status)
112 {
113 switch (status)
114 {
115 case CheckStatus.Error: return ToolTipIcon.Error;
116 case CheckStatus.Warning: return ToolTipIcon.Warning;
117 case CheckStatus.Information: return ToolTipIcon.Info;
118 default: return ToolTipIcon.None;
77 } 119 }
78 } 120 }
79 121
80 private void ServerSummaryControl_Click(object sender, EventArgs e) 122 private void ServerSummaryControl_Click(object sender, EventArgs e)
81 { 123 {
84 126
85 private void ServerForm_FormClosing(object sender, FormClosingEventArgs e) 127 private void ServerForm_FormClosing(object sender, FormClosingEventArgs e)
86 { 128 {
87 ServerForm form = (ServerForm)sender; 129 ServerForm form = (ServerForm)sender;
88 form.FormClosing -= ServerForm_FormClosing; 130 form.FormClosing -= ServerForm_FormClosing;
131 Server server = form.Server;
89 serverForms.Remove(form.Server); 132 serverForms.Remove(form.Server);
133 if (server.IsEmpty())
134 {
135 monitor.DeleteServer(server);
136 }
90 RefreshDisplay(); 137 RefreshDisplay();
91 } 138 }
92 139
93 private void NewServerButton_Click(object sender, EventArgs e) 140 private void NewServerButton_Click(object sender, EventArgs e)
94 { 141 {
95 ShowServerForm(null); 142 ShowServerForm(null);
143 }
144
145 private void ServerSummaryForm_FormClosing(object sender, FormClosingEventArgs e)
146 {
147 if (e.CloseReason == CloseReason.None || e.CloseReason == CloseReason.UserClosing)
148 {
149 Hide();
150 e.Cancel = true;
151 }
152 }
153
154 private void SettingsButton_Click(object sender, EventArgs e)
155 {
156 new SettingsForm().Show();
157 }
158
159 private void NotifyIcon_BalloonTipClicked(object sender, EventArgs e)
160 {
161 CheckResult result = (CheckResult)(sender as NotifyIcon).Tag;
162 ServerForm form = ShowServerForm(result.Check.Server);
163 form.ShowLog(result.Check);
164 form.WindowState = FormWindowState.Normal;
165 }
166
167 private void ServerContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
168 {
169 Server server = getClickedServer((ContextMenuStrip)e.ClickedItem.Owner);
170 if (e.ClickedItem == DeleteServerMenuItem)
171 {
172 ServerContextMenu.Close();
173 DialogResult result = MessageBox.Show(
174 string.Format("The server \"{0}\" and its {1} {2} will be deleted.", server.Name, server.Checks.Count, server.Checks.Count == 1 ? "check" : "checks"),
175 "Delete server",
176 MessageBoxButtons.OKCancel,
177 MessageBoxIcon.Warning );
178 if (result == DialogResult.OK)
179 {
180 monitor.DeleteServer(server);
181 RefreshDisplay();
182 }
183 }
184 else if (e.ClickedItem == ToggleEnableServerMenuItem)
185 {
186 bool enable = ToggleEnableServerMenuItem.Text == "Enable";
187 server.Enabled = enable;
188 RefreshDisplay();
189 }
190 }
191
192 private void ServerContextMenu_Opening(object sender, CancelEventArgs e)
193 {
194 Server server = getClickedServer((ContextMenuStrip)sender);
195 ToggleEnableServerMenuItem.Text = server.Enabled ? "Disable" : "Enable";
196 }
197
198 private Server getClickedServer(ContextMenuStrip menu)
199 {
200 return ((ServerSummaryControl)menu.SourceControl).Server;
96 } 201 }
97 } 202 }
98 } 203 }