Skip to content

Commit

Permalink
Prepare newchandialog module for using qtpy
Browse files Browse the repository at this point in the history
  • Loading branch information
g1itch committed Nov 26, 2021
1 parent 43b69f1 commit 3c4c51a
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 118 deletions.
179 changes: 101 additions & 78 deletions src/bitmessageqt/addressvalidator.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
"""
Address validator module.
The validator for address and passphrase QLineEdits
used in `.dialogs.NewChanDialog`.
"""
# pylint: disable=too-many-branches,too-many-arguments
# pylint: disable=too-many-arguments

from PyQt4 import QtGui
from Queue import Empty

from qtpy import QtGui

from account import getSortedAccounts
from addresses import decodeAddress, addBMIfNotPresent
from queues import apiAddressGeneratorReturnQueue, addressGeneratorQueue
from queues import addressGeneratorQueue, apiAddressGeneratorReturnQueue
from tr import _translate
from utils import str_chan


class AddressPassPhraseValidatorMixin(object):
"""Bitmessage address or passphrase validator class for Qt UI"""
def setParams(
self,
passPhraseObject=None,
addressObject=None,
feedBackObject=None,
buttonBox=None,
addressMandatory=True,
self, passPhraseObject=None, addressObject=None,
feedBackObject=None, button=None, addressMandatory=True
):
"""Initialisation"""
"""Initialization"""
self.addressObject = addressObject
self.passPhraseObject = passPhraseObject
self.feedBackObject = feedBackObject
self.buttonBox = buttonBox
self.addressMandatory = addressMandatory
self.isValid = False
# save default text
self.okButtonLabel = self.buttonBox.button(QtGui.QDialogButtonBox.Ok).text()
self.okButton = button
self.okButtonLabel = button.text()

