/ src / bitmessageqt / addressvalidator.py.bak
addressvalidator.py.bak
  1  """
  2  Address validator module.
  3  """
  4  # pylint: disable=too-many-branches,too-many-arguments
  5  
  6  from Queue import Empty
  7  
  8  from PyQt4 import QtGui
  9  
 10  from addresses import decodeAddress, addBMIfNotPresent
 11  from bmconfigparser import config
 12  from queues import apiAddressGeneratorReturnQueue, addressGeneratorQueue
 13  from tr import _translate
 14  from utils import str_chan
 15  
 16  
 17  class AddressPassPhraseValidatorMixin(object):
 18      """Bitmessage address or passphrase validator class for Qt UI"""
 19      def setParams(
 20              self,
 21              passPhraseObject=None,
 22              addressObject=None,
 23              feedBackObject=None,
 24              buttonBox=None,
 25              addressMandatory=True,
 26      ):
 27          """Initialisation"""
 28          self.addressObject = addressObject
 29          self.passPhraseObject = passPhraseObject
 30          self.feedBackObject = feedBackObject
 31          self.buttonBox = buttonBox
 32          self.addressMandatory = addressMandatory
 33          self.isValid = False
 34          # save default text
 35          self.okButtonLabel = self.buttonBox.button(QtGui.QDialogButtonBox.Ok).text()
 36  
 37      def setError(self, string):
 38          """Indicate that the validation is pending or failed"""
 39          if string is not None and self.feedBackObject is not None:
 40              font = QtGui.QFont()
 41              font.setBold(True)
 42              self.feedBackObject.setFont(font)
 43              self.feedBackObject.setStyleSheet("QLabel { color : red; }")
 44              self.feedBackObject.setText(string)
 45          self.isValid = False
 46          if self.buttonBox:
 47              self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setEnabled(False)
 48              if string is not None and self.feedBackObject is not None:
 49                  self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setText(
 50                      _translate("AddressValidator", "Invalid"))
 51              else:
 52                  self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setText(
 53                      _translate("AddressValidator", "Validating..."))
 54  
 55      def setOK(self, string):
 56          """Indicate that the validation succeeded"""
 57          if string is not None and self.feedBackObject is not None:
 58              font = QtGui.QFont()
 59              font.setBold(False)
 60              self.feedBackObject.setFont(font)
 61              self.feedBackObject.setStyleSheet("QLabel { }")
 62              self.feedBackObject.setText(string)
 63          self.isValid = True
 64          if self.buttonBox:
 65              self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setEnabled(True)
 66              self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setText(self.okButtonLabel)
 67  
 68      def checkQueue(self):
 69          """Validator queue loop"""
 70          gotOne = False
 71  
 72          # wait until processing is done
 73          if not addressGeneratorQueue.empty():
 74              self.setError(None)
 75              return None
 76  
 77          while True:
 78              try:
 79                  addressGeneratorReturnValue = apiAddressGeneratorReturnQueue.get(False)
 80              except Empty:
 81                  if gotOne:
 82                      break
 83                  else:
 84                      return None
 85              else:
 86                  gotOne = True
 87  
 88          if not addressGeneratorReturnValue:
 89              self.setError(_translate("AddressValidator", "Address already present as one of your identities."))
 90              return (QtGui.QValidator.Intermediate, 0)
 91          if addressGeneratorReturnValue[0] == 'chan name does not match address':
 92              self.setError(
 93                  _translate(
 94                      "AddressValidator",
 95                      "Although the Bitmessage address you "
 96                      "entered was valid, it doesn't match the chan name."))
 97              return (QtGui.QValidator.Intermediate, 0)
 98          self.setOK(_translate("MainWindow", "Passphrase and address appear to be valid."))
 99  
100      def returnValid(self):
101          """Return the value of whether the validation was successful"""
102          if self.isValid:
103              return QtGui.QValidator.Acceptable
104          return QtGui.QValidator.Intermediate
105  
106      def validate(self, s, pos):
107          """Top level validator method"""
108          if self.addressObject is None:
109              address = None
110          else:
111              address = str(self.addressObject.text().toUtf8())
112              if address == "":
113                  address = None
114          if self.passPhraseObject is None:
115              passPhrase = ""
116          else:
117              passPhrase = str(self.passPhraseObject.text().toUtf8())
118              if passPhrase == "":
119                  passPhrase = None
120  
121          # no chan name
122          if passPhrase is None:
123              self.setError(_translate("AddressValidator", "Chan name/passphrase needed. You didn't enter a chan name."))
124              return (QtGui.QValidator.Intermediate, pos)
125  
126          if self.addressMandatory or address is not None:
127              # check if address already exists:
128              if address in config.addresses():
129                  self.setError(_translate("AddressValidator", "Address already present as one of your identities."))
130                  return (QtGui.QValidator.Intermediate, pos)
131  
132              # version too high
133              if decodeAddress(address)[0] == 'versiontoohigh':
134                  self.setError(
135                      _translate(
136                          "AddressValidator",
137                          "Address too new. Although that Bitmessage"
138                          " address might be valid, its version number"
139                          " is too new for us to handle. Perhaps you need"
140                          " to upgrade Bitmessage."))
141                  return (QtGui.QValidator.Intermediate, pos)
142  
143              # invalid
144              if decodeAddress(address)[0] != 'success':
145                  self.setError(_translate("AddressValidator", "The Bitmessage address is not valid."))
146                  return (QtGui.QValidator.Intermediate, pos)
147  
148          # this just disables the OK button without changing the feedback text
149          # but only if triggered by textEdited, not by clicking the Ok button
150          if not self.buttonBox.button(QtGui.QDialogButtonBox.Ok).hasFocus():
151              self.setError(None)
152  
153          # check through generator
154          if address is None:
155              addressGeneratorQueue.put(('createChan', 4, 1, str_chan + ' ' + str(passPhrase), passPhrase, False))
156          else:
157              addressGeneratorQueue.put(
158                  ('joinChan', addBMIfNotPresent(address),
159                   "{} {}".format(str_chan, passPhrase), passPhrase, False))
160  
161          if self.buttonBox.button(QtGui.QDialogButtonBox.Ok).hasFocus():
162              return (self.returnValid(), pos)
163          return (QtGui.QValidator.Intermediate, pos)
164  
165      def checkData(self):
166          """Validator Qt signal interface"""
167          return self.validate("", 0)
168  
169  
170  class AddressValidator(QtGui.QValidator, AddressPassPhraseValidatorMixin):
171      """AddressValidator class for Qt UI"""
172      def __init__(self, parent=None, passPhraseObject=None, feedBackObject=None, buttonBox=None, addressMandatory=True):
173          super(AddressValidator, self).__init__(parent)
174          self.setParams(passPhraseObject, parent, feedBackObject, buttonBox, addressMandatory)
175  
176  
177  class PassPhraseValidator(QtGui.QValidator, AddressPassPhraseValidatorMixin):
178      """PassPhraseValidator class for Qt UI"""
179      def __init__(self, parent=None, addressObject=None, feedBackObject=None, buttonBox=None, addressMandatory=False):
180          super(PassPhraseValidator, self).__init__(parent)
181          self.setParams(parent, addressObject, feedBackObject, buttonBox, addressMandatory)