MainWindow.xaml.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 MainWindow : Window
  26. {
  27. APIHandler handler;
  28. Regex charRegex = new Regex("[0-9a-fxA-FX]");
  29. Regex wholeStringRegex = new Regex("^[0-9a-fxA-FX]+$");
  30. Timer timer = new Timer();
  31. bool connected = false;
  32. public MainWindow()
  33. {
  34. InitializeComponent();
  35. }
  36. public void SetAPIHandler(APIHandler handler)
  37. {
  38. timer.Stop();
  39. timer = new Timer();
  40. this.handler = handler;
  41. networksPage.SetAPIHandler(handler);
  42. updateStatus();
  43. if (!connected)
  44. {
  45. MessageBox.Show("Unable to connect to ZerOTier Service");
  46. return;
  47. }
  48. updateNetworks();
  49. DataObject.AddPastingHandler(joinNetworkID, OnPaste);
  50. timer.Elapsed += new ElapsedEventHandler(OnUpdateTimer);
  51. timer.Interval = 2000;
  52. timer.Enabled = true;
  53. }
  54. private void updateStatus()
  55. {
  56. var status = handler.GetStatus();
  57. if (status != null)
  58. {
  59. connected = true;
  60. networkId.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  61. {
  62. this.networkId.Content = status.Address;
  63. }));
  64. versionString.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  65. {
  66. this.versionString.Content = status.Version;
  67. }));
  68. onlineStatus.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  69. {
  70. this.onlineStatus.Content = (status.Online ? "ONLINE" : "OFFLINE");
  71. }));
  72. }
  73. else
  74. {
  75. connected = false;
  76. networkId.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  77. {
  78. this.networkId.Content = "";
  79. }));
  80. versionString.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  81. {
  82. this.versionString.Content = "0";
  83. }));
  84. onlineStatus.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  85. {
  86. this.onlineStatus.Content = "OFFLINE";
  87. }));
  88. }
  89. }
  90. private void updateNetworks()
  91. {
  92. var networks = handler.GetNetworks();
  93. networksPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  94. {
  95. networksPage.setNetworks(networks);
  96. }));
  97. }
  98. private void updatePeers()
  99. {
  100. //var peers = handler.GetPeers();
  101. //peersPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  102. //{
  103. // peersPage.SetPeers(peers);
  104. //}));
  105. }
  106. private void OnUpdateTimer(object source, ElapsedEventArgs e)
  107. {
  108. updateStatus();
  109. updateNetworks();
  110. //updatePeers();
  111. }
  112. private void joinButton_Click(object sender, RoutedEventArgs e)
  113. {
  114. if (joinNetworkID.Text.Length < 16)
  115. {
  116. MessageBox.Show("Invalid Network ID");
  117. }
  118. else
  119. {
  120. handler.JoinNetwork(joinNetworkID.Text);
  121. }
  122. }
  123. private void OnNetworkEntered(object sender, TextCompositionEventArgs e)
  124. {
  125. e.Handled = !charRegex.IsMatch(e.Text);
  126. }
  127. private void OnPaste(object sender, DataObjectPastingEventArgs e)
  128. {
  129. var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true);
  130. if (!isText) return;
  131. var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
  132. if (!wholeStringRegex.IsMatch(text))
  133. {
  134. e.CancelCommand();
  135. }
  136. }
  137. }
  138. }