Sorted things out with validation of NewChanDialog

This commit is contained in:
Dmitri Bogomolov 2020-05-26 13:51:09 +03:00
parent d07ab6dfd3
commit fc6613faa3
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13
2 changed files with 112 additions and 65 deletions

View File

@ -1,22 +1,32 @@
from PyQt4 import QtGui """
The validator for address and passphrase QLineEdits
used in `.dialogs.NewChanDialog`.
"""
from Queue import Empty from Queue import Empty
from qtpy import QtGui
from addresses import decodeAddress, addBMIfNotPresent from addresses import decodeAddress, addBMIfNotPresent
from account import getSortedAccounts from account import getSortedAccounts
from queues import apiAddressGeneratorReturnQueue, addressGeneratorQueue from queues import apiAddressGeneratorReturnQueue, addressGeneratorQueue
from tr import _translate from tr import _translate
from utils import str_chan from utils import str_chan
class AddressPassPhraseValidatorMixin():
def setParams(self, passPhraseObject=None, addressObject=None, feedBackObject=None, buttonBox=None, addressMandatory=True): class AddressPassPhraseValidatorMixin(object):
def setParams(
self, passPhraseObject=None, addressObject=None,
feedBackObject=None, button=None, addressMandatory=True
):
self.addressObject = addressObject self.addressObject = addressObject
self.passPhraseObject = passPhraseObject self.passPhraseObject = passPhraseObject
self.feedBackObject = feedBackObject self.feedBackObject = feedBackObject
self.buttonBox = buttonBox
self.addressMandatory = addressMandatory self.addressMandatory = addressMandatory
self.isValid = False self.isValid = False
# save default text # save default text
self.okButtonLabel = self.buttonBox.button(QtGui.QDialogButtonBox.Ok).text() self.okButton = button
self.okButtonLabel = button.text()
def setError(self, string): def setError(self, string):
if string is not None and self.feedBackObject is not None: if string is not None and self.feedBackObject is not None:
@ -26,12 +36,14 @@ class AddressPassPhraseValidatorMixin():
self.feedBackObject.setStyleSheet("QLabel { color : red; }") self.feedBackObject.setStyleSheet("QLabel { color : red; }")
self.feedBackObject.setText(string) self.feedBackObject.setText(string)
self.isValid = False self.isValid = False
if self.buttonBox: if self.okButton:
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setEnabled(False) self.okButton.setEnabled(False)
if string is not None and self.feedBackObject is not None: if string is not None and self.feedBackObject is not None:
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setText(_translate("AddressValidator", "Invalid")) self.okButton.setText(
_translate("AddressValidator", "Invalid"))
else: else:
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setText(_translate("AddressValidator", "Validating...")) self.okButton.setText(
_translate("AddressValidator", "Validating..."))
def setOK(self, string): def setOK(self, string):
if string is not None and self.feedBackObject is not None: if string is not None and self.feedBackObject is not None:
@ -41,9 +53,9 @@ class AddressPassPhraseValidatorMixin():
self.feedBackObject.setStyleSheet("QLabel { }") self.feedBackObject.setStyleSheet("QLabel { }")
self.feedBackObject.setText(string) self.feedBackObject.setText(string)
self.isValid = True self.isValid = True
if self.buttonBox: if self.okButton:
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setEnabled(True) self.okButton.setEnabled(True)
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setText(self.okButtonLabel) self.okButton.setText(self.okButtonLabel)
def checkQueue(self): def checkQueue(self):
gotOne = False gotOne = False
@ -55,7 +67,8 @@ class AddressPassPhraseValidatorMixin():
while True: while True:
try: try:
addressGeneratorReturnValue = apiAddressGeneratorReturnQueue.get(False) addressGeneratorReturnValue = \
apiAddressGeneratorReturnQueue.get(False)
except Empty: except Empty:
if gotOne: if gotOne:
break break
@ -65,80 +78,115 @@ class AddressPassPhraseValidatorMixin():
gotOne = True gotOne = True
if len(addressGeneratorReturnValue) == 0: if len(addressGeneratorReturnValue) == 0:
self.setError(_translate("AddressValidator", "Address already present as one of your identities.")) self.setError(_translate(
return (QtGui.QValidator.Intermediate, 0) "AddressValidator",
if addressGeneratorReturnValue[0] == 'chan name does not match address': "Address already present as one of your identities."
self.setError(_translate("AddressValidator", "Although the Bitmessage address you entered was valid, it doesn\'t match the chan name.")) ))
return (QtGui.QValidator.Intermediate, 0) return
self.setOK(_translate("MainWindow", "Passphrase and address appear to be valid.")) 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."))
def returnValid(self): def returnValid(self):
if self.isValid: return QtGui.QValidator.Acceptable if self.isValid \
return QtGui.QValidator.Acceptable else QtGui.QValidator.Intermediate
else:
return QtGui.QValidator.Intermediate
def validate(self, s, pos): def validate(self, s, pos):
if self.addressObject is None: try:
address = self.addressObject.text().encode('utf-8')
except AttributeError:
address = None address = None
else: try:
address = str(self.addressObject.text().toUtf8()) passPhrase = self.passPhraseObject.text().encode('utf-8')
if address == "": except AttributeError:
address = None
if self.passPhraseObject is None:
passPhrase = "" passPhrase = ""
else:
passPhrase = str(self.passPhraseObject.text().toUtf8())
if passPhrase == "":
passPhrase = None
# no chan name # no chan name
if passPhrase is None: if not passPhrase:
self.setError(_translate("AddressValidator", "Chan name/passphrase needed. You didn't enter a chan name.")) self.setError(_translate(
return (QtGui.QValidator.Intermediate, pos) "AddressValidator",
"Chan name/passphrase needed. You didn't enter a chan name."
))
return (QtGui.QValidator.Intermediate, s, pos)
if self.addressMandatory or address is not None: if self.addressMandatory or address:
# check if address already exists: # check if address already exists:
if address in getSortedAccounts(): if address in getSortedAccounts():
self.setError(_translate("AddressValidator", "Address already present as one of your identities.")) self.setError(_translate(
return (QtGui.QValidator.Intermediate, pos) "AddressValidator",
"Address already present as one of your identities."
))
return (QtGui.QValidator.Intermediate, s, pos)
status = decodeAddress(address)[0]
# version too high # version too high
if decodeAddress(address)[0] == 'versiontoohigh': 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.")) self.setError(_translate(
return (QtGui.QValidator.Intermediate, pos) "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)
# invalid # invalid
if decodeAddress(address)[0] != 'success': if status != 'success':
self.setError(_translate("AddressValidator", "The Bitmessage address is not valid.")) self.setError(_translate(
return (QtGui.QValidator.Intermediate, pos) "AddressValidator",
"The Bitmessage address is not valid."
))
return (QtGui.QValidator.Intermediate, s, pos)
# this just disables the OK button without changing the feedback text # this just disables the OK button without changing the feedback text
# but only if triggered by textEdited, not by clicking the Ok button # but only if triggered by textEdited, not by clicking the Ok button
if not self.buttonBox.button(QtGui.QDialogButtonBox.Ok).hasFocus(): if not self.okButton.hasFocus():
self.setError(None) self.setError(None)
# check through generator # check through generator
if address is None: if not address:
addressGeneratorQueue.put(('createChan', 4, 1, str_chan + ' ' + str(passPhrase), passPhrase, False)) addressGeneratorQueue.put((
'createChan', 4, 1,
str_chan + ' ' + passPhrase, passPhrase, False
))
else: else:
addressGeneratorQueue.put(('joinChan', addBMIfNotPresent(address), str_chan + ' ' + str(passPhrase), passPhrase, False)) addressGeneratorQueue.put((
'joinChan', addBMIfNotPresent(address),
str_chan + ' ' + passPhrase, passPhrase, False
))
if self.buttonBox.button(QtGui.QDialogButtonBox.Ok).hasFocus(): if self.okButton.hasFocus():
return (self.returnValid(), pos) return (self.returnValid(), s, pos)
else: else:
return (QtGui.QValidator.Intermediate, pos) return (QtGui.QValidator.Intermediate, s, pos)
def checkData(self): def checkData(self):
return self.validate("", 0) return self.validate(u"", 0)
class AddressValidator(QtGui.QValidator, AddressPassPhraseValidatorMixin): class AddressValidator(QtGui.QValidator, AddressPassPhraseValidatorMixin):
def __init__(self, parent=None, passPhraseObject=None, feedBackObject=None, buttonBox=None, addressMandatory=True): def __init__(
self, parent=None, passPhraseObject=None, feedBackObject=None,
button=None, addressMandatory=True
):
super(AddressValidator, self).__init__(parent) super(AddressValidator, self).__init__(parent)
self.setParams(passPhraseObject, parent, feedBackObject, buttonBox, addressMandatory) self.setParams(
passPhraseObject, parent, feedBackObject, button,
addressMandatory)
class PassPhraseValidator(QtGui.QValidator, AddressPassPhraseValidatorMixin): class PassPhraseValidator(QtGui.QValidator, AddressPassPhraseValidatorMixin):
def __init__(self, parent=None, addressObject=None, feedBackObject=None, buttonBox=None, addressMandatory=False): def __init__(
self, parent=None, addressObject=None, feedBackObject=None,
button=None, addressMandatory=False
):
super(PassPhraseValidator, self).__init__(parent) super(PassPhraseValidator, self).__init__(parent)
self.setParams(parent, addressObject, feedBackObject, buttonBox, addressMandatory) self.setParams(
parent, addressObject, feedBackObject, button,
addressMandatory)

