From 35e964f66d4b69979b2c56f019e798445b038192 Mon Sep 17 00:00:00 2001 From: sledgehammer999 Date: Sat, 6 Dec 2014 19:07:26 +0200 Subject: [PATCH] Implement peer relevance column. Closes #1630. --- src/properties/peerlistdelegate.h | 5 +++-- src/properties/peerlistwidget.cpp | 33 +++++++++++++++++++++++++---- src/properties/peerlistwidget.h | 5 +++-- src/properties/propertieswidget.cpp | 10 ++++++--- 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/properties/peerlistdelegate.h b/src/properties/peerlistdelegate.h index f6ad975d9..a71ed3f8c 100644 --- a/src/properties/peerlistdelegate.h +++ b/src/properties/peerlistdelegate.h @@ -40,7 +40,7 @@ class PeerListDelegate: public QItemDelegate { public: enum PeerListColumns {COUNTRY, IP, PORT, CONNECTION, FLAGS, CLIENT, PROGRESS, DOWN_SPEED, UP_SPEED, - TOT_DOWN, TOT_UP, IP_HIDDEN, COL_COUNT}; + TOT_DOWN, TOT_UP, RELEVANCE, IP_HIDDEN, COL_COUNT}; public: PeerListDelegate(QObject *parent) : QItemDelegate(parent) {} @@ -64,7 +64,8 @@ public: QItemDelegate::drawDisplay(painter, opt, opt.rect, misc::friendlyUnit(speed)+tr("/s", "/second (i.e. per second)")); break; } - case PROGRESS:{ + case PROGRESS: + case RELEVANCE:{ QItemDelegate::drawBackground(painter, opt, index); qreal progress = index.data().toDouble(); QItemDelegate::drawDisplay(painter, opt, opt.rect, misc::accurateDoubleToString(progress*100.0, 1)+"%"); diff --git a/src/properties/peerlistwidget.cpp b/src/properties/peerlistwidget.cpp index 0929ba8e9..c4610fbb7 100644 --- a/src/properties/peerlistwidget.cpp +++ b/src/properties/peerlistwidget.cpp @@ -71,6 +71,7 @@ PeerListWidget::PeerListWidget(PropertiesWidget *parent): m_listModel->setHeaderData(PeerListDelegate::UP_SPEED, Qt::Horizontal, tr("Up Speed", "i.e: Upload speed")); m_listModel->setHeaderData(PeerListDelegate::TOT_DOWN, Qt::Horizontal, tr("Downloaded", "i.e: total data downloaded")); m_listModel->setHeaderData(PeerListDelegate::TOT_UP, Qt::Horizontal, tr("Uploaded", "i.e: total data uploaded")); + m_listModel->setHeaderData(PeerListDelegate::RELEVANCE, Qt::Horizontal, tr("Relevance", "i.e: How relevant this peer is to us. How many pieces it has that we don't.")); // Proxy model to support sorting without actually altering the underlying model m_proxyModel = new PeerListSortModel(); m_proxyModel->setDynamicSortFilter(true); @@ -327,6 +328,7 @@ void PeerListWidget::loadPeers(const QTorrentHandle &h, bool force_hostname_reso if (!h.is_valid()) return; boost::system::error_code ec; + libtorrent::torrent_status status = h.status(torrent_handle::query_pieces); std::vector peers; h.get_peer_info(peers); QSet old_peers_set = m_peerItems.keys().toSet(); @@ -341,14 +343,14 @@ void PeerListWidget::loadPeers(const QTorrentHandle &h, bool force_hostname_reso QString peer_ip = misc::toQString(ip_str); if (m_peerItems.contains(peer_ip)) { // Update existing peer - updatePeer(peer_ip, peer); + updatePeer(peer_ip, status, peer); old_peers_set.remove(peer_ip); if (force_hostname_resolution && m_resolver) { m_resolver->resolve(peer_ip); } } else { // Add new peer - m_peerItems[peer_ip] = addPeer(peer_ip, peer); + m_peerItems[peer_ip] = addPeer(peer_ip, status, peer); m_peerEndpoints[peer_ip] = peer.ip; // Resolve peer host name is asked if (m_resolver) @@ -366,7 +368,7 @@ void PeerListWidget::loadPeers(const QTorrentHandle &h, bool force_hostname_reso } } -QStandardItem* PeerListWidget::addPeer(const QString& ip, const peer_info& peer) { +QStandardItem* PeerListWidget::addPeer(const QString& ip, const libtorrent::torrent_status &status, const peer_info& peer) { int row = m_listModel->rowCount(); // Adding Peer to peer list m_listModel->insertRow(row); @@ -395,10 +397,11 @@ QStandardItem* PeerListWidget::addPeer(const QString& ip, const peer_info& peer) m_listModel->setData(m_listModel->index(row, PeerListDelegate::UP_SPEED), peer.payload_up_speed); m_listModel->setData(m_listModel->index(row, PeerListDelegate::TOT_DOWN), (qulonglong)peer.total_download); m_listModel->setData(m_listModel->index(row, PeerListDelegate::TOT_UP), (qulonglong)peer.total_upload); + m_listModel->setData(m_listModel->index(row, PeerListDelegate::RELEVANCE), getPeerRelevance(status, peer)); return m_listModel->item(row, PeerListDelegate::IP); } -void PeerListWidget::updatePeer(const QString& ip, const peer_info& peer) { +void PeerListWidget::updatePeer(const QString& ip, const libtorrent::torrent_status &status, const peer_info& peer) { QStandardItem *item = m_peerItems.value(ip); int row = item->row(); if (m_displayFlags) { @@ -422,6 +425,7 @@ void PeerListWidget::updatePeer(const QString& ip, const peer_info& peer) { m_listModel->setData(m_listModel->index(row, PeerListDelegate::UP_SPEED), peer.payload_up_speed); m_listModel->setData(m_listModel->index(row, PeerListDelegate::TOT_DOWN), (qulonglong)peer.total_download); m_listModel->setData(m_listModel->index(row, PeerListDelegate::TOT_UP), (qulonglong)peer.total_upload); + m_listModel->setData(m_listModel->index(row, PeerListDelegate::RELEVANCE), getPeerRelevance(status, peer)); } void PeerListWidget::handleResolved(const QString &ip, const QString &hostname) { @@ -582,3 +586,24 @@ void PeerListWidget::getFlags(const peer_info& peer, QString& flags, QString& to if (tooltip.endsWith(',', Qt::CaseInsensitive)) tooltip.chop(1); } + +double PeerListWidget::getPeerRelevance(const torrent_status& status, const libtorrent::peer_info &peer) +{ + int localMissing = 0; + int remoteHaves = 0; + libtorrent::bitfield local = status.pieces; + libtorrent::bitfield remote = peer.pieces; + + for (int i=0; igetCurrentTabWidget() != transferList || state != VISIBLE) return; try { - libtorrent::torrent_status status = h.status(torrent_handle::query_accurate_download_counters - | torrent_handle::query_distributed_copies - | torrent_handle::query_pieces); // Transfer infos if (stackedProperties->currentIndex() == PropTabBar::MAIN_TAB) { + libtorrent::torrent_status status = h.status(torrent_handle::query_accurate_download_counters + | torrent_handle::query_distributed_copies + | torrent_handle::query_pieces); + wasted->setText(misc::friendlyUnit(status.total_failed_bytes+status.total_redundant_bytes)); upTotal->setText(misc::friendlyUnit(status.all_time_upload) + " ("+misc::friendlyUnit(status.total_payload_upload)+" "+tr("this session")+")"); dlTotal->setText(misc::friendlyUnit(status.all_time_download) + " ("+misc::friendlyUnit(status.total_payload_download)+" "+tr("this session")+")"); @@ -382,6 +383,9 @@ void PropertiesWidget::loadDynamicData() { return; } if (stackedProperties->currentIndex() == PropTabBar::FILES_TAB) { + libtorrent::torrent_status status = h.status(torrent_handle::query_accurate_download_counters + | torrent_handle::query_distributed_copies + | torrent_handle::query_pieces); // Files progress if (h.is_valid() && status.has_metadata) { qDebug("Updating priorities in files tab");