Skip to content

Commit 6ba3b2f

Browse files
authored
Merge pull request #4081 from xdustinface/pr-fix-issue-3521
backport: Some wallet related PRs
2 parents ff30f3f + 773e39e commit 6ba3b2f

38 files changed

+446
-170
lines changed

src/bench/coin_selection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static void addCoin(const CAmount& nValue, const CWallet& wallet, std::vector<CO
3333
// (https://github.com/bitcoin/bitcoin/issues/7883#issuecomment-224807484)
3434
static void CoinSelection(benchmark::State& state)
3535
{
36-
const CWallet wallet("dummy", WalletDatabase::CreateDummy());
36+
const CWallet wallet(WalletLocation(), WalletDatabase::CreateDummy());
3737
std::vector<COutput> vCoins;
3838
LOCK(wallet.cs_wallet);
3939

src/interfaces/wallet.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,10 @@ class WalletImpl : public Wallet
484484
}
485485
bool hdEnabled() override { return m_wallet.IsHDEnabled(); }
486486
CoinJoin::Client& coinJoin() override { return m_coinjoin; }
487+
std::unique_ptr<Handler> handleUnload(UnloadFn fn) override
488+
{
489+
return MakeHandler(m_wallet.NotifyUnload.connect(fn));
490+
}
487491
std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) override
488492
{
489493
return MakeHandler(m_wallet.ShowProgress.connect(fn));

src/interfaces/wallet.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ class Wallet
249249

250250
virtual CoinJoin::Client& coinJoin() = 0;
251251

252+
//! Register handler for unload message.
253+
using UnloadFn = std::function<void()>;
254+
virtual std::unique_ptr<Handler> handleUnload(UnloadFn fn) = 0;
255+
252256
//! Register handler for show progress messages.
253257
using ShowProgressFn = std::function<void(const std::string& title, int progress)>;
254258
virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;

src/qt/askpassphrasedialog.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ void AskPassphraseDialog::accept()
140140
if (model->wallet().hdEnabled()) {
141141
QMessageBox::warning(this, tr("Wallet encrypted"),
142142
"<qt>" +
143-
tr("%1 will close now to finish the encryption process. "
143+
tr("Your wallet is now encrypted. "
144144
"Remember that encrypting your wallet cannot fully protect "
145-
"your funds from being stolen by malware infecting your computer.").arg(tr(PACKAGE_NAME)) +
145+
"your funds from being stolen by malware infecting your computer.") +
146146
"<br><br><b>" +
147147
tr("IMPORTANT: Any previous backups you have made of your wallet file "
148148
"should be replaced with the newly generated, encrypted wallet file. "
@@ -152,17 +152,16 @@ void AskPassphraseDialog::accept()
152152
} else {
153153
QMessageBox::warning(this, tr("Wallet encrypted"),
154154
"<qt>" +
155-
tr("%1 will close now to finish the encryption process. "
155+
tr("Your wallet is now encrypted. "
156156
"Remember that encrypting your wallet cannot fully protect "
157-
"your funds from being stolen by malware infecting your computer.").arg(tr(PACKAGE_NAME)) +
157+
"your funds from being stolen by malware infecting your computer.") +
158158
"<br><br><b>" +
159159
tr("IMPORTANT: Any previous backups you have made of your wallet file "
160160
"should be replaced with the newly generated, encrypted wallet file. "
161161
"For security reasons, previous backups of the unencrypted wallet file "
162162
"will become useless as soon as you start using the new, encrypted wallet.") +
163163
"</b></qt>");
164164
}
165-
QApplication::quit();
166165
}
167166
else
168167
{

src/qt/bitcoingui.cpp

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -639,8 +639,19 @@ void BitcoinGUI::createToolBars()
639639

640640
#ifdef ENABLE_WALLET
641641
m_wallet_selector = new QComboBox(this);
642-
m_wallet_selector->setHidden(true);
643-
connect(m_wallet_selector, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(setCurrentWallet(const QString&)));
642+
connect(m_wallet_selector, SIGNAL(currentIndexChanged(int)), this, SLOT(setCurrentWalletBySelectorIndex(int)));
643+
644+
QVBoxLayout* walletSelectorLayout = new QVBoxLayout(this);
645+
walletSelectorLayout->addWidget(m_wallet_selector);
646+
walletSelectorLayout->setSpacing(0);
647+
walletSelectorLayout->setMargin(0);
648+
walletSelectorLayout->setContentsMargins(5, 0, 5, 0);
649+
QWidget* walletSelector = new QWidget(this);
650+
walletSelector->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
651+
walletSelector->setObjectName("walletSelector");
652+
walletSelector->setLayout(walletSelectorLayout);
653+
m_wallet_selector_action = appToolBar->insertWidget(appToolBarLogoAction, walletSelector);
654+
m_wallet_selector_action->setVisible(false);
644655
#endif
645656

646657
QLabel *logoLabel = new QLabel();
@@ -769,32 +780,44 @@ bool BitcoinGUI::addWallet(WalletModel *walletModel)
769780
if(!walletFrame)
770781
return false;
771782
const QString name = walletModel->getWalletName();
783+
QString display_name = name.isEmpty() ? "["+tr("default wallet")+"]" : name;
772784
setWalletActionsEnabled(true);
773-
m_wallet_selector->addItem(name);
785+
m_wallet_selector->addItem(display_name, name);
774786
if (m_wallet_selector->count() == 2) {
775-
m_wallet_selector->setHidden(false);
776-
QVBoxLayout* layout = new QVBoxLayout(this);
777-
layout->addWidget(m_wallet_selector);
778-
layout->setSpacing(0);
779-
layout->setMargin(0);
780-
layout->setContentsMargins(5, 0, 5, 0);
781-
QWidget* walletSelector = new QWidget(this);
782-
walletSelector->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
783-
walletSelector->setObjectName("walletSelector");
784-
walletSelector->setLayout(layout);
785-
appToolBar->insertWidget(appToolBarLogoAction, walletSelector);
787+
m_wallet_selector_action->setVisible(true);
786788
}
787789
rpcConsole->addWallet(walletModel);
788790
return walletFrame->addWallet(walletModel);
789791
}
790792

793+
bool BitcoinGUI::removeWallet(WalletModel* walletModel)
794+
{
795+
if (!walletFrame) return false;
796+
QString name = walletModel->getWalletName();
797+
int index = m_wallet_selector->findData(name);
798+
m_wallet_selector->removeItem(index);
799+
if (m_wallet_selector->count() == 0) {
800+
setWalletActionsEnabled(false);
801+
} else if (m_wallet_selector->count() == 1) {
802+
m_wallet_selector_action->setVisible(false);
803+
}
804+
rpcConsole->removeWallet(walletModel);
805+
return walletFrame->removeWallet(name);
806+
}
807+
791808
bool BitcoinGUI::setCurrentWallet(const QString& name)
792809
{
793810
if(!walletFrame)
794811
return false;
795812
return walletFrame->setCurrentWallet(name);
796813
}
797814

815+
bool BitcoinGUI::setCurrentWalletBySelectorIndex(int index)
816+
{
817+
QString internal_name = m_wallet_selector->itemData(index).toString();
818+
return setCurrentWallet(internal_name);
819+
}
820+
798821
void BitcoinGUI::removeAllWallets()
799822
{
800823
if(!walletFrame)

src/qt/bitcoingui.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class BitcoinGUI : public QMainWindow
7676
functionality.
7777
*/
7878
bool addWallet(WalletModel *walletModel);
79+
bool removeWallet(WalletModel* walletModel);
7980
void removeAllWallets();
8081
#endif // ENABLE_WALLET
8182
bool enableWallet;
@@ -140,8 +141,8 @@ class BitcoinGUI : public QMainWindow
140141
QAction *openAction;
141142
QAction *showHelpMessageAction;
142143
QAction *showCoinJoinHelpAction;
144+
QAction *m_wallet_selector_action = nullptr;
143145

144-
QLabel *m_wallet_selector_label;
145146
QComboBox *m_wallet_selector;
146147

147148
QSystemTrayIcon *trayIcon;
@@ -238,6 +239,7 @@ public Q_SLOTS:
238239

239240
#ifdef ENABLE_WALLET
240241
bool setCurrentWallet(const QString& name);
242+
bool setCurrentWalletBySelectorIndex(int index);
241243
/** Set the UI status indicators based on the currently selected wallet.
242244
*/
243245
void updateWalletStatus();

src/qt/dash.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ public Q_SLOTS:
212212
/// Handle runaway exceptions. Shows a message box with the problem and quits the program.
213213
void handleRunawayException(const QString &message);
214214
void addWallet(WalletModel* walletModel);
215+
void removeWallet();
215216

216217
Q_SIGNALS:
217218
void requestedInitialize();
@@ -454,11 +455,22 @@ void BitcoinApplication::addWallet(WalletModel* walletModel)
454455

455456
connect(walletModel, SIGNAL(coinsSent(WalletModel*, SendCoinsRecipient, QByteArray)),
456457
paymentServer, SLOT(fetchPaymentACK(WalletModel*, const SendCoinsRecipient&, QByteArray)));
458+
connect(walletModel, SIGNAL(unload()), this, SLOT(removeWallet()));
457459

458460
m_wallet_models.push_back(walletModel);
459461
#endif
460462
}
461463

464+
void BitcoinApplication::removeWallet()
465+
{
466+
#ifdef ENABLE_WALLET
467+
WalletModel* walletModel = static_cast<WalletModel*>(sender());
468+
m_wallet_models.erase(std::find(m_wallet_models.begin(), m_wallet_models.end(), walletModel));
469+
window->removeWallet(walletModel);
470+
walletModel->deleteLater();
471+
#endif
472+
}
473+
462474
void BitcoinApplication::initializeResult(bool success)
463475
{
464476
qDebug() << __func__ << ": Initialization result: " << success;
@@ -478,8 +490,10 @@ void BitcoinApplication::initializeResult(bool success)
478490

479491
#ifdef ENABLE_WALLET
480492
m_handler_load_wallet = m_node.handleLoadWallet([this](std::unique_ptr<interfaces::Wallet> wallet) {
481-
QMetaObject::invokeMethod(this, "addWallet", Qt::QueuedConnection,
482-
Q_ARG(WalletModel*, new WalletModel(std::move(wallet), m_node, optionsModel)));
493+
WalletModel* wallet_model = new WalletModel(std::move(wallet), m_node, optionsModel, nullptr);
494+
// Fix wallet model thread affinity.
495+
wallet_model->moveToThread(thread());
496+
QMetaObject::invokeMethod(this, "addWallet", Qt::QueuedConnection, Q_ARG(WalletModel*, wallet_model));
483497
});
484498

485499
for (auto& wallet : m_node.getWallets()) {

src/qt/res/css/dark.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,10 @@ QToolBar > QToolButton:checked {
680680
color: #c7c7c7;
681681
}
682682

683+
QToolBar > QToolButton:disabled {
684+
color: #4a4a4b;
685+
}
686+
683687
/******************************************************
684688
QToolTip
685689
******************************************************/

src/qt/res/css/light.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,10 @@ QToolBar > QToolButton:checked {
665665
color: #555;
666666
}
667667

668+
QToolBar > QToolButton:disabled {
669+
color: #a7a7a7;
670+
}
671+
668672
/******************************************************
669673
QToolTip
670674
******************************************************/

src/qt/rpcconsole.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,8 @@ void RPCConsole::addWallet(WalletModel * const walletModel)
743743
{
744744
const QString name = walletModel->getWalletName();
745745
// use name for text and internal data object (to allow to move to a wallet id later)
746-
ui->WalletSelector->addItem(name, name);
746+
QString display_name = name.isEmpty() ? "["+tr("default wallet")+"]" : name;
747+
ui->WalletSelector->addItem(display_name, name);
747748
if (ui->WalletSelector->count() == 2 && !isVisible()) {
748749
// First wallet added, set to default so long as the window isn't presently visible (and potentially in use)
749750
ui->WalletSelector->setCurrentIndex(1);
@@ -753,6 +754,16 @@ void RPCConsole::addWallet(WalletModel * const walletModel)
753754
ui->WalletSelectorLabel->setVisible(true);
754755
}
755756
}
757+
758+
void RPCConsole::removeWallet(WalletModel * const walletModel)
759+
{
760+
const QString name = walletModel->getWalletName();
761+
ui->WalletSelector->removeItem(ui->WalletSelector->findData(name));
762+
if (ui->WalletSelector->count() == 2) {
763+
ui->WalletSelector->setVisible(false);
764+
ui->WalletSelectorLabel->setVisible(false);
765+
}
766+
}
756767
#endif
757768

758769
static QString categoryClass(int category)

0 commit comments

Comments
 (0)