handle removing of networks we are no longer connected to from the UI

This commit is contained in:
Grant Limberg 2016-11-16 15:50:56 -08:00
parent 78a8ceda0e
commit 3c248ec61a
2 changed files with 100 additions and 23 deletions

View File

@ -21,7 +21,7 @@ namespace WinUI
public partial class NetworkInfoView : UserControl public partial class NetworkInfoView : UserControl
{ {
private APIHandler handler; private APIHandler handler;
private ZeroTierNetwork network; public ZeroTierNetwork network;
public NetworkInfoView(APIHandler handler, ZeroTierNetwork network) public NetworkInfoView(APIHandler handler, ZeroTierNetwork network)
{ {
@ -31,19 +31,41 @@ namespace WinUI
this.network = network; this.network = network;
UpdateNetworkData(); UpdateNetworkData();
allowDefault.Checked += AllowDefault_CheckStateChanged;
allowDefault.Unchecked += AllowDefault_CheckStateChanged;
allowGlobal.Checked += AllowGlobal_CheckStateChanged;
allowGlobal.Unchecked += AllowGlobal_CheckStateChanged;
allowManaged.Checked += AllowManaged_CheckStateChanged;
allowManaged.Unchecked += AllowManaged_CheckStateChanged;
} }
private void UpdateNetworkData() private void UpdateNetworkData()
{ {
this.networkId.Text = network.NetworkId;
this.networkName.Text = network.NetworkName; if (this.networkId.Text != network.NetworkId)
this.networkStatus.Text = network.NetworkStatus; this.networkId.Text = network.NetworkId;
this.networkType.Text = network.NetworkType;
this.macAddress.Text = network.MacAddress; if (this.networkName.Text != network.NetworkName)
this.mtu.Text = network.MTU.ToString(); this.networkName.Text = network.NetworkName;
if (this.networkStatus.Text != network.NetworkStatus)
this.networkStatus.Text = network.NetworkStatus;
if (this.networkType.Text != network.NetworkType)
this.networkType.Text = network.NetworkType;
if (this.macAddress.Text != network.MacAddress)
this.macAddress.Text = network.MacAddress;
if (this.mtu.Text != network.MTU.ToString())
this.mtu.Text = network.MTU.ToString();
this.broadcastEnabled.Text = (network.BroadcastEnabled ? "ENABLED" : "DISABLED"); this.broadcastEnabled.Text = (network.BroadcastEnabled ? "ENABLED" : "DISABLED");
this.bridgingEnabled.Text = (network.Bridge ? "ENABLED" : "DISABLED"); this.bridgingEnabled.Text = (network.Bridge ? "ENABLED" : "DISABLED");
this.deviceName.Text = network.DeviceName;
if (this.deviceName.Text != network.DeviceName)
this.deviceName.Text = network.DeviceName;
string iplist = ""; string iplist = "";
for (int i = 0; i < network.AssignedAddresses.Length; ++i) for (int i = 0; i < network.AssignedAddresses.Length; ++i)
@ -53,19 +75,12 @@ namespace WinUI
iplist += "\n"; iplist += "\n";
} }
this.managedIps.Text = iplist; if (this.managedIps.Text != iplist)
this.managedIps.Text = iplist;
this.allowDefault.IsChecked = network.AllowDefault; this.allowDefault.IsChecked = network.AllowDefault;
this.allowGlobal.IsChecked = network.AllowGlobal; this.allowGlobal.IsChecked = network.AllowGlobal;
this.allowManaged.IsChecked = network.AllowManaged; this.allowManaged.IsChecked = network.AllowManaged;
allowDefault.Checked += AllowDefault_CheckStateChanged;
allowDefault.Unchecked += AllowDefault_CheckStateChanged;
allowGlobal.Checked += AllowGlobal_CheckStateChanged;
allowGlobal.Unchecked += AllowGlobal_CheckStateChanged;
allowManaged.Checked += AllowManaged_CheckStateChanged;
allowManaged.Unchecked += AllowManaged_CheckStateChanged;
} }
public bool HasNetwork(ZeroTierNetwork network) public bool HasNetwork(ZeroTierNetwork network)
@ -76,6 +91,13 @@ namespace WinUI
return false; return false;
} }
public void SetNetworkInfo(ZeroTierNetwork network)
{
this.network = network;
UpdateNetworkData();
}
private void leaveButton_Click(object sender, RoutedEventArgs e) private void leaveButton_Click(object sender, RoutedEventArgs e)
{ {
handler.LeaveNetwork(network.NetworkId); handler.LeaveNetwork(network.NetworkId);

View File

@ -34,19 +34,74 @@ namespace WinUI
public void setNetworks(List<ZeroTierNetwork> networks) public void setNetworks(List<ZeroTierNetwork> networks)
{ {
this.wrapPanel.Children.Clear();
if (networks == null) if (networks == null)
{ {
this.wrapPanel.Children.Clear();
return; return;
} }
for (int i = 0; i < networks.Count; ++i) foreach (ZeroTierNetwork network in networks)
{ {
this.wrapPanel.Children.Add( NetworkInfoView view = ChildWithNetwork(network);
new NetworkInfoView( if (view != null)
handler, {
networks.ElementAt<ZeroTierNetwork>(i))); view.SetNetworkInfo(network);
}
else
{
wrapPanel.Children.Add(
new NetworkInfoView(
handler,
network));
}
} }
// remove networks we're no longer joined to.
List<ZeroTierNetwork> tmpList = GetNetworksFromChildren();
foreach (ZeroTierNetwork n in networks)
{
if (tmpList.Contains(n))
{
tmpList.Remove(n);
}
}
foreach (ZeroTierNetwork n in tmpList)
{
NetworkInfoView view = ChildWithNetwork(n);
if (view != null)
{
wrapPanel.Children.Remove(view);
}
}
}
private NetworkInfoView ChildWithNetwork(ZeroTierNetwork network)
{
List<NetworkInfoView> list = wrapPanel.Children.OfType<NetworkInfoView>().ToList();
foreach (NetworkInfoView view in list)
{
if (view.HasNetwork(network))
{
return view;
}
}
return null;
}
private List<ZeroTierNetwork> GetNetworksFromChildren()
{
List<ZeroTierNetwork> networks = new List<ZeroTierNetwork>(wrapPanel.Children.Count);
List<NetworkInfoView> list = wrapPanel.Children.OfType<NetworkInfoView>().ToList();
foreach (NetworkInfoView n in list)
{
networks.Add(n.network);
}
return networks;
} }
} }
} }