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 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
|
{
***************************************************************************
* *
* This source is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This code is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* A copy of the GNU General Public License is available on the World *
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
* obtain it by writing to the Free Software Foundation, *
* Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA. *
* *
***************************************************************************
Author: Balázs Székely
}
unit opkman_updates;
{$mode objfpc}{$H+}
{$INCLUDE opkman_fpcdef.inc}
interface
uses
Classes, SysUtils, fpjson, fpjsonrtti, dateutils,
// LazUtils
Laz2_XMLCfg, LazIDEIntf,
// OpkMan
opkman_serializablepackages, opkman_options, opkman_common,
{$IFDEF MSWINDOWS}
opkman_const,
{$IFDEF FPC311}zipper,{$ELSE}opkman_zip,{$ENDIF}
{$ENDIF}
{$IFDEF FPC311}fphttpclient{$ELSE}opkman_httpclient{$ENDIF}
;
const
OpkVersion = 1;
type
{ TUpdateLazPackages }
TUpdateLazPackages = class(TCollectionItem)
private
FName: String;
FVersion: String;
FForceNotify: Boolean;
FInternalVersion: Integer;
published
property Name: String read FName write FName;
property Version: String read FVersion write FVersion;
property ForceNotify: Boolean read FForceNotify write FForceNotify;
property InternalVersion: Integer read FInternalVersion write FInternalVersion;
end;
{ TUpdatePackageData }
TUpdatePackageData = class(TPersistent)
private
FDownloadZipURL: String;
FDisableInOPM: Boolean;
FName: String;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
published
property Name: String read FName write FName;
property DownloadZipURL: String read FDownloadZipURL write FDownloadZipURL;
property DisableInOPM: Boolean read FDisableInOPM write FDisableInOPM;
end;
{TUpdatePackage}
TUpdatePackage = class(TPersistent)
private
FUpdatePackageData: TUpdatePackageData;
FUpdateLazPackages: TCollection;
FLastError: String;
procedure Clear;
public
constructor Create;
destructor Destroy; override;
function LoadFromJSON(const AJSON: TJSONStringType): Boolean;
function SaveToJSON(var AJSON: TJSONStringType): Boolean;
property LastError: String read FLastError;
published
property UpdatePackageData: TUpdatePackageData read FUpdatePackageData write FUpdatePackageData;
property UpdateLazPackages: TCollection read FUpdateLazPackages write FUpdateLazPackages;
end;
{ TUpdates }
TUpdates = class(TThread)
private
FHTTPClient: TFPHTTPClient;
FUpdatePackage: TUpdatePackage;
FVersion: Integer;
FNeedToBreak: Boolean;
FBusyUpdating: Boolean;
FBusySaving: Boolean;
FOpenSSLAvailable: Boolean;
FOnUpdate: TNotifyEvent;
FTime: QWORD;
FInterval: Cardinal;
FFileName: String;
function GetUpdateInfo(const AURL: String; var AJSON: TJSONStringType): Boolean;
procedure DoOnUpdate;
procedure Load;
procedure Save;
procedure AssignPackageData(AMetaPackage: TMetaPackage);
procedure ResetPackageData(AMetaPackage: TMetaPackage);
procedure CheckForOpenSSL;
procedure CheckForUpdates;
function IsTimeToUpdate: Boolean;
protected
procedure Execute; override;
public
constructor Create(const AFileName: String);
destructor Destroy; override;
procedure StartUpdate;
procedure StopUpdate;
published
property OnUpdate: TNotifyEvent read FOnUpdate write FOnUpdate;
end;
var
Updates: TUpdates = nil;
implementation
{ TUpdatePackage }
procedure TUpdatePackage.Clear;
var
I: Integer;
begin
FUpdatePackageData.Clear;
for I := FUpdateLazPackages.Count - 1 downto 0 do
FUpdateLazPackages.Items[I].Free;
FUpdateLazPackages.Clear;
end;
constructor TUpdatePackage.Create;
begin
FUpdatePackageData := TUpdatePackageData.Create;
FUpdateLazPackages := TCollection.Create(TUpdateLazPackages);
end;
destructor TUpdatePackage.Destroy;
var
I: Integer;
begin
FUpdatePackageData.Free;
for I := FUpdateLazPackages.Count - 1 downto 0 do
FUpdateLazPackages.Items[I].Free;
FUpdateLazPackages.Free;
inherited Destroy;
end;
function TUpdatePackage.LoadFromJSON(const AJSON: TJSONStringType): Boolean;
var
DeStreamer: TJSONDeStreamer;
begin
DeStreamer := TJSONDeStreamer.Create(nil);
try
Clear;
try
DeStreamer.JSONToObject(AJSON, Self);
Result := True;
except
on E: Exception do
begin
FLastError := E.Message;
Result := False;
end;
end;
finally
DeStreamer.Free;
end;
end;
function TUpdatePackage.SaveToJSON(var AJSON: TJSONStringType): Boolean;
var
Streamer: TJSONStreamer;
begin
Result := False;
Streamer := TJSONStreamer.Create(nil);
try
Streamer.Options := Streamer.Options + [jsoUseFormatString];
try
AJSON := Streamer.ObjectToJSONString(Self);
Result := AJSON <> '';
except
on E: Exception do
begin
FLastError := E.Message;
Result := False;
end;
end;
finally
Streamer.Free;
end;
end;
{ TUpdatePackageData }
constructor TUpdatePackageData.Create;
begin
Clear;
end;
destructor TUpdatePackageData.Destroy;
begin
//
inherited Destroy;
end;
procedure TUpdatePackageData.Clear;
begin
FName := '';
FDownloadZipURL := '';
FDisableInOPM := False;
end;
{ TUpdates }
constructor TUpdates.Create(const AFileName: String);
begin
inherited Create(True);
FreeOnTerminate := True;
FFileName := AFileName;
FHTTPClient := TFPHTTPClient.Create(nil);
{$IFDEF FPC311}
FHTTPClient.IOTimeout := Options.ConTimeOut*1000;
{$ENDIF}
if Options.ProxyEnabled then
begin
FHTTPClient.Proxy.Host:= Options.ProxyServer;
FHTTPClient.Proxy.Port:= Options.ProxyPort;
FHTTPClient.Proxy.UserName:= Options.ProxyUser;
FHTTPClient.Proxy.Password:= Options.ProxyPassword;
end;
FUpdatePackage := TUpdatePackage.Create;
end;
destructor TUpdates.Destroy;
begin
FHTTPClient.Free;
FUpdatePackage.Free;
inherited Destroy;
end;
procedure TUpdates.Load;
var
PackageCount: Integer;
LazarusPkgCount: Integer;
I, J: Integer;
Path, SubPath: String;
PackageName: String;
LazarusPkgName: String;
MetaPkg: TMetaPackage;
LazarusPkg: TLazarusPackage;
HasUpdate: Boolean;
FXML: TXMLConfig;
begin
if (not Assigned(SerializablePackages)) or (SerializablePackages.Count = 0) then
Exit;
FXML := TXMLConfig.Create(FFileName);
try
FVersion := FXML.GetValue('Version/Value', 0);
PackageCount := FXML.GetValue('Count/Value', 0);
for I := 0 to PackageCount - 1 do
begin
Path := 'Package' + IntToStr(I) + '/';
PackageName := FXML.GetValue(Path + 'Name', '');
MetaPkg := SerializablePackages.FindMetaPackage(PackageName, fpbPackageName);
if MetaPkg <> nil then
begin
HasUpdate := False;
MetaPkg.DownloadZipURL := FXML.GetValue(Path + 'DownloadZipURL', '');
MetaPkg.DisableInOPM := FXML.GetValue(Path + 'DisableInOPM', False);
MetaPkg.Rating := FXML.GetValue(Path + 'Rating', 0);
LazarusPkgCount := FXML.GetValue(Path + 'Count', 0);
for J := 0 to LazarusPkgCount - 1 do
begin
SubPath := Path + 'PackageFile' + IntToStr(J) + '/';
LazarusPkgName := FXML.GetValue(SubPath + 'Name', '');
LazarusPkg := MetaPkg.FindLazarusPackage(LazarusPkgName);
if LazarusPkg <> nil then
begin
LazarusPkg.UpdateVersion := FXML.GetValue(SubPath + 'UpdateVersion', '');
LazarusPkg.ForceNotify := FXML.GetValue(SubPath + 'ForceNotify', False);
LazarusPkg.InternalVersion := FXML.GetValue(SubPath + 'InternalVersion', 0);;
LazarusPkg.InternalVersionOld := FXML.GetValue(SubPath + 'InternalVersionOld', 0);
LazarusPkg.RefreshHasUpdate;
if not HasUpdate then
HasUpdate := (LazarusPkg.HasUpdate) and (LazarusPkg.InstalledFileVersion < LazarusPkg.UpdateVersion);
end;
end;
MetaPkg.HasUpdate := HasUpdate;
end;
end;
finally
FXML.Free;
end;
if Assigned(FOnUpdate) and (not FNeedToBreak) then
Synchronize(@DoOnUpdate);
end;
procedure TUpdates.Save;
var
I, J: Integer;
Path, SubPath: String;
MetaPkg: TMetaPackage;
LazarusPkg: TLazarusPackage;
FXML: TXMLConfig;
begin
if (not Assigned(SerializablePackages)) or (SerializablePackages.Count = 0) or (FBusySaving) then
Exit;
FBusySaving := True;
FXML := TXMLConfig.CreateClean(FFileName);
try
FXML.SetDeleteValue('Version/Value', OpkVersion, 0);
FXML.SetDeleteValue('Count/Value', SerializablePackages.Count, 0);
for I := 0 to SerializablePackages.Count - 1 do
begin
MetaPkg := SerializablePackages.Items[I];
Path := 'Package' + IntToStr(I) + '/';
FXML.SetDeleteValue(Path + 'Name', MetaPkg.Name, '');
FXML.SetDeleteValue(Path + 'DownloadZipURL', MetaPkg.DownloadZipURL, '');
FXML.SetDeleteValue(Path + 'DisableInOPM', MetaPkg.DisableInOPM, False);
FXML.SetDeleteValue(Path + 'Rating', MetaPkg.Rating, 0);
FXML.SetDeleteValue(Path + 'Count', SerializablePackages.Items[I].LazarusPackages.Count, 0);
for J := 0 to SerializablePackages.Items[I].LazarusPackages.Count - 1 do
begin
SubPath := Path + 'PackageFile' + IntToStr(J) + '/';
LazarusPkg := TLazarusPackage(SerializablePackages.Items[I].LazarusPackages.Items[J]);
FXML.SetDeleteValue(SubPath + 'Name', LazarusPkg.Name, '');
FXML.SetDeleteValue(SubPath + 'UpdateVersion', LazarusPkg.UpdateVersion, '');
FXML.SetDeleteValue(SubPath + 'ForceNotify', LazarusPkg.ForceNotify, False);
FXML.SetDeleteValue(SubPath + 'InternalVersion', LazarusPkg.InternalVersion, 0);
FXML.SetDeleteValue(SubPath + 'InternalVersionOld', LazarusPkg.InternalVersionOld, 0);
end;
end;
FXML.Flush;
finally
FXML.Free;
FBusySaving := False;
end;
end;
procedure TUpdates.AssignPackageData(AMetaPackage: TMetaPackage);
var
I: Integer;
HasUpdate: Boolean;
LazarusPkg: TLazarusPackage;
UpdLazPkgs: TUpdateLazPackages;
begin
if FBusySaving then
Exit;
HasUpdate := False;
AMetaPackage.DownloadZipURL := FUpdatePackage.FUpdatePackageData.DownloadZipURL;
AMetaPackage.DisableInOPM := FUpdatePackage.FUpdatePackageData.DisableInOPM;
for I := 0 to FUpdatePackage.FUpdateLazPackages.Count - 1 do
begin
UpdLazPkgs := TUpdateLazPackages(FUpdatePackage.FUpdateLazPackages.Items[I]);
LazarusPkg := AMetaPackage.FindLazarusPackage(UpdLazPkgs.Name);
if LazarusPkg <> nil then
begin
LazarusPkg.UpdateVersion := UpdLazPkgs.Version;
LazarusPkg.ForceNotify := UpdLazPkgs.ForceNotify;
LazarusPkg.InternalVersion := UpdLazPkgs.InternalVersion;
LazarusPkg.RefreshHasUpdate;
if not HasUpdate then
HasUpdate := (LazarusPkg.HasUpdate) and (LazarusPkg.InstalledFileVersion < LazarusPkg.UpdateVersion);
end;
end;
AMetaPackage.HasUpdate := HasUpdate;
end;
procedure TUpdates.ResetPackageData(AMetaPackage: TMetaPackage);
var
I: Integer;
LazarusPkg: TLazarusPackage;
begin
if FBusySaving then
Exit;
AMetaPackage.DownloadZipURL := '';
AMetaPackage.DisableInOPM := False;
AMetaPackage.HasUpdate := False;
for I := 0 to AMetaPackage.LazarusPackages.Count - 1 do
begin
LazarusPkg := AMetaPackage.FindLazarusPackage(TLazarusPackage(AMetaPackage.LazarusPackages.Items[I]).Name);
if LazarusPkg <> nil then
begin
LazarusPkg.HasUpdate := False;
LazarusPkg.UpdateVersion := '';
LazarusPkg.ForceNotify := False;
LazarusPkg.InternalVersion := 0;
LazarusPkg.InternalVersionOld := 0;
end;
end;
end;
procedure TUpdates.CheckForOpenSSL;
{$IFDEF MSWINDOWS}
var
ParamPath, libeaydll, ssleaydll, ZipFile: String;
UnZipper: TUnZipper;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
ParamPath := ExtractFilePath(ParamStr(0));
libeaydll := ParamPath + 'libeay32.dll';
ssleaydll := ParamPath + 'ssleay32.dll';
FOpenSSLAvailable := FileExists(libeaydll) and FileExists(ssleaydll);
if not FOpenSSLAvailable then
begin
ZipFile := ParamPath + ExtractFileName(cOpenSSLURL);
try
FHTTPClient.Get(cOpenSSLURL, ZipFile);
except
end;
if FileExists(ZipFile) then
begin
UnZipper := TUnZipper.Create;
try
try
UnZipper.FileName := ZipFile;
UnZipper.Examine;
UnZipper.UnZipAllFiles;
except
end;
finally
UnZipper.Free;
end;
DeleteFile(ZipFile);
FOpenSSLAvailable := FileExists(libeaydll) and FileExists(ssleaydll);
end;
end;
{$ELSE}
FOpenSSLAvailable := True;
{$ENDIF}
end;
function TUpdates.IsTimeToUpdate: Boolean;
begin
Result := Assigned(SerializablePackages) and (FOpenSSLAvailable) and
(not FBusyUpdating) and (not FNeedToBreak);
case Options.CheckForUpdates of
0: Result := MinutesBetween(Now, Options.LastUpdate) >= 2;
1: Result := HoursBetween(Now, Options.LastUpdate) >= 1;
2: Result := DaysBetween(Now, Options.LastUpdate) >= 1;
3: Result := WeeksBetween(Now, Options.LastUpdate) >= 1;
4: Result := MonthsBetween(Now, Options.LastUpdate) >= 1;
5: Result := False;
end;
end;
function TUpdates.GetUpdateInfo(const AURL: String; var AJSON: TJSONStringType): Boolean;
var
URL: String;
Ms: TMemoryStream;
begin
Result := False;
if Trim(AURL) = '' then
Exit;
if Pos('.json', AURL) = 0 then
Exit;
URL := FixProtocol(AURL);
Ms := TMemoryStream.Create;
try
try
FHTTPClient.AllowRedirect := True;
FHTTPClient.HTTPMethod('GET', URL, MS, []);
if FHTTPClient.ResponseStatusCode = 200 then
begin
if Ms.Size > 0 then
begin
MS.Position := 0;
SetLength(AJSON, MS.Size);
MS.Read(Pointer(AJSON)^, Length(AJSON));
Result := Length(AJSON) > 0;
{since the class name has changed form "UpdatePackageFiles" to "UpdateLazPackages",
we have to replace the references in the old JSONs(we don't have access to the files, they are
located at the developers update page.}
if Result then
AJSON := StringReplace(AJSON, 'UpdatePackageFiles', 'UpdateLazPackages', [rfReplaceAll, rfIgnoreCase]);
end;
end;
except
Result := False;
end;
finally
Ms.Free;
end;
end;
procedure TUpdates.DoOnUpdate;
begin
if Assigned(FOnUpdate) then
FOnUpdate(Self);
end;
procedure TUpdates.CheckForUpdates;
var
I: Integer;
JSON: TJSONStringType;
begin
FBusyUpdating := True;
try
Options.LastUpdate := Now;
Options.Changed := True;
for I := 0 to SerializablePackages.Count - 1 do
begin
if FNeedToBreak then
Break;
JSON := '';
if (Assigned(LazarusIDE) and LazarusIDE.IDEIsClosing) then
Break;
if GetUpdateInfo(Trim(SerializablePackages.Items[I].DownloadURL), JSON) then
begin
if FUpdatePackage.LoadFromJSON(JSON) then
AssignPackageData(SerializablePackages.Items[I])
else
ResetPackageData(SerializablePackages.Items[I]);
end
else
ResetPackageData(SerializablePackages.Items[I]);
end;
if Assigned(FOnUpdate) and (not FNeedToBreak) then
Synchronize(@DoOnUpdate);
finally
FBusyUpdating := False;
end;
end;
procedure TUpdates.Execute;
begin
while not Terminated do
begin
Sleep(1);
if (GetTickCount64 - FTime > FInterval) then
begin
FTime := GetTickCount64;
if IsTimeToUpdate then
CheckForUpdates;
end;
if FNeedToBreak then
Break;
end;
end;
procedure TUpdates.StartUpdate;
begin
Load;
CheckForOpenSSL;
FTime := GetTickCount64;
FInterval := 6000;
Start;
end;
procedure TUpdates.StopUpdate;
begin
Save;
FHTTPClient.Terminate;
FNeedToBreak := True;
end;
end.
|