1 #include "rpcconsole.h"
2 #include "ui_rpcconsole.h"
4 #include "clientmodel.h"
5 #include "bitcoinrpc.h"
16 #include <openssl/crypto.h>
18 // TODO: make it possible to filter out categories (esp debug messages when implemented)
19 // TODO: receive errors and debug messages through ClientModel
21 const int CONSOLE_SCROLLBACK = 50;
22 const int CONSOLE_HISTORY = 50;
24 const QSize ICON_SIZE(24, 24);
30 {"cmd-request", ":/icons/tx_input"},
31 {"cmd-reply", ":/icons/tx_output"},
32 {"cmd-error", ":/icons/tx_output"},
33 {"misc", ":/icons/tx_inout"},
37 /* Object for executing console RPC commands in a separate thread.
39 class RPCExecutor: public QObject
44 void request(const QString &command);
46 void reply(int category, const QString &command);
49 #include "rpcconsole.moc"
51 void RPCExecutor::start()
57 * Split shell command line into a list of arguments. Aims to emulate \c bash and friends.
59 * - Arguments are delimited with whitespace
60 * - Extra whitespace at the beginning and end and between arguments will be ignored
61 * - Text can be "double" or 'single' quoted
62 * - The backslash \c \ is used as escape character
63 * - Outside quotes, any character can be escaped
64 * - Within double quotes, only escape \c " and backslashes before a \c " or another backslash
65 * - Within single quotes, no escaping is possible and no special interpretation takes place
67 * @param[out] args Parsed arguments will be appended to this list
68 * @param[in] strCommand Command line to split
70 bool parseCommandLine(std::vector<std::string> &args, const std::string &strCommand)
79 STATE_ESCAPE_DOUBLEQUOTED
80 } state = STATE_EATING_SPACES;
82 foreach(char ch, strCommand)
86 case STATE_ARGUMENT: // In or after argument
87 case STATE_EATING_SPACES: // Handle runs of whitespace
90 case '"': state = STATE_DOUBLEQUOTED; break;
91 case '\'': state = STATE_SINGLEQUOTED; break;
92 case '\\': state = STATE_ESCAPE_OUTER; break;
93 case ' ': case '\n': case '\t':
94 if(state == STATE_ARGUMENT) // Space ends argument
96 args.push_back(curarg);
99 state = STATE_EATING_SPACES;
101 default: curarg += ch; state = STATE_ARGUMENT;
104 case STATE_SINGLEQUOTED: // Single-quoted string
107 case '\'': state = STATE_ARGUMENT; break;
108 default: curarg += ch;
111 case STATE_DOUBLEQUOTED: // Double-quoted string
114 case '"': state = STATE_ARGUMENT; break;
115 case '\\': state = STATE_ESCAPE_DOUBLEQUOTED; break;
116 default: curarg += ch;
119 case STATE_ESCAPE_OUTER: // '\' outside quotes
120 curarg += ch; state = STATE_ARGUMENT;
122 case STATE_ESCAPE_DOUBLEQUOTED: // '\' in double-quoted text
123 if(ch != '"' && ch != '\\') curarg += '\\'; // keep '\' for everything but the quote and '\' itself
124 curarg += ch; state = STATE_DOUBLEQUOTED;
128 switch(state) // final state
130 case STATE_EATING_SPACES:
133 args.push_back(curarg);
135 default: // ERROR to end in one of the other states
140 void RPCExecutor::request(const QString &command)
142 std::vector<std::string> args;
143 if(!parseCommandLine(args, command.toStdString()))
145 emit reply(RPCConsole::CMD_ERROR, QString("Parse error: unbalanced ' or \""));
149 return; // Nothing to do
152 std::string strPrint;
153 // Convert argument list to JSON objects in method-dependent way,
154 // and pass it along with the method name to the dispatcher.
155 json_spirit::Value result = tableRPC.execute(
157 RPCConvertValues(args[0], std::vector<std::string>(args.begin() + 1, args.end())));
159 // Format result reply
160 if (result.type() == json_spirit::null_type)
162 else if (result.type() == json_spirit::str_type)
163 strPrint = result.get_str();
165 strPrint = write_string(result, true);
167 emit reply(RPCConsole::CMD_REPLY, QString::fromStdString(strPrint));
169 catch (json_spirit::Object& objError)
171 try // Nice formatting for standard-format error
173 int code = find_value(objError, "code").get_int();
174 std::string message = find_value(objError, "message").get_str();
175 emit reply(RPCConsole::CMD_ERROR, QString::fromStdString(message) + " (code " + QString::number(code) + ")");
177 catch(std::runtime_error &) // raised when converting to invalid type, i.e. missing code or message
178 { // Show raw JSON object
179 emit reply(RPCConsole::CMD_ERROR, QString::fromStdString(write_string(json_spirit::Value(objError), false)));
182 catch (std::exception& e)
184 emit reply(RPCConsole::CMD_ERROR, QString("Error: ") + QString::fromStdString(e.what()));
188 RPCConsole::RPCConsole(QWidget *parent) :
190 ui(new Ui::RPCConsole),
196 ui->openDebugLogfileButton->setIcon(QIcon(":/icons/export"));
197 ui->showCLOptionsButton->setIcon(QIcon(":/icons/options"));
200 // Install event filter for up and down arrow
201 ui->lineEdit->installEventFilter(this);
202 ui->messagesWidget->installEventFilter(this);
204 connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
206 // set OpenSSL version label
207 ui->openSSLVersion->setText(SSLeay_version(SSLEAY_VERSION));
214 RPCConsole::~RPCConsole()
220 bool RPCConsole::eventFilter(QObject* obj, QEvent *event)
222 if(event->type() == QEvent::KeyPress) // Special key handling
224 QKeyEvent *keyevt = static_cast<QKeyEvent*>(event);
225 int key = keyevt->key();
226 Qt::KeyboardModifiers mod = keyevt->modifiers();
229 case Qt::Key_Up: if(obj == ui->lineEdit) { browseHistory(-1); return true; } break;
230 case Qt::Key_Down: if(obj == ui->lineEdit) { browseHistory(1); return true; } break;
231 case Qt::Key_PageUp: /* pass paging keys to messages widget */
232 case Qt::Key_PageDown:
233 if(obj == ui->lineEdit)
235 QApplication::postEvent(ui->messagesWidget, new QKeyEvent(*keyevt));
240 // Typing in messages widget brings focus to line edit, and redirects key there
241 // Exclude most combinations and keys that emit no text, except paste shortcuts
242 if(obj == ui->messagesWidget && (
243 (!mod && !keyevt->text().isEmpty() && key != Qt::Key_Tab) ||
244 ((mod & Qt::ControlModifier) && key == Qt::Key_V) ||
245 ((mod & Qt::ShiftModifier) && key == Qt::Key_Insert)))
247 ui->lineEdit->setFocus();
248 QApplication::postEvent(ui->lineEdit, new QKeyEvent(*keyevt));
253 return QDialog::eventFilter(obj, event);
256 void RPCConsole::setClientModel(ClientModel *model)
258 this->clientModel = model;
261 // Subscribe to information, replies, messages, errors
262 connect(model, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
263 connect(model, SIGNAL(numBlocksChanged(int,int)), this, SLOT(setNumBlocks(int,int)));
265 // Provide initial values
266 ui->clientVersion->setText(model->formatFullVersion());
267 ui->clientName->setText(model->clientName());
268 ui->buildDate->setText(model->formatBuildDate());
269 ui->startupTime->setText(model->formatClientStartupTime());
271 setNumConnections(model->getNumConnections());
272 ui->isTestNet->setChecked(model->isTestNet());
274 setNumBlocks(model->getNumBlocks(), model->getNumBlocksOfPeers());
278 static QString categoryClass(int category)
282 case RPCConsole::CMD_REQUEST: return "cmd-request"; break;
283 case RPCConsole::CMD_REPLY: return "cmd-reply"; break;
284 case RPCConsole::CMD_ERROR: return "cmd-error"; break;
285 default: return "misc";
289 void RPCConsole::clear()
291 ui->messagesWidget->clear();
292 ui->lineEdit->clear();
293 ui->lineEdit->setFocus();
295 // Add smoothly scaled icon images.
296 // (when using width/height on an img, Qt uses nearest instead of linear interpolation)
297 for(int i=0; ICON_MAPPING[i].url; ++i)
299 ui->messagesWidget->document()->addResource(
300 QTextDocument::ImageResource,
301 QUrl(ICON_MAPPING[i].url),
302 QImage(ICON_MAPPING[i].source).scaled(ICON_SIZE, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
305 // Set default style sheet
306 ui->messagesWidget->document()->setDefaultStyleSheet(
308 "td.time { color: #808080; padding-top: 3px; } "
309 "td.message { font-family: Monospace; font-size: 12px; } "
310 "td.cmd-request { color: #006060; } "
311 "td.cmd-error { color: red; } "
312 "b { color: #006060; } "
315 message(CMD_REPLY, (tr("Welcome to the NovaCoin RPC console.") + "<br>" +
316 tr("Use up and down arrows to navigate history, and <b>Ctrl-L</b> to clear screen.") + "<br>" +
317 tr("Type <b>help</b> for an overview of available commands.")), true);
320 void RPCConsole::message(int category, const QString &message, bool html)
322 QTime time = QTime::currentTime();
323 QString timeString = time.toString();
325 out += "<table><tr><td class=\"time\" width=\"65\">" + timeString + "</td>";
326 out += "<td class=\"icon\" width=\"32\"><img src=\"" + categoryClass(category) + "\"></td>";
327 out += "<td class=\"message " + categoryClass(category) + "\" valign=\"middle\">";
331 out += GUIUtil::HtmlEscape(message, true);
332 out += "</td></tr></table>";
333 ui->messagesWidget->append(out);
336 void RPCConsole::setNumConnections(int count)
338 ui->numberOfConnections->setText(QString::number(count));
341 void RPCConsole::setNumBlocks(int count, int countOfPeers)
343 ui->numberOfBlocks->setText(QString::number(count));
344 ui->totalBlocks->setText(QString::number(countOfPeers));
347 // If there is no current number available display N/A instead of 0, which can't ever be true
348 ui->totalBlocks->setText(clientModel->getNumBlocksOfPeers() == 0 ? tr("N/A") : QString::number(clientModel->getNumBlocksOfPeers()));
349 ui->lastBlockTime->setText(clientModel->getLastBlockDate().toString());
353 void RPCConsole::on_lineEdit_returnPressed()
355 QString cmd = ui->lineEdit->text();
356 ui->lineEdit->clear();
360 message(CMD_REQUEST, cmd);
361 emit cmdRequest(cmd);
362 // Truncate history from current position
363 history.erase(history.begin() + historyPtr, history.end());
364 // Append command to history
366 // Enforce maximum history size
367 while(history.size() > CONSOLE_HISTORY)
368 history.removeFirst();
369 // Set pointer to end of history
370 historyPtr = history.size();
371 // Scroll console view to end
376 void RPCConsole::browseHistory(int offset)
378 historyPtr += offset;
381 if(historyPtr > history.size())
382 historyPtr = history.size();
384 if(historyPtr < history.size())
385 cmd = history.at(historyPtr);
386 ui->lineEdit->setText(cmd);
389 void RPCConsole::startExecutor()
391 QThread* thread = new QThread;
392 RPCExecutor *executor = new RPCExecutor();
393 executor->moveToThread(thread);
395 // Notify executor when thread started (in executor thread)
396 connect(thread, SIGNAL(started()), executor, SLOT(start()));
397 // Replies from executor object must go to this object
398 connect(executor, SIGNAL(reply(int,QString)), this, SLOT(message(int,QString)));
399 // Requests from this object must go to executor
400 connect(this, SIGNAL(cmdRequest(QString)), executor, SLOT(request(QString)));
401 // On stopExecutor signal
402 // - queue executor for deletion (in execution thread)
403 // - quit the Qt event loop in the execution thread
404 connect(this, SIGNAL(stopExecutor()), executor, SLOT(deleteLater()));
405 connect(this, SIGNAL(stopExecutor()), thread, SLOT(quit()));
406 // Queue the thread for deletion (in this thread) when it is finished
407 connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
409 // Default implementation of QThread::run() simply spins up an event loop in the thread,
410 // which is what we want.
414 void RPCConsole::on_tabWidget_currentChanged(int index)
416 if(ui->tabWidget->widget(index) == ui->tab_console)
418 ui->lineEdit->setFocus();
422 void RPCConsole::on_openDebugLogfileButton_clicked()
424 GUIUtil::openDebugLogfile();
427 void RPCConsole::scrollToEnd()
429 QScrollBar *scrollbar = ui->messagesWidget->verticalScrollBar();
430 scrollbar->setValue(scrollbar->maximum());
433 void RPCConsole::on_showCLOptionsButton_clicked()
435 GUIUtil::HelpMessageBox help;