Menu

[r736]: / trunk / Src / FmRegistrationDlg.pas  Maximize  Restore  History

Download this file

512 lines (466 with data), 16.2 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
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
{
* FmRegistrationDlg.pas
*
* Wizard style dialog box that collects information and sends application
* registration to web server.
*
* $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 FmRegistrationDlg.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2006-2009 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit FmRegistrationDlg;
interface
uses
// Delphi
StdCtrls, ComCtrls, Controls, ExtCtrls, Classes, Forms,
// Project
FmWizardDlg, FrBrowserBase, FrFixedHTMLDlg, FrHTMLDlg, UBaseObjects,
UCSSBuilder;
type
{
TRegistrationDlg:
Wizard style dialog box that collects information and sends application
registration to web server.
}
TRegistrationDlg = class(TWizardDlg, INoPublicConstruct)
chkMailList: TCheckBox;
edEmail: TEdit;
edName: TEdit;
edRegCode: TEdit;
edReport: TMemo;
frmMailListIntro: TFixedHTMLDlgFrame;
frmPrivacy: TFixedHTMLDlgFrame;
gbRequired: TGroupBox;
gbMailList: TGroupBox;
lblEmail: TLabel;
lblInstructions: TLabel;
lblIntro: TLabel;
lblIntroExplain: TLabel;
lblMailListConfirm: TLabel;
lblName: TLabel;
lblRegCode: TLabel;
lblReport: TLabel;
lblSubmit: TLabel;
lblThanks: TLabel;
tsAboutUser: TTabSheet;
tsFinish: TTabSheet;
tsIntro: TTabSheet;
tsSubmit: TTabSheet;
procedure chkMailListClick(Sender: TObject);
strict private
fRegistered: Boolean; // Flag indicating whether program was registered
procedure BuildCSS(Sender: TObject; const CSSBuilder: TCSSBuilder);
{Sets CSS for required font for use in HTML frames.
@param Sender [in] Not used.
@param CSSBuilder [in] Object used to create CSS.
}
procedure BuildSubmission(const Report: TStrings);
{Builds registration submission as list of values in name=value format.
@param Report [in] Stores submission on completion.
}
procedure DoRegistration;
{Registers program and displays registration code.
}
function RegisterWithWebServer: string;
{Gathers required registration data, sends to web server and gets
registration code from it.
@return Registration code.
@except EWebService exception raised if there is a failure in
interaction with web service.
}
function ValidateUserInfo: Boolean;
{Validates data entered by user. Displays any error messages.
@return True if data valid, false if not.
}
strict protected
procedure ArrangeForm; override;
{Vertically arranges controls as required and sizes the tab sheets to be
able to display the longest page.
}
procedure ConfigForm; override;
{Sets font styles where necessary and initialises HTML frames.
}
procedure InitForm; override;
{Initialises controls.
}
procedure BeginPage(const PageIdx: Integer); override;
{Performs required initialisation when a page is displayed.
@param PageIdx [in] Index page to be initialised.
}
function HeadingText(const PageIdx: Integer): string; override;
{Gets text of heading of a wizard page.
@param PageIdx [in] Index of page for which heading is required.
@return Heading text.
}
procedure MoveForward(const PageIdx: Integer;
var CanMove: Boolean); override;
{Performs required processing on moving forward from pages.
@param PageIdx [in] Index of page we are leaving.
@param CanMove [in/out] Flag set to indicate whether can leave page.
Defaults to true.
}
procedure UpdateButtons(const PageIdx: Integer); override;
{Updates wizard buttons depending on page and state.
@param PageIdx [in] Index of current page.
}
public
class function Execute(const Owner: TComponent): Boolean;
{Displays dialog box.
@param Owner [in] Component that owns this dialog.
@return True if program was registered, false otherwise.
}
end;
implementation
uses
// Delphi
SysUtils, Graphics, Math,
// Project
UAppInfo, UCSSUtils, UEmailHelper, UFontHelper, UCtrlArranger, UMessageBox,
URegistrar, USystemInfo, UUserDetails;
{$R *.dfm}
const
// Index of wizard pages
cIntroPageIdx = 0;
cAboutUserPageIdx = 1;
cSubmitPageIdx = 2;
cFinishPageIdx = 3;
resourcestring
// Pages headings
sIntroHeading = 'Register CodeSnip';
sAboutUserHeading = 'About you';
sSubmitHeading = 'Submit registration';
sFinishHeading = 'Registration complete';
// Submit button caption
sSubmitBtnCaption = '&Submit';
// Mailing list confirmation message
sMailListConfirm = 'You chose to subscribe the the CodeSnip mailing list. '
+ 'For your own protection you need to confirm your subscription. '
+ 'An email has been sent to %s that explains how to do this.';
// Error messages
sErrNameRequired = 'You need to provide your name or a nickname.';
sErrEmailRequired = 'Your email address is needed to subscribe to the '
+ 'mailing list.';
sErrEmailInvalid = 'Your email address does not appear to be valid.';
{ TRegistrationDlg }
procedure TRegistrationDlg.ArrangeForm;
{Vertically arranges controls as required and sizes the tab sheets to be able
to display the longest page.
}
begin
// set heights of all labels with AutoSize = False
TCtrlArranger.SetLabelHeights(Self);
// tsIntro tabsheet
lblIntroExplain.Top := TCtrlArranger.BottomOf(lblIntro, 8);
lblInstructions.Top := TCtrlArranger.BottomOf(lblIntroExplain, 8);
// tsAboutUser tabsheet
edName.Top := TCtrlArranger.BottomOf(lblName, 8);
gbRequired.ClientHeight := TCtrlArranger.BottomOf(edName, 12);
gbMailList.Top := TCtrlArranger.BottomOf(gbRequired, 8);
frmMailListIntro.Height := frmMailListIntro.DocHeight;
chkMailList.Top := TCtrlArranger.BottomOf(frmMailListIntro, 8);
TCtrlArranger.AlignVCentres(
TCtrlArranger.BottomOf(chkMailList, 8),
[lblEmail, edEmail]
);
frmPrivacy.Top := TCtrlArranger.BottomOf([lblEmail, edEmail], 8);
frmPrivacy.Height := frmPrivacy.DocHeight;
gbMailList.ClientHeight := TCtrlArranger.BottomOf(frmPrivacy, 12);
// tsSubmit tabsheet
edReport.Top := TCtrlArranger.BottomOf(lblReport, 8);
lblSubmit.Top := TCtrlArranger.BottomOf(edReport, 8);
// tsFinish tabsheet
lblRegCode.Top := TCtrlArranger.BottomOf(lblThanks, 8);
edRegCode.Top := TCtrlArranger.BottomOf(lblRegCode, 4);
lblMailListConfirm.Top := TCtrlArranger.BottomOf(edRegCode, 8);
// set required height
pnlBody.ClientHeight := TCtrlArranger.MaxContainerHeight(
[tsAboutUser, tsFinish, tsIntro, tsSubmit]
) + pnlBody.ClientHeight - tsAboutUser.Height;
// Arrange inherited controls and size the form
inherited;
end;
procedure TRegistrationDlg.BeginPage(const PageIdx: Integer);
{Performs required initialisation when a page is displayed.
@param PageIdx [in] Index page to be initialised.
}
begin
inherited;
case PageIdx of
cSubmitPageIdx:
// Create data to be sent to website ready for submission
BuildSubmission(edReport.Lines);
cAboutUserPageIdx:
// Focus first control on page
edName.SetFocus;
cFinishPageIdx:
begin
// Set up confirmation messages
if chkMailList.Checked then
begin
lblMailListConfirm.Caption := Format(
sMailListConfirm, [Trim(edEmail.Text)]
);
lblMailListConfirm.Visible := True;
end
else
lblMailListConfirm.Visible := False;
end;
end;
end;
procedure TRegistrationDlg.BuildCSS(Sender: TObject;
const CSSBuilder: TCSSBuilder);
{Sets CSS for required font for use in HTML frames.
@param Sender [in] Not used.
@param CSSBuilder [in] Object used to create CSS.
}
var
DefaultFont: TFont; // default font for OS
begin
DefaultFont := TFont.Create;
try
TFontHelper.SetDefaultFont(DefaultFont, False);
with CSSBuilder.Selectors['body'] do
AddProperty(CSSFontProps(DefaultFont));
finally
FreeAndNil(DefaultFont);
end;
end;
procedure TRegistrationDlg.BuildSubmission(const Report: TStrings);
{Builds registration submission as list of values in name=value format.
@param Report [in] Stores submission on completion.
}
begin
Report.Clear;
Report.Values['ProgId'] := TAppInfo.ProgramID;
Report.Values['ProgName'] := TAppInfo.ProgramName;
Report.Values['ProgVer'] := TAppInfo.ProgramReleaseVersion;
Report.Values['ProgKey'] := TAppInfo.ProgramKey;
Report.Values['UserName'] := Trim(edName.Text);
Report.Values['OSDesc'] :=
Format('%0:s. IE Version %1:d.', [TOSInfo.Description, TOSInfo.BrowserVer]);
Report.Values['MailList'] := IntToStr(Ord(chkMailList.Checked));
if chkMailList.Checked then
Report.Values['UserEmail'] := Trim(edEmail.Text)
else
Report.Values['UserEmail'] := '';
end;
procedure TRegistrationDlg.chkMailListClick(Sender: TObject);
{Updates state and focus of email address edit box as check box is checked and
cleared.
@param Sender [in] Not used.
}
begin
if chkMailList.Checked then
begin
edEmail.Enabled := True;
edEmail.Color := clWindow;
edEmail.SetFocus;
end
else
begin
edEmail.Enabled := False;
edEmail.ParentColor := True;
end;
end;
procedure TRegistrationDlg.ConfigForm;
{Sets font styles where necessary and initialises HTML frames.
}
procedure LoadHTMLFrame(const Frm: TFixedHTMLDlgFrame; const ResName: string);
{Safely loads HTML into an HTML frame. To do this requires the tab sheet
containing the frame to be active, so we find and activate the required tab
before loading the HTML into the frame.
@param Frm [in] HTML frame to be initialised with loaded HTML.
@param ResName [in] Name of resource containing HTML.
}
var
Ctrl: TWinControl; // Scans through frame's parents looking for tab sheet
begin
Ctrl := Frm.Parent;
while Assigned(Ctrl) and not (Ctrl is TTabSheet) do
Ctrl := Ctrl.Parent;
Assert(Assigned(Ctrl),
ClassName + '.ConfigForm:LoadHTMLFrame: HTML Frame not on a tab sheet');
pcWizard.ActivePage := Ctrl as TTabSheet;
Frm.Initialise(ResName);
end;
begin
inherited;
gbRequired.Font.Style := [fsBold];
gbMailList.Font.Style := [fsBold];
edName.Font.Style := [];
lblName.Font.Style := [];
chkMailList.Font.Style := [];
edEMail.Font.Style := [];
lblEmail.Font.Style := [];
TFontHelper.SetDefaultMonoFont(edRegCode.Font, False);
frmMailListIntro.OnBuildCSS := BuildCSS;
frmPrivacy.OnBuildCSS := BuildCSS;
LoadHTMLFrame(frmMailListIntro, 'dlg-registration-maillist.html');
LoadHTMLFrame(frmPrivacy, 'frm-emailprivacy.html');
end;
procedure TRegistrationDlg.DoRegistration;
{Registers program and displays registration code.
}
var
UserDetails: TUserDetails; // information about user
begin
Screen.Cursor := crHourglass;
try
// register with server
edRegCode.Text := RegisterWithWebServer;
// record registration & user details
UserDetails := TUserDetails.Create(Trim(edName.Text), Trim(edEmail.Text));
TAppInfo.RegisterProgram(edRegCode.Text, UserDetails.Name);
TUserDetailsPersist.Update(UserDetails);
fRegistered := True;
finally
Screen.Cursor := crDefault;
end;
end;
class function TRegistrationDlg.Execute(const Owner: TComponent): Boolean;
{Displays dialog box.
@param Owner [in] Component that owns this dialog.
@return True if program was registered, false otherwise.
}
begin
with InternalCreate(Owner) do
try
ShowModal;
Result := fRegistered;
finally
Free;
end;
end;
function TRegistrationDlg.HeadingText(const PageIdx: Integer): string;
{Gets text of heading of a wizard page.
@param PageIdx [in] Index of page for which heading is required.
@return Heading text.
}
const
// Map of page indexes to page heading
cPageHeadings: array[0..3] of string = (
sIntroHeading, sAboutUserHeading, sSubmitHeading, sFinishHeading
);
begin
Result := cPageHeadings[PageIdx];
end;
procedure TRegistrationDlg.InitForm;
{Initialises controls.
}
var
UserDetails: TUserDetails; // any known information about user
begin
inherited;
// Use user name if known
UserDetails := TUserDetailsPersist.Load;
edName.Text := UserDetails.Name;
edEmail.Text := UserDetails.Email;
end;
procedure TRegistrationDlg.MoveForward(const PageIdx: Integer;
var CanMove: Boolean);
{Performs required processing on moving forward from pages.
@param PageIdx [in] Index of page we are leaving.
@param CanMove [in/out] Flag set to indicate whether can leave page.
Defaults to true.
}
begin
inherited;
case PageIdx of
cAboutUserPageIdx:
CanMove := ValidateUserInfo;
cSubmitPageIdx:
DoRegistration;
end;
end;
function TRegistrationDlg.RegisterWithWebServer: string;
{Gathers required registration data, sends to web server and gets registration
code from it.
@return Registration code.
@except EWebService exception raised if there is a failure in interaction
with web service.
}
var
Reg: TRegistrar; // object that communicates with web server
PostData: TStringList; // list of data items to be sent to web server
begin
PostData := nil;
Reg := TRegistrar.Create;
try
PostData := TStringList.Create;
BuildSubmission(PostData);
Result := Reg.Submit(PostData); // raises exception on fail result
finally
FreeAndNil(PostData);
FreeAndNil(Reg);
end;
end;
procedure TRegistrationDlg.UpdateButtons(const PageIdx: Integer);
{Updates wizard buttons depending on page and state.
@param PageIdx [in] Index of current page.
}
begin
inherited;
if PageIdx = cSubmitPageIdx then
btnNext.Caption := sSubmitBtnCaption;
btnCancel.Enabled := PageIdx <> cFinishPageIdx;
end;
function TRegistrationDlg.ValidateUserInfo: Boolean;
{Validates data entered by user. Displays any error messages.
@return True if data valid, false if not.
}
// ---------------------------------------------------------------------------
function ValidateEmailAddress(const Email: string): Boolean;
{Checks that email address is valid by doing some basic checks on it.
Displays any error messages.
@param Email [in] Email address to check.
@return True if address if OK, false if not.
}
begin
Result := True;
if Email = '' then
begin
Result := False;
TMessageBox.Error(Self, sErrEmailRequired);
end
else if not IsValidEmailAddress(Email) then
begin
Result := False;
TMessageBox.Error(Self, sErrEmailInvalid);
end;
end;
// ---------------------------------------------------------------------------
begin
Result := True;
if Trim(edName.Text) = '' then
begin
Result := False;
TMessageBox.Error(Self, sErrNameRequired);
end
else if chkMailList.Checked then
Result := ValidateEmailAddress(Trim(edEmail.Text));
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.