From 2ca4b7d03adfa3cc7c9c6a597a11762142d5b34b Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Mon, 4 Mar 2013 00:43:06 -0500 Subject: Created IConfigurationManager --- .../Configuration/ConfigurationHelper.cs | 68 ++++++++++++++++++ .../Configuration/IApplicationPaths.cs | 82 ++++++++++++++++++++++ .../Configuration/IConfigurationManager.cs | 36 ++++++++++ 3 files changed, 186 insertions(+) create mode 100644 MediaBrowser.Common/Configuration/ConfigurationHelper.cs create mode 100644 MediaBrowser.Common/Configuration/IApplicationPaths.cs create mode 100644 MediaBrowser.Common/Configuration/IConfigurationManager.cs (limited to 'MediaBrowser.Common/Configuration') diff --git a/MediaBrowser.Common/Configuration/ConfigurationHelper.cs b/MediaBrowser.Common/Configuration/ConfigurationHelper.cs new file mode 100644 index 0000000000..fc74a6a4d0 --- /dev/null +++ b/MediaBrowser.Common/Configuration/ConfigurationHelper.cs @@ -0,0 +1,68 @@ +using MediaBrowser.Model.Serialization; +using System; +using System.IO; +using System.Linq; + +namespace MediaBrowser.Common.Configuration +{ + /// + /// Class ConfigurationHelper + /// + public static class ConfigurationHelper + { + /// + /// Reads an xml configuration file from the file system + /// It will immediately re-serialize and save if new serialization data is available due to property changes + /// + /// The type. + /// The path. + /// The XML serializer. + /// System.Object. + public static object GetXmlConfiguration(Type type, string path, IXmlSerializer xmlSerializer) + { + object configuration; + + byte[] buffer = null; + + // Use try/catch to avoid the extra file system lookup using File.Exists + try + { + buffer = File.ReadAllBytes(path); + + configuration = xmlSerializer.DeserializeFromBytes(type, buffer); + } + catch (FileNotFoundException) + { + configuration = Activator.CreateInstance(type); + } + + // Take the object we just got and serialize it back to bytes + var newBytes = xmlSerializer.SerializeToBytes(configuration); + + // If the file didn't exist before, or if something has changed, re-save + if (buffer == null || !buffer.SequenceEqual(newBytes)) + { + // Save it after load in case we got new items + File.WriteAllBytes(path, newBytes); + } + + return configuration; + } + + + /// + /// Reads an xml configuration file from the file system + /// It will immediately save the configuration after loading it, just + /// in case there are new serializable properties + /// + /// + /// The path. + /// The XML serializer. + /// ``0. + public static T GetXmlConfiguration(string path, IXmlSerializer xmlSerializer) + where T : class + { + return GetXmlConfiguration(typeof(T), path, xmlSerializer) as T; + } + } +} diff --git a/MediaBrowser.Common/Configuration/IApplicationPaths.cs b/MediaBrowser.Common/Configuration/IApplicationPaths.cs new file mode 100644 index 0000000000..d2446ce46d --- /dev/null +++ b/MediaBrowser.Common/Configuration/IApplicationPaths.cs @@ -0,0 +1,82 @@ + +namespace MediaBrowser.Common.Configuration +{ + /// + /// Interface IApplicationPaths + /// + public interface IApplicationPaths + { + /// + /// Gets the path to the program data folder + /// + /// The program data path. + string ProgramDataPath { get; } + + /// + /// Gets the path to the program system folder + /// + /// The program data path. + string ProgramSystemPath { get; } + + /// + /// Gets the folder path to the data directory + /// + /// The data directory. + string DataPath { get; } + + /// + /// Gets the image cache path. + /// + /// The image cache path. + string ImageCachePath { get; } + + /// + /// Gets the path to the plugin directory + /// + /// The plugins path. + string PluginsPath { get; } + + /// + /// Gets the path to the plugin configurations directory + /// + /// The plugin configurations path. + string PluginConfigurationsPath { get; } + + /// + /// Gets the path to where temporary update files will be stored + /// + /// The plugin configurations path. + string TempUpdatePath { get; } + + /// + /// Gets the path to the log directory + /// + /// The log directory path. + string LogDirectoryPath { get; } + + /// + /// Gets the path to the application configuration root directory + /// + /// The configuration directory path. + string ConfigurationDirectoryPath { get; } + + /// + /// Gets the path to the system configuration file + /// + /// The system configuration file path. + string SystemConfigurationFilePath { get; } + + /// + /// Gets the folder path to the cache directory + /// + /// The cache directory. + string CachePath { get; } + + /// + /// Gets the folder path to the temp directory within the cache folder + /// + /// The temp directory. + string TempDirectory { get; } + } + +} diff --git a/MediaBrowser.Common/Configuration/IConfigurationManager.cs b/MediaBrowser.Common/Configuration/IConfigurationManager.cs new file mode 100644 index 0000000000..0d0759b666 --- /dev/null +++ b/MediaBrowser.Common/Configuration/IConfigurationManager.cs @@ -0,0 +1,36 @@ +using MediaBrowser.Model.Configuration; +using System; + +namespace MediaBrowser.Common.Configuration +{ + public interface IConfigurationManager + { + /// + /// Occurs when [configuration updated]. + /// + event EventHandler ConfigurationUpdated; + + /// + /// Gets or sets the application paths. + /// + /// The application paths. + IApplicationPaths CommonApplicationPaths { get; } + + /// + /// Gets the configuration. + /// + /// The configuration. + BaseApplicationConfiguration CommonConfiguration { get; } + + /// + /// Saves the configuration. + /// + void SaveConfiguration(); + + /// + /// Replaces the configuration. + /// + /// The new configuration. + void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration); + } +} -- cgit v1.2.3