refact: Instead of looping, use the applyToTorrents function.

This commit is contained in:
Stiliyan Tonev (Bark) 2025-05-21 08:30:28 +03:00
commit 3c78a57d7b

View file

@ -158,7 +158,7 @@ namespace
void applyToTorrents(const QStringList &idList, Func func)
requires std::invocable<Func, BitTorrent::Torrent *>
{
if ((idList.size() == 1) && (idList[0] == u"all"))
if ((idList.size() == 1) && (idList[0] == u"all" || idList[0] == u"*"))
{
for (BitTorrent::Torrent *const torrent : asConst(BitTorrent::Session::instance()->torrents()))
func(torrent);
@ -1116,31 +1116,10 @@ void TorrentsController::addAction()
void TorrentsController::addTrackersAction()
{
requireParams({u"hash"_s, u"urls"_s});
const QString hashParam = params()[u"hash"_s];
QList<BitTorrent::Torrent *> torrents;
const QList<BitTorrent::TrackerEntry> entries = BitTorrent::parseTrackerEntries(params()[u"urls"_s]);
const QList<BitTorrent::TrackerEntry> trackers = BitTorrent::parseTrackerEntries(params()[u"urls"_s]);
const QStringList idStrings = params()[u"hash"_s].split(u'|');
const QStringList idStrings = hashParam.split(u'|', Qt::SkipEmptyParts);
for (const QString &hash : idStrings)
{
if (hash == u"*"_s)
continue;
const auto id = BitTorrent::TorrentID::fromString(hash);
BitTorrent::Torrent *const torrent = BitTorrent::Session::instance()->getTorrent(id);
if (!torrent)
throw APIError(APIErrorType::NotFound);
torrents.append(torrent);
}
if (hashParam == u"*"_s)
{
// add this tracker to all torrents
torrents = BitTorrent::Session::instance()->torrents();
}
for (BitTorrent::Torrent *const torrent : asConst(torrents))
torrent->addTrackers(entries);
applyToTorrents(idStrings, [&trackers](BitTorrent::Torrent *const torrent) { torrent->addTrackers(trackers); });
}
void TorrentsController::editTrackerAction()
@ -1202,7 +1181,7 @@ void TorrentsController::removeTrackersAction()
{
requireParams({u"hash"_s, u"urls"_s});
const QString hashParam = params()[u"hash"_s];
const QStringList idStrings = params()[u"hash"_s].split(u'|', Qt::SkipEmptyParts);
const QStringList urlsParam = params()[u"urls"_s].split(u'|', Qt::SkipEmptyParts);
QStringList urls;
@ -1210,31 +1189,7 @@ void TorrentsController::removeTrackersAction()
for (const QString &urlStr : urlsParam)
urls << QUrl::fromPercentEncoding(urlStr.toLatin1());
QList<BitTorrent::Torrent *> torrents;
const QStringList idStrings = hashParam.split(u'|', Qt::SkipEmptyParts);
for (const QString &hash : idStrings)
{
if (hash == u"*"_s)
continue;
const auto id = BitTorrent::TorrentID::fromString(hash);
BitTorrent::Torrent *const torrent = BitTorrent::Session::instance()->getTorrent(id);
if (!torrent)
throw APIError(APIErrorType::NotFound);
torrents.append(torrent);
}
if (hashParam == u"*"_s)
{
// remove trackers from all torrents
torrents = BitTorrent::Session::instance()->torrents();
}
for (BitTorrent::Torrent *const torrent : asConst(torrents))
torrent->removeTrackers(urls);
setResult(QString());
applyToTorrents(idStrings, [&urls](BitTorrent::Torrent *const torrent) { torrent->removeTrackers(urls); });
}
void TorrentsController::addPeersAction()