Utilities/PersistentSettings.cs
author moel.mich
Fri, 17 Sep 2010 18:32:11 +0000
changeset 187 a7c93597137f
parent 166 fa9dfbfc4145
child 344 3145aadca3d2
permissions -rw-r--r--
Fixed an InvalidOperationException in OpenHardwareMonitor.GUI.SensorGadget.OnPaint caused by sensors with null as value.
moel@165
     1
/*
moel@165
     2
  
moel@165
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@165
     4
moel@165
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@165
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@165
     7
  the License. You may obtain a copy of the License at
moel@165
     8
 
moel@165
     9
  http://www.mozilla.org/MPL/
moel@165
    10
moel@165
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@165
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@165
    13
  for the specific language governing rights and limitations under the License.
moel@165
    14
moel@165
    15
  The Original Code is the Open Hardware Monitor code.
moel@165
    16
moel@165
    17
  The Initial Developer of the Original Code is 
moel@165
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@165
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@165
    20
  the Initial Developer. All Rights Reserved.
moel@165
    21
moel@165
    22
  Contributor(s):
moel@165
    23
moel@165
    24
  Alternatively, the contents of this file may be used under the terms of
moel@165
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@165
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@165
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@165
    28
  of those above. If you wish to allow use of your version of this file only
moel@165
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@165
    30
  use your version of this file under the terms of the MPL, indicate your
moel@165
    31
  decision by deleting the provisions above and replace them with the notice
moel@165
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@165
    33
  the provisions above, a recipient may use your version of this file under
moel@165
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@165
    35
 
moel@165
    36
*/
moel@165
    37
moel@165
    38
using System.Collections.Generic;
moel@183
    39
using System.Globalization;
moel@165
    40
using System.Drawing;
moel@165
    41
using System.Xml;
moel@165
    42
using OpenHardwareMonitor.Hardware;
moel@165
    43
moel@165
    44
namespace OpenHardwareMonitor {
moel@165
    45
  public class PersistentSettings : ISettings {
moel@165
    46
moel@165
    47
    private IDictionary<string, string> settings = 
moel@165
    48
      new Dictionary<string, string>();
moel@165
    49
moel@165
    50
    public void Load(string fileName) {
moel@165
    51
      XmlDocument doc = new XmlDocument();
moel@165
    52
      try {
moel@165
    53
        doc.Load(fileName);
moel@165
    54
      } catch {
moel@165
    55
        return;
moel@165
    56
      }
moel@165
    57
      XmlNodeList list = doc.GetElementsByTagName("appSettings");
moel@165
    58
      foreach (XmlNode node in list) {
moel@165
    59
        XmlNode parent = node.ParentNode;
moel@165
    60
        if (parent != null && parent.Name == "configuration" && 
moel@165
    61
          parent.ParentNode is XmlDocument) {
moel@165
    62
          foreach (XmlNode child in node.ChildNodes) {
moel@165
    63
            if (child.Name == "add") {
moel@165
    64
              XmlAttributeCollection attributes = child.Attributes;
moel@165
    65
              XmlAttribute keyAttribute = attributes["key"];
moel@165
    66
              XmlAttribute valueAttribute = attributes["value"];
moel@165
    67
              if (keyAttribute != null && valueAttribute != null && 
moel@165
    68
                keyAttribute.Value != null) {
moel@165
    69
                settings.Add(keyAttribute.Value, valueAttribute.Value);
moel@165
    70
              }
moel@165
    71
            }
moel@165
    72
          }
moel@165
    73
        }
moel@165
    74
      }      
moel@165
    75
    }
moel@165
    76
moel@165
    77
    public void Save(string fileName) {
moel@165
    78
      XmlDocument doc = new XmlDocument();
moel@165
    79
      doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));
moel@165
    80
      XmlElement configuration = doc.CreateElement("configuration");
moel@165
    81
      doc.AppendChild(configuration);
moel@165
    82
      XmlElement appSettings = doc.CreateElement("appSettings");
moel@165
    83
      configuration.AppendChild(appSettings);
moel@165
    84
      foreach (KeyValuePair<string, string> keyValuePair in settings) {
moel@165
    85
        XmlElement add = doc.CreateElement("add");
moel@165
    86
        add.SetAttribute("key", keyValuePair.Key);
moel@165
    87
        add.SetAttribute("value", keyValuePair.Value);
moel@165
    88
        appSettings.AppendChild(add);
moel@165
    89
      }
moel@165
    90
      doc.Save(fileName);
moel@165
    91
    }
