111 lines
4.0 KiB
C#
111 lines
4.0 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, Tuple<string, Bitmap, bool, bool>> menuOpenVpnService;
|
|||
|
private Dictionary<string, Tuple<string, Bitmap, bool, bool>> menuNetwork;
|
|||
|
|
|||
|
public Tray()
|
|||
|
{
|
|||
|
this.initIcon();
|
|||
|
Model.Tray.Instance.setObserver(this);
|
|||
|
}
|
|||
|
|
|||
|
public override void update()
|
|||
|
{
|
|||
|
this.menuOpenVpnService = Model.Tray.Instance.getMenuOpenVpnService();
|
|||
|
this.menuNetwork = Model.Tray.Instance.getMenuNetwork();
|
|||
|
this.trayi.ContextMenuStrip = this.genMenu();
|
|||
|
}
|
|||
|
|
|||
|
private void initIcon()
|
|||
|
{
|
|||
|
this.trayi = new NotifyIcon();
|
|||
|
this.trayi.Visible = true;
|
|||
|
this.trayi.Icon = new Icon(Resources.IconNothing, 40, 40);
|
|||
|
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());
|
|||
|
menu.Items.AddRange(this.genMenuIt(this.menuNetwork, Controller.Tray.Click_Network));
|
|||
|
menu.Items.Add(new ToolStripSeparator());
|
|||
|
menu.Items.AddRange(this.genMenuIt(this.menuOpenVpnService, Controller.Tray.Click_Service));
|
|||
|
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);
|
|||
|
}
|
|||
|
|
|||
|
internal void Dispose()
|
|||
|
{
|
|||
|
this.trayi.Visible = false;
|
|||
|
Application.ExitThread();
|
|||
|
}
|
|||
|
|
|||
|
internal void setService(bool run)
|
|||
|
{
|
|||
|
this.showBallonTooltip((run ? "Service erfolgreich gestartet." : "Service erfolgreich beendet"), ToolTipIcon.Info);
|
|||
|
Model.Tray.Instance.setService(run);
|
|||
|
}
|
|||
|
|
|||
|
internal void showError(string text)
|
|||
|
{
|
|||
|
this.showBallonTooltip(text, ToolTipIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|