Browse Source

WebUI: Implement limit/offset.

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

18
src/webui/btjson.cpp

@ -235,7 +235,8 @@ static QVariantMap toMap(const QTorrentHandle& h) @@ -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 @@ -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);
}
/**

2
src/webui/btjson.h

@ -45,7 +45,7 @@ private: @@ -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);

10
src/webui/requesthandler.cpp

@ -186,12 +186,20 @@ void RequestHandler::action_public_images() @@ -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);
}

Loading…
Cancel
Save