don't set column width in update, but only in create
[electrum-nvc.git] / gui / qt / main_window.py
index 63013a5..174fbdf 100644 (file)
@@ -41,6 +41,8 @@ from electrum import mnemonic
 from electrum import util, bitcoin, commands, Interface, Wallet
 from electrum import SimpleConfig, Wallet, WalletStorage
 
+from electrum.paymentrequest import PR_UNPAID, PR_PAID
+
 
 from electrum import bmp, pyqrnative
 
@@ -69,8 +71,11 @@ import re
 from util import *
 
 
-
-
+def format_status(x):
+    if x == PR_UNPAID:
+        return _('Unpaid')
+    elif x == PR_PAID:
+        return _('Paid')
 
 
 class StatusBarButton(QPushButton):
@@ -336,6 +341,7 @@ class ElectrumWindow(QMainWindow):
         raw_transaction_menu.addAction(_("&From file"), self.do_process_from_file)
         raw_transaction_menu.addAction(_("&From text"), self.do_process_from_text)
         raw_transaction_menu.addAction(_("&From the blockchain"), self.do_process_from_txid)
+        self.raw_transaction_menu = raw_transaction_menu
 
         help_menu = menubar.addMenu(_("&Help"))
         help_menu.addAction(_("&About"), self.show_about)
@@ -794,11 +800,11 @@ class ElectrumWindow(QMainWindow):
         return lambda s, *args: s.do_protect(func, args)
 
 
-    def do_send(self):
+    def read_send_tab(self):
         label = unicode( self.message_e.text() )
 
         if self.gui_object.payment_request:
-            outputs = self.gui_object.payment_request.outputs
+            outputs = self.gui_object.payment_request.get_outputs()
         else:
             outputs = self.payto_e.get_outputs()
 
@@ -832,16 +838,23 @@ class ElectrumWindow(QMainWindow):
             if not self.question(_("The fee for this transaction seems unusually high.\nAre you really sure you want to pay %(fee)s in fees?")%{ 'fee' : self.format_amount(fee) + ' '+ self.base_unit()}):
                 return
 
-        self.send_tx(outputs, fee, label)
+        coins = self.get_coins()
+        return outputs, fee, label, coins
 
 
+    def do_send(self):
+        r = self.read_send_tab()
+        if not r:
+            return
+        outputs, fee, label, coins = r
+        self.send_tx(outputs, fee, label, coins)
+
 
     @protected
-    def send_tx(self, outputs, fee, label, password):
+    def send_tx(self, outputs, fee, label, coins, password):
         self.send_button.setDisabled(True)
 
         # first, create an unsigned tx 
-        coins = self.get_coins()
         try:
             tx = self.wallet.make_unsigned_transaction(outputs, fee, None, coins = coins)
             tx.error = None
@@ -890,12 +903,27 @@ class ElectrumWindow(QMainWindow):
     def broadcast_transaction(self, tx):
 
         def broadcast_thread():
-            if self.gui_object.payment_request:
-                refund_address = self.wallet.addresses()[0]
-                status, msg = self.gui_object.payment_request.send_ack(str(tx), refund_address)
+            pr = self.gui_object.payment_request
+            if pr is None:
+                return self.wallet.sendtx(tx)
+
+            if pr.has_expired():
                 self.gui_object.payment_request = None
-            else:
-                status, msg =  self.wallet.sendtx(tx)
+                return False, _("Payment request has expired")
+
+            status, msg =  self.wallet.sendtx(tx)
+            if not status:
+                return False, msg
+
+            self.invoices[pr.get_id()] = (pr.get_domain(), pr.get_memo(), pr.get_amount(), PR_PAID, tx.hash())
+            self.wallet.storage.put('invoices', self.invoices)
+            self.update_invoices_tab()
+            self.gui_object.payment_request = None
+            refund_address = self.wallet.addresses()[0]
+            ack_status, ack_msg = pr.send_ack(str(tx), refund_address)
+            if ack_status:
+                msg = ack_msg
+
             return status, msg
 
         def broadcast_done(status, msg):
@@ -924,10 +952,19 @@ class ElectrumWindow(QMainWindow):
     def payment_request_ok(self):
         pr = self.gui_object.payment_request
         pr_id = pr.get_id()
-        # save it
-        self.invoices[pr_id] = (pr.get_domain(), pr.get_memo(), pr.get_amount())
-        self.wallet.storage.put('invoices', self.invoices)
-        self.update_invoices_tab()
+        if pr_id not in self.invoices:
+            self.invoices[pr_id] = (pr.get_domain(), pr.get_memo(), pr.get_amount(), PR_UNPAID, None)
+            self.wallet.storage.put('invoices', self.invoices)
+            self.update_invoices_tab()
+        else:
+            print_error('invoice already in list')
+
+        status = self.invoices[pr_id][3]
+        if status == PR_PAID:
+            self.do_clear()
+            self.show_message("invoice already paid")
+            self.gui_object.payment_request = None
+            return
 
         self.payto_help.show()
         self.payto_help.set_alt(lambda: self.show_pr_details(pr))
