using System; using System.IO; using System.Text; using System.Xml.Serialization; namespace TBF { /// /// Class to be serialized to an XML file... /// Public members are local settings. /// These settings are modified by dialogs invoked by menu items /// Edit->Preferences ... PreferencesDlg ... S1.1(language, etc. local settings), and /// Edit->Advanced settings ... AdvancedSettingsDlg ... S1.2(spec. where bench settings are stored) /// [XmlRootAttribute("TestBenchFramework")] public class LocalSettings { /// /// Available languages. /// Method ToString should give a supported culture info string: /// ("en","de","fr","es","it","cn", etc.). /// public enum Languages { en, de, sk, Count, } /// /// User interface language ("en","de","fr","es","it","cn", etc.). /// public string Language = "en"; /// /// Names and database settings of test benches. /// [XmlArrayAttribute("TestBenches")] public DatabaseSettings[] testBenches; [XmlIgnore] public DatabaseSettings[] TestBenches { get { return testBenches; } } [XmlIgnore] public int BenchesCount { get { return testBenches != null ? testBenches.GetLength(0) : 0; } } /// /// Currently selected bench name (local or remote) or null (= no selection done). /// [XmlElementAttribute("LastBench")] public string LastBenchName; /// /// Currently selected procedure name or null (= no selection done yet). /// [XmlElementAttribute("LastProcedure")] public string LastProcedureName; public int RightPaneHorizSplitterDistance; public Forms.ResultsConfig.Meters ResultsConfigMeters; public Forms.ResultsConfig.TestsAre ResultsConfigTests; /// /// Copy public fields from a LocalSettings object to this object. /// /// Origin public void Assign(LocalSettings ls) { if (ls != null) { Language = ls.Language; testBenches = new DatabaseSettings[ls.testBenches.Length]; for (int i = 0; i < testBenches.Length; i++) testBenches[i] = ls.testBenches[i]; LastBenchName = ls.LastBenchName; LastProcedureName = ls.LastProcedureName; RightPaneHorizSplitterDistance = ls.RightPaneHorizSplitterDistance; ResultsConfigMeters = ls.ResultsConfigMeters; ResultsConfigTests = ls.ResultsConfigTests; } } [XmlIgnore] public readonly bool AlwaysAskPasswdWhenUnlocking; [XmlIgnore] public readonly bool RestoreUserWhenLeavingDialog; public LocalSettings() { AlwaysAskPasswdWhenUnlocking = false; RestoreUserWhenLeavingDialog = true; } /// /// Load public fields of this class from the XML file. /// public void Load() { try { using (TextReader reader = new StreamReader(Program.LocalSettingsFileName)) { LocalSettings ls = (new XmlSerializer(typeof(LocalSettings))).Deserialize(reader) as LocalSettings; // Copy the settings just in case De-serialize() completes OK this.Assign(ls); } } catch (Exception e) { string msg = e.Message; } } /// /// Save public fields of this class to the XML file. /// public void Save() { try { using (TextWriter writer = new StreamWriter(Program.LocalSettingsFileName)) { (new XmlSerializer(typeof(LocalSettings))).Serialize(writer, this); } } catch (Exception e) { string msg = e.Message; } } } }