-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazbbaboutupdate.pas
264 lines (231 loc) · 7.27 KB
/
lazbbaboutupdate.pas
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
{******************************************************************************
lazbbabout - About box for author applications
Check new versions functions
bb - sdtp - april 2021
Can change width to adapt to program
Deprecated unit. Use lazbbaboutdlg unit. Don't need to change your code
*******************************************************************************}
unit lazbbaboutupdate;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
Buttons, lclintf, fphttpclient, fpopenssl, openssl, opensslsockets, lazbbutils;
type
{ TAboutBox }
TAboutBox = class(TForm)
BtnOK: TBitBtn;
Image1: TImage;
LProductName: TLabel;
LSourceCode: TLabel;
LWebSite: TLabel;
LVersion: TLabel;
LUpdate: TLabel;
LCopyright: TLabel;
LProgPage: TLabel;
PnlDesc: TPanel;
procedure FormActivate(Sender: TObject);
procedure FormChangeBounds(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure LabelMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure LabelMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure LURLClick(Sender: TObject);
procedure LabelMouseEnter(Sender: TObject);
procedure LabelMouseLeave(Sender: TObject);
private
public
ErrorNum: Integer;
ErrorMessage: String; // Errormessage passed to main app
UrlWebsite: String; // Author Web site
UrlProgSite: String; // URL of program page with download link
UrlSourceCode: String; // URL of source code if exists
LastUpdate: TDateTime;
ChkVerUrl: String; // New version check theme (Github)
Version: String; // Current version
LastVersion: String; // Dernière version
sLastUpdateSearch: String; // Date de la dernière recherche
sUpdateAvailable: string; // Nouvelle version disponible
sNoUpdateAvailable: string; // Pas de nouvelle version disponible
ProgName: String; // Nom du programme
Checked, NewVersion: Boolean; // New version check
function ChkNewVersion (url: string=''): string;
end;
var
AboutBox: TAboutBox;
implementation
{$R *.lfm}
// Get new version on Github
// Get latest release page
// extract version value fromn header title
function TAboutBox.ChkNewVersion(url: string=''): string;
var
MyHTTPCli: TFPHTTPClient;
spage: string;
stagurl: string;
titlebeg, titleend: Integer;
A: TStringArray;
sl: TStringList;
s: string;
i, p: integer;
begin
result:= '';
sl:= TStringList.create();
if length(url)=0 then url:= ChkVerUrl;
{ SSL initialization has to be done by hand here }
InitSSLInterface;
MyHTTPCli:= TFPHTTPClient.Create(nil);
try
MyHTTPCli.IOTimeout:= 20000;
MyHTTPCli.AllowRedirect:= true;
MyHTTPCli.AddHeader('User-Agent','Mozilla 5.0 (bb84000 '+ProgName+')');
// Parse last release page to get last version
// <title>Release Version 0.9.1.7 - 08/04/2021 etc
sl.text:= MyHTTPCli.Get(url);
Application.ProcessMessages; //let loading complete
if sl.Count > 0 then
begin
for i:= 0 to sl.Count-1 do
begin
p:= pos('<title>', sl.Strings[i]) ;
if p > 0 then
begin
s:=Copy(sl.Strings[i], p+8, 40);
A:= s.Split(' ');
break;
end;
end;
// Avoid exception if we dont find the proper line
if length(A)>=2 then result:= A[2]
else result:= '';
end;
except
on e:Exception do
ErrorMessage:= e.message
end;
if Assigned(MyHTTPCli) then MyHTTPCli.Free;
if Assigned(sl) then sl.free;
end;
{ TAboutBox }
// Click on Web site URL : launch default browser on author home page
// Click on update URL : launch default browser new version web page
procedure TAboutBox.LURLClick(Sender: TObject);
var
sendername: string;
url: string;
begin
SenderName:= UpperCase(Tcomponent(Sender).Name);
if SenderName= 'LUPDATE' then
begin
// New version found, click get download page
if NewVersion then
begin
// Link to program binaries is in the help file
OpenURL(UrlProgSite);
exit;
end;
// No new version at the moment, check it
//url:= UrlUpdate;
LastUpdate:= now();
LUpdate.Hint:= sLastUpdateSearch + ': ' + DateToStr(trunc(LastUpdate));
checked:= true;
LastVersion:= ChkNewVersion(ChkVerUrl);
if VersionToInt(LastVersion)>VersionToInt(Version) then
begin
LUpdate.Caption:= Format(sUpdateAvailable, [LastVersion]);
NewVersion:= true;
end else
begin
LUpdate.Caption:= sNoUpdateAvailable;
end;
end;
if SenderName= 'LWEBSITE' then
begin
url:= UrlWebsite;
If length(url) > 0 then OpenURL(url);
end;
if SenderName= 'LSOURCECODE' then
begin
url:= UrlSourceCode;
If length(url) > 0 then OpenURL(url);
end;
if SenderName= 'LPROGPAGE' then
begin
url:= UrlProgSite;
If length(url) > 0 then OpenURL(url);
end;
end;
procedure TAboutBox.FormActivate(Sender: TObject);
begin
//LWebSite.Caption:= 'Web site';
LWebSite.Hint:= UrlWebsite;
LProgPage.Hint:= UrlProgSite;
if length(UrlSourceCode)=0 then
begin
LSourceCode.Caption:= '';
LVersion.Top:= LProductName.Top+25;
LCopyright.Top:= LVersion.Top+25;
LUpdate.Top:= LCopyright.Top+25;
LWebsite.Top:= LUpdate.Top+25;
LProgPage.Top:= LWebsite.Top+25;
LSourceCode.visible:= false;
end else
begin
//LSourceCode.Caption:= 'Source code';
LSourceCode.Hint:= UrlSourceCode;
LVersion.Top:= LProductName.Top+20;
LCopyright.Top:= LVersion.Top+20;
LUpdate.Top:= LCopyright.Top+20;
LWebsite.Top:= LUpdate.Top+20;
LProgPage.Top:= LWebsite.Top+20;
LSourceCode.Top:= LProgPage.Top+20;
LSourceCode.visible:= true;
end;
end;
// Resize controls when box is resized
procedure TAboutBox.FormChangeBounds(Sender: TObject);
begin
PnlDesc.left:= 7;
PnlDesc.Width:= ClientWidth-14;
LProductName.left:= 0;
LproductName.Width:= PnlDesc.Width;
LVersion.left:= 0;
LVersion.width:= PnlDesc.Width;
LUpdate.Left:= 0;
LUpdate.Width:= PnlDesc.Width;
LCopyright.left:= 0;
LCopyright.Width:= PnlDesc.Width;
LProgPage.Left:= 0;
LProgPage.Width:= PnlDesc.Width;
LWebsite.Left:= 0;
LWebSite.Width:= PnlDesc.Width;
LSourceCode.Width:= PnlDesc.Width;
BtnOk.Left:= (ClientWidth-BtnOK.Width) div 2;
end;
procedure TAboutBox.FormCreate(Sender: TObject);
begin
inherited;
UrlSourceCode:='';
end;
// URL Label reactions : cursor changes to hand when mouse enter and brignt when selected
procedure TAboutBox.LabelMouseEnter(Sender: TObject);
begin
if TLabel(Sender).Name='LVersion' then TLabel(Sender).Cursor:= crHelp
else TLabel(Sender).Cursor:= crHandPoint;
end;
procedure TAboutBox.LabelMouseLeave(Sender: TObject);
begin
TLabel(Sender).Cursor:= crDefault;
end;
procedure TAboutBox.LabelMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
TLabel(Sender).font.style:= [fsBold];
end;
procedure TAboutBox.LabelMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
TLabel(Sender).Font.style:= [];
end;
end.