Menu

[r2034]: / trunk / Src / FmUpdateDlg.pas  Maximize  Restore  History

Download this file

472 lines (433 with data), 14.0 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
{
* FmUpdateDlg.pas
*
* Dialog box that updates the CodeSnip database from web.
*
* $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 FmUpdateDlg.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2005-2011 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit FmUpdateDlg;
interface
uses
// Project
Forms, StdCtrls, Controls, ExtCtrls, Classes, Messages,
// Delphi
FmGenericViewDlg, UBaseObjects, UMemoProgBarMgr, UUpdateMgr;
type
{
TUpdateHeadlineStyle:
Enumeration that specifies display style of "headline" text in dialog.
}
TUpdateHeadlineStyle = (
hsNormal,
hsCancelled,
hsError
);
{
TUpdateDlg:
Implements dialog box that updates database from web.
}
TUpdateDlg = class(TGenericViewDlg, INoPublicConstruct)
btnCancel: TButton;
btnDoUpdate: TButton;
lblUpdateFromWeb: TLabel;
lblError: TLabel;
edProgress: TMemo;
lblHeadline: TLabel;
btnNews: TButton;
procedure btnCancelClick(Sender: TObject);
procedure btnDoUpdateClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure FormDestroy(Sender: TObject);
procedure btnNewsClick(Sender: TObject);
strict private
fProgressBarMgr: TMemoProgBarMgr; // Displays progress bar in progress memo
fDataUpdated: Boolean; // Flag true if any data was updated
fCancelled: Boolean; // Flag true if user cancelled update
fUpdateMgr: TUpdateMgr; // Handles updating from web
procedure WMActivateApp(var Msg: TMessage); message WM_ACTIVATEAPP;
{Responds to activation of application and this window. Refreshes display.
@param Msg [in/out] Not used.
}
function GetDataDir: string;
{Returns directory where data files stored and ensures it exists.
@return Data directory.
}
procedure UpdateStatusHandler(Sender: TObject; Status: TUpdateStatus;
var Cancel: Boolean);
{Event handler called by update manager to report progress and permit user
to cancel update.
@param Sender [in] Not used.
@param Status [in] Current status of update manager.
@param Cancel [in/out] Flag that cancels update when set true.
}
procedure DownloadProgressHandler(Sender: TObject; const BytesReceived,
BytesExpected: Int64; var Cancel: Boolean);
{OnProgress event handler for update manager object. Displays download
progress using a progress bar.
@param Sender [in] Not used.
@param BytesReceived [in] Number of bytes downloaded to date.
@param BytesExpected [in] Number of bytes expected in download.
@param Cancel [in/out] Set to true to cancel update. (This does not
cancel a download).
}
procedure ProgressMsg(const Msg: string);
{Writes a message to progress control.
@param Msg [in] Message to be written.
}
procedure HeadlineMsg(const Msg: string;
const Kind: TUpdateHeadlineStyle = hsNormal);
{Displays messages in headline label. Display state depends on kind of
headline display required.
@param Msg [in] Message to be displayed
@param Kind [in] Style of message to be displayed.
}
protected
procedure ArrangeForm; override;
{Positions additional controls added to inherited form.
}
procedure InitForm; override;
{Initialises controls.
}
public
class function Execute(AOwner: TComponent): Boolean;
{Displays dialog box and returns whether updated files were downloaded.
true if files were updated and false if not.
@param AOwner [in] Component that owns dialog box (and aligns it if a
form).
@return True if files were actually downloaded or false if no update
needed, an error occurred or if user cancelled.
}
end;
implementation
uses
// Delphi
SysUtils,
// Project
FmNewsDlg, UAppInfo, UColours, UConsts, UCtrlArranger, UStrUtils, UUtils;
{$R *.dfm}
resourcestring
// Progress report messages
sLoggingOn = 'Logging on to web server';
sCheckForUpdates = 'Checking for updates';
sDownloading = 'Downloading database';
sDownloaded = 'Database downloaded';
sUpdating = 'Updating local database';
sUpToDate = 'Database is up to date';
sLoggingOff = 'Logging off';
sCompleted = 'Update completed';
sCancelled = 'Update cancelled';
sCancelling = 'Cancelling...';
// Dialog box messages
sRunning = 'Performing update';
sUpdtSuccess = 'Files updated successfully';
sUpdtUpToDate = 'Database is up to date';
sUpdtCancelled = 'Update cancelled';
sUpdtError = '%0:s:';
// Detailed error message
sErrorDetail = 'Full details of "%0:s" error message are:'
+ EOL2 + '%1:s';
{ TDownloadDlg }
procedure TUpdateDlg.ArrangeForm;
{Positions additional controls added to inherited form.
}
begin
// Arrange inherited controls
inherited;
TCtrlArranger.SetLabelHeights(Self);
// Controls in initial display
btnDoUpdate.Top := TCtrlArranger.BottomOf(lblUpdateFromWeb, 16);
// Arrange additonal cancel button
btnCancel.Left := btnClose.Left + btnClose.Width - btnCancel.Width;
btnCancel.Top := btnClose.Top;
// Arrange "latest news" button
btnNews.Left := 8;
btnNews.Top := btnClose.Top;
// Align error label
lblError.Left := (pnlBody.Width - lblError.Width) div 2;
end;
procedure TUpdateDlg.btnCancelClick(Sender: TObject);
{Cancels database update.
@param Sender [in] Not used.
}
begin
inherited;
// Sets cancelled flag checked during download
fCancelled := True;
ProgressMsg(sCancelling);
end;
procedure TUpdateDlg.btnDoUpdateClick(Sender: TObject);
{Update button clicked. Updates database from web.
@param Sender [in] Not used.
}
begin
inherited;
// Create update manager
fUpdateMgr := TUpdateMgr.Create(GetDataDir);
try
fUpdateMgr.OnStatus := UpdateStatusHandler;
fUpdateMgr.OnDownloadProgress := DownloadProgressHandler;
// Update control visibility
lblUpdateFromWeb.Visible := False;
lblError.Visible := False;
btnDoUpdate.Visible := False;
btnClose.Visible := False;
btnCancel.Visible := True;
edProgress.Visible := True;
HeadlineMsg(sRunning);
// Reset download flag to allow download to continue. Clicking cancel button
// sets this flag true
fCancelled := False;
// Perform the download and display message depending on result
fDataUpdated := False;
case fUpdateMgr.Execute of
urUpdated:
begin
// connection to web succeeded and files were updated
HeadlineMsg(sUpdtSuccess);
fDataUpdated := True;
end;
urNoUpdate:
begin
// connection to web succeeded but no files were updated
HeadlineMsg(sUpdtUpToDate);
end;
urCancelled:
begin
// user cancelled update
HeadlineMsg(sUpdtCancelled, hsCancelled);
end;
urError:
begin
// an error occured during update
fProgressBarMgr.Hide;
ProgressMsg('');
ProgressMsg(fUpdateMgr.LongError);
HeadlineMsg(Format(sUpdtError, [fUpdateMgr.ShortError]), hsError);
end;
end;
finally
FreeAndNil(fUpdateMgr);
// Reset cancelled flag
fCancelled := False;
// Hide cancel button and show close button
btnCancel.Visible := False;
btnClose.Visible := True;
end;
end;
procedure TUpdateDlg.btnNewsClick(Sender: TObject);
{Displays latest CodeSnip news in dialog box.
@param Sender [in] Not used.
}
begin
TNewsDlg.Execute(Self);
end;
procedure TUpdateDlg.DownloadProgressHandler(Sender: TObject;
const BytesReceived, BytesExpected: Int64; var Cancel: Boolean);
{OnProgress event handler for update manager object. Displays download
progress using a progress bar.
@param Sender [in] Not used.
@param BytesReceived [in] Number of bytes downloaded to date.
@param BytesExpected [in] Number of bytes expected in download.
@param Cancel [in/out] Set to true to cancel update. (This does not
cancel a download).
}
begin
if BytesReceived = 0 then
fProgressBarMgr.Max := BytesExpected;
fProgressBarMgr.Position := BytesReceived;
Cancel := fCancelled;
Application.ProcessMessages;
end;
class function TUpdateDlg.Execute(AOwner: TComponent): Boolean;
{Displays dialog box and returns whether updated files were downloaded.
true if files were updated and false if not.
@param AOwner [in] Component that owns dialog box (and aligns it if a form).
@return True if files were actually downloaded or false if no update needed,
an error occurred or if user cancelled.
}
begin
with InternalCreate(AOwner) do
try
ShowModal;
Result := fDataUpdated;
finally
Free;
end;
end;
procedure TUpdateDlg.FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
{Checks if form can close following click on Close or system menu button.
Prevents closure while downloading.
@param Sender [in] Not used.
@param CanClose [in/out] Flag to be set false to prevent dialog closing.
}
begin
inherited;
if btnClose.Visible then
CanClose := True
else
begin
CanClose := False;
fCancelled := True;
end;
end;
procedure TUpdateDlg.FormCreate(Sender: TObject);
{Creates owned objects and initialises flags when form is created.
@param Sender [in] Not used.
}
begin
inherited;
// Record that no update yet taken place
fDataUpdated := False;
// Object that handles location and display of progress bar
fProgressBarMgr := TMemoProgBarMgr.Create(edProgress);
end;
procedure TUpdateDlg.FormDestroy(Sender: TObject);
{Form destruction event handler. Destroys owned objects.
@param Sender [in] Not used.
}
begin
inherited;
FreeAndNil(fProgressBarMgr);
end;
function TUpdateDlg.GetDataDir: string;
{Returns directory where data files stored and ensures it exists.
@return Data directory.
}
begin
Result := TAppInfo.AppDataDir;
EnsureFolders(Result);
end;
procedure TUpdateDlg.HeadlineMsg(const Msg: string;
const Kind: TUpdateHeadlineStyle);
{Displays messages in headline label. Display state depends on kind of
headline display required.
@param Msg [in] Message to be displayed
@param Kind [in] Style of message to be displayed.
}
begin
// Display message
lblHeadline.Caption := Msg;
lblHeadline.Visible := True;
// Determine appearance
case Kind of
hsNormal:
begin
// Normal message: standard colours
lblHeadline.ParentFont := True;
lblError.Visible := False;
end;
hsCancelled:
begin
// Cancel message: show in warning text colour
lblHeadline.Font.Color := clWarningText;
lblError.Visible := False;
end;
hsError:
begin
// Error message: show in warning text colour followed by extra error
// info. Make sure headline ends in one space to separate headline from
// error message
lblHeadline.Caption := StrTrimRight(lblHeadline.Caption) + ' ';
lblHeadline.Font.Color := clWarningText;
TCtrlArranger.MoveToRightOf(lblHeadline, lblError);
lblError.Top := lblHeadline.Top;
lblError.Visible := True;
end;
end;
end;
procedure TUpdateDlg.InitForm;
{Initialises controls.
}
begin
inherited;
// Hide Cancel and show Close buttons
btnCancel.Visible := False;
btnClose.Visible := True;
end;
procedure TUpdateDlg.ProgressMsg(const Msg: string);
{Writes a message to progress control.
@param Msg [in] Message to be written.
}
begin
edProgress.Lines.Add(Msg);
end;
procedure TUpdateDlg.UpdateStatusHandler(Sender: TObject;
Status: TUpdateStatus; var Cancel: Boolean);
{Event handler called by update manager to report progress and permit user
to cancel update.
@param Sender [in] Not used.
@param Status [in] Current status of update manager.
@param Cancel [in/out] Flag that cancels update when set true.
}
begin
// Update UI according to status
case Status of
usLogOn:
ProgressMsg(sLoggingOn);
usCheckForUpdates:
ProgressMsg(sCheckForUpdates);
usDownloadStart:
begin
ProgressMsg(sDownloading + ' ');
fProgressBarMgr.Show(edProgress.Lines.Count - 1);
end;
usDownloadEnd:
begin
fProgressBarMgr.Hide;
ProgressMsg(sDownloaded);
end;
usUpdating:
ProgressMsg(sUpdating);
usNoUpdate:
ProgressMsg(sUpToDate);
usLogOff:
ProgressMsg(sLoggingOff);
usCompleted:
ProgressMsg(sCompleted);
usCancelled:
ProgressMsg(sCancelled);
end;
// Application needs to process messages to allow any UI updates
Application.ProcessMessages;
// Halt update if user cancelled
Cancel := fCancelled;
end;
procedure TUpdateDlg.WMActivateApp(var Msg: TMessage);
{Responds to activation of application and this window. Refreshes display.
@param Msg [in/out] Not used.
}
begin
// This is needed since hiding window and switching back causes some of frame
// controls to be hidden. Also sometimes the controls are not displayed
// correctly when dialog is first displayed.
Refresh;
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.