view ServerMonitor/Forms/SettingsForm.cs @ 11:75ca86e0862c

Add setting to hide to notification area.
author Brad Greco <brad@bgreco.net>
date Mon, 15 Apr 2019 19:24:25 -0400
parents 052aa62cb42a
children 7626b099aefd
line wrap: on
line source

using Microsoft.Win32;
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.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ServerMonitorApp
{
    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;
            foreach (ComboBox comboBox in new object[] { ErrorComboBox, WarningComboBox, InformationComboBox })
            {
                comboBox.DataSource = Enum.GetValues(typeof(FailAction));
                comboBox.Format += FailActionComboBox_Format;
            }
            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;
        }

        private bool GetAutorun()
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(autorunKey, false);
            string value = (string)key.GetValue(autorunName, string.Empty);
            return value.StartsWith(Application.ExecutablePath);
        }

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

        private void FailActionComboBox_Format(object sender, ListControlConvertEventArgs e)
        {
            e.Value = e.Value.ToString().Substring(0, 1) + Regex.Replace(e.Value.ToString(), "(\\B[A-Z])", " $1").ToLower().Substring(1);
        }

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

        private void CancelSettingsButton_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}