-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmMember.pas
339 lines (296 loc) · 9.44 KB
/
frmMember.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
unit frmMember;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.DBCtrls,
SCMSimpleConnect, SCMUtility, dmSCM,
FireDAC.Comp.Client, FireDAC.Stan.Intf, FireDAC.Stan.Option,
FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Pool,
FireDAC.Stan.Async, FireDAC.Phys, FireDAC.VCLUI.Wait, Data.DB, System.Actions,
Vcl.ActnList, Vcl.WinXCtrls, Vcl.ExtCtrls, ProgramSetting, Vcl.ComCtrls,
exeinfo, Vcl.Imaging.pngimage, SCMDefines;
type
TMember = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
chkbUseOsAuthentication: TCheckBox;
edtPassword: TEdit;
edtServerName: TEdit;
edtUser: TEdit;
btnConnect: TButton;
btnDisconnect: TButton;
ActionList1: TActionList;
actnConnect: TAction;
actnDisconnect: TAction;
actnManageMembers: TAction;
ActivityIndicator1: TActivityIndicator;
lblAniIndicatorStatus: TLabel;
Timer1: TTimer;
Panel1: TPanel;
Image1: TImage;
StatusMsg: TLabel;
btnManageMembers: TButton;
procedure actnConnectExecute(Sender: TObject);
procedure actnConnectUpdate(Sender: TObject);
procedure actnDisconnectExecute(Sender: TObject);
procedure actnDisconnectUpdate(Sender: TObject);
procedure btnManageMembersClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
fLoginTimeOut: integer;
fConnectionCountdown: integer;
procedure ConnectOnTerminate(Sender: TObject); // THREAD.
procedure Status_ConnectionDescription;
procedure LoadFromSettings; // JSON Program Settings
procedure LoadSettings; // JSON Program Settings
procedure SaveToSettings; // JSON Program Settings
function GetSCMVerInfo(): string;
procedure ManageMembers(var Msg: TMessage); message SCM_INITIALISE;
public
{ Public declarations }
end;
var
Member: TMember;
implementation
{$R *.dfm}
uses frmManageMember;
procedure TMember.actnConnectExecute(Sender: TObject);
var
sc: TSimpleConnect;
myThread: TThread;
begin
if (Assigned(SCM) and (SCM.scmConnection.Connected = false)) then
begin
lblAniIndicatorStatus.Caption := 'Connecting ' +
IntToStr(CONNECTIONTIMEOUT);
StatusMsg.Caption := '';
ActivityIndicator1.Animate := true; // start spinning
lblAniIndicatorStatus.Visible := true; // a label 'Connecting'
fConnectionCountdown := CONNECTIONTIMEOUT - 1;
Timer1.Enabled := true; // start the countdown
myThread := TThread.CreateAnonymousThread(
procedure
begin
// can only be assigned if not connected
SCM.scmConnection.Params.Values['LoginTimeOut'] :=
IntToStr(fLoginTimeOut);
sc := TSimpleConnect.CreateWithConnection(Self, SCM.scmConnection);
sc.DBName := 'SwimClubMeet'; // DEFAULT
sc.SaveConfigAfterConnection := false; // using JSON not System.IniFiles
sc.SimpleMakeTemporyConnection(edtServerName.Text, edtUser.Text,
edtPassword.Text, chkbUseOsAuthentication.Checked);
sc.Free
end);
myThread.OnTerminate := ConnectOnTerminate;
myThread.Start;
end;
end;
procedure TMember.actnConnectUpdate(Sender: TObject);
begin
// verbose code - stop unecessary repaints ...
if Assigned(SCM) then
begin
if SCM.scmConnection.Connected and actnConnect.Enabled then
actnConnect.Enabled := false;
if not SCM.scmConnection.Connected and not actnConnect.Enabled then
actnConnect.Enabled := true;
end
else // D E F A U L T I N I T . Data module not created.
begin
if not actnConnect.Enabled then
actnConnect.Enabled := true;
end;
// btnConnect.Enabled := actnConnect.Enabled;
end;
procedure TMember.actnDisconnectExecute(Sender: TObject);
begin
if Assigned(SCM) then
begin
SCM.DeActivateTable;
SCM.scmConnection.Connected := false;
end;
ActivityIndicator1.Animate := false;
lblAniIndicatorStatus.Visible := false;
SaveToSettings; // As this was a OK connection - store parameters.
Status_ConnectionDescription;
// CALL IT DIRECTLY - ELSE IT WILL NOT WORK
actnDisconnectUpdate(Self);
actnConnectUpdate(Self);
end;
procedure TMember.actnDisconnectUpdate(Sender: TObject);
begin
// verbose code - stop unecessary repaints ...
if Assigned(SCM) then
begin
if SCM.scmConnection.Connected and not actnDisconnect.Enabled then
actnDisconnect.Enabled := true;
if not SCM.scmConnection.Connected and actnDisconnect.Enabled then
actnDisconnect.Enabled := false;
end
else // D E F A U L T I N I T . Data module not created.
begin
if actnDisconnect.Enabled then
actnDisconnect.Enabled := false;
end;
end;
procedure TMember.btnManageMembersClick(Sender: TObject);
begin
if Assigned(SCM) and SCM.IsActive and SCM.scmConnection.Connected then
PostMessage(Handle, SCM_INITIALISE, 0, 0);
end;
procedure TMember.ConnectOnTerminate(Sender: TObject);
begin
lblAniIndicatorStatus.Visible := false;
ActivityIndicator1.Animate := false;
Timer1.Enabled := false;
fConnectionCountdown := CONNECTIONTIMEOUT - 1;
if TThread(Sender).FatalException <> nil then
begin
// something went wrong
// Exit;
end;
if not Assigned(SCM) then
exit;
// C O N N E C T E D .
if (SCM.scmConnection.Connected) then
begin
SCM.ActivateTable;
if (SCM.IsActive = true) then
PostMessage(Handle, SCM_INITIALISE, 0, 0);
end;
if not SCM.scmConnection.Connected then
begin
// Attempt to connect failed.
StatusMsg.Caption :=
'A connection couldn''t be made. (Check you input values.)';
end
else
// Status : SwimClub name + APP and DB version.
Status_ConnectionDescription;
// nothing here works
// actnDisconnect.Update;
// actnConnect.Update;
// UpdateAction(actnDisconnect);
// UpdateAction(actnConnect);
// ActionList1.UpdateAction(actnDisconnect);
// ActionList1.UpdateAction(actnConnect);
// CALL IT DIRECTLY ....
actnDisconnectUpdate(Self);
actnConnectUpdate(Self);
end;
procedure TMember.FormCreate(Sender: TObject);
begin
// Initialization of params.
application.ShowHint := true;
ActivityIndicator1.Animate := false;
fLoginTimeOut := CONNECTIONTIMEOUT; // DEFAULT 20 - defined in ProgramSetting
fConnectionCountdown := CONNECTIONTIMEOUT - 1;
Timer1.Enabled := false;
lblAniIndicatorStatus.Visible := false;
StatusMsg.Caption := '';
// A Class that uses JSON to read and write application configuration
if Settings = nil then
Settings := TPrgSetting.Create;
// C R E A T E T H E D A T A M O D U L E .
if NOT Assigned(SCM) then
SCM := TSCM.Create(Self);
if SCM.scmConnection.Connected then
SCM.scmConnection.Connected := false;
// READ APPLICATION C O N F I G U R A T I O N PARAMS.
// JSON connection settings. Windows location :
// %SYSTEMDRIVE\%%USER%\%USERNAME%\AppData\Roaming\Artanemus\SwimClubMeet\Member
LoadSettings;
Status_ConnectionDescription;
end;
function TMember.GetSCMVerInfo: string;
var
myExeInfo: TExeInfo;
begin
result := '';
// if connected - display the application version
// and the SwimClubMeet database version.
if Assigned(SCM) then
if SCM.scmConnection.Connected then
result := 'DB v' + SCM.GetDBVerInfo;
// get the application version number
myExeInfo := TExeInfo.Create(Self);
result := 'App v' + myExeInfo.FileVersion + ' - ' + result;
myExeInfo.Free;
end;
procedure TMember.LoadFromSettings;
begin
edtServerName.Text := Settings.Server;
edtUser.Text := Settings.User;
edtPassword.Text := Settings.Password;
chkbUseOsAuthentication.Checked := Settings.OSAuthent;
fLoginTimeOut := Settings.LoginTimeOut;
end;
procedure TMember.LoadSettings;
begin
if Settings = nil then
Settings := TPrgSetting.Create;
if not FileExists(Settings.GetDefaultSettingsFilename()) then
begin
ForceDirectories(Settings.GetSettingsFolder());
Settings.SaveToFile();
end;
Settings.LoadFromFile();
LoadFromSettings();
end;
procedure TMember.ManageMembers(var Msg: TMessage);
var
dlg: TManageMember;
begin
// Hide the MAIN-FORM and display frmManageMember
Visible := false;
dlg := TManageMember.Create(Self);
// PARAMS: TFDConnection, SwimClubID, MemberID.
dlg.Prepare(SCM.scmConnection, 1, 0);
dlg.ShowModal;
dlg.Free;
Visible := true;
ExecuteAction(actnDisconnect);
Close(); // terminate application ....
end;
procedure TMember.SaveToSettings;
begin
Settings.Server := edtServerName.Text;
Settings.User := edtUser.Text;
Settings.Password := edtPassword.Text;
if chkbUseOsAuthentication.Checked then
Settings.OSAuthent := true
else
Settings.OSAuthent := false;
Settings.LoginTimeOut := fLoginTimeOut;
Settings.SaveToFile();
end;
procedure TMember.Status_ConnectionDescription;
var
s: string;
begin
if Assigned(SCM) and SCM.IsActive then
begin
// STATUS BAR CAPTION.
StatusMsg.Caption := 'Connected to SwimClubMeet. ';
StatusMsg.Caption := StatusMsg.Caption + GetSCMVerInfo;
if Assigned(SCM.dsSwimClub.DataSet) then
s := SCM.dsSwimClub.DataSet.FieldByName('Caption').AsString
else
s := '';
StatusMsg.Caption := StatusMsg.Caption + sLineBreak + s;
end
else
StatusMsg.Caption := '';
end;
procedure TMember.Timer1Timer(Sender: TObject);
begin
// countdown from CONNECTIONTIMEOUT
fConnectionCountdown := fConnectionCountdown - 1;
lblAniIndicatorStatus.Caption := 'Connecting ' +
IntToStr(fConnectionCountdown);
end;
end.