Menu

[r2000]: / trunk / Src / FmProxyServerDlg.pas  Maximize  Restore  History

Download this file

355 lines (323 with data), 10.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
{
* FmProxyServerDlg.pas
*
* Implements a dialog box that enables users to specify (or remove) a proxy
* server for use by CodeSnip's web services.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is FmProxyServerDlg.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2009-2011 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit FmProxyServerDlg;
interface
uses
// Delphi
StdCtrls, Controls, ExtCtrls, Classes, StdActns, ActnList,
// Project
FmGenericOKDlg;
type
{
TProxyServerDlg:
Ddialog box that enables users to specify (or remove) a proxy server for use
by CodeSnip's web services.
}
TProxyServerDlg = class(TGenericOKDlg)
cbUseProxy: TCheckBox;
gbProxy: TGroupBox;
lblIPAddress: TLabel;
lblIPAddressReq: TLabel;
lblPassword1: TLabel;
lblPassword2: TLabel;
lblPort: TLabel;
lblPortReq: TLabel;
lblReqExplain: TLabel;
lblReqSymbol: TLabel;
lblUserName: TLabel;
edIPAddress: TEdit;
edPassword1: TEdit;
edPassword2: TEdit;
edPort: TEdit;
edUserName: TEdit;
alMain: TActionList;
actCut: TEditCut;
actCopy: TEditCopy;
actPaste: TEditPaste;
actSelectAll: TEditSelectAll;
procedure btnOKClick(Sender: TObject);
procedure edIPAddressKeyPress(Sender: TObject; var Key: Char);
procedure edPortKeyPress(Sender: TObject; var Key: Char);
procedure cbUseProxyClick(Sender: TObject);
strict private
procedure Validate;
{Validates data entered into controls.
}
procedure SaveData;
{Stores data entered in controls in settings.
}
procedure SetProxyCtrlState(const Flag: Boolean);
{Sets enabled state of all controls in "proxy server details" group box.
@param Flag [in] True if controls are to be enabled, False if to be
disabled.
}
strict protected
procedure ConfigForm; override;
{Configures form. Ensures correct font is used and sets password
character.
}
procedure InitForm; override;
{Initialises controls on form from values read from settings.
}
public
class function Execute(const AOwner: TComponent): Boolean;
{Creates and displays the proxy server dialog box.
@param AOwner [in] Component that owns the dialog box.
}
end;
implementation
uses
// Delphi
SysUtils, Windows, Character,
// Project
UConsts, UExceptions, UFontHelper, UMessageBox, USettings, UStructs,
USystemInfo, UStrUtils, UUtils;
{$R *.dfm}
{ TProxyServerDlg }
procedure TProxyServerDlg.btnOKClick(Sender: TObject);
{Handles OK button click. Validates entered data and saves proxy information
in settings. Handles any errors.
@param Sender [in] Not used.
}
begin
try
ModalResult := mrNone;
Validate;
SaveData;
ModalResult := mrOK;
except
on E: EDataEntry do
begin
if Assigned(E.Ctrl) then
begin
E.Ctrl.SetFocus;
TMessageBox.Error(Self, E.Message);
end;
end;
end;
end;
procedure TProxyServerDlg.cbUseProxyClick(Sender: TObject);
{Handles click on "Use proxy server" check box. Updates state of other
controls depending on whether check box is checked.
@param Sender [in] Not used.
}
begin
inherited;
SetProxyCtrlState(cbUseProxy.Checked);
end;
procedure TProxyServerDlg.ConfigForm;
{Configures form. Ensures correct font is used and sets password character.
}
begin
inherited;
TFontHelper.SetDefaultBaseFont(lblIPAddressReq.Font, False);
TFontHelper.SetDefaultBaseFont(lblPortReq.Font, False);
TFontHelper.SetDefaultBaseFont(lblReqSymbol.Font, False);
if TOSInfo.IsVistaOrLater then
begin
edPassword1.PasswordChar := '•';
edPassword2.PasswordChar := '•';
end
else
begin
edPassword1.PasswordChar := '*';
edPassword2.PasswordChar := '*';
end;
end;
procedure TProxyServerDlg.edIPAddressKeyPress(Sender: TObject; var Key: Char);
{Filters keypresses in IP address edit box. Permits only numbers or dots to be
entered.
@param Sender [in] Not used.
@param Key [in/out] Key pressed. Set to #0 if key is not permitted.
}
const
cDot = '.'; // dot separator (not decimal point)
begin
if not TCharacter.IsDigit(Key) and (Key <> cDot) and (Key <> BACKSPACE) then
Key := #0
else if (Key = cDot) and (
(edIPAddress.SelStart = 0) or (StrCountDelims(cDot, edIPAddress.Text) = 3)
) then
Key := #0;
if Key = #0 then
KeyErrorBeep;
end;
procedure TProxyServerDlg.edPortKeyPress(Sender: TObject; var Key: Char);
{Filters keypresses in Port edit box. Permits only numbers to be entered.
@param Sender [in] Not used.
@param Key [in/out] Key pressed. Set to #0 if key is not permitted.
}
begin
if not TCharacter.IsDigit(Key) and (Key <> BACKSPACE) then
Key := #0;
if Key = #0 then
KeyErrorBeep;
end;
class function TProxyServerDlg.Execute(const AOwner: TComponent): Boolean;
{Creates and displays the proxy server dialog box.
@param AOwner [in] Component that owns the dialog box.
}
begin
with Create(AOwner) do
try
Result := ShowModal = mrOK;
finally
Free;
end;
end;
procedure TProxyServerDlg.InitForm;
{Initialises controls on form from values read from settings.
}
var
Section: ISettingsSection; // settings section containing current settings
begin
inherited;
// init control contents from proxy server settings
Section := Settings.ReadSection(ssProxyServer);
cbUseProxy.Checked := Boolean(StrToIntDef(Section.ItemValues['UseProxy'], 0));
edIPAddress.Text := Section.ItemValues['IPAddress'];
edPort.Text := Section.ItemValues['Port'];
edUserName.Text := Section.ItemValues['UserName'];
edPassword1.Text := Section.GetEncryptedItemValue('Password');
edPassword2.Text := edPassword1.Text;
// init control state
SetProxyCtrlState(cbUseProxy.Checked);
end;
procedure TProxyServerDlg.SaveData;
{Stores data entered in controls in settings.
}
var
Section: ISettingsSection; // settings section to receive data
begin
Section := Settings.EmptySection(ssProxyServer);
Section.ItemValues['UseProxy'] := IntToStr(Ord(cbUseProxy.Checked));
Section.ItemValues['IPAddress'] := edIPAddress.Text;
Section.ItemValues['Port'] := edPort.Text;
Section.ItemValues['UserName'] := edUserName.Text;
Section.SetEncryptedItemValue('Password', edPassword1.Text);
Section.Save;
end;
procedure TProxyServerDlg.SetProxyCtrlState(const Flag: Boolean);
{Sets enabled state of all controls in "proxy server details" group box.
@param Flag [in] True if controls are to be enabled, False if to be
disabled.
}
var
Idx: Integer; // loops through all child controls of group box
begin
for Idx := 0 to Pred(gbProxy.ControlCount) do
gbProxy.Controls[Idx].Enabled := Flag;
gbProxy.Enabled := Flag;
end;
procedure TProxyServerDlg.Validate;
{Validates data entered into controls.
}
// ---------------------------------------------------------------------------
function IsValidIPAddress(const Addr: string): Boolean;
{Checks if an IP address has valid format.
@param Addr [in] IP address to check.
@return True if IP address has valid format, False otherwise.
}
var
Quads: TStringList; // quads of IP address
Quad: string; // a quad
IQuad: Integer; // integer value of a quad
const
IPQuadRange: TRange = (Min: 0; Max: 255); // range of valid IP quads
begin
Result := False;
Quads := TStringList.Create;
try
// split IP address into quads (they are separated by dots)
StrExplode(Addr, '.', Quads);
if Quads.Count <> 4 then
Exit; // must be 4 quads
for Quad in Quads do
begin
if not TryStrToInt(Quad, IQuad) then
Exit; // each quad must be an integer
if not IPQuadRange.Contains(IQuad) then
Exit; // each quad must be storable in a byte
end;
finally
FreeAndNil(Quads);
end;
Result := True;
end;
function IsValidPort(const Port: string): Boolean;
{Checks if a port is valid.
@param Port [in] String representation of port to be checked.
@return True if port is valid, False otherwise.
}
var
IPort: Integer; // port nunber as integer
const
PortNumbers: TRange = (Min: 0; Max: 65535); // range of valid port numbers
begin
Result := False;
// check port is a valid number
if not TryStrToInt(Port, IPort) then
Exit;
// check port in range (see http://www.iana.org/assignments/port-numbers)
if not PortNumbers.Contains(IPort) then
Exit;
Result := True;
end;
// ---------------------------------------------------------------------------
resourcestring
// Error messages
sMissingIPAddress = 'IP address must be specified';
sInvalidIPAddress = 'Invalid IP address';
sMissingPort = 'Port must be specified';
sInvalidPort = 'Port must be in range 0..65535';
sMissingUserName = 'User name must be specified';
sPasswordMismatch = 'Passwords are not the same. Please re-enter.';
begin
if not cbUseProxy.Checked then
Exit;
if edIPAddress.Text = '' then
raise EDataEntry.Create(sMissingIPAddress, edIPAddress);
if not IsValidIPAddress(edIPAddress.Text) then
raise EDataEntry.Create(sInvalidIPAddress, edIPAddress);
if edPort.Text = '' then
raise EDataEntry.Create(sMissingPort, edPort);
if not IsValidPort(edPort.Text) then
raise EDataEntry.Create(sInvalidPort, edPort);
if (edPassword1.Text <> '') and (edUserName.Text = '') then
raise EDataEntry.Create(sMissingUserName, edUserName);
if edPassword1.Text <> edPassword2.Text then
raise EDataEntry.Create(sPasswordMismatch, edPassword1);
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.