// // Config.cs // // Author: // Jacob Johnston (jacobj@inchoatethoughts.com) // // Copyright (C) 2009 Jacob Johnston // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser 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 Lesser 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 . using System; using System.Collections.Generic; using System.IO; using System.Xml; namespace ZenLibrary.RuleBase { /// /// The Config class is a singleton charged with writing and reading /// the application's configuration settings. /// public class Config { static Config instance = null; private bool suppressWrite = true; private string browsePath = string.Empty; private string mp3tagPath = string.Empty; /// /// Config is a singleton so the constructor is private. /// private Config() { } /// /// Gets (and creates) the single instance of the Config class. /// public static Config Instance { get { if (instance == null) instance = new Config(); return instance; } } /// /// Gets and sets the stored path to the media library. /// public string BrowsePath { get { return browsePath; } set { browsePath = value; SaveConfig(); } } public string MP3TagPath { get { return mp3tagPath; } set { mp3tagPath = value; SaveConfig(); } } /// /// Calls all application and rule settings to be saved. /// public void SaveConfig() { if (suppressWrite) // Don't rewrite the config file until we've read in the old one. return; string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ZenLibrary"); if (!Directory.Exists(path)) Directory.CreateDirectory(path); FileStream fileStream = new FileStream(path + "\\config.xml", FileMode.Create); XmlWriterSettings writerSettings = new XmlWriterSettings(); writerSettings.Indent = true; writerSettings.NewLineOnAttributes = true; XmlWriter writer = XmlWriter.Create(fileStream, writerSettings); writer.WriteStartDocument(); writer.WriteStartElement("Settings"); writer.WriteStartElement("ApplicationSettings"); writer.WriteAttributeString("BrowsePath", browsePath); writer.WriteAttributeString("MP3TagPath", mp3tagPath); writer.WriteEndElement(); writer.WriteStartElement("Rules"); writer.WriteStartElement("DirectoryRules"); foreach (Rule rule in RuleSet.Instance.DirectoryRules) { rule.WriteXml(writer); } writer.WriteEndElement(); writer.WriteStartElement("FileRules"); foreach (Rule rule in RuleSet.Instance.FileRules) { rule.WriteXml(writer); } writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Close(); fileStream.Close(); } /// /// Reads the existing config file and loads all rule settings into the rule instances. /// public void ReadConfig() { string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ZenLibrary"); path = path + "\\config.xml"; if (!File.Exists(path)) { suppressWrite = false; return; } FileStream fileStream = new FileStream(path, FileMode.Open); try { XmlReaderSettings readerSettings = new XmlReaderSettings(); readerSettings.IgnoreWhitespace = true; XmlReader reader = XmlReader.Create(fileStream, readerSettings); while (reader.Read()) { switch (reader.Name) { case "ApplicationSettings": browsePath = reader["BrowsePath"]; mp3tagPath = reader["MP3TagPath"]; if (!File.Exists(mp3tagPath)) mp3tagPath = string.Empty; break; case "DirectoryRules": ReadRules(reader, RuleSet.Instance.DirectoryRules); break; case "FileRules": ReadRules(reader, RuleSet.Instance.FileRules); break; } } reader.Close(); } catch { // Do Nothing // If we fail to read the config, we'll cut our losses // and wait for the config to be rebuilt the next time // anything is changed in the config. } finally { suppressWrite = false; fileStream.Close(); } } /// /// Separated logic for reading a particular rule. /// /// The XMLReader to read the rule config element. /// The list of rules that this rule will be put into. private void ReadRules(XmlReader reader, List rules) { reader.Read(); // File Rules Open Element while (reader.NodeType != XmlNodeType.EndElement) { bool foundRule = false; foreach (Rule rule in rules) { if (rule.GetType().ToString().CompareTo(reader.Name) == 0) { rule.ReadXml(reader); reader.Read(); foundRule = true; break; } } if (!foundRule) { reader.Read(); } } } } }