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.
31 window.qBittorrent ??= {};
32 window.qBittorrent.Cache ??= (() => {
33 const exports = () => {
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"))
53 class BuildInfoCache {
58 url: "api/v2/app/buildInfo",
61 onSuccess: (responseJSON) => {
65 deepFreeze(responseJSON);
66 this.#m_store = responseJSON;
76 class PreferencesCache {
80 // onFailure: () => {},
81 // onSuccess: () => {}
85 url: "api/v2/app/preferences",
89 if (typeof obj.onFailure === "function")
92 onSuccess: (responseJSON, responseText) => {
96 deepFreeze(responseJSON);
97 this.#m_store = responseJSON;
99 if (typeof obj.onSuccess === "function")
100 obj.onSuccess(responseJSON, responseText);
106 return this.#m_store;
111 // onFailure: () => {},
112 // onSuccess: () => {}
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.");
121 url: "api/v2/app/setPreferences",
124 "json": JSON.stringify(obj.data)
126 onFailure: (xhr) => {
127 if (typeof obj.onFailure === "function")
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))
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);
148 class QbtVersionCache {
153 url: "api/v2/app/version",
156 onSuccess: (responseText) => {
159 this.#m_store = responseText;
165 return this.#m_store;
171 Object.freeze(window.qBittorrent.Cache);