Merge #16106: gui: Sort wallets in open wallet menu

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
This commit is contained in:
Wladimir J. van der Laan 2019-07-08 16:27:18 +02:00 committed by pasta
parent ce9d941767
commit a63956ad32
3 changed files with 20 additions and 14 deletions

View File

@ -485,13 +485,12 @@ void BitcoinGUI::createActions()
connect(openAction, &QAction::triggered, this, &BitcoinGUI::openClicked); connect(openAction, &QAction::triggered, this, &BitcoinGUI::openClicked);
connect(m_open_wallet_menu, &QMenu::aboutToShow, [this] { connect(m_open_wallet_menu, &QMenu::aboutToShow, [this] {
m_open_wallet_menu->clear(); m_open_wallet_menu->clear();
std::vector<std::string> available_wallets = m_wallet_controller->getWalletsAvailableToOpen(); for (const std::pair<const std::string, bool>& i : m_wallet_controller->listWalletDir()) {
std::vector<std::string> wallets = m_node.listWalletDir(); const std::string& path = i.first;
for (const auto& path : wallets) {
QString name = path.empty() ? QString("["+tr("default wallet")+"]") : QString::fromStdString(path); QString name = path.empty() ? QString("["+tr("default wallet")+"]") : QString::fromStdString(path);
QAction* action = m_open_wallet_menu->addAction(name); 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 // This wallet is already loaded
action->setEnabled(false); action->setEnabled(false);
continue; continue;
@ -524,7 +523,7 @@ void BitcoinGUI::createActions()
assert(invoked); assert(invoked);
}); });
} }
if (wallets.empty()) { if (m_open_wallet_menu->isEmpty()) {
QAction* action = m_open_wallet_menu->addAction(tr("No wallets available")); QAction* action = m_open_wallet_menu->addAction(tr("No wallets available"));
action->setEnabled(false); 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::walletAdded, this, &BitcoinGUI::addWallet);
connect(wallet_controller, &WalletController::walletRemoved, this, &BitcoinGUI::removeWallet); 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); addWallet(wallet_model);
} }
} }

View File

@ -39,19 +39,22 @@ WalletController::~WalletController()
m_activity_thread.wait(); m_activity_thread.wait();
} }
std::vector<WalletModel*> WalletController::getWallets() const std::vector<WalletModel*> WalletController::getOpenWallets() const
{ {
QMutexLocker locker(&m_mutex); QMutexLocker locker(&m_mutex);
return m_wallets; return m_wallets;
} }
std::vector<std::string> WalletController::getWalletsAvailableToOpen() const std::map<std::string, bool> WalletController::listWalletDir() const
{ {
QMutexLocker locker(&m_mutex); QMutexLocker locker(&m_mutex);
std::vector<std::string> wallets = m_node.listWalletDir(); std::map<std::string, bool> wallets;
for (const std::string& name : m_node.listWalletDir()) {
wallets[name] = false;
}
for (WalletModel* wallet_model : m_wallets) { for (WalletModel* wallet_model : m_wallets) {
auto it = std::remove(wallets.begin(), wallets.end(), wallet_model->wallet().getWalletName()); auto it = wallets.find(wallet_model->wallet().getWalletName());
if (it != wallets.end()) wallets.erase(it); if (it != wallets.end()) it->second = true;
} }
return wallets; return wallets;
} }

View File

@ -8,7 +8,7 @@
#include <qt/walletmodel.h> #include <qt/walletmodel.h>
#include <sync.h> #include <sync.h>
#include <list> #include <map>
#include <memory> #include <memory>
#include <vector> #include <vector>
@ -40,8 +40,12 @@ public:
WalletController(interfaces::Node& node, OptionsModel* options_model, QObject* parent); WalletController(interfaces::Node& node, OptionsModel* options_model, QObject* parent);
~WalletController(); ~WalletController();
std::vector<WalletModel*> getWallets() const; //! Returns wallet models currently open.
std::vector<std::string> getWalletsAvailableToOpen() const; std::vector<WalletModel*> getOpenWallets() const;
//! Returns all wallet names in the wallet dir mapped to whether the wallet
//! is loaded.
std::map<std::string, bool> listWalletDir() const;
OpenWalletActivity* openWallet(const std::string& name, QWidget* parent = nullptr); OpenWalletActivity* openWallet(const std::string& name, QWidget* parent = nullptr);
void closeWallet(WalletModel* wallet_model, QWidget* parent = nullptr); void closeWallet(WalletModel* wallet_model, QWidget* parent = nullptr);