URI: 
       tMerge pull request #3923 from SomberNight/gui_add_tx_ismine - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit 0d43820ab7261d67c7a885c03559a1831ef0a1ec
   DIR parent 363f3766d753f905bafe770114a30f489c448620
  HTML Author: ThomasV <thomasv@electrum.org>
       Date:   Wed, 21 Feb 2018 10:50:17 +0100
       
       Merge pull request #3923 from SomberNight/gui_add_tx_ismine
       
       local tx: restructure exception handling wrt wallet.add_transaction and QT
       Diffstat:
         M gui/qt/history_list.py              |      18 +++++++-----------
         M gui/qt/main_window.py               |      20 +++++++++++++++++++-
         M gui/qt/transaction_dialog.py        |      16 ++++------------
         M lib/wallet.py                       |      17 +++++++++++++----
       
       4 files changed, 43 insertions(+), 28 deletions(-)
       ---
   DIR diff --git a/gui/qt/history_list.py b/gui/qt/history_list.py
       t@@ -26,7 +26,7 @@
        import webbrowser
        import datetime
        
       -from electrum.wallet import UnrelatedTransactionException, TX_HEIGHT_LOCAL
       +from electrum.wallet import AddTransactionException, TX_HEIGHT_LOCAL
        from .util import *
        from electrum.i18n import _
        from electrum.util import block_explorer_URL
       t@@ -356,16 +356,12 @@ class HistoryList(MyTreeWidget, AcceptFileDragDrop):
                self.parent.need_update.set()
        
            def onFileAdded(self, fn):
       -        with open(fn) as f:
       -            tx = self.parent.tx_from_text(f.read())
       -            try:
       -                self.wallet.add_transaction(tx.txid(), tx)
       -            except UnrelatedTransactionException as e:
       -                self.parent.show_error(e)
       -            else:
       -                self.wallet.save_transactions(write=True)
       -                # need to update at least: history_list, utxo_list, address_list
       -                self.parent.need_update.set()
       +        try:
       +            with open(fn) as f:
       +                tx = self.parent.tx_from_text(f.read())
       +                self.parent.save_transaction_into_wallet(tx)
       +        except IOError as e:
       +            self.parent.show_error(e)
        
            def export_history_dialog(self):
                d = WindowModalDialog(self, _('Export History'))
   DIR diff --git a/gui/qt/main_window.py b/gui/qt/main_window.py
       t@@ -51,7 +51,7 @@ from electrum.util import (format_time, format_satoshis, PrintError,
        from electrum import Transaction
        from electrum import util, bitcoin, commands, coinchooser
        from electrum import paymentrequest
       -from electrum.wallet import Multisig_Wallet
       +from electrum.wallet import Multisig_Wallet, AddTransactionException
        
        from .amountedit import AmountEdit, BTCAmountEdit, MyLineEdit, FeerateEdit
        from .qrcodewidget import QRCodeWidget, QRDialog
       t@@ -3125,3 +3125,21 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
                if is_final:
                    new_tx.set_rbf(False)
                self.show_transaction(new_tx, tx_label)
       +
       +    def save_transaction_into_wallet(self, tx):
       +        try:
       +            if not self.wallet.add_transaction(tx.txid(), tx):
       +                self.show_error(_("Transaction could not be saved.") + "\n" +
       +                                       _("It conflicts with current history."))
       +                return False
       +        except AddTransactionException as e:
       +            self.show_error(e)
       +            return False
       +        else:
       +            self.wallet.save_transactions(write=True)
       +            # need to update at least: history_list, utxo_list, address_list
       +            self.need_update.set()
       +            self.show_message(_("Transaction saved successfully"))
       +            return True
       +
       +
   DIR diff --git a/gui/qt/transaction_dialog.py b/gui/qt/transaction_dialog.py
       t@@ -35,7 +35,7 @@ from electrum.i18n import _
        from electrum.plugins import run_hook
        
        from electrum.util import bfh
       -from electrum.wallet import UnrelatedTransactionException
       +from electrum.wallet import AddTransactionException
        
        from .util import *
        
       t@@ -179,17 +179,9 @@ class TxDialog(QDialog, MessageBoxMixin):
                self.main_window.sign_tx(self.tx, sign_done)
        
            def save(self):
       -        if not self.wallet.add_transaction(self.tx.txid(), self.tx):
       -            self.show_error(_("Transaction could not be saved. It conflicts with current history."))
       -            return
       -        self.wallet.save_transactions(write=True)
       -
       -        # need to update at least: history_list, utxo_list, address_list
       -        self.main_window.need_update.set()
       -
       -        self.save_button.setDisabled(True)
       -        self.show_message(_("Transaction saved successfully"))
       -        self.saved = True
       +        if self.main_window.save_transaction_into_wallet(self.tx):
       +            self.save_button.setDisabled(True)
       +            self.saved = True
        
        
            def export(self):
   DIR diff --git a/lib/wallet.py b/lib/wallet.py
       t@@ -157,9 +157,18 @@ def sweep(privkeys, network, config, recipient, fee=None, imax=100):
            return tx
        
        
       -class UnrelatedTransactionException(Exception):
       -    def __init__(self):
       -        self.args = ("Transaction is unrelated to this wallet ", )
       +class AddTransactionException(Exception):
       +    pass
       +
       +
       +class UnrelatedTransactionException(AddTransactionException):
       +    def __str__(self):
       +        return _("Transaction is unrelated to this wallet.")
       +
       +
       +class NotIsMineTransactionException(AddTransactionException):
       +    def __str__(self):
       +        return _("Only transactions with inputs owned by the wallet can be added.")
        
        
        class Abstract_Wallet(PrintError):
       t@@ -768,7 +777,7 @@ class Abstract_Wallet(PrintError):
                    # do not save if tx is local and not mine
                    if tx_height == TX_HEIGHT_LOCAL and not is_mine:
                        # FIXME the test here should be for "not all is_mine"; cannot detect conflict in some cases
       -                return False
       +                raise NotIsMineTransactionException()
                    # raise exception if unrelated to wallet
                    is_for_me = any([self.is_mine(self.get_txout_address(txo)) for txo in tx.outputs()])
                    if not is_mine and not is_for_me: