Changed the project files to Visual Studio 2010. Fixed some Code Analysis warnings.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
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
9 http://www.mozilla.org/MPL/
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.
15 The Original Code is the Open Hardware Monitor code.
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.
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.
39 using System.Collections.Generic;
41 using System.Globalization;
43 using System.Threading;
45 namespace OpenHardwareMonitor.Hardware {
47 public class Computer : IComputer {
49 private List<IGroup> groups = new List<IGroup>();
51 private bool open = false;
52 private bool hddEnabled = false;
53 private ISettings settings;
56 this.settings = new Settings();
59 public Computer(ISettings settings) {
61 this.settings = settings;
63 this.settings = new Settings();
67 private void Add(IGroup group) {
68 if (groups.Contains(group))
73 if (HardwareAdded != null)
74 foreach (IHardware hardware in group.Hardware)
75 HardwareAdded(hardware);
78 private void Remove(IGroup group) {
79 if (!groups.Contains(group))
84 if (HardwareRemoved != null)
85 foreach (IHardware hardware in group.Hardware)
86 HardwareRemoved(hardware);
93 Add(new Mainboard.MainboardGroup(settings));
94 Add(new CPU.CPUGroup(settings));
95 Add(new ATI.ATIGroup(settings));
96 Add(new Nvidia.NvidiaGroup(settings));
97 Add(new TBalancer.TBalancerGroup(settings));
100 Add(new HDD.HDDGroup(settings));
105 public bool HDDEnabled {
106 get { return hddEnabled; }
108 if (open && value && !hddEnabled) {
109 Add(new HDD.HDDGroup(settings));
110 } else if (open && !value && hddEnabled) {
111 List<IGroup> list = new List<IGroup>();
112 foreach (IGroup group in groups)
113 if (group is HDD.HDDGroup)
115 foreach (IGroup group in list)
122 public IHardware[] Hardware {
124 List<IHardware> list = new List<IHardware>();
125 foreach (IGroup group in groups)
126 foreach (IHardware hardware in group.Hardware)
128 return list.ToArray();
132 private void NewSection(TextWriter writer) {
133 for (int i = 0; i < 8; i++)
134 writer.Write("----------");
139 private int CompareSensor(ISensor a, ISensor b) {
140 int c = a.SensorType.CompareTo(b.SensorType);
142 return a.Index.CompareTo(b.Index);
147 private void ReportHardwareSensorTree(IHardware hardware, TextWriter w,
149 w.WriteLine("{0}|", space);
150 w.WriteLine("{0}+-+ {1} ({2})",
151 space, hardware.Name, hardware.Identifier);
152 ISensor[] sensors = hardware.Sensors;
153 Array.Sort<ISensor>(sensors, CompareSensor);
154 foreach (ISensor sensor in sensors) {
155 w.WriteLine("{0}| +- {1}[{2}] : {3} : {4}",
156 space, sensor.SensorType, sensor.Index,
157 string.Format(CultureInfo.InvariantCulture, "{0} : {1} : {2}",
158 sensor.Value, sensor.Min, sensor.Max), sensor.Name);
160 foreach (IHardware subHardware in hardware.SubHardware)
161 ReportHardwareSensorTree(subHardware, w, "| ");
164 private void ReportHardwareParameterTree(IHardware hardware, TextWriter w,
166 w.WriteLine("{0}|", space);
167 w.WriteLine("{0}+-+ {1} ({2})",
168 space, hardware.Name, hardware.Identifier);
169 ISensor[] sensors = hardware.Sensors;
170 Array.Sort<ISensor>(sensors, CompareSensor);
171 foreach (ISensor sensor in sensors) {
172 if (sensor.Parameters.Length > 0) {
173 w.WriteLine("{0}| +- {1}[{2}] : {3}",
174 space, sensor.SensorType, sensor.Index, sensor.Name);
175 foreach (IParameter parameter in sensor.Parameters) {
176 w.WriteLine("{0}| +- {1} : {2}",
177 space, parameter.Name,
178 string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
179 parameter.DefaultValue, parameter.Value));
183 foreach (IHardware subHardware in hardware.SubHardware)
184 ReportHardwareParameterTree(subHardware, w, "| ");
187 private void ReportHardware(IHardware hardware, TextWriter w) {
188 string hardwareReport = hardware.GetReport();
189 if (hardwareReport != null && hardwareReport != "") {
191 w.Write(hardwareReport);
193 foreach (IHardware subHardware in hardware.SubHardware)
194 ReportHardware(subHardware, w);
197 public string GetReport() {
199 using (StringWriter w = new StringWriter(CultureInfo.InvariantCulture)) {
202 w.WriteLine("Open Hardware Monitor Report");
205 Version version = typeof(Computer).Assembly.GetName().Version;
208 w.Write("Version: "); w.WriteLine(version.ToString());
212 w.Write("Common Language Runtime: ");
213 w.WriteLine(Environment.Version.ToString());
214 w.Write("Operating System: ");
215 w.WriteLine(Environment.OSVersion.ToString());
216 w.Write("Process Type: ");
217 w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit");
221 w.WriteLine("Sensors");
223 foreach (IGroup group in groups) {
224 foreach (IHardware hardware in group.Hardware)
225 ReportHardwareSensorTree(hardware, w, "");
230 w.WriteLine("Parameters");
232 foreach (IGroup group in groups) {
233 foreach (IHardware hardware in group.Hardware)
234 ReportHardwareParameterTree(hardware, w, "");
238 foreach (IGroup group in groups) {
239 string report = group.GetReport();
240 if (report != null && report != "") {
245 IHardware[] hardwareArray = group.Hardware;
246 foreach (IHardware hardware in hardwareArray)
247 ReportHardware(hardware, w);
254 public void Close() {
258 foreach (IGroup group in groups)
265 public event HardwareEventHandler HardwareAdded;
266 public event HardwareEventHandler HardwareRemoved;
268 public void Accept(IVisitor visitor) {
270 visitor.VisitComputer(this);
273 public void Traverse(IVisitor visitor) {
274 foreach (IGroup group in groups)
275 foreach (IHardware hardware in group.Hardware)
276 hardware.Accept(visitor);
279 private class Settings : ISettings {
281 public bool Contains(string name) {
285 public void SetValue(string name, string value) { }
287 public string GetValue(string name, string value) {
291 public void Remove(string name) { }