gui: Sort wallets in open wallet menu

This commit is contained in:
João Barbosa 2019-05-27 19:07:05 +01:00
parent b5fa2319d8
commit 224eb9534a
3 changed files with 16 additions and 11 deletions

View file

@ -371,13 +371,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;
@ -410,7 +409,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);
} }

View file

@ -46,13 +46,16 @@ std::vector<WalletModel*> WalletController::getWallets() const
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>
@ -41,7 +41,10 @@ public:
~WalletController(); ~WalletController();
std::vector<WalletModel*> getWallets() const; std::vector<WalletModel*> getWallets() const;
std::vector<std::string> getWalletsAvailableToOpen() 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);