Refactoring: New class Computer manages all the hardware and creates events.
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;
43 namespace OpenHardwareMonitor.Hardware {
45 public delegate void HardwareEventHandler(IHardware hardware);
47 public class Computer {
49 private List<IGroup> groups = new List<IGroup>();
51 private bool open = false;
52 private bool hddEnabled = false;
56 private void Add(IGroup group) {
57 if (groups.Contains(group))
62 if (HardwareAdded != null)
63 foreach (IHardware hardware in group.Hardware)
64 HardwareAdded(hardware);
67 private void Remove(IGroup group) {
68 if (!groups.Contains(group))
73 if (HardwareRemoved != null)
74 foreach (IHardware hardware in group.Hardware)
75 HardwareRemoved(hardware);
82 Add(new SMBIOS.SMBIOSGroup());
83 Add(new LPC.LPCGroup());
84 Add(new CPU.CPUGroup());
85 Add(new ATI.ATIGroup());
86 Add(new Nvidia.NvidiaGroup());
87 Add(new TBalancer.TBalancerGroup());
90 Add(new HDD.HDDGroup());
95 public void Update() {
96 foreach (IGroup group in groups)
97 foreach (IHardware hardware in group.Hardware)
101 public bool HDDEnabled {
102 get { return hddEnabled; }
104 if (open && value && !hddEnabled) {
105 Add(new HDD.HDDGroup());
106 } else if (open && !value && hddEnabled) {
107 List<IGroup> list = new List<IGroup>();
108 foreach (IGroup group in groups)
109 if (group is HDD.HDDGroup)
111 foreach (IGroup group in list)
118 public IEnumerable<IHardware> Hardware {
120 foreach (IGroup group in groups)
121 foreach (IHardware hardware in group.Hardware)
122 yield return hardware;
126 private void NewSection(TextWriter writer) {
127 for (int i = 0; i < 8; i++)
128 writer.Write("----------");
133 public void SaveReport(Version version) {
135 using (TextWriter w =
136 new StreamWriter("OpenHardwareMonitor.Report.txt")) {
139 w.WriteLine("Open Hardware Monitor Report");
143 w.Write("Version: "); w.WriteLine(version.ToString());
147 foreach (IGroup group in groups) {
148 foreach (IHardware hardware in group.Hardware) {
150 w.WriteLine("+-+ {0} ({1})",
151 new object[] { hardware.Name, hardware.Identifier });
152 foreach (ISensor sensor in hardware.Sensors) {
153 w.WriteLine("| +- {0} : {1} : {2} : {3}",
154 new object[] { sensor.SensorType, sensor.Index, sensor.Name,
161 foreach (IGroup group in groups) {
162 string report = group.GetReport();
163 if (report != null) {
168 IHardware[] hardwareArray = group.Hardware;
169 foreach (IHardware hardware in hardwareArray) {
170 string hardwareReport = hardware.GetReport();
171 if (hardwareReport != null) {
173 w.Write(hardwareReport);
180 public void Close() {
184 foreach (IGroup group in groups)
191 public event HardwareEventHandler HardwareAdded;
192 public event HardwareEventHandler HardwareRemoved;