ToolbarItem.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Net.Http;
  7. using System.Runtime.CompilerServices;
  8. using System.Text;
  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.Shapes;
  18. using System.Text.RegularExpressions;
  19. using System.Timers;
  20. using System.Windows.Threading;
  21. using System.IO;
  22. using System.Diagnostics;
  23. using Microsoft.Win32;
  24. namespace WinUI
  25. {
  26. /// <summary>
  27. /// Interaction logic for ToolbarItem.xaml
  28. /// </summary>
  29. public partial class ToolbarItem : Window, INotifyPropertyChanged
  30. {
  31. private APIHandler handler = APIHandler.Instance;
  32. private Point netListLocation = new Point(0, 0);
  33. private Point joinNetLocation = new Point(0, 0);
  34. private Point aboutViewLocation = new Point(0, 0);
  35. private Point prefsViewLocation = new Point(0, 0);
  36. private NetworkListView netListView = new NetworkListView();
  37. private JoinNetworkView joinNetView = null;
  38. private AboutView aboutView = null;
  39. private PreferencesView prefsView = null;
  40. private NetworkMonitor mon = NetworkMonitor.Instance;
  41. private ObservableCollection<MenuItem> _networkCollection = new ObservableCollection<MenuItem>();
  42. public ObservableCollection<MenuItem> NetworkCollection
  43. {
  44. get { return _networkCollection; }
  45. set { _networkCollection = value; }
  46. }
  47. private string nodeId;
  48. public ToolbarItem()
  49. {
  50. InitializeComponent();
  51. mon.SubscribeNetworkUpdates(updateNetworks);
  52. mon.SubscribeStatusUpdates(updateStatus);
  53. SystemEvents.DisplaySettingsChanged += new EventHandler(SystemEvents_DisplaySettingsChanged);
  54. }
  55. ~ToolbarItem()
  56. {
  57. mon.UnsubscribeNetworkUpdates(updateNetworks);
  58. mon.UnsubscribeStatusUpdates(updateStatus);
  59. }
  60. public event PropertyChangedEventHandler PropertyChanged;
  61. protected void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
  62. {
  63. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  64. }
  65. private void updateNetworks(List<ZeroTierNetwork> networks)
  66. {
  67. if (networks != null)
  68. {
  69. Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  70. {
  71. NetworkCollection.Clear();
  72. foreach (ZeroTierNetwork n in networks)
  73. {
  74. MenuItem item = new MenuItem();
  75. item.Header = n.Title.Replace("_", "__");
  76. item.DataContext = n;
  77. item.IsChecked = n.IsConnected;
  78. item.Click += ToolbarItem_NetworkClicked;
  79. NetworkCollection.Add(item);
  80. }
  81. }));
  82. }
  83. }
  84. private void updateStatus(ZeroTierStatus status)
  85. {
  86. if (status != null)
  87. {
  88. Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  89. {
  90. nodeIdMenuItem.Header = "Node ID: " + status.Address;
  91. nodeIdMenuItem.IsEnabled = true;
  92. nodeId = status.Address;
  93. if (CentralAPI.Instance.HasAccessToken())
  94. {
  95. newNetworkItem.IsEnabled = true;
  96. }
  97. else
  98. {
  99. newNetworkItem.IsEnabled = false;
  100. }
  101. }));
  102. }
  103. }
  104. private void ToolbarItem_NodeIDClicked(object sender, System.Windows.RoutedEventArgs e)
  105. {
  106. try
  107. {
  108. Clipboard.SetDataObject(nodeId);
  109. }
  110. catch (ArgumentNullException)
  111. {
  112. // tried to copy a null nodeId
  113. Console.WriteLine("ArgumentNullException");
  114. }
  115. catch (Exception ex)
  116. {
  117. Console.WriteLine(ex.ToString());
  118. }
  119. }
  120. private void ToolbarItem_ShowNetworksClicked(object sender, System.Windows.RoutedEventArgs e)
  121. {
  122. if (netListView == null)
  123. {
  124. netListView = new WinUI.NetworkListView();
  125. netListView.Closed += ShowNetworksClosed;
  126. }
  127. bool netListNeedsMoving = true;
  128. if (netListLocation.X > 0 && netListLocation.Y > 0)
  129. {
  130. netListView.Left = netListLocation.X;
  131. netListView.Top = netListLocation.Y;
  132. netListNeedsMoving = false;
  133. }
  134. netListView.Show();
  135. if (netListNeedsMoving)
  136. {
  137. setWindowPosition(netListView);
  138. netListLocation.X = netListView.Left;
  139. netListLocation.Y = netListView.Top;
  140. }
  141. netListView.Activate();
  142. }
  143. private void ShowNetworksClosed(object sender, System.EventArgs e)
  144. {
  145. netListView = null;
  146. }
  147. private void ToolbarItem_JoinNetworkClicked(object sender, System.EventArgs e)
  148. {
  149. if (joinNetView == null)
  150. {
  151. joinNetView = new JoinNetworkView();
  152. joinNetView.Closed += JoinNetworkClosed;
  153. bool needsMove = true;
  154. if (joinNetLocation.X > 0 && joinNetLocation.Y > 0)
  155. {
  156. joinNetView.Left = joinNetLocation.X;
  157. joinNetView.Top = joinNetLocation.Y;
  158. needsMove = false;
  159. }
  160. joinNetView.Show();
  161. if (needsMove)
  162. {
  163. setWindowPosition(joinNetView);
  164. joinNetLocation.X = joinNetView.Left;
  165. joinNetLocation.Y = joinNetView.Top;
  166. }
  167. }
  168. else
  169. {
  170. joinNetView.Activate();
  171. }
  172. }
  173. private void JoinNetworkClosed(object sender, System.EventArgs e)
  174. {
  175. joinNetView = null;
  176. }
  177. private void ToolbarItem_CentralClicked(object sender, System.EventArgs e)
  178. {
  179. Process.Start("https://my.zerotier.com");
  180. }
  181. private void ToolbarItem_AboutClicked(object sender, System.EventArgs e)
  182. {
  183. if (aboutView == null)
  184. {
  185. aboutView = new AboutView();
  186. aboutView.Closed += AboutClosed;
  187. bool needsMove = true;
  188. if (aboutViewLocation.X > 0 && aboutViewLocation.Y > 0)
  189. {
  190. aboutView.Left = aboutViewLocation.X;
  191. aboutView.Top = aboutViewLocation.Y;
  192. needsMove = false;
  193. }
  194. aboutView.Show();
  195. if (needsMove)
  196. {
  197. setWindowPosition(aboutView);
  198. aboutViewLocation.X = aboutView.Left;
  199. aboutViewLocation.Y = aboutView.Top;
  200. }
  201. }
  202. else
  203. {
  204. aboutView.Activate();
  205. }
  206. }
  207. private void AboutClosed(object sender, System.EventArgs e)
  208. {
  209. aboutView = null;
  210. }
  211. private void ToolbarItem_PreferencesClicked(object sender, System.EventArgs e)
  212. {
  213. if (prefsView == null)
  214. {
  215. prefsView = new PreferencesView();
  216. prefsView.Closed += PreferencesClosed;
  217. bool needsMove = true;
  218. if (prefsViewLocation.X > 0 && prefsViewLocation.Y > 0)
  219. {
  220. prefsView.Left = prefsViewLocation.X;
  221. prefsView.Top = prefsViewLocation.Y;
  222. needsMove = false;
  223. }
  224. prefsView.Show();
  225. if (needsMove)
  226. {
  227. setWindowPosition(prefsView);
  228. prefsViewLocation.X = prefsView.Left;
  229. prefsViewLocation.Y = prefsView.Top;
  230. }
  231. }
  232. else
  233. {
  234. prefsView.Activate();
  235. }
  236. }
  237. private void PreferencesClosed(object sender, System.EventArgs e)
  238. {
  239. prefsView = null;
  240. }
  241. private void ToolbarItem_QuitClicked(object sender, System.EventArgs e)
  242. {
  243. NetworkMonitor.Instance.StopMonitor();
  244. Close();
  245. Application.Current.Shutdown();
  246. }
  247. private void ToolbarItem_NetworkClicked(object sender, System.Windows.RoutedEventArgs e)
  248. {
  249. if(sender.GetType() == typeof(MenuItem))
  250. {
  251. MenuItem item = e.Source as MenuItem;
  252. if (item.DataContext != null)
  253. {
  254. ZeroTierNetwork network = item.DataContext as ZeroTierNetwork;
  255. if (item.IsChecked)
  256. {
  257. APIHandler.Instance.LeaveNetwork(Dispatcher, network.NetworkId);
  258. }
  259. else
  260. {
  261. APIHandler.Instance.JoinNetwork(Dispatcher, network.NetworkId, network.AllowManaged, network.AllowGlobal, network.AllowDefault);
  262. }
  263. }
  264. }
  265. }
  266. private async void ToolbarItem_NewNetwork(object sender, System.Windows.RoutedEventArgs e)
  267. {
  268. if (CentralAPI.Instance.HasAccessToken())
  269. {
  270. CentralAPI api = CentralAPI.Instance;
  271. CentralNetwork newNetwork = await api.CreateNewNetwork();
  272. APIHandler handler = APIHandler.Instance;
  273. handler.JoinNetwork(this.Dispatcher, newNetwork.Id);
  274. string nodeId = APIHandler.Instance.NodeAddress();
  275. bool authorized = await CentralAPI.Instance.AuthorizeNode(nodeId, newNetwork.Id);
  276. }
  277. }
  278. private void setWindowPosition(Window w)
  279. {
  280. double width = w.ActualWidth;
  281. double height = w.ActualHeight;
  282. double screenHeight = SystemParameters.PrimaryScreenHeight;
  283. double screenWidth = SystemParameters.PrimaryScreenWidth;
  284. double top = screenHeight - height - 40;
  285. double left = screenWidth - width - 20;
  286. w.Top = top;
  287. w.Left = left;
  288. }
  289. private void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
  290. {
  291. // reset cached locations to (0, 0) when display size changes
  292. netListLocation.X = 0;
  293. netListLocation.Y = 0;
  294. joinNetLocation.X = 0;
  295. joinNetLocation.Y = 0;
  296. aboutViewLocation.X = 0;
  297. aboutViewLocation.Y = 0;
  298. prefsViewLocation.X = 0;
  299. prefsViewLocation.Y = 0;
  300. }
  301. }
  302. }