using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using Tango.Logging; using Tango.Serialization; namespace Tango.Settings { /// /// Represents a settings manager for loading and saving application settings. /// public static class SettingsManager { private static LogManager LogManager = LogManager.Default; /// /// Gets or sets a value indicating whether the settings manager is initialized. /// public static bool IsInitialized { get; private set; } /// /// Gets or sets the default settings. /// /// /// The default. /// public static SettingsCollection Default { get; private set; } /// /// Gets or sets the default file path. /// /// /// The default file path. /// public static String DefaultFilePath { get; private set; } /// /// Gets or sets the default folder for application settings. /// public static String DefaultFolder { get; private set; } /// /// Saves application settings to XML file. /// /// The path to the XML file. /// The instance of the SettingsCollection to save. public static void SaveToXML(String filePath, SettingsCollection stCollection) { LogManager.Log("Saving application configuration to " + "'" + filePath + "'"); XmlDataSerializer serializer = new XmlDataSerializer(); serializer.SerializeToFile(stCollection, filePath); } /// /// Loads application settings from XML file. /// /// The file path. /// public static SettingsCollection LoadFromXML(String filePath) { LogManager.Log("Loading application configuration from " + "'" + filePath + "'"); XmlDataSerializer serialier = new XmlDataSerializer(); return serialier.DeserializeFromFile(filePath); } /// /// Saves the default settings. /// public static void SaveDefaultSettings() { SaveToXML(DefaultFilePath, Default); } /// /// Restores to default. /// public static void RestoreToDefault() { Default = new SettingsCollection(); SaveDefaultSettings(); } /// /// Initializes the class. /// static SettingsManager() { Initialize(); } /// /// Initializes the settings manager. /// private static void Initialize() { if (IsInitialized) return; IsInitialized = true; LogManager.Log("Initializing application configuration..."); DefaultFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "Settings.xml"); DefaultFolder = Path.GetDirectoryName(DefaultFilePath); bool waited = false; Retry: try { Default = LoadFromXML(DefaultFilePath); } catch (Exception ex) { if (!waited) { LogManager.Log(ex, "Could not load application configuration. Retrying in 1 second..."); waited = true; Thread.Sleep(1000); goto Retry; } else { LogManager.Log(ex, "Could not load application configuration."); } try { LogManager.Log("Creating application configuration directory structure..."); Directory.CreateDirectory(DefaultFolder); } catch (Exception ex2) { LogManager.Log(ex2, "Could not generate directory structure."); } } if (Default == null) { Default = new SettingsCollection(); try { LogManager.Log("Generating default configuration file."); SaveToXML(DefaultFilePath, Default); } catch (Exception ex) { LogManager.Log(ex, "Could not generate default configuration file."); } } else { LogManager.Log("Application configuration loaded successfully."); } } } }