GUI/SensorSystemTray.cs
changeset 40 2392f7402fb6
child 42 47385d4fc990
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/GUI/SensorSystemTray.cs	Fri Feb 12 00:36:56 2010 +0000
     1.3 @@ -0,0 +1,121 @@
     1.4 +/*
     1.5 +  
     1.6 +  Version: MPL 1.1/GPL 2.0/LGPL 2.1
     1.7 +
     1.8 +  The contents of this file are subject to the Mozilla Public License Version
     1.9 +  1.1 (the "License"); you may not use this file except in compliance with
    1.10 +  the License. You may obtain a copy of the License at
    1.11 + 
    1.12 +  http://www.mozilla.org/MPL/
    1.13 +
    1.14 +  Software distributed under the License is distributed on an "AS IS" basis,
    1.15 +  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    1.16 +  for the specific language governing rights and limitations under the License.
    1.17 +
    1.18 +  The Original Code is the Open Hardware Monitor code.
    1.19 +
    1.20 +  The Initial Developer of the Original Code is 
    1.21 +  Michael Möller <m.moeller@gmx.ch>.
    1.22 +  Portions created by the Initial Developer are Copyright (C) 2009-2010
    1.23 +  the Initial Developer. All Rights Reserved.
    1.24 +
    1.25 +  Contributor(s):
    1.26 +
    1.27 +  Alternatively, the contents of this file may be used under the terms of
    1.28 +  either the GNU General Public License Version 2 or later (the "GPL"), or
    1.29 +  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    1.30 +  in which case the provisions of the GPL or the LGPL are applicable instead
    1.31 +  of those above. If you wish to allow use of your version of this file only
    1.32 +  under the terms of either the GPL or the LGPL, and not to allow others to
    1.33 +  use your version of this file under the terms of the MPL, indicate your
    1.34 +  decision by deleting the provisions above and replace them with the notice
    1.35 +  and other provisions required by the GPL or the LGPL. If you do not delete
    1.36 +  the provisions above, a recipient may use your version of this file under
    1.37 +  the terms of any one of the MPL, the GPL or the LGPL.
    1.38 + 
    1.39 +*/
    1.40 +
    1.41 +using System;
    1.42 +using System.Collections.Generic;
    1.43 +using System.Text;
    1.44 +using System.Windows.Forms;
    1.45 +using OpenHardwareMonitor.Hardware;
    1.46 +using OpenHardwareMonitor.Utilities;
    1.47 +
    1.48 +namespace OpenHardwareMonitor.GUI {
    1.49 +  public class SensorSystemTray : IDisposable {
    1.50 +    private Computer computer;
    1.51 +    private List<SensorNotifyIcon> list = new List<SensorNotifyIcon>();
    1.52 +
    1.53 +    public SensorSystemTray(Computer computer) {
    1.54 +      this.computer = computer;
    1.55 +      computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
    1.56 +      computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
    1.57 +    }
    1.58 +
    1.59 +    private void HardwareRemoved(IHardware hardware) {
    1.60 +      hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
    1.61 +      hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
    1.62 +      foreach (ISensor sensor in hardware.Sensors) 
    1.63 +        SensorRemoved(sensor);
    1.64 +    }
    1.65 +   
    1.66 +    private void HardwareAdded(IHardware hardware) {
    1.67 +      foreach (ISensor sensor in hardware.Sensors)
    1.68 +        SensorAdded(sensor);
    1.69 +      hardware.SensorAdded += new SensorEventHandler(SensorAdded);
    1.70 +      hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
    1.71 +    }
    1.72 +
    1.73 +    private void SensorAdded(ISensor sensor) {
    1.74 +      if (Config.Get(sensor.Identifier + "/tray", false)) 
    1.75 +        Add(sensor);   
    1.76 +    }
    1.77 +
    1.78 +    private void SensorRemoved(ISensor sensor) {
    1.79 +      if (Contains(sensor)) {        
    1.80 +        Remove(sensor);
    1.81 +        Config.Set(sensor.Identifier + "/tray", true);
    1.82 +      }
    1.83 +    }
    1.84 +
    1.85 +    public void Dispose() {
    1.86 +      foreach (SensorNotifyIcon icon in list)
    1.87 +        icon.Dispose();
    1.88 +    }
    1.89 +
    1.90 +    public void Redraw() {
    1.91 +      foreach (SensorNotifyIcon icon in list)
    1.92 +        icon.Update();
    1.93 +    }
    1.94 +
    1.95 +    public bool Contains(ISensor sensor) {
    1.96 +      foreach (SensorNotifyIcon icon in list)
    1.97 +        if (icon.Sensor == sensor)
    1.98 +          return true;
    1.99 +      return false;
   1.100 +    }
   1.101 +
   1.102 +    public void Add(ISensor sensor) {
   1.103 +      if (Contains(sensor)) {
   1.104 +        return;
   1.105 +      } else {
   1.106 +        list.Add(new SensorNotifyIcon(this, sensor));
   1.107 +        Config.Set(sensor.Identifier + "/tray", true);
   1.108 +      }
   1.109 +    }
   1.110 +
   1.111 +    public void Remove(ISensor sensor) {
   1.112 +      Config.Remove(sensor.Identifier + "/tray");
   1.113 +      SensorNotifyIcon instance = null;
   1.114 +      foreach (SensorNotifyIcon icon in list)
   1.115 +        if (icon.Sensor == sensor)
   1.116 +          instance = icon;
   1.117 +      if (instance != null) {
   1.118 +        list.Remove(instance);
   1.119 +        instance.Dispose();
   1.120 +      }
   1.121 +    }
   1.122 +
   1.123 +  }
   1.124 +}