diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2013-10-18 14:03:17 +0200 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@gmail.com> | 2013-10-23 09:15:25 +0200 |
commit | 786b066f037bf2d7ba7fe0df1a4e15b22ab65599 (patch) | |
tree | 5477946fc4ebfedd3c3cec67e03d0a0f8caabf7e /src/qt | |
parent | 8a7f37c7972504d25225c96f225503c329aaf6fa (diff) | |
download | bitcoin-786b066f037bf2d7ba7fe0df1a4e15b22ab65599.tar.xz |
qt: move bitcoin URI formatting to guiutil
Follow the same pattern as the parseBitcoinURI function.
Diffstat (limited to 'src/qt')
-rw-r--r-- | src/qt/guiutil.cpp | 28 | ||||
-rw-r--r-- | src/qt/guiutil.h | 1 | ||||
-rw-r--r-- | src/qt/receiverequestdialog.cpp | 84 |
3 files changed, 56 insertions, 57 deletions
diff --git a/src/qt/guiutil.cpp b/src/qt/guiutil.cpp index e7334e67cb..9bb2fa7c99 100644 --- a/src/qt/guiutil.cpp +++ b/src/qt/guiutil.cpp @@ -153,6 +153,34 @@ bool parseBitcoinURI(QString uri, SendCoinsRecipient *out) return parseBitcoinURI(uriInstance, out); } +QString formatBitcoinURI(const SendCoinsRecipient &info) +{ + QString ret = QString("bitcoin:%1").arg(info.address); + int paramCount = 0; + + if (info.amount) + { + ret += QString("?amount=%1").arg(BitcoinUnits::format(BitcoinUnits::BTC, info.amount)); + paramCount++; + } + + if (!info.label.isEmpty()) + { + QString lbl(QUrl::toPercentEncoding(info.label)); + ret += QString("%1label=%2").arg(paramCount == 0 ? "?" : "&").arg(lbl); + paramCount++; + } + + if (!info.message.isEmpty()) + { + QString msg(QUrl::toPercentEncoding(info.message));; + ret += QString("%1message=%2").arg(paramCount == 0 ? "?" : "&").arg(msg); + paramCount++; + } + + return ret; +} + bool isDust(const QString& address, qint64 amount) { CTxDestination dest = CBitcoinAddress(address.toStdString()).Get(); diff --git a/src/qt/guiutil.h b/src/qt/guiutil.h index 8472700f48..956f550989 100644 --- a/src/qt/guiutil.h +++ b/src/qt/guiutil.h @@ -35,6 +35,7 @@ namespace GUIUtil // See Bitcoin URI definition discussion here: https://bitcointalk.org/index.php?topic=33490.0 bool parseBitcoinURI(const QUrl &uri, SendCoinsRecipient *out); bool parseBitcoinURI(QString uri, SendCoinsRecipient *out); + QString formatBitcoinURI(const SendCoinsRecipient &info); // Returns true if given address+amount meets "dust" definition bool isDust(const QString& address, qint64 amount); diff --git a/src/qt/receiverequestdialog.cpp b/src/qt/receiverequestdialog.cpp index 861458f8d8..efbc6ebd70 100644 --- a/src/qt/receiverequestdialog.cpp +++ b/src/qt/receiverequestdialog.cpp @@ -112,75 +112,45 @@ void ReceiveRequestDialog::setModel(OptionsModel *model) void ReceiveRequestDialog::genCode() { - QString uri = getURI(); + QString uri = GUIUtil::formatBitcoinURI(info); ui->btnSaveAs->setEnabled(false); ui->outUri->setPlainText(uri); + #ifdef USE_QRCODE - if (uri != "") + ui->lblQRCode->setText(""); + if(!uri.isEmpty()) { - ui->lblQRCode->setText(""); - - QRcode *code = QRcode_encodeString(uri.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1); - if (!code) - { - ui->lblQRCode->setText(tr("Error encoding URI into QR Code.")); - return; - } - QImage myImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32); - myImage.fill(0xffffff); - unsigned char *p = code->data; - for (int y = 0; y < code->width; y++) + // limit URI length + if (uri.length() > MAX_URI_LENGTH) { - for (int x = 0; x < code->width; x++) + ui->lblQRCode->setText(tr("Resulting URI too long, try to reduce the text for label / message.")); + } else { + QRcode *code = QRcode_encodeString(uri.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1); + if (!code) { - myImage.setPixel(x + 4, y + 4, ((*p & 1) ? 0x0 : 0xffffff)); - p++; + ui->lblQRCode->setText(tr("Error encoding URI into QR Code.")); + return; } - } - QRcode_free(code); + QImage myImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32); + myImage.fill(0xffffff); + unsigned char *p = code->data; + for (int y = 0; y < code->width; y++) + { + for (int x = 0; x < code->width; x++) + { + myImage.setPixel(x + 4, y + 4, ((*p & 1) ? 0x0 : 0xffffff)); + p++; + } + } + QRcode_free(code); - ui->lblQRCode->setPixmap(QPixmap::fromImage(myImage).scaled(300, 300)); - ui->btnSaveAs->setEnabled(true); + ui->lblQRCode->setPixmap(QPixmap::fromImage(myImage).scaled(300, 300)); + ui->btnSaveAs->setEnabled(true); + } } #endif } -QString ReceiveRequestDialog::getURI() -{ - QString ret = QString("bitcoin:%1").arg(info.address); - int paramCount = 0; - - if (ui->lnReqAmount->validate()) - { - // even if we allow a non BTC unit input in lnReqAmount, we generate the URI with BTC as unit (as defined in BIP21) - ret += QString("?amount=%1").arg(BitcoinUnits::format(BitcoinUnits::BTC, ui->lnReqAmount->value())); - paramCount++; - } - - if (!ui->lnLabel->text().isEmpty()) - { - QString lbl(QUrl::toPercentEncoding(ui->lnLabel->text())); - ret += QString("%1label=%2").arg(paramCount == 0 ? "?" : "&").arg(lbl); - paramCount++; - } - - if (!ui->lnMessage->text().isEmpty()) - { - QString msg(QUrl::toPercentEncoding(ui->lnMessage->text())); - ret += QString("%1message=%2").arg(paramCount == 0 ? "?" : "&").arg(msg); - paramCount++; - } - - // limit URI length - if (ret.length() > MAX_URI_LENGTH) - { - ui->lblQRCode->setText(tr("Resulting URI too long, try to reduce the text for label / message.")); - return QString(""); - } - - return ret; -} - void ReceiveRequestDialog::on_lnReqAmount_textChanged() { genCode(); |