/// /// Copyright (c) 2013-2019 Sensus Slovensko a.s. /// using System; using System.IO; using System.Security.Cryptography; using System.Text; using System.Xml.Serialization; using log4net; 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 { private static readonly ILog log = LogManager.GetLogger(typeof(LocalSettings)); static XmlSerializer serializer = XmlSerializer.FromTypes(new[] { typeof(LocalSettings) })[0]; /// /// Configuration file ecryption/decryption key and initialization vector /// static byte[] key = new byte[] { 83, 254, 105, 64, 184, 201, 195, 127, 52, 99, 77, 45, 252, 132, 163, 156 }; static byte[] IV = new byte[] { 189, 23, 137, 56, 204, 241, 242, 118, 28, 89, 9, 198, 224, 111, 186, 125 }; /// /// 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 bool LastProcedureIsRemote; public int LastProcedureNr; public int BatchNr; public int RightPaneHorizSplitterDistance; public int TopPaneVerticalSplitterDistance; /// MainWnd public bool MainWndMaximized; public int MainWndLeft; public int MainWndTop; public int MainWndWidth; public int MainWndHeight; /// Components public bool ComponentsDlgMaximized; 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; public Config.Entities.CameraDisplayResolution Resolution; [XmlArrayAttribute("Counters")] public int[] Counters; [XmlArrayAttribute("ComponentsColumnWidths")] public int[] ComponentsColumnWidths; [XmlIgnore] public int ComponentsColumnCount { get { return (ComponentsColumnWidths != null) ? ComponentsColumnWidths.Length : 0; } } /// Paths public bool PathsDlgMaximized; 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; } } [XmlArrayAttribute("HeatMetersColumnWidths")] public int[] HeatMetersColumnWidths; [XmlIgnore] public int HeatMetersColumnCount { get { return (HeatMetersColumnWidths != null) ? HeatMetersColumnWidths.Length : 0; } } /// Transitions public bool TransitionsDlgMaximized; 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 bool MetrologyDlgMaximized; public int MetrologyDlgLeft; public int MetrologyDlgTop; public int MetrologyDlgWidth; public int MetrologyDlgHeight; /// Conditions public bool ConditionsDlgMaximized; public int ConditionsDlgLeft; public int ConditionsDlgTop; public int ConditionsDlgWidth; public int ConditionsDlgHeight; /// Uncertainty public bool UncertaintyDlgMaximized; public int UncertaintyDlgLeft; public int UncertaintyDlgTop; public int UncertaintyDlgWidth; public int UncertaintyDlgHeight; /// Procedures public bool ProceduresDlgMaximized; 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 bool OneProcedureDlgMaximized; public int OneProcedureDlgLeft; public int OneProcedureDlgTop; public int OneProcedureDlgWidth; public int OneProcedureDlgHeight; [XmlArrayAttribute("ProfilesColumnWidths")] public int[] ProfilesColumnWidths; [XmlIgnore] public int ProfilesColumnCount { get { return (ProfilesColumnWidths != null) ? ProfilesColumnWidths.Length : 0; } } 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; /// iPerlCommunicationsForm public int iPerlCommunicationsFormLeft; public int iPerlCommunicationsFormTop; public long iPerlCommunicationsFormCheckboxes; /// 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; } } /// Prefix history public string[] PrefixHistory; [XmlIgnore] public int PrefixHistoryCount { get { return (PrefixHistory != null) ? PrefixHistory.Length : 0; } } /// Remarks history public string[] RemarkHistory; [XmlIgnore] public int RemarksHistoryCount { get { return (RemarkHistory != null) ? RemarkHistory.Length : 0; } } /// Tester history public string[] TesterHistory; [XmlIgnore] public int TesterHistoryCount { get { return (TesterHistory != null) ? TesterHistory.Length : 0; } } public bool PrintOrderOnlyGoodResult; public bool PrintOrderOnlyLastResult; /// Legalizator history public string[] LastLegalizators; [XmlIgnore] public int LastLegalizatorsCount { get { return (LastLegalizators != null) ? LastLegalizators.Length : 0; } } /// Configuration of results public Config.Entities.MetersArrangement ResultsConfigMeters; public Config.Entities.TestsArrangement ResultsConfigTests; public int ResultsConfigMetersInOneGroup; public bool ShowDisabledPositions; public int ResultsConfigMinWidth; public int ResultsConfigMinHeight; public string[] RsltItems_Screen_SingleWM; public string[] RsltItems_Screen_CombinedWM; public string[] RsltItems_Screen_HeatM; /// Configuration of graphs public bool Graph1_On; public bool Graph2_On; public bool Graph3_On; public bool Graph4_On; public bool Graph5_On; public bool Graph6_On; public bool Graph7_On; public bool Graph8_On; public GraphLib.PlotterGraphPaneEx.LayoutMode GraphsLayoutMode; [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. /// /// Name of the file. /// /// LocalSettings object or null when Load() fails /// public static LocalSettings Load(string fileName) { try { bool isEncrypted = true; using (StreamReader reader = new StreamReader(fileName)) { isEncrypted = !reader.ReadLine().StartsWith(" /// Save public fields of this class to the XML file. /// public void Save() { try { #if false /// Write settings to an unencrypted file using (TextWriter writer = new StreamWriter(Program.LocalSettingsFileName)) { serializer.Serialize(writer, this); log.Warn("LocalSettings succesfully saved"); } #else /// Encrypt and write to file 'Program.LocalSettingsFileName' using (RijndaelManaged aes = new RijndaelManaged { Padding = PaddingMode.Zeros }) { using (FileStream fsCrypt = new FileStream(Program.LocalSettingsFileName, FileMode.Create)) { using (ICryptoTransform encryptor = aes.CreateEncryptor(key, IV)) { using (CryptoStream cs = new CryptoStream(fsCrypt, encryptor, CryptoStreamMode.Write)) { using (MemoryStream mStream = new MemoryStream()) { using (var writer = new StreamWriter(mStream)) { /// Serialize and write settings to a memory stream serializer.Serialize(writer, this); writer.Flush(); mStream.Position = 0; using (var reader = new StreamReader(mStream)) { /// Encrypt and write to file 'Program.LocalSettingsFileName' int data; while ((data = mStream.ReadByte()) != -1) cs.WriteByte((byte)data); log.WarnFormat("Succesfully encrypted and saved to '{0}'", Program.LocalSettingsFileName); } } } } } } } #endif } catch (Exception e) { log.ErrorFormat("Failed to save to '{0}': {1}", Program.LocalSettingsFileName, 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; } } }