1 #include "rpcconsole.h"
2 #include "ui_rpcconsole.h"
4 #include "clientmodel.h"
5 #include "bitcoinrpc.h"
7 #include "dialogwindowflags.h"
17 #include <openssl/crypto.h>
19 // TODO: make it possible to filter out categories (esp debug messages when implemented)
20 // TODO: receive errors and debug messages through ClientModel
22 const int CONSOLE_SCROLLBACK = 50;
23 const int CONSOLE_HISTORY = 50;
25 const QSize ICON_SIZE(24, 24);
27 const int INITIAL_TRAFFIC_GRAPH_MINS = 30;
33 {"cmd-request", ":/icons/tx_input"},
34 {"cmd-reply", ":/icons/tx_output"},
35 {"cmd-error", ":/icons/tx_output"},
36 {"misc", ":/icons/tx_inout"},
40 /* Object for executing console RPC commands in a separate thread.
42 class RPCExecutor: public QObject
47 void request(const QString &command);
49 void reply(int category, const QString &command);
52 #include "rpcconsole.moc"
54 void RPCExecutor::start()
60 * Split shell command line into a list of arguments. Aims to emulate \c bash and friends.
62 * - Arguments are delimited with whitespace
63 * - Extra whitespace at the beginning and end and between arguments will be ignored
64 * - Text can be "double" or 'single' quoted
65 * - The backslash \c \ is used as escape character
66 * - Outside quotes, any character can be escaped
67 * - Within double quotes, only escape \c " and backslashes before a \c " or another backslash
68 * - Within single quotes, no escaping is possible and no special interpretation takes place
70 * @param[out] args Parsed arguments will be appended to this list
71 * @param[in] strCommand Command line to split
73 bool parseCommandLine(std::vector<std::string> &args, const std::string &strCommand)
82 STATE_ESCAPE_DOUBLEQUOTED
83 } state = STATE_EATING_SPACES;
85 foreach(char ch, strCommand)
89 case STATE_ARGUMENT: // In or after argument
90 case STATE_EATING_SPACES: // Handle runs of whitespace
93 case '"': state = STATE_DOUBLEQUOTED; break;
94 case '\'': state = STATE_SINGLEQUOTED; break;
95 case '\\': state = STATE_ESCAPE_OUTER; break;
96 case ' ': case '\n': case '\t':
97 if(state == STATE_ARGUMENT) // Space ends argument
99 args.push_back(curarg);
102 state = STATE_EATING_SPACES;
104 default: curarg += ch; state = STATE_ARGUMENT;
107 case STATE_SINGLEQUOTED: // Single-quoted string
110 case '\'': state = STATE_ARGUMENT; break;
111 default: curarg += ch;
114 case STATE_DOUBLEQUOTED: // Double-quoted string
117 case '"': state = STATE_ARGUMENT; break;
118 case '\\': state = STATE_ESCAPE_DOUBLEQUOTED; break;
119 default: curarg += ch;
122 case STATE_ESCAPE_OUTER: // '\' outside quotes
123 curarg += ch; state = STATE_ARGUMENT;
125 case STATE_ESCAPE_DOUBLEQUOTED: // '\' in double-quoted text
126 if(ch != '"' && ch != '\\') curarg += '\\'; // keep '\' for everything but the quote and '\' itself
127 curarg += ch; state = STATE_DOUBLEQUOTED;
131 switch(state) // final state
133 case STATE_EATING_SPACES:
136 args.push_back(curarg);
138 default: // ERROR to end in one of the other states
143 void RPCExecutor::request(const QString &command)
145 std::vector<std::string> args;
146 if(!parseCommandLine(args, command.toStdString()))
148 emit reply(RPCConsole::CMD_ERROR, QString("Parse error: unbalanced ' or \""));
152 return; // Nothing to do
155 std::string strPrint;
156 // Convert argument list to JSON objects in method-dependent way,
157 // and pass it along with the method name to the dispatcher.
158 json_spirit::Value result = tableRPC.execute(
160 RPCConvertValues(args[0], std::vector<std::string>(args.begin() + 1, args.end())));
162 // Format result reply
163 if (result.type() == json_spirit::null_type)
165 else if (result.type() == json_spirit::str_type)
166 strPrint = result.get_str();
168 strPrint = write_string(result, true);
170 emit reply(RPCConsole::CMD_REPLY, QString::fromStdString(strPrint));
172 catch (json_spirit::Object& objError)
174 try // Nice formatting for standard-format error
176 int code = find_value(objError, "code").get_int();
177 std::string message = find_value(objError, "message").get_str();
178 emit reply(RPCConsole::CMD_ERROR, QString::fromStdString(message) + " (code " + QString::number(code) + ")");
180 catch(std::runtime_error &) // raised when converting to invalid type, i.e. missing code or message
181 { // Show raw JSON object
182 emit reply(RPCConsole::CMD_ERROR, QString::fromStdString(write_string(json_spirit::Value(objError), false)));
185 catch (std::exception& e)
187 emit reply(RPCConsole::CMD_ERROR, QString("Error: ") + QString::fromStdString(e.what()));
191 RPCConsole::RPCConsole(QWidget *parent) :
193 ui(new Ui::RPCConsole),
199 ui->openDebugLogfileButton->setIcon(QIcon(":/icons/export"));
200 ui->showCLOptionsButton->setIcon(QIcon(":/icons/options"));
203 // Install event filter for up and down arrow
204 ui->lineEdit->installEventFilter(this);
205 ui->messagesWidget->installEventFilter(this);
207 connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
208 connect(ui->btnClearTrafficGraph, SIGNAL(clicked()), ui->trafficGraph, SLOT(clear()));
210 // set OpenSSL version label
211 ui->openSSLVersion->setText(SSLeay_version(SSLEAY_VERSION));
214 setTrafficGraphRange(INITIAL_TRAFFIC_GRAPH_MINS);
219 RPCConsole::~RPCConsole()
225 bool RPCConsole::eventFilter(QObject* obj, QEvent *event)
227 if(event->type() == QEvent::KeyPress) // Special key handling
229 QKeyEvent *keyevt = static_cast<QKeyEvent*>(event);
230 int key = keyevt->key();
231 Qt::KeyboardModifiers mod = keyevt->modifiers();
234 case Qt::Key_Up: if(obj == ui->lineEdit) { browseHistory(-1); return true; } break;
235 case Qt::Key_Down: if(obj == ui->lineEdit) { browseHistory(1); return true; } break;
236 case Qt::Key_PageUp: /* pass paging keys to messages widget */
237 case Qt::Key_PageDown:
238 if(obj == ui->lineEdit)
240 QApplication::postEvent(ui->messagesWidget, new QKeyEvent(*keyevt));
245 // Typing in messages widget brings focus to line edit, and redirects key there
246 // Exclude most combinations and keys that emit no text, except paste shortcuts
247 if(obj == ui->messagesWidget && (
248 (!mod && !keyevt->text().isEmpty() && key != Qt::Key_Tab) ||
249 ((mod & Qt::ControlModifier) && key == Qt::Key_V) ||
250 ((mod & Qt::ShiftModifier) && key == Qt::Key_Insert)))
252 ui->lineEdit->setFocus();
253 QApplication::postEvent(ui->lineEdit, new QKeyEvent(*keyevt));
258 return QWidget::eventFilter(obj, event);
261 void RPCConsole::setClientModel(ClientModel *model)
263 this->clientModel = model;
264 ui->trafficGraph->setClientModel(model);
267 // Subscribe to information, replies, messages, errors
268 connect(model, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
269 connect(model, SIGNAL(numBlocksChanged(int,int)), this, SLOT(setNumBlocks(int,int)));
271 updateTrafficStats(model->getTotalBytesRecv(), model->getTotalBytesSent());
272 connect(model, SIGNAL(bytesChanged(quint64,quint64)), this, SLOT(updateTrafficStats(quint64, quint64)));
273 // Provide initial values
274 ui->clientVersion->setText(model->formatFullVersion());
275 ui->clientName->setText(model->clientName());
276 ui->buildDate->setText(model->formatBuildDate());
277 ui->startupTime->setText(model->formatClientStartupTime());
279 setNumConnections(model->getNumConnections());
280 ui->isTestNet->setChecked(model->isTestNet());
282 setNumBlocks(model->getNumBlocks(), model->getNumBlocksOfPeers());
286 static QString categoryClass(int category)
290 case RPCConsole::CMD_REQUEST: return "cmd-request"; break;
291 case RPCConsole::CMD_REPLY: return "cmd-reply"; break;
292 case RPCConsole::CMD_ERROR: return "cmd-error"; break;
293 default: return "misc";
297 void RPCConsole::clear()
299 ui->messagesWidget->clear();
300 ui->lineEdit->clear();
301 ui->lineEdit->setFocus();
303 // Add smoothly scaled icon images.
304 // (when using width/height on an img, Qt uses nearest instead of linear interpolation)
305 for(int i=0; ICON_MAPPING[i].url; ++i)
307 ui->messagesWidget->document()->addResource(
308 QTextDocument::ImageResource,
309 QUrl(ICON_MAPPING[i].url),
310 QImage(ICON_MAPPING[i].source).scaled(ICON_SIZE, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
313 // Set default style sheet
314 ui->messagesWidget->document()->setDefaultStyleSheet(
316 "td.time { color: #808080; padding-top: 3px; } "
317 "td.message { font-family: Monospace; font-size: 12px; } "
318 "td.cmd-request { color: #006060; } "
319 "td.cmd-error { color: red; } "
320 "b { color: #006060; } "
323 message(CMD_REPLY, (tr("Welcome to the NovaCoin RPC console.") + "<br>" +
324 tr("Use up and down arrows to navigate history, and <b>Ctrl-L</b> to clear screen.") + "<br>" +
325 tr("Type <b>help</b> for an overview of available commands.")), true);
328 void RPCConsole::message(int category, const QString &message, bool html)
330 QTime time = QTime::currentTime();
331 QString timeString = time.toString();
333 out += "<table><tr><td class=\"time\" width=\"65\">" + timeString + "</td>";
334 out += "<td class=\"icon\" width=\"32\"><img src=\"" + categoryClass(category) + "\"></td>";
335 out += "<td class=\"message " + categoryClass(category) + "\" valign=\"middle\">";
339 out += GUIUtil::HtmlEscape(message, true);
340 out += "</td></tr></table>";
341 ui->messagesWidget->append(out);
344 void RPCConsole::setNumConnections(int count)
346 ui->numberOfConnections->setText(QString::number(count));
349 void RPCConsole::setNumBlocks(int count, int countOfPeers)
351 ui->numberOfBlocks->setText(QString::number(count));
352 ui->totalBlocks->setText(QString::number(countOfPeers));
355 // If there is no current number available display N/A instead of 0, which can't ever be true
356 ui->totalBlocks->setText(clientModel->getNumBlocksOfPeers() == 0 ? tr("N/A") : QString::number(clientModel->getNumBlocksOfPeers()));
357 ui->lastBlockTime->setText(clientModel->getLastBlockDate().toString());
361 void RPCConsole::on_lineEdit_returnPressed()
363 QString cmd = ui->lineEdit->text();
364 ui->lineEdit->clear();
368 message(CMD_REQUEST, cmd);
369 emit cmdRequest(cmd);
370 // Truncate history from current position
371 history.erase(history.begin() + historyPtr, history.end());
372 // Append command to history
374 // Enforce maximum history size
375 while(history.size() > CONSOLE_HISTORY)
376 history.removeFirst();
377 // Set pointer to end of history
378 historyPtr = history.size();
379 // Scroll console view to end
384 void RPCConsole::browseHistory(int offset)
386 historyPtr += offset;
389 if(historyPtr > history.size())
390 historyPtr = history.size();
392 if(historyPtr < history.size())
393 cmd = history.at(historyPtr);
394 ui->lineEdit->setText(cmd);
397 void RPCConsole::startExecutor()
399 QThread* thread = new QThread;
400 RPCExecutor *executor = new RPCExecutor();
401 executor->moveToThread(thread);
403 // Notify executor when thread started (in executor thread)
404 connect(thread, SIGNAL(started()), executor, SLOT(start()));
405 // Replies from executor object must go to this object
406 connect(executor, SIGNAL(reply(int,QString)), this, SLOT(message(int,QString)));
407 // Requests from this object must go to executor
408 connect(this, SIGNAL(cmdRequest(QString)), executor, SLOT(request(QString)));
409 // On stopExecutor signal
410 // - queue executor for deletion (in execution thread)
411 // - quit the Qt event loop in the execution thread
412 connect(this, SIGNAL(stopExecutor()), executor, SLOT(deleteLater()));
413 connect(this, SIGNAL(stopExecutor()), thread, SLOT(quit()));
414 // Queue the thread for deletion (in this thread) when it is finished
415 connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
417 // Default implementation of QThread::run() simply spins up an event loop in the thread,
418 // which is what we want.
422 void RPCConsole::on_tabWidget_currentChanged(int index)
424 if(ui->tabWidget->widget(index) == ui->tab_console)
426 ui->lineEdit->setFocus();
430 void RPCConsole::on_openDebugLogfileButton_clicked()
432 GUIUtil::openDebugLogfile();
435 void RPCConsole::scrollToEnd()
437 QScrollBar *scrollbar = ui->messagesWidget->verticalScrollBar();
438 scrollbar->setValue(scrollbar->maximum());
441 void RPCConsole::on_showCLOptionsButton_clicked()
443 GUIUtil::HelpMessageBox help;
446 void RPCConsole::on_sldGraphRange_valueChanged(int value)
448 const int multiplier = 5; // each position on the slider represents 5 min
449 int mins = value * multiplier;
450 setTrafficGraphRange(mins);
453 QString RPCConsole::FormatBytes(quint64 bytes)
456 return QString(tr("%1 B")).arg(bytes);
457 if(bytes < 1024 * 1024)
458 return QString(tr("%1 KB")).arg(bytes / 1024);
459 if(bytes < 1024 * 1024 * 1024)
460 return QString(tr("%1 MB")).arg(bytes / 1024 / 1024);
462 return QString(tr("%1 GB")).arg(bytes / 1024 / 1024 / 1024);
465 void RPCConsole::setTrafficGraphRange(int mins)
467 ui->trafficGraph->setGraphRangeMins(mins);
468 ui->lblGraphRange->setText(GUIUtil::formatDurationStr(mins * 60));
471 void RPCConsole::updateTrafficStats(quint64 totalBytesIn, quint64 totalBytesOut)
473 ui->lblBytesIn->setText(FormatBytes(totalBytesIn));
474 ui->lblBytesOut->setText(FormatBytes(totalBytesOut));
477 void RPCConsole::resizeEvent(QResizeEvent *event)
479 QWidget::resizeEvent(event);
482 void RPCConsole::showEvent(QShowEvent *event)
484 QWidget::showEvent(event);
490 void RPCConsole::hideEvent(QHideEvent *event)
492 QWidget::hideEvent(event);
498 void RPCConsole::keyPressEvent(QKeyEvent *event)
500 if(windowType() != Qt::Widget && event->key() == Qt::Key_Escape)