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