gui: Add OpenWalletActivity

This commit is contained in:
João Barbosa 2019-01-21 16:58:20 +00:00
parent 4c8982a88e
commit 8847cdaaae
4 changed files with 56 additions and 13 deletions

View file

@ -456,7 +456,7 @@ int GuiMain(int argc, char* argv[])
// IMPORTANT if it is no longer a typedef use the normal variant above
qRegisterMetaType< CAmount >("CAmount");
qRegisterMetaType< std::function<void()> >("std::function<void()>");
qRegisterMetaType<QMessageBox::Icon>("QMessageBox::Icon");
/// 2. Parse command-line options. We do this after qt in order to show an error if there are problems parsing these
// Command-line options take precedence:
node->setupServerArgs();

View file

@ -371,7 +371,9 @@ void BitcoinGUI::createActions()
QString name = path.empty() ? QString("["+tr("default wallet")+"]") : QString::fromStdString(path);
QAction* action = m_open_wallet_action->menu()->addAction(name);
connect(action, &QAction::triggered, [this, path] {
setCurrentWallet(m_wallet_controller->openWallet(path));
OpenWalletActivity* activity = m_wallet_controller->openWallet(path);
connect(activity, &OpenWalletActivity::opened, this, &BitcoinGUI::setCurrentWallet);
connect(activity, &OpenWalletActivity::finished, activity, &QObject::deleteLater);
});
}
});

View file

@ -55,17 +55,12 @@ std::vector<std::string> WalletController::getWalletsAvailableToOpen() const
return wallets;
}
WalletModel* WalletController::openWallet(const std::string& name, QWidget* parent)
OpenWalletActivity* WalletController::openWallet(const std::string& name, QWidget* parent)
{
std::string error, warning;
WalletModel* wallet_model = getOrCreateWallet(m_node.loadWallet(name, error, warning));
if (!wallet_model) {
QMessageBox::warning(parent, tr("Open Wallet"), QString::fromStdString(error));
}
if (!warning.empty()) {
QMessageBox::information(parent, tr("Open Wallet"), QString::fromStdString(warning));
}
return wallet_model;
OpenWalletActivity* activity = new OpenWalletActivity(this, name);
activity->moveToThread(&m_activity_thread);
QMetaObject::invokeMethod(activity, "open", Qt::QueuedConnection);
return activity;
}
WalletModel* WalletController::getOrCreateWallet(std::unique_ptr<interfaces::Wallet> wallet)
@ -124,3 +119,24 @@ void WalletController::removeAndDeleteWallet(WalletModel* wallet_model)
// CWallet shared pointer.
delete wallet_model;
}
OpenWalletActivity::OpenWalletActivity(WalletController* wallet_controller, const std::string& name)
: m_wallet_controller(wallet_controller)
, m_name(name)
{}
void OpenWalletActivity::open()
{
std::string error, warning;
std::unique_ptr<interfaces::Wallet> wallet = m_wallet_controller->m_node.loadWallet(m_name, error, warning);
if (!warning.empty()) {
Q_EMIT message(QMessageBox::Warning, QString::fromStdString(warning));
}
if (wallet) {
Q_EMIT opened(m_wallet_controller->getOrCreateWallet(std::move(wallet)));
} else {
Q_EMIT message(QMessageBox::Critical, QString::fromStdString(error));
}
Q_EMIT finished();
}

View file

@ -12,6 +12,7 @@
#include <memory>
#include <vector>
#include <QMessageBox>
#include <QMutex>
#include <QThread>
@ -23,6 +24,8 @@ class Handler;
class Node;
} // namespace interfaces
class OpenWalletActivity;
/**
* Controller between interfaces::Node, WalletModel instances and the GUI.
*/
@ -40,7 +43,7 @@ public:
std::vector<WalletModel*> getWallets() const;
std::vector<std::string> getWalletsAvailableToOpen() const;
WalletModel* openWallet(const std::string& name, QWidget* parent = nullptr);
OpenWalletActivity* openWallet(const std::string& name, QWidget* parent = nullptr);
private Q_SLOTS:
void addWallet(WalletModel* wallet_model);
@ -59,6 +62,28 @@ private:
mutable QMutex m_mutex;
std::vector<WalletModel*> m_wallets;
std::unique_ptr<interfaces::Handler> m_handler_load_wallet;
friend class OpenWalletActivity;
};
class OpenWalletActivity : public QObject
{
Q_OBJECT
public:
OpenWalletActivity(WalletController* wallet_controller, const std::string& name);
public Q_SLOTS:
void open();
Q_SIGNALS:
void message(QMessageBox::Icon icon, const QString text);
void finished();
void opened(WalletModel* wallet_model);
private:
WalletController* const m_wallet_controller;
std::string const m_name;
};
#endif // BITCOIN_QT_WALLETCONTROLLER_H