forked from gphilippot/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpdateCheck.pb
425 lines (331 loc) · 12.5 KB
/
UpdateCheck.pb
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
;--------------------------------------------------------------------------------------------
; Copyright (c) Fantaisie Software. All rights reserved.
; Dual licensed under the GPL and Fantaisie Software licenses.
; See LICENSE and LICENSE-FANTAISIE in the project root for license information.
;--------------------------------------------------------------------------------------------
; The URL that contains the update check file
;
; Example file:
;
; <?xml version="1.0" encoding="UTF-8"?>
; <versions xmlns="http://www.purebasic.com/namespace">
; <version category="bugfix" number="5.11" name="PureBasic 5.11" />
; <version category="beta" number="5.30" beta="3" name="PureBasic 5.30 beta 3" />
; <version category="release" number="5.20" lts="1" name="PureBasic 5.20 LTS" />
; </versions>
;
; Possible values for each release: (note that names are case sensitive)
;
; category:
; "release" - a feature release: 5.10, 5.20, ...
; "bugfix" - a bugfix release: 5.11, 5.12, ...
; "beta" - a beta release
;
; number:
; release number in the form "5.20"
;
; beta:
; beta number for beta releases
;
; lts:
; "1" if the release is a LTS release
;
; name:
; full name of the version for display
;
#UPDATE_CHECK_URL = #ProductWebSite$ + "/versions.xml"
; info about an available version
;
Structure Release
Name$ ; string for display
Category.l ; see below
Number.l ; release number (same as #PB_Compiler_Version)
Beta.l ; number of beta release (if category is "beta")
LTS.l ; true if this is an LTS version
SortKey.l ; for simpler sorting
EndStructure
; more important release last for sorting (so a final is higher than a beta)
Enumeration 1
#CATEGORY_Beta
#CATEGORY_Bugfix
#CATEGORY_Final
EndEnumeration
Global UpdateCheck_DownloadComplete
Global UpdateCheck_DownloadResult
Global UpdateCheck_ShowNoUpdates
Procedure VisitDownloadSite()
CompilerIf #DEMO = 0
; For registered users, directly open the secure download page
Select UCase(CurrentLanguage$)
Case "DEUTSCH": Url$ = #ProductWebSite$ + "/securedownload/Login.php?language=DE"
Case "FRANCAIS": Url$ = #ProductWebSite$ + "/securedownload/Login.php?language=FR"
Default: Url$ = #ProductWebSite$ + "/securedownload/Login.php?language="
EndSelect
CompilerElse
; For demo users, show the main download page
Select UCase(CurrentLanguage$)
Case "DEUTSCH": Url$ = #ProductWebSite$ + "/german/download.php"
Case "FRANCAIS": Url$ = #ProductWebSite$ + "/french/download.php"
Default: Url$ = #ProductWebSite$ + "/download.php"
EndSelect
CompilerEndIf
OpenWebBrowser(Url$)
EndProcedure
Procedure UpdateWindowEvents(EventID)
If EventID = #PB_Event_Menu ; Little wrapper to map the shortcut events (identified as menu)
EventID = #PB_Event_Gadget ; to normal gadget events...
GadgetID = EventMenu()
Else
GadgetID = EventGadget()
EndIf
Select EventID
Case #PB_Event_CloseWindow
Quit = 1
Case #PB_Event_Gadget
Select GadgetID
Case #GADGET_Updates_Website
VisitDownloadSite()
Quit = 1
Case #GADGET_Updates_Settings
OpenPreferencesWindow()
Quit = 1
Case #GADGET_Updates_Ok
Quit = 1
EndSelect
EndSelect
If Quit
If MemorizeWindow
UpdateWindowDialog\Close(@UpdateWindowPosition)
Else
UpdateWindowDialog\Close()
EndIf
EndIf
EndProcedure
Procedure ReadVersionFile(FileName$, List Releases.Release())
ClearList(Releases())
Result = #False
If LoadXML(#XML_UpdateCheck, FileName$)
If XMLStatus(#XML_UpdateCheck) = #PB_XML_Success And MainXMLNode(#XML_UpdateCheck)
; check the namespace
*AllVersions = MainXMLNode(#XML_UpdateCheck)
If ResolveXMLNodeName(*AllVersions, "/") = #UpdateCheckNamespace$ + "/versions"
; examine child nodes
*Version = ChildXMLNode(*AllVersions)
While *Version
If XMLNodeType(*Version) = #PB_XML_Normal And GetXMLNodeName(*Version) = "version"
Select GetXMLAttribute(*Version, "category")
Case "final": Category = #CATEGORY_Final
Case "bugfix": Category = #CATEGORY_Bugfix
Case "beta": Category = #CATEGORY_Beta
Default: Category = -1
EndSelect
If Category <> -1 ; filter unknown categories (for future expandability)
AddElement(Releases())
Releases()\Name$ = GetXMLAttribute(*Version, "name")
Releases()\Category = Category
Releases()\Number = Int(ValD(Trim(GetXMLAttribute(*Version, "number"))) * 100)
LTS$ = GetXMLAttribute(*Version, "lts")
If LTS$
Releases()\LTS = Val(LTS$)
EndIf
Beta$ = GetXMLAttribute(*Version, "beta")
If Beta$ <> ""
Releases()\Beta = Val(Beta$)
EndIf
; calculate a sort key
Releases()\SortKey = Releases()\Number * 10000 + Releases()\Category * 100 + Releases()\Beta
EndIf
EndIf
*Version = NextXMLNode(*Version)
Wend
Result = #True
EndIf
EndIf
FreeXML(#XML_UpdateCheck)
EndIf
; sort to have the highest release first
SortStructuredList(Releases(), #PB_Sort_Descending, OffsetOf(Release\SortKey), #PB_Long)
ProcedureReturn Result
EndProcedure
Procedure UpdateCheckTimer()
; test if the download is complete
If Not UpdateCheck_DownloadComplete
ProcedureReturn
EndIf
; kill the timer
RemoveWindowTimer(#WINDOW_Main, #TIMER_UpdateCheck)
If Not UpdateCheck_DownloadResult
; download failed
If UpdateCheck_ShowNoUpdates
MessageRequester(Language("Updates","Title"), Language("Updates","Error"))
EndIf
ProcedureReturn
EndIf
; perform the update check
Protected NewList CheckedReleases.Release()
Protected NewList AvailableReleases.Release()
; read the file from the previous check (if any)
ReadVersionFile(UpdateCheckFile$, CheckedReleases())
; read the newly downloaded file
If ReadVersionFile(UpdateCheckFile$ + "_new", AvailableReleases())
; replace the old file with the current one
DeleteFile(UpdateCheckFile$)
RenameFile(UpdateCheckFile$ + "_new", UpdateCheckFile$)
Else
If UpdateCheck_ShowNoUpdates
MessageRequester(Language("Updates","Title"), Language("Updates","Error"))
EndIf
DeleteFile(UpdateCheckFile$ + "_new")
ProcedureReturn
EndIf
; get the current compiler version
TokenizeCompilerVersion(DefaultCompiler\VersionString$, @CurrentNumber, @CurrentBeta, @OS, @Processor)
If CurrentBeta = $FFFF
; a final release
If (CurrentNumber % 10) = 0
CurrentCategory = #CATEGORY_Final
Else
CurrentCategory = #CATEGORY_Bugfix
EndIf
CurrentBeta = 0
Else
; a beta release
CurrentCategory = #CATEGORY_Beta
CurrentBeta >> 8 ; remove the alpha part
EndIf
; filter the available releases list by:
; - the list of previously checked versions
; - the current compiler (default one)
; - the preference settings
;
ForEach AvailableReleases()
Filter = #False
; check against current version
If AvailableReleases()\Number < CurrentNumber
Filter = #True
ElseIf AvailableReleases()\Number = CurrentNumber And AvailableReleases()\Category < CurrentCategory
Filter = #True
ElseIf AvailableReleases()\Number = CurrentNumber And AvailableReleases()\Category = CurrentCategory And AvailableReleases()\Beta <= CurrentBeta
Filter = #True
EndIf
; check against preferences
If UpdateCheckVersions = #UPDATE_Version_LTS And (AvailableReleases()\LTS = 0 Or AvailableReleases()\Category = #CATEGORY_Beta)
Filter = #True
ElseIf UpdateCheckVersions = #UPDATE_Version_Final And AvailableReleases()\Category = #CATEGORY_Beta
Filter = #True
EndIf
; check against previously seen versions
ForEach CheckedReleases()
If CheckedReleases()\Category = AvailableReleases()\Category And
CheckedReleases()\Number = AvailableReleases()\Number And
CheckedReleases()\Beta = AvailableReleases()\Beta And
CheckedReleases()\LTS = AvailableReleases()\LTS And
CheckedReleases()\Name$ = AvailableReleases()\Name$
Filter = #True
Break
EndIf
Next CheckedReleases()
If Filter
DeleteElement(AvailableReleases())
EndIf
Next AvailableReleases()
; open the dialog only if one or more releases remain!
;
If ListSize(AvailableReleases()) > 0
UpdateWindowDialog = OpenDialog(?Dialog_Updates, WindowID(#WINDOW_Main), @UpdateWindowPosition)
If UpdateWindowDialog
EnsureWindowOnDesktop(#WINDOW_Updates)
; get the right text
If #DEMO
Text$ = Language("Updates", "MessageDemo")
ElseIf ListSize(AvailableReleases()) = 1
Text$ = Language("Updates", "MessageSingle")
Else
Text$ = Language("Updates", "MessageMulti")
EndIf
Text$ + ":" + #NewLine + #NewLine
ForEach AvailableReleases()
Text$ + AvailableReleases()\Name$ + #NewLine
Next AvailableReleases()
SetGadgetText(#GADGET_Updates_Message, Text$)
SetGadgetColor(#GADGET_Updates_Website, #PB_Gadget_FrontColor, $F00000)
SetGadgetColor(#GADGET_Updates_Settings, #PB_Gadget_FrontColor, $F00000)
UpdateWindowDialog\GuiUpdate() ; needed in case the text is very big
HideWindow(#WINDOW_Updates, #False)
; fix required for the centereing of non-resizable windows in the dialog manager
; (works only if window is visible)
CompilerIf #CompileLinuxGtk2
If UpdateWindowPosition\x = -1 And UpdateWindowPosition\y = -1
While WindowEvent(): Wend
gtk_window_set_position_(WindowID(#WINDOW_Updates), #GTK_WIN_POS_CENTER)
EndIf
CompilerEndIf
EndIf
ElseIf UpdateCheck_ShowNoUpdates = #True
If UpdateCheckVersions = #UPDATE_Version_LTS
NoUpdate$ = Language("Updates", "NoUpdatesLTS")
ElseIf UpdateCheckVersions = #UPDATE_Version_All
NoUpdate$ = Language("Updates", "NoUpdatesBeta")
Else
NoUpdate$ = Language("Updates", "NoUpdates")
EndIf
MessageRequester(Language("Updates","Title"), NoUpdate$, #FLAG_Info)
EndIf
; update the "last check" time
LastUpdateCheck = Date()
EndProcedure
; a thread to download the update check file in the background
;
Procedure UpdateCheck_Download(Dummy)
CompilerIf #PB_Compiler_Thread = 0
CompilerError "Need threadsafe for this routine"
CompilerEndIf
; debugging
;UpdateCheck_DownloadResult = CopyFile("b:\versions.xml", UpdateCheckFile$ + "_new")
UpdateCheck_DownloadResult = ReceiveHTTPFile(#UPDATE_CHECK_URL, UpdateCheckFile$ + "_new")
UpdateCheck_DownloadComplete = #True
EndProcedure
Procedure UpdateCheck_Start()
If Not InitNetwork()
ProcedureReturn
EndIf
; close any existing window
If IsWindow(#WINDOW_Updates)
UpdateWindowEvents(#PB_Event_CloseWindow)
EndIf
; initiate a background download
UpdateCheck_DownloadComplete = #False
CreateThread(@UpdateCheck_Download(), 0)
; set a timer to read the result when download is complete
AddWindowTimer(#WINDOW_Main, #TIMER_UpdateCheck, 500)
EndProcedure
Procedure CheckForUpdatesManual()
; show a requester if no updates are found
UpdateCheck_ShowNoUpdates = #True
; delete old file, so any found version is returned, even
; if it was already shown in a previous check
DeleteFile(UpdateCheckFile$)
UpdateCheck_Start()
EndProcedure
Procedure CheckForUpdatesSchedule()
; When doing this check, do not show the requester if there
; are no updates
UpdateCheck_ShowNoUpdates = #False
; Do the check only if the configured interval elapsed
;
Select UpdateCheckInterval
Case #UPDATE_Interval_Start
UpdateCheck_Start()
Case #UPDATE_Interval_Weekly
If Date() > LastUpdateCheck + (7 * 24 * 60 * 60)
UpdateCheck_Start()
EndIf
Case #UPDATE_Interval_Monthly
Now = Date()
If Year(Now) > Year(LastUpdateCheck) Or Month(Now) > Month(LastUpdateCheck)
UpdateCheck_Start()
EndIf
Case #UPDATE_Interval_Never
; don't check
EndSelect
EndProcedure