You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

MenuViewController.cs 14 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. using Shadowsocks.Controller;
  2. using Shadowsocks.Model;
  3. using Shadowsocks.Properties;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. namespace Shadowsocks.View
  11. {
  12. public class MenuViewController
  13. {
  14. // yes this is just a menu view controller
  15. // when config form is closed, it moves away from RAM
  16. // and it should just does anything related to the config form
  17. private ShadowsocksController controller;
  18. private UpdateChecker updateChecker;
  19. private NotifyIcon notifyIcon1;
  20. private ContextMenu contextMenu1;
  21. // this is a copy of configuration that we are working on
  22. private bool _isFirstRun;
  23. private MenuItem enableItem;
  24. private MenuItem AutoStartupItem;
  25. private MenuItem ShareOverLANItem;
  26. private MenuItem SeperatorItem;
  27. private MenuItem ConfigItem;
  28. private MenuItem menuItem4;
  29. private MenuItem editPACFileItem;
  30. private MenuItem QRCodeItem;
  31. private MenuItem ShowLogItem;
  32. private MenuItem aboutItem;
  33. private MenuItem ServersItem;
  34. private MenuItem menuItem3;
  35. private MenuItem quitItem;
  36. private MenuItem menuItem1;
  37. private ConfigForm configForm;
  38. public MenuViewController(ShadowsocksController controller)
  39. {
  40. LoadMenu();
  41. LoadTrayIcon();
  42. this.controller = controller;
  43. controller.EnableStatusChanged += controller_EnableStatusChanged;
  44. controller.ConfigChanged += controller_ConfigChanged;
  45. controller.PACFileReadyToOpen += controller_PACFileReadyToOpen;
  46. controller.ShareOverLANStatusChanged += controller_ShareOverLANStatusChanged;
  47. this.updateChecker = new UpdateChecker();
  48. updateChecker.NewVersionFound += updateChecker_NewVersionFound;
  49. LoadCurrentConfiguration();
  50. updateChecker.CheckUpdate();
  51. if (controller.GetConfiguration().isDefault)
  52. {
  53. _isFirstRun = true;
  54. ShowConfigForm();
  55. }
  56. }
  57. private void LoadTrayIcon()
  58. {
  59. int dpi;
  60. Graphics graphics = Graphics.FromHwnd(IntPtr.Zero);
  61. dpi = (int)graphics.DpiX;
  62. graphics.Dispose();
  63. Bitmap icon = null;
  64. if (dpi < 97)
  65. {
  66. // dpi = 96;
  67. icon = Resources.ss16;
  68. }
  69. else if (dpi < 121)
  70. {
  71. // dpi = 120;
  72. icon = Resources.ss20;
  73. }
  74. else
  75. {
  76. icon = Resources.ss24;
  77. }
  78. notifyIcon1 = new NotifyIcon();
  79. notifyIcon1.Icon = Icon.FromHandle(icon.GetHicon());
  80. notifyIcon1.Visible = true;
  81. notifyIcon1.ContextMenu = contextMenu1;
  82. notifyIcon1.DoubleClick +=notifyIcon1_DoubleClick;
  83. }
  84. private void LoadMenu()
  85. {
  86. this.contextMenu1 = new System.Windows.Forms.ContextMenu();
  87. this.enableItem = new System.Windows.Forms.MenuItem();
  88. this.AutoStartupItem = new System.Windows.Forms.MenuItem();
  89. this.ShareOverLANItem = new System.Windows.Forms.MenuItem();
  90. this.ServersItem = new System.Windows.Forms.MenuItem();
  91. this.SeperatorItem = new System.Windows.Forms.MenuItem();
  92. this.ConfigItem = new System.Windows.Forms.MenuItem();
  93. this.menuItem4 = new System.Windows.Forms.MenuItem();
  94. this.editPACFileItem = new System.Windows.Forms.MenuItem();
  95. this.QRCodeItem = new System.Windows.Forms.MenuItem();
  96. this.ShowLogItem = new System.Windows.Forms.MenuItem();
  97. this.aboutItem = new System.Windows.Forms.MenuItem();
  98. this.menuItem3 = new System.Windows.Forms.MenuItem();
  99. this.quitItem = new System.Windows.Forms.MenuItem();
  100. this.menuItem1 = new System.Windows.Forms.MenuItem();
  101. //
  102. // contextMenu1
  103. //
  104. this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  105. this.enableItem,
  106. this.ServersItem,
  107. this.menuItem1,
  108. this.AutoStartupItem,
  109. this.ShareOverLANItem,
  110. this.editPACFileItem,
  111. this.menuItem4,
  112. this.QRCodeItem,
  113. this.ShowLogItem,
  114. this.aboutItem,
  115. this.menuItem3,
  116. this.quitItem});
  117. //
  118. // enableItem
  119. //
  120. this.enableItem.Index = 0;
  121. this.enableItem.Text = "&Enable";
  122. this.enableItem.Click += new System.EventHandler(this.EnableItem_Click);
  123. //
  124. // AutoStartupItem
  125. //
  126. this.AutoStartupItem.Index = 3;
  127. this.AutoStartupItem.Text = "Start on Boot";
  128. this.AutoStartupItem.Click += new System.EventHandler(this.AutoStartupItem_Click);
  129. //
  130. // ShareOverLANItem
  131. //
  132. this.ShareOverLANItem.Index = 4;
  133. this.ShareOverLANItem.Text = "Share over LAN";
  134. this.ShareOverLANItem.Click += new System.EventHandler(this.ShareOverLANItem_Click);
  135. //
  136. // ServersItem
  137. //
  138. this.ServersItem.Index = 1;
  139. this.ServersItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  140. this.SeperatorItem,
  141. this.ConfigItem});
  142. this.ServersItem.Text = "&Servers";
  143. //
  144. // SeperatorItem
  145. //
  146. this.SeperatorItem.Index = 0;
  147. this.SeperatorItem.Text = "-";
  148. //
  149. // ConfigItem
  150. //
  151. this.ConfigItem.Index = 1;
  152. this.ConfigItem.Text = "Edit Servers...";
  153. this.ConfigItem.Click += new System.EventHandler(this.Config_Click);
  154. //
  155. // menuItem4
  156. //
  157. this.menuItem4.Index = 6;
  158. this.menuItem4.Text = "-";
  159. //
  160. // editPACFileItem
  161. //
  162. this.editPACFileItem.Index = 5;
  163. this.editPACFileItem.Text = "Edit &PAC File...";
  164. this.editPACFileItem.Click += new System.EventHandler(this.EditPACFileItem_Click);
  165. //
  166. // QRCodeItem
  167. //
  168. this.QRCodeItem.Index = 7;
  169. this.QRCodeItem.Text = "Show &QRCode...";
  170. this.QRCodeItem.Click += new System.EventHandler(this.QRCodeItem_Click);
  171. //
  172. // ShowLogItem
  173. //
  174. this.ShowLogItem.Index = 8;
  175. this.ShowLogItem.Text = "Show Logs...";
  176. this.ShowLogItem.Click += new System.EventHandler(this.ShowLogItem_Click);
  177. //
  178. // aboutItem
  179. //
  180. this.aboutItem.Index = 9;
  181. this.aboutItem.Text = "About...";
  182. this.aboutItem.Click += new System.EventHandler(this.AboutItem_Click);
  183. //
  184. // menuItem3
  185. //
  186. this.menuItem3.Index = 10;
  187. this.menuItem3.Text = "-";
  188. //
  189. // quitItem
  190. //
  191. this.quitItem.Index = 11;
  192. this.quitItem.Text = "&Quit";
  193. this.quitItem.Click += new System.EventHandler(this.Quit_Click);
  194. //
  195. // menuItem1
  196. //
  197. this.menuItem1.Index = 2;
  198. this.menuItem1.Text = "-";
  199. }
  200. private void controller_ConfigChanged(object sender, EventArgs e)
  201. {
  202. LoadCurrentConfiguration();
  203. }
  204. private void controller_EnableStatusChanged(object sender, EventArgs e)
  205. {
  206. enableItem.Checked = controller.GetConfiguration().enabled;
  207. }
  208. void controller_ShareOverLANStatusChanged(object sender, EventArgs e)
  209. {
  210. ShareOverLANItem.Checked = controller.GetConfiguration().shareOverLan;
  211. }
  212. void controller_PACFileReadyToOpen(object sender, ShadowsocksController.PathEventArgs e)
  213. {
  214. string argument = @"/select, " + e.Path;
  215. System.Diagnostics.Process.Start("explorer.exe", argument);
  216. }
  217. void updateChecker_NewVersionFound(object sender, EventArgs e)
  218. {
  219. notifyIcon1.BalloonTipTitle = "Shadowsocks " + updateChecker.LatestVersionNumber + " Update Found";
  220. notifyIcon1.BalloonTipText = "Click here to download";
  221. notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
  222. notifyIcon1.BalloonTipClicked += notifyIcon1_BalloonTipClicked;
  223. notifyIcon1.ShowBalloonTip(5000);
  224. _isFirstRun = false;
  225. }
  226. void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
  227. {
  228. System.Diagnostics.Process.Start(updateChecker.LatestVersionURL);
  229. }
  230. private void LoadCurrentConfiguration()
  231. {
  232. Configuration config = controller.GetConfiguration();
  233. UpdateServersMenu();
  234. enableItem.Checked = config.enabled;
  235. ShareOverLANItem.Checked = config.shareOverLan;
  236. AutoStartupItem.Checked = AutoStartup.Check();
  237. }
  238. private void UpdateServersMenu()
  239. {
  240. var items = ServersItem.MenuItems;
  241. items.Clear();
  242. Configuration configuration = controller.GetConfiguration();
  243. for (int i = 0; i < configuration.configs.Count; i++)
  244. {
  245. Server server = configuration.configs[i];
  246. MenuItem item = new MenuItem(string.IsNullOrEmpty(server.remarks) ? server.server + ":" + server.server_port : server.server + ":" + server.server_port + " (" + server.remarks + ")");
  247. item.Tag = i;
  248. item.Click += AServerItem_Click;
  249. items.Add(item);
  250. }
  251. items.Add(SeperatorItem);
  252. items.Add(ConfigItem);
  253. if (configuration.index >= 0 && configuration.index < configuration.configs.Count)
  254. {
  255. items[configuration.index].Checked = true;
  256. }
  257. }
  258. private void ShowConfigForm()
  259. {
  260. if (configForm != null)
  261. {
  262. configForm.Focus();
  263. }
  264. else
  265. {
  266. configForm = new ConfigForm(controller);
  267. configForm.Show();
  268. configForm.FormClosed += configForm_FormClosed;
  269. }
  270. }
  271. void configForm_FormClosed(object sender, FormClosedEventArgs e)
  272. {
  273. configForm = null;
  274. GC.Collect();
  275. GC.WaitForPendingFinalizers();
  276. ShowFirstTimeBalloon();
  277. }
  278. private void Config_Click(object sender, EventArgs e)
  279. {
  280. ShowConfigForm();
  281. }
  282. private void Quit_Click(object sender, EventArgs e)
  283. {
  284. controller.Stop();
  285. notifyIcon1.Visible = false;
  286. Application.Exit();
  287. }
  288. private void ShowFirstTimeBalloon()
  289. {
  290. if (_isFirstRun)
  291. {
  292. notifyIcon1.BalloonTipTitle = "Shadowsocks is here";
  293. notifyIcon1.BalloonTipText = "You can turn on/off Shadowsocks in the context menu";
  294. notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
  295. notifyIcon1.ShowBalloonTip(0);
  296. _isFirstRun = false;
  297. }
  298. }
  299. private void AboutItem_Click(object sender, EventArgs e)
  300. {
  301. Process.Start("https://github.com/clowwindy/shadowsocks-csharp");
  302. }
  303. private void notifyIcon1_DoubleClick(object sender, EventArgs e)
  304. {
  305. ShowConfigForm();
  306. }
  307. private void EnableItem_Click(object sender, EventArgs e)
  308. {
  309. enableItem.Checked = !enableItem.Checked;
  310. controller.ToggleEnable(enableItem.Checked);
  311. }
  312. private void ShareOverLANItem_Click(object sender, EventArgs e)
  313. {
  314. ShareOverLANItem.Checked = !ShareOverLANItem.Checked;
  315. controller.ToggleShareOverLAN(ShareOverLANItem.Checked);
  316. }
  317. private void EditPACFileItem_Click(object sender, EventArgs e)
  318. {
  319. controller.TouchPACFile();
  320. }
  321. private void AServerItem_Click(object sender, EventArgs e)
  322. {
  323. MenuItem item = (MenuItem)sender;
  324. controller.SelectServerIndex((int)item.Tag);
  325. }
  326. private void ShowLogItem_Click(object sender, EventArgs e)
  327. {
  328. string argument = Logging.LogFile;
  329. System.Diagnostics.Process.Start("notepad.exe", argument);
  330. }
  331. private void QRCodeItem_Click(object sender, EventArgs e)
  332. {
  333. QRCodeForm qrCodeForm = new QRCodeForm(controller.GetQRCodeForCurrentServer());
  334. //qrCodeForm.Icon = this.Icon;
  335. // TODO
  336. qrCodeForm.Show();
  337. }
  338. private void AutoStartupItem_Click(object sender, EventArgs e) {
  339. AutoStartupItem.Checked = !AutoStartupItem.Checked;
  340. if (!AutoStartup.Set(AutoStartupItem.Checked)) {
  341. MessageBox.Show("Failed to edit registry");
  342. }
  343. }
  344. }
  345. }