Replaced all menus with the native implementations MainMenu and ContextMenu. Fixed a bug with the sensor renaming context menu.
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;
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 Mainboard.MainboardGroup());
83 Add(new CPU.CPUGroup());
84 Add(new ATI.ATIGroup());
85 Add(new Nvidia.NvidiaGroup());
86 Add(new TBalancer.TBalancerGroup());
89 Add(new HDD.HDDGroup());
94 public bool HDDEnabled {
95 get { return hddEnabled; }
97 if (open && value && !hddEnabled) {
98 Add(new HDD.HDDGroup());
99 } else if (open && !value && hddEnabled) {
100 List<IGroup> list = new List<IGroup>();
101 foreach (IGroup group in groups)
102 if (group is HDD.HDDGroup)
104 foreach (IGroup group in list)
111 public IHardware[] Hardware {
113 List<IHardware> list = new List<IHardware>();
114 foreach (IGroup group in groups)
115 foreach (IHardware hardware in group.Hardware)
117 return list.ToArray();
121 private void NewSection(TextWriter writer) {
122 for (int i = 0; i < 8; i++)
123 writer.Write("----------");
128 private int CompareSensor(ISensor a, ISensor b) {
129 int c = a.SensorType.CompareTo(b.SensorType);
131 return a.Index.CompareTo(b.Index);
136 private void ReportHardwareSensorTree(IHardware hardware, TextWriter w,
138 w.WriteLine("{0}|", space);
139 w.WriteLine("{0}+-+ {1} ({2})",
140 space, hardware.Name, hardware.Identifier);
141 ISensor[] sensors = hardware.Sensors;
142 Array.Sort<ISensor>(sensors, CompareSensor);
143 foreach (ISensor sensor in sensors) {
144 w.WriteLine("{0}| +- {1}[{2}] : {3} : {4}",
145 space, sensor.SensorType, sensor.Index,
146 string.Format(CultureInfo.InvariantCulture, "{0} : {1} : {2}",
147 sensor.Value, sensor.Min, sensor.Max), sensor.Name);
149 foreach (IHardware subHardware in hardware.SubHardware)
150 ReportHardwareSensorTree(subHardware, w, "| ");
153 private void ReportHardwareParameterTree(IHardware hardware, TextWriter w,
155 w.WriteLine("{0}|", space);
156 w.WriteLine("{0}+-+ {1} ({2})",
157 space, hardware.Name, hardware.Identifier);
158 ISensor[] sensors = hardware.Sensors;
159 Array.Sort<ISensor>(sensors, CompareSensor);
160 foreach (ISensor sensor in sensors) {
161 if (sensor.Parameters.Length > 0) {
162 w.WriteLine("{0}| +- {1}[{2}] : {3}",
163 space, sensor.SensorType, sensor.Index, sensor.Name);
164 foreach (IParameter parameter in sensor.Parameters) {
165 w.WriteLine("{0}| +- {1} : {2}",
166 space, parameter.Name,
167 string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
168 parameter.DefaultValue, parameter.Value));
172 foreach (IHardware subHardware in hardware.SubHardware)
173 ReportHardwareParameterTree(subHardware, w, "| ");
176 private void ReportHardware(IHardware hardware, TextWriter w) {
177 string hardwareReport = hardware.GetReport();
178 if (hardwareReport != null && hardwareReport != "") {
180 w.Write(hardwareReport);
182 foreach (IHardware subHardware in hardware.SubHardware)
183 ReportHardware(subHardware, w);
186 public string GetReport() {
188 using (StringWriter w = new StringWriter()) {
191 w.WriteLine("Open Hardware Monitor Report");
194 Version version = typeof(Computer).Assembly.GetName().Version;
197 w.Write("Version: "); w.WriteLine(version.ToString());
201 w.Write("Common Language Runtime: ");
202 w.WriteLine(Environment.Version.ToString());
203 w.Write("Operating System: ");
204 w.WriteLine(Environment.OSVersion.ToString());
205 w.Write("Process Type: ");
206 w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit");
210 w.WriteLine("Sensors");
212 foreach (IGroup group in groups) {
213 foreach (IHardware hardware in group.Hardware)
214 ReportHardwareSensorTree(hardware, w, "");
219 w.WriteLine("Parameters");
221 foreach (IGroup group in groups) {
222 foreach (IHardware hardware in group.Hardware)
223 ReportHardwareParameterTree(hardware, w, "");
227 foreach (IGroup group in groups) {
228 string report = group.GetReport();
229 if (report != null && report != "") {
234 IHardware[] hardwareArray = group.Hardware;
235 foreach (IHardware hardware in hardwareArray)
236 ReportHardware(hardware, w);
243 public void Close() {
247 foreach (IGroup group in groups)
254 public event HardwareEventHandler HardwareAdded;
255 public event HardwareEventHandler HardwareRemoved;
257 public void Accept(IVisitor visitor) {
258 visitor.VisitComputer(this);
261 public void Traverse(IVisitor visitor) {
262 foreach (IGroup group in groups)
263 foreach (IHardware hardware in group.Hardware)
264 hardware.Accept(visitor);