EduNetworkBuilder/EduNetworkBuilder/NBSettings.cs

164 lines
6.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Reflection;
using System.Xml.Serialization;
using System.IO;
using System.Windows.Forms;
namespace EduNetworkBuilder
{
/// <summary>
/// We have settings that need to be loaded and saved. Mono has issues with
/// properties.settings.default stuff, so we need to load/save from an xml file
/// when we are using mono. So this class does that
/// </summary>
public class NBSettings
{
public string LastPath = "";
public List<string> ScoreList = new List<string>();
public bool AutoStartPuzzles = true;
public string ChosenLanguage = "en";
public bool LanguageHasBeenChosen = false;
public int MainWindowX = -1;
public int MainWindowY = -1;
public int MainWindowWidth = -1;
public int MainWindowHeight = -1;
public string ProcessingLevel = "none";
public NBSettings()
{
//We need this for reflection
}
public NBSettings(NBSettings fromSettings)
{
if (fromSettings == null) return; //Break if we are going to error out.
Duplicate(fromSettings, this);
}
static void Duplicate(NBSettings FromClass, NBSettings ToClass)
{
if (FromClass == null) return; //Break if we are going to error out.
if (ToClass == null) return; //Break if we are going to error out.
//
ToClass.LastPath = FromClass.LastPath;
ToClass.AutoStartPuzzles = FromClass.AutoStartPuzzles;
ToClass.ChosenLanguage = FromClass.ChosenLanguage;
ToClass.LanguageHasBeenChosen = FromClass.LanguageHasBeenChosen;
ToClass.MainWindowX = FromClass.MainWindowX;
ToClass.MainWindowY = FromClass.MainWindowY;
ToClass.MainWindowHeight = FromClass.MainWindowHeight;
ToClass.MainWindowWidth = FromClass.MainWindowWidth;
foreach (string one in FromClass.ScoreList)
{
if (!ToClass.ScoreList.Contains(one))
ToClass.ScoreList.Add(one);
}
}
public NBSettings(bool UsingMono)
{
if(UsingMono)
{
try
{
//Try loading from xml file
string filename = GetFilename();
if (File.Exists(filename))
{
XmlSerializer sr = new XmlSerializer(this.GetType());
TextReader Reader = new StreamReader(filename);
NBSettings settings = (NBSettings)sr.Deserialize(Reader);
Duplicate(settings, this);
Reader.Close();
}
}
catch (Exception e)
{
MessageBox.Show("Error Loading Settings:\n" + e.ToString(),"Error Loading Settings");
}
}
else
{
LastPath = Properties.Settings.Default.LastPath;
AutoStartPuzzles = Properties.Settings.Default.AutoStartPuzzles;
ChosenLanguage = Properties.Settings.Default.ChosenLanguage;
LanguageHasBeenChosen = Properties.Settings.Default.LanguageHasBeenChosen;
MainWindowX = Properties.Settings.Default.MainWindowX;
MainWindowY = Properties.Settings.Default.MainWindowY;
MainWindowHeight = Properties.Settings.Default.MainWindowHeight;
MainWindowWidth = Properties.Settings.Default.MainWindowWidth;
foreach(string one in Properties.Settings.Default.ScoreList)
{
if (!ScoreList.Contains(one))
ScoreList.Add(one);
}
}
}
public static string GetFilename()
{
string BaseDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string FileName = "EduNetworkBuilder_config.xml";
return Path.Combine(BaseDir, FileName);
}
public void Save()
{
Save(NB.IsRunningOnMono());
}
public void Save(bool UsingMono)
{
if(UsingMono)
{
//Save it to an XML file.
try
{
string filename = GetFilename();
if(!Directory.Exists(Path.GetDirectoryName(filename)))
{
Directory.CreateDirectory(Path.GetDirectoryName(filename));
}
XmlSerializer sr = new XmlSerializer(this.GetType());
TextWriter writer = new StreamWriter(filename);
sr.Serialize(writer, this);
writer.Close();
}
catch(Exception e)
{
MessageBox.Show("ERROR Saving Settings:\n" + e.ToString(),"Error saving settings.");
}
}
else
{
if (Properties.Settings.Default.ScoreList == null)
Properties.Settings.Default.ScoreList = new System.Collections.Specialized.StringCollection();
Properties.Settings.Default.LastPath = LastPath;
Properties.Settings.Default.AutoStartPuzzles = AutoStartPuzzles;
Properties.Settings.Default.ChosenLanguage = ChosenLanguage;
Properties.Settings.Default.LanguageHasBeenChosen = LanguageHasBeenChosen;
Properties.Settings.Default.MainWindowX = MainWindowX;
Properties.Settings.Default.MainWindowY = MainWindowY;
Properties.Settings.Default.MainWindowHeight = MainWindowHeight;
Properties.Settings.Default.MainWindowWidth = MainWindowWidth;
Properties.Settings.Default.ScoreList.Clear();
foreach(string One in ScoreList)
{
Properties.Settings.Default.ScoreList.Add(One);
}
Properties.Settings.Default.Save();
}
}
}
}