Fixed Issue 162.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 The contents of this file are subject to the Mozilla Public License Version
6 1.1 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.mozilla.org/MPL/
11 Software distributed under the License is distributed on an "AS IS" basis,
12 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 for the specific language governing rights and limitations under the License.
15 The Original Code is the Open Hardware Monitor code.
17 The Initial Developer of the Original Code is
18 Michael Möller <m.moeller@gmx.ch>.
19 Portions created by the Initial Developer are Copyright (C) 2009-2010
20 the Initial Developer. All Rights Reserved.
24 Alternatively, the contents of this file may be used under the terms of
25 either the GNU General Public License Version 2 or later (the "GPL"), or
26 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 in which case the provisions of the GPL or the LGPL are applicable instead
28 of those above. If you wish to allow use of your version of this file only
29 under the terms of either the GPL or the LGPL, and not to allow others to
30 use your version of this file under the terms of the MPL, indicate your
31 decision by deleting the provisions above and replace them with the notice
32 and other provisions required by the GPL or the LGPL. If you do not delete
33 the provisions above, a recipient may use your version of this file under
34 the terms of any one of the MPL, the GPL or the LGPL.
38 using System.Collections.Generic;
39 using System.Globalization;
42 using OpenHardwareMonitor.Hardware;
44 namespace OpenHardwareMonitor {
45 public class PersistentSettings : ISettings {
47 private IDictionary<string, string> settings =
48 new Dictionary<string, string>();
50 public void Load(string fileName) {
51 XmlDocument doc = new XmlDocument();
57 XmlNodeList list = doc.GetElementsByTagName("appSettings");
58 foreach (XmlNode node in list) {
59 XmlNode parent = node.ParentNode;
60 if (parent != null && parent.Name == "configuration" &&
61 parent.ParentNode is XmlDocument) {
62 foreach (XmlNode child in node.ChildNodes) {
63 if (child.Name == "add") {
64 XmlAttributeCollection attributes = child.Attributes;
65 XmlAttribute keyAttribute = attributes["key"];
66 XmlAttribute valueAttribute = attributes["value"];
67 if (keyAttribute != null && valueAttribute != null &&
68 keyAttribute.Value != null) {
69 settings.Add(keyAttribute.Value, valueAttribute.Value);
77 public void Save(string fileName) {
78 XmlDocument doc = new XmlDocument();
79 doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));
80 XmlElement configuration = doc.CreateElement("configuration");
81 doc.AppendChild(configuration);
82 XmlElement appSettings = doc.CreateElement("appSettings");
83 configuration.AppendChild(appSettings);
84 foreach (KeyValuePair<string, string> keyValuePair in settings) {
85 XmlElement add = doc.CreateElement("add");
86 add.SetAttribute("key", keyValuePair.Key);
87 add.SetAttribute("value", keyValuePair.Value);
88 appSettings.AppendChild(add);
93 public bool Contains(string name) {
94 return settings.ContainsKey(name);
97 public void SetValue(string name, string value) {
98 settings[name] = value;
101 public string GetValue(string name, string value) {
103 if (settings.TryGetValue(name, out result))
109 public void Remove(string name) {
110 settings.Remove(name);
113 public void SetValue(string name, int value) {
114 settings[name] = value.ToString();
117 public int GetValue(string name, int value) {
119 if (settings.TryGetValue(name, out str)) {
121 if (int.TryParse(str, out parsedValue))
130 public void SetValue(string name, float value) {
131 settings[name] = value.ToString(CultureInfo.InvariantCulture);
134 public float GetValue(string name, float value) {
136 if (settings.TryGetValue(name, out str)) {
138 if (float.TryParse(str, NumberStyles.Float,
139 CultureInfo.InvariantCulture, out parsedValue))
148 public void SetValue(string name, bool value) {
149 settings[name] = value ? "true" : "false";
152 public bool GetValue(string name, bool value) {
154 if (settings.TryGetValue(name, out str)) {
155 return str == "true";
161 public void SetValue(string name, Color color) {
162 settings[name] = color.ToArgb().ToString("X8");
165 public Color GetValue(string name, Color value) {
167 if (settings.TryGetValue(name, out str)) {
169 if (int.TryParse(str, NumberStyles.HexNumber,
170 CultureInfo.InvariantCulture, out parsedValue))
171 return Color.FromArgb(parsedValue);