Opkman: If available load the json from local source. Useful if the internet connection is slow. Feature requested by a forum user.

git-svn-id: trunk@62678 -
This commit is contained in:
balazs 2020-02-27 10:27:12 +00:00
parent 9875a0b309
commit 8f44d34808
6 changed files with 109 additions and 36 deletions

View File

@ -265,6 +265,8 @@ resourcestring
rsOptions_tsFolders_Caption = 'Folders';
rsOptions_tsProfiles_Caption = 'Profiles';
rsOptions_lbRemoteRepository_Caption = 'Remote repository';
rsOptions_cbLoadJsonLocally_Caption = 'If available, parse the json from local source';
rsOptions_cbLoadJsonLocally_Hint = 'If this option is checked the json is parsed from a local source, useful if the internet connection is slow. After 25 local parse, OPM will attempt a live update';
rsOptions_cbForceDownloadExtract_Caption = 'Always force download and extract';
rsOptions_cbForceDownloadExtract_Hint = 'If this option is checked the packages are always re-downloaded/extracted before install';
rsOptions_lbConTimeOut_Caption = 'Connection timeout (seconds):';

View File

@ -31,7 +31,7 @@ unit opkman_downloader;
interface
uses
Classes, SysUtils, fpjson, LazIDEIntf,
Classes, SysUtils, fpjson, LazIDEIntf, md5,
// OpkMan
opkman_common, opkman_serializablepackages, opkman_const, opkman_options,
{$IFDEF FPC311}fphttpclient{$ELSE}opkman_httpclient{$ENDIF};
@ -256,6 +256,7 @@ end;
procedure TThreadDownload.DoOnJSONDownloadCompleted;
var
JSON: TJSONStringType;
JSONFile: String;
begin
if Assigned(FOnJSONComplete) then
begin
@ -263,6 +264,9 @@ begin
begin
SetLength(JSON, FMS.Size);
FMS.Read(Pointer(JSON)^, Length(JSON));
JSONFile := ExtractFilePath(LocalRepositoryConfigFile) + 'packagelist' + '_' + MD5Print(MD5String(Options.RemoteRepository[Options.ActiveRepositoryIndex])) + '.json';
FMS.Position := 0;
FMS.SaveToFile(JSONFile);
SerializablePackages.JSONToPackages(JSON);
FOnJSONComplete(Self, JSON, FErrTyp, '');
end

View File

@ -29,7 +29,7 @@ unit opkman_mainfrm;
interface
uses
Classes, SysUtils, fpjson, Graphics, laz.VirtualTrees,
Classes, SysUtils, fpjson, Graphics, laz.VirtualTrees, md5,
// LCL
Forms, Controls, Dialogs, StdCtrls, ExtCtrls, Buttons, Menus, ComCtrls, Clipbrd,
InterfaceBase, LCLIntf, LCLVersion, LCLProc, LCLPlatformDef,
@ -257,6 +257,11 @@ end;
procedure TMainFrm.GetPackageList(const ARepositoryHasChanged: Boolean = False);
var
JSONFile: String;
JSON: TJSONStringType;
MS: TMemoryStream;
SuccessfullyLoaded: Boolean;
begin
cbFilterBy.ItemIndex := 0;
cbFilterByChange(cbFilterBy);
@ -269,8 +274,41 @@ begin
SetupMessage(rsMainFrm_rsMessageChangingRepository);
Sleep(1500);
end;
SetupMessage(rsMainFrm_rsMessageDownload);
PackageDownloader.DownloadJSON(Options.ConTimeOut*1000);
SuccessfullyLoaded := False;
JSONFile := ExtractFilePath(LocalRepositoryConfigFile) + 'packagelist' + '_' + MD5Print(MD5String(Options.RemoteRepository[Options.ActiveRepositoryIndex])) + '.json';
if Options.LoadJsonLocally and (Options.LoadJsonLocallyCnt < 25) and FileExists(JSONFile) and (FileSizeUtf8(JSONFile) > 0) then
begin
MS := TMemoryStream.Create;
try
MS.LoadFromFile(JSONFile);
MS.Position := 0;
SetLength(JSON, MS.Size);
MS.Read(Pointer(JSON)^, Length(JSON));
SuccessfullyLoaded := SerializablePackages.JSONToPackages(JSON);
if SuccessfullyLoaded then
begin
DoOnJSONDownloadCompleted(Self, JSON, etNone);
Options.LoadJsonLocallyCnt := Options.LoadJsonLocallyCnt + 1;
end
else
Options.LoadJsonLocallyCnt := 25;
Options.Changed := True;
finally
MS.Free;
end;
end;
if not SuccessfullyLoaded then
begin
if Options.LoadJsonLocally then
begin
Options.LoadJsonLocallyCnt := 0;
Options.Changed := True;
end;
SetupMessage(rsMainFrm_rsMessageDownload);
PackageDownloader.DownloadJSON(Options.ConTimeOut*1000);
end;
end;
function TMainFrm.IsSomethingChecked(const AResolveDependencies: Boolean = True): Boolean;

