/ src / bitmessageqt / newchandialog.py
newchandialog.py
 1  """
 2  src/bitmessageqt/newchandialog.py
 3  =================================
 4  
 5  """
 6  
 7  from PyQt4 import QtCore, QtGui
 8  
 9  from . import widgets
10  from addresses import addBMIfNotPresent
11  from .addressvalidator import AddressValidator, PassPhraseValidator
12  from queues import (
13      addressGeneratorQueue, apiAddressGeneratorReturnQueue, UISignalQueue)
14  from tr import _translate
15  from .utils import str_chan
16  
17  
18  class NewChanDialog(QtGui.QDialog):
19      """The `New Chan` dialog"""
20      def __init__(self, parent=None):
21          super(NewChanDialog, self).__init__(parent)
22          widgets.load('newchandialog.ui', self)
23          self.parent = parent
24          self.chanAddress.setValidator(
25              AddressValidator(
26                  self.chanAddress,
27                  self.chanPassPhrase,
28                  self.validatorFeedback,
29                  self.buttonBox,
30                  False))
31          self.chanPassPhrase.setValidator(
32              PassPhraseValidator(
33                  self.chanPassPhrase,
34                  self.chanAddress,
35                  self.validatorFeedback,
36                  self.buttonBox,
37                  False))
38  
39          self.timer = QtCore.QTimer()
40          QtCore.QObject.connect(  # pylint: disable=no-member
41              self.timer, QtCore.SIGNAL("timeout()"), self.delayedUpdateStatus)
42          self.timer.start(500)  # milliseconds
43          self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
44          self.show()
45  
46      def delayedUpdateStatus(self):
47          """Related to updating the UI for the chan passphrase validity"""
48          self.chanPassPhrase.validator().checkQueue()
49  
50      def accept(self):
51          """Proceed in joining the chan"""
52          self.timer.stop()
53          self.hide()
54          apiAddressGeneratorReturnQueue.queue.clear()
55          if self.chanAddress.text().toUtf8() == "":
56              addressGeneratorQueue.put(
57                  ('createChan', 4, 1, str_chan + ' ' + str(self.chanPassPhrase.text().toUtf8()),
58                   self.chanPassPhrase.text().toUtf8(),
59                   True))
60          else:
61              addressGeneratorQueue.put(
62                  ('joinChan', addBMIfNotPresent(self.chanAddress.text().toUtf8()),
63                   str_chan + ' ' + str(self.chanPassPhrase.text().toUtf8()),
64                   self.chanPassPhrase.text().toUtf8(),
65                   True))
66          addressGeneratorReturnValue = apiAddressGeneratorReturnQueue.get(True)
67          if addressGeneratorReturnValue and addressGeneratorReturnValue[0] != 'chan name does not match address':
68              UISignalQueue.put(('updateStatusBar', _translate(
69                  "newchandialog", "Successfully created / joined chan %1").arg(str(self.chanPassPhrase.text()))))
70              self.parent.ui.tabWidget.setCurrentIndex(
71                  self.parent.ui.tabWidget.indexOf(self.parent.ui.chans)
72              )
73              self.done(QtGui.QDialog.Accepted)
74          else:
75              UISignalQueue.put(('updateStatusBar', _translate("newchandialog", "Chan creation / joining failed")))
76              self.done(QtGui.QDialog.Rejected)
77  
78      def reject(self):
79          """Cancel joining the chan"""
80          self.timer.stop()
81          self.hide()
82          UISignalQueue.put(('updateStatusBar', _translate("newchandialog", "Chan creation / joining cancelled")))
83          self.done(QtGui.QDialog.Rejected)