2 #include "bitcoinaddressvalidator.h"
3 #include "walletmodel.h"
4 #include "bitcoinunits.h"
10 #include <QDoubleValidator>
14 #include <QTextDocument> // For Qt::escape
15 #include <QAbstractItemView>
16 #include <QApplication>
18 #include <QFileDialog>
19 #include <QDesktopServices>
21 QString GUIUtil::dateTimeStr(qint64 nTime)
23 return dateTimeStr(QDateTime::fromTime_t((qint32)nTime));
26 QString GUIUtil::dateTimeStr(const QDateTime &date)
28 return date.date().toString(Qt::SystemLocaleShortDate) + QString(" ") + date.toString("hh:mm");
31 QFont GUIUtil::bitcoinAddressFont()
33 QFont font("Monospace");
34 font.setStyleHint(QFont::TypeWriter);
38 void GUIUtil::setupAddressWidget(QLineEdit *widget, QWidget *parent)
40 widget->setMaxLength(BitcoinAddressValidator::MaxAddressLength);
41 widget->setValidator(new BitcoinAddressValidator(parent));
42 widget->setFont(bitcoinAddressFont());
45 void GUIUtil::setupAmountWidget(QLineEdit *widget, QWidget *parent)
47 QDoubleValidator *amountValidator = new QDoubleValidator(parent);
48 amountValidator->setDecimals(8);
49 amountValidator->setBottom(0.0);
50 widget->setValidator(amountValidator);
51 widget->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
54 bool GUIUtil::parseBitcoinURI(const QUrl &uri, SendCoinsRecipient *out)
56 if(uri.scheme() != QString("bitcoin"))
59 SendCoinsRecipient rv;
60 rv.address = uri.path();
62 QList<QPair<QString, QString> > items = uri.queryItems();
63 for (QList<QPair<QString, QString> >::iterator i = items.begin(); i != items.end(); i++)
65 bool fShouldReturnFalse = false;
66 if (i->first.startsWith("req-"))
68 i->first.remove(0, 4);
69 fShouldReturnFalse = true;
72 if (i->first == "label")
75 fShouldReturnFalse = false;
77 else if (i->first == "amount")
79 if(!i->second.isEmpty())
81 if(!BitcoinUnits::parse(BitcoinUnits::BTC, i->second, &rv.amount))
86 fShouldReturnFalse = false;
89 if (fShouldReturnFalse)
99 bool GUIUtil::parseBitcoinURI(QString uri, SendCoinsRecipient *out)
101 // Convert bitcoin:// to bitcoin:
103 // Cannot handle this later, because bitcoin:// will cause Qt to see the part after // as host,
104 // which will lowercase it (and thus invalidate the address).
105 if(uri.startsWith("bitcoin://"))
107 uri.replace(0, 10, "bitcoin:");
109 QUrl uriInstance(uri);
110 return parseBitcoinURI(uriInstance, out);
113 QString GUIUtil::HtmlEscape(const QString& str, bool fMultiLine)
115 QString escaped = Qt::escape(str);
118 escaped = escaped.replace("\n", "<br>\n");
123 QString GUIUtil::HtmlEscape(const std::string& str, bool fMultiLine)
125 return HtmlEscape(QString::fromStdString(str), fMultiLine);
128 void GUIUtil::copyEntryData(QAbstractItemView *view, int column, int role)
130 if(!view || !view->selectionModel())
132 QModelIndexList selection = view->selectionModel()->selectedRows(column);
134 if(!selection.isEmpty())
137 QApplication::clipboard()->setText(selection.at(0).data(role).toString());
141 QString GUIUtil::getSaveFileName(QWidget *parent, const QString &caption,
143 const QString &filter,
144 QString *selectedSuffixOut)
146 QString selectedFilter;
148 if(dir.isEmpty()) // Default to user documents location
150 myDir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
156 QString result = QFileDialog::getSaveFileName(parent, caption, myDir, filter, &selectedFilter);
158 /* Extract first suffix from filter pattern "Description (*.foo)" or "Description (*.foo *.bar ...) */
159 QRegExp filter_re(".* \\(\\*\\.(.*)[ \\)]");
160 QString selectedSuffix;
161 if(filter_re.exactMatch(selectedFilter))
163 selectedSuffix = filter_re.cap(1);
166 /* Add suffix if needed */
167 QFileInfo info(result);
168 if(!result.isEmpty())
170 if(info.suffix().isEmpty() && !selectedSuffix.isEmpty())
172 /* No suffix specified, add selected suffix */
173 if(!result.endsWith("."))
175 result.append(selectedSuffix);
179 /* Return selected suffix if asked to */
180 if(selectedSuffixOut)
182 *selectedSuffixOut = selectedSuffix;