update calls to is_complete() method. fixes #693
[electrum-nvc.git] / gui / qt / transaction_dialog.py
1 #!/usr/bin/env python
2 #
3 # Electrum - lightweight Bitcoin client
4 # Copyright (C) 2012 thomasv@gitorious
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 import sys, time, datetime, re, threading
20 from electrum.i18n import _, set_language
21 from electrum.util import print_error, print_msg
22 import os.path, json, ast, traceback
23 import shutil
24 import StringIO
25
26
27 try:
28     import PyQt4
29 except Exception:
30     sys.exit("Error: Could not import PyQt4 on Linux systems, you may try 'sudo apt-get install python-qt4'")
31
32 from PyQt4.QtGui import *
33 from PyQt4.QtCore import *
34 import PyQt4.QtCore as QtCore
35
36 from electrum import transaction
37 from util import MyTreeWidget
38
39 class TxDialog(QDialog):
40
41     def __init__(self, tx, parent):
42         self.tx = tx
43         tx_dict = tx.as_dict()
44         self.parent = parent
45         self.wallet = parent.wallet
46             
47         QDialog.__init__(self)
48         self.setMinimumWidth(600)
49         self.setWindowTitle(_("Transaction"))
50         self.setModal(1)
51
52         vbox = QVBoxLayout()
53         self.setLayout(vbox)
54
55         vbox.addWidget(QLabel(_("Transaction ID:")))
56         self.tx_hash_e  = QLineEdit()
57         self.tx_hash_e.setReadOnly(True)
58         vbox.addWidget(self.tx_hash_e)
59         self.status_label = QLabel()
60         vbox.addWidget(self.status_label)
61
62         self.date_label = QLabel()
63         vbox.addWidget(self.date_label)
64         self.amount_label = QLabel()
65         vbox.addWidget(self.amount_label)
66         self.fee_label = QLabel()
67         vbox.addWidget(self.fee_label)
68
69         self.add_io(vbox)
70
71         vbox.addStretch(1)
72
73         buttons = QHBoxLayout()
74         vbox.addLayout( buttons )
75
76         buttons.addStretch(1)
77
78         self.sign_button = b = QPushButton(_("Sign"))
79         b.clicked.connect(self.sign)
80         buttons.addWidget(b)
81
82         self.broadcast_button = b = QPushButton(_("Broadcast"))
83         b.clicked.connect(self.broadcast)
84         b.hide()
85         buttons.addWidget(b)
86
87         self.save_button = b = QPushButton(_("Save"))
88         b.clicked.connect(self.save)
89         buttons.addWidget(b)
90
91         cancelButton = QPushButton(_("Close"))
92         cancelButton.clicked.connect(lambda: self.done(0))
93         buttons.addWidget(cancelButton)
94         cancelButton.setDefault(True)
95         
96         self.update()
97
98
99
100
101     def sign(self):
102         tx_dict = self.tx.as_dict()
103         input_info = json.loads(tx_dict["input_info"])
104         self.parent.sign_raw_transaction(self.tx, input_info)
105         self.update()
106
107
108     def save(self):
109         name = 'signed_%s.txn' % (self.tx.hash()[0:8]) if self.tx.is_complete() else 'unsigned.txn'
110         fileName = self.parent.getSaveFileName(_("Select where to save your signed transaction"), name, "*.txn")
111         if fileName:
112             with open(fileName, "w+") as f:
113                 f.write(json.dumps(self.tx.as_dict(),indent=4) + '\n')
114             self.show_message(_("Transaction saved successfully"))
115
116
117
118     def update(self):
119
120         is_relevant, is_mine, v, fee = self.wallet.get_tx_value(self.tx)
121
122         if self.tx.is_complete():
123             status = _("Status: Signed")
124             self.sign_button.hide()
125             tx_hash = self.tx.hash()
126
127             if tx_hash in self.wallet.transactions.keys():
128                 conf, timestamp = self.wallet.verifier.get_confirmations(tx_hash)
129                 if timestamp:
130                     time_str = datetime.datetime.fromtimestamp(timestamp).isoformat(' ')[:-3]
131                 else:
132                     time_str = 'pending'
133                 status = _("Status: %d confirmations")%conf
134                 self.broadcast_button.hide()
135             else:
136                 time_str = None
137                 conf = 0
138                 self.broadcast_button.show()
139         else:
140             status = _("Status: Unsigned")
141             time_str = None
142             if not self.wallet.is_watching_only():
143                 self.sign_button.show()
144             else:
145                 self.sign_button.hide()
146             self.broadcast_button.hide()
147             tx_hash = 'unknown'
148
149         self.tx_hash_e.setText(tx_hash)
150         self.status_label.setText(status)
151
152         if time_str is not None:
153             self.date_label.setText(_("Date: %s")%time_str)
154             self.date_label.show()
155         else:
156             self.date_label.hide()
157
158
159         # if we are not synchronized, we cannot tell
160         if self.parent.network is None or not self.parent.network.is_running() or not self.parent.network.is_connected():
161             return
162         if not self.wallet.up_to_date:
163             return
164
165         if is_relevant:    
166             if is_mine:
167                 if fee is not None: 
168                     self.amount_label.setText(_("Amount sent:")+' %s'% self.parent.format_amount(v-fee) + ' ' + self.parent.base_unit())
169                     self.fee_label.setText(_("Transaction fee")+': %s'% self.parent.format_amount(fee) + ' ' + self.parent.base_unit())
170                 else:
171                     self.amount_label.setText(_("Amount sent:")+' %s'% self.parent.format_amount(v) + ' ' + self.parent.base_unit())
172                     self.fee_label.setText(_("Transaction fee")+': '+ _("unknown"))
173             else:
174                 self.amount_label.setText(_("Amount received:")+' %s'% self.parent.format_amount(v) + ' ' + self.parent.base_unit())
175         else:
176             self.amount_label.setText(_("Transaction unrelated to your wallet"))
177
178
179     def exec_menu(self, position,l):
180         item = l.itemAt(position)
181         if not item: return
182         addr = unicode(item.text(0))
183         menu = QMenu()
184         menu.addAction(_("Copy to clipboard"), lambda: self.parent.app.clipboard().setText(addr))
185         menu.exec_(l.viewport().mapToGlobal(position))
186
187
188     def add_io(self, vbox):
189
190         if self.tx.locktime > 0:
191             vbox.addWidget(QLabel("LockTime: %d\n" % self.tx.locktime))
192
193         vbox.addWidget(QLabel(_("Inputs")))
194         def format_input(x):
195             if x.get('is_coinbase'):
196                 return 'coinbase'
197             else:
198                 _hash = x.get('prevout_hash')
199                 return _hash[0:16] + '...' + _hash[-8:] + ":%d"%x.get('prevout_n') + u'\t' + "%s"%x.get('address')
200         lines = map(format_input, self.tx.inputs )
201         i_text = QTextEdit()
202         i_text.setText('\n'.join(lines))
203         i_text.setReadOnly(True)
204         i_text.setMaximumHeight(100)
205         vbox.addWidget(i_text)
206
207         vbox.addWidget(QLabel(_("Outputs")))
208         lines = map(lambda x: x[0] + u'\t\t' + self.parent.format_amount(x[1]), self.tx.outputs)
209         o_text = QTextEdit()
210         o_text.setText('\n'.join(lines))
211         o_text.setReadOnly(True)
212         o_text.setMaximumHeight(100)
213         vbox.addWidget(o_text)
214
215         
216
217
218     def broadcast(self):
219         result, result_message = self.wallet.sendtx( self.tx )
220         if result:
221             self.show_message(_("Transaction successfully sent")+': %s' % (result_message))
222         else:
223             self.show_message(_("There was a problem sending your transaction:") + '\n %s' % (result_message))
224
225     def show_message(self, msg):
226         QMessageBox.information(self, _('Message'), msg, _('OK'))
227
228
229
230