Skip to content

Commit 9c71998

Browse files
committed
Merge #14784: qt: Use WalletModel* instead of the wallet name as map key
91b0c5b qt: Use WalletModel* instead of wallet name in console window (João Barbosa) b2ce86c qt: Use WalletModel* instead of wallet name in main window (João Barbosa) d2a1adf qt: Factor out WalletModel::getDisplayName() (João Barbosa) Pull request description: This a small refactor that doesn't change behavior. This is also necessary if in the future we allow renaming wallets. Tree-SHA512: 1820d0ff28e84b1d862097f1f55b52f94520fa50c9b1939d235a448a48159748c3bbf99b19e4cb1ff4f91efc008c0971b4c25a91f645f9d43792c8aeaa93cf9e
2 parents 88bbcdc + 91b0c5b commit 9c71998

File tree

9 files changed

+65
-66
lines changed

9 files changed

+65
-66
lines changed

src/qt/bitcoin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2011-2018 The Bitcoin Core developers
1+
// Copyright (c) 2011-2019 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -431,7 +431,7 @@ void BitcoinApplication::addWallet(WalletModel* walletModel)
431431
window->addWallet(walletModel);
432432

433433
if (m_wallet_models.empty()) {
434-
window->setCurrentWallet(walletModel->getWalletName());
434+
window->setCurrentWallet(walletModel);
435435
}
436436

437437
#ifdef ENABLE_BIP70

