GUI/SensorSystemTray.cs
author moel.mich
Sun, 25 Apr 2010 14:40:19 +0000
changeset 97 1a2d7dad320e
parent 64 15181001ee61
child 109 70d0c3102424
permissions -rw-r--r--
Fixed Issue 43.
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@42
    40
using System.Drawing;
moel@40
    41
using System.Text;
moel@40
    42
using System.Windows.Forms;
moel@40
    43
using OpenHardwareMonitor.Hardware;
moel@40
    44
using OpenHardwareMonitor.Utilities;
moel@40
    45
moel@40
    46
namespace OpenHardwareMonitor.GUI {
moel@40
    47
  public class SensorSystemTray : IDisposable {
moel@83
    48
    private IComputer computer;
moel@40
    49
    private List<SensorNotifyIcon> list = new List<SensorNotifyIcon>();
moel@40
    50
moel@83
    51
    public SensorSystemTray(IComputer computer) {
moel@40
    52
      this.computer = computer;
moel@40
    53
      computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
moel@40
    54
      computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
moel@40
    55
    }
moel@40
    56
moel@40
    57
    private void HardwareRemoved(IHardware hardware) {
moel@40
    58
      hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
moel@40
    59
      hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
moel@40
    60
      foreach (ISensor sensor in hardware.Sensors) 
moel@40
    61
        SensorRemoved(sensor);
moel@64
    62
      foreach (IHardware subHardware in hardware.SubHardware)
moel@64
    63
        HardwareRemoved(subHardware);
moel@40
    64
    }
moel@64
    65
moel@40
    66
    private void HardwareAdded(IHardware hardware) {
moel@40
    67
      foreach (ISensor sensor in hardware.Sensors)
moel@40
    68
        SensorAdded(sensor);
moel@40
    69
      hardware.SensorAdded += new SensorEventHandler(SensorAdded);
moel@40
    70
      hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
moel@64
    71
      foreach (IHardware subHardware in hardware.SubHardware)
moel@64
    72
        HardwareAdded(subHardware);
moel@40
    73
    }
moel@40
    74
moel@40
    75
    private void SensorAdded(ISensor sensor) {
moel@40
    76
      if (Config.Get(sensor.Identifier + "/tray", false)) 
moel@42
    77
        Add(sensor, false);   
moel@40
    78
    }
moel@40
    79
moel@40
    80
    private void SensorRemoved(ISensor sensor) {
moel@42
    81
      if (Contains(sensor)) 
moel@42
    82
        Remove(sensor, false);
moel@40
    83
    }
moel@40
    84
moel@40
    85
    public void Dispose() {
moel@40
    86
      foreach (SensorNotifyIcon icon in list)
moel@40
    87
        icon.Dispose();
moel@40
    88
    }
moel@40
    89
moel@40
    90
    public void Redraw() {
moel@40
    91
      foreach (SensorNotifyIcon icon in list)
moel@40
    92
        icon.Update();
moel@40
    93
    }
moel@40
    94
moel@40
    95
    public bool Contains(ISensor sensor) {
moel@40
    96
      foreach (SensorNotifyIcon icon in list)
moel@40
    97
        if (icon.Sensor == sensor)
moel@40
    98
          return true;
moel@40
    99
      return false;
moel@40
   100
    }
moel@40
   101
moel@42
   102
    public void Add(ISensor sensor, bool balloonTip) {
moel@40
   103
      if (Contains(sensor)) {
moel@40
   104
        return;
moel@42
   105
      } else {        
moel@42
   106
        list.Add(new SensorNotifyIcon(this, sensor, balloonTip));
moel@40
   107
        Config.Set(sensor.Identifier + "/tray", true);
moel@40
   108
      }
moel@40
   109
    }
moel@40
   110
moel@40
   111
    public void Remove(ISensor sensor) {
moel@42
   112
      Remove(sensor, true);
moel@42
   113
    }
moel@42
   114
moel@42
   115
    private void Remove(ISensor sensor, bool deleteConfig) {
moel@42
   116
      if (deleteConfig) {
moel@42
   117
        Config.Remove(sensor.Identifier + "/tray");
moel@42
   118
        Config.Remove(sensor.Identifier + "/traycolor");
moel@42
   119
      }
moel@40
   120
      SensorNotifyIcon instance = null;
moel@40
   121
      foreach (SensorNotifyIcon icon in list)
moel@40
   122
        if (icon.Sensor == sensor)
moel@40
   123
          instance = icon;
moel@40
   124
      if (instance != null) {
moel@40
   125
        list.Remove(instance);
moel@40
   126
        instance.Dispose();
moel@40
   127
      }
moel@40
   128
    }
moel@40
   129
moel@40
   130
  }
moel@40
   131
}