View File

@ -57,6 +57,8 @@ type
FRemoteRepository: TStringList;
FRemoteRepositoryTmp: TStringList;
FActiveRepositoryIndex: Integer;
FLoadJsonLocally: Boolean;
FLoadJsonLocallyCnt: Integer;
FForceDownloadAndExtract: Boolean;
FDeleteZipAfterInstall: Boolean;
FIncompatiblePackages: Boolean;
@ -105,6 +107,8 @@ type
property RemoteRepository: TStringList read FRemoteRepository write FRemoteRepository;
property RemoteRepositoryTmp: TStringList read FRemoteRepositoryTmp write FRemoteRepositoryTmp;
property ActiveRepositoryIndex: Integer read FActiveRepositoryIndex write FActiveRepositoryIndex;
property LoadJsonLocally: Boolean read FLoadJsonLocally write FLoadJsonLocally;
property LoadJsonLocallyCnt: Integer read FLoadJsonLocallyCnt write FLoadJsonLocallyCnt;
property ForceDownloadAndExtract: Boolean read FForceDownloadAndExtract write FForceDownloadAndExtract;
property DeleteZipAfterInstall: Boolean read FDeleteZipAfterInstall write FDeleteZipAfterInstall;
property IncompatiblePackages: Boolean read FIncompatiblePackages write FIncompatiblePackages;
@ -196,6 +200,8 @@ begin
if Trim(FRemoteRepository.Text) = '' then
FRemoteRepository.Add(cRemoteRepository);
FActiveRepositoryIndex := FXML.GetValue('General/ActiveRepositoryIndex/Value', 0);
FLoadJsonLocally := FXML.GetValue('General/LoadJsonLocally/Value', False);
FLoadJsonLocallyCnt := FXML.GetValue('General/LoadJsonLocallyCnt/Value', 0);
FForceDownloadAndExtract := FXML.GetValue('General/ForceDownloadAndExtract/Value', True);
FDeleteZipAfterInstall := FXML.GetValue('General/DeleteZipAfterInstall/Value', True);
FIncompatiblePackages := FXML.GetValue('General/IncompatiblePackages/Value', True);
@ -235,6 +241,8 @@ begin
FXML.SetDeleteValue('Version/Value', OpkVersion, 0);
FXML.SetDeleteValue('General/RemoteRepository/Value', FRemoteRepository.Text, '');
FXML.SetDeleteValue('General/ActiveRepositoryIndex/Value', FActiveRepositoryIndex, 0);
FXML.SetDeleteValue('General/LoadJsonLocally/Value', FLoadJsonLocally, False);
FXML.SetDeleteValue('General/LoadJsonLocallyCnt/Value', FLoadJsonLocallyCnt, 0);
FXML.SetDeleteValue('General/ForceDownloadAndExtract/Value', FForceDownloadAndExtract, True);
FXML.SetDeleteValue('General/DeleteZipAfterInstall/Value', FDeleteZipAfterInstall, True);
FXML.SetDeleteValue('General/IncompatiblePackages/Value', FIncompatiblePackages, True);
@ -281,6 +289,8 @@ begin
FHintFormOptionColors.Clear;
CheckColors;
FActiveRepositoryIndex := 0;
FLoadJsonLocally := False;
FLoadJsonLocallyCnt := 0;
FForceDownloadAndExtract := True;
FDeleteZipAfterInstall := True;
FIncompatiblePackages := True;

View File

