mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-08-14 02:27:09 -07:00
Avoid redundant data copy by using JSON objects directly
This commit is contained in:
parent
f6ee96ed83
commit
cfe83275d3
3 changed files with 51 additions and 50 deletions
|
@ -131,9 +131,9 @@ namespace
|
|||
}
|
||||
}
|
||||
|
||||
QVariantList getStickyTrackers(const BitTorrent::TorrentHandle *const torrent)
|
||||
QJsonArray getStickyTrackers(const BitTorrent::TorrentHandle *const torrent)
|
||||
{
|
||||
uint seedsDHT = 0, seedsPeX = 0, seedsLSD = 0, leechesDHT = 0, leechesPeX = 0, leechesLSD = 0;
|
||||
int seedsDHT = 0, seedsPeX = 0, seedsLSD = 0, leechesDHT = 0, leechesPeX = 0, leechesLSD = 0;
|
||||
for (const BitTorrent::PeerInfo &peer : asConst(torrent->peers())) {
|
||||
if (peer.isConnecting()) continue;
|
||||
|
||||
|
@ -161,7 +161,7 @@ namespace
|
|||
const QString privateMsg {QCoreApplication::translate("TrackerListWidget", "This torrent is private")};
|
||||
const bool isTorrentPrivate = torrent->isPrivate();
|
||||
|
||||
const QVariantHash dht {
|
||||
const QJsonObject dht {
|
||||
{KEY_TRACKER_URL, "** [DHT] **"},
|
||||
{KEY_TRACKER_TIER, ""},
|
||||
{KEY_TRACKER_MSG, (isTorrentPrivate ? privateMsg : "")},
|
||||
|
@ -172,7 +172,7 @@ namespace
|
|||
{KEY_TRACKER_LEECHES_COUNT, leechesDHT}
|
||||
};
|
||||
|
||||
const QVariantHash pex {
|
||||
const QJsonObject pex {
|
||||
{KEY_TRACKER_URL, "** [PeX] **"},
|
||||
{KEY_TRACKER_TIER, ""},
|
||||
{KEY_TRACKER_MSG, (isTorrentPrivate ? privateMsg : "")},
|
||||
|
@ -183,7 +183,7 @@ namespace
|
|||
{KEY_TRACKER_LEECHES_COUNT, leechesPeX}
|
||||
};
|
||||
|
||||
const QVariantHash lsd {
|
||||
const QJsonObject lsd {
|
||||
{KEY_TRACKER_URL, "** [LSD] **"},
|
||||
{KEY_TRACKER_TIER, ""},
|
||||
{KEY_TRACKER_MSG, (isTorrentPrivate ? privateMsg : "")},
|
||||
|
@ -194,7 +194,7 @@ namespace
|
|||
{KEY_TRACKER_LEECHES_COUNT, leechesLSD}
|
||||
};
|
||||
|
||||
return QVariantList {dht, pex, lsd};
|
||||
return {dht, pex, lsd};
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -309,14 +309,14 @@ void TorrentsController::propertiesAction()
|
|||
checkParams({"hash"});
|
||||
|
||||
const QString hash {params()["hash"]};
|
||||
QVariantHash dataDict;
|
||||
QJsonObject dataDict;
|
||||
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
|
||||
if (!torrent)
|
||||
throw APIError(APIErrorType::NotFound);
|
||||
|
||||
dataDict[KEY_PROP_TIME_ELAPSED] = torrent->activeTime();
|
||||
dataDict[KEY_PROP_SEEDING_TIME] = torrent->seedingTime();
|
||||
dataDict[KEY_PROP_ETA] = torrent->eta();
|
||||
dataDict[KEY_PROP_ETA] = static_cast<double>(torrent->eta());
|
||||
dataDict[KEY_PROP_CONNECT_COUNT] = torrent->connectionsCount();
|
||||
dataDict[KEY_PROP_CONNECT_COUNT_LIMIT] = torrent->connectionsLimit();
|
||||
dataDict[KEY_PROP_DOWNLOADED] = torrent->totalDownload();
|
||||
|
@ -344,11 +344,11 @@ void TorrentsController::propertiesAction()
|
|||
dataDict[KEY_PROP_PIECE_SIZE] = torrent->pieceLength();
|
||||
dataDict[KEY_PROP_PIECES_HAVE] = torrent->piecesHave();
|
||||
dataDict[KEY_PROP_CREATED_BY] = torrent->creator();
|
||||
dataDict[KEY_PROP_ADDITION_DATE] = torrent->addedTime().toTime_t();
|
||||
dataDict[KEY_PROP_ADDITION_DATE] = static_cast<double>(torrent->addedTime().toSecsSinceEpoch());
|
||||
if (torrent->hasMetadata()) {
|
||||
dataDict[KEY_PROP_LAST_SEEN] = torrent->lastSeenComplete().isValid() ? static_cast<int>(torrent->lastSeenComplete().toTime_t()) : -1;
|
||||
dataDict[KEY_PROP_COMPLETION_DATE] = torrent->completedTime().isValid() ? static_cast<int>(torrent->completedTime().toTime_t()) : -1;
|
||||
dataDict[KEY_PROP_CREATION_DATE] = torrent->creationDate().toTime_t();
|
||||
dataDict[KEY_PROP_CREATION_DATE] = static_cast<double>(torrent->creationDate().toSecsSinceEpoch());
|
||||
}
|
||||
else {
|
||||
dataDict[KEY_PROP_LAST_SEEN] = -1;
|
||||
|
@ -358,7 +358,7 @@ void TorrentsController::propertiesAction()
|
|||
dataDict[KEY_PROP_SAVE_PATH] = Utils::Fs::toNativePath(torrent->savePath());
|
||||
dataDict[KEY_PROP_COMMENT] = torrent->comment();
|
||||
|
||||
setResult(QJsonObject::fromVariantHash(dataDict));
|
||||
setResult(dataDict);
|
||||
}
|
||||
|
||||
// Returns the trackers for a torrent in JSON format.
|
||||
|
@ -381,13 +381,13 @@ void TorrentsController::trackersAction()
|
|||
if (!torrent)
|
||||
throw APIError(APIErrorType::NotFound);
|
||||
|
||||
QVariantList trackerList = getStickyTrackers(torrent);
|
||||
QJsonArray trackerList = getStickyTrackers(torrent);
|
||||
|
||||
QHash<QString, BitTorrent::TrackerInfo> trackersData = torrent->trackerInfos();
|
||||
for (const BitTorrent::TrackerEntry &tracker : asConst(torrent->trackers())) {
|
||||
const BitTorrent::TrackerInfo data = trackersData.value(tracker.url());
|
||||
|
||||
trackerList << QVariantHash {
|
||||
trackerList << QJsonObject {
|
||||
{KEY_TRACKER_URL, tracker.url()},
|
||||
{KEY_TRACKER_TIER, tracker.tier()},
|
||||
{KEY_TRACKER_STATUS, static_cast<int>(tracker.status())},
|
||||
|
@ -399,7 +399,7 @@ void TorrentsController::trackersAction()
|
|||
};
|
||||
}
|
||||
|
||||
setResult(QJsonArray::fromVariantList(trackerList));
|
||||
setResult(trackerList);
|
||||
}
|
||||
|
||||
// Returns the web seeds for a torrent in JSON format.
|
||||
|
@ -411,18 +411,18 @@ void TorrentsController::webseedsAction()
|
|||
checkParams({"hash"});
|
||||
|
||||
const QString hash {params()["hash"]};
|
||||
QVariantList webSeedList;
|
||||
QJsonArray webSeedList;
|
||||
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
|
||||
if (!torrent)
|
||||
throw APIError(APIErrorType::NotFound);
|
||||
|
||||
for (const QUrl &webseed : asConst(torrent->urlSeeds())) {
|
||||
webSeedList.append(QVariantHash {
|
||||
webSeedList.append(QJsonObject {
|
||||
{KEY_WEBSEED_URL, webseed.toString()}
|
||||
});
|
||||
}
|
||||
|
||||
setResult(QJsonArray::fromVariantList(webSeedList));
|
||||
setResult(webSeedList);
|
||||
}
|
||||
|
||||
// Returns the files in a torrent in JSON format.
|
||||
|
@ -440,7 +440,7 @@ void TorrentsController::filesAction()
|
|||
checkParams({"hash"});
|
||||
|
||||
const QString hash {params()["hash"]};
|
||||
QVariantList fileList;
|
||||
QJsonArray fileList;
|
||||
const BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
|
||||
if (!torrent)
|
||||
throw APIError(APIErrorType::NotFound);
|
||||
|
@ -451,7 +451,7 @@ void TorrentsController::filesAction()
|
|||
const QVector<qreal> fileAvailability = torrent->availableFileFractions();
|
||||
const BitTorrent::TorrentInfo info = torrent->info();
|
||||
for (int i = 0; i < torrent->filesCount(); ++i) {
|
||||
QVariantHash fileDict = {
|
||||
QJsonObject fileDict = {
|
||||
{KEY_FILE_PROGRESS, fp[i]},
|
||||
{KEY_FILE_PRIORITY, static_cast<int>(priorities[i])},
|
||||
{KEY_FILE_SIZE, torrent->fileSize(i)},
|
||||
|
@ -464,7 +464,7 @@ void TorrentsController::filesAction()
|
|||
fileDict[KEY_FILE_NAME] = Utils::Fs::toNativePath(fileName);
|
||||
|
||||
const BitTorrent::TorrentInfo::PieceRange idx = info.filePieces(i);
|
||||
fileDict[KEY_FILE_PIECE_RANGE] = QVariantList {idx.first(), idx.last()};
|
||||
fileDict[KEY_FILE_PIECE_RANGE] = QJsonArray {idx.first(), idx.last()};
|
||||
|
||||
if (i == 0)
|
||||
fileDict[KEY_FILE_IS_SEED] = torrent->isSeed();
|
||||
|
@ -473,7 +473,7 @@ void TorrentsController::filesAction()
|
|||
}
|
||||
}
|
||||
|
||||
setResult(QJsonArray::fromVariantList(fileList));
|
||||
setResult(fileList);
|
||||
}
|
||||
|
||||
// Returns an array of hashes (of each pieces respectively) for a torrent in JSON format.
|
||||
|
@ -483,17 +483,16 @@ void TorrentsController::pieceHashesAction()
|
|||
checkParams({"hash"});
|
||||
|
||||
const QString hash {params()["hash"]};
|
||||
QVariantList pieceHashes;
|
||||
QJsonArray pieceHashes;
|
||||
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
|
||||
if (!torrent)
|
||||
throw APIError(APIErrorType::NotFound);
|
||||
|
||||
const QVector<QByteArray> hashes = torrent->info().pieceHashes();
|
||||
pieceHashes.reserve(hashes.size());
|
||||
for (const QByteArray &hash : hashes)
|
||||
pieceHashes.append(hash.toHex());
|
||||
pieceHashes.append(QString(hash.toHex()));
|
||||
|
||||
setResult(QJsonArray::fromVariantList(pieceHashes));
|
||||
setResult(pieceHashes);
|
||||
}
|
||||
|
||||
// Returns an array of states (of each pieces respectively) for a torrent in JSON format.
|
||||
|
@ -506,13 +505,12 @@ void TorrentsController::pieceStatesAction()
|
|||
checkParams({"hash"});
|
||||
|
||||
const QString hash {params()["hash"]};
|
||||
QVariantList pieceStates;
|
||||
QJsonArray pieceStates;
|
||||
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
|
||||
if (!torrent)
|
||||
throw APIError(APIErrorType::NotFound);
|
||||
|
||||
const QBitArray states = torrent->pieces();
|
||||
pieceStates.reserve(states.size());
|
||||
for (int i = 0; i < states.size(); ++i)
|
||||
pieceStates.append(static_cast<int>(states[i]) * 2);
|
||||
|
||||
|
@ -522,7 +520,7 @@ void TorrentsController::pieceStatesAction()
|
|||
pieceStates[i] = 1;
|
||||
}
|
||||
|
||||
setResult(QJsonArray::fromVariantList(pieceStates));
|
||||
setResult(pieceStates);
|
||||
}
|
||||
|
||||
void TorrentsController::addAction()
|
||||
|
@ -743,7 +741,7 @@ void TorrentsController::uploadLimitAction()
|
|||
checkParams({"hashes"});
|
||||
|
||||
const QStringList hashes {params()["hashes"].split('|')};
|
||||
QVariantHash map;
|
||||
QJsonObject map;
|
||||
for (const QString &hash : hashes) {
|
||||
int limit = -1;
|
||||
const BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
|
||||
|
@ -752,7 +750,7 @@ void TorrentsController::uploadLimitAction()
|
|||
map[hash] = limit;
|
||||
}
|
||||
|
||||
setResult(QJsonObject::fromVariantHash(map));
|
||||
setResult(map);
|
||||
}
|
||||
|
||||
void TorrentsController::downloadLimitAction()
|
||||
|
@ -760,7 +758,7 @@ void TorrentsController::downloadLimitAction()
|
|||
checkParams({"hashes"});
|
||||
|
||||
const QStringList hashes {params()["hashes"].split('|')};
|
||||
QVariantHash map;
|
||||
QJsonObject map;
|
||||
for (const QString &hash : hashes) {
|
||||
int limit = -1;
|
||||
const BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
|
||||
|
@ -769,7 +767,7 @@ void TorrentsController::downloadLimitAction()
|
|||
map[hash] = limit;
|
||||
}
|
||||
|
||||
setResult(QJsonObject::fromVariantHash(map));
|
||||
setResult(map);
|
||||
}
|
||||
|
||||
void TorrentsController::setUploadLimitAction()
|
||||
|
@ -1103,6 +1101,8 @@ void TorrentsController::deleteTagsAction()
|
|||
|
||||
void TorrentsController::tagsAction()
|
||||
{
|
||||
const QStringList tags = BitTorrent::Session::instance()->tags().toList();
|
||||
setResult(QJsonArray::fromStringList(tags));
|
||||
QJsonArray result;
|
||||
for (const QString &tag : asConst(BitTorrent::Session::instance()->tags()))
|
||||
result << tag;
|
||||
setResult(result);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue