From 63ed69789ba869429ab765a0f5b9e4a4b1151fc5 Mon Sep 17 00:00:00 2001 From: ngosang Date: Wed, 15 Jul 2015 00:10:58 +0200 Subject: [PATCH] Allow to copy all peers with a keyboard shortcut --- src/gui/properties/peerlistwidget.cpp | 51 +++++++++++++++------------ src/gui/properties/peerlistwidget.h | 5 ++- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/gui/properties/peerlistwidget.cpp b/src/gui/properties/peerlistwidget.cpp index 7094c9b53..a84125f8c 100644 --- a/src/gui/properties/peerlistwidget.cpp +++ b/src/gui/properties/peerlistwidget.cpp @@ -109,6 +109,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); } PeerListWidget::~PeerListWidget() @@ -119,6 +120,7 @@ PeerListWidget::~PeerListWidget() delete m_listDelegate; if (m_resolver) delete m_resolver; + delete copyHotkey; } void PeerListWidget::updatePeerHostNameResolutionState() @@ -151,19 +153,6 @@ void PeerListWidget::showPeerListMenu(const QPoint&) BitTorrent::TorrentHandle *const torrent = m_properties->getCurrentTorrent(); if (!torrent) return; - QModelIndexList selectedIndexes = selectionModel()->selectedRows(); - QStringList selectedPeerIPs; - QStringList selectedPeerIPPort; - foreach (const QModelIndex &index, selectedIndexes) { - int row = m_proxyModel->mapToSource(index).row(); - QString myip = m_listModel->data(m_listModel->index(row, PeerListDelegate::IP_HIDDEN)).toString(); - QString myport = m_listModel->data(m_listModel->index(row, PeerListDelegate::PORT)).toString(); - selectedPeerIPs << myip; - if (myip.indexOf(".") == -1) // IPv6 - selectedPeerIPPort << "[" + myip + "]:" + myport; - else // IPv4 - selectedPeerIPPort << myip + ":" + myport; - } // Add Peer Action QAction *addPeerAct = 0; if (!torrent->isQueued() && !torrent->isChecking()) { @@ -172,7 +161,7 @@ void PeerListWidget::showPeerListMenu(const QPoint&) } QAction *banAct = 0; QAction *copyPeerAct = 0; - if (!selectedPeerIPs.isEmpty()) { + if (!selectionModel()->selectedRows().isEmpty()) { 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")); @@ -201,28 +190,28 @@ void PeerListWidget::showPeerListMenu(const QPoint&) return; } if (act == banAct) { - banSelectedPeers(selectedPeerIPs); + banSelectedPeers(); return; } if (act == copyPeerAct) { -#if defined(Q_OS_WIN) || defined(Q_OS_OS2) - QApplication::clipboard()->setText(selectedPeerIPPort.join("\r\n")); -#else - QApplication::clipboard()->setText(selectedPeerIPPort.join("\n")); -#endif + copySelectedPeers(); + return; } } -void PeerListWidget::banSelectedPeers(const QStringList& peer_ips) +void PeerListWidget::banSelectedPeers() { // Confirm first - int ret = QMessageBox::question(this, tr("Are you sure? -- qBittorrent"), tr("Are you sure you want to ban permanently the selected peers?"), + int ret = QMessageBox::question(this, tr("Ban peer permanently"), tr("Are you sure you want to ban permanently the selected peers?"), tr("&Yes"), tr("&No"), QString(), 0, 1); if (ret) return; - foreach (const QString &ip, peer_ips) { + QModelIndexList selectedIndexes = selectionModel()->selectedRows(); + foreach (const QModelIndex &index, selectedIndexes) { + int row = m_proxyModel->mapToSource(index).row(); + QString ip = m_listModel->data(m_listModel->index(row, PeerListDelegate::IP_HIDDEN)).toString(); qDebug("Banning peer %s...", ip.toLocal8Bit().data()); Logger::instance()->addMessage(tr("Manually banning peer %1...").arg(ip)); BitTorrent::Session::instance()->banIP(ip); @@ -231,6 +220,22 @@ void PeerListWidget::banSelectedPeers(const QStringList& peer_ips) loadPeers(m_properties->getCurrentTorrent()); } +void PeerListWidget::copySelectedPeers() +{ + QModelIndexList selectedIndexes = selectionModel()->selectedRows(); + QStringList selectedPeers; + foreach (const QModelIndex &index, selectedIndexes) { + int row = m_proxyModel->mapToSource(index).row(); + QString ip = m_listModel->data(m_listModel->index(row, PeerListDelegate::IP_HIDDEN)).toString(); + QString myport = m_listModel->data(m_listModel->index(row, PeerListDelegate::PORT)).toString(); + if (ip.indexOf(".") == -1) // IPv6 + selectedPeers << "[" + ip + "]:" + myport; + else // IPv4 + selectedPeers << ip + ":" + myport; + } + QApplication::clipboard()->setText(selectedPeers.join("\n")); +} + void PeerListWidget::clear() { qDebug("clearing peer list"); m_peerItems.clear(); diff --git a/src/gui/properties/peerlistwidget.h b/src/gui/properties/peerlistwidget.h index e6f792f76..656f87998 100644 --- a/src/gui/properties/peerlistwidget.h +++ b/src/gui/properties/peerlistwidget.h @@ -35,6 +35,7 @@ #include #include #include +#include namespace Net { @@ -80,7 +81,8 @@ protected slots: void loadSettings(); void saveSettings() const; void showPeerListMenu(const QPoint&); - void banSelectedPeers(const QStringList& peer_ips); + void banSelectedPeers(); + void copySelectedPeers(); void handleSortColumnChanged(int col); private: @@ -97,6 +99,7 @@ private: QPointer m_resolver; PropertiesWidget *m_properties; bool m_displayFlags; + QShortcut *copyHotkey; }; #endif // PEERLISTWIDGET_H