From e778b1d1b6115bf74d5fb1be79dedc52bb48a740 Mon Sep 17 00:00:00 2001 From: Mark Hamann Date: Fri, 26 Feb 2016 07:26:24 -0800 Subject: [PATCH] Rearrange Config a bit. Getting ready to add versioning. --- MorseTrainer/Config.cs | 90 ++++++++++++++++++++++++++++++ MorseTrainer/Form1.cs | 124 ++++++++++++----------------------------- 2 files changed, 127 insertions(+), 87 deletions(-) diff --git a/MorseTrainer/Config.cs b/MorseTrainer/Config.cs index 0b929fc..87bb61d 100644 --- a/MorseTrainer/Config.cs +++ b/MorseTrainer/Config.cs @@ -31,6 +31,7 @@ namespace MorseTrainer public class Config { public static readonly Config Default; + public const int CurrentVersion = 1; static Config() { @@ -47,6 +48,95 @@ namespace MorseTrainer Default._kochIndex = 1; Default._favorNew = true; Default._custom = ""; + + Default._version = CurrentVersion; + } + + /// + /// Loads the configuration at path. Creates a default if it doesn't exist. + /// + /// Path to the config file + /// A Config object + public static Config Load(String path) + { + Config config = null; + + if (!System.IO.File.Exists(path)) + { + Config.Save(Config.Default, path); + } + + System.IO.Stream stream = null; + try + { + stream = System.IO.File.Open(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None); + System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); + config = (Config)bf.Deserialize(stream); + } + catch (Exception) + { + throw; + } + finally + { + if (stream != null) + { + stream.Close(); + } + } + return config; + } + + /// + /// Saves a configuration to path + /// + /// A config object + /// Path to the config file + public static void Save(Config config, String path) + { + System.IO.Stream stream = null; + try + { + stream = System.IO.File.Open(path, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None); + System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); + bf.Serialize(stream, config); + } + catch (Exception ex) + { + throw ex; + } + finally + { + if (stream != null) + { + stream.Close(); + } + } + } + + /// + /// Deletes the config file at path + /// + /// Path to the config file + public static void Delete(String path) + { + if (System.IO.File.Exists(path)) + { + System.IO.File.Delete(path); + } + } + + private int _version; + + /// + /// Gets the version of the Config file + /// + public int Version + { + get + { + return _version; + } } private UInt16 _frequency; diff --git a/MorseTrainer/Form1.cs b/MorseTrainer/Form1.cs index 175d808..c135f4d 100644 --- a/MorseTrainer/Form1.cs +++ b/MorseTrainer/Form1.cs @@ -41,6 +41,7 @@ namespace MorseTrainer private static readonly String CONFIG_FILE_NAME = "config.cfg"; + #region Form Maintenance public Form1() { InitializeComponent(); @@ -127,71 +128,23 @@ namespace MorseTrainer cmbKoch.Items.Add(MorseInfo.ExpandProsigns(Koch.Order[i].ToString())); } - Config config = LoadConfig(); + Config config = Config.Load(CONFIG_FILE_NAME); ApplyConfig(config); } + private void Form1_FormClosed(object sender, FormClosedEventArgs e) + { + Config.Save(ExtractConfig(), CONFIG_FILE_NAME); + if (_runner.IsRunning) + { + _runner.RequestStop(); + } + _player.CloseAndJoin(); + } + + #endregion + #region Configuration - private void DeleteConfigFile() - { - if (System.IO.File.Exists(CONFIG_FILE_NAME)) - { - System.IO.File.Delete(CONFIG_FILE_NAME); - } - } - - private Config LoadConfig() - { - Config config = null; - - if (!System.IO.File.Exists(CONFIG_FILE_NAME)) - { - SaveConfig(Config.Default); - } - - System.IO.Stream stream = null; - try - { - stream = System.IO.File.Open(CONFIG_FILE_NAME, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None); - System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); - config = (Config)bf.Deserialize(stream); - } - catch (Exception) - { - throw; - } - finally - { - if (stream != null) - { - stream.Close(); - } - } - return config; - } - - private void SaveConfig(Config config) - { - System.IO.Stream stream = null; - try - { - stream = System.IO.File.Open(CONFIG_FILE_NAME, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None); - System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); - bf.Serialize(stream, config); - } - catch (Exception ex) - { - throw ex; - } - finally - { - if (stream != null) - { - stream.Close(); - } - } - } - private void ApplyConfig(Config config) { Frequency = config.Frequency; @@ -357,6 +310,7 @@ namespace MorseTrainer } #endregion + #region Analysis private void Analyze() { String sent = _player.Sent; @@ -364,6 +318,8 @@ namespace MorseTrainer _analyzer.Analyze(sent, recorded); } + #endregion + #region User Interface #region Helper Functions @@ -1247,6 +1203,24 @@ namespace MorseTrainer #endregion + #region Context Menu Items + private void mnuContextRestoreDefaults_Click(object sender, EventArgs e) + { + Config.Delete(CONFIG_FILE_NAME); + ApplyConfig(Config.Load(CONFIG_FILE_NAME)); + } + + private void mnuContextSetProsignKeys_Click(object sender, EventArgs e) + { + + } + + private void mnuContextAbout_Click(object sender, EventArgs e) + { + About about = new About(); + about.ShowDialog(); + } + #endregion #region User Key private bool UserKey(char key) @@ -1269,6 +1243,7 @@ namespace MorseTrainer } #endregion + #region Private Fields private ToneGenerator _toneGenerator; private CharGenerator _charGenerator; private WordToToneBuilder _builder; @@ -1278,31 +1253,6 @@ namespace MorseTrainer private StringBuilder _recorded; private WaveStream _pendingWavestream; - private void Form1_FormClosed(object sender, FormClosedEventArgs e) - { - SaveConfig(ExtractConfig()); - if (_runner.IsRunning) - { - _runner.RequestStop(); - } - _player.CloseAndJoin(); - } - - private void mnuContextRestoreDefaults_Click(object sender, EventArgs e) - { - DeleteConfigFile(); - ApplyConfig(LoadConfig()); - } - - private void mnuContextSetProsignKeys_Click(object sender, EventArgs e) - { - - } - - private void mnuContextAbout_Click(object sender, EventArgs e) - { - About about = new About(); - about.ShowDialog(); - } + #endregion } }