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 // IMPORTANT if it is no longer a typedef use the normal variant above
qRegisterMetaType< CAmount >("CAmount"); qRegisterMetaType< CAmount >("CAmount");
qRegisterMetaType< std::function<void()> >("std::function<void()>"); 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 /// 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: // Command-line options take precedence:
node->setupServerArgs(); node->setupServerArgs();

View file

@ -371,7 +371,9 @@ void BitcoinGUI::createActions()
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_action->menu()->addAction(name); QAction* action = m_open_wallet_action->menu()->addAction(name);
connect(action, &QAction::triggered, [this, path] { 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; return wallets;
} }
WalletModel* WalletController::openWallet(const std::string& name, QWidget* parent) OpenWalletActivity* WalletController::openWallet(const std::string& name, QWidget* parent)
{ {
std::string error, warning; OpenWalletActivity* activity = new OpenWalletActivity(this, name);
WalletModel* wallet_model = getOrCreateWallet(m_node.loadWallet(name, error, warning)); activity->moveToThread(&m_activity_thread);
if (!wallet_model) { QMetaObject::invokeMethod(activity, "open", Qt::QueuedConnection);
QMessageBox::warning(parent, tr("Open Wallet"), QString::fromStdString(error)); return activity;
}
if (!warning.empty()) {
QMessageBox::information(parent, tr("Open Wallet"), QString::fromStdString(warning));
}
return wallet_model;
} }
WalletModel* WalletController::getOrCreateWallet(std::unique_ptr<interfaces::Wallet> wallet) WalletModel* WalletController::getOrCreateWallet(std::unique_ptr<interfaces::Wallet> wallet)
@ -124,3 +119,24 @@ void WalletController::removeAndDeleteWallet(WalletModel* wallet_model)
// CWallet shared pointer. // CWallet shared pointer.
delete wallet_model; 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 <memory>
#include <vector> #include <vector>
#include <QMessageBox>
#include <QMutex> #include <QMutex>
#include <QThread> #include <QThread>
@ -23,6 +24,8 @@ class Handler;
class Node; class Node;
} // namespace interfaces } // namespace interfaces
class OpenWalletActivity;
/** /**
* Controller between interfaces::Node, WalletModel instances and the GUI. * Controller between interfaces::Node, WalletModel instances and the GUI.
*/ */
@ -40,7 +43,7 @@ public:
std::vector<WalletModel*> getWallets() const; std::vector<WalletModel*> getWallets() const;
std::vector<std::string> getWalletsAvailableToOpen() 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: private Q_SLOTS:
void addWallet(WalletModel* wallet_model); void addWallet(WalletModel* wallet_model);
@ -59,6 +62,28 @@ private:
mutable QMutex m_mutex; mutable QMutex m_mutex;
std::vector<WalletModel*> m_wallets; std::vector<WalletModel*> m_wallets;
std::unique_ptr<interfaces::Handler> m_handler_load_wallet; 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 #endif // BITCOIN_QT_WALLETCONTROLLER_H