GUI/SensorSystemTray.cs
author moel.mich
Fri, 12 Feb 2010 08:17:51 +0000
changeset 41 d92bacc2116e
child 42 47385d4fc990
permissions -rw-r--r--
Changed the T-Balancer detection and added more report details.
moel@40
     1
/*
moel@40
     2
  
moel@40
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@40
     4
moel@40
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@40
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@40
     7
  the License. You may obtain a copy of the License at
moel@40
     8
 
moel@40
     9
  http://www.mozilla.org/MPL/
moel@40
    10
moel@40
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@40
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@40
    13
  for the specific language governing rights and limitations under the License.
moel@40
    14
moel@40
    15
  The Original Code is the Open Hardware Monitor code.
moel@40
    16
moel@40
    17
  The Initial Developer of the Original Code is 
moel@40
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@40
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@40
    20
  the Initial Developer. All Rights Reserved.
moel@40
    21
moel@40
    22
  Contributor(s):
moel@40
    23
moel@40
    24
  Alternatively, the contents of this file may be used under the terms of
moel@40
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@40
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@40
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@40
    28
  of those above. If you wish to allow use of your version of this file only
moel@40
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@40
    30
  use your version of this file under the terms of the MPL, indicate your
moel@40
    31
  decision by deleting the provisions above and replace them with the notice
moel@40
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@40
    33
  the provisions above, a recipient may use your version of this file under
moel@40
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@40
    35
 
moel@40
    36
*/
moel@40
    37
moel@40
    38
using System;
moel@40
    39
using System.Collections.Generic;
moel@40
    40
using System.Text;
moel@40
    41
using System.Windows.Forms;
moel@40
    42
using OpenHardwareMonitor.Hardware;
moel@40
    43
using OpenHardwareMonitor.Utilities;
moel@40
    44
moel@40
    45
namespace OpenHardwareMonitor.GUI {
moel@40
    46
  public class SensorSystemTray : IDisposable {
moel@40
    47
    private Computer computer;
moel@40
    48
    private List<SensorNotifyIcon> list = new List<SensorNotifyIcon>();
moel@40
    49
moel@40
    50
    public SensorSystemTray(Computer computer) {
moel@40
    51
      this.computer = computer;
moel@40
    52
      computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
moel@40
    53
      computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
moel@40
    54
    }
moel@40
    55
moel@40
    56
    private void HardwareRemoved(IHardware hardware) {
moel@40
    57
      hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
moel@40
    58
      hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
moel@40
    59
      foreach (ISensor sensor in hardware.Sensors) 
moel@40
    60
        SensorRemoved(sensor);
moel@40
    61
    }
moel@40
    62
   
moel@40
    63
    private void HardwareAdded(IHardware hardware) {
moel@40
    64
      foreach (ISensor sensor in hardware.Sensors)
moel@40
    65
        SensorAdded(sensor);
moel@40
    66
      hardware.SensorAdded += new SensorEventHandler(SensorAdded);
moel@40
    67
      hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
moel@40
    68
    }
moel@40
    69
moel@40
    70
    private void SensorAdded(ISensor sensor) {
moel@40
    71
      if (Config.Get(sensor.Identifier + "/tray", false)) 
moel@40
    72
        Add(sensor);   
moel@40
    73
    }
moel@40
    74
moel@40
    75
    private void SensorRemoved(ISensor sensor) {
moel@40
    76
      if (Contains(sensor)) {        
moel@40
    77
        Remove(sensor);
moel@40
    78
        Config.Set(sensor.Identifier + "/tray", true);
moel@40
    79
      }
moel@40
    80
    }
moel@40
    81
moel@40
    82
    public void Dispose() {
moel@40
    83
      foreach (SensorNotifyIcon icon in list)
moel@40
    84
        icon.Dispose();
moel@40
    85
    }
moel@40
    86
moel@40
    87
    public void Redraw() {
moel@40
    88
      foreach (SensorNotifyIcon icon in list)
moel@40
    89
        icon.Update();
moel@40
    90
    }
moel@40
    91
moel@40
    92
    public bool Contains(ISensor sensor) {
moel@40
    93
      foreach (SensorNotifyIcon icon in list)
moel@40
    94
        if (icon.Sensor == sensor)
moel@40
    95
          return true;
moel@40
    96
      return false;
moel@40
    97
    }
moel@40
    98
moel@40
    99
    public void Add(ISensor sensor) {
moel@40
   100
      if (Contains(sensor)) {
moel@40
   101
        return;
moel@40
   102
      } else {
moel@40
   103
        list.Add(new SensorNotifyIcon(this, sensor));
moel@40
   104
        Config.Set(sensor.Identifier + "/tray", true);
moel@40
   105
      }
moel@40
   106
    }
moel@40
   107
moel@40
   108
    public void Remove(ISensor sensor) {
moel@40
   109
      Config.Remove(sensor.Identifier + "/tray");
moel@40
   110
      SensorNotifyIcon instance = null;
moel@40
   111
      foreach (SensorNotifyIcon icon in list)
moel@40
   112
        if (icon.Sensor == sensor)
moel@40
   113
          instance = icon;
moel@40
   114
      if (instance != null) {
moel@40
   115
        list.Remove(instance);
moel@40
   116
        instance.Dispose();
moel@40
   117
      }
moel@40
   118
    }
moel@40
   119
moel@40
   120
  }
moel@40
   121
}