Menu

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

Download this file

370 lines (332 with data), 10.4 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
{
* FmMailingListDlg.pas
*
* Dialog box that is used to subscribe to the CodeSnip mailing list.
*
* $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 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.
*
* Contributor(s)
* NONE
*
* ***** 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.
}
procedure SaveUserDetails;
{Updates persistent storage with any change in user details.
}
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, UCtrlArranger, UEmailHelper, UFontHelper, UHTMLUtils,
UMailListSubscriber, UMessageBox, UUserDetails, 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.
}
begin
// set control heights
TCtrlArranger.SetLabelHeights(Self);
frmMailList.Height := frmMailList.DocHeight;
frmPrivacy.Height := frmPrivacy.DocHeight;
// align controls
TCtrlArranger.AlignVCentres(
TCtrlArranger.BottomOf(frmMailList, 8), [lblEmail, edEmail]
);
frmPrivacy.Top := TCtrlArranger.BottomOf([lblEmail, edEmail]);
TCtrlArranger.AlignVCentres(
TCtrlArranger.BottomOf(frmPrivacy, 8), [lblName, edName]
);
lblSubmit.Top := TCtrlArranger.BottomOf([lblName, edName], 8);
lblSubscribing.Top := lblSubmit.Top;
// set body panel height
pnlBody.ClientHeight := TCtrlArranger.TotalControlHeight(pnlData) + 8;
// align inherited controls and size form
inherited;
// 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);
SaveUserDetails;
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.
}
var
UserDetails: TUserDetails; // details of user
begin
inherited;
// Use any registered user name as default name
UserDetails := TUserDetailsPersist.Load;
edName.Text := UserDetails.Name;
edEmail.Text := UserDetails.Email;
// 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.SaveUserDetails;
{Updates persistent storage with any change in user details.
}
begin
TUserDetailsPersist.Update(TUserDetails.Create(edName.Text, edEmail.Text));
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 := Trim(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.