def setError(self, string):
"""Indicate that the validation is pending or failed"""
Expand All @@ -42,13 +40,13 @@ def setError(self, string):
self.feedBackObject.setStyleSheet("QLabel { color : red; }")
self.feedBackObject.setText(string)
self.isValid = False
if self.buttonBox:
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setEnabled(False)
if self.okButton:
self.okButton.setEnabled(False)
if string is not None and self.feedBackObject is not None:
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setText(
self.okButton.setText(
_translate("AddressValidator", "Invalid"))
else:
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setText(
self.okButton.setText(
_translate("AddressValidator", "Validating..."))

def setOK(self, string):
Expand All @@ -60,9 +58,9 @@ def setOK(self, string):
self.feedBackObject.setStyleSheet("QLabel { }")
self.feedBackObject.setText(string)
self.isValid = True
if self.buttonBox:
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setEnabled(True)
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setText(self.okButtonLabel)
if self.okButton:
self.okButton.setEnabled(True)
self.okButton.setText(self.okButtonLabel)

def checkQueue(self):
"""Validator queue loop"""
Expand All @@ -75,7 +73,8 @@ def checkQueue(self):

while True:
try:
addressGeneratorReturnValue = apiAddressGeneratorReturnQueue.get(False)
addressGeneratorReturnValue = \
apiAddressGeneratorReturnQueue.get(False)
except Empty:
if gotOne:
break
Expand All @@ -85,96 +84,120 @@ def checkQueue(self):
gotOne = True

if not addressGeneratorReturnValue:
self.setError(_translate("AddressValidator", "Address already present as one of your identities."))
return (QtGui.QValidator.Intermediate, 0)
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 (QtGui.QValidator.Intermediate, 0)
self.setOK(_translate("MainWindow", "Passphrase and address appear to be valid."))
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."))

def returnValid(self):
"""Return the value of whether the validation was successful"""
if self.isValid:
return QtGui.QValidator.Acceptable
return QtGui.QValidator.Intermediate
return QtGui.QValidator.Acceptable if self.isValid \
else QtGui.QValidator.Intermediate

def validate(self, s, pos):
"""Top level validator method"""
if self.addressObject is None:
try:
address = self.addressObject.text().encode('utf-8')
except AttributeError:
address = None
else:
address = str(self.addressObject.text().toUtf8())
if address == "":
address = None
if self.passPhraseObject is None:
try:
passPhrase = self.passPhraseObject.text().encode('utf-8')
except AttributeError:
passPhrase = ""
else:
passPhrase = str(self.passPhraseObject.text().toUtf8())
if passPhrase == "":
passPhrase = None

# no chan name
if passPhrase is None:
self.setError(_translate("AddressValidator", "Chan name/passphrase needed. You didn't enter a chan name."))
return (QtGui.QValidator.Intermediate, pos)

if self.addressMandatory or address is not None:
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:
# check if address already exists:
if address in getSortedAccounts():
self.setError(_translate("AddressValidator", "Address already present as one of your identities."))
return (QtGui.QValidator.Intermediate, pos)
self.setError(_translate(
"AddressValidator",
"Address already present as one of your identities."
))
return (QtGui.QValidator.Intermediate, s, pos)

status = decodeAddress(address)[0]
# version too high
if decodeAddress(address)[0] == '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, pos)

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)
# invalid
if decodeAddress(address)[0] != 'success':
self.setError(_translate("AddressValidator", "The Bitmessage address is not valid."))
return (QtGui.QValidator.Intermediate, pos)
if status != 'success':
self.setError(_translate(
"AddressValidator",
"The Bitmessage address is not valid."
))
return (QtGui.QValidator.Intermediate, s, pos)

# this just disables the OK button without changing the feedback text
# 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)

# check through generator
if address is None:
addressGeneratorQueue.put(('createChan', 4, 1, str_chan + ' ' + str(passPhrase), passPhrase, False))
if not address:
addressGeneratorQueue.put((
'createChan', 4, 1,
str_chan + ' ' + passPhrase, passPhrase, False
))
else:
addressGeneratorQueue.put(
('joinChan', addBMIfNotPresent(address),
"{} {}".format(str_chan, passPhrase), passPhrase, False))
addressGeneratorQueue.put((
'joinChan', addBMIfNotPresent(address),
"{} {}".format(str_chan, passPhrase), passPhrase, False
))

if self.buttonBox.button(QtGui.QDialogButtonBox.Ok).hasFocus():
return (self.returnValid(), pos)
return (QtGui.QValidator.Intermediate, pos)
if self.okButton.hasFocus():
return (self.returnValid(), s, pos)
else:
return (QtGui.QValidator.Intermediate, s, pos)

def checkData(self):
"""Validator Qt signal interface"""
return self.validate("", 0)
return self.validate(u"", 0)


class AddressValidator(QtGui.QValidator, AddressPassPhraseValidatorMixin):
"""AddressValidator class for Qt UI"""
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)
self.setParams(passPhraseObject, parent, feedBackObject, buttonBox, addressMandatory)
self.setParams(
passPhraseObject, parent, feedBackObject, button,
addressMandatory)


class PassPhraseValidator(QtGui.QValidator, AddressPassPhraseValidatorMixin):
"""PassPhraseValidator class for Qt UI"""
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)
self.setParams(parent, addressObject, feedBackObject, buttonBox, addressMandatory)
self.setParams(
parent, addressObject, feedBackObject, button,
addressMandatory)
84 changes: 44 additions & 40 deletions src/bitmessageqt/newchandialog.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"""
src/bitmessageqt/newchandialog.py
=================================
NewChanDialog class definition
"""

from PyQt4 import QtCore, QtGui
from qtpy import QtCore, QtWidgets

import widgets
from addresses import addBMIfNotPresent
Expand All @@ -15,30 +13,21 @@
from utils import str_chan