View File

@ -21,10 +21,10 @@ class NewChanDialog(QtWidgets.QDialog):
self.parent = parent self.parent = parent
self.chanAddress.setValidator(AddressValidator( self.chanAddress.setValidator(AddressValidator(
self.chanAddress, self.chanPassPhrase, self.validatorFeedback, self.chanAddress, self.chanPassPhrase, self.validatorFeedback,
self.buttonBox, False)) self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok), False))
self.chanPassPhrase.setValidator(PassPhraseValidator( self.chanPassPhrase.setValidator(PassPhraseValidator(
self.chanPassPhrase, self.chanAddress, self.validatorFeedback, self.chanPassPhrase, self.chanAddress, self.validatorFeedback,
self.buttonBox, False)) self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok), False))
self.timer = QtCore.QTimer() self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.delayedUpdateStatus) self.timer.timeout.connect(self.delayedUpdateStatus)
@ -41,17 +41,16 @@ class NewChanDialog(QtWidgets.QDialog):
self.timer.stop() self.timer.stop()
self.hide() self.hide()
apiAddressGeneratorReturnQueue.queue.clear() apiAddressGeneratorReturnQueue.queue.clear()
passPhrase = self.chanPassPhrase.text().encode('utf-8')
if self.chanAddress.text() == "": if self.chanAddress.text() == "":
addressGeneratorQueue.put(( addressGeneratorQueue.put((
'createChan', 4, 1, 'createChan', 4, 1,
str_chan + ' ' + str(self.chanPassPhrase.text()), str_chan + ' ' + passPhrase, passPhrase, True
self.chanPassPhrase.text(), True
)) ))
else: else:
addressGeneratorQueue.put(( addressGeneratorQueue.put((
'joinChan', addBMIfNotPresent(self.chanAddress.text()), 'joinChan', addBMIfNotPresent(self.chanAddress.text()),
str_chan + ' ' + str(self.chanPassPhrase.text()), str_chan + ' ' + passPhrase, passPhrase, True
self.chanPassPhrase.text(), True
)) ))
addressGeneratorReturnValue = apiAddressGeneratorReturnQueue.get(True) addressGeneratorReturnValue = apiAddressGeneratorReturnQueue.get(True)
if (len(addressGeneratorReturnValue) > 0 if (len(addressGeneratorReturnValue) > 0
@ -62,7 +61,7 @@ class NewChanDialog(QtWidgets.QDialog):
_translate( _translate(
"newchandialog", "newchandialog",
"Successfully created / joined chan {0}" "Successfully created / joined chan {0}"
).format(self.chanPassPhrase.text()) ).format(passPhrase)
)) ))
self.parent.ui.tabWidget.setCurrentIndex( self.parent.ui.tabWidget.setCurrentIndex(
self.parent.ui.tabWidget.indexOf(self.parent.ui.chans) self.parent.ui.tabWidget.indexOf(self.parent.ui.chans)