1 #include "askpassphrasedialog.h"
2 #include "ui_askpassphrasedialog.h"
4 #include "guiconstants.h"
5 #include "walletmodel.h"
11 AskPassphraseDialog::AskPassphraseDialog(Mode mode, QWidget *parent) :
13 ui(new Ui::AskPassphraseDialog),
19 ui->passEdit1->setMaxLength(MAX_PASSPHRASE_SIZE);
20 ui->passEdit2->setMaxLength(MAX_PASSPHRASE_SIZE);
21 ui->passEdit3->setMaxLength(MAX_PASSPHRASE_SIZE);
23 // Setup Caps Lock detection.
24 ui->passEdit1->installEventFilter(this);
25 ui->passEdit2->installEventFilter(this);
26 ui->passEdit3->installEventFilter(this);
30 case Encrypt: // Ask passphrase x2
31 ui->passLabel1->hide();
32 ui->passEdit1->hide();
33 ui->warningLabel->setText(tr("Enter the new passphrase to the wallet.<br/>Please use a passphrase of <b>10 or more random characters</b>, or <b>eight or more words</b>."));
34 setWindowTitle(tr("Encrypt wallet"));
36 case Unlock: // Ask passphrase
37 ui->warningLabel->setText(tr("This operation needs your wallet passphrase to unlock the wallet."));
38 ui->passLabel2->hide();
39 ui->passEdit2->hide();
40 ui->passLabel3->hide();
41 ui->passEdit3->hide();
42 setWindowTitle(tr("Unlock wallet"));
44 case Decrypt: // Ask passphrase
45 ui->warningLabel->setText(tr("This operation needs your wallet passphrase to decrypt the wallet."));
46 ui->passLabel2->hide();
47 ui->passEdit2->hide();
48 ui->passLabel3->hide();
49 ui->passEdit3->hide();
50 setWindowTitle(tr("Decrypt wallet"));
52 case ChangePass: // Ask old passphrase + new passphrase x2
53 setWindowTitle(tr("Change passphrase"));
54 ui->warningLabel->setText(tr("Enter the old and new passphrase to the wallet."));
59 connect(ui->passEdit1, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
60 connect(ui->passEdit2, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
61 connect(ui->passEdit3, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
64 AskPassphraseDialog::~AskPassphraseDialog()
66 // Attempt to overwrite text so that they do not linger around in memory
67 ui->passEdit1->setText(QString(" ").repeated(ui->passEdit1->text().size()));
68 ui->passEdit2->setText(QString(" ").repeated(ui->passEdit2->text().size()));
69 ui->passEdit3->setText(QString(" ").repeated(ui->passEdit3->text().size()));
73 void AskPassphraseDialog::setModel(WalletModel *model)
78 void AskPassphraseDialog::accept()
80 SecureString oldpass, newpass1, newpass2;
83 oldpass.reserve(MAX_PASSPHRASE_SIZE);
84 newpass1.reserve(MAX_PASSPHRASE_SIZE);
85 newpass2.reserve(MAX_PASSPHRASE_SIZE);
86 // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
87 // Alternately, find a way to make this input mlock()'d to begin with.
88 oldpass.assign(ui->passEdit1->text().toStdString().c_str());
89 newpass1.assign(ui->passEdit2->text().toStdString().c_str());
90 newpass2.assign(ui->passEdit3->text().toStdString().c_str());
95 if(newpass1.empty() || newpass2.empty())
97 // Cannot encrypt with empty passphrase
100 QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm wallet encryption"),
101 tr("Warning: If you encrypt your wallet and lose your passphrase, you will <b>LOSE ALL OF YOUR COINS</b>!") + "<br><br>" + tr("Are you sure you wish to encrypt your wallet?"),
102 QMessageBox::Yes|QMessageBox::Cancel,
103 QMessageBox::Cancel);
104 if(retval == QMessageBox::Yes)
106 if(newpass1 == newpass2)
108 if(model->setWalletEncrypted(true, newpass1))
110 QMessageBox::warning(this, tr("Wallet encrypted"),
112 tr("NovaCoin will close now to finish the encryption process. "
113 "Remember that encrypting your wallet cannot fully protect "
114 "your coins from being stolen by malware infecting your computer.") +
116 tr("IMPORTANT: Any previous backups you have made of your wallet file "
117 "should be replaced with the newly generated, encrypted wallet file. "
118 "For security reasons, previous backups of the unencrypted wallet file "
119 "will become useless as soon as you start using the new, encrypted wallet.") +
121 QApplication::quit();
125 QMessageBox::critical(this, tr("Wallet encryption failed"),
126 tr("Wallet encryption failed due to an internal error. Your wallet was not encrypted."));
128 QDialog::accept(); // Success
132 QMessageBox::critical(this, tr("Wallet encryption failed"),
133 tr("The supplied passphrases do not match."));
138 QDialog::reject(); // Cancelled
142 if(!model->setWalletLocked(false, oldpass))
144 QMessageBox::critical(this, tr("Wallet unlock failed"),
145 tr("The passphrase entered for the wallet decryption was incorrect."));
149 QDialog::accept(); // Success
153 if(!model->setWalletEncrypted(false, oldpass))
155 QMessageBox::critical(this, tr("Wallet decryption failed"),
156 tr("The passphrase entered for the wallet decryption was incorrect."));
160 QDialog::accept(); // Success
164 if(newpass1 == newpass2)
166 if(model->changePassphrase(oldpass, newpass1))
168 QMessageBox::information(this, tr("Wallet encrypted"),
169 tr("Wallet passphrase was successfully changed."));
170 QDialog::accept(); // Success
174 QMessageBox::critical(this, tr("Wallet encryption failed"),
175 tr("The passphrase entered for the wallet decryption was incorrect."));
180 QMessageBox::critical(this, tr("Wallet encryption failed"),
181 tr("The supplied passphrases do not match."));
187 void AskPassphraseDialog::textChanged()
189 // Validate input, set Ok button to enabled when acceptable
190 bool acceptable = false;
193 case Encrypt: // New passphrase x2
194 acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
196 case Unlock: // Old passphrase x1
198 acceptable = !ui->passEdit1->text().isEmpty();
200 case ChangePass: // Old passphrase x1, new passphrase x2
201 acceptable = !ui->passEdit1->text().isEmpty() && !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
204 ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable);
207 bool AskPassphraseDialog::event(QEvent *event)
209 // Detect Caps Lock key press.
210 if (event->type() == QEvent::KeyPress) {
211 QKeyEvent *ke = static_cast<QKeyEvent *>(event);
212 if (ke->key() == Qt::Key_CapsLock) {
213 fCapsLock = !fCapsLock;
216 ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
218 ui->capsLabel->clear();
221 return QWidget::event(event);
224 bool AskPassphraseDialog::eventFilter(QObject *object, QEvent *event)
227 * There is no good OS-independent way to check a key state in Qt, but we
228 * can detect Caps Lock by checking for the following condition:
229 * Shift key is down and the result is a lower case character, or
230 * Shift key is not down and the result is an upper case character.
232 if (event->type() == QEvent::KeyPress) {
233 QKeyEvent *ke = static_cast<QKeyEvent *>(event);
234 QString str = ke->text();
235 if (str.length() != 0) {
236 const QChar *psz = str.unicode();
237 bool fShift = (ke->modifiers() & Qt::ShiftModifier) != 0;
238 if ((fShift && psz->isLower()) || (!fShift && psz->isUpper())) {
240 ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
241 } else if (psz->isLetter()) {
243 ui->capsLabel->clear();
247 return QDialog::eventFilter(object, event);