netmonitor/NetMonitorTray/Controller/ControllerTray.cs

171 lines
5.5 KiB
C#
Raw Normal View History

2015-03-04 23:21:34 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NetMonitorUtils;
using BlubbFish.Utils;
2015-11-16 01:10:59 +01:00
using System.Threading;
using System.Runtime.CompilerServices;
2015-03-04 23:21:34 +01:00
namespace NetMonitorTray.Controller {
public class Tray : OwnController {
private static View.Tray tray;
private static OpenVpnSetter vpn;
private static NetworkSetter network;
private static Controller.Window window;
2015-11-16 01:10:59 +01:00
private static Thread trayMenuRefresh;
2015-03-04 23:21:34 +01:00
/// <summary>
/// Tray Controller
/// </summary>
public Tray() {
vpn = new OpenVpnSetter("vpn.ini", "config.ini");
network = new NetworkSetter("network.ini", "config.ini");
2015-11-16 01:10:59 +01:00
trayMenuRefresh = new Thread(menuRefresh);
2015-03-04 23:21:34 +01:00
}
/// <summary>
/// Init!
/// </summary>
override protected void init() {
tray = new View.Tray();
window = new Controller.Window();
tray.setWorkingIcon();
try {
2015-11-16 01:10:59 +01:00
updateMenu();
2015-03-04 23:21:34 +01:00
} catch(Exception e) {
tray.showError(e.Message);
tray.Model.connectedStatus = false;
}
2015-11-16 01:10:59 +01:00
trayMenuRefresh.Start();
}
private static void menuRefresh() {
while(true) {
Thread.Sleep(10 * 60 * 1000); //Wait 10 Minutes
updateMenu();
}
}
[MethodImpl(MethodImplOptions.Synchronized)]
private static void updateMenu() {
List<string> adapter = tray.Model.getAdapterList();
tray.Model.NetworkSelected = network.getNetwork(adapter);
List<string> vpns = tray.Model.getVpnList();
tray.Model.VpnSelected = vpn.getStatus(vpns);
checkWorkingIcon();
}
private static void checkWorkingIcon() {
bool w = false;
foreach(KeyValuePair<string, string> item in tray.Model.VpnSelected) {
if(item.Value == "connected") {
w = true;
}
}
tray.Model.connectedStatus = w;
2015-03-04 23:21:34 +01:00
}
/// <summary>
/// User klickt auf den Eintrag Beenden
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
internal static void Click_Quit(object sender, EventArgs e) {
2015-11-16 01:10:59 +01:00
trayMenuRefresh.Abort();
while(trayMenuRefresh.ThreadState != ThreadState.Aborted) { Thread.Sleep(100); }
2015-03-04 23:21:34 +01:00
tray.Dispose();
Application.Exit();
}
2015-11-16 01:10:59 +01:00
2015-03-04 23:21:34 +01:00
/// <summary>
/// User klick auf einen Eintrag Service Starten/Stoppen
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
internal static void Click_Vpn(object sender, EventArgs e) {
tray.setWorkingIcon();
string[] conf = ((ToolStripMenuItem)sender).Name.Split('|');
switch(tray.Model.VpnSelected[conf[1]]) {
case "disconnected":
case "unknown":
try {
vpn.setStart(conf[1]);
for(int i = 0; i < 10; i++) {
string status = vpn.getStatus(conf[1]);
if(status.StartsWith("[connected|")) {
tray.Model.VpnSelected[conf[1]] = "connected";
status = status.Substring(0, status.Length - 1).Substring(1);
string[] ip = status.Split('|');
tray.setVpn(true, "Verbunden am: " + ip[5] + "\nmit IP: " + ip[1] + "\nConfig: " + conf[1]);
break;
}
System.Threading.Thread.Sleep(3000);
}
} catch(Exception ex) {
tray.showError("Fehler beim starten von Openvpn mit " + conf[1] + ": " + ex.Message);
}
break;
case "connected":
try {
vpn.setStop(conf[1]);
for(int i = 0; i < 10; i++) {
string status = vpn.getStatus(conf[1]);
if(status == "[disconnected]") {
tray.Model.VpnSelected[conf[1]] = "disconnected";
tray.setVpn(false, "Config: " + conf[1]);
break;
}
System.Threading.Thread.Sleep(1000);
}
} catch(Exception ex) {
tray.showError("Fehler beim beenden von Openvpn mit " + conf + ": " + ex.Message);
}
break;
}
2015-11-16 01:10:59 +01:00
checkWorkingIcon();
2015-03-04 23:21:34 +01:00
}
2015-11-16 01:10:59 +01:00
2015-03-04 23:21:34 +01:00
/// <summary>
/// User klickt Doppelt auf das TrayIcon
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
internal static void Click_Tray(object sender, EventArgs e) {
Click_Open(sender, e);
}
2015-11-16 01:10:59 +01:00
2015-03-04 23:21:34 +01:00
/// <summary>
/// User klickt auf den Tray-Ballon
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
internal static void Click_Ballon(object sender, EventArgs e) {
2015-11-16 01:10:59 +01:00
Click_Open(sender, e);
2015-03-04 23:21:34 +01:00
}
/// <summary>
/// User klickt auf einen Eintrag Netzwerk
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
internal static void Click_Network(object sender, EventArgs e) {
tray.setWorkingIcon();
try {
string n = ((ToolStripMenuItem)sender).Name;
2015-11-16 01:10:59 +01:00
network.setNetwork(n.Split('|')[1]);
2015-03-04 23:21:34 +01:00
tray.Model.NetworkSelected[n.Split('|')[0]] = n.Split('|')[1];
} catch(Exception ex) {
tray.showError("Fehler beim setzen des Netzwerks: " + ex.Message);
}
2015-11-16 01:10:59 +01:00
checkWorkingIcon();
2015-03-04 23:21:34 +01:00
}
/// <summary>
/// User klickt auf den Eintrag Öffnen
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
internal static void Click_Open(object sender, EventArgs e) {
window.execute();
}
}
}