abc92feae7ad6f1b44dab7eb4792742e1a9d2459
[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 electrum.plugins import run_hook
38
39 from util import MyTreeWidget
40
41 class TxDialog(QDialog):
42
43     def __init__(self, tx, parent):
44         self.tx = tx
45         tx_dict = tx.as_dict()
46         self.parent = parent
47         self.wallet = parent.wallet
48             
49         QDialog.__init__(self)
50         self.setMinimumWidth(600)
51         self.setWindowTitle(_("Transaction"))
52         self.setModal(1)
53
54         vbox = QVBoxLayout()
55         self.setLayout(vbox)
56
57         vbox.addWidget(QLabel(_("Transaction ID:")))
58         self.tx_hash_e  = QLineEdit()
59         self.tx_hash_e.setReadOnly(True)
60         vbox.addWidget(self.tx_hash_e)
61         self.status_label = QLabel()
62         vbox.addWidget(self.status_label)
63
64         self.date_label = QLabel()
65         vbox.addWidget(self.date_label)
66         self.amount_label = QLabel()
67         vbox.addWidget(self.amount_label)
68         self.fee_label = QLabel()
69         vbox.addWidget(self.fee_label)
70
71         self.add_io(vbox)
72
73         vbox.addStretch(1)
74
75         buttons = QHBoxLayout()
76         vbox.addLayout( buttons )
77
78         buttons.addStretch(1)
79
80         self.sign_button = b = QPushButton(_("Sign"))
81         b.clicked.connect(self.sign)
82         buttons.addWidget(b)
83
84         self.broadcast_button = b = QPushButton(_("Broadcast"))
85         b.clicked.connect(lambda: self.parent.broadcast_transaction(self.tx))
86
87         b.hide()
88         buttons.addWidget(b)
89
90         self.save_button = b = QPushButton(_("Save"))
91         b.clicked.connect(self.save)
92         buttons.addWidget(b)
93
94         cancelButton = QPushButton(_("Close"))
95         cancelButton.clicked.connect(lambda: self.done(0))
96         buttons.addWidget(cancelButton)
97         cancelButton.setDefault(True)
98
99         run_hook('init_transaction_dialog', self, buttons)
100         
101         self.update()
102
103
104
105
106     def sign(self):
107         tx_dict = self.tx.as_dict()
108         input_info = json.loads(tx_dict["input_info"])
109         self.parent.sign_raw_transaction(self.tx, input_info)
110         self.update()
111
112
113     def save(self):
114         name = 'signed_%s.txn' % (self.tx.hash()[0:8]) if self.tx.is_complete() else 'unsigned.txn'
115         fileName = self.parent.getSaveFileName(_("Select where to save your signed transaction"), name, "*.txn")
116         if fileName:
117             with open(fileName, "w+") as f:
118                 f.write(json.dumps(self.tx.as_dict(),indent=4) + '\n')
119             self.show_message(_("Transaction saved successfully"))
120
121
122
123     def update(self):
124
125         is_relevant, is_mine, v, fee = self.wallet.get_tx_value(self.tx)
126
127         if self.tx.is_complete():
128             status = _("Status: Signed")
129             self.sign_button.hide()
130             tx_hash = self.tx.hash()
131
132             if tx_hash in self.wallet.transactions.keys():
133                 conf, timestamp = self.wallet.verifier.get_confirmations(tx_hash)
134                 if timestamp:
135                     time_str = datetime.datetime.fromtimestamp(timestamp).isoformat(' ')[:-3]
136                 else:
137                     time_str = 'pending'
138                 status = _("Status: %d confirmations")%conf
139                 self.broadcast_button.hide()
140             else:
141                 time_str = None
142                 conf = 0
143                 self.broadcast_button.show()
144         else:
145             status = _("Status: Unsigned")
146             time_str = None
147             if not self.wallet.is_watching_only():
148                 self.sign_button.show()
149             else:
150                 self.sign_button.hide()
151             self.broadcast_button.hide()
152             tx_hash = 'unknown'
153
154         self.tx_hash_e.setText(tx_hash)
155         self.status_label.setText(status)
156
157         if time_str is not None:
158             self.date_label.setText(_("Date: %s")%time_str)
159             self.date_label.show()
160         else:
161             self.date_label.hide()
162
163
164         # if we are not synchronized, we cannot tell
165         if self.parent.network is None or not self.parent.network.is_running() or not self.parent.network.is_connected():
166             return
167         if not self.wallet.up_to_date:
168             return
169
170         if is_relevant:    
171             if is_mine:
172                 if fee is not None: 
173                     self.amount_label.setText(_("Amount sent:")+' %s'% self.parent.format_amount(v-fee) + ' ' + self.parent.base_unit())
174                     self.fee_label.setText(_("Transaction fee")+': %s'% self.parent.format_amount(fee) + ' ' + self.parent.base_unit())
175                 else:
176                     self.amount_label.setText(_("Amount sent:")+' %s'% self.parent.format_amount(v) + ' ' + self.parent.base_unit())
177                     self.fee_label.setText(_("Transaction fee")+': '+ _("unknown"))
178             else:
179                 self.amount_label.setText(_("Amount received:")+' %s'% self.parent.format_amount(v) + ' ' + self.parent.base_unit())
180         else:
181             self.amount_label.setText(_("Transaction unrelated to your wallet"))
182
183
184     def exec_menu(self, position,l):
185         item = l.itemAt(position)
186         if not item: return
187         addr = unicode(item.text(0))
188         menu = QMenu()
189         menu.addAction(_("Copy to clipboard"), lambda: self.parent.app.clipboard().setText(addr))
190         menu.exec_(l.viewport().mapToGlobal(position))
191
192
193     def add_io(self, vbox):
194
195         if self.tx.locktime > 0:
196             vbox.addWidget(QLabel("LockTime: %d\n" % self.tx.locktime))
197
198         vbox.addWidget(QLabel(_("Inputs")))
199         def format_input(x):
200             if x.get('is_coinbase'):
201                 return 'coinbase'
202             else:
203                 _hash = x.get('prevout_hash')
204                 return _hash[0:16] + '...' + _hash[-8:] + ":%d"%x.get('prevout_n') + u'\t' + "%s"%x.get('address')
205         lines = map(format_input, self.tx.inputs )
206         i_text = QTextEdit()
207         i_text.setText('\n'.join(lines))
208         i_text.setReadOnly(True)
209         i_text.setMaximumHeight(100)
210         vbox.addWidget(i_text)
211
212         vbox.addWidget(QLabel(_("Outputs")))
213         lines = map(lambda x: x[0] + u'\t\t' + self.parent.format_amount(x[1]), self.tx.outputs)
214         o_text = QTextEdit()
215         o_text.setText('\n'.join(lines))
216         o_text.setReadOnly(True)
217         o_text.setMaximumHeight(100)
218         vbox.addWidget(o_text)
219
220         
221
222     def show_message(self, msg):
223         QMessageBox.information(self, _('Message'), msg, _('OK'))
224
225
226
227