d56f09ef37070ebd9bc8535ff9a5dd4ca6e32cab
[electrum-nvc.git] / gui / qt / password_dialog.py
1 #!/usr/bin/env python
2 #
3 # Electrum - lightweight Bitcoin client
4 # Copyright (C) 2013 ecdsa@github
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 from PyQt4.QtGui import *
20 from PyQt4.QtCore import *
21 from electrum.i18n import _
22 from util import *
23
24
25
26 def make_password_dialog(self, wallet, msg):
27
28     self.pw = QLineEdit()
29     self.pw.setEchoMode(2)
30     self.new_pw = QLineEdit()
31     self.new_pw.setEchoMode(2)
32     self.conf_pw = QLineEdit()
33     self.conf_pw.setEchoMode(2)
34     
35     vbox = QVBoxLayout()
36     label = QLabel(msg)
37     label.setWordWrap(True)
38
39     grid = QGridLayout()
40     grid.setSpacing(8)
41     grid.setColumnMinimumWidth(0, 70)
42     grid.setColumnStretch(1,1)
43
44     logo = QLabel()
45     lockfile = ":icons/lock.png" if wallet and wallet.use_encryption else ":icons/unlock.png"
46     logo.setPixmap(QPixmap(lockfile).scaledToWidth(36))
47     logo.setAlignment(Qt.AlignCenter)
48
49     grid.addWidget(logo,  0, 0)
50     grid.addWidget(label, 0, 1, 1, 2)
51     vbox.addLayout(grid)
52
53     grid = QGridLayout()
54     grid.setSpacing(8)
55     grid.setColumnMinimumWidth(0, 250)
56     grid.setColumnStretch(1,1)
57     
58     if wallet and wallet.use_encryption:
59         grid.addWidget(QLabel(_('Password')), 0, 0)
60         grid.addWidget(self.pw, 0, 1)
61         
62     grid.addWidget(QLabel(_('New Password')), 1, 0)
63     grid.addWidget(self.new_pw, 1, 1)
64
65     grid.addWidget(QLabel(_('Confirm Password')), 2, 0)
66     grid.addWidget(self.conf_pw, 2, 1)
67     vbox.addLayout(grid)
68
69     vbox.addStretch(1)
70     vbox.addLayout(ok_cancel_buttons(self))
71     return vbox
72
73
74 def run_password_dialog(self, wallet, parent):
75         
76     if wallet and wallet.is_watching_only():
77         QMessageBox.information(parent, _('Error'), _('This is a watching-only wallet'), _('OK'))
78         return False, None, None
79
80     if not self.exec_():
81         return False, None, None
82
83     password = unicode(self.pw.text()) if wallet and wallet.use_encryption else None
84     new_password = unicode(self.new_pw.text())
85     new_password2 = unicode(self.conf_pw.text())
86
87     if new_password != new_password2:
88         QMessageBox.warning(parent, _('Error'), _('Passwords do not match'), _('OK'))
89         # Retry
90         return run_password_dialog(self, wallet, parent)
91
92     if not new_password:
93         new_password = None
94
95     return True, password, new_password
96
97
98
99 class PasswordDialog(QDialog):
100
101     def __init__(self, wallet, parent):
102         QDialog.__init__(self, parent)
103         self.setModal(1)
104         self.wallet = wallet
105         self.parent = parent
106         self.setWindowTitle(_("Set Password"))
107         msg = (_('Your wallet is encrypted. Use this dialog to change your password.') + ' '\
108                +_('To disable wallet encryption, enter an empty new password.')) \
109                if wallet.use_encryption else _('Your wallet keys are not encrypted')
110         self.setLayout(make_password_dialog(self, wallet, msg))
111
112
113     def run(self):
114         ok, password, new_password = run_password_dialog(self, self.wallet, self.parent)
115         if not ok:
116             return
117
118         try:
119             self.wallet.check_password(password)
120         except Exception:
121             QMessageBox.warning(self.parent, _('Error'), _('Incorrect Password'), _('OK'))
122             return False, None, None
123
124         try:
125             self.wallet.update_password(password, new_password)
126         except:
127             import traceback, sys
128             traceback.print_exc(file=sys.stdout)
129             QMessageBox.warning(self.parent, _('Error'), _('Failed to update password'), _('OK'))
130             return
131
132         if new_password:
133             QMessageBox.information(self.parent, _('Success'), _('Password was updated successfully'), _('OK'))
134         else:
135             QMessageBox.information(self.parent, _('Success'), _('This wallet is not encrypted'), _('OK'))
136
137
138
139