Display External IP Address in status bar
[qBittorrent.git] / src / webui / www / private / scripts / cache.js
blob67d718a1f3357caaf4489e5044de1401ffed6c5e
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2024 Mike Tzou (Chocobo1)
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.
29 "use strict";
31 window.qBittorrent ??= {};
32 window.qBittorrent.Cache ??= (() => {
33 const exports = () => {
34 return {
35 buildInfo: new BuildInfoCache(),
36 preferences: new PreferencesCache(),
37 qbtVersion: new QbtVersionCache()
41 const deepFreeze = (obj) => {
42 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze#examples
44 const keys = Reflect.ownKeys(obj);
45 for (const key of keys) {
46 const value = obj[key];
47 if ((value && (typeof value === "object")) || (typeof value === "function"))
48 deepFreeze(value);
50 Object.freeze(obj);
53 class BuildInfoCache {
54 #m_store = {};
56 init() {
57 new Request.JSON({
58 url: "api/v2/app/buildInfo",
59 method: "get",
60 noCache: true,
61 onSuccess: (responseJSON) => {
62 if (!responseJSON)
63 return;
65 deepFreeze(responseJSON);
66 this.#m_store = responseJSON;
68 }).send();
71 get() {
72 return this.#m_store;
76 class PreferencesCache {
77 #m_store = {};
79 // obj: {
80 // onFailure: () => {},
81 // onSuccess: () => {}
82 // }
83 init(obj = {}) {
84 new Request.JSON({
85 url: "api/v2/app/preferences",
86 method: "get",
87 noCache: true,
88 onFailure: (xhr) => {
89 if (typeof obj.onFailure === "function")
90 obj.onFailure(xhr);
92 onSuccess: (responseJSON, responseText) => {
93 if (!responseJSON)
94 return;
96 deepFreeze(responseJSON);
97 this.#m_store = responseJSON;
99 if (typeof obj.onSuccess === "function")
100 obj.onSuccess(responseJSON, responseText);
102 }).send();
105 get() {
106 return this.#m_store;
109 // obj: {
110 // data: {},
111 // onFailure: () => {},
112 // onSuccess: () => {}
113 // }
114 set(obj) {
115 if (typeof obj !== "object")
116 throw new Error("`obj` is not an object.");
117 if (typeof obj.data !== "object")
118 throw new Error("`data` is not an object.");
120 new Request({
121 url: "api/v2/app/setPreferences",
122 method: "post",
123 data: {
124 "json": JSON.stringify(obj.data)
126 onFailure: (xhr) => {
127 if (typeof obj.onFailure === "function")
128 obj.onFailure(xhr);
130 onSuccess: (responseText, responseXML) => {
131 this.#m_store = structuredClone(this.#m_store);
132 for (const key in obj.data) {
133 if (!Object.hasOwn(obj.data, key))
134 continue;
136 const value = obj.data[key];
137 this.#m_store[key] = value;
139 deepFreeze(this.#m_store);
141 if (typeof obj.onSuccess === "function")
142 obj.onSuccess(responseText, responseXML);
144 }).send();
148 class QbtVersionCache {
149 #m_store = "";
151 init() {
152 new Request({
153 url: "api/v2/app/version",
154 method: "get",
155 noCache: true,
156 onSuccess: (responseText) => {
157 if (!responseText)
158 return;
159 this.#m_store = responseText;
161 }).send();
164 get() {
165 return this.#m_store;
169 return exports();
170 })();
171 Object.freeze(window.qBittorrent.Cache);