Browse Source

WebUI: Implement limit/offset.

adaptive-webui-19844
Vladimir Golovnev (Glassez) 10 years ago
parent
commit
e279dcf904
  1. 16
      src/webui/btjson.cpp
  2. 2
      src/webui/btjson.h
  3. 10
      src/webui/requesthandler.cpp

16
src/webui/btjson.cpp

@ -235,7 +235,8 @@ static QVariantMap toMap(const QTorrentHandle& h)
* - "eta": Torrent ETA * - "eta": Torrent ETA
* - "state": Torrent state * - "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; QVariantList torrent_list;
@ -252,6 +253,19 @@ QByteArray btjson::getTorrents(QString filter, QString label, QString sortedColu
} }
std::sort(torrent_list.begin(), torrent_list.end(), QTorrentCompare(sortedColumn, reverse)); std::sort(torrent_list.begin(), torrent_list.end(), QTorrentCompare(sortedColumn, reverse));
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); return json::toJson(torrent_list);
} }

2
src/webui/btjson.h

@ -45,7 +45,7 @@ private:
public: 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); QString sortedColumn = "name", bool reverse = false, int limit = 0, int offset = 0);
static QByteArray getTrackersForTorrent(const QString& hash); static QByteArray getTrackersForTorrent(const QString& hash);
static QByteArray getPropertiesForTorrent(const QString& hash); static QByteArray getPropertiesForTorrent(const QString& hash);
static QByteArray getFilesForTorrent(const QString& hash); static QByteArray getFilesForTorrent(const QString& hash);

10
src/webui/requesthandler.cpp

@ -186,12 +186,20 @@ void RequestHandler::action_public_images()
printFile(path); 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() void RequestHandler::action_json_torrents()
{ {
const QStringMap& gets = request().gets; const QStringMap& gets = request().gets;
print(btjson::getTorrents( 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); ), CONTENT_TYPE_JS);
} }

Loading…
Cancel
Save