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,
40 const torrentPeersTable = new window.qBittorrent.DynamicTable.TorrentPeersTable();
41 let loadTorrentPeersTimer = -1;
42 let syncTorrentPeersLastResponseId = 0;
43 let show_flags = true;
45 const loadTorrentPeersData = () => {
46 if ($("propPeers").classList.contains("invisible")
47 || $("propertiesPanel_collapseToggle").classList.contains("panel-expand")) {
48 syncTorrentPeersLastResponseId = 0;
49 torrentPeersTable.clear();
52 const current_hash = torrentsTable.getCurrentTorrentID();
53 if (current_hash === "") {
54 syncTorrentPeersLastResponseId = 0;
55 torrentPeersTable.clear();
56 clearTimeout(loadTorrentPeersTimer);
59 const url = new URI("api/v2/sync/torrentPeers")
60 .setData("rid", syncTorrentPeersLastResponseId)
61 .setData("hash", current_hash);
66 .then(async (response) => {
70 const responseJSON = await response.json();
72 $("error_div").textContent = "";
74 const full_update = (responseJSON["full_update"] === true);
76 torrentPeersTable.clear();
77 if (responseJSON["rid"])
78 syncTorrentPeersLastResponseId = responseJSON["rid"];
79 if (responseJSON["peers"]) {
80 for (const key in responseJSON["peers"]) {
81 if (!Object.hasOwn(responseJSON["peers"], key))
84 responseJSON["peers"][key]["rowId"] = key;
85 torrentPeersTable.updateRowData(responseJSON["peers"][key]);
88 if (responseJSON["peers_removed"]) {
89 responseJSON["peers_removed"].each((hash) => {
90 torrentPeersTable.removeRow(hash);
93 torrentPeersTable.updateTable(full_update);
95 if (responseJSON["show_flags"]) {
96 if (show_flags !== responseJSON["show_flags"]) {
97 show_flags = responseJSON["show_flags"];
98 torrentPeersTable.columns["country"].force_hide = !show_flags;
99 torrentPeersTable.updateColumn("country");
104 torrentPeersTable.clear();
109 clearTimeout(loadTorrentPeersTimer);
110 loadTorrentPeersTimer = loadTorrentPeersData.delay(window.qBittorrent.Client.getSyncMainDataInterval());
114 const updateData = () => {
115 clearTimeout(loadTorrentPeersTimer);
116 loadTorrentPeersTimer = -1;
117 loadTorrentPeersData();
120 const clear = () => {
121 torrentPeersTable.clear();
124 const torrentPeersContextMenu = new window.qBittorrent.ContextMenu.ContextMenu({
125 targets: "#torrentPeersTableDiv",
126 menu: "torrentPeersMenu",
128 addPeer: (element, ref) => {
129 const hash = torrentsTable.getCurrentTorrentID();
135 icon: "images/qbittorrent-tray.svg",
136 title: "QBT_TR(Add Peers)QBT_TR[CONTEXT=PeersAdditionDialog]",
137 loadMethod: "iframe",
138 contentURL: "addpeers.html?hash=" + hash,
143 paddingHorizontal: 0,
148 banPeer: (element, ref) => {
149 const selectedPeers = torrentPeersTable.selectedRowsIds();
150 if (selectedPeers.length === 0)
153 if (confirm("QBT_TR(Are you sure you want to permanently ban the selected peers?)QBT_TR[CONTEXT=PeerListWidget]")) {
154 fetch("api/v2/transfer/banPeers", {
156 body: new URLSearchParams({
157 "hash": torrentsTable.getCurrentTorrentID(),
158 "peers": selectedPeers.join("|")
169 const selectedPeers = torrentPeersTable.selectedRowsIds();
171 if (selectedPeers.length >= 1) {
172 this.showItem("copyPeer");
173 this.showItem("banPeer");
176 this.hideItem("copyPeer");
177 this.hideItem("banPeer");
182 new ClipboardJS("#CopyPeerInfo", {
184 return torrentPeersTable.selectedRowsIds().join("\n");
188 torrentPeersTable.setup("torrentPeersTableDiv", "torrentPeersTableFixedHeaderDiv", torrentPeersContextMenu);
192 Object.freeze(window.qBittorrent.PropPeers);