From 224eb9534a8d2b0f140ecb0cc00c61af8ba1da4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Barbosa?= Date: Mon, 27 May 2019 19:07:05 +0100 Subject: [PATCH 1/2] gui: Sort wallets in open wallet menu --- src/qt/bitcoingui.cpp | 9 ++++----- src/qt/walletcontroller.cpp | 11 +++++++---- src/qt/walletcontroller.h | 7 +++++-- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index babb2ce5189..1fa05d550ac 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -371,13 +371,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; @@ -410,7 +409,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); } diff --git a/src/qt/walletcontroller.cpp b/src/qt/walletcontroller.cpp index 019bd658234..3bb0b8dbb69 100644 --- a/src/qt/walletcontroller.cpp +++ b/src/qt/walletcontroller.cpp @@ -46,13 +46,16 @@ std::vector WalletController::getWallets() const 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 19b3a822531..dcd64aac7b4 100644 --- a/src/qt/walletcontroller.h +++ b/src/qt/walletcontroller.h @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include @@ -41,7 +41,10 @@ public: ~WalletController(); std::vector getWallets() const; - std::vector getWalletsAvailableToOpen() 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); From fa90fe6440301edba71a6f0b1ba3ef1c78d5eae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Barbosa?= Date: Mon, 27 May 2019 22:14:29 +0100 Subject: [PATCH 2/2] refactor: Rename getWallets to getOpenWallets in WalletController --- src/qt/bitcoingui.cpp | 2 +- src/qt/walletcontroller.cpp | 2 +- src/qt/walletcontroller.h | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index 1fa05d550ac..98c291aa634 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -639,7 +639,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 3bb0b8dbb69..4d489d7f44e 100644 --- a/src/qt/walletcontroller.cpp +++ b/src/qt/walletcontroller.cpp @@ -40,7 +40,7 @@ WalletController::~WalletController() m_activity_thread.wait(); } -std::vector WalletController::getWallets() const +std::vector WalletController::getOpenWallets() const { QMutexLocker locker(&m_mutex); return m_wallets; diff --git a/src/qt/walletcontroller.h b/src/qt/walletcontroller.h index dcd64aac7b4..03039dd7950 100644 --- a/src/qt/walletcontroller.h +++ b/src/qt/walletcontroller.h @@ -40,7 +40,8 @@ public: WalletController(interfaces::Node& node, const PlatformStyle* platform_style, OptionsModel* options_model, QObject* parent); ~WalletController(); - std::vector getWallets() 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.