-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
WebUI: Store durable settings in client data API #23191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Bittorrent Client using Qt and libtorrent. | ||
* Copyright (C) 2025 Thomas Piccirello <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; either version 2 | ||
* of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* | ||
* In addition, as a special exception, the copyright holders give permission to | ||
* link this program with the OpenSSL project's "OpenSSL" library (or with | ||
* modified versions of it that use the same license as the "OpenSSL" library), | ||
* and distribute the linked executables. You must obey the GNU General Public | ||
* License in all respects for all of the code used other than "OpenSSL". If you | ||
* modify file(s), you may extend this exception to your version of the file(s), | ||
* but you are not obligated to do so. If you do not wish to do so, delete this | ||
* exception statement from your version. | ||
*/ | ||
|
||
"use strict"; | ||
|
||
window.qBittorrent ??= {}; | ||
window.qBittorrent.ClientData ??= (() => { | ||
const exports = () => { | ||
return new ClientData(); | ||
}; | ||
|
||
// this is exposed as a singleton | ||
class ClientData { | ||
/** | ||
* @type Map<string, any> | ||
*/ | ||
#cache = new Map(); | ||
|
||
#keyPrefix = "qbt_"; | ||
|
||
#addKeyPrefix(data) { | ||
return Object.fromEntries(Object.entries(data).map(([key, value]) => ([`${this.#keyPrefix}${key}`, value]))); | ||
} | ||
|
||
#removeKeyPrefix(data) { | ||
return Object.fromEntries(Object.entries(data).map(([key, value]) => ([key.substring(this.#keyPrefix.length), value]))); | ||
} | ||
|
||
/** | ||
* @param {string[]} keys | ||
* @returns {Record<string, any>} | ||
*/ | ||
async #fetch(keys) { | ||
Piccirello marked this conversation as resolved.
Show resolved
Hide resolved
|
||
keys = keys.map(key => `${this.#keyPrefix}${key}`); | ||
return await fetch("api/v2/clientdata/load", { | ||
method: "POST", | ||
body: new URLSearchParams({ | ||
keys: JSON.stringify(keys) | ||
}) | ||
}) | ||
.then(async (response) => { | ||
if (!response.ok) | ||
return; | ||
|
||
const data = await response.json(); | ||
return this.#removeKeyPrefix(data); | ||
}); | ||
} | ||
|
||
/** | ||
* @param {Record<string, any>} data | ||
*/ | ||
async #set(data) { | ||
Piccirello marked this conversation as resolved.
Show resolved
Hide resolved
|
||
data = this.#addKeyPrefix(data); | ||
await fetch("api/v2/clientdata/store", { | ||
method: "POST", | ||
body: new URLSearchParams({ | ||
data: JSON.stringify(data) | ||
}) | ||
}) | ||
.then((response) => { | ||
if (!response.ok) | ||
throw new Error("Failed to store client data"); | ||
}); | ||
} | ||
|
||
/** | ||
* @param {string} key | ||
* @returns {any} | ||
*/ | ||
get(key) { | ||
return this.#cache.get(key); | ||
} | ||
|
||
/** | ||
* @param {string[]} keys | ||
* @returns {Record<string, any>} | ||
*/ | ||
async fetch(keys = []) { | ||
const keysToFetch = keys.filter((key) => !this.#cache.has(key)); | ||
if (keysToFetch.length > 0) { | ||
const fetchedData = await this.#fetch(keysToFetch); | ||
for (const [key, value] of Object.entries(fetchedData)) | ||
this.#cache.set(key, value); | ||
} | ||
|
||
return Object.fromEntries(keys.map((key) => ([key, this.#cache.get(key)]))); | ||
} | ||
Comment on lines
+96
to
+113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On a second look, I will redesign these two function:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This suggestion is not consistent with usage. See There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
My suggestion will work in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer the current interface and think it makes sense. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The interface is confusing. It will produce questions like these: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems to me you're imaging some convoluted, unlikely scenario because I made different design decisions than you would have. I can't imagine any developer actually facing the confusion you're describing. Good thing is, if people do somehow get confused by that, we can always change it later.
You're trying to over optimize JS code. I would also like to note that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On a second thought, I don't insist on suggested version anymore. It will bring However parts of my previous reply still applies:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can add some jsdoc if you really think people will get confused about which to use. |
||
|
||
/** | ||
* @param {Record<string, any>} data | ||
*/ | ||
async set(data) { | ||
await this.#set(data); | ||
|
||
// update cache | ||
for (const [key, value] of Object.entries(data)) | ||
this.#cache.set(key, value); | ||
} | ||
} | ||
|
||
return exports(); | ||
})(); | ||
Object.freeze(window.qBittorrent.ClientData); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ window.qBittorrent.Client ??= (() => { | |
return { | ||
setup: setup, | ||
initializeCaches: initializeCaches, | ||
initializeClientData: initializeClientData, | ||
closeWindow: closeWindow, | ||
closeFrameWindow: closeFrameWindow, | ||
getSyncMainDataInterval: getSyncMainDataInterval, | ||
|
@@ -56,15 +57,47 @@ window.qBittorrent.Client ??= (() => { | |
const tagMap = new Map(); | ||
|
||
let cacheAllSettled; | ||
let clientDataPromise; | ||
const setup = () => { | ||
// fetch various data and store it in memory | ||
clientDataPromise = window.qBittorrent.ClientData.fetch([ | ||
"show_search_engine", | ||
"show_rss_reader", | ||
"show_log_viewer", | ||
"speed_in_browser_title_bar", | ||
"show_top_toolbar", | ||
"show_status_bar", | ||
"show_filters_sidebar", | ||
"hide_zero_status_filters", | ||
"color_scheme", | ||
"full_url_tracker_column", | ||
"use_alt_row_colors", | ||
"use_virtual_list", | ||
"dblclick_complete", | ||
"dblclick_download", | ||
"dblclick_filter", | ||
"search_in_filter", | ||
"qbt_selected_log_levels", | ||
"add_torrent_default_category", | ||
]); | ||
|
||
cacheAllSettled = Promise.allSettled([ | ||
window.qBittorrent.Cache.buildInfo.init(), | ||
window.qBittorrent.Cache.preferences.init(), | ||
window.qBittorrent.Cache.qbtVersion.init() | ||
window.qBittorrent.Cache.qbtVersion.init(), | ||
clientDataPromise, | ||
]); | ||
}; | ||
|
||
const initializeClientData = async () => { | ||
try { | ||
await clientDataPromise; | ||
} | ||
catch (error) { | ||
console.error(`Failed to initialize client data. Reason: "${error}".`); | ||
} | ||
}; | ||
|
||
const initializeCaches = async () => { | ||
const results = await cacheAllSettled; | ||
for (const [idx, result] of results.entries()) { | ||
|
@@ -223,8 +256,8 @@ let queueing_enabled = true; | |
let serverSyncMainDataInterval = 1500; | ||
let customSyncMainDataInterval = null; | ||
let useSubcategories = true; | ||
const useAutoHideZeroStatusFilters = localPreferences.get("hide_zero_status_filters", "false") === "true"; | ||
const displayFullURLTrackerColumn = localPreferences.get("full_url_tracker_column", "false") === "true"; | ||
let useAutoHideZeroStatusFilters = false; | ||
let displayFullURLTrackerColumn = false; | ||
|
||
/* Categories filter */ | ||
const CATEGORIES_ALL = "b4af0e4c-e76d-4bac-a392-46cbc18d9655"; | ||
|
@@ -250,6 +283,8 @@ const TRACKERS_WARNING = "82a702c5-210c-412b-829f-97632d7557e9"; | |
// Map<trackerHost: String, Map<trackerURL: String, torrents: Set>> | ||
const trackerMap = new Map(); | ||
|
||
const clientData = window.qBittorrent.ClientData; | ||
|
||
let selectedTracker = localPreferences.get("selected_tracker", TRACKERS_ALL); | ||
let setTrackerFilter = () => {}; | ||
|
||
|
@@ -258,7 +293,13 @@ let selectedStatus = localPreferences.get("selected_filter", "all"); | |
let setStatusFilter = () => {}; | ||
let toggleFilterDisplay = () => {}; | ||
|
||
window.addEventListener("DOMContentLoaded", (event) => { | ||
window.addEventListener("DOMContentLoaded", async (event) => { | ||
await window.qBittorrent.Client.initializeClientData(); | ||
window.qBittorrent.ColorScheme.update(); | ||
|
||
useAutoHideZeroStatusFilters = clientData.get("hide_zero_status_filters") === true; | ||
displayFullURLTrackerColumn = clientData.get("full_url_tracker_column") === true; | ||
|
||
window.qBittorrent.LocalPreferences.upgrade(); | ||
|
||
let isSearchPanelLoaded = false; | ||
|
@@ -405,6 +446,13 @@ window.addEventListener("DOMContentLoaded", (event) => { | |
localPreferences.set(`filter_${filterListID.replace("FilterList", "")}_collapsed`, filterList.classList.toggle("invisible").toString()); | ||
}; | ||
|
||
const highlightSelectedStatus = () => { | ||
const statusFilter = document.getElementById("statusFilterList"); | ||
const filterID = `${selectedStatus}_filter`; | ||
for (const status of statusFilter.children) | ||
status.classList.toggle("selectedFilter", (status.id === filterID)); | ||
}; | ||
|
||
new MochaUI.Panel({ | ||
id: "Filters", | ||
title: "Panel", | ||
|
@@ -426,35 +474,35 @@ window.addEventListener("DOMContentLoaded", (event) => { | |
initializeWindows(); | ||
|
||
// Show Top Toolbar is enabled by default | ||
let showTopToolbar = localPreferences.get("show_top_toolbar", "true") === "true"; | ||
let showTopToolbar = clientData.get("show_top_toolbar") !== false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Continuing #23191 (comment) The code has become less straightforward. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is now more straightforward. Rather than some additional parameter that applies in unknown situations, the caller can use native JS to get their default value. See #23191 (comment)
In what way does this not work reliably? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It is always clear that the default value only applies when the actual value doesn't exist. This pattern is extensively used in our C++ code base and in Qt class methods. We never had a problem/confusion with it.
You told me about it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Different languages have different paradigms. In JS, the nullish coalescing operator can be used to assign a default value when the value is nullish (either
Sorry, I really just don't see this as true. To me, my solution is modern idiomatic JS. |
||
if (!showTopToolbar) { | ||
document.getElementById("showTopToolbarLink").firstElementChild.style.opacity = "0"; | ||
document.getElementById("mochaToolbar").classList.add("invisible"); | ||
} | ||
|
||
// Show Status Bar is enabled by default | ||
let showStatusBar = localPreferences.get("show_status_bar", "true") === "true"; | ||
let showStatusBar = clientData.get("show_status_bar") !== false; | ||
if (!showStatusBar) { | ||
document.getElementById("showStatusBarLink").firstElementChild.style.opacity = "0"; | ||
document.getElementById("desktopFooterWrapper").classList.add("invisible"); | ||
} | ||
|
||
// Show Filters Sidebar is enabled by default | ||
let showFiltersSidebar = localPreferences.get("show_filters_sidebar", "true") === "true"; | ||
let showFiltersSidebar = clientData.get("show_filters_sidebar") !== false; | ||
if (!showFiltersSidebar) { | ||
document.getElementById("showFiltersSidebarLink").firstElementChild.style.opacity = "0"; | ||
document.getElementById("filtersColumn").classList.add("invisible"); | ||
document.getElementById("filtersColumn_handle").classList.add("invisible"); | ||
} | ||
|
||
let speedInTitle = localPreferences.get("speed_in_browser_title_bar") === "true"; | ||
let speedInTitle = clientData.get("speed_in_browser_title_bar") === true; | ||
if (!speedInTitle) | ||
document.getElementById("speedInBrowserTitleBarLink").firstElementChild.style.opacity = "0"; | ||
|
||
// After showing/hiding the toolbar + status bar | ||
window.qBittorrent.Client.showSearchEngine(localPreferences.get("show_search_engine") !== "false"); | ||
window.qBittorrent.Client.showRssReader(localPreferences.get("show_rss_reader") !== "false"); | ||
window.qBittorrent.Client.showLogViewer(localPreferences.get("show_log_viewer") === "true"); | ||
window.qBittorrent.Client.showSearchEngine(clientData.get("show_search_engine") !== false); | ||
window.qBittorrent.Client.showRssReader(clientData.get("show_rss_reader") !== false); | ||
window.qBittorrent.Client.showLogViewer(clientData.get("show_log_viewer") === true); | ||
|
||
// After Show Top Toolbar | ||
MochaUI.Desktop.setDesktopSize(); | ||
|
@@ -571,13 +619,6 @@ window.addEventListener("DOMContentLoaded", (event) => { | |
window.qBittorrent.Filters.clearStatusFilter(); | ||
}; | ||
|
||
const highlightSelectedStatus = () => { | ||
const statusFilter = document.getElementById("statusFilterList"); | ||
const filterID = `${selectedStatus}_filter`; | ||
for (const status of statusFilter.children) | ||
status.classList.toggle("selectedFilter", (status.id === filterID)); | ||
}; | ||
|
||
const updateCategoryList = () => { | ||
const categoryList = document.getElementById("categoryFilterList"); | ||
if (!categoryList) | ||
|
@@ -1223,7 +1264,7 @@ window.addEventListener("DOMContentLoaded", (event) => { | |
|
||
document.getElementById("showTopToolbarLink").addEventListener("click", (e) => { | ||
showTopToolbar = !showTopToolbar; | ||
localPreferences.set("show_top_toolbar", showTopToolbar.toString()); | ||
clientData.set({ show_top_toolbar: showTopToolbar }).catch(console.error); | ||
if (showTopToolbar) { | ||
document.getElementById("showTopToolbarLink").firstElementChild.style.opacity = "1"; | ||
document.getElementById("mochaToolbar").classList.remove("invisible"); | ||
|
@@ -1237,7 +1278,7 @@ window.addEventListener("DOMContentLoaded", (event) => { | |
|
||
document.getElementById("showStatusBarLink").addEventListener("click", (e) => { | ||
showStatusBar = !showStatusBar; | ||
localPreferences.set("show_status_bar", showStatusBar.toString()); | ||
clientData.set({ show_status_bar: showStatusBar }).catch(console.error); | ||
if (showStatusBar) { | ||
document.getElementById("showStatusBarLink").firstElementChild.style.opacity = "1"; | ||
document.getElementById("desktopFooterWrapper").classList.remove("invisible"); | ||
|
@@ -1274,7 +1315,7 @@ window.addEventListener("DOMContentLoaded", (event) => { | |
|
||
document.getElementById("showFiltersSidebarLink").addEventListener("click", (e) => { | ||
showFiltersSidebar = !showFiltersSidebar; | ||
localPreferences.set("show_filters_sidebar", showFiltersSidebar.toString()); | ||
clientData.set({ show_filters_sidebar: showFiltersSidebar }).catch(console.error); | ||
if (showFiltersSidebar) { | ||
document.getElementById("showFiltersSidebarLink").firstElementChild.style.opacity = "1"; | ||
document.getElementById("filtersColumn").classList.remove("invisible"); | ||
|
@@ -1290,7 +1331,7 @@ window.addEventListener("DOMContentLoaded", (event) => { | |
|
||
document.getElementById("speedInBrowserTitleBarLink").addEventListener("click", (e) => { | ||
speedInTitle = !speedInTitle; | ||
localPreferences.set("speed_in_browser_title_bar", speedInTitle.toString()); | ||
clientData.set({ speed_in_browser_title_bar: speedInTitle }).catch(console.error); | ||
if (speedInTitle) | ||
document.getElementById("speedInBrowserTitleBarLink").firstElementChild.style.opacity = "1"; | ||
else | ||
|
@@ -1300,19 +1341,19 @@ window.addEventListener("DOMContentLoaded", (event) => { | |
|
||
document.getElementById("showSearchEngineLink").addEventListener("click", (e) => { | ||
window.qBittorrent.Client.showSearchEngine(!window.qBittorrent.Client.isShowSearchEngine()); | ||
localPreferences.set("show_search_engine", window.qBittorrent.Client.isShowSearchEngine().toString()); | ||
clientData.set({ show_search_engine: window.qBittorrent.Client.isShowSearchEngine() }).catch(console.error); | ||
updateTabDisplay(); | ||
}); | ||
|
||
document.getElementById("showRssReaderLink").addEventListener("click", (e) => { | ||
window.qBittorrent.Client.showRssReader(!window.qBittorrent.Client.isShowRssReader()); | ||
localPreferences.set("show_rss_reader", window.qBittorrent.Client.isShowRssReader().toString()); | ||
clientData.set({ show_rss_reader: window.qBittorrent.Client.isShowRssReader() }).catch(console.error); | ||
updateTabDisplay(); | ||
}); | ||
|
||
document.getElementById("showLogViewerLink").addEventListener("click", (e) => { | ||
window.qBittorrent.Client.showLogViewer(!window.qBittorrent.Client.isShowLogViewer()); | ||
localPreferences.set("show_log_viewer", window.qBittorrent.Client.isShowLogViewer().toString()); | ||
clientData.set({ show_log_viewer: window.qBittorrent.Client.isShowLogViewer() }).catch(console.error); | ||
updateTabDisplay(); | ||
}); | ||
|
||
|
@@ -1369,7 +1410,7 @@ window.addEventListener("DOMContentLoaded", (event) => { | |
// main window tabs | ||
|
||
const showTransfersTab = () => { | ||
const showFiltersSidebar = localPreferences.get("show_filters_sidebar", "true") === "true"; | ||
const showFiltersSidebar = clientData.get("show_filters_sidebar") !== false; | ||
if (showFiltersSidebar) { | ||
document.getElementById("filtersColumn").classList.remove("invisible"); | ||
document.getElementById("filtersColumn_handle").classList.remove("invisible"); | ||
|
Uh oh!
There was an error while loading. Please reload this page.