Update CMakeLists.txt - play with openssl
[novacoin.git] / src / qt / rpcconsole.cpp
1 #include "rpcconsole.h"
2 #include "ui_rpcconsole.h"
3
4 #include "clientmodel.h"
5 #include "bitcoinrpc.h"
6 #include "guiutil.h"
7 #include "dialogwindowflags.h"
8
9 #include <QTime>
10 #include <QTimer>
11 #include <QThread>
12 #include <QTextEdit>
13 #include <QKeyEvent>
14 #include <QUrl>
15 #include <QScrollBar>
16 #include <QStringList>
17 #include <QAbstractItemView>
18
19 #include <openssl/crypto.h>
20 #include <db_cxx.h>
21
22 // TODO: make it possible to filter out categories (esp debug messages when implemented)
23 // TODO: receive errors and debug messages through ClientModel
24
25 const int CONSOLE_HISTORY = 50;
26
27 const QSize ICON_SIZE(24, 24);
28
29 const int INITIAL_TRAFFIC_GRAPH_MINS = 30;
30
31 const struct {
32     const char *url;
33     const char *source;
34 } ICON_MAPPING[] = {
35     {"cmd-request", ":/icons/tx_input"},
36     {"cmd-reply", ":/icons/tx_output"},
37     {"cmd-error", ":/icons/tx_output"},
38     {"misc", ":/icons/tx_inout"},
39     {NULL, NULL}
40 };
41
42 /* Object for executing console RPC commands in a separate thread.
43 */
44 class RPCExecutor: public QObject
45 {
46     Q_OBJECT
47 public slots:
48     void start();
49     void request(const QString &command);
50 signals:
51     void reply(int category, const QString &command);
52 };
53
54 #include "rpcconsole.moc"
55
56 void RPCExecutor::start()
57 {
58    // Nothing to do
59 }
60
61 /**
62  * Split shell command line into a list of arguments. Aims to emulate \c bash and friends.
63  *
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
71  *
72  * @param[out]   args        Parsed arguments will be appended to this list
73  * @param[in]    strCommand  Command line to split
74  */
75 bool parseCommandLine(std::vector<std::string> &args, const std::string &strCommand)
76 {
77     enum CmdParseState
78     {
79         STATE_EATING_SPACES,
80         STATE_ARGUMENT,
81         STATE_SINGLEQUOTED,
82         STATE_DOUBLEQUOTED,
83         STATE_ESCAPE_OUTER,
84         STATE_ESCAPE_DOUBLEQUOTED
85     } state = STATE_EATING_SPACES;
86     std::string curarg;
87     foreach(char ch, strCommand)
88     {
89         switch(state)
90         {
91         case STATE_ARGUMENT: // In or after argument
92         case STATE_EATING_SPACES: // Handle runs of whitespace
93             switch(ch)
94             {
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
100                 {
101                     args.push_back(curarg);
102                     curarg.clear();
103                 }
104                 state = STATE_EATING_SPACES;
105                 break;
106             default: curarg += ch; state = STATE_ARGUMENT;
107             }
108             break;
109         case STATE_SINGLEQUOTED: // Single-quoted string
110             switch(ch)
111             {
112             case '\'': state = STATE_ARGUMENT; break;
113             default: curarg += ch;
114             }
115             break;
116         case STATE_DOUBLEQUOTED: // Double-quoted string
117             switch(ch)
118             {
119             case '"': state = STATE_ARGUMENT; break;
120             case '\\': state = STATE_ESCAPE_DOUBLEQUOTED; break;
121             default: curarg += ch;
122             }
123             break;
124         case STATE_ESCAPE_OUTER: // '\' outside quotes
125             curarg += ch; state = STATE_ARGUMENT;
126             break;
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;
130             break;
131         }
132     }
133     switch(state) // final state
134     {
135     case STATE_EATING_SPACES:
136         return true;
137     case STATE_ARGUMENT:
138         args.push_back(curarg);
139         return true;
140     default: // ERROR to end in one of the other states
141         return false;
142     }
143 }
144
145 void RPCExecutor::request(const QString &command)
146 {
147     std::vector<std::string> args;
148     if(!parseCommandLine(args, command.toStdString()))
149     {
150         emit reply(RPCConsole::CMD_ERROR, QString("Parse error: unbalanced ' or \""));
151         return;
152     }
153     if(args.empty())
154         return; // Nothing to do
155     try
156     {
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(
161             args[0],
162             RPCConvertValues(args[0], std::vector<std::string>(args.begin() + 1, args.end())));
163
164         // Format result reply
165         if (result.type() == json_spirit::null_type)
166             strPrint = "";
167         else if (result.type() == json_spirit::str_type)
168             strPrint = result.get_str();
169         else
170             strPrint = write_string(result, true);
171
172         emit reply(RPCConsole::CMD_REPLY, QString::fromStdString(strPrint));
173     }
174     catch (json_spirit::Object& objError)
175     {
176         try // Nice formatting for standard-format error
177         {
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) + ")");
181         }
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)));
185         }
186     }
187     catch (std::exception& e)
188     {
189         emit reply(RPCConsole::CMD_ERROR, QString("Error: ") + QString::fromStdString(e.what()));
190     }
191 }
192
193 RPCConsole::RPCConsole(QWidget *parent) :
194     QWidget(parent),
195     ui(new Ui::RPCConsole),
196     historyPtr(0)
197 {
198     ui->setupUi(this);
199
200 #ifndef Q_OS_MAC
201     ui->openDebugLogfileButton->setIcon(QIcon(":/icons/export"));
202     ui->openConfigurationfileButton->setIcon(QIcon(":/icons/export"));
203     ui->showCLOptionsButton->setIcon(QIcon(":/icons/options"));
204 #endif
205
206     // Install event filter for up and down arrow
207     ui->lineEdit->installEventFilter(this);
208     ui->messagesWidget->installEventFilter(this);
209
210     connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
211     connect(ui->btnClearTrafficGraph, SIGNAL(clicked()), ui->trafficGraph, SLOT(clear()));
212
213     // set library version labels
214     ui->openSSLVersion->setText(SSLeay_version(SSLEAY_VERSION));
215     ui->berkeleyDBVersion->setText(DbEnv::version(0, 0, 0));
216
217     startExecutor();
218     setTrafficGraphRange(INITIAL_TRAFFIC_GRAPH_MINS);
219
220     clear();
221 }
222
223 RPCConsole::~RPCConsole()
224 {
225     emit stopExecutor();
226     delete ui;
227 }
228
229 bool RPCConsole::eventFilter(QObject* obj, QEvent *event)
230 {
231     if(event->type() == QEvent::KeyPress) // Special key handling
232     {
233         QKeyEvent *keyevt = static_cast<QKeyEvent*>(event);
234         int key = keyevt->key();
235         Qt::KeyboardModifiers mod = keyevt->modifiers();
236         switch(key)
237         {
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)
243             {
244                 QApplication::postEvent(ui->messagesWidget, new QKeyEvent(*keyevt));
245                 return true;
246             }
247             break;
248         case Qt::Key_Return:
249         case Qt::Key_Enter:
250             // forward these events to lineEdit
251             if(obj == (QObject*)autoCompleter->popup()) {
252                 QApplication::postEvent(ui->lineEdit, new QKeyEvent(*keyevt));
253                 return true;
254             }
255             break;
256         default:
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)))
263             {
264                 ui->lineEdit->setFocus();
265                 QApplication::postEvent(ui->lineEdit, new QKeyEvent(*keyevt));
266                 autoCompleter->popup()->hide();
267                 return true;
268             }
269         }
270     }
271     return QWidget::eventFilter(obj, event);
272 }
273
274 void RPCConsole::setClientModel(ClientModel *model)
275 {
276     this->clientModel = model;
277     ui->trafficGraph->setClientModel(model);
278     if(model)
279     {
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)));
283
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());
291
292         setNumConnections(model->getNumConnections());
293         ui->isTestNet->setChecked(model->isTestNet());
294
295         setNumBlocks(model->getNumBlocks(), model->getNumBlocksOfPeers());
296
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)
301         {
302             wordList << commandList[i].c_str();
303             wordList << ("help " + commandList[i]).c_str();
304         }
305
306         wordList.sort();
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);
314     }
315 }
316
317 static QString categoryClass(int category)
318 {
319     switch(category)
320     {
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";
325     }
326 }
327
328 void RPCConsole::clear()
329 {
330     ui->messagesWidget->clear();
331     history.clear();
332     historyPtr = 0;
333     ui->lineEdit->clear();
334     ui->lineEdit->setFocus();
335
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)
339     {
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));
344     }
345
346     // Set default style sheet
347     ui->messagesWidget->document()->setDefaultStyleSheet(
348                 "table { }"
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; } "
354                 );
355
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);
359 }
360
361 void RPCConsole::message(int category, const QString &message, bool html)
362 {
363     QTime time = QTime::currentTime();
364     QString timeString = time.toString();
365     QString out;
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\">";
369     if(html)
370         out += message;
371     else
372         out += GUIUtil::HtmlEscape(message, true);
373     out += "</td></tr></table>";
374     ui->messagesWidget->append(out);
375 }
376
377 void RPCConsole::setNumConnections(int count)
378 {
379     if (!clientModel)
380         return;
381
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)) + ")";
385
386     ui->numberOfConnections->setText(connections);
387 }
388
389 void RPCConsole::setNumBlocks(int count, int countOfPeers)
390 {
391     ui->numberOfBlocks->setText(QString::number(count));
392     ui->totalBlocks->setText(QString::number(countOfPeers));
393     if(clientModel)
394     {
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());
398     }
399 }
400
401 void RPCConsole::on_lineEdit_returnPressed()
402 {
403     QString cmd = ui->lineEdit->text();
404     ui->lineEdit->clear();
405
406     if(!cmd.isEmpty())
407     {
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
413         history.append(cmd);
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
420         scrollToEnd();
421     }
422 }
423
424 void RPCConsole::browseHistory(int offset)
425 {
426     historyPtr += offset;
427     if(historyPtr < 0)
428         historyPtr = 0;
429     if(historyPtr > history.size())
430         historyPtr = history.size();
431     QString cmd;
432     if(historyPtr < history.size())
433         cmd = history.at(historyPtr);
434     ui->lineEdit->setText(cmd);
435 }
436
437 void RPCConsole::startExecutor()
438 {
439     QThread* thread = new QThread;
440     RPCExecutor *executor = new RPCExecutor();
441     executor->moveToThread(thread);
442
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()));
456
457     // Default implementation of QThread::run() simply spins up an event loop in the thread,
458     // which is what we want.
459     thread->start();
460 }
461
462 void RPCConsole::on_tabWidget_currentChanged(int index)
463 {
464     if(ui->tabWidget->widget(index) == ui->tab_console)
465     {
466         ui->lineEdit->setFocus();
467     }
468 }
469
470 void RPCConsole::on_openDebugLogfileButton_clicked()
471 {
472     GUIUtil::openDebugLogfile();
473 }
474
475 void RPCConsole::on_openConfigurationfileButton_clicked()
476 {
477     GUIUtil::openConfigfile();
478 }
479
480 void RPCConsole::scrollToEnd()
481 {
482     QScrollBar *scrollbar = ui->messagesWidget->verticalScrollBar();
483     scrollbar->setValue(scrollbar->maximum());
484 }
485
486 void RPCConsole::on_showCLOptionsButton_clicked()
487 {
488     GUIUtil::HelpMessageBox help;
489     help.exec();
490 }
491 void RPCConsole::on_sldGraphRange_valueChanged(int value)
492 {
493     const int multiplier = 5; // each position on the slider represents 5 min
494     int mins = value * multiplier;
495     setTrafficGraphRange(mins);
496 }
497
498 QString RPCConsole::FormatBytes(quint64 bytes)
499 {
500     if(bytes < 1024)
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);
506
507     return QString(tr("%1 GB")).arg(bytes / 1024 / 1024 / 1024);
508 }
509
510 void RPCConsole::setTrafficGraphRange(int mins)
511 {
512     ui->trafficGraph->setGraphRangeMins(mins);
513     ui->lblGraphRange->setText(GUIUtil::formatDurationStr(mins * 60));
514 }
515
516 void RPCConsole::updateTrafficStats(quint64 totalBytesIn, quint64 totalBytesOut)
517 {
518     ui->lblBytesIn->setText(FormatBytes(totalBytesIn));
519     ui->lblBytesOut->setText(FormatBytes(totalBytesOut));
520 }
521
522 void RPCConsole::resizeEvent(QResizeEvent *event)
523 {
524     QWidget::resizeEvent(event);
525 }
526
527 void RPCConsole::showEvent(QShowEvent *event)
528 {
529     QWidget::showEvent(event);
530
531     if (!clientModel)
532         return;
533 }
534
535 void RPCConsole::hideEvent(QHideEvent *event)
536 {
537     QWidget::hideEvent(event);
538
539     if (!clientModel)
540         return;
541 }
542
543 void RPCConsole::keyPressEvent(QKeyEvent *event)
544 {
545 #ifdef ANDROID
546     if(windowType() != Qt::Widget && event->key() == Qt::Key_Back)
547     {
548         close();
549     }
550 #else
551     if(windowType() != Qt::Widget && event->key() == Qt::Key_Escape)
552     {
553         close();
554     }
555 #endif
556 }