From 6b5faba6b2084e310cf2466bd71319fdb08db32b Mon Sep 17 00:00:00 2001 From: Thomas Piccirello Date: Fri, 27 Jun 2025 22:45:36 -0700 Subject: [PATCH] WebUI: Support managing category download path --- src/webui/www/private/newcategory.html | 116 +++++++++++++++++--- src/webui/www/private/scripts/client.js | 7 +- src/webui/www/private/scripts/mocha-init.js | 11 +- 3 files changed, 113 insertions(+), 21 deletions(-) diff --git a/src/webui/www/private/newcategory.html b/src/webui/www/private/newcategory.html index 3abdc6f2a..91cd22e02 100644 --- a/src/webui/www/private/newcategory.html +++ b/src/webui/www/private/newcategory.html @@ -3,7 +3,7 @@ - QBT_TR(New Category)QBT_TR[CONTEXT=TransferListWidget] + QBT_TR(New Category)QBT_TR[CONTEXT=Category] @@ -26,27 +26,78 @@ } }); + const defaultDownloadPath = () => { + const category = document.getElementById("categoryName").value.trim(); + return `${pref.temp_path}/${category}`; + }; + + const setDownloadPath = (option) => { + const downloadPath = document.getElementById("downloadPath"); + const defaultPath = defaultDownloadPath(); + switch (option) { + case "default": + downloadPath.disabled = true; + downloadPath.value = pref.temp_path_enabled ? defaultPath : ""; + downloadPath.placeholder = ""; + break; + case "yes": + downloadPath.disabled = false; + if ((categoryData !== undefined) && (categoryData.downloadPath !== false) && (categoryData.downloadPath !== null)) + downloadPath.value = categoryData.downloadPath; + else + downloadPath.value = ""; + downloadPath.placeholder = defaultPath; + break; + case "no": + downloadPath.disabled = true; + downloadPath.value = ""; + downloadPath.placeholder = ""; + break; + } + }; + const searchParams = new URLSearchParams(window.location.search); const uriAction = window.qBittorrent.Misc.safeTrim(searchParams.get("action")); const uriHashes = window.qBittorrent.Misc.safeTrim(searchParams.get("hashes")); const uriCategoryName = window.qBittorrent.Misc.safeTrim(searchParams.get("categoryName")); - const uriSavePath = window.qBittorrent.Misc.safeTrim(searchParams.get("savePath")); + const pref = window.parent.qBittorrent.Cache.preferences.get(); + const categoryData = window.parent.qBittorrent.Client.categoryMap.get(uriCategoryName); + + const useDownloadPathElem = document.getElementById("useDownloadPath"); + const categoryNameElem = document.getElementById("categoryName"); + const savePathElem = document.getElementById("savePath"); if (uriAction === "edit") { if (!uriCategoryName) return; - document.getElementById("categoryName").disabled = true; - document.getElementById("categoryName").value = window.qBittorrent.Misc.escapeHtml(uriCategoryName); - document.getElementById("savePath").value = window.qBittorrent.Misc.escapeHtml(uriSavePath); - document.getElementById("savePath").focus(); + categoryNameElem.disabled = true; + categoryNameElem.value = uriCategoryName; + savePathElem.value = categoryData.savePath; + savePathElem.placeholder = `${pref.save_path}/${uriCategoryName}`; + savePathElem.focus(); + + switch (categoryData.downloadPath) { + case false: + useDownloadPathElem.selectedIndex = 2; + setDownloadPath("no"); + break; + case null: + useDownloadPathElem.selectedIndex = 0; + setDownloadPath("default"); + break; + default: + useDownloadPathElem.selectedIndex = 1; + setDownloadPath("yes"); + break; + } } else if (uriAction === "createSubcategory") { - document.getElementById("categoryName").value = window.qBittorrent.Misc.escapeHtml(uriCategoryName); - document.getElementById("categoryName").focus(); + categoryNameElem.value = uriCategoryName; + categoryNameElem.focus(); } else { - document.getElementById("categoryName").focus(); + categoryNameElem.focus(); } document.getElementById("categoryNameButton").addEventListener("click", (e) => { @@ -66,6 +117,16 @@ return true; }; + const body = {}; + const useDownloadPath = document.getElementById("useDownloadPath"); + if (useDownloadPath.value === "no") { + body.downloadPathEnabled = false; + } + else if (useDownloadPath.value === "yes") { + body.downloadPathEnabled = true; + body.downloadPath = document.getElementById("downloadPath").value.trim(); + } + switch (uriAction) { case "set": if ((uriHashes === "") || !verifyCategoryName(categoryName)) @@ -74,6 +135,7 @@ fetch("api/v2/torrents/createCategory", { method: "POST", body: new URLSearchParams({ + ...body, category: categoryName, savePath: savePath }) @@ -110,6 +172,7 @@ fetch("api/v2/torrents/createCategory", { method: "POST", body: new URLSearchParams({ + ...body, category: categoryName, savePath: savePath }) @@ -129,6 +192,7 @@ fetch("api/v2/torrents/editCategory", { method: "POST", body: new URLSearchParams({ + ...body, category: uriCategoryName, // category name can't be changed savePath: savePath }) @@ -146,6 +210,10 @@ } }); + useDownloadPathElem.addEventListener("change", (e) => { + setDownloadPath(e.target.value); + }); + window.qBittorrent.pathAutofill.attachPathAutofill(); }); @@ -153,10 +221,32 @@
- - - - +
+ + +
+
+ + +
+ +
+ QBT_TR(Save path for incomplete torrents:)QBT_TR[CONTEXT=Category] + +
+ + +
+
+ + +
+
+
diff --git a/src/webui/www/private/scripts/client.js b/src/webui/www/private/scripts/client.js index 399e6e058..59462273e 100644 --- a/src/webui/www/private/scripts/client.js +++ b/src/webui/www/private/scripts/client.js @@ -832,12 +832,15 @@ window.addEventListener("DOMContentLoaded", (event) => { if (categoryData === undefined) { window.qBittorrent.Client.categoryMap.set(responseName, { savePath: responseData.savePath, + downloadPath: responseData.download_path ?? null, torrents: new Set() }); } else { - // only the save path can change for existing categories - categoryData.savePath = responseData.savePath; + if (responseData.savePath !== undefined) + categoryData.savePath = responseData.savePath; + if (responseData.download_path !== undefined) + categoryData.downloadPath = responseData.download_path; } } updateCategories = true; diff --git a/src/webui/www/private/scripts/mocha-init.js b/src/webui/www/private/scripts/mocha-init.js index b564124fc..7ea2bb13d 100644 --- a/src/webui/www/private/scripts/mocha-init.js +++ b/src/webui/www/private/scripts/mocha-init.js @@ -869,7 +869,7 @@ const initializeWindows = () => { paddingVertical: 0, paddingHorizontal: 0, width: 400, - height: 150 + height: 200 }); }; @@ -910,7 +910,7 @@ const initializeWindows = () => { paddingVertical: 0, paddingHorizontal: 0, width: 400, - height: 150 + height: 200 }); }; @@ -932,7 +932,7 @@ const initializeWindows = () => { paddingVertical: 0, paddingHorizontal: 0, width: 400, - height: 150 + height: 200 }); }; @@ -940,8 +940,7 @@ const initializeWindows = () => { const contentURL = new URL("newcategory.html", window.location); contentURL.search = new URLSearchParams({ action: "edit", - categoryName: category, - savePath: categoryMap.get(category).savePath + categoryName: category }); new MochaUI.Window({ id: "editCategoryPage", @@ -955,7 +954,7 @@ const initializeWindows = () => { paddingVertical: 0, paddingHorizontal: 0, width: 400, - height: 150 + height: 200 }); };