Browse Source

qt: move bitcoin URI formatting to guiutil

Follow the same pattern as the parseBitcoinURI function.
0.10
Wladimir J. van der Laan 11 years ago
parent
commit
786b066f03
  1. 28
      src/qt/guiutil.cpp
  2. 1
      src/qt/guiutil.h
  3. 84
      src/qt/receiverequestdialog.cpp

28
src/qt/guiutil.cpp

@ -153,6 +153,34 @@ bool parseBitcoinURI(QString uri, SendCoinsRecipient *out) @@ -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();

1
src/qt/guiutil.h

@ -35,6 +35,7 @@ namespace GUIUtil @@ -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);

84
src/qt/receiverequestdialog.cpp

@ -112,75 +112,45 @@ void ReceiveRequestDialog::setModel(OptionsModel *model) @@ -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();

Loading…
Cancel
Save