Bump to 4.6.7
[qBittorrent.git] / src / gui / addtorrentparamswidget.cpp
blobd82fe600b169b863de269159eec7925b9616cd86
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 "flowlayout.h"
37 #include "fspathedit.h"
38 #include "torrenttagsdialog.h"
39 #include "ui_addtorrentparamswidget.h"
41 namespace
43 std::optional<bool> toOptionalBool(const QVariant &data)
45 if (!data.isValid())
46 return std::nullopt;
48 Q_ASSERT(data.userType() == QMetaType::Bool);
49 return data.toBool();
52 BitTorrent::AddTorrentParams cleanParams(BitTorrent::AddTorrentParams params)
54 if (!params.useAutoTMM.has_value() || params.useAutoTMM.value())
56 params.savePath = Path();
57 params.downloadPath = Path();
58 params.useDownloadPath = std::nullopt;
61 if (!params.useDownloadPath.has_value() || !params.useDownloadPath.value())
63 params.downloadPath = Path();
66 return params;
70 AddTorrentParamsWidget::AddTorrentParamsWidget(BitTorrent::AddTorrentParams addTorrentParams, QWidget *parent)
71 : QWidget(parent)
72 , m_ui {new Ui::AddTorrentParamsWidget}
74 m_ui->setupUi(this);
76 m_ui->savePathEdit->setMode(FileSystemPathEdit::Mode::DirectorySave);
77 m_ui->savePathEdit->setDialogCaption(tr("Choose save path"));
79 m_ui->downloadPathEdit->setMode(FileSystemPathEdit::Mode::DirectorySave);
80 m_ui->downloadPathEdit->setDialogCaption(tr("Choose save path"));
82 m_ui->useDownloadPathComboBox->addItem(tr("Default"));
83 m_ui->useDownloadPathComboBox->addItem(tr("Yes"), true);
84 m_ui->useDownloadPathComboBox->addItem(tr("No"), false);
86 m_ui->comboTTM->addItem(tr("Default"));
87 m_ui->comboTTM->addItem(tr("Manual"), false);
88 m_ui->comboTTM->addItem(tr("Automatic"), true);
90 m_ui->contentLayoutComboBox->addItem(tr("Default"));
91 m_ui->contentLayoutComboBox->addItem(tr("Original"), QVariant::fromValue(BitTorrent::TorrentContentLayout::Original));
92 m_ui->contentLayoutComboBox->addItem(tr("Create subfolder"), QVariant::fromValue(BitTorrent::TorrentContentLayout::Subfolder));
93 m_ui->contentLayoutComboBox->addItem(tr("Don't create subfolder"), QVariant::fromValue(BitTorrent::TorrentContentLayout::NoSubfolder));
95 m_ui->stopConditionComboBox->addItem(tr("Default"));
96 m_ui->stopConditionComboBox->addItem(tr("None"), QVariant::fromValue(BitTorrent::Torrent::StopCondition::None));
97 m_ui->stopConditionComboBox->addItem(tr("Metadata received"), QVariant::fromValue(BitTorrent::Torrent::StopCondition::MetadataReceived));
98 m_ui->stopConditionComboBox->addItem(tr("Files checked"), QVariant::fromValue(BitTorrent::Torrent::StopCondition::FilesChecked));
100 m_ui->startTorrentComboBox->addItem(tr("Default"));
101 m_ui->startTorrentComboBox->addItem(tr("Yes"), true);
102 m_ui->startTorrentComboBox->addItem(tr("No"), false);
104 m_ui->addToQueueTopComboBox->addItem(tr("Default"));
105 m_ui->addToQueueTopComboBox->addItem(tr("Yes"), true);
106 m_ui->addToQueueTopComboBox->addItem(tr("No"), false);
108 connect(m_ui->tagsEditButton, &QAbstractButton::clicked, this, [this]
110 auto *dlg = new TorrentTagsDialog(m_addTorrentParams.tags, this);
111 dlg->setAttribute(Qt::WA_DeleteOnClose);
112 connect(dlg, &TorrentTagsDialog::accepted, this, [this, dlg]
114 m_addTorrentParams.tags = dlg->tags();
115 m_ui->tagsLineEdit->setText(m_addTorrentParams.tags.join(u", "_s));
117 dlg->open();
120 auto *miscParamsLayout = new FlowLayout(m_ui->miscParamsWidget);
121 miscParamsLayout->setContentsMargins(0, 0, 0, 0);
122 miscParamsLayout->addWidget(m_ui->contentLayoutWidget);
123 miscParamsLayout->addWidget(m_ui->skipCheckingCheckBox);
124 miscParamsLayout->setAlignment(m_ui->skipCheckingCheckBox, Qt::AlignVCenter);
125 miscParamsLayout->addWidget(m_ui->startTorrentWidget);
126 miscParamsLayout->addWidget(m_ui->stopConditionWidget);
127 miscParamsLayout->addWidget(m_ui->addToQueueTopWidget);
129 setAddTorrentParams(std::move(addTorrentParams));
132 AddTorrentParamsWidget::~AddTorrentParamsWidget()
134 delete m_ui;
137 void AddTorrentParamsWidget::setAddTorrentParams(BitTorrent::AddTorrentParams addTorrentParams)
139 m_addTorrentParams = std::move(addTorrentParams);
140 populate();
143 BitTorrent::AddTorrentParams AddTorrentParamsWidget::addTorrentParams() const
145 return cleanParams(m_addTorrentParams);
148 void AddTorrentParamsWidget::populate()
150 m_ui->comboTTM->disconnect(this);
151 m_ui->comboTTM->setCurrentIndex(m_addTorrentParams.useAutoTMM
152 ? m_ui->comboTTM->findData(*m_addTorrentParams.useAutoTMM) : 0);
153 connect(m_ui->comboTTM, &QComboBox::currentIndexChanged, this, [this]
155 m_addTorrentParams.useAutoTMM = toOptionalBool(m_ui->comboTTM->currentData());
157 populateSavePathOptions();
160 m_ui->categoryComboBox->disconnect(this);
161 m_ui->categoryComboBox->clear();
162 QStringList categories = BitTorrent::Session::instance()->categories();
163 std::sort(categories.begin(), categories.end(), Utils::Compare::NaturalLessThan<Qt::CaseInsensitive>());
164 if (!m_addTorrentParams.category.isEmpty())
165 m_ui->categoryComboBox->addItem(m_addTorrentParams.category);
166 m_ui->categoryComboBox->addItem(u""_s);
167 for (const QString &category : asConst(categories))
169 if (category != m_addTorrentParams.category)
170 m_ui->categoryComboBox->addItem(category);
172 connect(m_ui->categoryComboBox, &QComboBox::currentIndexChanged, this, [this]
174 m_addTorrentParams.category = m_ui->categoryComboBox->currentText();
176 const auto *btSession = BitTorrent::Session::instance();
177 const bool useAutoTMM = m_addTorrentParams.useAutoTMM.value_or(!btSession->isAutoTMMDisabledByDefault());
178 if (useAutoTMM)
180 const auto downloadPathOption = btSession->categoryOptions(m_addTorrentParams.category).downloadPath;
181 m_ui->useDownloadPathComboBox->setCurrentIndex(downloadPathOption.has_value()
182 ? m_ui->useDownloadPathComboBox->findData(downloadPathOption->enabled) : 0);
185 populateDefaultPaths();
188 m_ui->savePathEdit->disconnect(this);
189 m_ui->downloadPathEdit->disconnect(this);
190 m_ui->useDownloadPathComboBox->disconnect(this);
192 populateSavePathOptions();
194 connect(m_ui->savePathEdit, &FileSystemPathLineEdit::selectedPathChanged, this, [this]
196 m_addTorrentParams.savePath = m_ui->savePathEdit->selectedPath();
198 connect(m_ui->downloadPathEdit, &FileSystemPathLineEdit::selectedPathChanged, this, [this]
200 m_addTorrentParams.downloadPath = m_ui->downloadPathEdit->selectedPath();
202 connect(m_ui->useDownloadPathComboBox, &QComboBox::currentIndexChanged, this, [this]
204 m_addTorrentParams.useDownloadPath = toOptionalBool(m_ui->useDownloadPathComboBox->currentData());
206 loadCustomDownloadPath();
209 m_ui->contentLayoutComboBox->disconnect(this);
210 m_ui->contentLayoutComboBox->setCurrentIndex(m_addTorrentParams.contentLayout
211 ? m_ui->contentLayoutComboBox->findData(QVariant::fromValue(*m_addTorrentParams.contentLayout)) : 0);
212 connect(m_ui->contentLayoutComboBox, &QComboBox::currentIndexChanged, this, [this]
214 const QVariant data = m_ui->contentLayoutComboBox->currentData();
215 if (!data.isValid())
216 m_addTorrentParams.contentLayout = std::nullopt;
217 else
218 m_addTorrentParams.contentLayout = data.value<BitTorrent::TorrentContentLayout>();
221 m_ui->stopConditionComboBox->disconnect(this);
222 m_ui->stopConditionComboBox->setCurrentIndex(m_addTorrentParams.stopCondition
223 ? m_ui->stopConditionComboBox->findData(QVariant::fromValue(*m_addTorrentParams.stopCondition)) : 0);
224 connect(m_ui->stopConditionComboBox, &QComboBox::currentIndexChanged, this, [this]
226 const QVariant data = m_ui->stopConditionComboBox->currentData();
227 if (!data.isValid())
228 m_addTorrentParams.stopCondition = std::nullopt;
229 else
230 m_addTorrentParams.stopCondition = data.value<BitTorrent::Torrent::StopCondition>();
233 m_ui->tagsLineEdit->setText(m_addTorrentParams.tags.join(u", "_s));
235 m_ui->startTorrentComboBox->disconnect(this);
236 m_ui->startTorrentComboBox->setCurrentIndex(m_addTorrentParams.addPaused
237 ? m_ui->startTorrentComboBox->findData(!*m_addTorrentParams.addPaused) : 0);
238 connect(m_ui->startTorrentComboBox, &QComboBox::currentIndexChanged, this, [this]
240 const QVariant data = m_ui->startTorrentComboBox->currentData();
241 if (!data.isValid())
242 m_addTorrentParams.addPaused = std::nullopt;
243 else
244 m_addTorrentParams.addPaused = !data.toBool();
247 m_ui->skipCheckingCheckBox->disconnect(this);
248 m_ui->skipCheckingCheckBox->setChecked(m_addTorrentParams.skipChecking);
249 connect(m_ui->skipCheckingCheckBox, &QCheckBox::toggled, this, [this]
251 m_addTorrentParams.skipChecking = m_ui->skipCheckingCheckBox->isChecked();
254 m_ui->addToQueueTopComboBox->disconnect(this);
255 m_ui->addToQueueTopComboBox->setCurrentIndex(m_addTorrentParams.addToQueueTop
256 ? m_ui->addToQueueTopComboBox->findData(*m_addTorrentParams.addToQueueTop) : 0);
257 connect(m_ui->addToQueueTopComboBox, &QComboBox::currentIndexChanged, this, [this]
259 const QVariant data = m_ui->addToQueueTopComboBox->currentData();
260 if (!data.isValid())
261 m_addTorrentParams.addToQueueTop = std::nullopt;
262 else
263 m_addTorrentParams.addToQueueTop = data.toBool();
267 void AddTorrentParamsWidget::loadCustomSavePathOptions()
269 [[maybe_unused]] const auto *btSession = BitTorrent::Session::instance();
270 Q_ASSERT(!m_addTorrentParams.useAutoTMM.value_or(!btSession->isAutoTMMDisabledByDefault()));
272 m_ui->savePathEdit->setSelectedPath(m_addTorrentParams.savePath);
274 m_ui->useDownloadPathComboBox->setCurrentIndex(m_addTorrentParams.useDownloadPath
275 ? m_ui->useDownloadPathComboBox->findData(*m_addTorrentParams.useDownloadPath) : 0);
277 loadCustomDownloadPath();
280 void AddTorrentParamsWidget::loadCustomDownloadPath()
282 populateDefaultDownloadPath();
284 if (!m_addTorrentParams.useDownloadPath.has_value())
286 // Default "Download path" settings
288 m_ui->downloadPathEdit->setEnabled(false);
289 m_ui->downloadPathEdit->blockSignals(true);
290 m_ui->downloadPathEdit->setSelectedPath(Path());
292 else
294 // Overridden "Download path" settings
296 const bool useDownloadPath = m_addTorrentParams.useDownloadPath.value();
297 if (useDownloadPath)
299 m_ui->downloadPathEdit->setSelectedPath(m_addTorrentParams.downloadPath);
301 m_ui->downloadPathEdit->blockSignals(false);
302 m_ui->downloadPathEdit->setEnabled(true);
304 else
306 m_ui->downloadPathEdit->setEnabled(false);
307 m_ui->downloadPathEdit->blockSignals(true);
309 m_ui->downloadPathEdit->setSelectedPath(Path());
314 void AddTorrentParamsWidget::loadCategorySavePathOptions()
316 const auto *btSession = BitTorrent::Session::instance();
317 Q_ASSERT(m_addTorrentParams.useAutoTMM.value_or(!btSession->isAutoTMMDisabledByDefault()));
319 const auto downloadPathOption = btSession->categoryOptions(m_addTorrentParams.category).downloadPath;
320 m_ui->useDownloadPathComboBox->setCurrentIndex(downloadPathOption.has_value()
321 ? m_ui->useDownloadPathComboBox->findData(downloadPathOption->enabled) : 0);
324 void AddTorrentParamsWidget::populateDefaultPaths()
326 const auto *btSession = BitTorrent::Session::instance();
328 const Path defaultSavePath = btSession->suggestedSavePath(
329 m_ui->categoryComboBox->currentText(), toOptionalBool(m_ui->comboTTM->currentData()));
330 m_ui->savePathEdit->setPlaceholder(defaultSavePath);
332 populateDefaultDownloadPath();
335 void AddTorrentParamsWidget::populateDefaultDownloadPath()
337 const auto *btSession = BitTorrent::Session::instance();
339 const std::optional<bool> useDownloadPath = toOptionalBool(m_ui->useDownloadPathComboBox->currentData());
340 if (useDownloadPath.value_or(btSession->isDownloadPathEnabled()))
342 const Path defaultDownloadPath = btSession->suggestedDownloadPath(
343 m_ui->categoryComboBox->currentText(), toOptionalBool(m_ui->comboTTM->currentData()));
344 m_ui->downloadPathEdit->setPlaceholder(defaultDownloadPath);
346 else
348 m_ui->downloadPathEdit->setPlaceholder(Path());
352 void AddTorrentParamsWidget::populateSavePathOptions()
354 if (!m_addTorrentParams.useAutoTMM.has_value())
356 // Default TMM settings
358 m_ui->groupBoxSavePath->setEnabled(false);
359 m_ui->defaultsNoteLabel->setVisible(true);
360 m_ui->savePathEdit->blockSignals(true);
361 m_ui->savePathEdit->setSelectedPath(Path());
362 m_ui->downloadPathEdit->blockSignals(true);
363 m_ui->useDownloadPathComboBox->blockSignals(true);
364 m_ui->downloadPathEdit->setSelectedPath(Path());
366 const auto *btSession = BitTorrent::Session::instance();
367 const bool useAutoTMM = !btSession->isAutoTMMDisabledByDefault();
369 if (useAutoTMM)
371 loadCategorySavePathOptions();
373 else
375 m_ui->useDownloadPathComboBox->setCurrentIndex(
376 m_ui->useDownloadPathComboBox->findData(btSession->isDownloadPathEnabled()));
379 else
381 // Overridden TMM settings
383 const bool useAutoTMM = m_addTorrentParams.useAutoTMM.value();
384 if (useAutoTMM)
386 m_ui->groupBoxSavePath->setEnabled(false);
387 m_ui->defaultsNoteLabel->setVisible(true);
388 m_ui->savePathEdit->blockSignals(true);
389 m_ui->savePathEdit->setSelectedPath(Path());
390 m_ui->downloadPathEdit->blockSignals(true);
391 m_ui->useDownloadPathComboBox->blockSignals(true);
392 m_ui->downloadPathEdit->setSelectedPath(Path());
394 loadCategorySavePathOptions();
396 else
398 loadCustomSavePathOptions();
400 m_ui->groupBoxSavePath->setEnabled(true);
401 m_ui->defaultsNoteLabel->setVisible(false);
402 m_ui->savePathEdit->blockSignals(false);
403 m_ui->useDownloadPathComboBox->blockSignals(false);
407 populateDefaultPaths();