view ServerMonitor/Forms/ServerSummaryForm.cs @ 5:b6fe203af9d5

Private key passwords and validation
author Brad Greco <brad@bgreco.net>
date Thu, 28 Feb 2019 21:19:32 -0500
parents 3142e52cbe69
children c1dffaac66fa
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();
            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);
            }
        }

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

        private void CollectPrivateKeyPasswords()
        {
            foreach (Server server in monitor.Servers)
            {
                ServerForm.OpenPrivateKey(monitor, server, this);
            }
        }

        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;
        }
    }
}