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,23 +2208,23 @@ 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 (torrent->ratioLimit() != Torrent::NO_RATIO_LIMIT)
if (const qreal ratioLimit = resolveLimitValue(torrent->ratioLimit(), Torrent::USE_GLOBAL_RATIO, globalMaxRatio());
ratioLimit >= 0)
{ {
const qreal ratio = torrent->realRatio(); const qreal ratio = torrent->realRatio();
qreal ratioLimit = torrent->ratioLimit();
if (ratioLimit == Torrent::USE_GLOBAL_RATIO)
// If Global Max Ratio is really set...
ratioLimit = globalMaxRatio();
if (ratioLimit >= 0)
{
qDebug("Ratio: %f (limit: %f)", ratio, ratioLimit); qDebug("Ratio: %f (limit: %f)", ratio, ratioLimit);
if ((ratio <= Torrent::MAX_RATIO) && (ratio >= ratioLimit)) if ((ratio <= Torrent::MAX_RATIO) && (ratio >= ratioLimit))
@ -2235,12 +2235,12 @@ void SessionImpl::processShareLimits()
if (m_maxRatioAction == Remove) if (m_maxRatioAction == Remove)
{ {
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent."), torrentName)); LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent."), torrentName));
deleteTorrent(torrent->id()); deleteTorrent(torrentID);
} }
else if (m_maxRatioAction == DeleteFiles) else if (m_maxRatioAction == DeleteFiles)
{ {
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent and deleted its content."), torrentName)); LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent and deleted its content."), torrentName));
deleteTorrent(torrent->id(), DeleteTorrentAndFiles); deleteTorrent(torrentID, DeleteTorrentAndFiles);
} }
else if ((m_maxRatioAction == Pause) && !torrent->isPaused()) else if ((m_maxRatioAction == Pause) && !torrent->isPaused())
{ {
@ -2256,20 +2256,12 @@ void SessionImpl::processShareLimits()
continue; 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; const qlonglong seedingTimeInMinutes = torrent->finishedTime() / 60;
int seedingTimeLimit = torrent->seedingTimeLimit();
if (seedingTimeLimit == Torrent::USE_GLOBAL_SEEDING_TIME)
{
// If Global Seeding Time Limit is really set...
seedingTimeLimit = globalMaxSeedingMinutes();
}
if (seedingTimeLimit >= 0)
{
if ((seedingTimeInMinutes <= Torrent::MAX_SEEDING_TIME) && (seedingTimeInMinutes >= seedingTimeLimit)) if ((seedingTimeInMinutes <= Torrent::MAX_SEEDING_TIME) && (seedingTimeInMinutes >= seedingTimeLimit))
{ {
const QString description = tr("Torrent reached the seeding time limit."); const QString description = tr("Torrent reached the seeding time limit.");
@ -2278,12 +2270,12 @@ void SessionImpl::processShareLimits()
if (m_maxRatioAction == Remove) if (m_maxRatioAction == Remove)
{ {
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent."), torrentName)); LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent."), torrentName));
deleteTorrent(torrent->id()); deleteTorrent(torrentID);
} }
else if (m_maxRatioAction == DeleteFiles) else if (m_maxRatioAction == DeleteFiles)
{ {
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent and deleted its content."), torrentName)); LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent and deleted its content."), torrentName));
deleteTorrent(torrent->id(), DeleteTorrentAndFiles); deleteTorrent(torrentID, DeleteTorrentAndFiles);
} }
else if ((m_maxRatioAction == Pause) && !torrent->isPaused()) else if ((m_maxRatioAction == Pause) && !torrent->isPaused())
{ {
@ -2295,22 +2287,16 @@ void SessionImpl::processShareLimits()
torrent->setSuperSeeding(true); 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("Super seeding enabled."), torrentName));
} }
}
continue;
} }
} }
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; const qlonglong inactiveSeedingTimeInMinutes = torrent->timeSinceActivity() / 60;
int inactiveSeedingTimeLimit = torrent->inactiveSeedingTimeLimit();
if (inactiveSeedingTimeLimit == Torrent::USE_GLOBAL_INACTIVE_SEEDING_TIME)
{
// If Global Seeding Time Limit is really set...
inactiveSeedingTimeLimit = globalMaxInactiveSeedingMinutes();
}
if (inactiveSeedingTimeLimit >= 0)
{
if ((inactiveSeedingTimeInMinutes <= Torrent::MAX_INACTIVE_SEEDING_TIME) && (inactiveSeedingTimeInMinutes >= inactiveSeedingTimeLimit)) if ((inactiveSeedingTimeInMinutes <= Torrent::MAX_INACTIVE_SEEDING_TIME) && (inactiveSeedingTimeInMinutes >= inactiveSeedingTimeLimit))
{ {
const QString description = tr("Torrent reached the inactive seeding time limit."); const QString description = tr("Torrent reached the inactive seeding time limit.");
@ -2319,12 +2305,12 @@ void SessionImpl::processShareLimits()
if (m_maxRatioAction == Remove) if (m_maxRatioAction == Remove)
{ {
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent."), torrentName)); LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent."), torrentName));
deleteTorrent(torrent->id()); deleteTorrent(torrentID);
} }
else if (m_maxRatioAction == DeleteFiles) else if (m_maxRatioAction == DeleteFiles)
{ {
LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent and deleted its content."), torrentName)); LogMsg(u"%1 %2 %3"_s.arg(description, tr("Removed torrent and deleted its content."), torrentName));
deleteTorrent(torrent->id(), DeleteTorrentAndFiles); deleteTorrent(torrentID, DeleteTorrentAndFiles);
} }
else if ((m_maxRatioAction == Pause) && !torrent->isPaused()) else if ((m_maxRatioAction == Pause) && !torrent->isPaused())
{ {
@ -2339,8 +2325,6 @@ void SessionImpl::processShareLimits()
} }
} }
} }
}
}
} }
void SessionImpl::fileSearchFinished(const TorrentID &id, const Path &savePath, const PathList &fileNames) void SessionImpl::fileSearchFinished(const TorrentID &id, const Path &savePath, const PathList &fileNames)