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 30 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
9 years ago
9 years ago
9 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
9 years ago
9 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
9 years ago
9 years ago
10 years ago
10 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. using ZXing;
  7. using ZXing.Common;
  8. using ZXing.QrCode;
  9. using Shadowsocks.Controller;
  10. using Shadowsocks.Model;
  11. using Shadowsocks.Properties;
  12. using Shadowsocks.Util;
  13. namespace Shadowsocks.View
  14. {
  15. public class MenuViewController
  16. {
  17. // yes this is just a menu view controller
  18. // when config form is closed, it moves away from RAM
  19. // and it should just do anything related to the config form
  20. private ShadowsocksController controller;
  21. private UpdateChecker updateChecker;
  22. private NotifyIcon _notifyIcon;
  23. private ContextMenu contextMenu1;
  24. private bool _isFirstRun;
  25. private bool _isStartupChecking;
  26. private MenuItem enableItem;
  27. private MenuItem modeItem;
  28. private MenuItem AutoStartupItem;
  29. private MenuItem ShareOverLANItem;
  30. private MenuItem SeperatorItem;
  31. private MenuItem ConfigItem;
  32. private MenuItem ServersItem;
  33. private MenuItem globalModeItem;
  34. private MenuItem PACModeItem;
  35. private MenuItem localPACItem;
  36. private MenuItem onlinePACItem;
  37. private MenuItem editLocalPACItem;
  38. private MenuItem updateFromGFWListItem;
  39. private MenuItem editGFWUserRuleItem;
  40. private MenuItem editOnlinePACItem;
  41. private MenuItem autoCheckUpdatesToggleItem;
  42. private ConfigForm configForm;
  43. private List<LogForm> logForms = new List<LogForm>();
  44. private bool logFormsVisible = false;
  45. private string _urlToOpen;
  46. public MenuViewController(ShadowsocksController controller)
  47. {
  48. this.controller = controller;
  49. LoadMenu();
  50. controller.EnableStatusChanged += controller_EnableStatusChanged;
  51. controller.ConfigChanged += controller_ConfigChanged;
  52. controller.PACFileReadyToOpen += controller_FileReadyToOpen;
  53. controller.UserRuleFileReadyToOpen += controller_FileReadyToOpen;
  54. controller.ShareOverLANStatusChanged += controller_ShareOverLANStatusChanged;
  55. controller.EnableGlobalChanged += controller_EnableGlobalChanged;
  56. controller.Errored += controller_Errored;
  57. controller.UpdatePACFromGFWListCompleted += controller_UpdatePACFromGFWListCompleted;
  58. controller.UpdatePACFromGFWListError += controller_UpdatePACFromGFWListError;
  59. _notifyIcon = new NotifyIcon();
  60. UpdateTrayIcon();
  61. _notifyIcon.Visible = true;
  62. _notifyIcon.ContextMenu = contextMenu1;
  63. _notifyIcon.BalloonTipClicked += notifyIcon1_BalloonTipClicked;
  64. _notifyIcon.MouseClick += notifyIcon1_Click;
  65. _notifyIcon.MouseDoubleClick += notifyIcon1_DoubleClick;
  66. _notifyIcon.BalloonTipClosed += _notifyIcon_BalloonTipClosed;
  67. this.updateChecker = new UpdateChecker();
  68. updateChecker.CheckUpdateCompleted += updateChecker_CheckUpdateCompleted;
  69. LoadCurrentConfiguration();
  70. Configuration config = controller.GetConfigurationCopy();
  71. if (config.isDefault)
  72. {
  73. _isFirstRun = true;
  74. ShowConfigForm();
  75. }
  76. else if(config.autoCheckUpdate)
  77. {
  78. _isStartupChecking = true;
  79. updateChecker.CheckUpdate(config, 3000);
  80. }
  81. }
  82. void controller_Errored(object sender, System.IO.ErrorEventArgs e)
  83. {
  84. MessageBox.Show(e.GetException().ToString(), String.Format(I18N.GetString("Shadowsocks Error: {0}"), e.GetException().Message));
  85. }
  86. private void UpdateTrayIcon()
  87. {
  88. int dpi;
  89. Graphics graphics = Graphics.FromHwnd(IntPtr.Zero);
  90. dpi = (int)graphics.DpiX;
  91. graphics.Dispose();
  92. Bitmap icon = null;
  93. if (dpi < 97)
  94. {
  95. // dpi = 96;
  96. icon = Resources.ss16;
  97. }
  98. else if (dpi < 121)
  99. {
  100. // dpi = 120;
  101. icon = Resources.ss20;
  102. }
  103. else
  104. {
  105. icon = Resources.ss24;
  106. }
  107. Configuration config = controller.GetConfigurationCopy();
  108. bool enabled = config.enabled;
  109. bool global = config.global;
  110. icon = getTrayIconByState(icon, enabled, global);
  111. _notifyIcon.Icon = Icon.FromHandle(icon.GetHicon());
  112. string serverInfo = null;
  113. if (controller.GetCurrentStrategy() != null)
  114. {
  115. serverInfo = controller.GetCurrentStrategy().Name;
  116. }
  117. else
  118. {
  119. serverInfo = config.GetCurrentServer().FriendlyName();
  120. }
  121. // we want to show more details but notify icon title is limited to 63 characters
  122. string text = I18N.GetString("Shadowsocks") + " " + UpdateChecker.Version + "\n" +
  123. (enabled ?
  124. I18N.GetString("System Proxy On: ") + (global ? I18N.GetString("Global") : I18N.GetString("PAC")) :
  125. String.Format(I18N.GetString("Running: Port {0}"), config.localPort)) // this feedback is very important because they need to know Shadowsocks is running
  126. + "\n" + serverInfo;
  127. _notifyIcon.Text = text.Substring(0, Math.Min(63, text.Length));
  128. }
  129. private Bitmap getTrayIconByState(Bitmap originIcon, bool enabled, bool global)
  130. {
  131. Bitmap iconCopy = new Bitmap(originIcon);
  132. for (int x = 0; x < iconCopy.Width; x++)
  133. {
  134. for (int y = 0; y < iconCopy.Height; y++)
  135. {
  136. Color color = originIcon.GetPixel(x, y);
  137. if (color.A != 0 && color.R > 30)
  138. {
  139. if (!enabled)
  140. {
  141. iconCopy.SetPixel(x, y, Color.FromArgb((byte)(color.A / 1.25), color.R, color.G, color.B));
  142. }
  143. else if (global)
  144. {
  145. Color flyBlue = Color.FromArgb(25, 125, 191);
  146. // Muliply with flyBlue
  147. int red = color.R * flyBlue.R / 255;
  148. int green = color.G * flyBlue.G / 255;
  149. int blue = color.B * flyBlue.B / 255;
  150. iconCopy.SetPixel(x, y, Color.FromArgb(color.A, red, green, blue));
  151. }
  152. }
  153. else
  154. {
  155. iconCopy.SetPixel(x, y, Color.FromArgb(color.A, color.R, color.G, color.B));
  156. }
  157. }
  158. }
  159. return iconCopy;
  160. }
  161. private MenuItem CreateMenuItem(string text, EventHandler click)
  162. {
  163. return new MenuItem(I18N.GetString(text), click);
  164. }
  165. private MenuItem CreateMenuGroup(string text, MenuItem[] items)
  166. {
  167. return new MenuItem(I18N.GetString(text), items);
  168. }
  169. private void LoadMenu()
  170. {
  171. this.contextMenu1 = new ContextMenu(new MenuItem[] {
  172. this.enableItem = CreateMenuItem("Enable System Proxy", new EventHandler(this.EnableItem_Click)),
  173. this.modeItem = CreateMenuGroup("Mode", new MenuItem[] {
  174. this.PACModeItem = CreateMenuItem("PAC", new EventHandler(this.PACModeItem_Click)),
  175. this.globalModeItem = CreateMenuItem("Global", new EventHandler(this.GlobalModeItem_Click))
  176. }),
  177. this.ServersItem = CreateMenuGroup("Servers", new MenuItem[] {
  178. this.SeperatorItem = new MenuItem("-"),
  179. this.ConfigItem = CreateMenuItem("Edit Servers...", new EventHandler(this.Config_Click)),
  180. CreateMenuItem("Statistics Config...", StatisticsConfigItem_Click),
  181. CreateMenuItem("Show QRCode...", new EventHandler(this.QRCodeItem_Click)),
  182. CreateMenuItem("Scan QRCode from Screen...", new EventHandler(this.ScanQRCodeItem_Click))
  183. }),
  184. CreateMenuGroup("PAC ", new MenuItem[] {
  185. this.localPACItem = CreateMenuItem("Local PAC", new EventHandler(this.LocalPACItem_Click)),
  186. this.onlinePACItem = CreateMenuItem("Online PAC", new EventHandler(this.OnlinePACItem_Click)),
  187. new MenuItem("-"),
  188. this.editLocalPACItem = CreateMenuItem("Edit Local PAC File...", new EventHandler(this.EditPACFileItem_Click)),
  189. this.updateFromGFWListItem = CreateMenuItem("Update Local PAC from GFWList", new EventHandler(this.UpdatePACFromGFWListItem_Click)),
  190. this.editGFWUserRuleItem = CreateMenuItem("Edit User Rule for GFWList...", new EventHandler(this.EditUserRuleFileForGFWListItem_Click)),
  191. this.editOnlinePACItem = CreateMenuItem("Edit Online PAC URL...", new EventHandler(this.UpdateOnlinePACURLItem_Click)),
  192. }),
  193. new MenuItem("-"),
  194. this.AutoStartupItem = CreateMenuItem("Start on Boot", new EventHandler(this.AutoStartupItem_Click)),
  195. this.ShareOverLANItem = CreateMenuItem("Allow Clients from LAN", new EventHandler(this.ShareOverLANItem_Click)),
  196. new MenuItem("-"),
  197. CreateMenuItem("Show Logs...", new EventHandler(this.ShowLogItem_Click)),
  198. CreateMenuGroup("Updates...", new MenuItem[] {
  199. CreateMenuItem("Check for Updates...", new EventHandler(this.checkUpdatesItem_Click)),
  200. new MenuItem("-"),
  201. this.autoCheckUpdatesToggleItem = CreateMenuItem("Check for Updates at Startup", new EventHandler(this.autoCheckUpdatesToggleItem_Click)),
  202. }),
  203. CreateMenuItem("About...", new EventHandler(this.AboutItem_Click)),
  204. new MenuItem("-"),
  205. CreateMenuItem("Quit", new EventHandler(this.Quit_Click))
  206. });
  207. }
  208. private void controller_ConfigChanged(object sender, EventArgs e)
  209. {
  210. LoadCurrentConfiguration();
  211. UpdateTrayIcon();
  212. }
  213. private void controller_EnableStatusChanged(object sender, EventArgs e)
  214. {
  215. enableItem.Checked = controller.GetConfigurationCopy().enabled;
  216. modeItem.Enabled = enableItem.Checked;
  217. }
  218. void controller_ShareOverLANStatusChanged(object sender, EventArgs e)
  219. {
  220. ShareOverLANItem.Checked = controller.GetConfigurationCopy().shareOverLan;
  221. }
  222. void controller_EnableGlobalChanged(object sender, EventArgs e)
  223. {
  224. globalModeItem.Checked = controller.GetConfigurationCopy().global;
  225. PACModeItem.Checked = !globalModeItem.Checked;
  226. }
  227. void controller_FileReadyToOpen(object sender, ShadowsocksController.PathEventArgs e)
  228. {
  229. string argument = @"/select, " + e.Path;
  230. System.Diagnostics.Process.Start("explorer.exe", argument);
  231. }
  232. void ShowBalloonTip(string title, string content, ToolTipIcon icon, int timeout)
  233. {
  234. _notifyIcon.BalloonTipTitle = title;
  235. _notifyIcon.BalloonTipText = content;
  236. _notifyIcon.BalloonTipIcon = icon;
  237. _notifyIcon.ShowBalloonTip(timeout);
  238. }
  239. void controller_UpdatePACFromGFWListError(object sender, System.IO.ErrorEventArgs e)
  240. {
  241. ShowBalloonTip(I18N.GetString("Failed to update PAC file"), e.GetException().Message, ToolTipIcon.Error, 5000);
  242. Logging.LogUsefulException(e.GetException());
  243. }
  244. void controller_UpdatePACFromGFWListCompleted(object sender, GFWListUpdater.ResultEventArgs e)
  245. {
  246. string result = e.Success ? I18N.GetString("PAC updated") : I18N.GetString("No updates found. Please report to GFWList if you have problems with it.");
  247. ShowBalloonTip(I18N.GetString("Shadowsocks"), result, ToolTipIcon.Info, 1000);
  248. }
  249. void updateChecker_CheckUpdateCompleted(object sender, EventArgs e)
  250. {
  251. if (updateChecker.NewVersionFound)
  252. {
  253. ShowBalloonTip(String.Format(I18N.GetString("Shadowsocks {0} Update Found"), updateChecker.LatestVersionNumber), I18N.GetString("Click here to update"), ToolTipIcon.Info, 5000);
  254. }
  255. else if (!_isStartupChecking)
  256. {
  257. ShowBalloonTip(I18N.GetString("Shadowsocks"), I18N.GetString("No update is available"), ToolTipIcon.Info, 5000);
  258. }
  259. _isStartupChecking = false;
  260. }
  261. void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
  262. {
  263. if (updateChecker.NewVersionFound)
  264. {
  265. updateChecker.NewVersionFound = false; /* Reset the flag */
  266. if (System.IO.File.Exists(updateChecker.LatestVersionLocalName))
  267. {
  268. string argument = "/select, \"" + updateChecker.LatestVersionLocalName + "\"";
  269. System.Diagnostics.Process.Start("explorer.exe", argument);
  270. }
  271. }
  272. }
  273. private void _notifyIcon_BalloonTipClosed(object sender, EventArgs e)
  274. {
  275. if (updateChecker.NewVersionFound)
  276. {
  277. updateChecker.NewVersionFound = false; /* Reset the flag */
  278. }
  279. }
  280. private void LoadCurrentConfiguration()
  281. {
  282. Configuration config = controller.GetConfigurationCopy();
  283. UpdateServersMenu();
  284. enableItem.Checked = config.enabled;
  285. modeItem.Enabled = config.enabled;
  286. globalModeItem.Checked = config.global;
  287. PACModeItem.Checked = !config.global;
  288. ShareOverLANItem.Checked = config.shareOverLan;
  289. AutoStartupItem.Checked = AutoStartup.Check();
  290. onlinePACItem.Checked = onlinePACItem.Enabled && config.useOnlinePac;
  291. localPACItem.Checked = !onlinePACItem.Checked;
  292. UpdatePACItemsEnabledStatus();
  293. UpdateUpdateMenu();
  294. }
  295. private void UpdateServersMenu()
  296. {
  297. var items = ServersItem.MenuItems;
  298. while (items[0] != SeperatorItem)
  299. {
  300. items.RemoveAt(0);
  301. }
  302. int i = 0;
  303. foreach (var strategy in controller.GetStrategies())
  304. {
  305. MenuItem item = new MenuItem(strategy.Name);
  306. item.Tag = strategy.ID;
  307. item.Click += AStrategyItem_Click;
  308. items.Add(i, item);
  309. i++;
  310. }
  311. int strategyCount = i;
  312. Configuration configuration = controller.GetConfigurationCopy();
  313. foreach (var server in configuration.configs)
  314. {
  315. MenuItem item = new MenuItem(server.FriendlyName());
  316. item.Tag = i - strategyCount;
  317. item.Click += AServerItem_Click;
  318. items.Add(i, item);
  319. i++;
  320. }
  321. foreach (MenuItem item in items)
  322. {
  323. if (item.Tag != null && (item.Tag.ToString() == configuration.index.ToString() || item.Tag.ToString() == configuration.strategy))
  324. {
  325. item.Checked = true;
  326. }
  327. }
  328. }
  329. private void ShowConfigForm()
  330. {
  331. if (configForm != null)
  332. {
  333. configForm.Activate();
  334. }
  335. else
  336. {
  337. configForm = new ConfigForm(controller);
  338. configForm.Show();
  339. configForm.FormClosed += configForm_FormClosed;
  340. }
  341. }
  342. private void ShowLogForms()
  343. {
  344. if (logForms.Count == 0)
  345. {
  346. LogForm f = new LogForm(controller, Logging.LogFilePath);
  347. f.Show();
  348. f.FormClosed += logForm_FormClosed;
  349. logForms.Add(f);
  350. logFormsVisible = true;
  351. }
  352. else
  353. {
  354. logFormsVisible = !logFormsVisible;
  355. foreach (LogForm f in logForms)
  356. {
  357. f.Visible = logFormsVisible;
  358. }
  359. }
  360. }
  361. void logForm_FormClosed(object sender, FormClosedEventArgs e)
  362. {
  363. logForms.Remove((LogForm)sender);
  364. }
  365. void configForm_FormClosed(object sender, FormClosedEventArgs e)
  366. {
  367. configForm = null;
  368. Utils.ReleaseMemory(true);
  369. if (_isFirstRun)
  370. {
  371. CheckUpdateForFirstRun();
  372. ShowFirstTimeBalloon();
  373. _isFirstRun = false;
  374. }
  375. }
  376. private void Config_Click(object sender, EventArgs e)
  377. {
  378. ShowConfigForm();
  379. }
  380. private void Quit_Click(object sender, EventArgs e)
  381. {
  382. controller.Stop();
  383. _notifyIcon.Visible = false;
  384. Application.Exit();
  385. }
  386. private void CheckUpdateForFirstRun()
  387. {
  388. Configuration config = controller.GetConfigurationCopy();
  389. if (!config.isDefault)
  390. {
  391. _isStartupChecking = true;
  392. updateChecker.CheckUpdate(config, 3000);
  393. }
  394. }
  395. private void ShowFirstTimeBalloon()
  396. {
  397. _notifyIcon.BalloonTipTitle = I18N.GetString("Shadowsocks is here");
  398. _notifyIcon.BalloonTipText = I18N.GetString("You can turn on/off Shadowsocks in the context menu");
  399. _notifyIcon.BalloonTipIcon = ToolTipIcon.Info;
  400. _notifyIcon.ShowBalloonTip(0);
  401. }
  402. private void AboutItem_Click(object sender, EventArgs e)
  403. {
  404. Process.Start("https://github.com/shadowsocks/shadowsocks-windows");
  405. }
  406. private void notifyIcon1_Click(object sender, MouseEventArgs e)
  407. {
  408. if (e.Button == MouseButtons.Left)
  409. {
  410. // TODO: show something interesting
  411. }
  412. else if (e.Button == MouseButtons.Middle)
  413. {
  414. ShowLogForms();
  415. }
  416. }
  417. private void notifyIcon1_DoubleClick(object sender, MouseEventArgs e)
  418. {
  419. if (e.Button == MouseButtons.Left)
  420. {
  421. ShowConfigForm();
  422. }
  423. }
  424. private void EnableItem_Click(object sender, EventArgs e)
  425. {
  426. controller.ToggleEnable(!enableItem.Checked);
  427. }
  428. private void GlobalModeItem_Click(object sender, EventArgs e)
  429. {
  430. controller.ToggleGlobal(true);
  431. }
  432. private void PACModeItem_Click(object sender, EventArgs e)
  433. {
  434. controller.ToggleGlobal(false);
  435. }
  436. private void ShareOverLANItem_Click(object sender, EventArgs e)
  437. {
  438. ShareOverLANItem.Checked = !ShareOverLANItem.Checked;
  439. controller.ToggleShareOverLAN(ShareOverLANItem.Checked);
  440. }
  441. private void EditPACFileItem_Click(object sender, EventArgs e)
  442. {
  443. controller.TouchPACFile();
  444. }
  445. private void UpdatePACFromGFWListItem_Click(object sender, EventArgs e)
  446. {
  447. controller.UpdatePACFromGFWList();
  448. }
  449. private void EditUserRuleFileForGFWListItem_Click(object sender, EventArgs e)
  450. {
  451. controller.TouchUserRuleFile();
  452. }
  453. private void AServerItem_Click(object sender, EventArgs e)
  454. {
  455. MenuItem item = (MenuItem)sender;
  456. controller.SelectServerIndex((int)item.Tag);
  457. }
  458. private void AStrategyItem_Click(object sender, EventArgs e)
  459. {
  460. MenuItem item = (MenuItem)sender;
  461. controller.SelectStrategy((string)item.Tag);
  462. }
  463. private void ShowLogItem_Click(object sender, EventArgs e)
  464. {
  465. LogForm f = new LogForm(controller, Logging.LogFilePath);
  466. f.Show();
  467. f.FormClosed += logForm_FormClosed;
  468. logForms.Add(f);
  469. }
  470. private void StatisticsConfigItem_Click(object sender, EventArgs e)
  471. {
  472. StatisticsStrategyConfigurationForm form = new StatisticsStrategyConfigurationForm(controller);
  473. form.Show();
  474. }
  475. private void QRCodeItem_Click(object sender, EventArgs e)
  476. {
  477. QRCodeForm qrCodeForm = new QRCodeForm(controller.GetQRCodeForCurrentServer());
  478. //qrCodeForm.Icon = this.Icon;
  479. // TODO
  480. qrCodeForm.Show();
  481. }
  482. private void ScanQRCodeItem_Click(object sender, EventArgs e)
  483. {
  484. foreach (Screen screen in Screen.AllScreens)
  485. {
  486. using (Bitmap fullImage = new Bitmap(screen.Bounds.Width,
  487. screen.Bounds.Height))
  488. {
  489. using (Graphics g = Graphics.FromImage(fullImage))
  490. {
  491. g.CopyFromScreen(screen.Bounds.X,
  492. screen.Bounds.Y,
  493. 0, 0,
  494. fullImage.Size,
  495. CopyPixelOperation.SourceCopy);
  496. }
  497. int maxTry = 10;
  498. for (int i = 0; i < maxTry; i++)
  499. {
  500. int marginLeft = (int)((double)fullImage.Width * i / 2.5 / maxTry);
  501. int marginTop = (int)((double)fullImage.Height * i / 2.5 / maxTry);
  502. Rectangle cropRect = new Rectangle(marginLeft, marginTop, fullImage.Width - marginLeft * 2, fullImage.Height - marginTop * 2);
  503. Bitmap target = new Bitmap(screen.Bounds.Width, screen.Bounds.Height);
  504. double imageScale = (double)screen.Bounds.Width / (double)cropRect.Width;
  505. using (Graphics g = Graphics.FromImage(target))
  506. {
  507. g.DrawImage(fullImage, new Rectangle(0, 0, target.Width, target.Height),
  508. cropRect,
  509. GraphicsUnit.Pixel);
  510. }
  511. var source = new BitmapLuminanceSource(target);
  512. var bitmap = new BinaryBitmap(new HybridBinarizer(source));
  513. QRCodeReader reader = new QRCodeReader();
  514. var result = reader.decode(bitmap);
  515. if (result != null)
  516. {
  517. var success = controller.AddServerBySSURL(result.Text);
  518. QRCodeSplashForm splash = new QRCodeSplashForm();
  519. if (success)
  520. {
  521. splash.FormClosed += splash_FormClosed;
  522. }
  523. else if (result.Text.StartsWith("http://") || result.Text.StartsWith("https://"))
  524. {
  525. _urlToOpen = result.Text;
  526. splash.FormClosed += openURLFromQRCode;
  527. }
  528. else
  529. {
  530. MessageBox.Show(I18N.GetString("Failed to decode QRCode"));
  531. return;
  532. }
  533. double minX = Int32.MaxValue, minY = Int32.MaxValue, maxX = 0, maxY = 0;
  534. foreach (ResultPoint point in result.ResultPoints)
  535. {
  536. minX = Math.Min(minX, point.X);
  537. minY = Math.Min(minY, point.Y);
  538. maxX = Math.Max(maxX, point.X);
  539. maxY = Math.Max(maxY, point.Y);
  540. }
  541. minX /= imageScale;
  542. minY /= imageScale;
  543. maxX /= imageScale;
  544. maxY /= imageScale;
  545. // make it 20% larger
  546. double margin = (maxX - minX) * 0.20f;
  547. minX += -margin + marginLeft;
  548. maxX += margin + marginLeft;
  549. minY += -margin + marginTop;
  550. maxY += margin + marginTop;
  551. splash.Location = new Point(screen.Bounds.X, screen.Bounds.Y);
  552. // we need a panel because a window has a minimal size
  553. // TODO: test on high DPI
  554. splash.TargetRect = new Rectangle((int)minX + screen.Bounds.X, (int)minY + screen.Bounds.Y, (int)maxX - (int)minX, (int)maxY - (int)minY);
  555. splash.Size = new Size(fullImage.Width, fullImage.Height);
  556. splash.Show();
  557. return;
  558. }
  559. }
  560. }
  561. }
  562. MessageBox.Show(I18N.GetString("No QRCode found. Try to zoom in or move it to the center of the screen."));
  563. }
  564. void splash_FormClosed(object sender, FormClosedEventArgs e)
  565. {
  566. ShowConfigForm();
  567. }
  568. void openURLFromQRCode(object sender, FormClosedEventArgs e)
  569. {
  570. Process.Start(_urlToOpen);
  571. }
  572. private void AutoStartupItem_Click(object sender, EventArgs e)
  573. {
  574. AutoStartupItem.Checked = !AutoStartupItem.Checked;
  575. if (!AutoStartup.Set(AutoStartupItem.Checked))
  576. {
  577. MessageBox.Show(I18N.GetString("Failed to update registry"));
  578. }
  579. }
  580. private void LocalPACItem_Click(object sender, EventArgs e)
  581. {
  582. if (!localPACItem.Checked)
  583. {
  584. localPACItem.Checked = true;
  585. onlinePACItem.Checked = false;
  586. controller.UseOnlinePAC(false);
  587. UpdatePACItemsEnabledStatus();
  588. }
  589. }
  590. private void OnlinePACItem_Click(object sender, EventArgs e)
  591. {
  592. if (!onlinePACItem.Checked)
  593. {
  594. if (controller.GetConfigurationCopy().pacUrl.IsNullOrEmpty())
  595. {
  596. UpdateOnlinePACURLItem_Click(sender, e);
  597. }
  598. if (!controller.GetConfigurationCopy().pacUrl.IsNullOrEmpty())
  599. {
  600. localPACItem.Checked = false;
  601. onlinePACItem.Checked = true;
  602. controller.UseOnlinePAC(true);
  603. }
  604. UpdatePACItemsEnabledStatus();
  605. }
  606. }
  607. private void UpdateOnlinePACURLItem_Click(object sender, EventArgs e)
  608. {
  609. string origPacUrl = controller.GetConfigurationCopy().pacUrl;
  610. string pacUrl = Microsoft.VisualBasic.Interaction.InputBox(
  611. I18N.GetString("Please input PAC Url"),
  612. I18N.GetString("Edit Online PAC URL"),
  613. origPacUrl, -1, -1);
  614. if (!pacUrl.IsNullOrEmpty() && pacUrl != origPacUrl)
  615. {
  616. controller.SavePACUrl(pacUrl);
  617. }
  618. }
  619. private void UpdatePACItemsEnabledStatus()
  620. {
  621. if (this.localPACItem.Checked)
  622. {
  623. this.editLocalPACItem.Enabled = true;
  624. this.updateFromGFWListItem.Enabled = true;
  625. this.editGFWUserRuleItem.Enabled = true;
  626. this.editOnlinePACItem.Enabled = false;
  627. }
  628. else
  629. {
  630. this.editLocalPACItem.Enabled = false;
  631. this.updateFromGFWListItem.Enabled = false;
  632. this.editGFWUserRuleItem.Enabled = false;
  633. this.editOnlinePACItem.Enabled = true;
  634. }
  635. }
  636. private void UpdateUpdateMenu()
  637. {
  638. Configuration configuration = controller.GetConfigurationCopy();
  639. autoCheckUpdatesToggleItem.Checked = configuration.autoCheckUpdate;
  640. }
  641. private void autoCheckUpdatesToggleItem_Click(object sender, EventArgs e)
  642. {
  643. Configuration configuration = controller.GetConfigurationCopy();
  644. controller.ToggleCheckingUpdate(!configuration.autoCheckUpdate);
  645. UpdateUpdateMenu();
  646. }
  647. private void checkUpdatesItem_Click(object sender, EventArgs e)
  648. {
  649. updateChecker.CheckUpdate(controller.GetConfigurationCopy());
  650. }
  651. }
  652. }