Menu

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

Download this file

399 lines (361 with data), 13.1 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
{
* 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
SysUtils, ExtActns,
// Project
Notifications.UData, Notifications.URecorderThread, UThreadGroup;
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 facility off in
/// preferences or if a check has been made within the preceding 24 hrs.
/// </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(TNotificationRecorderThread)
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>Finds if an update of the CodeSnip program 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 or if the
/// user has disabled automatic checking for program updates.</remarks>
function GetNotification(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 facility off in
/// preferences or if a check has been made within the preceding 24 hrs.
/// </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(TNotificationRecorderThread)
strict protected
/// <summary>Checks if the local copy of the online database is up to date
/// 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 or if the
/// user has disabled automatic checking for database updates.</remarks>
function GetNotification(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;
type
/// <summary>Wrapper around the 'UpdateChecks' section of the per-user
/// config file that records information about when update availability was
/// checked.</summary>
TUpdateCheckerConfig = class(TObject)
public
type
/// <summary>
/// <para>Enumeration of supported kinds of update check.</para>
/// <para>- ukProgram - program update checks.</para>
/// <para>- ukDatabase - database update checks.</para>
/// </summary>
TCheckKind = (ckProgram, ckDatabase);
strict private
var
/// <summary>Dates and times of last successful update check for each
/// supported kind of update.<summary>
fLastUpdateCheck: array[TCheckKind] of TDateTime;
/// <summary>Returns the config file name of the value that stores that
/// stores the last upate check date for the given update check.</summary>
function ValueName(const Kind: TCheckKind): string;
/// <summary>Formats the given TDateTime value as a string.</summary>
function FormatDate(const DT: TDateTime): string;
/// <summary>Converts the given string as a TDateTime value.</summary>
/// <exceptions>If S is not a valid config file date a fatal exception will
/// be raised.</exceptions>
function ParseDate(const S: string): TDateTime;
public
/// <summary>Construct new object instance and reads last update check
/// times from storage.</summary>
constructor Create;
/// <summary>Writes the last update check times to storage then destroys
/// the object.</summary>
destructor Destroy; override;
/// <summary>Uses information from storage to determine if update
/// availability should be checked for the given update kind.</summary>
function CanCheck(Kind: TCheckKind): Boolean;
/// <summary>Update the last update check time for the given update kind.
/// </summary>
procedure RecordCheck(Kind: TCheckKind);
end;
implementation
uses
// Delphi
Classes, Windows, DateUtils,
// Project
FmUpdateDlg, UAppInfo, UPreferences, UProgramUpdateChecker, USettings,
UStrUtils, UUpdateMgr, UUtils;
{ 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.GetNotification(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;
Config: TUpdateCheckerConfig;
begin
try
Config := TUpdateCheckerConfig.Create;
try
if not Preferences.AutoCheckProgramUpdates
or not Config.CanCheck(ckProgram) then
Exit(False);
Config.RecordCheck(ckProgram);
finally
Config.Free;
end;
UpdateChecker := TProgramUpdateChecker.Create;
try
if not UpdateChecker.Execute('Auto') 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;
except
// Swallow any exception and return false to indicate "no update"
Result := False;
end;
end;
{ TDatabaseUpdateCheckerThread }
function TDatabaseUpdateCheckerThread.GetNotification(
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: TUpdateMgr;
Content: TArray<string>;
TaskCallback: TProc;
Config: TUpdateCheckerConfig;
begin
try
Config := TUpdateCheckerConfig.Create;
try
if not Preferences.AutoCheckDatabaseUpdates
or not Config.CanCheck(ckDatabase) then
Exit(False);
Config.RecordCheck(ckDatabase);
finally
Config.Free;
end;
UpdateMgr := TUpdateMgr.Create(TAppInfo.AppDataDir);
try
if UpdateMgr.CheckForUpdates in [uqUpToDate, uqError] then
Exit(False);
Content := TArray<string>.Create(sContent1, sContent2);
TaskCallback :=
procedure
begin
TUpdateDlg.Execute(nil);
end;
N := TNotificationData.Create(
sTitle, Content, HelpKeyword, TaskCallback, sUpdatePrompt
);
Result := True;
finally
UpdateMgr.Free;
end
except
// Swallow any exception and return false to indicate "no update"
Result := False;
end;
end;
{ TUpdateCheckerMgr }
constructor TUpdateCheckerMgr.Create;
begin
inherited Create;
fThreads := TThreadGroup.Create;
fThreads.Add([
TProgramUpdateCheckerThread.Create(1000), // begins work after 10s delay
TDatabaseUpdateCheckerThread.Create(2000) // 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;
{ TUpdateCheckerConfig }
function TUpdateCheckerConfig.CanCheck(Kind: TCheckKind): Boolean;
begin
Result := DaysBetween(NowGMT, fLastUpdateCheck[Kind]) > 0;
end;
constructor TUpdateCheckerConfig.Create;
var
Storage: ISettingsSection;
K: TCheckKind;
Value: string;
begin
inherited Create;
Storage := Settings.ReadSection(ssUpdateChecks);
for K := Low(TCheckKind) to High(TCheckKind) do
begin
Value := Storage.ItemValues[ValueName(K)];
if Value <> '' then
fLastUpdateCheck[K] := ParseDate(Storage.ItemValues[ValueName(K)])
else
fLastUpdateCheck[K] := EncodeDate(1899, 12, 30);
end;
end;
destructor TUpdateCheckerConfig.Destroy;
var
Storage: ISettingsSection;
K: TCheckKind;
begin
Storage := Settings.EmptySection(ssUpdateChecks);
for K := Low(TCheckKind) to High(TCheckKind) do
Storage.ItemValues[ValueName(K)] := FormatDate(fLastUpdateCheck[K]);
Storage.Save;
inherited;
end;
function TUpdateCheckerConfig.FormatDate(const DT: TDateTime): string;
begin
// In config file we store date in SQL date format
Result := FormatDateTime('yyyy"-"mm"-"dd" "hh":"nn":"ss', DT);
end;
function TUpdateCheckerConfig.ParseDate(const S: string): TDateTime;
begin
Result := EncodeDate(
StrToInt(StrSlice(S, 1, 4)),
StrToInt(StrSlice(S, 6, 2)),
StrToInt(StrSlice(S, 9, 2))
)
+
EncodeTime(
StrToInt(StrSlice(S, 12, 2)),
StrToInt(StrSlice(S, 15, 2)),
StrToInt(StrSlice(S, 18, 2)),
0
);
end;
procedure TUpdateCheckerConfig.RecordCheck(Kind: TCheckKind);
begin
fLastUpdateCheck[Kind] := NowGMT;
end;
function TUpdateCheckerConfig.ValueName(const Kind: TCheckKind): string;
const
NameMap: array[TCheckKind] of string = (
'Program', 'Database'
);
NameFmt = 'Last%sCheck';
begin
Result := Format(NameFmt, [NameMap[Kind]]);
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.