frmConfig.pas
上传用户:youjie821
上传日期:2013-01-27
资源大小:459k
文件大小:8k
- unit frmConfig;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls, Mask, RzEdit, RzButton, RzRadChk, RzPanel, RzCmboBx,
- RzLabel, RzTabs, ActnList, ExtCtrls, RzDlgBtn, IniFiles;
- type
- TConfigForm = class(TForm)
- RzDialogButtons1: TRzDialogButtons;
- ActionList1: TActionList;
- actOk: TAction;
- actCancel: TAction;
- actHelp: TAction;
- actUserProxy: TAction;
- pcMain: TRzPageControl;
- RzTabSheet1: TRzTabSheet;
- RzLabel1: TRzLabel;
- cbxConnectType: TRzComboBox;
- RzGroupBox2: TRzGroupBox;
- Label4: TLabel;
- Label5: TLabel;
- Label6: TLabel;
- Label7: TLabel;
- chkProxy: TRzCheckBox;
- edtProxyIP: TRzEdit;
- edtProxyPort: TRzEdit;
- edtProxyPass: TRzEdit;
- edtProxyUser: TRzEdit;
- RzTabSheet2: TRzTabSheet;
- Label1: TLabel;
- Label8: TLabel;
- RzGroupBox1: TRzGroupBox;
- Label2: TLabel;
- Label3: TLabel;
- chkAuthentication: TRzCheckBox;
- edtUsername: TRzEdit;
- edtPassword: TRzEdit;
- edtServerIP: TRzEdit;
- edtServerPort: TRzEdit;
- procedure actOkExecute(Sender: TObject);
- procedure actCancelExecute(Sender: TObject);
- procedure actHelpExecute(Sender: TObject);
- procedure chkAuthenticationClick(Sender: TObject);
- procedure cbxConnectTypeChange(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure chkProxyClick(Sender: TObject);
- private
- { Private declarations }
- procedure EnabledControls(Value: Boolean);
- public
- { Public declarations }
- end;
- TConnectType = (ctDirect, ctNat, ctProxy);
- PConnectInfo = ^TConnectInfo;
- TConnectInfo = record
- ConnectType: TConnectType;
- ProxyIP: string;
- ProxyPort: Integer;
- ProxyUser: string;
- ProxyPass: string;
- end;
- TLiveUpdateInfo = record
- ServerIP: string;
- ServerPort: Integer;
- Username: string;
- Password: string;
- Project: string;
- end;
- TSettings = class(TObject)
- private
- FConnectInfo: TConnectInfo;
- FLiveUpdateInfo: TLiveUpdateInfo;
- FIniFile: TIniFile;
- procedure SaveToFile;
- procedure LoadFromFile;
- public
- constructor Create(FileName: string);
- destructor Destroy; override;
- property ConnectInfo: TConnectInfo read FConnectInfo;
- property LiveUpdateInfo: TLiveUpdateInfo read FLiveUpdateInfo;
- end;
- TApp = class(TObject)
- private
- FSettings: TSettings;
- public
- constructor Create;
- destructor Destroy; override;
- property Settings: TSettings read FSettings;
- end;
- function GetApp: TApp;
- procedure ShowConfigForm;
- implementation
- {$R *.dfm}
- var
- G_App: TApp = nil;
- function GetApp;
- begin
- if G_App = nil then
- G_App:= TApp.Create;
- Result:= G_App;
- end;
- procedure ShowConfigForm;
- begin
- with TConfigForm.Create(nil) do
- try
- ShowModal;
- finally
- Free;
- end;
- end;
- procedure TConfigForm.actOkExecute(Sender: TObject);
- begin
- ModalResult:= mrOk;
- end;
- procedure TConfigForm.actCancelExecute(Sender: TObject);
- begin
- ModalResult:= mrCancel;
- end;
- procedure TConfigForm.actHelpExecute(Sender: TObject);
- begin
- MessageBox(0, '显示 LiveUpdate 的帮助信息', '提示', MB_ICONINFORMATION or MB_OK)
- end;
- procedure TConfigForm.chkAuthenticationClick(Sender: TObject);
- begin
- edtUsername.Enabled:= chkAuthentication.Checked;
- edtPassword.Enabled:= chkAuthentication.Checked;
- end;
- procedure TConfigForm.cbxConnectTypeChange(Sender: TObject);
- begin
- chkProxy.Enabled:= cbxConnectType.ItemIndex = 1;
- EnabledControls(chkProxy.Enabled);
- end;
- procedure TConfigForm.FormCreate(Sender: TObject);
- begin
- with GetApp.Settings.ConnectInfo do
- begin
- case ConnectType of
- ctDirect:
- begin
- cbxConnectType.ItemIndex:= 0;
- chkProxy.Enabled:= False;
- end;
- ctNat:
- begin
- cbxConnectType.ItemIndex:= 1;
- chkProxy.Enabled:= True;
- EnabledControls(False);
- end;
- ctProxy:
- begin
- cbxConnectType.ItemIndex:= 1;
- chkProxy.Enabled:= True;
- EnabledControls(True);
- end;
- end;
- edtProxyIP.Text:= ProxyIP;
- edtProxyPort.Text:= IntToStr(ProxyPort);
- edtProxyUser.Text:= ProxyUser;
- edtProxyPass.Text:= ProxyPass;
- end;
- with GetApp.Settings.LiveUpdateInfo do
- begin
- edtServerIP.Text:= ServerIP;
- edtServerPort.Text:= IntToStr(ServerPort);
- edtUsername.Text:= Username;
- edtPassword.Text:= Password;
- end;
- pcMain.ActivePageIndex:= 0;
- end;
- procedure TConfigForm.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- if ModalResult = mrOk then
- begin
- with GetApp.Settings.ConnectInfo do
- begin
- case cbxConnectType.ItemIndex of
- 0: ConnectType:= ctDirect;
- 1:
- begin
- if chkProxy.Checked then
- begin
- ConnectType:= ctProxy;
- ProxyIP:= edtProxyIP.Text;
- ProxyPort:= StrToInt(edtProxyPort.Text);
- ProxyUser:= edtProxyUser.Text;
- ProxyPass:= edtProxyPass.Text;
- end
- else
- ConnectType:= ctNat;
- end;
- end;
- end;
- with GetApp.Settings.LiveUpdateInfo do
- begin
- ServerIP:= edtServerIP.Text;
- ServerPort:= StrToInt(edtServerPort.Text);
- if chkAuthentication.Checked then
- begin
- Username:= edtUsername.Text;
- Password:= edtPassword.Text;
- end
- else
- begin
- Username:= '';
- Password:= '';
- end;
- end;
- end;
- end;
- procedure TConfigForm.chkProxyClick(Sender: TObject);
- begin
- edtProxyIP.Enabled:= chkProxy.Checked;
- edtProxyPort.Enabled:= chkProxy.Checked;
- edtProxyUser.Enabled:= chkProxy.Checked;
- edtProxyPass.Enabled:= chkProxy.Checked;
- end;
- { TSettings }
- constructor TSettings.Create(FileName: string);
- begin
- FIniFile:= TIniFile.Create(FileName);
- LoadFromFile;
- end;
- destructor TSettings.Destroy;
- begin
- SaveToFile;
- FIniFile.Free;
- inherited Destroy;
- end;
- procedure TSettings.LoadFromFile;
- begin
- with FConnectInfo do
- begin
- ConnectType:= TConnectType(FIniFile.ReadInteger('Connection', 'ConnectType', 0));
- ProxyIP:= FIniFile.ReadString('Connection', 'ProxyIP', '');
- ProxyPort:= FIniFile.ReadInteger('Connection', 'ProxyPort', 0);
- ProxyUser:= FIniFile.ReadString('Connection', 'ProxyUser', '');
- ProxyPass:= FIniFile.ReadString('Connection', 'ProxyPass', '');
- end;
- with FLiveUpdateInfo do
- begin
- ServerIP:= FIniFile.ReadString('LiveUpdate', 'ServerIP', '');
- ServerPort:= FIniFile.ReadInteger('LiveUpdate', 'ServerPort', 0);
- Username:= FIniFile.ReadString('LiveUpdate', 'Username', '');
- Password:= FIniFile.ReadString('LiveUpdate', 'Password', '');
- Project:= FIniFile.ReadString('LiveUpdate', 'Project', '短信客户端');
- end;
- end;
- procedure TSettings.SaveToFile;
- begin
- with FConnectInfo do
- begin
- FIniFile.WriteInteger('Connection', 'ConnectType', Integer(ConnectType));
- FIniFile.WriteString('Connection', 'ProxyIP', ProxyIP);
- FIniFile.WriteInteger('Connection', 'ProxyPort', ProxyPort);
- FIniFile.WriteString('Connection', 'ProxyUser', ProxyUser);
- FIniFile.WriteString('Connection', 'ProxyPass', ProxyPass);
- end;
- with FLiveUpdateInfo do
- begin
- FIniFile.WriteString('LiveUpdate', 'ServerIP', ServerIP);
- FIniFile.WriteInteger('LiveUpdate', 'ServerPort', ServerPort);
- FIniFile.WriteString('LiveUpdate', 'Username', Username);
- FIniFile.WriteString('LiveUpdate', 'Password', Password);
- FIniFile.WriteString('LiveUpdate', 'Project', Project);
- end;
- end;
- { TApp }
- constructor TApp.Create;
- begin
- FSettings:= TSettings.Create(ExtractFilePath(Application.ExeName) + 'Client.ini');
- end;
- destructor TApp.Destroy;
- begin
- FSettings.Free;
- inherited Destroy;
- end;
- procedure TConfigForm.EnabledControls(Value: Boolean);
- begin
- edtProxyIP.Enabled:= Value and chkProxy.Checked;
- edtProxyPort.Enabled:= Value and chkProxy.Checked;
- edtProxyUser.Enabled:= Value and chkProxy.Checked;
- edtProxyPass.Enabled:= Value and chkProxy.Checked;
- end;
- end.