Display External IP Address in status bar
[qBittorrent.git] / src / webui / www / private / scripts / prop-peers.js
blob40dbca28f3559e99591181e787c5c16eff918636
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2018  Thomas Piccirello <thomas.piccirello@gmail.com>
4  *
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.
9  *
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.
14  *
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.
18  *
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.
27  */
29 "use strict";
31 window.qBittorrent ??= {};
32 window.qBittorrent.PropPeers ??= (() => {
33     const exports = () => {
34         return {
35             updateData: updateData,
36             clear: clear
37         };
38     };
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").hasClass("invisible")
47             || $("propertiesPanel_collapseToggle").hasClass("panel-expand")) {
48             syncTorrentPeersLastResponseId = 0;
49             torrentPeersTable.clear();
50             return;
51         }
52         const current_hash = torrentsTable.getCurrentTorrentID();
53         if (current_hash === "") {
54             syncTorrentPeersLastResponseId = 0;
55             torrentPeersTable.clear();
56             clearTimeout(loadTorrentPeersTimer);
57             return;
58         }
59         const url = new URI("api/v2/sync/torrentPeers");
60         url.setData("rid", syncTorrentPeersLastResponseId);
61         url.setData("hash", current_hash);
62         new Request.JSON({
63             url: url,
64             method: "get",
65             noCache: true,
66             onComplete: () => {
67                 clearTimeout(loadTorrentPeersTimer);
68                 loadTorrentPeersTimer = loadTorrentPeersData.delay(window.qBittorrent.Client.getSyncMainDataInterval());
69             },
70             onSuccess: (response) => {
71                 $("error_div").textContent = "";
72                 if (response) {
73                     const full_update = (response["full_update"] === true);
74                     if (full_update)
75                         torrentPeersTable.clear();
76                     if (response["rid"])
77                         syncTorrentPeersLastResponseId = response["rid"];
78                     if (response["peers"]) {
79                         for (const key in response["peers"]) {
80                             if (!Object.hasOwn(response["peers"], key))
81                                 continue;
83                             response["peers"][key]["rowId"] = key;
84                             torrentPeersTable.updateRowData(response["peers"][key]);
85                         }
86                     }
87                     if (response["peers_removed"]) {
88                         response["peers_removed"].each((hash) => {
89                             torrentPeersTable.removeRow(hash);
90                         });
91                     }
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");
99                         }
100                     }
101                 }
102                 else {
103                     torrentPeersTable.clear();
104                 }
105             }
106         }).send();
107     };
109     const updateData = () => {
110         clearTimeout(loadTorrentPeersTimer);
111         loadTorrentPeersTimer = -1;
112         loadTorrentPeersData();
113     };
115     const clear = () => {
116         torrentPeersTable.clear();
117     };
119     const torrentPeersContextMenu = new window.qBittorrent.ContextMenu.ContextMenu({
120         targets: "#torrentPeersTableDiv",
121         menu: "torrentPeersMenu",
122         actions: {
123             addPeer: (element, ref) => {
124                 const hash = torrentsTable.getCurrentTorrentID();
125                 if (!hash)
126                     return;
128                 new MochaUI.Window({
129                     id: "addPeersPage",
130                     icon: "images/qbittorrent-tray.svg",
131                     title: "QBT_TR(Add Peers)QBT_TR[CONTEXT=PeersAdditionDialog]",
132                     loadMethod: "iframe",
133                     contentURL: "addpeers.html?hash=" + hash,
134                     scrollbars: false,
135                     resizable: false,
136                     maximizable: false,
137                     paddingVertical: 0,
138                     paddingHorizontal: 0,
139                     width: 350,
140                     height: 260
141                 });
142             },
143             banPeer: (element, ref) => {
144                 const selectedPeers = torrentPeersTable.selectedRowsIds();
145                 if (selectedPeers.length === 0)
146                     return;
148                 if (confirm("QBT_TR(Are you sure you want to permanently ban the selected peers?)QBT_TR[CONTEXT=PeerListWidget]")) {
149                     new Request({
150                         url: "api/v2/transfer/banPeers",
151                         method: "post",
152                         data: {
153                             hash: torrentsTable.getCurrentTorrentID(),
154                             peers: selectedPeers.join("|")
155                         }
156                     }).send();
157                 }
158             }
159         },
160         offsets: {
161             x: 0,
162             y: 2
163         },
164         onShow: function() {
165             const selectedPeers = torrentPeersTable.selectedRowsIds();
167             if (selectedPeers.length >= 1) {
168                 this.showItem("copyPeer");
169                 this.showItem("banPeer");
170             }
171             else {
172                 this.hideItem("copyPeer");
173                 this.hideItem("banPeer");
174             }
175         }
176     });
178     new ClipboardJS("#CopyPeerInfo", {
179         text: (trigger) => {
180             return torrentPeersTable.selectedRowsIds().join("\n");
181         }
182     });
184     torrentPeersTable.setup("torrentPeersTableDiv", "torrentPeersTableFixedHeaderDiv", torrentPeersContextMenu);
186     return exports();
187 })();
188 Object.freeze(window.qBittorrent.PropPeers);