Bugfix: Replace "URL" with "URI" where we aren't actually working with URLs
[novacoin.git] / src / qt / sendcoinsdialog.cpp
index 38a0a65..592ae6f 100644 (file)
@@ -1,15 +1,17 @@
 #include "sendcoinsdialog.h"
 #include "ui_sendcoinsdialog.h"
 #include "walletmodel.h"
-#include "guiutil.h"
+#include "bitcoinunits.h"
 #include "addressbookpage.h"
 #include "optionsmodel.h"
 #include "sendcoinsentry.h"
-
+#include "guiutil.h"
+#include "askpassphrasedialog.h"
 
 #include <QMessageBox>
 #include <QLocale>
-#include <QDebug>
+#include <QTextDocument>
+#include <QScrollBar>
 
 SendCoinsDialog::SendCoinsDialog(QWidget *parent) :
     QDialog(parent),
@@ -18,9 +20,18 @@ SendCoinsDialog::SendCoinsDialog(QWidget *parent) :
 {
     ui->setupUi(this);
 
+#ifdef Q_WS_MAC // Icons on push buttons are very uncommon on Mac
+    ui->addButton->setIcon(QIcon());
+    ui->clearButton->setIcon(QIcon());
+    ui->sendButton->setIcon(QIcon());
+#endif
+
     addEntry();
 
     connect(ui->addButton, SIGNAL(clicked()), this, SLOT(addEntry()));
+    connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
+
+    fNewRecipientAllowed = true;
 }
 
 void SendCoinsDialog::setModel(WalletModel *model)
@@ -35,6 +46,11 @@ void SendCoinsDialog::setModel(WalletModel *model)
             entry->setModel(model);
         }
     }
+    if(model)
+    {
+        setBalance(model->getBalance(), model->getUnconfirmedBalance());
+        connect(model, SIGNAL(balanceChanged(qint64, qint64)), this, SLOT(setBalance(qint64, qint64)));
+    }
 }
 
 SendCoinsDialog::~SendCoinsDialog()
@@ -46,6 +62,10 @@ void SendCoinsDialog::on_sendButton_clicked()
 {
     QList<SendCoinsRecipient> recipients;
     bool valid = true;
+
+    if(!model)
+        return;
+
     for(int i = 0; i < ui->entries->count(); ++i)
     {
         SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
@@ -71,9 +91,11 @@ void SendCoinsDialog::on_sendButton_clicked()
     QStringList formatted;
     foreach(const SendCoinsRecipient &rcp, recipients)
     {
-        formatted.append(tr("%1 BTC to %2 (%3)").arg(GUIUtil::formatMoney(rcp.amount), rcp.label, rcp.address));
+        formatted.append(tr("<b>%1</b> to %2 (%3)").arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, rcp.amount), Qt::escape(rcp.label), rcp.address));
     }
 
+    fNewRecipientAllowed = false;
+
     QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),
                           tr("Are you sure you want to send %1?").arg(formatted.join(tr(" and "))),
           QMessageBox::Yes|QMessageBox::Cancel,
@@ -81,6 +103,15 @@ void SendCoinsDialog::on_sendButton_clicked()
 
     if(retval != QMessageBox::Yes)
     {
+        fNewRecipientAllowed = true;
+        return;
+    }
+
+    WalletModel::UnlockContext ctx(model->requestUnlock());
+    if(!ctx.isValid())
+    {
+        // Unlock wallet was cancelled
+        fNewRecipientAllowed = true;
         return;
     }
 
@@ -105,7 +136,7 @@ void SendCoinsDialog::on_sendButton_clicked()
     case WalletModel::AmountWithFeeExceedsBalance:
         QMessageBox::warning(this, tr("Send Coins"),
             tr("Total exceeds your balance when the %1 transaction fee is included").
-            arg(GUIUtil::formatMoney(sendstatus.fee)),
+            arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, sendstatus.fee)),
             QMessageBox::Ok, QMessageBox::Ok);
         break;
     case WalletModel::DuplicateAddress:
@@ -118,7 +149,6 @@ void SendCoinsDialog::on_sendButton_clicked()
             tr("Error: Transaction creation failed  "),
             QMessageBox::Ok, QMessageBox::Ok);
         break;
-        break;
     case WalletModel::TransactionCommitFailed:
         QMessageBox::warning(this, tr("Send Coins"),
             tr("Error: 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."),
@@ -128,22 +158,17 @@ void SendCoinsDialog::on_sendButton_clicked()
         accept();
         break;
     }
+    fNewRecipientAllowed = true;
 }
 
 void SendCoinsDialog::clear()
 {
     // Remove entries until only one left
-    while(ui->entries->count() > 1)
+    while(ui->entries->count())
     {
         delete ui->entries->takeAt(0)->widget();
     }
-
-    // Reset the entry that is left to empty
-    SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(0)->widget());
-    if(entry)
-    {
-        entry->clear();
-    }
+    addEntry();
 
     updateRemoveEnabled();
 
@@ -160,7 +185,7 @@ void SendCoinsDialog::accept()
     clear();
 }
 
-void SendCoinsDialog::addEntry()
+SendCoinsEntry *SendCoinsDialog::addEntry()
 {
     SendCoinsEntry *entry = new SendCoinsEntry(this);
     entry->setModel(model);
@@ -171,6 +196,13 @@ void SendCoinsDialog::addEntry()
 
     // Focus the field, so that entry can start immediately
     entry->clear();
+    entry->setFocus();
+    ui->scrollAreaWidgetContents->resize(ui->scrollAreaWidgetContents->sizeHint());
+    QCoreApplication::instance()->processEvents();
+    QScrollBar* bar = ui->scrollArea->verticalScrollBar();
+    if (bar)
+        bar->setSliderPosition(bar->maximum());
+    return entry;
 }
 
 void SendCoinsDialog::updateRemoveEnabled()
@@ -208,3 +240,47 @@ QWidget *SendCoinsDialog::setupTabChain(QWidget *prev)
     QWidget::setTabOrder(ui->addButton, ui->sendButton);
     return ui->sendButton;
 }
+
+void SendCoinsDialog::pasteEntry(const SendCoinsRecipient &rv)
+{
+    if (!fNewRecipientAllowed)
+        return;
+
+    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);
+}
+
+
+void SendCoinsDialog::handleURI(const QString &uri)
+{
+    SendCoinsRecipient rv;
+    if(!GUIUtil::parseBitcoinURI(uri, &rv))
+    {
+        return;
+    }
+    pasteEntry(rv);
+}
+
+void SendCoinsDialog::setBalance(qint64 balance, qint64 unconfirmedBalance)
+{
+    Q_UNUSED(unconfirmedBalance);
+    if(!model || !model->getOptionsModel())
+        return;
+
+    int unit = model->getOptionsModel()->getDisplayUnit();
+    ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance));
+}