Skip to content

Commit

Permalink
Merge bitcoin#5891 qt_console_history_persist
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-jr committed Jun 27, 2016
2 parents 8d22335 + a869f28 commit 08eb7da
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
62 changes: 53 additions & 9 deletions src/qt/rpcconsole.cpp
Expand Up @@ -26,6 +26,7 @@

#include <QKeyEvent>
#include <QMenu>
#include <QSettings>
#include <QScrollBar>
#include <QSignalMapper>
#include <QThread>
Expand Down Expand Up @@ -56,6 +57,9 @@ const struct {
{NULL, NULL}
};

//don't add private key handling cmd's to the history
const QStringList RPCConsole::historyFilter = QStringList() << "importprivkey" << "signrawtransaction" << "walletpassphrase" << "walletpassphrasechange" << "encryptwallet";

/* Object for executing console RPC commands in a separate thread.
*/
class RPCExecutor : public QObject
Expand Down Expand Up @@ -280,11 +284,32 @@ RPCConsole::RPCConsole(const PlatformStyle *platformStyle, QWidget *parent) :
ui->peerHeading->setText(tr("Select a peer to view detailed information."));

clear();

//load history
QSettings settings;
int size = settings.beginReadArray("nRPCConsoleWindowHistory");
history.clear();
for (int i=0; i<size; i++){
settings.setArrayIndex(i);
history.append(settings.value("cmd").toString());
}
historyPtr = history.size();
settings.endArray();
}

RPCConsole::~RPCConsole()
{
GUIUtil::saveWindowGeometry("nRPCConsoleWindow", this);

//persist history
QSettings settings;
settings.beginWriteArray("nRPCConsoleWindowHistory");
for (int i = 0; i < history.size(); ++i) {
settings.setArrayIndex(i);
settings.setValue("cmd", history.at(i));
}
settings.endArray();

Q_EMIT stopExecutor();
RPCUnregisterTimerInterface(rpcTimerInterface);
delete rpcTimerInterface;
Expand Down Expand Up @@ -545,24 +570,41 @@ void RPCConsole::on_lineEdit_returnPressed()

if(!cmd.isEmpty())
{
cmdBeforeBrowsing = QString();

message(CMD_REQUEST, cmd);
Q_EMIT cmdRequest(cmd);
// Remove command, if already in history
history.removeOne(cmd);
// Append command to history
history.append(cmd);
// Enforce maximum history size
while(history.size() > CONSOLE_HISTORY)
history.removeFirst();
// Set pointer to end of history
historyPtr = history.size();

bool storeHistory = true;
Q_FOREACH(QString unallowedCmd, historyFilter)
{
if(cmd.trimmed().startsWith(unallowedCmd))
storeHistory = false; break;
}

if (storeHistory)
{
// Remove command, if already in history
history.removeOne(cmd);
// Append command to history
history.append(cmd);
// Enforce maximum history size
while(history.size() > CONSOLE_HISTORY)
history.removeFirst();
// Set pointer to end of history
historyPtr = history.size();
}
// Scroll console view to end
scrollToEnd();
}
}

void RPCConsole::browseHistory(int offset)
{
// store current text when start browsing through the history
if(historyPtr == history.size())
cmdBeforeBrowsing = ui->lineEdit->text();

historyPtr += offset;
if(historyPtr < 0)
historyPtr = 0;
Expand All @@ -571,6 +613,8 @@ void RPCConsole::browseHistory(int offset)
QString cmd;
if(historyPtr < history.size())
cmd = history.at(historyPtr);
else if(historyPtr == history.size() && !cmdBeforeBrowsing.isNull())
cmd = cmdBeforeBrowsing;
ui->lineEdit->setText(cmd);
}

Expand Down
2 changes: 2 additions & 0 deletions src/qt/rpcconsole.h
Expand Up @@ -129,6 +129,8 @@ public Q_SLOTS:
ClientModel *clientModel;
QStringList history;
int historyPtr;
const static QStringList historyFilter;
QString cmdBeforeBrowsing;
NodeId cachedNodeid;
const PlatformStyle *platformStyle;
RPCTimerInterface *rpcTimerInterface;
Expand Down

0 comments on commit 08eb7da

Please sign in to comment.