From e279dcf904507c90625d021da0ef6126577a4585 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Tue, 25 Nov 2014 09:27:22 +0300 Subject: [PATCH] WebUI: Implement limit/offset. --- src/webui/btjson.cpp | 18 ++++++++++++++++-- src/webui/btjson.h | 2 +- src/webui/requesthandler.cpp | 10 +++++++++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/webui/btjson.cpp b/src/webui/btjson.cpp index 332ed556b..4ea1483de 100644 --- a/src/webui/btjson.cpp +++ b/src/webui/btjson.cpp @@ -235,7 +235,8 @@ static QVariantMap toMap(const QTorrentHandle& h) * - "eta": Torrent ETA * - "state": Torrent state */ -QByteArray btjson::getTorrents(QString filter, QString label, QString sortedColumn, bool reverse) +QByteArray btjson::getTorrents(QString filter, QString label, + QString sortedColumn, bool reverse, int limit, int offset) { QVariantList torrent_list; @@ -252,7 +253,20 @@ QByteArray btjson::getTorrents(QString filter, QString label, QString sortedColu } std::sort(torrent_list.begin(), torrent_list.end(), QTorrentCompare(sortedColumn, reverse)); - return json::toJson(torrent_list); + int size = torrent_list.size(); + // normalize offset + if (offset < 0) + offset = size - offset; + if ((offset >= size) || (offset < 0)) + offset = 0; + // normalize limit + if (limit <= 0) + limit = -1; // unlimited + + if ((limit > 0) || (offset > 0)) + return json::toJson(torrent_list.mid(offset, limit)); + else + return json::toJson(torrent_list); } /** diff --git a/src/webui/btjson.h b/src/webui/btjson.h index 0d1a63c56..e8bf66896 100644 --- a/src/webui/btjson.h +++ b/src/webui/btjson.h @@ -45,7 +45,7 @@ private: public: static QByteArray getTorrents(QString filter = "all", QString label = QString(), - QString sortedColumn = "name", bool reverse = false); + QString sortedColumn = "name", bool reverse = false, int limit = 0, int offset = 0); 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 844fab554..092bd11e3 100644 --- a/src/webui/requesthandler.cpp +++ b/src/webui/requesthandler.cpp @@ -186,12 +186,20 @@ void RequestHandler::action_public_images() printFile(path); } +// GET params: +// - filter (string): all, downloading, completed, paused, active, inactive +// - label (string): torrent label for filtering by it (empty string means "unlabeled"; no "label" param presented means "any label") +// - sort (string): name of column for sorting by its value +// - reverse (bool): enable reverse sorting +// - limit (int): set limit number of torrents returned (if greater than 0, otherwise - unlimited) +// - offset (int): set offset (if less than 0 - offset from end) void RequestHandler::action_json_torrents() { const QStringMap& gets = request().gets; print(btjson::getTorrents( - gets["filter"], gets["label"], gets["sort"], gets["reverse"] == "true" + gets["filter"], gets["label"], gets["sort"], gets["reverse"] == "true", + gets["limit"].toInt(), gets["offset"].toInt() ), CONTENT_TYPE_JS); }