-
Notifications
You must be signed in to change notification settings - Fork 0
/
Options.pas
377 lines (329 loc) · 9.57 KB
/
Options.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
{ ************************************************************************
NetTime is copyrighted by Graham Mainwaring. Permission is hereby
granted to use, modify, redistribute and create derivative works
provided this attribution is not removed. I also request that if you
make any useful changes, please e-mail the diffs to [email protected]
so that I can include them in an 'official' release.
************************************************************************ }
unit Options;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, configobj, NetTimeCommon;
type
TfrmOptions = class;
TfrmOptions = class(TForm)
Label1: TLabel;
edHostname: TEdit;
Label2: TLabel;
edTimeFreq: TEdit;
Label3: TLabel;
edLostSync: TEdit;
Label4: TLabel;
edWarnAdj: TEdit;
btnFindServer: TButton;
Label5: TLabel;
edRetry: TEdit;
cbxProtocol: TComboBox;
CbxNeverWarn: TCheckBox;
btnOK: TButton;
btnCancel: TButton;
cbxServer: TCheckBox;
Label7: TLabel;
edPort: TEdit;
Label8: TLabel;
Label9: TLabel;
edHostname1: TEdit;
btnFindServer1: TButton;
cbxProtocol1: TComboBox;
edPort1: TEdit;
edHostname2: TEdit;
btnFindServer2: TButton;
cbxProtocol2: TComboBox;
edPort2: TEdit;
edHostname3: TEdit;
btnFindServer3: TButton;
cbxProtocol3: TComboBox;
edPort3: TEdit;
edHostname4: TEdit;
btnFindServer4: TButton;
cbxProtocol4: TComboBox;
edPort4: TEdit;
btnHelp: TButton;
btnAutoConfig: TButton;
cbxShowInTray: TCheckBox;
cbxServiceAutoStart: TCheckBox;
procedure FormShow(Sender: TObject);
procedure btnFindServerClick(Sender: TObject);
procedure CbxNeverWarnClick(Sender: TObject);
procedure btnOKClick(Sender: TObject);
procedure btnHelpClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure cbxProtocolChange(Sender: TObject);
procedure btnAutoConfigClick(Sender: TObject);
private
co: TConfigObj;
Hostnames: array[0..MaxServers-1] of TEdit;
Protocols: array[0..MaxServers-1] of TComboBox;
Ports: array[0..MaxServers-1] of TEdit;
FindButtons: array[0..MaxServers-1] of TButton;
public
tt: TNetTimeServerBase;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure ReadFromRegistry;
procedure ReadFromRunning(tt: TNetTimeServerBase);
procedure ReadFromObject;
procedure WriteToRegistry;
procedure WriteToRunning(tt: TNetTimeServerBase);
procedure WriteToObject;
end;
implementation
{$R *.DFM}
uses registry, findhost, timewrap, autoconfig;
constructor TfrmOptions.Create(AOwner: TComponent);
begin
inherited;
co := TConfigObj.Create;
ReadFromObject;
end;
destructor TfrmOptions.Destroy;
begin
co.Free;
inherited;
end;
procedure TfrmOptions.FormShow(Sender: TObject);
begin
if Assigned(tt) then
ReadFromRunning(tt);
end;
procedure TfrmOptions.btnFindServerClick(Sender: TObject);
var
fFH: TfrmFindServer;
s: string;
p: integer;
i: integer;
n: integer;
begin
n := -1;
for i := 0 to MaxServers-1 do
if Sender = FindButtons[i] then
n := i;
if n = -1 then
exit;
fFH := TfrmFindServer.Create(nil);
if fFH.ShowModal = mrOK then
begin
if (fFH.lstSuccess.ItemIndex >= 0) then
begin
s := fFH.lstSuccess.Items[fFH.lstSuccess.ItemIndex];
p := pos(' ',s);
if p > 0 then
s := copy(s,1,p-1);
Hostnames[n].Text := s;
Protocols[n].ItemIndex := 0;
Ports[n].Text := inttostr(NTP_Port);
end;
end;
fFH.Release;
end;
procedure TfrmOptions.ReadFromRegistry;
begin
co.ReadFromRegistry;
ReadFromObject;
end;
procedure TfrmOptions.ReadFromRunning(tt: TNetTimeServerBase);
begin
co.ReadFromRegistry;
co.ReadFromRunning(tt);
ReadFromObject;
end;
procedure TfrmOptions.ReadFromObject;
var
i: integer;
procedure ReadServer(const n: integer);
begin
Hostnames[n].Text := co.Servers[n].Hostname;
Protocols[n].ItemIndex := integer(co.Servers[n].Protocol);
Ports[n].Text := inttostr(co.Servers[n].Port);
end;
begin
for i := 0 to co.ServerCount-1 do
ReadServer(i);
edTimeFreq.Text := inttostr(co.SyncFreq);
edLostSync.Text := inttostr(co.LostSync);
edWarnAdj.Text := inttostr(co.WarnAdj);
cbxNeverWarn.Checked := (edWarnAdj.Text = '0');
edWarnAdj.Enabled := not cbxNeverWarn.Checked;
edRetry.Text := inttostr(co.Retry);
cbxServer.Checked := co.Server;
cbxShowInTray.Checked := co.LoadOnLogin;
cbxServiceAutoStart.Checked := co.ServiceOnBoot;
end;
procedure TfrmOptions.WriteToRegistry;
begin
WriteToObject;
co.WriteToRegistry;
end;
procedure TfrmOptions.WriteToRunning(tt: TNetTimeServerBase);
begin
WriteToObject;
co.WriteToRunning(tt);
end;
procedure TfrmOptions.WriteToObject;
var
i: integer;
procedure WriteServer(const n: integer);
var
Srv: TServerDef;
begin
if Hostnames[n].Text = '' then
exit;
Srv.Hostname := Hostnames[n].Text;
Srv.Protocol := TTimeProto(Protocols[n].ItemIndex);
Srv.Port := strtoint(Ports[n].Text);
co.AddServer(Srv);
end;
begin
co.ClearServerList;
for i := 0 to MaxServers-1 do
WriteServer(i);
co.SyncFreq := strtoint(edTimeFreq.Text);
co.LostSync := strtoint(edLostSync.Text);
co.WarnAdj := strtoint(edWarnAdj.Text);
co.Retry := strtoint(edRetry.Text);
co.Server := cbxServer.Checked;
co.LoadOnLogin := cbxShowInTray.Checked;
co.ServiceOnBoot := cbxServiceAutoStart.Checked;
end;
procedure TfrmOptions.CbxNeverWarnClick(Sender: TObject);
begin
if CbxNeverWarn.Checked then
begin
EdWarnAdj.Text := '0';
EdWarnAdj.Enabled := false;
end
else
begin
if EdWarnAdj.Text = '0' then
EdWarnAdj.Text := inttostr(DefaultRetry);
EdWarnAdj.Enabled := true;
end;
end;
procedure TfrmOptions.btnOKClick(Sender: TObject);
var
Problems: string;
begin
WriteToObject;
Problems := '';
if (co.SyncFreq < 600) or (co.Retry < 600) then
Problems := Problems + '* Update intervals lower than ten minutes are strongly '+
'discouraged when synchronizing to public servers, in order to avoid excessive '+
'bandwidth costs for the server operators.' + #13#10 + #13#10;
if co.Retry < co.SyncFreq then
Problems := Problems + '* The retry interval is the time to wait when a server is '+
'down. This should be higher than the normal sync interval, to avoid '+
'creating heavy traffic to a server that can''t handle it.' + #13#10 + #13#10;
if co.LostSync < (co.SyncFreq + (co.Retry * 2)) then
Problems := Problems + '* The Max Free Run interval is the maximum amount of time '+
'that can elapse before we consider the local clock to be out of sync. It is '+
'recommended that this be long enough to allow at least two retries.' + #13#10 + #13#10;
if Problems <> '' then
if MessageDlg('The following problems were found with your configuration:' + #13#10 + #13#10 +
Problems + 'Do you want to correct these problems automatically?',
mtWarning, [mbYes,mbNo], 0) = mrYes then
begin
if co.SyncFreq < 600 then
co.SyncFreq := 600;
if co.Retry < 600 then
co.Retry := 600;
if co.Retry < co.SyncFreq then
co.Retry := co.SyncFreq;
if co.LostSync < (co.SyncFreq + (co.Retry * 2)) then
co.LostSync := (co.SyncFreq + (co.Retry * 2));
ReadFromObject;
end;
WriteToRegistry;
if Assigned(tt) then
WriteToRunning(tt);
end;
procedure TfrmOptions.btnHelpClick(Sender: TObject);
begin
Application.HelpCommand(HELP_CONTEXT,Self.HelpContext);
end;
procedure TfrmOptions.FormCreate(Sender: TObject);
begin
Hostnames[0] := edHostname;
Hostnames[1] := edHostname1;
Hostnames[2] := edHostname2;
Hostnames[3] := edHostname3;
Hostnames[4] := edHostname4;
Protocols[0] := cbxProtocol;
Protocols[1] := cbxProtocol1;
Protocols[2] := cbxProtocol2;
Protocols[3] := cbxProtocol3;
Protocols[4] := cbxProtocol4;
Ports[0] := edPort;
Ports[1] := edPort1;
Ports[2] := edPort2;
Ports[3] := edPort3;
Ports[4] := edPort4;
FindButtons[0] := btnFindServer;
FindButtons[1] := btnFindServer1;
FindButtons[2] := btnFindServer2;
FindButtons[3] := btnFindServer3;
FindButtons[4] := btnFindServer4;
end;
procedure TfrmOptions.cbxProtocolChange(Sender: TObject);
var
i: integer;
begin
for i := 0 to MaxServers-1 do
if Sender = Protocols[i] then
Ports[i].Text := inttostr(
DefaultPortForProtocol(TTimeProto(Protocols[i].ItemIndex)));
end;
procedure TfrmOptions.btnAutoConfigClick(Sender: TObject);
var
fAC: TfrmAutoConfigure;
function UpToComma(const s: string): string;
var
p: integer;
begin
p := pos(',',s);
if p = 0 then
result := s
else
result := copy(s,1,p-1);
end;
procedure SetItUp(const idx: integer; const hn: string);
begin
if hn <> '' then
begin
Hostnames[idx].Text := hn;
Protocols[idx].ItemIndex := 0;
Ports[idx].Text := inttostr(NTP_Port);
end
else
begin
Hostnames[idx].Text := '';
Protocols[idx].ItemIndex := -1;
Ports[idx].Text := '';
end;
end;
begin
fAC := TfrmAutoConfigure.Create(Application);
try
if fAC.ShowModal = mrOK then
begin
SetItUp(0,UpToComma(fAC.lblServer1.Caption));
SetItUp(1,UpToComma(fAC.lblServer2.Caption));
SetItUp(2,UpToComma(fAC.lblServer3.Caption));
SetItUp(3,UpToComma(fAC.lblServer4.Caption));
SetItUp(4,UpToComma(fAC.lblServer5.Caption));
end;
finally
fAC.Free;
end;
end;
end.