ToolbarItem.xaml.cs 12 KB

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