Mercurial > servermonitor
comparison ServerMonitor/Forms/SettingsForm.cs @ 29:f6235dc0a8ec
Add ability to play a sound on check failure.
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Fri, 14 Jun 2019 21:01:55 -0400 |
parents | 7626b099aefd |
children | 7645122aa7a9 |
comparison
equal
deleted
inserted
replaced
28:437442cd8090 | 29:f6235dc0a8ec |
---|---|
1 using Microsoft.Win32; | 1 using Microsoft.Win32; |
2 using ServerMonitorApp.Properties; | 2 using ServerMonitorApp.Properties; |
3 using System; | 3 using System; |
4 using System.IO; | |
5 using System.Linq; | |
4 using System.Text.RegularExpressions; | 6 using System.Text.RegularExpressions; |
5 using System.Windows.Forms; | 7 using System.Windows.Forms; |
6 | 8 |
7 namespace ServerMonitorApp | 9 namespace ServerMonitorApp |
8 { | 10 { |
9 /// <summary>Application settings form.</summary> | 11 /// <summary>Application settings form.</summary> |
10 public partial class SettingsForm : Form | 12 public partial class SettingsForm : Form |
11 { | 13 { |
12 private readonly string autorunKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; | 14 private readonly string autorunKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; |
13 private readonly string autorunName = "ServerMonitor"; | 15 private readonly string autorunName = "ServerMonitor"; |
16 private readonly string[] soundOptions = { "None", "Windows error", "Custom sound..." }; | |
17 private readonly int customSoundIndex = 2; | |
14 | 18 |
15 public SettingsForm() | 19 public SettingsForm() |
16 { | 20 { |
17 InitializeComponent(); | 21 InitializeComponent(); |
18 } | 22 } |
24 foreach (ComboBox comboBox in new object[] { ErrorComboBox, WarningComboBox, InformationComboBox }) | 28 foreach (ComboBox comboBox in new object[] { ErrorComboBox, WarningComboBox, InformationComboBox }) |
25 { | 29 { |
26 comboBox.DataSource = Enum.GetValues(typeof(FailAction)); | 30 comboBox.DataSource = Enum.GetValues(typeof(FailAction)); |
27 comboBox.Format += FailActionComboBox_Format; | 31 comboBox.Format += FailActionComboBox_Format; |
28 } | 32 } |
33 // Populate the sound options combo boxes. | |
34 foreach (ComboBox comboBox in new object[] { ErrorSoundComboBox, WarningSoundComboBox, InformationSoundComboBox }) | |
35 { | |
36 comboBox.Items.AddRange(soundOptions); | |
37 // Add the already-selected custom sound, if any. | |
38 if (getSoundSettingIndex(comboBox) == customSoundIndex) | |
39 comboBox.Items.Insert(customSoundIndex, getSoundSetting(comboBox)); | |
40 } | |
29 // Initialize controls with current settings. | 41 // Initialize controls with current settings. |
30 AutorunCheckBox.Checked = GetAutorun(); | 42 AutorunCheckBox.Checked = GetAutorun(); |
31 KeepRunningCheckBox.Checked = Settings.Default.HideToNotificationArea; | 43 KeepRunningCheckBox.Checked = Settings.Default.HideToNotificationArea; |
32 KeepLogDaysInput.Value = Settings.Default.KeepLogDays; | 44 KeepLogDaysInput.Value = Settings.Default.KeepLogDays; |
33 ErrorComboBox.SelectedItem = Settings.Default.ErrorAction; | 45 ErrorComboBox.SelectedItem = Settings.Default.ErrorAction; |
34 WarningComboBox.SelectedItem = Settings.Default.WarningAction; | 46 WarningComboBox.SelectedItem = Settings.Default.WarningAction; |
35 InformationComboBox.SelectedItem = Settings.Default.InformationAction; | 47 InformationComboBox.SelectedItem = Settings.Default.InformationAction; |
48 ErrorSoundComboBox.SelectedIndex = getSoundSettingIndex(ErrorSoundComboBox); | |
49 WarningSoundComboBox.SelectedIndex = getSoundSettingIndex(WarningSoundComboBox); | |
50 InformationSoundComboBox.SelectedIndex = getSoundSettingIndex(InformationSoundComboBox); | |
36 } | 51 } |
37 | 52 |
38 /// <summary>Gets whether an autorun registry key for this program exists.</summary> | 53 /// <summary>Gets whether an autorun registry key for this program exists.</summary> |
39 /// <returns>Whether an autorun registry key for this program exists</returns> | 54 /// <returns>Whether an autorun registry key for this program exists</returns> |
40 private bool GetAutorun() | 55 private bool GetAutorun() |
69 Settings.Default.HideToNotificationArea = KeepRunningCheckBox.Checked; | 84 Settings.Default.HideToNotificationArea = KeepRunningCheckBox.Checked; |
70 Settings.Default.KeepLogDays = (int)KeepLogDaysInput.Value; | 85 Settings.Default.KeepLogDays = (int)KeepLogDaysInput.Value; |
71 Settings.Default.ErrorAction = (FailAction)ErrorComboBox.SelectedItem; | 86 Settings.Default.ErrorAction = (FailAction)ErrorComboBox.SelectedItem; |
72 Settings.Default.WarningAction = (FailAction)WarningComboBox.SelectedItem; | 87 Settings.Default.WarningAction = (FailAction)WarningComboBox.SelectedItem; |
73 Settings.Default.InformationAction = (FailAction)InformationComboBox.SelectedItem; | 88 Settings.Default.InformationAction = (FailAction)InformationComboBox.SelectedItem; |
89 Settings.Default.ErrorSound = getSelectedSound(ErrorSoundComboBox); | |
90 Settings.Default.WarningSound = getSelectedSound(WarningSoundComboBox); | |
91 Settings.Default.InformationSound = getSelectedSound(InformationSoundComboBox); | |
74 Settings.Default.Save(); | 92 Settings.Default.Save(); |
75 SetAutorun(AutorunCheckBox.Checked); | 93 SetAutorun(AutorunCheckBox.Checked); |
76 Close(); | 94 Close(); |
77 } | 95 } |
78 | 96 |
79 /// <summary>Closes the form without saving settings.</summary> | 97 /// <summary>Closes the form without saving settings.</summary> |
80 private void CancelSettingsButton_Click(object sender, EventArgs e) | 98 private void CancelSettingsButton_Click(object sender, EventArgs e) |
81 { | 99 { |
82 Close(); | 100 Close(); |
83 } | 101 } |
102 | |
103 /// <summary>Shows a file browser when "Custom cound..." is selected.</summary> | |
104 private void SoundComboBox_SelectedIndexChanged(object sender, EventArgs e) | |
105 { | |
106 ComboBox comboBox = (ComboBox)sender; | |
107 // Show the dialog if the last item is selected. | |
108 if (comboBox.SelectedIndex == comboBox.Items.Count - 1) | |
109 { | |
110 OpenFileDialog dialog = new OpenFileDialog() | |
111 { | |
112 Title = "Select audio file", | |
113 Filter = "Audio files (*.wav;*.mp3;*.wma,*.aif,*.aiff,*.asf,*.aac,*.m4a)|*.wav;*.mp3;*.wma,*.aif,*.aiff,*.asf,*.aac,*.m4a|All files (*.*)|*.*" | |
114 }; | |
115 if (dialog.ShowDialog() == DialogResult.OK) | |
116 { | |
117 // Remove the previous custom sound, if any. | |
118 if (comboBox.Items.Count > soundOptions.Length) | |
119 comboBox.Items.RemoveAt(customSoundIndex); | |
120 // Add the custom sound and mark it as selected. | |
121 comboBox.Items.Insert(customSoundIndex, dialog.FileName); | |
122 comboBox.SelectedIndex = customSoundIndex; | |
123 } | |
124 else | |
125 { | |
126 // Restore the existing setting if the dialog was cancelled. | |
127 comboBox.SelectedIndex = getSoundSettingIndex(comboBox); | |
128 } | |
129 } | |
130 } | |
131 | |
132 /// <summary>Shows only the filename of the custom sound.</summary> | |
133 private void SoundComboBox_Format(object sender, ListControlConvertEventArgs e) | |
134 { | |
135 ComboBox comboBox = (ComboBox)sender; | |
136 if (!soundOptions.Contains(e.Value)) | |
137 e.Value = Path.GetFileName(e.Value.ToString()); | |
138 } | |
139 | |
140 /// <summary>Returns the selected sound for a combo box as stored in program settings.</summary> | |
141 /// <param name="comboBox">The combo box to read from.</param> | |
142 /// <returns> | |
143 /// The index ("0" or "1") of the selected option, or the | |
144 /// path to the custom sound if a custom sound is selected. | |
145 /// </returns> | |
146 private string getSelectedSound(ComboBox comboBox) | |
147 { | |
148 return comboBox.SelectedIndex == customSoundIndex ? comboBox.SelectedItem.ToString() : comboBox.SelectedIndex.ToString(); | |
149 } | |
150 | |
151 /// <summary>Reads the sound setting for a combobox from program settings.</summary> | |
152 /// <param name="comboBox">The combo box to read data for.</param> | |
153 /// <returns> | |
154 /// The index ("0" or "1") of the selected option, or the | |
155 /// path to the custom sound if a custom sound is selected. | |
156 /// </returns> | |
157 private string getSoundSetting(ComboBox comboBox) | |
158 { | |
159 string setting = string.Empty; | |
160 if (comboBox == ErrorSoundComboBox) | |
161 setting = Settings.Default.ErrorSound; | |
162 if (comboBox == WarningSoundComboBox) | |
163 setting = Settings.Default.WarningSound; | |
164 if (comboBox == InformationSoundComboBox) | |
165 setting = Settings.Default.InformationSound; | |
166 return setting; | |
167 } | |
168 | |
169 /// <summary>Gets the index of the combo box item that corresponds to its program sound setting.</summary> | |
170 /// <param name="comboBox">The combo box to read data for.</param> | |
171 /// <returns>The index of the combo box item that corresponds to its program sound setting.</returns> | |
172 private int getSoundSettingIndex(ComboBox comboBox) | |
173 { | |
174 // If the string is not numeric, it contains the path to the custom sound. | |
175 return int.TryParse(getSoundSetting(comboBox), out int index) ? index : customSoundIndex; | |
176 } | |
177 | |
84 } | 178 } |
85 } | 179 } |