From: CryptoManiac Date: Mon, 16 Jun 2014 01:10:47 +0000 (+0400) Subject: Implement DumpWallet and ImportWallet calls from menu. X-Git-Tag: v0.4.4.6-nvc-update4~1^2~18 X-Git-Url: https://git.novaco.in/?p=novacoin.git;a=commitdiff_plain;h=5411280b44a6c211ec7ce15919068a92ff2d9f51 Implement DumpWallet and ImportWallet calls from menu. --- diff --git a/src/qt/bitcoin.qrc b/src/qt/bitcoin.qrc index f5ea25c..a1c6958 100644 --- a/src/qt/bitcoin.qrc +++ b/src/qt/bitcoin.qrc @@ -40,6 +40,8 @@ res/icons/filesave.png res/icons/qrcode.png res/icons/debugwindow.png + res/icons/dump.png + res/icons/import.png res/images/about.png diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index 8aeab44..922a6ae 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -24,6 +24,7 @@ #include "askpassphrasedialog.h" #include "notificator.h" #include "guiutil.h" +#include "ui_interface.h" #include "rpcconsole.h" #ifdef Q_OS_MAC @@ -252,6 +253,10 @@ void BitcoinGUI::createActions() encryptWalletAction->setCheckable(true); backupWalletAction = new QAction(QIcon(":/icons/filesave"), tr("&Backup Wallet..."), this); backupWalletAction->setToolTip(tr("Backup wallet to another location")); + dumpWalletAction = new QAction(QIcon(":/icons/dump"), tr("&Dump Wallet..."), this); + dumpWalletAction->setStatusTip(tr("Export wallet's keys to a text file")); + importWalletAction = new QAction(QIcon(":/icons/import"), tr("&Import Wallet..."), this); + importWalletAction->setStatusTip(tr("Import a file's keys into a wallet")); changePassphraseAction = new QAction(QIcon(":/icons/key"), tr("&Change Passphrase..."), this); changePassphraseAction->setToolTip(tr("Change the passphrase used for wallet encryption")); signMessageAction = new QAction(QIcon(":/icons/edit"), tr("Sign &message..."), this); @@ -269,6 +274,8 @@ void BitcoinGUI::createActions() connect(toggleHideAction, SIGNAL(triggered()), this, SLOT(toggleHidden())); connect(encryptWalletAction, SIGNAL(triggered(bool)), this, SLOT(encryptWallet(bool))); connect(backupWalletAction, SIGNAL(triggered()), this, SLOT(backupWallet())); + connect(dumpWalletAction, SIGNAL(triggered()), this, SLOT(dumpWallet())); + connect(importWalletAction, SIGNAL(triggered()), this, SLOT(importWallet())); connect(changePassphraseAction, SIGNAL(triggered()), this, SLOT(changePassphrase())); connect(signMessageAction, SIGNAL(triggered()), this, SLOT(gotoSignMessageTab())); connect(verifyMessageAction, SIGNAL(triggered()), this, SLOT(gotoVerifyMessageTab())); @@ -287,6 +294,9 @@ void BitcoinGUI::createMenuBar() // Configure the menus QMenu *file = appMenuBar->addMenu(tr("&File")); file->addAction(backupWalletAction); + file->addSeparator(); + file->addAction(dumpWalletAction); + file->addAction(importWalletAction); file->addAction(exportAction); file->addAction(signMessageAction); file->addAction(verifyMessageAction); @@ -586,6 +596,56 @@ void BitcoinGUI::error(const QString &title, const QString &message, bool modal) } } +void BitcoinGUI::message(const QString &title, const QString &message, unsigned int style, const QString &detail) +{ + QString strTitle = tr("NovaCoin") + " - "; + // Default to information icon + int nMBoxIcon = QMessageBox::Information; + int nNotifyIcon = Notificator::Information; + + + // Check for usage of predefined title + switch (style) { + case CClientUIInterface::MSG_ERROR: + strTitle += tr("Error"); + break; + case CClientUIInterface::MSG_WARNING: + strTitle += tr("Warning"); + break; + case CClientUIInterface::MSG_INFORMATION: + strTitle += tr("Information"); + break; + default: + strTitle += title; // Use supplied title + } + + // Check for error/warning icon + if (style & CClientUIInterface::ICON_ERROR) { + nMBoxIcon = QMessageBox::Critical; + nNotifyIcon = Notificator::Critical; + } + else if (style & CClientUIInterface::ICON_WARNING) { + nMBoxIcon = QMessageBox::Warning; + nNotifyIcon = Notificator::Warning; + } + + // Display message + if (style & CClientUIInterface::MODAL) { + // Check for buttons, use OK as default, if none was supplied + QMessageBox::StandardButton buttons; + buttons = QMessageBox::Ok; + + QMessageBox mBox((QMessageBox::Icon)nMBoxIcon, strTitle, message, buttons); + + if(!detail.isEmpty()) { mBox.setDetailedText(detail); } + + mBox.exec(); + } + else + notificator->notify((Notificator::Class)nNotifyIcon, strTitle, message); +} + + void BitcoinGUI::changeEvent(QEvent *e) { QMainWindow::changeEvent(e); @@ -827,6 +887,74 @@ void BitcoinGUI::backupWallet() } } +void BitcoinGUI::dumpWallet() +{ + if(!walletModel) + return; + + WalletModel::UnlockContext ctx(walletModel->requestUnlock()); + if(!ctx.isValid()) + { + // Unlock wallet failed or was cancelled + return; + } + +#if QT_VERSION < 0x050000 + QString saveDir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation); +#else + QString saveDir = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation); +#endif + QString filename = QFileDialog::getSaveFileName(this, tr("Export Wallet"), saveDir, tr("Wallet Text (*.txt)")); + if(!filename.isEmpty()) { + if(!walletModel->dumpWallet(filename)) { + error(tr("DumpWallet Failed"), + tr("An error happened while trying to save the keys to your location.\n" + "Keys were not saved") + ,CClientUIInterface::MSG_ERROR); + } + else + message(tr("DumpWallet Successful"), + tr("Keys were saved to this file:\n%2") + .arg(filename) + ,CClientUIInterface::MSG_INFORMATION); + } +} + +void BitcoinGUI::importWallet() +{ + if(!walletModel) + return; + + WalletModel::UnlockContext ctx(walletModel->requestUnlock()); + if(!ctx.isValid()) + { + // Unlock wallet failed or was cancelled + return; + } + +#if QT_VERSION < 0x050000 + QString openDir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation); +#else + QString openDir = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation); +#endif + QString filename = QFileDialog::getOpenFileName(this, tr("Import Wallet"), openDir, tr("Wallet Text (*.txt)")); + if(!filename.isEmpty()) { + if(!walletModel->importWallet(filename)) { + error(tr("Import Failed"), + tr("An error happened while trying to import the keys.\n" + "Some or all keys from:\n %1,\n were not imported into your wallet.") + .arg(filename) + ,CClientUIInterface::MSG_ERROR); + } + else + message(tr("Import Successful"), + tr("All keys from:\n %1,\n were imported into your wallet.") + .arg(filename) + ,CClientUIInterface::MSG_INFORMATION); + } +} + + void BitcoinGUI::changePassphrase() { AskPassphraseDialog dlg(AskPassphraseDialog::ChangePass, this); diff --git a/src/qt/bitcoingui.h b/src/qt/bitcoingui.h index c67e887..b4dd750 100644 --- a/src/qt/bitcoingui.h +++ b/src/qt/bitcoingui.h @@ -87,6 +87,8 @@ private: QAction *exportAction; QAction *encryptWalletAction; QAction *backupWalletAction; + QAction *dumpWalletAction; + QAction *importWalletAction; QAction *changePassphraseAction; QAction *aboutQtAction; QAction *openRPCConsoleAction; @@ -120,6 +122,8 @@ public slots: /** Notify the user of an error in the network or transaction handling code. */ void error(const QString &title, const QString &message, bool modal); + void message(const QString &title, const QString &message, unsigned int style, const QString &detail=QString()); + /** Asks the user whether to pay the transaction fee or to cancel the transaction. It is currently not possible to pass a return value to another thread through BlockingQueuedConnection, so an indirected pointer is used. @@ -166,6 +170,10 @@ private slots: /** Backup the wallet */ void backupWallet(); /** Change encrypted wallet passphrase */ + + void dumpWallet(); + void importWallet(); + void changePassphrase(); /** Ask for passphrase to unlock wallet temporarily */ void unlockWallet(); diff --git a/src/qt/res/icons/dump.png b/src/qt/res/icons/dump.png new file mode 100644 index 0000000..cb41511 Binary files /dev/null and b/src/qt/res/icons/dump.png differ diff --git a/src/qt/res/icons/import.png b/src/qt/res/icons/import.png new file mode 100644 index 0000000..aee3c01 Binary files /dev/null and b/src/qt/res/icons/import.png differ diff --git a/src/qt/walletmodel.cpp b/src/qt/walletmodel.cpp index 115df69..d82e790 100644 --- a/src/qt/walletmodel.cpp +++ b/src/qt/walletmodel.cpp @@ -316,6 +316,16 @@ void WalletModel::getStakeWeightFromValue(const int64& nTime, const int64& nValu wallet->GetStakeWeightFromValue(nTime, nValue, nWeight); } +bool WalletModel::dumpWallet(const QString &filename) +{ + return DumpWallet(wallet, filename.toLocal8Bit().data()); +} + +bool WalletModel::importWallet(const QString &filename) +{ + return ImportWallet(wallet, filename.toLocal8Bit().data()); +} + bool WalletModel::backupWallet(const QString &filename) { return BackupWallet(*wallet, filename.toLocal8Bit().data()); diff --git a/src/qt/walletmodel.h b/src/qt/walletmodel.h index 5f10e50..4c8597c 100644 --- a/src/qt/walletmodel.h +++ b/src/qt/walletmodel.h @@ -96,6 +96,9 @@ public: // Wallet backup bool backupWallet(const QString &filename); + bool dumpWallet(const QString &filename); + bool importWallet(const QString &filename); + void getStakeWeight(quint64& nMinWeight, quint64& nMaxWeight, quint64& nWeight); void getStakeWeightFromValue(const qint64& nTime, const qint64& nValue, quint64& nWeight); diff --git a/src/ui_interface.h b/src/ui_interface.h index 0f7fdef..1029f7e 100644 --- a/src/ui_interface.h +++ b/src/ui_interface.h @@ -56,7 +56,13 @@ public: MORE = 0x00010000, SETUP = 0x00020000, // Force blocking, modal message box dialog (not just OS notification) - MODAL = 0x00040000 + MODAL = 0x00040000, + + /** Predefined combinations for certain default usage cases */ + MSG_INFORMATION = ICON_INFORMATION, + MSG_WARNING = (ICON_WARNING | OK | MODAL), + MSG_ERROR = (ICON_ERROR | OK | MODAL) + }; /** Show message box. */