GUI/SensorSystemTray.cs
author moel.mich
Tue, 25 May 2010 18:57:28 +0000
changeset 127 76aaf45a01c7
parent 83 3fdadd4a830f
permissions -rw-r--r--
Added a workaround for the "You must keep the stream open for the lifetime of the Image." problem of the Image.FromStream method. This also reduced the overall memory usage (private working set).
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@109
    76
      if (Config.Get(new Identifier(sensor.Identifier, 
moel@109
    77
        "tray").ToString(), false)) 
moel@42
    78
        Add(sensor, false);   
moel@40
    79
    }
moel@40
    80
moel@40
    81
    private void SensorRemoved(ISensor sensor) {
moel@42
    82
      if (Contains(sensor)) 
moel@42
    83
        Remove(sensor, false);
moel@40
    84
    }
moel@40
    85
moel@40
    86
    public void Dispose() {
moel@40
    87
      foreach (SensorNotifyIcon icon in list)
moel@40
    88
        icon.Dispose();
moel@40
    89
    }
moel@40
    90
moel@40
    91
    public void Redraw() {
moel@40
    92
      foreach (SensorNotifyIcon icon in list)
moel@40
    93
        icon.Update();
moel@40
    94
    }
moel@40
    95
moel@40
    96
    public bool Contains(ISensor sensor) {
moel@40
    97
      foreach (SensorNotifyIcon icon in list)
moel@40
    98
        if (icon.Sensor == sensor)
moel@40
    99
          return true;
moel@40
   100
      return false;
moel@40
   101
    }
moel@40
   102
moel@42
   103
    public void Add(ISensor sensor, bool balloonTip) {
moel@40
   104
      if (Contains(sensor)) {
moel@40
   105
        return;
moel@42
   106
      } else {        
moel@42
   107
        list.Add(new SensorNotifyIcon(this, sensor, balloonTip));
moel@109
   108
        Config.Set(new Identifier(sensor.Identifier, "tray").ToString(), true);
moel@40
   109
      }
moel@40
   110
    }
moel@40
   111
moel@40
   112
    public void Remove(ISensor sensor) {
moel@42
   113
      Remove(sensor, true);
moel@42
   114
    }
moel@42
   115
moel@42
   116
    private void Remove(ISensor sensor, bool deleteConfig) {
moel@42
   117
      if (deleteConfig) {
moel@109
   118
        Config.Remove(
moel@109
   119
          new Identifier(sensor.Identifier, "tray").ToString());
moel@109
   120
        Config.Remove(
moel@109
   121
          new Identifier(sensor.Identifier, "traycolor").ToString());
moel@42
   122
      }
moel@40
   123
      SensorNotifyIcon instance = null;
moel@40
   124
      foreach (SensorNotifyIcon icon in list)
moel@40
   125
        if (icon.Sensor == sensor)
moel@40
   126
          instance = icon;
moel@40
   127
      if (instance != null) {
moel@40
   128
        list.Remove(instance);
moel@40
   129
        instance.Dispose();
moel@40
   130
      }
moel@40
   131
    }
moel@40
   132
moel@40
   133
  }
moel@40
   134
}