update to 0.4 preview
[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
8 #include <QTime>
9 #include <QTimer>
10 #include <QThread>
11 #include <QTextEdit>
12 #include <QKeyEvent>
13 #include <QUrl>
14 #include <QScrollBar>
15
16 #include <openssl/crypto.h>
17
18 // TODO: make it possible to filter out categories (esp debug messages when implemented)
19 // TODO: receive errors and debug messages through ClientModel
20
21 const int CONSOLE_SCROLLBACK = 50;
22 const int CONSOLE_HISTORY = 50;
23
24 const QSize ICON_SIZE(24, 24);
25
26 const struct {
27     const char *url;
28     const char *source;
29 } ICON_MAPPING[] = {
30     {"cmd-request", ":/icons/tx_input"},
31     {"cmd-reply", ":/icons/tx_output"},
32     {"cmd-error", ":/icons/tx_output"},
33     {"misc", ":/icons/tx_inout"},
34     {NULL, NULL}
35 };
36
37 /* Object for executing console RPC commands in a separate thread.
38 */
39 class RPCExecutor: public QObject
40 {
41     Q_OBJECT
42 public slots:
43     void start();
44     void request(const QString &command);
45 signals:
46     void reply(int category, const QString &command);
47 };
48
49 #include "rpcconsole.moc"
50
51 void RPCExecutor::start()
52 {
53    // Nothing to do
54 }
55
56 /**
57  * Split shell command line into a list of arguments. Aims to emulate \c bash and friends.
58  *
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
66  *
67  * @param[out]   args        Parsed arguments will be appended to this list
68  * @param[in]    strCommand  Command line to split
69  */
70 bool parseCommandLine(std::vector<std::string> &args, const std::string &strCommand)
71 {
72     enum CmdParseState
73     {
74         STATE_EATING_SPACES,
75         STATE_ARGUMENT,
76         STATE_SINGLEQUOTED,
77         STATE_DOUBLEQUOTED,
78         STATE_ESCAPE_OUTER,
79         STATE_ESCAPE_DOUBLEQUOTED
80     } state = STATE_EATING_SPACES;
81     std::string curarg;
82     foreach(char ch, strCommand)
83     {
84         switch(state)
85         {
86         case STATE_ARGUMENT: // In or after argument
87         case STATE_EATING_SPACES: // Handle runs of whitespace
88             switch(ch)
89             {
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
95                 {
96                     args.push_back(curarg);
97                     curarg.clear();
98                 }
99                 state = STATE_EATING_SPACES;
100                 break;
101             default: curarg += ch; state = STATE_ARGUMENT;
102             }
103             break;
104         case STATE_SINGLEQUOTED: // Single-quoted string
105             switch(ch)
106             {
107             case '\'': state = STATE_ARGUMENT; break;
108             default: curarg += ch;
109             }
110             break;
111         case STATE_DOUBLEQUOTED: // Double-quoted string
112             switch(ch)
113             {
114             case '"': state = STATE_ARGUMENT; break;
115             case '\\': state = STATE_ESCAPE_DOUBLEQUOTED; break;
116             default: curarg += ch;
117             }
118             break;
119         case STATE_ESCAPE_OUTER: // '\' outside quotes
120             curarg += ch; state = STATE_ARGUMENT;
121             break;
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;
125             break;
126         }
127     }
128     switch(state) // final state
129     {
130     case STATE_EATING_SPACES:
131         return true;
132     case STATE_ARGUMENT:
133         args.push_back(curarg);
134         return true;
135     default: // ERROR to end in one of the other states
136         return false;
137     }
138 }
139
140 void RPCExecutor::request(const QString &command)
141 {
142     std::vector<std::string> args;
143     if(!parseCommandLine(args, command.toStdString()))
144     {
145         emit reply(RPCConsole::CMD_ERROR, QString("Parse error: unbalanced ' or \""));
146         return;
147     }
148     if(args.empty())
149         return; // Nothing to do
150     try
151     {
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(
156             args[0],
157             RPCConvertValues(args[0], std::vector<std::string>(args.begin() + 1, args.end())));
158
159         // Format result reply
160         if (result.type() == json_spirit::null_type)
161             strPrint = "";
162         else if (result.type() == json_spirit::str_type)
163             strPrint = result.get_str();
164         else
165             strPrint = write_string(result, true);
166
167         emit reply(RPCConsole::CMD_REPLY, QString::fromStdString(strPrint));
168     }
169     catch (json_spirit::Object& objError)
170     {
171         try // Nice formatting for standard-format error
172         {
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) + ")");
176         }
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)));
180         }
181     }
182     catch (std::exception& e)
183     {
184         emit reply(RPCConsole::CMD_ERROR, QString("Error: ") + QString::fromStdString(e.what()));
185     }
186 }
187
188 RPCConsole::RPCConsole(QWidget *parent) :
189     QDialog(parent),
190     ui(new Ui::RPCConsole),
191     historyPtr(0)
192 {
193     ui->setupUi(this);
194
195 #ifndef Q_OS_MAC
196     ui->openDebugLogfileButton->setIcon(QIcon(":/icons/export"));
197     ui->showCLOptionsButton->setIcon(QIcon(":/icons/options"));
198 #endif
199
200     // Install event filter for up and down arrow
201     ui->lineEdit->installEventFilter(this);
202     ui->messagesWidget->installEventFilter(this);
203
204     connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
205
206     // set OpenSSL version label
207     ui->openSSLVersion->setText(SSLeay_version(SSLEAY_VERSION));
208
209     startExecutor();
210
211     clear();
212 }
213
214 RPCConsole::~RPCConsole()
215 {
216     emit stopExecutor();
217     delete ui;
218 }
219
220 bool RPCConsole::eventFilter(QObject* obj, QEvent *event)
221 {
222     if(event->type() == QEvent::KeyPress) // Special key handling
223     {
224         QKeyEvent *keyevt = static_cast<QKeyEvent*>(event);
225         int key = keyevt->key();
226         Qt::KeyboardModifiers mod = keyevt->modifiers();
227         switch(key)
228         {
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)
234             {
235                 QApplication::postEvent(ui->messagesWidget, new QKeyEvent(*keyevt));
236                 return true;
237             }
238             break;
239         default:
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)))
246             {
247                 ui->lineEdit->setFocus();
248                 QApplication::postEvent(ui->lineEdit, new QKeyEvent(*keyevt));
249                 return true;
250             }
251         }
252     }
253     return QDialog::eventFilter(obj, event);
254 }
255
256 void RPCConsole::setClientModel(ClientModel *model)
257 {
258     this->clientModel = model;
259     if(model)
260     {
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)));
264
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());
270
271         setNumConnections(model->getNumConnections());
272         ui->isTestNet->setChecked(model->isTestNet());
273
274         setNumBlocks(model->getNumBlocks(), model->getNumBlocksOfPeers());
275     }
276 }
277
278 static QString categoryClass(int category)
279 {
280     switch(category)
281     {
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";
286     }
287 }
288
289 void RPCConsole::clear()
290 {
291     ui->messagesWidget->clear();
292     ui->lineEdit->clear();
293     ui->lineEdit->setFocus();
294
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)
298     {
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));
303     }
304
305     // Set default style sheet
306     ui->messagesWidget->document()->setDefaultStyleSheet(
307                 "table { }"
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; } "
313                 );
314
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);
318 }
319
320 void RPCConsole::message(int category, const QString &message, bool html)
321 {
322     QTime time = QTime::currentTime();
323     QString timeString = time.toString();
324     QString out;
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\">";
328     if(html)
329         out += message;
330     else
331         out += GUIUtil::HtmlEscape(message, true);
332     out += "</td></tr></table>";
333     ui->messagesWidget->append(out);
334 }
335
336 void RPCConsole::setNumConnections(int count)
337 {
338     ui->numberOfConnections->setText(QString::number(count));
339 }
340
341 void RPCConsole::setNumBlocks(int count, int countOfPeers)
342 {
343     ui->numberOfBlocks->setText(QString::number(count));
344     ui->totalBlocks->setText(QString::number(countOfPeers));
345     if(clientModel)
346     {
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());
350     }
351 }
352
353 void RPCConsole::on_lineEdit_returnPressed()
354 {
355     QString cmd = ui->lineEdit->text();
356     ui->lineEdit->clear();
357
358     if(!cmd.isEmpty())
359     {
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
365         history.append(cmd);
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
372         scrollToEnd();
373     }
374 }
375
376 void RPCConsole::browseHistory(int offset)
377 {
378     historyPtr += offset;
379     if(historyPtr < 0)
380         historyPtr = 0;
381     if(historyPtr > history.size())
382         historyPtr = history.size();
383     QString cmd;
384     if(historyPtr < history.size())
385         cmd = history.at(historyPtr);
386     ui->lineEdit->setText(cmd);
387 }
388
389 void RPCConsole::startExecutor()
390 {
391     QThread* thread = new QThread;
392     RPCExecutor *executor = new RPCExecutor();
393     executor->moveToThread(thread);
394
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()));
408
409     // Default implementation of QThread::run() simply spins up an event loop in the thread,
410     // which is what we want.
411     thread->start();
412 }
413
414 void RPCConsole::on_tabWidget_currentChanged(int index)
415 {
416     if(ui->tabWidget->widget(index) == ui->tab_console)
417     {
418         ui->lineEdit->setFocus();
419     }
420 }
421
422 void RPCConsole::on_openDebugLogfileButton_clicked()
423 {
424     GUIUtil::openDebugLogfile();
425 }
426
427 void RPCConsole::scrollToEnd()
428 {
429     QScrollBar *scrollbar = ui->messagesWidget->verticalScrollBar();
430     scrollbar->setValue(scrollbar->maximum());
431 }
432
433 void RPCConsole::on_showCLOptionsButton_clicked()
434 {
435     GUIUtil::HelpMessageBox help;
436     help.exec();
437 }