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
= () => {
40 url
: "api/v2/torrents/categories",
43 onSuccess
: (data
) => {
46 for (const i
in data
) {
47 if (!Object
.hasOwn(data
, i
))
50 const category
= data
[i
];
51 const option
= new Element("option");
52 option
.value
= category
.name
;
53 option
.textContent
= category
.name
;
54 $("categorySelect").appendChild(option
);
61 const getPreferences
= () => {
62 const pref
= window
.parent
.qBittorrent
.Cache
.preferences
.get();
64 defaultSavePath
= pref
.save_path
;
65 $("savepath").value
= defaultSavePath
;
66 $("startTorrent").checked
= !pref
.add_stopped_enabled
;
67 $("addToTopOfQueue").checked
= pref
.add_to_top_of_queue
;
69 if (pref
.auto_tmm_enabled
) {
70 $("autoTMM").selectedIndex
= 1;
71 $("savepath").disabled
= true;
74 $("autoTMM").selectedIndex
= 0;
77 if (pref
.torrent_stop_condition
=== "MetadataReceived")
78 $("stopCondition").selectedIndex
= 1;
79 else if (pref
.torrent_stop_condition
=== "FilesChecked")
80 $("stopCondition").selectedIndex
= 2;
82 $("stopCondition").selectedIndex
= 0;
84 if (pref
.torrent_content_layout
=== "Subfolder")
85 $("contentLayout").selectedIndex
= 1;
86 else if (pref
.torrent_content_layout
=== "NoSubfolder")
87 $("contentLayout").selectedIndex
= 2;
89 $("contentLayout").selectedIndex
= 0;
92 const changeCategorySelect
= (item
) => {
93 if (item
.value
=== "\\other") {
94 item
.nextElementSibling
.hidden
= false;
95 item
.nextElementSibling
.value
= "";
96 item
.nextElementSibling
.select();
98 if ($("autoTMM").selectedIndex
=== 1)
99 $("savepath").value
= defaultSavePath
;
102 item
.nextElementSibling
.hidden
= true;
103 const text
= item
.options
[item
.selectedIndex
].textContent
;
104 item
.nextElementSibling
.value
= text
;
106 if ($("autoTMM").selectedIndex
=== 1) {
107 const categoryName
= item
.value
;
108 const category
= categories
[categoryName
];
109 let savePath
= defaultSavePath
;
110 if (category
!== undefined)
111 savePath
= (category
["savePath"] !== "") ? category
["savePath"] : `${defaultSavePath}/${categoryName}`;
112 $("savepath").value
= savePath
;
117 const changeTMM
= (item
) => {
118 if (item
.selectedIndex
=== 1) {
119 $("savepath").disabled
= true;
121 const categorySelect
= $("categorySelect");
122 const categoryName
= categorySelect
.options
[categorySelect
.selectedIndex
].value
;
123 const category
= categories
[categoryName
];
124 $("savepath").value
= (category
=== undefined) ? "" : category
["savePath"];
127 $("savepath").disabled
= false;
128 $("savepath").value
= defaultSavePath
;
132 $(window
).addEventListener("load", () => {
139 Object
.freeze(window
.qBittorrent
.Download
);