Utilities/PersistentSettings.cs
author moel.mich
Wed, 08 Sep 2010 19:29:58 +0000
changeset 182 4801e9eaf979
parent 165 813d8bc3192f
child 183 3096735e99b2
permissions -rw-r--r--
Fixed some Code Analysis warnings.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     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
     8  
     9   http://www.mozilla.org/MPL/
    10 
    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.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    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.
    21 
    22   Contributor(s):
    23 
    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.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.Drawing;
    41 using System.Text;
    42 using System.Xml;
    43 using OpenHardwareMonitor.Hardware;
    44 
    45 namespace OpenHardwareMonitor {
    46   public class PersistentSettings : ISettings {
    47 
    48     private IDictionary<string, string> settings = 
    49       new Dictionary<string, string>();
    50 
    51     public void Load(string fileName) {
    52       XmlDocument doc = new XmlDocument();
    53       try {
    54         doc.Load(fileName);
    55       } catch {
    56         return;
    57       }
    58       XmlNodeList list = doc.GetElementsByTagName("appSettings");
    59       foreach (XmlNode node in list) {
    60         XmlNode parent = node.ParentNode;
    61         if (parent != null && parent.Name == "configuration" && 
    62           parent.ParentNode is XmlDocument) {
    63           foreach (XmlNode child in node.ChildNodes) {
    64             if (child.Name == "add") {
    65               XmlAttributeCollection attributes = child.Attributes;
    66               XmlAttribute keyAttribute = attributes["key"];
    67               XmlAttribute valueAttribute = attributes["value"];
    68               if (keyAttribute != null && valueAttribute != null && 
    69                 keyAttribute.Value != null) {
    70                 settings.Add(keyAttribute.Value, valueAttribute.Value);
    71               }
    72             }
    73           }
    74         }
    75       }      
    76     }
    77 
    78     public void Save(string fileName) {
    79       XmlDocument doc = new XmlDocument();
    80       doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));
    81       XmlElement configuration = doc.CreateElement("configuration");
    82       doc.AppendChild(configuration);
    83       XmlElement appSettings = doc.CreateElement("appSettings");
    84       configuration.AppendChild(appSettings);
    85       foreach (KeyValuePair<string, string> keyValuePair in settings) {
    86         XmlElement add = doc.CreateElement("add");
    87         add.SetAttribute("key", keyValuePair.Key);
    88         add.SetAttribute("value", keyValuePair.Value);
    89         appSettings.AppendChild(add);
    90       }
    91       doc.Save(fileName);
    92     }
    93 
    94     public bool Contains(string name) {
    95       return settings.ContainsKey(name);
    96     }
    97 
    98     public void SetValue(string name, string value) {
    99       settings[name] = value;
   100     }
   101 
   102     public string GetValue(string name, string value) {
   103       string result;
   104       if (settings.TryGetValue(name, out result))
   105         return result;
   106       else
   107         return value;
   108     }
   109 
   110     public void Remove(string name) {
   111       settings.Remove(name);
   112     }
   113 
   114     public void SetValue(string name, int value) {
   115       settings[name] = value.ToString();
   116     }
   117 
   118     public int GetValue(string name, int value) {
   119       string str;
   120       if (settings.TryGetValue(name, out str)) {
   121         int parsedValue;
   122         if (int.TryParse(str, out parsedValue))
   123           return parsedValue;
   124         else
   125           return value;
   126       } else {
   127         return value;
   128       }
   129     }
   130 
   131     public void SetValue(string name, bool value) {
   132       settings[name] = value ? "true" : "false";
   133     }
   134 
   135     public bool GetValue(string name, bool value) {
   136       string str;
   137       if (settings.TryGetValue(name, out str)) {
   138         return str == "true";
   139       } else {
   140         return value;
   141       }
   142     }
   143 
   144     public void SetValue(string name, Color color) {
   145       settings[name] = color.ToArgb().ToString("X8");
   146     }
   147 
   148     public Color GetValue(string name, Color value) {
   149       string str;
   150       if (settings.TryGetValue(name, out str)) {
   151         int parsedValue;
   152         if (int.TryParse(str,
   153           System.Globalization.NumberStyles.HexNumber,
   154           System.Globalization.CultureInfo.InvariantCulture, out parsedValue))
   155           return Color.FromArgb(parsedValue);
   156         else
   157           return value;
   158       } else {
   159         return value;
   160       }
   161     }
   162   }
   163 }