moel@40: /*
moel@40:   
moel@40:   Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@40: 
moel@40:   The contents of this file are subject to the Mozilla Public License Version
moel@40:   1.1 (the "License"); you may not use this file except in compliance with
moel@40:   the License. You may obtain a copy of the License at
moel@40:  
moel@40:   http://www.mozilla.org/MPL/
moel@40: 
moel@40:   Software distributed under the License is distributed on an "AS IS" basis,
moel@40:   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@40:   for the specific language governing rights and limitations under the License.
moel@40: 
moel@40:   The Original Code is the Open Hardware Monitor code.
moel@40: 
moel@40:   The Initial Developer of the Original Code is 
moel@40:   Michael Möller <m.moeller@gmx.ch>.
moel@40:   Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@40:   the Initial Developer. All Rights Reserved.
moel@40: 
moel@40:   Contributor(s):
moel@40: 
moel@40:   Alternatively, the contents of this file may be used under the terms of
moel@40:   either the GNU General Public License Version 2 or later (the "GPL"), or
moel@40:   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@40:   in which case the provisions of the GPL or the LGPL are applicable instead
moel@40:   of those above. If you wish to allow use of your version of this file only
moel@40:   under the terms of either the GPL or the LGPL, and not to allow others to
moel@40:   use your version of this file under the terms of the MPL, indicate your
moel@40:   decision by deleting the provisions above and replace them with the notice
moel@40:   and other provisions required by the GPL or the LGPL. If you do not delete
moel@40:   the provisions above, a recipient may use your version of this file under
moel@40:   the terms of any one of the MPL, the GPL or the LGPL.
moel@40:  
moel@40: */
moel@40: 
moel@40: using System;
moel@40: using System.Collections.Generic;
moel@42: using System.Drawing;
moel@40: using System.Text;
moel@40: using System.Windows.Forms;
moel@40: using OpenHardwareMonitor.Hardware;
moel@40: using OpenHardwareMonitor.Utilities;
moel@40: 
moel@40: namespace OpenHardwareMonitor.GUI {
moel@40:   public class SensorSystemTray : IDisposable {
moel@83:     private IComputer computer;
moel@40:     private List<SensorNotifyIcon> list = new List<SensorNotifyIcon>();
moel@40: 
moel@83:     public SensorSystemTray(IComputer computer) {
moel@40:       this.computer = computer;
moel@40:       computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
moel@40:       computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
moel@40:     }
moel@40: 
moel@40:     private void HardwareRemoved(IHardware hardware) {
moel@40:       hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
moel@40:       hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
moel@40:       foreach (ISensor sensor in hardware.Sensors) 
moel@40:         SensorRemoved(sensor);
moel@64:       foreach (IHardware subHardware in hardware.SubHardware)
moel@64:         HardwareRemoved(subHardware);
moel@40:     }
moel@64: 
moel@40:     private void HardwareAdded(IHardware hardware) {
moel@40:       foreach (ISensor sensor in hardware.Sensors)
moel@40:         SensorAdded(sensor);
moel@40:       hardware.SensorAdded += new SensorEventHandler(SensorAdded);
moel@40:       hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
moel@64:       foreach (IHardware subHardware in hardware.SubHardware)
moel@64:         HardwareAdded(subHardware);
moel@40:     }
moel@40: 
moel@40:     private void SensorAdded(ISensor sensor) {
moel@40:       if (Config.Get(sensor.Identifier + "/tray", false)) 
moel@42:         Add(sensor, false);   
moel@40:     }
moel@40: 
moel@40:     private void SensorRemoved(ISensor sensor) {
moel@42:       if (Contains(sensor)) 
moel@42:         Remove(sensor, false);
moel@40:     }
moel@40: 
moel@40:     public void Dispose() {
moel@40:       foreach (SensorNotifyIcon icon in list)
moel@40:         icon.Dispose();
moel@40:     }
moel@40: 
moel@40:     public void Redraw() {
moel@40:       foreach (SensorNotifyIcon icon in list)
moel@40:         icon.Update();
moel@40:     }
moel@40: 
moel@40:     public bool Contains(ISensor sensor) {
moel@40:       foreach (SensorNotifyIcon icon in list)
moel@40:         if (icon.Sensor == sensor)
moel@40:           return true;
moel@40:       return false;
moel@40:     }
moel@40: 
moel@42:     public void Add(ISensor sensor, bool balloonTip) {
moel@40:       if (Contains(sensor)) {
moel@40:         return;
moel@42:       } else {        
moel@42:         list.Add(new SensorNotifyIcon(this, sensor, balloonTip));
moel@40:         Config.Set(sensor.Identifier + "/tray", true);
moel@40:       }
moel@40:     }
moel@40: 
moel@40:     public void Remove(ISensor sensor) {
moel@42:       Remove(sensor, true);
moel@42:     }
moel@42: 
moel@42:     private void Remove(ISensor sensor, bool deleteConfig) {
moel@42:       if (deleteConfig) {
moel@42:         Config.Remove(sensor.Identifier + "/tray");
moel@42:         Config.Remove(sensor.Identifier + "/traycolor");
moel@42:       }
moel@40:       SensorNotifyIcon instance = null;
moel@40:       foreach (SensorNotifyIcon icon in list)
moel@40:         if (icon.Sensor == sensor)
moel@40:           instance = icon;
moel@40:       if (instance != null) {
moel@40:         list.Remove(instance);
moel@40:         instance.Dispose();
moel@40:       }
moel@40:     }
moel@40: 
moel@40:   }
moel@40: }