NetworkListView.xaml.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 NetworkListView : 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 NetworkListView()
  32. {
  33. InitializeComponent();
  34. APIHandler.Instance.GetStatus(updateStatus);
  35. if (!connected)
  36. {
  37. MessageBox.Show("Unable to connect to ZerOTier Service");
  38. return;
  39. }
  40. APIHandler.Instance.GetNetworks(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(ZeroTierStatus status)
  47. {
  48. if (status != null)
  49. {
  50. connected = true;
  51. networkId.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  52. {
  53. this.networkId.Text = status.Address;
  54. }));
  55. versionString.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  56. {
  57. this.versionString.Content = status.Version;
  58. }));
  59. onlineStatus.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  60. {
  61. this.onlineStatus.Content = (status.Online ? "ONLINE" : "OFFLINE");
  62. }));
  63. }
  64. else
  65. {
  66. connected = false;
  67. networkId.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  68. {
  69. this.networkId.Text = "";
  70. }));
  71. versionString.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  72. {
  73. this.versionString.Content = "0";
  74. }));
  75. onlineStatus.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  76. {
  77. this.onlineStatus.Content = "OFFLINE";
  78. }));
  79. }
  80. }
  81. private void updateNetworks(List<ZeroTierNetwork> networks)
  82. {
  83. if (networks != null)
  84. {
  85. networksPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  86. {
  87. networksPage.setNetworks(networks);
  88. }));
  89. }
  90. }
  91. private void OnUpdateTimer(object source, ElapsedEventArgs e)
  92. {
  93. APIHandler.Instance.GetStatus(updateStatus);
  94. APIHandler.Instance.GetNetworks(updateNetworks);
  95. }
  96. private void joinButton_Click(object sender, RoutedEventArgs e)
  97. {
  98. if (joinNetworkID.Text.Length < 16)
  99. {
  100. MessageBox.Show("Invalid Network ID");
  101. }
  102. else
  103. {
  104. APIHandler.Instance.JoinNetwork(joinNetworkID.Text);
  105. }
  106. }
  107. private void OnNetworkEntered(object sender, TextCompositionEventArgs e)
  108. {
  109. e.Handled = !charRegex.IsMatch(e.Text);
  110. }
  111. private void OnPaste(object sender, DataObjectPastingEventArgs e)
  112. {
  113. var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true);
  114. if (!isText) return;
  115. var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
  116. if (!wholeStringRegex.IsMatch(text))
  117. {
  118. e.CancelCommand();
  119. }
  120. }
  121. }
  122. }