/// /// 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 { static XmlSerializer serializer = XmlSerializer.FromTypes(new[] { typeof(LocalSettings) })[0]; /// /// User interface language (used as CultureInfo(..) constructor argument). /// public string Language = "en"; /// /// Names and database settings of test benches. /// [XmlArrayAttribute("TestBenches")] public Config.DatabaseSettings[] TestBenches; [XmlIgnore] public int BenchesCount { get { return (TestBenches != null) ? TestBenches.GetLength(0) : 0; } } /// /// Login method: UserName, FullName or Number /// public Users.Entities.LoginMethod LoginMethod; /// /// 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 double RealDensity; /// water density [kg/m3] public double AtTemperature; /// measured at temperature [degree C] public int RightPaneHorizSplitterDistance; public int TopPaneVerticalSplitterDistance; /// 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; /// Pop-up results public int PopupResultsLeft; public int PopupResultsTop; public int PopupResultsWidth; public int PopupResultsHeight; [XmlArrayAttribute("ComponentsColumnWidths")] public int[] ComponentsColumnWidths; [XmlIgnore] public int ComponentsColumnCount { get { return (ComponentsColumnWidths != null) ? ComponentsColumnWidths.Length : 0; } } /// Paths public int PathsDlgLeft; public int PathsDlgTop; public int PathsDlgWidth; public int PathsDlgHeight; /// iPerlCommunicationsForm public int iPerlCommunicationsFormLeft; public int iPerlCommunicationsFormTop; [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; } } [XmlArrayAttribute("HeatMetersColumnWidths")] public int[] HeatMetersColumnWidths; [XmlIgnore] public int HeatMetersColumnCount { get { return (HeatMetersColumnWidths != null) ? HeatMetersColumnWidths.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; [XmlArrayAttribute("ProceduresColumnWidths")] public int[] ProceduresColumnWidths; [XmlIgnore] public int ProceduresColumnCount { get { return (ProceduresColumnWidths != null) ? ProceduresColumnWidths.Length : 0; } } /// OneProcedure public int OneProcedureDlgLeft; public int OneProcedureDlgTop; public int OneProcedureDlgWidth; public int OneProcedureDlgHeight; public int PrintOrderLeft; public int PrintOrderTop; [XmlArrayAttribute("HistoryColumnWidths")] public int[] HistoryColumnWidths; [XmlIgnore] public int HistoryColumnCount { get { return (HistoryColumnWidths != null) ? HistoryColumnWidths.Length : 0; } } /// Endurance test cycle steps public int EnduranceDlgLeft; public int EnduranceDlgTop; public int EnduranceDlgWidth; public int EnduranceDlgHeight; /// Serial numbers public string[] LastSNTexts; [XmlIgnore] public int LastSNTextsCount { get { return (LastSNTexts != null) ? LastSNTexts.Length : 0; } } public string[] LastMainSNTexts; [XmlIgnore] public int LastMainSNTextsCount { get { return (LastMainSNTexts != null) ? LastMainSNTexts.Length : 0; } } public string[] LastAuxSNTexts; [XmlIgnore] public int LastAuxSNTextsCount { get { return (LastAuxSNTexts != null) ? LastAuxSNTexts.Length : 0; } } public string LastText1; public string LastText2; public string LastText3; public string LastRemark; /// 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; } } public bool PrintOrderOnlyGoodResult; public bool PrintOrderOnlyLastResult; /// Configuration of results public Config.Entities.MetersArrangement ResultsConfigMeters; public Config.Entities.TestsArrangement ResultsConfigTests; public int ResultsConfigMetersInOneGroup; public string[] RsltItems_Screen_SingleWM; public string[] RsltItems_Screen_CombinedWM; public string[] RsltItems_Screen_HeatM; public string[] RsltItems_Printer_SingleWM; /// obsolete public string[] RsltItems_Printer_CombinedWM; /// obsolete public string[] RsltItems_Printer_HeatM; /// obsolete [XmlArrayAttribute("RsltsClmnWidths")] public int[] RsltsClmnWidths; [XmlIgnore] public int RsltsClmnCount { get { return (RsltsClmnWidths != null) ? RsltsClmnWidths.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 = serializer.Deserialize(reader) as LocalSettings; if (ls.RealDensity < 500 || ls.RealDensity > 1500) { /// Use default value if density was not set yet ls.RealDensity = 998.2008; /// kg/m3 ls.AtTemperature = 20.0; /// deg.C } 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)) { serializer.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 = currentHistoryCount - 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; } } }