Allow to copy all peers with a keyboard shortcut

This commit is contained in:
ngosang 2015-07-15 00:10:58 +02:00
parent cef3c9a34d
commit 63ed69789b
2 changed files with 32 additions and 24 deletions

View File

@ -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();

View File

@ -35,6 +35,7 @@
#include <QHash>
#include <QPointer>
#include <QSet>
#include <QShortcut>
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<Net::ReverseResolution> m_resolver;
PropertiesWidget *m_properties;
bool m_displayFlags;
QShortcut *copyHotkey;
};
#endif // PEERLISTWIDGET_H