Many changes on chan create/join dialog
- refactored to use the .ui file - input logic change, address is always optional - interactive input validation - runs asynchronously to the main window - address generator thread can now validate chans in addition to just adding themmaster
parent
5c9bfe09a2
commit
3d4869851e
@ -0,0 +1,137 @@
|
||||
from PyQt4 import QtGui
|
||||
from Queue import Empty
|
||||
|
||||
from addresses import decodeAddress, addBMIfNotPresent
|
||||
from account import getSortedAccounts
|
||||
from shared import apiAddressGeneratorReturnQueue, addressGeneratorQueue
|
||||
from tr import _translate
|
||||
from utils import str_chan
|
||||
|
||||
class AddressPassPhraseValidatorMixin():
|
||||
def setParams(self, passPhraseObject=None, addressObject=None, feedBackObject=None, buttonBox=None, addressMandatory=True):
|
||||
self.addressObject = addressObject
|
||||
self.passPhraseObject = passPhraseObject
|
||||
self.feedBackObject = feedBackObject
|
||||
self.buttonBox = buttonBox
|
||||
self.addressMandatory = addressMandatory
|
||||
self.isValid = False
|
||||
|
||||
def setError(self, string):
|
||||
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
|
||||
if self.buttonBox:
|
||||
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setEnabled(False)
|
||||
|
||||
def setOK(self, string):
|
||||
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
|
||||
if self.buttonBox:
|
||||
self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setEnabled(True)
|
||||
|
||||
def checkQueue(self):
|
||||
gotOne = False
|
||||
|
||||
# wait until processing is done
|
||||
if not addressGeneratorQueue.empty():
|
||||
self.setError(None)
|
||||
return
|
||||
|
||||
while True:
|
||||
try:
|
||||
addressGeneratorReturnValue = apiAddressGeneratorReturnQueue.get(False)
|
||||
except Empty:
|
||||
if gotOne:
|
||||
break
|
||||
else:
|
||||
return
|
||||
else:
|
||||
gotOne = True
|
||||
|
||||
if len(addressGeneratorReturnValue) == 0:
|
||||
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."))
|
||||
|
||||
def returnValid(self):
|
||||
if self.isValid:
|
||||
return QtGui.QValidator.Acceptable
|
||||
else:
|
||||
return QtGui.QValidator.Intermediate
|
||||
|
||||
def validate(self, s, pos):
|
||||
if self.addressObject is None:
|
||||
address = None
|
||||
else:
|
||||
address = str(self.addressObject.text().toUtf8())
|
||||
if address == "":
|
||||
address = None
|
||||
if self.passPhraseObject is None:
|
||||
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:
|
||||
# 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)
|
||||
|
||||
# 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)
|
||||
|
||||
# invalid
|
||||
if decodeAddress(address)[0] != 'success':
|
||||
self.setError(_translate("AddressValidator", "The Bitmessage address is not valid."))
|
||||
return (QtGui.QValidator.Intermediate, 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():
|
||||
self.setError(None)
|
||||
|
||||
# check through generator
|
||||
if address is None:
|
||||
addressGeneratorQueue.put(('createChan', 4, 1, str_chan + ' ' + str(passPhrase), passPhrase, False))
|
||||
else:
|
||||
addressGeneratorQueue.put(('joinChan', addBMIfNotPresent(address), str_chan + ' ' + str(passPhrase), passPhrase, False))
|
||||
|
||||
if self.buttonBox.button(QtGui.QDialogButtonBox.Ok).hasFocus():
|
||||
return (self.returnValid(), pos)
|
||||
else:
|
||||
return (QtGui.QValidator.Intermediate, pos)
|
||||
|
||||
def checkData(self):
|
||||
return self.validate("", 0)
|
||||
|
||||
class AddressValidator(QtGui.QValidator, AddressPassPhraseValidatorMixin):
|
||||
def __init__(self, parent=None, passPhraseObject=None, feedBackObject=None, buttonBox=None, addressMandatory=True):
|
||||
super(AddressValidator, self).__init__(parent)
|
||||
self.setParams(passPhraseObject, parent, feedBackObject, buttonBox, addressMandatory)
|
||||
|
||||
|
||||
class PassPhraseValidator(QtGui.QValidator, AddressPassPhraseValidatorMixin):
|
||||
def __init__(self, parent=None, addressObject=None, feedBackObject=None, buttonBox=None, addressMandatory=False):
|
||||
super(PassPhraseValidator, self).__init__(parent)
|
||||
self.setParams(parent, addressObject, feedBackObject, buttonBox, addressMandatory)
|
@ -1,107 +1,52 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Form implementation generated from reading ui file 'newchandialog.ui'
|
||||
#
|
||||
# Created: Wed Aug 7 16:51:29 2013
|
||||
# by: PyQt4 UI code generator 4.10
|
||||
#
|
||||
# WARNING! All changes made in this file will be lost!
|
||||
|
||||
from PyQt4 import QtCore, QtGui
|
||||
|
||||
try:
|
||||
_fromUtf8 = QtCore.QString.fromUtf8
|
||||
except AttributeError:
|
||||
def _fromUtf8(s):
|
||||
return s
|
||||
|
||||
try:
|
||||
_encoding = QtGui.QApplication.UnicodeUTF8
|
||||
def _translate(context, text, disambig):
|
||||
return QtGui.QApplication.translate(context, text, disambig, _encoding)
|
||||
except AttributeError:
|
||||
def _translate(context, text, disambig):
|
||||
return QtGui.QApplication.translate(context, text, disambig)
|
||||
|
||||
class Ui_newChanDialog(object):
|
||||
def setupUi(self, newChanDialog):
|
||||
newChanDialog.setObjectName(_fromUtf8("newChanDialog"))
|
||||
newChanDialog.resize(553, 422)
|
||||
newChanDialog.setMinimumSize(QtCore.QSize(0, 0))
|
||||
self.formLayout = QtGui.QFormLayout(newChanDialog)
|
||||
self.formLayout.setObjectName(_fromUtf8("formLayout"))
|
||||
self.radioButtonCreateChan = QtGui.QRadioButton(newChanDialog)
|
||||
self.radioButtonCreateChan.setObjectName(_fromUtf8("radioButtonCreateChan"))
|
||||
self.formLayout.setWidget(0, QtGui.QFormLayout.LabelRole, self.radioButtonCreateChan)
|
||||
self.radioButtonJoinChan = QtGui.QRadioButton(newChanDialog)
|
||||
self.radioButtonJoinChan.setChecked(True)
|
||||
self.radioButtonJoinChan.setObjectName(_fromUtf8("radioButtonJoinChan"))
|
||||
self.formLayout.setWidget(1, QtGui.QFormLayout.LabelRole, self.radioButtonJoinChan)
|
||||
self.groupBoxCreateChan = QtGui.QGroupBox(newChanDialog)
|
||||
self.groupBoxCreateChan.setObjectName(_fromUtf8("groupBoxCreateChan"))
|
||||
self.gridLayout = QtGui.QGridLayout(self.groupBoxCreateChan)
|
||||
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
|
||||
self.label_4 = QtGui.QLabel(self.groupBoxCreateChan)
|
||||
self.label_4.setWordWrap(True)
|
||||
self.label_4.setObjectName(_fromUtf8("label_4"))
|
||||
self.gridLayout.addWidget(self.label_4, 0, 0, 1, 1)
|
||||
self.label_5 = QtGui.QLabel(self.groupBoxCreateChan)
|
||||
self.label_5.setObjectName(_fromUtf8("label_5"))
|
||||
self.gridLayout.addWidget(self.label_5, 1, 0, 1, 1)
|
||||
self.lineEditChanNameCreate = QtGui.QLineEdit(self.groupBoxCreateChan)
|
||||
self.lineEditChanNameCreate.setObjectName(_fromUtf8("lineEditChanNameCreate"))
|
||||
self.gridLayout.addWidget(self.lineEditChanNameCreate, 2, 0, 1, 1)
|
||||
self.formLayout.setWidget(2, QtGui.QFormLayout.SpanningRole, self.groupBoxCreateChan)
|
||||
self.groupBoxJoinChan = QtGui.QGroupBox(newChanDialog)
|
||||
self.groupBoxJoinChan.setObjectName(_fromUtf8("groupBoxJoinChan"))
|
||||
self.gridLayout_2 = QtGui.QGridLayout(self.groupBoxJoinChan)
|
||||
self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))
|
||||
self.label = QtGui.QLabel(self.groupBoxJoinChan)
|
||||
self.label.setWordWrap(True)
|
||||
self.label.setObjectName(_fromUtf8("label"))
|
||||
self.gridLayout_2.addWidget(self.label, 0, 0, 1, 1)
|
||||
self.label_2 = QtGui.QLabel(self.groupBoxJoinChan)
|
||||
self.label_2.setObjectName(_fromUtf8("label_2"))
|
||||
self.gridLayout_2.addWidget(self.label_2, 1, 0, 1, 1)
|
||||
self.lineEditChanNameJoin = QtGui.QLineEdit(self.groupBoxJoinChan)
|
||||
self.lineEditChanNameJoin.setObjectName(_fromUtf8("lineEditChanNameJoin"))
|
||||
self.gridLayout_2.addWidget(self.lineEditChanNameJoin, 2, 0, 1, 1)
|
||||
self.label_3 = QtGui.QLabel(self.groupBoxJoinChan)
|
||||
self.label_3.setObjectName(_fromUtf8("label_3"))
|
||||
self.gridLayout_2.addWidget(self.label_3, 3, 0, 1, 1)
|
||||
self.lineEditChanBitmessageAddress = QtGui.QLineEdit(self.groupBoxJoinChan)
|
||||
self.lineEditChanBitmessageAddress.setObjectName(_fromUtf8("lineEditChanBitmessageAddress"))
|
||||
self.gridLayout_2.addWidget(self.lineEditChanBitmessageAddress, 4, 0, 1, 1)
|
||||
self.formLayout.setWidget(3, QtGui.QFormLayout.SpanningRole, self.groupBoxJoinChan)
|
||||
spacerItem = QtGui.QSpacerItem(389, 2, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
|
||||
self.formLayout.setItem(4, QtGui.QFormLayout.FieldRole, spacerItem)
|
||||
self.buttonBox = QtGui.QDialogButtonBox(newChanDialog)
|
||||
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
|
||||
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
|
||||
self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
|
||||
self.formLayout.setWidget(5, QtGui.QFormLayout.FieldRole, self.buttonBox)
|
||||
|
||||
self.retranslateUi(newChanDialog)
|
||||
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), newChanDialog.accept)
|
||||
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), newChanDialog.reject)
|
||||
QtCore.QObject.connect(self.radioButtonJoinChan, QtCore.SIGNAL(_fromUtf8("toggled(bool)")), self.groupBoxJoinChan.setShown)
|
||||
QtCore.QObject.connect(self.radioButtonCreateChan, QtCore.SIGNAL(_fromUtf8("toggled(bool)")), self.groupBoxCreateChan.setShown)
|
||||
QtCore.QMetaObject.connectSlotsByName(newChanDialog)
|
||||
newChanDialog.setTabOrder(self.radioButtonJoinChan, self.radioButtonCreateChan)
|
||||
newChanDialog.setTabOrder(self.radioButtonCreateChan, self.lineEditChanNameCreate)
|
||||
newChanDialog.setTabOrder(self.lineEditChanNameCreate, self.lineEditChanNameJoin)
|
||||
newChanDialog.setTabOrder(self.lineEditChanNameJoin, self.lineEditChanBitmessageAddress)
|
||||
newChanDialog.setTabOrder(self.lineEditChanBitmessageAddress, self.buttonBox)
|
||||
|
||||
def retranslateUi(self, newChanDialog):
|
||||
newChanDialog.setWindowTitle(_translate("newChanDialog", "Dialog", None))
|
||||
self.radioButtonCreateChan.setText(_translate("newChanDialog", "Create a new chan", None))
|
||||
self.radioButtonJoinChan.setText(_translate("newChanDialog", "Join a chan", None))
|
||||
self.groupBoxCreateChan.setTitle(_translate("newChanDialog", "Create a chan", None))
|
||||
self.label_4.setText(_translate("newChanDialog", "<html><head/><body><p>Enter a name for your chan. If you choose a sufficiently complex chan name (like a strong and unique passphrase) and none of your friends share it publicly then the chan will be secure and private. If you and someone else both create a chan with the same chan name then it is currently very likely that they will be the same chan.</p></body></html>", None))
|
||||
self.label_5.setText(_translate("newChanDialog", "Chan name:", None))
|
||||
self.groupBoxJoinChan.setTitle(_translate("newChanDialog", "Join a chan", None))
|
||||
self.label.setText(_translate("newChanDialog", "<html><head/><body><p>A chan exists when a group of people share the same decryption keys. The keys and bitmessage address used by a chan are generated from a human-friendly word or phrase (the chan name). To send a message to everyone in the chan, send a normal person-to-person message to the chan address.</p><p>Chans are experimental and completely unmoderatable.</p></body></html>", None))
|
||||
self.label_2.setText(_translate("newChanDialog", "Chan name:", None))
|
||||
self.label_3.setText(_translate("newChanDialog", "Chan bitmessage address:", None))
|
||||
|
||||
from addresses import addBMIfNotPresent
|
||||
from addressvalidator import AddressValidator, PassPhraseValidator
|
||||
from shared import apiAddressGeneratorReturnQueue, addressGeneratorQueue, UISignalQueue
|
||||
from retranslateui import RetranslateMixin
|
||||
from tr import _translate
|
||||
from utils import str_chan
|
||||
import widgets
|
||||
|
||||
class NewChanDialog(QtGui.QDialog, RetranslateMixin):
|
||||
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))
|
||||
QtCore.QObject.connect(self.chanAddress, QtCore.SIGNAL('textEdited()'), self.chanAddress.validator(), QtCore.SLOT('checkData(self)'))
|
||||
QtCore.QObject.connect(self.chanPassPhrase, QtCore.SIGNAL('textEdited()'), self.chanPassPhrase.validator(), QtCore.SLOT('checkData(self)'))
|
||||
|
||||
self.timer = QtCore.QTimer()
|
||||
QtCore.QObject.connect(self.timer, QtCore.SIGNAL("timeout()"), self.delayedUpdateStatus)
|
||||
self.timer.start(500) # milliseconds
|
||||
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
|
||||
self.show()
|
||||
|
||||
def delayedUpdateStatus(self):
|
||||
self.chanPassPhrase.validator().checkQueue()
|
||||
|
||||
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))
|
||||
else:
|
||||
addressGeneratorQueue.put(('joinChan', addBMIfNotPresent(self.chanAddress.text().toUtf8()), str_chan + ' ' + str(self.chanPassPhrase.text().toUtf8()), self.chanPassPhrase.text().toUtf8(), True))
|
||||
addressGeneratorReturnValue = apiAddressGeneratorReturnQueue.get(True)
|
||||
if len(addressGeneratorReturnValue) > 0 and addressGeneratorReturnValue[0] != 'chan name does not match address':
|
||||
UISignalQueue.put(('updateStatusBar', _translate("newchandialog", "Successfully created / joined chan %1").arg(str(self.chanPassPhrase.text().toUtf8()))))
|
||||
self.parent.ui.tabWidget.setCurrentIndex(3)
|
||||
self.parent.rerenderAddressBook()
|
||||
self.done(QtGui.QDialog.Accepted)
|
||||
else:
|
||||
UISignalQueue.put(('updateStatusBar', _translate("newchandialog", "Chan creation / joining failed")))
|
||||
self.done(QtGui.QDialog.Rejected)
|
||||
|
||||
def reject(self):
|
||||
self.timer.stop()
|
||||
self.hide()
|
||||
UISignalQueue.put(('updateStatusBar', _translate("newchandialog", "Chan creation / joining cancelled")))
|
||||
self.done(QtGui.QDialog.Rejected)
|
||||
|
Loading…
Reference in New Issue