Skip to content

Commit

Permalink
WaitingDialog class for better encapsulation
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasV committed May 10, 2014
1 parent 9e2db24 commit 6665a70
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 47 deletions.
76 changes: 29 additions & 47 deletions gui/qt/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,6 @@ def __init__(self, config, network, gui_object):
self.connect(self, QtCore.SIGNAL('update_status'), self.update_status)
self.connect(self, QtCore.SIGNAL('banner_signal'), lambda: self.console.showMessage(self.network.banner) )
self.connect(self, QtCore.SIGNAL('transaction_signal'), lambda: self.notify_transactions() )
self.connect(self, QtCore.SIGNAL('send_tx2'), self.send_tx2)
self.connect(self, QtCore.SIGNAL('send_tx3'), self.send_tx3)
self.connect(self, QtCore.SIGNAL('payment_request_ok'), self.payment_request_ok)
self.connect(self, QtCore.SIGNAL('payment_request_error'), self.payment_request_error)

Expand Down Expand Up @@ -814,15 +812,6 @@ def do_send(self):
self.send_tx(outputs, fee, label)


def waiting_dialog(self, message):
d = QDialog(self)
d.setWindowTitle('Please wait')
l = QLabel(message)
vbox = QVBoxLayout(d)
vbox.addWidget(l)
d.show()
return d


@protected
def send_tx(self, outputs, fee, label, password):
Expand All @@ -846,55 +835,48 @@ def sign_thread():
keypairs = {}
self.wallet.add_keypairs_from_wallet(tx, keypairs, password)
self.wallet.sign_transaction(tx, keypairs, password)
self.signed_tx_data = (tx, fee, label)
self.emit(SIGNAL('send_tx2'))
self.tx_wait_dialog = self.waiting_dialog('Signing..')
threading.Thread(target=sign_thread).start()
return tx, fee, label

def sign_done(tx, fee, label):
if tx.error:
self.show_message(tx.error)
return
if tx.requires_fee(self.wallet.verifier) and fee < MIN_RELAY_TX_FEE:
QMessageBox.warning(self, _('Error'), _("This transaction requires a higher fee, or it will not be propagated by the network."), _('OK'))
return
if label:
self.wallet.set_label(tx.hash(), label)

if not self.gui_object.payment_request:
if not tx.is_complete() or self.config.get('show_before_broadcast'):
self.show_transaction(tx)
return

def send_tx2(self):
tx, fee, label = self.signed_tx_data
self.tx_wait_dialog.accept()

if tx.error:
self.show_message(tx.error)
return
self.broadcast_transaction(tx)

if tx.requires_fee(self.wallet.verifier) and fee < MIN_RELAY_TX_FEE:
QMessageBox.warning(self, _('Error'), _("This transaction requires a higher fee, or it will not be propagated by the network."), _('OK'))
return
WaitingDialog(self, 'Signing..').start(sign_thread, sign_done)

if label:
self.wallet.set_label(tx.hash(), label)

if not tx.is_complete() or self.config.get('show_before_broadcast'):
self.show_transaction(tx)
return

def broadcast_transaction(self, tx):

def broadcast_thread():
if self.gui_object.payment_request:
print "sending ack"
refund_address = self.wallet.addresses()[0]
self.gui_object.payment_request.send_ack(str(tx), refund_address)
status, msg = self.gui_object.payment_request.send_ack(str(tx), refund_address)
self.gui_object.payment_request = None
# note: BIP 70 recommends not broadcasting the tx to the network and letting the merchant do that
self.tx_broadcast_result = self.wallet.sendtx(tx)
self.emit(SIGNAL('send_tx3'))

self.tx_broadcast_dialog = self.waiting_dialog('Broadcasting..')
threading.Thread(target=broadcast_thread).start()

else:
status, msg = self.wallet.sendtx(tx)
return status, msg

def broadcast_done(status, msg):
if status:
QMessageBox.information(self, '', _('Payment sent.') + '\n' + msg, _('OK'))
self.do_clear()
else:
QMessageBox.warning(self, _('Error'), msg, _('OK'))

def send_tx3(self):
self.tx_broadcast_dialog.accept()
status, msg = self.tx_broadcast_result
if status:
QMessageBox.information(self, '', _('Payment sent.') + '\n' + msg, _('OK'))
self.do_clear()
else:
QMessageBox.warning(self, _('Error'), msg, _('OK'))
WaitingDialog(self, 'Broadcasting..').start(broadcast_thread, broadcast_done)



Expand Down
23 changes: 23 additions & 0 deletions gui/qt/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@
import os.path
import time

import threading

class WaitingDialog(QDialog):
def __init__(self, parent, message):
QDialog.__init__(self, parent)
self.setWindowTitle('Please wait')
l = QLabel(message)
vbox = QVBoxLayout(self)
vbox.addWidget(l)
self.show()

def start(self, run_thread, on_complete=None):
def my_thread():
self.result = run_thread()
self.emit(SIGNAL('done'))
self.accept()

if on_complete:
self.connect(self, SIGNAL('done'), lambda: on_complete(*self.result))

threading.Thread(target=my_thread).start()



class Timer(QThread):
def run(self):
Expand Down

0 comments on commit 6665a70

Please sign in to comment.