//-----------------------------------------------------------------------
//
// 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 preferences file.
///
public class PreferencesFileAccess : XmlParserHelper
{
///
/// Preference label for UNITS.
///
public const string UNITS = "units";
///
/// Preference label for UNITS.
///
public const string DEFAULT_TIMEOUT = "default_timeout";
///
/// Preference label for Message Queue Size.
///
public const string MSG_QUEUE_SIZE = "msg_queue_size";
///
/// Preference label for Secondary Y axis threshold.
///
public const string SECONDARY_Y_THRESHOLD = "secondary_y_threshold";
///
/// Preference label for VEHICLE.
///
public const string VEHICLE = "vehicle";
///
/// Preference label for OBDINFOSITE.
///
public const string OBDINFOSITE = "obdinfosite";
///
/// Preference label for LAST_DATA_DIRECTORY.
///
public const string LAST_DATA_DIRECTORY = "lastDataDirectory";
///
/// Preference label for ADVANCED_MODE.
///
public const string ADVANCED_MODE = "advancedMode";
///
/// Preference label for DEVELOPER_MODE.
///
public const string DEVELOPER_MODE = "developerMode";
///
/// Preference label for WINDOW_SIZE.
///
public const string WINDOW_SIZE = "windowSize";
///
/// Preference label for SEND_DELAY.
///
public const string SEND_DELAY = "sendDelay";
///
/// Preference label for BLOCK_SIZE.
///
public const string BLOCK_SIZE = "blockSize";
///
/// Preference label for FRAME_SPACING.
///
public const string FRAME_SPACING = "frameSpacing";
///
/// Name of file.
///
private string fileName;
///
/// Gets List of preferences.
///
public IDictionary preferences { get; private set; }
///
/// Initializes a new instance of the class.
///
/// Logging interface.
/// Name of file.
/// Data file directory path.
public PreferencesFileAccess(ILogging iLogging, string fileName, string dataFileDir)
: base(iLogging, dataFileDir)
{
this.fileName = fileName;
this.preferences = new SortedDictionary();
}
///
/// Save the current list to file.
///
/// 'true' when successful.
public bool save()
{
bool success = false;
List prefList = new List(this.preferences.Values);
XmlSerializer xms = new XmlSerializer(prefList.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, prefList);
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 into the current list.
///
public void load()
{
this.iLogging.appendText("Loading Preferences." + "\r\n");
List prefList = new List();
XmlSerializer xms = new XmlSerializer(prefList.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);
prefList = (List)xms.Deserialize(sr);
}
catch (Exception ex)
{
// Just log the exception, it's usually because the file doesn't exist yet.
this.iLogging.appendText(ex.Message + "\r\n");
}
finally
{
if (sr != null)
{
sr.Close();
}
}
this.preferences.Clear();
foreach (XmlClass.preference pref in prefList)
{
this.preferences.Add(pref.name, pref);
}
this.iLogging.appendText(prefList.Count + " Preferences Loaded from XML file." + "\r\n");
}
}
}