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.

ProxyForm.cs 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using Shadowsocks.Controller;
  5. using Shadowsocks.Model;
  6. using Shadowsocks.Properties;
  7. namespace Shadowsocks.View
  8. {
  9. public partial class ProxyForm : Form
  10. {
  11. private ShadowsocksController controller;
  12. // this is a copy of configuration that we are working on
  13. private Configuration _modifiedConfiguration;
  14. public ProxyForm(ShadowsocksController controller)
  15. {
  16. this.Font = System.Drawing.SystemFonts.MessageBoxFont;
  17. InitializeComponent();
  18. UpdateTexts();
  19. this.Icon = Icon.FromHandle(Resources.ssw128.GetHicon());
  20. this.controller = controller;
  21. controller.ConfigChanged += controller_ConfigChanged;
  22. UpdateEnabled();
  23. LoadCurrentConfiguration();
  24. }
  25. private void UpdateTexts()
  26. {
  27. UseProxyCheckBox.Text = I18N.GetString("Use Proxy");
  28. ProxyAddrLabel.Text = I18N.GetString("Proxy Addr");
  29. ProxyPortLable.Text = I18N.GetString("Proxy Port");
  30. OKButton.Text = I18N.GetString("OK");
  31. MyCancelButton.Text = I18N.GetString("Cancel");
  32. this.Text = I18N.GetString("Edit Proxy");
  33. }
  34. private void controller_ConfigChanged(object sender, EventArgs e)
  35. {
  36. LoadCurrentConfiguration();
  37. }
  38. private void LoadCurrentConfiguration()
  39. {
  40. _modifiedConfiguration = controller.GetConfigurationCopy();
  41. UseProxyCheckBox.Checked = _modifiedConfiguration.useProxy;
  42. ProxyServerTextBox.Text = _modifiedConfiguration.proxyServer;
  43. ProxyPortTextBox.Text = _modifiedConfiguration.proxyPort.ToString();
  44. }
  45. private void OKButton_Click(object sender, EventArgs e)
  46. {
  47. if (UseProxyCheckBox.Checked)
  48. {
  49. try
  50. {
  51. var proxy = ProxyServerTextBox.Text;
  52. var port = int.Parse(ProxyPortTextBox.Text);
  53. Configuration.CheckServer(proxy);
  54. Configuration.CheckPort(port);
  55. controller.EnableProxy(proxy, port);
  56. }
  57. catch (FormatException)
  58. {
  59. MessageBox.Show(I18N.GetString("Illegal port number format"));
  60. return;
  61. }
  62. catch (Exception ex)
  63. {
  64. MessageBox.Show(ex.Message);
  65. return;
  66. }
  67. }
  68. else
  69. {
  70. controller.DisableProxy();
  71. }
  72. this.Close();
  73. }
  74. private void CancelButton_Click(object sender, EventArgs e)
  75. {
  76. this.Close();
  77. }
  78. private void ProxyForm_FormClosed(object sender, FormClosedEventArgs e)
  79. {
  80. controller.ConfigChanged -= controller_ConfigChanged;
  81. }
  82. private void UseProxyCheckBox_CheckedChanged(object sender, EventArgs e)
  83. {
  84. UpdateEnabled();
  85. }
  86. private void UpdateEnabled()
  87. {
  88. if (UseProxyCheckBox.Checked)
  89. {
  90. ProxyServerTextBox.Enabled = true;
  91. ProxyPortTextBox.Enabled = true;
  92. }
  93. else
  94. {
  95. ProxyServerTextBox.Enabled = false;
  96. ProxyPortTextBox.Enabled = false;
  97. }
  98. }
  99. }
  100. }