WebUI: Implement server-side torrent filtering.

This commit is contained in:
Vladimir Golovnev (Glassez) 2014-11-21 12:42:46 +03:00
parent e887f574d7
commit 5edf0e251f
6 changed files with 197 additions and 6 deletions

View file

@ -32,6 +32,7 @@
#include "fs_utils.h"
#include "qbtsession.h"
#include "torrentpersistentdata.h"
#include "qtorrentfilter.h"
#include "jsonutils.h"
#include <QDebug>
@ -213,7 +214,7 @@ static QVariantMap toMap(const QTorrentHandle& h)
* - "eta": Torrent ETA
* - "state": Torrent state
*/
QByteArray btjson::getTorrents()
QByteArray btjson::getTorrents(QString filter, QString label)
{
QVariantList torrent_list;
@ -221,8 +222,12 @@ QByteArray btjson::getTorrents()
std::vector<torrent_handle>::const_iterator it = torrents.begin();
std::vector<torrent_handle>::const_iterator end = torrents.end();
QTorrentFilter torrentFilter(filter, label);
for( ; it != end; ++it) {
torrent_list.append(toMap(QTorrentHandle(*it)));
QTorrentHandle torrent = QTorrentHandle(*it);
if (torrentFilter.apply(torrent))
torrent_list.append(toMap(torrent));
}
return json::toJson(torrent_list);

View file

@ -34,6 +34,8 @@
#include <QCoreApplication>
#include <QString>
class QTorrentHandle;
class btjson
{
Q_DECLARE_TR_FUNCTIONS(misc)
@ -42,7 +44,7 @@ private:
btjson() {}
public:
static QByteArray getTorrents();
static QByteArray getTorrents(QString filter = "all", QString label = QString());
static QByteArray getTrackersForTorrent(const QString& hash);
static QByteArray getPropertiesForTorrent(const QString& hash);
static QByteArray getFilesForTorrent(const QString& hash);

View file

@ -0,0 +1,118 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2014 Vladimir Golovnev <glassez@yandex.ru>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include "torrentpersistentdata.h"
#include "qtorrentfilter.h"
QTorrentFilter::QTorrentFilter(QString filter, QString label)
: type_(All)
, label_(label)
{
if (filter == "downloading")
type_ = Downloading;
else if (filter == "completed")
type_ = Completed;
else if (filter == "paused")
type_ = Paused;
else if (filter == "active")
type_ = Active;
else if (filter == "inactive")
type_ = Inactive;
}
bool QTorrentFilter::apply(const QTorrentHandle& h) const
{
if (!torrentHasLabel(h))
return false;
switch (type_) {
case Downloading:
return isTorrentDownloading(h);
case Completed:
return isTorrentCompleted(h);
case Paused:
return isTorrentPaused(h);
case Active:
return isTorrentActive(h);
case Inactive:
return isTorrentInactive(h);
default: // All
return true;
}
}
bool QTorrentFilter::isTorrentDownloading(const QTorrentHandle &h) const
{
const QTorrentState state = h.torrentState();
return state == QTorrentState::Downloading
|| state == QTorrentState::StalledDownloading
|| state == QTorrentState::CheckingDownloading
|| state == QTorrentState::PausedDownloading
|| state == QTorrentState::QueuedDownloading;
}
bool QTorrentFilter::isTorrentCompleted(const QTorrentHandle &h) const
{
const QTorrentState state = h.torrentState();
return state == QTorrentState::Uploading
|| state == QTorrentState::StalledUploading
|| state == QTorrentState::CheckingUploading
|| state == QTorrentState::PausedUploading
|| state == QTorrentState::QueuedUploading;
}
bool QTorrentFilter::isTorrentPaused(const QTorrentHandle &h) const
{
const QTorrentState state = h.torrentState();
return state == QTorrentState::PausedDownloading
|| state == QTorrentState::PausedUploading;
}
bool QTorrentFilter::isTorrentActive(const QTorrentHandle &h) const
{
const QTorrentState state = h.torrentState();
return state == QTorrentState::Downloading
|| state == QTorrentState::Uploading;
}
bool QTorrentFilter::isTorrentInactive(const QTorrentHandle &h) const
{
return !isTorrentActive(h);
}
bool QTorrentFilter::torrentHasLabel(const QTorrentHandle &h) const
{
if (label_.isNull())
return true;
else
return TorrentPersistentData::getLabel(h.hash()) == label_;
}

View file

@ -0,0 +1,63 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2014 Vladimir Golovnev <glassez@yandex.ru>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#ifndef QTORRENTFILTER_H
#define QTORRENTFILTER_H
#include "qtorrenthandle.h"
class QTorrentFilter
{
public:
enum
{
All,
Downloading,
Completed,
Paused,
Active,
Inactive
};
// label: pass empty string for "no label" or null string (QString()) for "any label"
QTorrentFilter(QString filter, QString label = QString());
bool apply(const QTorrentHandle& h) const;
private:
int type_;
QString label_;
bool isTorrentDownloading(const QTorrentHandle &h) const;
bool isTorrentCompleted(const QTorrentHandle &h) const;
bool isTorrentPaused(const QTorrentHandle &h) const;
bool isTorrentActive(const QTorrentHandle &h) const;
bool isTorrentInactive(const QTorrentHandle &h) const;
bool torrentHasLabel(const QTorrentHandle &h) const;
};
#endif // QTORRENTFILTER_H

View file

@ -188,7 +188,8 @@ void RequestHandler::action_public_images()
void RequestHandler::action_json_torrents()
{
print(btjson::getTorrents(), CONTENT_TYPE_JS);
const QStringMap& gets = request().gets;
print(btjson::getTorrents(gets["filter"], gets["label"]), CONTENT_TYPE_JS);
}
void RequestHandler::action_json_preferences()

View file

@ -11,7 +11,8 @@ HEADERS += $$PWD/httpserver.h \
$$PWD/extra_translations.h \
$$PWD/webapplication.h \
$$PWD/abstractrequesthandler.h \
$$PWD/requesthandler.h
$$PWD/requesthandler.h \
$$PWD/qtorrentfilter.h
SOURCES += $$PWD/httpserver.cpp \
$$PWD/httpconnection.cpp \
@ -21,7 +22,8 @@ SOURCES += $$PWD/httpserver.cpp \
$$PWD/prefjson.cpp \
$$PWD/webapplication.cpp \
$$PWD/abstractrequesthandler.cpp \
$$PWD/requesthandler.cpp
$$PWD/requesthandler.cpp \
$$PWD/qtorrentfilter.cpp
# QJson JSON parser/serializer for using with Qt4
lessThan(QT_MAJOR_VERSION, 5) {