Menu

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

Download this file

365 lines (325 with data), 10.5 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
{
* 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-2011 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit FmRegistrationDlg;
interface
uses
// Delphi
StdCtrls, ComCtrls, Controls, ExtCtrls, Classes,
// Project
FmWizardDlg, UBaseObjects;
type
{
TRegistrationDlg:
Wizard style dialog box that collects information and sends application
registration to web server.
}
TRegistrationDlg = class(TWizardDlg, INoPublicConstruct)
edName: TEdit;
edRegCode: TEdit;
edReport: TMemo;
lblInstructions: TLabel;
lblIntro: TLabel;
lblIntroExplain: TLabel;
lblName: TLabel;
lblRegCode: TLabel;
lblReport: TLabel;
lblSubmit: TLabel;
lblThanks: TLabel;
tsAboutUser: TTabSheet;
tsFinish: TTabSheet;
tsIntro: TTabSheet;
tsSubmit: TTabSheet;
strict private
fRegistered: Boolean; // Flag indicating whether program was registered
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 EWebError 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, Forms,
// Project
UAppInfo, UFontHelper, UCtrlArranger, UMessageBox, UStrUtils, USystemInfo,
UUserDetails, UUserDetailsPersist, Web.URegistrar;
{$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';
// Error messages
sErrNameRequired = 'You need to provide your name or a nickname.';
{ 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);
// 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);
// 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;
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'] := StrTrim(edName.Text);
Report.Values['OSDesc'] :=
Format('%0:s. IE Version %1:d.', [TOSInfo.Description, TOSInfo.BrowserVer]);
end;
procedure TRegistrationDlg.ConfigForm;
{Sets font styles where necessary and initialises HTML frames.
}
begin
inherited;
TFontHelper.SetDefaultMonoFont(edRegCode.Font, False);
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(StrTrim(edName.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;
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 EWebError 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.
}
begin
Result := True;
if StrTrim(edName.Text) = '' then
begin
Result := False;
TMessageBox.Error(Self, sErrNameRequired);
end;
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.