2020-05-31 15:21:49 +02:00
|
|
|
"""
|
2021-03-03 17:15:38 +01:00
|
|
|
The validator for address and passphrase QLineEdits
|
|
|
|
used in `.dialogs.NewChanDialog`.
|
2020-05-31 15:21:49 +02:00
|
|
|
"""
|
2021-03-03 17:15:38 +01:00
|
|
|
# pylint: disable=too-many-arguments
|
2020-05-31 15:21:49 +02:00
|
|
|
|
2016-10-28 22:07:16 +02:00
|
|
|
from Queue import Empty
|
|
|
|
|
2021-03-03 17:15:38 +01:00
|
|
|
from qtpy import QtGui
|
|
|
|
|
2016-10-28 22:07:16 +02:00
|
|
|
from account import getSortedAccounts
|
2020-05-31 15:21:49 +02:00
|
|
|
from addresses import decodeAddress, addBMIfNotPresent
|
2021-03-03 17:15:38 +01:00
|
|
|
from queues import addressGeneratorQueue, apiAddressGeneratorReturnQueue
|
2016-10-28 22:07:16 +02:00
|
|
|
from tr import _translate
|
|
|
|
from utils import str_chan
|
|
|
|
|
2020-05-31 15:21:49 +02:00
|
|
|
|
|
|
|
class AddressPassPhraseValidatorMixin(object):
|
|
|
|
"""Bitmessage address or passphrase validator class for Qt UI"""
|
|
|
|
def setParams(
|
2021-03-03 17:15:38 +01:00
|
|
|
self, passPhraseObject=None, addressObject=None,
|
|
|
|
feedBackObject=None, button=None, addressMandatory=True
|
2020-05-31 15:21:49 +02:00
|
|
|
):
|
2021-03-03 17:15:38 +01:00
|
|
|
"""Initialization"""
|
2016-10-28 22:07:16 +02:00
|
|
|
self.addressObject = addressObject
|
|
|
|
self.passPhraseObject = passPhraseObject
|
|
|
|
self.feedBackObject = feedBackObject
|
|
|
|
self.addressMandatory = addressMandatory
|
|
|
|
self.isValid = False
|
2017-06-24 12:14:23 +02:00
|
|
|
# save default text
|
2021-03-03 17:15:38 +01:00
|
|
|
self.okButton = button
|
|
|
|
self.okButtonLabel = button.text()
|
2016-10-28 22:07:16 +02:00
|
|
|
|
|
|
|
def setError(self, string):
|
2020-05-31 15:21:49 +02:00
|
|
|
"""Indicate that the validation is pending or failed"""
|
2016-10-28 22:07:16 +02:00
|
|
|
if string is not None and self.feedBackObject is not None:
|
|
|
|
font = QtGui.QFont()
|
|
|
|
font.setBold(True)
|
|
|
|
self.feedBackObject.setFont(font)
|
|
|
|
self.feedBackObject.setStyleSheet("QLabel { color : red; }")
|
|
|
|
self.feedBackObject.setText(string)
|
|
|
|
self.isValid = False
|
2021-03-03 17:15:38 +01:00
|
|
|
if self.okButton:
|
|
|
|
self.okButton.setEnabled(False)
|
2017-06-24 12:14:23 +02:00
|
|
|
if string is not None and self.feedBackObject is not None:
|
2021-03-03 17:15:38 +01:00
|
|
|
self.okButton.setText(
|
2020-05-31 15:21:49 +02:00
|
|
|
_translate("AddressValidator", "Invalid"))
|
2017-06-24 12:14:23 +02:00
|
|
|
else:
|
2021-03-03 17:15:38 +01:00
|
|
|
self.okButton.setText(
|
2020-05-31 15:21:49 +02:00
|
|
|
_translate("AddressValidator", "Validating..."))
|
2016-10-28 22:07:16 +02:00
|
|
|
|
|
|
|
def setOK(self, string):
|
2020-05-31 15:21:49 +02:00
|
|
|
"""Indicate that the validation succeeded"""
|
2016-10-28 22:07:16 +02:00
|
|
|
if string is not None and self.feedBackObject is not None:
|
|
|
|
font = QtGui.QFont()
|
|
|
|
font.setBold(False)
|
|
|
|
self.feedBackObject.setFont(font)
|
|
|
|
self.feedBackObject.setStyleSheet("QLabel { }")
|
|
|
|
self.feedBackObject.setText(string)
|
|
|
|
self.isValid = True
|
2021-03-03 17:15:38 +01:00
|
|
|
if self.okButton:
|
|
|
|
self.okButton.setEnabled(True)
|
|
|
|
self.okButton.setText(self.okButtonLabel)
|
2016-10-28 22:07:16 +02:00
|
|
|
|
|
|
|
def checkQueue(self):
|
2020-05-31 15:21:49 +02:00
|
|
|
"""Validator queue loop"""
|
2016-10-28 22:07:16 +02:00
|
|
|
gotOne = False
|
|
|
|
|
|
|
|
# wait until processing is done
|
|
|
|
if not addressGeneratorQueue.empty():
|
|
|
|
self.setError(None)
|
2020-05-31 15:21:49 +02:00
|
|
|
return None
|
2016-10-28 22:07:16 +02:00
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
2021-03-03 17:15:38 +01:00
|
|
|
addressGeneratorReturnValue = \
|
|
|
|
apiAddressGeneratorReturnQueue.get(False)
|
2016-10-28 22:07:16 +02:00
|
|
|
except Empty:
|
|
|
|
if gotOne:
|
|
|
|
break
|
|
|
|
else:
|
2020-05-31 15:21:49 +02:00
|
|
|
return None
|
2016-10-28 22:07:16 +02:00
|
|
|
else:
|
|
|
|
gotOne = True
|
|
|
|
|
2020-05-31 15:21:49 +02:00
|
|
|
if not addressGeneratorReturnValue:
|
2021-03-03 17:15:38 +01:00
|
|
|
self.setError(_translate(
|
|
|
|
"AddressValidator",
|
|
|
|
"Address already present as one of your identities."
|
|
|
|
))
|
|
|
|
return
|
|
|
|
if addressGeneratorReturnValue[0] == \
|
|
|
|
'chan name does not match address':
|
|
|
|
self.setError(_translate(
|
|
|
|
"AddressValidator",
|
|
|
|
"Although the Bitmessage address you entered was valid,"
|
|
|
|
" it doesn\'t match the chan name."
|
|
|
|
))
|
|
|
|
return
|
|
|
|
self.setOK(_translate(
|
|
|
|
"MainWindow", "Passphrase and address appear to be valid."))
|
2016-10-28 22:07:16 +02:00
|
|
|
|
|
|
|
def returnValid(self):
|
2020-05-31 15:21:49 +02:00
|
|
|
"""Return the value of whether the validation was successful"""
|
2021-03-03 17:15:38 +01:00
|
|
|
return QtGui.QValidator.Acceptable if self.isValid \
|
|
|
|
else QtGui.QValidator.Intermediate
|
2016-10-28 22:07:16 +02:00
|
|
|
|
|
|
|
def validate(self, s, pos):
|
2020-05-31 15:21:49 +02:00
|
|
|
"""Top level validator method"""
|
2021-03-03 17:15:38 +01:00
|
|
|
try:
|
|
|
|
address = self.addressObject.text().encode('utf-8')
|
|
|
|
except AttributeError:
|
2016-10-28 22:07:16 +02:00
|
|
|
address = None
|
2021-03-03 17:15:38 +01:00
|
|
|
try:
|
|
|
|
passPhrase = self.passPhraseObject.text().encode('utf-8')
|
|
|
|
except AttributeError:
|
2016-10-28 22:07:16 +02:00
|
|
|
passPhrase = ""
|
|
|
|
|
|
|
|
# no chan name
|
2021-03-03 17:15:38 +01:00
|
|
|
if not passPhrase:
|
|
|
|
self.setError(_translate(
|
|
|
|
"AddressValidator",
|
|
|
|
"Chan name/passphrase needed. You didn't enter a chan name."
|
|
|
|
))
|
|
|
|
return (QtGui.QValidator.Intermediate, s, pos)
|
|
|
|
|
|
|
|
if self.addressMandatory or address:
|
2016-10-28 22:07:16 +02:00
|
|
|
# check if address already exists:
|
|
|
|
if address in getSortedAccounts():
|
2021-03-03 17:15:38 +01:00
|
|
|
self.setError(_translate(
|
|
|
|
"AddressValidator",
|
|
|
|
"Address already present as one of your identities."
|
|
|
|
))
|
|
|
|
return (QtGui.QValidator.Intermediate, s, pos)
|
2016-10-28 22:07:16 +02:00
|
|
|
|
2021-03-03 17:15:38 +01:00
|
|
|
status = decodeAddress(address)[0]
|
2016-10-28 22:07:16 +02:00
|
|
|
# version too high
|
2021-03-03 17:15:38 +01:00
|
|
|
if status == 'versiontoohigh':
|
|
|
|
self.setError(_translate(
|
|
|
|
"AddressValidator",
|
|
|
|
"Address too new. Although that Bitmessage address"
|
|
|
|
" might be valid, its version number is too new"
|
|
|
|
" for us to handle. Perhaps you need to upgrade"
|
|
|
|
" Bitmessage."
|
|
|
|
))
|
|
|
|
return (QtGui.QValidator.Intermediate, s, pos)
|
2016-10-28 22:07:16 +02:00
|
|
|
# invalid
|
2021-03-03 17:15:38 +01:00
|
|
|
if status != 'success':
|
|
|
|
self.setError(_translate(
|
|
|
|
"AddressValidator",
|
|
|
|
"The Bitmessage address is not valid."
|
|
|
|
))
|
|
|
|
return (QtGui.QValidator.Intermediate, s, pos)
|
2016-10-28 22:07:16 +02:00
|
|
|
|
|
|
|
# this just disables the OK button without changing the feedback text
|
|
|
|
# but only if triggered by textEdited, not by clicking the Ok button
|
2021-03-03 17:15:38 +01:00
|
|
|
if not self.okButton.hasFocus():
|
2016-10-28 22:07:16 +02:00
|
|
|
self.setError(None)
|
|
|
|
|
|
|
|
# check through generator
|
2021-03-03 17:15:38 +01:00
|
|
|
if not address:
|
|
|
|
addressGeneratorQueue.put((
|
|
|
|
'createChan', 4, 1,
|
|
|
|
str_chan + ' ' + passPhrase, passPhrase, False
|
|
|
|
))
|
2016-10-28 22:07:16 +02:00
|
|
|
else:
|
2021-03-03 17:15:38 +01:00
|
|
|
addressGeneratorQueue.put((
|
|
|
|
'joinChan', addBMIfNotPresent(address),
|
|
|
|
"{} {}".format(str_chan, passPhrase), passPhrase, False
|
|
|
|
))
|
2016-10-28 22:07:16 +02:00
|
|
|
|
2021-03-03 17:15:38 +01:00
|
|
|
if self.okButton.hasFocus():
|
|
|
|
return (self.returnValid(), s, pos)
|
|
|
|
else:
|
|
|
|
return (QtGui.QValidator.Intermediate, s, pos)
|
2016-10-28 22:07:16 +02:00
|
|
|
|
|
|
|
def checkData(self):
|
2020-05-31 15:21:49 +02:00
|
|
|
"""Validator Qt signal interface"""
|
2021-03-03 17:15:38 +01:00
|
|
|
return self.validate(u"", 0)
|
2016-10-28 22:07:16 +02:00
|
|
|
|
2020-05-31 15:21:49 +02:00
|
|
|
|
2016-10-28 22:07:16 +02:00
|
|
|
class AddressValidator(QtGui.QValidator, AddressPassPhraseValidatorMixin):
|
2020-05-31 15:21:49 +02:00
|
|
|
"""AddressValidator class for Qt UI"""
|
2021-03-03 17:15:38 +01:00
|
|
|
def __init__(
|
|
|
|
self, parent=None, passPhraseObject=None, feedBackObject=None,
|
|
|
|
button=None, addressMandatory=True
|
|
|
|
):
|
2016-10-28 22:07:16 +02:00
|
|
|
super(AddressValidator, self).__init__(parent)
|
2021-03-03 17:15:38 +01:00
|
|
|
self.setParams(
|
|
|
|
passPhraseObject, parent, feedBackObject, button,
|
|
|
|
addressMandatory)
|
2016-10-28 22:07:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
class PassPhraseValidator(QtGui.QValidator, AddressPassPhraseValidatorMixin):
|
2020-05-31 15:21:49 +02:00
|
|
|
"""PassPhraseValidator class for Qt UI"""
|
2021-03-03 17:15:38 +01:00
|
|
|
def __init__(
|
|
|
|
self, parent=None, addressObject=None, feedBackObject=None,
|
|
|
|
button=None, addressMandatory=False
|
|
|
|
):
|
2016-10-28 22:07:16 +02:00
|
|
|
super(PassPhraseValidator, self).__init__(parent)
|
2021-03-03 17:15:38 +01:00
|
|
|
self.setParams(
|
|
|
|
parent, addressObject, feedBackObject, button,
|
|
|
|
addressMandatory)
|