2012-05-26 21:42:44 +04:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
|
|
|
* Copyright (C) 2012, Christophe Dumez
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Contact : chris@qbittorrent.org
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "btjson.h"
|
2014-12-21 16:00:00 +03:00
|
|
|
#include "base/logger.h"
|
2015-09-25 11:10:05 +03:00
|
|
|
#include "base/utils/misc.h"
|
|
|
|
#include "base/utils/fs.h"
|
|
|
|
#include "base/preferences.h"
|
|
|
|
#include "base/bittorrent/session.h"
|
|
|
|
#include "base/bittorrent/sessionstatus.h"
|
|
|
|
#include "base/bittorrent/torrenthandle.h"
|
|
|
|
#include "base/bittorrent/trackerentry.h"
|
2015-11-12 22:19:44 +03:00
|
|
|
#include "base/bittorrent/peerinfo.h"
|
2015-09-25 11:10:05 +03:00
|
|
|
#include "base/torrentfilter.h"
|
2015-11-12 22:19:44 +03:00
|
|
|
#include "base/net/geoipmanager.h"
|
2014-06-04 10:11:38 +04:00
|
|
|
#include "jsonutils.h"
|
2012-05-26 21:42:44 +04:00
|
|
|
|
2014-10-25 15:49:57 +04:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QVariant>
|
2015-12-05 22:49:46 +03:00
|
|
|
#ifndef QBT_USES_QT5
|
2014-11-24 21:37:16 +03:00
|
|
|
#include <QMetaType>
|
|
|
|
#endif
|
2012-07-21 19:02:09 +04:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
|
2012-05-26 21:42:44 +04:00
|
|
|
#include <QElapsedTimer>
|
2012-07-21 19:02:09 +04:00
|
|
|
#endif
|
2012-05-26 21:42:44 +04:00
|
|
|
|
2012-07-21 19:02:09 +04:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
|
|
|
|
|
2012-05-26 21:42:44 +04:00
|
|
|
#define CACHED_VARIABLE(VARTYPE, VAR, DUR) \
|
2014-11-20 17:46:14 +03:00
|
|
|
static VARTYPE VAR; \
|
|
|
|
static QElapsedTimer cacheTimer; \
|
|
|
|
static bool initialized = false; \
|
|
|
|
if (initialized && !cacheTimer.hasExpired(DUR)) \
|
2014-12-08 04:40:58 +03:00
|
|
|
return json::toJson(VAR); \
|
2014-11-20 17:46:14 +03:00
|
|
|
initialized = true; \
|
|
|
|
cacheTimer.start(); \
|
|
|
|
VAR = VARTYPE()
|
2012-05-26 21:42:44 +04:00
|
|
|
|
2012-05-26 22:14:48 +04:00
|
|
|
#define CACHED_VARIABLE_FOR_HASH(VARTYPE, VAR, DUR, HASH) \
|
2014-11-20 17:46:14 +03:00
|
|
|
static VARTYPE VAR; \
|
|
|
|
static QString prev_hash; \
|
|
|
|
static QElapsedTimer cacheTimer; \
|
|
|
|
if (prev_hash == HASH && !cacheTimer.hasExpired(DUR)) \
|
2014-12-08 04:40:58 +03:00
|
|
|
return json::toJson(VAR); \
|
2014-11-20 17:46:14 +03:00
|
|
|
prev_hash = HASH; \
|
|
|
|
cacheTimer.start(); \
|
|
|
|
VAR = VARTYPE()
|
2012-05-26 22:14:48 +04:00
|
|
|
|
2012-07-21 19:02:09 +04:00
|
|
|
#else
|
|
|
|
// We don't support caching for Qt < 4.7 at the moment
|
|
|
|
#define CACHED_VARIABLE(VARTYPE, VAR, DUR) \
|
2014-11-20 17:46:14 +03:00
|
|
|
VARTYPE VAR
|
2012-07-21 19:02:09 +04:00
|
|
|
|
|
|
|
#define CACHED_VARIABLE_FOR_HASH(VARTYPE, VAR, DUR, HASH) \
|
2014-11-20 17:46:14 +03:00
|
|
|
VARTYPE VAR
|
2012-07-21 19:02:09 +04:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-05-26 21:42:44 +04:00
|
|
|
// Numerical constants
|
|
|
|
static const int CACHE_DURATION_MS = 1500; // 1500ms
|
|
|
|
|
|
|
|
// Torrent keys
|
|
|
|
static const char KEY_TORRENT_HASH[] = "hash";
|
|
|
|
static const char KEY_TORRENT_NAME[] = "name";
|
|
|
|
static const char KEY_TORRENT_SIZE[] = "size";
|
|
|
|
static const char KEY_TORRENT_PROGRESS[] = "progress";
|
|
|
|
static const char KEY_TORRENT_DLSPEED[] = "dlspeed";
|
|
|
|
static const char KEY_TORRENT_UPSPEED[] = "upspeed";
|
|
|
|
static const char KEY_TORRENT_PRIORITY[] = "priority";
|
|
|
|
static const char KEY_TORRENT_SEEDS[] = "num_seeds";
|
2014-11-08 18:13:00 +03:00
|
|
|
static const char KEY_TORRENT_NUM_COMPLETE[] = "num_complete";
|
2012-05-26 21:42:44 +04:00
|
|
|
static const char KEY_TORRENT_LEECHS[] = "num_leechs";
|
2014-11-08 18:13:00 +03:00
|
|
|
static const char KEY_TORRENT_NUM_INCOMPLETE[] = "num_incomplete";
|
2012-05-26 21:42:44 +04:00
|
|
|
static const char KEY_TORRENT_RATIO[] = "ratio";
|
|
|
|
static const char KEY_TORRENT_ETA[] = "eta";
|
|
|
|
static const char KEY_TORRENT_STATE[] = "state";
|
2014-12-09 19:54:35 +03:00
|
|
|
static const char KEY_TORRENT_SEQUENTIAL_DOWNLOAD[] = "seq_dl";
|
|
|
|
static const char KEY_TORRENT_FIRST_LAST_PIECE_PRIO[] = "f_l_piece_prio";
|
2016-02-09 11:56:48 +03:00
|
|
|
static const char KEY_TORRENT_CATEGORY[] = "category";
|
2015-01-30 23:58:27 +03:00
|
|
|
static const char KEY_TORRENT_SUPER_SEEDING[] = "super_seeding";
|
2015-04-15 20:13:18 +03:00
|
|
|
static const char KEY_TORRENT_FORCE_START[] = "force_start";
|
2015-09-06 04:00:08 +03:00
|
|
|
static const char KEY_TORRENT_SAVE_PATH[] = "save_path";
|
2016-02-04 00:14:57 +03:00
|
|
|
static const char KEY_TORRENT_ADDED_ON[] = "added_on";
|
|
|
|
static const char KEY_TORRENT_COMPLETION_ON[] = "completion_on";
|
2012-05-26 21:42:44 +04:00
|
|
|
|
2015-11-12 22:19:44 +03:00
|
|
|
// Peer keys
|
|
|
|
static const char KEY_PEER_IP[] = "ip";
|
|
|
|
static const char KEY_PEER_PORT[] = "port";
|
|
|
|
static const char KEY_PEER_COUNTRY_CODE[] = "country_code";
|
|
|
|
static const char KEY_PEER_COUNTRY[] = "country";
|
|
|
|
static const char KEY_PEER_CLIENT[] = "client";
|
|
|
|
static const char KEY_PEER_PROGRESS[] = "progress";
|
|
|
|
static const char KEY_PEER_DOWN_SPEED[] = "dl_speed";
|
|
|
|
static const char KEY_PEER_UP_SPEED[] = "up_speed";
|
|
|
|
static const char KEY_PEER_TOT_DOWN[] = "downloaded";
|
|
|
|
static const char KEY_PEER_TOT_UP[] = "uploaded";
|
|
|
|
static const char KEY_PEER_CONNECTION_TYPE[] = "connection";
|
|
|
|
static const char KEY_PEER_FLAGS[] = "flags";
|
|
|
|
static const char KEY_PEER_FLAGS_DESCRIPTION[] = "flags_desc";
|
|
|
|
static const char KEY_PEER_RELEVANCE[] = "relevance";
|
|
|
|
|
2012-05-26 21:42:44 +04:00
|
|
|
// Tracker keys
|
|
|
|
static const char KEY_TRACKER_URL[] = "url";
|
|
|
|
static const char KEY_TRACKER_STATUS[] = "status";
|
|
|
|
static const char KEY_TRACKER_MSG[] = "msg";
|
|
|
|
static const char KEY_TRACKER_PEERS[] = "num_peers";
|
|
|
|
|
2015-06-12 18:52:01 +03:00
|
|
|
// Web seed keys
|
|
|
|
static const char KEY_WEBSEED_URL[] = "url";
|
|
|
|
|
2012-05-26 21:42:44 +04:00
|
|
|
// Torrent keys (Properties)
|
|
|
|
static const char KEY_PROP_TIME_ELAPSED[] = "time_elapsed";
|
2014-11-08 18:13:00 +03:00
|
|
|
static const char KEY_PROP_SEEDING_TIME[] = "seeding_time";
|
2015-07-08 16:27:33 +03:00
|
|
|
static const char KEY_PROP_ETA[] = "eta";
|
2012-05-26 21:42:44 +04:00
|
|
|
static const char KEY_PROP_CONNECT_COUNT[] = "nb_connections";
|
2014-11-08 18:13:00 +03:00
|
|
|
static const char KEY_PROP_CONNECT_COUNT_LIMIT[] = "nb_connections_limit";
|
2015-07-08 16:27:33 +03:00
|
|
|
static const char KEY_PROP_DOWNLOADED[] = "total_downloaded";
|
|
|
|
static const char KEY_PROP_DOWNLOADED_SESSION[] = "total_downloaded_session";
|
|
|
|
static const char KEY_PROP_UPLOADED[] = "total_uploaded";
|
|
|
|
static const char KEY_PROP_UPLOADED_SESSION[] = "total_uploaded_session";
|
|
|
|
static const char KEY_PROP_DL_SPEED[] = "dl_speed";
|
|
|
|
static const char KEY_PROP_DL_SPEED_AVG[] = "dl_speed_avg";
|
|
|
|
static const char KEY_PROP_UP_SPEED[] = "up_speed";
|
|
|
|
static const char KEY_PROP_UP_SPEED_AVG[] = "up_speed_avg";
|
|
|
|
static const char KEY_PROP_DL_LIMIT[] = "dl_limit";
|
|
|
|
static const char KEY_PROP_UP_LIMIT[] = "up_limit";
|
|
|
|
static const char KEY_PROP_WASTED[] = "total_wasted";
|
|
|
|
static const char KEY_PROP_SEEDS[] = "seeds";
|
|
|
|
static const char KEY_PROP_SEEDS_TOTAL[] = "seeds_total";
|
|
|
|
static const char KEY_PROP_PEERS[] = "peers";
|
|
|
|
static const char KEY_PROP_PEERS_TOTAL[] = "peers_total";
|
2012-05-26 21:42:44 +04:00
|
|
|
static const char KEY_PROP_RATIO[] = "share_ratio";
|
2015-07-08 16:27:33 +03:00
|
|
|
static const char KEY_PROP_REANNOUNCE[] = "reannounce";
|
|
|
|
static const char KEY_PROP_TOTAL_SIZE[] = "total_size";
|
|
|
|
static const char KEY_PROP_PIECES_NUM[] = "pieces_num";
|
|
|
|
static const char KEY_PROP_PIECE_SIZE[] = "piece_size";
|
|
|
|
static const char KEY_PROP_PIECES_HAVE[] = "pieces_have";
|
|
|
|
static const char KEY_PROP_CREATED_BY[] = "created_by";
|
|
|
|
static const char KEY_PROP_LAST_SEEN[] = "last_seen";
|
|
|
|
static const char KEY_PROP_ADDITION_DATE[] = "addition_date";
|
|
|
|
static const char KEY_PROP_COMPLETION_DATE[] = "completion_date";
|
|
|
|
static const char KEY_PROP_CREATION_DATE[] = "creation_date";
|
|
|
|
static const char KEY_PROP_SAVE_PATH[] = "save_path";
|
|
|
|
static const char KEY_PROP_COMMENT[] = "comment";
|
2012-05-26 21:42:44 +04:00
|
|
|
|
|
|
|
// File keys
|
|
|
|
static const char KEY_FILE_NAME[] = "name";
|
|
|
|
static const char KEY_FILE_SIZE[] = "size";
|
|
|
|
static const char KEY_FILE_PROGRESS[] = "progress";
|
|
|
|
static const char KEY_FILE_PRIORITY[] = "priority";
|
|
|
|
static const char KEY_FILE_IS_SEED[] = "is_seed";
|
|
|
|
|
2012-05-26 23:58:53 +04:00
|
|
|
// TransferInfo keys
|
2014-11-08 18:13:00 +03:00
|
|
|
static const char KEY_TRANSFER_DLSPEED[] = "dl_info_speed";
|
|
|
|
static const char KEY_TRANSFER_DLDATA[] = "dl_info_data";
|
2014-12-13 02:48:30 +03:00
|
|
|
static const char KEY_TRANSFER_DLRATELIMIT[] = "dl_rate_limit";
|
2014-11-08 18:13:00 +03:00
|
|
|
static const char KEY_TRANSFER_UPSPEED[] = "up_info_speed";
|
|
|
|
static const char KEY_TRANSFER_UPDATA[] = "up_info_data";
|
2014-12-13 02:48:30 +03:00
|
|
|
static const char KEY_TRANSFER_UPRATELIMIT[] = "up_rate_limit";
|
2014-12-14 12:00:00 +03:00
|
|
|
static const char KEY_TRANSFER_DHT_NODES[] = "dht_nodes";
|
2014-12-14 12:00:00 +03:00
|
|
|
static const char KEY_TRANSFER_CONNECTION_STATUS[] = "connection_status";
|
2012-05-26 23:58:53 +04:00
|
|
|
|
2015-01-29 16:42:04 +03:00
|
|
|
// Sync main data keys
|
|
|
|
static const char KEY_SYNC_MAINDATA_QUEUEING[] = "queueing";
|
2015-01-29 19:56:14 +03:00
|
|
|
static const char KEY_SYNC_MAINDATA_USE_ALT_SPEED_LIMITS[] = "use_alt_speed_limits";
|
2015-01-30 01:01:41 +03:00
|
|
|
static const char KEY_SYNC_MAINDATA_REFRESH_INTERVAL[] = "refresh_interval";
|
2015-01-29 16:42:04 +03:00
|
|
|
|
2015-11-12 22:19:44 +03:00
|
|
|
// Sync torrent peers keys
|
|
|
|
static const char KEY_SYNC_TORRENT_PEERS_SHOW_FLAGS[] = "show_flags";
|
|
|
|
|
2015-01-06 22:25:14 +03:00
|
|
|
static const char KEY_FULL_UPDATE[] = "full_update";
|
|
|
|
static const char KEY_RESPONSE_ID[] = "rid";
|
|
|
|
static const char KEY_SUFFIX_REMOVED[] = "_removed";
|
|
|
|
|
2014-12-21 16:00:00 +03:00
|
|
|
// Log keys
|
|
|
|
static const char KEY_LOG_ID[] = "id";
|
|
|
|
static const char KEY_LOG_TIMESTAMP[] = "timestamp";
|
|
|
|
static const char KEY_LOG_MSG_TYPE[] = "type";
|
|
|
|
static const char KEY_LOG_MSG_MESSAGE[] = "message";
|
|
|
|
static const char KEY_LOG_PEER_IP[] = "ip";
|
|
|
|
static const char KEY_LOG_PEER_BLOCKED[] = "blocked";
|
|
|
|
static const char KEY_LOG_PEER_REASON[] = "reason";
|
|
|
|
|
2015-01-06 23:04:23 +03:00
|
|
|
QVariantMap getTranserInfoMap();
|
2015-04-19 18:17:47 +03:00
|
|
|
QVariantMap toMap(BitTorrent::TorrentHandle *const torrent);
|
2015-01-06 22:25:14 +03:00
|
|
|
void processMap(QVariantMap prevData, QVariantMap data, QVariantMap &syncData);
|
2015-04-11 12:43:22 +03:00
|
|
|
void processHash(QVariantHash prevData, QVariantHash data, QVariantMap &syncData, QVariantList &removedItems);
|
2015-01-06 22:25:14 +03:00
|
|
|
void processList(QVariantList prevData, QVariantList data, QVariantList &syncData, QVariantList &removedItems);
|
|
|
|
QVariantMap generateSyncData(int acceptedResponseId, QVariantMap data, QVariantMap &lastAcceptedData, QVariantMap &lastData);
|
|
|
|
|
2014-11-24 21:37:16 +03:00
|
|
|
class QTorrentCompare
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
QTorrentCompare(QString key, bool greaterThan = false)
|
|
|
|
: key_(key)
|
|
|
|
, greaterThan_(greaterThan)
|
2015-12-05 22:49:46 +03:00
|
|
|
#ifndef QBT_USES_QT5
|
2014-11-24 21:37:16 +03:00
|
|
|
, type_(QVariant::Invalid)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(QVariant torrent1, QVariant torrent2)
|
|
|
|
{
|
2015-12-05 22:49:46 +03:00
|
|
|
#ifndef QBT_USES_QT5
|
2014-11-24 21:37:16 +03:00
|
|
|
if (type_ == QVariant::Invalid)
|
|
|
|
type_ = torrent1.toMap().value(key_).type();
|
|
|
|
|
2015-09-21 11:45:40 +03:00
|
|
|
switch (static_cast<QMetaType::Type>(type_)) {
|
|
|
|
case QMetaType::Int:
|
2014-11-24 21:37:16 +03:00
|
|
|
return greaterThan_ ? torrent1.toMap().value(key_).toInt() > torrent2.toMap().value(key_).toInt()
|
2014-12-08 04:40:58 +03:00
|
|
|
: torrent1.toMap().value(key_).toInt() < torrent2.toMap().value(key_).toInt();
|
2015-09-21 11:45:40 +03:00
|
|
|
case QMetaType::LongLong:
|
2014-11-24 21:37:16 +03:00
|
|
|
return greaterThan_ ? torrent1.toMap().value(key_).toLongLong() > torrent2.toMap().value(key_).toLongLong()
|
2014-12-08 04:40:58 +03:00
|
|
|
: torrent1.toMap().value(key_).toLongLong() < torrent2.toMap().value(key_).toLongLong();
|
2015-09-21 11:45:40 +03:00
|
|
|
case QMetaType::ULongLong:
|
2014-11-24 21:37:16 +03:00
|
|
|
return greaterThan_ ? torrent1.toMap().value(key_).toULongLong() > torrent2.toMap().value(key_).toULongLong()
|
2014-12-08 04:40:58 +03:00
|
|
|
: torrent1.toMap().value(key_).toULongLong() < torrent2.toMap().value(key_).toULongLong();
|
2014-11-24 21:37:16 +03:00
|
|
|
case QMetaType::Float:
|
|
|
|
return greaterThan_ ? torrent1.toMap().value(key_).toFloat() > torrent2.toMap().value(key_).toFloat()
|
2014-12-08 04:40:58 +03:00
|
|
|
: torrent1.toMap().value(key_).toFloat() < torrent2.toMap().value(key_).toFloat();
|
2015-09-21 11:45:40 +03:00
|
|
|
case QMetaType::Double:
|
2014-11-24 21:37:16 +03:00
|
|
|
return greaterThan_ ? torrent1.toMap().value(key_).toDouble() > torrent2.toMap().value(key_).toDouble()
|
2014-12-08 04:40:58 +03:00
|
|
|
: torrent1.toMap().value(key_).toDouble() < torrent2.toMap().value(key_).toDouble();
|
2014-11-24 21:37:16 +03:00
|
|
|
default:
|
|
|
|
return greaterThan_ ? torrent1.toMap().value(key_).toString() > torrent2.toMap().value(key_).toString()
|
2014-12-08 04:40:58 +03:00
|
|
|
: torrent1.toMap().value(key_).toString() < torrent2.toMap().value(key_).toString();
|
2014-11-24 21:37:16 +03:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
return greaterThan_ ? torrent1.toMap().value(key_) > torrent2.toMap().value(key_)
|
2014-12-08 04:40:58 +03:00
|
|
|
: torrent1.toMap().value(key_) < torrent2.toMap().value(key_);
|
2014-11-24 21:37:16 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QString key_;
|
|
|
|
bool greaterThan_;
|
2015-12-05 22:49:46 +03:00
|
|
|
#ifndef QBT_USES_QT5
|
2014-11-24 21:37:16 +03:00
|
|
|
QVariant::Type type_;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2012-05-26 21:42:44 +04:00
|
|
|
/**
|
|
|
|
* Returns all the torrents in JSON format.
|
|
|
|
*
|
|
|
|
* The return value is a JSON-formatted list of dictionaries.
|
|
|
|
* The dictionary keys are:
|
|
|
|
* - "hash": Torrent hash
|
|
|
|
* - "name": Torrent name
|
|
|
|
* - "size": Torrent size
|
|
|
|
* - "progress: Torrent progress
|
|
|
|
* - "dlspeed": Torrent download speed
|
|
|
|
* - "upspeed": Torrent upload speed
|
2014-11-08 18:13:00 +03:00
|
|
|
* - "priority": Torrent priority (-1 if queuing is disabled)
|
|
|
|
* - "num_seeds": Torrent seeds connected to
|
|
|
|
* - "num_complete": Torrent seeds in the swarm
|
|
|
|
* - "num_leechs": Torrent leechers connected to
|
|
|
|
* - "num_incomplete": Torrent leechers in the swarm
|
2012-05-26 21:42:44 +04:00
|
|
|
* - "ratio": Torrent share ratio
|
|
|
|
* - "eta": Torrent ETA
|
|
|
|
* - "state": Torrent state
|
2014-12-14 12:00:00 +03:00
|
|
|
* - "seq_dl": Torrent sequential download state
|
|
|
|
* - "f_l_piece_prio": Torrent first last piece priority state
|
2015-04-15 20:13:18 +03:00
|
|
|
* - "force_start": Torrent force start state
|
2016-02-09 11:56:48 +03:00
|
|
|
* - "category": Torrent category
|
2012-05-26 21:42:44 +04:00
|
|
|
*/
|
2016-02-09 11:56:48 +03:00
|
|
|
QByteArray btjson::getTorrents(QString filter, QString category,
|
2014-12-08 04:40:58 +03:00
|
|
|
QString sortedColumn, bool reverse, int limit, int offset)
|
2012-05-26 21:42:44 +04:00
|
|
|
{
|
2015-06-13 13:57:27 +03:00
|
|
|
QVariantList torrentList;
|
2016-02-09 11:56:48 +03:00
|
|
|
TorrentFilter torrentFilter(filter, TorrentFilter::AnyHash, category);
|
2015-04-19 18:17:47 +03:00
|
|
|
foreach (BitTorrent::TorrentHandle *const torrent, BitTorrent::Session::instance()->torrents()) {
|
|
|
|
if (torrentFilter.match(torrent))
|
2015-06-13 13:57:27 +03:00
|
|
|
torrentList.append(toMap(torrent));
|
2014-11-20 17:46:14 +03:00
|
|
|
}
|
|
|
|
|
2015-06-13 13:57:27 +03:00
|
|
|
std::sort(torrentList.begin(), torrentList.end(), QTorrentCompare(sortedColumn, reverse));
|
|
|
|
int size = torrentList.size();
|
2014-11-25 09:27:22 +03:00
|
|
|
// normalize offset
|
|
|
|
if (offset < 0)
|
2015-02-02 15:00:55 +03:00
|
|
|
offset = size + offset;
|
2014-11-25 09:27:22 +03:00
|
|
|
if ((offset >= size) || (offset < 0))
|
|
|
|
offset = 0;
|
|
|
|
// normalize limit
|
|
|
|
if (limit <= 0)
|
|
|
|
limit = -1; // unlimited
|
|
|
|
|
|
|
|
if ((limit > 0) || (offset > 0))
|
2015-06-13 13:57:27 +03:00
|
|
|
return json::toJson(torrentList.mid(offset, limit));
|
2014-11-25 09:27:22 +03:00
|
|
|
else
|
2015-06-13 13:57:27 +03:00
|
|
|
return json::toJson(torrentList);
|
2012-05-26 21:42:44 +04:00
|
|
|
}
|
|
|
|
|
2015-01-06 23:04:23 +03:00
|
|
|
/**
|
|
|
|
* The function returns the changed data from the server to synchronize with the web client.
|
|
|
|
* Return value is map in JSON format.
|
|
|
|
* Map contain the key:
|
|
|
|
* - "Rid": ID response
|
|
|
|
* Map can contain the keys:
|
|
|
|
* - "full_update": full data update flag
|
|
|
|
* - "torrents": dictionary contains information about torrents.
|
|
|
|
* - "torrents_removed": a list of hashes of removed torrents
|
2016-02-09 11:56:48 +03:00
|
|
|
* - "categories": list of categories
|
|
|
|
* - "categories_removed": list of removed categories
|
2015-01-29 16:42:04 +03:00
|
|
|
* - "server_state": map contains information about the state of the server
|
2015-01-06 23:04:23 +03:00
|
|
|
* The keys of the 'torrents' dictionary are hashes of torrents.
|
|
|
|
* Each value of the 'torrents' dictionary contains map. The map can contain following keys:
|
|
|
|
* - "name": Torrent name
|
|
|
|
* - "size": Torrent size
|
|
|
|
* - "progress: Torrent progress
|
|
|
|
* - "dlspeed": Torrent download speed
|
|
|
|
* - "upspeed": Torrent upload speed
|
|
|
|
* - "priority": Torrent priority (-1 if queuing is disabled)
|
|
|
|
* - "num_seeds": Torrent seeds connected to
|
|
|
|
* - "num_complete": Torrent seeds in the swarm
|
|
|
|
* - "num_leechs": Torrent leechers connected to
|
|
|
|
* - "num_incomplete": Torrent leechers in the swarm
|
|
|
|
* - "ratio": Torrent share ratio
|
|
|
|
* - "eta": Torrent ETA
|
|
|
|
* - "state": Torrent state
|
|
|
|
* - "seq_dl": Torrent sequential download state
|
|
|
|
* - "f_l_piece_prio": Torrent first last piece priority state
|
|
|
|
* Server state map may contain the following keys:
|
2015-05-04 02:09:30 +03:00
|
|
|
* - "connection_status": connection status
|
2015-01-06 23:04:23 +03:00
|
|
|
* - "dht_nodes": DHT nodes count
|
|
|
|
* - "dl_info_data": bytes downloaded
|
|
|
|
* - "dl_info_speed": download speed
|
2015-05-04 02:09:30 +03:00
|
|
|
* - "dl_rate_limit: download rate limit
|
2015-01-06 23:04:23 +03:00
|
|
|
* - "up_info_data: bytes uploaded
|
|
|
|
* - "up_info_speed: upload speed
|
|
|
|
* - "up_rate_limit: upload speed limit
|
2015-01-29 16:42:04 +03:00
|
|
|
* - "queueing": priority system usage flag
|
2015-01-30 01:01:41 +03:00
|
|
|
* - "refresh_interval": torrents table refresh interval
|
2015-01-06 23:04:23 +03:00
|
|
|
*/
|
|
|
|
QByteArray btjson::getSyncMainData(int acceptedResponseId, QVariantMap &lastData, QVariantMap &lastAcceptedData)
|
|
|
|
{
|
|
|
|
QVariantMap data;
|
2015-04-11 12:43:22 +03:00
|
|
|
QVariantHash torrents;
|
2015-01-06 23:04:23 +03:00
|
|
|
|
2016-05-01 11:05:52 +03:00
|
|
|
BitTorrent::Session *const session = BitTorrent::Session::instance();
|
|
|
|
|
|
|
|
foreach (BitTorrent::TorrentHandle *const torrent, session->torrents()) {
|
2015-01-06 23:04:23 +03:00
|
|
|
QVariantMap map = toMap(torrent);
|
|
|
|
map.remove(KEY_TORRENT_HASH);
|
2015-04-19 18:17:47 +03:00
|
|
|
torrents[torrent->hash()] = map;
|
2015-01-06 23:04:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
data["torrents"] = torrents;
|
|
|
|
|
2016-02-09 11:56:48 +03:00
|
|
|
QVariantList categories;
|
2016-05-01 11:05:52 +03:00
|
|
|
foreach (const QString &category, session->categories())
|
2016-02-09 11:56:48 +03:00
|
|
|
categories << category;
|
2015-01-06 23:04:23 +03:00
|
|
|
|
2016-02-09 11:56:48 +03:00
|
|
|
data["categories"] = categories;
|
2015-01-29 16:42:04 +03:00
|
|
|
|
|
|
|
QVariantMap serverState = getTranserInfoMap();
|
2016-05-01 11:05:52 +03:00
|
|
|
serverState[KEY_SYNC_MAINDATA_QUEUEING] = session->isQueueingSystemEnabled();
|
|
|
|
serverState[KEY_SYNC_MAINDATA_USE_ALT_SPEED_LIMITS] = session->isAltGlobalSpeedLimitEnabled();
|
|
|
|
serverState[KEY_SYNC_MAINDATA_REFRESH_INTERVAL] = session->refreshInterval();
|
2015-01-29 16:42:04 +03:00
|
|
|
data["server_state"] = serverState;
|
2015-01-06 23:04:23 +03:00
|
|
|
|
|
|
|
return json::toJson(generateSyncData(acceptedResponseId, data, lastAcceptedData, lastData));
|
|
|
|
}
|
|
|
|
|
2015-11-12 22:19:44 +03:00
|
|
|
QByteArray btjson::getSyncTorrentPeersData(int acceptedResponseId, QString hash, QVariantMap &lastData, QVariantMap &lastAcceptedData)
|
|
|
|
{
|
|
|
|
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
|
|
|
|
if (!torrent) {
|
|
|
|
qWarning() << Q_FUNC_INFO << "Invalid torrent " << qPrintable(hash);
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariantMap data;
|
|
|
|
QVariantHash peers;
|
|
|
|
QList<BitTorrent::PeerInfo> peersList = torrent->peers();
|
|
|
|
#ifndef DISABLE_COUNTRIES_RESOLUTION
|
|
|
|
bool resolvePeerCountries = Preferences::instance()->resolvePeerCountries();
|
|
|
|
#else
|
|
|
|
bool resolvePeerCountries = false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
data[KEY_SYNC_TORRENT_PEERS_SHOW_FLAGS] = resolvePeerCountries;
|
|
|
|
|
|
|
|
foreach (const BitTorrent::PeerInfo &pi, peersList) {
|
|
|
|
if (pi.address().ip.isNull()) continue;
|
|
|
|
QVariantMap peer;
|
|
|
|
#ifndef DISABLE_COUNTRIES_RESOLUTION
|
|
|
|
if (resolvePeerCountries) {
|
|
|
|
peer[KEY_PEER_COUNTRY_CODE] = pi.country().toLower();
|
|
|
|
peer[KEY_PEER_COUNTRY] = Net::GeoIPManager::CountryName(pi.country());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
peer[KEY_PEER_IP] = pi.address().ip.toString();
|
|
|
|
peer[KEY_PEER_PORT] = pi.address().port;
|
|
|
|
peer[KEY_PEER_CLIENT] = pi.client();
|
|
|
|
peer[KEY_PEER_PROGRESS] = pi.progress();
|
|
|
|
peer[KEY_PEER_DOWN_SPEED] = pi.payloadDownSpeed();
|
|
|
|
peer[KEY_PEER_UP_SPEED] = pi.payloadUpSpeed();
|
|
|
|
peer[KEY_PEER_TOT_DOWN] = pi.totalDownload();
|
|
|
|
peer[KEY_PEER_TOT_UP] = pi.totalUpload();
|
|
|
|
peer[KEY_PEER_CONNECTION_TYPE] = pi.connectionType();
|
|
|
|
peer[KEY_PEER_FLAGS] = pi.flags();
|
|
|
|
peer[KEY_PEER_FLAGS_DESCRIPTION] = pi.flagsDescription();
|
|
|
|
peer[KEY_PEER_RELEVANCE] = pi.relevance();
|
|
|
|
peers[pi.address().ip.toString() + ":" + QString::number(pi.address().port)] = peer;
|
|
|
|
}
|
|
|
|
|
|
|
|
data["peers"] = peers;
|
|
|
|
|
|
|
|
return json::toJson(generateSyncData(acceptedResponseId, data, lastAcceptedData, lastData));
|
|
|
|
}
|
|
|
|
|
2012-05-26 21:42:44 +04:00
|
|
|
/**
|
|
|
|
* Returns the trackers for a torrent in JSON format.
|
|
|
|
*
|
|
|
|
* The return value is a JSON-formatted list of dictionaries.
|
|
|
|
* The dictionary keys are:
|
|
|
|
* - "url": Tracker URL
|
|
|
|
* - "status": Tracker status
|
|
|
|
* - "num_peers": Tracker peer count
|
|
|
|
* - "msg": Tracker message (last)
|
|
|
|
*/
|
2014-06-04 10:11:38 +04:00
|
|
|
QByteArray btjson::getTrackersForTorrent(const QString& hash)
|
2012-05-26 21:42:44 +04:00
|
|
|
{
|
2015-06-13 13:57:27 +03:00
|
|
|
CACHED_VARIABLE_FOR_HASH(QVariantList, trackerList, CACHE_DURATION_MS, hash);
|
2015-04-19 18:17:47 +03:00
|
|
|
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
|
|
|
|
if (!torrent) {
|
|
|
|
qWarning() << Q_FUNC_INFO << "Invalid torrent " << qPrintable(hash);
|
|
|
|
return QByteArray();
|
|
|
|
}
|
2014-11-20 17:46:14 +03:00
|
|
|
|
2015-04-19 18:17:47 +03:00
|
|
|
QHash<QString, BitTorrent::TrackerInfo> trackers_data = torrent->trackerInfos();
|
|
|
|
foreach (const BitTorrent::TrackerEntry &tracker, torrent->trackers()) {
|
2015-06-13 13:57:27 +03:00
|
|
|
QVariantMap trackerDict;
|
|
|
|
trackerDict[KEY_TRACKER_URL] = tracker.url();
|
2015-04-19 18:17:47 +03:00
|
|
|
const BitTorrent::TrackerInfo data = trackers_data.value(tracker.url());
|
|
|
|
QString status;
|
|
|
|
switch (tracker.status()) {
|
|
|
|
case BitTorrent::TrackerEntry::NotContacted:
|
|
|
|
status = tr("Not contacted yet"); break;
|
|
|
|
case BitTorrent::TrackerEntry::Updating:
|
|
|
|
status = tr("Updating..."); break;
|
|
|
|
case BitTorrent::TrackerEntry::Working:
|
|
|
|
status = tr("Working"); break;
|
|
|
|
case BitTorrent::TrackerEntry::NotWorking:
|
|
|
|
status = tr("Not working"); break;
|
2014-11-20 17:46:14 +03:00
|
|
|
}
|
2015-06-13 13:57:27 +03:00
|
|
|
trackerDict[KEY_TRACKER_STATUS] = status;
|
|
|
|
trackerDict[KEY_TRACKER_PEERS] = data.numPeers;
|
|
|
|
trackerDict[KEY_TRACKER_MSG] = data.lastMessage.trimmed();
|
2015-04-19 18:17:47 +03:00
|
|
|
|
2015-06-13 13:57:27 +03:00
|
|
|
trackerList.append(trackerDict);
|
2012-05-26 21:42:44 +04:00
|
|
|
}
|
|
|
|
|
2015-06-13 13:57:27 +03:00
|
|
|
return json::toJson(trackerList);
|
2012-05-26 21:42:44 +04:00
|
|
|
}
|
|
|
|
|
2015-06-12 18:52:01 +03:00
|
|
|
/**
|
|
|
|
* Returns the web seeds for a torrent in JSON format.
|
|
|
|
*
|
|
|
|
* The return value is a JSON-formatted list of dictionaries.
|
|
|
|
* The dictionary keys are:
|
|
|
|
* - "url": Web seed URL
|
|
|
|
*/
|
|
|
|
QByteArray btjson::getWebSeedsForTorrent(const QString& hash)
|
|
|
|
{
|
|
|
|
CACHED_VARIABLE_FOR_HASH(QVariantList, webSeedList, CACHE_DURATION_MS, hash);
|
|
|
|
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
|
|
|
|
if (!torrent) {
|
|
|
|
qWarning() << Q_FUNC_INFO << "Invalid torrent " << qPrintable(hash);
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (const QUrl &webseed, torrent->urlSeeds()) {
|
|
|
|
QVariantMap webSeedDict;
|
|
|
|
webSeedDict[KEY_WEBSEED_URL] = webseed.toString();
|
|
|
|
webSeedList.append(webSeedDict);
|
|
|
|
}
|
|
|
|
|
|
|
|
return json::toJson(webSeedList);
|
|
|
|
}
|
|
|
|
|
2012-05-26 21:42:44 +04:00
|
|
|
/**
|
|
|
|
* Returns the properties for a torrent in JSON format.
|
|
|
|
*
|
|
|
|
* The return value is a JSON-formatted dictionary.
|
|
|
|
* The dictionary keys are:
|
|
|
|
* - "time_elapsed": Torrent elapsed time
|
2014-11-08 18:13:00 +03:00
|
|
|
* - "seeding_time": Torrent elapsed time while complete
|
2015-07-08 16:27:33 +03:00
|
|
|
* - "eta": Torrent ETA
|
2012-05-26 21:42:44 +04:00
|
|
|
* - "nb_connections": Torrent connection count
|
2014-11-08 18:13:00 +03:00
|
|
|
* - "nb_connections_limit": Torrent connection count limit
|
2015-07-08 16:27:33 +03:00
|
|
|
* - "total_downloaded": Total data uploaded for torrent
|
|
|
|
* - "total_downloaded_session": Total data downloaded this session
|
|
|
|
* - "total_uploaded": Total data uploaded for torrent
|
|
|
|
* - "total_uploaded_session": Total data uploaded this session
|
|
|
|
* - "dl_speed": Torrent download speed
|
|
|
|
* - "dl_speed_avg": Torrent average download speed
|
|
|
|
* - "up_speed": Torrent upload speed
|
|
|
|
* - "up_speed_avg": Torrent average upload speed
|
|
|
|
* - "dl_limit": Torrent download limit
|
|
|
|
* - "up_limit": Torrent upload limit
|
|
|
|
* - "total_wasted": Total data wasted for torrent
|
|
|
|
* - "seeds": Torrent connected seeds
|
|
|
|
* - "seeds_total": Torrent total number of seeds
|
|
|
|
* - "peers": Torrent connected peers
|
|
|
|
* - "peers_total": Torrent total number of peers
|
2012-05-26 21:42:44 +04:00
|
|
|
* - "share_ratio": Torrent share ratio
|
2015-07-08 16:27:33 +03:00
|
|
|
* - "reannounce": Torrent next reannounce time
|
|
|
|
* - "total_size": Torrent total size
|
|
|
|
* - "pieces_num": Torrent pieces count
|
|
|
|
* - "piece_size": Torrent piece size
|
|
|
|
* - "pieces_have": Torrent pieces have
|
|
|
|
* - "created_by": Torrent creator
|
|
|
|
* - "last_seen": Torrent last seen complete
|
|
|
|
* - "addition_date": Torrent addition date
|
|
|
|
* - "completion_date": Torrent completion date
|
|
|
|
* - "creation_date": Torrent creation date
|
|
|
|
* - "save_path": Torrent save path
|
|
|
|
* - "comment": Torrent comment
|
2012-05-26 21:42:44 +04:00
|
|
|
*/
|
2014-06-04 10:11:38 +04:00
|
|
|
QByteArray btjson::getPropertiesForTorrent(const QString& hash)
|
2012-05-26 21:42:44 +04:00
|
|
|
{
|
2015-06-13 13:57:27 +03:00
|
|
|
CACHED_VARIABLE_FOR_HASH(QVariantMap, dataDict, CACHE_DURATION_MS, hash);
|
2015-04-19 18:17:47 +03:00
|
|
|
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
|
|
|
|
if (!torrent) {
|
|
|
|
qWarning() << Q_FUNC_INFO << "Invalid torrent " << qPrintable(hash);
|
2014-11-20 17:46:14 +03:00
|
|
|
return QByteArray();
|
|
|
|
}
|
2012-05-26 21:42:44 +04:00
|
|
|
|
2015-06-13 13:57:27 +03:00
|
|
|
dataDict[KEY_PROP_TIME_ELAPSED] = torrent->activeTime();
|
|
|
|
dataDict[KEY_PROP_SEEDING_TIME] = torrent->seedingTime();
|
2015-07-08 16:27:33 +03:00
|
|
|
dataDict[KEY_PROP_ETA] = torrent->eta();
|
2015-06-13 13:57:27 +03:00
|
|
|
dataDict[KEY_PROP_CONNECT_COUNT] = torrent->connectionsCount();
|
|
|
|
dataDict[KEY_PROP_CONNECT_COUNT_LIMIT] = torrent->connectionsLimit();
|
2015-07-08 16:27:33 +03:00
|
|
|
dataDict[KEY_PROP_DOWNLOADED] = torrent->totalDownload();
|
|
|
|
dataDict[KEY_PROP_DOWNLOADED_SESSION] = torrent->totalPayloadDownload();
|
|
|
|
dataDict[KEY_PROP_UPLOADED] = torrent->totalUpload();
|
|
|
|
dataDict[KEY_PROP_UPLOADED_SESSION] = torrent->totalPayloadUpload();
|
|
|
|
dataDict[KEY_PROP_DL_SPEED] = torrent->downloadPayloadRate();
|
|
|
|
dataDict[KEY_PROP_DL_SPEED_AVG] = torrent->totalDownload() / (1 + torrent->activeTime() - torrent->finishedTime());
|
|
|
|
dataDict[KEY_PROP_UP_SPEED] = torrent->uploadPayloadRate();
|
|
|
|
dataDict[KEY_PROP_UP_SPEED_AVG] = torrent->totalUpload() / (1 + torrent->activeTime());
|
|
|
|
dataDict[KEY_PROP_DL_LIMIT] = torrent->downloadLimit() <= 0 ? -1 : torrent->downloadLimit();
|
|
|
|
dataDict[KEY_PROP_UP_LIMIT] = torrent->uploadLimit() <= 0 ? -1 : torrent->uploadLimit();
|
|
|
|
dataDict[KEY_PROP_WASTED] = torrent->wastedSize();
|
|
|
|
dataDict[KEY_PROP_SEEDS] = torrent->seedsCount();
|
|
|
|
dataDict[KEY_PROP_SEEDS_TOTAL] = torrent->totalSeedsCount();
|
|
|
|
dataDict[KEY_PROP_PEERS] = torrent->leechsCount();
|
|
|
|
dataDict[KEY_PROP_PEERS_TOTAL] = torrent->totalLeechersCount();
|
2015-04-19 18:17:47 +03:00
|
|
|
const qreal ratio = torrent->realRatio();
|
2015-06-13 13:57:27 +03:00
|
|
|
dataDict[KEY_PROP_RATIO] = ratio > BitTorrent::TorrentHandle::MAX_RATIO ? -1 : ratio;
|
2015-07-08 16:27:33 +03:00
|
|
|
dataDict[KEY_PROP_REANNOUNCE] = torrent->nextAnnounce();
|
|
|
|
dataDict[KEY_PROP_TOTAL_SIZE] = torrent->totalSize();
|
|
|
|
dataDict[KEY_PROP_PIECES_NUM] = torrent->piecesCount();
|
|
|
|
dataDict[KEY_PROP_PIECE_SIZE] = torrent->pieceLength();
|
|
|
|
dataDict[KEY_PROP_PIECES_HAVE] = torrent->piecesHave();
|
|
|
|
dataDict[KEY_PROP_CREATED_BY] = torrent->creator();
|
|
|
|
dataDict[KEY_PROP_ADDITION_DATE] = torrent->addedTime().toTime_t();
|
|
|
|
if (torrent->hasMetadata()) {
|
|
|
|
dataDict[KEY_PROP_LAST_SEEN] = torrent->lastSeenComplete().isValid() ? (int)torrent->lastSeenComplete().toTime_t() : -1;
|
|
|
|
dataDict[KEY_PROP_COMPLETION_DATE] = torrent->completedTime().isValid() ? (int)torrent->completedTime().toTime_t() : -1;
|
|
|
|
dataDict[KEY_PROP_CREATION_DATE] = torrent->creationDate().toTime_t();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dataDict[KEY_PROP_LAST_SEEN] = -1;
|
|
|
|
dataDict[KEY_PROP_COMPLETION_DATE] = -1;
|
|
|
|
dataDict[KEY_PROP_CREATION_DATE] = -1;
|
|
|
|
}
|
2015-10-24 15:28:29 +03:00
|
|
|
dataDict[KEY_PROP_SAVE_PATH] = Utils::Fs::toNativePath(torrent->savePath());
|
2015-07-08 16:27:33 +03:00
|
|
|
dataDict[KEY_PROP_COMMENT] = torrent->comment();
|
2015-04-19 18:17:47 +03:00
|
|
|
|
2015-06-13 13:57:27 +03:00
|
|
|
return json::toJson(dataDict);
|
2012-05-26 21:42:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the files in a torrent in JSON format.
|
|
|
|
*
|
|
|
|
* The return value is a JSON-formatted list of dictionaries.
|
|
|
|
* The dictionary keys are:
|
|
|
|
* - "name": File name
|
|
|
|
* - "size": File size
|
|
|
|
* - "progress": File progress
|
|
|
|
* - "priority": File priority
|
|
|
|
* - "is_seed": Flag indicating if torrent is seeding/complete
|
|
|
|
*/
|
2014-06-04 10:11:38 +04:00
|
|
|
QByteArray btjson::getFilesForTorrent(const QString& hash)
|
2012-05-26 21:42:44 +04:00
|
|
|
{
|
2015-06-13 13:57:27 +03:00
|
|
|
CACHED_VARIABLE_FOR_HASH(QVariantList, fileList, CACHE_DURATION_MS, hash);
|
2015-04-19 18:17:47 +03:00
|
|
|
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
|
|
|
|
if (!torrent) {
|
|
|
|
qWarning() << Q_FUNC_INFO << "Invalid torrent " << qPrintable(hash);
|
|
|
|
return QByteArray();
|
2014-11-20 17:46:14 +03:00
|
|
|
}
|
2015-04-19 18:17:47 +03:00
|
|
|
|
|
|
|
if (!torrent->hasMetadata())
|
2015-07-09 19:11:04 +03:00
|
|
|
return json::toJson(fileList);
|
2015-04-19 18:17:47 +03:00
|
|
|
|
|
|
|
const QVector<int> priorities = torrent->filePriorities();
|
|
|
|
QVector<qreal> fp = torrent->filesProgress();
|
|
|
|
for (int i = 0; i < torrent->filesCount(); ++i) {
|
2015-06-13 13:57:27 +03:00
|
|
|
QVariantMap fileDict;
|
2015-04-19 18:17:47 +03:00
|
|
|
QString fileName = torrent->filePath(i);
|
|
|
|
if (fileName.endsWith(".!qB", Qt::CaseInsensitive))
|
|
|
|
fileName.chop(4);
|
2015-06-13 13:57:27 +03:00
|
|
|
fileDict[KEY_FILE_NAME] = Utils::Fs::toNativePath(fileName);
|
2015-04-19 18:17:47 +03:00
|
|
|
const qlonglong size = torrent->fileSize(i);
|
2015-06-13 13:57:27 +03:00
|
|
|
fileDict[KEY_FILE_SIZE] = size;
|
|
|
|
fileDict[KEY_FILE_PROGRESS] = fp[i];
|
|
|
|
fileDict[KEY_FILE_PRIORITY] = priorities[i];
|
2015-04-19 18:17:47 +03:00
|
|
|
if (i == 0)
|
2015-06-13 13:57:27 +03:00
|
|
|
fileDict[KEY_FILE_IS_SEED] = torrent->isSeed();
|
2015-04-19 18:17:47 +03:00
|
|
|
|
2015-06-13 13:57:27 +03:00
|
|
|
fileList.append(fileDict);
|
2012-05-26 21:42:44 +04:00
|
|
|
}
|
|
|
|
|
2015-06-13 13:57:27 +03:00
|
|
|
return json::toJson(fileList);
|
2012-05-26 21:42:44 +04:00
|
|
|
}
|
2012-05-26 23:58:53 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the global transfer information in JSON format.
|
|
|
|
*
|
|
|
|
* The return value is a JSON-formatted dictionary.
|
|
|
|
* The dictionary keys are:
|
2014-11-08 18:13:00 +03:00
|
|
|
* - "dl_info_speed": Global download rate
|
|
|
|
* - "dl_info_data": Data downloaded this session
|
|
|
|
* - "up_info_speed": Global upload rate
|
|
|
|
* - "up_info_data": Data uploaded this session
|
2014-12-14 12:00:00 +03:00
|
|
|
* - "dl_rate_limit": Download rate limit
|
|
|
|
* - "up_rate_limit": Upload rate limit
|
2014-12-14 12:00:00 +03:00
|
|
|
* - "dht_nodes": DHT nodes connected to
|
2014-12-14 12:00:00 +03:00
|
|
|
* - "connection_status": Connection status
|
2012-05-26 23:58:53 +04:00
|
|
|
*/
|
2014-06-04 10:11:38 +04:00
|
|
|
QByteArray btjson::getTransferInfo()
|
2012-05-26 23:58:53 +04:00
|
|
|
{
|
2015-01-06 23:04:23 +03:00
|
|
|
return json::toJson(getTranserInfoMap());
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariantMap getTranserInfoMap()
|
|
|
|
{
|
|
|
|
QVariantMap map;
|
2015-04-19 18:17:47 +03:00
|
|
|
BitTorrent::SessionStatus sessionStatus = BitTorrent::Session::instance()->status();
|
|
|
|
map[KEY_TRANSFER_DLSPEED] = sessionStatus.payloadDownloadRate();
|
|
|
|
map[KEY_TRANSFER_DLDATA] = sessionStatus.totalPayloadDownload();
|
|
|
|
map[KEY_TRANSFER_UPSPEED] = sessionStatus.payloadUploadRate();
|
|
|
|
map[KEY_TRANSFER_UPDATA] = sessionStatus.totalPayloadUpload();
|
2016-05-01 11:05:52 +03:00
|
|
|
map[KEY_TRANSFER_DLRATELIMIT] = BitTorrent::Session::instance()->downloadSpeedLimit();
|
|
|
|
map[KEY_TRANSFER_UPRATELIMIT] = BitTorrent::Session::instance()->uploadSpeedLimit();
|
2015-04-19 18:17:47 +03:00
|
|
|
map[KEY_TRANSFER_DHT_NODES] = sessionStatus.dhtNodes();
|
|
|
|
if (!BitTorrent::Session::instance()->isListening())
|
2015-01-06 23:04:23 +03:00
|
|
|
map[KEY_TRANSFER_CONNECTION_STATUS] = "disconnected";
|
2014-12-14 12:00:00 +03:00
|
|
|
else
|
2015-04-19 18:17:47 +03:00
|
|
|
map[KEY_TRANSFER_CONNECTION_STATUS] = sessionStatus.hasIncomingConnections() ? "connected" : "firewalled";
|
2015-01-06 23:04:23 +03:00
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2015-02-01 20:08:45 +03:00
|
|
|
QByteArray btjson::getTorrentsRatesLimits(QStringList &hashes, bool downloadLimits)
|
|
|
|
{
|
|
|
|
QVariantMap map;
|
|
|
|
|
|
|
|
foreach (const QString &hash, hashes) {
|
|
|
|
int limit = -1;
|
2015-04-19 18:17:47 +03:00
|
|
|
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
|
|
|
|
if (torrent)
|
|
|
|
limit = downloadLimits ? torrent->downloadLimit() : torrent->uploadLimit();
|
2015-02-01 20:08:45 +03:00
|
|
|
map[hash] = limit;
|
|
|
|
}
|
|
|
|
|
2015-11-12 22:19:44 +03:00
|
|
|
return json::toJson(map);
|
2015-02-01 20:08:45 +03:00
|
|
|
}
|
|
|
|
|
2015-04-19 18:17:47 +03:00
|
|
|
QVariantMap toMap(BitTorrent::TorrentHandle *const torrent)
|
2015-01-06 23:04:23 +03:00
|
|
|
{
|
|
|
|
QVariantMap ret;
|
2015-04-19 18:17:47 +03:00
|
|
|
ret[KEY_TORRENT_HASH] = QString(torrent->hash());
|
|
|
|
ret[KEY_TORRENT_NAME] = torrent->name();
|
|
|
|
ret[KEY_TORRENT_SIZE] = torrent->wantedSize();
|
|
|
|
ret[KEY_TORRENT_PROGRESS] = torrent->progress();
|
|
|
|
ret[KEY_TORRENT_DLSPEED] = torrent->downloadPayloadRate();
|
|
|
|
ret[KEY_TORRENT_UPSPEED] = torrent->uploadPayloadRate();
|
|
|
|
ret[KEY_TORRENT_PRIORITY] = torrent->queuePosition();
|
|
|
|
ret[KEY_TORRENT_SEEDS] = torrent->seedsCount();
|
2016-12-27 14:36:08 +03:00
|
|
|
ret[KEY_TORRENT_NUM_COMPLETE] = torrent->totalSeedsCount();
|
2015-04-19 18:17:47 +03:00
|
|
|
ret[KEY_TORRENT_LEECHS] = torrent->leechsCount();
|
2016-12-27 14:36:08 +03:00
|
|
|
ret[KEY_TORRENT_NUM_INCOMPLETE] = torrent->totalLeechersCount();
|
2015-04-19 18:17:47 +03:00
|
|
|
const qreal ratio = torrent->realRatio();
|
|
|
|
ret[KEY_TORRENT_RATIO] = (ratio > BitTorrent::TorrentHandle::MAX_RATIO) ? -1 : ratio;
|
|
|
|
ret[KEY_TORRENT_STATE] = torrent->state().toString();
|
|
|
|
ret[KEY_TORRENT_ETA] = torrent->eta();
|
|
|
|
ret[KEY_TORRENT_SEQUENTIAL_DOWNLOAD] = torrent->isSequentialDownload();
|
|
|
|
if (torrent->hasMetadata())
|
|
|
|
ret[KEY_TORRENT_FIRST_LAST_PIECE_PRIO] = torrent->hasFirstLastPiecePriority();
|
2016-02-09 11:56:48 +03:00
|
|
|
ret[KEY_TORRENT_CATEGORY] = torrent->category();
|
2015-04-19 18:17:47 +03:00
|
|
|
ret[KEY_TORRENT_SUPER_SEEDING] = torrent->superSeeding();
|
|
|
|
ret[KEY_TORRENT_FORCE_START] = torrent->isForced();
|
2015-10-24 15:28:29 +03:00
|
|
|
ret[KEY_TORRENT_SAVE_PATH] = Utils::Fs::toNativePath(torrent->savePath());
|
2016-04-28 01:51:20 +03:00
|
|
|
ret[KEY_TORRENT_ADDED_ON] = torrent->addedTime().toTime_t();
|
|
|
|
ret[KEY_TORRENT_COMPLETION_ON] = torrent->completedTime().toTime_t();
|
2015-01-06 23:04:23 +03:00
|
|
|
|
|
|
|
return ret;
|
2012-05-26 23:58:53 +04:00
|
|
|
}
|
2015-01-06 22:25:14 +03:00
|
|
|
|
|
|
|
// Compare two structures (prevData, data) and calculate difference (syncData).
|
|
|
|
// Structures encoded as map.
|
|
|
|
void processMap(QVariantMap prevData, QVariantMap data, QVariantMap &syncData)
|
|
|
|
{
|
|
|
|
// initialize output variable
|
|
|
|
syncData.clear();
|
|
|
|
|
|
|
|
QVariantList removedItems;
|
|
|
|
foreach (QString key, data.keys()) {
|
|
|
|
removedItems.clear();
|
|
|
|
|
2015-09-21 11:45:40 +03:00
|
|
|
switch (static_cast<QMetaType::Type>(data[key].type())) {
|
|
|
|
case QMetaType::QVariantMap: {
|
2015-01-06 22:25:14 +03:00
|
|
|
QVariantMap map;
|
2015-04-11 12:43:22 +03:00
|
|
|
processMap(prevData[key].toMap(), data[key].toMap(), map);
|
|
|
|
if (!map.isEmpty())
|
|
|
|
syncData[key] = map;
|
|
|
|
}
|
|
|
|
break;
|
2015-09-21 11:45:40 +03:00
|
|
|
case QMetaType::QVariantHash: {
|
2015-04-11 12:43:22 +03:00
|
|
|
QVariantMap map;
|
|
|
|
processHash(prevData[key].toHash(), data[key].toHash(), map, removedItems);
|
2015-01-30 17:04:25 +03:00
|
|
|
if (!map.isEmpty())
|
|
|
|
syncData[key] = map;
|
2015-01-06 22:25:14 +03:00
|
|
|
if (!removedItems.isEmpty())
|
|
|
|
syncData[key + KEY_SUFFIX_REMOVED] = removedItems;
|
|
|
|
}
|
|
|
|
break;
|
2015-09-21 11:45:40 +03:00
|
|
|
case QMetaType::QVariantList: {
|
2015-01-06 22:25:14 +03:00
|
|
|
QVariantList list;
|
|
|
|
processList(prevData[key].toList(), data[key].toList(), list, removedItems);
|
|
|
|
if (!list.isEmpty())
|
|
|
|
syncData[key] = list;
|
|
|
|
if (!removedItems.isEmpty())
|
|
|
|
syncData[key + KEY_SUFFIX_REMOVED] = removedItems;
|
|
|
|
}
|
|
|
|
break;
|
2015-09-21 11:45:40 +03:00
|
|
|
case QMetaType::QString:
|
|
|
|
case QMetaType::LongLong:
|
2015-01-06 22:25:14 +03:00
|
|
|
case QMetaType::Float:
|
2015-09-21 11:45:40 +03:00
|
|
|
case QMetaType::Int:
|
|
|
|
case QMetaType::Bool:
|
|
|
|
case QMetaType::Double:
|
|
|
|
case QMetaType::ULongLong:
|
|
|
|
case QMetaType::UInt:
|
2016-02-09 11:56:48 +03:00
|
|
|
case QMetaType::QDateTime:
|
2015-01-06 22:25:14 +03:00
|
|
|
if (prevData[key] != data[key])
|
|
|
|
syncData[key] = data[key];
|
|
|
|
break;
|
|
|
|
default:
|
2016-06-20 08:49:15 +03:00
|
|
|
Q_ASSERT_X(false, "processMap"
|
|
|
|
, QString("Unexpected type: %1")
|
|
|
|
.arg(QMetaType::typeName(static_cast<QMetaType::Type>(data[key].type())))
|
|
|
|
.toUtf8().constData());
|
2015-01-06 22:25:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare two lists of structures (prevData, data) and calculate difference (syncData, removedItems).
|
|
|
|
// Structures encoded as map.
|
|
|
|
// Lists are encoded as hash table (indexed by structure key value) to improve ease of searching for removed items.
|
2015-04-11 12:43:22 +03:00
|
|
|
void processHash(QVariantHash prevData, QVariantHash data, QVariantMap &syncData, QVariantList &removedItems)
|
2015-01-06 22:25:14 +03:00
|
|
|
{
|
|
|
|
// initialize output variables
|
|
|
|
syncData.clear();
|
|
|
|
removedItems.clear();
|
|
|
|
|
|
|
|
if (prevData.isEmpty()) {
|
|
|
|
// If list was empty before, then difference is a whole new list.
|
2015-01-30 17:04:25 +03:00
|
|
|
foreach (QString key, data.keys())
|
|
|
|
syncData[key] = data[key];
|
2015-01-06 22:25:14 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
foreach (QString key, data.keys()) {
|
|
|
|
switch (data[key].type()) {
|
|
|
|
case QVariant::Map:
|
|
|
|
if (!prevData.contains(key)) {
|
|
|
|
// new list item found - append it to syncData
|
|
|
|
syncData[key] = data[key];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
QVariantMap map;
|
|
|
|
processMap(prevData[key].toMap(), data[key].toMap(), map);
|
|
|
|
// existing list item found - remove it from prevData
|
|
|
|
prevData.remove(key);
|
|
|
|
if (!map.isEmpty())
|
|
|
|
// changed list item found - append its changes to syncData
|
|
|
|
syncData[key] = map;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Q_ASSERT(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!prevData.isEmpty()) {
|
|
|
|
// prevData contains only items that are missing now -
|
|
|
|
// put them in removedItems
|
|
|
|
foreach (QString s, prevData.keys())
|
|
|
|
removedItems << s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare two lists of simple value (prevData, data) and calculate difference (syncData, removedItems).
|
|
|
|
void processList(QVariantList prevData, QVariantList data, QVariantList &syncData, QVariantList &removedItems)
|
|
|
|
{
|
|
|
|
// initialize output variables
|
|
|
|
syncData.clear();
|
|
|
|
removedItems.clear();
|
|
|
|
|
|
|
|
if (prevData.isEmpty()) {
|
|
|
|
// If list was empty before, then difference is a whole new list.
|
|
|
|
syncData = data;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
foreach (QVariant item, data) {
|
|
|
|
if (!prevData.contains(item))
|
|
|
|
// new list item found - append it to syncData
|
|
|
|
syncData.append(item);
|
|
|
|
else
|
|
|
|
// unchanged list item found - remove it from prevData
|
|
|
|
prevData.removeOne(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!prevData.isEmpty())
|
|
|
|
// prevData contains only items that are missing now -
|
|
|
|
// put them in removedItems
|
|
|
|
removedItems = prevData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariantMap generateSyncData(int acceptedResponseId, QVariantMap data, QVariantMap &lastAcceptedData, QVariantMap &lastData)
|
|
|
|
{
|
|
|
|
QVariantMap syncData;
|
|
|
|
bool fullUpdate = true;
|
|
|
|
int lastResponseId = 0;
|
|
|
|
if (acceptedResponseId > 0) {
|
|
|
|
lastResponseId = lastData[KEY_RESPONSE_ID].toInt();
|
|
|
|
|
|
|
|
if (lastResponseId == acceptedResponseId)
|
|
|
|
lastAcceptedData = lastData;
|
|
|
|
|
|
|
|
int lastAcceptedResponseId = lastAcceptedData[KEY_RESPONSE_ID].toInt();
|
|
|
|
|
|
|
|
if (lastAcceptedResponseId == acceptedResponseId) {
|
|
|
|
processMap(lastAcceptedData, data, syncData);
|
|
|
|
fullUpdate = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fullUpdate) {
|
|
|
|
lastAcceptedData.clear();
|
|
|
|
syncData = data;
|
2015-04-12 23:52:12 +03:00
|
|
|
|
2015-12-05 22:49:46 +03:00
|
|
|
#if (QBT_USES_QT5 && QT_VERSION < QT_VERSION_CHECK(5, 5, 0))
|
2015-04-12 23:52:12 +03:00
|
|
|
// QJsonDocument::fromVariant() supports QVariantHash only
|
|
|
|
// since Qt5.5, so manually convert data["torrents"]
|
|
|
|
QVariantMap torrentsMap;
|
|
|
|
QVariantHash torrents = data["torrents"].toHash();
|
|
|
|
foreach (const QString &key, torrents.keys())
|
|
|
|
torrentsMap[key] = torrents[key];
|
|
|
|
syncData["torrents"] = torrentsMap;
|
|
|
|
#endif
|
|
|
|
|
2015-01-06 22:25:14 +03:00
|
|
|
syncData[KEY_FULL_UPDATE] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastResponseId = lastResponseId % 1000000 + 1; // cycle between 1 and 1000000
|
|
|
|
lastData = data;
|
|
|
|
lastData[KEY_RESPONSE_ID] = lastResponseId;
|
|
|
|
syncData[KEY_RESPONSE_ID] = lastResponseId;
|
|
|
|
|
|
|
|
return syncData;
|
2015-01-06 23:04:23 +03:00
|
|
|
}
|
2014-12-21 16:00:00 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the log in JSON format.
|
|
|
|
*
|
|
|
|
* The return value is an array of dictionaries.
|
|
|
|
* The dictionary keys are:
|
|
|
|
* - "id": id of the message
|
|
|
|
* - "timestamp": milliseconds since epoch
|
|
|
|
* - "type": type of the message (int, see MsgType)
|
|
|
|
* - "message": text of the message
|
|
|
|
*/
|
|
|
|
QByteArray btjson::getLog(bool normal, bool info, bool warning, bool critical, int lastKnownId)
|
|
|
|
{
|
|
|
|
Logger* const logger = Logger::instance();
|
|
|
|
QVariantList msgList;
|
|
|
|
|
|
|
|
foreach (const Log::Msg& msg, logger->getMessages(lastKnownId)) {
|
|
|
|
if (!((msg.type == Log::NORMAL && normal)
|
|
|
|
|| (msg.type == Log::INFO && info)
|
|
|
|
|| (msg.type == Log::WARNING && warning)
|
|
|
|
|| (msg.type == Log::CRITICAL && critical)))
|
|
|
|
continue;
|
|
|
|
QVariantMap map;
|
|
|
|
map[KEY_LOG_ID] = msg.id;
|
|
|
|
map[KEY_LOG_TIMESTAMP] = msg.timestamp;
|
|
|
|
map[KEY_LOG_MSG_TYPE] = msg.type;
|
|
|
|
map[KEY_LOG_MSG_MESSAGE] = msg.message;
|
|
|
|
msgList.append(map);
|
|
|
|
}
|
|
|
|
|
|
|
|
return json::toJson(msgList);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the peer log in JSON format.
|
|
|
|
*
|
|
|
|
* The return value is an array of dictionaries.
|
|
|
|
* The dictionary keys are:
|
|
|
|
* - "id": id of the message
|
|
|
|
* - "timestamp": milliseconds since epoch
|
|
|
|
* - "ip": IP of the peer
|
|
|
|
* - "blocked": whether or not the peer was blocked
|
|
|
|
* - "reason": reason of the block
|
|
|
|
*/
|
|
|
|
QByteArray btjson::getPeerLog(int lastKnownId)
|
|
|
|
{
|
|
|
|
Logger* const logger = Logger::instance();
|
|
|
|
QVariantList peerList;
|
|
|
|
|
|
|
|
foreach (const Log::Peer& peer, logger->getPeers(lastKnownId)) {
|
|
|
|
QVariantMap map;
|
|
|
|
map[KEY_LOG_ID] = peer.id;
|
|
|
|
map[KEY_LOG_TIMESTAMP] = peer.timestamp;
|
|
|
|
map[KEY_LOG_PEER_IP] = peer.ip;
|
|
|
|
map[KEY_LOG_PEER_BLOCKED] = peer.blocked;
|
|
|
|
map[KEY_LOG_PEER_REASON] = peer.reason;
|
|
|
|
peerList.append(map);
|
|
|
|
}
|
|
|
|
|
|
|
|
return json::toJson(peerList);
|
|
|
|
}
|