11"""
2- Address validator module.
2+ The validator for address and passphrase QLineEdits
3+ used in `.dialogs.NewChanDialog`.
34"""
4- # pylint: disable=too-many-branches,too-many- arguments
5+ # pylint: disable=too-many-arguments
56
6- from PyQt4 import QtGui
77from Queue import Empty
88
9+ from PyQt5 import QtGui
10+
911from account import getSortedAccounts
1012from addresses import decodeAddress , addBMIfNotPresent
11- from queues import apiAddressGeneratorReturnQueue , addressGeneratorQueue
13+ from queues import addressGeneratorQueue , apiAddressGeneratorReturnQueue
1214from tr import _translate
1315from utils import str_chan
1416
1517
1618class AddressPassPhraseValidatorMixin (object ):
1719 """Bitmessage address or passphrase validator class for Qt UI"""
1820 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
2523 ):
26- """Initialisation """
24+ """Initialization """
2725 self .addressObject = addressObject
2826 self .passPhraseObject = passPhraseObject
2927 self .feedBackObject = feedBackObject
30- self .buttonBox = buttonBox
3128 self .addressMandatory = addressMandatory
3229 self .isValid = False
3330 # save default text
34- self .okButtonLabel = self .buttonBox .button (QtGui .QDialogButtonBox .Ok ).text ()
31+ self .okButton = button
32+ self .okButtonLabel = button .text ()
3533
3634 def setError (self , string ):
3735 """Indicate that the validation is pending or failed"""
@@ -42,13 +40,13 @@ def setError(self, string):
4240 self .feedBackObject .setStyleSheet ("QLabel { color : red; }" )
4341 self .feedBackObject .setText (string )
4442 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 )
4745 if string is not None and self .feedBackObject is not None :
48- self .buttonBox . button ( QtGui . QDialogButtonBox . Ok ) .setText (
46+ self .okButton .setText (
4947 _translate ("AddressValidator" , "Invalid" ))
5048 else :
51- self .buttonBox . button ( QtGui . QDialogButtonBox . Ok ) .setText (
49+ self .okButton .setText (
5250 _translate ("AddressValidator" , "Validating..." ))
5351
5452 def setOK (self , string ):
@@ -60,9 +58,9 @@ def setOK(self, string):
6058 self .feedBackObject .setStyleSheet ("QLabel { }" )
6159 self .feedBackObject .setText (string )
6260 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 )
6664
6765 def checkQueue (self ):
6866 """Validator queue loop"""
@@ -75,7 +73,8 @@ def checkQueue(self):
7573
7674 while True :
7775 try :
78- addressGeneratorReturnValue = apiAddressGeneratorReturnQueue .get (False )
76+ addressGeneratorReturnValue = \
77+ apiAddressGeneratorReturnQueue .get (False )
7978 except Empty :
8079 if gotOne :
8180 break
@@ -85,96 +84,120 @@ def checkQueue(self):
8584 gotOne = True
8685
8786 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." ))
98102
99103 def returnValid (self ):
100104 """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
104107
105108 def validate (self , s , pos ):
106109 """Top level validator method"""
107- if self .addressObject is None :
110+ try :
111+ address = self .addressObject .text ().encode ('utf-8' )
112+ except AttributeError :
108113 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 :
114117 passPhrase = ""
115- else :
116- passPhrase = str (self .passPhraseObject .text ().toUtf8 ())
117- if passPhrase == "" :
118- passPhrase = None
119118
120119 # 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 :
126128 # check if address already exists:
127129 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 )
130135
136+ status = decodeAddress (address )[0 ]
131137 # 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 )
142147 # 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 )
146154
147155 # this just disables the OK button without changing the feedback text
148156 # 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 ():
150158 self .setError (None )
151159
152160 # 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+ ))
155166 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+ ))
159171
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 )
163176
164177 def checkData (self ):
165178 """Validator Qt signal interface"""
166- return self .validate ("" , 0 )
179+ return self .validate (u "" , 0 )
167180
168181
169182class AddressValidator (QtGui .QValidator , AddressPassPhraseValidatorMixin ):
170183 """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+ ):
172188 super (AddressValidator , self ).__init__ (parent )
173- self .setParams (passPhraseObject , parent , feedBackObject , buttonBox , addressMandatory )
189+ self .setParams (
190+ passPhraseObject , parent , feedBackObject , button ,
191+ addressMandatory )
174192
175193
176194class PassPhraseValidator (QtGui .QValidator , AddressPassPhraseValidatorMixin ):
177195 """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+ ):
179200 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