view ServerMonitor/Forms/SettingsForm.cs @ 10:9e77c0dccb66

Add update checker
author Brad Greco <brad@bgreco.net>
date Mon, 08 Apr 2019 21:31:03 -0400
parents 052aa62cb42a
children 75ca86e0862c
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();
            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.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();
        }
    }
}