view ServerMonitor/Controls/FileCheckControl.cs @ 19:b3128fe10d57

Add About form and license.
author Brad Greco <brad@bgreco.net>
date Wed, 29 May 2019 20:18:37 -0400
parents 7127d5b5ac75
children 832a07de0f90
line wrap: on
line source

using System;

namespace ServerMonitorApp
{
    /// <summary>Control for editing a file check.</summary>
    [CheckType(typeof(FileCheck))]
    public partial class FileCheckControl : CheckControl
    {
        public FileCheckControl()
        {
            InitializeComponent();
        }

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

        /// <summary>Sets the values of the controls from a check's properties.</summary>
        public override void LoadCheck(Check check1)
        {
            FileCheck check = (FileCheck)check1;
            FileTextBox.Text = check.File;
            FileSizeCheckBox.Checked = check.CheckFileSize;
            FileSizeComparisonComboBox.SelectedIndex = check.FileSizeLessThan ? 0 : 1;
            FileSizeTextBox.Text = check.FileSizeInSelectedUnits.ToString();
            FileSizeUnitsComboBox.SelectedIndex = (int)check.FileSizeUnits;
            DateModifiedCheckBox.Checked = check.CheckDateModified;
            DateModifiedComparisonComboBox.SelectedIndex = check.DateModifiedOlderThan ? 0 : 1;
            DateModifiedTextBox.Text = check.DateModified.ToString();
            DateModifiedUnitsComboBox.SelectedIndex = (int)check.DateModifiedUnits;

            // Blank out text boxes that are disabled and have a blank-looking value.
            if (FileSizeTextBox.Text == "0" && !check.CheckFileSize)
                FileSizeTextBox.Clear();
            if (DateModifiedTextBox.Text == "0" && !check.CheckDateModified)
                DateModifiedTextBox.Clear();
        }

        /// <summary>Updates the properties of a check from user input.</summary>
        public override void UpdateCheck(Check check1)
        {
            FileCheck check = (FileCheck)check1;
            check.File = FileTextBox.Text;
            check.CheckFileSize = FileSizeCheckBox.Checked;
            check.FileSizeLessThan = FileSizeComparisonComboBox.SelectedIndex == 0;
            try
            {
                check.FileSizeInSelectedUnits = double.Parse(FileSizeTextBox.Text);
            }
            catch
            {
                if (check.CheckFileSize)
                    throw new UpdateCheckException("File size must be numeric.");
                else
                    check.FileSizeInSelectedUnits = 0;
            }
            check.FileSizeUnits = (SizeUnits)FileSizeUnitsComboBox.SelectedIndex;
            check.CheckDateModified = DateModifiedCheckBox.Checked;
            check.DateModifiedOlderThan = DateModifiedComparisonComboBox.SelectedIndex == 0;
            try
            {
                check.DateModified = double.Parse(DateModifiedTextBox.Text);
            }
            catch
            {
                if (check.CheckDateModified)
                    throw new UpdateCheckException("Date modified must be numeric.");
                else
                    check.DateModified = 0;
            }
            check.DateModifiedUnits = (TimeUnits)DateModifiedUnitsComboBox.SelectedIndex;
        }
    }
}