mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-08-22 22:33:34 -07:00
parent
2f34c9b2f0
commit
6830e32c72
3 changed files with 42 additions and 71 deletions
|
@ -76,12 +76,8 @@ window.qBittorrent.Cache ??= (() => {
|
|||
class PreferencesCache {
|
||||
#m_store = {};
|
||||
|
||||
// obj: {
|
||||
// onFailure: () => {},
|
||||
// onSuccess: () => {}
|
||||
// }
|
||||
init(obj = {}) {
|
||||
return fetch("api/v2/app/preferences", {
|
||||
async init() {
|
||||
return await fetch("api/v2/app/preferences", {
|
||||
method: "GET",
|
||||
cache: "no-store"
|
||||
})
|
||||
|
@ -94,12 +90,7 @@ window.qBittorrent.Cache ??= (() => {
|
|||
deepFreeze(responseJSON);
|
||||
this.#m_store = responseJSON;
|
||||
|
||||
if (typeof obj.onSuccess === "function")
|
||||
obj.onSuccess(responseJSON, responseText);
|
||||
},
|
||||
(error) => {
|
||||
if (typeof obj.onFailure === "function")
|
||||
obj.onFailure(error);
|
||||
return responseJSON;
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -107,45 +98,29 @@ window.qBittorrent.Cache ??= (() => {
|
|||
return this.#m_store;
|
||||
}
|
||||
|
||||
// obj: {
|
||||
// data: {},
|
||||
// onFailure: () => {},
|
||||
// onSuccess: () => {}
|
||||
// }
|
||||
set(obj) {
|
||||
if (typeof obj !== "object")
|
||||
throw new Error("`obj` is not an object.");
|
||||
if (typeof obj.data !== "object")
|
||||
async set(data) {
|
||||
if (typeof data !== "object")
|
||||
throw new Error("`data` is not an object.");
|
||||
|
||||
fetch("api/v2/app/setPreferences", {
|
||||
return await fetch("api/v2/app/setPreferences", {
|
||||
method: "POST",
|
||||
body: new URLSearchParams({
|
||||
json: JSON.stringify(obj.data)
|
||||
json: JSON.stringify(data)
|
||||
})
|
||||
})
|
||||
.then(async (response) => {
|
||||
.then((response) => {
|
||||
if (!response.ok)
|
||||
return;
|
||||
|
||||
this.#m_store = structuredClone(this.#m_store);
|
||||
for (const key in obj.data) {
|
||||
if (!Object.hasOwn(obj.data, key))
|
||||
for (const key in data) {
|
||||
if (!Object.hasOwn(data, key))
|
||||
continue;
|
||||
|
||||
const value = obj.data[key];
|
||||
const value = data[key];
|
||||
this.#m_store[key] = value;
|
||||
}
|
||||
deepFreeze(this.#m_store);
|
||||
|
||||
if (typeof obj.onSuccess === "function") {
|
||||
const responseText = await response.text();
|
||||
obj.onSuccess(responseText);
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
if (typeof obj.onFailure === "function")
|
||||
obj.onFailure(error);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -58,13 +58,10 @@
|
|||
// Set current "Delete files" choice as the default
|
||||
rememberButton.addEventListener("click", (e) => {
|
||||
window.qBittorrent.Cache.preferences.set({
|
||||
data: {
|
||||
delete_torrent_content_files: deleteCB.checked
|
||||
},
|
||||
onSuccess: () => {
|
||||
}).then(() => {
|
||||
prefDeleteContentFiles = deleteCB.checked;
|
||||
setRememberBtnEnabled(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -2228,8 +2228,8 @@ Use ';' to split multiple entries. Can use wildcard '*'.)QBT_TR[CONTEXT=OptionsD
|
|||
};
|
||||
|
||||
const loadPreferences = () => {
|
||||
window.parent.qBittorrent.Cache.preferences.init({
|
||||
onSuccess: (pref) => {
|
||||
window.parent.qBittorrent.Cache.preferences.init()
|
||||
.then((pref) => {
|
||||
// Behavior tab
|
||||
// Language
|
||||
updateWebuiLocaleSelect(pref.locale);
|
||||
|
@ -2650,7 +2650,9 @@ Use ';' to split multiple entries. Can use wildcard '*'.)QBT_TR[CONTEXT=OptionsD
|
|||
document.getElementById("i2pOutboundQuantity").value = pref.i2p_outbound_quantity;
|
||||
document.getElementById("i2pInboundLength").value = pref.i2p_inbound_length;
|
||||
document.getElementById("i2pOutboundLength").value = pref.i2p_outbound_length;
|
||||
}
|
||||
}).catch((error) => {
|
||||
console.error(error);
|
||||
alert("QBT_TR(Unable to load program preferences, qBittorrent is probably unreachable.)QBT_TR[CONTEXT=HttpServer]");
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -3169,17 +3171,14 @@ Use ';' to split multiple entries. Can use wildcard '*'.)QBT_TR[CONTEXT=OptionsD
|
|||
settings["i2p_outbound_length"] = Number(document.getElementById("i2pOutboundLength").value);
|
||||
|
||||
// Send it to qBT
|
||||
window.parent.qBittorrent.Cache.preferences.set({
|
||||
data: settings,
|
||||
onFailure: () => {
|
||||
alert("QBT_TR(Unable to save program preferences, qBittorrent is probably unreachable.)QBT_TR[CONTEXT=HttpServer]");
|
||||
window.parent.qBittorrent.Client.closeWindow(document.getElementById("preferencesPage"));
|
||||
},
|
||||
onSuccess: () => {
|
||||
window.parent.qBittorrent.Cache.preferences.set(settings)
|
||||
.then(() => {
|
||||
// Close window
|
||||
window.parent.location.reload();
|
||||
window.parent.qBittorrent.Client.closeWindow(document.getElementById("preferencesPage"));
|
||||
}
|
||||
}).catch((error) => {
|
||||
alert("QBT_TR(Unable to save program preferences, qBittorrent is probably unreachable.)QBT_TR[CONTEXT=HttpServer]");
|
||||
window.parent.qBittorrent.Client.closeWindow(document.getElementById("preferencesPage"));
|
||||
});
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue