244 lines
9.1 KiB
C#
244 lines
9.1 KiB
C#
///
|
|
/// Copyright (c) 2021 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Xml.Serialization;
|
|
|
|
namespace OrderManagement
|
|
{
|
|
/// <summary>
|
|
/// Class to be serialized to an XML file...
|
|
/// </summary>
|
|
[XmlRootAttribute("SNPrinting")]
|
|
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 };
|
|
|
|
public Mode Mode;
|
|
public string Language; /// User interface language used as CultureInfo(..) constructor argument
|
|
public int DelayBetweenLabels; /// Delay between printing two consecutive labels in ms
|
|
public bool IsUserLoginRequired; /// true = require user login on program start-up
|
|
public string TracingDBConnString; /// Connection string to MySQL database with production tracing records
|
|
public string UsersDBConnString; /// Connection string to MySQL database with authorized users
|
|
public string[] SapNumberHistory; /// SAP number history
|
|
public string[] SeparatorHistory; /// Separator history
|
|
public int CurrentSNTrail; /// Serial number trail to be printed now
|
|
public int RemainingSNsCount; /// Remaining count of serial numbers to be printed in this session
|
|
|
|
|
|
/// Parameterless constructor required by serialization
|
|
public LocalSettings() { }
|
|
|
|
public LocalSettings(bool createNew)
|
|
{
|
|
if (createNew)
|
|
{
|
|
Mode = Mode.Standalone;
|
|
Language = "EN";
|
|
DelayBetweenLabels = 1000;
|
|
IsUserLoginRequired = false;
|
|
TracingDBConnString = string.Empty;
|
|
UsersDBConnString = string.Empty;
|
|
SapNumberHistory = new string[] { };
|
|
SeparatorHistory = new string[] { "4VQ" };
|
|
CurrentSNTrail = 1;
|
|
RemainingSNsCount = 0;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Load public fields of this class from the XML file.
|
|
/// </summary>
|
|
/// <returns>LocalSettings object or null when Load() fails</returns>
|
|
public static LocalSettings Load(string fileName)
|
|
{
|
|
try
|
|
{
|
|
bool isEncrypted = true;
|
|
using (StreamReader reader = new StreamReader(fileName))
|
|
{
|
|
isEncrypted = !reader.ReadLine().StartsWith("<?xml version=");
|
|
reader.Close();
|
|
}
|
|
|
|
if (isEncrypted)
|
|
{
|
|
/// Write settings to a string
|
|
StringBuilder plain = new StringBuilder();
|
|
|
|
using (RijndaelManaged aes = new RijndaelManaged { Padding = PaddingMode.Zeros })
|
|
{
|
|
using (FileStream fsCrypt = new FileStream(fileName, FileMode.Open))
|
|
{
|
|
using (ICryptoTransform decryptor = aes.CreateDecryptor(key, IV))
|
|
{
|
|
using (CryptoStream cs = new CryptoStream(fsCrypt, decryptor, CryptoStreamMode.Read))
|
|
{
|
|
using (MemoryStream mStream = new MemoryStream())
|
|
{
|
|
using (var writer = new BinaryWriter(mStream))
|
|
{
|
|
int data;
|
|
while ((data = cs.ReadByte()) != -1)
|
|
{
|
|
if (data != 0) writer.Write((byte)data);
|
|
}
|
|
writer.Flush();
|
|
mStream.Position = 0;
|
|
|
|
using (var reader = new StreamReader(mStream))
|
|
{
|
|
return serializer.Deserialize(reader) as LocalSettings;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
using (StreamReader reader = new StreamReader(fileName))
|
|
{
|
|
return serializer.Deserialize(reader) as LocalSettings;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
string msg = e.Message;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Save public fields of this class to the XML file.
|
|
/// </summary>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates history stored in a string array by a new latest string.
|
|
/// </summary>
|
|
/// <param name="lastStrValue">Last entered string</param>
|
|
/// <returns>true = history updated</returns>
|
|
public static bool UpdateHistory(string lastStrValue, ref string[] history)
|
|
{
|
|
const int MaxHistoryLen = 10;
|
|
|
|
if (string.IsNullOrEmpty(lastStrValue)) return false;
|
|
|
|
int currentHistoryLength = (history != null) ? history.Length : 0;
|
|
|
|
int match = -1;
|
|
for (int i = 0; i < currentHistoryLength; i++)
|
|
{
|
|
if (history[i] == lastStrValue)
|
|
{
|
|
match = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (match >= 0 || currentHistoryLength >= MaxHistoryLen)
|
|
{
|
|
/// History does not have to be extended (because of a match) or should not be extended (because of the lenght)
|
|
if (match < 0) match = currentHistoryLength - 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[currentHistoryLength + 1];
|
|
newHistory[0] = lastStrValue;
|
|
for (int j = 1; j <= currentHistoryLength; j++)
|
|
{
|
|
newHistory[j] = history[j - 1];
|
|
}
|
|
history = newHistory;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Load Purchase order combo box items from the LocalSettings PurchaseOrderHistory array
|
|
/// </summary>
|
|
/// <param name="comboBox">Puchase order ComboBox</param>
|
|
public static void PrepareCombo(string[] history, System.Windows.Forms.ComboBox comboBox)
|
|
{
|
|
if (history != null)
|
|
{
|
|
for (int i = 0; i < history.Length; i++)
|
|
{
|
|
comboBox.Items.Add(history[i]);
|
|
}
|
|
}
|
|
|
|
if (comboBox.Items.Count > 0)
|
|
{
|
|
comboBox.Text = comboBox.Items[0].ToString();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public enum Mode
|
|
{
|
|
Standalone,
|
|
WithDatabase,
|
|
}
|
|
}
|