@ -1,7 +1,7 @@
object OptionsFrm: TOptionsFrm
Left = 338
Left = 399
Height = 597
Top = 131
Top = 156
Width = 644
BorderIcons = [biSystemMenu]
Caption = 'Options'
@ -84,17 +84,17 @@ object OptionsFrm: TOptionsFrm
end
object cbForceDownloadExtract: TCheckBox
AnchorSideLeft.Control = lbRemoteRepository
AnchorSideTop.Control = pnRepositories
AnchorSideTop.Control = cbLoadJsonLocally
AnchorSideTop.Side = asrBottom
Left = 6
Height = 19
Top = 56
Width = 235
BorderSpacing.Top = 6
Top = 77
Width = 236
BorderSpacing.Top = 2
Caption = ' Force download and extract of packages'
ParentShowHint = False
ShowHint = True
TabOrder = 1
TabOrder = 2
end
object cbDeleteZipAfterInstall: TCheckBox
AnchorSideLeft.Control = lbRemoteRepository
@ -102,20 +102,20 @@ object OptionsFrm: TOptionsFrm
AnchorSideTop.Side = asrBottom
Left = 6
Height = 19
Top = 77
Top = 98
Width = 294
BorderSpacing.Top = 2
Caption = 'Delete downloaded zip files after installation/update'
ParentShowHint = False
ShowHint = True
TabOrder = 2
TabOrder = 3
end
object lbUpdates: TLabel
AnchorSideLeft.Control = lbRemoteRepository
AnchorSideTop.Control = Bevel1
Left = 6
Height = 15
Top = 199
Top = 220
Width = 146
BorderSpacing.Top = 5
Caption = 'Check for package updates:'
@ -127,7 +127,7 @@ object OptionsFrm: TOptionsFrm
AnchorSideTop.Side = asrBottom
Left = 6
Height = 23
Top = 220
Top = 241
Width = 209
BorderSpacing.Top = 6
ItemHeight = 15
@ -141,7 +141,7 @@ object OptionsFrm: TOptionsFrm
'Never'
)
Style = csDropDownList
TabOrder = 4
TabOrder = 7
Text = 'Every few minutes'
end
object lbLastUpdate: TLabel
@ -151,7 +151,7 @@ object OptionsFrm: TOptionsFrm
AnchorSideTop.Side = asrCenter
Left = 230
Height = 15
Top = 224
Top = 245
Width = 64
BorderSpacing.Left = 15
Caption = 'Last update:'
@ -213,7 +213,7 @@ object OptionsFrm: TOptionsFrm
AnchorSideTop.Side = asrBottom
Left = 6
Height = 15
Top = 258
Top = 279
Width = 204
BorderSpacing.Top = 15
Caption = 'Show newly added packages for(days):'
@ -226,11 +226,11 @@ object OptionsFrm: TOptionsFrm
AnchorSideTop.Side = asrCenter
Left = 216
Height = 23
Top = 254
Top = 275
Width = 76
BorderSpacing.Left = 6
MaxValue = 365
TabOrder = 5
TabOrder = 8
Value = 31
end
object cbRegularIcons: TCheckBox
@ -239,11 +239,11 @@ object OptionsFrm: TOptionsFrm
AnchorSideTop.Side = asrBottom
Left = 6
Height = 19
Top = 283
Top = 304
Width = 232
BorderSpacing.Top = 10
Caption = 'Show regular icon for installed packages'
TabOrder = 6
TabOrder = 9
end
object cbUseDefaultTheme: TCheckBox
AnchorSideLeft.Control = lbDaysToShowNewPackages
@ -251,18 +251,18 @@ object OptionsFrm: TOptionsFrm
AnchorSideTop.Side = asrBottom
Left = 6
Height = 19
Top = 441
Top = 462
Width = 116
BorderSpacing.Top = 7
Caption = 'Use default theme'
TabOrder = 8
TabOrder = 11
end
object Bevel1: TBevel
AnchorSideTop.Control = lbConTimeOut
AnchorSideTop.Side = asrBottom
Left = 0
Height = 2
Top = 194
Top = 215
Width = 620
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Top = 25
@ -272,7 +272,7 @@ object OptionsFrm: TOptionsFrm
AnchorSideTop.Side = asrBottom
Left = 4
Height = 2
Top = 327
Top = 348
Width = 620
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Top = 25
@ -283,7 +283,7 @@ object OptionsFrm: TOptionsFrm
AnchorSideTop.Side = asrBottom
Left = 6
Height = 15
Top = 154
Top = 175
Width = 164
BorderSpacing.Top = 16
Caption = 'Connection timeout(seconds): '
@ -298,11 +298,11 @@ object OptionsFrm: TOptionsFrm
AnchorSideTop.Side = asrCenter
Left = 176
Height = 23
Top = 150
Top = 171
Width = 55
BorderSpacing.Left = 6
MinValue = 1
TabOrder = 3
TabOrder = 6
Value = 10
end
object Bevel3: TBevel
@ -310,7 +310,7 @@ object OptionsFrm: TOptionsFrm
AnchorSideTop.Side = asrBottom
Left = 4
Height = 2
Top = 432
Top = 453
Width = 620
Anchors = [akTop, akLeft, akRight]
BorderSpacing.Top = 10
@ -322,7 +322,7 @@ object OptionsFrm: TOptionsFrm
AnchorSideRight.Side = asrBottom
Left = 6
Height = 88
Top = 334
Top = 355
Width = 608
Anchors = [akTop, akLeft, akRight]
AutoFill = True
@ -344,7 +344,7 @@ object OptionsFrm: TOptionsFrm
'It''s triggered by SHIFT, moves with the mouse'
'Off'
)
TabOrder = 7
TabOrder = 10
object bColors: TButton
AnchorSideTop.Control = rbHintFormOptions
AnchorSideRight.Control = rbHintFormOptions
@ -370,13 +370,13 @@ object OptionsFrm: TOptionsFrm
AnchorSideTop.Side = asrBottom
Left = 6
Height = 19
Top = 98
Top = 119
Width = 227
BorderSpacing.Top = 2
Caption = 'Warn me about incompatible packages'
ParentShowHint = False
ShowHint = True
TabOrder = 9
TabOrder = 4
end
object cbAlreadyInstalledPackages: TCheckBox
AnchorSideLeft.Control = lbRemoteRepository
@ -384,13 +384,27 @@ object OptionsFrm: TOptionsFrm
AnchorSideTop.Side = asrBottom
Left = 6
Height = 19
Top = 119
Top = 140
Width = 242
BorderSpacing.Top = 2
Caption = 'Warn me about already installed packages'
ParentShowHint = False
ShowHint = True
TabOrder = 10
TabOrder = 5
end
object cbLoadJsonLocally: TCheckBox
AnchorSideLeft.Control = lbRemoteRepository
AnchorSideTop.Control = pnRepositories
AnchorSideTop.Side = asrBottom
Left = 6
Height = 19
Top = 56
Width = 230
BorderSpacing.Top = 6
Caption = 'If available, parse json from local source'
ParentShowHint = False
ShowHint = True
TabOrder = 1
end
end
end

