/
wccurlclientcontrols.pas
496 lines (417 loc) · 12.5 KB
/
wccurlclientcontrols.pas
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
unit WCCurlClientControls;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, Controls, Graphics, ValEdit,
ComCtrls, StdCtrls, wccurlclient, Grids,
JSONPropStorage;
type
{ TWCClientPropStorage }
TWCClientPropStorage = class(TJSONPropStorage)
private
FDefaults : TStringList;
function GetDefaults(aIndex : Integer) : String;
function GetDevice : String;
function GetHostName : String;
function GetMetadata : String;
function GetPassword : String;
function GetProxy : String;
function GetSID : String;
function GetUserName : String;
function GetProp(aPropName : Integer) : String;
function GetVerifyTLS : Boolean;
procedure SetDefaults(aIndex : Integer; AValue : String);
procedure SetDevice(AValue : String);
procedure SetHostName(AValue : String);
procedure SetMetadata(AValue : String);
procedure SetPassword(AValue : String);
procedure SetProxy(AValue : String);
procedure SetSID(AValue : String);
procedure SetUserName(AValue : String);
procedure SetProp(aPropName : Integer; const aValue : String);
procedure SetVerifyTLS(AValue : Boolean);
public
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
property Defaults[aIndex : Integer] : String read GetDefaults write SetDefaults;
const HOST_POS = 0;
const PROXY_POS = 1;
const USER_POS = 2;
const PWRD_POS = 3;
const DEVICE_POS = 4;
const META_POS = 5;
const SID_POS = 6;
const MAX_PROPS = SID_POS;
property HostName : String read GetHostName write SetHostName;
property Proxy : String read GetProxy write SetProxy;
property UserName : String read GetUserName write SetUserName;
property Password : String read GetPassword write SetPassword;
property Device : String read GetDevice write SetDevice;
property Metadata : String read GetMetadata write SetMetadata;
property SID : String read GetSID write SetSID;
property VerifyTLS : Boolean read GetVerifyTLS write SetVerifyTLS;
end;
{ TWCClientConfigEditor }
TWCClientConfigEditor = class(TCustomControl)
private
FCURLClient : TWCCURLClient;
FProps : TWCClientPropStorage;
FValues : TValueListEditor;
FVerifyTLS : TCheckBox;
function GetDevice : String;
function GetHostName : String;
function GetMetadata : String;
function GetPassword : String;
function GetProxy : String;
function GetSID : String;
function GetUserName : String;
function GetVerifyTLS : Boolean;
procedure SetCURLClient(AValue : TWCCURLClient);
procedure SetDevice(AValue : String);
procedure SetHostName(AValue : String);
procedure SetMetadata(AValue : String);
procedure SetPassword(AValue : String);
procedure SetProps(AValue : TWCClientPropStorage);
procedure SetProxy(AValue : String);
procedure SetSID(AValue : String);
procedure SetUserName(AValue : String);
procedure ValuesDrawCell(Sender : TObject; aCol, aRow : Integer;
aRect : TRect; aState : TGridDrawState);
procedure OptsEditingDone(Sender : TObject);
procedure VerifyTSLCBChange(Sender : TObject);
procedure SetVerifyTLS(AValue : Boolean);
public
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
procedure Apply;
procedure RestoreProps;
procedure SaveProps;
property Props : TWCClientPropStorage read FProps write SetProps;
property CURLClient : TWCCURLClient read FCURLClient write SetCURLClient;
property HostName : String read GetHostName write SetHostName;
property Proxy : String read GetProxy write SetProxy;
property UserName : String read GetUserName write SetUserName;
property Password : String read GetPassword write SetPassword;
property Device : String read GetDevice write SetDevice;
property Metadata : String read GetMetadata write SetMetadata;
property SID : String read GetSID write SetSID;
property VerifyTLS : Boolean read GetVerifyTLS write SetVerifyTLS;
end;
resourcestring
rsServer = 'Server';
rsProxy = 'Proxy';
rsUser = 'User name';
rsPwrd = 'Password';
rsDevice = 'Device';
rsMeta = 'Metadata';
rsSID = 'Session ID';
rsVerifyTLS = 'Verify TLS';
implementation
const
csServer = 'Server';
csProxy = 'Proxy';
csUser = 'User';
csPwrd = 'Password';
csDevice = 'Device';
csMeta = 'Metadata';
csSID = 'SID';
csVerifyTLS = 'VerifyTLS';
PROPS_STR : Array [0..6] of String = (csServer, csProxy, csUser, csPwrd,
csDevice, csMeta, csSID);
{ TWCClientConfigEditor }
procedure TWCClientConfigEditor.SetCURLClient(AValue : TWCCURLClient);
begin
if FCURLClient = AValue then Exit;
FCURLClient := AValue;
Apply;
end;
function TWCClientConfigEditor.GetDevice : String;
begin
Result := FValues.Cells[1, TWCClientPropStorage.DEVICE_POS];
end;
function TWCClientConfigEditor.GetHostName : String;
begin
Result := FValues.Cells[1, TWCClientPropStorage.HOST_POS];
end;
function TWCClientConfigEditor.GetMetadata : String;
begin
Result := FValues.Cells[1, TWCClientPropStorage.META_POS];
end;
function TWCClientConfigEditor.GetPassword : String;
begin
Result := FValues.Cells[1, TWCClientPropStorage.PWRD_POS];
end;
function TWCClientConfigEditor.GetProxy : String;
begin
Result := FValues.Cells[1, TWCClientPropStorage.PROXY_POS];
end;
function TWCClientConfigEditor.GetSID : String;
begin
Result := FValues.Cells[1, TWCClientPropStorage.SID_POS];
end;
function TWCClientConfigEditor.GetUserName : String;
begin
Result := FValues.Cells[1, TWCClientPropStorage.USER_POS];
end;
function TWCClientConfigEditor.GetVerifyTLS : Boolean;
begin
Result := FVerifyTLS.Checked;
end;
procedure TWCClientConfigEditor.SetDevice(AValue : String);
begin
FValues.Cells[1, TWCClientPropStorage.DEVICE_POS] := AValue;
end;
procedure TWCClientConfigEditor.SetHostName(AValue : String);
begin
FValues.Cells[1, TWCClientPropStorage.HOST_POS] := AValue;
end;
procedure TWCClientConfigEditor.SetMetadata(AValue : String);
begin
FValues.Cells[1, TWCClientPropStorage.META_POS] := AValue;
end;
procedure TWCClientConfigEditor.SetPassword(AValue : String);
begin
FValues.Cells[1, TWCClientPropStorage.PWRD_POS] := AValue;
end;
procedure TWCClientConfigEditor.SetProxy(AValue : String);
begin
FValues.Cells[1, TWCClientPropStorage.PROXY_POS] := AValue;
end;
procedure TWCClientConfigEditor.SetSID(AValue : String);
begin
FValues.Cells[1, TWCClientPropStorage.SID_POS] := AValue;
end;
procedure TWCClientConfigEditor.SetUserName(AValue : String);
begin
FValues.Cells[1, TWCClientPropStorage.USER_POS] := AValue;
end;
procedure TWCClientConfigEditor.ValuesDrawCell(Sender : TObject; aCol,
aRow : Integer; aRect : TRect; aState : TGridDrawState);
var
pen : TPen;
br : TBrush;
fnt : TFont;
ts : TTextStyle;
S : String;
SG : TValueListEditor;
begin
SG := TValueListEditor(Sender);
S := SG.Cells[aCol, aRow];
if (aCol = 1) and (aRow = TWCClientPropStorage.PWRD_POS) then
if Length(S) > 0 then
S := '****';
pen := SG.Canvas.Pen;
br := SG.Canvas.Brush;
fnt := SG.Canvas.Font;
ts := SG.Canvas.TextStyle;
ts.Alignment := taLeftJustify;
ts.Layout := tlCenter;
ts.Wordbreak := True;
ts.SingleLine := True;
ts.Opaque := false;
if aCol = 0 then
begin
br.Color := clBtnFace;
Pen.Style := psClear;
fnt.Color := clBtnText;
end else
begin
br.Color := clWindow;
if gdFixed in aState then br.Color := $DDDDDD;
Pen.Style := psClear;
fnt.Color := clWindowText;
if gdSelected in aState then
begin
br.Color := clHighlight;
fnt.Color := clHighlightText;
pen.Color := clHighlightText;
pen.Style := psDot;
end;
end;
SG.Canvas.Rectangle(aRect);
fnt.Style := [];
SG.Canvas.TextRect(aRect, aRect.Left + 2, aRect.Top + 2, S, ts);
end;
procedure TWCClientConfigEditor.SetProps(AValue : TWCClientPropStorage);
begin
if FProps = AValue then Exit;
FProps := AValue;
RestoreProps;
end;
procedure TWCClientConfigEditor.OptsEditingDone(Sender : TObject);
begin
Apply;
end;
procedure TWCClientConfigEditor.VerifyTSLCBChange(Sender : TObject);
begin
if Assigned(FCURLClient) then
FCURLClient.VerifyTSL := FVerifyTLS.Checked;
end;
procedure TWCClientConfigEditor.SetVerifyTLS(AValue : Boolean);
begin
FVerifyTLS.Checked := AValue;
end;
constructor TWCClientConfigEditor.Create(AOwner : TComponent);
begin
inherited Create(AOwner);
FCURLClient := nil;
FValues := TValueListEditor.Create(Self);
FValues.Parent := Self;
FValues.Strings.AddPair(rsServer, '');
FValues.Strings.AddPair(rsProxy, '');
FValues.Strings.AddPair(rsUser, '');
FValues.Strings.AddPair(csPwrd, '');
FValues.Strings.AddPair(csDevice, '');
FValues.Strings.AddPair(csMeta, '');
FValues.Strings.AddPair(csSID, '');
FValues.DisplayOptions := [doAutoColResize, doKeyColFixed];
FValues.OnEditingDone := @OptsEditingDone;
FValues.OnDrawCell := @ValuesDrawCell;
FValues.Align := alClient;
FVerifyTLS := TCheckBox.Create(Self);
FVerifyTLS.Parent := Self;
FVerifyTLS.Caption := rsVerifyTLS;
FVerifyTLS.Top := 100;
FVerifyTLS.OnChange := @VerifyTSLCBChange;
FVerifyTLS.Align := alBottom;
end;
destructor TWCClientConfigEditor.Destroy;
begin
inherited Destroy;
end;
procedure TWCClientConfigEditor.Apply;
begin
if Assigned(FCURLClient) then
with FCURLClient do
begin
Setts.Device := GetDevice;
Setts.SetProxy(GetProxy);
Setts.MetaData := GetMetadata;
Setts.Host := GetHostName;
Setts.SID := GetSID;
end;
end;
procedure TWCClientConfigEditor.RestoreProps;
var
i : integer;
begin
if Assigned(FProps) then
begin
for i := 0 to FProps.MAX_PROPS-1 do
FValues.Cells[1, i] := FProps.GetProp(i);
VerifyTLS := FProps.VerifyTLS;
end;
end;
procedure TWCClientConfigEditor.SaveProps;
var
i : integer;
begin
if Assigned(FProps) then
begin
for i := 0 to FProps.MAX_PROPS do
FProps.SetProp(i, FValues.Cells[1, i]);
FProps.VerifyTLS := VerifyTLS;
end;
end;
{ TWCClientPropStorage }
function TWCClientPropStorage.GetDefaults(aIndex : Integer) : String;
begin
if (aIndex >= 0) and (aIndex <= MAX_PROPS) then
Result := FDefaults[aIndex] else
Result := '';
end;
function TWCClientPropStorage.GetDevice : String;
begin
Result := GetProp(DEVICE_POS);
end;
function TWCClientPropStorage.GetHostName : String;
begin
Result := GetProp(HOST_POS);
end;
function TWCClientPropStorage.GetMetadata : String;
begin
Result := GetProp(META_POS);
end;
function TWCClientPropStorage.GetPassword : String;
begin
Result := GetProp(PWRD_POS);
end;
function TWCClientPropStorage.GetProxy : String;
begin
Result := GetProp(PROXY_POS);
end;
function TWCClientPropStorage.GetSID : String;
begin
Result := GetProp(SID_POS);
end;
function TWCClientPropStorage.GetUserName : String;
begin
Result := GetProp(USER_POS);
end;
function TWCClientPropStorage.GetProp(aPropName : Integer) : String;
begin
Result := ReadString(PROPS_STR[aPropName], FDefaults[aPropName]);
end;
function TWCClientPropStorage.GetVerifyTLS : Boolean;
begin
Result := ReadBoolean(csVerifyTLS, true);
end;
procedure TWCClientPropStorage.SetDefaults(aIndex : Integer; AValue : String);
begin
FDefaults[aIndex] := AValue;
end;
procedure TWCClientPropStorage.SetDevice(AValue : String);
begin
SetProp(DEVICE_POS, AValue);
end;
procedure TWCClientPropStorage.SetHostName(AValue : String);
begin
SetProp(HOST_POS, AValue);
end;
procedure TWCClientPropStorage.SetMetadata(AValue : String);
begin
SetProp(META_POS, AValue);
end;
procedure TWCClientPropStorage.SetPassword(AValue : String);
begin
SetProp(PWRD_POS, AValue);
end;
procedure TWCClientPropStorage.SetProxy(AValue : String);
begin
SetProp(PROXY_POS, AValue);
end;
procedure TWCClientPropStorage.SetSID(AValue : String);
begin
SetProp(SID_POS, AValue);
end;
procedure TWCClientPropStorage.SetUserName(AValue : String);
begin
SetProp(USER_POS, AValue);
end;
procedure TWCClientPropStorage.SetProp(aPropName : Integer;
const aValue : String);
begin
WriteString(PROPS_STR[aPropName], aValue);
end;
procedure TWCClientPropStorage.SetVerifyTLS(AValue : Boolean);
begin
WriteBoolean(csVerifyTLS, AValue);
end;
constructor TWCClientPropStorage.Create(AOwner : TComponent);
begin
inherited Create(AOwner);
FDefaults := TStringList.Create;
FDefaults.Add('https://localhost:443');
FDefaults.Add('');
FDefaults.Add('');
FDefaults.Add('');
FDefaults.Add('user-device');
FDefaults.Add('');
FDefaults.Add('');
end;
destructor TWCClientPropStorage.Destroy;
begin
FDefaults.Free;
inherited Destroy;
end;
end.