2 #include "bitcoinaddressvalidator.h"
3 #include "walletmodel.h"
4 #include "bitcoinunits.h"
10 #include <QDoubleValidator>
14 #include <QFileDialog>
15 #include <QDesktopServices>
17 QString GUIUtil::dateTimeStr(qint64 nTime)
19 return dateTimeStr(QDateTime::fromTime_t((qint32)nTime));
22 QString GUIUtil::dateTimeStr(const QDateTime &date)
24 return date.date().toString(Qt::SystemLocaleShortDate) + QString(" ") + date.toString("hh:mm");
27 QFont GUIUtil::bitcoinAddressFont()
29 QFont font("Monospace");
30 font.setStyleHint(QFont::TypeWriter);
34 void GUIUtil::setupAddressWidget(QLineEdit *widget, QWidget *parent)
36 widget->setMaxLength(BitcoinAddressValidator::MaxAddressLength);
37 widget->setValidator(new BitcoinAddressValidator(parent));
38 widget->setFont(bitcoinAddressFont());
41 void GUIUtil::setupAmountWidget(QLineEdit *widget, QWidget *parent)
43 QDoubleValidator *amountValidator = new QDoubleValidator(parent);
44 amountValidator->setDecimals(8);
45 amountValidator->setBottom(0.0);
46 widget->setValidator(amountValidator);
47 widget->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
50 bool GUIUtil::parseBitcoinURL(const QUrl *url, SendCoinsRecipient *out)
52 if(url->scheme() != QString("bitcoin"))
55 SendCoinsRecipient rv;
56 rv.address = url->path();
57 rv.label = url->queryItemValue("label");
59 QString amount = url->queryItemValue("amount");
64 else // Amount is non-empty
66 if(!BitcoinUnits::parse(BitcoinUnits::BTC, amount, &rv.amount))
78 bool GUIUtil::parseBitcoinURL(QString url, SendCoinsRecipient *out)
80 // Convert bitcoin:// to bitcoin:
82 // Cannot handle this later, because bitcoin:// will cause Qt to see the part after // as host,
83 // which will lowercase it (and thus invalidate the address).
84 if(url.startsWith("bitcoin://"))
86 url.replace(0, 10, "bitcoin:");
88 QUrl urlInstance(url);
89 return parseBitcoinURL(&urlInstance, out);
92 QString GUIUtil::getSaveFileName(QWidget *parent, const QString &caption,
94 const QString &filter,
95 QString *selectedSuffixOut)
97 QString selectedFilter;
99 if(dir.isEmpty()) // Default to user documents location
101 myDir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
107 QString result = QFileDialog::getSaveFileName(parent, caption, myDir, filter, &selectedFilter);
109 /* Extract first suffix from filter pattern "Description (*.foo)" or "Description (*.foo *.bar ...) */
110 QRegExp filter_re(".* \\(\\*\\.(.*)[ \\)]");
111 QString selectedSuffix;
112 if(filter_re.exactMatch(selectedFilter))
114 selectedSuffix = filter_re.cap(1);
117 /* Add suffix if needed */
118 QFileInfo info(result);
119 if(!result.isEmpty())
121 if(info.suffix().isEmpty() && !selectedSuffix.isEmpty())
123 /* No suffix specified, add selected suffix */
124 if(!result.endsWith("."))
126 result.append(selectedSuffix);
130 /* Return selected suffix if asked to */
131 if(selectedSuffixOut)
133 *selectedSuffixOut = selectedSuffix;