Allow to refresh existing search
[qBittorrent.git] / src / webui / www / private / scripts / prop-general.js
blobdbf80e8a91cba0f3a6d7b139b4c5900b1d61eb69
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.PropGeneral ??= (() => {
33     const exports = () => {
34         return {
35             updateData: updateData,
36             clear: clear
37         };
38     };
40     const piecesBar = new window.qBittorrent.PiecesBar.PiecesBar([], {
41         height: 18
42     });
43     $("progress").appendChild(piecesBar);
45     const clearData = () => {
46         document.getElementById("progressPercentage").textContent = "";
47         $("time_elapsed").textContent = "";
48         $("eta").textContent = "";
49         $("nb_connections").textContent = "";
50         $("total_downloaded").textContent = "";
51         $("total_uploaded").textContent = "";
52         $("dl_speed").textContent = "";
53         $("up_speed").textContent = "";
54         $("dl_limit").textContent = "";
55         $("up_limit").textContent = "";
56         $("total_wasted").textContent = "";
57         $("seeds").textContent = "";
58         $("peers").textContent = "";
59         $("share_ratio").textContent = "";
60         $("popularity").textContent = "";
61         $("reannounce").textContent = "";
62         $("last_seen").textContent = "";
63         $("total_size").textContent = "";
64         $("pieces").textContent = "";
65         $("created_by").textContent = "";
66         $("addition_date").textContent = "";
67         $("completion_date").textContent = "";
68         $("creation_date").textContent = "";
69         $("torrent_hash_v1").textContent = "";
70         $("torrent_hash_v2").textContent = "";
71         $("save_path").textContent = "";
72         $("comment").textContent = "";
73         $("private").textContent = "";
74         piecesBar.clear();
75     };
77     let loadTorrentDataTimer = -1;
78     const loadTorrentData = () => {
79         if ($("propGeneral").classList.contains("invisible")
80             || $("propertiesPanel_collapseToggle").classList.contains("panel-expand")) {
81             // Tab changed, don't do anything
82             return;
83         }
84         const current_id = torrentsTable.getCurrentTorrentID();
85         if (current_id === "") {
86             clearData();
87             clearTimeout(loadTorrentDataTimer);
88             return;
89         }
91         const propertiesURL = new URL("api/v2/torrents/properties", window.location);
92         propertiesURL.search = new URLSearchParams({
93             hash: current_id
94         });
95         fetch(propertiesURL, {
96                 method: "GET",
97                 cache: "no-store"
98             })
99             .then(async (response) => {
100                 if (!response.ok) {
101                     $("error_div").textContent = "QBT_TR(qBittorrent client is not reachable)QBT_TR[CONTEXT=HttpServer]";
102                     clearTimeout(loadTorrentDataTimer);
103                     loadTorrentDataTimer = loadTorrentData.delay(10000);
104                     return;
105                 }
107                 $("error_div").textContent = "";
109                 const data = await response.json();
110                 if (data) {
111                     // Update Torrent data
113                     document.getElementById("progressPercentage").textContent = window.qBittorrent.Misc.friendlyPercentage(data.progress);
115                     const timeElapsed = (data.seeding_time > 0)
116                         ? "QBT_TR(%1 (seeded for %2))QBT_TR[CONTEXT=PropertiesWidget]"
117                         .replace("%1", window.qBittorrent.Misc.friendlyDuration(data.time_elapsed))
118                         .replace("%2", window.qBittorrent.Misc.friendlyDuration(data.seeding_time))
119                         : window.qBittorrent.Misc.friendlyDuration(data.time_elapsed);
120                     $("time_elapsed").textContent = timeElapsed;
122                     $("eta").textContent = window.qBittorrent.Misc.friendlyDuration(data.eta, window.qBittorrent.Misc.MAX_ETA);
124                     const nbConnections = "QBT_TR(%1 (%2 max))QBT_TR[CONTEXT=PropertiesWidget]"
125                         .replace("%1", data.nb_connections)
126                         .replace("%2", ((data.nb_connections_limit < 0) ? "∞" : data.nb_connections_limit));
127                     $("nb_connections").textContent = nbConnections;
129                     const totalDownloaded = "QBT_TR(%1 (%2 this session))QBT_TR[CONTEXT=PropertiesWidget]"
130                         .replace("%1", window.qBittorrent.Misc.friendlyUnit(data.total_downloaded))
131                         .replace("%2", window.qBittorrent.Misc.friendlyUnit(data.total_downloaded_session));
132                     $("total_downloaded").textContent = totalDownloaded;
134                     const totalUploaded = "QBT_TR(%1 (%2 this session))QBT_TR[CONTEXT=PropertiesWidget]"
135                         .replace("%1", window.qBittorrent.Misc.friendlyUnit(data.total_uploaded))
136                         .replace("%2", window.qBittorrent.Misc.friendlyUnit(data.total_uploaded_session));
137                     $("total_uploaded").textContent = totalUploaded;
139                     const dlSpeed = "QBT_TR(%1 (%2 avg.))QBT_TR[CONTEXT=PropertiesWidget]"
140                         .replace("%1", window.qBittorrent.Misc.friendlyUnit(data.dl_speed, true))
141                         .replace("%2", window.qBittorrent.Misc.friendlyUnit(data.dl_speed_avg, true));
142                     $("dl_speed").textContent = dlSpeed;
144                     const upSpeed = "QBT_TR(%1 (%2 avg.))QBT_TR[CONTEXT=PropertiesWidget]"
145                         .replace("%1", window.qBittorrent.Misc.friendlyUnit(data.up_speed, true))
146                         .replace("%2", window.qBittorrent.Misc.friendlyUnit(data.up_speed_avg, true));
147                     $("up_speed").textContent = upSpeed;
149                     const dlLimit = (data.dl_limit === -1)
150                         ? "∞"
151                         : window.qBittorrent.Misc.friendlyUnit(data.dl_limit, true);
152                     $("dl_limit").textContent = dlLimit;
154                     const upLimit = (data.up_limit === -1)
155                         ? "∞"
156                         : window.qBittorrent.Misc.friendlyUnit(data.up_limit, true);
157                     $("up_limit").textContent = upLimit;
159                     $("total_wasted").textContent = window.qBittorrent.Misc.friendlyUnit(data.total_wasted);
161                     const seeds = "QBT_TR(%1 (%2 total))QBT_TR[CONTEXT=PropertiesWidget]"
162                         .replace("%1", data.seeds)
163                         .replace("%2", data.seeds_total);
164                     $("seeds").textContent = seeds;
166                     const peers = "QBT_TR(%1 (%2 total))QBT_TR[CONTEXT=PropertiesWidget]"
167                         .replace("%1", data.peers)
168                         .replace("%2", data.peers_total);
169                     $("peers").textContent = peers;
171                     $("share_ratio").textContent = data.share_ratio.toFixed(2);
173                     $("popularity").textContent = data.popularity.toFixed(2);
175                     $("reannounce").textContent = window.qBittorrent.Misc.friendlyDuration(data.reannounce);
177                     const lastSeen = (data.last_seen >= 0)
178                         ? new Date(data.last_seen * 1000).toLocaleString()
179                         : "QBT_TR(Never)QBT_TR[CONTEXT=PropertiesWidget]";
180                     $("last_seen").textContent = lastSeen;
182                     const totalSize = (data.total_size >= 0) ? window.qBittorrent.Misc.friendlyUnit(data.total_size) : "";
183                     $("total_size").textContent = totalSize;
185                     const pieces = (data.pieces_num >= 0)
186                         ? "QBT_TR(%1 x %2 (have %3))QBT_TR[CONTEXT=PropertiesWidget]"
187                         .replace("%1", data.pieces_num)
188                         .replace("%2", window.qBittorrent.Misc.friendlyUnit(data.piece_size))
189                         .replace("%3", data.pieces_have)
190                         : "";
191                     $("pieces").textContent = pieces;
193                     $("created_by").textContent = data.created_by;
195                     const additionDate = (data.addition_date >= 0)
196                         ? new Date(data.addition_date * 1000).toLocaleString()
197                         : "QBT_TR(Unknown)QBT_TR[CONTEXT=HttpServer]";
198                     $("addition_date").textContent = additionDate;
200                     const completionDate = (data.completion_date >= 0)
201                         ? new Date(data.completion_date * 1000).toLocaleString()
202                         : "";
203                     $("completion_date").textContent = completionDate;
205                     const creationDate = (data.creation_date >= 0)
206                         ? new Date(data.creation_date * 1000).toLocaleString()
207                         : "";
208                     $("creation_date").textContent = creationDate;
210                     const torrentHashV1 = (data.infohash_v1 !== "")
211                         ? data.infohash_v1
212                         : "QBT_TR(N/A)QBT_TR[CONTEXT=PropertiesWidget]";
213                     $("torrent_hash_v1").textContent = torrentHashV1;
215                     const torrentHashV2 = (data.infohash_v2 !== "")
216                         ? data.infohash_v2
217                         : "QBT_TR(N/A)QBT_TR[CONTEXT=PropertiesWidget]";
218                     $("torrent_hash_v2").textContent = torrentHashV2;
220                     $("save_path").textContent = data.save_path;
222                     $("comment").innerHTML = window.qBittorrent.Misc.parseHtmlLinks(window.qBittorrent.Misc.escapeHtml(data.comment));
224                     $("private").textContent = (data.has_metadata
225                         ? (data.private
226                             ? "QBT_TR(Yes)QBT_TR[CONTEXT=PropertiesWidget]"
227                             : "QBT_TR(No)QBT_TR[CONTEXT=PropertiesWidget]")
228                         : "QBT_TR(N/A)QBT_TR[CONTEXT=PropertiesWidget]");
229                 }
230                 else {
231                     clearData();
232                 }
233                 clearTimeout(loadTorrentDataTimer);
234                 loadTorrentDataTimer = loadTorrentData.delay(5000);
235             });
237         const pieceStatesURL = new URL("api/v2/torrents/pieceStates", window.location);
238         pieceStatesURL.search = new URLSearchParams({
239             hash: current_id
240         });
241         fetch(pieceStatesURL, {
242                 method: "GET",
243                 cache: "no-store"
244             })
245             .then(async (response) => {
246                 if (!response.ok) {
247                     $("error_div").textContent = "QBT_TR(qBittorrent client is not reachable)QBT_TR[CONTEXT=HttpServer]";
248                     clearTimeout(loadTorrentDataTimer);
249                     loadTorrentDataTimer = loadTorrentData.delay(10000);
250                     return;
251                 }
253                 $("error_div").textContent = "";
255                 const data = await response.json();
256                 if (data)
257                     piecesBar.setPieces(data);
258                 else
259                     clearData();
261                 clearTimeout(loadTorrentDataTimer);
262                 loadTorrentDataTimer = loadTorrentData.delay(5000);
263             });
264     };
266     const updateData = () => {
267         clearTimeout(loadTorrentDataTimer);
268         loadTorrentDataTimer = -1;
269         loadTorrentData();
270     };
272     const clear = () => {
273         clearData();
274     };
276     return exports();
277 })();
278 Object.freeze(window.qBittorrent.PropGeneral);