2013-11-04 16:20:43 +01:00
// Copyright (c) 2011-2013 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2011-05-12 14:49:42 +02:00
# include "sendcoinsdialog.h"
2011-05-12 14:44:52 +02:00
# include "ui_sendcoinsdialog.h"
2013-01-23 21:51:02 +01:00
2013-11-16 17:37:31 +01:00
# include "addresstablemodel.h"
2011-07-25 21:35:45 +02:00
# include "bitcoinunits.h"
2013-11-16 17:37:31 +01:00
# include "coincontroldialog.h"
2013-04-13 07:13:08 +02:00
# include "guiutil.h"
2011-06-01 09:34:12 +02:00
# include "optionsmodel.h"
2011-07-16 19:01:05 +02:00
# include "sendcoinsentry.h"
2013-04-13 07:13:08 +02:00
# include "walletmodel.h"
2012-08-07 19:19:14 +02:00
# include "base58.h"
2013-08-12 17:03:03 +02:00
# include "coincontrol.h"
2013-11-16 17:37:31 +01:00
# include "ui_interface.h"
2011-05-12 17:55:24 +02:00
2011-05-15 19:31:20 +02:00
# include <QMessageBox>
2011-12-07 06:00:04 +01:00
# include <QScrollBar>
2013-04-13 07:13:08 +02:00
# include <QTextDocument>
2011-05-12 20:16:42 +02:00
2011-07-16 19:01:05 +02:00
SendCoinsDialog : : SendCoinsDialog ( QWidget * parent ) :
2011-05-12 14:44:52 +02:00
QDialog ( parent ) ,
2011-05-30 20:20:12 +02:00
ui ( new Ui : : SendCoinsDialog ) ,
model ( 0 )
2011-05-12 14:44:52 +02:00
{
ui - > setupUi ( this ) ;
2011-07-08 19:25:35 +02:00
2012-09-21 19:06:53 +02:00
# ifdef Q_OS_MAC // Icons on push buttons are very uncommon on Mac
2011-10-07 13:21:45 +02:00
ui - > addButton - > setIcon ( QIcon ( ) ) ;
ui - > clearButton - > setIcon ( QIcon ( ) ) ;
ui - > sendButton - > setIcon ( QIcon ( ) ) ;
# endif
2013-08-12 17:03:03 +02:00
# if QT_VERSION >= 0x040700
ui - > lineEditCoinControlChange - > setPlaceholderText ( tr ( " Enter a Bitcoin address (e.g. 1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L) " ) ) ;
# endif
2011-10-07 13:21:45 +02:00
2011-07-16 19:01:05 +02:00
addEntry ( ) ;
2011-05-15 19:31:20 +02:00
2011-07-16 19:01:05 +02:00
connect ( ui - > addButton , SIGNAL ( clicked ( ) ) , this , SLOT ( addEntry ( ) ) ) ;
2011-09-27 16:46:19 +02:00
connect ( ui - > clearButton , SIGNAL ( clicked ( ) ) , this , SLOT ( clear ( ) ) ) ;
2011-12-24 05:27:12 +01:00
2013-08-12 17:03:03 +02:00
// Coin Control
ui - > lineEditCoinControlChange - > setFont ( GUIUtil : : bitcoinAddressFont ( ) ) ;
connect ( ui - > pushButtonCoinControl , SIGNAL ( clicked ( ) ) , this , SLOT ( coinControlButtonClicked ( ) ) ) ;
connect ( ui - > checkBoxCoinControlChange , SIGNAL ( stateChanged ( int ) ) , this , SLOT ( coinControlChangeChecked ( int ) ) ) ;
connect ( ui - > lineEditCoinControlChange , SIGNAL ( textEdited ( const QString & ) ) , this , SLOT ( coinControlChangeEdited ( const QString & ) ) ) ;
// Coin Control: clipboard actions
QAction * clipboardQuantityAction = new QAction ( tr ( " Copy quantity " ) , this ) ;
QAction * clipboardAmountAction = new QAction ( tr ( " Copy amount " ) , this ) ;
QAction * clipboardFeeAction = new QAction ( tr ( " Copy fee " ) , this ) ;
QAction * clipboardAfterFeeAction = new QAction ( tr ( " Copy after fee " ) , this ) ;
QAction * clipboardBytesAction = new QAction ( tr ( " Copy bytes " ) , this ) ;
QAction * clipboardPriorityAction = new QAction ( tr ( " Copy priority " ) , this ) ;
QAction * clipboardLowOutputAction = new QAction ( tr ( " Copy low output " ) , this ) ;
QAction * clipboardChangeAction = new QAction ( tr ( " Copy change " ) , this ) ;
connect ( clipboardQuantityAction , SIGNAL ( triggered ( ) ) , this , SLOT ( coinControlClipboardQuantity ( ) ) ) ;
connect ( clipboardAmountAction , SIGNAL ( triggered ( ) ) , this , SLOT ( coinControlClipboardAmount ( ) ) ) ;
connect ( clipboardFeeAction , SIGNAL ( triggered ( ) ) , this , SLOT ( coinControlClipboardFee ( ) ) ) ;
connect ( clipboardAfterFeeAction , SIGNAL ( triggered ( ) ) , this , SLOT ( coinControlClipboardAfterFee ( ) ) ) ;
connect ( clipboardBytesAction , SIGNAL ( triggered ( ) ) , this , SLOT ( coinControlClipboardBytes ( ) ) ) ;
connect ( clipboardPriorityAction , SIGNAL ( triggered ( ) ) , this , SLOT ( coinControlClipboardPriority ( ) ) ) ;
connect ( clipboardLowOutputAction , SIGNAL ( triggered ( ) ) , this , SLOT ( coinControlClipboardLowOutput ( ) ) ) ;
connect ( clipboardChangeAction , SIGNAL ( triggered ( ) ) , this , SLOT ( coinControlClipboardChange ( ) ) ) ;
ui - > labelCoinControlQuantity - > addAction ( clipboardQuantityAction ) ;
ui - > labelCoinControlAmount - > addAction ( clipboardAmountAction ) ;
ui - > labelCoinControlFee - > addAction ( clipboardFeeAction ) ;
ui - > labelCoinControlAfterFee - > addAction ( clipboardAfterFeeAction ) ;
ui - > labelCoinControlBytes - > addAction ( clipboardBytesAction ) ;
ui - > labelCoinControlPriority - > addAction ( clipboardPriorityAction ) ;
ui - > labelCoinControlLowOutput - > addAction ( clipboardLowOutputAction ) ;
ui - > labelCoinControlChange - > addAction ( clipboardChangeAction ) ;
2011-12-24 05:27:12 +01:00
fNewRecipientAllowed = true ;
2011-05-12 14:44:52 +02:00
}
2011-06-30 18:05:29 +02:00
void SendCoinsDialog : : setModel ( WalletModel * model )
2011-05-30 20:20:12 +02:00
{
this - > model = model ;
2011-07-16 19:01:05 +02:00
2013-08-23 13:07:20 +02:00
if ( model & & model - > getOptionsModel ( ) )
2011-07-16 19:01:05 +02:00
{
2013-08-23 13:07:20 +02:00
for ( int i = 0 ; i < ui - > entries - > count ( ) ; + + i )
2011-07-16 19:01:05 +02:00
{
2013-08-23 13:07:20 +02:00
SendCoinsEntry * entry = qobject_cast < SendCoinsEntry * > ( ui - > entries - > itemAt ( i ) - > widget ( ) ) ;
if ( entry )
{
entry - > setModel ( model ) ;
}
2011-07-16 19:01:05 +02:00
}
2013-08-23 13:07:20 +02:00
2012-02-14 12:08:00 +01:00
setBalance ( model - > getBalance ( ) , model - > getUnconfirmedBalance ( ) , model - > getImmatureBalance ( ) ) ;
connect ( model , SIGNAL ( balanceChanged ( qint64 , qint64 , qint64 ) ) , this , SLOT ( setBalance ( qint64 , qint64 , qint64 ) ) ) ;
2012-06-09 15:41:21 +02:00
connect ( model - > getOptionsModel ( ) , SIGNAL ( displayUnitChanged ( int ) ) , this , SLOT ( updateDisplayUnit ( ) ) ) ;
2013-08-12 17:03:03 +02:00
// Coin Control
connect ( model - > getOptionsModel ( ) , SIGNAL ( displayUnitChanged ( int ) ) , this , SLOT ( coinControlUpdateLabels ( ) ) ) ;
connect ( model - > getOptionsModel ( ) , SIGNAL ( coinControlFeaturesChanged ( bool ) ) , this , SLOT ( coinControlFeatureChanged ( bool ) ) ) ;
connect ( model - > getOptionsModel ( ) , SIGNAL ( transactionFeeChanged ( qint64 ) ) , this , SLOT ( coinControlUpdateLabels ( ) ) ) ;
ui - > frameCoinControl - > setVisible ( model - > getOptionsModel ( ) - > getCoinControlFeatures ( ) ) ;
coinControlUpdateLabels ( ) ;
2011-11-08 21:18:36 +01:00
}
2011-05-30 20:20:12 +02:00
}
2011-05-12 14:44:52 +02:00
SendCoinsDialog : : ~ SendCoinsDialog ( )
{
delete ui ;
}
2011-05-12 17:55:24 +02:00
void SendCoinsDialog : : on_sendButton_clicked ( )
{
2013-08-24 15:07:17 +02:00
if ( ! model | | ! model - > getOptionsModel ( ) )
return ;
2011-07-16 19:01:05 +02:00
QList < SendCoinsRecipient > recipients ;
bool valid = true ;
2011-11-08 21:18:36 +01:00
2011-07-16 19:01:05 +02:00
for ( int i = 0 ; i < ui - > entries - > count ( ) ; + + i )
{
SendCoinsEntry * entry = qobject_cast < SendCoinsEntry * > ( ui - > entries - > itemAt ( i ) - > widget ( ) ) ;
if ( entry )
{
if ( entry - > validate ( ) )
{
recipients . append ( entry - > getValue ( ) ) ;
}
else
{
valid = false ;
}
}
}
2011-05-30 20:20:12 +02:00
2011-07-16 19:01:05 +02:00
if ( ! valid | | recipients . isEmpty ( ) )
2011-05-14 17:25:05 +02:00
{
2011-05-15 19:31:20 +02:00
return ;
2011-05-14 17:25:05 +02:00
}
2011-05-27 21:43:05 +02:00
2011-07-16 19:01:05 +02:00
// Format confirmation message
QStringList formatted ;
foreach ( const SendCoinsRecipient & rcp , recipients )
{
2013-09-13 16:49:35 +02:00
// generate bold amount string
QString amount = " <b> " + BitcoinUnits : : formatWithUnit ( model - > getOptionsModel ( ) - > getDisplayUnit ( ) , rcp . amount ) ;
amount . append ( " </b> " ) ;
// generate monospace address string
QString address = " <span style='font-family: monospace;'> " + rcp . address ;
address . append ( " </span> " ) ;
QString recipientElement ;
2013-10-24 16:02:39 +02:00
if ( ! rcp . paymentRequest . IsInitialized ( ) ) // normal payment
2013-07-22 08:50:39 +02:00
{
2013-09-13 16:49:35 +02:00
if ( rcp . label . length ( ) > 0 ) // label with address
2013-08-30 20:04:48 +02:00
{
2013-09-13 16:49:35 +02:00
recipientElement = tr ( " %1 to %2 " ) . arg ( amount , GUIUtil : : HtmlEscape ( rcp . label ) ) ;
recipientElement . append ( QString ( " (%1) " ) . arg ( address ) ) ;
2013-08-30 20:04:48 +02:00
}
2013-09-13 16:49:35 +02:00
else // just address
2013-08-30 20:04:48 +02:00
{
2013-09-13 16:49:35 +02:00
recipientElement = tr ( " %1 to %2 " ) . arg ( amount , address ) ;
2013-08-30 20:04:48 +02:00
}
2013-07-22 08:50:39 +02:00
}
2013-10-24 16:02:39 +02:00
else if ( ! rcp . authenticatedMerchant . isEmpty ( ) ) // secure payment request
2013-07-22 08:50:39 +02:00
{
2013-09-13 16:49:35 +02:00
recipientElement = tr ( " %1 to %2 " ) . arg ( amount , GUIUtil : : HtmlEscape ( rcp . authenticatedMerchant ) ) ;
2013-07-22 08:50:39 +02:00
}
2013-10-24 16:02:39 +02:00
else // insecure payment request
{
recipientElement = tr ( " %1 to %2 " ) . arg ( amount , address ) ;
}
2013-09-13 16:49:35 +02:00
formatted . append ( recipientElement ) ;
2011-07-16 19:01:05 +02:00
}
2011-06-25 19:32:36 +02:00
2011-12-24 05:27:12 +01:00
fNewRecipientAllowed = false ;
2011-07-07 17:33:15 +02:00
2011-08-24 22:07:26 +02:00
WalletModel : : UnlockContext ctx ( model - > requestUnlock ( ) ) ;
if ( ! ctx . isValid ( ) )
{
// Unlock wallet was cancelled
2011-12-24 05:27:12 +01:00
fNewRecipientAllowed = true ;
2011-08-24 22:07:26 +02:00
return ;
}
2013-08-30 20:04:48 +02:00
// prepare transaction for getting txFee earlier
WalletModelTransaction currentTransaction ( recipients ) ;
2013-08-12 17:03:03 +02:00
WalletModel : : SendCoinsReturn prepareStatus ;
if ( model - > getOptionsModel ( ) - > getCoinControlFeatures ( ) ) // coin control enabled
prepareStatus = model - > prepareTransaction ( currentTransaction , CoinControlDialog : : coinControl ) ;
else
prepareStatus = model - > prepareTransaction ( currentTransaction ) ;
2013-10-30 15:37:41 +01:00
// process prepareStatus and on error generate message shown to user
processSendCoinsReturn ( prepareStatus ,
BitcoinUnits : : formatWithUnit ( model - > getOptionsModel ( ) - > getDisplayUnit ( ) , currentTransaction . getTransactionFee ( ) ) ) ;
2013-08-30 20:04:48 +02:00
if ( prepareStatus . status ! = WalletModel : : OK ) {
fNewRecipientAllowed = true ;
return ;
}
qint64 txFee = currentTransaction . getTransactionFee ( ) ;
QString questionString = tr ( " Are you sure you want to send? " ) ;
questionString . append ( " <br /><br />%1 " ) ;
if ( txFee > 0 )
{
// append fee string if a fee is required
questionString . append ( " <hr /><span style='color:#aa0000;'> " ) ;
questionString . append ( BitcoinUnits : : formatWithUnit ( model - > getOptionsModel ( ) - > getDisplayUnit ( ) , txFee ) ) ;
questionString . append ( " </span> " ) ;
questionString . append ( tr ( " added as transaction fee " ) ) ;
}
2013-11-17 14:43:23 +01:00
// add total amount in all subdivision units
questionString . append ( " <hr /> " ) ;
qint64 totalAmount = currentTransaction . getTotalTransactionAmount ( ) + txFee ;
QStringList alternativeUnits ;
foreach ( BitcoinUnits : : Unit u , BitcoinUnits : : availableUnits ( ) )
2013-08-30 20:04:48 +02:00
{
2013-11-17 14:43:23 +01:00
if ( u ! = model - > getOptionsModel ( ) - > getDisplayUnit ( ) )
alternativeUnits . append ( BitcoinUnits : : formatWithUnit ( u , totalAmount ) ) ;
2013-08-30 20:04:48 +02:00
}
2013-11-17 14:43:23 +01:00
questionString . append ( tr ( " Total Amount %1 (= %2) " )
2013-11-22 13:53:05 +01:00
. arg ( BitcoinUnits : : formatWithUnit ( model - > getOptionsModel ( ) - > getDisplayUnit ( ) , totalAmount ) )
. arg ( alternativeUnits . join ( " " + tr ( " or " ) + " " ) ) ) ;
2013-08-30 20:04:48 +02:00
QMessageBox : : StandardButton retval = QMessageBox : : question ( this , tr ( " Confirm send coins " ) ,
questionString . arg ( formatted . join ( " <br /> " ) ) ,
2013-09-13 16:49:35 +02:00
QMessageBox : : Yes | QMessageBox : : Cancel ,
2013-08-30 20:04:48 +02:00
QMessageBox : : Cancel ) ;
if ( retval ! = QMessageBox : : Yes )
{
fNewRecipientAllowed = true ;
return ;
}
// now send the prepared transaction
2013-10-30 15:37:41 +01:00
WalletModel : : SendCoinsReturn sendStatus = model - > sendCoins ( currentTransaction ) ;
// process sendStatus and on error generate message shown to user
processSendCoinsReturn ( sendStatus ) ;
if ( sendStatus . status = = WalletModel : : OK )
2013-08-30 20:04:48 +02:00
{
2011-05-31 22:24:53 +02:00
accept ( ) ;
2013-08-12 17:03:03 +02:00
CoinControlDialog : : coinControl - > UnSelectAll ( ) ;
coinControlUpdateLabels ( ) ;
2011-05-14 17:25:05 +02:00
}
2011-12-24 05:27:12 +01:00
fNewRecipientAllowed = true ;
2011-05-12 17:55:24 +02:00
}
2011-07-16 19:01:05 +02:00
void SendCoinsDialog : : clear ( )
2011-05-12 17:55:24 +02:00
{
2011-07-16 19:01:05 +02:00
// Remove entries until only one left
2011-08-07 16:04:48 +02:00
while ( ui - > entries - > count ( ) )
2011-07-16 19:01:05 +02:00
{
delete ui - > entries - > takeAt ( 0 ) - > widget ( ) ;
}
2011-08-07 16:04:48 +02:00
addEntry ( ) ;
2011-05-13 22:00:27 +02:00
2013-11-22 13:53:05 +01:00
updateTabsAndLabels ( ) ;
2011-07-07 17:33:15 +02:00
}
void SendCoinsDialog : : reject ( )
{
clear ( ) ;
}
void SendCoinsDialog : : accept ( )
{
clear ( ) ;
}
2011-07-16 19:01:05 +02:00
2011-08-07 16:04:48 +02:00
SendCoinsEntry * SendCoinsDialog : : addEntry ( )
2011-07-16 19:01:05 +02:00
{
SendCoinsEntry * entry = new SendCoinsEntry ( this ) ;
entry - > setModel ( model ) ;
ui - > entries - > addWidget ( entry ) ;
connect ( entry , SIGNAL ( removeEntry ( SendCoinsEntry * ) ) , this , SLOT ( removeEntry ( SendCoinsEntry * ) ) ) ;
2013-08-12 17:03:03 +02:00
connect ( entry , SIGNAL ( payAmountChanged ( ) ) , this , SLOT ( coinControlUpdateLabels ( ) ) ) ;
2011-07-16 19:01:05 +02:00
2013-11-22 13:53:05 +01:00
updateTabsAndLabels ( ) ;
2011-07-16 19:01:05 +02:00
// Focus the field, so that entry can start immediately
entry - > clear ( ) ;
2011-12-07 06:00:04 +01:00
entry - > setFocus ( ) ;
ui - > scrollAreaWidgetContents - > resize ( ui - > scrollAreaWidgetContents - > sizeHint ( ) ) ;
2013-04-02 17:30:14 +02:00
qApp - > processEvents ( ) ;
2011-12-07 06:00:04 +01:00
QScrollBar * bar = ui - > scrollArea - > verticalScrollBar ( ) ;
2012-06-09 15:41:21 +02:00
if ( bar )
2011-12-07 06:00:04 +01:00
bar - > setSliderPosition ( bar - > maximum ( ) ) ;
2011-08-07 16:04:48 +02:00
return entry ;
2011-07-16 19:01:05 +02:00
}
2013-11-22 13:53:05 +01:00
void SendCoinsDialog : : updateTabsAndLabels ( )
2011-07-16 19:01:05 +02:00
{
setupTabChain ( 0 ) ;
2013-08-12 17:03:03 +02:00
coinControlUpdateLabels ( ) ;
2011-07-16 19:01:05 +02:00
}
void SendCoinsDialog : : removeEntry ( SendCoinsEntry * entry )
{
delete entry ;
2013-11-22 13:53:05 +01:00
// If the last entry was removed add an empty one
if ( ! ui - > entries - > count ( ) )
addEntry ( ) ;
updateTabsAndLabels ( ) ;
2011-07-16 19:01:05 +02:00
}
QWidget * SendCoinsDialog : : setupTabChain ( QWidget * prev )
{
for ( int i = 0 ; i < ui - > entries - > count ( ) ; + + i )
{
SendCoinsEntry * entry = qobject_cast < SendCoinsEntry * > ( ui - > entries - > itemAt ( i ) - > widget ( ) ) ;
if ( entry )
{
prev = entry - > setupTabChain ( prev ) ;
}
}
QWidget : : setTabOrder ( prev , ui - > addButton ) ;
QWidget : : setTabOrder ( ui - > addButton , ui - > sendButton ) ;
return ui - > sendButton ;
}
2011-08-07 16:04:48 +02:00
2013-01-25 18:46:53 +01:00
void SendCoinsDialog : : setAddress ( const QString & address )
{
SendCoinsEntry * entry = 0 ;
// Replace the first entry if it is still unused
if ( ui - > entries - > count ( ) = = 1 )
{
SendCoinsEntry * first = qobject_cast < SendCoinsEntry * > ( ui - > entries - > itemAt ( 0 ) - > widget ( ) ) ;
if ( first - > isClear ( ) )
{
entry = first ;
}
}
if ( ! entry )
{
entry = addEntry ( ) ;
}
entry - > setAddress ( address ) ;
}
2011-08-07 16:04:48 +02:00
void SendCoinsDialog : : pasteEntry ( const SendCoinsRecipient & rv )
{
2012-06-09 15:41:21 +02:00
if ( ! fNewRecipientAllowed )
2011-12-24 05:27:12 +01:00
return ;
2011-08-07 16:04:48 +02:00
SendCoinsEntry * entry = 0 ;
// Replace the first entry if it is still unused
if ( ui - > entries - > count ( ) = = 1 )
{
SendCoinsEntry * first = qobject_cast < SendCoinsEntry * > ( ui - > entries - > itemAt ( 0 ) - > widget ( ) ) ;
if ( first - > isClear ( ) )
{
entry = first ;
}
}
if ( ! entry )
{
entry = addEntry ( ) ;
}
entry - > setValue ( rv ) ;
2013-11-22 13:53:05 +01:00
updateTabsAndLabels ( ) ;
2011-08-07 16:04:48 +02:00
}
2013-07-22 08:50:39 +02:00
bool SendCoinsDialog : : handlePaymentRequest ( const SendCoinsRecipient & rv )
2011-08-07 16:04:48 +02:00
{
2013-09-13 16:49:35 +02:00
QString strSendCoins = tr ( " Send Coins " ) ;
2013-10-24 16:02:39 +02:00
if ( rv . paymentRequest . IsInitialized ( ) ) {
2013-07-22 08:50:39 +02:00
// Expired payment request?
const payments : : PaymentDetails & details = rv . paymentRequest . getDetails ( ) ;
2013-04-13 07:13:08 +02:00
if ( details . has_expires ( ) & & ( int64_t ) details . expires ( ) < GetTime ( ) )
2013-07-22 08:50:39 +02:00
{
2013-10-30 15:37:41 +01:00
emit message ( strSendCoins , tr ( " Payment request expired " ) ,
CClientUIInterface : : MSG_WARNING ) ;
2013-07-22 08:50:39 +02:00
return false ;
}
}
else {
2012-08-07 19:19:14 +02:00
CBitcoinAddress address ( rv . address . toStdString ( ) ) ;
2013-07-22 08:50:39 +02:00
if ( ! address . IsValid ( ) ) {
2013-10-30 15:37:41 +01:00
emit message ( strSendCoins , tr ( " Invalid payment address %1 " ) . arg ( rv . address ) ,
CClientUIInterface : : MSG_WARNING ) ;
2012-08-07 19:19:14 +02:00
return false ;
2013-07-22 08:50:39 +02:00
}
2011-08-07 16:04:48 +02:00
}
2012-03-28 14:55:29 +02:00
2013-07-22 08:50:39 +02:00
pasteEntry ( rv ) ;
return true ;
2011-08-07 16:04:48 +02:00
}
2011-09-22 19:02:01 +02:00
2012-02-14 12:08:00 +01:00
void SendCoinsDialog : : setBalance ( qint64 balance , qint64 unconfirmedBalance , qint64 immatureBalance )
2011-09-22 19:02:01 +02:00
{
Q_UNUSED ( unconfirmedBalance ) ;
2012-02-14 12:08:00 +01:00
Q_UNUSED ( immatureBalance ) ;
2011-11-08 21:18:36 +01:00
2013-08-24 15:07:17 +02:00
if ( model & & model - > getOptionsModel ( ) )
{
ui - > labelBalance - > setText ( BitcoinUnits : : formatWithUnit ( model - > getOptionsModel ( ) - > getDisplayUnit ( ) , balance ) ) ;
}
2011-09-22 19:02:01 +02:00
}
2012-06-09 15:41:21 +02:00
void SendCoinsDialog : : updateDisplayUnit ( )
{
2013-08-24 15:07:17 +02:00
setBalance ( model - > getBalance ( ) , 0 , 0 ) ;
2012-06-09 15:41:21 +02:00
}
2013-10-30 15:37:41 +01:00
void SendCoinsDialog : : processSendCoinsReturn ( const WalletModel : : SendCoinsReturn & sendCoinsReturn , const QString & msgArg )
{
QPair < QString , CClientUIInterface : : MessageBoxFlags > msgParams ;
// Default to a warning message, override if error message is needed
msgParams . second = CClientUIInterface : : MSG_WARNING ;
// This comment is specific to SendCoinsDialog usage of WalletModel::SendCoinsReturn.
// WalletModel::TransactionCommitFailed is used only in WalletModel::sendCoins()
// all others are used only in WalletModel::prepareTransaction()
switch ( sendCoinsReturn . status )
{
case WalletModel : : InvalidAddress :
msgParams . first = tr ( " The recipient address is not valid, please recheck. " ) ;
break ;
case WalletModel : : InvalidAmount :
msgParams . first = tr ( " The amount to pay must be larger than 0. " ) ;
break ;
case WalletModel : : AmountExceedsBalance :
msgParams . first = tr ( " The amount exceeds your balance. " ) ;
break ;
case WalletModel : : AmountWithFeeExceedsBalance :
msgParams . first = tr ( " The total exceeds your balance when the %1 transaction fee is included. " ) . arg ( msgArg ) ;
break ;
case WalletModel : : DuplicateAddress :
msgParams . first = tr ( " Duplicate address found, can only send to each address once per send operation. " ) ;
break ;
case WalletModel : : TransactionCreationFailed :
msgParams . first = tr ( " Transaction creation failed! " ) ;
msgParams . second = CClientUIInterface : : MSG_ERROR ;
break ;
case WalletModel : : TransactionCommitFailed :
msgParams . first = tr ( " The transaction was rejected! This might happen if some of the coins in your wallet were already spent, such as if you used a copy of wallet.dat and coins were spent in the copy but not marked as spent here. " ) ;
msgParams . second = CClientUIInterface : : MSG_ERROR ;
break ;
// OK and Aborted are included to prevent a compiler warning.
case WalletModel : : OK :
case WalletModel : : Aborted :
default :
return ;
}
emit message ( tr ( " Send Coins " ) , msgParams . first , msgParams . second ) ;
}
2013-08-12 17:03:03 +02:00
// Coin Control: copy label "Quantity" to clipboard
void SendCoinsDialog : : coinControlClipboardQuantity ( )
{
GUIUtil : : setClipboard ( ui - > labelCoinControlQuantity - > text ( ) ) ;
}
// Coin Control: copy label "Amount" to clipboard
void SendCoinsDialog : : coinControlClipboardAmount ( )
{
GUIUtil : : setClipboard ( ui - > labelCoinControlAmount - > text ( ) . left ( ui - > labelCoinControlAmount - > text ( ) . indexOf ( " " ) ) ) ;
}
// Coin Control: copy label "Fee" to clipboard
void SendCoinsDialog : : coinControlClipboardFee ( )
{
GUIUtil : : setClipboard ( ui - > labelCoinControlFee - > text ( ) . left ( ui - > labelCoinControlFee - > text ( ) . indexOf ( " " ) ) ) ;
}
// Coin Control: copy label "After fee" to clipboard
void SendCoinsDialog : : coinControlClipboardAfterFee ( )
{
GUIUtil : : setClipboard ( ui - > labelCoinControlAfterFee - > text ( ) . left ( ui - > labelCoinControlAfterFee - > text ( ) . indexOf ( " " ) ) ) ;
}
// Coin Control: copy label "Bytes" to clipboard
void SendCoinsDialog : : coinControlClipboardBytes ( )
{
GUIUtil : : setClipboard ( ui - > labelCoinControlBytes - > text ( ) ) ;
}
// Coin Control: copy label "Priority" to clipboard
void SendCoinsDialog : : coinControlClipboardPriority ( )
{
GUIUtil : : setClipboard ( ui - > labelCoinControlPriority - > text ( ) ) ;
}
// Coin Control: copy label "Low output" to clipboard
void SendCoinsDialog : : coinControlClipboardLowOutput ( )
{
GUIUtil : : setClipboard ( ui - > labelCoinControlLowOutput - > text ( ) ) ;
}
// Coin Control: copy label "Change" to clipboard
void SendCoinsDialog : : coinControlClipboardChange ( )
{
GUIUtil : : setClipboard ( ui - > labelCoinControlChange - > text ( ) . left ( ui - > labelCoinControlChange - > text ( ) . indexOf ( " " ) ) ) ;
}
// Coin Control: settings menu - coin control enabled/disabled by user
void SendCoinsDialog : : coinControlFeatureChanged ( bool checked )
{
ui - > frameCoinControl - > setVisible ( checked ) ;
if ( ! checked & & model ) // coin control features disabled
CoinControlDialog : : coinControl - > SetNull ( ) ;
}
// Coin Control: button inputs -> show actual coin control dialog
void SendCoinsDialog : : coinControlButtonClicked ( )
{
CoinControlDialog dlg ;
dlg . setModel ( model ) ;
dlg . exec ( ) ;
coinControlUpdateLabels ( ) ;
}
// Coin Control: checkbox custom change address
void SendCoinsDialog : : coinControlChangeChecked ( int state )
{
if ( model )
{
if ( state = = Qt : : Checked )
CoinControlDialog : : coinControl - > destChange = CBitcoinAddress ( ui - > lineEditCoinControlChange - > text ( ) . toStdString ( ) ) . Get ( ) ;
else
CoinControlDialog : : coinControl - > destChange = CNoDestination ( ) ;
}
ui - > lineEditCoinControlChange - > setEnabled ( ( state = = Qt : : Checked ) ) ;
ui - > labelCoinControlChangeLabel - > setVisible ( ( state = = Qt : : Checked ) ) ;
}
// Coin Control: custom change address changed
void SendCoinsDialog : : coinControlChangeEdited ( const QString & text )
{
if ( model )
{
CoinControlDialog : : coinControl - > destChange = CBitcoinAddress ( text . toStdString ( ) ) . Get ( ) ;
// label for the change address
ui - > labelCoinControlChangeLabel - > setStyleSheet ( " QLabel{color:black;} " ) ;
if ( text . isEmpty ( ) )
ui - > labelCoinControlChangeLabel - > setText ( " " ) ;
else if ( ! CBitcoinAddress ( text . toStdString ( ) ) . IsValid ( ) )
{
ui - > labelCoinControlChangeLabel - > setStyleSheet ( " QLabel{color:red;} " ) ;
ui - > labelCoinControlChangeLabel - > setText ( tr ( " Warning: Invalid Bitcoin address " ) ) ;
}
else
{
QString associatedLabel = model - > getAddressTableModel ( ) - > labelForAddress ( text ) ;
if ( ! associatedLabel . isEmpty ( ) )
ui - > labelCoinControlChangeLabel - > setText ( associatedLabel ) ;
else
{
CPubKey pubkey ;
CKeyID keyid ;
CBitcoinAddress ( text . toStdString ( ) ) . GetKeyID ( keyid ) ;
if ( model - > getPubKey ( keyid , pubkey ) )
ui - > labelCoinControlChangeLabel - > setText ( tr ( " (no label) " ) ) ;
else
{
ui - > labelCoinControlChangeLabel - > setStyleSheet ( " QLabel{color:red;} " ) ;
ui - > labelCoinControlChangeLabel - > setText ( tr ( " Warning: Unknown change address " ) ) ;
}
}
}
}
}
// Coin Control: update labels
void SendCoinsDialog : : coinControlUpdateLabels ( )
{
if ( ! model | | ! model - > getOptionsModel ( ) | | ! model - > getOptionsModel ( ) - > getCoinControlFeatures ( ) )
return ;
// set pay amounts
CoinControlDialog : : payAmounts . clear ( ) ;
for ( int i = 0 ; i < ui - > entries - > count ( ) ; + + i )
{
SendCoinsEntry * entry = qobject_cast < SendCoinsEntry * > ( ui - > entries - > itemAt ( i ) - > widget ( ) ) ;
if ( entry )
CoinControlDialog : : payAmounts . append ( entry - > getValue ( ) . amount ) ;
}
if ( CoinControlDialog : : coinControl - > HasSelected ( ) )
{
// actual coin control calculation
CoinControlDialog : : updateLabels ( model , this ) ;
// show coin control stats
ui - > labelCoinControlAutomaticallySelected - > hide ( ) ;
ui - > widgetCoinControl - > show ( ) ;
}
else
{
// hide coin control stats
ui - > labelCoinControlAutomaticallySelected - > show ( ) ;
ui - > widgetCoinControl - > hide ( ) ;
ui - > labelCoinControlInsuffFunds - > hide ( ) ;
}
}