Mercurial > servermonitor
view 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 |
line wrap: on
line source
using ServerMonitorApp.Properties; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace ServerMonitorApp { public partial class ServerSummaryForm : Form { private readonly Dictionary<Server, ServerForm> serverForms = new Dictionary<Server, ServerForm>(); private ServerMonitor monitor; public ServerSummaryForm() { InitializeComponent(); } public void AlertServerForm(Check check) { bool existingForm = serverForms.ContainsKey(check.Server); ServerForm form = ShowServerForm(check.Server, false); Win32Helpers.FlashWindowEx(form); if (!existingForm) { form.ShowLog(check); } } public void ShowBalloon(CheckResult result) { string title = string.Format("{0}: {1} failed", result.Check.Server.Name, result.Check.Name); NotifyIcon.Tag = result; NotifyIcon.ShowBalloonTip(30000, title, result.Message, GetToolTipIcon(result.CheckStatus)); } private void ServerSummaryForm_Load(object sender, EventArgs e) { Helpers.FormatImageButton(NewServerButton); Helpers.FormatImageButton(SettingsButton); monitor = new ServerMonitor(this); while (true) { try { monitor.LoadServers(); break; } catch (Exception ex) { DialogResult result = MessageBox.Show("Could not load servers. Please fix or delete the file " + monitor.ConfigFile + Environment.NewLine + Environment.NewLine + "Error details:" + Environment.NewLine + ex.GetAllMessages(), "Error loading servers", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error); if (result == DialogResult.Cancel) { Environment.Exit(1); } } } NotifyIcon.Icon = new Icon(Icon, 16, 16); monitor.CheckStatusChanged += Monitor_CheckStatusChanged; RefreshDisplay(); } private ServerForm ShowServerForm(Server server, bool activate = true) { bool isNewServer = false; if (server == null) { server = new Server(); monitor.AddServer(server); isNewServer = true; } if (serverForms.TryGetValue(server, out ServerForm form)) { if (activate) form.Activate(); } else { form = new ServerForm(monitor, server, isNewServer); serverForms[server] = form; form.FormClosing += ServerForm_FormClosing; form.Show(activate); } return form; } private void RefreshDisplay() { ServerPanel.Controls.Clear(); foreach (Server server in monitor.Servers) { ServerSummaryControl control = new ServerSummaryControl(server); control.ContextMenuStrip = ServerContextMenu; control.Click += ServerSummaryControl_Click; ServerPanel.Controls.Add(control); } } private void Monitor_CheckStatusChanged(object sender, CheckStatusChangedEventArgs e) { if (e.CheckResult != null) ServerPanel.Controls.Cast<ServerSummaryControl>().FirstOrDefault(c => c.Server == e.Check.Server).Refresh(); } private ToolTipIcon GetToolTipIcon(CheckStatus status) { switch (status) { case CheckStatus.Error: return ToolTipIcon.Error; case CheckStatus.Warning: return ToolTipIcon.Warning; case CheckStatus.Information: return ToolTipIcon.Info; default: return ToolTipIcon.None; } } private void ServerSummaryControl_Click(object sender, EventArgs e) { ShowServerForm(((ServerSummaryControl)sender).Server); } private void ServerForm_FormClosing(object sender, FormClosingEventArgs e) { ServerForm form = (ServerForm)sender; form.FormClosing -= ServerForm_FormClosing; Server server = form.Server; serverForms.Remove(form.Server); if (server.IsEmpty()) { monitor.DeleteServer(server); } RefreshDisplay(); } private void NewServerButton_Click(object sender, EventArgs e) { ShowServerForm(null); } private void ServerSummaryForm_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.None || e.CloseReason == CloseReason.UserClosing) { Hide(); e.Cancel = true; } } private void SettingsButton_Click(object sender, EventArgs e) { new SettingsForm().Show(); } private void NotifyIcon_BalloonTipClicked(object sender, EventArgs e) { CheckResult result = (CheckResult)(sender as NotifyIcon).Tag; ServerForm form = ShowServerForm(result.Check.Server); form.ShowLog(result.Check); form.WindowState = FormWindowState.Normal; } private void ServerContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { Server server = getClickedServer((ContextMenuStrip)e.ClickedItem.Owner); if (e.ClickedItem == DeleteServerMenuItem) { ServerContextMenu.Close(); DialogResult result = MessageBox.Show( string.Format("The server \"{0}\" and its {1} {2} will be deleted.", server.Name, server.Checks.Count, server.Checks.Count == 1 ? "check" : "checks"), "Delete server", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning ); if (result == DialogResult.OK) { monitor.DeleteServer(server); RefreshDisplay(); } } else if (e.ClickedItem == ToggleEnableServerMenuItem) { bool enable = ToggleEnableServerMenuItem.Text == "Enable"; server.Enabled = enable; RefreshDisplay(); } } private void ServerContextMenu_Opening(object sender, CancelEventArgs e) { Server server = getClickedServer((ContextMenuStrip)sender); ToggleEnableServerMenuItem.Text = server.Enabled ? "Disable" : "Enable"; } private Server getClickedServer(ContextMenuStrip menu) { return ((ServerSummaryControl)menu.SourceControl).Server; } } }