Add checkpoint at block 14189, v3 lock-in since supermajority and bnProofOfStakeLimit...
[novacoin.git] / src / qt / qrcodedialog.cpp
1 #include "qrcodedialog.h"
2 #include "ui_qrcodedialog.h"
3 #include "guiutil.h"
4
5 #include <QPixmap>
6 #include <QUrl>
7 #include <QDebug>
8
9 #include <qrencode.h>
10
11 #define EXPORT_IMAGE_SIZE 256
12
13 QRCodeDialog::QRCodeDialog(const QString &addr, const QString &label, bool enableReq, QWidget *parent) :
14     QDialog(parent), ui(new Ui::QRCodeDialog), address(addr)
15 {
16     ui->setupUi(this);
17     setWindowTitle(QString("%1").arg(address));
18     setAttribute(Qt::WA_DeleteOnClose);
19
20     ui->chkReqPayment->setVisible(enableReq);
21     ui->lnReqAmount->setVisible(enableReq);
22     ui->lblAmount->setVisible(enableReq);
23     ui->lblBTC->setVisible(enableReq);
24
25     ui->lnLabel->setText(label);
26
27     genCode();
28 }
29
30 QRCodeDialog::~QRCodeDialog()
31 {
32     delete ui;
33 }
34
35 void QRCodeDialog::genCode()
36 {
37     QString uri = getURI();
38
39     if (uri != "")
40     {
41         ui->lblQRCode->setText("");
42
43         QRcode *code = QRcode_encodeString(uri.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);
44         if (!code)
45         {
46             ui->lblQRCode->setText(tr("Error encoding URI into QR Code."));
47             return;
48         }
49         myImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32);
50         myImage.fill(0xffffff);
51         unsigned char *p = code->data;
52         for (int y = 0; y < code->width; y++)
53         {
54             for (int x = 0; x < code->width; x++)
55             {
56                 myImage.setPixel(x + 4, y + 4, ((*p & 1) ? 0x0 : 0xffffff));
57                 p++;
58             }
59         }
60         QRcode_free(code);
61         ui->lblQRCode->setPixmap(QPixmap::fromImage(myImage).scaled(300, 300));
62     }
63     else
64         ui->lblQRCode->setText(tr("Resulting URI too long, try to reduce the text for label / message."));
65 }
66
67 QString QRCodeDialog::getURI()
68 {
69     QString ret = QString("bitcoin:%1").arg(address);
70
71     int paramCount = 0;
72     if (ui->chkReqPayment->isChecked() && !ui->lnReqAmount->text().isEmpty())
73     {
74         bool ok = false;
75         ui->lnReqAmount->text().toDouble(&ok);
76         if (ok)
77         {
78             ret += QString("?amount=%1").arg(ui->lnReqAmount->text());
79             paramCount++;
80         }
81     }
82
83     if (!ui->lnLabel->text().isEmpty())
84     {
85         QString lbl(QUrl::toPercentEncoding(ui->lnLabel->text()));
86         ret += QString("%1label=%2").arg(paramCount == 0 ? "?" : "&").arg(lbl);
87         paramCount++;
88     }
89
90     if (!ui->lnMessage->text().isEmpty())
91     {
92         QString msg(QUrl::toPercentEncoding(ui->lnMessage->text()));
93         ret += QString("%1message=%2").arg(paramCount == 0 ? "?" : "&").arg(msg);
94         paramCount++;
95     }
96
97     // limit URI length to 255 chars, to prevent a DoS against the QR-Code dialog
98     if (ret.length() < 256)
99         return ret;
100     else
101         return QString("");
102 }
103
104 void QRCodeDialog::on_lnReqAmount_textChanged(const QString &arg1)
105 {
106     genCode();
107 }
108
109 void QRCodeDialog::on_lnLabel_textChanged(const QString &arg1)
110 {
111     genCode();
112 }
113
114 void QRCodeDialog::on_lnMessage_textChanged(const QString &arg1)
115 {
116     genCode();
117 }
118
119 void QRCodeDialog::on_btnSaveAs_clicked()
120 {
121     QString fn = GUIUtil::getSaveFileName(this, tr("Save Image..."), QString(), tr("PNG Images (*.png)"));
122     if (!fn.isEmpty())
123         myImage.scaled(EXPORT_IMAGE_SIZE, EXPORT_IMAGE_SIZE).save(fn);
124 }
125
126 void QRCodeDialog::on_chkReqPayment_toggled(bool)
127 {
128     genCode();
129 }