From bdd0c59ab0b412b3e8595bd95f635f975e22bb85 Mon Sep 17 00:00:00 2001 From: Philip Kaufmann Date: Sat, 24 Aug 2013 15:07:17 +0200 Subject: [PATCH] Bitcoin-Qt: fixes for using display unit from options - extend PaymentServer with setOptionsModel() and rework initNetManager() to make use of that - fix all other places in the code to use display unit from options and no hard-coded unit --- src/qt/bitcoin.cpp | 3 ++- src/qt/bitcoingui.cpp | 5 ++++- src/qt/paymentserver.cpp | 22 ++++++++++++++++------ src/qt/paymentserver.h | 18 ++++++++++++------ src/qt/sendcoinsdialog.cpp | 24 ++++++++++-------------- src/qt/test/paymentservertests.cpp | 3 ++- src/qt/transactiondesc.cpp | 26 +++++++++++++------------- src/qt/transactiondesc.h | 2 +- src/qt/transactiontablemodel.cpp | 14 +++++++------- 9 files changed, 67 insertions(+), 50 deletions(-) diff --git a/src/qt/bitcoin.cpp b/src/qt/bitcoin.cpp index 9c290fa71..f2386f3cf 100644 --- a/src/qt/bitcoin.cpp +++ b/src/qt/bitcoin.cpp @@ -300,7 +300,8 @@ int main(int argc, char *argv[]) optionsModel.Upgrade(); // Must be done after AppInit2 PaymentServer::LoadRootCAs(); - paymentServer->initNetManager(optionsModel); + paymentServer->setOptionsModel(&optionsModel); + paymentServer->initNetManager(); if (splashref) splash.finish(&window); diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index ad32c9ea6..bb9eb60e5 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -670,9 +670,12 @@ void BitcoinGUI::closeEvent(QCloseEvent *event) void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee) { + if (!clientModel || !clientModel->getOptionsModel()) + return; + QString strMessage = tr("This transaction is over the size limit. You can still send it for a fee of %1, " "which goes to the nodes that process your transaction and helps to support the network. " - "Do you want to pay the fee?").arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nFeeRequired)); + "Do you want to pay the fee?").arg(BitcoinUnits::formatWithUnit(clientModel->getOptionsModel()->getDisplayUnit(), nFeeRequired)); QMessageBox::StandardButton retval = QMessageBox::question( this, tr("Confirm transaction fee"), strMessage, QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes); diff --git a/src/qt/paymentserver.cpp b/src/qt/paymentserver.cpp index ff3c2a098..c7c6f6706 100644 --- a/src/qt/paymentserver.cpp +++ b/src/qt/paymentserver.cpp @@ -92,7 +92,7 @@ static void ReportInvalidCertificate(const QSslCertificate& cert) } // -// Load openSSL's list of root certificate authorities +// Load OpenSSL's list of root certificate authorities // void PaymentServer::LoadRootCAs(X509_STORE* _store) { @@ -147,7 +147,7 @@ void PaymentServer::LoadRootCAs(X509_STORE* _store) const unsigned char *data = (const unsigned char *)certData.data(); X509* x509 = d2i_X509(0, &data, certData.size()); - if (x509 && X509_STORE_add_cert( PaymentServer::certStore, x509)) + if (x509 && X509_STORE_add_cert(PaymentServer::certStore, x509)) { // Note: X509_STORE_free will free the X509* objects when // the PaymentServer is destroyed @@ -303,18 +303,20 @@ bool PaymentServer::eventFilter(QObject *, QEvent *event) return false; } -void PaymentServer::initNetManager(const OptionsModel& options) +void PaymentServer::initNetManager() { + if (!optionsModel) + return; if (netManager != NULL) delete netManager; // netManager is used to fetch paymentrequests given in bitcoin: URI's netManager = new QNetworkAccessManager(this); - // Use proxy settings from options: + // Use proxy settings from optionsModel: QString proxyIP; quint16 proxyPort; - if (options.getProxySettings(proxyIP, proxyPort)) + if (optionsModel->getProxySettings(proxyIP, proxyPort)) { QNetworkProxy proxy; proxy.setType(QNetworkProxy::Socks5Proxy); @@ -435,13 +437,16 @@ bool PaymentServer::processPaymentRequest(PaymentRequestPlus& request, QList& recipients) { + if (!optionsModel) + return false; + QList > sendingTos = request.getPayTo(); qint64 totalAmount = 0; foreach(const PAIRTYPE(CScript, qint64)& sendingTo, sendingTos) { CTxOut txOut(sendingTo.second, sendingTo.first); if (txOut.IsDust(CTransaction::nMinRelayTxFee)) { QString message = QObject::tr("Requested payment amount (%1) too small") - .arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, sendingTo.second)); + .arg(BitcoinUnits::formatWithUnit(optionsModel->getDisplayUnit(), sendingTo.second)); qDebug() << message; emit reportError(tr("Payment request error"), message, CClientUIInterface::MODAL); return false; @@ -614,3 +619,8 @@ PaymentServer::reportSslErrors(QNetworkReply* reply, const QList &err } emit reportError(tr("Network request error"), errString, CClientUIInterface::MODAL); } + +void PaymentServer::setOptionsModel(OptionsModel *optionsModel) +{ + this->optionsModel = optionsModel; +} diff --git a/src/qt/paymentserver.h b/src/qt/paymentserver.h index 7c6b2eabf..131ede518 100644 --- a/src/qt/paymentserver.h +++ b/src/qt/paymentserver.h @@ -17,7 +17,7 @@ // received at or during startup in a list. // // When startup is finished and the main window is -// show, a signal is sent to slot uiReady(), which +// shown, a signal is sent to slot uiReady(), which // emits a receivedURL() signal for any payment // requests that happened during startup. // @@ -70,13 +70,16 @@ public: // Return certificate store static X509_STORE* getCertStore() { return certStore; } - // Setup networking (options is used to get proxy settings) - void initNetManager(const OptionsModel& options); + // Setup networking + void initNetManager(); // Constructor registers this on the parent QApplication to // receive QEvent::FileOpen events bool eventFilter(QObject *object, QEvent *event); + // OptionsModel is used for getting proxy settings and display unit + void setOptionsModel(OptionsModel *optionsModel); + signals: // Fired when a valid payment request is received void receivedPaymentRequest(SendCoinsRecipient); @@ -106,12 +109,15 @@ private: void handleURIOrFile(const QString& s); void fetchRequest(const QUrl& url); - bool saveURIs; // true during startup + bool saveURIs; // true during startup QLocalServer* uriServer; - static X509_STORE* certStore; // Trusted root certificates + + static X509_STORE* certStore; // Trusted root certificates static void freeCertStore(); - QNetworkAccessManager* netManager; // Used to fetch payment requests + QNetworkAccessManager* netManager; // Used to fetch payment requests + + OptionsModel *optionsModel; }; #endif // PAYMENTSERVER_H diff --git a/src/qt/sendcoinsdialog.cpp b/src/qt/sendcoinsdialog.cpp index 9086f6614..e3fec6104 100644 --- a/src/qt/sendcoinsdialog.cpp +++ b/src/qt/sendcoinsdialog.cpp @@ -63,12 +63,12 @@ SendCoinsDialog::~SendCoinsDialog() void SendCoinsDialog::on_sendButton_clicked() { + if(!model || !model->getOptionsModel()) + return; + QList recipients; bool valid = true; - if(!model) - return; - for(int i = 0; i < ui->entries->count(); ++i) { SendCoinsEntry *entry = qobject_cast(ui->entries->itemAt(i)->widget()); @@ -94,7 +94,7 @@ void SendCoinsDialog::on_sendButton_clicked() QStringList formatted; foreach(const SendCoinsRecipient &rcp, recipients) { - QString amount = BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, rcp.amount); + QString amount = BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(), rcp.amount); if (rcp.authenticatedMerchant.isEmpty()) { QString address = rcp.address; @@ -158,7 +158,7 @@ void SendCoinsDialog::on_sendButton_clicked() case WalletModel::AmountWithFeeExceedsBalance: QMessageBox::warning(this, tr("Send Coins"), tr("The total exceeds your balance when the %1 transaction fee is included."). - arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, sendstatus.fee)), + arg(BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(), sendstatus.fee)), QMessageBox::Ok, QMessageBox::Ok); break; case WalletModel::DuplicateAddress: @@ -338,18 +338,14 @@ void SendCoinsDialog::setBalance(qint64 balance, qint64 unconfirmedBalance, qint { Q_UNUSED(unconfirmedBalance); Q_UNUSED(immatureBalance); - if(!model || !model->getOptionsModel()) - return; - int unit = model->getOptionsModel()->getDisplayUnit(); - ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance)); + if(model && model->getOptionsModel()) + { + ui->labelBalance->setText(BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(), balance)); + } } void SendCoinsDialog::updateDisplayUnit() { - if(model && model->getOptionsModel()) - { - // Update labelBalance with the current balance and the current unit - ui->labelBalance->setText(BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(), model->getBalance())); - } + setBalance(model->getBalance(), 0, 0); } diff --git a/src/qt/test/paymentservertests.cpp b/src/qt/test/paymentservertests.cpp index 2e26ab0c9..6c8ad62b2 100644 --- a/src/qt/test/paymentservertests.cpp +++ b/src/qt/test/paymentservertests.cpp @@ -58,7 +58,8 @@ void PaymentServerTests::paymentServerTests() X509_STORE* caStore = X509_STORE_new(); X509_STORE_add_cert(caStore, parse_b64der_cert(caCert_BASE64)); PaymentServer::LoadRootCAs(caStore); - server->initNetManager(optionsModel); + server->setOptionsModel(&optionsModel); + server->initNetManager(); server->uiReady(); // Now feed PaymentRequests to server, and observe signals it produces: diff --git a/src/qt/transactiondesc.cpp b/src/qt/transactiondesc.cpp index 25ff3623c..55875c2e4 100644 --- a/src/qt/transactiondesc.cpp +++ b/src/qt/transactiondesc.cpp @@ -32,7 +32,7 @@ QString TransactionDesc::FormatTxStatus(const CWalletTx& wtx) } } -QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx) +QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx, int unit) { QString strHTML; @@ -129,7 +129,7 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx) nUnmatured += wallet->GetCredit(txout); strHTML += "" + tr("Credit") + ": "; if (wtx.IsInMainChain()) - strHTML += BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nUnmatured)+ " (" + tr("matures in %n more block(s)", "", wtx.GetBlocksToMaturity()) + ")"; + strHTML += BitcoinUnits::formatWithUnit(unit, nUnmatured)+ " (" + tr("matures in %n more block(s)", "", wtx.GetBlocksToMaturity()) + ")"; else strHTML += "(" + tr("not accepted") + ")"; strHTML += "
"; @@ -139,7 +139,7 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx) // // Credit // - strHTML += "" + tr("Credit") + ": " + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nNet) + "
"; + strHTML += "" + tr("Credit") + ": " + BitcoinUnits::formatWithUnit(unit, nNet) + "
"; } else { @@ -175,7 +175,7 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx) } } - strHTML += "" + tr("Debit") + ": " + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, -txout.nValue) + "
"; + strHTML += "" + tr("Debit") + ": " + BitcoinUnits::formatWithUnit(unit, -txout.nValue) + "
"; } if (fAllToMe) @@ -183,13 +183,13 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx) // Payment to self int64 nChange = wtx.GetChange(); int64 nValue = nCredit - nChange; - strHTML += "" + tr("Debit") + ": " + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, -nValue) + "
"; - strHTML += "" + tr("Credit") + ": " + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nValue) + "
"; + strHTML += "" + tr("Debit") + ": " + BitcoinUnits::formatWithUnit(unit, -nValue) + "
"; + strHTML += "" + tr("Credit") + ": " + BitcoinUnits::formatWithUnit(unit, nValue) + "
"; } int64 nTxFee = nDebit - GetValueOut(wtx); if (nTxFee > 0) - strHTML += "" + tr("Transaction fee") + ": " + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, -nTxFee) + "
"; + strHTML += "" + tr("Transaction fee") + ": " + BitcoinUnits::formatWithUnit(unit, -nTxFee) + "
"; } else { @@ -198,14 +198,14 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx) // BOOST_FOREACH(const CTxIn& txin, wtx.vin) if (wallet->IsMine(txin)) - strHTML += "" + tr("Debit") + ": " + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, -wallet->GetDebit(txin)) + "
"; + strHTML += "" + tr("Debit") + ": " + BitcoinUnits::formatWithUnit(unit, -wallet->GetDebit(txin)) + "
"; BOOST_FOREACH(const CTxOut& txout, wtx.vout) if (wallet->IsMine(txout)) - strHTML += "" + tr("Credit") + ": " + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, wallet->GetCredit(txout)) + "
"; + strHTML += "" + tr("Credit") + ": " + BitcoinUnits::formatWithUnit(unit, wallet->GetCredit(txout)) + "
"; } } - strHTML += "" + tr("Net amount") + ": " + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nNet, true) + "
"; + strHTML += "" + tr("Net amount") + ": " + BitcoinUnits::formatWithUnit(unit, nNet, true) + "
"; // // Message @@ -243,10 +243,10 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx) strHTML += "

