qBittorrent/src/gui/addnewtorrentdialog.cpp

694 lines
26 KiB
C++
Raw Normal View History

2012-05-15 20:57:31 +04:00
/*
2015-04-19 18:17:47 +03:00
* Bittorrent Client using Qt and libtorrent.
2018-04-14 22:53:45 +03:00
* Copyright (C) 2012 Christophe Dumez <chris@qbittorrent.org>
2012-05-15 20:57:31 +04:00
*
* 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.
*/
#include "addnewtorrentdialog.h"
2015-06-02 12:09:15 +03:00
#include <QDebug>
#include <QDir>
#include <QMenu>
#include <QPushButton>
#include <QShortcut>
#include <QString>
#include <QUrl>
#include <QVector>
2015-06-02 12:09:15 +03:00
#include "base/bittorrent/downloadpriority.h"
#include "base/bittorrent/magneturi.h"
#include "base/bittorrent/session.h"
#include "base/bittorrent/torrenthandle.h"
#include "base/global.h"
#include "base/net/downloadmanager.h"
#include "base/settingsstorage.h"
#include "base/torrentfileguard.h"
2015-09-25 11:10:05 +03:00
#include "base/utils/fs.h"
#include "base/utils/misc.h"
#include "base/utils/string.h"
2018-04-14 22:53:45 +03:00
#include "autoexpandabledialog.h"
2015-06-02 12:09:15 +03:00
#include "proplistdelegate.h"
#include "raisedmessagebox.h"
2015-06-02 12:09:15 +03:00
#include "torrentcontentfiltermodel.h"
#include "torrentcontentmodel.h"
#include "ui_addnewtorrentdialog.h"
#include "uithememanager.h"
#include "utils.h"
2012-05-15 20:57:31 +04:00
namespace
{
#define SETTINGS_KEY(name) "AddNewTorrentDialog/" name
const QString KEY_ENABLED = QStringLiteral(SETTINGS_KEY("Enabled"));
const QString KEY_DEFAULTCATEGORY = QStringLiteral(SETTINGS_KEY("DefaultCategory"));
const QString KEY_TREEHEADERSTATE = QStringLiteral(SETTINGS_KEY("TreeHeaderState"));
const QString KEY_TOPLEVEL = QStringLiteral(SETTINGS_KEY("TopLevel"));
const QString KEY_SAVEPATHHISTORY = QStringLiteral(SETTINGS_KEY("SavePathHistory"));
const QString KEY_SAVEPATHHISTORYLENGTH = QStringLiteral(SETTINGS_KEY("SavePathHistoryLength"));
const QString KEY_REMEMBERLASTSAVEPATH = QStringLiteral(SETTINGS_KEY("RememberLastSavePath"));
2017-04-05 10:21:12 +03:00
// just a shortcut
inline SettingsStorage *settings()
{
return SettingsStorage::instance();
}
}
const int AddNewTorrentDialog::minPathHistoryLength;
const int AddNewTorrentDialog::maxPathHistoryLength;
AddNewTorrentDialog::AddNewTorrentDialog(const BitTorrent::AddTorrentParams &inParams, QWidget *parent)
: QDialog(parent)
2018-04-14 22:53:45 +03:00
, m_ui(new Ui::AddNewTorrentDialog)
, m_contentModel(nullptr)
, m_contentDelegate(nullptr)
, m_hasMetadata(false)
, m_oldIndex(0)
, m_torrentParams(inParams)
, m_storeDialogSize(SETTINGS_KEY("DialogSize"))
, m_storeSplitterState(SETTINGS_KEY("SplitterState"))
2012-05-15 20:57:31 +04:00
{
// TODO: set dialog file properties using m_torrentParams.filePriorities
2018-04-14 22:53:45 +03:00
m_ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
2018-04-14 22:53:45 +03:00
m_ui->lblMetaLoading->setVisible(false);
m_ui->progMetaLoading->setVisible(false);
2018-04-14 22:53:45 +03:00
m_ui->savePath->setMode(FileSystemPathEdit::Mode::DirectorySave);
m_ui->savePath->setDialogCaption(tr("Choose save path"));
m_ui->savePath->setMaxVisibleItems(20);
const auto *session = BitTorrent::Session::instance();
if (m_torrentParams.addPaused == TriStateBool::True)
2018-04-14 22:53:45 +03:00
m_ui->startTorrentCheckBox->setChecked(false);
else if (m_torrentParams.addPaused == TriStateBool::False)
2018-04-14 22:53:45 +03:00
m_ui->startTorrentCheckBox->setChecked(true);
else
2018-04-14 22:53:45 +03:00
m_ui->startTorrentCheckBox->setChecked(!session->isAddTorrentPaused());
2018-04-14 22:53:45 +03:00
m_ui->comboTTM->blockSignals(true); // the TreeView size isn't correct if the slot does it job at this point
m_ui->comboTTM->setCurrentIndex(!session->isAutoTMMDisabledByDefault());
m_ui->comboTTM->blockSignals(false);
populateSavePathComboBox();
2018-04-14 22:53:45 +03:00
connect(m_ui->savePath, &FileSystemPathEdit::selectedPathChanged, this, &AddNewTorrentDialog::onSavePathChanged);
const bool rememberLastSavePath = settings()->loadValue(KEY_REMEMBERLASTSAVEPATH, false).toBool();
m_ui->checkBoxRememberLastSavePath->setChecked(rememberLastSavePath);
if (m_torrentParams.createSubfolder == TriStateBool::True)
2018-04-14 22:53:45 +03:00
m_ui->createSubfolderCheckBox->setChecked(true);
else if (m_torrentParams.createSubfolder == TriStateBool::False)
2018-04-14 22:53:45 +03:00
m_ui->createSubfolderCheckBox->setChecked(false);
else
2018-04-14 22:53:45 +03:00
m_ui->createSubfolderCheckBox->setChecked(session->isCreateTorrentSubfolder());
m_ui->sequentialCheckBox->setChecked(m_torrentParams.sequential);
m_ui->firstLastCheckBox->setChecked(m_torrentParams.firstLastPiecePriority);
2018-04-14 22:53:45 +03:00
m_ui->skipCheckingCheckBox->setChecked(m_torrentParams.skipChecking);
m_ui->doNotDeleteTorrentCheckBox->setVisible(TorrentFileGuard::autoDeleteMode() != TorrentFileGuard::Never);
// Load categories
2017-09-24 14:54:42 +03:00
QStringList categories = session->categories().keys();
std::sort(categories.begin(), categories.end(), Utils::String::naturalLessThan<Qt::CaseInsensitive>);
QString defaultCategory = settings()->loadValue(KEY_DEFAULTCATEGORY).toString();
2015-11-03 18:05:15 +03:00
if (!m_torrentParams.category.isEmpty())
2018-04-14 22:53:45 +03:00
m_ui->categoryComboBox->addItem(m_torrentParams.category);
if (!defaultCategory.isEmpty())
2018-04-14 22:53:45 +03:00
m_ui->categoryComboBox->addItem(defaultCategory);
m_ui->categoryComboBox->addItem("");
2015-11-03 18:05:15 +03:00
for (const QString &category : asConst(categories))
if (category != defaultCategory && category != m_torrentParams.category)
2018-04-14 22:53:45 +03:00
m_ui->categoryComboBox->addItem(category);
2015-11-03 18:05:15 +03:00
2018-04-14 22:53:45 +03:00
m_ui->contentTreeView->header()->setSortIndicator(0, Qt::AscendingOrder);
loadState();
// Signal / slots
2018-04-14 22:53:45 +03:00
connect(m_ui->doNotDeleteTorrentCheckBox, &QCheckBox::clicked, this, &AddNewTorrentDialog::doNotDeleteTorrentClicked);
QShortcut *editHotkey = new QShortcut(Qt::Key_F2, m_ui->contentTreeView, nullptr, nullptr, Qt::WidgetShortcut);
connect(editHotkey, &QShortcut::activated
, this, [this]() { m_ui->contentTreeView->renameSelectedFile(m_torrentInfo); });
connect(m_ui->contentTreeView, &QAbstractItemView::doubleClicked
, this, [this]() { m_ui->contentTreeView->renameSelectedFile(m_torrentInfo); });
2018-04-14 22:53:45 +03:00
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setFocus();
2012-05-15 20:57:31 +04:00
}
AddNewTorrentDialog::~AddNewTorrentDialog()
{
saveState();
delete m_contentDelegate;
2018-04-14 22:53:45 +03:00
delete m_ui;
2012-05-15 20:57:31 +04:00
}
bool AddNewTorrentDialog::isEnabled()
{
return SettingsStorage::instance()->loadValue(KEY_ENABLED, true).toBool();
}
void AddNewTorrentDialog::setEnabled(bool value)
{
SettingsStorage::instance()->storeValue(KEY_ENABLED, value);
}
bool AddNewTorrentDialog::isTopLevel()
{
return SettingsStorage::instance()->loadValue(KEY_TOPLEVEL, true).toBool();
}
void AddNewTorrentDialog::setTopLevel(bool value)
{
SettingsStorage::instance()->storeValue(KEY_TOPLEVEL, value);
}
int AddNewTorrentDialog::savePathHistoryLength()
{
const int defaultHistoryLength = 8;
const int value = settings()->loadValue(KEY_SAVEPATHHISTORYLENGTH, defaultHistoryLength).toInt();
return qBound(minPathHistoryLength, value, maxPathHistoryLength);
}
void AddNewTorrentDialog::setSavePathHistoryLength(int value)
{
const int clampedValue = qBound(minPathHistoryLength, value, maxPathHistoryLength);
const int oldValue = savePathHistoryLength();
if (clampedValue == oldValue)
return;
settings()->storeValue(KEY_SAVEPATHHISTORYLENGTH, clampedValue);
settings()->storeValue(KEY_SAVEPATHHISTORY
, QStringList(settings()->loadValue(KEY_SAVEPATHHISTORY).toStringList().mid(0, clampedValue)));
}
void AddNewTorrentDialog::loadState()
{
Utils::Gui::resize(this, m_storeDialogSize);
m_ui->splitter->restoreState(m_storeSplitterState);
m_headerState = settings()->loadValue(KEY_TREEHEADERSTATE).toByteArray();
}
void AddNewTorrentDialog::saveState()
{
m_storeDialogSize = size();
m_storeSplitterState = m_ui->splitter->saveState();
if (m_contentModel)
2018-04-14 22:53:45 +03:00
settings()->storeValue(KEY_TREEHEADERSTATE, m_ui->contentTreeView->header()->saveState());
}
void AddNewTorrentDialog::show(const QString &source, const BitTorrent::AddTorrentParams &inParams, QWidget *parent)
2012-05-15 20:57:31 +04:00
{
2019-02-22 04:59:31 +03:00
auto *dlg = new AddNewTorrentDialog(inParams, parent);
if (Net::DownloadManager::hasSupportedScheme(source)) {
2015-04-19 18:17:47 +03:00
// Launch downloader
2019-03-03 12:43:42 +03:00
Net::DownloadManager::instance()->download(
Net::DownloadRequest(source).limit(MAX_TORRENT_SIZE)
2019-03-03 12:43:42 +03:00
, dlg, &AddNewTorrentDialog::handleDownloadFinished);
return;
}
const BitTorrent::MagnetUri magnetUri(source);
const bool isLoaded = magnetUri.isValid()
? dlg->loadMagnet(magnetUri)
2019-02-13 17:41:38 +03:00
: dlg->loadTorrentFile(source);
2019-02-13 17:41:38 +03:00
if (isLoaded)
dlg->QDialog::show();
2019-02-13 17:41:38 +03:00
else
delete dlg;
2012-05-15 20:57:31 +04:00
}
void AddNewTorrentDialog::show(const QString &source, QWidget *parent)
{
show(source, BitTorrent::AddTorrentParams(), parent);
}
2019-02-13 17:41:38 +03:00
bool AddNewTorrentDialog::loadTorrentFile(const QString &torrentPath)
2012-05-15 20:57:31 +04:00
{
2019-02-13 17:41:38 +03:00
const QString decodedPath = torrentPath.startsWith("file://", Qt::CaseInsensitive)
? QUrl::fromEncoded(torrentPath.toLocal8Bit()).toLocalFile()
: torrentPath;
2015-04-19 18:17:47 +03:00
QString error;
2019-02-13 17:41:38 +03:00
m_torrentInfo = BitTorrent::TorrentInfo::loadFromFile(decodedPath, &error);
2015-04-19 18:17:47 +03:00
if (!m_torrentInfo.isValid()) {
2019-02-13 17:41:38 +03:00
RaisedMessageBox::critical(this, tr("Invalid torrent")
, tr("Failed to load the torrent: %1.\nError: %2", "Don't remove the '\n' characters. They insert a newline.")
.arg(Utils::Fs::toNativePath(decodedPath), error));
return false;
}
2019-02-13 17:41:38 +03:00
m_torrentGuard.reset(new TorrentFileGuard(decodedPath));
return loadTorrentImpl();
}
bool AddNewTorrentDialog::loadTorrentImpl()
{
m_hasMetadata = true;
2015-04-19 18:17:47 +03:00
m_hash = m_torrentInfo.hash();
// Prevent showing the dialog if download is already present
2015-04-19 18:17:47 +03:00
if (BitTorrent::Session::instance()->isKnownTorrent(m_hash)) {
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(m_hash);
if (torrent) {
if (torrent->isPrivate() || m_torrentInfo.isPrivate()) {
RaisedMessageBox::warning(this, tr("Torrent is already present"), tr("Torrent '%1' is already in the transfer list. Trackers haven't been merged because it is a private torrent.").arg(torrent->name()), QMessageBox::Ok);
}
else {
torrent->addTrackers(m_torrentInfo.trackers());
torrent->addUrlSeeds(m_torrentInfo.urlSeeds());
RaisedMessageBox::information(this, tr("Torrent is already present"), tr("Torrent '%1' is already in the transfer list. Trackers have been merged.").arg(torrent->name()), QMessageBox::Ok);
}
2015-04-19 18:17:47 +03:00
}
else {
RaisedMessageBox::information(this, tr("Torrent is already present"), tr("Torrent is already queued for processing."), QMessageBox::Ok);
2015-04-19 18:17:47 +03:00
}
return false;
}
m_ui->labelHashData->setText(m_hash);
setupTreeview();
2018-04-14 22:53:45 +03:00
TMMChanged(m_ui->comboTTM->currentIndex());
return true;
2012-05-15 20:57:31 +04:00
}
bool AddNewTorrentDialog::loadMagnet(const BitTorrent::MagnetUri &magnetUri)
2012-05-15 20:57:31 +04:00
{
if (!magnetUri.isValid()) {
RaisedMessageBox::critical(this, tr("Invalid magnet link"), tr("This magnet link was not recognized"));
return false;
}
m_torrentGuard.reset(new TorrentFileGuard(QString()));
m_hash = magnetUri.hash();
// Prevent showing the dialog if download is already present
2015-04-19 18:17:47 +03:00
if (BitTorrent::Session::instance()->isKnownTorrent(m_hash)) {
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(m_hash);
if (torrent) {
if (torrent->isPrivate()) {
RaisedMessageBox::warning(this, tr("Torrent is already present"), tr("Torrent '%1' is already in the transfer list. Trackers haven't been merged because it is a private torrent.").arg(torrent->name()), QMessageBox::Ok);
}
else {
torrent->addTrackers(magnetUri.trackers());
torrent->addUrlSeeds(magnetUri.urlSeeds());
RaisedMessageBox::information(this, tr("Torrent is already present"), tr("Magnet link '%1' is already in the transfer list. Trackers have been merged.").arg(torrent->name()), QMessageBox::Ok);
}
2015-04-19 18:17:47 +03:00
}
else {
RaisedMessageBox::information(this, tr("Torrent is already present"), tr("Magnet link is already queued for processing."), QMessageBox::Ok);
2015-04-19 18:17:47 +03:00
}
return false;
}
2018-04-18 16:59:41 +03:00
connect(BitTorrent::Session::instance(), &BitTorrent::Session::metadataLoaded, this, &AddNewTorrentDialog::updateMetadata);
2015-04-19 18:17:47 +03:00
// Set dialog title
2018-04-14 22:53:45 +03:00
QString torrentName = magnetUri.name();
setWindowTitle(torrentName.isEmpty() ? tr("Magnet link") : torrentName);
2012-05-15 20:57:31 +04:00
setupTreeview();
2018-04-14 22:53:45 +03:00
TMMChanged(m_ui->comboTTM->currentIndex());
2012-05-20 19:14:02 +04:00
BitTorrent::Session::instance()->loadMetadata(magnetUri);
setMetadataProgressIndicator(true, tr("Retrieving metadata..."));
m_ui->labelHashData->setText(m_hash);
return true;
2012-05-15 20:57:31 +04:00
}
2015-04-19 18:17:47 +03:00
void AddNewTorrentDialog::showEvent(QShowEvent *event)
{
QDialog::showEvent(event);
if (!isTopLevel()) return;
2015-04-19 18:17:47 +03:00
activateWindow();
raise();
}
2012-05-15 20:57:31 +04:00
void AddNewTorrentDialog::saveSavePathHistory() const
{
// Get current history
QStringList history = settings()->loadValue(KEY_SAVEPATHHISTORY).toStringList();
QVector<QDir> historyDirs;
for (const QString &path : asConst(history))
historyDirs << QDir {path};
const QDir selectedSavePath {m_ui->savePath->selectedPath()};
const int selectedSavePathIndex = historyDirs.indexOf(selectedSavePath);
if (selectedSavePathIndex > 0)
history.removeAt(selectedSavePathIndex);
if (selectedSavePathIndex != 0)
// Add last used save path to the front of history
history.push_front(selectedSavePath.absolutePath());
// Save history
settings()->storeValue(KEY_SAVEPATHHISTORY, QStringList {history.mid(0, savePathHistoryLength())});
2012-05-15 20:57:31 +04:00
}
2018-04-14 22:53:45 +03:00
// savePath is a folder, not an absolute file path
int AddNewTorrentDialog::indexOfSavePath(const QString &savePath)
2012-05-15 20:57:31 +04:00
{
2018-04-14 22:53:45 +03:00
QDir saveDir(savePath);
for (int i = 0; i < m_ui->savePath->count(); ++i)
if (QDir(m_ui->savePath->item(i)) == saveDir)
return i;
return -1;
2012-05-15 20:57:31 +04:00
}
void AddNewTorrentDialog::updateDiskSpaceLabel()
{
// Determine torrent size
2018-09-07 14:12:38 +03:00
qlonglong torrentSize = 0;
if (m_hasMetadata) {
if (m_contentModel) {
const QVector<BitTorrent::DownloadPriority> priorities = m_contentModel->model()->getFilePriorities();
2015-04-19 18:17:47 +03:00
Q_ASSERT(priorities.size() == m_torrentInfo.filesCount());
for (int i = 0; i < priorities.size(); ++i)
if (priorities[i] > BitTorrent::DownloadPriority::Ignored)
2018-04-14 22:53:45 +03:00
torrentSize += m_torrentInfo.fileSize(i);
}
else {
2018-04-14 22:53:45 +03:00
torrentSize = m_torrentInfo.totalSize();
}
2012-05-15 20:57:31 +04:00
}
const QString sizeString = tr("%1 (Free space on disk: %2)").arg(
((torrentSize > 0) ? Utils::Misc::friendlyUnit(torrentSize) : tr("Not available", "This size is unavailable."))
, Utils::Misc::friendlyUnit(Utils::Fs::freeDiskSpaceOnPath(m_ui->savePath->selectedPath())));
m_ui->labelSizeData->setText(sizeString);
2012-05-15 20:57:31 +04:00
}
void AddNewTorrentDialog::onSavePathChanged(const QString &newPath)
2012-05-15 20:57:31 +04:00
{
Q_UNUSED(newPath);
// Remember index
2018-04-14 22:53:45 +03:00
m_oldIndex = m_ui->savePath->currentIndex();
updateDiskSpaceLabel();
}
void AddNewTorrentDialog::categoryChanged(int index)
{
Q_UNUSED(index);
2018-04-14 22:53:45 +03:00
if (m_ui->comboTTM->currentIndex() == 1) {
QString savePath = BitTorrent::Session::instance()->categorySavePath(m_ui->categoryComboBox->currentText());
m_ui->savePath->setSelectedPath(Utils::Fs::toNativePath(savePath));
updateDiskSpaceLabel();
}
}
void AddNewTorrentDialog::setSavePath(const QString &newPath)
{
int existingIndex = indexOfSavePath(newPath);
if (existingIndex < 0) {
// New path, prepend to combo box
2018-04-14 22:53:45 +03:00
m_ui->savePath->insertItem(0, newPath);
existingIndex = 0;
}
2018-04-14 22:53:45 +03:00
m_ui->savePath->setCurrentIndex(existingIndex);
onSavePathChanged(newPath);
2012-05-15 20:57:31 +04:00
}
void AddNewTorrentDialog::populateSavePathComboBox()
2012-05-15 20:57:31 +04:00
{
2018-04-14 22:53:45 +03:00
m_ui->savePath->clear();
// Load save path history
const QStringList savePathHistory {settings()->loadValue(KEY_SAVEPATHHISTORY).toStringList()};
for (const QString &savePath : savePathHistory)
m_ui->savePath->addItem(savePath);
const bool rememberLastSavePath {settings()->loadValue(KEY_REMEMBERLASTSAVEPATH, false).toBool()};
const QString defSavePath {BitTorrent::Session::instance()->defaultSavePath()};
if (!m_torrentParams.savePath.isEmpty())
setSavePath(m_torrentParams.savePath);
else if (!rememberLastSavePath)
setSavePath(defSavePath);
// else last used save path will be selected since it is the first in the list
2012-05-15 20:57:31 +04:00
}
2017-04-05 10:21:12 +03:00
void AddNewTorrentDialog::displayContentTreeMenu(const QPoint &)
{
2018-04-14 22:53:45 +03:00
const QModelIndexList selectedRows = m_ui->contentTreeView->selectionModel()->selectedRows(0);
const auto applyPriorities = [this, selectedRows](const BitTorrent::DownloadPriority prio)
{
for (const QModelIndex &index : selectedRows) {
m_contentModel->setData(
m_contentModel->index(index.row(), PRIORITY, index.parent())
, static_cast<int>(prio));
2012-05-15 20:57:31 +04:00
}
};
QMenu *menu = new QMenu(this);
menu->setAttribute(Qt::WA_DeleteOnClose);
if (selectedRows.size() == 1) {
QAction *actRename = menu->addAction(UIThemeManager::instance()->getIcon("edit-rename"), tr("Rename..."));
connect(actRename, &QAction::triggered, this, [this]() { m_ui->contentTreeView->renameSelectedFile(m_torrentInfo); });
menu->addSeparator();
2012-05-15 20:57:31 +04:00
}
QMenu *subMenu = menu->addMenu(tr("Priority"));
connect(m_ui->actionNotDownloaded, &QAction::triggered, subMenu, [applyPriorities]()
{
applyPriorities(BitTorrent::DownloadPriority::Ignored);
});
subMenu->addAction(m_ui->actionNotDownloaded);
connect(m_ui->actionNormal, &QAction::triggered, subMenu, [applyPriorities]()
{
applyPriorities(BitTorrent::DownloadPriority::Normal);
});
subMenu->addAction(m_ui->actionNormal);
connect(m_ui->actionHigh, &QAction::triggered, subMenu, [applyPriorities]()
{
applyPriorities(BitTorrent::DownloadPriority::High);
});
subMenu->addAction(m_ui->actionHigh);
connect(m_ui->actionMaximum, &QAction::triggered, subMenu, [applyPriorities]()
{
applyPriorities(BitTorrent::DownloadPriority::Maximum);
});
subMenu->addAction(m_ui->actionMaximum);
menu->popup(QCursor::pos());
2012-05-15 20:57:31 +04:00
}
void AddNewTorrentDialog::accept()
2012-05-15 20:57:31 +04:00
{
// TODO: Check if destination actually exists
2018-04-14 22:53:45 +03:00
m_torrentParams.skipChecking = m_ui->skipCheckingCheckBox->isChecked();
// Category
2018-04-14 22:53:45 +03:00
m_torrentParams.category = m_ui->categoryComboBox->currentText();
if (m_ui->defaultCategoryCheckbox->isChecked())
settings()->storeValue(KEY_DEFAULTCATEGORY, m_torrentParams.category);
2015-11-03 18:05:15 +03:00
settings()->storeValue(KEY_REMEMBERLASTSAVEPATH, m_ui->checkBoxRememberLastSavePath->isChecked());
// Save file priorities
if (m_contentModel)
m_torrentParams.filePriorities = m_contentModel->model()->getFilePriorities();
2018-04-14 22:53:45 +03:00
m_torrentParams.addPaused = TriStateBool(!m_ui->startTorrentCheckBox->isChecked());
m_torrentParams.createSubfolder = TriStateBool(m_ui->createSubfolderCheckBox->isChecked());
2015-04-19 18:17:47 +03:00
m_torrentParams.sequential = m_ui->sequentialCheckBox->isChecked();
m_torrentParams.firstLastPiecePriority = m_ui->firstLastCheckBox->isChecked();
2018-04-14 22:53:45 +03:00
QString savePath = m_ui->savePath->selectedPath();
if (m_ui->comboTTM->currentIndex() != 1) { // 0 is Manual mode and 1 is Automatic mode. Handle all non 1 values as manual mode.
m_torrentParams.useAutoTMM = TriStateBool::False;
m_torrentParams.savePath = savePath;
saveSavePathHistory();
2015-04-19 18:17:47 +03:00
}
else {
m_torrentParams.useAutoTMM = TriStateBool::True;
}
2015-04-19 18:17:47 +03:00
setEnabled(!m_ui->checkBoxNeverShow->isChecked());
2015-04-19 18:17:47 +03:00
// Add torrent
if (!m_hasMetadata)
BitTorrent::Session::instance()->addTorrent(m_hash, m_torrentParams);
2015-04-19 18:17:47 +03:00
else
BitTorrent::Session::instance()->addTorrent(m_torrentInfo, m_torrentParams);
2015-04-19 18:17:47 +03:00
m_torrentGuard->markAsAddedToSession();
QDialog::accept();
}
void AddNewTorrentDialog::reject()
{
2015-04-19 18:17:47 +03:00
if (!m_hasMetadata) {
setMetadataProgressIndicator(false);
2015-04-19 18:17:47 +03:00
BitTorrent::Session::instance()->cancelLoadMetadata(m_hash);
}
2015-04-19 18:17:47 +03:00
QDialog::reject();
}
2015-04-19 18:17:47 +03:00
void AddNewTorrentDialog::updateMetadata(const BitTorrent::TorrentInfo &info)
{
2015-04-19 18:17:47 +03:00
if (info.hash() != m_hash) return;
disconnect(BitTorrent::Session::instance(), &BitTorrent::Session::metadataLoaded, this, &AddNewTorrentDialog::updateMetadata);
2015-04-19 18:17:47 +03:00
if (!info.isValid()) {
RaisedMessageBox::critical(this, tr("I/O Error"), ("Invalid metadata."));
2015-04-19 18:17:47 +03:00
setMetadataProgressIndicator(false, tr("Invalid metadata"));
return;
}
2015-04-19 18:17:47 +03:00
// Good to go
m_torrentInfo = info;
m_hasMetadata = true;
setMetadataProgressIndicator(true, tr("Parsing metadata..."));
// Update UI
setupTreeview();
setMetadataProgressIndicator(false, tr("Metadata retrieval complete"));
2012-05-15 20:57:31 +04:00
}
void AddNewTorrentDialog::setMetadataProgressIndicator(bool visibleIndicator, const QString &labelText)
{
// Always show info label when waiting for metadata
2018-04-14 22:53:45 +03:00
m_ui->lblMetaLoading->setVisible(true);
m_ui->lblMetaLoading->setText(labelText);
m_ui->progMetaLoading->setVisible(visibleIndicator);
}
void AddNewTorrentDialog::setupTreeview()
{
if (!m_hasMetadata) {
m_ui->labelCommentData->setText(tr("Not Available", "This comment is unavailable"));
m_ui->labelDateData->setText(tr("Not Available", "This date is unavailable"));
}
else {
// Set dialog title
2015-04-19 18:17:47 +03:00
setWindowTitle(m_torrentInfo.name());
// Set torrent information
2019-09-15 09:35:02 +03:00
m_ui->labelCommentData->setText(Utils::Misc::parseHtmlLinks(m_torrentInfo.comment().toHtmlEscaped()));
m_ui->labelDateData->setText(!m_torrentInfo.creationDate().isNull() ? m_torrentInfo.creationDate().toString(Qt::DefaultLocaleShortDate) : tr("Not available"));
// Prepare content tree
m_contentModel = new TorrentContentFilterModel(this);
2018-04-18 16:59:41 +03:00
connect(m_contentModel->model(), &TorrentContentModel::filteredFilesChanged, this, &AddNewTorrentDialog::updateDiskSpaceLabel);
2018-04-14 22:53:45 +03:00
m_ui->contentTreeView->setModel(m_contentModel);
m_contentDelegate = new PropListDelegate(nullptr);
2018-04-14 22:53:45 +03:00
m_ui->contentTreeView->setItemDelegate(m_contentDelegate);
connect(m_ui->contentTreeView, &QAbstractItemView::clicked, m_ui->contentTreeView
2019-07-24 20:41:09 +03:00
, qOverload<const QModelIndex &>(&QAbstractItemView::edit));
2018-04-14 22:53:45 +03:00
connect(m_ui->contentTreeView, &QWidget::customContextMenuRequested, this, &AddNewTorrentDialog::displayContentTreeMenu);
// List files in torrent
m_contentModel->model()->setupModelData(m_torrentInfo);
if (!m_headerState.isEmpty())
2018-04-14 22:53:45 +03:00
m_ui->contentTreeView->header()->restoreState(m_headerState);
// Hide useless columns after loading the header state
2018-04-14 22:53:45 +03:00
m_ui->contentTreeView->hideColumn(PROGRESS);
m_ui->contentTreeView->hideColumn(REMAINING);
m_ui->contentTreeView->hideColumn(AVAILABILITY);
// Expand single-item folders recursively
QModelIndex currentIndex;
while (m_contentModel->rowCount(currentIndex) == 1) {
currentIndex = m_contentModel->index(0, 0, currentIndex);
m_ui->contentTreeView->setExpanded(currentIndex, true);
}
}
updateDiskSpaceLabel();
}
2015-04-19 18:17:47 +03:00
void AddNewTorrentDialog::handleDownloadFinished(const Net::DownloadResult &result)
2015-04-19 18:17:47 +03:00
{
2019-02-13 17:41:38 +03:00
QString error;
switch (result.status) {
case Net::DownloadStatus::Success:
m_torrentInfo = BitTorrent::TorrentInfo::load(result.data, &error);
if (!m_torrentInfo.isValid()) {
RaisedMessageBox::critical(this, tr("Invalid torrent"), tr("Failed to load from URL: %1.\nError: %2")
.arg(result.url, error));
return;
}
2019-02-13 17:41:38 +03:00
m_torrentGuard.reset(new TorrentFileGuard);
if (loadTorrentImpl())
open();
else
deleteLater();
break;
case Net::DownloadStatus::RedirectedToMagnet:
if (loadMagnet(BitTorrent::MagnetUri(result.magnet)))
open();
else
deleteLater();
break;
default:
RaisedMessageBox::critical(this, tr("Download Error"),
tr("Cannot download '%1': %2").arg(result.url, result.errorString));
deleteLater();
}
2015-04-19 18:17:47 +03:00
}
2016-05-08 22:47:50 +03:00
void AddNewTorrentDialog::TMMChanged(int index)
{
2016-05-08 22:47:50 +03:00
if (index != 1) { // 0 is Manual mode and 1 is Automatic mode. Handle all non 1 values as manual mode.
populateSavePathComboBox();
2018-04-14 22:53:45 +03:00
m_ui->groupBoxSavePath->setEnabled(true);
m_ui->savePath->blockSignals(false);
m_ui->savePath->setCurrentIndex(m_oldIndex < m_ui->savePath->count() ? m_oldIndex : m_ui->savePath->count() - 1);
}
else {
2018-04-14 22:53:45 +03:00
m_ui->groupBoxSavePath->setEnabled(false);
m_ui->savePath->blockSignals(true);
m_ui->savePath->clear();
QString savePath = BitTorrent::Session::instance()->categorySavePath(m_ui->categoryComboBox->currentText());
m_ui->savePath->addItem(savePath);
updateDiskSpaceLabel();
}
}
2016-03-07 07:22:18 +03:00
void AddNewTorrentDialog::doNotDeleteTorrentClicked(bool checked)
{
m_torrentGuard->setAutoRemove(!checked);
}