Provide torrent creation feature via WebAPI
[qBittorrent.git] / src / gui / addtorrentparamswidget.cpp
blobc9cced609317a624558db2e2c3d9dbea5e17eb05
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 BitTorrent::AddTorrentParams addTorrentParams = cleanParams(m_addTorrentParams);
147 addTorrentParams.ratioLimit = m_ui->torrentShareLimitsWidget->ratioLimit();
148 addTorrentParams.seedingTimeLimit = m_ui->torrentShareLimitsWidget->seedingTimeLimit();
149 addTorrentParams.inactiveSeedingTimeLimit = m_ui->torrentShareLimitsWidget->inactiveSeedingTimeLimit();
151 return addTorrentParams;
154 void AddTorrentParamsWidget::populate()
156 m_ui->comboTTM->disconnect(this);
157 m_ui->comboTTM->setCurrentIndex(m_addTorrentParams.useAutoTMM
158 ? m_ui->comboTTM->findData(*m_addTorrentParams.useAutoTMM) : 0);
159 connect(m_ui->comboTTM, &QComboBox::currentIndexChanged, this, [this]
161 m_addTorrentParams.useAutoTMM = toOptionalBool(m_ui->comboTTM->currentData());
163 populateSavePathOptions();
166 m_ui->categoryComboBox->disconnect(this);
167 m_ui->categoryComboBox->clear();
168 QStringList categories = BitTorrent::Session::instance()->categories();
169 std::sort(categories.begin(), categories.end(), Utils::Compare::NaturalLessThan<Qt::CaseInsensitive>());
170 if (!m_addTorrentParams.category.isEmpty())
171 m_ui->categoryComboBox->addItem(m_addTorrentParams.category);
172 m_ui->categoryComboBox->addItem(u""_s);
173 for (const QString &category : asConst(categories))
175 if (category != m_addTorrentParams.category)
176 m_ui->categoryComboBox->addItem(category);
178 connect(m_ui->categoryComboBox, &QComboBox::currentIndexChanged, this, [this]
180 m_addTorrentParams.category = m_ui->categoryComboBox->currentText();
182 const auto *btSession = BitTorrent::Session::instance();
183 const bool useAutoTMM = m_addTorrentParams.useAutoTMM.value_or(!btSession->isAutoTMMDisabledByDefault());
184 if (useAutoTMM)
186 const auto downloadPathOption = btSession->categoryOptions(m_addTorrentParams.category).downloadPath;
187 m_ui->useDownloadPathComboBox->setCurrentIndex(downloadPathOption.has_value()
188 ? m_ui->useDownloadPathComboBox->findData(downloadPathOption->enabled) : 0);
191 populateDefaultPaths();
194 m_ui->savePathEdit->disconnect(this);
195 m_ui->downloadPathEdit->disconnect(this);
196 m_ui->useDownloadPathComboBox->disconnect(this);
198 populateSavePathOptions();
200 connect(m_ui->savePathEdit, &FileSystemPathLineEdit::selectedPathChanged, this, [this]
202 m_addTorrentParams.savePath = m_ui->savePathEdit->selectedPath();
204 connect(m_ui->downloadPathEdit, &FileSystemPathLineEdit::selectedPathChanged, this, [this]
206 m_addTorrentParams.downloadPath = m_ui->downloadPathEdit->selectedPath();
208 connect(m_ui->useDownloadPathComboBox, &QComboBox::currentIndexChanged, this, [this]
210 m_addTorrentParams.useDownloadPath = toOptionalBool(m_ui->useDownloadPathComboBox->currentData());
212 loadCustomDownloadPath();
215 m_ui->contentLayoutComboBox->disconnect(this);
216 m_ui->contentLayoutComboBox->setCurrentIndex(m_addTorrentParams.contentLayout
217 ? m_ui->contentLayoutComboBox->findData(QVariant::fromValue(*m_addTorrentParams.contentLayout)) : 0);
218 connect(m_ui->contentLayoutComboBox, &QComboBox::currentIndexChanged, this, [this]
220 const QVariant data = m_ui->contentLayoutComboBox->currentData();
221 if (!data.isValid())
222 m_addTorrentParams.contentLayout = std::nullopt;
223 else
224 m_addTorrentParams.contentLayout = data.value<BitTorrent::TorrentContentLayout>();
227 m_ui->stopConditionComboBox->disconnect(this);
228 m_ui->stopConditionComboBox->setCurrentIndex(m_addTorrentParams.stopCondition
229 ? m_ui->stopConditionComboBox->findData(QVariant::fromValue(*m_addTorrentParams.stopCondition)) : 0);
230 connect(m_ui->stopConditionComboBox, &QComboBox::currentIndexChanged, this, [this]
232 const QVariant data = m_ui->stopConditionComboBox->currentData();
233 if (!data.isValid())
234 m_addTorrentParams.stopCondition = std::nullopt;
235 else
236 m_addTorrentParams.stopCondition = data.value<BitTorrent::Torrent::StopCondition>();
239 m_ui->tagsLineEdit->setText(Utils::String::joinIntoString(m_addTorrentParams.tags, u", "_s));
241 m_ui->startTorrentComboBox->disconnect(this);
242 m_ui->startTorrentComboBox->setCurrentIndex(m_addTorrentParams.addPaused
243 ? m_ui->startTorrentComboBox->findData(!*m_addTorrentParams.addPaused) : 0);
244 connect(m_ui->startTorrentComboBox, &QComboBox::currentIndexChanged, this, [this]
246 const QVariant data = m_ui->startTorrentComboBox->currentData();
247 if (!data.isValid())
248 m_addTorrentParams.addPaused = std::nullopt;
249 else
250 m_addTorrentParams.addPaused = !data.toBool();
253 m_ui->skipCheckingCheckBox->disconnect(this);
254 m_ui->skipCheckingCheckBox->setChecked(m_addTorrentParams.skipChecking);
255 connect(m_ui->skipCheckingCheckBox, &QCheckBox::toggled, this, [this]
257 m_addTorrentParams.skipChecking = m_ui->skipCheckingCheckBox->isChecked();
260 m_ui->addToQueueTopComboBox->disconnect(this);
261 m_ui->addToQueueTopComboBox->setCurrentIndex(m_addTorrentParams.addToQueueTop
262 ? m_ui->addToQueueTopComboBox->findData(*m_addTorrentParams.addToQueueTop) : 0);
263 connect(m_ui->addToQueueTopComboBox, &QComboBox::currentIndexChanged, this, [this]
265 const QVariant data = m_ui->addToQueueTopComboBox->currentData();
266 if (!data.isValid())
267 m_addTorrentParams.addToQueueTop = std::nullopt;
268 else
269 m_addTorrentParams.addToQueueTop = data.toBool();
272 m_ui->torrentShareLimitsWidget->setTorrentShareLimits(m_addTorrentParams.ratioLimit
273 , m_addTorrentParams.seedingTimeLimit, m_addTorrentParams.inactiveSeedingTimeLimit);
276 void AddTorrentParamsWidget::loadCustomSavePathOptions()
278 [[maybe_unused]] const auto *btSession = BitTorrent::Session::instance();
279 Q_ASSERT(!m_addTorrentParams.useAutoTMM.value_or(!btSession->isAutoTMMDisabledByDefault()));
281 m_ui->savePathEdit->setSelectedPath(m_addTorrentParams.savePath);
283 m_ui->useDownloadPathComboBox->setCurrentIndex(m_addTorrentParams.useDownloadPath
284 ? m_ui->useDownloadPathComboBox->findData(*m_addTorrentParams.useDownloadPath) : 0);
286 loadCustomDownloadPath();
289 void AddTorrentParamsWidget::loadCustomDownloadPath()
291 populateDefaultDownloadPath();
293 if (!m_addTorrentParams.useDownloadPath.has_value())
295 // Default "Download path" settings
297 m_ui->downloadPathEdit->setEnabled(false);
298 m_ui->downloadPathEdit->blockSignals(true);
299 m_ui->downloadPathEdit->setSelectedPath(Path());
301 else
303 // Overridden "Download path" settings
305 const bool useDownloadPath = m_addTorrentParams.useDownloadPath.value();
306 if (useDownloadPath)
308 m_ui->downloadPathEdit->setSelectedPath(m_addTorrentParams.downloadPath);
310 m_ui->downloadPathEdit->blockSignals(false);
311 m_ui->downloadPathEdit->setEnabled(true);
313 else
315 m_ui->downloadPathEdit->setEnabled(false);
316 m_ui->downloadPathEdit->blockSignals(true);
318 m_ui->downloadPathEdit->setSelectedPath(Path());
323 void AddTorrentParamsWidget::loadCategorySavePathOptions()
325 const auto *btSession = BitTorrent::Session::instance();
326 Q_ASSERT(m_addTorrentParams.useAutoTMM.value_or(!btSession->isAutoTMMDisabledByDefault()));
328 const auto downloadPathOption = btSession->categoryOptions(m_addTorrentParams.category).downloadPath;
329 m_ui->useDownloadPathComboBox->setCurrentIndex(downloadPathOption.has_value()
330 ? m_ui->useDownloadPathComboBox->findData(downloadPathOption->enabled) : 0);
333 void AddTorrentParamsWidget::populateDefaultPaths()
335 const auto *btSession = BitTorrent::Session::instance();
337 const Path defaultSavePath = btSession->suggestedSavePath(
338 m_ui->categoryComboBox->currentText(), toOptionalBool(m_ui->comboTTM->currentData()));
339 m_ui->savePathEdit->setPlaceholder(defaultSavePath);
341 populateDefaultDownloadPath();
344 void AddTorrentParamsWidget::populateDefaultDownloadPath()
346 const auto *btSession = BitTorrent::Session::instance();
348 const std::optional<bool> useDownloadPath = toOptionalBool(m_ui->useDownloadPathComboBox->currentData());
349 if (useDownloadPath.value_or(btSession->isDownloadPathEnabled()))
351 const Path defaultDownloadPath = btSession->suggestedDownloadPath(
352 m_ui->categoryComboBox->currentText(), toOptionalBool(m_ui->comboTTM->currentData()));
353 m_ui->downloadPathEdit->setPlaceholder(defaultDownloadPath);
355 else
357 m_ui->downloadPathEdit->setPlaceholder(Path());
361 void AddTorrentParamsWidget::populateSavePathOptions()
363 if (!m_addTorrentParams.useAutoTMM.has_value())
365 // Default TMM settings
367 m_ui->groupBoxSavePath->setEnabled(false);
368 m_ui->defaultsNoteLabel->setVisible(true);
369 m_ui->savePathEdit->blockSignals(true);
370 m_ui->savePathEdit->setSelectedPath(Path());
371 m_ui->downloadPathEdit->blockSignals(true);
372 m_ui->useDownloadPathComboBox->blockSignals(true);
373 m_ui->downloadPathEdit->setSelectedPath(Path());
375 const auto *btSession = BitTorrent::Session::instance();
376 const bool useAutoTMM = !btSession->isAutoTMMDisabledByDefault();
378 if (useAutoTMM)
380 loadCategorySavePathOptions();
382 else
384 m_ui->useDownloadPathComboBox->setCurrentIndex(
385 m_ui->useDownloadPathComboBox->findData(btSession->isDownloadPathEnabled()));
388 else
390 // Overridden TMM settings
392 const bool useAutoTMM = m_addTorrentParams.useAutoTMM.value();
393 if (useAutoTMM)
395 m_ui->groupBoxSavePath->setEnabled(false);
396 m_ui->defaultsNoteLabel->setVisible(true);
397 m_ui->savePathEdit->blockSignals(true);
398 m_ui->savePathEdit->setSelectedPath(Path());
399 m_ui->downloadPathEdit->blockSignals(true);
400 m_ui->useDownloadPathComboBox->blockSignals(true);
401 m_ui->downloadPathEdit->setSelectedPath(Path());
403 loadCategorySavePathOptions();
405 else
407 loadCustomSavePathOptions();
409 m_ui->groupBoxSavePath->setEnabled(true);
410 m_ui->defaultsNoteLabel->setVisible(false);
411 m_ui->savePathEdit->blockSignals(false);
412 m_ui->useDownloadPathComboBox->blockSignals(false);
416 populateDefaultPaths();