From a63956ad32462a5179af1645752a45971aac841c Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Mon, 8 Jul 2019 16:27:18 +0200 Subject: [PATCH] Merge #16106: gui: Sort wallets in open wallet menu MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fa90fe6440301edba71a6f0b1ba3ef1c78d5eae4 refactor: Rename getWallets to getOpenWallets in WalletController (João Barbosa) 224eb9534a8d2b0f140ecb0cc00c61af8ba1da4e 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 fa90fe6440301edba71a6f0b1ba3ef1c78d5eae4 Tree-SHA512: 349ea195021e56760dea3551126d1b9dc4821faab44aaf2858229db4fddaf0ce0b5eb0a8fa5025f47c77134b003067a77e8c340f9655a99e1305d430ccecced8 --- src/qt/bitcoingui.cpp | 11 +++++------ src/qt/walletcontroller.cpp | 13 ++++++++----- src/qt/walletcontroller.h | 10 +++++++--- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index 88840c1650..24ba2be0d5 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -485,13 +485,12 @@ void BitcoinGUI::createActions() connect(openAction, &QAction::triggered, this, &BitcoinGUI::openClicked); connect(m_open_wallet_menu, &QMenu::aboutToShow, [this] { m_open_wallet_menu->clear(); - std::vector available_wallets = m_wallet_controller->getWalletsAvailableToOpen(); - std::vector wallets = m_node.listWalletDir(); - for (const auto& path : wallets) { + for (const std::pair& i : m_wallet_controller->listWalletDir()) { + const std::string& path = i.first; QString name = path.empty() ? QString("["+tr("default wallet")+"]") : QString::fromStdString(path); QAction* action = m_open_wallet_menu->addAction(name); - if (std::find(available_wallets.begin(), available_wallets.end(), path) == available_wallets.end()) { + if (i.second) { // This wallet is already loaded action->setEnabled(false); continue; @@ -524,7 +523,7 @@ void BitcoinGUI::createActions() assert(invoked); }); } - if (wallets.empty()) { + if (m_open_wallet_menu->isEmpty()) { QAction* action = m_open_wallet_menu->addAction(tr("No wallets available")); action->setEnabled(false); } @@ -863,7 +862,7 @@ void BitcoinGUI::setWalletController(WalletController* wallet_controller) connect(wallet_controller, &WalletController::walletAdded, this, &BitcoinGUI::addWallet); connect(wallet_controller, &WalletController::walletRemoved, this, &BitcoinGUI::removeWallet); - for (WalletModel* wallet_model : m_wallet_controller->getWallets()) { + for (WalletModel* wallet_model : m_wallet_controller->getOpenWallets()) { addWallet(wallet_model); } } diff --git a/src/qt/walletcontroller.cpp b/src/qt/walletcontroller.cpp index c67bf8900d..e2409ef37d 100644 --- a/src/qt/walletcontroller.cpp +++ b/src/qt/walletcontroller.cpp @@ -39,19 +39,22 @@ WalletController::~WalletController() m_activity_thread.wait(); } -std::vector WalletController::getWallets() const +std::vector WalletController::getOpenWallets() const { QMutexLocker locker(&m_mutex); return m_wallets; } -std::vector WalletController::getWalletsAvailableToOpen() const +std::map WalletController::listWalletDir() const { QMutexLocker locker(&m_mutex); - std::vector wallets = m_node.listWalletDir(); + std::map wallets; + for (const std::string& name : m_node.listWalletDir()) { + wallets[name] = false; + } for (WalletModel* wallet_model : m_wallets) { - auto it = std::remove(wallets.begin(), wallets.end(), wallet_model->wallet().getWalletName()); - if (it != wallets.end()) wallets.erase(it); + auto it = wallets.find(wallet_model->wallet().getWalletName()); + if (it != wallets.end()) it->second = true; } return wallets; } diff --git a/src/qt/walletcontroller.h b/src/qt/walletcontroller.h index 3f0da1a87d..7a4746a44c 100644 --- a/src/qt/walletcontroller.h +++ b/src/qt/walletcontroller.h @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include @@ -40,8 +40,12 @@ public: WalletController(interfaces::Node& node, OptionsModel* options_model, QObject* parent); ~WalletController(); - std::vector getWallets() const; - std::vector getWalletsAvailableToOpen() const; + //! Returns wallet models currently open. + std::vector getOpenWallets() const; + + //! Returns all wallet names in the wallet dir mapped to whether the wallet + //! is loaded. + std::map listWalletDir() const; OpenWalletActivity* openWallet(const std::string& name, QWidget* parent = nullptr); void closeWallet(WalletModel* wallet_model, QWidget* parent = nullptr);