Menu

[r152]: / trunk / Src / FmMailingListDlg.pas  Maximize  Restore  History

Download this file

404 lines (363 with data), 12.3 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
{
* FmMailingListDlg.pas
*
* Dialog box that is used to subscribe to the CodeSnip mailing list.
*
* v1.0 of 18 Nov 2006 - Original version.
* v1.1 of 08 Feb 2007 - Moved control initialisation code from FormCreate
* event handler to new overridden InitForm method and
* deleted FormCreate method.
* v1.2 of 15 Dec 2008 - Replaced custom email address checking code with
* routine from UEmailHelper unit.
* - Made private and protected sections strict.
* v1.3 of 13 Jan 2009 - Replaced control char literals with constants.
* v1.4 of 25 Jan 2009 - Changed to use routines from UHTMLUtils to generate
* HTML tags instead of using literal strings.
* - Removed class reference from call to TMailingListDlg
* constructor.
* v1.5 of 13 May 2009 - Changed to use revised web service constructor.
* - Removed reference to deleted UParams unit.
* v1.6 of 19 Jul 2009 - Modified to accommodate the new Vista default font in
* controls. Some labels replaced by HTML frames.
* - Controls now dynamically arranged vertically and
* dialog box sizes itself to tallest tab sheet.
*
*
* ***** 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 FmMailingListDlg.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.
*
* ***** END LICENSE BLOCK *****
}
unit FmMailingListDlg;
interface
uses
// Delphi
StdCtrls, Controls, ExtCtrls, Classes, Forms,
// Project
FmGenericDlg, FrBrowserBase, FrFixedHTMLDlg, FrHTMLDlg, FrHTMLTpltDlg;
type
{
TMailingListDlg:
Defines a dialog box that is used to subscribe to the CodeSnip mailing list.
}
TMailingListDlg = class(TGenericDlg)
btnCancel: TButton;
btnSubmit: TButton;
edEmail: TEdit;
edName: TEdit;
frmResult: THTMLTpltDlgFrame;
lblEmail: TLabel;
lblName: TLabel;
lblSubmit: TLabel;
lblSubscribing: TLabel;
pnlData: TPanel;
pnlResult: TPanel;
frmPrivacy: TFixedHTMLDlgFrame;
frmMailList: TFixedHTMLDlgFrame;
procedure btnSubmitClick(Sender: TObject);
strict private
procedure Subscribing(const Flag: Boolean);
{Updates display according whether we are currently using web service to
perform subscription.
@param Flag [in] True if subscribing and false if not.
}
procedure ShowResultPane(const Msg: string);
{Displays result pane and shows a message in it.
@param Msg [in] Message to display in result pane.
}
procedure LoadResultAsHTML(const Msg: string);
{Displays result message as HTML.
@param Msg [in] Message to convert to HTML.
}
function ValidateEmailAddress: Boolean;
{Checks that entered email address is valid by doing some basic checks on
it. Displays any error messages.
@return True if address if OK, false if not.
}
function Subscribe: string;
{Attempts to subscribe user mailing list and handles message returned from
web service.
@return Message returned from web service.
}
strict protected
procedure ConfigForm; override;
{Initialises content of HTML frames and sets fonts as required.
}
procedure ArrangeForm; override;
{Positions controls and sets form size according to body panel dimensions.
}
procedure InitForm; override;
{Populates and initialises controls.
}
function ModalResultOnEsc: Integer; override;
{Gets modal result returned from dialog when user presses ESC key.
@return mrCancel.
}
public
class procedure Execute(const AOwner: TComponent);
{Displays dialog box.
@param AOwner [in] Owning form.
}
end;
implementation
uses
// Delphi
SysUtils, Math,
// Project
UAppInfo, UConsts, UGraphicUtils, UEmailHelper, UFontHelper, UHTMLUtils,
UMailListSubscriber, UMessageBox, UUtils;
{$R *.dfm}
resourcestring
// Button caption
sCloseBtnCaption = '&Finish';
// Error messages
sErrEmailRequired = 'Please provide your email address.';
sErrEmailInvalid = 'Your email address does not appear to be valid.';
{ TMailingListDlg }
procedure TMailingListDlg.ArrangeForm;
{Positions controls and sets form size according to body panel dimensions.
}
procedure SetLabelHeight(const Lbl: TLabel);
{Sets height of a label to accommodate the text it contains in its font.
@param Lbl [in] Label whose height is to be set.
}
begin
Lbl.Height := StringExtent(Lbl.Caption, Lbl.Font, Lbl.Width).cy;
end;
function BottomOf(const Ctrl: TControl): Integer;
{Gets position of bottom of a control relative to its parent control in
pixels.
@param Ctr [in] Control to check.
@return Required position.
}
begin
Result := Ctrl.Top + Ctrl.Height;
end;
function VCentre(const ATop: Integer;
const Ctrls: array of TControl): Integer;
{Vertically centres a list of controls.
@param ATop [in] Top tallest control to be aligned.
@param Ctrls [in] Array of controls to be aligned.
@return Height occupied by controls (= height of tallest control).
}
var
I: Integer; // loops thru all controls to be aligned
begin
Result := 0;
for I := Low(Ctrls) to High(Ctrls) do
if Ctrls[I].Height > Result then
Result := Ctrls[I].Height;
for I := Low(Ctrls) to High(Ctrls) do
Ctrls[I].Top := ATop + (Result - Ctrls[I].Height) div 2;
end;
var
ATop: Integer; // indicates top of various controls
begin
frmMailList.Height := frmMailList.DocHeight;
frmPrivacy.Height := frmPrivacy.DocHeight;
SetLabelHeight(lblEmail);
SetLabelHeight(lblName);
SetLabelHeight(lblSubmit);
SetLabelHeight(lblSubscribing);
ATop := BottomOf(frmMailList) + 8;
frmPrivacy.Top := ATop + VCentre(ATop, [lblEmail, edEmail]);
ATop := BottomOf(frmPrivacy) + 8;
lblSubmit.Top := ATop + VCentre(ATop, [lblName, edName]) + 8;
lblSubscribing.Top := lblSubmit.Top;
pnlBody.ClientHeight := Max(BottomOf(lblSubmit), BottomOf(lblSubscribing))
+ 8;
inherited; // aligns inherited controls and sizes form
// now align buttons added in this dialog
btnCancel.Left := btnHelp.Left - btnCancel.Width - 4;
btnSubmit.Left := btnCancel.Left - btnSubmit.Width - 4;
btnCancel.Top := btnHelp.Top;
btnSubmit.Top := btnHelp.Top;
end;
procedure TMailingListDlg.btnSubmitClick(Sender: TObject);
{Submit button handler. Validates user input then submits subscription request
to web service and updates display and controls.
@param Sender [in] Not used.
}
var
Response: string; // message sent in response to request by web service
begin
try
// Check user's entries
if not ValidateEmailAddress then
Exit;
// Do subscription
Subscribing(True);
Response := Subscribe;
ShowResultPane(Response);
finally
Subscribing(False);
end;
end;
procedure TMailingListDlg.ConfigForm;
{Initialises content of HTML frames and sets fonts as required.
}
begin
inherited;
frmPrivacy.Initialise('frm-emailprivacy.html');
frmMailList.Initialise('dlg-mlist-maillist.html');
TFontHelper.SetDefaultBaseFont(lblSubscribing.Font, False);
end;
class procedure TMailingListDlg.Execute(const AOwner: TComponent);
{Displays dialog box.
@param AOwner [in] Owning form.
}
begin
with Create(AOwner) do
try
ShowModal;
finally
Free;
end;
end;
procedure TMailingListDlg.InitForm;
{Populates and initialises controls.
}
begin
inherited;
// Use any registered user name as default name
edName.Text := TAppInfo.RegisteredUser;
// Make sure correct pane is displayed
pnlResult.Hide;
pnlData.Show;
end;
procedure TMailingListDlg.LoadResultAsHTML(const Msg: string);
{Displays result message as HTML.
@param Msg [in] Message to convert to HTML.
}
var
Lines: TStringList; // lines of message
LineIdx: Integer; // loops thru lines of message
Values: TStringList; // map of HTML placeholders to actual values
begin
Values := nil;
// Record message as lines of valid HTML text
Lines := TStringList.Create;
try
Lines.Text := Msg;
for LineIdx := 0 to Pred(Lines.Count) do
Lines[LineIdx] := MakeSafeHTMLText(Lines[LineIdx]);
// Map placeholders to content
Values := TStringList.Create;
// heading is in first line of message
if Lines.Count > 0 then
begin
Values.Values['Heading'] := Lines[0];
Lines.Delete(0);
end;
// remainder of lines are paragraphs
Values.Values['Paragraphs'] :=
MakeTag('p', ttOpen) +
JoinStr(
Lines, MakeTag('p', ttClose) + EOL + MakeTag('p', ttOpen), False
) +
MakeTag('p', ttClose);
// Resolve and load template:
frmResult.Initialise('dlg-mlist-result-tplt.html', Values);
finally
FreeAndNil(Values);
FreeAndNil(Lines);
end;
end;
function TMailingListDlg.ModalResultOnEsc: Integer;
{Gets modal result returned from dialog when user presses ESC key.
@return mrCancel.
}
begin
Result := btnCancel.ModalResult;
end;
procedure TMailingListDlg.ShowResultPane(const Msg: string);
{Displays result pane and shows a message in it.
@param Msg [in] Message to display in result pane.
}
begin
// Display result pane
pnlData.Hide;
pnlResult.Show;
// Load HTML
LoadResultAsHTML(Msg);
// Update dialog's buttons
btnCancel.Caption := sCloseBtnCaption;
btnSubmit.Hide;
end;
function TMailingListDlg.Subscribe: string;
{Attempts to subscribe user mailing list and handles message returned from
web service.
@return Message returned from web service.
}
var
MLSubs: TMailListSubscriber; // mailing list web service
begin
MLSubs := TMailListSubscriber.Create;
try
Result := MLSubs.Subscribe(edEmail.Text, edName.Text);
finally
FreeAndNil(MLSubs);
end;
end;
procedure TMailingListDlg.Subscribing(const Flag: Boolean);
{Updates display according whether we are currently using web service to
perform subscription.
@param Flag [in] True if subscribing and false if not.
}
begin
if Flag then
Screen.Cursor := crHourGlass
else
Screen.Cursor := crDefault;
btnCancel.Enabled := not Flag;
btnSubmit.Enabled := not Flag;
lblSubmit.Visible := not Flag;
lblSubscribing.Visible := Flag;
edEmail.Enabled := not Flag;
edName.Enabled := not Flag;
Application.ProcessMessages;
end;
function TMailingListDlg.ValidateEmailAddress: Boolean;
{Checks that entered email address is valid by doing some basic checks on it.
Displays any error messages.
@return True if address if OK, false if not.
}
var
Email: string; // entered email address
begin
Email := edEmail.Text;
// Assume failure
Result := False;
// Check if no email address supplied
if Email = '' then
begin
TMessageBox.Error(Self, sErrEmailRequired);
Exit;
end;
// Do some basic checks on address
if not IsValidEmailAddress(Email) then
begin
TMessageBox.Error(Self, sErrEmailInvalid);
Exit;
end;
// If we get here all is OK
Result := True;
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.