Fixed some problems when compiling in Mono and running on Linux.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 The contents of this file are subject to the Mozilla Public License Version
6 1.1 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.mozilla.org/MPL/
11 Software distributed under the License is distributed on an "AS IS" basis,
12 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 for the specific language governing rights and limitations under the License.
15 The Original Code is the Open Hardware Monitor code.
17 The Initial Developer of the Original Code is
18 Michael Möller <m.moeller@gmx.ch>.
19 Portions created by the Initial Developer are Copyright (C) 2009-2010
20 the Initial Developer. All Rights Reserved.
24 Alternatively, the contents of this file may be used under the terms of
25 either the GNU General Public License Version 2 or later (the "GPL"), or
26 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 in which case the provisions of the GPL or the LGPL are applicable instead
28 of those above. If you wish to allow use of your version of this file only
29 under the terms of either the GPL or the LGPL, and not to allow others to
30 use your version of this file under the terms of the MPL, indicate your
31 decision by deleting the provisions above and replace them with the notice
32 and other provisions required by the GPL or the LGPL. If you do not delete
33 the provisions above, a recipient may use your version of this file under
34 the terms of any one of the MPL, the GPL or the LGPL.
39 using System.Collections.Generic;
42 using System.Windows.Forms;
43 using OpenHardwareMonitor.Hardware;
44 using OpenHardwareMonitor.Utilities;
46 namespace OpenHardwareMonitor.GUI {
47 public class SystemTray : IDisposable {
48 private IComputer computer;
49 private PersistentSettings settings;
50 private List<SensorNotifyIcon> list = new List<SensorNotifyIcon>();
51 private bool mainIconEnabled = false;
52 private NotifyIcon mainIcon;
54 public SystemTray(IComputer computer, PersistentSettings settings) {
55 this.computer = computer;
56 this.settings = settings;
57 computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
58 computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
60 this.mainIcon = new NotifyIcon();
62 ContextMenu contextMenu = new ContextMenu();
63 MenuItem hideShowItem = new MenuItem("Hide/Show");
64 hideShowItem.Click += delegate(object obj, EventArgs args) {
65 SendHideShowCommand();
67 contextMenu.MenuItems.Add(hideShowItem);
68 contextMenu.MenuItems.Add(new MenuItem("-"));
69 MenuItem exitItem = new MenuItem("Exit");
70 exitItem.Click += delegate(object obj, EventArgs args) {
73 contextMenu.MenuItems.Add(exitItem);
74 this.mainIcon.ContextMenu = contextMenu;
75 this.mainIcon.DoubleClick += delegate(object obj, EventArgs args) {
76 SendHideShowCommand();
78 this.mainIcon.Icon = EmbeddedResources.GetIcon("smallicon.ico");
81 private void HardwareRemoved(IHardware hardware) {
82 hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
83 hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
84 foreach (ISensor sensor in hardware.Sensors)
85 SensorRemoved(sensor);
86 foreach (IHardware subHardware in hardware.SubHardware)
87 HardwareRemoved(subHardware);
90 private void HardwareAdded(IHardware hardware) {
91 foreach (ISensor sensor in hardware.Sensors)
93 hardware.SensorAdded += new SensorEventHandler(SensorAdded);
94 hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
95 foreach (IHardware subHardware in hardware.SubHardware)
96 HardwareAdded(subHardware);
99 private void SensorAdded(ISensor sensor) {
100 if (settings.GetValue(new Identifier(sensor.Identifier,
101 "tray").ToString(), false))
105 private void SensorRemoved(ISensor sensor) {
106 if (Contains(sensor))
107 Remove(sensor, false);
110 public void Dispose() {
111 foreach (SensorNotifyIcon icon in list)
116 public void Redraw() {
117 foreach (SensorNotifyIcon icon in list)
121 public bool Contains(ISensor sensor) {
122 foreach (SensorNotifyIcon icon in list)
123 if (icon.Sensor == sensor)
128 public void Add(ISensor sensor, bool balloonTip) {
129 if (Contains(sensor)) {
132 list.Add(new SensorNotifyIcon(this, sensor, balloonTip, settings));
133 UpdateMainIconVisibilty();
134 settings.SetValue(new Identifier(sensor.Identifier, "tray").ToString(), true);
138 public void Remove(ISensor sensor) {
139 Remove(sensor, true);
142 private void Remove(ISensor sensor, bool deleteConfig) {
145 new Identifier(sensor.Identifier, "tray").ToString());
147 new Identifier(sensor.Identifier, "traycolor").ToString());
149 SensorNotifyIcon instance = null;
150 foreach (SensorNotifyIcon icon in list)
151 if (icon.Sensor == sensor)
153 if (instance != null) {
154 list.Remove(instance);
155 UpdateMainIconVisibilty();
160 public event EventHandler HideShowCommand;
162 public void SendHideShowCommand() {
163 if (HideShowCommand != null)
164 HideShowCommand(this, null);
167 public event EventHandler ExitCommand;
169 public void SendExitCommand() {
170 if (ExitCommand != null)
171 ExitCommand(this, null);
174 private void UpdateMainIconVisibilty() {
175 if (mainIconEnabled) {
176 mainIcon.Visible = list.Count == 0;
178 mainIcon.Visible = false;
182 public bool IsMainIconEnabled {
183 get { return mainIconEnabled; }
185 if (mainIconEnabled != value) {
186 mainIconEnabled = value;
187 UpdateMainIconVisibilty();