mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-07-16 02:03:07 -07:00
Follow project coding style. Issue #2192.
This commit is contained in:
parent
1e3a57ad92
commit
aea6c38b33
2 changed files with 424 additions and 415 deletions
|
@ -51,8 +51,10 @@
|
|||
#include "peerlistsortmodel.h"
|
||||
#include "peerlistwidget.h"
|
||||
|
||||
PeerListWidget::PeerListWidget(PropertiesWidget *parent):
|
||||
QTreeView(parent), m_properties(parent), m_displayFlags(false)
|
||||
PeerListWidget::PeerListWidget(PropertiesWidget *parent)
|
||||
: QTreeView(parent)
|
||||
, m_properties(parent)
|
||||
, m_displayFlags(false)
|
||||
{
|
||||
// Load settings
|
||||
loadSettings();
|
||||
|
@ -85,7 +87,7 @@ PeerListWidget::PeerListWidget(PropertiesWidget *parent):
|
|||
//Explicitly set the column visibility. When columns are added/removed
|
||||
//between versions this prevents some of them being hidden due to
|
||||
//incorrect restoreState() being used.
|
||||
for (unsigned int i=0; i<PeerListDelegate::IP_HIDDEN; i++)
|
||||
for (unsigned int i = 0; i < PeerListDelegate::IP_HIDDEN; i++)
|
||||
showColumn(i);
|
||||
hideColumn(PeerListDelegate::IP_HIDDEN);
|
||||
hideColumn(PeerListDelegate::COL_COUNT);
|
||||
|
@ -94,7 +96,7 @@ PeerListWidget::PeerListWidget(PropertiesWidget *parent):
|
|||
//To also mitigate the above issue, we have to resize each column when
|
||||
//its size is 0, because explicitly 'showing' the column isn't enough
|
||||
//in the above scenario.
|
||||
for (unsigned int i=0; i<PeerListDelegate::IP_HIDDEN; i++)
|
||||
for (unsigned int i = 0; i < PeerListDelegate::IP_HIDDEN; i++)
|
||||
if (!columnWidth(i))
|
||||
resizeColumnToContents(i);
|
||||
// Context menu
|
||||
|
@ -110,7 +112,7 @@ PeerListWidget::PeerListWidget(PropertiesWidget *parent):
|
|||
// SIGNAL/SLOT
|
||||
connect(header(), SIGNAL(sectionClicked(int)), SLOT(handleSortColumnChanged(int)));
|
||||
handleSortColumnChanged(header()->sortIndicatorSection());
|
||||
copyHotkey = new QShortcut(QKeySequence(Qt::ControlModifier + Qt::Key_C), this, SLOT(copySelectedPeers()), 0, Qt::WidgetShortcut);
|
||||
m_copyHotkey = new QShortcut(QKeySequence(Qt::ControlModifier + Qt::Key_C), this, SLOT(copySelectedPeers()), 0, Qt::WidgetShortcut);
|
||||
}
|
||||
|
||||
PeerListWidget::~PeerListWidget()
|
||||
|
@ -121,7 +123,7 @@ PeerListWidget::~PeerListWidget()
|
|||
delete m_listDelegate;
|
||||
if (m_resolver)
|
||||
delete m_resolver;
|
||||
delete copyHotkey;
|
||||
delete m_copyHotkey;
|
||||
}
|
||||
|
||||
void PeerListWidget::updatePeerHostNameResolutionState()
|
||||
|
@ -129,11 +131,11 @@ void PeerListWidget::updatePeerHostNameResolutionState()
|
|||
if (Preferences::instance()->resolvePeerHostNames()) {
|
||||
if (!m_resolver) {
|
||||
m_resolver = new Net::ReverseResolution(this);
|
||||
connect(m_resolver, SIGNAL(ipResolved(QString,QString)), SLOT(handleResolved(QString,QString)));
|
||||
connect(m_resolver, SIGNAL(ipResolved(QString, QString)), SLOT(handleResolved(QString, QString)));
|
||||
loadPeers(m_properties->getCurrentTorrent(), true);
|
||||
}
|
||||
} else {
|
||||
if (m_resolver)
|
||||
}
|
||||
else if (m_resolver) {
|
||||
delete m_resolver;
|
||||
}
|
||||
}
|
||||
|
@ -156,7 +158,7 @@ void PeerListWidget::updatePeerCountryResolutionState()
|
|||
void PeerListWidget::showPeerListMenu(const QPoint&)
|
||||
{
|
||||
QMenu menu;
|
||||
bool empty_menu = true;
|
||||
bool emptyMenu = true;
|
||||
BitTorrent::TorrentHandle *const torrent = m_properties->getCurrentTorrent();
|
||||
if (!torrent) return;
|
||||
|
||||
|
@ -164,7 +166,7 @@ void PeerListWidget::showPeerListMenu(const QPoint&)
|
|||
QAction *addPeerAct = 0;
|
||||
if (!torrent->isQueued() && !torrent->isChecking()) {
|
||||
addPeerAct = menu.addAction(GuiIconProvider::instance()->getIcon("user-group-new"), tr("Add a new peer..."));
|
||||
empty_menu = false;
|
||||
emptyMenu = false;
|
||||
}
|
||||
QAction *banAct = 0;
|
||||
QAction *copyPeerAct = 0;
|
||||
|
@ -172,9 +174,9 @@ void PeerListWidget::showPeerListMenu(const QPoint&)
|
|||
copyPeerAct = menu.addAction(GuiIconProvider::instance()->getIcon("edit-copy"), tr("Copy selected"));
|
||||
menu.addSeparator();
|
||||
banAct = menu.addAction(GuiIconProvider::instance()->getIcon("user-group-delete"), tr("Ban peer permanently"));
|
||||
empty_menu = false;
|
||||
emptyMenu = false;
|
||||
}
|
||||
if (empty_menu) return;
|
||||
if (emptyMenu) return;
|
||||
QAction *act = menu.exec(QCursor::pos());
|
||||
if (act == 0) return;
|
||||
if (act == addPeerAct) {
|
||||
|
@ -243,7 +245,8 @@ void PeerListWidget::copySelectedPeers()
|
|||
QApplication::clipboard()->setText(selectedPeers.join("\n"));
|
||||
}
|
||||
|
||||
void PeerListWidget::clear() {
|
||||
void PeerListWidget::clear()
|
||||
{
|
||||
qDebug("clearing peer list");
|
||||
m_peerItems.clear();
|
||||
m_peerAddresses.clear();
|
||||
|
@ -255,44 +258,47 @@ void PeerListWidget::clear() {
|
|||
}
|
||||
}
|
||||
|
||||
void PeerListWidget::loadSettings() {
|
||||
void PeerListWidget::loadSettings()
|
||||
{
|
||||
header()->restoreState(Preferences::instance()->getPeerListState());
|
||||
}
|
||||
|
||||
void PeerListWidget::saveSettings() const {
|
||||
void PeerListWidget::saveSettings() const
|
||||
{
|
||||
Preferences::instance()->setPeerListState(header()->saveState());
|
||||
}
|
||||
|
||||
void PeerListWidget::loadPeers(BitTorrent::TorrentHandle *const torrent, bool force_hostname_resolution) {
|
||||
void PeerListWidget::loadPeers(BitTorrent::TorrentHandle *const torrent, bool forceHostnameResolution)
|
||||
{
|
||||
if (!torrent) return;
|
||||
|
||||
QList<BitTorrent::PeerInfo> peers = torrent->peers();
|
||||
QSet<QString> old_peers_set = m_peerItems.keys().toSet();
|
||||
QSet<QString> oldeersSet = m_peerItems.keys().toSet();
|
||||
|
||||
foreach (const BitTorrent::PeerInfo &peer, peers) {
|
||||
BitTorrent::PeerAddress addr = peer.address();
|
||||
if (addr.ip.isNull()) continue;
|
||||
|
||||
QString peer_ip = addr.ip.toString();
|
||||
if (m_peerItems.contains(peer_ip)) {
|
||||
QString peerIp = addr.ip.toString();
|
||||
if (m_peerItems.contains(peerIp)) {
|
||||
// Update existing peer
|
||||
updatePeer(peer_ip, torrent, peer);
|
||||
old_peers_set.remove(peer_ip);
|
||||
if (force_hostname_resolution && m_resolver) {
|
||||
m_resolver->resolve(peer_ip);
|
||||
updatePeer(peerIp, torrent, peer);
|
||||
oldeersSet.remove(peerIp);
|
||||
if (forceHostnameResolution && m_resolver)
|
||||
m_resolver->resolve(peerIp);
|
||||
}
|
||||
} else {
|
||||
else {
|
||||
// Add new peer
|
||||
m_peerItems[peer_ip] = addPeer(peer_ip, torrent, peer);
|
||||
m_peerAddresses[peer_ip] = addr;
|
||||
m_peerItems[peerIp] = addPeer(peerIp, torrent, peer);
|
||||
m_peerAddresses[peerIp] = addr;
|
||||
// Resolve peer host name is asked
|
||||
if (m_resolver)
|
||||
m_resolver->resolve(peer_ip);
|
||||
m_resolver->resolve(peerIp);
|
||||
}
|
||||
}
|
||||
// Delete peers that are gone
|
||||
QSetIterator<QString> it(old_peers_set);
|
||||
while(it.hasNext()) {
|
||||
QSetIterator<QString> it(oldeersSet);
|
||||
while (it.hasNext()) {
|
||||
const QString& ip = it.next();
|
||||
m_missingFlags.remove(ip);
|
||||
m_peerAddresses.remove(ip);
|
||||
|
@ -301,7 +307,8 @@ void PeerListWidget::loadPeers(BitTorrent::TorrentHandle *const torrent, bool fo
|
|||
}
|
||||
}
|
||||
|
||||
QStandardItem* PeerListWidget::addPeer(const QString& ip, BitTorrent::TorrentHandle *const torrent, const BitTorrent::PeerInfo &peer) {
|
||||
QStandardItem* PeerListWidget::addPeer(const QString& ip, BitTorrent::TorrentHandle *const torrent, const BitTorrent::PeerInfo &peer)
|
||||
{
|
||||
int row = m_listModel->rowCount();
|
||||
// Adding Peer to peer list
|
||||
m_listModel->insertRow(row);
|
||||
|
@ -313,9 +320,10 @@ QStandardItem* PeerListWidget::addPeer(const QString& ip, BitTorrent::TorrentHan
|
|||
const QIcon ico = GuiIconProvider::instance()->getFlagIcon(peer.country());
|
||||
if (!ico.isNull()) {
|
||||
m_listModel->setData(m_listModel->index(row, PeerListDelegate::COUNTRY), ico, Qt::DecorationRole);
|
||||
const QString country_name = Net::GeoIPManager::CountryName(peer.country());
|
||||
m_listModel->setData(m_listModel->index(row, PeerListDelegate::COUNTRY), country_name, Qt::ToolTipRole);
|
||||
} else {
|
||||
const QString countryName = Net::GeoIPManager::CountryName(peer.country());
|
||||
m_listModel->setData(m_listModel->index(row, PeerListDelegate::COUNTRY), countryName, Qt::ToolTipRole);
|
||||
}
|
||||
else {
|
||||
m_missingFlags.insert(ip);
|
||||
}
|
||||
}
|
||||
|
@ -334,15 +342,16 @@ QStandardItem* PeerListWidget::addPeer(const QString& ip, BitTorrent::TorrentHan
|
|||
return m_listModel->item(row, PeerListDelegate::IP);
|
||||
}
|
||||
|
||||
void PeerListWidget::updatePeer(const QString &ip, BitTorrent::TorrentHandle *const torrent, const BitTorrent::PeerInfo &peer) {
|
||||
void PeerListWidget::updatePeer(const QString &ip, BitTorrent::TorrentHandle *const torrent, const BitTorrent::PeerInfo &peer)
|
||||
{
|
||||
QStandardItem *item = m_peerItems.value(ip);
|
||||
int row = item->row();
|
||||
if (m_displayFlags) {
|
||||
const QIcon ico = GuiIconProvider::instance()->getFlagIcon(peer.country());
|
||||
if (!ico.isNull()) {
|
||||
m_listModel->setData(m_listModel->index(row, PeerListDelegate::COUNTRY), ico, Qt::DecorationRole);
|
||||
const QString country_name = Net::GeoIPManager::CountryName(peer.country());
|
||||
m_listModel->setData(m_listModel->index(row, PeerListDelegate::COUNTRY), country_name, Qt::ToolTipRole);
|
||||
const QString countryName = Net::GeoIPManager::CountryName(peer.country());
|
||||
m_listModel->setData(m_listModel->index(row, PeerListDelegate::COUNTRY), countryName, Qt::ToolTipRole);
|
||||
m_missingFlags.remove(ip);
|
||||
}
|
||||
}
|
||||
|
@ -361,7 +370,8 @@ void PeerListWidget::updatePeer(const QString &ip, BitTorrent::TorrentHandle *co
|
|||
m_listModel->setData(m_listModel->index(row, PeerListDelegate::RELEVANCE), getPeerRelevance(torrent->pieces(), peer.pieces()));
|
||||
}
|
||||
|
||||
void PeerListWidget::handleResolved(const QString &ip, const QString &hostname) {
|
||||
void PeerListWidget::handleResolved(const QString &ip, const QString &hostname)
|
||||
{
|
||||
QStandardItem *item = m_peerItems.value(ip, 0);
|
||||
if (item) {
|
||||
qDebug("Resolved %s -> %s", qPrintable(ip), qPrintable(hostname));
|
||||
|
@ -374,7 +384,8 @@ void PeerListWidget::handleSortColumnChanged(int col)
|
|||
if (col == PeerListDelegate::COUNTRY) {
|
||||
qDebug("Sorting by decoration");
|
||||
m_proxyModel->setSortRole(Qt::ToolTipRole);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
m_proxyModel->setSortRole(Qt::DisplayRole);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,22 +54,20 @@ QT_END_NAMESPACE
|
|||
|
||||
namespace BitTorrent
|
||||
{
|
||||
|
||||
class TorrentHandle;
|
||||
class PeerInfo;
|
||||
struct PeerAddress;
|
||||
|
||||
class TorrentHandle;
|
||||
class PeerInfo;
|
||||
struct PeerAddress;
|
||||
}
|
||||
|
||||
class PeerListWidget : public QTreeView {
|
||||
class PeerListWidget: public QTreeView
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
PeerListWidget(PropertiesWidget *parent);
|
||||
explicit PeerListWidget(PropertiesWidget *parent);
|
||||
~PeerListWidget();
|
||||
|
||||
public slots:
|
||||
void loadPeers(BitTorrent::TorrentHandle *const torrent, bool force_hostname_resolution = false);
|
||||
void loadPeers(BitTorrent::TorrentHandle *const torrent, bool forceHostnameResolution = false);
|
||||
QStandardItem *addPeer(const QString &ip, BitTorrent::TorrentHandle *const torrent, const BitTorrent::PeerInfo &peer);
|
||||
void updatePeer(const QString &ip, BitTorrent::TorrentHandle *const torrent, const BitTorrent::PeerInfo &peer);
|
||||
void handleResolved(const QString &ip, const QString &hostname);
|
||||
|
@ -77,7 +75,7 @@ public slots:
|
|||
void updatePeerCountryResolutionState();
|
||||
void clear();
|
||||
|
||||
protected slots:
|
||||
private slots:
|
||||
void loadSettings();
|
||||
void saveSettings() const;
|
||||
void showPeerListMenu(const QPoint&);
|
||||
|
@ -99,7 +97,7 @@ private:
|
|||
QPointer<Net::ReverseResolution> m_resolver;
|
||||
PropertiesWidget *m_properties;
|
||||
bool m_displayFlags;
|
||||
QShortcut *copyHotkey;
|
||||
QShortcut *m_copyHotkey;
|
||||
};
|
||||
|
||||
#endif // PEERLISTWIDGET_H
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue