/* * Copyright (c) 2009 Craig Sutherland * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */ using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Xml; using System.Xml.Serialization; using FastForward.WinCore.Configuration; namespace FastForward.VisualStudio { /// /// The configuration for the Visual Studio plugin. /// [XmlRoot("VisualStudio")] public class VisualStudioConfig { #region Constructors /// /// Initialise a new . /// public VisualStudioConfig() { Servers = new List(); } #endregion #region Public properties #region FileName /// /// The file name the configuration was loaded from. /// [XmlIgnore] public string FileName { get; private set; } #endregion #region Servers /// /// The configuration of the servers. /// [XmlElement("Server")] public List Servers { get; set; } #endregion #endregion #region Public methods #region Load() /// /// Load the configuration from the default location. /// /// public static VisualStudioConfig Load() { var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Path.Combine("FastForward.NET", "VisualStudioConfig.xml")); return Load(path); } /// /// Load the configuration from a file. /// /// public static VisualStudioConfig Load(string fileName) { VisualStudioConfig value = null; if (File.Exists(fileName)) { using (var file = File.OpenRead(fileName)) { using (var reader = XmlReader.Create(file)) { var serialiser = new XmlSerializer(typeof(VisualStudioConfig)); try { value = serialiser.Deserialize(reader) as VisualStudioConfig; } catch { // Ignore any errors in the configuration value = new VisualStudioConfig(); } } } } else { value = new VisualStudioConfig(); } value.FileName = fileName; return value; } #endregion #region Save() /// /// Save the configuration back to the original file. /// public void Save() { Save(FileName); } /// /// Save the configuration to a new file. /// public void Save(string fileName) { var directory = Path.GetDirectoryName(fileName); if (!Directory.Exists(directory)) Directory.CreateDirectory(directory); using (var file = File.Create(fileName)) { var settings = new XmlWriterSettings { CheckCharacters = true, ConformanceLevel = ConformanceLevel.Document, Encoding = UTF8Encoding.UTF8, Indent = true, NewLineHandling = NewLineHandling.Entitize, OmitXmlDeclaration = false, }; using (var writer = XmlWriter.Create(file, settings)) { var serialiser = new XmlSerializer(typeof(VisualStudioConfig)); serialiser.Serialize(writer, this); } } } #endregion #region Clone() /// /// Clone these application settings. /// /// public VisualStudioConfig Clone() { var clone = new VisualStudioConfig { FileName = FileName }; foreach (var server in Servers) { clone.Servers.Add(server.Clone()); } return clone; } #endregion #endregion } }