moel@63
|
1 |
/*
|
moel@63
|
2 |
|
moel@63
|
3 |
Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
moel@63
|
4 |
|
moel@63
|
5 |
The contents of this file are subject to the Mozilla Public License Version
|
moel@63
|
6 |
1.1 (the "License"); you may not use this file except in compliance with
|
moel@63
|
7 |
the License. You may obtain a copy of the License at
|
moel@63
|
8 |
|
moel@63
|
9 |
http://www.mozilla.org/MPL/
|
moel@63
|
10 |
|
moel@63
|
11 |
Software distributed under the License is distributed on an "AS IS" basis,
|
moel@63
|
12 |
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
moel@63
|
13 |
for the specific language governing rights and limitations under the License.
|
moel@63
|
14 |
|
moel@63
|
15 |
The Original Code is the Open Hardware Monitor code.
|
moel@63
|
16 |
|
moel@63
|
17 |
The Initial Developer of the Original Code is
|
moel@63
|
18 |
Michael Möller <m.moeller@gmx.ch>.
|
moel@63
|
19 |
Portions created by the Initial Developer are Copyright (C) 2009-2010
|
moel@63
|
20 |
the Initial Developer. All Rights Reserved.
|
moel@63
|
21 |
|
moel@63
|
22 |
Contributor(s):
|
moel@63
|
23 |
|
moel@63
|
24 |
Alternatively, the contents of this file may be used under the terms of
|
moel@63
|
25 |
either the GNU General Public License Version 2 or later (the "GPL"), or
|
moel@63
|
26 |
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
moel@63
|
27 |
in which case the provisions of the GPL or the LGPL are applicable instead
|
moel@63
|
28 |
of those above. If you wish to allow use of your version of this file only
|
moel@63
|
29 |
under the terms of either the GPL or the LGPL, and not to allow others to
|
moel@63
|
30 |
use your version of this file under the terms of the MPL, indicate your
|
moel@63
|
31 |
decision by deleting the provisions above and replace them with the notice
|
moel@63
|
32 |
and other provisions required by the GPL or the LGPL. If you do not delete
|
moel@63
|
33 |
the provisions above, a recipient may use your version of this file under
|
moel@63
|
34 |
the terms of any one of the MPL, the GPL or the LGPL.
|
moel@63
|
35 |
|
moel@63
|
36 |
*/
|
moel@63
|
37 |
|
moel@63
|
38 |
using System;
|
moel@63
|
39 |
using System.Collections.Generic;
|
moel@63
|
40 |
using System.Drawing;
|
moel@63
|
41 |
using System.IO;
|
moel@63
|
42 |
|
moel@63
|
43 |
namespace OpenHardwareMonitor.Utilities {
|
moel@63
|
44 |
|
moel@63
|
45 |
public sealed class Config {
|
moel@63
|
46 |
private static readonly Config instance = new Config();
|
moel@63
|
47 |
|
moel@63
|
48 |
private string fileName;
|
moel@63
|
49 |
|
moel@63
|
50 |
private System.Configuration.Configuration config;
|
moel@63
|
51 |
|
moel@63
|
52 |
private Config() {
|
moel@63
|
53 |
this.fileName = Path.ChangeExtension(
|
moel@63
|
54 |
System.Windows.Forms.Application.ExecutablePath, ".config");
|
moel@63
|
55 |
System.Configuration.ExeConfigurationFileMap fileMap =
|
moel@63
|
56 |
new System.Configuration.ExeConfigurationFileMap();
|
moel@63
|
57 |
fileMap.ExeConfigFilename = fileName;
|
moel@63
|
58 |
config = System.Configuration.ConfigurationManager.
|
moel@63
|
59 |
OpenMappedExeConfiguration(fileMap,
|
moel@63
|
60 |
System.Configuration.ConfigurationUserLevel.None);
|
moel@63
|
61 |
}
|
moel@63
|
62 |
|
moel@128
|
63 |
private void SaveConfig() {
|
moel@63
|
64 |
string tempName = Path.ChangeExtension(fileName, ".tmp");
|
moel@63
|
65 |
|
moel@63
|
66 |
if (File.Exists(tempName))
|
moel@63
|
67 |
File.Delete(tempName);
|
moel@63
|
68 |
try {
|
moel@63
|
69 |
config.SaveAs(tempName);
|
moel@63
|
70 |
if (File.Exists(fileName) && File.Exists(tempName))
|
moel@63
|
71 |
File.Delete(fileName);
|
moel@63
|
72 |
File.Move(tempName, fileName);
|
moel@63
|
73 |
} catch (System.Configuration.ConfigurationErrorsException) { }
|
moel@63
|
74 |
}
|
moel@63
|
75 |
|
moel@128
|
76 |
public static void Save() {
|
moel@128
|
77 |
instance.SaveConfig();
|
moel@128
|
78 |
}
|
moel@128
|
79 |
|
moel@63
|
80 |
public static Config Settings {
|
moel@63
|
81 |
get {
|
moel@63
|
82 |
return instance;
|
moel@63
|
83 |
}
|
moel@63
|
84 |
}
|
moel@63
|
85 |
|
moel@63
|
86 |
public string this[string name] {
|
moel@63
|
87 |
get {
|
moel@63
|
88 |
System.Configuration.KeyValueConfigurationElement element =
|
moel@63
|
89 |
config.AppSettings.Settings[name];
|
moel@63
|
90 |
if (element != null)
|
moel@63
|
91 |
return element.Value;
|
moel@63
|
92 |
else
|
moel@63
|
93 |
return null;
|
moel@63
|
94 |
}
|
moel@63
|
95 |
set {
|
moel@63
|
96 |
config.AppSettings.Settings.Remove(name);
|
moel@63
|
97 |
config.AppSettings.Settings.Add(name, value);
|
moel@63
|
98 |
}
|
moel@63
|
99 |
}
|
moel@63
|
100 |
|
moel@63
|
101 |
public static bool Contains(string name) {
|
moel@63
|
102 |
System.Configuration.KeyValueConfigurationElement element =
|
moel@63
|
103 |
instance.config.AppSettings.Settings[name];
|
moel@63
|
104 |
return element != null;
|
moel@63
|
105 |
}
|
moel@63
|
106 |
|
moel@63
|
107 |
public static void Remove(string name) {
|
moel@63
|
108 |
instance.config.AppSettings.Settings.Remove(name);
|
moel@63
|
109 |
}
|
moel@63
|
110 |
|
moel@63
|
111 |
public static void Set(string name, bool value) {
|
moel@63
|
112 |
instance[name] = value ? "true" : "false";
|
moel@63
|
113 |
}
|
moel@63
|
114 |
|
moel@63
|
115 |
public static bool Get(string name, bool value) {
|
moel@63
|
116 |
System.Configuration.KeyValueConfigurationElement element =
|
moel@63
|
117 |
instance.config.AppSettings.Settings[name];
|
moel@63
|
118 |
if (element == null)
|
moel@63
|
119 |
return value;
|
moel@63
|
120 |
else
|
moel@63
|
121 |
return element.Value == "true";
|
moel@63
|
122 |
}
|
moel@63
|
123 |
|
moel@63
|
124 |
public static void Set(string name, int value) {
|
moel@63
|
125 |
instance[name] = value.ToString();
|
moel@63
|
126 |
}
|
moel@63
|
127 |
|
moel@63
|
128 |
public static int Get(string name, int value) {
|
moel@63
|
129 |
System.Configuration.KeyValueConfigurationElement element =
|
moel@63
|
130 |
instance.config.AppSettings.Settings[name];
|
moel@63
|
131 |
if (element == null)
|
moel@63
|
132 |
return value;
|
moel@63
|
133 |
else {
|
moel@63
|
134 |
int parsedValue;
|
moel@63
|
135 |
if (int.TryParse(element.Value, out parsedValue))
|
moel@63
|
136 |
return parsedValue;
|
moel@63
|
137 |
else
|
moel@63
|
138 |
return value;
|
moel@63
|
139 |
}
|
moel@63
|
140 |
}
|
moel@63
|
141 |
|
moel@63
|
142 |
public static void Set(string name, Color color) {
|
moel@63
|
143 |
instance[name] = color.ToArgb().ToString("X8");
|
moel@63
|
144 |
}
|
moel@63
|
145 |
|
moel@63
|
146 |
public static Color Get(string name, Color value) {
|
moel@63
|
147 |
System.Configuration.KeyValueConfigurationElement element =
|
moel@63
|
148 |
instance.config.AppSettings.Settings[name];
|
moel@63
|
149 |
if (element == null)
|
moel@63
|
150 |
return value;
|
moel@63
|
151 |
else {
|
moel@63
|
152 |
int parsedValue;
|
moel@63
|
153 |
if (int.TryParse(element.Value,
|
moel@63
|
154 |
System.Globalization.NumberStyles.HexNumber,
|
moel@63
|
155 |
System.Globalization.CultureInfo.InvariantCulture, out parsedValue))
|
moel@63
|
156 |
return Color.FromArgb(parsedValue);
|
moel@63
|
157 |
else
|
moel@63
|
158 |
return value;
|
moel@63
|
159 |
}
|
moel@63
|
160 |
}
|
moel@63
|
161 |
|
moel@63
|
162 |
public static void Set(string name, float value) {
|
moel@63
|
163 |
instance[name] = value.ToString(
|
moel@63
|
164 |
System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
|
moel@63
|
165 |
}
|
moel@63
|
166 |
|
moel@63
|
167 |
public static float Get(string name, float value) {
|
moel@63
|
168 |
System.Configuration.KeyValueConfigurationElement element =
|
moel@63
|
169 |
instance.config.AppSettings.Settings[name];
|
moel@63
|
170 |
if (element == null)
|
moel@63
|
171 |
return value;
|
moel@63
|
172 |
else {
|
moel@63
|
173 |
float parsedValue;
|
moel@63
|
174 |
if (float.TryParse(element.Value,
|
moel@63
|
175 |
System.Globalization.NumberStyles.Float,
|
moel@63
|
176 |
System.Globalization.CultureInfo.InvariantCulture, out parsedValue))
|
moel@63
|
177 |
return parsedValue;
|
moel@63
|
178 |
else
|
moel@63
|
179 |
return value;
|
moel@63
|
180 |
}
|
moel@63
|
181 |
}
|
moel@63
|
182 |
}
|
moel@63
|
183 |
}
|