-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathciUpdater.pas
More file actions
456 lines (342 loc) · 14.6 KB
/
Copy pathciUpdater.pas
File metadata and controls
456 lines (342 loc) · 14.6 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
UNIT ciUpdater;
{=============================================================================================================
2026.05.18
www.GabrielMoraru.com
--------------------------------------------------------------------------------------------------------------
Automatic program Updater & News announcer
This updater checks if a new version (or news) is available online.
Platform: Windows, macOS, Linux, iOS, Android (framework-neutral; works in both VCL and FMX projects).
Features:
You can target (show the news) only a group of customers or all (Paying customers/Trial users/Demo users/All).
The library can check for news at start up, after a predefined seconds delay.
This is useful because the program might freeze for some miliseconds
(depending on how bussy is the server) while downloading the News file from the Internet.
No personal user data is sent to the server.
--------------------------------------------------------------------------------------------------------------
The Updater
Checks the website every x hours to see if updates of the product (app) are available.
The Announcer
The online files keep information not only about the updates but also it keeps news (like "Discount available if you purchase by the end of the day").
The program can retrieve and display the news to the user (only once).
--------------------------------------------------------------------------------------------------------------
The online file
The data is kept in an INI file. A graphic editor is available for this.
See the RNews record.
Example of usage:
Updater:= TUpdater.Create(URL1);
Updater.URLDownload:= URL2;
Updater.CheckForNews;
Projects\Testers\LightUpdater\Tester_Updater.dpr
==============================================================================================================}
INTERFACE
USES
System.SysUtils, System.DateUtils, System.Classes,
{$IFDEF FRAMEWORK_FMX} FMX.Types {$ELSE} Vcl.ExtCtrls {$ENDIF},
ciUpdaterRec, LightCore.Types;
TYPE
TCheckWhen = (cwNever,
cwStartUp, // Force to check for news now (the value of Delay is taken into consideration)
cwHours); // Check for news but ONLY if the specified number of hours has passed
TUpdater = class(TObject)
private
Timer : TTimer;
LocalNewsID : Integer; { The online counter is saved to disk after we successfully read it from the online file }
FUpdaterStart : TNotifyEvent;
FUpdaterEnd : TNotifyEvent;
FConnectError : TNotifyMsgEvent;
FHasNews : TNotifyEvent;
FNoNews : TNotifyEvent;
procedure GetNewsDelay;
procedure TimerTimer(Sender: TObject);
procedure Clear;
function TooLongNoSee: Boolean;
protected
URLNewsFile : string; { The URL from where we download the news file containing the RNews record. Mandatory }
public
{ Input parameters }
Delay : Integer; { In seconds. Set it to zero to get the news right away. }
When : TCheckWhen;
CheckEvery : Integer; { In hours. How often to check for news. Set it to zero to check every time the program starts. }
ShowConnectFail: Boolean; { If true, show error messages when the program fails to connect to the internet. }
ForceNewsFound : Boolean; { For DEBUGGING. If true, the object will always say that it has found news }
{ URLs }
URLDownload : string; { URL from where the user can download the new update. Not mandatory }
URLRelHistory : string; { URL where the user can see the Release History. Not mandatory }
{ Outputs }
NewsRec : RNews; { Temporary record }
HasNews : Boolean; { Returns true if news were found }
LastUpdate : TDateTime; { We signal with -1 that we don't know yet the value. We need to read it from disk, in this case (only once) }
ConnectionError: Boolean;
constructor Create(CONST aURLNewsFile: string);
destructor Destroy; override;
{ One-shot init + first check for the global Updater singleton.
Assigns the same URL to both URLDownload and URLRelHistory; callers that need them
split should construct TUpdater manually. Defaults: Delay=10s, CheckEvery=24h,
ShowConnectFail=FALSE, When=cwHours. }
class procedure InitAndCheck(const aNewsURL, aProductURL: string; aOnConnectError: TNotifyMsgEvent); static;
function NewVersionFound(CONST AppVersion: string): Boolean;
function IsTimeToCheckAgain: Boolean;
procedure CheckForNews;
function GetNews: Boolean;
procedure LoadFrom(CONST FileName: string);
procedure SaveTo (CONST FileName: string);
procedure Load;
procedure Save;
{ Events }
property OnUpdateStart : TNotifyEvent read FUpdaterStart write FUpdaterStart;
property OnHasNews : TNotifyEvent read FHasNews write FHasNews;
property OnNoNews : TNotifyEvent read FNoNews write FNoNews;
property OnConnectError: TNotifyMsgEvent read FConnectError write FConnectError;
property OnUpdateEnd : TNotifyEvent read FUpdaterEnd write FUpdaterEnd;
end;
VAR
Updater: TUpdater; { Only one instance per app! }
function CompareVersions(const V1, V2: string): Integer;
IMPLEMENTATION
USES
LightCore, LightCore.TextFile, LightCore.IO, LightCore.Download, LightCore.INIFile, LightCore.AppData;
Const
TooLongNoSeeInterval = 180; { Force to check for updates every 180 days even if the updater is disabled }
{--------------------------------------------------------------------------------------------------
CREATE
--------------------------------------------------------------------------------------------------}
constructor TUpdater.Create(CONST aURLNewsFile: string);
begin
Assert(Updater = NIL, 'Updater already created!');
inherited Create;
URLNewsFile:= aURLNewsFile;
Timer:= TTimer.Create(NIL);
Timer.Enabled:= FALSE;
Timer.OnTimer:= TimerTimer;
Clear; { Default settings }
{ Don't bother the user on first startup. Probably he has the latest version anyway. }
if AppDataCore.RunningFirstTime
then Delay:= 300
else Delay:= 30;
{ URLDownload and URLRelHistory default to '' — caller must set them after Create (see comment in type declaration). }
{ Load user settings }
if FileExists(AppDataCore.IniFile)
then Load;
end;
class procedure TUpdater.InitAndCheck(const aNewsURL, aProductURL: string; aOnConnectError: TNotifyMsgEvent);
begin
Updater:= TUpdater.Create(aNewsURL);
Updater.URLDownload := aProductURL;
Updater.URLRelHistory := aProductURL;
Updater.Delay := 10;
Updater.When := cwHours;
Updater.CheckEvery := 24;
Updater.ShowConnectFail:= FALSE;
Updater.OnConnectError := aOnConnectError;
Updater.CheckForNews;
end;
{ Default parameters }
procedure TUpdater.Clear;
begin
NewsRec.Clear;
When := cwHours;
HasNews := FALSE;
ConnectionError := FALSE;
LocalNewsID := 0;
LastUpdate := 0; { We signal with -1 that we don't know yet the value. We need to read it from disk, in this case (only once) }
{ Parameters }
CheckEvery := 12; { Hours. Default interval for checking updates. }
ForceNewsFound := FALSE;
ShowConnectFail := TRUE; { If true, show error messages when the program fails to connect to the internet. }
end;
destructor TUpdater.Destroy;
begin
FreeAndNil(Timer);
TRY
Save;
EXCEPT
on E: Exception DO AppDataCore.LogError('Updater.Save failed: ' + E.Message);
END;
inherited Destroy;
end;
{--------------------------------------------------------------------------------------------------
GET NEWS
--------------------------------------------------------------------------------------------------}
{ Main function.
Set When = cwStartUp then call CheckForNews at program startup. }
procedure TUpdater.CheckForNews;
begin
case When of
cwNever : if TooLongNoSee then GetNewsDelay; { Still check if we haven't done it in 6 months }
cwStartUp: GetNewsDelay;
cwHours : if IsTimeToCheckAgain { This will check for news ONLY if the specified number of hours has passed }
then GetNewsDelay;
else
Raise Exception.Create('Unknown type in TCheckWhen');
end;
end;
{ Check for news few seconds later. We want to check for news some seconds after the program started so we don't freeze the program imediatelly after startup }
procedure TUpdater.GetNewsDelay;
begin
if Delay = 0
then GetNews
else
begin
Timer.Interval:= Delay * 1000;
Timer.Enabled:= TRUE;
end;
end;
procedure TUpdater.TimerTimer(Sender: TObject);
begin
Timer.Enabled:= FALSE; { Disable automatic checking if we already checked once manually }
GetNews;
end;
{ Where we store the News file locally }
function UpdaterFileLocation: string;
begin
Result:= AppDataCore.AppDataFolder+ 'Online_v4.News';
end;
{ Download data from website right now.
Returns TRUE if we need to show the form (in case of error or news) and FALSE if no errors AND no news. }
function TUpdater.GetNews: Boolean;
VAR ErrorMsg: string;
begin
Timer.Enabled:= FALSE;
HasNews:= FALSE;
Assert(URLNewsFile <> '', 'Updater URLNewsFile is empty!');
if Assigned(FUpdaterStart)
then FUpdaterStart(Self);
{ Download the news file }
LightCore.Download.DownloadToFile(URLNewsFile, UpdaterFileLocation, ErrorMsg); { Returns false if the Internet connection failed. If the URL is invalid, probably it will return the content of the 404 page (if the server automatically returns a 404 page). }
Result:= ErrorMsg = '';
{ Parse the news file }
if Result
then
begin
Result:= NewsRec.LoadFrom(UpdaterFileLocation);
if NOT Result then
begin
{ Detect if the server returned an HTML page instead of the news file }
VAR FileContent:= StringFromFile(UpdaterFileLocation);
VAR FileSize:= LightCore.IO.GetFileSize(UpdaterFileLocation);
VAR DetailMsg: string;
if (FileSize < 25*KB)
AND ( ( (PosInsensitive('<html', FileContent) > 0)
AND (PosInsensitive('<body', FileContent) > 0))
OR (PosInsensitive('<!doctype ', FileContent) > 0)
OR (PosInsensitive('<meta name', FileContent) > 0))
then DetailMsg:= 'Server returned an HTML page instead of the news file. The file URL may be invalid.'
else DetailMsg:= 'The news file has an invalid format (version mismatch or corruption).';
ConnectionError:= TRUE; { Treat malformed/HTML responses as a connection-level failure for the UI }
if Assigned(FConnectError)
then FConnectError(Self, DetailMsg);
if ShowConnectFail
then AppDataCore.LogError(DetailMsg);
EXIT;
end;
{ Success: clear any prior error state }
ConnectionError:= FALSE;
LastUpdate:= Now; { Last SUCCESFUL update= now }
{ Compare local news with the online news }
HasNews := (NewsRec.NewsID > LocalNewsID) OR ForceNewsFound; { ForceNewsFound is for debugging }
LocalNewsID:= NewsRec.NewsID;
if HasNews AND Assigned(FHasNews)
then FHasNews(Self);
if NOT HasNews AND Assigned(FNoNews)
then FNoNews(Self)
end
else
begin
ConnectionError:= TRUE;
if Assigned(FConnectError)
then FConnectError(Self, ErrorMsg);
end;
if Assigned(FUpdaterEnd) then FUpdaterEnd(Self);
end;
{--------------------------------------------------------------------------------------------------
UTIL
--------------------------------------------------------------------------------------------------}
{ Returns true interval passed since the last check if higher than CheckEvery
Still check for updates every 180 days, EVEN if the updater is disabled. }
function TUpdater.IsTimeToCheckAgain: Boolean;
begin
Result:= ForceNewsFound
OR (CheckEvery > 0) AND (System.DateUtils.HoursBetween(Now, LastUpdate) >= CheckEvery);
if NOT Result
AND TooLongNoSee
then Result:= TRUE;
end;
{ Returns true if we haven't checked for updates in the last 180 days }
function TUpdater.TooLongNoSee: Boolean;
begin
Result:= System.DateUtils.DaysBetween(Now, LastUpdate) >= TooLongNoSeeInterval;
end;
{ Returns true when the online version is higher than the local version }
function TUpdater.NewVersionFound(CONST AppVersion: string): boolean; // Obtain AppVersion via TAppData.GetVersionInfo
begin
Result:= (NewsRec.AppVersion <> '?') AND (CompareVersions(NewsRec.AppVersion, AppVersion) > 0);
end;
{ Load/save object settings }
procedure TUpdater.Save;
begin
SaveTo(AppDataCore.IniFile);
end;
procedure TUpdater.Load;
begin
LoadFrom(AppDataCore.IniFile);
end;
procedure TUpdater.SaveTo(CONST FileName: string);
begin
VAR IniFile:= TIniFileEx.Create('Updater', FileName);
try
{ Internal state }
IniFile.WriteDate ('LastUpdate__', LastUpdate);
IniFile.Write ('LocalCounter', LocalNewsID);
{ User settings }
IniFile.Write ('When', Ord(When));
IniFile.Write ('CheckEvery', CheckEvery);
IniFile.Write ('ForceNewsFound', ForceNewsFound);
IniFile.Write ('ShowConnectFail', ShowConnectFail);
finally
FreeAndNil(IniFile);
end;
end;
procedure TUpdater.LoadFrom(CONST FileName: string);
begin
VAR IniFile := TIniFileEx.Create('Updater', FileName);
try
{ Internal state}
{ Default 0 (epoch) — NOT Now — so that an INI missing this key triggers TooLongNoSee on the next IsTimeToCheckAgain. }
LastUpdate := IniFile.ReadDate('LastUpdate__', 0);
LocalNewsID := IniFile.Read('LocalCounter', 0);
{ User settings }
When := TCheckWhen(IniFile.Read('When', Ord(cwHours)));
CheckEvery := IniFile.Read('CheckEvery', 12);
ForceNewsFound := IniFile.Read('ForceNewsFound', FALSE);
ShowConnectFail := IniFile.Read('ShowConnectFail', TRUE);
finally
FreeAndNil(IniFile);
end;
end;
{ Compares two version strings segment by segment (e.g. "9.55.0.0" vs "9.9.0.0").
Returns: >0 if V1>V2, 0 if equal, <0 if V1<V2.
Missing segments are treated as 0. }
function CompareVersions(const V1, V2: string): Integer;
VAR
Parts1, Parts2: TArray<string>;
i, N1, N2: Integer;
begin
Parts1:= V1.Split(['.']);
Parts2:= V2.Split(['.']);
VAR MaxLen:= Length(Parts1);
if Length(Parts2) > MaxLen
then MaxLen:= Length(Parts2);
for i:= 0 to MaxLen-1 do
begin
N1:= 0;
N2:= 0;
if i < Length(Parts1)
then TryStrToInt(Parts1[i], N1);
if i < Length(Parts2)
then TryStrToInt(Parts2[i], N2);
if N1 <> N2
then EXIT(N1 - N2);
end;
Result:= 0;
end;
end.