Skip to content

Commit c99f58a

Browse files
w0xltshaavanfurszy
committed
gui: Add Wallet Restore in the GUI
Co-authored-by: Shashwat Vangani <[email protected]> Co-authored-by: furszy <[email protected]>
1 parent b11ab25 commit c99f58a

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

src/qt/bitcoingui.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include <QDateTime>
4949
#include <QDragEnterEvent>
5050
#include <QKeySequence>
51+
#include <QInputDialog>
5152
#include <QListWidget>
5253
#include <QMenu>
5354
#include <QMenuBar>
@@ -348,6 +349,12 @@ void BitcoinGUI::createActions()
348349
m_create_wallet_action->setEnabled(false);
349350
m_create_wallet_action->setStatusTip(tr("Create a new wallet"));
350351

352+
//: Name of the menu item that restores wallet from a backup file.
353+
m_restore_wallet_action = new QAction(tr("Restore Wallet…"), this);
354+
m_restore_wallet_action->setEnabled(false);
355+
//: Status tip for Restore Wallet menu item
356+
m_restore_wallet_action->setStatusTip(tr("Restore a wallet from a backup file"));
357+
351358
m_close_all_wallets_action = new QAction(tr("Close All Wallets…"), this);
352359
m_close_all_wallets_action->setStatusTip(tr("Close all wallets"));
353360

@@ -412,6 +419,25 @@ void BitcoinGUI::createActions()
412419
action->setEnabled(false);
413420
}
414421
});
422+
connect(m_restore_wallet_action, &QAction::triggered, [this] {
423+
QString backup_file = GUIUtil::getOpenFileName(this,
424+
//: The title for Restore Wallet File Windows
425+
tr("Load Wallet Backup"), QString(),
426+
//: The file extension for Restore Wallet File Windows
427+
tr("Wallet Data File (*.dat)"), nullptr);
428+
if (backup_file.isEmpty()) return;
429+
430+
bool walletNameOk;
431+
//: Title of the Restore Wallet input dialog (where the wallet name is entered)
432+
QString walletName = QInputDialog::getText(this, tr("Restore Name"), tr("Wallet Name:"), QLineEdit::Normal, "", &walletNameOk);
433+
if (!walletNameOk || walletName.isEmpty()) return;
434+
435+
auto activity = new RestoreWalletActivity(m_wallet_controller, this);
436+
connect(activity, &RestoreWalletActivity::restored, this, &BitcoinGUI::setCurrentWallet, Qt::QueuedConnection);
437+
438+
auto backup_file_path = fs::PathFromString(backup_file.toStdString());
439+
activity->restore(backup_file_path, walletName.toStdString());
440+
});
415441
connect(m_close_wallet_action, &QAction::triggered, [this] {
416442
m_wallet_controller->closeWallet(walletFrame->currentWalletModel(), this);
417443
});
@@ -447,6 +473,7 @@ void BitcoinGUI::createMenuBar()
447473
{
448474
file->addAction(m_create_wallet_action);
449475
file->addAction(m_open_wallet_action);
476+
file->addAction(m_restore_wallet_action);
450477
file->addAction(m_close_wallet_action);
451478
file->addAction(m_close_all_wallets_action);
452479
file->addSeparator();
@@ -642,6 +669,7 @@ void BitcoinGUI::setWalletController(WalletController* wallet_controller)
642669
m_create_wallet_action->setEnabled(true);
643670
m_open_wallet_action->setEnabled(true);
644671
m_open_wallet_action->setMenu(m_open_wallet_menu);
672+
m_restore_wallet_action->setEnabled(true);
645673

646674
GUIUtil::ExceptionSafeConnect(wallet_controller, &WalletController::walletAdded, this, &BitcoinGUI::addWallet);
647675
connect(wallet_controller, &WalletController::walletRemoved, this, &BitcoinGUI::removeWallet);

src/qt/bitcoingui.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ class BitcoinGUI : public QMainWindow
157157
QAction* m_create_wallet_action{nullptr};
158158
QAction* m_open_wallet_action{nullptr};
159159
QMenu* m_open_wallet_menu{nullptr};
160+
QAction* m_restore_wallet_action{nullptr};
160161
QAction* m_close_wallet_action{nullptr};
161162
QAction* m_close_all_wallets_action{nullptr};
162163
QAction* m_wallet_selector_label_action = nullptr;

src/qt/walletcontroller.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,46 @@ void LoadWalletsActivity::load()
373373
QTimer::singleShot(0, this, [this] { Q_EMIT finished(); });
374374
});
375375
}
376+
377+
RestoreWalletActivity::RestoreWalletActivity(WalletController* wallet_controller, QWidget* parent_widget)
378+
: WalletControllerActivity(wallet_controller, parent_widget)
379+
{
380+
}
381+
382+
void RestoreWalletActivity::restore(const fs::path& backup_file, const std::string& wallet_name)
383+
{
384+
QString name = QString::fromStdString(wallet_name);
385+
386+
showProgressDialog(
387+
//: Title of progress window which is displayed when wallets are being restored.
388+
tr("Restore Wallet"),
389+
/*: Descriptive text of the restore wallets progress window which indicates to
390+
the user that wallets are currently being restored.*/
391+
tr("Restoring Wallet <b>%1</b>…").arg(name.toHtmlEscaped()));
392+
393+
QTimer::singleShot(0, worker(), [this, backup_file, wallet_name] {
394+
std::unique_ptr<interfaces::Wallet> wallet = node().walletLoader().restoreWallet(backup_file, wallet_name, m_error_message, m_warning_message);
395+
396+
if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet));
397+
398+
QTimer::singleShot(0, this, &RestoreWalletActivity::finish);
399+
});
400+
}
401+
402+
void RestoreWalletActivity::finish()
403+
{
404+
if (!m_error_message.empty()) {
405+
//: Title of message box which is displayed when the wallet could not be restored.
406+
QMessageBox::critical(m_parent_widget, tr("Restore wallet failed"), QString::fromStdString(m_error_message.translated));
407+
} else if (!m_warning_message.empty()) {
408+
//: Title of message box which is displayed when the wallet is restored with some warning.
409+
QMessageBox::warning(m_parent_widget, tr("Restore wallet warning"), QString::fromStdString(Join(m_warning_message, Untranslated("\n")).translated));
410+
} else {
411+
//: Title of message box which is displayed when the wallet is successfully restored.
412+
QMessageBox::information(m_parent_widget, tr("Restore wallet message"), QString::fromStdString(Untranslated("Wallet restored successfully \n").translated));
413+
}
414+
415+
if (m_wallet_model) Q_EMIT restored(m_wallet_model);
416+
417+
Q_EMIT finished();
418+
}

src/qt/walletcontroller.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class Node;
3333
class Wallet;
3434
} // namespace interfaces
3535

36+
namespace fs { class path; }
37+
3638
class AskPassphraseDialog;
3739
class CreateWalletActivity;
3840
class CreateWalletDialog;
@@ -155,4 +157,20 @@ class LoadWalletsActivity : public WalletControllerActivity
155157
void load();
156158
};
157159

160+
class RestoreWalletActivity : public WalletControllerActivity
161+
{
162+
Q_OBJECT
163+
164+
public:
165+
RestoreWalletActivity(WalletController* wallet_controller, QWidget* parent_widget);
166+
167+
void restore(const fs::path& backup_file, const std::string& wallet_name);
168+
169+
Q_SIGNALS:
170+
void restored(WalletModel* wallet_model);
171+
172+
private:
173+
void finish();
174+
};
175+
158176
#endif // BITCOIN_QT_WALLETCONTROLLER_H

0 commit comments

Comments
 (0)