Skip to content

Commit 1702b6c

Browse files
authored
Correctly handle share limits in torrent options dialog
PR #20485.
1 parent f65af03 commit 1702b6c

File tree

6 files changed

+201
-330
lines changed

6 files changed

+201
-330
lines changed

src/gui/addtorrentparamswidget.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ void AddTorrentParamsWidget::setAddTorrentParams(BitTorrent::AddTorrentParams ad
144144
BitTorrent::AddTorrentParams AddTorrentParamsWidget::addTorrentParams() const
145145
{
146146
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();
147+
addTorrentParams.ratioLimit = m_ui->torrentShareLimitsWidget->ratioLimit().value();
148+
addTorrentParams.seedingTimeLimit = m_ui->torrentShareLimitsWidget->seedingTimeLimit().value();
149+
addTorrentParams.inactiveSeedingTimeLimit = m_ui->torrentShareLimitsWidget->inactiveSeedingTimeLimit().value();
150150

151151
return addTorrentParams;
152152
}
@@ -269,8 +269,9 @@ void AddTorrentParamsWidget::populate()
269269
m_addTorrentParams.addToQueueTop = data.toBool();
270270
});
271271

272-
m_ui->torrentShareLimitsWidget->setTorrentShareLimits(m_addTorrentParams.ratioLimit
273-
, m_addTorrentParams.seedingTimeLimit, m_addTorrentParams.inactiveSeedingTimeLimit);
272+
m_ui->torrentShareLimitsWidget->setRatioLimit(m_addTorrentParams.ratioLimit);
273+
m_ui->torrentShareLimitsWidget->setSeedingTimeLimit(m_addTorrentParams.seedingTimeLimit);
274+
m_ui->torrentShareLimitsWidget->setInactiveSeedingTimeLimit(m_addTorrentParams.inactiveSeedingTimeLimit);
274275
}
275276

276277
void AddTorrentParamsWidget::loadCustomSavePathOptions()

src/gui/torrentoptionsdialog.cpp

Lines changed: 38 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Bittorrent Client using Qt and libtorrent.
3+
* Copyright (C) 2024 Vladimir Golovnev <glassez@yandex.ru>
34
* Copyright (C) 2020 thalieht
45
* Copyright (C) 2011 Christian Kandeler
56
* Copyright (C) 2011 Christophe Dumez <chris@qbittorrent.org>
@@ -49,8 +50,6 @@
4950

