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

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 ServersItem;
  28. private MenuItem globalModeItem;
  29. private MenuItem PACModeItem;
  30. private ConfigForm configForm;
  31. public MenuViewController(ShadowsocksController controller)
  32. {
  33. this.controller = controller;
  34. LoadMenu();
  35. controller.EnableStatusChanged += controller_EnableStatusChanged;
  36. controller.ConfigChanged += controller_ConfigChanged;
  37. controller.PACFileReadyToOpen += controller_PACFileReadyToOpen;
  38. controller.ShareOverLANStatusChanged += controller_ShareOverLANStatusChanged;
  39. controller.EnableGlobalChanged += controller_EnableGlobalChanged;
  40. controller.Errored += controller_Errored;
  41. controller.UpdatePACFromGFWListCompleted += controller_UpdatePACFromGFWListCompleted;
  42. controller.UpdatePACFromGFWListError += controller_UpdatePACFromGFWListError;
  43. _notifyIcon = new NotifyIcon();
  44. UpdateTrayIcon();
  45. _notifyIcon.Visible = true;
  46. _notifyIcon.ContextMenu = contextMenu1;
  47. _notifyIcon.MouseDoubleClick += notifyIcon1_DoubleClick;
  48. this.updateChecker = new UpdateChecker();
  49. updateChecker.NewVersionFound += updateChecker_NewVersionFound;
  50. LoadCurrentConfiguration();
  51. updateChecker.CheckUpdate();
  52. if (controller.GetConfiguration().isDefault)
  53. {
  54. _isFirstRun = true;
  55. ShowConfigForm();
  56. }
  57. }
  58. void controller_Errored(object sender, System.IO.ErrorEventArgs e)
  59. {
  60. MessageBox.Show(e.GetException().ToString(), String.Format(I18N.GetString("Shadowsocks Error: {0}"), e.GetException().Message));
  61. }
  62. private void UpdateTrayIcon()
  63. {
  64. int dpi;
  65. Graphics graphics = Graphics.FromHwnd(IntPtr.Zero);
  66. dpi = (int)graphics.DpiX;
  67. graphics.Dispose();
  68. Bitmap icon = null;
  69. if (dpi < 97)
  70. {
  71. // dpi = 96;
  72. icon = Resources.ss16;
  73. }
  74. else if (dpi < 121)
  75. {
  76. // dpi = 120;
  77. icon = Resources.ss20;
  78. }
  79. else
  80. {
  81. icon = Resources.ss24;
  82. }
  83. bool enabled = controller.GetConfiguration().enabled;
  84. if (!enabled)
  85. {
  86. Bitmap iconCopy = new Bitmap(icon);
  87. for (int x = 0; x < iconCopy.Width; x++)
  88. {
  89. for (int y = 0; y < iconCopy.Height; y++)
  90. {
  91. Color color = icon.GetPixel(x, y);
  92. iconCopy.SetPixel(x, y , Color.FromArgb((byte)(color.A / 1.25), color.R, color.G, color.B));
  93. }
  94. }
  95. icon = iconCopy;
  96. }
  97. _notifyIcon.Icon = Icon.FromHandle(icon.GetHicon());
  98. string text = I18N.GetString("Shadowsocks") + " " + UpdateChecker.Version + "\n" + (enabled ? I18N.GetString("Enabled") : I18N.GetString("Disabled")) + "\n" + controller.GetCurrentServer().FriendlyName();
  99. _notifyIcon.Text = text.Substring(0, Math.Min(63, text.Length));
  100. }
  101. private MenuItem CreateMenuItem(string text, EventHandler click)
  102. {
  103. return new MenuItem(I18N.GetString(text), click);
  104. }
  105. private MenuItem CreateMenuGroup(string text, MenuItem[] items)
  106. {
  107. return new MenuItem(I18N.GetString(text), items);
  108. }
  109. private void LoadMenu()
  110. {
  111. this.contextMenu1 = new ContextMenu(new MenuItem[] {
  112. this.enableItem = CreateMenuItem("Enable", new EventHandler(this.EnableItem_Click)),
  113. CreateMenuGroup("Mode", new MenuItem[] {
  114. this.PACModeItem = CreateMenuItem("PAC", new EventHandler(this.PACModeItem_Click)),
  115. this.globalModeItem = CreateMenuItem("Global", new EventHandler(this.GlobalModeItem_Click))
  116. }),
  117. this.ServersItem = CreateMenuGroup("Servers", new MenuItem[] {
  118. this.SeperatorItem = new MenuItem("-"),
  119. this.ConfigItem = CreateMenuItem("Edit Servers...", new EventHandler(this.Config_Click))
  120. }),
  121. new MenuItem("-"),
  122. this.AutoStartupItem = CreateMenuItem("Start on Boot", new EventHandler(this.AutoStartupItem_Click)),
  123. this.ShareOverLANItem = CreateMenuItem("Share over LAN", new EventHandler(this.ShareOverLANItem_Click)),
  124. CreateMenuItem("Edit PAC File...", new EventHandler(this.EditPACFileItem_Click)),
  125. CreateMenuItem("Update PAC File via gfwlist...", new EventHandler(this.UpdatePACFromGFWListItem_Click)),
  126. new MenuItem("-"),
  127. CreateMenuItem("Show QRCode...", new EventHandler(this.QRCodeItem_Click)),
  128. CreateMenuItem("Show Logs...", new EventHandler(this.ShowLogItem_Click)),
  129. CreateMenuItem("About...", new EventHandler(this.AboutItem_Click)),
  130. new MenuItem("-"),
  131. CreateMenuItem("Quit", new EventHandler(this.Quit_Click))
  132. });
  133. }
  134. private void controller_ConfigChanged(object sender, EventArgs e)
  135. {
  136. LoadCurrentConfiguration();
  137. UpdateTrayIcon();
  138. }
  139. private void controller_EnableStatusChanged(object sender, EventArgs e)
  140. {
  141. enableItem.Checked = controller.GetConfiguration().enabled;
  142. }
  143. void controller_ShareOverLANStatusChanged(object sender, EventArgs e)
  144. {
  145. ShareOverLANItem.Checked = controller.GetConfiguration().shareOverLan;
  146. }
  147. void controller_EnableGlobalChanged(object sender, EventArgs e)
  148. {
  149. globalModeItem.Checked = controller.GetConfiguration().global;
  150. PACModeItem.Checked = !globalModeItem.Checked;
  151. }
  152. void controller_PACFileReadyToOpen(object sender, ShadowsocksController.PathEventArgs e)
  153. {
  154. string argument = @"/select, " + e.Path;
  155. System.Diagnostics.Process.Start("explorer.exe", argument);
  156. }
  157. void controller_UpdatePACFromGFWListError(object sender, System.IO.ErrorEventArgs e)
  158. {
  159. _notifyIcon.BalloonTipTitle = I18N.GetString("Update PAC File via gfwlist...");
  160. _notifyIcon.BalloonTipText = I18N.GetString("Update PAC file failed");
  161. _notifyIcon.BalloonTipIcon = ToolTipIcon.Info;
  162. _notifyIcon.ShowBalloonTip(5000);
  163. Logging.LogUsefulException(e.GetException());
  164. }
  165. void controller_UpdatePACFromGFWListCompleted(object sender, EventArgs e)
  166. {
  167. _notifyIcon.BalloonTipTitle = I18N.GetString("Update PAC File via gfwlist...");
  168. _notifyIcon.BalloonTipText = I18N.GetString("Update PAC file succeed");
  169. _notifyIcon.BalloonTipIcon = ToolTipIcon.Info;
  170. _notifyIcon.ShowBalloonTip(5000);
  171. }
  172. void updateChecker_NewVersionFound(object sender, EventArgs e)
  173. {
  174. _notifyIcon.BalloonTipTitle = String.Format(I18N.GetString("Shadowsocks {0} Update Found"), updateChecker.LatestVersionNumber);
  175. _notifyIcon.BalloonTipText = I18N.GetString("Click here to download");
  176. _notifyIcon.BalloonTipIcon = ToolTipIcon.Info;
  177. _notifyIcon.BalloonTipClicked += notifyIcon1_BalloonTipClicked;
  178. _notifyIcon.ShowBalloonTip(5000);
  179. _isFirstRun = false;
  180. }
  181. void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
  182. {
  183. System.Diagnostics.Process.Start(updateChecker.LatestVersionURL);
  184. }
  185. private void LoadCurrentConfiguration()
  186. {
  187. Configuration config = controller.GetConfiguration();
  188. UpdateServersMenu();
  189. enableItem.Checked = config.enabled;
  190. globalModeItem.Checked = config.global;
  191. PACModeItem.Checked = !config.global;
  192. ShareOverLANItem.Checked = config.shareOverLan;
  193. AutoStartupItem.Checked = AutoStartup.Check();
  194. }
  195. private void UpdateServersMenu()
  196. {
  197. var items = ServersItem.MenuItems;
  198. items.Clear();
  199. Configuration configuration = controller.GetConfiguration();
  200. for (int i = 0; i < configuration.configs.Count; i++)
  201. {
  202. Server server = configuration.configs[i];
  203. MenuItem item = new MenuItem(server.FriendlyName());
  204. item.Tag = i;
  205. item.Click += AServerItem_Click;
  206. items.Add(item);
  207. }
  208. items.Add(SeperatorItem);
  209. items.Add(ConfigItem);
  210. if (configuration.index >= 0 && configuration.index < configuration.configs.Count)
  211. {
  212. items[configuration.index].Checked = true;
  213. }
  214. }
  215. private void ShowConfigForm()
  216. {
  217. if (configForm != null)
  218. {
  219. configForm.Activate();
  220. }
  221. else
  222. {
  223. configForm = new ConfigForm(controller);
  224. configForm.Show();
  225. configForm.FormClosed += configForm_FormClosed;
  226. }
  227. }
  228. void configForm_FormClosed(object sender, FormClosedEventArgs e)
  229. {
  230. configForm = null;
  231. Util.Util.ReleaseMemory();
  232. ShowFirstTimeBalloon();
  233. }
  234. private void Config_Click(object sender, EventArgs e)
  235. {
  236. ShowConfigForm();
  237. }
  238. private void Quit_Click(object sender, EventArgs e)
  239. {
  240. controller.Stop();
  241. _notifyIcon.Visible = false;
  242. Application.Exit();
  243. }
  244. private void ShowFirstTimeBalloon()
  245. {
  246. if (_isFirstRun)
  247. {
  248. _notifyIcon.BalloonTipTitle = I18N.GetString("Shadowsocks is here");
  249. _notifyIcon.BalloonTipText = I18N.GetString("You can turn on/off Shadowsocks in the context menu");
  250. _notifyIcon.BalloonTipIcon = ToolTipIcon.Info;
  251. _notifyIcon.ShowBalloonTip(0);
  252. _isFirstRun = false;
  253. }
  254. }
  255. private void AboutItem_Click(object sender, EventArgs e)
  256. {
  257. Process.Start("https://github.com/shadowsocks/shadowsocks-csharp");
  258. }
  259. private void notifyIcon1_DoubleClick(object sender, MouseEventArgs e)
  260. {
  261. if (e.Button == MouseButtons.Left)
  262. {
  263. ShowConfigForm();
  264. }
  265. }
  266. private void EnableItem_Click(object sender, EventArgs e)
  267. {
  268. controller.ToggleEnable(!enableItem.Checked);
  269. }
  270. private void GlobalModeItem_Click(object sender, EventArgs e)
  271. {
  272. controller.ToggleGlobal(true);
  273. }
  274. private void PACModeItem_Click(object sender, EventArgs e)
  275. {
  276. controller.ToggleGlobal(false);
  277. }
  278. private void ShareOverLANItem_Click(object sender, EventArgs e)
  279. {
  280. ShareOverLANItem.Checked = !ShareOverLANItem.Checked;
  281. controller.ToggleShareOverLAN(ShareOverLANItem.Checked);
  282. }
  283. private void EditPACFileItem_Click(object sender, EventArgs e)
  284. {
  285. controller.TouchPACFile();
  286. }
  287. private void UpdatePACFromGFWListItem_Click(object sender, EventArgs e)
  288. {
  289. _notifyIcon.BalloonTipTitle = I18N.GetString("Shadowsocks") + " " + UpdateChecker.Version;
  290. _notifyIcon.BalloonTipText = I18N.GetString("Update PAC File via gfwlist...");
  291. _notifyIcon.BalloonTipIcon = ToolTipIcon.Info;
  292. _notifyIcon.ShowBalloonTip(5000);
  293. controller.UpdatePACFromGFWList();
  294. }
  295. private void AServerItem_Click(object sender, EventArgs e)
  296. {
  297. MenuItem item = (MenuItem)sender;
  298. controller.SelectServerIndex((int)item.Tag);
  299. }
  300. private void ShowLogItem_Click(object sender, EventArgs e)
  301. {
  302. string argument = Logging.LogFile;
  303. System.Diagnostics.Process.Start("notepad.exe", argument);
  304. }
  305. private void QRCodeItem_Click(object sender, EventArgs e)
  306. {
  307. QRCodeForm qrCodeForm = new QRCodeForm(controller.GetQRCodeForCurrentServer());
  308. //qrCodeForm.Icon = this.Icon;
  309. // TODO
  310. qrCodeForm.Show();
  311. }
  312. private void AutoStartupItem_Click(object sender, EventArgs e) {
  313. AutoStartupItem.Checked = !AutoStartupItem.Checked;
  314. if (!AutoStartup.Set(AutoStartupItem.Checked)) {
  315. MessageBox.Show("Failed to edit registry");
  316. }
  317. }
  318. }
  319. }