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 16 kB

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