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;
44 using System.Security;
45 using System.Security.Permissions;
48 namespace OpenHardwareMonitor.Hardware {
50 public class Computer : IComputer {
52 private List<IGroup> groups = new List<IGroup>();
54 private bool open = false;
55 private bool hddEnabled = false;
56 private ISettings settings;
59 this.settings = new Settings();
62 public Computer(ISettings settings) {
64 this.settings = settings;
66 this.settings = new Settings();
70 private void Add(IGroup group) {
71 if (groups.Contains(group))
76 if (HardwareAdded != null)
77 foreach (IHardware hardware in group.Hardware)
78 HardwareAdded(hardware);
81 private void Remove(IGroup group) {
82 if (!groups.Contains(group))
87 if (HardwareRemoved != null)
88 foreach (IHardware hardware in group.Hardware)
89 HardwareRemoved(hardware);
92 [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
99 Add(new Mainboard.MainboardGroup(settings));
100 Add(new CPU.CPUGroup(settings));
101 Add(new ATI.ATIGroup(settings));
102 Add(new Nvidia.NvidiaGroup(settings));
103 Add(new TBalancer.TBalancerGroup(settings));
106 Add(new HDD.HDDGroup(settings));
111 public bool HDDEnabled {
112 get { return hddEnabled; }
114 [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
116 if (open && value && !hddEnabled) {
117 Add(new HDD.HDDGroup(settings));
118 } else if (open && !value && hddEnabled) {
119 List<IGroup> list = new List<IGroup>();
120 foreach (IGroup group in groups)
121 if (group is HDD.HDDGroup)
123 foreach (IGroup group in list)
130 public IHardware[] Hardware {
132 List<IHardware> list = new List<IHardware>();
133 foreach (IGroup group in groups)
134 foreach (IHardware hardware in group.Hardware)
136 return list.ToArray();
140 private static void NewSection(TextWriter writer) {
141 for (int i = 0; i < 8; i++)
142 writer.Write("----------");
147 private int CompareSensor(ISensor a, ISensor b) {
148 int c = a.SensorType.CompareTo(b.SensorType);
150 return a.Index.CompareTo(b.Index);
155 private void ReportHardwareSensorTree(IHardware hardware, TextWriter w,
157 w.WriteLine("{0}|", space);
158 w.WriteLine("{0}+-+ {1} ({2})",
159 space, hardware.Name, hardware.Identifier);
160 ISensor[] sensors = hardware.Sensors;
161 Array.Sort<ISensor>(sensors, CompareSensor);
162 foreach (ISensor sensor in sensors) {
163 w.WriteLine("{0}| +- {1}[{2}] : {3} : {4}",
164 space, sensor.SensorType, sensor.Index,
165 string.Format(CultureInfo.InvariantCulture, "{0} : {1} : {2}",
166 sensor.Value, sensor.Min, sensor.Max), sensor.Name);
168 foreach (IHardware subHardware in hardware.SubHardware)
169 ReportHardwareSensorTree(subHardware, w, "| ");
172 private void ReportHardwareParameterTree(IHardware hardware, TextWriter w,
174 w.WriteLine("{0}|", space);
175 w.WriteLine("{0}+-+ {1} ({2})",
176 space, hardware.Name, hardware.Identifier);
177 ISensor[] sensors = hardware.Sensors;
178 Array.Sort<ISensor>(sensors, CompareSensor);
179 foreach (ISensor sensor in sensors) {
180 if (sensor.Parameters.Length > 0) {
181 w.WriteLine("{0}| +- {1}[{2}] : {3}",
182 space, sensor.SensorType, sensor.Index, sensor.Name);
183 foreach (IParameter parameter in sensor.Parameters) {
184 w.WriteLine("{0}| +- {1} : {2}",
185 space, parameter.Name,
186 string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
187 parameter.DefaultValue, parameter.Value));
191 foreach (IHardware subHardware in hardware.SubHardware)
192 ReportHardwareParameterTree(subHardware, w, "| ");
195 private void ReportHardware(IHardware hardware, TextWriter w) {
196 string hardwareReport = hardware.GetReport();
197 if (!string.IsNullOrEmpty(hardwareReport)) {
199 w.Write(hardwareReport);
201 foreach (IHardware subHardware in hardware.SubHardware)
202 ReportHardware(subHardware, w);
205 public string GetReport() {
207 using (StringWriter w = new StringWriter(CultureInfo.InvariantCulture)) {
210 w.WriteLine("Open Hardware Monitor Report");
213 Version version = typeof(Computer).Assembly.GetName().Version;
216 w.Write("Version: "); w.WriteLine(version.ToString());
220 w.Write("Common Language Runtime: ");
221 w.WriteLine(Environment.Version.ToString());
222 w.Write("Operating System: ");
223 w.WriteLine(Environment.OSVersion.ToString());
224 w.Write("Process Type: ");
225 w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit");
229 w.WriteLine("Sensors");
231 foreach (IGroup group in groups) {
232 foreach (IHardware hardware in group.Hardware)
233 ReportHardwareSensorTree(hardware, w, "");
238 w.WriteLine("Parameters");
240 foreach (IGroup group in groups) {
241 foreach (IHardware hardware in group.Hardware)
242 ReportHardwareParameterTree(hardware, w, "");
246 foreach (IGroup group in groups) {
247 string report = group.GetReport();
248 if (!string.IsNullOrEmpty(report)) {
253 IHardware[] hardwareArray = group.Hardware;
254 foreach (IHardware hardware in hardwareArray)
255 ReportHardware(hardware, w);
262 public void Close() {
266 foreach (IGroup group in groups)
275 public event HardwareEventHandler HardwareAdded;
276 public event HardwareEventHandler HardwareRemoved;
278 public void Accept(IVisitor visitor) {
280 throw new ArgumentNullException("visitor");
281 visitor.VisitComputer(this);
284 public void Traverse(IVisitor visitor) {
285 foreach (IGroup group in groups)
286 foreach (IHardware hardware in group.Hardware)
287 hardware.Accept(visitor);
290 private class Settings : ISettings {
292 public bool Contains(string name) {
296 public void SetValue(string name, string value) { }
298 public string GetValue(string name, string value) {
302 public void Remove(string name) { }