View File

@ -60,6 +60,7 @@ type
bColors: TButton;
cbIncompatiblePackages: TCheckBox;
cbAlreadyInstalledPackages: TCheckBox;
cbLoadJsonLocally: TCheckBox;
cbProxy: TCheckBox;
cbForceDownloadExtract: TCheckBox;
cbDeleteZipAfterInstall: TCheckBox;
@ -420,6 +421,7 @@ begin
if Options.RemoteRepositoryTmp.Count > 0 then
Options.RemoteRepository.Text := Options.RemoteRepositoryTmp.Text;
Options.ActiveRepositoryIndex := cbRemoteRepository.ItemIndex;
Options.LoadJsonLocally := cbLoadJsonLocally.Checked;
Options.ForceDownloadAndExtract := cbForceDownloadExtract.Checked;
Options.ConTimeOut := spConTimeOut.Value;
Options.DeleteZipAfterInstall := cbDeleteZipAfterInstall.Checked;
@ -488,10 +490,13 @@ begin
for I := 0 to Options.RemoteRepository.Count - 1 do
cbRemoteRepository.Items.Add(Options.RemoteRepository.Strings[I]);
cbRemoteRepository.ItemIndex := Options.ActiveRepositoryIndex;
cbLoadJsonLocally.Checked := Options.LoadJsonLocally;
cbForceDownloadExtract.Checked := Options.ForceDownloadAndExtract;
cbDeleteZipAfterInstall.Checked := Options.DeleteZipAfterInstall;
cbIncompatiblePackages.Checked := Options.IncompatiblePackages;
cbAlreadyInstalledPackages.Checked := Options.AlreadyInstalledPackages;
cbLoadJsonLocally.Caption := rsOptions_cbLoadJsonLocally_Caption;
cbLoadJsonLocally.Hint := rsOptions_cbLoadJsonLocally_Hint;
cbForceDownloadExtract.Caption := rsOptions_cbForceDownloadExtract_Caption;
cbForceDownloadExtract.Hint := rsOptions_cbForceDownloadExtract_Hint;
lbConTimeOut.Caption := rsOptions_lbConTimeOut_Caption;