Skip to content

Commit

Permalink
[Qt] add context menu with unban option to ban table
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasschnelli committed Sep 16, 2015
1 parent 5f42132 commit 770ca79
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
65 changes: 52 additions & 13 deletions src/qt/rpcconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,9 @@ RPCConsole::RPCConsole(const PlatformStyle *platformStyle, QWidget *parent) :
clientModel(0),
historyPtr(0),
cachedNodeid(-1),
contextMenu(0),
platformStyle(platformStyle)
peersTableContextMenu(0),
banTableContextMenu(0)
{
ui->setupUi(this);
GUIUtil::restoreWindowGeometry("nRPCConsoleWindow", this->size(), this);
Expand Down Expand Up @@ -351,9 +352,7 @@ void RPCConsole::setClientModel(ClientModel *model)
ui->peerWidget->setColumnWidth(PeerTableModel::Address, ADDRESS_COLUMN_WIDTH);
ui->peerWidget->setColumnWidth(PeerTableModel::Subversion, SUBVERSION_COLUMN_WIDTH);
ui->peerWidget->setColumnWidth(PeerTableModel::Ping, PING_COLUMN_WIDTH);

// set up ban table
ui->banlistWidget->setModel(model->getBanTableModel());
ui->peerWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);

// create context menu actions
QAction* disconnectAction = new QAction(tr("&Disconnect Node"), this);
Expand All @@ -363,15 +362,15 @@ void RPCConsole::setClientModel(ClientModel *model)
QAction* banAction365d = new QAction(tr("&Ban Node for 1 year"), this);

// create context menu
contextMenu = new QMenu();
contextMenu->addAction(disconnectAction);
contextMenu->addAction(banAction1h);
contextMenu->addAction(banAction24h);
contextMenu->addAction(banAction7d);
contextMenu->addAction(banAction365d);
peersTableContextMenu = new QMenu();
peersTableContextMenu->addAction(disconnectAction);
peersTableContextMenu->addAction(banAction1h);
peersTableContextMenu->addAction(banAction24h);
peersTableContextMenu->addAction(banAction7d);
peersTableContextMenu->addAction(banAction365d);

// context menu signals
connect(ui->peerWidget, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showMenu(const QPoint&)));
connect(ui->peerWidget, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showPeersTableContextMenu(const QPoint&)));
connect(disconnectAction, SIGNAL(triggered()), this, SLOT(disconnectSelectedNode()));

//add a signal mapping, use int instead of int64_t for bantime because signalmapper only supports int or objects
Expand All @@ -392,6 +391,26 @@ void RPCConsole::setClientModel(ClientModel *model)
this, SLOT(peerSelected(const QItemSelection &, const QItemSelection &)));
connect(model->getPeerTableModel(), SIGNAL(layoutChanged()), this, SLOT(peerLayoutChanged()));

// set up ban table
ui->banlistWidget->setModel(model->getBanTableModel());
ui->banlistWidget->verticalHeader()->hide();
ui->banlistWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui->banlistWidget->setColumnWidth(BanTableModel::Address, ADDRESS_COLUMN_WIDTH);
ui->banlistWidget->setColumnWidth(BanTableModel::Bantime, PING_COLUMN_WIDTH);
ui->banlistWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->banlistWidget->setSelectionMode(QAbstractItemView::SingleSelection);
ui->banlistWidget->setContextMenuPolicy(Qt::CustomContextMenu);
ui->banlistWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);

// create context menu actions
QAction* unbanAction = new QAction(tr("&Unban Node"), this);
banTableContextMenu = new QMenu();
banTableContextMenu->addAction(unbanAction);

// context menu signals
connect(ui->banlistWidget, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showBanTableContextMenu(const QPoint&)));
connect(unbanAction, SIGNAL(triggered()), this, SLOT(unbanSelectedNode()));

// Provide initial values
ui->clientVersion->setText(model->formatFullVersion());
ui->clientUserAgent->setText(model->formatSubVersion());
Expand Down Expand Up @@ -744,11 +763,18 @@ void RPCConsole::hideEvent(QHideEvent *event)
clientModel->getPeerTableModel()->stopAutoRefresh();
}

void RPCConsole::showMenu(const QPoint& point)
void RPCConsole::showPeersTableContextMenu(const QPoint& point)
{
QModelIndex index = ui->peerWidget->indexAt(point);
if (index.isValid())
contextMenu->exec(QCursor::pos());
peersTableContextMenu->exec(QCursor::pos());
}

void RPCConsole::showBanTableContextMenu(const QPoint& point)
{
QModelIndex index = ui->banlistWidget->indexAt(point);
if (index.isValid())
banTableContextMenu->exec(QCursor::pos());
}

void RPCConsole::disconnectSelectedNode()
Expand Down Expand Up @@ -782,6 +808,19 @@ void RPCConsole::banSelectedNode(int bantime)
}
}

void RPCConsole::unbanSelectedNode()
{
// Get currently selected peer address
QString strNode = GUIUtil::getEntryData(ui->banlistWidget, 0, BanTableModel::Address);
CSubNet possibleSubnet(strNode.toStdString());

if (possibleSubnet.IsValid())
{
CNode::Unban(possibleSubnet);
clientModel->updateBanlist();
}
}

void RPCConsole::clearSelectedNode()
{
ui->peerWidget->selectionModel()->clearSelection();
Expand Down
9 changes: 7 additions & 2 deletions src/qt/rpcconsole.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ private Q_SLOTS:
void showEvent(QShowEvent *event);
void hideEvent(QHideEvent *event);
/** Show custom context menu on Peers tab */
void showMenu(const QPoint& point);
void showPeersTableContextMenu(const QPoint& point);
/** Show custom context menu on Bans tab */
void showBanTableContextMenu(const QPoint& point);

public Q_SLOTS:
void clear();
Expand All @@ -82,6 +84,8 @@ public Q_SLOTS:
void disconnectSelectedNode();
/** Ban a selected node on the Peers tab */
void banSelectedNode(int bantime);
/** Unban a selected node on the Bans tab */
void unbanSelectedNode();

Q_SIGNALS:
// For RPC command executor
Expand Down Expand Up @@ -109,9 +113,10 @@ public Q_SLOTS:
QStringList history;
int historyPtr;
NodeId cachedNodeid;
QMenu *contextMenu;
const PlatformStyle *platformStyle;
RPCTimerInterface *rpcTimerInterface;
QMenu *peersTableContextMenu;
QMenu *banTableContextMenu;
};

#endif // BITCOIN_QT_RPCCONSOLE_H

0 comments on commit 770ca79

Please sign in to comment.