diff 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 diff
--- a/ServerMonitor/Forms/ServerSummaryForm.cs	Fri Jan 11 22:34:18 2019 -0500
+++ b/ServerMonitor/Forms/ServerSummaryForm.cs	Sun Feb 10 20:51:26 2019 -0500
@@ -1,4 +1,5 @@
-using System;
+using ServerMonitorApp.Properties;
+using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
@@ -19,10 +20,29 @@
             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);
-            monitor = new ServerMonitor();
+            Helpers.FormatImageButton(SettingsButton);
+            monitor = new ServerMonitor(this);
             while (true)
             {
                 try
@@ -41,21 +61,12 @@
                     }
                 }
             }
+            NotifyIcon.Icon = new Icon(Icon, 16, 16);
+            monitor.CheckStatusChanged += Monitor_CheckStatusChanged;
             RefreshDisplay();
         }
 
-        private void RefreshDisplay()
-        {
-            ServerPanel.Controls.Clear();
-            foreach (Server server in monitor.Servers)
-            {
-                ServerSummaryControl control = new ServerSummaryControl(server);
-                control.Click += ServerSummaryControl_Click;
-                ServerPanel.Controls.Add(control);
-            }
-        }
-
-        private void ShowServerForm(Server server)
+        private ServerForm ShowServerForm(Server server, bool activate = true)
         {
             bool isNewServer = false;
             if (server == null)
@@ -66,14 +77,45 @@
             }
             if (serverForms.TryGetValue(server, out ServerForm form))
             {
-                form.Activate();
+                if (activate)
+                    form.Activate();
             }
             else
             {
                 form = new ServerForm(monitor, server, isNewServer);
                 serverForms[server] = form;
                 form.FormClosing += ServerForm_FormClosing;
-                form.Show();
+                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;
             }
         }
 
@@ -86,7 +128,12 @@
         {
             ServerForm form = (ServerForm)sender;
             form.FormClosing -= ServerForm_FormClosing;
+            Server server = form.Server;
             serverForms.Remove(form.Server);
+            if (server.IsEmpty())
+            {
+                monitor.DeleteServer(server);
+            }
             RefreshDisplay();
         }
 
@@ -94,5 +141,63 @@
         {
             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;
+        }
     }
 }