Simplify share ratio limits processing code

PR #19971.
This commit is contained in:
Vladimir Golovnev 2023-11-21 22:14:27 +03:00 committed by GitHub
parent c3adc90f7e
commit 60eaff9fcb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2208,135 +2208,119 @@ void SessionImpl::processShareLimits()
{ {
qDebug("Processing share limits..."); qDebug("Processing share limits...");
const auto resolveLimitValue = []<typename T>(const T limit, const T useGlobalLimit, const T globalLimit) -> T
{
return (limit == useGlobalLimit) ? globalLimit : limit;
};
// We shouldn't iterate over `m_torrents` in the loop below // We shouldn't iterate over `m_torrents` in the loop below
// since `deleteTorrent()` modifies it indirectly // since `deleteTorrent()` modifies it indirectly
const QHash<TorrentID, TorrentImpl *> torrents {m_torrents}; const QHash<TorrentID, TorrentImpl *> torrents {m_torrents};
for (TorrentImpl *const torrent : torrents) for (const auto &[torrentID, torrent] : torrents.asKeyValueRange())
{ {
if (torrent->isFinished() && !torrent->isForced()) if (!torrent->isFinished() || torrent->isForced())
continue;
if (const qreal ratioLimit = resolveLimitValue(torrent->ratioLimit(), Torrent::USE_GLOBAL_RATIO, globalMaxRatio());
ratioLimit >= 0)
{ {
if (torrent->ratioLimit() != Torrent::NO_RATIO_LIMIT) const qreal ratio = torrent->realRatio();
qDebug("Ratio: %f (limit: %f)", ratio, ratioLimit);
if ((ratio <= Torrent::MAX_RATIO) && (ratio >= ratioLimit))
{ {
const qreal ratio = torrent->realRatio(); const QString description = tr("Torrent reached the share ratio limit.");
qreal ratioLimit = torrent->ratioLimit(); const QString torrentName = tr("Torrent: \"%1\".").arg(torrent->name());
if (ratioLimit == Torrent::USE_GLOBAL_RATIO)
// If Global Max Ratio is really set...
ratioLimit = globalMaxRatio();
if (ratioLimit >= 0) if (m_maxRatioAction == Remove)
{ {
qDebug("Ratio: %f (limit: %f)", ratio, ratioLimit); LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent."), torrentName));
deleteTorrent(torrentID);
if ((ratio <= Torrent::MAX_RATIO) && (ratio >= ratioLimit))
{
const QString description = tr("Torrent reached the share ratio limit.");
const QString torrentName = tr("Torrent: \"%1\".").arg(torrent->name());
if (m_maxRatioAction == Remove)
{
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent."), torrentName));
deleteTorrent(torrent->id());
}
else if (m_maxRatioAction == DeleteFiles)
{
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent and deleted its content."), torrentName));
deleteTorrent(torrent->id(), DeleteTorrentAndFiles);
}
else if ((m_maxRatioAction == Pause) && !torrent->isPaused())
{
torrent->pause();
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Torrent paused."), torrentName));
}
else if ((m_maxRatioAction == EnableSuperSeeding) && !torrent->isPaused() && !torrent->superSeeding())
{
torrent->setSuperSeeding(true);
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Super seeding enabled."), torrentName));
}
continue;
}
} }
else if (m_maxRatioAction == DeleteFiles)
{
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent and deleted its content."), torrentName));
deleteTorrent(torrentID, DeleteTorrentAndFiles);
}
else if ((m_maxRatioAction == Pause) && !torrent->isPaused())
{
torrent->pause();
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Torrent paused."), torrentName));
}
else if ((m_maxRatioAction == EnableSuperSeeding) && !torrent->isPaused() && !torrent->superSeeding())
{
torrent->setSuperSeeding(true);
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Super seeding enabled."), torrentName));
}
continue;
} }
}
if (torrent->seedingTimeLimit() != Torrent::NO_SEEDING_TIME_LIMIT) if (const int seedingTimeLimit = resolveLimitValue(torrent->seedingTimeLimit(), Torrent::USE_GLOBAL_SEEDING_TIME, globalMaxSeedingMinutes());
seedingTimeLimit >= 0)
{
const qlonglong seedingTimeInMinutes = torrent->finishedTime() / 60;
if ((seedingTimeInMinutes <= Torrent::MAX_SEEDING_TIME) && (seedingTimeInMinutes >= seedingTimeLimit))
{ {
const qlonglong seedingTimeInMinutes = torrent->finishedTime() / 60; const QString description = tr("Torrent reached the seeding time limit.");
int seedingTimeLimit = torrent->seedingTimeLimit(); const QString torrentName = tr("Torrent: \"%1\".").arg(torrent->name());
if (seedingTimeLimit == Torrent::USE_GLOBAL_SEEDING_TIME)
if (m_maxRatioAction == Remove)
{ {
// If Global Seeding Time Limit is really set... LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent."), torrentName));
seedingTimeLimit = globalMaxSeedingMinutes(); deleteTorrent(torrentID);
}
else if (m_maxRatioAction == DeleteFiles)
{
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent and deleted its content."), torrentName));
deleteTorrent(torrentID, DeleteTorrentAndFiles);
}
else if ((m_maxRatioAction == Pause) && !torrent->isPaused())
{
torrent->pause();
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Torrent paused."), torrentName));
}
else if ((m_maxRatioAction == EnableSuperSeeding) && !torrent->isPaused() && !torrent->superSeeding())
{
torrent->setSuperSeeding(true);
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Super seeding enabled."), torrentName));
} }
if (seedingTimeLimit >= 0) continue;
{
if ((seedingTimeInMinutes <= Torrent::MAX_SEEDING_TIME) && (seedingTimeInMinutes >= seedingTimeLimit))
{
const QString description = tr("Torrent reached the seeding time limit.");
const QString torrentName = tr("Torrent: \"%1\".").arg(torrent->name());
if (m_maxRatioAction == Remove)
{
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent."), torrentName));
deleteTorrent(torrent->id());
}
else if (m_maxRatioAction == DeleteFiles)
{
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent and deleted its content."), torrentName));
deleteTorrent(torrent->id(), DeleteTorrentAndFiles);
}
else if ((m_maxRatioAction == Pause) && !torrent->isPaused())
{
torrent->pause();
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Torrent paused."), torrentName));
}
else if ((m_maxRatioAction == EnableSuperSeeding) && !torrent->isPaused() && !torrent->superSeeding())
{
torrent->setSuperSeeding(true);
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Super seeding enabled."), torrentName));
}
}
}
} }
}
if (torrent->inactiveSeedingTimeLimit() != Torrent::NO_INACTIVE_SEEDING_TIME_LIMIT) if (const int inactiveSeedingTimeLimit = resolveLimitValue(torrent->inactiveSeedingTimeLimit(), Torrent::USE_GLOBAL_INACTIVE_SEEDING_TIME, globalMaxInactiveSeedingMinutes());
inactiveSeedingTimeLimit >= 0)
{
const qlonglong inactiveSeedingTimeInMinutes = torrent->timeSinceActivity() / 60;
if ((inactiveSeedingTimeInMinutes <= Torrent::MAX_INACTIVE_SEEDING_TIME) && (inactiveSeedingTimeInMinutes >= inactiveSeedingTimeLimit))
{ {
const qlonglong inactiveSeedingTimeInMinutes = torrent->timeSinceActivity() / 60; const QString description = tr("Torrent reached the inactive seeding time limit.");
int inactiveSeedingTimeLimit = torrent->inactiveSeedingTimeLimit(); const QString torrentName = tr("Torrent: \"%1\".").arg(torrent->name());
if (inactiveSeedingTimeLimit == Torrent::USE_GLOBAL_INACTIVE_SEEDING_TIME)
if (m_maxRatioAction == Remove)
{ {
// If Global Seeding Time Limit is really set... LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent."), torrentName));
inactiveSeedingTimeLimit = globalMaxInactiveSeedingMinutes(); deleteTorrent(torrentID);
} }
else if (m_maxRatioAction == DeleteFiles)
if (inactiveSeedingTimeLimit >= 0)
{ {
if ((inactiveSeedingTimeInMinutes <= Torrent::MAX_INACTIVE_SEEDING_TIME) && (inactiveSeedingTimeInMinutes >= inactiveSeedingTimeLimit)) LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent and deleted its content."), torrentName));
{ deleteTorrent(torrentID, DeleteTorrentAndFiles);
const QString description = tr("Torrent reached the inactive seeding time limit."); }
const QString torrentName = tr("Torrent: \"%1\".").arg(torrent->name()); else if ((m_maxRatioAction == Pause) && !torrent->isPaused())
{
if (m_maxRatioAction == Remove) torrent->pause();
{ LogMsg(u"%1 %2 %3"_s.arg(description, tr("Torrent paused."), torrentName));
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent."), torrentName)); }
deleteTorrent(torrent->id()); else if ((m_maxRatioAction == EnableSuperSeeding) && !torrent->isPaused() && !torrent->superSeeding())
} {
else if (m_maxRatioAction == DeleteFiles) torrent->setSuperSeeding(true);
{ LogMsg(u"%1 %2 %3"_s.arg(description, tr("Super seeding enabled."), torrentName));
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent and deleted its content."), torrentName));
deleteTorrent(torrent->id(), DeleteTorrentAndFiles);
}
else if ((m_maxRatioAction == Pause) && !torrent->isPaused())
{
torrent->pause();
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Torrent paused."), torrentName));
}
else if ((m_maxRatioAction == EnableSuperSeeding) && !torrent->isPaused() && !torrent->superSeeding())
{
torrent->setSuperSeeding(true);
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Super seeding enabled."), torrentName));
}
}
} }
} }
} }