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

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