Don't stuck loading on mismatching info-hashes in resume data
[qBittorrent.git] / src / gui / addtorrentparamswidget.cpp
blobdfad28bff71e69b56112526f7b8e22c4adc97212
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2023 Vladimir Golovnev <glassez@yandex.ru>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "addtorrentparamswidget.h"
31 #include <QVariant>
33 #include "base/bittorrent/session.h"
34 #include "base/bittorrent/torrent.h"
35 #include "base/utils/compare.h"
36 #include "base/utils/string.h"
37 #include "flowlayout.h"
38 #include "fspathedit.h"
39 #include "torrenttagsdialog.h"
40 #include "ui_addtorrentparamswidget.h"
42 namespace
44 std::optional<bool> toOptionalBool(const QVariant &data)
46 if (!data.isValid())
47 return std::nullopt;
49 Q_ASSERT(data.userType() == QMetaType::Bool);
50 return data.toBool();
53 BitTorrent::AddTorrentParams cleanParams(BitTorrent::AddTorrentParams params)
55 if (!params.useAutoTMM.has_value() || params.useAutoTMM.value())
57 params.savePath = Path();
58 params.downloadPath = Path();
59 params.useDownloadPath = std::nullopt;
62 if (!params.useDownloadPath.has_value() || !params.useDownloadPath.value())
64 params.downloadPath = Path();
67 return params;
71 AddTorrentParamsWidget::AddTorrentParamsWidget(BitTorrent::AddTorrentParams addTorrentParams, QWidget *parent)
72 : QWidget(parent)
73 , m_ui {new Ui::AddTorrentParamsWidget}
75 m_ui->setupUi(this);
77 m_ui->savePathEdit->setMode(FileSystemPathEdit::Mode::DirectorySave);
78 m_ui->savePathEdit->setDialogCaption(tr("Choose save path"));
80 m_ui->downloadPathEdit->setMode(FileSystemPathEdit::Mode::DirectorySave);
81 m_ui->downloadPathEdit->setDialogCaption(tr("Choose save path"));
83 m_ui->useDownloadPathComboBox->addItem(tr("Default"));
84 m_ui->useDownloadPathComboBox->addItem(tr("Yes"), true);
85 m_ui->useDownloadPathComboBox->addItem(tr("No"), false);
87 m_ui->comboTTM->addItem(tr("Default"));
88 m_ui->comboTTM->addItem(tr("Manual"), false);
89 m_ui->comboTTM->addItem(tr("Automatic"), true);
91 m_ui->contentLayoutComboBox->addItem(tr("Default"));
92 m_ui->contentLayoutComboBox->addItem(tr("Original"), QVariant::fromValue(BitTorrent::TorrentContentLayout::Original));
93 m_ui->contentLayoutComboBox->addItem(tr("Create subfolder"), QVariant::fromValue(BitTorrent::TorrentContentLayout::Subfolder));
94 m_ui->contentLayoutComboBox->addItem(tr("Don't create subfolder"), QVariant::fromValue(BitTorrent::TorrentContentLayout::NoSubfolder));
96 m_ui->stopConditionComboBox->addItem(tr("Default"));
97 m_ui->stopConditionComboBox->addItem(tr("None"), QVariant::fromValue(BitTorrent::Torrent::StopCondition::None));
98 m_ui->stopConditionComboBox->addItem(tr("Metadata received"), QVariant::fromValue(BitTorrent::Torrent::StopCondition::MetadataReceived));
99 m_ui->stopConditionComboBox->addItem(tr("Files checked"), QVariant::fromValue(BitTorrent::Torrent::StopCondition::FilesChecked));
101 m_ui->startTorrentComboBox->addItem(tr("Default"));
102 m_ui->startTorrentComboBox->addItem(tr("Yes"), true);
103 m_ui->startTorrentComboBox->addItem(tr("No"), false);
105 m_ui->addToQueueTopComboBox->addItem(tr("Default"));
106 m_ui->addToQueueTopComboBox->addItem(tr("Yes"), true);
107 m_ui->addToQueueTopComboBox->addItem(tr("No"), false);
109 connect(m_ui->tagsEditButton, &QAbstractButton::clicked, this, [this]
111 auto *dlg = new TorrentTagsDialog(m_addTorrentParams.tags, this);
112 dlg->setAttribute(Qt::WA_DeleteOnClose);
113 connect(dlg, &TorrentTagsDialog::accepted, this, [this, dlg]
115 m_addTorrentParams.tags = dlg->tags();
116 m_ui->tagsLineEdit->setText(Utils::String::joinIntoString(m_addTorrentParams.tags, u", "_s));
118 dlg->open();
121 auto *miscParamsLayout = new FlowLayout(m_ui->miscParamsWidget);
122 miscParamsLayout->setContentsMargins(0, 0, 0, 0);
123 miscParamsLayout->addWidget(m_ui->contentLayoutWidget);
124 miscParamsLayout->addWidget(m_ui->skipCheckingCheckBox);
125 miscParamsLayout->setAlignment(m_ui->skipCheckingCheckBox, Qt::AlignVCenter);
126 miscParamsLayout->addWidget(m_ui->startTorrentWidget);
127 miscParamsLayout->addWidget(m_ui->stopConditionWidget);
128 miscParamsLayout->addWidget(m_ui->addToQueueTopWidget);
130 setAddTorrentParams(std::move(addTorrentParams));
133 AddTorrentParamsWidget::~AddTorrentParamsWidget()
135 delete m_ui;
138 void AddTorrentParamsWidget::setAddTorrentParams(BitTorrent::AddTorrentParams addTorrentParams)
140 m_addTorrentParams = std::move(addTorrentParams);
141 populate();
144 BitTorrent::AddTorrentParams AddTorrentParamsWidget::addTorrentParams() const
146 return cleanParams(m_addTorrentParams);
149 void AddTorrentParamsWidget::populate()
151 m_ui->comboTTM->disconnect(this);
152 m_ui->comboTTM->setCurrentIndex(m_addTorrentParams.useAutoTMM
153 ? m_ui->comboTTM->findData(*m_addTorrentParams.useAutoTMM) : 0);
154 connect(m_ui->comboTTM, &QComboBox::currentIndexChanged, this, [this]
156 m_addTorrentParams.useAutoTMM = toOptionalBool(m_ui->comboTTM->currentData());
158 populateSavePathOptions();
161 m_ui->categoryComboBox->disconnect(this);
162 m_ui->categoryComboBox->clear();
163 QStringList categories = BitTorrent::Session::instance()->categories();
164 std::sort(categories.begin(), categories.end(), Utils::Compare::NaturalLessThan<Qt::CaseInsensitive>());
165 if (!m_addTorrentParams.category.isEmpty())
166 m_ui->categoryComboBox->addItem(m_addTorrentParams.category);
167 m_ui->categoryComboBox->addItem(u""_s);
168 for (const QString &category : asConst(categories))
170 if (category != m_addTorrentParams.category)
171 m_ui->categoryComboBox->addItem(category);
173 connect(m_ui->categoryComboBox, &QComboBox::currentIndexChanged, this, [this]
175 m_addTorrentParams.category = m_ui->categoryComboBox->currentText();
177 const auto *btSession = BitTorrent::Session::instance();
178 const bool useAutoTMM = m_addTorrentParams.useAutoTMM.value_or(!btSession->isAutoTMMDisabledByDefault());
179 if (useAutoTMM)
181 const auto downloadPathOption = btSession->categoryOptions(m_addTorrentParams.category).downloadPath;
182 m_ui->useDownloadPathComboBox->setCurrentIndex(downloadPathOption.has_value()
183 ? m_ui->useDownloadPathComboBox->findData(downloadPathOption->enabled) : 0);
186 populateDefaultPaths();
189 m_ui->savePathEdit->disconnect(this);
190 m_ui->downloadPathEdit->disconnect(this);
191 m_ui->useDownloadPathComboBox->disconnect(this);
193 populateSavePathOptions();
195 connect(m_ui->savePathEdit, &FileSystemPathLineEdit::selectedPathChanged, this, [this]
197 m_addTorrentParams.savePath = m_ui->savePathEdit->selectedPath();
199 connect(m_ui->downloadPathEdit, &FileSystemPathLineEdit::selectedPathChanged, this, [this]
201 m_addTorrentParams.downloadPath = m_ui->downloadPathEdit->selectedPath();
203 connect(m_ui->useDownloadPathComboBox, &QComboBox::currentIndexChanged, this, [this]
205 m_addTorrentParams.useDownloadPath = toOptionalBool(m_ui->useDownloadPathComboBox->currentData());
207 loadCustomDownloadPath();
210 m_ui->contentLayoutComboBox->disconnect(this);
211 m_ui->contentLayoutComboBox->setCurrentIndex(m_addTorrentParams.contentLayout
212 ? m_ui->contentLayoutComboBox->findData(QVariant::fromValue(*m_addTorrentParams.contentLayout)) : 0);
213 connect(m_ui->contentLayoutComboBox, &QComboBox::currentIndexChanged, this, [this]
215 const QVariant data = m_ui->contentLayoutComboBox->currentData();
216 if (!data.isValid())
217 m_addTorrentParams.contentLayout = std::nullopt;
218 else
219 m_addTorrentParams.contentLayout = data.value<BitTorrent::TorrentContentLayout>();
222 m_ui->stopConditionComboBox->disconnect(this);
223 m_ui->stopConditionComboBox->setCurrentIndex(m_addTorrentParams.stopCondition
224 ? m_ui->stopConditionComboBox->findData(QVariant::fromValue(*m_addTorrentParams.stopCondition)) : 0);
225 connect(m_ui->stopConditionComboBox, &QComboBox::currentIndexChanged, this, [this]
227 const QVariant data = m_ui->stopConditionComboBox->currentData();
228 if (!data.isValid())
229 m_addTorrentParams.stopCondition = std::nullopt;
230 else
231 m_addTorrentParams.stopCondition = data.value<BitTorrent::Torrent::StopCondition>();
234 m_ui->tagsLineEdit->setText(Utils::String::joinIntoString(m_addTorrentParams.tags, u", "_s));
236 m_ui->startTorrentComboBox->disconnect(this);
237 m_ui->startTorrentComboBox->setCurrentIndex(m_addTorrentParams.addPaused
238 ? m_ui->startTorrentComboBox->findData(!*m_addTorrentParams.addPaused) : 0);
239 connect(m_ui->startTorrentComboBox, &QComboBox::currentIndexChanged, this, [this]
241 const QVariant data = m_ui->startTorrentComboBox->currentData();
242 if (!data.isValid())
243 m_addTorrentParams.addPaused = std::nullopt;
244 else
245 m_addTorrentParams.addPaused = !data.toBool();
248 m_ui->skipCheckingCheckBox->disconnect(this);
249 m_ui->skipCheckingCheckBox->setChecked(m_addTorrentParams.skipChecking);
250 connect(m_ui->skipCheckingCheckBox, &QCheckBox::toggled, this, [this]
252 m_addTorrentParams.skipChecking = m_ui->skipCheckingCheckBox->isChecked();
255 m_ui->addToQueueTopComboBox->disconnect(this);
256 m_ui->addToQueueTopComboBox->setCurrentIndex(m_addTorrentParams.addToQueueTop
257 ? m_ui->addToQueueTopComboBox->findData(*m_addTorrentParams.addToQueueTop) : 0);
258 connect(m_ui->addToQueueTopComboBox, &QComboBox::currentIndexChanged, this, [this]
260 const QVariant data = m_ui->addToQueueTopComboBox->currentData();
261 if (!data.isValid())
262 m_addTorrentParams.addToQueueTop = std::nullopt;
263 else
264 m_addTorrentParams.addToQueueTop = data.toBool();
268 void AddTorrentParamsWidget::loadCustomSavePathOptions()
270 [[maybe_unused]] const auto *btSession = BitTorrent::Session::instance();
271 Q_ASSERT(!m_addTorrentParams.useAutoTMM.value_or(!btSession->isAutoTMMDisabledByDefault()));
273 m_ui->savePathEdit->setSelectedPath(m_addTorrentParams.savePath);
275 m_ui->useDownloadPathComboBox->setCurrentIndex(m_addTorrentParams.useDownloadPath
276 ? m_ui->useDownloadPathComboBox->findData(*m_addTorrentParams.useDownloadPath) : 0);
278 loadCustomDownloadPath();
281 void AddTorrentParamsWidget::loadCustomDownloadPath()
283 populateDefaultDownloadPath();
285 if (!m_addTorrentParams.useDownloadPath.has_value())
287 // Default "Download path" settings
289 m_ui->downloadPathEdit->setEnabled(false);
290 m_ui->downloadPathEdit->blockSignals(true);
291 m_ui->downloadPathEdit->setSelectedPath(Path());
293 else
295 // Overridden "Download path" settings
297 const bool useDownloadPath = m_addTorrentParams.useDownloadPath.value();
298 if (useDownloadPath)
300 m_ui->downloadPathEdit->setSelectedPath(m_addTorrentParams.downloadPath);
302 m_ui->downloadPathEdit->blockSignals(false);
303 m_ui->downloadPathEdit->setEnabled(true);
305 else
307 m_ui->downloadPathEdit->setEnabled(false);
308 m_ui->downloadPathEdit->blockSignals(true);
310 m_ui->downloadPathEdit->setSelectedPath(Path());
315 void AddTorrentParamsWidget::loadCategorySavePathOptions()
317 const auto *btSession = BitTorrent::Session::instance();
318 Q_ASSERT(m_addTorrentParams.useAutoTMM.value_or(!btSession->isAutoTMMDisabledByDefault()));
320 const auto downloadPathOption = btSession->categoryOptions(m_addTorrentParams.category).downloadPath;
321 m_ui->useDownloadPathComboBox->setCurrentIndex(downloadPathOption.has_value()
322 ? m_ui->useDownloadPathComboBox->findData(downloadPathOption->enabled) : 0);
325 void AddTorrentParamsWidget::populateDefaultPaths()
327 const auto *btSession = BitTorrent::Session::instance();
329 const Path defaultSavePath = btSession->suggestedSavePath(
330 m_ui->categoryComboBox->currentText(), toOptionalBool(m_ui->comboTTM->currentData()));
331 m_ui->savePathEdit->setPlaceholder(defaultSavePath);
333 populateDefaultDownloadPath();
336 void AddTorrentParamsWidget::populateDefaultDownloadPath()
338 const auto *btSession = BitTorrent::Session::instance();
340 const std::optional<bool> useDownloadPath = toOptionalBool(m_ui->useDownloadPathComboBox->currentData());
341 if (useDownloadPath.value_or(btSession->isDownloadPathEnabled()))
343 const Path defaultDownloadPath = btSession->suggestedDownloadPath(
344 m_ui->categoryComboBox->currentText(), toOptionalBool(m_ui->comboTTM->currentData()));
345 m_ui->downloadPathEdit->setPlaceholder(defaultDownloadPath);
347 else
349 m_ui->downloadPathEdit->setPlaceholder(Path());
353 void AddTorrentParamsWidget::populateSavePathOptions()
355 if (!m_addTorrentParams.useAutoTMM.has_value())
357 // Default TMM settings
359 m_ui->groupBoxSavePath->setEnabled(false);
360 m_ui->defaultsNoteLabel->setVisible(true);
361 m_ui->savePathEdit->blockSignals(true);
362 m_ui->savePathEdit->setSelectedPath(Path());
363 m_ui->downloadPathEdit->blockSignals(true);
364 m_ui->useDownloadPathComboBox->blockSignals(true);
365 m_ui->downloadPathEdit->setSelectedPath(Path());
367 const auto *btSession = BitTorrent::Session::instance();
368 const bool useAutoTMM = !btSession->isAutoTMMDisabledByDefault();
370 if (useAutoTMM)
372 loadCategorySavePathOptions();
374 else
376 m_ui->useDownloadPathComboBox->setCurrentIndex(
377 m_ui->useDownloadPathComboBox->findData(btSession->isDownloadPathEnabled()));
380 else
382 // Overridden TMM settings
384 const bool useAutoTMM = m_addTorrentParams.useAutoTMM.value();
385 if (useAutoTMM)
387 m_ui->groupBoxSavePath->setEnabled(false);
388 m_ui->defaultsNoteLabel->setVisible(true);
389 m_ui->savePathEdit->blockSignals(true);
390 m_ui->savePathEdit->setSelectedPath(Path());
391 m_ui->downloadPathEdit->blockSignals(true);
392 m_ui->useDownloadPathComboBox->blockSignals(true);
393 m_ui->downloadPathEdit->setSelectedPath(Path());
395 loadCategorySavePathOptions();
397 else
399 loadCustomSavePathOptions();
401 m_ui->groupBoxSavePath->setEnabled(true);
402 m_ui->defaultsNoteLabel->setVisible(false);
403 m_ui->savePathEdit->blockSignals(false);
404 m_ui->useDownloadPathComboBox->blockSignals(false);
408 populateDefaultPaths();