Restructure IPC URL handling (fixes #851)
[novacoin.git] / src / qt / guiutil.cpp
1 #include "guiutil.h"
2 #include "bitcoinaddressvalidator.h"
3 #include "walletmodel.h"
4 #include "bitcoinunits.h"
5
6 #include "headers.h"
7
8 #include <QString>
9 #include <QDateTime>
10 #include <QDoubleValidator>
11 #include <QFont>
12 #include <QLineEdit>
13 #include <QUrl>
14 #include <QTextDocument> // For Qt::escape
15 #include <QAbstractItemView>
16 #include <QApplication>
17 #include <QClipboard>
18
19 QString GUIUtil::dateTimeStr(qint64 nTime)
20 {
21     return dateTimeStr(QDateTime::fromTime_t((qint32)nTime));
22 }
23
24 QString GUIUtil::dateTimeStr(const QDateTime &date)
25 {
26     return date.date().toString(Qt::SystemLocaleShortDate) + QString(" ") + date.toString("hh:mm");
27 }
28
29 QFont GUIUtil::bitcoinAddressFont()
30 {
31     QFont font("Monospace");
32     font.setStyleHint(QFont::TypeWriter);
33     return font;
34 }
35
36 void GUIUtil::setupAddressWidget(QLineEdit *widget, QWidget *parent)
37 {
38     widget->setMaxLength(BitcoinAddressValidator::MaxAddressLength);
39     widget->setValidator(new BitcoinAddressValidator(parent));
40     widget->setFont(bitcoinAddressFont());
41 }
42
43 void GUIUtil::setupAmountWidget(QLineEdit *widget, QWidget *parent)
44 {
45     QDoubleValidator *amountValidator = new QDoubleValidator(parent);
46     amountValidator->setDecimals(8);
47     amountValidator->setBottom(0.0);
48     widget->setValidator(amountValidator);
49     widget->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
50 }
51
52 bool GUIUtil::parseBitcoinURL(const QUrl &url, SendCoinsRecipient *out)
53 {
54     if(url.scheme() != QString("bitcoin"))
55         return false;
56
57     SendCoinsRecipient rv;
58     rv.address = url.path();
59     rv.amount = 0;
60     QList<QPair<QString, QString> > items = url.queryItems();
61     for (QList<QPair<QString, QString> >::iterator i = items.begin(); i != items.end(); i++)
62     {
63         bool fShouldReturnFalse = false;
64         if (i->first.startsWith("req-"))
65         {
66             i->first.remove(0, 4);
67             fShouldReturnFalse = true;
68         }
69
70         if (i->first == "label")
71         {
72             rv.label = i->second;
73             fShouldReturnFalse = false;
74         }
75         else if (i->first == "amount")
76         {
77             if(!i->second.isEmpty())
78             {
79                 if(!BitcoinUnits::parse(BitcoinUnits::BTC, i->second, &rv.amount))
80                 {
81                     return false;
82                 }
83             }
84             fShouldReturnFalse = false;
85         }
86
87         if (fShouldReturnFalse)
88             return false;
89     }
90     if(out)
91     {
92         *out = rv;
93     }
94     return true;
95 }
96
97 bool GUIUtil::parseBitcoinURL(QString url, SendCoinsRecipient *out)
98 {
99     // Convert bitcoin:// to bitcoin:
100     //
101     //    Cannot handle this later, because bitcoin:// will cause Qt to see the part after // as host,
102     //    which will lowercase it (and thus invalidate the address).
103     if(url.startsWith("bitcoin://"))
104     {
105         url.replace(0, 10, "bitcoin:");
106     }
107     QUrl urlInstance(url);
108     return parseBitcoinURL(urlInstance, out);
109 }
110
111 QString GUIUtil::HtmlEscape(const QString& str, bool fMultiLine)
112 {
113     QString escaped = Qt::escape(str);
114     if(fMultiLine)
115     {
116         escaped = escaped.replace("\n", "<br>\n");
117     }
118     return escaped;
119 }
120
121 QString GUIUtil::HtmlEscape(const std::string& str, bool fMultiLine)
122 {
123     return HtmlEscape(QString::fromStdString(str), fMultiLine);
124 }
125
126 void GUIUtil::copyEntryData(QAbstractItemView *view, int column, int role)
127 {
128     if(!view || !view->selectionModel())
129         return;
130     QModelIndexList selection = view->selectionModel()->selectedRows(column);
131
132     if(!selection.isEmpty())
133     {
134         // Copy first item
135         QApplication::clipboard()->setText(selection.at(0).data(role).toString());
136     }
137 }