GUI code LOL

This commit is contained in:
Melon 2020-09-23 20:22:51 +01:00
parent cc507148fd
commit 3f57dc43c5

View File

@ -1,132 +1,173 @@
using System; using System;
using System.Net.NetworkInformation; using System.IO;
using System.Threading; using System.Net.NetworkInformation;
using Gtk; using System.Reflection;
using MelonVPNCore; using System.Threading;
using Gtk;
public partial class MainWindow : Window using MelonVPNCore;
public partial class MainWindow : Window
{ {
private Label statusLabel; private Label statusLabel;
private Button startBtn; private Button startBtn;
private Button stopBtn; private Button stopBtn;
private Button refreshBtn; private Button refreshBtn;
private Button viewClientsBtn; private ScrolledWindow clientsListScroller;
private TextView clientsListText;
private Thread startupCheckThread; private Thread startupCheckThread;
private ThreadWrapper wrapper; private ThreadWrapper wrapper;
private StatusIcon statusIcon;
public MainWindow() : base(WindowType.Toplevel) public static readonly string MelonIconImg = "MiniMelonVPNIcon.png";
{ public static readonly string MelonOnlineImg = "MiniMelonVPNOnline.png";
public MainWindow() : base(WindowType.Toplevel)
{
Build(); Build();
Title = "Melon VPN"; Title = "Melon VPN";
UpdateIcon(MelonIconImg);
SetSizeRequest(300, 300); SetSizeRequest(300, 300);
SetDefaultSize(300, 300); SetDefaultSize(300, 300);
SetPosition(WindowPosition.Center); SetPosition(WindowPosition.CenterAlways);
Resizable = false; Resizable = false;
TypeHint = Gdk.WindowTypeHint.Normal; TypeHint = Gdk.WindowTypeHint.Normal;
Present(); Present();
statusLabel = new Label statusLabel = new Label
{ {
Visible = true, Visible = true,
Text = "Loading..." Text = "Loading..."
}; };
fixed1.Put(statusLabel, 10, 10); fixed1.Put(statusLabel, 10, 10);
startBtn = new Button startBtn = new Button
{ {
Visible = true, Visible = true,
Label = "Start" Label = "Start"
}; };
startBtn.SetSizeRequest(80, 30); startBtn.SetSizeRequest(80, 30);
startBtn.Clicked += OnStartClicked; startBtn.Clicked += OnStartClicked;
fixed1.Put(startBtn, 10, 40); fixed1.Put(startBtn, 10, 40);
stopBtn = new Button stopBtn = new Button
{ {
Visible = true, Visible = true,
Label = "Stop" Label = "Stop"
}; };
stopBtn.SetSizeRequest(80, 30); stopBtn.SetSizeRequest(80, 30);
stopBtn.Clicked += OnStopClicked; stopBtn.Clicked += OnStopClicked;
fixed1.Put(stopBtn, 100, 40); fixed1.Put(stopBtn, 100, 40);
refreshBtn = new Button refreshBtn = new Button
{ {
Visible = true, Visible = true,
Label = "Refresh" Label = "Refresh"
}; };
refreshBtn.SetSizeRequest(80, 30); refreshBtn.SetSizeRequest(80, 30);
refreshBtn.Clicked += OnRefreshClicked; refreshBtn.Clicked += OnRefreshClicked;
fixed1.Put(refreshBtn, 190, 40); fixed1.Put(refreshBtn, 190, 40);
viewClientsBtn = new Button clientsListText = new TextView
{ {
Visible = true, Visible = true,
Label = "View Clients" Editable = false
}; };
viewClientsBtn.SetSizeRequest(100, 30);
viewClientsBtn.Clicked += OnViewClientsClicked;
fixed1.Put(viewClientsBtn, 10, 80);
GUISocketServer.Receive += delegate (object sender, ClientResponseState s) clientsListScroller = new ScrolledWindow
{
Visible = true
};
clientsListScroller.SetSizeRequest(280, 200);
clientsListScroller.Add(clientsListText);
fixed1.Put(clientsListScroller, 10, 80);
GUISocketServer.Receive += delegate (object sender, ClientResponseState s)
{ {
Console.WriteLine(s); Console.WriteLine(s);
Application.Invoke(delegate Application.Invoke(delegate
{ {
UpdateStatus(s); UpdateStatus(s);
}); });
}; };
GUISocketServer.ClientListUpdate += delegate (object sender, ConnectedClient[] clients) GUISocketServer.ClientListUpdate += delegate (object sender, ConnectedClient[] clients)
{ {
Console.WriteLine(clients.Length); Console.WriteLine(clients.Length);
foreach (ConnectedClient c in clients) string[][] a = new string[clients.Length][];
{ for (int i = 0; i < clients.Length; i++) a[i] = new string[] { clients[i].name, clients[i].ip };
Console.WriteLine(c.name + "--" + c.ip); clientsListText.Buffer.Text = GenerateTable(a);
} };
};
wrapper = new ThreadWrapper(GUISocketServer.StartServer); wrapper = new ThreadWrapper(GUISocketServer.StartServer);
startupCheckThread = new Thread(StartupCheckMethod) startupCheckThread = new Thread(StartupCheckMethod)
{ {
IsBackground = true IsBackground = true
}; };
startupCheckThread.Start(); startupCheckThread.Start();
statusIcon = new StatusIcon
{
Visible = true
};
} }
string GetVpnInternalIp() void UpdateIcon(string file)
{ {
if (NetworkInterface.GetIsNetworkAvailable()) DirectoryInfo ExeLocation = Directory.GetParent(Assembly.GetEntryAssembly().Location);
string IconPath = System.IO.Path.Combine(ExeLocation.FullName, file);
Console.WriteLine("Trying to update icon to: " + IconPath);
if (File.Exists(IconPath))
{ {
foreach (NetworkInterface f in NetworkInterface.GetAllNetworkInterfaces()) Icon = new Gdk.Pixbuf(IconPath);
{ if (statusIcon != null) statusIcon.Icon = new Gdk.Pixbuf(IconPath);
}
}
string GenerateTable(string[][] a)
{
if (a.Length == 0) return "";
int maxlength = a[0][0].Length;
foreach (string b in a[0])
{
if (b.Length > maxlength) maxlength = b.Length;
}
string[] c = new string[a.Length];
for (int i = 0; i < a.Length; i++) c[i] = a[i][0].PadRight(maxlength, ' ') + a[i][1];
return string.Join("\n", c);
}
string GetVpnInternalIp()
{
if (NetworkInterface.GetIsNetworkAvailable())
{
foreach (NetworkInterface f in NetworkInterface.GetAllNetworkInterfaces())
{
IPInterfaceProperties p = f.GetIPProperties(); IPInterfaceProperties p = f.GetIPProperties();
IPAddressInformationCollection addressesColl = p.AnycastAddresses; IPAddressInformationCollection addressesColl = p.AnycastAddresses;
foreach (IPAddressInformation ip in addressesColl) foreach (IPAddressInformation ip in addressesColl)
{ {
if (ip.Address.ToString().StartsWith("10.137.248.", StringComparison.CurrentCulture)) if (ip.Address.ToString().StartsWith("10.137.248.", StringComparison.CurrentCulture))
{ {
return ip.Address.ToString(); return ip.Address.ToString();
} }
} }
} }
} }
return ""; return "";
} }
void StartupCheckMethod() void StartupCheckMethod()
{ {
ClientResponseState s = Client.SendDataMessage(DataMessage.Status); ClientResponseState s = Client.SendDataMessage(DataMessage.Status);
Console.WriteLine(s); Console.WriteLine(s);
Application.Invoke(delegate Application.Invoke(delegate
{ {
UpdateStatus(s); UpdateStatus(s);
}); });
} }
void Refresh() void Refresh()
{ {
ClientResponseState s = Client.SendDataMessage(DataMessage.Status); ClientResponseState s = Client.SendDataMessage(DataMessage.Status);
switch (s) switch (s)
{ {
@ -143,70 +184,73 @@ public partial class MainWindow : Window
case ClientResponseState.Offline: case ClientResponseState.Offline:
UpdateStatus(s); UpdateStatus(s);
break; break;
} }
} }
void UpdateStatus(ClientResponseState s) void UpdateStatus(ClientResponseState s)
{ {
switch (s) switch (s)
{ {
case ClientResponseState.Error: case ClientResponseState.Error:
statusLabel.Text = "Client Error"; statusLabel.Text = "Client Error";
break; UpdateIcon(MelonIconImg);
break;
case ClientResponseState.ServerError: case ClientResponseState.ServerError:
statusLabel.Text = "Server Error"; statusLabel.Text = "Server Error";
UpdateIcon(MelonIconImg);
break; break;
case ClientResponseState.Blank: case ClientResponseState.Blank:
statusLabel.Text = "No reply"; statusLabel.Text = "No reply";
UpdateIcon(MelonIconImg);
break; break;
case ClientResponseState.Online: case ClientResponseState.Online:
statusLabel.Text = "Online - " + GetVpnInternalIp(); statusLabel.Text = "Online - " + GetVpnInternalIp();
UpdateIcon(MelonOnlineImg);
break; break;
case ClientResponseState.Offline: case ClientResponseState.Offline:
statusLabel.Text = "Offline"; statusLabel.Text = "Offline";
break; UpdateIcon(MelonIconImg);
default:
statusLabel.Text = "Unknown";
break; break;
} }
} }
protected void OnDeleteEvent(object sender, DeleteEventArgs a) protected void OnDeleteEvent(object sender, DeleteEventArgs a)
{ {
if (wrapper != null) if (wrapper != null)
{ {
wrapper.Kill(); wrapper.Kill();
wrapper.Join(); wrapper.Join();
} }
Application.Quit(); if (statusIcon != null) statusIcon.Dispose();
a.RetVal = true; Application.Quit();
} a.RetVal = true;
}
void OnStartClicked(object sender, EventArgs e)
void OnStartClicked(object sender, EventArgs e)
{ {
ClientResponseState s = Client.SendDataMessage(DataMessage.Start); ClientResponseState s = Client.SendDataMessage(DataMessage.Start);
switch (s) switch (s)
{ {
case ClientResponseState.Error: case ClientResponseState.Error:
case ClientResponseState.ServerError: case ClientResponseState.ServerError:
case ClientResponseState.Offline: case ClientResponseState.Offline:
UpdateStatus(s); UpdateStatus(s);
MessageDialog md = new MessageDialog(this, MessageDialog md = new MessageDialog(this,
DialogFlags.DestroyWithParent, MessageType.Error, DialogFlags.DestroyWithParent, MessageType.Error,
ButtonsType.Ok, "Error starting VPN. Is the daemon running?"); ButtonsType.Ok, "Error starting VPN. Is the daemon running?");
md.Run(); md.Run();
md.Destroy(); md.Destroy();
break; break;
} }
} }
void OnStopClicked(object sender, EventArgs e) void OnStopClicked(object sender, EventArgs e)
{ {
ClientResponseState s = Client.SendDataMessage(DataMessage.Stop); ClientResponseState s = Client.SendDataMessage(DataMessage.Stop);
switch (s) switch (s)
{ {
case ClientResponseState.Error: case ClientResponseState.Error:
case ClientResponseState.ServerError: case ClientResponseState.ServerError:
case ClientResponseState.Online: case ClientResponseState.Online:
UpdateStatus(s); UpdateStatus(s);
MessageDialog md = new MessageDialog(this, MessageDialog md = new MessageDialog(this,
@ -214,8 +258,8 @@ public partial class MainWindow : Window
ButtonsType.Ok, "Error stopping VPN. Is the daemon running?"); ButtonsType.Ok, "Error stopping VPN. Is the daemon running?");
md.Run(); md.Run();
md.Destroy(); md.Destroy();
break; break;
} }
} }
void OnRefreshClicked(object sender, EventArgs e) void OnRefreshClicked(object sender, EventArgs e)
@ -223,8 +267,8 @@ public partial class MainWindow : Window
Refresh(); Refresh();
} }
void OnViewClientsClicked(object sender, EventArgs e) void OnViewClientsClicked(object sender, EventArgs e)
{ {
Refresh(); Refresh();
} }
} }