moel@165
|
1 |
/*
|
moel@165
|
2 |
|
moel@344
|
3 |
This Source Code Form is subject to the terms of the Mozilla Public
|
moel@344
|
4 |
License, v. 2.0. If a copy of the MPL was not distributed with this
|
moel@344
|
5 |
file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
moel@165
|
6 |
|
moel@429
|
7 |
Copyright (C) 2009-2014 Michael Möller <mmoeller@openhardwaremonitor.org>
|
moel@344
|
8 |
|
moel@165
|
9 |
*/
|
moel@165
|
10 |
|
moel@165
|
11 |
using System.Collections.Generic;
|
moel@429
|
12 |
using System.Drawing;
|
moel@183
|
13 |
using System.Globalization;
|
moel@429
|
14 |
using System.IO;
|
moel@429
|
15 |
using System.Text;
|
moel@165
|
16 |
using System.Xml;
|
moel@165
|
17 |
using OpenHardwareMonitor.Hardware;
|
moel@165
|
18 |
|
moel@165
|
19 |
namespace OpenHardwareMonitor {
|
moel@165
|
20 |
public class PersistentSettings : ISettings {
|
moel@165
|
21 |
|
moel@165
|
22 |
private IDictionary<string, string> settings =
|
moel@165
|
23 |
new Dictionary<string, string>();
|
moel@165
|
24 |
|
moel@165
|
25 |
public void Load(string fileName) {
|
moel@165
|
26 |
XmlDocument doc = new XmlDocument();
|
moel@165
|
27 |
try {
|
moel@165
|
28 |
doc.Load(fileName);
|
moel@165
|
29 |
} catch {
|
moel@429
|
30 |
try {
|
moel@429
|
31 |
File.Delete(fileName);
|
moel@429
|
32 |
} catch { }
|
moel@429
|
33 |
|
moel@429
|
34 |
string backupFileName = fileName + ".backup";
|
moel@429
|
35 |
try {
|
moel@429
|
36 |
doc.Load(backupFileName);
|
moel@429
|
37 |
} catch {
|
moel@429
|
38 |
try {
|
moel@429
|
39 |
File.Delete(backupFileName);
|
moel@429
|
40 |
} catch { }
|
moel@429
|
41 |
|
moel@429
|
42 |
return;
|
moel@429
|
43 |
}
|
moel@165
|
44 |
}
|
moel@429
|
45 |
|
moel@165
|
46 |
XmlNodeList list = doc.GetElementsByTagName("appSettings");
|
moel@165
|
47 |
foreach (XmlNode node in list) {
|
moel@165
|
48 |
XmlNode parent = node.ParentNode;
|
moel@165
|
49 |
if (parent != null && parent.Name == "configuration" &&
|
moel@165
|
50 |
parent.ParentNode is XmlDocument) {
|
moel@165
|
51 |
foreach (XmlNode child in node.ChildNodes) {
|
moel@165
|
52 |
if (child.Name == "add") {
|
moel@165
|
53 |
XmlAttributeCollection attributes = child.Attributes;
|
moel@165
|
54 |
XmlAttribute keyAttribute = attributes["key"];
|
moel@165
|
55 |
XmlAttribute valueAttribute = attributes["value"];
|
moel@165
|
56 |
if (keyAttribute != null && valueAttribute != null &&
|
moel@165
|
57 |
keyAttribute.Value != null) {
|
moel@165
|
58 |
settings.Add(keyAttribute.Value, valueAttribute.Value);
|
moel@165
|
59 |
}
|
moel@165
|
60 |
}
|
moel@165
|
61 |
}
|
moel@165
|
62 |
}
|
moel@429
|
63 |
}
|
moel@165
|
64 |
}
|
moel@165
|
65 |
|
moel@165
|
66 |
public void Save(string fileName) {
|
moel@429
|
67 |
|
moel@165
|
68 |
XmlDocument doc = new XmlDocument();
|
moel@165
|
69 |
doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));
|
moel@165
|
70 |
XmlElement configuration = doc.CreateElement("configuration");
|
moel@165
|
71 |
doc.AppendChild(configuration);
|
moel@165
|
72 |
XmlElement appSettings = doc.CreateElement("appSettings");
|
moel@165
|
73 |
configuration.AppendChild(appSettings);
|
moel@165
|
74 |
foreach (KeyValuePair<string, string> keyValuePair in settings) {
|
moel@165
|
75 |
XmlElement add = doc.CreateElement("add");
|
moel@165
|
76 |
add.SetAttribute("key", keyValuePair.Key);
|
moel@165
|
77 |
add.SetAttribute("value", keyValuePair.Value);
|
moel@165
|
78 |
appSettings.AppendChild(add);
|
moel@165
|
79 |
}
|
moel@429
|
80 |
|
moel@429
|
81 |
byte[] file;
|
moel@429
|
82 |
using (var memory = new MemoryStream()) {
|
moel@429
|
83 |
using (var writer = new StreamWriter(memory, Encoding.UTF8)) {
|
moel@429
|
84 |
doc.Save(writer);
|
moel@429
|
85 |
}
|
moel@429
|
86 |
file = memory.ToArray();
|
moel@429
|
87 |
}
|
moel@429
|
88 |
|
moel@429
|
89 |
string backupFileName = fileName + ".backup";
|
moel@429
|
90 |
if (File.Exists(fileName)) {
|
moel@429
|
91 |
try {
|
moel@429
|
92 |
File.Delete(backupFileName);
|
moel@429
|
93 |
} catch { }
|
moel@429
|
94 |
try {
|
moel@429
|
95 |
File.Move(fileName, backupFileName);
|
moel@429
|
96 |
} catch { }
|
moel@429
|
97 |
}
|
moel@429
|
98 |
|
moel@429
|
99 |
using (var stream = new FileStream(fileName,
|
moel@429
|
100 |
FileMode.Create, FileAccess.Write))
|
moel@429
|
101 |
{
|
moel@429
|
102 |
stream.Write(file, 0, file.Length);
|
moel@429
|
103 |
}
|
moel@429
|
104 |
|
moel@429
|
105 |
try {
|
moel@429
|
106 |
File.Delete(backupFileName);
|
moel@429
|
107 |
} catch { }
|
moel@165
|
108 |
}
|
moel@165
|
109 |
|
moel@165
|
110 |
public bool Contains(string name) {
|
moel@165
|
111 |
return settings.ContainsKey(name);
|
moel@165
|
112 |
}
|
moel@165
|
113 |
|
moel@166
|
114 |
public void SetValue(string name, string value) {
|
moel@165
|
115 |
settings[name] = value;
|
moel@165
|
116 |
}
|
moel@165
|
117 |
|
moel@166
|
118 |
public string GetValue(string name, string value) {
|
moel@165
|
119 |
string result;
|
moel@165
|
120 |
if (settings.TryGetValue(name, out result))
|
moel@165
|
121 |
return result;
|
moel@165
|
122 |
else
|
moel@165
|
123 |
return value;
|
moel@165
|
124 |
}
|
moel@165
|
125 |
|
moel@165
|
126 |
public void Remove(string name) {
|
moel@165
|
127 |
settings.Remove(name);
|
moel@165
|
128 |
}
|
moel@165
|
129 |
|
moel@166
|
130 |
public void SetValue(string name, int value) {
|
moel@165
|
131 |
settings[name] = value.ToString();
|
moel@165
|
132 |
}
|
moel@165
|
133 |
|
moel@166
|
134 |
public int GetValue(string name, int value) {
|
moel@165
|
135 |
string str;
|
moel@165
|
136 |
if (settings.TryGetValue(name, out str)) {
|
moel@165
|
137 |
int parsedValue;
|
moel@165
|
138 |
if (int.TryParse(str, out parsedValue))
|
moel@165
|
139 |
return parsedValue;
|
moel@165
|
140 |
else
|
moel@165
|
141 |
return value;
|
moel@165
|
142 |
} else {
|
moel@165
|
143 |
return value;
|
moel@165
|
144 |
}
|
moel@165
|
145 |
}
|
moel@165
|
146 |
|
moel@183
|
147 |
public void SetValue(string name, float value) {
|
moel@183
|
148 |
settings[name] = value.ToString(CultureInfo.InvariantCulture);
|
moel@183
|
149 |
}
|
moel@183
|
150 |
|
moel@183
|
151 |
public float GetValue(string name, float value) {
|
moel@183
|
152 |
string str;
|
moel@183
|
153 |
if (settings.TryGetValue(name, out str)) {
|
moel@183
|
154 |
float parsedValue;
|
moel@183
|
155 |
if (float.TryParse(str, NumberStyles.Float,
|
moel@183
|
156 |
CultureInfo.InvariantCulture, out parsedValue))
|
moel@183
|
157 |
return parsedValue;
|
moel@183
|
158 |
else
|
moel@183
|
159 |
return value;
|
moel@183
|
160 |
} else {
|
moel@183
|
161 |
return value;
|
moel@183
|
162 |
}
|
moel@183
|
163 |
}
|
moel@183
|
164 |
|
moel@166
|
165 |
public void SetValue(string name, bool value) {
|
moel@165
|
166 |
settings[name] = value ? "true" : "false";
|
moel@165
|
167 |
}
|
moel@165
|
168 |
|
moel@166
|
169 |
public bool GetValue(string name, bool value) {
|
moel@165
|
170 |
string str;
|
moel@165
|
171 |
if (settings.TryGetValue(name, out str)) {
|
moel@165
|
172 |
return str == "true";
|
moel@165
|
173 |
} else {
|
moel@165
|
174 |
return value;
|
moel@165
|
175 |
}
|
moel@165
|
176 |
}
|
moel@165
|
177 |
|
moel@166
|
178 |
public void SetValue(string name, Color color) {
|
moel@165
|
179 |
settings[name] = color.ToArgb().ToString("X8");
|
moel@165
|
180 |
}
|
moel@165
|
181 |
|
moel@166
|
182 |
public Color GetValue(string name, Color value) {
|
moel@165
|
183 |
string str;
|
moel@165
|
184 |
if (settings.TryGetValue(name, out str)) {
|
moel@165
|
185 |
int parsedValue;
|
moel@183
|
186 |
if (int.TryParse(str, NumberStyles.HexNumber,
|
moel@183
|
187 |
CultureInfo.InvariantCulture, out parsedValue))
|
moel@165
|
188 |
return Color.FromArgb(parsedValue);
|
moel@165
|
189 |
else
|
moel@165
|
190 |
return value;
|
moel@165
|
191 |
} else {
|
moel@165
|
192 |
return value;
|
moel@165
|
193 |
}
|
moel@165
|
194 |
}
|
moel@165
|
195 |
}
|
moel@165
|
196 |
}
|