class NewChanDialog(QtGui.QDialog):
"""The `New Chan` dialog"""
class NewChanDialog(QtWidgets.QDialog):
"""The "New Chan" dialog"""
def __init__(self, parent=None):
super(NewChanDialog, self).__init__(parent)
widgets.load('newchandialog.ui', self)
self.parent = parent
self.chanAddress.setValidator(
AddressValidator(
self.chanAddress,
self.chanPassPhrase,
self.validatorFeedback,
self.buttonBox,
False))
self.chanPassPhrase.setValidator(
PassPhraseValidator(
self.chanPassPhrase,
self.chanAddress,
self.validatorFeedback,
self.buttonBox,
False))
self.chanAddress.setValidator(AddressValidator(
self.chanAddress, self.chanPassPhrase, self.validatorFeedback,
self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok), False))
self.chanPassPhrase.setValidator(PassPhraseValidator(
self.chanPassPhrase, self.chanAddress, self.validatorFeedback,
self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok), False))

self.timer = QtCore.QTimer()
QtCore.QObject.connect( # pylint: disable=no-member
self.timer, QtCore.SIGNAL("timeout()"), self.delayedUpdateStatus)
self.timer.timeout.connect(self.delayedUpdateStatus)
self.timer.start(500) # milliseconds
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.show()
Expand All @@ -52,32 +41,47 @@ def accept(self):
self.timer.stop()
self.hide()
apiAddressGeneratorReturnQueue.queue.clear()
if self.chanAddress.text().toUtf8() == "":
addressGeneratorQueue.put(
('createChan', 4, 1, str_chan + ' ' + str(self.chanPassPhrase.text().toUtf8()),
self.chanPassPhrase.text().toUtf8(),
True))
passPhrase = self.chanPassPhrase.text().encode('utf-8')
if self.chanAddress.text() == "":
addressGeneratorQueue.put((
'createChan', 4, 1,
str_chan + ' ' + passPhrase, passPhrase, True
))
else:
addressGeneratorQueue.put(
('joinChan', addBMIfNotPresent(self.chanAddress.text().toUtf8()),
str_chan + ' ' + str(self.chanPassPhrase.text().toUtf8()),
self.chanPassPhrase.text().toUtf8(),
True))
addressGeneratorQueue.put((
'joinChan', addBMIfNotPresent(self.chanAddress.text()),
str_chan + ' ' + passPhrase, passPhrase, True
))
addressGeneratorReturnValue = apiAddressGeneratorReturnQueue.get(True)
if addressGeneratorReturnValue and addressGeneratorReturnValue[0] != 'chan name does not match address':
UISignalQueue.put(('updateStatusBar', _translate(
"newchandialog", "Successfully created / joined chan %1").arg(unicode(self.chanPassPhrase.text()))))
if (
len(addressGeneratorReturnValue) > 0
and addressGeneratorReturnValue[0]
!= 'chan name does not match address'
):
UISignalQueue.put((
'updateStatusBar',
_translate(
"newchandialog",
"Successfully created / joined chan {0}"
).format(passPhrase)
))
self.parent.ui.tabWidget.setCurrentIndex(
self.parent.ui.tabWidget.indexOf(self.parent.ui.chans)
)
self.done(QtGui.QDialog.Accepted)
self.done(QtWidgets.QDialog.Accepted)
else:
UISignalQueue.put(('updateStatusBar', _translate("newchandialog", "Chan creation / joining failed")))
self.done(QtGui.QDialog.Rejected)
UISignalQueue.put((
'updateStatusBar',
_translate("newchandialog", "Chan creation / joining failed")
))
self.done(QtWidgets.QDialog.Rejected)

def reject(self):
"""Cancel joining the chan"""
self.timer.stop()
self.hide()
UISignalQueue.put(('updateStatusBar', _translate("newchandialog", "Chan creation / joining cancelled")))
self.done(QtGui.QDialog.Rejected)
UISignalQueue.put((
'updateStatusBar',
_translate("newchandialog", "Chan creation / joining cancelled")
))
self.done(QtWidgets.QDialog.Rejected)

0 comments on commit 3c4c51a

Please sign in to comment.