//-----------------------------------------------------------------------
//
// Copyright © 2012 Nils Hammar. All rights reserved.
//
//-----------------------------------------------------------------------
/*
* Software to access vehicle information via the OBD-II connector.
*
* Copyright © 2012 Nils Hammar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Alternative licensing is possible, see the licensing document.
*
* The above text may not be removed or modified.
*/
namespace DataSource.FileAccess
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
using global::SharedObjects;
using global::SharedObjects.Misc;
///
/// Access class for gauge data.
///
public class GaugeFileAccess : XmlParserHelper
{
///
/// Name of file with data.
///
private string fileName;
///
/// Gets List of configured gauges.
///
private List gaugeitemsInt = new List();
///
/// Gets List of configured gauges.
///
public IList gaugeitems
{
get { return this.gaugeitemsInt; }
}
///
/// Initializes a new instance of the class.
///
/// Logging instance.
/// Name of file containing gauge configuration.
/// Data file directory path.
public GaugeFileAccess(ILogging iLogging, string fileName, string dataFileDir)
: base(iLogging, dataFileDir)
{
this.fileName = fileName;
}
///
/// Save data to file.
///
/// 'true' at success.
public bool save()
{
bool success = false;
XmlSerializer xms = new XmlSerializer(this.gaugeitemsInt.GetType());
xms.UnknownNode += new XmlNodeEventHandler(xms_UnknownNode);
xms.UnknownAttribute += new XmlAttributeEventHandler(xms_UnknownAttribute);
xms.UnknownElement += new XmlElementEventHandler(xms_UnknownElement);
xms.UnreferencedObject += new UnreferencedObjectEventHandler(xms_UnreferencedObject);
TextWriter writer = null;
try
{
writer = new StreamWriter(this.dataFileDir + this.fileName);
xms.Serialize(writer, this.gaugeitemsInt);
success = true;
}
catch (Exception ex)
{
MessageBox.Show(
ex.Message,
"Save failed",
MessageBoxButtons.OK,
MessageBoxIcon.Hand);
}
finally
{
if (writer != null)
{
writer.Close();
}
}
return success;
}
///
/// Load data from file.
///
public void load()
{
this.iLogging.appendText("Loading Commspeeds." + "\r\n");
this.gaugeitemsInt = new List();
XmlSerializer xms = new XmlSerializer(this.gaugeitemsInt.GetType());
xms.UnknownNode += new XmlNodeEventHandler(xms_UnknownNode);
xms.UnknownAttribute += new XmlAttributeEventHandler(xms_UnknownAttribute);
xms.UnknownElement += new XmlElementEventHandler(xms_UnknownElement);
xms.UnreferencedObject += new UnreferencedObjectEventHandler(xms_UnreferencedObject);
StreamReader sr = null;
try
{
sr = new StreamReader(this.dataFileDir + this.fileName);
this.gaugeitemsInt = (List)xms.Deserialize(sr);
}
catch (Exception ex)
{
MessageBox.Show(
ex.Message,
"Load failed",
MessageBoxButtons.OK,
MessageBoxIcon.Hand);
}
finally
{
if (sr != null)
{
sr.Close();
}
}
this.gaugeitemsInt.Sort(Utils.gaugeitem_sorter);
this.iLogging.appendText(this.gaugeitemsInt.Count + " Commspeeds Loaded from XML file." + "\r\n");
}
}
}