Bugfix: Replace "URL" with "URI" where we aren't actually working with URLs
[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 #include <QFileDialog>
19 #include <QDesktopServices>
20
21 QString GUIUtil::dateTimeStr(qint64 nTime)
22 {
23     return dateTimeStr(QDateTime::fromTime_t((qint32)nTime));
24 }
25
26 QString GUIUtil::dateTimeStr(const QDateTime &date)
27 {
28     return date.date().toString(Qt::SystemLocaleShortDate) + QString(" ") + date.toString("hh:mm");
29 }
30
31 QFont GUIUtil::bitcoinAddressFont()
32 {
33     QFont font("Monospace");
34     font.setStyleHint(QFont::TypeWriter);
35     return font;
36 }
37
38 void GUIUtil::setupAddressWidget(QLineEdit *widget, QWidget *parent)
39 {
40     widget->setMaxLength(BitcoinAddressValidator::MaxAddressLength);
41     widget->setValidator(new BitcoinAddressValidator(parent));
42     widget->setFont(bitcoinAddressFont());
43 }
44
45 void GUIUtil::setupAmountWidget(QLineEdit *widget, QWidget *parent)
46 {
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);
52 }
53
54 bool GUIUtil::parseBitcoinURI(const QUrl &uri, SendCoinsRecipient *out)
55 {
56     if(uri.scheme() != QString("bitcoin"))
57         return false;
58
59     SendCoinsRecipient rv;
60     rv.address = uri.path();
61     rv.amount = 0;
62     QList<QPair<QString, QString> > items = uri.queryItems();
63     for (QList<QPair<QString, QString> >::iterator i = items.begin(); i != items.end(); i++)
64     {
65         bool fShouldReturnFalse = false;
66         if (i->first.startsWith("req-"))
67         {
68             i->first.remove(0, 4);
69             fShouldReturnFalse = true;
70         }
71
72         if (i->first == "label")
73         {
74             rv.label = i->second;
75             fShouldReturnFalse = false;
76         }
77         else if (i->first == "amount")
78         {
79             if(!i->second.isEmpty())
80             {
81                 if(!BitcoinUnits::parse(BitcoinUnits::BTC, i->second, &rv.amount))
82                 {
83                     return false;
84                 }
85             }
86             fShouldReturnFalse = false;
87         }
88
89         if (fShouldReturnFalse)
90             return false;
91     }
92     if(out)
93     {
94         *out = rv;
95     }
96     return true;
97 }
98
99 bool GUIUtil::parseBitcoinURI(QString uri, SendCoinsRecipient *out)
100 {
101     // Convert bitcoin:// to bitcoin:
102     //
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://"))
106     {
107         uri.replace(0, 10, "bitcoin:");
108     }
109     QUrl uriInstance(uri);
110     return parseBitcoinURI(uriInstance, out);
111 }
112
113 QString GUIUtil::HtmlEscape(const QString& str, bool fMultiLine)
114 {
115     QString escaped = Qt::escape(str);
116     if(fMultiLine)
117     {
118         escaped = escaped.replace("\n", "<br>\n");
119     }
120     return escaped;
121 }
122
123 QString GUIUtil::HtmlEscape(const std::string& str, bool fMultiLine)
124 {
125     return HtmlEscape(QString::fromStdString(str), fMultiLine);
126 }
127
128 void GUIUtil::copyEntryData(QAbstractItemView *view, int column, int role)
129 {
130     if(!view || !view->selectionModel())
131         return;
132     QModelIndexList selection = view->selectionModel()->selectedRows(column);
133
134     if(!selection.isEmpty())
135     {
136         // Copy first item
137         QApplication::clipboard()->setText(selection.at(0).data(role).toString());
138     }
139 }
140
141 QString GUIUtil::getSaveFileName(QWidget *parent, const QString &caption,
142                                  const QString &dir,
143                                  const QString &filter,
144                                  QString *selectedSuffixOut)
145 {
146     QString selectedFilter;
147     QString myDir;
148     if(dir.isEmpty()) // Default to user documents location
149     {
150         myDir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
151     }
152     else
153     {
154         myDir = dir;
155     }
156     QString result = QFileDialog::getSaveFileName(parent, caption, myDir, filter, &selectedFilter);
157
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))
162     {
163         selectedSuffix = filter_re.cap(1);
164     }
165
166     /* Add suffix if needed */
167     QFileInfo info(result);
168     if(!result.isEmpty())
169     {
170         if(info.suffix().isEmpty() && !selectedSuffix.isEmpty())
171         {
172             /* No suffix specified, add selected suffix */
173             if(!result.endsWith("."))
174                 result.append(".");
175             result.append(selectedSuffix);
176         }
177     }
178
179     /* Return selected suffix if asked to */
180     if(selectedSuffixOut)
181     {
182         *selectedSuffixOut = selectedSuffix;
183     }
184     return result;
185 }
186