Implement QTorrentHandle torrentState() and eta().

This commit is contained in:
Vladimir Golovnev (Glassez) 2014-11-21 12:42:02 +03:00
parent c24610ec80
commit e887f574d7
2 changed files with 120 additions and 1 deletions

View File

@ -40,6 +40,7 @@
#include "preferences.h"
#include "qtorrenthandle.h"
#include "torrentpersistentdata.h"
#include "qbtsession.h"
#include <libtorrent/version.hpp>
#include <libtorrent/magnet_uri.hpp>
#include <libtorrent/torrent_info.hpp>
@ -403,7 +404,52 @@ bool QTorrentHandle::has_metadata() const {
}
void QTorrentHandle::file_progress(std::vector<size_type>& 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;
}

View File

@ -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<libtorrent::size_type>& fp) const;
QTorrentState torrentState() const;
qulonglong eta() const;
//
// Setters