Utilities/PersistentSettings.cs
author moel.mich
Sun, 27 May 2012 14:23:31 +0000
changeset 344 3145aadca3d2
parent 183 3096735e99b2
child 429 5d87c9fc69f6
permissions -rw-r--r--
Changed the license to the Mozilla Public License 2.0 and update the licensing information.
     1 /*
     2  
     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/.
     6  
     7   Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System.Collections.Generic;
    12 using System.Globalization;
    13 using System.Drawing;
    14 using System.Xml;
    15 using OpenHardwareMonitor.Hardware;
    16 
    17 namespace OpenHardwareMonitor {
    18   public class PersistentSettings : ISettings {
    19 
    20     private IDictionary<string, string> settings = 
    21       new Dictionary<string, string>();
    22 
    23     public void Load(string fileName) {
    24       XmlDocument doc = new XmlDocument();
    25       try {
    26         doc.Load(fileName);
    27       } catch {
    28         return;
    29       }
    30       XmlNodeList list = doc.GetElementsByTagName("appSettings");
    31       foreach (XmlNode node in list) {
    32         XmlNode parent = node.ParentNode;
    33         if (parent != null && parent.Name == "configuration" && 
    34           parent.ParentNode is XmlDocument) {
    35           foreach (XmlNode child in node.ChildNodes) {
    36             if (child.Name == "add") {
    37               XmlAttributeCollection attributes = child.Attributes;
    38               XmlAttribute keyAttribute = attributes["key"];
    39               XmlAttribute valueAttribute = attributes["value"];
    40               if (keyAttribute != null && valueAttribute != null && 
    41                 keyAttribute.Value != null) {
    42                 settings.Add(keyAttribute.Value, valueAttribute.Value);
    43               }
    44             }
    45           }
    46         }
    47       }      
    48     }
    49 
    50     public void Save(string fileName) {
    51       XmlDocument doc = new XmlDocument();
    52       doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));
    53       XmlElement configuration = doc.CreateElement("configuration");
    54       doc.AppendChild(configuration);
    55       XmlElement appSettings = doc.CreateElement("appSettings");
    56       configuration.AppendChild(appSettings);
    57       foreach (KeyValuePair<string, string> keyValuePair in settings) {
    58         XmlElement add = doc.CreateElement("add");
    59         add.SetAttribute("key", keyValuePair.Key);
    60         add.SetAttribute("value", keyValuePair.Value);
    61         appSettings.AppendChild(add);
    62       }
    63       doc.Save(fileName);
    64     }
    65 
    66     public bool Contains(string name) {
    67       return settings.ContainsKey(name);
    68     }
    69 
    70     public void SetValue(string name, string value) {
    71       settings[name] = value;
    72     }
    73 
    74     public string GetValue(string name, string value) {
    75       string result;
    76       if (settings.TryGetValue(name, out result))
    77         return result;
    78       else
    79         return value;
    80     }
    81 
    82     public void Remove(string name) {
    83       settings.Remove(name);
    84     }
    85 
    86     public void SetValue(string name, int value) {
    87       settings[name] = value.ToString();
    88     }
    89 
    90     public int GetValue(string name, int value) {
    91       string str;
    92       if (settings.TryGetValue(name, out str)) {
    93         int parsedValue;
    94         if (int.TryParse(str, out parsedValue))
    95           return parsedValue;
    96         else
    97           return value;
    98       } else {
    99         return value;
   100       }
   101     }
   102 
   103     public void SetValue(string name, float value) {
   104       settings[name] = value.ToString(CultureInfo.InvariantCulture);
   105     }
   106 
   107     public float GetValue(string name, float value) {
   108       string str;
   109       if (settings.TryGetValue(name, out str)) {
   110         float parsedValue;
   111         if (float.TryParse(str, NumberStyles.Float, 
   112           CultureInfo.InvariantCulture, out parsedValue))
   113           return parsedValue;
   114         else
   115           return value;
   116       } else {
   117         return value;
   118       }
   119     }
   120 
   121     public void SetValue(string name, bool value) {
   122       settings[name] = value ? "true" : "false";
   123     }
   124 
   125     public bool GetValue(string name, bool value) {
   126       string str;
   127       if (settings.TryGetValue(name, out str)) {
   128         return str == "true";
   129       } else {
   130         return value;
   131       }
   132     }
   133 
   134     public void SetValue(string name, Color color) {
   135       settings[name] = color.ToArgb().ToString("X8");
   136     }
   137 
   138     public Color GetValue(string name, Color value) {
   139       string str;
   140       if (settings.TryGetValue(name, out str)) {
   141         int parsedValue;
   142         if (int.TryParse(str, NumberStyles.HexNumber,
   143           CultureInfo.InvariantCulture, out parsedValue))
   144           return Color.FromArgb(parsedValue);
   145         else
   146           return value;
   147       } else {
   148         return value;
   149       }
   150     }
   151   }
   152 }