-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomSettingsProvider.cs
283 lines (257 loc) · 12.3 KB
/
CustomSettingsProvider.cs
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Xml.Linq;
namespace AdvancedSettings
{
class CustomSettingsProvider : SettingsProvider
{
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
base.Initialize(this.ApplicationName, config);
}
public override string ApplicationName
{
get { return System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; }
set { }
}
public static Action<Exception> ErrorHandler { get; set; }
private void OnError(Exception ex)
{
var handler = ErrorHandler;
if (handler != null)
handler(ex);
}
public static Action<string, object> MessageHandler { get; set; }
private void OnMessage(string message, object data)
{
var handler = MessageHandler;
if (handler != null)
handler(message, data);
}
private void OnMessage(string message)
{
OnMessage(message, null);
}
public static string XmlFileName = "settings.xml";
// XML
public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
{
var settings = new SettingsPropertyValueCollection();
try
{
XDocument doc = new XDocument();
bool hasFile;
if (string.IsNullOrEmpty(XmlFileName))
{
hasFile = false;
OnMessage(string.Format("Cannot find file {0}. Loading default settings instead.", XmlFileName));
}
else
{
try
{
doc = XDocument.Load(XmlFileName);
hasFile = true;
}
catch (FileNotFoundException)
{
hasFile = false;
OnMessage(string.Format("Cannot load file {0}. Loading default settings instead.", XmlFileName));
}
}
if (hasFile)
{
foreach (SettingsProperty propDef in collection)
{
var setting = new SettingsPropertyValue(propDef);
var allSettings = doc.Element("settings").Elements("setting");
var currentSetting = allSettings.SingleOrDefault(x => x.Attribute("name").Value == propDef.Name);
var settingValues = currentSetting.Elements("settingValue");
if (settingValues.Count() > 0)
{
var settingElement = settingValues.MaxBy(x => x.Attribute("dateTimeAdded").GetAs<DateTime>());
if (settingElement != null)
{
setting.PropertyValue = Convert.ChangeType(settingElement.Value, Type.GetType(currentSetting.Attribute("type").Value));
}
}
setting.IsDirty = false;
settings.Add(setting);
}
}
else
{
foreach (SettingsProperty propDef in collection)
{
var setting = new SettingsPropertyValue(propDef);
setting.IsDirty = false;
settings.Add(setting);
}
}
}
catch (Exception ex)
{
OnError(ex);
}
return settings;
}
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
{
try
{
if (!string.IsNullOrEmpty(XmlFileName))
{
bool isNewFile;
XDocument doc;
try
{
doc = XDocument.Load(XmlFileName);
isNewFile = false;
}
catch (FileNotFoundException)
{
doc = new XDocument();
isNewFile = true;
OnMessage(string.Format("Creating new Settings file at: {0}", XmlFileName));
}
if (isNewFile)
{
List<XElement> elements = new List<XElement>();
foreach (SettingsPropertyValue setting in collection)
{
// add header elements for each setting
XElement el =
new XElement("setting",
new XAttribute("dateTimeAdded", DateTime.Now),
new XAttribute("defaultValue", setting.Property.DefaultValue),
new XAttribute("type", setting.Property.PropertyType.FullName),
new XAttribute("name", setting.Name));
bool isDefault;
var a = setting.PropertyValue.ToString();
var b = setting.Property.DefaultValue.ToString();
if (setting.Property.PropertyType == typeof(bool))
{
isDefault = a.Equals(b, StringComparison.InvariantCultureIgnoreCase);
}
else
{
isDefault = a.Equals(b);
}
if (!isDefault)
{
SaveSettingHistory att = setting.Property.Attributes.ContainsAttribute<SaveSettingHistory>();
if (att == null || att.Save)
{ // if the attribute doesn't exist, or we are asking to save history, add new line below
XElement el1 = new XElement("settingValue");
el1.SetAttributeValue("dateTimeAdded", DateTime.Now);
el1.Value = setting.PropertyValue.ToString();
el.Add(el1);
}
else
{ // we have explicitly said not to save history, so change the default value to the current value
el.SetAttributeValue("defaultValue", setting.PropertyValue);
}
}
elements.Add(el);
}
XElement settingsElement = new XElement("settings", elements);
settingsElement.SetAttributeValue("machineName", Environment.MachineName);
settingsElement.SetAttributeValue("dateTimeSaved", DateTime.Now);
doc.Add(settingsElement);
}
else
{
// only add elements where the setting has changed or is not the default value.
// a header element should already exist unless it is a new version with new settings, so check if header exists.
foreach (SettingsPropertyValue setting in collection)
{
if (setting.IsDirty)
{
var settingElement = doc.Element("settings").Elements("setting").SingleOrDefault(x => x.Attribute("name").Value == setting.Name);
if (settingElement == null)
{
// create a new header
settingElement = new XElement("setting",
new XAttribute("dateTimeAdded", DateTime.Now),
new XAttribute("defaultValue", setting.Property.DefaultValue),
new XAttribute("type", setting.Property.PropertyType.FullName),
new XAttribute("name", setting.Name));
}
var settingsElements = settingElement.Elements("settingValue");
if (settingsElements.Count() == 0)
{
// only add if not default
bool isDefault;
var a = setting.PropertyValue.ToString();
var b = setting.Property.DefaultValue.ToString();
if (setting.Property.PropertyType == typeof(bool))
{
isDefault = a.Equals(b, StringComparison.InvariantCultureIgnoreCase);
}
else
{
isDefault = a.Equals(b);
}
if (isDefault)
{
continue;
}
}
else
{
// only add if different from latest setting
var latestSetting = settingsElements.MaxBy(x => x.Attribute("dateTimeAdded").GetAs<DateTime>()); //.Single(x => Convert.ToDateTime(x.Attribute("dateTimeAdded").Value) == maxDateTimeAdded);
var a = setting.PropertyValue.ToString();
var b = latestSetting.Value;
if ((setting.Property.PropertyType == typeof(bool) && a.Equals(b, StringComparison.InvariantCultureIgnoreCase))
|| a.Equals(b))
{
continue;
}
}
SaveSettingHistory att = setting.Property.Attributes.ContainsAttribute<SaveSettingHistory>();
if (att == null || att.Save)
{
XElement newElement = new XElement("settingValue");
newElement.SetAttributeValue("dateTimeAdded", DateTime.Now);
newElement.Value = setting.PropertyValue.ToString();
settingElement.Add(newElement);
}
else
{
settingElement.SetAttributeValue("defaultValue", setting.PropertyValue);
}
}
}
}
doc.Element("settings").SetAttributeValue("machineName", Environment.MachineName);
doc.Element("settings").SetAttributeValue("dateTimeSaved", DateTime.Now);
doc.Save(XmlFileName);
}
else
{
throw new FileNotFoundException("Need to specify the XML file name for the settings file (XmlFileName).");
}
}
catch (Exception ex)
{
OnError(ex);
}
}
}
[System.AttributeUsage(System.AttributeTargets.Property, AllowMultiple = false)]
internal class SaveSettingHistory : System.Attribute
{
public bool Save { get; set; }
public SaveSettingHistory()
{
}
public SaveSettingHistory(bool save)
{
this.Save = save;
}
}
}