Enable customizing the save statistics time interval
[qBittorrent.git] / src / webui / www / private / scripts / prop-trackers.js
blob5aa9ae7b7a4604f9418e9209e665628623875f0c
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2009  Christophe Dumez <chris@qbittorrent.org>
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.PropTrackers ??= (() => {
33     const exports = () => {
34         return {
35             updateData: updateData
36         };
37     };
39     let current_hash = "";
41     const torrentTrackersTable = new window.qBittorrent.DynamicTable.TorrentTrackersTable();
42     let loadTrackersDataTimer = -1;
44     const loadTrackersData = function() {
45         if ($("propTrackers").hasClass("invisible")
46             || $("propertiesPanel_collapseToggle").hasClass("panel-expand")) {
47             // Tab changed, don't do anything
48             return;
49         }
50         const new_hash = torrentsTable.getCurrentTorrentID();
51         if (new_hash === "") {
52             torrentTrackersTable.clear();
53             clearTimeout(loadTrackersDataTimer);
54             loadTrackersDataTimer = loadTrackersData.delay(10000);
55             return;
56         }
57         if (new_hash !== current_hash) {
58             torrentTrackersTable.clear();
59             current_hash = new_hash;
60         }
61         const url = new URI("api/v2/torrents/trackers?hash=" + current_hash);
62         new Request.JSON({
63             url: url,
64             method: "get",
65             noCache: true,
66             onComplete: function() {
67                 clearTimeout(loadTrackersDataTimer);
68                 loadTrackersDataTimer = loadTrackersData.delay(10000);
69             },
70             onSuccess: function(trackers) {
71                 const selectedTrackers = torrentTrackersTable.selectedRowsIds();
72                 torrentTrackersTable.clear();
74                 if (trackers) {
75                     trackers.each((tracker) => {
76                         let status;
77                         switch (tracker.status) {
78                             case 0:
79                                 status = "QBT_TR(Disabled)QBT_TR[CONTEXT=TrackerListWidget]";
80                                 break;
81                             case 1:
82                                 status = "QBT_TR(Not contacted yet)QBT_TR[CONTEXT=TrackerListWidget]";
83                                 break;
84                             case 2:
85                                 status = "QBT_TR(Working)QBT_TR[CONTEXT=TrackerListWidget]";
86                                 break;
87                             case 3:
88                                 status = "QBT_TR(Updating...)QBT_TR[CONTEXT=TrackerListWidget]";
89                                 break;
90                             case 4:
91                                 status = "QBT_TR(Not working)QBT_TR[CONTEXT=TrackerListWidget]";
92                                 break;
93                         }
95                         const row = {
96                             rowId: tracker.url,
97                             tier: (tracker.tier >= 0) ? tracker.tier : "",
98                             url: tracker.url,
99                             status: status,
100                             peers: (tracker.num_peers >= 0) ? tracker.num_peers : "QBT_TR(N/A)QBT_TR[CONTEXT=TrackerListWidget]",
101                             seeds: (tracker.num_seeds >= 0) ? tracker.num_seeds : "QBT_TR(N/A)QBT_TR[CONTEXT=TrackerListWidget]",
102                             leeches: (tracker.num_leeches >= 0) ? tracker.num_leeches : "QBT_TR(N/A)QBT_TR[CONTEXT=TrackerListWidget]",
103                             downloaded: (tracker.num_downloaded >= 0) ? tracker.num_downloaded : "QBT_TR(N/A)QBT_TR[CONTEXT=TrackerListWidget]",
104                             message: tracker.msg
105                         };
107                         torrentTrackersTable.updateRowData(row);
108                     });
110                     torrentTrackersTable.updateTable(false);
112                     if (selectedTrackers.length > 0)
113                         torrentTrackersTable.reselectRows(selectedTrackers);
114                 }
115             }
116         }).send();
117     };
119     const updateData = function() {
120         clearTimeout(loadTrackersDataTimer);
121         loadTrackersDataTimer = -1;
122         loadTrackersData();
123     };
125     const torrentTrackersContextMenu = new window.qBittorrent.ContextMenu.ContextMenu({
126         targets: "#torrentTrackersTableDiv",
127         menu: "torrentTrackersMenu",
128         actions: {
129             AddTracker: function(element, ref) {
130                 addTrackerFN();
131             },
132             EditTracker: function(element, ref) {
133                 // only allow editing of one row
134                 element.firstChild.click();
135                 editTrackerFN(element);
136             },
137             RemoveTracker: function(element, ref) {
138                 removeTrackerFN(element);
139             }
140         },
141         offsets: {
142             x: -15,
143             y: 2
144         },
145         onShow: function() {
146             const selectedTrackers = torrentTrackersTable.selectedRowsIds();
147             const containsStaticTracker = selectedTrackers.some((tracker) => {
148                 return (tracker.indexOf("** [") === 0);
149             });
151             if (containsStaticTracker || (selectedTrackers.length === 0)) {
152                 this.hideItem("EditTracker");
153                 this.hideItem("RemoveTracker");
154                 this.hideItem("CopyTrackerUrl");
155             }
156             else {
157                 this.showItem("EditTracker");
158                 this.showItem("RemoveTracker");
159                 this.showItem("CopyTrackerUrl");
160             }
161         }
162     });
164     const addTrackerFN = function() {
165         if (current_hash.length === 0)
166             return;
167         new MochaUI.Window({
168             id: "trackersPage",
169             icon: "images/qbittorrent-tray.svg",
170             title: "QBT_TR(Add trackers)QBT_TR[CONTEXT=TrackersAdditionDialog]",
171             loadMethod: "iframe",
172             contentURL: "addtrackers.html?hash=" + current_hash,
173             scrollbars: true,
174             resizable: false,
175             maximizable: false,
176             closable: true,
177             paddingVertical: 0,
178             paddingHorizontal: 0,
179             width: 500,
180             height: 250,
181             onCloseComplete: function() {
182                 updateData();
183             }
184         });
185     };
187     const editTrackerFN = function(element) {
188         if (current_hash.length === 0)
189             return;
191         const trackerUrl = encodeURIComponent(element.childNodes[1].textContent);
192         new MochaUI.Window({
193             id: "trackersPage",
194             icon: "images/qbittorrent-tray.svg",
195             title: "QBT_TR(Tracker editing)QBT_TR[CONTEXT=TrackerListWidget]",
196             loadMethod: "iframe",
197             contentURL: "edittracker.html?hash=" + current_hash + "&url=" + trackerUrl,
198             scrollbars: true,
199             resizable: false,
200             maximizable: false,
201             closable: true,
202             paddingVertical: 0,
203             paddingHorizontal: 0,
204             width: 500,
205             height: 150,
206             onCloseComplete: function() {
207                 updateData();
208             }
209         });
210     };
212     const removeTrackerFN = function(element) {
213         if (current_hash.length === 0)
214             return;
216         const selectedTrackers = torrentTrackersTable.selectedRowsIds();
217         new Request({
218             url: "api/v2/torrents/removeTrackers",
219             method: "post",
220             data: {
221                 hash: current_hash,
222                 urls: selectedTrackers.join("|")
223             },
224             onSuccess: function() {
225                 updateData();
226             }
227         }).send();
228     };
230     new ClipboardJS("#CopyTrackerUrl", {
231         text: function(trigger) {
232             return torrentTrackersTable.selectedRowsIds().join("\n");
233         }
234     });
236     torrentTrackersTable.setup("torrentTrackersTableDiv", "torrentTrackersTableFixedHeaderDiv", torrentTrackersContextMenu);
238     return exports();
239 })();
240 Object.freeze(window.qBittorrent.PropTrackers);