Hardware/Hardware.cs
author moel.mich
Sun, 15 May 2011 16:44:14 +0000
changeset 284 075a1822ef34
parent 247 6dc755f1970e
child 298 96263190189a
permissions -rw-r--r--
Fixed Issue 219.
moel@31
     1
/*
moel@31
     2
  
moel@31
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@31
     4
moel@31
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@31
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@31
     7
  the License. You may obtain a copy of the License at
moel@31
     8
 
moel@31
     9
  http://www.mozilla.org/MPL/
moel@31
    10
moel@31
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@31
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@31
    13
  for the specific language governing rights and limitations under the License.
moel@31
    14
moel@31
    15
  The Original Code is the Open Hardware Monitor code.
moel@31
    16
moel@31
    17
  The Initial Developer of the Original Code is 
moel@31
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@275
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2011
moel@31
    20
  the Initial Developer. All Rights Reserved.
moel@31
    21
moel@31
    22
  Contributor(s):
moel@31
    23
moel@31
    24
  Alternatively, the contents of this file may be used under the terms of
moel@31
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@31
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@31
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@31
    28
  of those above. If you wish to allow use of your version of this file only
moel@31
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@31
    30
  use your version of this file under the terms of the MPL, indicate your
moel@31
    31
  decision by deleting the provisions above and replace them with the notice
moel@31
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@31
    33
  the provisions above, a recipient may use your version of this file under
moel@31
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@31
    35
 
moel@31
    36
*/
moel@31
    37
moel@31
    38
using System;
moel@165
    39
using OpenHardwareMonitor.Collections;
moel@31
    40
moel@31
    41
namespace OpenHardwareMonitor.Hardware {
moel@165
    42
  internal abstract class Hardware : IHardware {
moel@31
    43
moel@275
    44
    private readonly Identifier identifier;
moel@275
    45
    protected readonly string name;
moel@275
    46
    private string customName;
moel@275
    47
    protected readonly ISettings settings;
moel@275
    48
    protected readonly ListSet<ISensor> active = new ListSet<ISensor>();
moel@275
    49
moel@275
    50
    public Hardware(string name, Identifier identifier, ISettings settings) {
moel@275
    51
      this.settings = settings;
moel@275
    52
      this.identifier = identifier;
moel@275
    53
      this.name = name;
moel@275
    54
      this.customName = settings.GetValue(
moel@275
    55
        new Identifier(Identifier, "name").ToString(), name);
moel@275
    56
    }
moel@31
    57
moel@64
    58
    public IHardware[] SubHardware {
moel@64
    59
      get { return new IHardware[0]; }
moel@64
    60
    }
moel@64
    61
moel@176
    62
    public virtual IHardware Parent {
moel@176
    63
      get { return null; }
moel@176
    64
    }
moel@176
    65
moel@275
    66
    public virtual ISensor[] Sensors {
moel@31
    67
      get { return active.ToArray(); }
moel@31
    68
    }
moel@31
    69
moel@275
    70
    protected virtual void ActivateSensor(ISensor sensor) {
moel@110
    71
      if (active.Add(sensor)) 
moel@31
    72
        if (SensorAdded != null)
moel@31
    73
          SensorAdded(sensor);
moel@31
    74
    }
moel@31
    75
moel@275
    76
    protected virtual void DeactivateSensor(ISensor sensor) {
moel@110
    77
      if (active.Remove(sensor))
moel@31
    78
        if (SensorRemoved != null)
moel@110
    79
          SensorRemoved(sensor);     
moel@31
    80
    }
moel@31
    81
moel@275
    82
    public string Name {
moel@275
    83
      get {
moel@275
    84
        return customName;
moel@275
    85
      }
moel@275
    86
      set {
moel@275
    87
        if (!string.IsNullOrEmpty(value))
moel@275
    88
          customName = value;
moel@275
    89
        else
moel@275
    90
          customName = name;
moel@275
    91
        settings.SetValue(new Identifier(Identifier, "name").ToString(), 
moel@275
    92
          customName);
moel@275
    93
      }
moel@275
    94
    }
moel@275
    95
moel@275
    96
    public Identifier Identifier {
moel@275
    97
      get {
moel@275
    98
        return identifier;
moel@275
    99
      }
moel@275
   100
    }
moel@275
   101
moel@64
   102
    #pragma warning disable 67
moel@31
   103
    public event SensorEventHandler SensorAdded;
moel@31
   104
    public event SensorEventHandler SensorRemoved;
moel@64
   105
    #pragma warning restore 67
moel@110
   106
  
moel@275
   107
    
moel@165
   108
    public abstract HardwareType HardwareType { get; }
moel@110
   109
moel@110
   110
    public virtual string GetReport() {
moel@110
   111
      return null;
moel@110
   112
    }
moel@110
   113
moel@110
   114
    public abstract void Update();
moel@110
   115
moel@110
   116
    public void Accept(IVisitor visitor) {
moel@167
   117
      if (visitor == null)
moel@167
   118
        throw new ArgumentNullException("visitor");
moel@167
   119
      visitor.VisitHardware(this);
moel@110
   120
    }
moel@110
   121
moel@275
   122
    public virtual void Traverse(IVisitor visitor) {
moel@110
   123
      foreach (ISensor sensor in active)
moel@110
   124
        sensor.Accept(visitor);
moel@110
   125
    }
moel@31
   126
  }
moel@31
   127
}