moel@133: /* moel@133: moel@344: This Source Code Form is subject to the terms of the Mozilla Public moel@344: License, v. 2.0. If a copy of the MPL was not distributed with this moel@344: file, You can obtain one at http://mozilla.org/MPL/2.0/. moel@133: moel@362: Copyright (C) 2009-2012 Michael Möller moel@344: 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@362: private UnitManager unitManager; moel@133: private List list = new List(); moel@133: private bool mainIconEnabled = false; moel@363: private NotifyIconAdv mainIcon; moel@133: moel@362: public SystemTray(IComputer computer, PersistentSettings settings, moel@362: UnitManager unitManager) moel@362: { moel@133: this.computer = computer; moel@165: this.settings = settings; moel@362: this.unitManager = unitManager; moel@133: computer.HardwareAdded += new HardwareEventHandler(HardwareAdded); moel@133: computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved); moel@133: moel@363: this.mainIcon = new NotifyIconAdv(); 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@362: list.Add(new SensorNotifyIcon(this, sensor, balloonTip, settings, unitManager)); 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: }