parse payto text
[electrum-nvc.git] / gui / qt / main_window.py
index d7ce7cc..486dca5 100644 (file)
@@ -29,7 +29,6 @@ import PyQt4
 from PyQt4.QtGui import *
 from PyQt4.QtCore import *
 import PyQt4.QtCore as QtCore
-print PyQt4.QtCore.PYQT_VERSION_STR
 
 from electrum.bitcoin import MIN_RELAY_TX_FEE, is_valid
 from electrum.plugins import run_hook
@@ -188,10 +187,12 @@ class ElectrumWindow(QMainWindow):
 
     def load_wallet(self, wallet):
         import electrum
+
         self.wallet = wallet
+        self.update_wallet_format()
+
         self.accounts_expanded = self.wallet.storage.get('accounts_expanded',{})
         self.current_account = self.wallet.storage.get("current_account", None)
-
         title = 'Electrum ' + self.wallet.electrum_version + '  -  ' + self.wallet.storage.path
         if self.wallet.is_watching_only(): title += ' [%s]' % (_('watching only'))
         self.setWindowTitle( title )
@@ -214,6 +215,16 @@ class ElectrumWindow(QMainWindow):
         run_hook('load_wallet', wallet)
 
 
+    def update_wallet_format(self):
+        # convert old-format imported keys
+        if self.wallet.imported_keys:
+            password = self.password_dialog(_("Please enter your password in order to update imported keys"))
+            try:
+                self.wallet.convert_imported_keys(password)
+            except:
+                self.show_message("error")
+
+
     def open_wallet(self):
         wallet_folder = self.wallet.storage.path
         filename = unicode( QFileDialog.getOpenFileName(self, "Select your wallet file", wallet_folder) )
@@ -406,10 +417,10 @@ class ElectrumWindow(QMainWindow):
     def format_amount(self, x, is_diff=False, whitespaces=False):
         return format_satoshis(x, is_diff, self.num_zeros, self.decimal_point, whitespaces)
 
-    def read_amount(self, x):
-        if x in['.', '']: return None
-        p = pow(10, self.decimal_point)
-        return int( p * Decimal(x) )
+
+    def get_decimal_point(self):
+        return self.decimal_point
+
 
     def base_unit(self):
         assert self.decimal_point in [5,8]
@@ -626,13 +637,15 @@ class ElectrumWindow(QMainWindow):
     def create_send_tab(self):
         w = QWidget()
 
-        grid = QGridLayout()
+        grid = QGridLayout(w)
         grid.setSpacing(8)
         grid.setColumnMinimumWidth(3,300)
         grid.setColumnStretch(5,1)
+        grid.setRowStretch(8, 1)
 
-
-        self.payto_e = QLineEdit()
+        from paytoedit import PayToEdit
+        self.amount_e = AmountEdit(self.get_decimal_point)
+        self.payto_e = PayToEdit(self.amount_e)
         self.payto_help = HelpButton(_('Recipient of the funds.') + '\n\n' + _('You may enter a Bitcoin address, a label from your list of contacts (a list of completions will be proposed), or an alias (email-like address that forwards to a Bitcoin address)'))
         grid.addWidget(QLabel(_('Pay to')), 1, 0)
         grid.addWidget(self.payto_e, 1, 1, 1, 3)
@@ -660,7 +673,6 @@ class ElectrumWindow(QMainWindow):
         grid.addWidget(self.from_list, 3, 1, 1, 3)
         self.set_pay_from([])
 
-        self.amount_e = AmountEdit(self.base_unit)
         self.amount_help = HelpButton(_('Amount to be sent.') + '\n\n' \
                                       + _('The amount will be displayed in red if you do not have enough funds in your wallet. Note that if you have frozen some of your addresses, the available funds will be lower than your total balance.') \
                                       + '\n\n' + _('Keyboard shortcut: type "!" to send all your coins.'))
