Skip to content

Commit 2679bb8

Browse files
committed
Merge #16106: gui: Sort wallets in open wallet menu
fa90fe6 refactor: Rename getWallets to getOpenWallets in WalletController (João Barbosa) 224eb95 gui: Sort wallets in open wallet menu (João Barbosa) Pull request description: Ensure wallets are sorted by name in the open wallet menu. This also improves the change done in #15957, since it avoids a second call to `listWalletDir`. ACKs for top commit: laanwj: code review ACK fa90fe6 Tree-SHA512: 349ea195021e56760dea3551126d1b9dc4821faab44aaf2858229db4fddaf0ce0b5eb0a8fa5025f47c77134b003067a77e8c340f9655a99e1305d430ccecced8
2 parents b5fa231 + fa90fe6 commit 2679bb8

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

src/qt/bitcoingui.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -371,13 +371,12 @@ void BitcoinGUI::createActions()
371371
connect(openAction, &QAction::triggered, this, &BitcoinGUI::openClicked);
372372
connect(m_open_wallet_menu, &QMenu::aboutToShow, [this] {
373373
m_open_wallet_menu->clear();
374-
std::vector<std::string> available_wallets = m_wallet_controller->getWalletsAvailableToOpen();
375-
std::vector<std::string> wallets = m_node.listWalletDir();
376-
for (const auto& path : wallets) {
374+
for (const std::pair<const std::string, bool>& i : m_wallet_controller->listWalletDir()) {
375+
const std::string& path = i.first;
377376
QString name = path.empty() ? QString("["+tr("default wallet")+"]") : QString::fromStdString(path);
378377
QAction* action = m_open_wallet_menu->addAction(name);
379378

380-
if (std::find(available_wallets.begin(), available_wallets.end(), path) == available_wallets.end()) {
379+
if (i.second) {
381380
// This wallet is already loaded
382381
action->setEnabled(false);
383382
continue;
@@ -410,7 +409,7 @@ void BitcoinGUI::createActions()
410409
assert(invoked);
411410
});
412411
}
413-
if (wallets.empty()) {
412+
if (m_open_wallet_menu->isEmpty()) {
414413
QAction* action = m_open_wallet_menu->addAction(tr("No wallets available"));
415414
action->setEnabled(false);
416415
}
@@ -640,7 +639,7 @@ void BitcoinGUI::setWalletController(WalletController* wallet_controller)
640639
connect(wallet_controller, &WalletController::walletAdded, this, &BitcoinGUI::addWallet);
641640
connect(wallet_controller, &WalletController::walletRemoved, this, &BitcoinGUI::removeWallet);
642641

643-
for (WalletModel* wallet_model : m_wallet_controller->getWallets()) {
642+
for (WalletModel* wallet_model : m_wallet_controller->getOpenWallets()) {
644643
addWallet(wallet_model);
645644
}
646645
}

src/qt/walletcontroller.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,22 @@ WalletController::~WalletController()
4040
m_activity_thread.wait();
4141
}
4242

43-
std::vector<WalletModel*> WalletController::getWallets() const
43+
std::vector<WalletModel*> WalletController::getOpenWallets() const
4444
{
4545
QMutexLocker locker(&m_mutex);
4646
return m_wallets;
4747
}
4848

49-
std::vector<std::string> WalletController::getWalletsAvailableToOpen() const
49+
std::map<std::string, bool> WalletController::listWalletDir() const
5050
{
5151
QMutexLocker locker(&m_mutex);
52-
std::vector<std::string> wallets = m_node.listWalletDir();
52+
std::map<std::string, bool> wallets;
53+
for (const std::string& name : m_node.listWalletDir()) {
54+
wallets[name] = false;
55+
}
5356
for (WalletModel* wallet_model : m_wallets) {
54-
auto it = std::remove(wallets.begin(), wallets.end(), wallet_model->wallet().getWalletName());
55-
if (it != wallets.end()) wallets.erase(it);
57+
auto it = wallets.find(wallet_model->wallet().getWalletName());
58+
if (it != wallets.end()) it->second = true;
5659
}
5760
return wallets;
5861
}

src/qt/walletcontroller.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <qt/walletmodel.h>
99
#include <sync.h>
1010

11-
#include <list>
11+
#include <map>
1212
#include <memory>
1313
#include <vector>
1414

@@ -40,8 +40,12 @@ class WalletController : public QObject
4040
WalletController(interfaces::Node& node, const PlatformStyle* platform_style, OptionsModel* options_model, QObject* parent);
4141
~WalletController();
4242

43-
std::vector<WalletModel*> getWallets() const;
44-
std::vector<std::string> getWalletsAvailableToOpen() const;
43+
//! Returns wallet models currently open.
44+
std::vector<WalletModel*> getOpenWallets() const;
45+
46+
//! Returns all wallet names in the wallet dir mapped to whether the wallet
47+
//! is loaded.
48+
std::map<std::string, bool> listWalletDir() const;
4549

4650
OpenWalletActivity* openWallet(const std::string& name, QWidget* parent = nullptr);
4751
void closeWallet(WalletModel* wallet_model, QWidget* parent = nullptr);

0 commit comments

Comments
 (0)