diff --git a/src/qt/bitcoin.cpp b/src/qt/bitcoin.cpp --- a/src/qt/bitcoin.cpp +++ b/src/qt/bitcoin.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2011-2016 The Bitcoin Core developers +// Copyright (c) 2011-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -361,7 +361,7 @@ window->addWallet(walletModel); if (m_wallet_models.empty()) { - window->setCurrentWallet(walletModel->getWalletName()); + window->setCurrentWallet(walletModel); } connect(walletModel, &WalletModel::coinsSent, paymentServer, diff --git a/src/qt/bitcoingui.h b/src/qt/bitcoingui.h --- a/src/qt/bitcoingui.h +++ b/src/qt/bitcoingui.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011-2016 The Bitcoin Core developers +// Copyright (c) 2011-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -213,7 +213,7 @@ unsigned int style, bool *ret = nullptr); #ifdef ENABLE_WALLET - bool setCurrentWallet(const QString &name); + bool setCurrentWallet(WalletModel *wallet_model); bool setCurrentWalletBySelectorIndex(int index); /** Set the UI status indicators based on the currently selected wallet. */ diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2011-2016 The Bitcoin Core developers +// Copyright (c) 2011-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -612,12 +612,12 @@ #ifdef ENABLE_WALLET bool BitcoinGUI::addWallet(WalletModel *walletModel) { - if (!walletFrame) return false; - const QString name = walletModel->getWalletName(); - QString display_name = - name.isEmpty() ? "[" + tr("default wallet") + "]" : name; + if (!walletFrame) { + return false; + } + const QString display_name = walletModel->getDisplayName(); setWalletActionsEnabled(true); - m_wallet_selector->addItem(display_name, name); + m_wallet_selector->addItem(display_name, QVariant::fromValue(walletModel)); if (m_wallet_selector->count() == 2) { m_wallet_selector_label_action->setVisible(true); m_wallet_selector_action->setVisible(true); @@ -630,8 +630,7 @@ if (!walletFrame) { return false; } - QString name = walletModel->getWalletName(); - int index = m_wallet_selector->findData(name); + int index = m_wallet_selector->findData(QVariant::fromValue(walletModel)); m_wallet_selector->removeItem(index); if (m_wallet_selector->count() == 0) { setWalletActionsEnabled(false); @@ -640,17 +639,20 @@ m_wallet_selector_action->setVisible(false); } rpcConsole->removeWallet(walletModel); - return walletFrame->removeWallet(name); + return walletFrame->removeWallet(walletModel); } -bool BitcoinGUI::setCurrentWallet(const QString &name) { - if (!walletFrame) return false; - return walletFrame->setCurrentWallet(name); +bool BitcoinGUI::setCurrentWallet(WalletModel *wallet_model) { + if (!walletFrame) { + return false; + } + return walletFrame->setCurrentWallet(wallet_model); } bool BitcoinGUI::setCurrentWalletBySelectorIndex(int index) { - QString internal_name = m_wallet_selector->itemData(index).toString(); - return setCurrentWallet(internal_name); + WalletModel *wallet_model = + m_wallet_selector->itemData(index).value(); + return setCurrentWallet(wallet_model); } void BitcoinGUI::removeAllWallets() { diff --git a/src/qt/rpcconsole.h b/src/qt/rpcconsole.h --- a/src/qt/rpcconsole.h +++ b/src/qt/rpcconsole.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011-2016 The Bitcoin Core developers +// Copyright (c) 2011-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -45,14 +45,14 @@ RPCParseCommandLine(interfaces::Node *node, std::string &strResult, const std::string &strCommand, bool fExecute, std::string *const pstrFilteredOut = nullptr, - const std::string *walletID = nullptr); + const WalletModel *wallet_model = nullptr); static bool RPCExecuteCommandLine(interfaces::Node &node, std::string &strResult, const std::string &strCommand, std::string *const pstrFilteredOut = nullptr, - const std::string *walletID = nullptr) { + const WalletModel *wallet_model = nullptr) { return RPCParseCommandLine(&node, strResult, strCommand, true, - pstrFilteredOut, walletID); + pstrFilteredOut, wallet_model); } void setClientModel(ClientModel *model); @@ -136,7 +136,7 @@ Q_SIGNALS: // For RPC command executor void stopExecutor(); - void cmdRequest(const QString &command, const QString &walletID); + void cmdRequest(const QString &command, const WalletModel *wallet_model); private: void startExecutor(); @@ -167,7 +167,7 @@ int consoleFontSize = 0; QCompleter *autoCompleter = nullptr; QThread thread; - QString m_last_wallet_id; + WalletModel *m_last_wallet_model{nullptr}; /** Update UI with latest network info from model. */ void updateNetworkState(); diff --git a/src/qt/rpcconsole.cpp b/src/qt/rpcconsole.cpp --- a/src/qt/rpcconsole.cpp +++ b/src/qt/rpcconsole.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2011-2016 The Bitcoin Core developers +// Copyright (c) 2011-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -82,7 +82,7 @@ RPCExecutor(interfaces::Node &node) : m_node(node) {} public Q_SLOTS: - void request(const QString &command, const QString &walletID); + void request(const QString &command, const WalletModel *wallet_model); Q_SIGNALS: void reply(int category, const QString &command); @@ -152,7 +152,7 @@ const std::string &strCommand, const bool fExecute, std::string *const pstrFilteredOut, - const std::string *walletID) { + const WalletModel *wallet_model) { std::vector> stack; stack.push_back(std::vector()); @@ -335,10 +335,10 @@ std::string uri; #ifdef ENABLE_WALLET - if (walletID) { + if (wallet_model) { QByteArray encodedName = QUrl::toPercentEncoding( - QString::fromStdString(*walletID)); + wallet_model->getWalletName()); uri = "/wallet/" + std::string(encodedName.constData(), encodedName.length()); @@ -442,7 +442,8 @@ } } -void RPCExecutor::request(const QString &command, const QString &walletID) { +void RPCExecutor::request(const QString &command, + const WalletModel *wallet_model) { try { std::string result; std::string executableCommand = command.toStdString() + "\n"; @@ -480,10 +481,8 @@ "getblock(getblockhash(0),true)[tx][0]\n\n"))); return; } - std::string wallet_id = walletID.toStdString(); if (!RPCConsole::RPCExecuteCommandLine( - m_node, result, executableCommand, nullptr, - walletID.isNull() ? nullptr : &wallet_id)) { + m_node, result, executableCommand, nullptr, wallet_model)) { Q_EMIT reply(RPCConsole::CMD_ERROR, QString("Parse error: unbalanced ' or \"")); return; @@ -819,12 +818,10 @@ #ifdef ENABLE_WALLET void RPCConsole::addWallet(WalletModel *const walletModel) { - const QString name = walletModel->getWalletName(); - // use name for text and internal data object (to allow to move to a wallet - // id later) - QString display_name = - name.isEmpty() ? "[" + tr("default wallet") + "]" : name; - ui->WalletSelector->addItem(display_name, name); + // use name for text and wallet model for internal data object (to allow to + // move to a wallet id later) + ui->WalletSelector->addItem(walletModel->getDisplayName(), + QVariant::fromValue(walletModel)); if (ui->WalletSelector->count() == 2 && !isVisible()) { // First wallet added, set to default so long as the window isn't // presently visible (and potentially in use) @@ -837,8 +834,8 @@ } void RPCConsole::removeWallet(WalletModel *const walletModel) { - const QString name = walletModel->getWalletName(); - ui->WalletSelector->removeItem(ui->WalletSelector->findData(name)); + ui->WalletSelector->removeItem( + ui->WalletSelector->findData(QVariant::fromValue(walletModel))); if (ui->WalletSelector->count() == 2) { ui->WalletSelector->setVisible(false); ui->WalletSelectorLabel->setVisible(false); @@ -1055,29 +1052,28 @@ cmdBeforeBrowsing = QString(); - QString walletID; + WalletModel *wallet_model{nullptr}; #ifdef ENABLE_WALLET const int wallet_index = ui->WalletSelector->currentIndex(); if (wallet_index > 0) { - walletID = (QString)ui->WalletSelector->itemData(wallet_index) - .value(); + wallet_model = ui->WalletSelector->itemData(wallet_index) + .value(); } - if (m_last_wallet_id != walletID) { - if (walletID.isNull()) { + if (m_last_wallet_model != wallet_model) { + if (wallet_model) { + message(CMD_REQUEST, tr("Executing command using \"%1\" wallet") + .arg(wallet_model->getWalletName())); + } else { message(CMD_REQUEST, tr("Executing command without any wallet")); - } else { - message( - CMD_REQUEST, - tr("Executing command using \"%1\" wallet").arg(walletID)); } - m_last_wallet_id = walletID; + m_last_wallet_model = wallet_model; } #endif message(CMD_REQUEST, QString::fromStdString(strFilteredCmd)); - Q_EMIT cmdRequest(cmd, walletID); + Q_EMIT cmdRequest(cmd, m_last_wallet_model); cmd = QString::fromStdString(strFilteredCmd); diff --git a/src/qt/walletframe.h b/src/qt/walletframe.h --- a/src/qt/walletframe.h +++ b/src/qt/walletframe.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011-2016 The Bitcoin Core developers +// Copyright (c) 2011-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -37,8 +37,8 @@ void setClientModel(ClientModel *clientModel); bool addWallet(WalletModel *walletModel); - bool setCurrentWallet(const QString &name); - bool removeWallet(const QString &name); + bool setCurrentWallet(WalletModel *wallet_model); + bool removeWallet(WalletModel *wallet_model); void removeAllWallets(); bool handlePaymentRequest(const SendCoinsRecipient &recipient); @@ -54,7 +54,7 @@ QStackedWidget *walletStack; BitcoinGUI *gui; ClientModel *clientModel; - QMap mapWalletViews; + QMap mapWalletViews; bool bOutOfSync; diff --git a/src/qt/walletframe.cpp b/src/qt/walletframe.cpp --- a/src/qt/walletframe.cpp +++ b/src/qt/walletframe.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2011-2016 The Bitcoin Core developers +// Copyright (c) 2011-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -39,8 +39,7 @@ return false; } - const QString name = walletModel->getWalletName(); - if (mapWalletViews.count(name) > 0) { + if (mapWalletViews.count(walletModel) > 0) { return false; } @@ -57,7 +56,7 @@ } walletStack->addWidget(walletView); - mapWalletViews[name] = walletView; + mapWalletViews[walletModel] = walletView; // Ensure a walletView is able to show the main window connect(walletView, &WalletView::showNormalIfMinimized, @@ -69,31 +68,31 @@ return true; } -bool WalletFrame::setCurrentWallet(const QString &name) { - if (mapWalletViews.count(name) == 0) { +bool WalletFrame::setCurrentWallet(WalletModel *wallet_model) { + if (mapWalletViews.count(wallet_model) == 0) { return false; } - WalletView *walletView = mapWalletViews.value(name); + WalletView *walletView = mapWalletViews.value(wallet_model); walletStack->setCurrentWidget(walletView); assert(walletView); walletView->updateEncryptionStatus(); return true; } -bool WalletFrame::removeWallet(const QString &name) { - if (mapWalletViews.count(name) == 0) { +bool WalletFrame::removeWallet(WalletModel *wallet_model) { + if (mapWalletViews.count(wallet_model) == 0) { return false; } - WalletView *walletView = mapWalletViews.take(name); + WalletView *walletView = mapWalletViews.take(wallet_model); walletStack->removeWidget(walletView); delete walletView; return true; } void WalletFrame::removeAllWallets() { - QMap::const_iterator i; + QMap::const_iterator i; for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i) { walletStack->removeWidget(i.value()); } @@ -111,35 +110,35 @@ void WalletFrame::showOutOfSyncWarning(bool fShow) { bOutOfSync = fShow; - QMap::const_iterator i; + QMap::const_iterator i; for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i) { i.value()->showOutOfSyncWarning(fShow); } } void WalletFrame::gotoOverviewPage() { - QMap::const_iterator i; + QMap::const_iterator i; for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i) { i.value()->gotoOverviewPage(); } } void WalletFrame::gotoHistoryPage() { - QMap::const_iterator i; + QMap::const_iterator i; for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i) { i.value()->gotoHistoryPage(); } } void WalletFrame::gotoReceiveCoinsPage() { - QMap::const_iterator i; + QMap::const_iterator i; for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i) { i.value()->gotoReceiveCoinsPage(); } } void WalletFrame::gotoSendCoinsPage(QString addr) { - QMap::const_iterator i; + QMap::const_iterator i; for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i) { i.value()->gotoSendCoinsPage(addr); } diff --git a/src/qt/walletmodel.h b/src/qt/walletmodel.h --- a/src/qt/walletmodel.h +++ b/src/qt/walletmodel.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011-2016 The Bitcoin Core developers +// Copyright (c) 2011-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -216,6 +216,7 @@ const CChainParams &getChainParams() const; QString getWalletName() const; + QString getDisplayName() const; bool isMultiwallet(); diff --git a/src/qt/walletmodel.cpp b/src/qt/walletmodel.cpp --- a/src/qt/walletmodel.cpp +++ b/src/qt/walletmodel.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2011-2016 The Bitcoin Core developers +// Copyright (c) 2011-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -483,6 +483,11 @@ return QString::fromStdString(m_wallet->getWalletName()); } +QString WalletModel::getDisplayName() const { + const QString name = getWalletName(); + return name.isEmpty() ? "[" + tr("default wallet") + "]" : name; +} + bool WalletModel::isMultiwallet() { return m_node.getWallets().size() > 1; }