view ServerMonitor/Forms/SettingsForm.cs @ 16:7626b099aefd

More comments.
author Brad Greco <brad@bgreco.net>
date Tue, 30 Apr 2019 20:40:58 -0400
parents 75ca86e0862c
children f6235dc0a8ec
line wrap: on
line source

using Microsoft.Win32;
using ServerMonitorApp.Properties;
using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace ServerMonitorApp
{
    /// <summary>Application settings form.</summary>
    public partial class SettingsForm : Form
    {
        private readonly string autorunKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
        private readonly string autorunName = "ServerMonitor";

        public SettingsForm()
        {
            InitializeComponent();
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            Icon = Resources.icon;
            // Populate each action combo box with the available actions to perform on failure.
            foreach (ComboBox comboBox in new object[] { ErrorComboBox, WarningComboBox, InformationComboBox })
            {
                comboBox.DataSource = Enum.GetValues(typeof(FailAction));
                comboBox.Format += FailActionComboBox_Format;
            }
            // Initialize controls with current settings.
            AutorunCheckBox.Checked = GetAutorun();
            KeepRunningCheckBox.Checked = Settings.Default.HideToNotificationArea;
            KeepLogDaysInput.Value = Settings.Default.KeepLogDays;
            ErrorComboBox.SelectedItem = Settings.Default.ErrorAction;
            WarningComboBox.SelectedItem = Settings.Default.WarningAction;
            InformationComboBox.SelectedItem = Settings.Default.InformationAction;
        }

        /// <summary>Gets whether an autorun registry key for this program exists.</summary>
        /// <returns>Whether an autorun registry key for this program exists</returns>
        private bool GetAutorun()
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(autorunKey, false);
            string value = (string)key.GetValue(autorunName, string.Empty);
            return value.StartsWith(Application.ExecutablePath);
        }

        /// <summary>Sets whether this program should automatically start with Windows.</summary>
        /// <param name="autorun">Whether autorun should be enabled or disabled.</param>
        private void SetAutorun(bool autorun)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(autorunKey, true);
            if (autorun)
                key.SetValue(autorunName, Application.ExecutablePath.ToString());
            else
                key.DeleteValue(autorunName, false);
        }

        /// <summary>Shows a human readable description of possible check failure actions in the combo box.</summary>
        private void FailActionComboBox_Format(object sender, ListControlConvertEventArgs e)
        {
            // Transform the "CamelCase" enum name in to a "Camel case" name, adding
            // spaces and making all characters besides the first lower case.
            e.Value = e.Value.ToString().Substring(0, 1) + Regex.Replace(e.Value.ToString(), "(\\B[A-Z])", " $1").ToLower().Substring(1);
        }

        /// <summary>Saves the user settings and closes the form.</summary>
        private void OkButton_Click(object sender, EventArgs e)
        {
            Settings.Default.HideToNotificationArea = KeepRunningCheckBox.Checked;
            Settings.Default.KeepLogDays = (int)KeepLogDaysInput.Value;
            Settings.Default.ErrorAction = (FailAction)ErrorComboBox.SelectedItem;
            Settings.Default.WarningAction = (FailAction)WarningComboBox.SelectedItem;
            Settings.Default.InformationAction = (FailAction)InformationComboBox.SelectedItem;
            Settings.Default.Save();
            SetAutorun(AutorunCheckBox.Checked);
            Close();
        }

        /// <summary>Closes the form without saving settings.</summary>
        private void CancelSettingsButton_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}