1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/Utilities/PersistentSettings.cs Sun Aug 08 13:57:26 2010 +0000
1.3 @@ -0,0 +1,163 @@
1.4 +/*
1.5 +
1.6 + Version: MPL 1.1/GPL 2.0/LGPL 2.1
1.7 +
1.8 + The contents of this file are subject to the Mozilla Public License Version
1.9 + 1.1 (the "License"); you may not use this file except in compliance with
1.10 + the License. You may obtain a copy of the License at
1.11 +
1.12 + http://www.mozilla.org/MPL/
1.13 +
1.14 + Software distributed under the License is distributed on an "AS IS" basis,
1.15 + WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1.16 + for the specific language governing rights and limitations under the License.
1.17 +
1.18 + The Original Code is the Open Hardware Monitor code.
1.19 +
1.20 + The Initial Developer of the Original Code is
1.21 + Michael Möller <m.moeller@gmx.ch>.
1.22 + Portions created by the Initial Developer are Copyright (C) 2009-2010
1.23 + the Initial Developer. All Rights Reserved.
1.24 +
1.25 + Contributor(s):
1.26 +
1.27 + Alternatively, the contents of this file may be used under the terms of
1.28 + either the GNU General Public License Version 2 or later (the "GPL"), or
1.29 + the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
1.30 + in which case the provisions of the GPL or the LGPL are applicable instead
1.31 + of those above. If you wish to allow use of your version of this file only
1.32 + under the terms of either the GPL or the LGPL, and not to allow others to
1.33 + use your version of this file under the terms of the MPL, indicate your
1.34 + decision by deleting the provisions above and replace them with the notice
1.35 + and other provisions required by the GPL or the LGPL. If you do not delete
1.36 + the provisions above, a recipient may use your version of this file under
1.37 + the terms of any one of the MPL, the GPL or the LGPL.
1.38 +
1.39 +*/
1.40 +
1.41 +using System;
1.42 +using System.Collections.Generic;
1.43 +using System.Drawing;
1.44 +using System.Text;
1.45 +using System.Xml;
1.46 +using OpenHardwareMonitor.Hardware;
1.47 +
1.48 +namespace OpenHardwareMonitor {
1.49 + public class PersistentSettings : ISettings {
1.50 +
1.51 + private IDictionary<string, string> settings =
1.52 + new Dictionary<string, string>();
1.53 +
1.54 + public void Load(string fileName) {
1.55 + XmlDocument doc = new XmlDocument();
1.56 + try {
1.57 + doc.Load(fileName);
1.58 + } catch {
1.59 + return;
1.60 + }
1.61 + XmlNodeList list = doc.GetElementsByTagName("appSettings");
1.62 + foreach (XmlNode node in list) {
1.63 + XmlNode parent = node.ParentNode;
1.64 + if (parent != null && parent.Name == "configuration" &&
1.65 + parent.ParentNode is XmlDocument) {
1.66 + foreach (XmlNode child in node.ChildNodes) {
1.67 + if (child.Name == "add") {
1.68 + XmlAttributeCollection attributes = child.Attributes;
1.69 + XmlAttribute keyAttribute = attributes["key"];
1.70 + XmlAttribute valueAttribute = attributes["value"];
1.71 + if (keyAttribute != null && valueAttribute != null &&
1.72 + keyAttribute.Value != null) {
1.73 + settings.Add(keyAttribute.Value, valueAttribute.Value);
1.74 + }
1.75 + }
1.76 + }
1.77 + }
1.78 + }
1.79 + }
1.80 +
1.81 + public void Save(string fileName) {
1.82 + XmlDocument doc = new XmlDocument();
1.83 + doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));
1.84 + XmlElement configuration = doc.CreateElement("configuration");
1.85 + doc.AppendChild(configuration);
1.86 + XmlElement appSettings = doc.CreateElement("appSettings");
1.87 + configuration.AppendChild(appSettings);
1.88 + foreach (KeyValuePair<string, string> keyValuePair in settings) {
1.89 + XmlElement add = doc.CreateElement("add");
1.90 + add.SetAttribute("key", keyValuePair.Key);
1.91 + add.SetAttribute("value", keyValuePair.Value);
1.92 + appSettings.AppendChild(add);
1.93 + }
1.94 + doc.Save(fileName);
1.95 + }
1.96 +
1.97 + public bool Contains(string name) {
1.98 + return settings.ContainsKey(name);
1.99 + }
1.100 +
1.101 + public void Set(string name, string value) {
1.102 + settings[name] = value;
1.103 + }
1.104 +
1.105 + public string Get(string name, string value) {
1.106 + string result;
1.107 + if (settings.TryGetValue(name, out result))
1.108 + return result;
1.109 + else
1.110 + return value;
1.111 + }
1.112 +
1.113 + public void Remove(string name) {
1.114 + settings.Remove(name);
1.115 + }
1.116 +
1.117 + public void Set(string name, int value) {
1.118 + settings[name] = value.ToString();
1.119 + }
1.120 +
1.121 + public int Get(string name, int value) {
1.122 + string str;
1.123 + if (settings.TryGetValue(name, out str)) {
1.124 + int parsedValue;
1.125 + if (int.TryParse(str, out parsedValue))
1.126 + return parsedValue;
1.127 + else
1.128 + return value;
1.129 + } else {
1.130 + return value;
1.131 + }
1.132 + }
1.133 +
1.134 + public void Set(string name, bool value) {
1.135 + settings[name] = value ? "true" : "false";
1.136 + }
1.137 +
1.138 + public bool Get(string name, bool value) {
1.139 + string str;
1.140 + if (settings.TryGetValue(name, out str)) {
1.141 + return str == "true";
1.142 + } else {
1.143 + return value;
1.144 + }
1.145 + }
1.146 +
1.147 + public void Set(string name, Color color) {
1.148 + settings[name] = color.ToArgb().ToString("X8");
1.149 + }
1.150 +
1.151 + public Color Get(string name, Color value) {
1.152 + string str;
1.153 + if (settings.TryGetValue(name, out str)) {
1.154 + int parsedValue;
1.155 + if (int.TryParse(str,
1.156 + System.Globalization.NumberStyles.HexNumber,
1.157 + System.Globalization.CultureInfo.InvariantCulture, out parsedValue))
1.158 + return Color.FromArgb(parsedValue);
1.159 + else
1.160 + return value;
1.161 + } else {
1.162 + return value;
1.163 + }
1.164 + }
1.165 + }
1.166 +}