Skip to content

Commit

Permalink
Use torrent-get::trackerList when RPC version is 17 or newer (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
lighterowl authored Dec 3, 2023
1 parent baf2594 commit a9746bb
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 71 deletions.
182 changes: 113 additions & 69 deletions main.pas
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ interface
sPrivateOn = 'ON';
sPrivateOff = 'OFF';

sSeparateTiersByEmptyLine = 'Separate tiers by an empty line:';

type

{ TMyHashMap example from hashmapdemo }
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions rpc.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1052,5 +1053,10 @@ procedure TRpc.Disconnect;
FRpcPath:='';
end;

function TRpc.ShouldUseTrackerList(): boolean;
begin
Result := FRPCVersion >= 17;
end;

end.

4 changes: 2 additions & 2 deletions torrprops.lfm
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ inherited TorrPropsForm: TTorrPropsForm
end
end
object tabAdvanced: TTabSheet
Caption = 'Advanced'
Caption = 'Trackers'
ClientHeight = 194
ClientWidth = 488
object edTrackers: TMemo
Expand All @@ -209,7 +209,7 @@ inherited TorrPropsForm: TTorrPropsForm
Height = 14
Top = 10
Width = 46
Caption = 'Trackers:'
Caption = ''
ParentColor = False
end
end
Expand Down

0 comments on commit a9746bb

Please sign in to comment.