@@ -935,7 +972,7 @@ class ElectrumWindow(QMainWindow):
         self.payto_e.setGreen()
         self.payto_e.setText(pr.domain)
         self.amount_e.setText(self.format_amount(pr.get_amount()))
-        self.message_e.setText(pr.memo)
+        self.message_e.setText(pr.get_memo())
 
     def payment_request_error(self):
         self.do_clear()
@@ -1008,6 +1045,8 @@ class ElectrumWindow(QMainWindow):
 
     def create_receive_tab(self):
         l, w = self.create_list_tab([ _('Address'), _('Label'), _('Balance'), _('Tx')])
+        for i,width in enumerate(self.column_widths['receive']):
+            l.setColumnWidth(i, width)
         l.setContextMenuPolicy(Qt.CustomContextMenu)
         l.customContextMenuRequested.connect(self.create_receive_menu)
         l.setSelectionMode(QAbstractItemView.ExtendedSelection)
@@ -1042,7 +1081,6 @@ class ElectrumWindow(QMainWindow):
         l.customContextMenuRequested.connect(self.create_contact_menu)
         for i,width in enumerate(self.column_widths['contacts']):
             l.setColumnWidth(i, width)
-
         self.connect(l, SIGNAL('itemDoubleClicked(QTreeWidgetItem*, int)'), lambda a, b: self.address_label_clicked(a,b,l,0,1))
         self.connect(l, SIGNAL('itemChanged(QTreeWidgetItem*, int)'), lambda a,b: self.address_label_changed(a,b,l,0,1))
         self.contacts_list = l
@@ -1065,11 +1103,11 @@ class ElectrumWindow(QMainWindow):
         l.clear()
         for key, value in invoices.items():
             try:
-                domain, memo, amount = value
+                domain, memo, amount, status, tx_hash = value
             except:
                 invoices.pop(key)
                 continue
-            item = QTreeWidgetItem( [ domain, memo, self.format_amount(amount), ""] )
+            item = QTreeWidgetItem( [ domain, memo, self.format_amount(amount), format_status(status)] )
             l.addTopLevelItem(item)
 
         l.setCurrentItem(l.topLevelItem(0))
@@ -1220,7 +1258,7 @@ class ElectrumWindow(QMainWindow):
 
     def show_invoice(self, key):
         from electrum.paymentrequest import PaymentRequest
-        domain, memo, value = self.invoices[key]
+        domain, memo, value, status, tx_hash = self.invoices[key]
         pr = PaymentRequest(self.config)
         pr.read_file(key)
         pr.domain = domain
@@ -1230,19 +1268,36 @@ class ElectrumWindow(QMainWindow):
     def show_pr_details(self, pr):
         msg = 'Domain: ' + pr.domain
         msg += '\nStatus: ' + pr.get_status()
-        msg += '\nMemo: ' + pr.memo
+        msg += '\nMemo: ' + pr.get_memo()
         msg += '\nPayment URL: ' + pr.payment_url
         msg += '\n\nOutputs:\n' + '\n'.join(map(lambda x: x[0] + ' ' + self.format_amount(x[1])+ self.base_unit(), pr.get_outputs()))
         QMessageBox.information(self, 'Invoice', msg , 'OK')
 
+    def do_pay_invoice(self, key):
+        from electrum.paymentrequest import PaymentRequest
+        domain, memo, value, status, tx_hash = self.invoices[key]
+        pr = PaymentRequest(self.config)
+        pr.read_file(key)
+        pr.domain = domain
+        self.gui_object.payment_request = pr
+        self.prepare_for_payment_request()
+        if pr.verify():
+            self.payment_request_ok()
+        else:
+            self.payment_request_error()
+            
+
     def create_invoice_menu(self, position):
         item = self.invoices_list.itemAt(position)
         if not item:
             return
         k = self.invoices_list.indexOfTopLevelItem(item)
         key = self.invoices.keys()[k]
+        domain, memo, value, status, tx_hash = self.invoices[key]
         menu = QMenu()
         menu.addAction(_("Details"), lambda: self.show_invoice(key))
+        if status == PR_UNPAID:
+            menu.addAction(_("Pay Now"), lambda: self.do_pay_invoice(key))
         menu.addAction(_("Delete"), lambda: self.delete_invoice(key))
         menu.exec_(self.invoices_list.viewport().mapToGlobal(position))
 
@@ -1273,8 +1328,6 @@ class ElectrumWindow(QMainWindow):
         l.insertChild = l.insertTopLevelItem
 
         l.clear()
-        for i,width in enumerate(self.column_widths['receive']):
-            l.setColumnWidth(i, width)
 
         accounts = self.wallet.get_accounts()
         if self.current_account is None: