From 5b9c8c9cdd8e12d2e477840df9d6ab809a613c12 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Sat, 20 Mar 2021 04:58:04 +0200 Subject: [PATCH 1/4] =?UTF-8?q?qt,=20rpc:=20Add=20"Executing=E2=80=A6"=20m?= =?UTF-8?q?essage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/qt/rpcconsole.cpp | 9 ++++++++- src/qt/test/apptests.cpp | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/qt/rpcconsole.cpp b/src/qt/rpcconsole.cpp index 83a111e9c8..b5d639e46c 100644 --- a/src/qt/rpcconsole.cpp +++ b/src/qt/rpcconsole.cpp @@ -958,6 +958,8 @@ void RPCConsole::on_lineEdit_returnPressed() #endif message(CMD_REQUEST, QString::fromStdString(strFilteredCmd)); + //: A console message indicating an entered command is currently being executed. + message(CMD_REPLY, tr("Executing…")); Q_EMIT cmdRequest(cmd, m_last_wallet_model); cmd = QString::fromStdString(strFilteredCmd); @@ -1004,7 +1006,12 @@ void RPCConsole::startExecutor() executor->moveToThread(&thread); // Replies from executor object must go to this object - connect(executor, &RPCExecutor::reply, this, qOverload(&RPCConsole::message)); + connect(executor, &RPCExecutor::reply, this, [this](int category, const QString& command) { + // Remove "Executing…" message. + ui->messagesWidget->undo(); + message(category, command); + scrollToEnd(); + }); // Requests from this object must go to executor connect(this, &RPCConsole::cmdRequest, executor, &RPCExecutor::request); diff --git a/src/qt/test/apptests.cpp b/src/qt/test/apptests.cpp index c1d5f84be5..cb3dbd2267 100644 --- a/src/qt/test/apptests.cpp +++ b/src/qt/test/apptests.cpp @@ -40,7 +40,7 @@ void TestRpcCommand(RPCConsole* console) QTest::keyClicks(lineEdit, "getblockchaininfo"); QTest::keyClick(lineEdit, Qt::Key_Return); QVERIFY(mw_spy.wait(1000)); - QCOMPARE(mw_spy.count(), 2); + QCOMPARE(mw_spy.count(), 4); QString output = messagesWidget->toPlainText(); UniValue value; value.read(output.right(output.size() - output.lastIndexOf(QChar::ObjectReplacementCharacter) - 1).toStdString()); From ccf790287c53edbc7b18983e07f520823436c003 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Sat, 20 Mar 2021 04:59:14 +0200 Subject: [PATCH 2/4] qt, rpc, refactor: Return early in RPCConsole::on_lineEdit_returnPressed --- src/qt/rpcconsole.cpp | 84 +++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/src/qt/rpcconsole.cpp b/src/qt/rpcconsole.cpp index b5d639e46c..1384c8e35a 100644 --- a/src/qt/rpcconsole.cpp +++ b/src/qt/rpcconsole.cpp @@ -926,57 +926,57 @@ void RPCConsole::on_lineEdit_returnPressed() { QString cmd = ui->lineEdit->text(); - if(!cmd.isEmpty()) - { - std::string strFilteredCmd; - try { - std::string dummy; - if (!RPCParseCommandLine(nullptr, dummy, cmd.toStdString(), false, &strFilteredCmd)) { - // Failed to parse command, so we cannot even filter it for the history - throw std::runtime_error("Invalid command line"); - } - } catch (const std::exception& e) { - QMessageBox::critical(this, "Error", QString("Error: ") + QString::fromStdString(e.what())); - return; - } + if (cmd.isEmpty()) { + return; + } - ui->lineEdit->clear(); + std::string strFilteredCmd; + try { + std::string dummy; + if (!RPCParseCommandLine(nullptr, dummy, cmd.toStdString(), false, &strFilteredCmd)) { + // Failed to parse command, so we cannot even filter it for the history + throw std::runtime_error("Invalid command line"); + } + } catch (const std::exception& e) { + QMessageBox::critical(this, "Error", QString("Error: ") + QString::fromStdString(e.what())); + return; + } - cmdBeforeBrowsing = QString(); + ui->lineEdit->clear(); #ifdef ENABLE_WALLET - WalletModel* wallet_model = ui->WalletSelector->currentData().value(); + WalletModel* wallet_model = ui->WalletSelector->currentData().value(); - if (m_last_wallet_model != wallet_model) { - if (wallet_model) { - message(CMD_REQUEST, tr("Executing command using \"%1\" wallet").arg(wallet_model->getWalletName())); - } else { - message(CMD_REQUEST, tr("Executing command without any wallet")); - } - m_last_wallet_model = wallet_model; + if (m_last_wallet_model != wallet_model) { + if (wallet_model) { + message(CMD_REQUEST, tr("Executing command using \"%1\" wallet").arg(wallet_model->getWalletName())); + } else { + message(CMD_REQUEST, tr("Executing command without any wallet")); } -#endif + m_last_wallet_model = wallet_model; + } +#endif // ENABLE_WALLET - message(CMD_REQUEST, QString::fromStdString(strFilteredCmd)); - //: A console message indicating an entered command is currently being executed. - message(CMD_REPLY, tr("Executing…")); - Q_EMIT cmdRequest(cmd, m_last_wallet_model); - - cmd = QString::fromStdString(strFilteredCmd); - - // Remove command, if already in history - history.removeOne(cmd); - // Append command to history - history.append(cmd); - // Enforce maximum history size - while(history.size() > CONSOLE_HISTORY) - history.removeFirst(); - // Set pointer to end of history - historyPtr = history.size(); + message(CMD_REQUEST, QString::fromStdString(strFilteredCmd)); + //: A console message indicating an entered command is currently being executed. + message(CMD_REPLY, tr("Executing…")); + Q_EMIT cmdRequest(cmd, m_last_wallet_model); - // Scroll console view to end - scrollToEnd(); + cmd = QString::fromStdString(strFilteredCmd); + + // Remove command, if already in history + history.removeOne(cmd); + // Append command to history + history.append(cmd); + // Enforce maximum history size + while (history.size() > CONSOLE_HISTORY) { + history.removeFirst(); } + // Set pointer to end of history + historyPtr = history.size(); + + // Scroll console view to end + scrollToEnd(); } void RPCConsole::browseHistory(int offset) From 0c32b9c5273a4933bda90aa9eb9b7eace6dcaa14 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Sat, 20 Mar 2021 05:06:32 +0200 Subject: [PATCH 3/4] qt, rpc: Accept stop RPC even another command is executing While here, clean up the command input by calling the trimmed function on the input from the command prompt. --- src/qt/rpcconsole.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/qt/rpcconsole.cpp b/src/qt/rpcconsole.cpp index 1384c8e35a..f9a53e8d60 100644 --- a/src/qt/rpcconsole.cpp +++ b/src/qt/rpcconsole.cpp @@ -924,7 +924,7 @@ void RPCConsole::setMempoolSize(long numberOfTxs, size_t dynUsage) void RPCConsole::on_lineEdit_returnPressed() { - QString cmd = ui->lineEdit->text(); + QString cmd = ui->lineEdit->text().trimmed(); if (cmd.isEmpty()) { return; @@ -942,6 +942,13 @@ void RPCConsole::on_lineEdit_returnPressed() return; } + // A special case allows to request shutdown even a long-running command is executed. + if (cmd == QLatin1String("stop")) { + std::string dummy; + RPCExecuteCommandLine(m_node, dummy, cmd.toStdString()); + return; + } + ui->lineEdit->clear(); #ifdef ENABLE_WALLET From 38eb37c0bd29b4cb825de905e8eec87636a5221b Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Sat, 20 Mar 2021 05:01:27 +0200 Subject: [PATCH 4/4] qt, rpc: Do not accept command while executing another one --- src/qt/rpcconsole.cpp | 6 ++++++ src/qt/rpcconsole.h | 1 + 2 files changed, 7 insertions(+) diff --git a/src/qt/rpcconsole.cpp b/src/qt/rpcconsole.cpp index f9a53e8d60..c762f2b9c8 100644 --- a/src/qt/rpcconsole.cpp +++ b/src/qt/rpcconsole.cpp @@ -949,6 +949,10 @@ void RPCConsole::on_lineEdit_returnPressed() return; } + if (m_is_executing) { + return; + } + ui->lineEdit->clear(); #ifdef ENABLE_WALLET @@ -967,6 +971,7 @@ void RPCConsole::on_lineEdit_returnPressed() message(CMD_REQUEST, QString::fromStdString(strFilteredCmd)); //: A console message indicating an entered command is currently being executed. message(CMD_REPLY, tr("Executing…")); + m_is_executing = true; Q_EMIT cmdRequest(cmd, m_last_wallet_model); cmd = QString::fromStdString(strFilteredCmd); @@ -1018,6 +1023,7 @@ void RPCConsole::startExecutor() ui->messagesWidget->undo(); message(category, command); scrollToEnd(); + m_is_executing = false; }); // Requests from this object must go to executor diff --git a/src/qt/rpcconsole.h b/src/qt/rpcconsole.h index 55a1decceb..75f466642b 100644 --- a/src/qt/rpcconsole.h +++ b/src/qt/rpcconsole.h @@ -166,6 +166,7 @@ public Q_SLOTS: QCompleter *autoCompleter = nullptr; QThread thread; WalletModel* m_last_wallet_model{nullptr}; + bool m_is_executing{false}; /** Update UI with latest network info from model. */ void updateNetworkState();