Browse Source

WebUI: Improve btjson.* indentation.

adaptive-webui-19844
Vladimir Golovnev (Glassez) 10 years ago
parent
commit
c24610ec80
  1. 358
      src/webui/btjson.cpp
  2. 17
      src/webui/btjson.h

358
src/webui/btjson.cpp

@ -47,32 +47,32 @@ using namespace libtorrent;
#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0) #if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
#define CACHED_VARIABLE(VARTYPE, VAR, DUR) \ #define CACHED_VARIABLE(VARTYPE, VAR, DUR) \
static VARTYPE VAR; \ static VARTYPE VAR; \
static QElapsedTimer cacheTimer; \ static QElapsedTimer cacheTimer; \
static bool initialized = false; \ static bool initialized = false; \
if (initialized && !cacheTimer.hasExpired(DUR)) \ if (initialized && !cacheTimer.hasExpired(DUR)) \
return json::toJson(VAR); \ return json::toJson(VAR); \
initialized = true; \ initialized = true; \
cacheTimer.start(); \ cacheTimer.start(); \
VAR = VARTYPE() VAR = VARTYPE()
#define CACHED_VARIABLE_FOR_HASH(VARTYPE, VAR, DUR, HASH) \ #define CACHED_VARIABLE_FOR_HASH(VARTYPE, VAR, DUR, HASH) \
static VARTYPE VAR; \ static VARTYPE VAR; \
static QString prev_hash; \ static QString prev_hash; \
static QElapsedTimer cacheTimer; \ static QElapsedTimer cacheTimer; \
if (prev_hash == HASH && !cacheTimer.hasExpired(DUR)) \ if (prev_hash == HASH && !cacheTimer.hasExpired(DUR)) \
return json::toJson(VAR); \ return json::toJson(VAR); \
prev_hash = HASH; \ prev_hash = HASH; \
cacheTimer.start(); \ cacheTimer.start(); \
VAR = VARTYPE() VAR = VARTYPE()
#else #else
// We don't support caching for Qt < 4.7 at the moment // We don't support caching for Qt < 4.7 at the moment
#define CACHED_VARIABLE(VARTYPE, VAR, DUR) \ #define CACHED_VARIABLE(VARTYPE, VAR, DUR) \
VARTYPE VAR VARTYPE VAR
#define CACHED_VARIABLE_FOR_HASH(VARTYPE, VAR, DUR, HASH) \ #define CACHED_VARIABLE_FOR_HASH(VARTYPE, VAR, DUR, HASH) \
VARTYPE VAR VARTYPE VAR
#endif #endif
@ -134,61 +134,63 @@ static const char KEY_TRANSFER_UPDATA[] = "up_info_data";
static QVariantMap toMap(const QTorrentHandle& h) static QVariantMap toMap(const QTorrentHandle& h)
{ {
libtorrent::torrent_status status = h.status(torrent_handle::query_accurate_download_counters); libtorrent::torrent_status status = h.status(torrent_handle::query_accurate_download_counters);
QVariantMap ret; QVariantMap ret;
ret[KEY_TORRENT_HASH] = h.hash(); ret[KEY_TORRENT_HASH] = h.hash();
ret[KEY_TORRENT_NAME] = h.name(); ret[KEY_TORRENT_NAME] = h.name();
ret[KEY_TORRENT_SIZE] = static_cast<qlonglong>(status.total_wanted); ret[KEY_TORRENT_SIZE] = static_cast<qlonglong>(status.total_wanted);
ret[KEY_TORRENT_PROGRESS] = h.progress(status); ret[KEY_TORRENT_PROGRESS] = h.progress(status);
ret[KEY_TORRENT_DLSPEED] = status.download_payload_rate; ret[KEY_TORRENT_DLSPEED] = status.download_payload_rate;
ret[KEY_TORRENT_UPSPEED] = status.upload_payload_rate; ret[KEY_TORRENT_UPSPEED] = status.upload_payload_rate;
if (QBtSession::instance()->isQueueingEnabled() && h.queue_position(status) >= 0) if (QBtSession::instance()->isQueueingEnabled() && h.queue_position(status) >= 0)
ret[KEY_TORRENT_PRIORITY] = h.queue_position(status); ret[KEY_TORRENT_PRIORITY] = h.queue_position(status);
else
ret[KEY_TORRENT_PRIORITY] = -1;
ret[KEY_TORRENT_SEEDS] = status.num_seeds;
ret[KEY_TORRENT_NUM_COMPLETE] = status.num_complete;
ret[KEY_TORRENT_LEECHS] = status.num_peers - status.num_seeds;
ret[KEY_TORRENT_NUM_INCOMPLETE] = status.num_incomplete;
const qreal ratio = QBtSession::instance()->getRealRatio(status);
ret[KEY_TORRENT_RATIO] = (ratio > 100.) ? -1 : ratio;
qulonglong eta = 0;
QString state;
if (h.is_paused(status)) {
if (h.has_error(status))
state = "error";
else else
state = h.is_seed(status) ? "pausedUP" : "pausedDL"; ret[KEY_TORRENT_PRIORITY] = -1;
} else { ret[KEY_TORRENT_SEEDS] = status.num_seeds;
if (QBtSession::instance()->isQueueingEnabled() && h.is_queued(status)) ret[KEY_TORRENT_NUM_COMPLETE] = status.num_complete;
state = h.is_seed(status) ? "queuedUP" : "queuedDL"; ret[KEY_TORRENT_LEECHS] = status.num_peers - status.num_seeds;
ret[KEY_TORRENT_NUM_INCOMPLETE] = status.num_incomplete;
const qreal ratio = QBtSession::instance()->getRealRatio(status);
ret[KEY_TORRENT_RATIO] = (ratio > 100.) ? -1 : ratio;
qulonglong eta = 0;
QString state;
if (h.is_paused(status)) {
if (h.has_error(status))
state = "error";
else
state = h.is_seed(status) ? "pausedUP" : "pausedDL";
}
else { else {
switch (status.state) { if (QBtSession::instance()->isQueueingEnabled() && h.is_queued(status)) {
case torrent_status::finished: state = h.is_seed(status) ? "queuedUP" : "queuedDL";
case torrent_status::seeding: }
state = status.upload_payload_rate > 0 ? "uploading" : "stalledUP"; else {
break; switch (status.state) {
case torrent_status::allocating: case torrent_status::finished:
case torrent_status::checking_files: case torrent_status::seeding:
case torrent_status::queued_for_checking: state = status.upload_payload_rate > 0 ? "uploading" : "stalledUP";
case torrent_status::checking_resume_data: break;
state = h.is_seed(status) ? "checkingUP" : "checkingDL"; case torrent_status::allocating:
break; case torrent_status::checking_files:
case torrent_status::downloading: case torrent_status::queued_for_checking:
case torrent_status::downloading_metadata: case torrent_status::checking_resume_data:
state = status.download_payload_rate > 0 ? "downloading" : "stalledDL"; state = h.is_seed(status) ? "checkingUP" : "checkingDL";
eta = QBtSession::instance()->getETA(h.hash(), status); break;
break; case torrent_status::downloading:
default: case torrent_status::downloading_metadata:
qWarning("Unrecognized torrent status, should not happen!!! status was %d", h.state()); state = status.download_payload_rate > 0 ? "downloading" : "stalledDL";
} eta = QBtSession::instance()->getETA(h.hash(), status);
break;
default:
qWarning("Unrecognized torrent status, should not happen!!! status was %d", h.state());
}
}
} }
} ret[KEY_TORRENT_ETA] = eta ? eta : MAX_ETA;
ret[KEY_TORRENT_ETA] = eta ? eta : MAX_ETA; ret[KEY_TORRENT_STATE] = state;
ret[KEY_TORRENT_STATE] = state;
return ret; return ret;
} }
/** /**
@ -213,14 +215,17 @@ static QVariantMap toMap(const QTorrentHandle& h)
*/ */
QByteArray btjson::getTorrents() QByteArray btjson::getTorrents()
{ {
CACHED_VARIABLE(QVariantList, torrent_list, CACHE_DURATION_MS); QVariantList torrent_list;
std::vector<torrent_handle> torrents = QBtSession::instance()->getTorrents();
std::vector<torrent_handle>::const_iterator it = torrents.begin(); std::vector<torrent_handle> torrents = QBtSession::instance()->getTorrents();
std::vector<torrent_handle>::const_iterator end = torrents.end(); std::vector<torrent_handle>::const_iterator it = torrents.begin();
for( ; it != end; ++it) { std::vector<torrent_handle>::const_iterator end = torrents.end();
torrent_list.append(toMap(QTorrentHandle(*it)));
} for( ; it != end; ++it) {
return json::toJson(torrent_list); torrent_list.append(toMap(QTorrentHandle(*it)));
}
return json::toJson(torrent_list);
} }
/** /**
@ -235,39 +240,40 @@ QByteArray btjson::getTorrents()
*/ */
QByteArray btjson::getTrackersForTorrent(const QString& hash) QByteArray btjson::getTrackersForTorrent(const QString& hash)
{ {
CACHED_VARIABLE_FOR_HASH(QVariantList, tracker_list, CACHE_DURATION_MS, hash); CACHED_VARIABLE_FOR_HASH(QVariantList, tracker_list, CACHE_DURATION_MS, hash);
try { try {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
QHash<QString, TrackerInfos> trackers_data = QBtSession::instance()->getTrackersInfo(hash); QHash<QString, TrackerInfos> trackers_data = QBtSession::instance()->getTrackersInfo(hash);
std::vector<announce_entry> vect_trackers = h.trackers(); std::vector<announce_entry> vect_trackers = h.trackers();
std::vector<announce_entry>::const_iterator it = vect_trackers.begin(); std::vector<announce_entry>::const_iterator it = vect_trackers.begin();
std::vector<announce_entry>::const_iterator end = vect_trackers.end(); std::vector<announce_entry>::const_iterator end = vect_trackers.end();
for (; it != end; ++it) { for (; it != end; ++it) {
QVariantMap tracker_dict; QVariantMap tracker_dict;
const QString tracker_url = misc::toQString(it->url); const QString tracker_url = misc::toQString(it->url);
tracker_dict[KEY_TRACKER_URL] = tracker_url; tracker_dict[KEY_TRACKER_URL] = tracker_url;
const TrackerInfos data = trackers_data.value(tracker_url, TrackerInfos(tracker_url)); const TrackerInfos data = trackers_data.value(tracker_url, TrackerInfos(tracker_url));
QString status; QString status;
if (it->verified) if (it->verified)
status = tr("Working"); status = tr("Working");
else { else {
if (it->updating && it->fails == 0) if (it->updating && it->fails == 0)
status = tr("Updating..."); status = tr("Updating...");
else else
status = it->fails > 0 ? tr("Not working") : tr("Not contacted yet"); status = it->fails > 0 ? tr("Not working") : tr("Not contacted yet");
} }
tracker_dict[KEY_TRACKER_STATUS] = status; tracker_dict[KEY_TRACKER_STATUS] = status;
tracker_dict[KEY_TRACKER_PEERS] = static_cast<qulonglong>(trackers_data.value(tracker_url, TrackerInfos(tracker_url)).num_peers); tracker_dict[KEY_TRACKER_PEERS] = static_cast<qulonglong>(trackers_data.value(tracker_url, TrackerInfos(tracker_url)).num_peers);
tracker_dict[KEY_TRACKER_MSG] = data.last_message.trimmed(); tracker_dict[KEY_TRACKER_MSG] = data.last_message.trimmed();
tracker_list.append(tracker_dict); tracker_list.append(tracker_dict);
}
}
catch(const std::exception& e) {
qWarning() << Q_FUNC_INFO << "Invalid torrent: " << misc::toQStringU(e.what());
return QByteArray();
} }
} catch(const std::exception& e) {
qWarning() << Q_FUNC_INFO << "Invalid torrent: " << misc::toQStringU(e.what());
return QByteArray();
}
return json::toJson(tracker_list); return json::toJson(tracker_list);
} }
/** /**
@ -294,41 +300,42 @@ QByteArray btjson::getTrackersForTorrent(const QString& hash)
*/ */
QByteArray btjson::getPropertiesForTorrent(const QString& hash) QByteArray btjson::getPropertiesForTorrent(const QString& hash)
{ {
CACHED_VARIABLE_FOR_HASH(QVariantMap, data, CACHE_DURATION_MS, hash); CACHED_VARIABLE_FOR_HASH(QVariantMap, data, CACHE_DURATION_MS, hash);
try { try {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
libtorrent::torrent_status status = h.status(torrent_handle::query_accurate_download_counters); libtorrent::torrent_status status = h.status(torrent_handle::query_accurate_download_counters);
if (!status.has_metadata) if (!status.has_metadata)
return QByteArray(); return QByteArray();
// Save path // Save path
QString save_path = fsutils::toNativePath(TorrentPersistentData::getSavePath(hash)); QString save_path = fsutils::toNativePath(TorrentPersistentData::getSavePath(hash));
if (save_path.isEmpty()) if (save_path.isEmpty())
save_path = fsutils::toNativePath(h.save_path()); save_path = fsutils::toNativePath(h.save_path());
data[KEY_PROP_SAVE_PATH] = save_path; data[KEY_PROP_SAVE_PATH] = save_path;
data[KEY_PROP_CREATION_DATE] = h.creation_date_unix(); data[KEY_PROP_CREATION_DATE] = h.creation_date_unix();
data[KEY_PROP_PIECE_SIZE] = static_cast<qlonglong>(h.piece_length()); data[KEY_PROP_PIECE_SIZE] = static_cast<qlonglong>(h.piece_length());
data[KEY_PROP_COMMENT] = h.comment(); data[KEY_PROP_COMMENT] = h.comment();
data[KEY_PROP_WASTED] = static_cast<qlonglong>(status.total_failed_bytes + status.total_redundant_bytes); data[KEY_PROP_WASTED] = static_cast<qlonglong>(status.total_failed_bytes + status.total_redundant_bytes);
data[KEY_PROP_UPLOADED] = static_cast<qlonglong>(status.all_time_upload); data[KEY_PROP_UPLOADED] = static_cast<qlonglong>(status.all_time_upload);
data[KEY_PROP_UPLOADED_SESSION] = static_cast<qlonglong>(status.total_payload_upload); data[KEY_PROP_UPLOADED_SESSION] = static_cast<qlonglong>(status.total_payload_upload);
data[KEY_PROP_DOWNLOADED] = static_cast<qlonglong>(status.all_time_download); data[KEY_PROP_DOWNLOADED] = static_cast<qlonglong>(status.all_time_download);
data[KEY_PROP_DOWNLOADED_SESSION] = static_cast<qlonglong>(status.total_payload_download); data[KEY_PROP_DOWNLOADED_SESSION] = static_cast<qlonglong>(status.total_payload_download);
data[KEY_PROP_UP_LIMIT] = h.upload_limit() <= 0 ? -1 : h.upload_limit(); data[KEY_PROP_UP_LIMIT] = h.upload_limit() <= 0 ? -1 : h.upload_limit();
data[KEY_PROP_DL_LIMIT] = h.download_limit() <= 0 ? -1 : h.download_limit(); data[KEY_PROP_DL_LIMIT] = h.download_limit() <= 0 ? -1 : h.download_limit();
data[KEY_PROP_TIME_ELAPSED] = status.active_time; data[KEY_PROP_TIME_ELAPSED] = status.active_time;
data[KEY_PROP_SEEDING_TIME] = status.seeding_time; data[KEY_PROP_SEEDING_TIME] = status.seeding_time;
data[KEY_PROP_CONNECT_COUNT] = status.num_connections; data[KEY_PROP_CONNECT_COUNT] = status.num_connections;
data[KEY_PROP_CONNECT_COUNT_LIMIT] = status.connections_limit; data[KEY_PROP_CONNECT_COUNT_LIMIT] = status.connections_limit;
const qreal ratio = QBtSession::instance()->getRealRatio(status); const qreal ratio = QBtSession::instance()->getRealRatio(status);
data[KEY_PROP_RATIO] = ratio > 100. ? -1 : ratio; data[KEY_PROP_RATIO] = ratio > 100. ? -1 : ratio;
} catch(const std::exception& e) { }
qWarning() << Q_FUNC_INFO << "Invalid torrent: " << misc::toQStringU(e.what()); catch(const std::exception& e) {
return QByteArray(); qWarning() << Q_FUNC_INFO << "Invalid torrent: " << misc::toQStringU(e.what());
} return QByteArray();
}
return json::toJson(data); return json::toJson(data);
} }
/** /**
@ -344,36 +351,37 @@ QByteArray btjson::getPropertiesForTorrent(const QString& hash)
*/ */
QByteArray btjson::getFilesForTorrent(const QString& hash) QByteArray btjson::getFilesForTorrent(const QString& hash)
{ {
CACHED_VARIABLE_FOR_HASH(QVariantList, file_list, CACHE_DURATION_MS, hash); CACHED_VARIABLE_FOR_HASH(QVariantList, file_list, CACHE_DURATION_MS, hash);
try { try {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if (!h.has_metadata()) if (!h.has_metadata())
return QByteArray(); return QByteArray();
const std::vector<int> priorities = h.file_priorities(); const std::vector<int> priorities = h.file_priorities();
std::vector<size_type> fp; std::vector<size_type> fp;
h.file_progress(fp); h.file_progress(fp);
for (int i = 0; i < h.num_files(); ++i) { for (int i = 0; i < h.num_files(); ++i) {
QVariantMap file_dict; QVariantMap file_dict;
QString fileName = h.filename_at(i); QString fileName = h.filename_at(i);
if (fileName.endsWith(".!qB", Qt::CaseInsensitive)) if (fileName.endsWith(".!qB", Qt::CaseInsensitive))
fileName.chop(4); fileName.chop(4);
file_dict[KEY_FILE_NAME] = fsutils::toNativePath(fileName); file_dict[KEY_FILE_NAME] = fsutils::toNativePath(fileName);
const size_type size = h.filesize_at(i); const size_type size = h.filesize_at(i);
file_dict[KEY_FILE_SIZE] = static_cast<qlonglong>(size); file_dict[KEY_FILE_SIZE] = static_cast<qlonglong>(size);
file_dict[KEY_FILE_PROGRESS] = (size > 0) ? (fp[i] / (double) size) : 1.; file_dict[KEY_FILE_PROGRESS] = (size > 0) ? (fp[i] / (double) size) : 1.;
file_dict[KEY_FILE_PRIORITY] = priorities[i]; file_dict[KEY_FILE_PRIORITY] = priorities[i];
if (i == 0) if (i == 0)
file_dict[KEY_FILE_IS_SEED] = h.is_seed(); file_dict[KEY_FILE_IS_SEED] = h.is_seed();
file_list.append(file_dict); file_list.append(file_dict);
}
}
catch (const std::exception& e) {
qWarning() << Q_FUNC_INFO << "Invalid torrent: " << misc::toQStringU(e.what());
return QByteArray();
} }
} catch (const std::exception& e) {
qWarning() << Q_FUNC_INFO << "Invalid torrent: " << misc::toQStringU(e.what());
return QByteArray();
}
return json::toJson(file_list); return json::toJson(file_list);
} }
/** /**
@ -388,11 +396,11 @@ QByteArray btjson::getFilesForTorrent(const QString& hash)
*/ */
QByteArray btjson::getTransferInfo() QByteArray btjson::getTransferInfo()
{ {
CACHED_VARIABLE(QVariantMap, info, CACHE_DURATION_MS); CACHED_VARIABLE(QVariantMap, info, CACHE_DURATION_MS);
session_status sessionStatus = QBtSession::instance()->getSessionStatus(); session_status sessionStatus = QBtSession::instance()->getSessionStatus();
info[KEY_TRANSFER_DLSPEED] = sessionStatus.payload_download_rate; info[KEY_TRANSFER_DLSPEED] = sessionStatus.payload_download_rate;
info[KEY_TRANSFER_DLDATA] = static_cast<qlonglong>(sessionStatus.total_payload_download); info[KEY_TRANSFER_DLDATA] = static_cast<qlonglong>(sessionStatus.total_payload_download);
info[KEY_TRANSFER_UPSPEED] = sessionStatus.payload_upload_rate; info[KEY_TRANSFER_UPSPEED] = sessionStatus.payload_upload_rate;
info[KEY_TRANSFER_UPDATA] = static_cast<qlonglong>(sessionStatus.total_payload_upload); info[KEY_TRANSFER_UPDATA] = static_cast<qlonglong>(sessionStatus.total_payload_upload);
return json::toJson(info); return json::toJson(info);
} }

17
src/webui/btjson.h

@ -34,18 +34,19 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QString> #include <QString>
class btjson { class btjson
Q_DECLARE_TR_FUNCTIONS(misc) {
Q_DECLARE_TR_FUNCTIONS(misc)
private: private:
btjson() {} btjson() {}
public: public:
static QByteArray getTorrents(); static QByteArray getTorrents();
static QByteArray getTrackersForTorrent(const QString& hash); static QByteArray getTrackersForTorrent(const QString& hash);
static QByteArray getPropertiesForTorrent(const QString& hash); static QByteArray getPropertiesForTorrent(const QString& hash);
static QByteArray getFilesForTorrent(const QString& hash); static QByteArray getFilesForTorrent(const QString& hash);
static QByteArray getTransferInfo(); static QByteArray getTransferInfo();
}; // class btjson }; // class btjson
#endif // BTJSON_H #endif // BTJSON_H

Loading…
Cancel
Save