Hardware/Hardware.cs
author moel.mich
Sun, 15 May 2011 21:43:40 +0000
changeset 288 a35a89a35532
parent 247 6dc755f1970e
child 298 96263190189a
permissions -rw-r--r--
Improved the selection dragging on the tree view. The selection moves now only when the dragging starts on the tree view. With the old implementation, double-clicking the gadget would change the selection, because the mouse is still pressed when the main window is shown.
     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-2011
    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 OpenHardwareMonitor.Collections;
    40 
    41 namespace OpenHardwareMonitor.Hardware {
    42   internal abstract class Hardware : IHardware {
    43 
    44     private readonly Identifier identifier;
    45     protected readonly string name;
    46     private string customName;
    47     protected readonly ISettings settings;
    48     protected readonly ListSet<ISensor> active = new ListSet<ISensor>();
    49 
    50     public Hardware(string name, Identifier identifier, ISettings settings) {
    51       this.settings = settings;
    52       this.identifier = identifier;
    53       this.name = name;
    54       this.customName = settings.GetValue(
    55         new Identifier(Identifier, "name").ToString(), name);
    56     }
    57 
    58     public IHardware[] SubHardware {
    59       get { return new IHardware[0]; }
    60     }
    61 
    62     public virtual IHardware Parent {
    63       get { return null; }
    64     }
    65 
    66     public virtual ISensor[] Sensors {
    67       get { return active.ToArray(); }
    68     }
    69 
    70     protected virtual void ActivateSensor(ISensor sensor) {
    71       if (active.Add(sensor)) 
    72         if (SensorAdded != null)
    73           SensorAdded(sensor);
    74     }
    75 
    76     protected virtual void DeactivateSensor(ISensor sensor) {
    77       if (active.Remove(sensor))
    78         if (SensorRemoved != null)
    79           SensorRemoved(sensor);     
    80     }
    81 
    82     public string Name {
    83       get {
    84         return customName;
    85       }
    86       set {
    87         if (!string.IsNullOrEmpty(value))
    88           customName = value;
    89         else
    90           customName = name;
    91         settings.SetValue(new Identifier(Identifier, "name").ToString(), 
    92           customName);
    93       }
    94     }
    95 
    96     public Identifier Identifier {
    97       get {
    98         return identifier;
    99       }
   100     }
   101 
   102     #pragma warning disable 67
   103     public event SensorEventHandler SensorAdded;
   104     public event SensorEventHandler SensorRemoved;
   105     #pragma warning restore 67
   106   
   107     
   108     public abstract HardwareType HardwareType { get; }
   109 
   110     public virtual string GetReport() {
   111       return null;
   112     }
   113 
   114     public abstract void Update();
   115 
   116     public void Accept(IVisitor visitor) {
   117       if (visitor == null)
   118         throw new ArgumentNullException("visitor");
   119       visitor.VisitHardware(this);
   120     }
   121 
   122     public virtual void Traverse(IVisitor visitor) {
   123       foreach (ISensor sensor in active)
   124         sensor.Accept(visitor);
   125     }
   126   }
   127 }