1 #include "rpcconsole.h"
2 #include "ui_rpcconsole.h"
4 #include "clientmodel.h"
5 #include "bitcoinrpc.h"
7 #include "dialogwindowflags.h"
16 #include <QStringList>
17 #include <QAbstractItemView>
19 #include <openssl/crypto.h>
22 // TODO: make it possible to filter out categories (esp debug messages when implemented)
23 // TODO: receive errors and debug messages through ClientModel
25 const int CONSOLE_HISTORY = 50;
27 const QSize ICON_SIZE(24, 24);
29 const int INITIAL_TRAFFIC_GRAPH_MINS = 30;
35 {"cmd-request", ":/icons/tx_input"},
36 {"cmd-reply", ":/icons/tx_output"},
37 {"cmd-error", ":/icons/tx_output"},
38 {"misc", ":/icons/tx_inout"},
42 /* Object for executing console RPC commands in a separate thread.
44 class RPCExecutor: public QObject
49 void request(const QString &command);
51 void reply(int category, const QString &command);
54 #include "rpcconsole.moc"
56 void RPCExecutor::start()
62 * Split shell command line into a list of arguments. Aims to emulate \c bash and friends.
64 * - Arguments are delimited with whitespace
65 * - Extra whitespace at the beginning and end and between arguments will be ignored
66 * - Text can be "double" or 'single' quoted
67 * - The backslash \c \ is used as escape character
68 * - Outside quotes, any character can be escaped
69 * - Within double quotes, only escape \c " and backslashes before a \c " or another backslash
70 * - Within single quotes, no escaping is possible and no special interpretation takes place
72 * @param[out] args Parsed arguments will be appended to this list
73 * @param[in] strCommand Command line to split
75 bool parseCommandLine(std::vector<std::string> &args, const std::string &strCommand)
84 STATE_ESCAPE_DOUBLEQUOTED
85 } state = STATE_EATING_SPACES;
87 foreach(char ch, strCommand)
91 case STATE_ARGUMENT: // In or after argument
92 case STATE_EATING_SPACES: // Handle runs of whitespace
95 case '"': state = STATE_DOUBLEQUOTED; break;
96 case '\'': state = STATE_SINGLEQUOTED; break;
97 case '\\': state = STATE_ESCAPE_OUTER; break;
98 case ' ': case '\n': case '\t':
99 if(state == STATE_ARGUMENT) // Space ends argument
101 args.push_back(curarg);
104 state = STATE_EATING_SPACES;
106 default: curarg += ch; state = STATE_ARGUMENT;
109 case STATE_SINGLEQUOTED: // Single-quoted string
112 case '\'': state = STATE_ARGUMENT; break;
113 default: curarg += ch;
116 case STATE_DOUBLEQUOTED: // Double-quoted string
119 case '"': state = STATE_ARGUMENT; break;
120 case '\\': state = STATE_ESCAPE_DOUBLEQUOTED; break;
121 default: curarg += ch;
124 case STATE_ESCAPE_OUTER: // '\' outside quotes
125 curarg += ch; state = STATE_ARGUMENT;
127 case STATE_ESCAPE_DOUBLEQUOTED: // '\' in double-quoted text
128 if(ch != '"' && ch != '\\') curarg += '\\'; // keep '\' for everything but the quote and '\' itself
129 curarg += ch; state = STATE_DOUBLEQUOTED;
133 switch(state) // final state
135 case STATE_EATING_SPACES:
138 args.push_back(curarg);
140 default: // ERROR to end in one of the other states
145 void RPCExecutor::request(const QString &command)
147 std::vector<std::string> args;
148 if(!parseCommandLine(args, command.toStdString()))
150 emit reply(RPCConsole::CMD_ERROR, QString("Parse error: unbalanced ' or \""));
154 return; // Nothing to do
157 std::string strPrint;
158 // Convert argument list to JSON objects in method-dependent way,
159 // and pass it along with the method name to the dispatcher.
160 json_spirit::Value result = tableRPC.execute(
162 RPCConvertValues(args[0], std::vector<std::string>(args.begin() + 1, args.end())));
164 // Format result reply
165 if (result.type() == json_spirit::null_type)
167 else if (result.type() == json_spirit::str_type)
168 strPrint = result.get_str();
170 strPrint = write_string(result, true);
172 emit reply(RPCConsole::CMD_REPLY, QString::fromStdString(strPrint));
174 catch (json_spirit::Object& objError)
176 try // Nice formatting for standard-format error
178 int code = find_value(objError, "code").get_int();
179 std::string message = find_value(objError, "message").get_str();
180 emit reply(RPCConsole::CMD_ERROR, QString::fromStdString(message) + " (code " + QString::number(code) + ")");
182 catch(std::runtime_error &) // raised when converting to invalid type, i.e. missing code or message
183 { // Show raw JSON object
184 emit reply(RPCConsole::CMD_ERROR, QString::fromStdString(write_string(json_spirit::Value(objError), false)));
187 catch (std::exception& e)
189 emit reply(RPCConsole::CMD_ERROR, QString("Error: ") + QString::fromStdString(e.what()));
193 RPCConsole::RPCConsole(QWidget *parent) :
195 ui(new Ui::RPCConsole),
201 ui->openDebugLogfileButton->setIcon(QIcon(":/icons/export"));
202 ui->openConfigurationfileButton->setIcon(QIcon(":/icons/export"));
203 ui->showCLOptionsButton->setIcon(QIcon(":/icons/options"));
206 // Install event filter for up and down arrow
207 ui->lineEdit->installEventFilter(this);
208 ui->messagesWidget->installEventFilter(this);
210 connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
211 connect(ui->btnClearTrafficGraph, SIGNAL(clicked()), ui->trafficGraph, SLOT(clear()));
213 // set library version labels
214 ui->openSSLVersion->setText(SSLeay_version(SSLEAY_VERSION));
215 ui->berkeleyDBVersion->setText(DbEnv::version(0, 0, 0));
218 setTrafficGraphRange(INITIAL_TRAFFIC_GRAPH_MINS);
223 RPCConsole::~RPCConsole()
229 bool RPCConsole::eventFilter(QObject* obj, QEvent *event)
231 if(event->type() == QEvent::KeyPress) // Special key handling
233 QKeyEvent *keyevt = static_cast<QKeyEvent*>(event);
234 int key = keyevt->key();
235 Qt::KeyboardModifiers mod = keyevt->modifiers();
238 case Qt::Key_Up: if(obj == ui->lineEdit) { browseHistory(-1); return true; } break;
239 case Qt::Key_Down: if(obj == ui->lineEdit) { browseHistory(1); return true; } break;
240 case Qt::Key_PageUp: /* pass paging keys to messages widget */
241 case Qt::Key_PageDown:
242 if(obj == ui->lineEdit)
244 QApplication::postEvent(ui->messagesWidget, new QKeyEvent(*keyevt));
250 // forward these events to lineEdit
251 if(obj == (QObject*)autoCompleter->popup()) {
252 QApplication::postEvent(ui->lineEdit, new QKeyEvent(*keyevt));
257 // Typing in messages widget brings focus to line edit, and redirects key there
258 // Exclude most combinations and keys that emit no text, except paste shortcuts
259 if(obj == ui->messagesWidget && (
260 (!mod && !keyevt->text().isEmpty() && key != Qt::Key_Tab) ||
261 ((mod & Qt::ControlModifier) && key == Qt::Key_V) ||
262 ((mod & Qt::ShiftModifier) && key == Qt::Key_Insert)))
264 ui->lineEdit->setFocus();
265 QApplication::postEvent(ui->lineEdit, new QKeyEvent(*keyevt));
266 autoCompleter->popup()->hide();
271 return QWidget::eventFilter(obj, event);
274 void RPCConsole::setClientModel(ClientModel *model)
276 this->clientModel = model;
277 ui->trafficGraph->setClientModel(model);
280 // Subscribe to information, replies, messages, errors
281 connect(model, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
282 connect(model, SIGNAL(numBlocksChanged(int,int)), this, SLOT(setNumBlocks(int,int)));
284 updateTrafficStats(model->getTotalBytesRecv(), model->getTotalBytesSent());
285 connect(model, SIGNAL(bytesChanged(quint64,quint64)), this, SLOT(updateTrafficStats(quint64, quint64)));
286 // Provide initial values
287 ui->clientVersion->setText(model->formatFullVersion());
288 ui->clientName->setText(model->clientName());
289 ui->buildDate->setText(model->formatBuildDate());
290 ui->startupTime->setText(model->formatClientStartupTime());
292 setNumConnections(model->getNumConnections());
293 ui->isTestNet->setChecked(model->isTestNet());
295 setNumBlocks(model->getNumBlocks(), model->getNumBlocksOfPeers());
297 //Setup autocomplete and attach it
298 QStringList wordList;
299 std::vector<std::string> commandList = tableRPC.listCommands();
300 for (size_t i = 0; i < commandList.size(); ++i)
302 wordList << commandList[i].c_str();
303 wordList << ("help " + commandList[i]).c_str();
307 autoCompleter = new QCompleter(wordList, this);
308 autoCompleter->setModelSorting(QCompleter::CaseSensitivelySortedModel);
309 // ui->lineEdit is initially disabled because running commands is only
310 // possible from now on.
311 ui->lineEdit->setEnabled(true);
312 ui->lineEdit->setCompleter(autoCompleter);
313 autoCompleter->popup()->installEventFilter(this);
317 static QString categoryClass(int category)
321 case RPCConsole::CMD_REQUEST: return "cmd-request"; break;
322 case RPCConsole::CMD_REPLY: return "cmd-reply"; break;
323 case RPCConsole::CMD_ERROR: return "cmd-error"; break;
324 default: return "misc";
328 void RPCConsole::clear()
330 ui->messagesWidget->clear();
333 ui->lineEdit->clear();
334 ui->lineEdit->setFocus();
336 // Add smoothly scaled icon images.
337 // (when using width/height on an img, Qt uses nearest instead of linear interpolation)
338 for(int i=0; ICON_MAPPING[i].url; ++i)
340 ui->messagesWidget->document()->addResource(
341 QTextDocument::ImageResource,
342 QUrl(ICON_MAPPING[i].url),
343 QImage(ICON_MAPPING[i].source).scaled(ICON_SIZE, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
346 // Set default style sheet
347 ui->messagesWidget->document()->setDefaultStyleSheet(
349 "td.time { color: #808080; padding-top: 3px; } "
350 "td.message { font-family: Monospace; } "
351 "td.cmd-request { color: #006060; } "
352 "td.cmd-error { color: red; } "
353 "b { color: #006060; } "
356 message(CMD_REPLY, (tr("Welcome to the NovaCoin RPC console.") + "<br>" +
357 tr("Use up and down arrows to navigate history, and <b>Ctrl-L</b> to clear screen.") + "<br>" +
358 tr("Type <b>help</b> for an overview of available commands.")), true);
361 void RPCConsole::message(int category, const QString &message, bool html)
363 QTime time = QTime::currentTime();
364 QString timeString = time.toString();
366 out += "<table><tr><td class=\"time\" width=\"65\">" + timeString + "</td>";
367 out += "<td class=\"icon\" width=\"32\"><img src=\"" + categoryClass(category) + "\"></td>";
368 out += "<td class=\"message " + categoryClass(category) + "\" valign=\"middle\">";
372 out += GUIUtil::HtmlEscape(message, true);
373 out += "</td></tr></table>";
374 ui->messagesWidget->append(out);
377 void RPCConsole::setNumConnections(int count)
382 QString connections = QString::number(count) + " (";
383 connections += tr("Inbound:") + " " + QString::number(clientModel->getNumConnections(CONNECTIONS_IN)) + " / ";
384 connections += tr("Outbound:") + " " + QString::number(clientModel->getNumConnections(CONNECTIONS_OUT)) + ")";
386 ui->numberOfConnections->setText(connections);
389 void RPCConsole::setNumBlocks(int count, int countOfPeers)
391 ui->numberOfBlocks->setText(QString::number(count));
392 ui->totalBlocks->setText(QString::number(countOfPeers));
395 // If there is no current number available display N/A instead of 0, which can't ever be true
396 ui->totalBlocks->setText(clientModel->getNumBlocksOfPeers() == 0 ? tr("N/A") : QString::number(clientModel->getNumBlocksOfPeers()));
397 ui->lastBlockTime->setText(clientModel->getLastBlockDate().toString());
401 void RPCConsole::on_lineEdit_returnPressed()
403 QString cmd = ui->lineEdit->text();
404 ui->lineEdit->clear();
408 message(CMD_REQUEST, cmd);
409 emit cmdRequest(cmd);
410 // Remove command, if already in history
411 history.removeOne(cmd);
412 // Append command to history
414 // Enforce maximum history size
415 while(history.size() > CONSOLE_HISTORY)
416 history.removeFirst();
417 // Set pointer to end of history
418 historyPtr = history.size();
419 // Scroll console view to end
424 void RPCConsole::browseHistory(int offset)
426 historyPtr += offset;
429 if(historyPtr > history.size())
430 historyPtr = history.size();
432 if(historyPtr < history.size())
433 cmd = history.at(historyPtr);
434 ui->lineEdit->setText(cmd);
437 void RPCConsole::startExecutor()
439 QThread* thread = new QThread;
440 RPCExecutor *executor = new RPCExecutor();
441 executor->moveToThread(thread);
443 // Notify executor when thread started (in executor thread)
444 connect(thread, SIGNAL(started()), executor, SLOT(start()));
445 // Replies from executor object must go to this object
446 connect(executor, SIGNAL(reply(int,QString)), this, SLOT(message(int,QString)));
447 // Requests from this object must go to executor
448 connect(this, SIGNAL(cmdRequest(QString)), executor, SLOT(request(QString)));
449 // On stopExecutor signal
450 // - queue executor for deletion (in execution thread)
451 // - quit the Qt event loop in the execution thread
452 connect(this, SIGNAL(stopExecutor()), executor, SLOT(deleteLater()));
453 connect(this, SIGNAL(stopExecutor()), thread, SLOT(quit()));
454 // Queue the thread for deletion (in this thread) when it is finished
455 connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
457 // Default implementation of QThread::run() simply spins up an event loop in the thread,
458 // which is what we want.
462 void RPCConsole::on_tabWidget_currentChanged(int index)
464 if(ui->tabWidget->widget(index) == ui->tab_console)
466 ui->lineEdit->setFocus();
470 void RPCConsole::on_openDebugLogfileButton_clicked()
472 GUIUtil::openDebugLogfile();
475 void RPCConsole::on_openConfigurationfileButton_clicked()
477 GUIUtil::openConfigfile();
480 void RPCConsole::scrollToEnd()
482 QScrollBar *scrollbar = ui->messagesWidget->verticalScrollBar();
483 scrollbar->setValue(scrollbar->maximum());
486 void RPCConsole::on_showCLOptionsButton_clicked()
488 GUIUtil::HelpMessageBox help;
491 void RPCConsole::on_sldGraphRange_valueChanged(int value)
493 const int multiplier = 5; // each position on the slider represents 5 min
494 int mins = value * multiplier;
495 setTrafficGraphRange(mins);
498 QString RPCConsole::FormatBytes(quint64 bytes)
501 return QString(tr("%1 B")).arg(bytes);
502 if(bytes < 1024 * 1024)
503 return QString(tr("%1 KB")).arg(bytes / 1024);
504 if(bytes < 1024 * 1024 * 1024)
505 return QString(tr("%1 MB")).arg(bytes / 1024 / 1024);
507 return QString(tr("%1 GB")).arg(bytes / 1024 / 1024 / 1024);
510 void RPCConsole::setTrafficGraphRange(int mins)
512 ui->trafficGraph->setGraphRangeMins(mins);
513 ui->lblGraphRange->setText(GUIUtil::formatDurationStr(mins * 60));
516 void RPCConsole::updateTrafficStats(quint64 totalBytesIn, quint64 totalBytesOut)
518 ui->lblBytesIn->setText(FormatBytes(totalBytesIn));
519 ui->lblBytesOut->setText(FormatBytes(totalBytesOut));
522 void RPCConsole::resizeEvent(QResizeEvent *event)
524 QWidget::resizeEvent(event);
527 void RPCConsole::showEvent(QShowEvent *event)
529 QWidget::showEvent(event);
535 void RPCConsole::hideEvent(QHideEvent *event)
537 QWidget::hideEvent(event);
543 void RPCConsole::keyPressEvent(QKeyEvent *event)
546 if(windowType() != Qt::Widget && event->key() == Qt::Key_Back)
551 if(windowType() != Qt::Widget && event->key() == Qt::Key_Escape)