From 78638a15be33d2f8e336117e23ca496841221d42 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sun, 13 Dec 2020 21:26:45 +0800 Subject: [PATCH] Migrate away from deprecated QVariant comparison operators Fortunately, serializing to JSON limits the data types to a very small subset and thus we are able to implement the comparison without much hassle. Fix up cba8d83b2195a2c323e5fc430b8f68a0aff33a6f. --- src/webui/api/torrentscontroller.cpp | 54 +++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/src/webui/api/torrentscontroller.cpp b/src/webui/api/torrentscontroller.cpp index 71b2c13da..2d18b1753 100644 --- a/src/webui/api/torrentscontroller.cpp +++ b/src/webui/api/torrentscontroller.cpp @@ -262,21 +262,59 @@ void TorrentsController::infoAction() for (const QString &hash : hashes) hashSet.insert(BitTorrent::InfoHash {hash}); + const TorrentFilter torrentFilter(filter, (hashes.isEmpty() ? TorrentFilter::AnyHash : hashSet), category); QVariantList torrentList; - TorrentFilter torrentFilter(filter, (hashes.isEmpty() ? TorrentFilter::AnyHash : hashSet), category); - for (BitTorrent::TorrentHandle *const torrent : asConst(BitTorrent::Session::instance()->torrents())) + for (const BitTorrent::TorrentHandle *torrent : asConst(BitTorrent::Session::instance()->torrents())) { if (torrentFilter.match(torrent)) torrentList.append(serialize(*torrent)); } - std::sort(torrentList.begin(), torrentList.end() - , [sortedColumn, reverse](const QVariant &torrent1, const QVariant &torrent2) + if (torrentList.isEmpty()) { - return reverse - ? (torrent1.toMap().value(sortedColumn) > torrent2.toMap().value(sortedColumn)) - : (torrent1.toMap().value(sortedColumn) < torrent2.toMap().value(sortedColumn)); - }); + setResult(QJsonObject {}); + return; + } + + if (!sortedColumn.isEmpty()) + { + if (!torrentList[0].toMap().contains(sortedColumn)) + throw APIError(APIErrorType::BadParams, tr("'sort' parameter is invalid")); + + const auto lessThan = [](const QVariant &left, const QVariant &right) -> bool + { + Q_ASSERT(left.type() == right.type()); + + switch (static_cast(left.type())) + { + case QMetaType::Bool: + return left.value() < right.value(); + case QMetaType::Double: + return left.value() < right.value(); + case QMetaType::Float: + return left.value() < right.value(); + case QMetaType::Int: + return left.value() < right.value(); + case QMetaType::LongLong: + return left.value() < right.value(); + case QMetaType::QString: + return left.value() < right.value(); + default: + qWarning("Unhandled QVariant comparison, type: %d, name: %s", left.type() + , QMetaType::typeName(left.type())); + break; + } + return false; + }; + + std::sort(torrentList.begin(), torrentList.end() + , [reverse, &sortedColumn, &lessThan](const QVariant &torrent1, const QVariant &torrent2) + { + const QVariant value1 {torrent1.toMap().value(sortedColumn)}; + const QVariant value2 {torrent2.toMap().value(sortedColumn)}; + return reverse ? lessThan(value2, value1) : lessThan(value1, value2); + }); + } const int size = torrentList.size(); // normalize offset