//----------------------------------------------------------------------- // // 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 filters file. /// public class FilterFileAccess : XmlParserHelper { /// /// Name of file with data. /// private string fileName; /// /// Gets List of filters. /// private List filtersInt = null; /// /// Gets List of filters. /// public IList filters { get { return this.filtersInt; } } /// /// Initializes a new instance of the class. /// /// Logging instance. /// File name to use. /// Data file directory path. public FilterFileAccess(ILogging iLogging, string fileName, string dataFileDir) : base(iLogging, dataFileDir) { this.fileName = fileName; this.filtersInt = new List(); } /// /// Save data to file. /// /// 'true' at success. public bool save() { bool success = false; XmlSerializer xms = new XmlSerializer(this.filtersInt.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.filtersInt); 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("Filters Modes." + "\r\n"); this.filtersInt = new List(); XmlSerializer xms = new XmlSerializer(this.filtersInt.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.filtersInt = (List)xms.Deserialize(sr); } catch (Exception ex) { MessageBox.Show( ex.Message, "Load failed", MessageBoxButtons.OK, MessageBoxIcon.Hand); } finally { if (sr != null) { sr.Close(); } } this.filtersInt.Sort(Utils.filter_sorter); this.iLogging.appendText(this.filtersInt.Count + " Filters Loaded from XML file." + "\r\n"); } } }