Sync: Adding shamap and documentation to enable easier sync next time OHM is updated.
3 This Source Code Form is subject to the terms of the Mozilla Public
4 License, v. 2.0. If a copy of the MPL was not distributed with this
5 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 Copyright (C) 2009-2014 Michael Möller <mmoeller@openhardwaremonitor.org>
11 using System.Collections.Generic;
13 using System.Globalization;
17 using OpenHardwareMonitor.Hardware;
19 namespace OpenHardwareMonitor {
20 public class PersistentSettings : ISettings {
22 private IDictionary<string, string> settings =
23 new Dictionary<string, string>();
25 public void Load(string fileName) {
26 XmlDocument doc = new XmlDocument();
31 File.Delete(fileName);
34 string backupFileName = fileName + ".backup";
36 doc.Load(backupFileName);
39 File.Delete(backupFileName);
46 XmlNodeList list = doc.GetElementsByTagName("appSettings");
47 foreach (XmlNode node in list) {
48 XmlNode parent = node.ParentNode;
49 if (parent != null && parent.Name == "configuration" &&
50 parent.ParentNode is XmlDocument) {
51 foreach (XmlNode child in node.ChildNodes) {
52 if (child.Name == "add") {
53 XmlAttributeCollection attributes = child.Attributes;
54 XmlAttribute keyAttribute = attributes["key"];
55 XmlAttribute valueAttribute = attributes["value"];
56 if (keyAttribute != null && valueAttribute != null &&
57 keyAttribute.Value != null) {
58 settings.Add(keyAttribute.Value, valueAttribute.Value);
66 public void Save(string fileName) {
68 XmlDocument doc = new XmlDocument();
69 doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));
70 XmlElement configuration = doc.CreateElement("configuration");
71 doc.AppendChild(configuration);
72 XmlElement appSettings = doc.CreateElement("appSettings");
73 configuration.AppendChild(appSettings);
74 foreach (KeyValuePair<string, string> keyValuePair in settings) {
75 XmlElement add = doc.CreateElement("add");
76 add.SetAttribute("key", keyValuePair.Key);
77 add.SetAttribute("value", keyValuePair.Value);
78 appSettings.AppendChild(add);
82 using (var memory = new MemoryStream()) {
83 using (var writer = new StreamWriter(memory, Encoding.UTF8)) {
86 file = memory.ToArray();
89 string backupFileName = fileName + ".backup";
90 if (File.Exists(fileName)) {
92 File.Delete(backupFileName);
95 File.Move(fileName, backupFileName);
99 using (var stream = new FileStream(fileName,
100 FileMode.Create, FileAccess.Write))
102 stream.Write(file, 0, file.Length);
106 File.Delete(backupFileName);
110 public bool Contains(string name) {
111 return settings.ContainsKey(name);
114 public void SetValue(string name, string value) {
115 settings[name] = value;
118 public string GetValue(string name, string value) {
120 if (settings.TryGetValue(name, out result))
126 public void Remove(string name) {
127 settings.Remove(name);
130 public void SetValue(string name, int value) {
131 settings[name] = value.ToString();
134 public int GetValue(string name, int value) {
136 if (settings.TryGetValue(name, out str)) {
138 if (int.TryParse(str, out parsedValue))
147 public void SetValue(string name, float value) {
148 settings[name] = value.ToString(CultureInfo.InvariantCulture);
151 public float GetValue(string name, float value) {
153 if (settings.TryGetValue(name, out str)) {
155 if (float.TryParse(str, NumberStyles.Float,
156 CultureInfo.InvariantCulture, out parsedValue))
165 public void SetValue(string name, bool value) {
166 settings[name] = value ? "true" : "false";
169 public bool GetValue(string name, bool value) {
171 if (settings.TryGetValue(name, out str)) {
172 return str == "true";
178 public void SetValue(string name, Color color) {
179 settings[name] = color.ToArgb().ToString("X8");
182 public Color GetValue(string name, Color value) {
184 if (settings.TryGetValue(name, out str)) {
186 if (int.TryParse(str, NumberStyles.HexNumber,
187 CultureInfo.InvariantCulture, out parsedValue))
188 return Color.FromArgb(parsedValue);