|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- using Shadowsocks.Controller;
- using Shadowsocks.Model;
- using Shadowsocks.Properties;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
-
- namespace Shadowsocks.View
- {
- public class MenuViewController
- {
- // yes this is just a menu view controller
- // when config form is closed, it moves away from RAM
- // and it should just do anything related to the config form
-
- private ShadowsocksController controller;
- private UpdateChecker updateChecker;
-
- private NotifyIcon _notifyIcon;
- private ContextMenu contextMenu1;
-
- private bool _isFirstRun;
- private MenuItem enableItem;
- private MenuItem AutoStartupItem;
- private MenuItem ShareOverLANItem;
- private MenuItem SeperatorItem;
- private MenuItem ConfigItem;
- private MenuItem ServersItem;
- private MenuItem globalModeItem;
- private MenuItem PACModeItem;
- private ConfigForm configForm;
-
- public MenuViewController(ShadowsocksController controller)
- {
- this.controller = controller;
-
- LoadMenu();
-
- controller.EnableStatusChanged += controller_EnableStatusChanged;
- controller.ConfigChanged += controller_ConfigChanged;
- controller.PACFileReadyToOpen += controller_PACFileReadyToOpen;
- controller.ShareOverLANStatusChanged += controller_ShareOverLANStatusChanged;
- controller.EnableGlobalChanged += controller_EnableGlobalChanged;
- controller.Errored += controller_Errored;
- controller.UpdatePACFromGFWListCompleted += controller_UpdatePACFromGFWListCompleted;
- controller.UpdatePACFromGFWListError += controller_UpdatePACFromGFWListError;
-
- _notifyIcon = new NotifyIcon();
- UpdateTrayIcon();
- _notifyIcon.Visible = true;
- _notifyIcon.ContextMenu = contextMenu1;
- _notifyIcon.MouseDoubleClick += notifyIcon1_DoubleClick;
-
- this.updateChecker = new UpdateChecker();
- updateChecker.NewVersionFound += updateChecker_NewVersionFound;
-
- LoadCurrentConfiguration();
-
- updateChecker.CheckUpdate();
-
- if (controller.GetConfiguration().isDefault)
- {
- _isFirstRun = true;
- ShowConfigForm();
- }
- }
-
- void controller_Errored(object sender, System.IO.ErrorEventArgs e)
- {
- MessageBox.Show(e.GetException().ToString(), String.Format(I18N.GetString("Shadowsocks Error: {0}"), e.GetException().Message));
- }
-
- private void UpdateTrayIcon()
- {
- int dpi;
- Graphics graphics = Graphics.FromHwnd(IntPtr.Zero);
- dpi = (int)graphics.DpiX;
- graphics.Dispose();
- Bitmap icon = null;
- if (dpi < 97)
- {
- // dpi = 96;
- icon = Resources.ss16;
- }
- else if (dpi < 121)
- {
- // dpi = 120;
- icon = Resources.ss20;
- }
- else
- {
- icon = Resources.ss24;
- }
- bool enabled = controller.GetConfiguration().enabled;
- if (!enabled)
- {
- Bitmap iconCopy = new Bitmap(icon);
- for (int x = 0; x < iconCopy.Width; x++)
- {
- for (int y = 0; y < iconCopy.Height; y++)
- {
- Color color = icon.GetPixel(x, y);
- iconCopy.SetPixel(x, y , Color.FromArgb((byte)(color.A / 1.25), color.R, color.G, color.B));
- }
- }
- icon = iconCopy;
- }
- _notifyIcon.Icon = Icon.FromHandle(icon.GetHicon());
-
- string text = I18N.GetString("Shadowsocks") + " " + UpdateChecker.Version + "\n" + (enabled ? I18N.GetString("Enabled") : I18N.GetString("Disabled")) + "\n" + controller.GetCurrentServer().FriendlyName();
- _notifyIcon.Text = text.Substring(0, Math.Min(63, text.Length));
- }
-
- private MenuItem CreateMenuItem(string text, EventHandler click)
- {
- return new MenuItem(I18N.GetString(text), click);
- }
-
- private MenuItem CreateMenuGroup(string text, MenuItem[] items)
- {
- return new MenuItem(I18N.GetString(text), items);
- }
-
- private void LoadMenu()
- {
- this.contextMenu1 = new ContextMenu(new MenuItem[] {
- this.enableItem = CreateMenuItem("Enable", new EventHandler(this.EnableItem_Click)),
- CreateMenuGroup("Mode", new MenuItem[] {
- this.PACModeItem = CreateMenuItem("PAC", new EventHandler(this.PACModeItem_Click)),
- this.globalModeItem = CreateMenuItem("Global", new EventHandler(this.GlobalModeItem_Click))
- }),
- this.ServersItem = CreateMenuGroup("Servers", new MenuItem[] {
- this.SeperatorItem = new MenuItem("-"),
- this.ConfigItem = CreateMenuItem("Edit Servers...", new EventHandler(this.Config_Click))
- }),
- new MenuItem("-"),
- this.AutoStartupItem = CreateMenuItem("Start on Boot", new EventHandler(this.AutoStartupItem_Click)),
- this.ShareOverLANItem = CreateMenuItem("Share over LAN", new EventHandler(this.ShareOverLANItem_Click)),
- CreateMenuItem("Edit PAC File...", new EventHandler(this.EditPACFileItem_Click)),
- CreateMenuItem("Update PAC from GFWList", new EventHandler(this.UpdatePACFromGFWListItem_Click)),
- new MenuItem("-"),
- CreateMenuItem("Show QRCode...", new EventHandler(this.QRCodeItem_Click)),
- CreateMenuItem("Show Logs...", new EventHandler(this.ShowLogItem_Click)),
- CreateMenuItem("About...", new EventHandler(this.AboutItem_Click)),
- new MenuItem("-"),
- CreateMenuItem("Quit", new EventHandler(this.Quit_Click))
- });
- }
-
- private void controller_ConfigChanged(object sender, EventArgs e)
- {
- LoadCurrentConfiguration();
- UpdateTrayIcon();
- }
-
- private void controller_EnableStatusChanged(object sender, EventArgs e)
- {
- enableItem.Checked = controller.GetConfiguration().enabled;
- }
-
- void controller_ShareOverLANStatusChanged(object sender, EventArgs e)
- {
- ShareOverLANItem.Checked = controller.GetConfiguration().shareOverLan;
- }
-
- void controller_EnableGlobalChanged(object sender, EventArgs e)
- {
- globalModeItem.Checked = controller.GetConfiguration().global;
- PACModeItem.Checked = !globalModeItem.Checked;
- }
-
- void controller_PACFileReadyToOpen(object sender, ShadowsocksController.PathEventArgs e)
- {
- string argument = @"/select, " + e.Path;
-
- System.Diagnostics.Process.Start("explorer.exe", argument);
- }
-
- void ShowBalloonTip(string title, string content, ToolTipIcon icon, int timeout)
- {
- _notifyIcon.BalloonTipTitle = title;
- _notifyIcon.BalloonTipText = content;
- _notifyIcon.BalloonTipIcon = icon;
- _notifyIcon.ShowBalloonTip(timeout);
- }
-
- void controller_UpdatePACFromGFWListError(object sender, System.IO.ErrorEventArgs e)
- {
- ShowBalloonTip(I18N.GetString("Failed to update PAC file"), e.GetException().Message, ToolTipIcon.Error, 5000);
- Logging.LogUsefulException(e.GetException());
- }
-
- void controller_UpdatePACFromGFWListCompleted(object sender, EventArgs e)
- {
- ShowBalloonTip(I18N.GetString("Shadowsocks"), I18N.GetString("PAC updated"), ToolTipIcon.Info, 1000);
- }
-
- void updateChecker_NewVersionFound(object sender, EventArgs e)
- {
- ShowBalloonTip(String.Format(I18N.GetString("Shadowsocks {0} Update Found"), updateChecker.LatestVersionNumber), I18N.GetString("Click here to download"), ToolTipIcon.Info, 5000);
- _notifyIcon.BalloonTipClicked += notifyIcon1_BalloonTipClicked;
- _isFirstRun = false;
- }
-
- void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
- {
- System.Diagnostics.Process.Start(updateChecker.LatestVersionURL);
- _notifyIcon.BalloonTipClicked -= notifyIcon1_BalloonTipClicked;
- }
-
-
- private void LoadCurrentConfiguration()
- {
- Configuration config = controller.GetConfiguration();
- UpdateServersMenu();
- enableItem.Checked = config.enabled;
- globalModeItem.Checked = config.global;
- PACModeItem.Checked = !config.global;
- ShareOverLANItem.Checked = config.shareOverLan;
- AutoStartupItem.Checked = AutoStartup.Check();
- }
-
- private void UpdateServersMenu()
- {
- var items = ServersItem.MenuItems;
-
- items.Clear();
-
- Configuration configuration = controller.GetConfiguration();
- for (int i = 0; i < configuration.configs.Count; i++)
- {
- Server server = configuration.configs[i];
- MenuItem item = new MenuItem(server.FriendlyName());
- item.Tag = i;
- item.Click += AServerItem_Click;
- items.Add(item);
- }
- items.Add(SeperatorItem);
- items.Add(ConfigItem);
-
- if (configuration.index >= 0 && configuration.index < configuration.configs.Count)
- {
- items[configuration.index].Checked = true;
- }
- }
-
- private void ShowConfigForm()
- {
- if (configForm != null)
- {
- configForm.Activate();
- }
- else
- {
- configForm = new ConfigForm(controller);
- configForm.Show();
- configForm.FormClosed += configForm_FormClosed;
- }
- }
-
- void configForm_FormClosed(object sender, FormClosedEventArgs e)
- {
- configForm = null;
- Util.Utils.ReleaseMemory();
- ShowFirstTimeBalloon();
- }
-
- private void Config_Click(object sender, EventArgs e)
- {
- ShowConfigForm();
- }
-
- private void Quit_Click(object sender, EventArgs e)
- {
- controller.Stop();
- _notifyIcon.Visible = false;
- Application.Exit();
- }
-
- private void ShowFirstTimeBalloon()
- {
- if (_isFirstRun)
- {
- _notifyIcon.BalloonTipTitle = I18N.GetString("Shadowsocks is here");
- _notifyIcon.BalloonTipText = I18N.GetString("You can turn on/off Shadowsocks in the context menu");
- _notifyIcon.BalloonTipIcon = ToolTipIcon.Info;
- _notifyIcon.ShowBalloonTip(0);
- _isFirstRun = false;
- }
- }
-
- private void AboutItem_Click(object sender, EventArgs e)
- {
- Process.Start("https://github.com/shadowsocks/shadowsocks-csharp");
- }
-
- private void notifyIcon1_DoubleClick(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left)
- {
- ShowConfigForm();
- }
- }
-
- private void EnableItem_Click(object sender, EventArgs e)
- {
- controller.ToggleEnable(!enableItem.Checked);
- }
-
- private void GlobalModeItem_Click(object sender, EventArgs e)
- {
- controller.ToggleGlobal(true);
- }
-
- private void PACModeItem_Click(object sender, EventArgs e)
- {
- controller.ToggleGlobal(false);
- }
-
- private void ShareOverLANItem_Click(object sender, EventArgs e)
- {
- ShareOverLANItem.Checked = !ShareOverLANItem.Checked;
- controller.ToggleShareOverLAN(ShareOverLANItem.Checked);
- }
-
- private void EditPACFileItem_Click(object sender, EventArgs e)
- {
- controller.TouchPACFile();
- }
-
- private void UpdatePACFromGFWListItem_Click(object sender, EventArgs e)
- {
- controller.UpdatePACFromGFWList();
- }
-
- private void AServerItem_Click(object sender, EventArgs e)
- {
- MenuItem item = (MenuItem)sender;
- controller.SelectServerIndex((int)item.Tag);
- }
-
- private void ShowLogItem_Click(object sender, EventArgs e)
- {
- string argument = Logging.LogFile;
-
- System.Diagnostics.Process.Start("notepad.exe", argument);
- }
-
- private void QRCodeItem_Click(object sender, EventArgs e)
- {
- QRCodeForm qrCodeForm = new QRCodeForm(controller.GetQRCodeForCurrentServer());
- //qrCodeForm.Icon = this.Icon;
- // TODO
- qrCodeForm.Show();
- }
-
- private void ScanQRCodeItem_Click(object sender, EventArgs e)
- {
- /*
- using (Bitmap image = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
- Screen.PrimaryScreen.Bounds.Height))
- {
- using (Graphics g = Graphics.FromImage(bmpScreenCapture))
- {
- g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
- Screen.PrimaryScreen.Bounds.Y,
- 0, 0,
- bmpScreenCapture.Size,
- CopyPixelOperation.SourceCopy);
- }
- resultPoints.Clear();
- /* var reader = new BarcodeReader
- {
- PossibleFormats = new List<BarcodeFormat>
- {
- BarcodeFormat.QR_CODE
- }
- };
-
- var result = reader.Decode(image);
- var result = barcodeReader.Decode(image);
- var timerStart = DateTime.Now.Ticks;
- var timerStop = DateTime.Now.Ticks;
-
- if (result == null)
- {
- txtDecoderContent.Text = "No barcode recognized";
- }
- labDuration.Text = new TimeSpan(timerStop - timerStart).Milliseconds.ToString("0 ms");
-
- }
- }
- * */
- }
-
- private void AutoStartupItem_Click(object sender, EventArgs e) {
- AutoStartupItem.Checked = !AutoStartupItem.Checked;
- if (!AutoStartup.Set(AutoStartupItem.Checked)) {
- MessageBox.Show("Failed to edit registry");
- }
- }
- }
- }
|