Allow to set torrent share limits per category

PR #23577.
This commit is contained in:
Vladimir Golovnev
2025-12-21 19:48:31 +03:00
committed by GitHub
parent 1c231ce014
commit b5d16dfeee
31 changed files with 480 additions and 283 deletions

View File

@@ -52,6 +52,29 @@ namespace
RemoveWithContentActionIndex,
SuperSeedingActionIndex
};
QString shareLimitActionName(const BitTorrent::ShareLimitAction shareLimitAction)
{
switch (shareLimitAction)
{
case BitTorrent::ShareLimitAction::Stop:
return TorrentShareLimitsWidget::tr("Stop torrent");
case BitTorrent::ShareLimitAction::Remove:
return TorrentShareLimitsWidget::tr("Remove torrent");
case BitTorrent::ShareLimitAction::RemoveWithContent:
return TorrentShareLimitsWidget::tr("Remove torrent and its content");
case BitTorrent::ShareLimitAction::EnableSuperSeeding:
return TorrentShareLimitsWidget::tr("Enable super seeding for torrent");
case BitTorrent::ShareLimitAction::Default:
return TorrentShareLimitsWidget::tr("Default");
}
return {};
}
}
TorrentShareLimitsWidget::TorrentShareLimitsWidget(QWidget *parent)
@@ -60,6 +83,30 @@ TorrentShareLimitsWidget::TorrentShareLimitsWidget(QWidget *parent)
{
m_ui->setupUi(this);
m_ui->comboBoxRatioMode->addItem({});
m_ui->comboBoxRatioMode->addItem(tr("Unlimited"));
m_ui->comboBoxRatioMode->addItem(tr("Set to"));
m_ui->comboBoxRatioMode->setCurrentIndex(UninitializedModeIndex);
m_ui->comboBoxSeedingTimeMode->addItem({});
m_ui->comboBoxSeedingTimeMode->addItem(tr("Unlimited"));
m_ui->comboBoxSeedingTimeMode->addItem(tr("Set to"));
m_ui->comboBoxSeedingTimeMode->setCurrentIndex(UninitializedModeIndex);
m_ui->comboBoxInactiveSeedingTimeMode->addItem({});
m_ui->comboBoxInactiveSeedingTimeMode->addItem(tr("Unlimited"));
m_ui->comboBoxInactiveSeedingTimeMode->addItem(tr("Set to"));
m_ui->comboBoxInactiveSeedingTimeMode->setCurrentIndex(UninitializedModeIndex);
m_ui->comboBoxAction->addItem({});
m_ui->comboBoxAction->addItem(shareLimitActionName(BitTorrent::ShareLimitAction::Stop));
m_ui->comboBoxAction->addItem(shareLimitActionName(BitTorrent::ShareLimitAction::Remove));
m_ui->comboBoxAction->addItem(shareLimitActionName(BitTorrent::ShareLimitAction::RemoveWithContent));
m_ui->comboBoxAction->addItem(shareLimitActionName(BitTorrent::ShareLimitAction::EnableSuperSeeding));
m_ui->comboBoxAction->setCurrentIndex(UninitializedActionIndex);
resetDefaultItemsText();
m_ui->spinBoxRatioValue->setEnabled(false);
m_ui->spinBoxRatioValue->setMaximum(std::numeric_limits<int>::max());
m_ui->spinBoxRatioValue->setSuffix({});
@@ -71,9 +118,25 @@ TorrentShareLimitsWidget::TorrentShareLimitsWidget(QWidget *parent)
m_ui->spinBoxInactiveSeedingTimeValue->setSuffix({});
m_ui->spinBoxInactiveSeedingTimeValue->clear();
connect(m_ui->comboBoxRatioMode, &QComboBox::currentIndexChanged, this, &TorrentShareLimitsWidget::refreshRatioLimitControls);
connect(m_ui->comboBoxSeedingTimeMode, &QComboBox::currentIndexChanged, this, &TorrentShareLimitsWidget::refreshSeedingTimeLimitControls);
connect(m_ui->comboBoxInactiveSeedingTimeMode, &QComboBox::currentIndexChanged, this, &TorrentShareLimitsWidget::refreshInactiveSeedingTimeLimitControls);
int prevIndex = UninitializedModeIndex;
connect(m_ui->comboBoxRatioMode, &QComboBox::currentIndexChanged, this
, [this, prevIndex](const int currentIndex) mutable
{
onRatioLimitModeChanged(currentIndex, prevIndex);
prevIndex = currentIndex;
});
connect(m_ui->comboBoxSeedingTimeMode, &QComboBox::currentIndexChanged, this
, [this, prevIndex](const int currentIndex) mutable
{
onSeedingTimeLimitModeChanged(currentIndex, prevIndex);
prevIndex = currentIndex;
});
connect(m_ui->comboBoxInactiveSeedingTimeMode, &QComboBox::currentIndexChanged, this
, [this, prevIndex](const int currentIndex) mutable
{
onInactiveSeedingTimeLimitModeChanged(currentIndex, prevIndex);
prevIndex = currentIndex;
});
}
TorrentShareLimitsWidget::~TorrentShareLimitsWidget()
@@ -83,11 +146,11 @@ TorrentShareLimitsWidget::~TorrentShareLimitsWidget()
void TorrentShareLimitsWidget::setRatioLimit(const qreal ratioLimit)
{
if (ratioLimit == BitTorrent::Torrent::USE_GLOBAL_RATIO)
if (ratioLimit == BitTorrent::DEFAULT_RATIO_LIMIT)
{
m_ui->comboBoxRatioMode->setCurrentIndex(DefaultModeIndex);
}
else if (ratioLimit == BitTorrent::Torrent::NO_RATIO_LIMIT)
else if (ratioLimit == BitTorrent::NO_RATIO_LIMIT)
{
m_ui->comboBoxRatioMode->setCurrentIndex(UnlimitedModeIndex);
}
@@ -100,11 +163,11 @@ void TorrentShareLimitsWidget::setRatioLimit(const qreal ratioLimit)
void TorrentShareLimitsWidget::setSeedingTimeLimit(const int seedingTimeLimit)
{
if (seedingTimeLimit == BitTorrent::Torrent::USE_GLOBAL_SEEDING_TIME)
if (seedingTimeLimit == BitTorrent::DEFAULT_SEEDING_TIME_LIMIT)
{
m_ui->comboBoxSeedingTimeMode->setCurrentIndex(DefaultModeIndex);
}
else if (seedingTimeLimit == BitTorrent::Torrent::NO_SEEDING_TIME_LIMIT)
else if (seedingTimeLimit == BitTorrent::NO_SEEDING_TIME_LIMIT)
{
m_ui->comboBoxSeedingTimeMode->setCurrentIndex(UnlimitedModeIndex);
}
@@ -117,11 +180,11 @@ void TorrentShareLimitsWidget::setSeedingTimeLimit(const int seedingTimeLimit)
void TorrentShareLimitsWidget::setInactiveSeedingTimeLimit(const int inactiveSeedingTimeLimit)
{
if (inactiveSeedingTimeLimit == BitTorrent::Torrent::USE_GLOBAL_INACTIVE_SEEDING_TIME)
if (inactiveSeedingTimeLimit == BitTorrent::DEFAULT_SEEDING_TIME_LIMIT)
{
m_ui->comboBoxInactiveSeedingTimeMode->setCurrentIndex(DefaultModeIndex);
}
else if (inactiveSeedingTimeLimit == BitTorrent::Torrent::NO_INACTIVE_SEEDING_TIME_LIMIT)
else if (inactiveSeedingTimeLimit == BitTorrent::NO_SEEDING_TIME_LIMIT)
{
m_ui->comboBoxInactiveSeedingTimeMode->setCurrentIndex(UnlimitedModeIndex);
}
@@ -155,25 +218,56 @@ void TorrentShareLimitsWidget::setShareLimitAction(const BitTorrent::ShareLimitA
}
}
void TorrentShareLimitsWidget::setDefaultLimits(const qreal ratioLimit, const int seedingTimeLimit, const int inactiveSeedingTimeLimit)
void TorrentShareLimitsWidget::setDefaults(UsedDefaults usedDefaults
, const qreal ratioLimit, const int seedingTimeLimit
, const int inactiveSeedingTimeLimit, const BitTorrent::ShareLimitAction action)
{
if (m_defaultRatioLimit != ratioLimit)
m_defaultRatioLimit = ratioLimit;
if (m_ui->comboBoxRatioMode->currentIndex() == DefaultModeIndex)
{
m_defaultRatioLimit = ratioLimit;
refreshRatioLimitControls();
if (m_defaultRatioLimit >= 0)
{
m_ui->spinBoxRatioValue->setValue(m_defaultRatioLimit);
}
else
{
m_ui->spinBoxRatioValue->clear();
}
}
if (m_defaultSeedingTimeLimit != seedingTimeLimit)
m_defaultSeedingTimeLimit = seedingTimeLimit;
if (m_ui->comboBoxSeedingTimeMode->currentIndex() == DefaultModeIndex)
{
m_defaultSeedingTimeLimit = seedingTimeLimit;
refreshSeedingTimeLimitControls();
if (m_defaultSeedingTimeLimit >= 0)
{
m_ui->spinBoxSeedingTimeValue->setValue(m_defaultSeedingTimeLimit);
m_ui->spinBoxSeedingTimeValue->setSuffix(tr(" min"));
}
else
{
m_ui->spinBoxSeedingTimeValue->setSuffix({});
m_ui->spinBoxSeedingTimeValue->clear();
}
}
if (m_defaultInactiveSeedingTimeLimit != inactiveSeedingTimeLimit)
m_defaultInactiveSeedingTimeLimit = inactiveSeedingTimeLimit;
if (m_ui->comboBoxInactiveSeedingTimeMode->currentIndex() == DefaultModeIndex)
{
m_defaultInactiveSeedingTimeLimit = inactiveSeedingTimeLimit;
refreshInactiveSeedingTimeLimitControls();
if (m_defaultInactiveSeedingTimeLimit >= 0)
{
m_ui->spinBoxInactiveSeedingTimeValue->setValue(m_defaultInactiveSeedingTimeLimit);
m_ui->spinBoxInactiveSeedingTimeValue->setSuffix(tr(" min"));
}
else
{
m_ui->spinBoxInactiveSeedingTimeValue->setSuffix({});
m_ui->spinBoxInactiveSeedingTimeValue->clear();
}
}
m_defaultShareLimitAction = action;
m_usedDefaults = usedDefaults;
resetDefaultItemsText();
}
std::optional<qreal> TorrentShareLimitsWidget::ratioLimit() const
@@ -181,9 +275,9 @@ std::optional<qreal> TorrentShareLimitsWidget::ratioLimit() const
switch (m_ui->comboBoxRatioMode->currentIndex())
{
case DefaultModeIndex:
return BitTorrent::Torrent::USE_GLOBAL_RATIO;
return BitTorrent::DEFAULT_RATIO_LIMIT;
case UnlimitedModeIndex:
return BitTorrent::Torrent::NO_RATIO_LIMIT;
return BitTorrent::NO_RATIO_LIMIT;
case AssignedModeIndex:
return m_ui->spinBoxRatioValue->value();
default:
@@ -196,9 +290,9 @@ std::optional<int> TorrentShareLimitsWidget::seedingTimeLimit() const
switch (m_ui->comboBoxSeedingTimeMode->currentIndex())
{
case DefaultModeIndex:
return BitTorrent::Torrent::USE_GLOBAL_SEEDING_TIME;
return BitTorrent::DEFAULT_SEEDING_TIME_LIMIT;
case UnlimitedModeIndex:
return BitTorrent::Torrent::NO_SEEDING_TIME_LIMIT;
return BitTorrent::NO_SEEDING_TIME_LIMIT;
case AssignedModeIndex:
return m_ui->spinBoxSeedingTimeValue->value();
default:
@@ -211,9 +305,9 @@ std::optional<int> TorrentShareLimitsWidget::inactiveSeedingTimeLimit() const
switch (m_ui->comboBoxInactiveSeedingTimeMode->currentIndex())
{
case DefaultModeIndex:
return BitTorrent::Torrent::USE_GLOBAL_INACTIVE_SEEDING_TIME;
return BitTorrent::DEFAULT_SEEDING_TIME_LIMIT;
case UnlimitedModeIndex:
return BitTorrent::Torrent::NO_INACTIVE_SEEDING_TIME_LIMIT;
return BitTorrent::NO_SEEDING_TIME_LIMIT;
case AssignedModeIndex:
return m_ui->spinBoxInactiveSeedingTimeValue->value();
default:
@@ -240,68 +334,97 @@ std::optional<BitTorrent::ShareLimitAction> TorrentShareLimitsWidget::shareLimit
}
}
void TorrentShareLimitsWidget::refreshRatioLimitControls()
void TorrentShareLimitsWidget::onRatioLimitModeChanged(const int currentIndex, const int previousIndex)
{
const auto index = m_ui->comboBoxRatioMode->currentIndex();
m_ui->spinBoxRatioValue->setEnabled(currentIndex == AssignedModeIndex);
m_ui->spinBoxRatioValue->setEnabled(index == AssignedModeIndex);
if (index == AssignedModeIndex)
if (previousIndex == AssignedModeIndex)
m_ratioLimit = m_ui->spinBoxRatioValue->value();
if (currentIndex == AssignedModeIndex)
{
m_ui->spinBoxRatioValue->setValue(m_ratioLimit);
}
else if ((index == DefaultModeIndex) && (m_defaultRatioLimit >= 0))
else if ((currentIndex == DefaultModeIndex) && (m_defaultRatioLimit >= 0))
{
m_ui->spinBoxRatioValue->setValue(m_defaultRatioLimit);
}
else
{
m_ratioLimit = m_ui->spinBoxRatioValue->value();
m_ui->spinBoxRatioValue->clear();
}
}
void TorrentShareLimitsWidget::refreshSeedingTimeLimitControls()
void TorrentShareLimitsWidget::onSeedingTimeLimitModeChanged(const int currentIndex, const int previousIndex)
{
const auto index = m_ui->comboBoxSeedingTimeMode->currentIndex();
m_ui->spinBoxSeedingTimeValue->setEnabled(currentIndex == AssignedModeIndex);
m_ui->spinBoxSeedingTimeValue->setEnabled(index == AssignedModeIndex);
if (index == AssignedModeIndex)
if (previousIndex == AssignedModeIndex)
m_seedingTimeLimit = m_ui->spinBoxSeedingTimeValue->value();
if (currentIndex == AssignedModeIndex)
{
m_ui->spinBoxSeedingTimeValue->setValue(m_seedingTimeLimit);
m_ui->spinBoxSeedingTimeValue->setSuffix(tr(" min"));
}
else if ((index == DefaultModeIndex) && (m_defaultSeedingTimeLimit >= 0))
else if ((currentIndex == DefaultModeIndex) && (m_defaultSeedingTimeLimit >= 0))
{
m_ui->spinBoxSeedingTimeValue->setValue(m_defaultSeedingTimeLimit);
m_ui->spinBoxSeedingTimeValue->setSuffix(tr(" min"));
}
else
{
m_seedingTimeLimit = m_ui->spinBoxSeedingTimeValue->value();
m_ui->spinBoxSeedingTimeValue->setSuffix({});
m_ui->spinBoxSeedingTimeValue->clear();
}
}
void TorrentShareLimitsWidget::refreshInactiveSeedingTimeLimitControls()
void TorrentShareLimitsWidget::onInactiveSeedingTimeLimitModeChanged(const int currentIndex, const int previousIndex)
{
const auto index = m_ui->comboBoxInactiveSeedingTimeMode->currentIndex();
m_ui->spinBoxInactiveSeedingTimeValue->setEnabled(currentIndex == AssignedModeIndex);
m_ui->spinBoxInactiveSeedingTimeValue->setEnabled(index == AssignedModeIndex);
if (index == AssignedModeIndex)
if (previousIndex == AssignedModeIndex)
m_inactiveSeedingTimeLimit = m_ui->spinBoxInactiveSeedingTimeValue->value();
if (currentIndex == AssignedModeIndex)
{
m_ui->spinBoxInactiveSeedingTimeValue->setValue(m_inactiveSeedingTimeLimit);
m_ui->spinBoxInactiveSeedingTimeValue->setSuffix(tr(" min"));
}
else if ((index == DefaultModeIndex) && (m_defaultInactiveSeedingTimeLimit >= 0))
else if ((currentIndex == DefaultModeIndex) && (m_defaultInactiveSeedingTimeLimit >= 0))
{
m_ui->spinBoxInactiveSeedingTimeValue->setValue(m_defaultInactiveSeedingTimeLimit);
m_ui->spinBoxInactiveSeedingTimeValue->setSuffix(tr(" min"));
}
else
{
m_inactiveSeedingTimeLimit = m_ui->spinBoxInactiveSeedingTimeValue->value();
m_ui->spinBoxInactiveSeedingTimeValue->setSuffix({});
m_ui->spinBoxInactiveSeedingTimeValue->clear();
}
}
void TorrentShareLimitsWidget::resetDefaultItemsText()
{
if (m_usedDefaults == UsedDefaults::Global)
{
m_ui->comboBoxRatioMode->setItemText(DefaultModeIndex, tr("Default"));
m_ui->comboBoxSeedingTimeMode->setItemText(DefaultModeIndex, tr("Default"));
m_ui->comboBoxInactiveSeedingTimeMode->setItemText(DefaultModeIndex, tr("Default"));
m_ui->comboBoxAction->setItemText(DefaultActionIndex
, (m_defaultShareLimitAction == BitTorrent::ShareLimitAction::Default)
? tr("Default")
: tr("Default (%1)", "Default (share limit action)").arg(shareLimitActionName(m_defaultShareLimitAction)));
}
else // TorrentOptions
{
m_ui->comboBoxRatioMode->setItemText(DefaultModeIndex, tr("From category"));
m_ui->comboBoxSeedingTimeMode->setItemText(DefaultModeIndex, tr("From category"));
m_ui->comboBoxInactiveSeedingTimeMode->setItemText(DefaultModeIndex, tr("From category"));
m_ui->comboBoxAction->setItemText(DefaultActionIndex
, (m_defaultShareLimitAction == BitTorrent::ShareLimitAction::Default)
? tr("From category")
: tr("From category (%1)", "From category (share limit action)").arg(shareLimitActionName(m_defaultShareLimitAction)));
}
}