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