2016-11-08 22:54:55 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2016-11-10 22:17:57 +00:00
|
|
|
|
using System.Collections.ObjectModel;
|
2016-11-17 22:13:05 +00:00
|
|
|
|
using System.ComponentModel;
|
2016-11-08 22:54:55 +00:00
|
|
|
|
using System.Linq;
|
2016-11-17 22:13:05 +00:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2016-11-08 22:54:55 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
using System.Windows.Data;
|
|
|
|
|
using System.Windows.Documents;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
using System.Windows.Shapes;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Timers;
|
|
|
|
|
using System.Windows.Threading;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
namespace WinUI
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Interaction logic for ToolbarItem.xaml
|
|
|
|
|
/// </summary>
|
2016-11-17 22:13:05 +00:00
|
|
|
|
public partial class ToolbarItem : Window, INotifyPropertyChanged
|
2016-11-08 22:54:55 +00:00
|
|
|
|
{
|
2016-11-09 00:31:07 +00:00
|
|
|
|
private APIHandler handler = APIHandler.Instance;
|
|
|
|
|
|
2016-11-22 19:53:06 +00:00
|
|
|
|
private Point netListLocation = new Point(0, 0);
|
|
|
|
|
private Point joinNetLocation = new Point(0, 0);
|
|
|
|
|
private Point aboutViewLocation = new Point(0, 0);
|
|
|
|
|
private Point prefsViewLocation = new Point(0, 0);
|
|
|
|
|
|
2016-11-21 20:54:27 +00:00
|
|
|
|
private NetworkListView netListView = new NetworkListView();
|
2016-11-18 19:33:46 +00:00
|
|
|
|
private JoinNetworkView joinNetView = null;
|
2016-11-18 19:42:15 +00:00
|
|
|
|
private AboutView aboutView = null;
|
|
|
|
|
private PreferencesView prefsView = null;
|
2016-11-10 22:17:57 +00:00
|
|
|
|
|
2016-11-14 22:56:36 +00:00
|
|
|
|
private NetworkMonitor mon = NetworkMonitor.Instance;
|
|
|
|
|
|
2016-11-17 22:13:05 +00:00
|
|
|
|
private ObservableCollection<MenuItem> _networkCollection = new ObservableCollection<MenuItem>();
|
2016-11-10 22:17:57 +00:00
|
|
|
|
|
2016-11-17 22:13:05 +00:00
|
|
|
|
public ObservableCollection<MenuItem> NetworkCollection
|
2016-11-10 22:17:57 +00:00
|
|
|
|
{
|
|
|
|
|
get { return _networkCollection; }
|
|
|
|
|
set { _networkCollection = value; }
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-18 22:52:04 +00:00
|
|
|
|
private string nodeId;
|
|
|
|
|
|
2016-11-08 22:54:55 +00:00
|
|
|
|
public ToolbarItem()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2016-11-10 22:17:57 +00:00
|
|
|
|
|
2016-11-14 22:56:36 +00:00
|
|
|
|
mon.SubscribeNetworkUpdates(updateNetworks);
|
|
|
|
|
mon.SubscribeStatusUpdates(updateStatus);
|
|
|
|
|
}
|
2016-11-10 22:17:57 +00:00
|
|
|
|
|
2016-11-14 22:56:36 +00:00
|
|
|
|
~ToolbarItem()
|
|
|
|
|
{
|
|
|
|
|
mon.UnsubscribeNetworkUpdates(updateNetworks);
|
|
|
|
|
mon.UnsubscribeStatusUpdates(updateStatus);
|
2016-11-10 22:17:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-17 22:13:05 +00:00
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
|
|
|
|
|
|
protected void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
|
|
|
|
|
{
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-10 22:17:57 +00:00
|
|
|
|
private void updateNetworks(List<ZeroTierNetwork> networks)
|
|
|
|
|
{
|
|
|
|
|
if (networks != null)
|
|
|
|
|
{
|
|
|
|
|
this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
|
|
|
|
|
{
|
2016-11-17 22:13:05 +00:00
|
|
|
|
NetworkCollection.Clear();
|
2016-11-10 22:17:57 +00:00
|
|
|
|
foreach (ZeroTierNetwork n in networks)
|
|
|
|
|
{
|
2016-11-17 22:13:05 +00:00
|
|
|
|
MenuItem item = new MenuItem();
|
|
|
|
|
item.Header = n.Title;
|
|
|
|
|
item.DataContext = n;
|
|
|
|
|
item.IsChecked = n.IsConnected;
|
|
|
|
|
item.Click += ToolbarItem_NetworkClicked;
|
2016-11-10 22:17:57 +00:00
|
|
|
|
|
2016-11-17 22:13:05 +00:00
|
|
|
|
NetworkCollection.Add(item);
|
|
|
|
|
}
|
2016-11-10 22:17:57 +00:00
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateStatus(ZeroTierStatus status)
|
|
|
|
|
{
|
|
|
|
|
if (status != null)
|
|
|
|
|
{
|
|
|
|
|
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
|
|
|
|
|
{
|
|
|
|
|
nodeIdMenuItem.Header = "Node ID: " + status.Address;
|
|
|
|
|
nodeIdMenuItem.IsEnabled = true;
|
2016-11-18 22:52:04 +00:00
|
|
|
|
nodeId = status.Address;
|
2016-11-10 22:17:57 +00:00
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-09 00:31:07 +00:00
|
|
|
|
private void ToolbarItem_NodeIDClicked(object sender, System.Windows.RoutedEventArgs e)
|
|
|
|
|
{
|
2016-11-18 22:52:04 +00:00
|
|
|
|
Clipboard.SetText(nodeId);
|
2016-11-09 00:31:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ToolbarItem_ShowNetworksClicked(object sender, System.Windows.RoutedEventArgs e)
|
|
|
|
|
{
|
2016-11-10 22:17:57 +00:00
|
|
|
|
if (netListView == null)
|
2016-11-09 00:31:07 +00:00
|
|
|
|
{
|
2016-11-10 22:17:57 +00:00
|
|
|
|
netListView = new WinUI.NetworkListView();
|
|
|
|
|
netListView.Closed += ShowNetworksClosed;
|
2016-11-09 00:31:07 +00:00
|
|
|
|
}
|
2016-11-22 19:53:06 +00:00
|
|
|
|
|
|
|
|
|
bool netListNeedsMoving = true;
|
|
|
|
|
if (netListLocation.X > 0 && netListLocation.Y > 0)
|
|
|
|
|
{
|
|
|
|
|
netListView.Left = netListLocation.X;
|
|
|
|
|
netListView.Top = netListLocation.Y;
|
|
|
|
|
netListNeedsMoving = false;
|
|
|
|
|
}
|
2016-11-21 23:31:32 +00:00
|
|
|
|
|
2016-11-21 20:54:27 +00:00
|
|
|
|
netListView.Show();
|
2016-11-21 23:31:32 +00:00
|
|
|
|
|
2016-11-22 19:53:06 +00:00
|
|
|
|
if (netListNeedsMoving)
|
|
|
|
|
{
|
|
|
|
|
setWindowPosition(netListView);
|
|
|
|
|
netListLocation.X = netListView.Left;
|
|
|
|
|
netListLocation.Y = netListView.Top;
|
|
|
|
|
}
|
2016-11-09 00:31:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ShowNetworksClosed(object sender, System.EventArgs e)
|
|
|
|
|
{
|
2016-11-10 22:17:57 +00:00
|
|
|
|
netListView = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ToolbarItem_JoinNetworkClicked(object sender, System.EventArgs e)
|
|
|
|
|
{
|
2016-11-18 19:33:46 +00:00
|
|
|
|
if (joinNetView == null)
|
|
|
|
|
{
|
|
|
|
|
joinNetView = new JoinNetworkView();
|
|
|
|
|
joinNetView.Closed += JoinNetworkClosed;
|
2016-11-22 19:53:06 +00:00
|
|
|
|
|
|
|
|
|
bool needsMove = true;
|
|
|
|
|
if (joinNetLocation.X > 0 && joinNetLocation.Y > 0)
|
|
|
|
|
{
|
|
|
|
|
joinNetView.Left = joinNetLocation.X;
|
|
|
|
|
joinNetView.Top = joinNetLocation.Y;
|
|
|
|
|
needsMove = false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-18 19:33:46 +00:00
|
|
|
|
joinNetView.Show();
|
2016-11-21 23:31:32 +00:00
|
|
|
|
|
2016-11-22 19:53:06 +00:00
|
|
|
|
if (needsMove)
|
|
|
|
|
{
|
|
|
|
|
setWindowPosition(joinNetView);
|
|
|
|
|
joinNetLocation.X = joinNetView.Left;
|
|
|
|
|
joinNetLocation.Y = joinNetView.Top;
|
|
|
|
|
}
|
2016-11-18 19:33:46 +00:00
|
|
|
|
}
|
2016-11-10 22:17:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void JoinNetworkClosed(object sender, System.EventArgs e)
|
|
|
|
|
{
|
2016-11-18 19:33:46 +00:00
|
|
|
|
joinNetView = null;
|
2016-11-09 00:31:07 +00:00
|
|
|
|
}
|
2016-11-17 22:13:05 +00:00
|
|
|
|
|
2016-11-18 19:42:15 +00:00
|
|
|
|
private void ToolbarItem_AboutClicked(object sender, System.EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (aboutView == null)
|
|
|
|
|
{
|
|
|
|
|
aboutView = new AboutView();
|
|
|
|
|
aboutView.Closed += AboutClosed;
|
2016-11-22 19:53:06 +00:00
|
|
|
|
|
|
|
|
|
bool needsMove = true;
|
|
|
|
|
if (aboutViewLocation.X > 0 && aboutViewLocation.Y > 0)
|
|
|
|
|
{
|
|
|
|
|
aboutView.Left = aboutViewLocation.X;
|
|
|
|
|
aboutView.Top = aboutViewLocation.Y;
|
|
|
|
|
needsMove = false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-18 19:42:15 +00:00
|
|
|
|
aboutView.Show();
|
2016-11-22 19:53:06 +00:00
|
|
|
|
|
|
|
|
|
if (needsMove)
|
|
|
|
|
{
|
|
|
|
|
setWindowPosition(aboutView);
|
|
|
|
|
aboutViewLocation.X = aboutView.Left;
|
|
|
|
|
aboutViewLocation.Y = aboutView.Top;
|
|
|
|
|
}
|
2016-11-18 19:42:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AboutClosed(object sender, System.EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
aboutView = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ToolbarItem_PreferencesClicked(object sender, System.EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (prefsView == null)
|
|
|
|
|
{
|
|
|
|
|
prefsView = new PreferencesView();
|
|
|
|
|
prefsView.Closed += PreferencesClosed;
|
2016-11-22 19:53:06 +00:00
|
|
|
|
|
|
|
|
|
bool needsMove = true;
|
|
|
|
|
if (prefsViewLocation.X > 0 && prefsViewLocation.Y > 0)
|
|
|
|
|
{
|
|
|
|
|
prefsView.Left = prefsViewLocation.X;
|
|
|
|
|
prefsView.Top = prefsViewLocation.Y;
|
|
|
|
|
needsMove = false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-18 19:42:15 +00:00
|
|
|
|
prefsView.Show();
|
2016-11-21 23:31:32 +00:00
|
|
|
|
|
2016-11-22 19:53:06 +00:00
|
|
|
|
if (needsMove)
|
|
|
|
|
{
|
|
|
|
|
setWindowPosition(prefsView);
|
|
|
|
|
prefsViewLocation.X = prefsView.Left;
|
|
|
|
|
prefsViewLocation.Y = prefsView.Top;
|
|
|
|
|
}
|
2016-11-18 19:42:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PreferencesClosed(object sender, System.EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
prefsView = null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-18 20:09:08 +00:00
|
|
|
|
private void ToolbarItem_QuitClicked(object sender, System.EventArgs e)
|
|
|
|
|
{
|
2016-11-18 22:23:22 +00:00
|
|
|
|
NetworkMonitor.Instance.StopMonitor();
|
2016-11-18 20:09:08 +00:00
|
|
|
|
this.Close();
|
|
|
|
|
Application.Current.Shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-17 22:13:05 +00:00
|
|
|
|
private void ToolbarItem_NetworkClicked(object sender, System.Windows.RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if(sender.GetType() == typeof(MenuItem))
|
|
|
|
|
{
|
|
|
|
|
MenuItem item = e.Source as MenuItem;
|
|
|
|
|
if (item.DataContext != null)
|
|
|
|
|
{
|
|
|
|
|
ZeroTierNetwork network = item.DataContext as ZeroTierNetwork;
|
|
|
|
|
if (item.IsChecked)
|
|
|
|
|
{
|
|
|
|
|
APIHandler.Instance.LeaveNetwork(network.NetworkId);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
APIHandler.Instance.JoinNetwork(network.NetworkId, network.AllowManaged, network.AllowGlobal, network.AllowDefault);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-21 23:31:32 +00:00
|
|
|
|
|
|
|
|
|
private void setWindowPosition(Window w)
|
|
|
|
|
{
|
|
|
|
|
double width = w.ActualWidth;
|
|
|
|
|
double height = w.ActualHeight;
|
|
|
|
|
|
|
|
|
|
double screenHeight = SystemParameters.PrimaryScreenHeight;
|
|
|
|
|
double screenWidth = SystemParameters.PrimaryScreenWidth;
|
|
|
|
|
|
|
|
|
|
double top = screenHeight - height - 40;
|
|
|
|
|
double left = screenWidth - width - 20;
|
|
|
|
|
|
|
|
|
|
w.Top = top;
|
|
|
|
|
w.Left = left;
|
|
|
|
|
}
|
2016-11-08 22:54:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|