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

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