Hardware/Computer.cs
changeset 28 9b205b2ab056
child 34 dc276daadb2c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Hardware/Computer.cs	Sun Feb 07 16:37:15 2010 +0000
     1.3 @@ -0,0 +1,197 @@
     1.4 +/*
     1.5 +  
     1.6 +  Version: MPL 1.1/GPL 2.0/LGPL 2.1
     1.7 +
     1.8 +  The contents of this file are subject to the Mozilla Public License Version
     1.9 +  1.1 (the "License"); you may not use this file except in compliance with
    1.10 +  the License. You may obtain a copy of the License at
    1.11 + 
    1.12 +  http://www.mozilla.org/MPL/
    1.13 +
    1.14 +  Software distributed under the License is distributed on an "AS IS" basis,
    1.15 +  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    1.16 +  for the specific language governing rights and limitations under the License.
    1.17 +
    1.18 +  The Original Code is the Open Hardware Monitor code.
    1.19 +
    1.20 +  The Initial Developer of the Original Code is 
    1.21 +  Michael Möller <m.moeller@gmx.ch>.
    1.22 +  Portions created by the Initial Developer are Copyright (C) 2009-2010
    1.23 +  the Initial Developer. All Rights Reserved.
    1.24 +
    1.25 +  Contributor(s):
    1.26 +
    1.27 +  Alternatively, the contents of this file may be used under the terms of
    1.28 +  either the GNU General Public License Version 2 or later (the "GPL"), or
    1.29 +  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    1.30 +  in which case the provisions of the GPL or the LGPL are applicable instead
    1.31 +  of those above. If you wish to allow use of your version of this file only
    1.32 +  under the terms of either the GPL or the LGPL, and not to allow others to
    1.33 +  use your version of this file under the terms of the MPL, indicate your
    1.34 +  decision by deleting the provisions above and replace them with the notice
    1.35 +  and other provisions required by the GPL or the LGPL. If you do not delete
    1.36 +  the provisions above, a recipient may use your version of this file under
    1.37 +  the terms of any one of the MPL, the GPL or the LGPL.
    1.38 + 
    1.39 +*/
    1.40 +
    1.41 +using System;
    1.42 +using System.Collections.Generic;
    1.43 +using System.IO;
    1.44 +using System.Text;
    1.45 +
    1.46 +namespace OpenHardwareMonitor.Hardware {
    1.47 +
    1.48 +  public delegate void HardwareEventHandler(IHardware hardware);
    1.49 +
    1.50 +  public class Computer {
    1.51 +
    1.52 +    private List<IGroup> groups = new List<IGroup>();
    1.53 +
    1.54 +    private bool open = false;
    1.55 +    private bool hddEnabled = false;
    1.56 +
    1.57 +    public Computer() { }
    1.58 +
    1.59 +    private void Add(IGroup group) {
    1.60 +      if (groups.Contains(group))
    1.61 +        return;
    1.62 +
    1.63 +      groups.Add(group);
    1.64 +
    1.65 +      if (HardwareAdded != null)
    1.66 +        foreach (IHardware hardware in group.Hardware)
    1.67 +          HardwareAdded(hardware);
    1.68 +    }
    1.69 +
    1.70 +    private void Remove(IGroup group) {
    1.71 +      if (!groups.Contains(group))
    1.72 +        return;
    1.73 +
    1.74 +      groups.Remove(group);
    1.75 +
    1.76 +      if (HardwareRemoved != null)
    1.77 +        foreach (IHardware hardware in group.Hardware)
    1.78 +          HardwareRemoved(hardware);
    1.79 +    }
    1.80 +
    1.81 +    public void Open() {
    1.82 +      if (open)
    1.83 +        return;
    1.84 +
    1.85 +      Add(new SMBIOS.SMBIOSGroup());
    1.86 +      Add(new LPC.LPCGroup());
    1.87 +      Add(new CPU.CPUGroup());
    1.88 +      Add(new ATI.ATIGroup());
    1.89 +      Add(new Nvidia.NvidiaGroup());
    1.90 +      Add(new TBalancer.TBalancerGroup());
    1.91 +
    1.92 +      if (hddEnabled)        
    1.93 +        Add(new HDD.HDDGroup());
    1.94 +
    1.95 +      open = true;
    1.96 +    }
    1.97 +
    1.98 +    public void Update() {
    1.99 +      foreach (IGroup group in groups)
   1.100 +        foreach (IHardware hardware in group.Hardware)
   1.101 +          hardware.Update();
   1.102 +    }
   1.103 +
   1.104 +    public bool HDDEnabled {
   1.105 +      get { return hddEnabled; }
   1.106 +      set {
   1.107 +        if (open && value && !hddEnabled) {
   1.108 +          Add(new HDD.HDDGroup());
   1.109 +        } else if (open && !value && hddEnabled) {
   1.110 +          List<IGroup> list = new List<IGroup>();
   1.111 +          foreach (IGroup group in groups)
   1.112 +            if (group is HDD.HDDGroup)
   1.113 +              list.Add(group);
   1.114 +          foreach (IGroup group in list)
   1.115 +            Remove(group);          
   1.116 +        }
   1.117 +        hddEnabled = value;
   1.118 +      }
   1.119 +    }
   1.120 +
   1.121 +    public IEnumerable<IHardware> Hardware {
   1.122 +      get {       
   1.123 +        foreach (IGroup group in groups)
   1.124 +          foreach (IHardware hardware in group.Hardware)
   1.125 +            yield return hardware;
   1.126 +      }
   1.127 +    }
   1.128 +
   1.129 +    private void NewSection(TextWriter writer) {
   1.130 +      for (int i = 0; i < 8; i++)
   1.131 +        writer.Write("----------");
   1.132 +      writer.WriteLine();
   1.133 +      writer.WriteLine();
   1.134 +    }
   1.135 +
   1.136 +    public void SaveReport(Version version) {
   1.137 +
   1.138 +      using (TextWriter w =
   1.139 +        new StreamWriter("OpenHardwareMonitor.Report.txt")) {
   1.140 +
   1.141 +        w.WriteLine();
   1.142 +        w.WriteLine("Open Hardware Monitor Report");
   1.143 +        w.WriteLine();
   1.144 +
   1.145 +        NewSection(w);
   1.146 +        w.Write("Version: "); w.WriteLine(version.ToString());
   1.147 +        w.WriteLine();
   1.148 +
   1.149 +        NewSection(w);
   1.150 +        foreach (IGroup group in groups) {
   1.151 +          foreach (IHardware hardware in group.Hardware) {
   1.152 +            w.WriteLine("|");
   1.153 +            w.WriteLine("+-+ {0} ({1})",
   1.154 +              new object[] { hardware.Name, hardware.Identifier });
   1.155 +            foreach (ISensor sensor in hardware.Sensors) {
   1.156 +              w.WriteLine("|   +- {0} : {1} : {2} : {3}",
   1.157 +                new object[] { sensor.SensorType, sensor.Index, sensor.Name, 
   1.158 +                  sensor.Value });
   1.159 +            }
   1.160 +          }
   1.161 +        }
   1.162 +        w.WriteLine();
   1.163 +
   1.164 +        foreach (IGroup group in groups) {
   1.165 +          string report = group.GetReport();
   1.166 +          if (report != null) {
   1.167 +            NewSection(w);
   1.168 +            w.Write(report);
   1.169 +          }
   1.170 +
   1.171 +          IHardware[] hardwareArray = group.Hardware;
   1.172 +          foreach (IHardware hardware in hardwareArray) {
   1.173 +            string hardwareReport = hardware.GetReport();
   1.174 +            if (hardwareReport != null) {
   1.175 +              NewSection(w);
   1.176 +              w.Write(hardwareReport);
   1.177 +            }
   1.178 +          }
   1.179 +        }
   1.180 +      }
   1.181 +    }
   1.182 +
   1.183 +    public void Close() {
   1.184 +      if (!open)
   1.185 +        return;
   1.186 +
   1.187 +      foreach (IGroup group in groups)
   1.188 +        group.Close();
   1.189 +      groups.Clear();
   1.190 +
   1.191 +      open = false;
   1.192 +    }
   1.193 +
   1.194 +    public event HardwareEventHandler HardwareAdded;
   1.195 +    public event HardwareEventHandler HardwareRemoved;
   1.196 +
   1.197 +
   1.198 +
   1.199 +  }
   1.200 +}