1
1
"""
2
- Address validator module.
2
+ The validator for address and passphrase QLineEdits
3
+ used in `.dialogs.NewChanDialog`.
3
4
"""
4
- # pylint: disable=too-many-branches,too-many- arguments
5
+ # pylint: disable=too-many-arguments
5
6
6
- from PyQt4 import QtGui
7
7
from Queue import Empty
8
8
9
+ from qtpy import QtGui
10
+
9
11
from account import getSortedAccounts
10
12
from addresses import decodeAddress , addBMIfNotPresent
11
- from queues import apiAddressGeneratorReturnQueue , addressGeneratorQueue
13
+ from queues import addressGeneratorQueue , apiAddressGeneratorReturnQueue
12
14
from tr import _translate
13
15
from utils import str_chan
14
16
15
17
16
18
class AddressPassPhraseValidatorMixin (object ):
17
19
"""Bitmessage address or passphrase validator class for Qt UI"""
18
20
def setParams (
19
- self ,
20
- passPhraseObject = None ,
21
- addressObject = None ,
22
- feedBackObject = None ,
23
- buttonBox = None ,
24
- addressMandatory = True ,
21
+ self , passPhraseObject = None , addressObject = None ,
22
+ feedBackObject = None , button = None , addressMandatory = True
25
23
):
26
- """Initialisation """
24
+ """Initialization """
27
25
self .addressObject = addressObject
28
26
self .passPhraseObject = passPhraseObject
29
27
self .feedBackObject = feedBackObject
30
- self .buttonBox = buttonBox
31
28
self .addressMandatory = addressMandatory
32
29
self .isValid = False
33
30
# save default text
34
- self .okButtonLabel = self .buttonBox .button (QtGui .QDialogButtonBox .Ok ).text ()
31
+ self .okButton = button
32
+ self .okButtonLabel = button .text ()
35
33
36
34
def setError (self , string ):
37
35
"""Indicate that the validation is pending or failed"""
@@ -42,13 +40,13 @@ def setError(self, string):
42
40
self .feedBackObject .setStyleSheet ("QLabel { color : red; }" )
43
41
self .feedBackObject .setText (string )
44
42
self .isValid = False
45
- if self .buttonBox :
46
- self .buttonBox . button ( QtGui . QDialogButtonBox . Ok ) .setEnabled (False )
43
+ if self .okButton :
44
+ self .okButton .setEnabled (False )
47
45
if string is not None and self .feedBackObject is not None :
48
- self .buttonBox . button ( QtGui . QDialogButtonBox . Ok ) .setText (
46
+ self .okButton .setText (
49
47
_translate ("AddressValidator" , "Invalid" ))
50
48
else :
51
- self .buttonBox . button ( QtGui . QDialogButtonBox . Ok ) .setText (
49
+ self .okButton .setText (
52
50
_translate ("AddressValidator" , "Validating..." ))
53
51
54
52
def setOK (self , string ):
@@ -60,9 +58,9 @@ def setOK(self, string):
60
58
self .feedBackObject .setStyleSheet ("QLabel { }" )
61
59
self .feedBackObject .setText (string )
62
60
self .isValid = True
63
- if self .buttonBox :
64
- self .buttonBox . button ( QtGui . QDialogButtonBox . Ok ) .setEnabled (True )
65
- self .buttonBox . button ( QtGui . QDialogButtonBox . Ok ) .setText (self .okButtonLabel )
61
+ if self .okButton :
62
+ self .okButton .setEnabled (True )
63
+ self .okButton .setText (self .okButtonLabel )
66
64
67
65
def checkQueue (self ):
68
66
"""Validator queue loop"""
@@ -75,7 +73,8 @@ def checkQueue(self):
75
73
76
74
while True :
77
75
try :
78
- addressGeneratorReturnValue = apiAddressGeneratorReturnQueue .get (False )
76
+ addressGeneratorReturnValue = \
77
+ apiAddressGeneratorReturnQueue .get (False )
79
78
except Empty :
80
79
if gotOne :
81
80
break
@@ -85,96 +84,120 @@ def checkQueue(self):
85
84
gotOne = True
86
85
87
86
if not addressGeneratorReturnValue :
88
- self .setError (_translate ("AddressValidator" , "Address already present as one of your identities." ))
89
- return (QtGui .QValidator .Intermediate , 0 )
90
- if addressGeneratorReturnValue [0 ] == 'chan name does not match address' :
91
- self .setError (
92
- _translate (
93
- "AddressValidator" ,
94
- "Although the Bitmessage address you "
95
- "entered was valid, it doesn't match the chan name." ))
96
- return (QtGui .QValidator .Intermediate , 0 )
97
- self .setOK (_translate ("MainWindow" , "Passphrase and address appear to be valid." ))
87
+ self .setError (_translate (
88
+ "AddressValidator" ,
89
+ "Address already present as one of your identities."
90
+ ))
91
+ return
92
+ if addressGeneratorReturnValue [0 ] == \
93
+ 'chan name does not match address' :
94
+ self .setError (_translate (
95
+ "AddressValidator" ,
96
+ "Although the Bitmessage address you entered was valid,"
97
+ " it doesn\' t match the chan name."
98
+ ))
99
+ return
100
+ self .setOK (_translate (
101
+ "MainWindow" , "Passphrase and address appear to be valid." ))
98
102
99
103
def returnValid (self ):
100
104
"""Return the value of whether the validation was successful"""
101
- if self .isValid :
102
- return QtGui .QValidator .Acceptable
103
- return QtGui .QValidator .Intermediate
105
+ return QtGui .QValidator .Acceptable if self .isValid \
106
+ else QtGui .QValidator .Intermediate
104
107
105
108
def validate (self , s , pos ):
106
109
"""Top level validator method"""
107
- if self .addressObject is None :
110
+ try :
111
+ address = self .addressObject .text ().encode ('utf-8' )
112
+ except AttributeError :
108
113
address = None
109
- else :
110
- address = str (self .addressObject .text ().toUtf8 ())
111
- if address == "" :
112
- address = None
113
- if self .passPhraseObject is None :
114
+ try :
115
+ passPhrase = self .passPhraseObject .text ().encode ('utf-8' )
116
+ except AttributeError :
114
117
passPhrase = ""
115
- else :
116
- passPhrase = str (self .passPhraseObject .text ().toUtf8 ())
117
- if passPhrase == "" :
118
- passPhrase = None
119
118
120
119
# no chan name
121
- if passPhrase is None :
122
- self .setError (_translate ("AddressValidator" , "Chan name/passphrase needed. You didn't enter a chan name." ))
123
- return (QtGui .QValidator .Intermediate , pos )
124
-
125
- if self .addressMandatory or address is not None :
120
+ if not passPhrase :
121
+ self .setError (_translate (
122
+ "AddressValidator" ,
123
+ "Chan name/passphrase needed. You didn't enter a chan name."
124
+ ))
125
+ return (QtGui .QValidator .Intermediate , s , pos )
126
+
127
+ if self .addressMandatory or address :
126
128
# check if address already exists:
127
129
if address in getSortedAccounts ():
128
- self .setError (_translate ("AddressValidator" , "Address already present as one of your identities." ))
129
- return (QtGui .QValidator .Intermediate , pos )
130
+ self .setError (_translate (
131
+ "AddressValidator" ,
132
+ "Address already present as one of your identities."
133
+ ))
134
+ return (QtGui .QValidator .Intermediate , s , pos )
130
135
136
+ status = decodeAddress (address )[0 ]
131
137
# version too high
132
- if decodeAddress (address )[0 ] == 'versiontoohigh' :
133
- self .setError (
134
- _translate (
135
- "AddressValidator" ,
136
- "Address too new. Although that Bitmessage"
137
- " address might be valid, its version number"
138
- " is too new for us to handle. Perhaps you need"
139
- " to upgrade Bitmessage." ))
140
- return (QtGui .QValidator .Intermediate , pos )
141
-
138
+ if status == 'versiontoohigh' :
139
+ self .setError (_translate (
140
+ "AddressValidator" ,
141
+ "Address too new. Although that Bitmessage address"
142
+ " might be valid, its version number is too new"
143
+ " for us to handle. Perhaps you need to upgrade"
144
+ " Bitmessage."
145
+ ))
146
+ return (QtGui .QValidator .Intermediate , s , pos )
142
147
# invalid
143
- if decodeAddress (address )[0 ] != 'success' :
144
- self .setError (_translate ("AddressValidator" , "The Bitmessage address is not valid." ))
145
- return (QtGui .QValidator .Intermediate , pos )
148
+ if status != 'success' :
149
+ self .setError (_translate (
150
+ "AddressValidator" ,
151
+ "The Bitmessage address is not valid."
152
+ ))
153
+ return (QtGui .QValidator .Intermediate , s , pos )
146
154
147
155
# this just disables the OK button without changing the feedback text
148
156
# but only if triggered by textEdited, not by clicking the Ok button
149
- if not self .buttonBox . button ( QtGui . QDialogButtonBox . Ok ) .hasFocus ():
157
+ if not self .okButton .hasFocus ():
150
158
self .setError (None )
151
159
152
160
# check through generator
153
- if address is None :
154
- addressGeneratorQueue .put (('createChan' , 4 , 1 , str_chan + ' ' + str (passPhrase ), passPhrase , False ))
161
+ if not address :
162
+ addressGeneratorQueue .put ((
163
+ 'createChan' , 4 , 1 ,
164
+ str_chan + ' ' + passPhrase , passPhrase , False
165
+ ))
155
166
else :
156
- addressGeneratorQueue .put (
157
- ('joinChan' , addBMIfNotPresent (address ),
158
- "{} {}" .format (str_chan , passPhrase ), passPhrase , False ))
167
+ addressGeneratorQueue .put ((
168
+ 'joinChan' , addBMIfNotPresent (address ),
169
+ "{} {}" .format (str_chan , passPhrase ), passPhrase , False
170
+ ))
159
171
160
- if self .buttonBox .button (QtGui .QDialogButtonBox .Ok ).hasFocus ():
161
- return (self .returnValid (), pos )
162
- return (QtGui .QValidator .Intermediate , pos )
172
+ if self .okButton .hasFocus ():
173
+ return (self .returnValid (), s , pos )
174
+ else :
175
+ return (QtGui .QValidator .Intermediate , s , pos )
163
176
164
177
def checkData (self ):
165
178
"""Validator Qt signal interface"""
166
- return self .validate ("" , 0 )
179
+ return self .validate (u "" , 0 )
167
180
168
181
169
182
class AddressValidator (QtGui .QValidator , AddressPassPhraseValidatorMixin ):
170
183
"""AddressValidator class for Qt UI"""
171
- def __init__ (self , parent = None , passPhraseObject = None , feedBackObject = None , buttonBox = None , addressMandatory = True ):
184
+ def __init__ (
185
+ self , parent = None , passPhraseObject = None , feedBackObject = None ,
186
+ button = None , addressMandatory = True
187
+ ):
172
188
super (AddressValidator , self ).__init__ (parent )
173
- self .setParams (passPhraseObject , parent , feedBackObject , buttonBox , addressMandatory )
189
+ self .setParams (
190
+ passPhraseObject , parent , feedBackObject , button ,
191
+ addressMandatory )
174
192
175
193
176
194
class PassPhraseValidator (QtGui .QValidator , AddressPassPhraseValidatorMixin ):
177
195
"""PassPhraseValidator class for Qt UI"""
178
- def __init__ (self , parent = None , addressObject = None , feedBackObject = None , buttonBox = None , addressMandatory = False ):
196
+ def __init__ (
197
+ self , parent = None , addressObject = None , feedBackObject = None ,
198
+ button = None , addressMandatory = False
199
+ ):
179
200
super (PassPhraseValidator , self ).__init__ (parent )
180
- self .setParams (parent , addressObject , feedBackObject , buttonBox , addressMandatory )
201
+ self .setParams (
202
+ parent , addressObject , feedBackObject , button ,
203
+ addressMandatory )
0 commit comments