Enable wordwrap for long message in passphrase dialog
[novacoin.git] / src / qt / askpassphrasedialog.cpp
1 #include "askpassphrasedialog.h"
2 #include "ui_askpassphrasedialog.h"
3
4 #include "guiconstants.h"
5 #include "walletmodel.h"
6
7 #include <QMessageBox>
8 #include <QPushButton>
9
10 AskPassphraseDialog::AskPassphraseDialog(Mode mode, QWidget *parent) :
11     QDialog(parent),
12     ui(new Ui::AskPassphraseDialog),
13     mode(mode),
14     model(0)
15 {
16     ui->setupUi(this);
17     ui->passEdit1->setMaxLength(MAX_PASSPHRASE_SIZE);
18     ui->passEdit2->setMaxLength(MAX_PASSPHRASE_SIZE);
19     ui->passEdit3->setMaxLength(MAX_PASSPHRASE_SIZE);
20
21     switch(mode)
22     {
23         case Encrypt: // Ask passphrase x2
24             ui->passLabel1->hide();
25             ui->passEdit1->hide();
26             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>."));
27             setWindowTitle(tr("Encrypt wallet"));
28             break;
29         case Unlock: // Ask passphrase
30             ui->warningLabel->setText(tr("This operation needs your wallet passphrase to unlock the wallet."));
31             ui->passLabel2->hide();
32             ui->passEdit2->hide();
33             ui->passLabel3->hide();
34             ui->passEdit3->hide();
35             setWindowTitle(tr("Unlock wallet"));
36             break;
37         case Decrypt:   // Ask passphrase
38             ui->warningLabel->setText(tr("This operation needs your wallet passphrase to decrypt the wallet."));
39             ui->passLabel2->hide();
40             ui->passEdit2->hide();
41             ui->passLabel3->hide();
42             ui->passEdit3->hide();
43             setWindowTitle(tr("Decrypt wallet"));
44             break;
45         case ChangePass: // Ask old passphrase + new passphrase x2
46             setWindowTitle(tr("Change passphrase"));
47             ui->warningLabel->setText(tr("Enter the old and new passphrase to the wallet."));
48             break;
49     }
50
51     textChanged();
52     connect(ui->passEdit1, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
53     connect(ui->passEdit2, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
54     connect(ui->passEdit3, SIGNAL(textChanged(QString)), this, SLOT(textChanged()));
55 }
56
57 AskPassphraseDialog::~AskPassphraseDialog()
58 {
59     // Attempt to overwrite text so that they do not linger around in memory
60     ui->passEdit1->setText(QString(" ").repeated(ui->passEdit1->text().size()));
61     ui->passEdit2->setText(QString(" ").repeated(ui->passEdit2->text().size()));
62     ui->passEdit3->setText(QString(" ").repeated(ui->passEdit3->text().size()));
63     delete ui;
64 }
65
66 void AskPassphraseDialog::setModel(WalletModel *model)
67 {
68     this->model = model;
69 }
70
71 void AskPassphraseDialog::accept()
72 {
73     std::string oldpass, newpass1, newpass2;
74     if(!model)
75         return;
76     // TODO: mlock memory / munlock on return so they will not be swapped out, really need "mlockedstring" wrapper class to do this safely
77     oldpass.reserve(MAX_PASSPHRASE_SIZE);
78     newpass1.reserve(MAX_PASSPHRASE_SIZE);
79     newpass2.reserve(MAX_PASSPHRASE_SIZE);
80     oldpass.assign(ui->passEdit1->text().toStdString());
81     newpass1.assign(ui->passEdit2->text().toStdString());
82     newpass2.assign(ui->passEdit3->text().toStdString());
83
84     switch(mode)
85     {
86     case Encrypt: {
87         if(newpass1.empty() || newpass2.empty())
88         {
89             // Cannot encrypt with empty passphrase
90             break;
91         }
92         QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm wallet encryption"),
93                  tr("WARNING: If you encrypt your wallet and lose your passphrase, you will <b>LOSE ALL OF YOUR BITCOINS</b>!\nAre you sure you wish to encrypt your wallet?"),
94                  QMessageBox::Yes|QMessageBox::Cancel,
95                  QMessageBox::Cancel);
96         if(retval == QMessageBox::Yes)
97         {
98             if(newpass1 == newpass2)
99             {
100                 if(model->setWalletEncrypted(true, newpass1))
101                 {
102                     QMessageBox::warning(this, tr("Wallet encrypted"),
103                                          tr("Bitcoin will close now to finish the encryption process. Remember that encrypting your wallet cannot fully protect your bitcoins from being stolen by malware infecting your computer."));
104                     QApplication::quit();
105                 }
106                 else
107                 {
108                     QMessageBox::critical(this, tr("Wallet encryption failed"),
109                                          tr("Wallet encryption failed due to an internal error. Your wallet was not encrypted."));
110                 }
111                 QDialog::accept(); // Success
112             }
113             else
114             {
115                 QMessageBox::critical(this, tr("Wallet encryption failed"),
116                                      tr("The supplied passphrases do not match."));
117             }
118         }
119         else
120         {
121             QDialog::reject(); // Cancelled
122         }
123         } break;
124     case Unlock:
125         if(!model->setWalletLocked(false, oldpass))
126         {
127             QMessageBox::critical(this, tr("Wallet unlock failed"),
128                                   tr("The passphrase entered for the wallet decryption was incorrect."));
129         }
130         else
131         {
132             QDialog::accept(); // Success
133         }
134         break;
135     case Decrypt:
136         if(!model->setWalletEncrypted(false, oldpass))
137         {
138             QMessageBox::critical(this, tr("Wallet decryption failed"),
139                                   tr("The passphrase entered for the wallet decryption was incorrect."));
140         }
141         else
142         {
143             QDialog::accept(); // Success
144         }
145         break;
146     case ChangePass:
147         if(newpass1 == newpass2)
148         {
149             if(model->changePassphrase(oldpass, newpass1))
150             {
151                 QMessageBox::information(this, tr("Wallet encrypted"),
152                                      tr("Wallet passphrase was succesfully changed."));
153                 QDialog::accept(); // Success
154             }
155             else
156             {
157                 QMessageBox::critical(this, tr("Wallet encryption failed"),
158                                      tr("The passphrase entered for the wallet decryption was incorrect."));
159             }
160         }
161         else
162         {
163             QMessageBox::critical(this, tr("Wallet encryption failed"),
164                                  tr("The supplied passphrases do not match."));
165         }
166         break;
167     }
168 }
169
170 void AskPassphraseDialog::textChanged()
171 {
172     // Validate input, set Ok button to enabled when accepable
173     bool acceptable = false;
174     switch(mode)
175     {
176     case Encrypt: // New passphrase x2
177         acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
178         break;
179     case Unlock: // Old passphrase x1
180     case Decrypt:
181         acceptable = !ui->passEdit1->text().isEmpty();
182         break;
183     case ChangePass: // Old passphrase x1, new passphrase x2
184         acceptable = !ui->passEdit1->text().isEmpty() && !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
185         break;
186     }
187     ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable);
188 }