aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Settings/OLD/SettingsManager.cs
blob: 7bc15d9509d182de2848890d2665616368652c4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
{
    /// <summary>
    /// Represents a settings manager for loading and saving application settings. 
    /// </summary>
    public static class SettingsManager
    {
        private static LogManager LogManager = LogManager.Default;

        /// <summary>
        /// Gets or sets a value indicating whether the settings manager is initialized.
        /// </summary>
        public static bool IsInitialized { get; private set; }

        /// <summary>
        /// Gets or sets the default settings.
        /// </summary>
        /// <value>
        /// The default.
        /// </value>
        public static SettingsCollection Default { get; private set; }

        /// <summary>
        /// Gets or sets the default file path.
        /// </summary>
        /// <value>
        /// The default file path.
        /// </value>
        public static String DefaultFilePath { get; private set; }

        /// <summary>
        /// Gets or sets the default folder for application settings.
        /// </summary>
        public static String DefaultFolder { get; private set; }

        /// <summary>
        /// Saves application settings to XML file.
        /// </summary>
        /// <param name="filePath">The path to the XML file.</param>
        /// <param name="stCollection">The instance of the SettingsCollection to save.</param>
        public static void SaveToXML(String filePath, SettingsCollection stCollection)
        {
            LogManager.Log("Saving application configuration to " + "'" + filePath + "'");
            XmlDataSerializer serializer = new XmlDataSerializer();
            serializer.SerializeToFile<SettingsCollection>(stCollection, filePath);
        }

        /// <summary>
        /// Loads application settings from XML file.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <returns></returns>
        public static SettingsCollection LoadFromXML(String filePath)
        {
            LogManager.Log("Loading application configuration from " + "'" + filePath + "'");
            XmlDataSerializer serialier = new XmlDataSerializer();
            return serialier.DeserializeFromFile<SettingsCollection>(filePath);
        }

        /// <summary>
        /// Saves the default settings.
        /// </summary>
        public static void SaveDefaultSettings()
        {
            SaveToXML(DefaultFilePath, Default);
        }

        /// <summary>
        /// Restores to default.
        /// </summary>
        public static void RestoreToDefault()
        {
            Default = new SettingsCollection();
            SaveDefaultSettings();
        }

        /// <summary>
        /// Initializes the <see cref="SettingsManager"/> class.
        /// </summary>
        static SettingsManager()
        {
            Initialize();
        }

        /// <summary>
        /// Initializes the settings manager.
        /// </summary>
        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.");
            }
        }
    }
}