Menu

[r2009]: / trunk / Src / FmNewsDlg.pas  Maximize  Restore  History

Download this file

350 lines (312 with data), 9.7 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
{
* FmNewsDlg.pas
*
* Implements a dialog box that displays news items from CodeSnip's RSS news
* feed.
*
* $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 FmNewsDlg.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2010-2011 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit FmNewsDlg;
interface
uses
// Delphi
Buttons, StdCtrls, Forms, Controls, ExtCtrls, Classes,
// Project
FmGenericViewDlg, FrBrowserBase, FrRSSNews, UBaseObjects, UExceptions, URSS20,
UXMLDocumentEx;
type
{
TNewsDlg:
Dialog box that displays news items from CodeSnip's RSS news feed.
}
TNewsDlg = class(TGenericViewDlg, INoPublicConstruct)
btnRSSFeed: TBitBtn;
frmHTML: TRSSNewsFrame;
lblDays: TLabel;
pnlTop: TPanel;
btnConfig: TButton;
/// <summary>RSS Feed button click handler. Displays RSS feed in default
/// web browser.</summary>
procedure btnRSSFeedClick(Sender: TObject);
/// <summary>Change button click handler. Displays news prefs page of
/// Preferences dialog box to enable number of days to display in news
/// feed to be changed.</summary>
procedure btnConfigClick(Sender: TObject);
strict private
/// <summary>Loads news from RSS feed, converts to HTML and displays in
/// browser control.</summary>
procedure LoadNews;
/// <summary>Displays brief message Msg as an HTML paragraph in browser
/// control.</summary>
procedure DisplayMessage(const Msg: string);
/// <summary>Displays a message in browser that informs that content is
/// loading.</summary>
procedure DisplayLoadingMsg;
/// <summary>Renders RSS news feed in HTML and displays in browser control.
/// </summary>
procedure DisplayNews(const RSS: TRSS20);
/// <summary>Displays a message in browser control informing that there are
/// no news items in RSS feed.</summary>
procedure DisplayNoNewsMsg;
/// <summary>Renders HTML describing exception E and displays it in browser
/// control.</summary>
procedure DisplayErrorMsg(E: ECodeSnip);
/// <summary>Gets and returns interface to XML document containing details
/// of RSS news feed from web.</summary>
function GetRSSDocument: IXMLDocumentEx;
/// <summary>Gets maximum number of days of news to be displayed.</summary>
function GetMaxNewsAge: Integer;
/// <summary>Updates label with current maximum news age.</summary>
procedure UpdateNewsAgeLbl;
strict protected
/// <summary>Arranges controls on form.</summary>
/// <remarks>Called from ancestor class.</remarks>
procedure ArrangeForm; override;
/// <summary>Initialises HTML frame.</summary>
/// <remarks>Called from ancestor class.</remarks>
procedure ConfigForm; override;
/// <summary>Initialises form's controls.</summary>
/// <remarks>Called from ancestor class.</remarks>
procedure InitForm; override;
/// <summary>Loads new from RSS feed after form is displayed.</summary>
/// <remarks>Called from ancestor class.</remarks>
procedure AfterShowForm; override;
public
/// <summary>Displays dialog box.</summary>
/// <param name="AOwner">TComponent [in] Control that owns dialog box.
/// </param>
class procedure Execute(AOwner: TComponent);
end;
implementation
uses
// Delphi
SysUtils, ExtActns,
// Project
FmPreferencesDlg, FrNewsPrefs, UCtrlArranger, UHTMLDetailUtils, UHTMLUtils,
UPreferences, UStrUtils, Web.UInfo, Web.UXMLRequestor;
{$R *.dfm}
{ TNewsDlg }
procedure TNewsDlg.AfterShowForm;
begin
LoadNews;
end;
procedure TNewsDlg.ArrangeForm;
begin
inherited;
TCtrlArranger.AlignVCentres(2, [lblDays, btnConfig]);
lblDays.Left := 0;
pnlTop.ClientHeight := TCtrlArranger.TotalControlHeight(pnlTop) + 8;
btnRSSFeed.Top := btnClose.Top;
end;
procedure TNewsDlg.btnConfigClick(Sender: TObject);
var
CurrentNewsAge: Integer;
begin
CurrentNewsAge := GetMaxNewsAge;
if TPreferencesDlg.Execute(Self, [TNewsPrefsFrame]) then
begin
if CurrentNewsAge <> GetMaxNewsAge then
LoadNews;
end;
end;
procedure TNewsDlg.btnRSSFeedClick(Sender: TObject);
var
BrowseAction: TBrowseURL; // action that displays RSS feed URL in browser
begin
BrowseAction := TBrowseURL.Create(nil);
try
BrowseAction.URL := TWebInfo.NewsFeedURL(GetMaxNewsAge);
BrowseAction.Execute;
finally
BrowseAction.Free;
end;
end;
procedure TNewsDlg.ConfigForm;
begin
inherited;
frmHTML.Initialise;
end;
procedure TNewsDlg.DisplayErrorMsg(E: ECodeSnip);
resourcestring
sErrorHeading = 'Error loading news:'; // fixed heading text
var
ErrHeadingAttrs: IHTMLAttributes; // HTML attributes of heading
ErrMessageAttrs: IHTMLAttributes; // HTML attributes of error message
begin
ErrHeadingAttrs := THTMLAttributes.Create('class', 'error-heading');
ErrMessageAttrs := THTMLAttributes.Create('class', 'error-message');
frmHTML.DisplayContent(
MakeCompoundTag('p', ErrHeadingAttrs, MakeSafeHTMLText(sErrorHeading)) +
MakeCompoundTag('p', ErrMessageAttrs, MakeSafeHTMLText(E.Message))
);
end;
procedure TNewsDlg.DisplayLoadingMsg;
resourcestring
sLoadingMsg = 'Loading...'; // message text
begin
DisplayMessage(sLoadingMsg);
end;
procedure TNewsDlg.DisplayMessage(const Msg: string);
var
HTMLAttrs: IHTMLAttributes; // HTML attributes
begin
HTMLAttrs := THTMLAttributes.Create('class', 'message');
frmHTML.DisplayContent(
MakeCompoundTag('p', HTMLAttrs, MakeSafeHTMLText(Msg))
);
end;
procedure TNewsDlg.DisplayNews(const RSS: TRSS20);
/// Renders the given RSS news item title as HTML. Rendered as a link if item
/// specifies a URL.
function TitleHTML(const Item: TRSS20Item): string;
resourcestring
sNoTitle = 'Untitled'; // text used when no title
var
Title: string; // title text
Link: string; // item's URL used for link
begin
Title := StrTrim(Item.Title);
if Title = '' then
Title := sNoTitle;
Link := StrTrim(Item.Link);
if Link = '' then
Result := MakeSafeHTMLText(Title)
else
Result := TextLink(Link, '', '', nil, Title);
Result := MakeCompoundTag('strong', Result);
end;
/// Renders given RSS new item's description as HTML.
function DescriptionHTML(const Item: TRSS20Item): string;
resourcestring
sNoDescription = 'No description.'; // text used when no description
var
Description: string; // description text
begin
Description := StrTrim(Item.Description);
if Description = '' then
Description := sNoDescription;
Result := MakeSafeHTMLText(Description);
end;
var
SB: TStringBuilder; // object used to construct HTML
Item: TRSS20Item; // references each RSS item in feed
begin
SB := TStringBuilder.Create;
try
SB.AppendLine(MakeTag('dl', ttOpen));
for Item in RSS do
begin
SB.AppendLine(MakeTag('dt', ttOpen));
SB.AppendLine(MakeCompoundTag('div', TitleHTML(Item)));
if Item.PubDateAsText <> '' then
SB.AppendLine(
MakeCompoundTag('div', MakeSafeHTMLText(DateTimeToStr(Item.PubDate)))
);
SB.AppendLine(MakeTag('dt', ttClose));
SB.AppendLine(MakeCompoundTag('dd', DescriptionHTML(Item)));
end;
SB.AppendLine(MakeTag('dl', ttClose));
frmHTML.DisplayContent(SB.ToString);
finally
SB.Free;
end;
end;
procedure TNewsDlg.DisplayNoNewsMsg;
resourcestring
sNoNews = 'There are no news items to display.'; // message text
begin
DisplayMessage(sNoNews);
end;
class procedure TNewsDlg.Execute(AOwner: TComponent);
begin
with InternalCreate(AOwner) do
try
ShowModal;
finally
Free;
end;
end;
function TNewsDlg.GetMaxNewsAge: Integer;
begin
Result := Preferences.NewsAge;
end;
function TNewsDlg.GetRSSDocument: IXMLDocumentEx;
var
Requestor: TXMLRequestor; // object that makes XML request
begin
Requestor := TXMLRequestor.Create;
try
Result := Requestor.GetDocument(TWebInfo.NewsFeedURL(GetMaxNewsAge));
finally
Requestor.Free;
end;
end;
procedure TNewsDlg.InitForm;
begin
inherited;
lblDays.Caption := '';
end;
procedure TNewsDlg.LoadNews;
var
RSSFeed: TRSS20; // object used to interpret RSS feed XML
begin
Screen.Cursor := crHourGlass;
try
DisplayLoadingMsg;
try
RSSFeed := TRSS20.Create;
try
RSSFeed.Load(GetRSSDocument);
if RSSFeed.Count > 0 then
DisplayNews(RSSFeed)
else
DisplayNoNewsMsg;
UpdateNewsAgeLbl;
finally
RSSFeed.Free;
end;
except
on E: ECodeSnip do
begin
lblDays.Caption := '';
DisplayErrorMsg(E);
end;
end;
finally
Screen.Cursor := crDefault;
end;
end;
procedure TNewsDlg.UpdateNewsAgeLbl;
resourcestring
// message displayed in label at
sNewsDays = 'CodeSnip news from the last %d days.';
begin
lblDays.Caption := Format(sNewsDays, [GetMaxNewsAge]);
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.