NetworkList.xaml.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Timers;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. using System.Windows.Threading;
  20. namespace WinUI
  21. {
  22. /// <summary>
  23. /// Interaction logic for MainWindow.xaml
  24. /// </summary>
  25. public partial class NetworkList : Window
  26. {
  27. Regex charRegex = new Regex("[0-9a-fxA-FX]");
  28. Regex wholeStringRegex = new Regex("^[0-9a-fxA-FX]+$");
  29. Timer timer = new Timer();
  30. bool connected = false;
  31. public NetworkList()
  32. {
  33. InitializeComponent();
  34. updateStatus();
  35. if (!connected)
  36. {
  37. MessageBox.Show("Unable to connect to ZerOTier Service");
  38. return;
  39. }
  40. updateNetworks();
  41. DataObject.AddPastingHandler(joinNetworkID, OnPaste);
  42. timer.Elapsed += new ElapsedEventHandler(OnUpdateTimer);
  43. timer.Interval = 2000;
  44. timer.Enabled = true;
  45. }
  46. private void updateStatus()
  47. {
  48. var status = APIHandler.Instance.GetStatus();
  49. if (status != null)
  50. {
  51. connected = true;
  52. networkId.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  53. {
  54. this.networkId.Content = status.Address;
  55. }));
  56. versionString.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  57. {
  58. this.versionString.Content = status.Version;
  59. }));
  60. onlineStatus.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  61. {
  62. this.onlineStatus.Content = (status.Online ? "ONLINE" : "OFFLINE");
  63. }));
  64. }
  65. else
  66. {
  67. connected = false;
  68. networkId.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  69. {
  70. this.networkId.Content = "";
  71. }));
  72. versionString.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  73. {
  74. this.versionString.Content = "0";
  75. }));
  76. onlineStatus.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  77. {
  78. this.onlineStatus.Content = "OFFLINE";
  79. }));
  80. }
  81. }
  82. private void updateNetworks()
  83. {
  84. var networks = APIHandler.Instance.GetNetworks();
  85. networksPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  86. {
  87. networksPage.setNetworks(networks);
  88. }));
  89. }
  90. private void updatePeers()
  91. {
  92. //var peers = handler.GetPeers();
  93. //peersPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  94. //{
  95. // peersPage.SetPeers(peers);
  96. //}));
  97. }
  98. private void OnUpdateTimer(object source, ElapsedEventArgs e)
  99. {
  100. updateStatus();
  101. updateNetworks();
  102. //updatePeers();
  103. }
  104. private void joinButton_Click(object sender, RoutedEventArgs e)
  105. {
  106. if (joinNetworkID.Text.Length < 16)
  107. {
  108. MessageBox.Show("Invalid Network ID");
  109. }
  110. else
  111. {
  112. APIHandler.Instance.JoinNetwork(joinNetworkID.Text);
  113. }
  114. }
  115. private void OnNetworkEntered(object sender, TextCompositionEventArgs e)
  116. {
  117. e.Handled = !charRegex.IsMatch(e.Text);
  118. }
  119. private void OnPaste(object sender, DataObjectPastingEventArgs e)
  120. {
  121. var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true);
  122. if (!isText) return;
  123. var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
  124. if (!wholeStringRegex.IsMatch(text))
  125. {
  126. e.CancelCommand();
  127. }
  128. }
  129. }
  130. }