Utilities/Config.cs
author moel.mich
Tue, 27 Jul 2010 18:38:11 +0000
changeset 159 eda3e3458cf4
parent 128 cea5477b4d72
permissions -rw-r--r--
Refactoring and fine tuning for Linux GUI.
     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       try {
    62         // try to load the settings
    63         System.Configuration.KeyValueConfigurationCollection collection =
    64           config.AppSettings.Settings;        
    65       } catch {
    66         // if an exception is thrown, start with a new config file
    67         if (File.Exists(fileName))
    68           File.Delete(fileName);
    69         config = System.Configuration.ConfigurationManager.
    70           OpenMappedExeConfiguration(fileMap,
    71           System.Configuration.ConfigurationUserLevel.None);
    72       }
    73     }
    74 
    75     private void SaveConfig() {
    76       string tempName = Path.ChangeExtension(fileName, ".tmp");
    77 
    78       if (File.Exists(tempName))
    79         File.Delete(tempName);
    80       try {
    81         config.SaveAs(tempName);
    82         if (File.Exists(fileName) && File.Exists(tempName))
    83           File.Delete(fileName);
    84         File.Move(tempName, fileName);
    85       } catch (System.Configuration.ConfigurationErrorsException) { }
    86     }
    87 
    88     public static void Save() {
    89       instance.SaveConfig();
    90     }
    91 
    92     public static Config Settings {
    93       get {
    94         return instance;
    95       }
    96     }
    97 
    98     public string this[string name] {
    99       get {
   100         System.Configuration.KeyValueConfigurationElement element =
   101           config.AppSettings.Settings[name];
   102         if (element != null)
   103           return element.Value;
   104         else
   105           return null;
   106       }
   107       set {
   108         config.AppSettings.Settings.Remove(name);
   109         config.AppSettings.Settings.Add(name, value);
   110       }
   111     }
   112 
   113     public static bool Contains(string name) {
   114       System.Configuration.KeyValueConfigurationElement element =
   115         instance.config.AppSettings.Settings[name];
   116       return element != null;
   117     }
   118 
   119     public static void Remove(string name) {
   120       instance.config.AppSettings.Settings.Remove(name);
   121     }
   122 
   123     public static void Set(string name, bool value) {
   124       instance[name] = value ? "true" : "false";
   125     }
   126 
   127     public static bool Get(string name, bool value) {
   128       System.Configuration.KeyValueConfigurationElement element =
   129         instance.config.AppSettings.Settings[name];
   130       if (element == null)
   131         return value;
   132       else
   133         return element.Value == "true"; 
   134     }
   135 
   136     public static void Set(string name, int value) {
   137       instance[name] = value.ToString();
   138     }
   139 
   140     public static int Get(string name, int value) {
   141       System.Configuration.KeyValueConfigurationElement element =
   142         instance.config.AppSettings.Settings[name];
   143       if (element == null)
   144         return value;
   145       else {
   146         int parsedValue;
   147         if (int.TryParse(element.Value, out parsedValue))
   148           return parsedValue;
   149         else
   150           return value;
   151       }
   152     }
   153 
   154     public static void Set(string name, Color color) {
   155       instance[name] = color.ToArgb().ToString("X8");
   156     }
   157 
   158     public static Color Get(string name, Color value) {
   159       System.Configuration.KeyValueConfigurationElement element =
   160         instance.config.AppSettings.Settings[name];
   161       if (element == null)
   162         return value;
   163       else {
   164         int parsedValue;
   165         if (int.TryParse(element.Value, 
   166           System.Globalization.NumberStyles.HexNumber, 
   167           System.Globalization.CultureInfo.InvariantCulture, out parsedValue))
   168           return Color.FromArgb(parsedValue);
   169         else
   170           return value;
   171       }
   172     }
   173 
   174     public static void Set(string name, float value) {
   175       instance[name] = value.ToString(
   176         System.Globalization.CultureInfo.InvariantCulture.NumberFormat); 
   177     }
   178 
   179     public static float Get(string name, float value) {
   180       System.Configuration.KeyValueConfigurationElement element =
   181         instance.config.AppSettings.Settings[name];
   182       if (element == null)
   183         return value;
   184       else {
   185         float parsedValue;
   186         if (float.TryParse(element.Value,
   187           System.Globalization.NumberStyles.Float,
   188           System.Globalization.CultureInfo.InvariantCulture, out parsedValue))
   189           return parsedValue;
   190         else
   191           return value;
   192       }
   193     }
   194   }
   195 }