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.

ConfigForm.cs 7.3 kB

12 years ago
10 years ago
12 years ago
10 years ago
12 years ago
10 years ago
12 years ago
10 years ago
10 years ago
10 years ago
10 years ago
12 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
12 years ago
12 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
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. using System.Diagnostics;
  8. using Microsoft.Win32;
  9. using Shadowsocks.Controller;
  10. using Shadowsocks.Model;
  11. using Shadowsocks.Properties;
  12. namespace Shadowsocks.View
  13. {
  14. public partial class ConfigForm : Form
  15. {
  16. private ShadowsocksController controller;
  17. private UpdateChecker updateChecker;
  18. // this is a copy of configuration that we are working on
  19. private Configuration _modifiedConfiguration;
  20. private int _oldSelectedIndex = -1;
  21. private bool _isFirstRun;
  22. public ConfigForm(ShadowsocksController controller)
  23. {
  24. InitializeComponent();
  25. this.Icon = Icon.FromHandle(Resources.ssw128.GetHicon());
  26. this.controller = controller;
  27. controller.ConfigChanged += controller_ConfigChanged;
  28. LoadCurrentConfiguration();
  29. }
  30. private void controller_ConfigChanged(object sender, EventArgs e)
  31. {
  32. LoadCurrentConfiguration();
  33. }
  34. void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
  35. {
  36. Process.Start(updateChecker.LatestVersionURL);
  37. }
  38. private void ShowWindow()
  39. {
  40. this.Opacity = 1;
  41. this.Show();
  42. IPTextBox.Focus();
  43. }
  44. private bool SaveOldSelectedServer()
  45. {
  46. try
  47. {
  48. if (_oldSelectedIndex == -1 || _oldSelectedIndex >= _modifiedConfiguration.configs.Count)
  49. {
  50. return true;
  51. }
  52. Server server = new Server
  53. {
  54. server = IPTextBox.Text,
  55. server_port = int.Parse(ServerPortTextBox.Text),
  56. password = PasswordTextBox.Text,
  57. local_port = int.Parse(ProxyPortTextBox.Text),
  58. method = EncryptionSelect.Text,
  59. remarks = RemarksTextBox.Text
  60. };
  61. Configuration.CheckServer(server);
  62. _modifiedConfiguration.configs[_oldSelectedIndex] = server;
  63. return true;
  64. }
  65. catch (FormatException)
  66. {
  67. MessageBox.Show("illegal port number format");
  68. }
  69. catch (Exception ex)
  70. {
  71. MessageBox.Show(ex.Message);
  72. }
  73. return false;
  74. }
  75. private void LoadSelectedServer()
  76. {
  77. if (ServersListBox.SelectedIndex >= 0 && ServersListBox.SelectedIndex < _modifiedConfiguration.configs.Count)
  78. {
  79. Server server = _modifiedConfiguration.configs[ServersListBox.SelectedIndex];
  80. IPTextBox.Text = server.server;
  81. ServerPortTextBox.Text = server.server_port.ToString();
  82. PasswordTextBox.Text = server.password;
  83. ProxyPortTextBox.Text = server.local_port.ToString();
  84. EncryptionSelect.Text = server.method ?? "aes-256-cfb";
  85. RemarksTextBox.Text = server.remarks;
  86. ServerGroupBox.Visible = true;
  87. //IPTextBox.Focus();
  88. }
  89. else
  90. {
  91. ServerGroupBox.Visible = false;
  92. }
  93. }
  94. private void LoadConfiguration(Configuration configuration)
  95. {
  96. ServersListBox.Items.Clear();
  97. foreach (Server server in _modifiedConfiguration.configs)
  98. {
  99. ServersListBox.Items.Add(string.IsNullOrEmpty(server.server) ? "New server" : string.IsNullOrEmpty(server.remarks)? server.server + ":" + server.server_port : server.server + ":" + server.server_port + " (" + server.remarks + ")");
  100. }
  101. }
  102. private void LoadCurrentConfiguration()
  103. {
  104. _modifiedConfiguration = controller.GetConfiguration();
  105. LoadConfiguration(_modifiedConfiguration);
  106. _oldSelectedIndex = _modifiedConfiguration.index;
  107. ServersListBox.SelectedIndex = _modifiedConfiguration.index;
  108. LoadSelectedServer();
  109. }
  110. private void ConfigForm_Load(object sender, EventArgs e)
  111. {
  112. }
  113. private void ServersListBox_SelectedIndexChanged(object sender, EventArgs e)
  114. {
  115. if (_oldSelectedIndex == ServersListBox.SelectedIndex)
  116. {
  117. // we are moving back to oldSelectedIndex or doing a force move
  118. return;
  119. }
  120. if (!SaveOldSelectedServer())
  121. {
  122. // why this won't cause stack overflow?
  123. ServersListBox.SelectedIndex = _oldSelectedIndex;
  124. return;
  125. }
  126. LoadSelectedServer();
  127. _oldSelectedIndex = ServersListBox.SelectedIndex;
  128. }
  129. private void AddButton_Click(object sender, EventArgs e)
  130. {
  131. if (!SaveOldSelectedServer())
  132. {
  133. return;
  134. }
  135. Server server = Configuration.GetDefaultServer();
  136. _modifiedConfiguration.configs.Add(server);
  137. LoadConfiguration(_modifiedConfiguration);
  138. ServersListBox.SelectedIndex = _modifiedConfiguration.configs.Count - 1;
  139. _oldSelectedIndex = ServersListBox.SelectedIndex;
  140. }
  141. private void DeleteButton_Click(object sender, EventArgs e)
  142. {
  143. _oldSelectedIndex = ServersListBox.SelectedIndex;
  144. if (_oldSelectedIndex >= 0 && _oldSelectedIndex < _modifiedConfiguration.configs.Count)
  145. {
  146. _modifiedConfiguration.configs.RemoveAt(_oldSelectedIndex);
  147. }
  148. if (_oldSelectedIndex >= _modifiedConfiguration.configs.Count)
  149. {
  150. // can be -1
  151. _oldSelectedIndex = _modifiedConfiguration.configs.Count - 1;
  152. }
  153. ServersListBox.SelectedIndex = _oldSelectedIndex;
  154. LoadConfiguration(_modifiedConfiguration);
  155. ServersListBox.SelectedIndex = _oldSelectedIndex;
  156. LoadSelectedServer();
  157. }
  158. private void OKButton_Click(object sender, EventArgs e)
  159. {
  160. if (!SaveOldSelectedServer())
  161. {
  162. return;
  163. }
  164. if (_modifiedConfiguration.configs.Count == 0)
  165. {
  166. MessageBox.Show("Please add at least one server");
  167. return;
  168. }
  169. controller.SaveServers(_modifiedConfiguration.configs);
  170. this.Hide();
  171. }
  172. private void CancelButton_Click(object sender, EventArgs e)
  173. {
  174. this.Close();
  175. }
  176. private void ConfigForm_Shown(object sender, EventArgs e)
  177. {
  178. IPTextBox.Focus();
  179. }
  180. private void ConfigForm_FormClosed(object sender, FormClosedEventArgs e)
  181. {
  182. controller.ConfigChanged -= controller_ConfigChanged;
  183. }
  184. }
  185. }