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