2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2018 Thomas Piccirello <thomas.piccirello@gmail.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
31 window
.qBittorrent
??= {};
32 window
.qBittorrent
.PropPeers
??= (() => {
33 const exports
= () => {
35 updateData
: updateData
39 const torrentPeersTable
= new window
.qBittorrent
.DynamicTable
.TorrentPeersTable();
40 let loadTorrentPeersTimer
= -1;
41 let syncTorrentPeersLastResponseId
= 0;
42 let show_flags
= true;
44 const loadTorrentPeersData = function() {
45 if ($("propPeers").hasClass("invisible")
46 || $("propertiesPanel_collapseToggle").hasClass("panel-expand")) {
47 syncTorrentPeersLastResponseId
= 0;
48 torrentPeersTable
.clear();
51 const current_hash
= torrentsTable
.getCurrentTorrentID();
52 if (current_hash
=== "") {
53 syncTorrentPeersLastResponseId
= 0;
54 torrentPeersTable
.clear();
55 clearTimeout(loadTorrentPeersTimer
);
56 loadTorrentPeersTimer
= loadTorrentPeersData
.delay(window
.qBittorrent
.Client
.getSyncMainDataInterval());
59 const url
= new URI("api/v2/sync/torrentPeers");
60 url
.setData("rid", syncTorrentPeersLastResponseId
);
61 url
.setData("hash", current_hash
);
66 onComplete: function() {
67 clearTimeout(loadTorrentPeersTimer
);
68 loadTorrentPeersTimer
= loadTorrentPeersData
.delay(window
.qBittorrent
.Client
.getSyncMainDataInterval());
70 onSuccess: function(response
) {
71 $("error_div").textContent
= "";
73 const full_update
= (response
["full_update"] === true);
75 torrentPeersTable
.clear();
77 syncTorrentPeersLastResponseId
= response
["rid"];
78 if (response
["peers"]) {
79 for (const key
in response
["peers"]) {
80 if (!Object
.hasOwn(response
["peers"], key
))
83 response
["peers"][key
]["rowId"] = key
;
84 torrentPeersTable
.updateRowData(response
["peers"][key
]);
87 if (response
["peers_removed"]) {
88 response
["peers_removed"].each((hash
) => {
89 torrentPeersTable
.removeRow(hash
);
92 torrentPeersTable
.updateTable(full_update
);
94 if (response
["show_flags"]) {
95 if (show_flags
!== response
["show_flags"]) {
96 show_flags
= response
["show_flags"];
97 torrentPeersTable
.columns
["country"].force_hide
= !show_flags
;
98 torrentPeersTable
.updateColumn("country");
103 torrentPeersTable
.clear();
109 const updateData = function() {
110 clearTimeout(loadTorrentPeersTimer
);
111 loadTorrentPeersTimer
= -1;
112 loadTorrentPeersData();
115 const torrentPeersContextMenu
= new window
.qBittorrent
.ContextMenu
.ContextMenu({
116 targets
: "#torrentPeersTableDiv",
117 menu
: "torrentPeersMenu",
119 addPeer: function(element
, ref
) {
120 const hash
= torrentsTable
.getCurrentTorrentID();
126 icon
: "images/qbittorrent-tray.svg",
127 title
: "QBT_TR(Add Peers)QBT_TR[CONTEXT=PeersAdditionDialog]",
128 loadMethod
: "iframe",
129 contentURL
: "addpeers.html?hash=" + hash
,
134 paddingHorizontal
: 0,
139 banPeer: function(element
, ref
) {
140 const selectedPeers
= torrentPeersTable
.selectedRowsIds();
141 if (selectedPeers
.length
=== 0)
144 if (confirm("QBT_TR(Are you sure you want to permanently ban the selected peers?)QBT_TR[CONTEXT=PeerListWidget]")) {
146 url
: "api/v2/transfer/banPeers",
149 hash
: torrentsTable
.getCurrentTorrentID(),
150 peers
: selectedPeers
.join("|")
161 const selectedPeers
= torrentPeersTable
.selectedRowsIds();
163 if (selectedPeers
.length
>= 1) {
164 this.showItem("copyPeer");
165 this.showItem("banPeer");
168 this.hideItem("copyPeer");
169 this.hideItem("banPeer");
174 new ClipboardJS("#CopyPeerInfo", {
175 text: function(trigger
) {
176 return torrentPeersTable
.selectedRowsIds().join("\n");
180 torrentPeersTable
.setup("torrentPeersTableDiv", "torrentPeersTableFixedHeaderDiv", torrentPeersContextMenu
);
184 Object
.freeze(window
.qBittorrent
.PropPeers
);