" + tr("Debug information") + "

"; BOOST_FOREACH(const CTxIn& txin, wtx.vin) if(wallet->IsMine(txin)) - strHTML += "" + tr("Debit") + ": " + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, -wallet->GetDebit(txin)) + "
"; + strHTML += "" + tr("Debit") + ": " + BitcoinUnits::formatWithUnit(unit, -wallet->GetDebit(txin)) + "
"; BOOST_FOREACH(const CTxOut& txout, wtx.vout) if(wallet->IsMine(txout)) - strHTML += "" + tr("Credit") + ": " + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, wallet->GetCredit(txout)) + "
"; + strHTML += "" + tr("Credit") + ": " + BitcoinUnits::formatWithUnit(unit, wallet->GetCredit(txout)) + "
"; strHTML += "
" + tr("Transaction") + ":
"; strHTML += GUIUtil::HtmlEscape(wtx.ToString(), true); @@ -274,7 +274,7 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx) strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[address].name) + " "; strHTML += QString::fromStdString(CBitcoinAddress(address).ToString()); } - strHTML = strHTML + " " + tr("Amount") + "=" + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, vout.nValue); + strHTML = strHTML + " " + tr("Amount") + "=" + BitcoinUnits::formatWithUnit(unit, vout.nValue); strHTML = strHTML + " IsMine=" + (wallet->IsMine(vout) ? tr("true") : tr("false")) + ""; } } diff --git a/src/qt/transactiondesc.h b/src/qt/transactiondesc.h index cb0dda5b5..a659281dd 100644 --- a/src/qt/transactiondesc.h +++ b/src/qt/transactiondesc.h @@ -14,7 +14,7 @@ class TransactionDesc: public QObject Q_OBJECT public: - static QString toHTML(CWallet *wallet, CWalletTx &wtx); + static QString toHTML(CWallet *wallet, CWalletTx &wtx, int unit); private: TransactionDesc() {} diff --git a/src/qt/transactiontablemodel.cpp b/src/qt/transactiontablemodel.cpp index baf1e1648..625e65729 100644 --- a/src/qt/transactiontablemodel.cpp +++ b/src/qt/transactiontablemodel.cpp @@ -48,11 +48,12 @@ struct TxLessThan class TransactionTablePriv { public: - TransactionTablePriv(CWallet *wallet, TransactionTableModel *parent): - wallet(wallet), - parent(parent) + TransactionTablePriv(CWallet *wallet, TransactionTableModel *parent) : + wallet(wallet), + parent(parent) { } + CWallet *wallet; TransactionTableModel *parent; @@ -200,19 +201,18 @@ public: } } - QString describe(TransactionRecord *rec) + QString describe(TransactionRecord *rec, int unit) { { LOCK(wallet->cs_wallet); std::map::iterator mi = wallet->mapWallet.find(rec->hash); if(mi != wallet->mapWallet.end()) { - return TransactionDesc::toHTML(wallet, mi->second); + return TransactionDesc::toHTML(wallet, mi->second, unit); } } return QString(""); } - }; TransactionTableModel::TransactionTableModel(CWallet* wallet, WalletModel *parent): @@ -561,7 +561,7 @@ QVariant TransactionTableModel::data(const QModelIndex &index, int role) const case DateRole: return QDateTime::fromTime_t(static_cast(rec->time)); case LongDescriptionRole: - return priv->describe(rec); + return priv->describe(rec, walletModel->getOptionsModel()->getDisplayUnit()); case AddressRole: return QString::fromStdString(rec->address); case LabelRole: