From 59ff08c107c05cff9339acb63f059be0644070b5 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Mon, 24 Nov 2014 21:37:16 +0300 Subject: [PATCH] WebUI: Implement server-side sorting. --- src/webui/btjson.cpp | 58 +++++++++++++++++++++++++++++++++++- src/webui/btjson.h | 3 +- src/webui/requesthandler.cpp | 5 +++- 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/webui/btjson.cpp b/src/webui/btjson.cpp index ac713ce84..332ed556b 100644 --- a/src/webui/btjson.cpp +++ b/src/webui/btjson.cpp @@ -37,6 +37,9 @@ #include #include +#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) +#include +#endif #if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0) #include #endif @@ -133,6 +136,58 @@ static const char KEY_TRANSFER_DLDATA[] = "dl_info_data"; static const char KEY_TRANSFER_UPSPEED[] = "up_info_speed"; static const char KEY_TRANSFER_UPDATA[] = "up_info_data"; +class QTorrentCompare +{ +public: + QTorrentCompare(QString key, bool greaterThan = false) + : key_(key) + , greaterThan_(greaterThan) +#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) + , type_(QVariant::Invalid) +#endif + { + } + + bool operator()(QVariant torrent1, QVariant torrent2) + { +#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) + if (type_ == QVariant::Invalid) + type_ = torrent1.toMap().value(key_).type(); + + switch (type_) { + case QVariant::Int: + return greaterThan_ ? torrent1.toMap().value(key_).toInt() > torrent2.toMap().value(key_).toInt() + : torrent1.toMap().value(key_).toInt() < torrent2.toMap().value(key_).toInt(); + case QVariant::LongLong: + return greaterThan_ ? torrent1.toMap().value(key_).toLongLong() > torrent2.toMap().value(key_).toLongLong() + : torrent1.toMap().value(key_).toLongLong() < torrent2.toMap().value(key_).toLongLong(); + case QVariant::ULongLong: + return greaterThan_ ? torrent1.toMap().value(key_).toULongLong() > torrent2.toMap().value(key_).toULongLong() + : torrent1.toMap().value(key_).toULongLong() < torrent2.toMap().value(key_).toULongLong(); + case QMetaType::Float: + return greaterThan_ ? torrent1.toMap().value(key_).toFloat() > torrent2.toMap().value(key_).toFloat() + : torrent1.toMap().value(key_).toFloat() < torrent2.toMap().value(key_).toFloat(); + case QVariant::Double: + return greaterThan_ ? torrent1.toMap().value(key_).toDouble() > torrent2.toMap().value(key_).toDouble() + : torrent1.toMap().value(key_).toDouble() < torrent2.toMap().value(key_).toDouble(); + default: + return greaterThan_ ? torrent1.toMap().value(key_).toString() > torrent2.toMap().value(key_).toString() + : torrent1.toMap().value(key_).toString() < torrent2.toMap().value(key_).toString(); + } +#else + return greaterThan_ ? torrent1.toMap().value(key_) > torrent2.toMap().value(key_) + : torrent1.toMap().value(key_) < torrent2.toMap().value(key_); +#endif + } + +private: + QString key_; + bool greaterThan_; +#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) + QVariant::Type type_; +#endif +}; + static QVariantMap toMap(const QTorrentHandle& h) { libtorrent::torrent_status status = h.status(torrent_handle::query_accurate_download_counters); @@ -180,7 +235,7 @@ static QVariantMap toMap(const QTorrentHandle& h) * - "eta": Torrent ETA * - "state": Torrent state */ -QByteArray btjson::getTorrents(QString filter, QString label) +QByteArray btjson::getTorrents(QString filter, QString label, QString sortedColumn, bool reverse) { QVariantList torrent_list; @@ -196,6 +251,7 @@ QByteArray btjson::getTorrents(QString filter, QString label) torrent_list.append(toMap(torrent)); } + std::sort(torrent_list.begin(), torrent_list.end(), QTorrentCompare(sortedColumn, reverse)); return json::toJson(torrent_list); } diff --git a/src/webui/btjson.h b/src/webui/btjson.h index 2b328d5ff..0d1a63c56 100644 --- a/src/webui/btjson.h +++ b/src/webui/btjson.h @@ -44,7 +44,8 @@ private: btjson() {} public: - static QByteArray getTorrents(QString filter = "all", QString label = QString()); + static QByteArray getTorrents(QString filter = "all", QString label = QString(), + QString sortedColumn = "name", bool reverse = false); static QByteArray getTrackersForTorrent(const QString& hash); static QByteArray getPropertiesForTorrent(const QString& hash); static QByteArray getFilesForTorrent(const QString& hash); diff --git a/src/webui/requesthandler.cpp b/src/webui/requesthandler.cpp index c5104bad3..844fab554 100644 --- a/src/webui/requesthandler.cpp +++ b/src/webui/requesthandler.cpp @@ -189,7 +189,10 @@ void RequestHandler::action_public_images() void RequestHandler::action_json_torrents() { const QStringMap& gets = request().gets; - print(btjson::getTorrents(gets["filter"], gets["label"]), CONTENT_TYPE_JS); + + print(btjson::getTorrents( + gets["filter"], gets["label"], gets["sort"], gets["reverse"] == "true" + ), CONTENT_TYPE_JS); } void RequestHandler::action_json_preferences()