介绍
本系列文章介绍请看:概述
本文旨在搞清楚以下问题:
- 代理服务器配置管理,添加,删除,修改,获取
加载服务器列表
Call Stack
/View/ConfigForm.cs:
ConfigForm::ConfigForm -> ConfigForm::LoadCurrentConfiguration -> ConfigForm::LoadServerNameListToUI
private void LoadCurrentConfiguration()
{
_modifiedConfiguration = controller.GetConfigurationCopy();
LoadServerNameListToUI(_modifiedConfiguration);
_lastSelectedIndex = _modifiedConfiguration.index;
if (_lastSelectedIndex < 0 || _lastSelectedIndex >= ServersListBox.Items.Count)
{
_lastSelectedIndex = 0;
}
ServersListBox.SelectedIndex = _lastSelectedIndex;
UpdateButtons();
LoadSelectedServerDetails();
ProxyPortTextBox.Text = _modifiedConfiguration.localPort.ToString();
PortableModeCheckBox.Checked = _modifiedConfiguration.portableMode;
}
// 逐行加载到ServersListBoxk列表控件
private void LoadServerNameListToUI(Configuration configuration)
{
ServersListBox.Items.Clear();
foreach (Server server in configuration.configs)
{
ServersListBox.Items.Add(server.FriendlyName());
}
}
添加服务器分析
拍脑门可知,创建流程应该是很简单的。读取form中的数据,然后创建Server对象,加入配置对象config即可。
具体怎么实现,还是得看代码说话
Call Stack
/View/ConfigForm.cs : OKButton_Click -> SaveValidConfiguration
private bool SaveValidConfiguration()
{
/*
根据form里填写的参数,构建server对象: Server server = GetServerDetailsFromUI();
检查配置:Configuration.CheckServer(server);
加入_modifiedConfiguration: _modifiedConfiguration.configs[_lastSelectedIndex] = server;
*/
if (!ValidateAndSaveSelectedServerDetails())
{
return false;
}
// 添加配置之后,count必然大于0
if (_modifiedConfiguration.configs.Count == 0)
{
MessageBox.Show(I18N.GetString("Please add at least one server"));
return false;
}
// 检查端口合法性,0-65535,不能用8123
int localPort = int.Parse(ProxyPortTextBox.Text);
Configuration.CheckLocalPort(localPort);
_modifiedConfiguration.localPort = localPort;
// 是否开启便携模式
_modifiedConfiguration.portableMode = PortableModeCheckBox.Checked;
// 通过Configuration.Save方法把当前最新配置写入配置文件
controller.SaveServers(_modifiedConfiguration.configs, _modifiedConfiguration.localPort, _modifiedConfiguration.portableMode);
// 设置当前代理服务器索引号
controller.SelectServerIndex(ServersListBox.SelectedIndex);
return true;
}