Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
askpassphrasedialog.cpp
Go to the documentation of this file.
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 #include <QKeyEvent>
10 
12  QDialog(parent),
13  ui(new Ui::AskPassphraseDialog),
14  mode(mode),
15  model(0),
16  fCapsLock(false)
17 {
18  ui->setupUi(this);
19  ui->passEdit1->setMaxLength(MAX_PASSPHRASE_SIZE);
20  ui->passEdit2->setMaxLength(MAX_PASSPHRASE_SIZE);
21  ui->passEdit3->setMaxLength(MAX_PASSPHRASE_SIZE);
22 
23  // Setup Caps Lock detection.
24  ui->passEdit1->installEventFilter(this);
25  ui->passEdit2->installEventFilter(this);
26  ui->passEdit3->installEventFilter(this);
27 
28  switch(mode)
29  {
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"));
35  break;
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"));
43  break;
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"));
51  break;
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."));
55  break;
56  }
57 
58  textChanged();
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()));
62 }
63 
65 {
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()));
70  delete ui;
71 }
72 
74 {
75  this->model = model;
76 }
77 
79 {
80  SecureString oldpass, newpass1, newpass2;
81  if(!model)
82  return;
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());
91 
92  switch(mode)
93  {
94  case Encrypt: {
95  if(newpass1.empty() || newpass2.empty())
96  {
97  // Cannot encrypt with empty passphrase
98  break;
99  }
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 FEATHERCOINS</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)
105  {
106  if(newpass1 == newpass2)
107  {
108  if(model->setWalletEncrypted(true, newpass1))
109  {
110  QMessageBox::warning(this, tr("Wallet encrypted"),
111  "<qt>" +
112  tr("Feathercoin will close now to finish the encryption process. "
113  "Remember that encrypting your wallet cannot fully protect "
114  "your feathercoins from being stolen by malware infecting your computer.") +
115  "<br><br><b>" +
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.") +
120  "</b></qt>");
121  QApplication::quit();
122  }
123  else
124  {
125  QMessageBox::critical(this, tr("Wallet encryption failed"),
126  tr("Wallet encryption failed due to an internal error. Your wallet was not encrypted."));
127  }
128  QDialog::accept(); // Success
129  }
130  else
131  {
132  QMessageBox::critical(this, tr("Wallet encryption failed"),
133  tr("The supplied passphrases do not match."));
134  }
135  }
136  else
137  {
138  QDialog::reject(); // Cancelled
139  }
140  } break;
141  case Unlock:
142  if(!model->setWalletLocked(false, oldpass))
143  {
144  QMessageBox::critical(this, tr("Wallet unlock failed"),
145  tr("The passphrase entered for the wallet decryption was incorrect."));
146  }
147  else
148  {
149  QDialog::accept(); // Success
150  }
151  break;
152  case Decrypt:
153  if(!model->setWalletEncrypted(false, oldpass))
154  {
155  QMessageBox::critical(this, tr("Wallet decryption failed"),
156  tr("The passphrase entered for the wallet decryption was incorrect."));
157  }
158  else
159  {
160  QDialog::accept(); // Success
161  }
162  break;
163  case ChangePass:
164  if(newpass1 == newpass2)
165  {
166  if(model->changePassphrase(oldpass, newpass1))
167  {
168  QMessageBox::information(this, tr("Wallet encrypted"),
169  tr("Wallet passphrase was successfully changed."));
170  QDialog::accept(); // Success
171  }
172  else
173  {
174  QMessageBox::critical(this, tr("Wallet encryption failed"),
175  tr("The passphrase entered for the wallet decryption was incorrect."));
176  }
177  }
178  else
179  {
180  QMessageBox::critical(this, tr("Wallet encryption failed"),
181  tr("The supplied passphrases do not match."));
182  }
183  break;
184  }
185 }
186 
188 {
189  // Validate input, set Ok button to enabled when acceptable
190  bool acceptable = false;
191  switch(mode)
192  {
193  case Encrypt: // New passphrase x2
194  acceptable = !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
195  break;
196  case Unlock: // Old passphrase x1
197  case Decrypt:
198  acceptable = !ui->passEdit1->text().isEmpty();
199  break;
200  case ChangePass: // Old passphrase x1, new passphrase x2
201  acceptable = !ui->passEdit1->text().isEmpty() && !ui->passEdit2->text().isEmpty() && !ui->passEdit3->text().isEmpty();
202  break;
203  }
204  ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(acceptable);
205 }
206 
207 bool AskPassphraseDialog::event(QEvent *event)
208 {
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;
214  }
215  if (fCapsLock) {
216  ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
217  } else {
218  ui->capsLabel->clear();
219  }
220  }
221  return QWidget::event(event);
222 }
223 
224 bool AskPassphraseDialog::eventFilter(QObject *object, QEvent *event)
225 {
226  /* Detect Caps Lock.
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.
231  */
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 >= 'a' && *psz <= 'z') || (!fShift && *psz >= 'A' && *psz <= 'Z')) {
239  fCapsLock = true;
240  ui->capsLabel->setText(tr("Warning: The Caps Lock key is on!"));
241  } else if (psz->isLetter()) {
242  fCapsLock = false;
243  ui->capsLabel->clear();
244  }
245  }
246  }
247  return QDialog::eventFilter(object, event);
248 }
bool event(QEvent *event)
Definition: aboutdialog.h:6
Ask passphrase twice and encrypt.
Ask passphrase and unlock.
Ui::AskPassphraseDialog * ui
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass)
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: allocators.h:269
AskPassphraseDialog(Mode mode, QWidget *parent=0)
AddressTableModel * parent
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString())
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:36
Multifunctional dialog to ask for passphrases.
bool setWalletEncrypted(bool encrypted, const SecureString &passphrase)
Ask passphrase and decrypt wallet.
bool eventFilter(QObject *object, QEvent *event)
Ask old passphrase + new passphrase twice.
void setModel(WalletModel *model)