3 * Copyright (c) 2008 Ishan Arora <ishan@qbittorrent.org>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 window.qBittorrent ??= {};
27 window.qBittorrent.Download ??= (() => {
28 const exports = () => {
30 changeCategorySelect: changeCategorySelect,
36 let defaultSavePath = "";
38 const getCategories = () => {
39 fetch("api/v2/torrents/categories", {
43 .then(async (response) => {
47 const data = await response.json();
50 for (const i in data) {
51 if (!Object.hasOwn(data, i))
54 const category = data[i];
55 const option = document.createElement("option");
56 option.value = category.name;
57 option.textContent = category.name;
58 $("categorySelect").appendChild(option);
63 const getPreferences = () => {
64 const pref = window.parent.qBittorrent.Cache.preferences.get();
66 defaultSavePath = pref.save_path;
67 $("savepath").value = defaultSavePath;
68 $("startTorrent").checked = !pref.add_stopped_enabled;
69 $("addToTopOfQueue").checked = pref.add_to_top_of_queue;
71 if (pref.auto_tmm_enabled) {
72 $("autoTMM").selectedIndex = 1;
73 $("savepath").disabled = true;
76 $("autoTMM").selectedIndex = 0;
79 if (pref.torrent_stop_condition === "MetadataReceived")
80 $("stopCondition").selectedIndex = 1;
81 else if (pref.torrent_stop_condition === "FilesChecked")
82 $("stopCondition").selectedIndex = 2;
84 $("stopCondition").selectedIndex = 0;
86 if (pref.torrent_content_layout === "Subfolder")
87 $("contentLayout").selectedIndex = 1;
88 else if (pref.torrent_content_layout === "NoSubfolder")
89 $("contentLayout").selectedIndex = 2;
91 $("contentLayout").selectedIndex = 0;
94 const changeCategorySelect = (item) => {
95 if (item.value === "\\other") {
96 item.nextElementSibling.hidden = false;
97 item.nextElementSibling.value = "";
98 item.nextElementSibling.select();
100 if ($("autoTMM").selectedIndex === 1)
101 $("savepath").value = defaultSavePath;
104 item.nextElementSibling.hidden = true;
105 const text = item.options[item.selectedIndex].textContent;
106 item.nextElementSibling.value = text;
108 if ($("autoTMM").selectedIndex === 1) {
109 const categoryName = item.value;
110 const category = categories[categoryName];
111 let savePath = defaultSavePath;
112 if (category !== undefined)
113 savePath = (category["savePath"] !== "") ? category["savePath"] : `${defaultSavePath}/${categoryName}`;
114 $("savepath").value = savePath;
119 const changeTMM = (item) => {
120 if (item.selectedIndex === 1) {
121 $("savepath").disabled = true;
123 const categorySelect = $("categorySelect");
124 const categoryName = categorySelect.options[categorySelect.selectedIndex].value;
125 const category = categories[categoryName];
126 $("savepath").value = (category === undefined) ? "" : category["savePath"];
129 $("savepath").disabled = false;
130 $("savepath").value = defaultSavePath;
134 $(window).addEventListener("load", () => {
141 Object.freeze(window.qBittorrent.Download);