2022-10-18 22:39:32 -04:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
|
|
|
* Copyright (C) 2009 Christophe Dumez <chris@qbittorrent.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2024-10-03 08:37:11 -04:00
|
|
|
"use strict";
|
2022-10-18 22:39:32 -04:00
|
|
|
|
2024-10-03 08:37:11 -04:00
|
|
|
window.qBittorrent ??= {};
|
|
|
|
window.qBittorrent.PropFiles ??= (() => {
|
|
|
|
const exports = () => {
|
2022-10-18 22:39:32 -04:00
|
|
|
return {
|
|
|
|
normalizePriority: normalizePriority,
|
|
|
|
isDownloadCheckboxExists: isDownloadCheckboxExists,
|
|
|
|
createDownloadCheckbox: createDownloadCheckbox,
|
|
|
|
updateDownloadCheckbox: updateDownloadCheckbox,
|
|
|
|
isPriorityComboExists: isPriorityComboExists,
|
|
|
|
createPriorityCombo: createPriorityCombo,
|
|
|
|
updatePriorityCombo: updatePriorityCombo,
|
|
|
|
updateData: updateData,
|
|
|
|
collapseIconClicked: collapseIconClicked,
|
|
|
|
expandFolder: expandFolder,
|
|
|
|
collapseFolder: collapseFolder
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const torrentFilesTable = new window.qBittorrent.DynamicTable.TorrentFilesTable();
|
|
|
|
const FilePriority = window.qBittorrent.FileTree.FilePriority;
|
|
|
|
const TriState = window.qBittorrent.FileTree.TriState;
|
|
|
|
let is_seed = true;
|
|
|
|
let current_hash = "";
|
|
|
|
|
|
|
|
const normalizePriority = function(priority) {
|
|
|
|
switch (priority) {
|
|
|
|
case FilePriority.Ignored:
|
|
|
|
case FilePriority.Normal:
|
|
|
|
case FilePriority.High:
|
|
|
|
case FilePriority.Maximum:
|
|
|
|
case FilePriority.Mixed:
|
|
|
|
return priority;
|
|
|
|
default:
|
|
|
|
return FilePriority.Normal;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const getAllChildren = function(id, fileId) {
|
|
|
|
const node = torrentFilesTable.getNode(id);
|
|
|
|
if (!node.isFolder) {
|
|
|
|
return {
|
|
|
|
rowIds: [id],
|
|
|
|
fileIds: [fileId]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const rowIds = [];
|
|
|
|
const fileIds = [];
|
|
|
|
|
|
|
|
const getChildFiles = function(node) {
|
|
|
|
if (node.isFolder) {
|
2024-10-03 08:37:11 -04:00
|
|
|
node.children.each((child) => {
|
2022-10-18 22:39:32 -04:00
|
|
|
getChildFiles(child);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rowIds.push(node.data.rowId);
|
|
|
|
fileIds.push(node.data.fileId);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-10-03 08:37:11 -04:00
|
|
|
node.children.each((child) => {
|
2022-10-18 22:39:32 -04:00
|
|
|
getChildFiles(child);
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
rowIds: rowIds,
|
|
|
|
fileIds: fileIds
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const fileCheckboxClicked = function(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
const checkbox = e.target;
|
|
|
|
const priority = checkbox.checked ? FilePriority.Normal : FilePriority.Ignored;
|
2024-10-03 08:37:11 -04:00
|
|
|
const id = checkbox.getAttribute("data-id");
|
|
|
|
const fileId = checkbox.getAttribute("data-file-id");
|
2022-10-18 22:39:32 -04:00
|
|
|
|
|
|
|
const rows = getAllChildren(id, fileId);
|
|
|
|
|
|
|
|
setFilePriority(rows.rowIds, rows.fileIds, priority);
|
|
|
|
updateGlobalCheckbox();
|
|
|
|
};
|
|
|
|
|
|
|
|
const fileComboboxChanged = function(e) {
|
|
|
|
const combobox = e.target;
|
|
|
|
const priority = combobox.value;
|
2024-10-03 08:37:11 -04:00
|
|
|
const id = combobox.getAttribute("data-id");
|
|
|
|
const fileId = combobox.getAttribute("data-file-id");
|
2022-10-18 22:39:32 -04:00
|
|
|
|
|
|
|
const rows = getAllChildren(id, fileId);
|
|
|
|
|
|
|
|
setFilePriority(rows.rowIds, rows.fileIds, priority);
|
|
|
|
updateGlobalCheckbox();
|
|
|
|
};
|
|
|
|
|
|
|
|
const isDownloadCheckboxExists = function(id) {
|
2024-10-03 08:37:11 -04:00
|
|
|
return ($("cbPrio" + id) !== null);
|
2022-10-18 22:39:32 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const createDownloadCheckbox = function(id, fileId, checked) {
|
2024-10-03 08:37:11 -04:00
|
|
|
const checkbox = new Element("input");
|
|
|
|
checkbox.type = "checkbox";
|
|
|
|
checkbox.id = "cbPrio" + id;
|
|
|
|
checkbox.setAttribute("data-id", id);
|
|
|
|
checkbox.setAttribute("data-file-id", fileId);
|
|
|
|
checkbox.className = "DownloadedCB";
|
|
|
|
checkbox.addEventListener("click", fileCheckboxClicked);
|
2022-10-18 22:39:32 -04:00
|
|
|
|
|
|
|
updateCheckbox(checkbox, checked);
|
|
|
|
return checkbox;
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateDownloadCheckbox = function(id, checked) {
|
2024-10-03 08:37:11 -04:00
|
|
|
const checkbox = $("cbPrio" + id);
|
2022-10-18 22:39:32 -04:00
|
|
|
updateCheckbox(checkbox, checked);
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateCheckbox = function(checkbox, checked) {
|
|
|
|
switch (checked) {
|
|
|
|
case TriState.Checked:
|
|
|
|
setCheckboxChecked(checkbox);
|
|
|
|
break;
|
|
|
|
case TriState.Unchecked:
|
|
|
|
setCheckboxUnchecked(checkbox);
|
|
|
|
break;
|
|
|
|
case TriState.Partial:
|
|
|
|
setCheckboxPartial(checkbox);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const isPriorityComboExists = function(id) {
|
2024-10-03 08:37:11 -04:00
|
|
|
return ($("comboPrio" + id) !== null);
|
2022-10-18 22:39:32 -04:00
|
|
|
};
|
|
|
|
|
2024-10-03 08:37:11 -04:00
|
|
|
const createPriorityCombo = (id, fileId, selectedPriority) => {
|
|
|
|
const createOption = (priority, isSelected, text) => {
|
|
|
|
const option = document.createElement("option");
|
|
|
|
option.value = priority.toString();
|
|
|
|
option.selected = isSelected;
|
|
|
|
option.textContent = text;
|
|
|
|
return option;
|
|
|
|
};
|
2022-10-18 22:39:32 -04:00
|
|
|
|
2024-10-03 08:37:11 -04:00
|
|
|
const select = document.createElement("select");
|
|
|
|
select.id = "comboPrio" + id;
|
|
|
|
select.setAttribute("data-id", id);
|
|
|
|
select.setAttribute("data-file-id", fileId);
|
|
|
|
select.addClass("combo_priority");
|
|
|
|
select.addEventListener("change", fileComboboxChanged);
|
2022-10-18 22:39:32 -04:00
|
|
|
|
2024-10-03 08:37:11 -04:00
|
|
|
select.appendChild(createOption(FilePriority.Ignored, (FilePriority.Ignored === selectedPriority), "Do not download"));
|
|
|
|
select.appendChild(createOption(FilePriority.Normal, (FilePriority.Normal === selectedPriority), "Normal"));
|
|
|
|
select.appendChild(createOption(FilePriority.High, (FilePriority.High === selectedPriority), "High"));
|
|
|
|
select.appendChild(createOption(FilePriority.Maximum, (FilePriority.Maximum === selectedPriority), "Maximum"));
|
2022-10-18 22:39:32 -04:00
|
|
|
|
|
|
|
// "Mixed" priority is for display only; it shouldn't be selectable
|
2024-10-03 08:37:11 -04:00
|
|
|
const mixedPriorityOption = createOption(FilePriority.Mixed, (FilePriority.Mixed === selectedPriority), "Mixed");
|
|
|
|
mixedPriorityOption.disabled = true;
|
|
|
|
select.appendChild(mixedPriorityOption);
|
2022-10-18 22:39:32 -04:00
|
|
|
|
|
|
|
return select;
|
|
|
|
};
|
|
|
|
|
|
|
|
const updatePriorityCombo = function(id, selectedPriority) {
|
2024-10-03 08:37:11 -04:00
|
|
|
const combobox = $("comboPrio" + id);
|
|
|
|
if (parseInt(combobox.value, 10) !== selectedPriority)
|
2022-10-18 22:39:32 -04:00
|
|
|
selectComboboxPriority(combobox, selectedPriority);
|
|
|
|
};
|
|
|
|
|
|
|
|
const selectComboboxPriority = function(combobox, priority) {
|
|
|
|
const options = combobox.options;
|
|
|
|
for (let i = 0; i < options.length; ++i) {
|
|
|
|
const option = options[i];
|
2024-10-03 08:37:11 -04:00
|
|
|
if (parseInt(option.value, 10) === priority)
|
|
|
|
option.selected = true;
|
2022-10-18 22:39:32 -04:00
|
|
|
else
|
2024-10-03 08:37:11 -04:00
|
|
|
option.selected = false;
|
2022-10-18 22:39:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
combobox.value = priority;
|
|
|
|
};
|
|
|
|
|
|
|
|
const switchCheckboxState = function(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
const rowIds = [];
|
|
|
|
const fileIds = [];
|
|
|
|
let priority = FilePriority.Ignored;
|
2024-10-03 08:37:11 -04:00
|
|
|
const checkbox = $("tristate_cb");
|
2022-10-18 22:39:32 -04:00
|
|
|
|
|
|
|
if (checkbox.state === "checked") {
|
|
|
|
setCheckboxUnchecked(checkbox);
|
|
|
|
// set file priority for all checked to Ignored
|
2024-10-03 08:37:11 -04:00
|
|
|
torrentFilesTable.getFilteredAndSortedRows().forEach((row) => {
|
2022-10-18 22:39:32 -04:00
|
|
|
const rowId = row.rowId;
|
|
|
|
const fileId = row.full_data.fileId;
|
|
|
|
const isChecked = (row.full_data.checked === TriState.Checked);
|
|
|
|
const isFolder = (fileId === -1);
|
|
|
|
if (!isFolder && isChecked) {
|
|
|
|
rowIds.push(rowId);
|
|
|
|
fileIds.push(fileId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setCheckboxChecked(checkbox);
|
|
|
|
priority = FilePriority.Normal;
|
|
|
|
// set file priority for all unchecked to Normal
|
2024-10-03 08:37:11 -04:00
|
|
|
torrentFilesTable.getFilteredAndSortedRows().forEach((row) => {
|
2022-10-18 22:39:32 -04:00
|
|
|
const rowId = row.rowId;
|
|
|
|
const fileId = row.full_data.fileId;
|
|
|
|
const isUnchecked = (row.full_data.checked === TriState.Unchecked);
|
|
|
|
const isFolder = (fileId === -1);
|
|
|
|
if (!isFolder && isUnchecked) {
|
|
|
|
rowIds.push(rowId);
|
|
|
|
fileIds.push(fileId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rowIds.length > 0)
|
|
|
|
setFilePriority(rowIds, fileIds, priority);
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateGlobalCheckbox = function() {
|
2024-10-03 08:37:11 -04:00
|
|
|
const checkbox = $("tristate_cb");
|
2022-10-18 22:39:32 -04:00
|
|
|
if (isAllCheckboxesChecked())
|
|
|
|
setCheckboxChecked(checkbox);
|
|
|
|
else if (isAllCheckboxesUnchecked())
|
|
|
|
setCheckboxUnchecked(checkbox);
|
|
|
|
else
|
|
|
|
setCheckboxPartial(checkbox);
|
|
|
|
};
|
|
|
|
|
|
|
|
const setCheckboxChecked = function(checkbox) {
|
|
|
|
checkbox.state = "checked";
|
|
|
|
checkbox.indeterminate = false;
|
|
|
|
checkbox.checked = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const setCheckboxUnchecked = function(checkbox) {
|
|
|
|
checkbox.state = "unchecked";
|
|
|
|
checkbox.indeterminate = false;
|
|
|
|
checkbox.checked = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
const setCheckboxPartial = function(checkbox) {
|
|
|
|
checkbox.state = "partial";
|
|
|
|
checkbox.indeterminate = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isAllCheckboxesChecked = function() {
|
2024-10-03 08:37:11 -04:00
|
|
|
const checkboxes = $$("input.DownloadedCB");
|
2022-10-18 22:39:32 -04:00
|
|
|
for (let i = 0; i < checkboxes.length; ++i) {
|
|
|
|
if (!checkboxes[i].checked)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isAllCheckboxesUnchecked = function() {
|
2024-10-03 08:37:11 -04:00
|
|
|
const checkboxes = $$("input.DownloadedCB");
|
2022-10-18 22:39:32 -04:00
|
|
|
for (let i = 0; i < checkboxes.length; ++i) {
|
|
|
|
if (checkboxes[i].checked)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const setFilePriority = function(ids, fileIds, priority) {
|
|
|
|
if (current_hash === "")
|
|
|
|
return;
|
|
|
|
|
|
|
|
clearTimeout(loadTorrentFilesDataTimer);
|
2024-10-03 08:37:11 -04:00
|
|
|
loadTorrentFilesDataTimer = -1;
|
|
|
|
|
2022-10-18 22:39:32 -04:00
|
|
|
new Request({
|
2024-10-03 08:37:11 -04:00
|
|
|
url: "api/v2/torrents/filePrio",
|
|
|
|
method: "post",
|
2022-10-18 22:39:32 -04:00
|
|
|
data: {
|
2024-10-03 08:37:11 -04:00
|
|
|
"hash": current_hash,
|
|
|
|
"id": fileIds.join("|"),
|
|
|
|
"priority": priority
|
2022-10-18 22:39:32 -04:00
|
|
|
},
|
|
|
|
onComplete: function() {
|
|
|
|
loadTorrentFilesDataTimer = loadTorrentFilesData.delay(1000);
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
|
|
|
|
const ignore = (priority === FilePriority.Ignored);
|
2024-10-03 08:37:11 -04:00
|
|
|
ids.forEach((_id) => {
|
2022-10-18 22:39:32 -04:00
|
|
|
torrentFilesTable.setIgnored(_id, ignore);
|
|
|
|
|
2024-10-03 08:37:11 -04:00
|
|
|
const combobox = $("comboPrio" + _id);
|
2022-10-18 22:39:32 -04:00
|
|
|
if (combobox !== null)
|
|
|
|
selectComboboxPriority(combobox, priority);
|
|
|
|
});
|
|
|
|
|
|
|
|
torrentFilesTable.updateTable(false);
|
|
|
|
};
|
|
|
|
|
2024-10-03 08:37:11 -04:00
|
|
|
let loadTorrentFilesDataTimer = -1;
|
2022-10-18 22:39:32 -04:00
|
|
|
const loadTorrentFilesData = function() {
|
2024-10-03 08:37:11 -04:00
|
|
|
if ($("propFiles").hasClass("invisible")
|
|
|
|
|| $("propertiesPanel_collapseToggle").hasClass("panel-expand")) {
|
2022-10-18 22:39:32 -04:00
|
|
|
// Tab changed, don't do anything
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const new_hash = torrentsTable.getCurrentTorrentID();
|
|
|
|
if (new_hash === "") {
|
|
|
|
torrentFilesTable.clear();
|
|
|
|
clearTimeout(loadTorrentFilesDataTimer);
|
|
|
|
loadTorrentFilesDataTimer = loadTorrentFilesData.delay(5000);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let loadedNewTorrent = false;
|
2024-10-03 08:37:11 -04:00
|
|
|
if (new_hash !== current_hash) {
|
2022-10-18 22:39:32 -04:00
|
|
|
torrentFilesTable.clear();
|
|
|
|
current_hash = new_hash;
|
|
|
|
loadedNewTorrent = true;
|
|
|
|
}
|
2024-10-03 08:37:11 -04:00
|
|
|
const url = new URI("api/v2/torrents/files?hash=" + current_hash);
|
2022-10-18 22:39:32 -04:00
|
|
|
new Request.JSON({
|
|
|
|
url: url,
|
2024-10-03 08:37:11 -04:00
|
|
|
method: "get",
|
2022-10-18 22:39:32 -04:00
|
|
|
noCache: true,
|
|
|
|
onComplete: function() {
|
|
|
|
clearTimeout(loadTorrentFilesDataTimer);
|
|
|
|
loadTorrentFilesDataTimer = loadTorrentFilesData.delay(5000);
|
|
|
|
},
|
|
|
|
onSuccess: function(files) {
|
|
|
|
clearTimeout(torrentFilesFilterInputTimer);
|
2024-10-03 08:37:11 -04:00
|
|
|
torrentFilesFilterInputTimer = -1;
|
2022-10-18 22:39:32 -04:00
|
|
|
|
|
|
|
if (files.length === 0) {
|
|
|
|
torrentFilesTable.clear();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
handleNewTorrentFiles(files);
|
|
|
|
if (loadedNewTorrent)
|
|
|
|
collapseAllNodes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).send();
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateData = function() {
|
|
|
|
clearTimeout(loadTorrentFilesDataTimer);
|
2024-10-03 08:37:11 -04:00
|
|
|
loadTorrentFilesDataTimer = -1;
|
2022-10-18 22:39:32 -04:00
|
|
|
loadTorrentFilesData();
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleNewTorrentFiles = function(files) {
|
|
|
|
is_seed = (files.length > 0) ? files[0].is_seed : true;
|
|
|
|
|
2024-10-03 08:37:11 -04:00
|
|
|
const rows = files.map((file, index) => {
|
2022-10-18 22:39:32 -04:00
|
|
|
let progress = (file.progress * 100).round(1);
|
|
|
|
if ((progress === 100) && (file.progress < 1))
|
|
|
|
progress = 99.9;
|
|
|
|
|
|
|
|
const ignore = (file.priority === FilePriority.Ignored);
|
|
|
|
const checked = (ignore ? TriState.Unchecked : TriState.Checked);
|
|
|
|
const remaining = (ignore ? 0 : (file.size * (1.0 - file.progress)));
|
|
|
|
const row = {
|
|
|
|
fileId: index,
|
|
|
|
checked: checked,
|
|
|
|
fileName: file.name,
|
|
|
|
name: window.qBittorrent.Filesystem.fileName(file.name),
|
|
|
|
size: file.size,
|
|
|
|
progress: progress,
|
|
|
|
priority: normalizePriority(file.priority),
|
|
|
|
remaining: remaining,
|
|
|
|
availability: file.availability
|
|
|
|
};
|
|
|
|
|
|
|
|
return row;
|
|
|
|
});
|
|
|
|
|
|
|
|
addRowsToTable(rows);
|
|
|
|
updateGlobalCheckbox();
|
|
|
|
};
|
|
|
|
|
|
|
|
const addRowsToTable = function(rows) {
|
|
|
|
const selectedFiles = torrentFilesTable.selectedRowsIds();
|
|
|
|
let rowId = 0;
|
|
|
|
|
|
|
|
const rootNode = new window.qBittorrent.FileTree.FolderNode();
|
|
|
|
|
2024-10-03 08:37:11 -04:00
|
|
|
rows.forEach((row) => {
|
2022-10-18 22:39:32 -04:00
|
|
|
const pathItems = row.fileName.split(window.qBittorrent.Filesystem.PathSeparator);
|
|
|
|
|
|
|
|
pathItems.pop(); // remove last item (i.e. file name)
|
|
|
|
let parent = rootNode;
|
2024-10-03 08:37:11 -04:00
|
|
|
pathItems.forEach((folderName) => {
|
|
|
|
if (folderName === ".unwanted")
|
2022-10-18 22:39:32 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
let folderNode = null;
|
|
|
|
if (parent.children !== null) {
|
|
|
|
for (let i = 0; i < parent.children.length; ++i) {
|
|
|
|
const childFolder = parent.children[i];
|
|
|
|
if (childFolder.name === folderName) {
|
|
|
|
folderNode = childFolder;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (folderNode === null) {
|
|
|
|
folderNode = new window.qBittorrent.FileTree.FolderNode();
|
|
|
|
folderNode.path = (parent.path === "")
|
|
|
|
? folderName
|
|
|
|
: [parent.path, folderName].join(window.qBittorrent.Filesystem.PathSeparator);
|
|
|
|
folderNode.name = folderName;
|
|
|
|
folderNode.rowId = rowId;
|
|
|
|
folderNode.root = parent;
|
|
|
|
parent.addChild(folderNode);
|
|
|
|
|
|
|
|
++rowId;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent = folderNode;
|
|
|
|
});
|
|
|
|
|
|
|
|
const isChecked = row.checked ? TriState.Checked : TriState.Unchecked;
|
|
|
|
const remaining = (row.priority === FilePriority.Ignored) ? 0 : row.remaining;
|
|
|
|
const childNode = new window.qBittorrent.FileTree.FileNode();
|
|
|
|
childNode.name = row.name;
|
|
|
|
childNode.path = row.fileName;
|
|
|
|
childNode.rowId = rowId;
|
|
|
|
childNode.size = row.size;
|
|
|
|
childNode.checked = isChecked;
|
|
|
|
childNode.remaining = remaining;
|
|
|
|
childNode.progress = row.progress;
|
|
|
|
childNode.priority = row.priority;
|
|
|
|
childNode.availability = row.availability;
|
|
|
|
childNode.root = parent;
|
|
|
|
childNode.data = row;
|
|
|
|
parent.addChild(childNode);
|
|
|
|
|
|
|
|
++rowId;
|
2024-10-03 08:37:11 -04:00
|
|
|
});
|
2022-10-18 22:39:32 -04:00
|
|
|
|
|
|
|
torrentFilesTable.populateTable(rootNode);
|
|
|
|
torrentFilesTable.updateTable(false);
|
|
|
|
|
|
|
|
if (selectedFiles.length > 0)
|
|
|
|
torrentFilesTable.reselectRows(selectedFiles);
|
|
|
|
};
|
|
|
|
|
|
|
|
const collapseIconClicked = function(event) {
|
2024-10-03 08:37:11 -04:00
|
|
|
const id = event.getAttribute("data-id");
|
2022-10-18 22:39:32 -04:00
|
|
|
const node = torrentFilesTable.getNode(id);
|
2024-10-03 08:37:11 -04:00
|
|
|
const isCollapsed = (event.parentElement.getAttribute("data-collapsed") === "true");
|
2022-10-18 22:39:32 -04:00
|
|
|
|
|
|
|
if (isCollapsed)
|
|
|
|
expandNode(node);
|
|
|
|
else
|
|
|
|
collapseNode(node);
|
|
|
|
};
|
|
|
|
|
|
|
|
const expandFolder = function(id) {
|
|
|
|
const node = torrentFilesTable.getNode(id);
|
2024-10-03 08:37:11 -04:00
|
|
|
if (node.isFolder)
|
2022-10-18 22:39:32 -04:00
|
|
|
expandNode(node);
|
|
|
|
};
|
|
|
|
|
|
|
|
const collapseFolder = function(id) {
|
|
|
|
const node = torrentFilesTable.getNode(id);
|
2024-10-03 08:37:11 -04:00
|
|
|
if (node.isFolder)
|
2022-10-18 22:39:32 -04:00
|
|
|
collapseNode(node);
|
|
|
|
};
|
|
|
|
|
|
|
|
const filesPriorityMenuClicked = function(priority) {
|
|
|
|
const selectedRows = torrentFilesTable.selectedRowsIds();
|
|
|
|
if (selectedRows.length === 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const rowIds = [];
|
|
|
|
const fileIds = [];
|
2024-10-03 08:37:11 -04:00
|
|
|
selectedRows.forEach((rowId) => {
|
|
|
|
const elem = $("comboPrio" + rowId);
|
2022-10-18 22:39:32 -04:00
|
|
|
rowIds.push(rowId);
|
2024-10-03 08:37:11 -04:00
|
|
|
fileIds.push(elem.getAttribute("data-file-id"));
|
2022-10-18 22:39:32 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
const uniqueRowIds = {};
|
|
|
|
const uniqueFileIds = {};
|
|
|
|
for (let i = 0; i < rowIds.length; ++i) {
|
|
|
|
const rows = getAllChildren(rowIds[i], fileIds[i]);
|
2024-10-03 08:37:11 -04:00
|
|
|
rows.rowIds.forEach((rowId) => {
|
2022-10-18 22:39:32 -04:00
|
|
|
uniqueRowIds[rowId] = true;
|
|
|
|
});
|
2024-10-03 08:37:11 -04:00
|
|
|
rows.fileIds.forEach((fileId) => {
|
2022-10-18 22:39:32 -04:00
|
|
|
uniqueFileIds[fileId] = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setFilePriority(Object.keys(uniqueRowIds), Object.keys(uniqueFileIds), priority);
|
|
|
|
};
|
|
|
|
|
2023-07-18 11:58:05 -04:00
|
|
|
const singleFileRename = function(hash) {
|
|
|
|
const rowId = torrentFilesTable.selectedRowsIds()[0];
|
|
|
|
if (rowId === undefined)
|
|
|
|
return;
|
2024-10-03 08:37:11 -04:00
|
|
|
const row = torrentFilesTable.rows.get(rowId);
|
2023-07-18 11:58:05 -04:00
|
|
|
if (!row)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const node = torrentFilesTable.getNode(rowId);
|
|
|
|
const path = node.path;
|
|
|
|
|
|
|
|
new MochaUI.Window({
|
2024-10-03 08:37:11 -04:00
|
|
|
id: "renamePage",
|
|
|
|
icon: "images/qbittorrent-tray.svg",
|
2023-07-18 11:58:05 -04:00
|
|
|
title: "Renaming",
|
2024-10-03 08:37:11 -04:00
|
|
|
loadMethod: "iframe",
|
|
|
|
contentURL: "rename_file.html?hash=" + hash + "&isFolder=" + node.isFolder
|
|
|
|
+ "&path=" + encodeURIComponent(path),
|
2023-07-18 11:58:05 -04:00
|
|
|
scrollbars: false,
|
|
|
|
resizable: true,
|
|
|
|
maximizable: false,
|
|
|
|
paddingVertical: 0,
|
|
|
|
paddingHorizontal: 0,
|
|
|
|
width: 400,
|
|
|
|
height: 100
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const multiFileRename = function(hash) {
|
|
|
|
new MochaUI.Window({
|
2024-10-03 08:37:11 -04:00
|
|
|
id: "multiRenamePage",
|
|
|
|
icon: "images/qbittorrent-tray.svg",
|
2023-07-18 11:58:05 -04:00
|
|
|
title: "Renaming",
|
|
|
|
data: { hash: hash, selectedRows: torrentFilesTable.selectedRows },
|
2024-10-03 08:37:11 -04:00
|
|
|
loadMethod: "xhr",
|
|
|
|
contentURL: "rename_files.html",
|
2023-07-18 11:58:05 -04:00
|
|
|
scrollbars: false,
|
|
|
|
resizable: true,
|
|
|
|
maximizable: false,
|
|
|
|
paddingVertical: 0,
|
|
|
|
paddingHorizontal: 0,
|
|
|
|
width: 800,
|
|
|
|
height: 420,
|
2024-10-03 08:37:11 -04:00
|
|
|
resizeLimit: { "x": [800], "y": [420] }
|
2023-07-18 11:58:05 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-10-18 22:39:32 -04:00
|
|
|
const torrentFilesContextMenu = new window.qBittorrent.ContextMenu.ContextMenu({
|
2024-10-03 08:37:11 -04:00
|
|
|
targets: "#torrentFilesTableDiv tr",
|
|
|
|
menu: "torrentFilesMenu",
|
2022-10-18 22:39:32 -04:00
|
|
|
actions: {
|
|
|
|
Rename: function(element, ref) {
|
|
|
|
const hash = torrentsTable.getCurrentTorrentID();
|
|
|
|
if (!hash)
|
|
|
|
return;
|
|
|
|
|
2024-10-03 08:37:11 -04:00
|
|
|
if (torrentFilesTable.selectedRowsIds().length > 1)
|
2023-07-18 11:58:05 -04:00
|
|
|
multiFileRename(hash);
|
2024-10-03 08:37:11 -04:00
|
|
|
else
|
2023-07-18 11:58:05 -04:00
|
|
|
singleFileRename(hash);
|
2022-10-18 22:39:32 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
FilePrioIgnore: function(element, ref) {
|
|
|
|
filesPriorityMenuClicked(FilePriority.Ignored);
|
|
|
|
},
|
|
|
|
FilePrioNormal: function(element, ref) {
|
|
|
|
filesPriorityMenuClicked(FilePriority.Normal);
|
|
|
|
},
|
|
|
|
FilePrioHigh: function(element, ref) {
|
|
|
|
filesPriorityMenuClicked(FilePriority.High);
|
|
|
|
},
|
|
|
|
FilePrioMaximum: function(element, ref) {
|
|
|
|
filesPriorityMenuClicked(FilePriority.Maximum);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
offsets: {
|
|
|
|
x: -15,
|
|
|
|
y: 2
|
|
|
|
},
|
|
|
|
onShow: function() {
|
|
|
|
if (is_seed)
|
2024-10-03 08:37:11 -04:00
|
|
|
this.hideItem("FilePrio");
|
2022-10-18 22:39:32 -04:00
|
|
|
else
|
2024-10-03 08:37:11 -04:00
|
|
|
this.showItem("FilePrio");
|
2022-10-18 22:39:32 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-10-03 08:37:11 -04:00
|
|
|
torrentFilesTable.setup("torrentFilesTableDiv", "torrentFilesTableFixedHeaderDiv", torrentFilesContextMenu);
|
2022-10-18 22:39:32 -04:00
|
|
|
// inject checkbox into table header
|
2024-10-03 08:37:11 -04:00
|
|
|
const tableHeaders = $$("#torrentFilesTableFixedHeaderDiv .dynamicTableHeader th");
|
2022-10-18 22:39:32 -04:00
|
|
|
if (tableHeaders.length > 0) {
|
2024-10-03 08:37:11 -04:00
|
|
|
const checkbox = new Element("input");
|
|
|
|
checkbox.type = "checkbox";
|
|
|
|
checkbox.id = "tristate_cb";
|
|
|
|
checkbox.addEventListener("click", switchCheckboxState);
|
2022-10-18 22:39:32 -04:00
|
|
|
|
|
|
|
const checkboxTH = tableHeaders[0];
|
|
|
|
checkbox.injectInside(checkboxTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
// default sort by name column
|
|
|
|
if (torrentFilesTable.getSortedColumn() === null)
|
2024-10-03 08:37:11 -04:00
|
|
|
torrentFilesTable.setSortedColumn("name");
|
2022-10-18 22:39:32 -04:00
|
|
|
|
|
|
|
// listen for changes to torrentFilesFilterInput
|
2024-10-03 08:37:11 -04:00
|
|
|
let torrentFilesFilterInputTimer = -1;
|
|
|
|
$("torrentFilesFilterInput").addEventListener("input", () => {
|
|
|
|
clearTimeout(torrentFilesFilterInputTimer);
|
2022-10-18 22:39:32 -04:00
|
|
|
|
2024-10-03 08:37:11 -04:00
|
|
|
const value = $("torrentFilesFilterInput").value;
|
|
|
|
torrentFilesTable.setFilter(value);
|
|
|
|
|
|
|
|
torrentFilesFilterInputTimer = setTimeout(() => {
|
|
|
|
torrentFilesFilterInputTimer = -1;
|
|
|
|
|
|
|
|
if (current_hash === "")
|
|
|
|
return;
|
|
|
|
|
|
|
|
torrentFilesTable.updateTable();
|
|
|
|
|
|
|
|
if (value.trim() === "")
|
|
|
|
collapseAllNodes();
|
|
|
|
else
|
|
|
|
expandAllNodes();
|
|
|
|
}, window.qBittorrent.Misc.FILTER_INPUT_DELAY);
|
2022-10-18 22:39:32 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show/hide a node's row
|
|
|
|
*/
|
|
|
|
const _hideNode = function(node, shouldHide) {
|
2024-10-03 08:37:11 -04:00
|
|
|
const span = $("filesTablefileName" + node.rowId);
|
2022-10-18 22:39:32 -04:00
|
|
|
// span won't exist if row has been filtered out
|
|
|
|
if (span === null)
|
|
|
|
return;
|
|
|
|
const rowElem = span.parentElement.parentElement;
|
|
|
|
if (shouldHide)
|
|
|
|
rowElem.addClass("invisible");
|
|
|
|
else
|
|
|
|
rowElem.removeClass("invisible");
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a node's collapsed state and icon
|
|
|
|
*/
|
|
|
|
const _updateNodeState = function(node, isCollapsed) {
|
2024-10-03 08:37:11 -04:00
|
|
|
const span = $("filesTablefileName" + node.rowId);
|
2022-10-18 22:39:32 -04:00
|
|
|
// span won't exist if row has been filtered out
|
|
|
|
if (span === null)
|
|
|
|
return;
|
|
|
|
const td = span.parentElement;
|
|
|
|
|
|
|
|
// store collapsed state
|
2024-10-03 08:37:11 -04:00
|
|
|
td.setAttribute("data-collapsed", isCollapsed);
|
2022-10-18 22:39:32 -04:00
|
|
|
|
|
|
|
// rotate the collapse icon
|
|
|
|
const collapseIcon = td.getElementsByClassName("filesTableCollapseIcon")[0];
|
|
|
|
if (isCollapsed)
|
|
|
|
collapseIcon.addClass("rotate");
|
|
|
|
else
|
|
|
|
collapseIcon.removeClass("rotate");
|
|
|
|
};
|
|
|
|
|
|
|
|
const _isCollapsed = function(node) {
|
2024-10-03 08:37:11 -04:00
|
|
|
const span = $("filesTablefileName" + node.rowId);
|
2022-10-18 22:39:32 -04:00
|
|
|
if (span === null)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const td = span.parentElement;
|
2024-10-03 08:37:11 -04:00
|
|
|
return td.getAttribute("data-collapsed") === "true";
|
2022-10-18 22:39:32 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const expandNode = function(node) {
|
|
|
|
_collapseNode(node, false, false, false);
|
|
|
|
};
|
|
|
|
|
|
|
|
const collapseNode = function(node) {
|
|
|
|
_collapseNode(node, true, false, false);
|
|
|
|
};
|
|
|
|
|
|
|
|
const expandAllNodes = function() {
|
|
|
|
const root = torrentFilesTable.getRoot();
|
2024-10-03 08:37:11 -04:00
|
|
|
root.children.each((node) => {
|
|
|
|
node.children.each((child) => {
|
2022-10-18 22:39:32 -04:00
|
|
|
_collapseNode(child, false, true, false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const collapseAllNodes = function() {
|
|
|
|
const root = torrentFilesTable.getRoot();
|
2024-10-03 08:37:11 -04:00
|
|
|
root.children.each((node) => {
|
|
|
|
node.children.each((child) => {
|
2022-10-18 22:39:32 -04:00
|
|
|
_collapseNode(child, true, true, false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collapses a folder node with the option to recursively collapse all children
|
|
|
|
* @param {FolderNode} node the node to collapse/expand
|
|
|
|
* @param {boolean} shouldCollapse true if the node should be collapsed, false if it should be expanded
|
|
|
|
* @param {boolean} applyToChildren true if the node's children should also be collapsed, recursively
|
|
|
|
* @param {boolean} isChildNode true if the current node is a child of the original node we collapsed/expanded
|
|
|
|
*/
|
|
|
|
const _collapseNode = function(node, shouldCollapse, applyToChildren, isChildNode) {
|
|
|
|
if (!node.isFolder)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const shouldExpand = !shouldCollapse;
|
|
|
|
const isNodeCollapsed = _isCollapsed(node);
|
|
|
|
const nodeInCorrectState = ((shouldCollapse && isNodeCollapsed) || (shouldExpand && !isNodeCollapsed));
|
|
|
|
const canSkipNode = (isChildNode && (!applyToChildren || nodeInCorrectState));
|
|
|
|
if (!isChildNode || applyToChildren || !canSkipNode)
|
|
|
|
_updateNodeState(node, shouldCollapse);
|
|
|
|
|
2024-10-03 08:37:11 -04:00
|
|
|
node.children.each((child) => {
|
2022-10-18 22:39:32 -04:00
|
|
|
_hideNode(child, shouldCollapse);
|
|
|
|
|
|
|
|
if (!child.isFolder)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// don't expand children that have been independently collapsed, unless applyToChildren is true
|
|
|
|
const shouldExpandChildren = (shouldExpand && applyToChildren);
|
|
|
|
const isChildCollapsed = _isCollapsed(child);
|
|
|
|
if (!shouldExpandChildren && isChildCollapsed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_collapseNode(child, shouldCollapse, applyToChildren, true);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return exports();
|
|
|
|
})();
|
|
|
|
Object.freeze(window.qBittorrent.PropFiles);
|