view ServerMonitor/Controls/HttpCheckControl.cs @ 32:2342e9459444

Fix schedule times not being saved.
author Brad Greco <brad@bgreco.net>
date Sun, 16 Jun 2019 16:55:14 -0400
parents b713b9db4c82
children
line wrap: on
line source

using System;

namespace ServerMonitorApp
{
    /// <summary>Control for editing an HTTP check.</summary>
    [CheckType(typeof(HttpCheck))]
    public partial class HttpCheckControl : CheckControl
    {
        public HttpCheckControl()
        {
            InitializeComponent();
        }

        private void HttpCheckControl_Load(object sender, System.EventArgs e)
        {
            // Initialize the combo boxes to non-empty values.
            MethodComboBox.SelectedIndex = 0;
        }

        /// <summary>Sets the values of the controls from a check's properties.</summary>
        public override void LoadCheck(Check check1)
        {
            HttpCheck check = (HttpCheck)check1;
            UrlTextBox.Text = check.Url;
            MethodComboBox.SelectedItem = check.Method ?? "GET";
            ResponseCodeCheckBox.Checked = check.CheckResponseCode;
            ResponseCodeTextBox.Text = check.ResponseCode.ToString();
            ResponseLengthCheckbox.Checked = check.CheckResponseLength;
            ResponseLengthMinTextBox.Text = check.ResponseLengthMin;
            ResponseLengthMaxTextBox.Text = check.ResponseLengthMax;
            ResponseBodyCheckBox.Checked = check.CheckResponseBody;
            ResponseBodyComboBox.SelectedIndex = (int)check.ResponseBodyMatchType;
            ResponseBodyTextBox.Text = check.ResponseBodyPattern;
            ResponseBodyRegexCheckBox.Checked = check.ResponseBodyUseRegex;
        }

        /// <summary>Updates the properties of a check from user input.</summary>
        public override void UpdateCheck(Check check1)
        {
            HttpCheck check = (HttpCheck)check1;
            check.Url = UrlTextBox.Text.Trim();
            check.Method = MethodComboBox.SelectedItem.ToString();
            check.CheckResponseCode = ResponseCodeCheckBox.Checked;
            try
            {
                check.ResponseCode = int.Parse(ResponseCodeTextBox.Text);
            }
            catch
            {
                throw new UpdateCheckException("Response code must be an integer.");
            }
            check.CheckResponseLength = ResponseLengthCheckbox.Checked;
            check.ResponseLengthMin = ResponseLengthMinTextBox.Text;
            check.ResponseLengthMax = ResponseLengthMaxTextBox.Text;
            check.CheckResponseBody = ResponseBodyCheckBox.Checked;
            check.ResponseBodyMatchType = (MatchType)ResponseBodyComboBox.SelectedIndex;
            check.ResponseBodyPattern = ResponseBodyTextBox.Text;
            check.ResponseBodyUseRegex = ResponseBodyRegexCheckBox.Checked;
        }

        private void MethodComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (MethodComboBox.SelectedIndex == 1)
            {
                ResponseBodyCheckBox.Checked = false;
                ResponseBodyCheckBox.Enabled = false;
            }
            else
            {
                ResponseBodyCheckBox.Enabled = true;
            }
        }
    }
}