GUI/SystemTray.cs
author moel.mich
Sun, 27 May 2012 14:23:31 +0000
changeset 344 3145aadca3d2
parent 303 f6db7258890e
child 362 1dfe9dac1651
permissions -rw-r--r--
Changed the license to the Mozilla Public License 2.0 and update the licensing information.
     1 /*
     2  
     3   This Source Code Form is subject to the terms of the Mozilla Public
     4   License, v. 2.0. If a copy of the MPL was not distributed with this
     5   file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7   Copyright (C) 2009-2011 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System;
    12 using System.Collections.Generic;
    13 using System.Drawing;
    14 using System.Text;
    15 using System.Windows.Forms;
    16 using OpenHardwareMonitor.Hardware;
    17 using OpenHardwareMonitor.Utilities;
    18 
    19 namespace OpenHardwareMonitor.GUI {
    20   public class SystemTray : IDisposable {
    21     private IComputer computer;
    22     private PersistentSettings settings;
    23     private List<SensorNotifyIcon> list = new List<SensorNotifyIcon>();
    24     private bool mainIconEnabled = false;
    25     private NotifyIcon mainIcon;
    26 
    27     public SystemTray(IComputer computer, PersistentSettings settings) {
    28       this.computer = computer;
    29       this.settings = settings;
    30       computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
    31       computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
    32 
    33       this.mainIcon = new NotifyIcon();
    34 
    35       ContextMenu contextMenu = new ContextMenu();
    36       MenuItem hideShowItem = new MenuItem("Hide/Show");
    37       hideShowItem.Click += delegate(object obj, EventArgs args) {
    38         SendHideShowCommand();
    39       };
    40       contextMenu.MenuItems.Add(hideShowItem);
    41       contextMenu.MenuItems.Add(new MenuItem("-"));      
    42       MenuItem exitItem = new MenuItem("Exit");
    43       exitItem.Click += delegate(object obj, EventArgs args) {
    44         SendExitCommand();
    45       };
    46       contextMenu.MenuItems.Add(exitItem);
    47       this.mainIcon.ContextMenu = contextMenu;
    48       this.mainIcon.DoubleClick += delegate(object obj, EventArgs args) {
    49         SendHideShowCommand();
    50       };
    51       this.mainIcon.Icon = EmbeddedResources.GetIcon("smallicon.ico");
    52       this.mainIcon.Text = "Open Hardware Monitor";
    53     }
    54 
    55     private void HardwareRemoved(IHardware hardware) {
    56       hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
    57       hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
    58       foreach (ISensor sensor in hardware.Sensors) 
    59         SensorRemoved(sensor);
    60       foreach (IHardware subHardware in hardware.SubHardware)
    61         HardwareRemoved(subHardware);
    62     }
    63 
    64     private void HardwareAdded(IHardware hardware) {
    65       foreach (ISensor sensor in hardware.Sensors)
    66         SensorAdded(sensor);
    67       hardware.SensorAdded += new SensorEventHandler(SensorAdded);
    68       hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
    69       foreach (IHardware subHardware in hardware.SubHardware)
    70         HardwareAdded(subHardware);
    71     }
    72 
    73     private void SensorAdded(ISensor sensor) {
    74       if (settings.GetValue(new Identifier(sensor.Identifier, 
    75         "tray").ToString(), false)) 
    76         Add(sensor, false);   
    77     }
    78 
    79     private void SensorRemoved(ISensor sensor) {
    80       if (Contains(sensor)) 
    81         Remove(sensor, false);
    82     }
    83 
    84     public void Dispose() {
    85       foreach (SensorNotifyIcon icon in list)
    86         icon.Dispose();
    87       mainIcon.Dispose();
    88     }
    89 
    90     public void Redraw() {
    91       foreach (SensorNotifyIcon icon in list)
    92         icon.Update();
    93     }
    94 
    95     public bool Contains(ISensor sensor) {
    96       foreach (SensorNotifyIcon icon in list)
    97         if (icon.Sensor == sensor)
    98           return true;
    99       return false;
   100     }
   101 
   102     public void Add(ISensor sensor, bool balloonTip) {
   103       if (Contains(sensor)) {
   104         return;
   105       } else {        
   106         list.Add(new SensorNotifyIcon(this, sensor, balloonTip, settings));
   107         UpdateMainIconVisibilty();
   108         settings.SetValue(new Identifier(sensor.Identifier, "tray").ToString(), true);
   109       }
   110     }
   111 
   112     public void Remove(ISensor sensor) {
   113       Remove(sensor, true);
   114     }
   115 
   116     private void Remove(ISensor sensor, bool deleteConfig) {
   117       if (deleteConfig) {
   118         settings.Remove(
   119           new Identifier(sensor.Identifier, "tray").ToString());
   120         settings.Remove(
   121           new Identifier(sensor.Identifier, "traycolor").ToString());
   122       }
   123       SensorNotifyIcon instance = null;
   124       foreach (SensorNotifyIcon icon in list)
   125         if (icon.Sensor == sensor)
   126           instance = icon;
   127       if (instance != null) {
   128         list.Remove(instance);
   129         UpdateMainIconVisibilty();
   130         instance.Dispose();        
   131       }
   132     }
   133 
   134     public event EventHandler HideShowCommand;
   135 
   136     public void SendHideShowCommand() {
   137       if (HideShowCommand != null)
   138         HideShowCommand(this, null);
   139     }
   140 
   141     public event EventHandler ExitCommand;
   142 
   143     public void SendExitCommand() {
   144       if (ExitCommand != null)
   145         ExitCommand(this, null);
   146     }
   147 
   148     private void UpdateMainIconVisibilty() {
   149       if (mainIconEnabled) {
   150         mainIcon.Visible = list.Count == 0;
   151       } else {
   152         mainIcon.Visible = false;
   153       }
   154     }
   155 
   156     public bool IsMainIconEnabled {
   157       get { return mainIconEnabled; }
   158       set {
   159         if (mainIconEnabled != value) {
   160           mainIconEnabled = value;
   161           UpdateMainIconVisibilty();
   162         }
   163       }
   164     }
   165   }
   166 }