Hardware/Computer.cs
author moel.mich
Sat, 18 Sep 2010 19:56:39 +0000
changeset 190 d3a54e407737
parent 171 81ab5e53122e
child 195 0ee888c485d5
permissions -rw-r--r--
Changed the trimming in the treeView to StringTrimming.EllipsisCharacter.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     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
     8  
     9   http://www.mozilla.org/MPL/
    10 
    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.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    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.
    21 
    22   Contributor(s):
    23 
    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.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.Globalization;
    41 using System.IO;
    42 using System.Security.Permissions;
    43 
    44 namespace OpenHardwareMonitor.Hardware {
    45 
    46   public class Computer : IComputer {
    47 
    48     private List<IGroup> groups = new List<IGroup>();
    49 
    50     private bool open = false;
    51     private bool hddEnabled = false;
    52     private ISettings settings;
    53 
    54     public Computer() {
    55       this.settings = new Settings();
    56     }
    57 
    58     public Computer(ISettings settings) {
    59       if (settings != null)
    60         this.settings = settings;
    61       else {
    62         this.settings = new Settings();
    63       }
    64     }
    65 
    66     private void Add(IGroup group) {
    67       if (groups.Contains(group))
    68         return;
    69 
    70       groups.Add(group);
    71 
    72       if (HardwareAdded != null)
    73         foreach (IHardware hardware in group.Hardware)
    74           HardwareAdded(hardware);
    75     }
    76 
    77     private void Remove(IGroup group) {
    78       if (!groups.Contains(group))
    79         return;
    80 
    81       groups.Remove(group);
    82 
    83       if (HardwareRemoved != null)
    84         foreach (IHardware hardware in group.Hardware)
    85           HardwareRemoved(hardware);
    86     }
    87 
    88     [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
    89     public void Open() {
    90       if (open)
    91         return;
    92 
    93       WinRing0.Open();
    94 
    95       Add(new Mainboard.MainboardGroup(settings));
    96       Add(new CPU.CPUGroup(settings));
    97       Add(new ATI.ATIGroup(settings));
    98       Add(new Nvidia.NvidiaGroup(settings));      
    99       Add(new TBalancer.TBalancerGroup(settings));
   100       Add(new Heatmaster.HeatmasterGroup(settings));
   101 
   102       if (hddEnabled)
   103         Add(new HDD.HDDGroup(settings));
   104 
   105       open = true;
   106     }
   107     
   108     public bool HDDEnabled {
   109       get { return hddEnabled; }
   110 
   111       [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
   112       set {
   113         if (open && value && !hddEnabled) {
   114           Add(new HDD.HDDGroup(settings));
   115         } else if (open && !value && hddEnabled) {
   116           List<IGroup> list = new List<IGroup>();
   117           foreach (IGroup group in groups)
   118             if (group is HDD.HDDGroup)
   119               list.Add(group);
   120           foreach (IGroup group in list)
   121             Remove(group);
   122         }
   123         hddEnabled = value;
   124       }
   125     }
   126 
   127     public IHardware[] Hardware {
   128       get {
   129         List<IHardware> list = new List<IHardware>();
   130         foreach (IGroup group in groups)
   131           foreach (IHardware hardware in group.Hardware)
   132             list.Add(hardware);
   133         return list.ToArray();
   134       }
   135     }
   136 
   137     private static void NewSection(TextWriter writer) {
   138       for (int i = 0; i < 8; i++)
   139         writer.Write("----------");
   140       writer.WriteLine();
   141       writer.WriteLine();
   142     }
   143 
   144     private int CompareSensor(ISensor a, ISensor b) {
   145       int c = a.SensorType.CompareTo(b.SensorType);
   146       if (c == 0)
   147         return a.Index.CompareTo(b.Index);
   148       else
   149         return c;
   150     }
   151 
   152     private void ReportHardwareSensorTree(IHardware hardware, TextWriter w,
   153       string space) {
   154       w.WriteLine("{0}|", space);
   155       w.WriteLine("{0}+-+ {1} ({2})",
   156         space, hardware.Name, hardware.Identifier);
   157       ISensor[] sensors = hardware.Sensors;
   158       Array.Sort<ISensor>(sensors, CompareSensor);
   159       foreach (ISensor sensor in sensors) {
   160         w.WriteLine("{0}|   +- {1}[{2}] : {3} : {4}",
   161           space, sensor.SensorType, sensor.Index, 
   162             string.Format(CultureInfo.InvariantCulture, "{0} : {1} : {2}",
   163             sensor.Value, sensor.Min, sensor.Max), sensor.Name);
   164       }
   165       foreach (IHardware subHardware in hardware.SubHardware)
   166         ReportHardwareSensorTree(subHardware, w, "|   ");
   167     }
   168 
   169     private void ReportHardwareParameterTree(IHardware hardware, TextWriter w,
   170       string space) {
   171       w.WriteLine("{0}|", space);
   172       w.WriteLine("{0}+-+ {1} ({2})",
   173         space, hardware.Name, hardware.Identifier);
   174       ISensor[] sensors = hardware.Sensors;
   175       Array.Sort<ISensor>(sensors, CompareSensor);
   176       foreach (ISensor sensor in sensors) {
   177         if (sensor.Parameters.Length > 0) {
   178           w.WriteLine("{0}|   +- {1}[{2}] : {3}",
   179             space, sensor.SensorType, sensor.Index, sensor.Name);
   180           foreach (IParameter parameter in sensor.Parameters) {
   181             w.WriteLine("{0}|      +- {1} : {2}",
   182               space, parameter.Name,
   183               string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
   184                 parameter.DefaultValue, parameter.Value));
   185           }
   186         }
   187       }
   188       foreach (IHardware subHardware in hardware.SubHardware)
   189         ReportHardwareParameterTree(subHardware, w, "|   ");
   190     }
   191 
   192     private void ReportHardware(IHardware hardware, TextWriter w) {
   193       string hardwareReport = hardware.GetReport();
   194       if (!string.IsNullOrEmpty(hardwareReport)) {
   195         NewSection(w);
   196         w.Write(hardwareReport);
   197       }
   198       foreach (IHardware subHardware in hardware.SubHardware)
   199         ReportHardware(subHardware, w);
   200     }
   201 
   202     public string GetReport() {
   203 
   204       using (StringWriter w = new StringWriter(CultureInfo.InvariantCulture)) {
   205 
   206         w.WriteLine();
   207         w.WriteLine("Open Hardware Monitor Report");
   208         w.WriteLine();
   209 
   210         Version version = typeof(Computer).Assembly.GetName().Version;
   211 
   212         NewSection(w);
   213         w.Write("Version: "); w.WriteLine(version.ToString());
   214         w.WriteLine();
   215 
   216         NewSection(w);
   217         w.Write("Common Language Runtime: ");
   218         w.WriteLine(Environment.Version.ToString());
   219         w.Write("Operating System: ");
   220         w.WriteLine(Environment.OSVersion.ToString());
   221         w.Write("Process Type: ");
   222         w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit");
   223         w.WriteLine();
   224 
   225         NewSection(w);
   226         w.WriteLine("Sensors");
   227         w.WriteLine();
   228         foreach (IGroup group in groups) {
   229           foreach (IHardware hardware in group.Hardware)
   230             ReportHardwareSensorTree(hardware, w, "");
   231         }
   232         w.WriteLine();
   233 
   234         NewSection(w);
   235         w.WriteLine("Parameters");
   236         w.WriteLine();
   237         foreach (IGroup group in groups) {
   238           foreach (IHardware hardware in group.Hardware)
   239             ReportHardwareParameterTree(hardware, w, "");
   240         }
   241         w.WriteLine();
   242 
   243         foreach (IGroup group in groups) {
   244           string report = group.GetReport();
   245           if (!string.IsNullOrEmpty(report)) {
   246             NewSection(w);
   247             w.Write(report);
   248           }
   249 
   250           IHardware[] hardwareArray = group.Hardware;
   251           foreach (IHardware hardware in hardwareArray)
   252             ReportHardware(hardware, w);
   253 
   254         }
   255         return w.ToString();
   256       }
   257     }
   258 
   259     public void Close() {      
   260       if (!open)
   261         return;
   262 
   263       foreach (IGroup group in groups)
   264         group.Close();
   265       groups.Clear();
   266 
   267       WinRing0.Close();
   268 
   269       open = false;
   270     }
   271 
   272     public event HardwareEventHandler HardwareAdded;
   273     public event HardwareEventHandler HardwareRemoved;
   274 
   275     public void Accept(IVisitor visitor) {
   276       if (visitor == null)
   277         throw new ArgumentNullException("visitor");
   278       visitor.VisitComputer(this);
   279     }
   280 
   281     public void Traverse(IVisitor visitor) {
   282       foreach (IGroup group in groups)
   283         foreach (IHardware hardware in group.Hardware) 
   284           hardware.Accept(visitor);
   285     }
   286 
   287     private class Settings : ISettings {
   288 
   289       public bool Contains(string name) {
   290         return false;
   291       }
   292 
   293       public void SetValue(string name, string value) { }
   294 
   295       public string GetValue(string name, string value) {
   296         return value;
   297       }
   298 
   299       public void Remove(string name) { }
   300     }
   301   }
   302 }