qt: askpassphrasedialog: Clear pass fields on accept
[novacoin.git] / src / qt / askpassphrasedialog.cpp
1 #include "askpassphrasedialog.h"
2 #include "ui_askpassphrasedialog.h"
3
4 #include "guiconstants.h"
5 #include "dialogwindowflags.h"
6 #include "walletmodel.h"
7
8 #include <QMessageBox>
9 #include <QPushButton>
10 #include <QKeyEvent>
11
12 extern bool fWalletUnlockMintOnly;
13
14 AskPassphraseDialog::AskPassphraseDialog(Mode mode, QWidget *parent) :
15     QDialog(parent, DIALOGWINDOWHINTS),
16     ui(new Ui::AskPassphraseDialog),
17     mode(mode),
18     model(0),
19     fCapsLock(false)
20 {
21     ui->setupUi(this);
22     ui->passEdit1->setMaxLength(MAX_PASSPHRASE_SIZE);
23     ui->passEdit2->setMaxLength(MAX_PASSPHRASE_SIZE);
24     ui->passEdit3->setMaxLength(MAX_PASSPHRASE_SIZE);
25
26     // Setup Caps Lock detection.
27     ui->passEdit1->installEventFilter(this);
28     ui->passEdit2->installEventFilter(this);
29     ui->passEdit3->installEventFilter(this);
30
31     switch(mode)
32     {
33         case Encrypt: // Ask passphrase x2
34             ui->passLabel1->hide();
35             ui->passEdit1->hide();
36             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>."));
37             setWindowTitle(tr("Encrypt wallet"));
38             break;
39         case Unlock: // Ask passphrase
40         case UnlockMining:
41             ui->warningLabel->setText(tr("This operation needs your wallet passphrase to unlock the wallet."));
42             ui->passLabel2->hide();
43             ui->passEdit2->hide();
44             ui->passLabel3->hide();
45             ui->passEdit3->hide();
46             setWindowTitle(tr("Unlock wallet"));
47             break;
48         case Decrypt:   // Ask passphrase
49             ui->warningLabel->setText(tr("This operation needs your wallet passphrase to decrypt the wallet."));
50             ui->passLabel2->hide();
51             ui->passEdit2->hide();
52             ui->passLabel3->hide();
53             ui->passEdit3->hide();
54             setWindowTitle(tr("Decrypt wallet"));
55             break;
56         case ChangePass: // Ask old passphrase + new passphrase x2
57             setWindowTitle(tr("Change passphrase"));
58             ui->warningLabel->setText(tr("Enter the old and new passphrase to the wallet."));
59             break;
60     }
61
62     textChanged();
63     connect(ui->passEdit1, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
64     connect(ui->passEdit2, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
65     connect(ui->passEdit3, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
66 }
67
68 AskPassphraseDialog::~AskPassphraseDialog()
69 {
70     secureClearPassFields();
71     delete ui;
72 }
73
74 void AskPassphraseDialog::setModel(WalletModel *model)
75 {
76     this->model = model;
77 }
78
79 void AskPassphraseDialog::accept()
80 {
81     SecureString oldpass, newpass1, newpass2;
82     if(!model)
83         return;
84     oldpass.reserve(MAX_PASSPHRASE_SIZE);
85     newpass1.reserve(MAX_PASSPHRASE_SIZE);
86     newpass2.reserve(MAX_PASSPHRASE_SIZE);
87     // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
88     // Alternately, find a way to make this input mlock()'d to begin with.
89     oldpass.assign(ui->passEdit1->text().toStdString().c_str());
90     newpass1.assign(ui->passEdit2->text().toStdString().c_str());
91     newpass2.assign(ui->passEdit3->text().toStdString().c_str());
92         
93         secureClearPassFields();
94
95     switch(mode)
96     {
97     case Encrypt: {
98         if(newpass1.empty() || newpass2.empty())
99         {
100             // Cannot encrypt with empty passphrase
101             break;
102         }
103         QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm wallet encryption"),
104                  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?"),
105                  QMessageBox::Yes|QMessageBox::Cancel,
106                  QMessageBox::Cancel);
107         if(retval == QMessageBox::Yes)
108         {
109             if(newpass1 == newpass2)
110             {
111                 if(model->setWalletEncrypted(true, newpass1))
112                 {
113                     QMessageBox::warning(this, tr("Wallet encrypted"),
114                                          "<qt>" + 
115                                          tr("NovaCoin will close now to finish the encryption process. "
116                                          "Remember that encrypting your wallet cannot fully protect "
117                                          "your coins from being stolen by malware infecting your computer.") + 
118                                          "<br><br><b>" + 
119                                          tr("IMPORTANT: Any previous backups you have made of your wallet file "
120                                          "should be replaced with the newly generated, encrypted wallet file. "
121                                          "For security reasons, previous backups of the unencrypted wallet file "
122                                          "will become useless as soon as you start using the new, encrypted wallet.") + 
123                                          "</b></qt>");
124                     QApplication::quit();
125                 }
126                 else
127                 {
128                     QMessageBox::critical(this, tr("Wallet encryption failed"),
129                                          tr("Wallet encryption failed due to an internal error. Your wallet was not encrypted."));
130                 }
131                 QDialog::accept(); // Success
132             }
133             else
134             {
135                 QMessageBox::critical(this, tr("Wallet encryption failed"),
136                                      tr("The supplied passphrases do not match."));
137             }
138         }
139         else
140         {
141             QDialog::reject(); // Cancelled
142         }
143         } break;
144     case Unlock:
145         if(!model->setWalletLocked(false, oldpass))
146         {
147             QMessageBox::critical(this, tr("Wallet unlock failed"),
148                                   tr("The passphrase entered for the wallet decryption was incorrect."));
149         }
150         else
151         {
152             QDialog::accept(); // Success
153         }
154         break;
155     case UnlockMining:
156         if(!model->setWalletLocked(false, oldpass))
157         {
158             QMessageBox::critical(this, tr("Wallet unlock failed"),
159                                   tr("The passphrase entered for the wallet decryption was incorrect."));
160         }
161         else
162         {
163             QDialog::accept(); // Success
164             fWalletUnlockMintOnly = true;
165         }
166         break;
167     case Decrypt:
168         if(!model->setWalletEncrypted(false, oldpass))
169         {
170             QMessageBox::critical(this, tr("Wallet decryption failed"),
171                                   tr("The passphrase entered for the wallet decryption was incorrect."));
172         }
173         else
174         {
175             QMessageBox::warning(this, tr("Wallet decrypted"),
176                                      "<qt>" + 
177                                      tr("NovaCoin will close now to finish the decryption process. ") +
178                                      "</b></qt>");
179             QApplication::quit();
180         }
181         break;
182     case ChangePass:
183         if(newpass1 == newpass2)
184         {
185             if(model->changePassphrase(oldpass, newpass1))
186             {
187                 QMessageBox::information(this, tr("Wallet encrypted"),
188                                      tr("Wallet passphrase was successfully changed."));
189                 QDialog::accept(); // Success
190             }
191             else
192             {
193                 QMessageBox::critical(this, tr("Wallet encryption failed"),
194                                      tr("The passphrase entered for the wallet decryption was incorrect."));
195             }
196         }
197         else
198         {
199             QMessageBox::critical(this, tr("Wallet encryption failed"),
200                                  tr("The supplied passphrases do not match."));
201         }
202         break;
203     }
204 }
205
206 void AskPassphraseDialog::textChanged()
207 {
208     // Validate input, set Ok button to enabled when acceptable
209     bool acceptable = false;
210     switch(mode)
211     {
212     case Encrypt: // New passphrase x2
213         acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
214         break;
215     case Unlock: // Old passphrase x1
216     case UnlockMining:
217     case Decrypt:
218         acceptable = !ui->passEdit1->text().isEmpty();
219         break;
220     case ChangePass: // Old passphrase x1, new passphrase x2
221         acceptable = !ui->passEdit1->text().isEmpty() && !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
222         break;
223     }
224     ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable);
225 }
226
227 bool AskPassphraseDialog::event(QEvent *event)
228 {
229     // Detect Caps Lock key press.
230     if (event->type() == QEvent::KeyPress) {
231         QKeyEvent *ke = static_cast<QKeyEvent *>(event);
232         if (ke->key() == Qt::Key_CapsLock) {
233             fCapsLock = !fCapsLock;
234         }
235         if (fCapsLock) {
236             ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
237         } else {
238             ui->capsLabel->clear();
239         }
240     }
241     return QWidget::event(event);
242 }
243
244 bool AskPassphraseDialog::eventFilter(QObject *object, QEvent *event)
245 {
246     /* Detect Caps Lock.
247      * There is no good OS-independent way to check a key state in Qt, but we
248      * can detect Caps Lock by checking for the following condition:
249      * Shift key is down and the result is a lower case character, or
250      * Shift key is not down and the result is an upper case character.
251      */
252     if (event->type() == QEvent::KeyPress) {
253         QKeyEvent *ke = static_cast<QKeyEvent *>(event);
254         QString str = ke->text();
255         if (str.length() != 0) {
256             const QChar *psz = str.unicode();
257             bool fShift = (ke->modifiers() & Qt::ShiftModifier) != 0;
258             if ((fShift && psz->isLower()) || (!fShift && psz->isUpper())) {
259                 fCapsLock = true;
260                 ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
261             } else if (psz->isLetter()) {
262                 fCapsLock = false;
263                 ui->capsLabel->clear();
264             }
265         }
266     }
267     return QDialog::eventFilter(object, event);
268 }
269
270 static void SecureClearQLineEdit(QLineEdit* edit)
271 {
272     // Attempt to overwrite text so that they do not linger around in memory
273     edit->setText(QString(" ").repeated(edit->text().size()));
274     edit->clear();
275 }
276
277 void AskPassphraseDialog::secureClearPassFields()
278 {
279     SecureClearQLineEdit(ui->passEdit1);
280     SecureClearQLineEdit(ui->passEdit2);
281     SecureClearQLineEdit(ui->passEdit3);
282 }