tbf/UserManagement/LocalSettings.cs

133 lines
3.6 KiB
C#

///
/// Copyright (c) 2017 Sensus Metering Systems
///
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
namespace UserManagement
{
/// <summary>
/// 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)
/// </summary>
[XmlRootAttribute("UserManagement")]
public class LocalSettings
{
static XmlSerializer serializer = XmlSerializer.FromTypes(new[] { typeof(LocalSettings) })[0];
/// <summary>
/// User interface language (used as CultureInfo(..) constructor argument).
/// </summary>
public string Language = "en";
public string ConnectionString = "SERVER=10.42.128.16; DATABASE=st_wr_shared_config; UID=u9305784; PASSWORD=p89344390; CHARSET=utf8;";
/// <summary>
/// Login method: UserName, FullName or Number
/// </summary>
public Users.Entities.LoginMethod LoginMethod;
/// MainWnd
public int MainWndLeft;
public int MainWndTop;
public int MainWndWidth;
public int MainWndHeight;
public bool ManiWndMaximized;
public LocalSettings()
{
}
/// <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
{
using (TextReader 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
{
using (TextWriter writer = new StreamWriter(Program.LocalSettingsFileName))
{
serializer.Serialize(writer, this);
}
}
catch (Exception e)
{
string msg = e.Message;
}
}
/// <summary>
/// Updates history stored in a string array by a new latest string.
/// </summary>
/// <param name="lastStrValue">Lste entered string</param>
/// <returns>true = updated and saved</returns>
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;
}
}
}