Utilities/Config.cs
author moel.mich
Wed, 30 Jun 2010 08:52:53 +0000
changeset 147 17f982ee3a46
parent 63 1a7c13ac7348
child 157 0b5cc38501e1
permissions -rw-r--r--
Added more revisions for the W83627THF, as there seems to exist chips with revision 0x82, 0x84, and 0x85 as well.
     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.IO;
    42 
    43 namespace OpenHardwareMonitor.Utilities {
    44 
    45   public sealed class Config {
    46     private static readonly Config instance = new Config();
    47 
    48     private string fileName;
    49 
    50     private System.Configuration.Configuration config;
    51 
    52     private Config() {
    53       this.fileName = Path.ChangeExtension(
    54         System.Windows.Forms.Application.ExecutablePath, ".config");
    55       System.Configuration.ExeConfigurationFileMap fileMap = 
    56         new System.Configuration.ExeConfigurationFileMap();
    57       fileMap.ExeConfigFilename = fileName;        
    58       config = System.Configuration.ConfigurationManager.
    59         OpenMappedExeConfiguration(fileMap, 
    60         System.Configuration.ConfigurationUserLevel.None);
    61     }
    62 
    63     private void SaveConfig() {
    64       string tempName = Path.ChangeExtension(fileName, ".tmp");
    65 
    66       if (File.Exists(tempName))
    67         File.Delete(tempName);
    68       try {
    69         config.SaveAs(tempName);
    70         if (File.Exists(fileName) && File.Exists(tempName))
    71           File.Delete(fileName);
    72         File.Move(tempName, fileName);
    73       } catch (System.Configuration.ConfigurationErrorsException) { }
    74     }
    75 
    76     public static void Save() {
    77       instance.SaveConfig();
    78     }
    79 
    80     public static Config Settings {
    81       get {
    82         return instance;
    83       }
    84     }
    85 
    86     public string this[string name] {
    87       get {
    88         System.Configuration.KeyValueConfigurationElement element =
    89           config.AppSettings.Settings[name];
    90         if (element != null)
    91           return element.Value;
    92         else
    93           return null;
    94       }
    95       set {
    96         config.AppSettings.Settings.Remove(name);
    97         config.AppSettings.Settings.Add(name, value);
    98       }
    99     }
   100 
   101     public static bool Contains(string name) {
   102       System.Configuration.KeyValueConfigurationElement element =
   103         instance.config.AppSettings.Settings[name];
   104       return element != null;
   105     }
   106 
   107     public static void Remove(string name) {
   108       instance.config.AppSettings.Settings.Remove(name);
   109     }
   110 
   111     public static void Set(string name, bool value) {
   112       instance[name] = value ? "true" : "false";
   113     }
   114 
   115     public static bool Get(string name, bool value) {
   116       System.Configuration.KeyValueConfigurationElement element =
   117         instance.config.AppSettings.Settings[name];
   118       if (element == null)
   119         return value;
   120       else
   121         return element.Value == "true"; 
   122     }
   123 
   124     public static void Set(string name, int value) {
   125       instance[name] = value.ToString();
   126     }
   127 
   128     public static int Get(string name, int value) {
   129       System.Configuration.KeyValueConfigurationElement element =
   130         instance.config.AppSettings.Settings[name];
   131       if (element == null)
   132         return value;
   133       else {
   134         int parsedValue;
   135         if (int.TryParse(element.Value, out parsedValue))
   136           return parsedValue;
   137         else
   138           return value;
   139       }
   140     }
   141 
   142     public static void Set(string name, Color color) {
   143       instance[name] = color.ToArgb().ToString("X8");
   144     }
   145 
   146     public static Color Get(string name, Color value) {
   147       System.Configuration.KeyValueConfigurationElement element =
   148         instance.config.AppSettings.Settings[name];
   149       if (element == null)
   150         return value;
   151       else {
   152         int parsedValue;
   153         if (int.TryParse(element.Value, 
   154           System.Globalization.NumberStyles.HexNumber, 
   155           System.Globalization.CultureInfo.InvariantCulture, out parsedValue))
   156           return Color.FromArgb(parsedValue);
   157         else
   158           return value;
   159       }
   160     }
   161 
   162     public static void Set(string name, float value) {
   163       instance[name] = value.ToString(
   164         System.Globalization.CultureInfo.InvariantCulture.NumberFormat); 
   165     }
   166 
   167     public static float Get(string name, float value) {
   168       System.Configuration.KeyValueConfigurationElement element =
   169         instance.config.AppSettings.Settings[name];
   170       if (element == null)
   171         return value;
   172       else {
   173         float parsedValue;
   174         if (float.TryParse(element.Value,
   175           System.Globalization.NumberStyles.Float,
   176           System.Globalization.CultureInfo.InvariantCulture, out parsedValue))
   177           return parsedValue;
   178         else
   179           return value;
   180       }
   181     }
   182   }
   183 }