1 #include "rpcconsole.h"
2 #include "ui_rpcconsole.h"
4 #include "clientmodel.h"
5 #include "bitcoinrpc.h"
7 #include "dialogwindowflags.h"
16 #include <QStringList>
18 #include <openssl/crypto.h>
21 // TODO: make it possible to filter out categories (esp debug messages when implemented)
22 // TODO: receive errors and debug messages through ClientModel
24 const int CONSOLE_HISTORY = 50;
26 const QSize ICON_SIZE(24, 24);
28 const int INITIAL_TRAFFIC_GRAPH_MINS = 30;
34 {"cmd-request", ":/icons/tx_input"},
35 {"cmd-reply", ":/icons/tx_output"},
36 {"cmd-error", ":/icons/tx_output"},
37 {"misc", ":/icons/tx_inout"},
41 /* Object for executing console RPC commands in a separate thread.
43 class RPCExecutor: public QObject
48 void request(const QString &command);
50 void reply(int category, const QString &command);
53 #include "rpcconsole.moc"
55 void RPCExecutor::start()
61 * Split shell command line into a list of arguments. Aims to emulate \c bash and friends.
63 * - Arguments are delimited with whitespace
64 * - Extra whitespace at the beginning and end and between arguments will be ignored
65 * - Text can be "double" or 'single' quoted
66 * - The backslash \c \ is used as escape character
67 * - Outside quotes, any character can be escaped
68 * - Within double quotes, only escape \c " and backslashes before a \c " or another backslash
69 * - Within single quotes, no escaping is possible and no special interpretation takes place
71 * @param[out] args Parsed arguments will be appended to this list
72 * @param[in] strCommand Command line to split
74 bool parseCommandLine(std::vector<std::string> &args, const std::string &strCommand)
83 STATE_ESCAPE_DOUBLEQUOTED
84 } state = STATE_EATING_SPACES;
86 foreach(char ch, strCommand)
90 case STATE_ARGUMENT: // In or after argument
91 case STATE_EATING_SPACES: // Handle runs of whitespace
94 case '"': state = STATE_DOUBLEQUOTED; break;
95 case '\'': state = STATE_SINGLEQUOTED; break;
96 case '\\': state = STATE_ESCAPE_OUTER; break;
97 case ' ': case '\n': case '\t':
98 if(state == STATE_ARGUMENT) // Space ends argument
100 args.push_back(curarg);
103 state = STATE_EATING_SPACES;
105 default: curarg += ch; state = STATE_ARGUMENT;
108 case STATE_SINGLEQUOTED: // Single-quoted string
111 case '\'': state = STATE_ARGUMENT; break;
112 default: curarg += ch;
115 case STATE_DOUBLEQUOTED: // Double-quoted string
118 case '"': state = STATE_ARGUMENT; break;
119 case '\\': state = STATE_ESCAPE_DOUBLEQUOTED; break;
120 default: curarg += ch;
123 case STATE_ESCAPE_OUTER: // '\' outside quotes
124 curarg += ch; state = STATE_ARGUMENT;
126 case STATE_ESCAPE_DOUBLEQUOTED: // '\' in double-quoted text
127 if(ch != '"' && ch != '\\') curarg += '\\'; // keep '\' for everything but the quote and '\' itself
128 curarg += ch; state = STATE_DOUBLEQUOTED;
132 switch(state) // final state
134 case STATE_EATING_SPACES:
137 args.push_back(curarg);
139 default: // ERROR to end in one of the other states
144 void RPCExecutor::request(const QString &command)
146 std::vector<std::string> args;
147 if(!parseCommandLine(args, command.toStdString()))
149 emit reply(RPCConsole::CMD_ERROR, QString("Parse error: unbalanced ' or \""));
153 return; // Nothing to do
156 std::string strPrint;
157 // Convert argument list to JSON objects in method-dependent way,
158 // and pass it along with the method name to the dispatcher.
159 json_spirit::Value result = tableRPC.execute(
161 RPCConvertValues(args[0], std::vector<std::string>(args.begin() + 1, args.end())));
163 // Format result reply
164 if (result.type() == json_spirit::null_type)
166 else if (result.type() == json_spirit::str_type)
167 strPrint = result.get_str();
169 strPrint = write_string(result, true);
171 emit reply(RPCConsole::CMD_REPLY, QString::fromStdString(strPrint));
173 catch (json_spirit::Object& objError)
175 try // Nice formatting for standard-format error
177 int code = find_value(objError, "code").get_int();
178 std::string message = find_value(objError, "message").get_str();
179 emit reply(RPCConsole::CMD_ERROR, QString::fromStdString(message) + " (code " + QString::number(code) + ")");
181 catch(std::runtime_error &) // raised when converting to invalid type, i.e. missing code or message
182 { // Show raw JSON object
183 emit reply(RPCConsole::CMD_ERROR, QString::fromStdString(write_string(json_spirit::Value(objError), false)));
186 catch (std::exception& e)
188 emit reply(RPCConsole::CMD_ERROR, QString("Error: ") + QString::fromStdString(e.what()));
192 RPCConsole::RPCConsole(QWidget *parent) :
194 ui(new Ui::RPCConsole),
200 ui->openDebugLogfileButton->setIcon(QIcon(":/icons/export"));
201 ui->openConfigurationfileButton->setIcon(QIcon(":/icons/export"));
202 ui->showCLOptionsButton->setIcon(QIcon(":/icons/options"));
205 // Install event filter for up and down arrow
206 ui->lineEdit->installEventFilter(this);
207 ui->messagesWidget->installEventFilter(this);
209 connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
210 connect(ui->btnClearTrafficGraph, SIGNAL(clicked()), ui->trafficGraph, SLOT(clear()));
212 // set library version labels
213 ui->openSSLVersion->setText(SSLeay_version(SSLEAY_VERSION));
214 ui->berkeleyDBVersion->setText(DbEnv::version(0, 0, 0));
217 setTrafficGraphRange(INITIAL_TRAFFIC_GRAPH_MINS);
222 RPCConsole::~RPCConsole()
228 bool RPCConsole::eventFilter(QObject* obj, QEvent *event)
230 if(event->type() == QEvent::KeyPress) // Special key handling
232 QKeyEvent *keyevt = static_cast<QKeyEvent*>(event);
233 int key = keyevt->key();
234 Qt::KeyboardModifiers mod = keyevt->modifiers();
237 case Qt::Key_Up: if(obj == ui->lineEdit) { browseHistory(-1); return true; } break;
238 case Qt::Key_Down: if(obj == ui->lineEdit) { browseHistory(1); return true; } break;
239 case Qt::Key_PageUp: /* pass paging keys to messages widget */
240 case Qt::Key_PageDown:
241 if(obj == ui->lineEdit)
243 QApplication::postEvent(ui->messagesWidget, new QKeyEvent(*keyevt));
249 // forward these events to lineEdit
250 if(obj == autoCompleter->popup()) {
251 QApplication::postEvent(ui->lineEdit, new QKeyEvent(*keyevt));
256 // Typing in messages widget brings focus to line edit, and redirects key there
257 // Exclude most combinations and keys that emit no text, except paste shortcuts
258 if(obj == ui->messagesWidget && (
259 (!mod && !keyevt->text().isEmpty() && key != Qt::Key_Tab) ||
260 ((mod & Qt::ControlModifier) && key == Qt::Key_V) ||
261 ((mod & Qt::ShiftModifier) && key == Qt::Key_Insert)))
263 ui->lineEdit->setFocus();
264 QApplication::postEvent(ui->lineEdit, new QKeyEvent(*keyevt));
265 autoCompleter->popup()->hide();
270 return QWidget::eventFilter(obj, event);
273 void RPCConsole::setClientModel(ClientModel *model)
275 this->clientModel = model;
276 ui->trafficGraph->setClientModel(model);
279 // Subscribe to information, replies, messages, errors
280 connect(model, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
281 connect(model, SIGNAL(numBlocksChanged(int,int)), this, SLOT(setNumBlocks(int,int)));
283 updateTrafficStats(model->getTotalBytesRecv(), model->getTotalBytesSent());
284 connect(model, SIGNAL(bytesChanged(quint64,quint64)), this, SLOT(updateTrafficStats(quint64, quint64)));
285 // Provide initial values
286 ui->clientVersion->setText(model->formatFullVersion());
287 ui->clientName->setText(model->clientName());
288 ui->buildDate->setText(model->formatBuildDate());
289 ui->startupTime->setText(model->formatClientStartupTime());
291 setNumConnections(model->getNumConnections());
292 ui->isTestNet->setChecked(model->isTestNet());
294 setNumBlocks(model->getNumBlocks(), model->getNumBlocksOfPeers());
296 //Setup autocomplete and attach it
297 QStringList wordList;
298 std::vector<std::string> commandList = tableRPC.listCommands();
299 for (size_t i = 0; i < commandList.size(); ++i)
301 wordList << commandList[i].c_str();
302 wordList << ("help " + commandList[i]).c_str();
306 autoCompleter = new QCompleter(wordList, this);
307 autoCompleter->setModelSorting(QCompleter::CaseSensitivelySortedModel);
308 // ui->lineEdit is initially disabled because running commands is only
309 // possible from now on.
310 ui->lineEdit->setEnabled(true);
311 ui->lineEdit->setCompleter(autoCompleter);
312 autoCompleter->popup()->installEventFilter(this);
316 static QString categoryClass(int category)
320 case RPCConsole::CMD_REQUEST: return "cmd-request"; break;
321 case RPCConsole::CMD_REPLY: return "cmd-reply"; break;
322 case RPCConsole::CMD_ERROR: return "cmd-error"; break;
323 default: return "misc";
327 void RPCConsole::clear()
329 ui->messagesWidget->clear();
332 ui->lineEdit->clear();
333 ui->lineEdit->setFocus();
335 // Add smoothly scaled icon images.
336 // (when using width/height on an img, Qt uses nearest instead of linear interpolation)
337 for(int i=0; ICON_MAPPING[i].url; ++i)
339 ui->messagesWidget->document()->addResource(
340 QTextDocument::ImageResource,
341 QUrl(ICON_MAPPING[i].url),
342 QImage(ICON_MAPPING[i].source).scaled(ICON_SIZE, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
345 // Set default style sheet
346 ui->messagesWidget->document()->setDefaultStyleSheet(
348 "td.time { color: #808080; padding-top: 3px; } "
349 "td.message { font-family: Monospace; } "
350 "td.cmd-request { color: #006060; } "
351 "td.cmd-error { color: red; } "
352 "b { color: #006060; } "
355 message(CMD_REPLY, (tr("Welcome to the NovaCoin RPC console.") + "<br>" +
356 tr("Use up and down arrows to navigate history, and <b>Ctrl-L</b> to clear screen.") + "<br>" +
357 tr("Type <b>help</b> for an overview of available commands.")), true);
360 void RPCConsole::message(int category, const QString &message, bool html)
362 QTime time = QTime::currentTime();
363 QString timeString = time.toString();
365 out += "<table><tr><td class=\"time\" width=\"65\">" + timeString + "</td>";
366 out += "<td class=\"icon\" width=\"32\"><img src=\"" + categoryClass(category) + "\"></td>";
367 out += "<td class=\"message " + categoryClass(category) + "\" valign=\"middle\">";
371 out += GUIUtil::HtmlEscape(message, true);
372 out += "</td></tr></table>";
373 ui->messagesWidget->append(out);
376 void RPCConsole::setNumConnections(int count)
381 QString connections = QString::number(count) + " (";
382 connections += tr("Inbound:") + " " + QString::number(clientModel->getNumConnections(CONNECTIONS_IN)) + " / ";
383 connections += tr("Outbound:") + " " + QString::number(clientModel->getNumConnections(CONNECTIONS_OUT)) + ")";
385 ui->numberOfConnections->setText(connections);
388 void RPCConsole::setNumBlocks(int count, int countOfPeers)
390 ui->numberOfBlocks->setText(QString::number(count));
391 ui->totalBlocks->setText(QString::number(countOfPeers));
394 // If there is no current number available display N/A instead of 0, which can't ever be true
395 ui->totalBlocks->setText(clientModel->getNumBlocksOfPeers() == 0 ? tr("N/A") : QString::number(clientModel->getNumBlocksOfPeers()));
396 ui->lastBlockTime->setText(clientModel->getLastBlockDate().toString());
400 void RPCConsole::on_lineEdit_returnPressed()
402 QString cmd = ui->lineEdit->text();
403 ui->lineEdit->clear();
407 message(CMD_REQUEST, cmd);
408 emit cmdRequest(cmd);
409 // Remove command, if already in history
410 history.removeOne(cmd);
411 // Append command to history
413 // Enforce maximum history size
414 while(history.size() > CONSOLE_HISTORY)
415 history.removeFirst();
416 // Set pointer to end of history
417 historyPtr = history.size();
418 // Scroll console view to end
423 void RPCConsole::browseHistory(int offset)
425 historyPtr += offset;
428 if(historyPtr > history.size())
429 historyPtr = history.size();
431 if(historyPtr < history.size())
432 cmd = history.at(historyPtr);
433 ui->lineEdit->setText(cmd);
436 void RPCConsole::startExecutor()
438 QThread* thread = new QThread;
439 RPCExecutor *executor = new RPCExecutor();
440 executor->moveToThread(thread);
442 // Notify executor when thread started (in executor thread)
443 connect(thread, SIGNAL(started()), executor, SLOT(start()));
444 // Replies from executor object must go to this object
445 connect(executor, SIGNAL(reply(int,QString)), this, SLOT(message(int,QString)));
446 // Requests from this object must go to executor
447 connect(this, SIGNAL(cmdRequest(QString)), executor, SLOT(request(QString)));
448 // On stopExecutor signal
449 // - queue executor for deletion (in execution thread)
450 // - quit the Qt event loop in the execution thread
451 connect(this, SIGNAL(stopExecutor()), executor, SLOT(deleteLater()));
452 connect(this, SIGNAL(stopExecutor()), thread, SLOT(quit()));
453 // Queue the thread for deletion (in this thread) when it is finished
454 connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
456 // Default implementation of QThread::run() simply spins up an event loop in the thread,
457 // which is what we want.
461 void RPCConsole::on_tabWidget_currentChanged(int index)
463 if(ui->tabWidget->widget(index) == ui->tab_console)
465 ui->lineEdit->setFocus();
469 void RPCConsole::on_openDebugLogfileButton_clicked()
471 GUIUtil::openDebugLogfile();
474 void RPCConsole::on_openConfigurationfileButton_clicked()
476 GUIUtil::openConfigfile();
479 void RPCConsole::scrollToEnd()
481 QScrollBar *scrollbar = ui->messagesWidget->verticalScrollBar();
482 scrollbar->setValue(scrollbar->maximum());
485 void RPCConsole::on_showCLOptionsButton_clicked()
487 GUIUtil::HelpMessageBox help;
490 void RPCConsole::on_sldGraphRange_valueChanged(int value)
492 const int multiplier = 5; // each position on the slider represents 5 min
493 int mins = value * multiplier;
494 setTrafficGraphRange(mins);
497 QString RPCConsole::FormatBytes(quint64 bytes)
500 return QString(tr("%1 B")).arg(bytes);
501 if(bytes < 1024 * 1024)
502 return QString(tr("%1 KB")).arg(bytes / 1024);
503 if(bytes < 1024 * 1024 * 1024)
504 return QString(tr("%1 MB")).arg(bytes / 1024 / 1024);
506 return QString(tr("%1 GB")).arg(bytes / 1024 / 1024 / 1024);
509 void RPCConsole::setTrafficGraphRange(int mins)
511 ui->trafficGraph->setGraphRangeMins(mins);
512 ui->lblGraphRange->setText(GUIUtil::formatDurationStr(mins * 60));
515 void RPCConsole::updateTrafficStats(quint64 totalBytesIn, quint64 totalBytesOut)
517 ui->lblBytesIn->setText(FormatBytes(totalBytesIn));
518 ui->lblBytesOut->setText(FormatBytes(totalBytesOut));
521 void RPCConsole::resizeEvent(QResizeEvent *event)
523 QWidget::resizeEvent(event);
526 void RPCConsole::showEvent(QShowEvent *event)
528 QWidget::showEvent(event);
534 void RPCConsole::hideEvent(QHideEvent *event)
536 QWidget::hideEvent(event);
542 void RPCConsole::keyPressEvent(QKeyEvent *event)
545 if(windowType() != Qt::Widget && event->key() == Qt::Key_Back)
550 if(windowType() != Qt::Widget && event->key() == Qt::Key_Escape)