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"
|
|
|
|
#include "misc.h"
|
2013-11-10 18:29:17 +04:00
|
|
|
#include "fs_utils.h"
|
2012-05-26 21:42:44 +04:00
|
|
|
#include "qbtsession.h"
|
|
|
|
#include "torrentpersistentdata.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>
|
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
|
|
|
|
2014-10-18 03:42:36 +04:00
|
|
|
#include <libtorrent/session_status.hpp>
|
|
|
|
|
2012-05-26 21:42:44 +04:00
|
|
|
using namespace libtorrent;
|
|
|
|
|
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) \
|
|
|
|
static VARTYPE VAR; \
|
|
|
|
static QElapsedTimer cacheTimer; \
|
|
|
|
static bool initialized = false; \
|
|
|
|
if (initialized && !cacheTimer.hasExpired(DUR)) \
|
2014-06-04 10:11:38 +04:00
|
|
|
return json::toJson(VAR); \
|
2012-05-26 21:42:44 +04:00
|
|
|
initialized = true; \
|
|
|
|
cacheTimer.start(); \
|
2014-06-04 10:11:38 +04:00
|
|
|
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) \
|
|
|
|
static VARTYPE VAR; \
|
|
|
|
static QString prev_hash; \
|
|
|
|
static QElapsedTimer cacheTimer; \
|
|
|
|
if (prev_hash == HASH && !cacheTimer.hasExpired(DUR)) \
|
2014-06-04 10:11:38 +04:00
|
|
|
return json::toJson(VAR); \
|
2012-05-26 22:14:48 +04:00
|
|
|
prev_hash = HASH; \
|
|
|
|
cacheTimer.start(); \
|
2014-06-04 10:11:38 +04:00
|
|
|
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) \
|
|
|
|
VARTYPE VAR
|
|
|
|
|
|
|
|
#define CACHED_VARIABLE_FOR_HASH(VARTYPE, VAR, DUR, HASH) \
|
|
|
|
VARTYPE VAR
|
|
|
|
|
|
|
|
#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";
|
|
|
|
static const char KEY_TORRENT_LEECHS[] = "num_leechs";
|
|
|
|
static const char KEY_TORRENT_RATIO[] = "ratio";
|
|
|
|
static const char KEY_TORRENT_ETA[] = "eta";
|
|
|
|
static const char KEY_TORRENT_STATE[] = "state";
|
|
|
|
|
|
|
|
// 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";
|
|
|
|
|
|
|
|
// Torrent keys (Properties)
|
|
|
|
static const char KEY_PROP_SAVE_PATH[] = "save_path";
|
|
|
|
static const char KEY_PROP_CREATION_DATE[] = "creation_date";
|
|
|
|
static const char KEY_PROP_PIECE_SIZE[] = "piece_size";
|
|
|
|
static const char KEY_PROP_COMMENT[] = "comment";
|
|
|
|
static const char KEY_PROP_WASTED[] = "total_wasted";
|
2012-07-02 00:59:12 +04:00
|
|
|
static const char KEY_PROP_UPLOADED[] = "total_uploaded";
|
|
|
|
static const char KEY_PROP_DOWNLOADED[] = "total_downloaded";
|
2012-05-26 21:42:44 +04:00
|
|
|
static const char KEY_PROP_UP_LIMIT[] = "up_limit";
|
|
|
|
static const char KEY_PROP_DL_LIMIT[] = "dl_limit";
|
|
|
|
static const char KEY_PROP_TIME_ELAPSED[] = "time_elapsed";
|
|
|
|
static const char KEY_PROP_CONNECT_COUNT[] = "nb_connections";
|
|
|
|
static const char KEY_PROP_RATIO[] = "share_ratio";
|
|
|
|
|
|
|
|
// 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
|
|
|
|
static const char KEY_TRANSFER_DLSPEED[] = "dl_info";
|
|
|
|
static const char KEY_TRANSFER_UPSPEED[] = "up_info";
|
|
|
|
|
2014-06-04 10:11:38 +04:00
|
|
|
static QVariantMap toMap(const QTorrentHandle& h)
|
2012-05-26 21:42:44 +04:00
|
|
|
{
|
2014-05-14 02:09:45 +04:00
|
|
|
libtorrent::torrent_status status = h.status(torrent_handle::query_accurate_download_counters);
|
|
|
|
|
2014-06-04 10:11:38 +04:00
|
|
|
QVariantMap ret;
|
|
|
|
ret[KEY_TORRENT_HASH] = h.hash();
|
|
|
|
ret[KEY_TORRENT_NAME] = h.name();
|
2014-08-08 03:46:54 +04:00
|
|
|
ret[KEY_TORRENT_SIZE] = misc::friendlyUnit(status.total_wanted, false, true); // FIXME: Should pass as Number, not formatted String (for sorting).
|
2014-06-04 10:11:38 +04:00
|
|
|
ret[KEY_TORRENT_PROGRESS] = (double)h.progress(status);
|
2014-08-08 03:46:54 +04:00
|
|
|
ret[KEY_TORRENT_DLSPEED] = misc::friendlyUnit(status.download_payload_rate, true, true); // FIXME: Should be passed as a Number
|
|
|
|
ret[KEY_TORRENT_UPSPEED] = misc::friendlyUnit(status.upload_payload_rate, true, true); // FIXME: Should be passed as a Number
|
2014-06-04 21:10:37 +04:00
|
|
|
if (QBtSession::instance()->isQueueingEnabled() && h.queue_position(status) >= 0)
|
|
|
|
ret[KEY_TORRENT_PRIORITY] = QString::number(h.queue_position(status));
|
2012-05-26 21:42:44 +04:00
|
|
|
else
|
2014-06-04 10:11:38 +04:00
|
|
|
ret[KEY_TORRENT_PRIORITY] = "*";
|
2014-05-14 02:09:45 +04:00
|
|
|
QString seeds = QString::number(status.num_seeds);
|
|
|
|
if (status.num_complete > 0)
|
|
|
|
seeds += " ("+QString::number(status.num_complete)+")";
|
2014-06-04 10:11:38 +04:00
|
|
|
ret[KEY_TORRENT_SEEDS] = seeds;
|
2014-05-14 02:09:45 +04:00
|
|
|
QString leechs = QString::number(status.num_peers - status.num_seeds);
|
|
|
|
if (status.num_incomplete > 0)
|
|
|
|
leechs += " ("+QString::number(status.num_incomplete)+")";
|
2014-06-04 10:11:38 +04:00
|
|
|
ret[KEY_TORRENT_LEECHS] = leechs;
|
2014-05-14 02:09:45 +04:00
|
|
|
const qreal ratio = QBtSession::instance()->getRealRatio(status);
|
2014-08-08 03:46:54 +04:00
|
|
|
ret[KEY_TORRENT_RATIO] = (ratio > 100.) ? QString::fromUtf8("∞") : misc::accurateDoubleToString(ratio, 1, false);
|
2012-05-26 21:42:44 +04:00
|
|
|
QString eta;
|
|
|
|
QString state;
|
2014-05-14 02:09:45 +04:00
|
|
|
if (h.is_paused(status)) {
|
|
|
|
if (h.has_error(status))
|
2012-05-26 21:42:44 +04:00
|
|
|
state = "error";
|
|
|
|
else
|
2014-05-14 02:09:45 +04:00
|
|
|
state = h.is_seed(status) ? "pausedUP" : "pausedDL";
|
2012-05-26 21:42:44 +04:00
|
|
|
} else {
|
2014-05-14 02:09:45 +04:00
|
|
|
if (QBtSession::instance()->isQueueingEnabled() && h.is_queued(status))
|
|
|
|
state = h.is_seed(status) ? "queuedUP" : "queuedDL";
|
2012-05-26 21:42:44 +04:00
|
|
|
else {
|
2014-05-14 02:09:45 +04:00
|
|
|
switch (status.state) {
|
2012-05-26 21:42:44 +04:00
|
|
|
case torrent_status::finished:
|
|
|
|
case torrent_status::seeding:
|
2014-05-14 02:09:45 +04:00
|
|
|
state = status.upload_payload_rate > 0 ? "uploading" : "stalledUP";
|
2012-05-26 21:42:44 +04:00
|
|
|
break;
|
|
|
|
case torrent_status::allocating:
|
|
|
|
case torrent_status::checking_files:
|
|
|
|
case torrent_status::queued_for_checking:
|
|
|
|
case torrent_status::checking_resume_data:
|
2014-05-14 02:09:45 +04:00
|
|
|
state = h.is_seed(status) ? "checkingUP" : "checkingDL";
|
2012-05-26 21:42:44 +04:00
|
|
|
break;
|
|
|
|
case torrent_status::downloading:
|
|
|
|
case torrent_status::downloading_metadata:
|
2014-05-14 02:09:45 +04:00
|
|
|
state = status.download_payload_rate > 0 ? "downloading" : "stalledDL";
|
|
|
|
eta = misc::userFriendlyDuration(QBtSession::instance()->getETA(h.hash(), status));
|
2012-05-26 21:42:44 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
qWarning("Unrecognized torrent status, should not happen!!! status was %d", h.state());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-04 10:11:38 +04:00
|
|
|
ret[KEY_TORRENT_ETA] = eta.isEmpty() ? QString::fromUtf8("∞") : eta;
|
|
|
|
ret[KEY_TORRENT_STATE] = state;
|
2012-05-26 21:42:44 +04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* - "priority": Torrent priority ('*' if queuing is disabled)
|
|
|
|
* - "num_seeds": Torrent seed count
|
|
|
|
* - "num_leechs": Torrent leecher count
|
|
|
|
* - "ratio": Torrent share ratio
|
|
|
|
* - "eta": Torrent ETA
|
|
|
|
* - "state": Torrent state
|
|
|
|
*/
|
2014-06-04 10:11:38 +04:00
|
|
|
QByteArray btjson::getTorrents()
|
2012-05-26 21:42:44 +04:00
|
|
|
{
|
2014-06-04 10:11:38 +04:00
|
|
|
CACHED_VARIABLE(QVariantList, torrent_list, CACHE_DURATION_MS);
|
2012-05-26 21:42:44 +04:00
|
|
|
std::vector<torrent_handle> torrents = QBtSession::instance()->getTorrents();
|
|
|
|
std::vector<torrent_handle>::const_iterator it = torrents.begin();
|
|
|
|
std::vector<torrent_handle>::const_iterator end = torrents.end();
|
|
|
|
for( ; it != end; ++it) {
|
2014-06-04 10:11:38 +04:00
|
|
|
torrent_list.append(toMap(QTorrentHandle(*it)));
|
2012-05-26 21:42:44 +04:00
|
|
|
}
|
2014-06-04 10:11:38 +04:00
|
|
|
return json::toJson(torrent_list);
|
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
|
|
|
{
|
2014-06-04 10:11:38 +04:00
|
|
|
CACHED_VARIABLE_FOR_HASH(QVariantList, tracker_list, CACHE_DURATION_MS, hash);
|
2012-05-26 21:42:44 +04:00
|
|
|
try {
|
|
|
|
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
|
|
|
|
QHash<QString, TrackerInfos> trackers_data = QBtSession::instance()->getTrackersInfo(hash);
|
|
|
|
std::vector<announce_entry> vect_trackers = h.trackers();
|
|
|
|
std::vector<announce_entry>::const_iterator it = vect_trackers.begin();
|
|
|
|
std::vector<announce_entry>::const_iterator end = vect_trackers.end();
|
|
|
|
for (; it != end; ++it) {
|
2014-06-04 10:11:38 +04:00
|
|
|
QVariantMap tracker_dict;
|
2012-05-26 21:42:44 +04:00
|
|
|
const QString tracker_url = misc::toQString(it->url);
|
2014-06-04 10:11:38 +04:00
|
|
|
tracker_dict[KEY_TRACKER_URL] = tracker_url;
|
2012-05-26 21:42:44 +04:00
|
|
|
const TrackerInfos data = trackers_data.value(tracker_url, TrackerInfos(tracker_url));
|
|
|
|
QString status;
|
|
|
|
if (it->verified)
|
|
|
|
status = tr("Working");
|
|
|
|
else {
|
|
|
|
if (it->updating && it->fails == 0)
|
|
|
|
status = tr("Updating...");
|
|
|
|
else
|
|
|
|
status = it->fails > 0 ? tr("Not working") : tr("Not contacted yet");
|
|
|
|
}
|
2014-06-04 10:11:38 +04:00
|
|
|
tracker_dict[KEY_TRACKER_STATUS] = status;
|
|
|
|
tracker_dict[KEY_TRACKER_PEERS] = QString::number(trackers_data.value(tracker_url, TrackerInfos(tracker_url)).num_peers);
|
|
|
|
tracker_dict[KEY_TRACKER_MSG] = data.last_message.trimmed();
|
2012-05-26 21:42:44 +04:00
|
|
|
|
|
|
|
tracker_list.append(tracker_dict);
|
|
|
|
}
|
2012-05-26 22:14:48 +04:00
|
|
|
} catch(const std::exception& e) {
|
2014-10-18 18:22:50 +04:00
|
|
|
qWarning() << Q_FUNC_INFO << "Invalid torrent: " << misc::toQStringU(e.what());
|
2014-06-04 10:11:38 +04:00
|
|
|
return QByteArray();
|
2012-05-26 21:42:44 +04:00
|
|
|
}
|
|
|
|
|
2014-06-04 10:11:38 +04:00
|
|
|
return json::toJson(tracker_list);
|
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:
|
|
|
|
* - "save_path": Torrent save path
|
|
|
|
* - "creation_date": Torrent creation date
|
|
|
|
* - "piece_size": Torrent piece size
|
|
|
|
* - "comment": Torrent comment
|
|
|
|
* - "total_wasted": Total data wasted for torrent
|
|
|
|
* - "total_uploaded": Total data uploaded for torrent
|
|
|
|
* - "total_downloaded": Total data uploaded for torrent
|
|
|
|
* - "up_limit": Torrent upload limit
|
|
|
|
* - "dl_limit": Torrent download limit
|
|
|
|
* - "time_elapsed": Torrent elapsed time
|
|
|
|
* - "nb_connections": Torrent connection count
|
|
|
|
* - "share_ratio": Torrent share ratio
|
|
|
|
*/
|
2014-06-04 10:11:38 +04:00
|
|
|
QByteArray btjson::getPropertiesForTorrent(const QString& hash)
|
2012-05-26 21:42:44 +04:00
|
|
|
{
|
2014-06-04 10:11:38 +04:00
|
|
|
CACHED_VARIABLE_FOR_HASH(QVariantMap, data, CACHE_DURATION_MS, hash);
|
2012-05-26 21:42:44 +04:00
|
|
|
try {
|
|
|
|
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
|
|
|
|
|
2014-05-14 02:09:45 +04:00
|
|
|
libtorrent::torrent_status status = h.status(torrent_handle::query_accurate_download_counters);
|
|
|
|
if (!status.has_metadata)
|
2014-06-04 10:11:38 +04:00
|
|
|
return QByteArray();
|
2012-05-26 21:42:44 +04:00
|
|
|
|
|
|
|
// Save path
|
2013-11-10 18:29:17 +04:00
|
|
|
QString save_path = fsutils::toNativePath(TorrentPersistentData::getSavePath(hash));
|
2012-05-26 21:42:44 +04:00
|
|
|
if (save_path.isEmpty())
|
2013-11-10 18:29:17 +04:00
|
|
|
save_path = fsutils::toNativePath(h.save_path());
|
2014-06-04 10:11:38 +04:00
|
|
|
data[KEY_PROP_SAVE_PATH] = save_path;
|
|
|
|
data[KEY_PROP_CREATION_DATE] = h.creation_date();
|
2014-08-08 03:46:54 +04:00
|
|
|
data[KEY_PROP_PIECE_SIZE] = misc::friendlyUnit(h.piece_length(), false, true);
|
2014-06-04 10:11:38 +04:00
|
|
|
data[KEY_PROP_COMMENT] = h.comment();
|
2014-08-08 03:46:54 +04:00
|
|
|
data[KEY_PROP_WASTED] = misc::friendlyUnit(status.total_failed_bytes + status.total_redundant_bytes, false, true);
|
|
|
|
data[KEY_PROP_UPLOADED] = QString(misc::friendlyUnit(status.all_time_upload, false, true) + " (" + misc::friendlyUnit(status.total_payload_upload, false, true) + " " + tr("this session") + ")");
|
|
|
|
data[KEY_PROP_DOWNLOADED] = QString(misc::friendlyUnit(status.all_time_download, false, true) + " (" + misc::friendlyUnit(status.total_payload_download, false, true) + " " + tr("this session") + ")");
|
|
|
|
data[KEY_PROP_UP_LIMIT] = h.upload_limit() <= 0 ? QString::fromUtf8("∞") : misc::friendlyUnit(h.upload_limit(), true, true);
|
|
|
|
data[KEY_PROP_DL_LIMIT] = h.download_limit() <= 0 ? QString::fromUtf8("∞") : misc::friendlyUnit(h.download_limit(), true, true);
|
2014-05-14 02:09:45 +04:00
|
|
|
QString elapsed_txt = misc::userFriendlyDuration(status.active_time);
|
|
|
|
if (h.is_seed(status))
|
|
|
|
elapsed_txt += " ("+tr("Seeded for %1", "e.g. Seeded for 3m10s").arg(misc::userFriendlyDuration(status.seeding_time))+")";
|
2014-06-04 10:11:38 +04:00
|
|
|
data[KEY_PROP_TIME_ELAPSED] = elapsed_txt;
|
|
|
|
data[KEY_PROP_CONNECT_COUNT] = QString(QString::number(status.num_connections) + " (" + tr("%1 max", "e.g. 10 max").arg(QString::number(status.connections_limit)) + ")");
|
2014-05-14 02:09:45 +04:00
|
|
|
const qreal ratio = QBtSession::instance()->getRealRatio(status);
|
2014-08-08 03:46:54 +04:00
|
|
|
data[KEY_PROP_RATIO] = ratio > 100. ? QString::fromUtf8("∞") : misc::accurateDoubleToString(ratio, 1, false);
|
2012-05-26 22:14:48 +04:00
|
|
|
} catch(const std::exception& e) {
|
2014-10-18 18:22:50 +04:00
|
|
|
qWarning() << Q_FUNC_INFO << "Invalid torrent: " << misc::toQStringU(e.what());
|
2014-06-04 10:11:38 +04:00
|
|
|
return QByteArray();
|
2012-05-26 21:42:44 +04:00
|
|
|
}
|
|
|
|
|
2014-06-04 10:11:38 +04:00
|
|
|
return json::toJson(data);
|
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
|
|
|
{
|
2014-06-04 10:11:38 +04:00
|
|
|
CACHED_VARIABLE_FOR_HASH(QVariantList, file_list, CACHE_DURATION_MS, hash);
|
2012-05-26 21:42:44 +04:00
|
|
|
try {
|
|
|
|
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
|
|
|
|
if (!h.has_metadata())
|
2014-06-04 10:11:38 +04:00
|
|
|
return QByteArray();
|
2012-05-26 21:42:44 +04:00
|
|
|
|
|
|
|
const std::vector<int> priorities = h.file_priorities();
|
|
|
|
std::vector<size_type> fp;
|
|
|
|
h.file_progress(fp);
|
|
|
|
for (int i = 0; i < h.num_files(); ++i) {
|
2014-06-04 10:11:38 +04:00
|
|
|
QVariantMap file_dict;
|
2012-05-29 19:32:18 +04:00
|
|
|
QString fileName = h.filename_at(i);
|
|
|
|
if (fileName.endsWith(".!qB", Qt::CaseInsensitive))
|
|
|
|
fileName.chop(4);
|
2014-06-04 10:11:38 +04:00
|
|
|
file_dict[KEY_FILE_NAME] = fsutils::toNativePath(fileName);
|
2012-05-26 21:42:44 +04:00
|
|
|
const size_type size = h.filesize_at(i);
|
2014-08-08 03:46:54 +04:00
|
|
|
file_dict[KEY_FILE_SIZE] = misc::friendlyUnit(size, false, true);
|
2014-06-04 10:11:38 +04:00
|
|
|
file_dict[KEY_FILE_PROGRESS] = (size > 0) ? (fp[i] / (double) size) : 1.;
|
|
|
|
file_dict[KEY_FILE_PRIORITY] = priorities[i];
|
2012-05-26 21:42:44 +04:00
|
|
|
if (i == 0)
|
2014-06-04 10:11:38 +04:00
|
|
|
file_dict[KEY_FILE_IS_SEED] = h.is_seed();
|
2012-05-26 21:42:44 +04:00
|
|
|
|
|
|
|
file_list.append(file_dict);
|
|
|
|
}
|
2012-05-26 22:14:48 +04:00
|
|
|
} catch (const std::exception& e) {
|
2014-10-18 18:22:50 +04:00
|
|
|
qWarning() << Q_FUNC_INFO << "Invalid torrent: " << misc::toQStringU(e.what());
|
2014-06-04 10:11:38 +04:00
|
|
|
return QByteArray();
|
2012-05-26 21:42:44 +04:00
|
|
|
}
|
|
|
|
|
2014-06-04 10:11:38 +04:00
|
|
|
return json::toJson(file_list);
|
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:
|
|
|
|
* - "dl_info": Global download info
|
|
|
|
* - "up_info": Global upload info
|
|
|
|
*/
|
2014-06-04 10:11:38 +04:00
|
|
|
QByteArray btjson::getTransferInfo()
|
2012-05-26 23:58:53 +04:00
|
|
|
{
|
2014-06-04 10:11:38 +04:00
|
|
|
CACHED_VARIABLE(QVariantMap, info, CACHE_DURATION_MS);
|
2012-05-26 23:58:53 +04:00
|
|
|
session_status sessionStatus = QBtSession::instance()->getSessionStatus();
|
2014-08-08 03:46:54 +04:00
|
|
|
info[KEY_TRANSFER_DLSPEED] = tr("D: %1/s - T: %2", "Download speed: x KiB/s - Transferred: x MiB").arg(misc::friendlyUnit(sessionStatus.payload_download_rate, true, true)).arg(misc::friendlyUnit(sessionStatus.total_payload_download, false, true));
|
|
|
|
info[KEY_TRANSFER_UPSPEED] = tr("U: %1/s - T: %2", "Upload speed: x KiB/s - Transferred: x MiB").arg(misc::friendlyUnit(sessionStatus.payload_upload_rate, true, true)).arg(misc::friendlyUnit(sessionStatus.total_payload_upload, false, true));
|
2014-06-04 10:11:38 +04:00
|
|
|
return json::toJson(info);
|
2012-05-26 23:58:53 +04:00
|
|
|
}
|