Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Wallet Restore in the GUI #471

Merged
merged 2 commits into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/release-notes/release-notes-471.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
GUI changes
--------

- A new menu item to restore a wallet from a backup file has been added (#471).
33 changes: 32 additions & 1 deletion src/qt/bitcoingui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <QCursor>
#include <QDateTime>
#include <QDragEnterEvent>
#include <QInputDialog>
#include <QKeySequence>
#include <QListWidget>
#include <QMenu>
Expand Down Expand Up @@ -348,6 +349,12 @@ void BitcoinGUI::createActions()
m_create_wallet_action->setEnabled(false);
m_create_wallet_action->setStatusTip(tr("Create a new wallet"));

//: Name of the menu item that restores wallet from a backup file.
m_restore_wallet_action = new QAction(tr("Restore Wallet…"), this);
m_restore_wallet_action->setEnabled(false);
//: Status tip for Restore Wallet menu item
m_restore_wallet_action->setStatusTip(tr("Restore a wallet from a backup file"));
w0xlt marked this conversation as resolved.
Show resolved Hide resolved

m_close_all_wallets_action = new QAction(tr("Close All Wallets…"), this);
m_close_all_wallets_action->setStatusTip(tr("Close all wallets"));

Expand Down Expand Up @@ -412,6 +419,27 @@ void BitcoinGUI::createActions()
action->setEnabled(false);
}
});
connect(m_restore_wallet_action, &QAction::triggered, [this] {
//: Name of the wallet data file format.
QString name_data_file = tr("Wallet Data");

//: The title for Restore Wallet File Windows
QString title_windows = tr("Load Wallet Backup");

QString backup_file = GUIUtil::getOpenFileName(this, title_windows, QString(), name_data_file + QLatin1String(" (*.dat)"), nullptr);
if (backup_file.isEmpty()) return;

bool wallet_name_ok;
//: Title of the Restore Wallet input dialog (where the wallet name is entered)
QString wallet_name = QInputDialog::getText(this, tr("Restore Name"), tr("Wallet Name:"), QLineEdit::Normal, "", &wallet_name_ok);
Comment on lines +433 to +434
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A translator comment followed by a line with more than one tr() could not work as expected.

if (!wallet_name_ok || wallet_name.isEmpty()) return;

auto activity = new RestoreWalletActivity(m_wallet_controller, this);
connect(activity, &RestoreWalletActivity::restored, this, &BitcoinGUI::setCurrentWallet, Qt::QueuedConnection);

auto backup_file_path = fs::PathFromString(backup_file.toStdString());
activity->restore(backup_file_path, wallet_name.toStdString());
});
connect(m_close_wallet_action, &QAction::triggered, [this] {
m_wallet_controller->closeWallet(walletFrame->currentWalletModel(), this);
});
Expand Down Expand Up @@ -450,8 +478,10 @@ void BitcoinGUI::createMenuBar()
file->addAction(m_close_wallet_action);
file->addAction(m_close_all_wallets_action);
file->addSeparator();
file->addAction(openAction);
file->addAction(backupWalletAction);
file->addAction(m_restore_wallet_action);
file->addSeparator();
file->addAction(openAction);
file->addAction(signMessageAction);
file->addAction(verifyMessageAction);
file->addAction(m_load_psbt_action);
Expand Down Expand Up @@ -642,6 +672,7 @@ void BitcoinGUI::setWalletController(WalletController* wallet_controller)
m_create_wallet_action->setEnabled(true);
m_open_wallet_action->setEnabled(true);
m_open_wallet_action->setMenu(m_open_wallet_menu);
m_restore_wallet_action->setEnabled(true);

GUIUtil::ExceptionSafeConnect(wallet_controller, &WalletController::walletAdded, this, &BitcoinGUI::addWallet);
connect(wallet_controller, &WalletController::walletRemoved, this, &BitcoinGUI::removeWallet);
Expand Down
1 change: 1 addition & 0 deletions src/qt/bitcoingui.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ class BitcoinGUI : public QMainWindow
QAction* m_create_wallet_action{nullptr};
QAction* m_open_wallet_action{nullptr};
QMenu* m_open_wallet_menu{nullptr};
QAction* m_restore_wallet_action{nullptr};
QAction* m_close_wallet_action{nullptr};
QAction* m_close_all_wallets_action{nullptr};
QAction* m_wallet_selector_label_action = nullptr;
Expand Down
43 changes: 43 additions & 0 deletions src/qt/walletcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,46 @@ void LoadWalletsActivity::load()
QTimer::singleShot(0, this, [this] { Q_EMIT finished(); });
});
}

RestoreWalletActivity::RestoreWalletActivity(WalletController* wallet_controller, QWidget* parent_widget)
: WalletControllerActivity(wallet_controller, parent_widget)
{
}

void RestoreWalletActivity::restore(const fs::path& backup_file, const std::string& wallet_name)
{
QString name = QString::fromStdString(wallet_name);

showProgressDialog(
//: Title of progress window which is displayed when wallets are being restored.
tr("Restore Wallet"),
/*: Descriptive text of the restore wallets progress window which indicates to
the user that wallets are currently being restored.*/
tr("Restoring Wallet <b>%1</b>…").arg(name.toHtmlEscaped()));

QTimer::singleShot(0, worker(), [this, backup_file, wallet_name] {
std::unique_ptr<interfaces::Wallet> wallet = node().walletLoader().restoreWallet(backup_file, wallet_name, m_error_message, m_warning_message);

if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet));

QTimer::singleShot(0, this, &RestoreWalletActivity::finish);
});
}

void RestoreWalletActivity::finish()
{
if (!m_error_message.empty()) {
//: Title of message box which is displayed when the wallet could not be restored.
QMessageBox::critical(m_parent_widget, tr("Restore wallet failed"), QString::fromStdString(m_error_message.translated));
} else if (!m_warning_message.empty()) {
//: Title of message box which is displayed when the wallet is restored with some warning.
QMessageBox::warning(m_parent_widget, tr("Restore wallet warning"), QString::fromStdString(Join(m_warning_message, Untranslated("\n")).translated));
} else {
//: Title of message box which is displayed when the wallet is successfully restored.
QMessageBox::information(m_parent_widget, tr("Restore wallet message"), QString::fromStdString(Untranslated("Wallet restored successfully \n").translated));
}

if (m_wallet_model) Q_EMIT restored(m_wallet_model);

Q_EMIT finished();
}
20 changes: 20 additions & 0 deletions src/qt/walletcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class Node;
class Wallet;
} // namespace interfaces

namespace fs {
class path;
}

class AskPassphraseDialog;
class CreateWalletActivity;
class CreateWalletDialog;
Expand Down Expand Up @@ -155,4 +159,20 @@ class LoadWalletsActivity : public WalletControllerActivity
void load();
};

class RestoreWalletActivity : public WalletControllerActivity
{
Q_OBJECT

public:
RestoreWalletActivity(WalletController* wallet_controller, QWidget* parent_widget);

void restore(const fs::path& backup_file, const std::string& wallet_name);

Q_SIGNALS:
void restored(WalletModel* wallet_model);

private:
void finish();
};

#endif // BITCOIN_QT_WALLETCONTROLLER_H