diff --git a/src/webui/btjson.cpp b/src/webui/btjson.cpp index 6756ac145..1452ef67b 100644 --- a/src/webui/btjson.cpp +++ b/src/webui/btjson.cpp @@ -32,6 +32,7 @@ #include "fs_utils.h" #include "qbtsession.h" #include "torrentpersistentdata.h" +#include "qtorrentfilter.h" #include "jsonutils.h" #include @@ -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::const_iterator it = torrents.begin(); std::vector::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); diff --git a/src/webui/btjson.h b/src/webui/btjson.h index 85be78bb4..2b328d5ff 100644 --- a/src/webui/btjson.h +++ b/src/webui/btjson.h @@ -34,6 +34,8 @@ #include #include +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); diff --git a/src/webui/qtorrentfilter.cpp b/src/webui/qtorrentfilter.cpp new file mode 100644 index 000000000..0572bf66a --- /dev/null +++ b/src/webui/qtorrentfilter.cpp @@ -0,0 +1,118 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2014 Vladimir Golovnev + * + * 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_; +} diff --git a/src/webui/qtorrentfilter.h b/src/webui/qtorrentfilter.h new file mode 100644 index 000000000..052c9ac6a --- /dev/null +++ b/src/webui/qtorrentfilter.h @@ -0,0 +1,63 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2014 Vladimir Golovnev + * + * 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 diff --git a/src/webui/requesthandler.cpp b/src/webui/requesthandler.cpp index 0bcded468..c5104bad3 100644 --- a/src/webui/requesthandler.cpp +++ b/src/webui/requesthandler.cpp @@ -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() diff --git a/src/webui/webui.pri b/src/webui/webui.pri index be4989a22..26c009443 100644 --- a/src/webui/webui.pri +++ b/src/webui/webui.pri @@ -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) {