-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathFCP.FormCheckUpdate.pas
311 lines (242 loc) · 7.17 KB
/
FCP.FormCheckUpdate.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
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
unit FCP.FormCheckUpdate;
interface
uses
Winapi.Windows, Winapi.Messages, Winapi.ShellApi, Winapi.WinInet,
System.SysUtils, System.Variants, System.Classes, System.Actions, //System.IniFiles,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.ActnList, Vcl.ExtCtrls,
JPL.Dialogs, JPL.Win.System, JPL.Win.FileSystem, JPL.Files, JPL.Strings, JPL.MemIniFile,
JPP.PngButton, JPP.SimplePanel,
FCP.Types, FCP.AppStrings;
const
WI_READY = 10;
WI_OK = 0;
WI_INTERNET_OPEN_ERROR = 1;
WI_INTERNET_OPEN_URL_ERROR = 2;
WI_ABORTED = 3;
WI_SETFILEPOS_ERROR = 4;
UA_MSIE7 = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)';
type
THttpStatusProc = function(BytesRead, BytesReadAll: DWORD): Boolean; stdcall;
TStatusColors = record
Background: TColor;
Font: TColor;
end;
TFormCheckUpdate = class(TForm)
pnBottom: TJppSimplePanel;
btnClose: TJppPngButton;
Actions: TActionList;
actEsc: TAction;
actClose: TAction;
lblUserVersionDesc: TLabel;
lblUserVersion: TLabel;
lblLatestVersionDesc: TLabel;
lblLatestVersion: TLabel;
actCheckNow: TAction;
actGoToDownloadPage: TAction;
btnCheckNow: TJppPngButton;
btnGoToDownloadPage: TJppPngButton;
me: TMemo;
tmCheckUpdate: TTimer;
procedure FormCreate(Sender: TObject);
procedure PrepareControls;
procedure actEscExecute(Sender: TObject);
procedure actCloseExecute(Sender: TObject);
procedure SetLang;
procedure actCheckNowExecute(Sender: TObject);
procedure actGoToDownloadPageExecute(Sender: TObject);
procedure SetStatusColors(Colors: TStatusColors);
procedure tmCheckUpdateTimer(Sender: TObject);
procedure ClearStatus;
private
StatusNone: TStatusColors;
StatusFailed: TStatusColors;
StatusNoUpdates: TStatusColors;
StatusUpdateAvailable: TStatusColors;
end;
var
FormCheckUpdate: TFormCheckUpdate;
function GetHtmlPageText(const URL: string): string;
implementation
uses
FCP.FormMain;
{$R *.dfm}
{$region ' GetHtmlPageText '}
function GetHtmlPageText(const URL: string): string;
var
hOpen, hOpenUrl: HINTERNET;
buffer: array[0..1023] of AnsiChar; // 10 KB
ms: TMemoryStream;
dwX: DWORD;
dwFlags: DWORD;
Headers, s: string;
begin;
// me := FormCheckUpdate.me;
Result := '';
hOpen := InternetOpen(
UA_MSIE7,
INTERNET_OPEN_TYPE_PRECONFIG,
'', '', 0
);
// me.Lines.Add('InternetOpen OK');
if hOpen = nil then Exit;
dwFlags := INTERNET_FLAG_RELOAD or INTERNET_FLAG_HYPERLINK;
Headers := 'Accept: */*';
hOpenUrl := InternetOpenURL(
hOpen, PChar(URL), PChar(Headers), Length(Headers),
dwFlags, 0
);
// me.Lines.Add('InternetOpenURL OK');
if hOpenUrl = nil then
begin
InternetCloseHandle(hOpen);
Exit;
end;
dwX := 0;
ms := TMemoryStream.Create;
try
FillChar(buffer, SizeOf(buffer), 0);
if InternetReadFile(hOpenUrl, @buffer, SizeOf(buffer), dwX) then
begin
ms.Write(buffer, dwX);
s := string(buffer);
Result := s;
end;
finally
if Assigned(ms) then try ms.Free; except end;
end;
InternetCloseHandle(hOpenUrl);
InternetCloseHandle(hOpen);
end;
{$endregion GetHtmlPageText}
procedure TFormCheckUpdate.FormCreate(Sender: TObject);
begin
Caption := 'Check for updates';
PrepareModuleStrings_CheckUpdate;
PrepareControls;
SetLang;
end;
{$region ' PrepareControls '}
procedure TFormCheckUpdate.PrepareControls;
begin
btnCheckNow.Appearance.Assign(FormMain.btnT1.Appearance);
btnGoToDownloadPage.Appearance.Assign(FormMain.btnT1.Appearance);
btnClose.Appearance.Assign(FormMain.btnT1.Appearance);
lblUserVersion.Caption := AppInfo.VersionStr;
lblLatestVersion.Caption := '';
me.Lines.Clear;
me.Font.Name := AP.MonospaceFont.Name;
me.Font.Size := AP.MonospaceFont.Size;
actGoToDownloadPage.Visible := False;
StatusNone.Background := clWindow; // clBtnFace; // FormCheckUpdate.Color;
StatusNone.Font := clWindowText;
StatusFailed.Background := $00DADAFA;
StatusFailed.Font := $000B0B4F;
StatusNoUpdates.Background := StatusNone.Background;
StatusNoUpdates.Font := StatusNone.Font;
StatusUpdateAvailable.Background := $00E3F0E6;
StatusUpdateAvailable.Font := $001A2F1D;
SetStatusColors(StatusNone);
end;
{$endregion PrepareControls}
procedure TFormCheckUpdate.SetLang;
begin
Caption := lsCheckUpdate.GetString('Caption', Caption);
end;
{$region ' CheckNow '}
procedure TFormCheckUpdate.actCheckNowExecute(Sender: TObject);
var
Ini: TJppMemIniFile;
s, fName: string;
sl: TStringList;
begin
me.Lines.Clear;
SetStatusColors(StatusNone);
actCheckNow.Enabled := False;
actGoToDownloadPage.Visible := False;
lblLatestVersion.Caption := '';
Randomize;
fName := rbs(TempDir) + '\ver_fcp_' + GetRandomHexStr(4) + '.ini';
Ini := TJppMemIniFile.Create(fName);
sl := TStringList.Create;
try
Ini.CurrentSection := 'VersionInfo';
me.Text := lsCheckUpdate.GetString('CheckingUpdates');
s := GetHtmlPageText(AppInfo.VerIniUrl);
if s = '' then
begin
me.Text := lsCheckUpdate.GetString('ConnectionFailed');
SetStatusColors(StatusFailed);
Exit;
end;
sl.Text := s;
me.Lines.Add(sl.Text);
Ini.SetStrings(sl);
if not Ini.SectionExists(Ini.CurrentSection) then
begin
me.Text := lsCheckUpdate.GetString('CannotCheck');
SetStatusColors(StatusFailed);
Exit;
end;
s := Ini.ReadString('LastVersion', '');
if s = '' then
begin
me.Text := lsCheckUpdate.GetString('CannotCheck');
SetStatusColors(StatusFailed);
Exit;
end;
lblLatestVersion.Caption := s;
// brak nowej wersji
if s = AppInfo.VersionStr then
begin
SetStatusColors(StatusNoUpdates);
me.Text := lsCheckUpdate.GetString('NoUpdates');
lblLatestVersion.Caption := s;
end
// dostêpna jest nowa wersja
else if s > AppInfo.VersionStr then
begin
SetStatusColors(StatusUpdateAvailable);
me.Text := lsCheckUpdate.GetString('UpdatesAvailable');
me.Lines.Add('');
me.Lines.Add('LatestVersion=' + s);
me.Lines.Add('ReleaseDate=' + Ini.ReadString('ReleaseDate', ''));
lblLatestVersion.Caption := s;
actGoToDownloadPage.Visible := True;
end;
finally
sl.Free;
Ini.Free;
actCheckNow.Enabled := True;
if FileExists(fName) then DelFile(fName);
end;
end;
{$endregion CheckNow}
procedure TFormCheckUpdate.SetStatusColors(Colors: TStatusColors);
begin
me.Font.Color := Colors.Font;
me.Color := Colors.Background;
end;
procedure TFormCheckUpdate.ClearStatus;
begin
SetStatusColors(StatusNone);
me.Lines.Clear;
actGoToDownloadPage.Visible := False;
end;
procedure TFormCheckUpdate.tmCheckUpdateTimer(Sender: TObject);
begin
tmCheckUpdate.Enabled := False;
actCheckNow.Execute;
end;
procedure TFormCheckUpdate.actCloseExecute(Sender: TObject);
begin
Close;
end;
procedure TFormCheckUpdate.actEscExecute(Sender: TObject);
begin
Close;
end;
procedure TFormCheckUpdate.actGoToDownloadPageExecute(Sender: TObject);
begin
ShellExecute(Handle, 'open', PChar(AppInfo.DownloadPage), '', '', SW_SHOW);
end;
end.