/// /// Copyright (c) 2015-2022 Sensus Slovensko a.s. /// using System; using System.IO; using System.Security.Cryptography; using System.Text; using System.Xml.Serialization; namespace ProductionTracing { /// /// 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("ProductionTracing")] public class 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; public bool IsUserLoginRequired; /// Database settings of servers. public string ConnectionString; public string UsersDBConnString; /// Statistics options public bool TwoDim; public string RowItemID; /// Caption of the selected row item public string ColumnItemID; /// Caption of the selected column item (applies when TwoDim == true) public bool FilterEnabled; public string FilteredItemId; /// Caption of the item to be filtered public string FilteredValue; /// MainWnd public int MainWndLeft; public int MainWndTop; public int MainWndWidth; public int MainWndHeight; public bool MainWndMaximized; /// History of texts entered in ComboBoxes public string[] Combo1History; public string[] Combo2History; public string[] Combo4History; public string[] Combo5History; public string[] Combo6History; public string[] Combo7History; public string[] Combo8History; public string[] Combo9History; /// Last configuration file name public string LastConfigFileName; /// Report file name history public string LastReportFileName; public string[] ReportFileNameHistory; [XmlIgnore] public int ReportFileNameHistoryCount { get { return (ReportFileNameHistory != null) ? ReportFileNameHistory.Length : 0; } } /// Report configuration public string[] ReportItems; /// Parameterless constructor required by serialization public LocalSettings() { } public LocalSettings(bool createNew) { if (createNew) { Language = "EN"; IsUserLoginRequired = false; #if DEBUG ConnectionString = "SERVER=localhost; DATABASE=nanjingshared; UID=root; PASSWORD=kraken; CHARSET=utf8;"; UsersDBConnString = "SERVER=localhost; DATABASE=nanjingshared; UID=root; PASSWORD=kraken; CHARSET=utf8;"; #else ConnectionString = "SERVER=10.116.192.200; DATABASE=nanjingshared; UID=nanjingshared; PASSWORD=C1ern4V0d4; CHARSET=utf8;"; UsersDBConnString = "SERVER=10.116.192.200; DATABASE=nanjingshared; UID=nanjingshared; PASSWORD=C1ern4V0d4; CHARSET=utf8;"; #endif MainWndLeft = 20; MainWndTop = 20; MainWndWidth = 1406; MainWndHeight = 750; MainWndMaximized = false; LastConfigFileName = string.Empty; } } /// /// Load public fields of this class from the XML 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 { /// 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); } } } } } } } } catch (Exception e) { string msg = e.Message; } } public static void PrepareCombo(System.Windows.Forms.ComboBox comboBox, string[] history) { if (history == null) history = new string[0]; for (int i = 0; i < history.Length; i++) comboBox.Items.Add(history[i]); if (comboBox.Items.Count > 0) comboBox.Text = comboBox.Items[0].ToString(); } /// /// 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; } return true; } } }