From a9746bbc9ee0a65b862ea71af483d132a49e5340 Mon Sep 17 00:00:00 2001 From: Daniel Kamil Kozar Date: Sun, 3 Dec 2023 18:16:24 +0100 Subject: [PATCH] Use torrent-get::trackerList when RPC version is 17 or newer (#69) --- main.pas | 182 +++++++++++++++++++++++++++++++------------------- rpc.pas | 6 ++ torrprops.lfm | 4 +- 3 files changed, 121 insertions(+), 71 deletions(-) diff --git a/main.pas b/main.pas index e5562fdb..47c6669f 100644 --- a/main.pas +++ b/main.pas @@ -152,6 +152,8 @@ interface sPrivateOn = 'ON'; sPrivateOff = 'OFF'; + sSeparateTiersByEmptyLine = 'Separate tiers by an empty line:'; + type { TMyHashMap example from hashmapdemo } @@ -790,6 +792,8 @@ TMainForm = class(TBaseForm) procedure SetCommentLinkColor; procedure OnThemeChanged; function CommentIsLink: Boolean; + function ArgsToTrackerList(torrentObj: TJSONObject) : TStringList; + procedure TrackerListToArgs(torrentSetArgs: TJSONObject; trackerEditField: TMemo); public procedure FillTorrentsList(list: TJSONArray); procedure FillPeersList(list: TJSONArray); @@ -3761,6 +3765,101 @@ procedure TMainForm.acTorrentPropsExecute(Sender: TObject); TorrentProps(0); end; +function TMainForm.ArgsToTrackerList(torrentObj: TJSONObject) : TStringList; +var + trlist: TStringList; + Trackers: TJSONArray; + tr: TJSONObject; + i: integer; +begin + trlist:=TStringList.Create; + if RpcObj.ShouldUseTrackerList then begin + writeln(torrentObj.Strings['trackerList']); + trlist.SetText(PChar(torrentObj.Strings['trackerList'])); + end + else begin + Trackers:=torrentObj.Arrays['trackers']; + for i:=0 to Trackers.Count - 1 do begin + tr:=Trackers[i] as TJSONObject; + trlist.AddObject(tr.Strings['announce'], TObject(PtrUInt(tr.Integers['id']))); + end; + end; + Result:=trlist; +end; + +procedure TMainForm.TrackerListToArgs(torrentSetArgs: TJSONObject; trackerEditField: TMemo); +var + AddT, EditT, DelT: TJSONArray; + sl, trlist: TStringList; + i, j: integer; + s: string; +begin + if RpcObj.ShouldUseTrackerList then begin + writeln(trackerEditField.Text); + torrentSetArgs.Add('trackerList', trackerEditField.Text); + end + else begin + sl:=TStringList.Create; + try + sl.Assign(trackerEditField.Lines); + // Removing unchanged trackers + i:=0; + while i < sl.Count do begin + s:=Trim(sl[i]); + if s = '' then begin + sl.Delete(i); + continue; + end; + j:=trlist.IndexOf(s); + if j >= 0 then begin + trlist.Delete(j); + sl.Delete(i); + continue; + end; + Inc(i); + end; + + AddT:=TJSONArray.Create; + EditT:=TJSONArray.Create; + DelT:=TJSONArray.Create; + try + for i:=0 to sl.Count - 1 do begin + s:=Trim(sl[i]); + if trlist.Count > 0 then begin + EditT.Add(PtrUInt(trlist.Objects[0])); + EditT.Add(UTF8Decode(s)); + trlist.Delete(0); + end + else + AddT.Add(UTF8Decode(s)); + end; + + for i:=0 to trlist.Count - 1 do + DelT.Add(PtrUInt(trlist.Objects[i])); + + if AddT.Count > 0 then begin + torrentSetArgs.Add('trackerAdd', AddT); + AddT:=nil; + end; + if EditT.Count > 0 then begin + torrentSetArgs.Add('trackerReplace', EditT); + EditT:=nil; + end; + if DelT.Count > 0 then begin + torrentSetArgs.Add('trackerRemove', DelT); + DelT:=nil; + end; + finally + DelT.Free; + EditT.Free; + AddT.Free; + end; + finally + sl.Free; + end; + end; +end; + procedure TMainForm.TorrentProps(PageNo: integer); const TR_RATIOLIMIT_GLOBAL = 0; // follow the global settings @@ -3772,26 +3871,33 @@ procedure TMainForm.TorrentProps(PageNo: integer); TR_IDLELIMIT_UNLIMITED = 2; // override the global settings, seeding regardless of activity var - req, args, t, tr: TJSONObject; + req, args, t: TJSONObject; i, j, id: integer; - ids, Trackers, AddT, EditT, DelT: TJSONArray; + ids: TJSONArray; TorrentIds: variant; s: string; - trlist, sl: TStringList; + trlist: TStringList; + trackerArg: string; begin gTorrentsClick(nil); id:=RpcObj.CurTorrentId; if id = 0 then exit; AppBusy; - trlist:=nil; with TTorrPropsForm.Create(Self) do try Page.ActivePageIndex:=PageNo; gTorrents.Tag:=1; gTorrents.EnsureSelectionVisible; TorrentIds:=GetSelectedTorrents; + + if RpcObj.ShouldUseTrackerList then begin + trackerArg := 'trackerList'; + txTrackers.Caption := sSeparateTiersByEmptyLine; + end + else trackerArg := 'trackers'; + args:=RpcObj.RequestInfo(id, ['downloadLimit', 'downloadLimitMode', 'downloadLimited', 'uploadLimit', 'uploadLimitMode', 'uploadLimited', - 'name', 'maxConnectedPeers', 'seedRatioMode', 'seedRatioLimit', 'seedIdleLimit', 'seedIdleMode', 'trackers']); + 'name', 'maxConnectedPeers', 'seedRatioMode', 'seedRatioLimit', 'seedIdleLimit', 'seedIdleMode', trackerArg]); if args = nil then begin CheckStatus(False); exit; @@ -3864,12 +3970,7 @@ procedure TMainForm.TorrentProps(PageNo: integer); edIdleSeedLimit.Value:=t.Integers['seedIdleLimit']; cbIdleSeedLimitClick(nil); - trlist:=TStringList.Create; - Trackers:=t.Arrays['trackers']; - for i:=0 to Trackers.Count - 1 do begin - tr:=Trackers[i] as TJSONObject; - trlist.AddObject(UTF8Decode(tr.Strings['announce']), TObject(PtrUInt(tr.Integers['id']))); - end; + trlist:=ArgsToTrackerList(t); edTrackers.Lines.Assign(trlist); end else begin @@ -3941,64 +4042,7 @@ procedure TMainForm.TorrentProps(PageNo: integer); if cbIdleSeedLimit.State = cbChecked then args.Add('seedIdleLimit', edIdleSeedLimit.Value); - sl:=TStringList.Create; - try - sl.Assign(edTrackers.Lines); - // Removing unchanged trackers - i:=0; - while i < sl.Count do begin - s:=Trim(sl[i]); - if s = '' then begin - sl.Delete(i); - continue; - end; - j:=trlist.IndexOf(s); - if j >= 0 then begin - trlist.Delete(j); - sl.Delete(i); - continue; - end; - Inc(i); - end; - - AddT:=TJSONArray.Create; - EditT:=TJSONArray.Create; - DelT:=TJSONArray.Create; - try - for i:=0 to sl.Count - 1 do begin - s:=Trim(sl[i]); - if trlist.Count > 0 then begin - EditT.Add(PtrUInt(trlist.Objects[0])); - EditT.Add(UTF8Decode(s)); - trlist.Delete(0); - end - else - AddT.Add(UTF8Decode(s)); - end; - - for i:=0 to trlist.Count - 1 do - DelT.Add(PtrUInt(trlist.Objects[i])); - - if AddT.Count > 0 then begin - args.Add('trackerAdd', AddT); - AddT:=nil; - end; - if EditT.Count > 0 then begin - args.Add('trackerReplace', EditT); - EditT:=nil; - end; - if DelT.Count > 0 then begin - args.Add('trackerRemove', DelT); - DelT:=nil; - end; - finally - DelT.Free; - EditT.Free; - AddT.Free; - end; - finally - sl.Free; - end; + TrackerListToArgs(args, edTrackers); end; args.Add('peer-limit', edPeerLimit.Value); diff --git a/rpc.pas b/rpc.pas index fc940046..2d81195e 100644 --- a/rpc.pas +++ b/rpc.pas @@ -140,6 +140,7 @@ TRpc = class function SendRequest(req: TJSONObject; ReturnArguments: boolean = True; ATimeOut: integer = -1): TJSONObject; function RequestInfo(TorrentId: integer; const Fields: array of const; const ExtraFields: array of string): TJSONObject; function RequestInfo(TorrentId: integer; const Fields: array of const): TJSONObject; + function ShouldUseTrackerList(): boolean; property Status: string read GetStatus write SetStatus; property InfoStatus: string read GetInfoStatus write SetInfoStatus; @@ -1052,5 +1053,10 @@ procedure TRpc.Disconnect; FRpcPath:=''; end; +function TRpc.ShouldUseTrackerList(): boolean; +begin + Result := FRPCVersion >= 17; +end; + end. diff --git a/torrprops.lfm b/torrprops.lfm index 852b3f7b..4e31e1a6 100644 --- a/torrprops.lfm +++ b/torrprops.lfm @@ -191,7 +191,7 @@ inherited TorrPropsForm: TTorrPropsForm end end object tabAdvanced: TTabSheet - Caption = 'Advanced' + Caption = 'Trackers' ClientHeight = 194 ClientWidth = 488 object edTrackers: TMemo @@ -209,7 +209,7 @@ inherited TorrPropsForm: TTorrPropsForm Height = 14 Top = 10 Width = 46 - Caption = 'Trackers:' + Caption = '' ParentColor = False end end