moel@165
    92
moel@165
    93
    public bool Contains(string name) {
moel@165
    94
      return settings.ContainsKey(name);
moel@165
    95
    }
moel@165
    96
moel@166
    97
    public void SetValue(string name, string value) {
moel@165
    98
      settings[name] = value;
moel@165
    99
    }
moel@165
   100
moel@166
   101
    public string GetValue(string name, string value) {
moel@165
   102
      string result;
moel@165
   103
      if (settings.TryGetValue(name, out result))
moel@165
   104
        return result;
moel@165
   105
      else
moel@165
   106
        return value;
moel@165
   107
    }
moel@165
   108
moel@165
   109
    public void Remove(string name) {
moel@165
   110
      settings.Remove(name);
moel@165
   111
    }
moel@165
   112
moel@166
   113
    public void SetValue(string name, int value) {
moel@165
   114
      settings[name] = value.ToString();
moel@165
   115
    }
moel@165
   116
moel@166
   117
    public int GetValue(string name, int value) {
moel@165
   118
      string str;
moel@165
   119
      if (settings.TryGetValue(name, out str)) {
moel@165
   120
        int parsedValue;
moel@165
   121
        if (int.TryParse(str, out parsedValue))
moel@165
   122
          return parsedValue;
moel@165
   123
        else
moel@165
   124
          return value;
moel@165
   125
      } else {
moel@165
   126
        return value;
moel@165
   127
      }
moel@165
   128
    }
moel@165
   129
moel@183
   130
    public void SetValue(string name, float value) {
moel@183
   131
      settings[name] = value.ToString(CultureInfo.InvariantCulture);
moel@183
   132
    }
moel@183
   133
moel@183
   134
    public float GetValue(string name, float value) {
moel@183
   135
      string str;
moel@183
   136
      if (settings.TryGetValue(name, out str)) {
moel@183
   137
        float parsedValue;
moel@183
   138
        if (float.TryParse(str, NumberStyles.Float, 
moel@183
   139
          CultureInfo.InvariantCulture, out parsedValue))
moel@183
   140
          return parsedValue;
moel@183
   141
        else
moel@183
   142
          return value;
moel@183
   143
      } else {
moel@183
   144
        return value;
moel@183
   145
      }
moel@183
   146
    }
moel@183
   147
moel@166
   148
    public void SetValue(string name, bool value) {
moel@165
   149
      settings[name] = value ? "true" : "false";
moel@165
   150
    }
moel@165
   151
moel@166
   152
    public bool GetValue(string name, bool value) {
moel@165
   153
      string str;
moel@165
   154
      if (settings.TryGetValue(name, out str)) {
moel@165
   155
        return str == "true";
moel@165
   156
      } else {
moel@165
   157
        return value;
moel@165
   158
      }
moel@165
   159
    }
moel@165
   160
moel@166
   161
    public void SetValue(string name, Color color) {
moel@165
   162
      settings[name] = color.ToArgb().ToString("X8");
moel@165
   163
    }
moel@165
   164
moel@166
   165
    public Color GetValue(string name, Color value) {
moel@165
   166
      string str;
moel@165
   167
      if (settings.TryGetValue(name, out str)) {
moel@165
   168
        int parsedValue;
moel@183
   169
        if (int.TryParse(str, NumberStyles.HexNumber,
moel@183
   170
          CultureInfo.InvariantCulture, out parsedValue))
moel@165
   171
          return Color.FromArgb(parsedValue);
moel@165
   172
        else
moel@165
   173
          return value;
moel@165
   174
      } else {
moel@165
   175
        return value;
moel@165
   176
      }
moel@165
   177
    }
moel@165
   178
  }
moel@165
   179
}