moel@28: /*
moel@28:   
moel@28:   Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@28: 
moel@28:   The contents of this file are subject to the Mozilla Public License Version
moel@28:   1.1 (the "License"); you may not use this file except in compliance with
moel@28:   the License. You may obtain a copy of the License at
moel@28:  
moel@28:   http://www.mozilla.org/MPL/
moel@28: 
moel@28:   Software distributed under the License is distributed on an "AS IS" basis,
moel@28:   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@28:   for the specific language governing rights and limitations under the License.
moel@28: 
moel@28:   The Original Code is the Open Hardware Monitor code.
moel@28: 
moel@28:   The Initial Developer of the Original Code is 
moel@28:   Michael Möller <m.moeller@gmx.ch>.
moel@28:   Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@28:   the Initial Developer. All Rights Reserved.
moel@28: 
moel@28:   Contributor(s):
moel@28: 
moel@28:   Alternatively, the contents of this file may be used under the terms of
moel@28:   either the GNU General Public License Version 2 or later (the "GPL"), or
moel@28:   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@28:   in which case the provisions of the GPL or the LGPL are applicable instead
moel@28:   of those above. If you wish to allow use of your version of this file only
moel@28:   under the terms of either the GPL or the LGPL, and not to allow others to
moel@28:   use your version of this file under the terms of the MPL, indicate your
moel@28:   decision by deleting the provisions above and replace them with the notice
moel@28:   and other provisions required by the GPL or the LGPL. If you do not delete
moel@28:   the provisions above, a recipient may use your version of this file under
moel@28:   the terms of any one of the MPL, the GPL or the LGPL.
moel@28:  
moel@28: */
moel@28: 
moel@28: using System;
moel@28: using System.Collections.Generic;
moel@28: using System.IO;
moel@34: using System.Globalization;
moel@28: using System.Text;
moel@28: 
moel@28: namespace OpenHardwareMonitor.Hardware {
moel@28: 
moel@28:   public delegate void HardwareEventHandler(IHardware hardware);
moel@28: 
moel@28:   public class Computer {
moel@28: 
moel@28:     private List<IGroup> groups = new List<IGroup>();
moel@28: 
moel@28:     private bool open = false;
moel@28:     private bool hddEnabled = false;
moel@28: 
moel@28:     public Computer() { }
moel@28: 
moel@28:     private void Add(IGroup group) {
moel@28:       if (groups.Contains(group))
moel@28:         return;
moel@28: 
moel@28:       groups.Add(group);
moel@28: 
moel@28:       if (HardwareAdded != null)
moel@28:         foreach (IHardware hardware in group.Hardware)
moel@28:           HardwareAdded(hardware);
moel@28:     }
moel@28: 
moel@28:     private void Remove(IGroup group) {
moel@28:       if (!groups.Contains(group))
moel@28:         return;
moel@28: 
moel@28:       groups.Remove(group);
moel@28: 
moel@28:       if (HardwareRemoved != null)
moel@28:         foreach (IHardware hardware in group.Hardware)
moel@28:           HardwareRemoved(hardware);
moel@28:     }
moel@28: 
moel@28:     public void Open() {
moel@28:       if (open)
moel@28:         return;
moel@28: 
moel@28:       Add(new SMBIOS.SMBIOSGroup());
moel@28:       Add(new LPC.LPCGroup());
moel@28:       Add(new CPU.CPUGroup());
moel@28:       Add(new ATI.ATIGroup());
moel@28:       Add(new Nvidia.NvidiaGroup());
moel@28:       Add(new TBalancer.TBalancerGroup());
moel@28: 
moel@28:       if (hddEnabled)        
moel@28:         Add(new HDD.HDDGroup());
moel@28: 
moel@28:       open = true;
moel@28:     }
moel@28: 
moel@28:     public void Update() {
moel@28:       foreach (IGroup group in groups)
moel@28:         foreach (IHardware hardware in group.Hardware)
moel@28:           hardware.Update();
moel@28:     }
moel@28: 
moel@28:     public bool HDDEnabled {
moel@28:       get { return hddEnabled; }
moel@28:       set {
moel@28:         if (open && value && !hddEnabled) {
moel@28:           Add(new HDD.HDDGroup());
moel@28:         } else if (open && !value && hddEnabled) {
moel@28:           List<IGroup> list = new List<IGroup>();
moel@28:           foreach (IGroup group in groups)
moel@28:             if (group is HDD.HDDGroup)
moel@28:               list.Add(group);
moel@28:           foreach (IGroup group in list)
moel@28:             Remove(group);          
moel@28:         }
moel@28:         hddEnabled = value;
moel@28:       }
moel@28:     }
moel@28: 
moel@28:     public IEnumerable<IHardware> Hardware {
moel@28:       get {       
moel@28:         foreach (IGroup group in groups)
moel@28:           foreach (IHardware hardware in group.Hardware)
moel@28:             yield return hardware;
moel@28:       }
moel@28:     }
moel@28: 
moel@28:     private void NewSection(TextWriter writer) {
moel@28:       for (int i = 0; i < 8; i++)
moel@28:         writer.Write("----------");
moel@28:       writer.WriteLine();
moel@28:       writer.WriteLine();
moel@28:     }
moel@28: 
moel@28:     public void SaveReport(Version version) {
moel@28: 
moel@28:       using (TextWriter w =
moel@34:         new StreamWriter("OpenHardwareMonitor.Report.txt")) {        
moel@28: 
moel@28:         w.WriteLine();
moel@28:         w.WriteLine("Open Hardware Monitor Report");
moel@28:         w.WriteLine();
moel@28: 
moel@28:         NewSection(w);
moel@28:         w.Write("Version: "); w.WriteLine(version.ToString());
moel@28:         w.WriteLine();
moel@28: 
moel@28:         NewSection(w);
moel@28:         foreach (IGroup group in groups) {
moel@28:           foreach (IHardware hardware in group.Hardware) {
moel@28:             w.WriteLine("|");
moel@28:             w.WriteLine("+-+ {0} ({1})",
moel@28:               new object[] { hardware.Name, hardware.Identifier });
moel@28:             foreach (ISensor sensor in hardware.Sensors) {
moel@28:               w.WriteLine("|   +- {0} : {1} : {2} : {3}",
moel@28:                 new object[] { sensor.SensorType, sensor.Index, sensor.Name, 
moel@34:                   string.Format(CultureInfo.InvariantCulture, "{0}", 
moel@34:                   sensor.Value) });
moel@28:             }
moel@28:           }
moel@28:         }
moel@28:         w.WriteLine();
moel@28: 
moel@28:         foreach (IGroup group in groups) {
moel@28:           string report = group.GetReport();
moel@28:           if (report != null) {
moel@28:             NewSection(w);
moel@28:             w.Write(report);
moel@28:           }
moel@28: 
moel@28:           IHardware[] hardwareArray = group.Hardware;
moel@28:           foreach (IHardware hardware in hardwareArray) {
moel@28:             string hardwareReport = hardware.GetReport();
moel@28:             if (hardwareReport != null) {
moel@28:               NewSection(w);
moel@28:               w.Write(hardwareReport);
moel@28:             }
moel@28:           }
moel@28:         }
moel@28:       }
moel@28:     }
moel@28: 
moel@28:     public void Close() {
moel@28:       if (!open)
moel@28:         return;
moel@28: 
moel@28:       foreach (IGroup group in groups)
moel@28:         group.Close();
moel@28:       groups.Clear();
moel@28: 
moel@28:       open = false;
moel@28:     }
moel@28: 
moel@28:     public event HardwareEventHandler HardwareAdded;
moel@28:     public event HardwareEventHandler HardwareRemoved;
moel@28: 
moel@28: 
moel@28: 
moel@28:   }
moel@28: }