mirror of
https://github.com/MarkAHamann/MorseTrainer.git
synced 2026-02-06 07:24:20 +01:00
Rearrange Config a bit. Getting ready to add versioning.
This commit is contained in:
parent
90fbfa32bb
commit
e778b1d1b6
|
|
@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads the configuration at path. Creates a default if it doesn't exist.
|
||||
/// </summary>
|
||||
/// <param name="path">Path to the config file</param>
|
||||
/// <returns>A Config object</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a configuration to path
|
||||
/// </summary>
|
||||
/// <param name="config">A config object</param>
|
||||
/// <param name="path">Path to the config file</param>
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes the config file at path
|
||||
/// </summary>
|
||||
/// <param name="path">Path to the config file</param>
|
||||
public static void Delete(String path)
|
||||
{
|
||||
if (System.IO.File.Exists(path))
|
||||
{
|
||||
System.IO.File.Delete(path);
|
||||
}
|
||||
}
|
||||
|
||||
private int _version;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the version of the Config file
|
||||
/// </summary>
|
||||
public int Version
|
||||
{
|
||||
get
|
||||
{
|
||||
return _version;
|
||||
}
|
||||
}
|
||||
|
||||
private UInt16 _frequency;
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue