/// /// Copyright (c) 2013-2015 Sensus Metering Systems /// 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, it, pl, cz, zh_CN, //fr, //es, //ru, //sk, //hu, //ro, 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 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 BatchNr; public int RightPaneHorizSplitterDistance; /// MainWnd public int MainWndLeft; public int MainWndTop; public int MainWndWidth; public int MainWndHeight; public bool ManiWndMaximized; /// Components public int ComponentsDlgLeft; public int ComponentsDlgTop; public int ComponentsDlgWidth; public int ComponentsDlgHeight; /// Paths public int PathsDlgLeft; public int PathsDlgTop; public int PathsDlgWidth; public int PathsDlgHeight; [XmlArrayAttribute("FeedingColumnWidths")] public int[] FeedingColumnWidths; [XmlIgnore] public int FeedingColumnCount { get { return (FeedingColumnWidths != null) ? FeedingColumnWidths.Length : 0; } } [XmlArrayAttribute("BenchColumnWidths")] public int[] BenchColumnWidths; [XmlIgnore] public int BenchColumnCount { get { return (BenchColumnWidths != null) ? BenchColumnWidths.Length : 0; } } [XmlArrayAttribute("OutputColumnWidths")] public int[] OutputColumnWidths; [XmlIgnore] public int OutputColumnCount { get { return (OutputColumnWidths != null) ? OutputColumnWidths.Length : 0; } } [XmlArrayAttribute("MetersColumnWidths")] public int[] MetersColumnWidths; [XmlIgnore] public int MetersColumnCount { get { return (MetersColumnWidths != null) ? MetersColumnWidths.Length : 0; } } /// Transitions public int TransitionsDlgLeft; public int TransitionsDlgTop; public int TransitionsDlgWidth; public int TransitionsDlgHeight; [XmlArrayAttribute("TransitionsColumnWidths")] public int[] TransitionsColumnWidths; [XmlIgnore] public int TransitionsColumnCount { get { return (TransitionsColumnWidths != null) ? TransitionsColumnWidths.Length : 0; } } /// Metrology public int MetrologyDlgLeft; public int MetrologyDlgTop; public int MetrologyDlgWidth; public int MetrologyDlgHeight; /// Procedures public int ProceduresDlgLeft; public int ProceduresDlgTop; public int ProceduresDlgWidth; public int ProceduresDlgHeight; /// OneProcedure public int OneProcedureDlgLeft; public int OneProcedureDlgTop; public int OneProcedureDlgWidth; public int OneProcedureDlgHeight; /// OneProcedure public int PrintOrderLeft; public int PrintOrderTop; /// Purchase order history public string[] PurchaseOrderHistory; [XmlIgnore] public int PurchaseOrderHistoryCount { get { return (PurchaseOrderHistory != null) ? PurchaseOrderHistory.Length : 0; } } /// Tester history public string[] TesterHistory; [XmlIgnore] public int TesterHistoryCount { get { return (TesterHistory != null) ? TesterHistory.Length : 0; } } /// Configuration of results public Forms.ResultsConfig.Meters ResultsConfigMeters; public Forms.ResultsConfig.TestsAre ResultsConfigTests; public string[] RsltItems_Screen_SingleWM; public string[] RsltItems_Screen_CombinedWM; public string[] RsltItems_Printer_SingleWM; public string[] RsltItems_Printer_CombinedWM; public string[] RsltItems_Disk_SingleWM; public string[] RsltItems_Disk_CombinedWM; [XmlArrayAttribute("RsltsSingleClmnWdths")] public int[] RsltsSingleClmnWdths; [XmlIgnore] public int RsltsSingleClmnCount { get { return (RsltsSingleClmnWdths != null) ? RsltsSingleClmnWdths.Length : 0; } } [XmlArrayAttribute("RsltsCombinedClmnWdths")] public int[] RsltsCombinedClmnWdths; [XmlIgnore] public int RsltsCombinedClmnCount { get { return (RsltsCombinedClmnWdths != null) ? RsltsCombinedClmnWdths.Length : 0; } } [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. /// /// LocalSettings object or null when Load() fails public static LocalSettings Load(string fileName) { try { using (TextReader reader = new StreamReader(fileName)) { LocalSettings ls = (new XmlSerializer(typeof(LocalSettings))).Deserialize(reader) as LocalSettings; // Copy the settings just in case De-serialize() completes OK return ls; } } catch (Exception e) { string msg = e.Message; return null; } } /// /// 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; } } /// /// Updates history stored in a string array by a new latest string. /// /// Lste entered string /// true = updated and saved public bool UpdateHistory(string lastStrValue, ref string[] history, int maxHistoryLen = 10) { if (string.IsNullOrEmpty(lastStrValue)) return false; int currentHistoryCount = (history != null) ? history.Length : 0; int match = -1; for (int i = 0; i < currentHistoryCount; i++) { if (history[i] == lastStrValue) { match = i; break; } } if (match >= 0 || currentHistoryCount >= maxHistoryLen) { /// History does not have to be (because of a match) or should not be extended (because of the lenght) if (match < 0) match = PurchaseOrderHistoryCount - 1; for (int j = match; j > 0; j--) { history[j] = history[j - 1]; } history[0] = lastStrValue; } else { /// History wiil be extended, new item inserted at the beginning string[] newHistory = new string[currentHistoryCount + 1]; newHistory[0] = lastStrValue; for (int j = 1; j <= currentHistoryCount; j++) { newHistory[j] = history[j - 1]; } history = newHistory; } Save(); return true; } } }