@@ -668,7 +680,7 @@ class ElectrumWindow(QMainWindow):
         grid.addWidget(self.amount_e, 4, 1, 1, 2)
         grid.addWidget(self.amount_help, 4, 3)
 
-        self.fee_e = AmountEdit(self.base_unit)
+        self.fee_e = AmountEdit(self.get_decimal_point)
         grid.addWidget(QLabel(_('Fee')), 5, 0)
         grid.addWidget(self.fee_e, 5, 1, 1, 2)
         grid.addWidget(HelpButton(
@@ -681,7 +693,7 @@ class ElectrumWindow(QMainWindow):
         self.send_button = EnterButton(_("Send"), self.do_send)
         grid.addWidget(self.send_button, 6, 1)
 
-        b = EnterButton(_("Clear"),self.do_clear)
+        b = EnterButton(_("Clear"), self.do_clear)
         grid.addWidget(b, 6, 2)
 
         self.payto_sig = QLabel('')
@@ -691,12 +703,6 @@ class ElectrumWindow(QMainWindow):
         QShortcut(QKeySequence("Down"), w, w.focusNextChild)
         w.setLayout(grid)
 
-        w2 = QWidget()
-        vbox = QVBoxLayout()
-        vbox.addWidget(w)
-        vbox.addStretch(1)
-        w2.setLayout(vbox)
-
         def entry_changed( is_fee ):
             self.funds_error = False
 
@@ -711,8 +717,8 @@ class ElectrumWindow(QMainWindow):
                 self.fee_e.setText( self.format_amount( fee ) )
                 return
 
-            amount = self.read_amount(str(self.amount_e.text()))
-            fee = self.read_amount(str(self.fee_e.text()))
+            amount = self.amount_e.get_amount()
+            fee = self.fee_e.get_amount()
 
             if not is_fee: fee = None
             if amount is None:
@@ -741,7 +747,7 @@ class ElectrumWindow(QMainWindow):
         self.fee_e.textChanged.connect(lambda: entry_changed(True) )
 
         run_hook('create_send_tab', grid)
-        return w2
+        return w
 
 
     def set_pay_from(self, l):
@@ -786,9 +792,9 @@ class ElectrumWindow(QMainWindow):
             if not is_valid(to_address):
                 QMessageBox.warning(self, _('Error'), _('Invalid Bitcoin Address') + ':\n' + to_address, _('OK'))
                 return
-
+            
             try:
-                amount = self.read_amount(unicode( self.amount_e.text()))
+                amount = self.amount_e.get_amount()
             except Exception:
                 QMessageBox.warning(self, _('Error'), _('Invalid Amount'), _('OK'))
                 return
@@ -796,7 +802,7 @@ class ElectrumWindow(QMainWindow):
             outputs = [(to_address, amount)]
 
         try:
-            fee = self.read_amount(unicode( self.fee_e.text()))
+            fee = self.fee_e.get_amount()
         except Exception:
             QMessageBox.warning(self, _('Error'), _('Invalid Fee'), _('OK'))
             return
@@ -817,6 +823,7 @@ class ElectrumWindow(QMainWindow):
 
     @protected
     def send_tx(self, outputs, fee, label, password):
+        self.send_button.setDisabled(True)
 
         # first, create an unsigned tx 
         domain = self.get_payment_sources()
@@ -826,6 +833,7 @@ class ElectrumWindow(QMainWindow):
         except Exception as e:
             traceback.print_exc(file=sys.stdout)
             self.show_message(str(e))
+            self.send_button.setDisabled(False)
             return
 
         # call hook to see if plugin needs gui interaction
@@ -842,9 +850,11 @@ class ElectrumWindow(QMainWindow):
         def sign_done(tx, fee, label):
             if tx.error:
                 self.show_message(tx.error)
+                self.send_button.setDisabled(False)
                 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'))
+                self.send_button.setDisabled(False)
                 return
             if label:
                 self.wallet.set_label(tx.hash(), label)
@@ -852,11 +862,14 @@ class ElectrumWindow(QMainWindow):
             if not self.gui_object.payment_request:
                 if not tx.is_complete() or self.config.get('show_before_broadcast'):
                     self.show_transaction(tx)
+                    self.do_clear()
+                    self.send_button.setDisabled(False)
                     return
 
             self.broadcast_transaction(tx)
 
-        WaitingDialog(self, 'Signing..').start(sign_thread, sign_done)
+        self.waiting_dialog = WaitingDialog(self, 'Signing..', sign_thread, sign_done)
+        self.waiting_dialog.start()
 
 
 
@@ -877,8 +890,10 @@ class ElectrumWindow(QMainWindow):
                 self.do_clear()
             else:
                 QMessageBox.warning(self, _('Error'), msg, _('OK'))
+            self.send_button.setDisabled(False)
 
-        WaitingDialog(self, 'Broadcasting..').start(broadcast_thread, broadcast_done)
+        self.waiting_dialog = WaitingDialog(self, 'Broadcasting..', broadcast_thread, broadcast_done)
+        self.waiting_dialog.start()
 
 
 
@@ -1483,7 +1498,7 @@ class ElectrumWindow(QMainWindow):
             QMessageBox.warning(self, _('Error'), _('Incorrect Password'), _('OK'))
             return
         from seed_dialog import SeedDialog
-        d = SeedDialog(self, mnemonic, self.wallet.imported_keys)
+        d = SeedDialog(self, mnemonic, self.wallet.has_imported_keys())
         d.exec_()
 
 
@@ -1722,7 +1737,7 @@ class ElectrumWindow(QMainWindow):
     def show_message(self, msg):
         QMessageBox.information(self, _('Message'), msg, _('OK'))
 
-    def password_dialog(self ):
+    def password_dialog(self, msg=None):
         d = QDialog(self)
         d.setModal(1)
         d.setWindowTitle(_("Enter Password"))
@@ -1731,7 +1746,8 @@ class ElectrumWindow(QMainWindow):
         pw.setEchoMode(2)
 
         vbox = QVBoxLayout()
-        msg = _('Please enter your password')
+        if not msg:
+            msg = _('Please enter your password')
         vbox.addWidget(QLabel(msg))
 
         grid = QGridLayout()
@@ -2110,7 +2126,7 @@ class ElectrumWindow(QMainWindow):
 
     @protected
     def do_import_privkey(self, password):
-        if not self.wallet.imported_keys:
+        if not self.wallet.has_imported_keys():
             r = QMessageBox.question(None, _('Warning'), '<b>'+_('Warning') +':\n</b><br/>'+ _('Imported keys are not recoverable from seed.') + ' ' \
                                          + _('If you ever need to restore your wallet from its seed, these keys will be lost.') + '<p>' \
                                          + _('Are you sure you understand what you are doing?'), 3, 4)
@@ -2176,7 +2192,7 @@ class ElectrumWindow(QMainWindow):
 
         fee_label = QLabel(_('Transaction fee') + ':')
         grid.addWidget(fee_label, 2, 0)
-        fee_e = AmountEdit(self.base_unit)
+        fee_e = AmountEdit(self.get_decimal_point)
         fee_e.setText(self.format_amount(self.wallet.fee).strip())
         grid.addWidget(fee_e, 2, 1)
         msg = _('Fee per kilobyte of transaction.') + ' ' \
@@ -2225,9 +2241,8 @@ class ElectrumWindow(QMainWindow):
         # run the dialog
         if not d.exec_(): return
 
-        fee = unicode(fee_e.text())
         try:
-            fee = self.read_amount(fee)
+            fee = self.fee_e.get_amount()
         except Exception:
             QMessageBox.warning(self, _('Error'), _('Invalid value') +': %s'%fee, _('OK'))
             return