view ServerMonitor/Forms/ServerSummaryForm.cs @ 9:7127d5b5ac75

Code cleanup and comments
author Brad Greco <brad@bgreco.net>
date Mon, 08 Apr 2019 21:29:54 -0400
parents 052aa62cb42a
children 9e77c0dccb66
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)
        {
            Size size = Settings.Default.SummaryFormSize;
            if (size.Height > 0 && size.Width > 0)
                Size = size;

            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);
                    }
                }
            }
            monitor.CheckStatusChanged += Monitor_CheckStatusChanged;
            RefreshDisplay();
            CollectPrivateKeyPasswords();
        }

        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)
            {
                server.EnabledChanged -= Server_EnabledChanged;
                server.EnabledChanged += Server_EnabledChanged;
                ServerSummaryControl control = new ServerSummaryControl(server);
                control.ContextMenuStrip = ServerContextMenu;
                control.Click += ServerSummaryControl_Click;
                ServerPanel.Controls.Add(control);
            }
            UpdateIcon();
        }

        private void RefreshServer(Server server)
        {
            ServerPanel.Controls.Cast<ServerSummaryControl>().FirstOrDefault(c => c.Server == server).Refresh();
            UpdateIcon();
        }

        private void UpdateIcon()
        {
            CheckStatus status = monitor.Servers
                .Select(s => s.Enabled
                    ? s.Status
                    : s.KeyStatus == KeyStatus.NeedPassword ? CheckStatus.Warning : CheckStatus.Success)
                .DefaultIfEmpty(CheckStatus.Success)
                .Max();
            Icon = status.GetIcon();
            NotifyIcon.Icon = Icon;
        }

        private void CollectPrivateKeyPasswords()
        {
            List<string> triedKeys = new List<string>();
            foreach (Server server in monitor.Servers)
            {
                if (triedKeys.Contains(server.KeyFile))
                    continue;
                ServerForm.OpenPrivateKey(monitor, server, this);
                triedKeys.Add(server.KeyFile);
            }
        }

        private void Server_EnabledChanged(object sender, EventArgs e)
        {
            RefreshServer((Server)sender);
        }

        private void Monitor_CheckStatusChanged(object sender, CheckStatusChangedEventArgs e)
        {
            if (e.CheckResult != null)
            {
                RefreshServer(e.Check.Server);
            }
        }

        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";
                if (enable)
                {
                    ServerContextMenu.Close();
                    ServerForm.OpenPrivateKey(monitor, server, this);
                }
                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;
        }

        private void ServerSummaryForm_ResizeEnd(object sender, EventArgs e)
        {
            Settings.Default.SummaryFormSize = Size;
            Settings.Default.Save();
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == Win32Helpers.WM_SHOWMONITOR)
                ShowWindow();
            base.WndProc(ref m);
        }

        private void NotifyIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                ShowWindow();
            else if (e.Button == MouseButtons.Right)
                NotificationIconMenu.Show();
        }

        private void ShowWindow()
        {
            if (WindowState == FormWindowState.Minimized)
                WindowState = FormWindowState.Normal;
            Show();
            TopMost = true;
            TopMost = false;
            Activate();
        }

        private void NotificationIconMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem == ShowServerMonitorMenuItem)
                ShowWindow();
            else if (e.ClickedItem == ExitMenuItem)
                Application.Exit();
        }
    }
}