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 {
57 return fetch("api/v2/app/buildInfo", {
61 .then(async (response) => {
65 const responseJSON = await response.json();
66 deepFreeze(responseJSON);
67 this.#m_store = responseJSON;
76 class PreferencesCache {
80 // onFailure: () => {},
81 // onSuccess: () => {}
83 async init(obj = {}) {
84 return fetch("api/v2/app/preferences", {
88 .then(async (response) => {
92 const responseText = await response.text();
93 const responseJSON = JSON.parse(responseText);
94 deepFreeze(responseJSON);
95 this.#m_store = responseJSON;
97 if (typeof obj.onSuccess === "function")
98 obj.onSuccess(responseJSON, responseText);
101 if (typeof obj.onFailure === "function")
102 obj.onFailure(error);
107 return this.#m_store;
112 // onFailure: () => {},
113 // onSuccess: () => {}
116 if (typeof obj !== "object")
117 throw new Error("`obj` is not an object.");
118 if (typeof obj.data !== "object")
119 throw new Error("`data` is not an object.");
121 fetch("api/v2/app/setPreferences", {
123 body: new URLSearchParams({
124 "json": JSON.stringify(obj.data)
127 .then(async (response) => {
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 const responseText = await response.text();
143 obj.onSuccess(responseText);
147 if (typeof obj.onFailure === "function")
148 obj.onFailure(error);
153 class QbtVersionCache {
157 return fetch("api/v2/app/version", {
161 .then(async (response) => {
165 const responseText = await response.text();
166 this.#m_store = responseText;
171 return this.#m_store;
177 Object.freeze(window.qBittorrent.Cache);