/*
* 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;
using FastForward.WinCore;
namespace FastForward.Monitor
{
///
/// The configuration for the monitor application.
///
[XmlRoot("Application")]
public class ApplicationConfig
{
#region Constructors
///
/// Initialise a new .
///
public ApplicationConfig()
{
Servers = new List();
Monitor = new MonitorDisplayConfig();
BuildTemplates = new List();
DefaultPluginLocation = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins");
LoadedPlugins = new List();
ConfiguredWindows = 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 Monitor
///
/// The UI configuration.
///
[XmlElement]
public MonitorDisplayConfig Monitor { get; set; }
#endregion
#region Servers
///
/// The configuration of the servers.
///
[XmlElement("Server")]
public List Servers { get; set; }
#endregion
#region BuildTemplates
///
/// The available build templates.
///
[XmlElement("BuildTemplate")]
public List BuildTemplates { get; set; }
#endregion
#region DefaultPluginLocation
///
/// Gets or sets the default plug-in location.
///
[XmlElement("PluginLocation")]
public string DefaultPluginLocation { get; set; }
#endregion
#region LoadedPlugins
///
/// The configured plugins.
///
[XmlElement("Plugin")]
public List LoadedPlugins { get; set; }
#endregion
#region ConfiguredWindows
///
/// Gets the configured windows.
///
[XmlElement("Window")]
public List ConfiguredWindows { get; set; }
#endregion
#endregion
#region Public methods
#region Load()
///
/// Load the configuration from the default location.
///
///
public static ApplicationConfig Load()
{
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
Path.Combine("FastForward.NET", "MonitorConfig.xml"));
return Load(path);
}
///
/// Load the configuration from a file.
///
///
public static ApplicationConfig Load(string fileName)
{
using (Trace.Start("Loading configuration from " + fileName))
{
ApplicationConfig value = null;
if (File.Exists(fileName))
{
using (var file = File.OpenRead(fileName))
{
using (var reader = XmlReader.Create(file))
{
var serialiser = new XmlSerializer(typeof(ApplicationConfig));
try
{
value = serialiser.Deserialize(reader) as ApplicationConfig;
}
catch
{
// Ignore any errors in the configuration
value = new ApplicationConfig();
}
}
}
}
else
{
value = new ApplicationConfig();
}
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(ApplicationConfig));
serialiser.Serialize(writer, this);
}
}
}
#endregion
#region Clone()
///
/// Clone these application settings.
///
///
public ApplicationConfig Clone()
{
var clone = new ApplicationConfig
{
FileName = FileName,
Monitor = Monitor.Clone()
};
foreach (var server in Servers)
{
clone.Servers.Add(server.Clone());
}
foreach (var template in BuildTemplates)
{
clone.BuildTemplates.Add(template.Clone());
}
foreach (var plugin in LoadedPlugins)
{
clone.LoadedPlugins.Add(plugin.Clone());
}
return clone;
}
#endregion
#region FindPlugin()
///
/// Attempt to find a plugin.
///
///
///
public PluginConfig FindPlugin(string key)
{
var plugin = (from record in this.LoadedPlugins
where key == record.Key
select record).SingleOrDefault();
return plugin;
}
#endregion
#endregion
}
}