netmonitor/NetMonitorTray/Views/ViewTray.cs
2015-03-04 22:21:34 +00:00

127 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using NetMonitorTray.Properties;
using BlubbFish.Utils;
namespace NetMonitorTray.View
{
public class Tray : OwnView
{
private NotifyIcon trayi;
private Dictionary<string, Dictionary<string, Tuple<string, Bitmap, bool, bool>>> menuVpn;
private Dictionary<string, Dictionary<string, Tuple<string, Bitmap, bool, bool>>> menuNetwork;
private Icon[] iconlist = { new Icon(Resources.IconNothing, 40, 40), new Icon(Resources.IconWorking, 40, 40), new Icon(Resources.IconConnected, 40, 40) };
public Tray()
{
this.init();
this.Model = Models.Tray.Instance;
this.Model.setObserver(this);
}
public override void update()
{
this.menuVpn = this.Model.getMenuVpn();
this.menuNetwork = this.Model.getMenuNetwork();
this.trayi.ContextMenuStrip = this.genMenu();
this.trayi.Icon = (this.Model.connectedStatus ? this.iconlist[2] : this.iconlist[0]);
}
override protected void init()
{
this.trayi = new NotifyIcon();
this.trayi.Visible = true;
this.trayi.Icon = this.iconlist[0];
this.trayi.Text = "NetMonitorTray";
this.trayi.DoubleClick += Controller.Tray.Click_Tray;
this.trayi.BalloonTipClicked += Controller.Tray.Click_Ballon;
}
private ContextMenuStrip genMenu()
{
ContextMenuStrip menu = new ContextMenuStrip();
ToolStripMenuItem m1 = new ToolStripMenuItem("Öffnen");
m1.Image = Resources.MenuImagesQuit;
m1.Click += Controller.Tray.Click_Open;
m1.Name = "Open";
menu.Items.Add(m1);
menu.Items.Add(new ToolStripSeparator());
foreach (String adapter in this.menuNetwork.Keys)
{
menu.Items.AddRange(this.genMenuIt(this.menuNetwork[adapter], Controller.Tray.Click_Network));
menu.Items.Add(new ToolStripSeparator());
}
foreach(String adapter in this.menuVpn.Keys) {
menu.Items.AddRange(this.genMenuIt(this.menuVpn[adapter], Controller.Tray.Click_Vpn));
menu.Items.Add(new ToolStripSeparator());
}
ToolStripMenuItem m2 = new ToolStripMenuItem("Beenden");
m2.Image = Resources.MenuImagesQuit;
m2.Click += Controller.Tray.Click_Quit;
m2.Name = "Quit";
menu.Items.Add(m2);
return menu;
}
private ToolStripMenuItem[] genMenuIt(Dictionary<string, Tuple<string, Bitmap, bool, bool>> dictionary, EventHandler ev)
{
List<ToolStripMenuItem> l = new List<ToolStripMenuItem>();
foreach (String key in dictionary.Keys)
{
ToolStripMenuItem m = new ToolStripMenuItem(dictionary[key].Item1);
if (dictionary[key].Item2 != null)
{
m.Image = dictionary[key].Item2;
}
m.Click += ev;
m.Name = key;
m.Enabled = dictionary[key].Item3;
if (dictionary[key].Item4)
{
m.Font = new System.Drawing.Font(m.Font, m.Font.Style | FontStyle.Bold);
}
l.Add(m);
}
return l.ToArray();
}
private void showBallonTooltip(string text, ToolTipIcon toolTipIcon, string title = "NetMonitor Tray")
{
this.trayi.BalloonTipIcon = toolTipIcon;
this.trayi.BalloonTipText = text;
this.trayi.BalloonTipTitle = title;
this.trayi.ShowBalloonTip(100);
}
override public void Dispose()
{
this.trayi.Visible = false;
Application.ExitThread();
}
internal void setVpn(bool run, string ip)
{
this.showBallonTooltip((run ? "OpenVpn erfolgreich gestartet.\n"+ip : "OpenVpn erfolgreich beendet\n"+ip), ToolTipIcon.Info);
this.Model.connectedStatus = run;
}
internal void showError(string text)
{
this.showBallonTooltip(text, ToolTipIcon.Error);
}
public Models.Tray Model { get; private set; }
internal void setWorkingIcon()
{
this.trayi.Icon = this.iconlist[1];
}
}
}