generalize plugins to all guis
[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 qt_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.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.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     self.setLayout(vbox) 
72
73
74 def run_password_dialog(self, wallet, parent):
75         
76     if not wallet.seed:
77         QMessageBox.information(parent, _('Error'), _('No seed'), _('OK'))
78         return
79
80     if not self.exec_(): return
81
82     password = unicode(self.pw.text()) if wallet.use_encryption else None
83     new_password = unicode(self.new_pw.text())
84     new_password2 = unicode(self.conf_pw.text())
85
86     try:
87         seed = wallet.decode_seed(password)
88     except:
89         QMessageBox.warning(parent, _('Error'), _('Incorrect Password'), _('OK'))
90         return
91
92     if new_password != new_password2:
93         QMessageBox.warning(parent, _('Error'), _('Passwords do not match'), _('OK'))
94         self.run() # Retry
95
96     try:
97         wallet.update_password(seed, password, new_password)
98     except:
99         QMessageBox.warning(parent, _('Error'), _('Failed to update password'), _('OK'))
100         return
101
102     if new_password:
103         QMessageBox.information(parent, _('Success'), _('Password was updated successfully'), _('OK'))
104     else:
105         QMessageBox.information(parent, _('Success'), _('This wallet is not encrypted'), _('OK'))
106
107
108
109
110 class PasswordDialog(QDialog):
111
112     def __init__(self, wallet, parent):
113         QDialog.__init__(self, parent)
114         self.setModal(1)
115         self.wallet = wallet
116         self.parent = parent
117         msg = (_('Your wallet is encrypted. Use this dialog to change your password.') + ' '\
118                +_('To disable wallet encryption, enter an empty new password.')) \
119                if wallet.use_encryption else _('Your wallet keys are not encrypted')
120         make_password_dialog(self, wallet, msg)
121
122
123     def run(self):
124         run_password_dialog(self, self.wallet, self.parent)
125
126
127