Menu

[r3189]: / trunk / Src / UUpdateCheckers.pas  Maximize  Restore  History

Download this file

364 lines (326 with data), 12.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements thread classes that check online to find if updates to the
* CodeSnip program or online database are available. Also implements a manager
* class for these threads.
}
unit UUpdateCheckers;
interface
uses
// Delphi
ExtActns,
// Project
Notifications.UData, Notifications.URecorderThread, UThreadGroup;
type
/// <summary>Abstract base class for threads that check for some update
/// and create a notification when an update is available that is added to
/// the notification queue.</summary>
TUpdateCheckerThread = class abstract(TNotificationRecorderThread)
strict private
/// <summary>Checks if it is OK to check for updates.</summary>
/// <remarks>Result depends on user's setting and time since last update
/// check.</remarks>
function CanUpdate: Boolean;
/// <summary>Records date and time of latest update check.</summary>
/// <remarks>The recorded date/time is used by future calls to CanUpdate.
/// </remarks>
procedure RecordUpdate;
strict protected
/// <summary>Returns the frequency of update checks, in days.</summary>
/// <remarks>A return value of zero indicates that no checks should be
/// made.</remarks>
function UpdateFrequency: Word; virtual; abstract;
/// <summary>Returns the name of the value in the 'UpdateChecks' settings
/// section that stores the date the last update check was made.</summary>
function LastUpdateSettingsName: string; virtual; abstract;
/// <summary>Checks to see if an update is available and, if so, creates a
/// notification record with details of how to obtain the update.</summary>
/// <param name="N">TNotificationData [out] The required notification
/// record if an update is available. Undefined otherwise.</param>
/// <returns>Boolean. True if an update is available or False if not.
/// </returns>
function DoCheck(out N: TNotificationData): Boolean; virtual; abstract;
/// <summary>Finds if an update is available and creates a notification
/// containing information about it if so.</summary>
/// <param name="N">TNotificationData [out] If an update is available this
/// parameter contains information about the update to be added to the
/// notification queue. If no update is available this value is undefined.
/// </param>
/// <returns>Boolean. True if an update is a available and a notification
/// record was created or False if no update is available.</returns>
/// <remarks>False is also returned if an error was encountered, if
/// automatic update check is disabled or if unsufficient time has
/// ellapsed since the last check.</remarks>
function GetNotification(out N: TNotificationData): Boolean; override;
end;
type
/// <summary>Thread that checks online to find if a new version of CodeSnip
/// is available.</summary>
/// <remarks>
/// <para>No check is made if the user has switched the automatic program
/// update checking facility off in preferences or if a check has been made
/// within the time period specified by the user.
/// </para>
/// <para>The thread fails silently as if no update is available if any
/// errors occur when accessing the internet or web service.</para>
/// <para>If an update is found a notification containing the details is
/// placed in the notification queue to await display.</para>
/// </remarks>
TProgramUpdateCheckerThread = class sealed(TUpdateCheckerThread)
strict private
var
/// <summary>Action that causes a web page to be displayed from where
/// a program update can be downloaded.</summary>
/// <remarks>This action is executed when the user clicks the 'task
/// button' in any program update notification window.</remarks>
fDownloadAction: TBrowseURL;
strict protected
/// <summary>Returns the frequency of update checks, in days.</summary>
/// <remarks>A return value of zero indicates that no checks should be
/// made.</remarks>
function UpdateFrequency: Word; override;
/// <summary>Returns the name of the value in the 'UpdateChecks' settings
/// section that stores the date the last update check was made.</summary>
function LastUpdateSettingsName: string; override;
/// <summary>Checks to see if a program update is available and, if so,
/// creates a notification record with details of how to obtain the update.
/// </summary>
/// <param name="N">TNotificationData [out] The required notification
/// record if an update is available. Undefined otherwise.</param>
/// <returns>Boolean. True if an update is available or False if not.
/// </returns>
function DoCheck(out N: TNotificationData): Boolean; override;
public
/// <summary>Creates the and initialises the thread instance.</summary>
/// <param name="StartDelay">Cardinal [in] Number of milliseconds to delay
/// the start of the thread's execution. Passing 0 indicates no delay.
/// Non-zero delays are rounded up to the nearest 1/5th second.</param>
constructor Create(StartDelay: Cardinal = 0);
/// <summary>Destroys thread object instance.</summary>
destructor Destroy; override;
end;
type
/// <summary>Thread that checks online to find if a new version of the online
/// code snippets database is available.</summary>
/// <remarks>
/// <para>No check is made if the user has switched the automatic database
/// update checking facility off in preferences or if a check has been made
/// within the time period specified by the user.
/// </para>
/// <para>The thread fails silently as if no update is available if any
/// errors occur when accessing the internet or web service.</para>
/// <para>If an update is found a notification containing the details is
/// placed in the notification queue to await display.</para>
/// </remarks>
TDatabaseUpdateCheckerThread = class sealed(TUpdateCheckerThread)
strict protected
/// <summary>Returns the frequency of update checks, in days.</summary>
/// <remarks>A return value of zero indicates that no checks should be
/// made.</remarks>
function UpdateFrequency: Word; override;
/// <summary>Returns the name of the value in the 'UpdateChecks' settings
/// section that stores the date the last update check was made.</summary>
function LastUpdateSettingsName: string; override;
/// <summary>Checks to see if a database update is available and, if so,
/// creates a notification record with details of how to obtain the update.
/// </summary>
/// <param name="N">TNotificationData [out] The required notification
/// record if an update is available. Undefined otherwise.</param>
/// <returns>Boolean. True if an update is available or False if not.
/// </returns>
function DoCheck(out N: TNotificationData): Boolean; override;
end;
type
/// <summary>Manages creation and lifetime of the background threads that
/// check for and notify program and online code snippets database updates.
/// </summary>
TUpdateCheckerMgr = class(TObject)
strict private
var
/// <summary>Manages the lifetime of the update threads.</summary>
fThreads: TThreadGroup;
public
/// <summary>Creates new object instance and creates and initialises the
/// update checker threads.</summary>
/// <remarks>The threads are created suspended.</remarks>
constructor Create;
/// <summary>Destroys object instance and update checker threads.</summary>
/// <remarks>Waits for any non-terminated threads to complete.</remarks>
destructor Destroy; override;
/// <summary>Starts the update checker threads.</summary>
procedure StartThreads;
/// <summary>Signals the update checker threads to terminate.</summary>
/// <remarks>Does not wait for the threads to complete.</remarks>
procedure StopThreads;
end;
implementation
uses
// Delphi
SysUtils, Classes, DateUtils,
// Project
FmDBUpdateDlg, UAppInfo, UPreferences, UProgramUpdateChecker, USettings,
UDBUpdateMgr, UUtils;
{ TUpdateCheckerThread }
function TUpdateCheckerThread.CanUpdate: Boolean;
var
Frequency: Word;
Storage: ISettingsSection;
LastUpdateCheck: TDateTime;
begin
Frequency := UpdateFrequency;
if Frequency = 0 then
Exit(False);
Storage := Settings.ReadSection(ssUpdateChecks);
LastUpdateCheck := Storage.GetDateTime(LastUpdateSettingsName);
Result := DaysBetween(NowGMT, LastUpdateCheck) >= Frequency;
end;
function TUpdateCheckerThread.GetNotification(out N: TNotificationData):
Boolean;
begin
try
if not CanUpdate then
Exit(False);
if not DoCheck(N) then
Exit(False);
RecordUpdate;
Result := True;
except
// Swallow any exception and return false to indicate "no update"
Result := False;
end;
end;
procedure TUpdateCheckerThread.RecordUpdate;
var
Storage: ISettingsSection;
begin
Storage := Settings.ReadSection(ssUpdateChecks);
Storage.SetDateTime(LastUpdateSettingsName, NowGMT);
Storage.Save;
end;
{ TProgramUpdateCheckerThread }
constructor TProgramUpdateCheckerThread.Create(StartDelay: Cardinal);
begin
inherited Create(StartDelay);
fDownloadAction := TBrowseURL.Create(nil);
end;
destructor TProgramUpdateCheckerThread.Destroy;
begin
fDownloadAction.Free;
inherited;
end;
function TProgramUpdateCheckerThread.DoCheck(out N: TNotificationData): Boolean;
resourcestring
sTitle = 'Program Update Available';
sContent = 'CodeSnip version %s is available for download.';
sDownloadPrompt = 'Download Now';
const
HelpKeyword = 'ProgramUpdateNotification';
var
UpdateChecker: TProgramUpdateChecker;
Content: TArray<string>;
TaskCallback: TProc;
begin
UpdateChecker := TProgramUpdateChecker.Create('Auto');
try
if not UpdateChecker.Execute then
Exit(False);
fDownloadAction.URL := UpdateChecker.DownloadURL;
Content := TArray<string>.Create(
Format(sContent, [string(UpdateChecker.LatestVersion)])
);
TaskCallback :=
procedure
begin
fDownloadAction.Execute;
end;
N := TNotificationData.Create(
sTitle, Content, HelpKeyword, TaskCallback, sDownloadPrompt
);
Result := True;
finally
UpdateChecker.Free;
end;
end;
function TProgramUpdateCheckerThread.LastUpdateSettingsName: string;
begin
Result := 'LastProgramCheck';
end;
function TProgramUpdateCheckerThread.UpdateFrequency: Word;
begin
Result := Preferences.AutoCheckProgramFrequency;
end;
{ TDatabaseUpdateCheckerThread }
function TDatabaseUpdateCheckerThread.DoCheck(
out N: TNotificationData): Boolean;
resourcestring
sTitle = 'Database Update Available';
sContent1 = 'The online Code Snippets Database has been updated.';
sContent2 = 'You can update your local copy of the database to reflect the '
+ 'changes.';
sUpdatePrompt = 'Update Now';
const
HelpKeyword = 'DatabaseUpdateNotification';
var
UpdateMgr: TDBUpdateMgr;
Content: TArray<string>;
TaskCallback: TProc;
begin
UpdateMgr := TDBUpdateMgr.Create(TAppInfo.AppDataDir, 'Auto');
try
if UpdateMgr.CheckForUpdates in [uqUpToDate, uqError] then
Exit(False);
Content := TArray<string>.Create(sContent1, sContent2);
TaskCallback :=
procedure
begin
TDBUpdateDlg.Execute(nil);
end;
N := TNotificationData.Create(
sTitle, Content, HelpKeyword, TaskCallback, sUpdatePrompt
);
Result := True;
finally
UpdateMgr.Free;
end
end;
function TDatabaseUpdateCheckerThread.LastUpdateSettingsName: string;
begin
Result := 'LastDatabaseCheck';
end;
function TDatabaseUpdateCheckerThread.UpdateFrequency: Word;
begin
Result := Preferences.AutoCheckDatabaseFrequency;
end;
{ TUpdateCheckerMgr }
constructor TUpdateCheckerMgr.Create;
begin
inherited Create;
fThreads := TThreadGroup.Create;
fThreads.Add([
TProgramUpdateCheckerThread.Create(10000), // begins work after 10s delay
TDatabaseUpdateCheckerThread.Create(20000) // begins work after 20s delay
]);
fThreads.SetPriorities(tpLowest);
end;
destructor TUpdateCheckerMgr.Destroy;
begin
fThreads.Free;
inherited;
end;
procedure TUpdateCheckerMgr.StartThreads;
begin
fThreads.Start;
end;
procedure TUpdateCheckerMgr.StopThreads;
begin
fThreads.Terminate;
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.