src/qt/bitcoingui.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2011-2018 The Bitcoin Core developers
1+
// Copyright (c) 2011-2019 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -569,10 +569,9 @@ bool BitcoinGUI::addWallet(WalletModel *walletModel)
569569
{
570570
if(!walletFrame)
571571
return false;
572-
const QString name = walletModel->getWalletName();
573-
QString display_name = name.isEmpty() ? "["+tr("default wallet")+"]" : name;
572+
const QString display_name = walletModel->getDisplayName();
574573
setWalletActionsEnabled(true);
575-
m_wallet_selector->addItem(display_name, name);
574+
m_wallet_selector->addItem(display_name, QVariant::fromValue(walletModel));
576575
if (m_wallet_selector->count() == 2) {
577576
m_wallet_selector_label_action->setVisible(true);
578577
m_wallet_selector_action->setVisible(true);
@@ -584,8 +583,7 @@ bool BitcoinGUI::addWallet(WalletModel *walletModel)
584583
bool BitcoinGUI::removeWallet(WalletModel* walletModel)
585584
{
586585
if (!walletFrame) return false;
587-
QString name = walletModel->getWalletName();
588-
int index = m_wallet_selector->findData(name);
586+
int index = m_wallet_selector->findData(QVariant::fromValue(walletModel));
589587
m_wallet_selector->removeItem(index);
590588
if (m_wallet_selector->count() == 0) {
591589
setWalletActionsEnabled(false);
@@ -594,20 +592,20 @@ bool BitcoinGUI::removeWallet(WalletModel* walletModel)
594592
m_wallet_selector_action->setVisible(false);
595593
}
596594
rpcConsole->removeWallet(walletModel);
597-
return walletFrame->removeWallet(name);
595+
return walletFrame->removeWallet(walletModel);
598596
}
599597

600-
bool BitcoinGUI::setCurrentWallet(const QString& name)
598+
bool BitcoinGUI::setCurrentWallet(WalletModel* wallet_model)
601599
{
602600
if(!walletFrame)
603601
return false;
604-
return walletFrame->setCurrentWallet(name);
602+
return walletFrame->setCurrentWallet(wallet_model);
605603
}
606604

607605
bool BitcoinGUI::setCurrentWalletBySelectorIndex(int index)
608606
{
609-
QString internal_name = m_wallet_selector->itemData(index).toString();
610-
return setCurrentWallet(internal_name);
607+
WalletModel* wallet_model = m_wallet_selector->itemData(index).value<WalletModel*>();
608+
return setCurrentWallet(wallet_model);
611609
}
612610

613611
void BitcoinGUI::removeAllWallets()

src/qt/bitcoingui.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2011-2018 The Bitcoin Core developers
1+
// Copyright (c) 2011-2019 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -206,7 +206,7 @@ public Q_SLOTS:
206206
void message(const QString &title, const QString &message, unsigned int style, bool *ret = nullptr);
207207

208208
#ifdef ENABLE_WALLET
209-
bool setCurrentWallet(const QString& name);
209+
bool setCurrentWallet(WalletModel* wallet_model);
210210
bool setCurrentWalletBySelectorIndex(int index);
211211
/** Set the UI status indicators based on the currently selected wallet.
212212
*/

src/qt/rpcconsole.cpp

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2011-2018 The Bitcoin Core developers
1+
// Copyright (c) 2011-2019 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -85,7 +85,7 @@ class RPCExecutor : public QObject
8585
explicit RPCExecutor(interfaces::Node& node) : m_node(node) {}
8686

8787
public Q_SLOTS:
88-
void request(const QString &command, const QString &walletID);
88+
void request(const QString &command, const WalletModel* wallet_model);
8989

9090
Q_SIGNALS:
9191
void reply(int category, const QString &command);
@@ -148,7 +148,7 @@ class QtRPCTimerInterface: public RPCTimerInterface
148148
* @param[out] pstrFilteredOut Command line, filtered to remove any sensitive data
149149
*/
150150

151-
bool RPCConsole::RPCParseCommandLine(interfaces::Node* node, std::string &strResult, const std::string &strCommand, const bool fExecute, std::string * const pstrFilteredOut, const std::string *walletID)
151+
bool RPCConsole::RPCParseCommandLine(interfaces::Node* node, std::string &strResult, const std::string &strCommand, const bool fExecute, std::string * const pstrFilteredOut, const WalletModel* wallet_model)
152152
{
153153
std::vector< std::vector<std::string> > stack;
154154
stack.push_back(std::vector<std::string>());
@@ -306,8 +306,8 @@ bool RPCConsole::RPCParseCommandLine(interfaces::Node* node, std::string &strRes
306306
std::string method = stack.back()[0];
307307
std::string uri;
308308
#ifdef ENABLE_WALLET
309-
if (walletID) {
310-
QByteArray encodedName = QUrl::toPercentEncoding(QString::fromStdString(*walletID));
309+
if (wallet_model) {
310+
QByteArray encodedName = QUrl::toPercentEncoding(wallet_model->getWalletName());
311311
uri = "/wallet/"+std::string(encodedName.constData(), encodedName.length());
312312
}
313313
#endif
@@ -387,7 +387,7 @@ bool RPCConsole::RPCParseCommandLine(interfaces::Node* node, std::string &strRes
387387
}
388388
}
389389

390-
void RPCExecutor::request(const QString &command, const QString &walletID)
390+
void RPCExecutor::request(const QString &command, const WalletModel* wallet_model)
391391
{
392392
try
393393
{
@@ -418,9 +418,7 @@ void RPCExecutor::request(const QString &command, const QString &walletID)
418418
" example: getblock(getblockhash(0),true)[tx][0]\n\n")));
419419
return;
420420
}
421-
std::string wallet_id = walletID.toStdString();
422-
if (!RPCConsole::RPCExecuteCommandLine(m_node, result, executableCommand, nullptr, walletID.isNull() ? nullptr : &wallet_id))
423-
{
421+
if (!RPCConsole::RPCExecuteCommandLine(m_node, result, executableCommand, nullptr, wallet_model)) {
424422
Q_EMIT reply(RPCConsole::CMD_ERROR, QString("Parse error: unbalanced ' or \""));
425423
return;
426424
}
@@ -698,10 +696,8 @@ void RPCConsole::setClientModel(ClientModel *model)
698696
#ifdef ENABLE_WALLET
699697
void RPCConsole::addWallet(WalletModel * const walletModel)
700698
{
701-
const QString name = walletModel->getWalletName();
702-
// use name for text and internal data object (to allow to move to a wallet id later)
703-
QString display_name = name.isEmpty() ? "["+tr("default wallet")+"]" : name;
704-
ui->WalletSelector->addItem(display_name, name);
699+
// use name for text and wallet model for internal data object (to allow to move to a wallet id later)
700+
ui->WalletSelector->addItem(walletModel->getDisplayName(), QVariant::fromValue(walletModel));
705701
if (ui->WalletSelector->count() == 2 && !isVisible()) {
706702
// First wallet added, set to default so long as the window isn't presently visible (and potentially in use)
707703
ui->WalletSelector->setCurrentIndex(1);
@@ -714,8 +710,7 @@ void RPCConsole::addWallet(WalletModel * const walletModel)
714710

715711
void RPCConsole::removeWallet(WalletModel * const walletModel)
716712
{
717-
const QString name = walletModel->getWalletName();
718-
ui->WalletSelector->removeItem(ui->WalletSelector->findData(name));
713+
ui->WalletSelector->removeItem(ui->WalletSelector->findData(QVariant::fromValue(walletModel)));
719714
if (ui->WalletSelector->count() == 2) {
720715
ui->WalletSelector->setVisible(false);
721716
ui->WalletSelectorLabel->setVisible(false);
@@ -910,25 +905,25 @@ void RPCConsole::on_lineEdit_returnPressed()
910905

911906
cmdBeforeBrowsing = QString();
912907

913-
QString walletID;
908+
WalletModel* wallet_model{nullptr};
914909
#ifdef ENABLE_WALLET
915910
const int wallet_index = ui->WalletSelector->currentIndex();
916911
if (wallet_index > 0) {
917-
walletID = (QString)ui->WalletSelector->itemData(wallet_index).value<QString>();
912+
wallet_model = ui->WalletSelector->itemData(wallet_index).value<WalletModel*>();
918913
}
919914

920-
if (m_last_wallet_id != walletID) {
921-
if (walletID.isNull()) {
922-
message(CMD_REQUEST, tr("Executing command without any wallet"));
915+
if (m_last_wallet_model != wallet_model) {
916+
if (wallet_model) {
917+
message(CMD_REQUEST, tr("Executing command using \"%1\" wallet").arg(wallet_model->getWalletName()));
923918
} else {
924-
message(CMD_REQUEST, tr("Executing command using \"%1\" wallet").arg(walletID));
919+
message(CMD_REQUEST, tr("Executing command without any wallet"));
925920
}
926-
m_last_wallet_id = walletID;
921+
m_last_wallet_model = wallet_model;
927922
}
928923
#endif
929924

930925
message(CMD_REQUEST, QString::fromStdString(strFilteredCmd));
931-
Q_EMIT cmdRequest(cmd, walletID);
926+
Q_EMIT cmdRequest(cmd, m_last_wallet_model);
932927

933928
cmd = QString::fromStdString(strFilteredCmd);
934929

src/qt/rpcconsole.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2011-2018 The Bitcoin Core developers
1+
// Copyright (c) 2011-2019 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -41,9 +41,9 @@ class RPCConsole: public QWidget
4141
explicit RPCConsole(interfaces::Node& node, const PlatformStyle *platformStyle, QWidget *parent);
4242
~RPCConsole();
4343

44-
static bool RPCParseCommandLine(interfaces::Node* node, std::string &strResult, const std::string &strCommand, bool fExecute, std::string * const pstrFilteredOut = nullptr, const std::string *walletID = nullptr);
45-
static bool RPCExecuteCommandLine(interfaces::Node& node, std::string &strResult, const std::string &strCommand, std::string * const pstrFilteredOut = nullptr, const std::string *walletID = nullptr) {
46-
return RPCParseCommandLine(&node, strResult, strCommand, true, pstrFilteredOut, walletID);
44+
static bool RPCParseCommandLine(interfaces::Node* node, std::string &strResult, const std::string &strCommand, bool fExecute, std::string * const pstrFilteredOut = nullptr, const WalletModel* wallet_model = nullptr);
45+
static bool RPCExecuteCommandLine(interfaces::Node& node, std::string &strResult, const std::string &strCommand, std::string * const pstrFilteredOut = nullptr, const WalletModel* wallet_model = nullptr) {
46+
return RPCParseCommandLine(&node, strResult, strCommand, true, pstrFilteredOut, wallet_model);
4747
}
4848

4949
void setClientModel(ClientModel *model);
@@ -133,7 +133,7 @@ public Q_SLOTS:
133133
Q_SIGNALS:
134134
// For RPC command executor
135135
void stopExecutor();
136-
void cmdRequest(const QString &command, const QString &walletID);
136+
void cmdRequest(const QString &command, const WalletModel* wallet_model);
137137

138138
private:
139139
void startExecutor();
@@ -165,7 +165,7 @@ public Q_SLOTS:
165165
int consoleFontSize = 0;
166166
QCompleter *autoCompleter = nullptr;
167167
QThread thread;
168-
QString m_last_wallet_id;
168+
WalletModel* m_last_wallet_model{nullptr};
169169

170170
/** Update UI with latest network info from model. */
171171
void updateNetworkState();

src/qt/walletframe.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2011-2018 The Bitcoin Core developers
1+
// Copyright (c) 2011-2019 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -46,8 +46,7 @@ bool WalletFrame::addWallet(WalletModel *walletModel)
4646
return false;
4747
}
4848

49-
const QString name = walletModel->getWalletName();
50-
if (mapWalletViews.count(name) > 0) {
49+
if (mapWalletViews.count(walletModel) > 0) {
5150
return false;
5251
}
5352

@@ -65,7 +64,7 @@ bool WalletFrame::addWallet(WalletModel *walletModel)
6564
}
6665

6766
walletStack->addWidget(walletView);
68-
mapWalletViews[name] = walletView;
67+
mapWalletViews[walletModel] = walletView;
6968

7069
// Ensure a walletView is able to show the main window
7170
connect(walletView, &WalletView::showNormalIfMinimized, [this]{
@@ -77,32 +76,32 @@ bool WalletFrame::addWallet(WalletModel *walletModel)
7776
return true;
7877
}
7978

80-
bool WalletFrame::setCurrentWallet(const QString& name)
79+
bool WalletFrame::setCurrentWallet(WalletModel* wallet_model)
8180
{
82-
if (mapWalletViews.count(name) == 0)
81+
if (mapWalletViews.count(wallet_model) == 0)
8382
return false;
8483

85-
WalletView *walletView = mapWalletViews.value(name);
84+
WalletView *walletView = mapWalletViews.value(wallet_model);
8685
walletStack->setCurrentWidget(walletView);
8786
assert(walletView);
8887
walletView->updateEncryptionStatus();
8988
return true;
9089
}
9190

92-
bool WalletFrame::removeWallet(const QString &name)
91+
bool WalletFrame::removeWallet(WalletModel* wallet_model)
9392
{
94-
if (mapWalletViews.count(name) == 0)
93+
if (mapWalletViews.count(wallet_model) == 0)
9594
return false;
9695

97-
WalletView *walletView = mapWalletViews.take(name);
96+
WalletView *walletView = mapWalletViews.take(wallet_model);
9897
walletStack->removeWidget(walletView);
9998
delete walletView;
10099
return true;
101100
}
102101

103102
void WalletFrame::removeAllWallets()
104103
{
105-
QMap<QString, WalletView*>::const_iterator i;
104+
QMap<WalletModel*, WalletView*>::const_iterator i;
106105
for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
107106
walletStack->removeWidget(i.value());
108107
mapWalletViews.clear();
@@ -120,35 +119,35 @@ bool WalletFrame::handlePaymentRequest(const SendCoinsRecipient &recipient)
120119
void WalletFrame::showOutOfSyncWarning(bool fShow)
121120
{
122121
bOutOfSync = fShow;
123-
QMap<QString, WalletView*>::const_iterator i;
122+
QMap<WalletModel*, WalletView*>::const_iterator i;
124123
for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
125124
i.value()->showOutOfSyncWarning(fShow);
126125
}
127126

128127
void WalletFrame::gotoOverviewPage()
129128
{
130-
QMap<QString, WalletView*>::const_iterator i;
129+
QMap<WalletModel*, WalletView*>::const_iterator i;
131130
for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
132131
i.value()->gotoOverviewPage();
133132
}
134133

135134
void WalletFrame::gotoHistoryPage()
136135
{
137-
QMap<QString, WalletView*>::const_iterator i;
136+
QMap<WalletModel*, WalletView*>::const_iterator i;
138137
for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
139138
i.value()->gotoHistoryPage();
140139
}
141140

142141
void WalletFrame::gotoReceiveCoinsPage()
143142
{
144-
QMap<QString, WalletView*>::const_iterator i;
143+
QMap<WalletModel*, WalletView*>::const_iterator i;
145144
for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
146145
i.value()->gotoReceiveCoinsPage();
147146
}
148147

149148
void WalletFrame::gotoSendCoinsPage(QString addr)
150149
{
151-
QMap<QString, WalletView*>::const_iterator i;
150+
QMap<WalletModel*, WalletView*>::const_iterator i;
152151
for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
153152
i.value()->gotoSendCoinsPage(addr);
154153
}

src/qt/walletframe.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2011-2018 The Bitcoin Core developers
1+
// Copyright (c) 2011-2019 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -37,8 +37,8 @@ class WalletFrame : public QFrame
3737
void setClientModel(ClientModel *clientModel);
3838

3939
bool addWallet(WalletModel *walletModel);
40-
bool setCurrentWallet(const QString& name);
41-
bool removeWallet(const QString &name);
40+
bool setCurrentWallet(WalletModel* wallet_model);
41+
bool removeWallet(WalletModel* wallet_model);
4242
void removeAllWallets();
4343

4444
bool handlePaymentRequest(const SendCoinsRecipient& recipient);
@@ -53,7 +53,7 @@ class WalletFrame : public QFrame
5353
QStackedWidget *walletStack;
5454
BitcoinGUI *gui;
5555
ClientModel *clientModel;
56-
QMap<QString, WalletView*> mapWalletViews;
56+
QMap<WalletModel*, WalletView*> mapWalletViews;
5757

5858
bool bOutOfSync;
5959

src/qt/walletmodel.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2011-2018 The Bitcoin Core developers
1+
// Copyright (c) 2011-2019 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -578,6 +578,12 @@ QString WalletModel::getWalletName() const
578578
return QString::fromStdString(m_wallet->getWalletName());
579579
}
580580

581+
QString WalletModel::getDisplayName() const
582+
{
583+
const QString name = getWalletName();
584+
return name.isEmpty() ? "["+tr("default wallet")+"]" : name;
585+
}
586+
581587
bool WalletModel::isMultiwallet()
582588
{
583589
return m_node.getWallets().size() > 1;

src/qt/walletmodel.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2011-2018 The Bitcoin Core developers
1+
// Copyright (c) 2011-2019 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -219,6 +219,7 @@ class WalletModel : public QObject
219219
interfaces::Wallet& wallet() const { return *m_wallet; }
220220

221221
QString getWalletName() const;
222+
QString getDisplayName() const;
222223

223224
bool isMultiwallet();
224225

0 commit comments

Comments
 (0)