Enable customizing the save statistics time interval
[qBittorrent.git] / src / gui / addtorrentparamswidget.cpp
blobdd5203dd2f1a5b92b5ceeee5bd242245dddac0fd
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().value();
148 addTorrentParams.seedingTimeLimit = m_ui->torrentShareLimitsWidget->seedingTimeLimit().value();
149 addTorrentParams.inactiveSeedingTimeLimit = m_ui->torrentShareLimitsWidget->inactiveSeedingTimeLimit().value();
150 addTorrentParams.shareLimitAction = m_ui->torrentShareLimitsWidget->shareLimitAction().value();
152 return addTorrentParams;
155 void AddTorrentParamsWidget::populate()
157 m_ui->comboTTM->disconnect(this);
158 m_ui->comboTTM->setCurrentIndex(m_addTorrentParams.useAutoTMM
159 ? m_ui->comboTTM->findData(*m_addTorrentParams.useAutoTMM) : 0);
160 connect(m_ui->comboTTM, &QComboBox::currentIndexChanged, this, [this]
162 m_addTorrentParams.useAutoTMM = toOptionalBool(m_ui->comboTTM->currentData());
164 populateSavePathOptions();
167 m_ui->categoryComboBox->disconnect(this);
168 m_ui->categoryComboBox->clear();
169 QStringList categories = BitTorrent::Session::instance()->categories();
170 std::sort(categories.begin(), categories.end(), Utils::Compare::NaturalLessThan<Qt::CaseInsensitive>());
171 if (!m_addTorrentParams.category.isEmpty())
172 m_ui->categoryComboBox->addItem(m_addTorrentParams.category);
173 m_ui->categoryComboBox->addItem(u""_s);
174 for (const QString &category : asConst(categories))
176 if (category != m_addTorrentParams.category)
177 m_ui->categoryComboBox->addItem(category);
179 connect(m_ui->categoryComboBox, &QComboBox::currentIndexChanged, this, [this]
181 m_addTorrentParams.category = m_ui->categoryComboBox->currentText();
183 const auto *btSession = BitTorrent::Session::instance();
184 const bool useAutoTMM = m_addTorrentParams.useAutoTMM.value_or(!btSession->isAutoTMMDisabledByDefault());
185 if (useAutoTMM)
187 const auto downloadPathOption = btSession->categoryOptions(m_addTorrentParams.category).downloadPath;
188 m_ui->useDownloadPathComboBox->setCurrentIndex(downloadPathOption.has_value()
189 ? m_ui->useDownloadPathComboBox->findData(downloadPathOption->enabled) : 0);
192 populateDefaultPaths();
195 m_ui->savePathEdit->disconnect(this);
196 m_ui->downloadPathEdit->disconnect(this);
197 m_ui->useDownloadPathComboBox->disconnect(this);
199 populateSavePathOptions();
201 connect(m_ui->savePathEdit, &FileSystemPathLineEdit::selectedPathChanged, this, [this]
203 m_addTorrentParams.savePath = m_ui->savePathEdit->selectedPath();
205 connect(m_ui->downloadPathEdit, &FileSystemPathLineEdit::selectedPathChanged, this, [this]
207 m_addTorrentParams.downloadPath = m_ui->downloadPathEdit->selectedPath();
209 connect(m_ui->useDownloadPathComboBox, &QComboBox::currentIndexChanged, this, [this]
211 m_addTorrentParams.useDownloadPath = toOptionalBool(m_ui->useDownloadPathComboBox->currentData());
213 loadCustomDownloadPath();
216 m_ui->contentLayoutComboBox->disconnect(this);
217 m_ui->contentLayoutComboBox->setCurrentIndex(m_addTorrentParams.contentLayout
218 ? m_ui->contentLayoutComboBox->findData(QVariant::fromValue(*m_addTorrentParams.contentLayout)) : 0);
219 connect(m_ui->contentLayoutComboBox, &QComboBox::currentIndexChanged, this, [this]
221 const QVariant data = m_ui->contentLayoutComboBox->currentData();
222 if (!data.isValid())
223 m_addTorrentParams.contentLayout = std::nullopt;
224 else
225 m_addTorrentParams.contentLayout = data.value<BitTorrent::TorrentContentLayout>();
228 m_ui->stopConditionComboBox->disconnect(this);
229 m_ui->stopConditionComboBox->setCurrentIndex(m_addTorrentParams.stopCondition
230 ? m_ui->stopConditionComboBox->findData(QVariant::fromValue(*m_addTorrentParams.stopCondition)) : 0);
231 connect(m_ui->stopConditionComboBox, &QComboBox::currentIndexChanged, this, [this]
233 const QVariant data = m_ui->stopConditionComboBox->currentData();
234 if (!data.isValid())
235 m_addTorrentParams.stopCondition = std::nullopt;
236 else
237 m_addTorrentParams.stopCondition = data.value<BitTorrent::Torrent::StopCondition>();
240 m_ui->tagsLineEdit->setText(Utils::String::joinIntoString(m_addTorrentParams.tags, u", "_s));
242 m_ui->startTorrentComboBox->disconnect(this);
243 m_ui->startTorrentComboBox->setCurrentIndex(m_addTorrentParams.addStopped
244 ? m_ui->startTorrentComboBox->findData(!*m_addTorrentParams.addStopped) : 0);
245 connect(m_ui->startTorrentComboBox, &QComboBox::currentIndexChanged, this, [this]
247 const QVariant data = m_ui->startTorrentComboBox->currentData();
248 if (!data.isValid())
249 m_addTorrentParams.addStopped = std::nullopt;
250 else
251 m_addTorrentParams.addStopped = !data.toBool();
254 m_ui->skipCheckingCheckBox->disconnect(this);
255 m_ui->skipCheckingCheckBox->setChecked(m_addTorrentParams.skipChecking);
256 connect(m_ui->skipCheckingCheckBox, &QCheckBox::toggled, this, [this]
258 m_addTorrentParams.skipChecking = m_ui->skipCheckingCheckBox->isChecked();
261 m_ui->addToQueueTopComboBox->disconnect(this);
262 m_ui->addToQueueTopComboBox->setCurrentIndex(m_addTorrentParams.addToQueueTop
263 ? m_ui->addToQueueTopComboBox->findData(*m_addTorrentParams.addToQueueTop) : 0);
264 connect(m_ui->addToQueueTopComboBox, &QComboBox::currentIndexChanged, this, [this]
266 const QVariant data = m_ui->addToQueueTopComboBox->currentData();
267 if (!data.isValid())
268 m_addTorrentParams.addToQueueTop = std::nullopt;
269 else
270 m_addTorrentParams.addToQueueTop = data.toBool();
273 m_ui->torrentShareLimitsWidget->setRatioLimit(m_addTorrentParams.ratioLimit);
274 m_ui->torrentShareLimitsWidget->setSeedingTimeLimit(m_addTorrentParams.seedingTimeLimit);
275 m_ui->torrentShareLimitsWidget->setInactiveSeedingTimeLimit(m_addTorrentParams.inactiveSeedingTimeLimit);
276 m_ui->torrentShareLimitsWidget->setShareLimitAction(m_addTorrentParams.shareLimitAction);
279 void AddTorrentParamsWidget::loadCustomSavePathOptions()
281 [[maybe_unused]] const auto *btSession = BitTorrent::Session::instance();
282 Q_ASSERT(!m_addTorrentParams.useAutoTMM.value_or(!btSession->isAutoTMMDisabledByDefault()));
284 m_ui->savePathEdit->setSelectedPath(m_addTorrentParams.savePath);
286 m_ui->useDownloadPathComboBox->setCurrentIndex(m_addTorrentParams.useDownloadPath
287 ? m_ui->useDownloadPathComboBox->findData(*m_addTorrentParams.useDownloadPath) : 0);
289 loadCustomDownloadPath();
292 void AddTorrentParamsWidget::loadCustomDownloadPath()
294 populateDefaultDownloadPath();
296 if (!m_addTorrentParams.useDownloadPath.has_value())
298 // Default "Download path" settings
300 m_ui->downloadPathEdit->setEnabled(false);
301 m_ui->downloadPathEdit->blockSignals(true);
302 m_ui->downloadPathEdit->setSelectedPath(Path());
304 else
306 // Overridden "Download path" settings
308 const bool useDownloadPath = m_addTorrentParams.useDownloadPath.value();
309 if (useDownloadPath)
311 m_ui->downloadPathEdit->setSelectedPath(m_addTorrentParams.downloadPath);
313 m_ui->downloadPathEdit->blockSignals(false);
314 m_ui->downloadPathEdit->setEnabled(true);
316 else
318 m_ui->downloadPathEdit->setEnabled(false);
319 m_ui->downloadPathEdit->blockSignals(true);
321 m_ui->downloadPathEdit->setSelectedPath(Path());
326 void AddTorrentParamsWidget::loadCategorySavePathOptions()
328 const auto *btSession = BitTorrent::Session::instance();
329 Q_ASSERT(m_addTorrentParams.useAutoTMM.value_or(!btSession->isAutoTMMDisabledByDefault()));
331 const auto downloadPathOption = btSession->categoryOptions(m_addTorrentParams.category).downloadPath;
332 m_ui->useDownloadPathComboBox->setCurrentIndex(downloadPathOption.has_value()
333 ? m_ui->useDownloadPathComboBox->findData(downloadPathOption->enabled) : 0);
336 void AddTorrentParamsWidget::populateDefaultPaths()
338 const auto *btSession = BitTorrent::Session::instance();
340 const Path defaultSavePath = btSession->suggestedSavePath(
341 m_ui->categoryComboBox->currentText(), toOptionalBool(m_ui->comboTTM->currentData()));
342 m_ui->savePathEdit->setPlaceholder(defaultSavePath);
344 populateDefaultDownloadPath();
347 void AddTorrentParamsWidget::populateDefaultDownloadPath()
349 const auto *btSession = BitTorrent::Session::instance();
351 const std::optional<bool> useDownloadPath = toOptionalBool(m_ui->useDownloadPathComboBox->currentData());
352 if (useDownloadPath.value_or(btSession->isDownloadPathEnabled()))
354 const Path defaultDownloadPath = btSession->suggestedDownloadPath(
355 m_ui->categoryComboBox->currentText(), toOptionalBool(m_ui->comboTTM->currentData()));
356 m_ui->downloadPathEdit->setPlaceholder(defaultDownloadPath);
358 else
360 m_ui->downloadPathEdit->setPlaceholder(Path());
364 void AddTorrentParamsWidget::populateSavePathOptions()
366 if (!m_addTorrentParams.useAutoTMM.has_value())
368 // Default TMM settings
370 m_ui->groupBoxSavePath->setEnabled(false);
371 m_ui->defaultsNoteLabel->setVisible(true);
372 m_ui->savePathEdit->blockSignals(true);
373 m_ui->savePathEdit->setSelectedPath(Path());
374 m_ui->downloadPathEdit->blockSignals(true);
375 m_ui->useDownloadPathComboBox->blockSignals(true);
376 m_ui->downloadPathEdit->setSelectedPath(Path());
378 const auto *btSession = BitTorrent::Session::instance();
379 const bool useAutoTMM = !btSession->isAutoTMMDisabledByDefault();
381 if (useAutoTMM)
383 loadCategorySavePathOptions();
385 else
387 m_ui->useDownloadPathComboBox->setCurrentIndex(
388 m_ui->useDownloadPathComboBox->findData(btSession->isDownloadPathEnabled()));
391 else
393 // Overridden TMM settings
395 const bool useAutoTMM = m_addTorrentParams.useAutoTMM.value();
396 if (useAutoTMM)
398 m_ui->groupBoxSavePath->setEnabled(false);
399 m_ui->defaultsNoteLabel->setVisible(true);
400 m_ui->savePathEdit->blockSignals(true);
401 m_ui->savePathEdit->setSelectedPath(Path());
402 m_ui->downloadPathEdit->blockSignals(true);
403 m_ui->useDownloadPathComboBox->blockSignals(true);
404 m_ui->downloadPathEdit->setSelectedPath(Path());
406 loadCategorySavePathOptions();
408 else
410 loadCustomSavePathOptions();
412 m_ui->groupBoxSavePath->setEnabled(true);
413 m_ui->defaultsNoteLabel->setVisible(false);
414 m_ui->savePathEdit->blockSignals(false);
415 m_ui->useDownloadPathComboBox->blockSignals(false);
419 populateDefaultPaths();