moel@133: /* moel@133: moel@133: Version: MPL 1.1/GPL 2.0/LGPL 2.1 moel@133: moel@133: The contents of this file are subject to the Mozilla Public License Version moel@133: 1.1 (the "License"); you may not use this file except in compliance with moel@133: the License. You may obtain a copy of the License at moel@133: moel@133: http://www.mozilla.org/MPL/ moel@133: moel@133: Software distributed under the License is distributed on an "AS IS" basis, moel@133: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License moel@133: for the specific language governing rights and limitations under the License. moel@133: moel@133: The Original Code is the Open Hardware Monitor code. moel@133: moel@133: The Initial Developer of the Original Code is moel@133: Michael Möller . moel@303: Portions created by the Initial Developer are Copyright (C) 2009-2011 moel@133: the Initial Developer. All Rights Reserved. moel@133: moel@133: Contributor(s): moel@133: moel@133: Alternatively, the contents of this file may be used under the terms of moel@133: either the GNU General Public License Version 2 or later (the "GPL"), or moel@133: the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), moel@133: in which case the provisions of the GPL or the LGPL are applicable instead moel@133: of those above. If you wish to allow use of your version of this file only moel@133: under the terms of either the GPL or the LGPL, and not to allow others to moel@133: use your version of this file under the terms of the MPL, indicate your moel@133: decision by deleting the provisions above and replace them with the notice moel@133: and other provisions required by the GPL or the LGPL. If you do not delete moel@133: the provisions above, a recipient may use your version of this file under moel@133: the terms of any one of the MPL, the GPL or the LGPL. moel@133: moel@133: */ moel@133: moel@133: using System; moel@133: using System.Collections.Generic; moel@133: using System.Drawing; moel@133: using System.Text; moel@133: using System.Windows.Forms; moel@133: using OpenHardwareMonitor.Hardware; moel@133: using OpenHardwareMonitor.Utilities; moel@133: moel@133: namespace OpenHardwareMonitor.GUI { moel@133: public class SystemTray : IDisposable { moel@133: private IComputer computer; moel@165: private PersistentSettings settings; moel@133: private List list = new List(); moel@133: private bool mainIconEnabled = false; moel@133: private NotifyIcon mainIcon; moel@133: moel@165: public SystemTray(IComputer computer, PersistentSettings settings) { moel@133: this.computer = computer; moel@165: this.settings = settings; moel@133: computer.HardwareAdded += new HardwareEventHandler(HardwareAdded); moel@133: computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved); moel@133: moel@133: this.mainIcon = new NotifyIcon(); moel@133: moel@156: ContextMenu contextMenu = new ContextMenu(); moel@156: MenuItem hideShowItem = new MenuItem("Hide/Show"); moel@133: hideShowItem.Click += delegate(object obj, EventArgs args) { moel@133: SendHideShowCommand(); moel@133: }; moel@156: contextMenu.MenuItems.Add(hideShowItem); moel@156: contextMenu.MenuItems.Add(new MenuItem("-")); moel@156: MenuItem exitItem = new MenuItem("Exit"); moel@133: exitItem.Click += delegate(object obj, EventArgs args) { moel@133: SendExitCommand(); moel@133: }; moel@156: contextMenu.MenuItems.Add(exitItem); moel@156: this.mainIcon.ContextMenu = contextMenu; moel@133: this.mainIcon.DoubleClick += delegate(object obj, EventArgs args) { moel@133: SendHideShowCommand(); moel@133: }; moel@133: this.mainIcon.Icon = EmbeddedResources.GetIcon("smallicon.ico"); moel@303: this.mainIcon.Text = "Open Hardware Monitor"; moel@133: } moel@133: moel@133: private void HardwareRemoved(IHardware hardware) { moel@133: hardware.SensorAdded -= new SensorEventHandler(SensorAdded); moel@133: hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved); moel@133: foreach (ISensor sensor in hardware.Sensors) moel@133: SensorRemoved(sensor); moel@133: foreach (IHardware subHardware in hardware.SubHardware) moel@133: HardwareRemoved(subHardware); moel@133: } moel@133: moel@133: private void HardwareAdded(IHardware hardware) { moel@133: foreach (ISensor sensor in hardware.Sensors) moel@133: SensorAdded(sensor); moel@133: hardware.SensorAdded += new SensorEventHandler(SensorAdded); moel@133: hardware.SensorRemoved += new SensorEventHandler(SensorRemoved); moel@133: foreach (IHardware subHardware in hardware.SubHardware) moel@133: HardwareAdded(subHardware); moel@133: } moel@133: moel@133: private void SensorAdded(ISensor sensor) { moel@166: if (settings.GetValue(new Identifier(sensor.Identifier, moel@133: "tray").ToString(), false)) moel@133: Add(sensor, false); moel@133: } moel@133: moel@133: private void SensorRemoved(ISensor sensor) { moel@133: if (Contains(sensor)) moel@133: Remove(sensor, false); moel@133: } moel@133: moel@133: public void Dispose() { moel@133: foreach (SensorNotifyIcon icon in list) moel@133: icon.Dispose(); moel@133: mainIcon.Dispose(); moel@133: } moel@133: moel@133: public void Redraw() { moel@133: foreach (SensorNotifyIcon icon in list) moel@133: icon.Update(); moel@133: } moel@133: moel@133: public bool Contains(ISensor sensor) { moel@133: foreach (SensorNotifyIcon icon in list) moel@133: if (icon.Sensor == sensor) moel@133: return true; moel@133: return false; moel@133: } moel@133: moel@133: public void Add(ISensor sensor, bool balloonTip) { moel@133: if (Contains(sensor)) { moel@133: return; moel@133: } else { moel@165: list.Add(new SensorNotifyIcon(this, sensor, balloonTip, settings)); moel@133: UpdateMainIconVisibilty(); moel@166: settings.SetValue(new Identifier(sensor.Identifier, "tray").ToString(), true); moel@133: } moel@133: } moel@133: moel@133: public void Remove(ISensor sensor) { moel@133: Remove(sensor, true); moel@133: } moel@133: moel@133: private void Remove(ISensor sensor, bool deleteConfig) { moel@133: if (deleteConfig) { moel@165: settings.Remove( moel@133: new Identifier(sensor.Identifier, "tray").ToString()); moel@165: settings.Remove( moel@133: new Identifier(sensor.Identifier, "traycolor").ToString()); moel@133: } moel@133: SensorNotifyIcon instance = null; moel@133: foreach (SensorNotifyIcon icon in list) moel@133: if (icon.Sensor == sensor) moel@133: instance = icon; moel@133: if (instance != null) { moel@133: list.Remove(instance); moel@133: UpdateMainIconVisibilty(); moel@133: instance.Dispose(); moel@133: } moel@133: } moel@133: moel@133: public event EventHandler HideShowCommand; moel@133: moel@133: public void SendHideShowCommand() { moel@133: if (HideShowCommand != null) moel@133: HideShowCommand(this, null); moel@133: } moel@133: moel@133: public event EventHandler ExitCommand; moel@133: moel@133: public void SendExitCommand() { moel@133: if (ExitCommand != null) moel@133: ExitCommand(this, null); moel@133: } moel@133: moel@133: private void UpdateMainIconVisibilty() { moel@133: if (mainIconEnabled) { moel@133: mainIcon.Visible = list.Count == 0; moel@133: } else { moel@133: mainIcon.Visible = false; moel@133: } moel@133: } moel@133: moel@133: public bool IsMainIconEnabled { moel@133: get { return mainIconEnabled; } moel@133: set { moel@133: if (mainIconEnabled != value) { moel@133: mainIconEnabled = value; moel@133: UpdateMainIconVisibilty(); moel@133: } moel@133: } moel@133: } moel@133: } moel@133: }