tbf/ResultsBrowser/LocalSettings.cs

145 lines
4.2 KiB
C#
Raw Normal View History

///
/// Copyright (c) 2015 Sensus Metering Systems
///
2015-02-20 02:59:11 +00:00
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
namespace ResultsBrowser
{
/// <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("ResultsBrowser")]
2015-02-20 02:59:11 +00:00
public class LocalSettings
{
/// <summary>
/// User interface language (used as CultureInfo(..) constructor argument).
/// </summary>
2015-02-20 02:59:11 +00:00
public string Language = "en";
2015-12-14 17:11:53 +00:00
/// <summary>C:\VSProjects\TBF\TestBenchFramework\LocalSettings.cs
2015-02-20 02:59:11 +00:00
/// Names and database settings of test benches.
/// </summary>
[XmlArrayAttribute("TestBenches")]
public Config.DatabaseSettings[] TestBenches;
2015-02-20 02:59:11 +00:00
[XmlIgnore]
public int BenchesCount { get { return (TestBenches != null) ? TestBenches.GetLength(0) : 0; } }
2015-02-20 02:59:11 +00:00
/// <summary>
/// Currently selected bench name (local or remote) or null (= no selection done).
/// </summary>
[XmlElementAttribute("LastBench")]
public string LastBenchName;
/// MainWnd
public int MainWndLeft;
public int MainWndTop;
public int MainWndWidth;
public int MainWndHeight;
public bool ManiWndMaximized;
2015-12-14 17:11:53 +00:00
/// Purchase order history
public string[] PurchaseOrderHistory;
[XmlIgnore]
public int PurchaseOrderHistoryCount { get { return (PurchaseOrderHistory != null) ? PurchaseOrderHistory.Length : 0; } }
2015-02-20 02:59:11 +00:00
public LocalSettings()
{
}
/// <summary>
/// Load public fields of this class from the XML file.
/// </summary>
public static LocalSettings Load(string fileName)
2015-02-20 02:59:11 +00:00
{
try
{
using (TextReader reader = new StreamReader(fileName))
{
2015-02-20 02:59:11 +00:00
LocalSettings ls = (new XmlSerializer(typeof(LocalSettings))).Deserialize(reader) as LocalSettings;
// Copy the settings just in case De-serialize() completes OK
return ls;
2015-02-20 02:59:11 +00:00
}
}
catch (Exception e)
{
string msg = e.Message;
return null;
2015-02-20 02:59:11 +00:00
}
}
/// <summary>
/// Save public fields of this class to the XML file.
/// </summary>
public void Save()
{
try
{
using (TextWriter writer = new StreamWriter(Program.LocalSettingsFileName))
{
(new XmlSerializer(typeof(LocalSettings))).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;
}
}
2015-02-20 02:59:11 +00:00
}