5051
namespace
5152
{
52-
const int MIXED_SHARE_LIMITS = -9;
53-
5453
void updateSliderValue(QSlider *slider, const int value)
5554
{
5655
if (value > slider->maximum())
@@ -283,47 +282,13 @@ TorrentOptionsDialog::TorrentOptionsDialog(QWidget *parent, const QVector<BitTor
283282
, this, &TorrentOptionsDialog::handleDownSpeedLimitChanged);
284283
}
285284

286-
const bool useGlobalValue = allSameRatio && allSameSeedingTime
287-
&& (firstTorrentRatio == BitTorrent::Torrent::USE_GLOBAL_RATIO)
288-
&& (firstTorrentSeedingTime == BitTorrent::Torrent::USE_GLOBAL_SEEDING_TIME)
289-
&& (firstTorrentInactiveSeedingTime == BitTorrent::Torrent::USE_GLOBAL_INACTIVE_SEEDING_TIME);
290-
291-
if (!allSameRatio || !allSameSeedingTime || !allSameInactiveSeedingTime)
292-
{
293-
m_ui->radioUseGlobalShareLimits->setChecked(false);
294-
m_ui->radioNoLimit->setChecked(false);
295-
m_ui->radioTorrentLimit->setChecked(false);
296-
}
297-
else if (useGlobalValue)
298-
{
299-
m_ui->radioUseGlobalShareLimits->setChecked(true);
300-
}
301-
else if ((firstTorrentRatio == BitTorrent::Torrent::NO_RATIO_LIMIT)
302-
&& (firstTorrentSeedingTime == BitTorrent::Torrent::NO_SEEDING_TIME_LIMIT)
303-
&& (firstTorrentInactiveSeedingTime == BitTorrent::Torrent::NO_INACTIVE_SEEDING_TIME_LIMIT))
304-
{
305-
m_ui->radioNoLimit->setChecked(true);
306-
}
307-
else
308-
{
309-
m_ui->radioTorrentLimit->setChecked(true);
310-
if (firstTorrentRatio >= 0)
311-
m_ui->checkMaxRatio->setChecked(true);
312-
if (firstTorrentSeedingTime >= 0)
313-
m_ui->checkMaxTime->setChecked(true);
314-
if (firstTorrentInactiveSeedingTime >= 0)
315-
m_ui->checkMaxInactiveTime->setChecked(true);
316-
}
317-
318-
const qreal maxRatio = (allSameRatio && (firstTorrentRatio >= 0))
319-
? firstTorrentRatio : session->globalMaxRatio();
320-
const int maxSeedingTime = (allSameSeedingTime && (firstTorrentSeedingTime >= 0))
321-
? firstTorrentSeedingTime : session->globalMaxSeedingMinutes();
322-
const int maxInactiveSeedingTime = (allSameInactiveSeedingTime && (firstTorrentInactiveSeedingTime >= 0))
323-
? firstTorrentInactiveSeedingTime : session->globalMaxInactiveSeedingMinutes();
324-
m_ui->spinRatioLimit->setValue(maxRatio);
325-
m_ui->spinTimeLimit->setValue(maxSeedingTime);
326-
m_ui->spinInactiveTimeLimit->setValue(maxInactiveSeedingTime);
285+
m_ui->torrentShareLimitsWidget->setDefaultLimits(session->globalMaxRatio(), session->globalMaxSeedingMinutes(), session->globalMaxInactiveSeedingMinutes());
286+
if (allSameRatio)
287+
m_ui->torrentShareLimitsWidget->setRatioLimit(firstTorrentRatio);
288+
if (allSameSeedingTime)
289+
m_ui->torrentShareLimitsWidget->setSeedingTimeLimit(firstTorrentSeedingTime);
290+
if (allSameInactiveSeedingTime)
291+
m_ui->torrentShareLimitsWidget->setInactiveSeedingTimeLimit(firstTorrentInactiveSeedingTime);
327292

328293
if (!allTorrentsArePrivate)
329294
{
@@ -369,27 +334,26 @@ TorrentOptionsDialog::TorrentOptionsDialog(QWidget *parent, const QVector<BitTor
369334

370335
m_initialValues =
371336
{
372-
m_ui->savePath->selectedPath(),
373-
m_ui->downloadPath->selectedPath(),
374-
m_ui->comboCategory->currentText(),
375-
getRatio(),
376-
getSeedingTime(),
377-
getInactiveSeedingTime(),
378-
m_ui->spinUploadLimit->value(),
379-
m_ui->spinDownloadLimit->value(),
380-
m_ui->checkAutoTMM->checkState(),
381-
m_ui->checkUseDownloadPath->checkState(),
382-
m_ui->checkDisableDHT->checkState(),
383-
m_ui->checkDisablePEX->checkState(),
384-
m_ui->checkDisableLSD->checkState(),
385-
m_ui->checkSequential->checkState(),
386-
m_ui->checkFirstLastPieces->checkState()
337+
.savePath = m_ui->savePath->selectedPath(),
338+
.downloadPath = m_ui->downloadPath->selectedPath(),
339+
.category = m_ui->comboCategory->currentText(),
340+
.ratio = m_ui->torrentShareLimitsWidget->ratioLimit(),
341+
.seedingTime = m_ui->torrentShareLimitsWidget->seedingTimeLimit(),
342+
.inactiveSeedingTime = m_ui->torrentShareLimitsWidget->inactiveSeedingTimeLimit(),
343+
.upSpeedLimit = m_ui->spinUploadLimit->value(),
344+
.downSpeedLimit = m_ui->spinDownloadLimit->value(),
345+
.autoTMM = m_ui->checkAutoTMM->checkState(),
346+
.useDownloadPath = m_ui->checkUseDownloadPath->checkState(),
347+
.disableDHT = m_ui->checkDisableDHT->checkState(),
348+
.disablePEX = m_ui->checkDisablePEX->checkState(),
349+
.disableLSD = m_ui->checkDisableLSD->checkState(),
350+
.sequential = m_ui->checkSequential->checkState(),
351+
.firstLastPieces = m_ui->checkFirstLastPieces->checkState()
387352
};
388353

389354
// Needs to be called after the initial values struct is initialized
390355
handleTMMChanged();
391356
handleUseDownloadPathChanged();
392-
handleRatioTypeChanged();
393357

394358
connect(m_ui->checkAutoTMM, &QCheckBox::clicked, this, &TorrentOptionsDialog::handleTMMChanged);
395359
connect(m_ui->checkUseDownloadPath, &QCheckBox::clicked, this, &TorrentOptionsDialog::handleUseDownloadPathChanged);
@@ -403,12 +367,6 @@ TorrentOptionsDialog::TorrentOptionsDialog(QWidget *parent, const QVector<BitTor
403367
connect(m_ui->spinDownloadLimit, qOverload<int>(&QSpinBox::valueChanged)
404368
, this, [this](const int value) { updateSliderValue(m_ui->sliderDownloadLimit, value); });
405369

406-
connect(m_ui->checkMaxRatio, &QCheckBox::toggled, m_ui->spinRatioLimit, &QWidget::setEnabled);
407-
connect(m_ui->checkMaxTime, &QCheckBox::toggled, m_ui->spinTimeLimit, &QWidget::setEnabled);
408-
connect(m_ui->checkMaxInactiveTime, &QCheckBox::toggled, m_ui->spinInactiveTimeLimit, &QSpinBox::setEnabled);
409-
410-
connect(m_ui->buttonGroup, &QButtonGroup::idClicked, this, &TorrentOptionsDialog::handleRatioTypeChanged);
411-
412370
if (const QSize dialogSize = m_storeDialogSize; dialogSize.isValid())
413371
resize(dialogSize);
414372
}
@@ -421,13 +379,6 @@ TorrentOptionsDialog::~TorrentOptionsDialog()
421379

422380
void TorrentOptionsDialog::accept()
423381
{
424-
if (m_ui->radioTorrentLimit->isChecked() && !m_ui->checkMaxRatio->isChecked()
425-
&& !m_ui->checkMaxTime->isChecked() && !m_ui->checkMaxInactiveTime->isChecked())
426-
{
427-
QMessageBox::critical(this, tr("No share limit method selected"), tr("Please select a limit method first"));
428-
return;
429-
}
430-
431382
auto *session = BitTorrent::Session::instance();
432383
for (const BitTorrent::TorrentID &id : asConst(m_torrentIDs))
433384
{
@@ -471,17 +422,23 @@ void TorrentOptionsDialog::accept()
471422
if (m_initialValues.downSpeedLimit != m_ui->spinDownloadLimit->value())
472423
torrent->setDownloadLimit(m_ui->spinDownloadLimit->value() * 1024);
473424

474-
const qreal ratioLimit = getRatio();
475-
if (m_initialValues.ratio != ratioLimit)
476-
torrent->setRatioLimit(ratioLimit);
425+
if (const std::optional<qreal> ratioLimit = m_ui->torrentShareLimitsWidget->ratioLimit();
426+
m_initialValues.ratio != ratioLimit)
427+
{
428+
torrent->setRatioLimit(ratioLimit.value());
429+
}
477430

478-
const int seedingTimeLimit = getSeedingTime();
479-
if (m_initialValues.seedingTime != seedingTimeLimit)
480-
torrent->setSeedingTimeLimit(seedingTimeLimit);
431+
if (const std::optional<int> seedingTimeLimit = m_ui->torrentShareLimitsWidget->seedingTimeLimit();
432+
m_initialValues.seedingTime != seedingTimeLimit)
433+
{
434+
torrent->setSeedingTimeLimit(seedingTimeLimit.value());
435+
}
481436

482-
const int inactiveSeedingTimeLimit = getInactiveSeedingTime();
483-
if (m_initialValues.inactiveSeedingTime != inactiveSeedingTimeLimit)
484-
torrent->setInactiveSeedingTimeLimit(inactiveSeedingTimeLimit);
437+
if (const std::optional<int> inactiveSeedingTimeLimit = m_ui->torrentShareLimitsWidget->inactiveSeedingTimeLimit();
438+
m_initialValues.inactiveSeedingTime != inactiveSeedingTimeLimit)
439+
{
440+
torrent->setInactiveSeedingTimeLimit(inactiveSeedingTimeLimit.value());
441+
}
485442

486443
if (!torrent->isPrivate())
487444
{
@@ -502,48 +459,6 @@ void TorrentOptionsDialog::accept()
502459
QDialog::accept();
503460
}
504461

505-
qreal TorrentOptionsDialog::getRatio() const
506-
{
507-
if (m_ui->buttonGroup->checkedId() == -1) // No radio button is selected
508-
return MIXED_SHARE_LIMITS;
509-
510-
if (m_ui->radioUseGlobalShareLimits->isChecked())
511-
return BitTorrent::Torrent::USE_GLOBAL_RATIO;
512-
513-
if (m_ui->radioNoLimit->isChecked() || !m_ui->checkMaxRatio->isChecked())
514-
return BitTorrent::Torrent::NO_RATIO_LIMIT;
515-
516-
return m_ui->spinRatioLimit->value();
517-
}
518-
519-
int TorrentOptionsDialog::getSeedingTime() const
520-
{
521-
if (m_ui->buttonGroup->checkedId() == -1) // No radio button is selected
522-
return MIXED_SHARE_LIMITS;
523-
524-
if (m_ui->radioUseGlobalShareLimits->isChecked())
525-
return BitTorrent::Torrent::USE_GLOBAL_SEEDING_TIME;
526-
527-
if (m_ui->radioNoLimit->isChecked() || !m_ui->checkMaxTime->isChecked())
528-
return BitTorrent::Torrent::NO_SEEDING_TIME_LIMIT;
529-
530-
return m_ui->spinTimeLimit->value();
531-
}
532-
533-
int TorrentOptionsDialog::getInactiveSeedingTime() const
534-
{
535-
if (m_ui->buttonGroup->checkedId() == -1) // No radio button is selected
536-
return MIXED_SHARE_LIMITS;
537-
538-
if (m_ui->radioUseGlobalShareLimits->isChecked())
539-
return BitTorrent::Torrent::USE_GLOBAL_INACTIVE_SEEDING_TIME;
540-
541-
if (m_ui->radioNoLimit->isChecked() || !m_ui->checkMaxInactiveTime->isChecked())
542-
return BitTorrent::Torrent::NO_INACTIVE_SEEDING_TIME_LIMIT;
543-
544-
return m_ui->spinInactiveTimeLimit->value();
545-
}
546-
547462
void TorrentOptionsDialog::handleCategoryChanged([[maybe_unused]] const int index)
548463
{
549464
if (m_ui->checkAutoTMM->checkState() == Qt::Checked)
@@ -619,31 +534,6 @@ void TorrentOptionsDialog::handleUseDownloadPathChanged()
619534
m_ui->downloadPath->setSelectedPath(BitTorrent::Session::instance()->downloadPath());
620535
}
621536

622-
void TorrentOptionsDialog::handleRatioTypeChanged()
623-
{
624-
if ((m_initialValues.ratio == MIXED_SHARE_LIMITS) || (m_initialValues.seedingTime == MIXED_SHARE_LIMITS)
625-
|| (m_initialValues.inactiveSeedingTime == MIXED_SHARE_LIMITS))
626-
{
627-
QAbstractButton *currentRadio = m_ui->buttonGroup->checkedButton();
628-
if (currentRadio && (currentRadio == m_previousRadio))
629-
{
630-
// Hack to deselect the currently selected radio button programmatically because Qt doesn't allow it in exclusive mode
631-
m_ui->buttonGroup->setExclusive(false);
632-
currentRadio->setChecked(false);
633-
m_ui->buttonGroup->setExclusive(true);
634-
}
635-
m_previousRadio = m_ui->buttonGroup->checkedButton();
636-
}
637-
638-
m_ui->checkMaxRatio->setEnabled(m_ui->radioTorrentLimit->isChecked());
639-
m_ui->checkMaxTime->setEnabled(m_ui->radioTorrentLimit->isChecked());
640-
m_ui->checkMaxInactiveTime->setEnabled(m_ui->radioTorrentLimit->isChecked());
641-
642-
m_ui->spinRatioLimit->setEnabled(m_ui->radioTorrentLimit->isChecked() && m_ui->checkMaxRatio->isChecked());
643-
m_ui->spinTimeLimit->setEnabled(m_ui->radioTorrentLimit->isChecked() && m_ui->checkMaxTime->isChecked());
644-
m_ui->spinInactiveTimeLimit->setEnabled(m_ui->radioTorrentLimit->isChecked() && m_ui->checkMaxInactiveTime->isChecked());
645-
}
646-
647537
void TorrentOptionsDialog::handleUpSpeedLimitChanged()
648538
{
649539
m_ui->spinUploadLimit->setMinimum(0);

src/gui/torrentoptionsdialog.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Bittorrent Client using Qt and libtorrent.
3+
* Copyright (C) 2024 Vladimir Golovnev <glassez@yandex.ru>
34
* Copyright (C) 2020 thalieht
45
* Copyright (C) 2011 Christian Kandeler
56
* Copyright (C) 2011 Christophe Dumez <chris@qbittorrent.org>
@@ -30,6 +31,8 @@
3031

3132
#pragma once
3233

34+
#include <optional>
35+
3336
#include <QDialog>
3437

3538
#include "base/path.h"
@@ -68,13 +71,7 @@ private slots:
6871
void handleUpSpeedLimitChanged();
6972
void handleDownSpeedLimitChanged();
7073

71-
void handleRatioTypeChanged();
72-
7374
private:
74-
qreal getRatio() const;
75-
int getSeedingTime() const;
76-
int getInactiveSeedingTime() const;
77-
7875
QVector<BitTorrent::TorrentID> m_torrentIDs;
7976
Ui::TorrentOptionsDialog *m_ui = nullptr;
8077
SettingValue<QSize> m_storeDialogSize;
@@ -87,9 +84,9 @@ private slots:
8784
Path savePath;
8885
Path downloadPath;
8986
QString category;
90-
qreal ratio;
91-
int seedingTime;
92-
int inactiveSeedingTime;
87+
std::optional<qreal> ratio;
88+
std::optional<int> seedingTime;
89+
std::optional<int> inactiveSeedingTime;
9390
int upSpeedLimit;
9491
int downSpeedLimit;
9592
Qt::CheckState autoTMM;

0 commit comments

Comments
 (0)