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 if (window
.qBittorrent
=== undefined) {
27 window
.qBittorrent
= {};
30 window
.qBittorrent
.Download
= (function() {
31 const exports = function() {
33 changeCategorySelect
: changeCategorySelect
,
39 let defaultSavePath
= "";
41 const getCategories = function() {
43 url
: 'api/v2/torrents/categories',
46 onSuccess: function(data
) {
49 for (const i
in data
) {
50 const category
= data
[i
];
51 const option
= new Element("option");
52 option
.set('value', category
.name
);
53 option
.set('html', category
.name
);
54 $('categorySelect').appendChild(option
);
61 const getPreferences = function() {
62 const pref
= window
.parent
.qBittorrent
.Cache
.preferences
.get();
64 defaultSavePath
= pref
.save_path
;
65 $('savepath').setProperty('value', defaultSavePath
);
66 $('startTorrent').checked
= !pref
.add_stopped_enabled
;
67 $('addToTopOfQueue').checked
= pref
.add_to_top_of_queue
;
69 if (pref
.auto_tmm_enabled
== 1) {
70 $('autoTMM').selectedIndex
= 1;
71 $('savepath').disabled
= true;
74 $('autoTMM').selectedIndex
= 0;
77 if (pref
.torrent_stop_condition
=== "MetadataReceived") {
78 $('stopCondition').selectedIndex
= 1;
80 else if (pref
.torrent_stop_condition
=== "FilesChecked") {
81 $('stopCondition').selectedIndex
= 2;
84 $('stopCondition').selectedIndex
= 0;
87 if (pref
.torrent_content_layout
=== "Subfolder") {
88 $('contentLayout').selectedIndex
= 1;
90 else if (pref
.torrent_content_layout
=== "NoSubfolder") {
91 $('contentLayout').selectedIndex
= 2;
94 $('contentLayout').selectedIndex
= 0;
98 const changeCategorySelect = function(item
) {
99 if (item
.value
== "\\other") {
100 item
.nextElementSibling
.hidden
= false;
101 item
.nextElementSibling
.value
= "";
102 item
.nextElementSibling
.select();
104 if ($('autoTMM').selectedIndex
== 1)
105 $('savepath').value
= defaultSavePath
;
108 item
.nextElementSibling
.hidden
= true;
109 const text
= item
.options
[item
.selectedIndex
].textContent
;
110 item
.nextElementSibling
.value
= text
;
112 if ($('autoTMM').selectedIndex
== 1) {
113 const categoryName
= item
.value
;
114 const category
= categories
[categoryName
];
115 let savePath
= defaultSavePath
;
116 if (category
!== undefined)
117 savePath
= (category
['savePath'] !== "") ? category
['savePath'] : `${defaultSavePath}/${categoryName}`;
118 $('savepath').value
= savePath
;
123 const changeTMM = function(item
) {
124 if (item
.selectedIndex
== 1) {
125 $('savepath').disabled
= true;
127 const categorySelect
= $('categorySelect');
128 const categoryName
= categorySelect
.options
[categorySelect
.selectedIndex
].value
;
129 const category
= categories
[categoryName
];
130 $('savepath').value
= (category
=== undefined) ? "" : category
['savePath'];
133 $('savepath').disabled
= false;
134 $('savepath').value
= defaultSavePath
;
138 $(window
).addEventListener("load", function() {
146 Object
.freeze(window
.qBittorrent
.Download
);