From e887f574d7112e06b7f30b7c05079fdf0abeb8e3 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Fri, 21 Nov 2014 12:42:02 +0300 Subject: [PATCH] Implement QTorrentHandle torrentState() and eta(). --- src/qtlibtorrent/qtorrenthandle.cpp | 89 ++++++++++++++++++++++++++++- src/qtlibtorrent/qtorrenthandle.h | 32 +++++++++++ 2 files changed, 120 insertions(+), 1 deletion(-) diff --git a/src/qtlibtorrent/qtorrenthandle.cpp b/src/qtlibtorrent/qtorrenthandle.cpp index d24b17839..e81d0fa99 100644 --- a/src/qtlibtorrent/qtorrenthandle.cpp +++ b/src/qtlibtorrent/qtorrenthandle.cpp @@ -40,6 +40,7 @@ #include "preferences.h" #include "qtorrenthandle.h" #include "torrentpersistentdata.h" +#include "qbtsession.h" #include #include #include @@ -403,7 +404,52 @@ bool QTorrentHandle::has_metadata() const { } void QTorrentHandle::file_progress(std::vector& fp) const { - torrent_handle::file_progress(fp, torrent_handle::piece_granularity); + torrent_handle::file_progress(fp, torrent_handle::piece_granularity); +} + +QTorrentState QTorrentHandle::torrentState() const { + QTorrentState state = QTorrentState::Unknown; + libtorrent::torrent_status s = status(torrent_handle::query_accurate_download_counters); + + if (is_paused(s)) { + if (has_error(s)) + state = QTorrentState::Error; + else + state = is_seed(s) ? QTorrentState::PausedUploading : QTorrentState::PausedDownloading; + } + else { + if (QBtSession::instance()->isQueueingEnabled() && is_queued(s)) { + state = is_seed(s) ? QTorrentState::QueuedUploading : QTorrentState::QueuedDownloading; + } + else { + switch (s.state) { + case torrent_status::finished: + case torrent_status::seeding: + state = s.upload_payload_rate > 0 ? QTorrentState::Uploading : QTorrentState::StalledUploading; + break; + case torrent_status::allocating: + case torrent_status::checking_files: + case torrent_status::queued_for_checking: + case torrent_status::checking_resume_data: + state = is_seed(s) ? QTorrentState::CheckingUploading : QTorrentState::CheckingDownloading; + break; + case torrent_status::downloading: + case torrent_status::downloading_metadata: + state = s.download_payload_rate > 0 ? QTorrentState::Downloading : QTorrentState::StalledDownloading; + break; + default: + qWarning("Unrecognized torrent status, should not happen!!! status was %d", this->state()); + } + } + } + + return state; +} + +qulonglong QTorrentHandle::eta() const +{ + libtorrent::torrent_status s = status(torrent_handle::query_accurate_download_counters); + return QBtSession::instance()->getETA(hash(), s); } // @@ -681,3 +727,44 @@ QString QTorrentHandle::filepath_at(const libtorrent::torrent_info &info, unsign return fsutils::fromNativePath(misc::toQStringU(info.files().file_path(index))); } + + +QTorrentState::QTorrentState(int value) + : m_value(value) +{ +} + +QString QTorrentState::toString() const +{ + switch (m_value) { + case Error: + return "error"; + case Uploading: + return "uploading"; + case PausedUploading: + return "pausedUP"; + case QueuedUploading: + return "queuedUP"; + case StalledUploading: + return "stalledUP"; + case CheckingUploading: + return "checkingUP"; + case Downloading: + return "downloading"; + case PausedDownloading: + return "pausedDL"; + case QueuedDownloading: + return "queuedDL"; + case StalledDownloading: + return "stalledDL"; + case CheckingDownloading: + return "checkingDL"; + default: + return "unknown"; + } +} + +QTorrentState::operator int() const +{ + return m_value; +} diff --git a/src/qtlibtorrent/qtorrenthandle.h b/src/qtlibtorrent/qtorrenthandle.h index f19a836d4..6c25dcc96 100644 --- a/src/qtlibtorrent/qtorrenthandle.h +++ b/src/qtlibtorrent/qtorrenthandle.h @@ -39,6 +39,36 @@ QT_BEGIN_NAMESPACE class QStringList; QT_END_NAMESPACE +class QTorrentState +{ +public: + enum { + Unknown = -1, + + Error, + + Uploading, + PausedUploading, + QueuedUploading, + StalledUploading, + CheckingUploading, + + Downloading, + PausedDownloading, + QueuedDownloading, + StalledDownloading, + CheckingDownloading + }; + + QTorrentState(int value); + + operator int() const; + QString toString() const; + +private: + int m_value; +}; + // A wrapper for torrent_handle in libtorrent // to interact well with Qt types class QTorrentHandle : public libtorrent::torrent_handle { @@ -94,6 +124,8 @@ public: void downloading_pieces(libtorrent::bitfield& bf) const; bool has_metadata() const; void file_progress(std::vector& fp) const; + QTorrentState torrentState() const; + qulonglong eta() const; // // Setters